script.c 615 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853
  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. static struct linkdb_node *sleep_db; // int oid -> struct script_state *
  289. #ifdef BETA_THREAD_TEST
  290. /**
  291. * MySQL Query Slave
  292. **/
  293. static SPIN_LOCK queryThreadLock;
  294. static rAthread queryThread = NULL;
  295. static ramutex queryThreadMutex = NULL;
  296. static racond queryThreadCond = NULL;
  297. static volatile int32 queryThreadTerminate = 0;
  298. struct queryThreadEntry {
  299. bool ok;
  300. bool type; /* main db or log db? */
  301. struct script_state *st;
  302. };
  303. /* Ladies and Gentleman the Manager! */
  304. struct {
  305. struct queryThreadEntry **entry;/* array of structs */
  306. int count;
  307. int timer;/* used to receive processed entries */
  308. } queryThreadData;
  309. #endif
  310. /*==========================================
  311. * (Only those needed) local declaration prototype
  312. *------------------------------------------*/
  313. const char* parse_subexpr(const char* p,int limit);
  314. int run_func(struct script_state *st);
  315. unsigned short script_instancegetid(struct script_state *st);
  316. enum {
  317. MF_NOMEMO, //0
  318. MF_NOTELEPORT,
  319. MF_NOSAVE,
  320. MF_NOBRANCH,
  321. MF_NOPENALTY,
  322. MF_NOZENYPENALTY,
  323. MF_PVP,
  324. MF_PVP_NOPARTY,
  325. MF_PVP_NOGUILD,
  326. MF_GVG,
  327. MF_GVG_NOPARTY, //10
  328. MF_NOTRADE,
  329. MF_NOSKILL,
  330. MF_NOWARP,
  331. MF_PARTYLOCK,
  332. MF_NOICEWALL,
  333. MF_SNOW,
  334. MF_FOG,
  335. MF_SAKURA,
  336. MF_LEAVES,
  337. /**
  338. * No longer available, keeping here just in case it's back someday. [Ind]
  339. **/
  340. //MF_RAIN, //20
  341. // 21 free
  342. MF_NOGO = 22,
  343. MF_CLOUDS,
  344. MF_CLOUDS2,
  345. MF_FIREWORKS,
  346. MF_GVG_CASTLE,
  347. MF_GVG_DUNGEON,
  348. MF_NIGHTENABLED,
  349. MF_NOBASEEXP,
  350. MF_NOJOBEXP, //30
  351. MF_NOMOBLOOT,
  352. MF_NOMVPLOOT,
  353. MF_NORETURN,
  354. MF_NOWARPTO,
  355. MF_NIGHTMAREDROP,
  356. MF_RESTRICTED,
  357. MF_NOCOMMAND,
  358. MF_NODROP,
  359. MF_JEXP,
  360. MF_BEXP, //40
  361. MF_NOVENDING,
  362. MF_LOADEVENT,
  363. MF_NOCHAT,
  364. MF_NOEXPPENALTY,
  365. MF_GUILDLOCK,
  366. MF_TOWN,
  367. MF_AUTOTRADE,
  368. MF_ALLOWKS,
  369. MF_MONSTER_NOTELEPORT,
  370. MF_PVP_NOCALCRANK, //50
  371. MF_BATTLEGROUND,
  372. MF_RESET,
  373. MF_CHANNELAUTOJOIN,
  374. MF_NOUSECART,
  375. MF_NOITEMCONSUMPTION,
  376. MF_SUMSTARTMIRACLE,
  377. MF_NOMINEEFFECT,
  378. MF_NOLOCKON,
  379. MF_NOTOMB,
  380. MF_SKILL_DAMAGE, //60
  381. MF_NOCOSTUME,
  382. MF_GVG_TE_CASTLE,
  383. MF_GVG_TE,
  384. MF_HIDEMOBHPBAR,
  385. };
  386. const char* script_op2name(int op)
  387. {
  388. #define RETURN_OP_NAME(type) case type: return #type
  389. switch( op )
  390. {
  391. RETURN_OP_NAME(C_NOP);
  392. RETURN_OP_NAME(C_POS);
  393. RETURN_OP_NAME(C_INT);
  394. RETURN_OP_NAME(C_PARAM);
  395. RETURN_OP_NAME(C_FUNC);
  396. RETURN_OP_NAME(C_STR);
  397. RETURN_OP_NAME(C_CONSTSTR);
  398. RETURN_OP_NAME(C_ARG);
  399. RETURN_OP_NAME(C_NAME);
  400. RETURN_OP_NAME(C_EOL);
  401. RETURN_OP_NAME(C_RETINFO);
  402. RETURN_OP_NAME(C_USERFUNC);
  403. RETURN_OP_NAME(C_USERFUNC_POS);
  404. RETURN_OP_NAME(C_REF);
  405. // operators
  406. RETURN_OP_NAME(C_OP3);
  407. RETURN_OP_NAME(C_LOR);
  408. RETURN_OP_NAME(C_LAND);
  409. RETURN_OP_NAME(C_LE);
  410. RETURN_OP_NAME(C_LT);
  411. RETURN_OP_NAME(C_GE);
  412. RETURN_OP_NAME(C_GT);
  413. RETURN_OP_NAME(C_EQ);
  414. RETURN_OP_NAME(C_NE);
  415. RETURN_OP_NAME(C_XOR);
  416. RETURN_OP_NAME(C_OR);
  417. RETURN_OP_NAME(C_AND);
  418. RETURN_OP_NAME(C_ADD);
  419. RETURN_OP_NAME(C_SUB);
  420. RETURN_OP_NAME(C_MUL);
  421. RETURN_OP_NAME(C_DIV);
  422. RETURN_OP_NAME(C_MOD);
  423. RETURN_OP_NAME(C_NEG);
  424. RETURN_OP_NAME(C_LNOT);
  425. RETURN_OP_NAME(C_NOT);
  426. RETURN_OP_NAME(C_R_SHIFT);
  427. RETURN_OP_NAME(C_L_SHIFT);
  428. RETURN_OP_NAME(C_ADD_POST);
  429. RETURN_OP_NAME(C_SUB_POST);
  430. RETURN_OP_NAME(C_ADD_PRE);
  431. RETURN_OP_NAME(C_SUB_PRE);
  432. default:
  433. ShowDebug("script_op2name: unexpected op=%d\n", op);
  434. return "???";
  435. }
  436. #undef RETURN_OP_NAME
  437. }
  438. #ifdef DEBUG_DUMP_STACK
  439. static void script_dump_stack(struct script_state* st)
  440. {
  441. int i;
  442. ShowMessage("\tstart = %d\n", st->start);
  443. ShowMessage("\tend = %d\n", st->end);
  444. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  445. ShowMessage("\tsp = %d\n", st->stack->sp);
  446. for( i = 0; i < st->stack->sp; ++i )
  447. {
  448. struct script_data* data = &st->stack->stack_data[i];
  449. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  450. switch( data->type )
  451. {
  452. case C_INT:
  453. case C_POS:
  454. ShowMessage(" %d\n", data->u.num);
  455. break;
  456. case C_STR:
  457. case C_CONSTSTR:
  458. ShowMessage(" \"%s\"\n", data->u.str);
  459. break;
  460. case C_NAME:
  461. 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));
  462. break;
  463. case C_RETINFO:
  464. {
  465. struct script_retinfo* ri = data->u.ri;
  466. 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);
  467. }
  468. break;
  469. default:
  470. ShowMessage("\n");
  471. break;
  472. }
  473. }
  474. }
  475. #endif
  476. /// Reports on the console the src of a script error.
  477. static void script_reportsrc(struct script_state *st)
  478. {
  479. struct block_list* bl;
  480. if( st->oid == 0 )
  481. return; //Can't report source.
  482. bl = map_id2bl(st->oid);
  483. if( bl == NULL )
  484. return;
  485. switch( bl->type ) {
  486. case BL_NPC:
  487. if( bl->m >= 0 )
  488. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  489. else
  490. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  491. break;
  492. default:
  493. if( bl->m >= 0 )
  494. 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);
  495. else
  496. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  497. break;
  498. }
  499. }
  500. /// Reports on the console information about the script data.
  501. static void script_reportdata(struct script_data* data)
  502. {
  503. if( data == NULL )
  504. return;
  505. switch( data->type ) {
  506. case C_NOP:// no value
  507. ShowDebug("Data: nothing (nil)\n");
  508. break;
  509. case C_INT:// number
  510. ShowDebug("Data: number value=%"PRId64"\n", data->u.num);
  511. break;
  512. case C_STR:
  513. case C_CONSTSTR:// string
  514. if( data->u.str ) {
  515. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  516. } else {
  517. ShowDebug("Data: string value=NULL\n");
  518. }
  519. break;
  520. case C_NAME:// reference
  521. if( reference_tovariable(data) ) {// variable
  522. const char* name = reference_getname(data);
  523. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  524. } else if( reference_toconstant(data) ) {// constant
  525. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  526. } else if( reference_toparam(data) ) {// param
  527. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  528. } else {// ???
  529. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  530. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  531. }
  532. break;
  533. case C_POS:// label
  534. ShowDebug("Data: label pos=%"PRId64"\n", data->u.num);
  535. break;
  536. default:
  537. ShowDebug("Data: %s\n", script_op2name(data->type));
  538. break;
  539. }
  540. }
  541. /// Reports on the console information about the current built-in function.
  542. static void script_reportfunc(struct script_state* st)
  543. {
  544. int params, id;
  545. struct script_data* data;
  546. if( !script_hasdata(st,0) )
  547. {// no stack
  548. return;
  549. }
  550. data = script_getdata(st,0);
  551. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  552. {// script currently not executing a built-in function or corrupt stack
  553. return;
  554. }
  555. id = reference_getid(data);
  556. params = script_lastdata(st)-1;
  557. if( params > 0 )
  558. {
  559. int i;
  560. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  561. for( i = 2; i <= script_lastdata(st); i++ )
  562. {
  563. script_reportdata(script_getdata(st,i));
  564. }
  565. }
  566. else
  567. {
  568. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  569. }
  570. }
  571. /*==========================================
  572. * Output error message
  573. *------------------------------------------*/
  574. static void disp_error_message2(const char *mes,const char *pos,int report)
  575. {
  576. error_msg = aStrdup(mes);
  577. error_pos = pos;
  578. error_report = report;
  579. longjmp( error_jump, 1 );
  580. }
  581. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  582. static void disp_warning_message(const char *mes, const char *pos) {
  583. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  584. }
  585. /// Checks event parameter validity
  586. static void check_event(struct script_state *st, const char *evt)
  587. {
  588. if( evt && evt[0] && !stristr(evt, "::On") )
  589. {
  590. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  591. script_reportsrc(st);
  592. }
  593. }
  594. /*==========================================
  595. * Hashes the input string
  596. *------------------------------------------*/
  597. static unsigned int calc_hash(const char* p)
  598. {
  599. unsigned int h;
  600. #if defined(SCRIPT_HASH_DJB2)
  601. h = 5381;
  602. while( *p ) // hash*33 + c
  603. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  604. #elif defined(SCRIPT_HASH_SDBM)
  605. h = 0;
  606. while( *p ) // hash*65599 + c
  607. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  608. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  609. h = 0;
  610. while( *p ){
  611. unsigned int g;
  612. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  613. g = h & 0xF0000000;
  614. if( g )
  615. {
  616. h ^= g >> 24;
  617. h &= ~g;
  618. }
  619. }
  620. #else // athena hash
  621. h = 0;
  622. while( *p )
  623. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  624. #endif
  625. return h % SCRIPT_HASH_SIZE;
  626. }
  627. /*==========================================
  628. * str_data manipulation functions
  629. *------------------------------------------*/
  630. /// Looks up string using the provided id.
  631. const char* get_str(int id)
  632. {
  633. Assert( id >= LABEL_START && id < str_size );
  634. return str_buf+str_data[id].str;
  635. }
  636. /// Returns the uid of the string, or -1.
  637. static int search_str(const char* p)
  638. {
  639. int i;
  640. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  641. if( strcasecmp(get_str(i),p) == 0 )
  642. return i;
  643. return -1;
  644. }
  645. /// Stores a copy of the string and returns its id.
  646. /// If an identical string is already present, returns its id instead.
  647. int add_str(const char* p)
  648. {
  649. int h;
  650. int len;
  651. h = calc_hash(p);
  652. if( str_hash[h] == 0 )
  653. {// empty bucket, add new node here
  654. str_hash[h] = str_num;
  655. }
  656. else
  657. {// scan for end of list, or occurence of identical string
  658. int i;
  659. for( i = str_hash[h]; ; i = str_data[i].next )
  660. {
  661. if( strcasecmp(get_str(i),p) == 0 )
  662. return i; // string already in list
  663. if( str_data[i].next == 0 )
  664. break; // reached the end
  665. }
  666. // append node to end of list
  667. str_data[i].next = str_num;
  668. }
  669. // grow list if neccessary
  670. if( str_num >= str_data_size )
  671. {
  672. str_data_size += 128;
  673. RECREATE(str_data,struct str_data_struct,str_data_size);
  674. memset(str_data + (str_data_size - 128), '\0', 128);
  675. }
  676. len=(int)strlen(p);
  677. // grow string buffer if neccessary
  678. while( str_pos+len+1 >= str_size )
  679. {
  680. str_size += 256;
  681. RECREATE(str_buf,char,str_size);
  682. memset(str_buf + (str_size - 256), '\0', 256);
  683. }
  684. safestrncpy(str_buf+str_pos, p, len+1);
  685. str_data[str_num].type = C_NOP;
  686. str_data[str_num].str = str_pos;
  687. str_data[str_num].next = 0;
  688. str_data[str_num].func = NULL;
  689. str_data[str_num].backpatch = -1;
  690. str_data[str_num].label = -1;
  691. str_pos += len+1;
  692. return str_num++;
  693. }
  694. /// Appends 1 byte to the script buffer.
  695. static void add_scriptb(int a)
  696. {
  697. if( script_pos+1 >= script_size )
  698. {
  699. script_size += SCRIPT_BLOCK_SIZE;
  700. RECREATE(script_buf,unsigned char,script_size);
  701. }
  702. script_buf[script_pos++] = (uint8)(a);
  703. }
  704. /// Appends a c_op value to the script buffer.
  705. /// The value is variable-length encoded into 8-bit blocks.
  706. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  707. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  708. static void add_scriptc(int a)
  709. {
  710. while( a >= 0x40 )
  711. {
  712. add_scriptb((a&0x3f)|0x40);
  713. a = (a - 0x40) >> 6;
  714. }
  715. add_scriptb(a);
  716. }
  717. /// Appends an integer value to the script buffer.
  718. /// The value is variable-length encoded into 8-bit blocks.
  719. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  720. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  721. static void add_scripti(int a)
  722. {
  723. while( a >= 0x40 )
  724. {
  725. add_scriptb((a&0x3f)|0xc0);
  726. a = (a - 0x40) >> 6;
  727. }
  728. add_scriptb(a|0x80);
  729. }
  730. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  731. ///
  732. /// @param l The id of the str_data entry
  733. // Maximum up to 16M
  734. static void add_scriptl(int l)
  735. {
  736. int backpatch = str_data[l].backpatch;
  737. switch(str_data[l].type){
  738. case C_POS:
  739. case C_USERFUNC_POS:
  740. add_scriptc(C_POS);
  741. add_scriptb(str_data[l].label);
  742. add_scriptb(str_data[l].label>>8);
  743. add_scriptb(str_data[l].label>>16);
  744. break;
  745. case C_NOP:
  746. case C_USERFUNC:
  747. // Embedded data backpatch there is a possibility of label
  748. add_scriptc(C_NAME);
  749. str_data[l].backpatch = script_pos;
  750. add_scriptb(backpatch);
  751. add_scriptb(backpatch>>8);
  752. add_scriptb(backpatch>>16);
  753. break;
  754. case C_INT:
  755. add_scripti(abs(str_data[l].val));
  756. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  757. add_scriptc(C_NEG);
  758. break;
  759. default: // assume C_NAME
  760. add_scriptc(C_NAME);
  761. add_scriptb(l);
  762. add_scriptb(l>>8);
  763. add_scriptb(l>>16);
  764. break;
  765. }
  766. }
  767. /*==========================================
  768. * Resolve the label
  769. *------------------------------------------*/
  770. void set_label(int l,int pos, const char* script_pos_cur)
  771. {
  772. int i;
  773. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  774. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  775. disp_error_message("set_label: invalid label name",script_pos_cur);
  776. return;
  777. }
  778. if(str_data[l].label!=-1){
  779. disp_error_message("set_label: dup label ",script_pos_cur);
  780. return;
  781. }
  782. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  783. str_data[l].label=pos;
  784. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  785. int next=GETVALUE(script_buf,i);
  786. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  787. SETVALUE(script_buf,i,pos);
  788. i=next;
  789. }
  790. }
  791. /// Skips spaces and/or comments.
  792. const char* skip_space(const char* p)
  793. {
  794. if( p == NULL )
  795. return NULL;
  796. for(;;)
  797. {
  798. while( ISSPACE(*p) )
  799. ++p;
  800. if( *p == '/' && p[1] == '/' )
  801. {// line comment
  802. while(*p && *p!='\n')
  803. ++p;
  804. }
  805. else if( *p == '/' && p[1] == '*' )
  806. {// block comment
  807. p += 2;
  808. for(;;)
  809. {
  810. if( *p == '\0' ) {
  811. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  812. return p;
  813. }
  814. if( *p == '*' && p[1] == '/' )
  815. {// end of block comment
  816. p += 2;
  817. break;
  818. }
  819. ++p;
  820. }
  821. }
  822. else
  823. break;
  824. }
  825. return p;
  826. }
  827. /// Skips a word.
  828. /// A word consists of undercores and/or alphanumeric characters,
  829. /// and valid variable prefixes/postfixes.
  830. static const char* skip_word(const char* p)
  831. {
  832. // prefix
  833. switch( *p ) {
  834. case '@':// temporary char variable
  835. ++p; break;
  836. case '#':// account variable
  837. p += ( p[1] == '#' ? 2 : 1 ); break;
  838. case '\'':// instance variable
  839. ++p; break;
  840. case '.':// npc variable
  841. p += ( p[1] == '@' ? 2 : 1 ); break;
  842. case '$':// global variable
  843. p += ( p[1] == '@' ? 2 : 1 ); break;
  844. }
  845. while( ISALNUM(*p) || *p == '_' )
  846. ++p;
  847. // postfix
  848. if( *p == '$' )// string
  849. p++;
  850. return p;
  851. }
  852. /// Adds a word to str_data.
  853. /// @see skip_word
  854. /// @see add_str
  855. static int add_word(const char* p)
  856. {
  857. char* word;
  858. int len;
  859. int i;
  860. // Check for a word
  861. len = skip_word(p) - p;
  862. if( len == 0 )
  863. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  864. // Duplicate the word
  865. word = (char*)aMalloc(len+1);
  866. memcpy(word, p, len);
  867. word[len] = 0;
  868. // add the word
  869. i = add_str(word);
  870. aFree(word);
  871. return i;
  872. }
  873. /// Parses a function call.
  874. /// The argument list can have parenthesis or not.
  875. /// The number of arguments is checked.
  876. static
  877. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  878. {
  879. const char* p2;
  880. const char* arg=NULL;
  881. int func;
  882. func = add_word(p);
  883. if( str_data[func].type == C_FUNC ){
  884. // buildin function
  885. add_scriptl(func);
  886. add_scriptc(C_ARG);
  887. arg = buildin_func[str_data[func].val].arg;
  888. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  889. // script defined function
  890. add_scriptl(buildin_callsub_ref);
  891. add_scriptc(C_ARG);
  892. add_scriptl(func);
  893. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  894. if( *arg == 0 )
  895. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  896. if( *arg != '*' )
  897. ++arg; // count func as argument
  898. } else {
  899. #ifdef SCRIPT_CALLFUNC_CHECK
  900. const char* name = get_str(func);
  901. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  902. #endif
  903. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  904. #ifdef SCRIPT_CALLFUNC_CHECK
  905. } else {;
  906. add_scriptl(buildin_callfunc_ref);
  907. add_scriptc(C_ARG);
  908. add_scriptc(C_STR);
  909. while( *name ) add_scriptb(*name ++);
  910. add_scriptb(0);
  911. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  912. if( *arg != '*' ) ++ arg;
  913. }
  914. #endif
  915. }
  916. p = skip_word(p);
  917. p = skip_space(p);
  918. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  919. syntax.curly[syntax.curly_count].count = 0;
  920. if( *p == ';' )
  921. {// <func name> ';'
  922. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  923. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  924. {// <func name> '(' ')'
  925. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  926. p = p2;
  927. /*
  928. } else if( 0 && require_paren && *p != '(' )
  929. {// <func name>
  930. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  931. */
  932. } else
  933. {// <func name> <arg list>
  934. if( require_paren ){
  935. if( *p != '(' )
  936. disp_error_message("need '('",p);
  937. ++p; // skip '('
  938. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  939. } else if( *p == '(' ){
  940. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  941. } else {
  942. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  943. }
  944. ++syntax.curly_count;
  945. while( *arg ) {
  946. p2=parse_subexpr(p,-1);
  947. if( p == p2 )
  948. break; // not an argument
  949. if( *arg != '*' )
  950. ++arg; // next argument
  951. p=skip_space(p2);
  952. if( *arg == 0 || *p != ',' )
  953. break; // no more arguments
  954. ++p; // skip comma
  955. }
  956. --syntax.curly_count;
  957. }
  958. if( *arg && *arg != '?' && *arg != '*' )
  959. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  960. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  961. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  962. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  963. if( *p != ')' )
  964. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  965. ++p;
  966. }
  967. add_scriptc(C_FUNC);
  968. return p;
  969. }
  970. /// Processes end of logical script line.
  971. /// @param first When true, only fix up scheduling data is initialized
  972. /// @param p Script position for error reporting in set_label
  973. static void parse_nextline(bool first, const char* p)
  974. {
  975. if( !first )
  976. {
  977. add_scriptc(C_EOL); // mark end of line for stack cleanup
  978. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  979. }
  980. // initialize data for new '-' label fix up scheduling
  981. str_data[LABEL_NEXTLINE].type = C_NOP;
  982. str_data[LABEL_NEXTLINE].backpatch = -1;
  983. str_data[LABEL_NEXTLINE].label = -1;
  984. }
  985. /**
  986. * Pushes a variable into stack, processing its array index if needed.
  987. * @see parse_variable
  988. */
  989. void parse_variable_sub_push(int word, const char *p2)
  990. {
  991. if( p2 ) { // Process the variable index
  992. const char *p3 = NULL;
  993. // Push the getelementofarray method into the stack
  994. add_scriptl(buildin_getelementofarray_ref);
  995. add_scriptc(C_ARG);
  996. add_scriptl(word);
  997. // Process the sub-expression for this assignment
  998. p3 = parse_subexpr(p2 + 1, 1);
  999. p3 = skip_space(p3);
  1000. if( *p3 != ']' ) // Closing parenthesis is required for this script
  1001. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1002. // Push the closing function stack operator onto the stack
  1003. add_scriptc(C_FUNC);
  1004. p3++;
  1005. } else // No array index, simply push the variable or value onto the stack
  1006. add_scriptl(word);
  1007. }
  1008. /// Parse a variable assignment using the direct equals operator
  1009. /// @param p script position where the function should run from
  1010. /// @return NULL if not a variable assignment, the new position otherwise
  1011. const char* parse_variable(const char* p) {
  1012. int word;
  1013. c_op type = C_NOP;
  1014. const char *p2 = NULL;
  1015. const char *var = p;
  1016. if ((p[0] == '+' && p[1] == '+' && (type = C_ADD_PRE)) // pre ++
  1017. || (p[0] == '-' && p[1] == '-' && (type = C_SUB_PRE))) // pre --
  1018. var = p = skip_space(&p[2]);
  1019. // skip the variable where applicable
  1020. p = skip_word(p);
  1021. p = skip_space(p);
  1022. if( p == NULL ) {// end of the line or invalid buffer
  1023. return NULL;
  1024. }
  1025. if( *p == '[' ) {// array variable so process the array as appropriate
  1026. int i,j;
  1027. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1028. if( *p ++ == ']' && --(j) == 0 ) break;
  1029. if( *p == '[' ) ++ j;
  1030. }
  1031. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1032. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1033. }
  1034. }
  1035. if( type == C_NOP &&
  1036. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1037. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1038. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1039. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1040. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1041. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1042. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1043. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1044. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1045. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1046. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_POST) ) // post ++
  1047. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_POST) ) // post --
  1048. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1049. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1050. ) )
  1051. {// failed to find a matching operator combination so invalid
  1052. return NULL;
  1053. }
  1054. switch( type ) {
  1055. case C_ADD_PRE: // pre ++
  1056. case C_SUB_PRE: // pre --
  1057. // Nothing more to skip
  1058. break;
  1059. case C_EQ: {// incremental modifier
  1060. p = skip_space( &p[1] );
  1061. }
  1062. break;
  1063. case C_L_SHIFT:
  1064. case C_R_SHIFT: {// left or right shift modifier
  1065. p = skip_space( &p[3] );
  1066. }
  1067. break;
  1068. default: {// normal incremental command
  1069. p = skip_space( &p[2] );
  1070. }
  1071. }
  1072. if( p == NULL ) {// end of line or invalid buffer
  1073. return NULL;
  1074. }
  1075. // push the set function onto the stack
  1076. add_scriptl(buildin_set_ref);
  1077. add_scriptc(C_ARG);
  1078. // always append parenthesis to avoid errors
  1079. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1080. syntax.curly[syntax.curly_count].count = 0;
  1081. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1082. // increment the total curly count for the position in the script
  1083. ++ syntax.curly_count;
  1084. // parse the variable currently being modified
  1085. word = add_word(var);
  1086. 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
  1087. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1088. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1089. if( type != C_EQ )
  1090. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1091. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1092. add_scripti(1);
  1093. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1094. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1095. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1096. add_scripti(1);
  1097. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1098. } else { // Process the value as an expression
  1099. p = parse_subexpr(p, -1);
  1100. if( type != C_EQ )
  1101. {// push the type of modifier onto the stack
  1102. add_scriptc(type);
  1103. }
  1104. }
  1105. // decrement the curly count for the position within the script
  1106. -- syntax.curly_count;
  1107. // close the script by appending the function operator
  1108. add_scriptc(C_FUNC);
  1109. // push the buffer from the method
  1110. return p;
  1111. }
  1112. /*
  1113. * Checks whether the gives string is a number literal
  1114. *
  1115. * Mainly necessary to differentiate between number literals and NPC name
  1116. * constants, since several of those start with a digit.
  1117. *
  1118. * All this does is to check if the string begins with an optional + or - sign,
  1119. * followed by a hexadecimal or decimal number literal literal and is NOT
  1120. * followed by a underscore or letter.
  1121. *
  1122. * @author : Hercules.ws
  1123. * @param p Pointer to the string to check
  1124. * @return Whether the string is a number literal
  1125. */
  1126. bool is_number(const char *p) {
  1127. const char *np;
  1128. if (!p)
  1129. return false;
  1130. if (*p == '-' || *p == '+')
  1131. p++;
  1132. np = p;
  1133. if (*p == '0' && p[1] == 'x') {
  1134. p+=2;
  1135. np = p;
  1136. // Hexadecimal
  1137. while (ISXDIGIT(*np))
  1138. np++;
  1139. } else {
  1140. // Decimal
  1141. while (ISDIGIT(*np))
  1142. np++;
  1143. }
  1144. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1145. return true;
  1146. return false;
  1147. }
  1148. /*==========================================
  1149. * Analysis section
  1150. *------------------------------------------*/
  1151. const char* parse_simpleexpr(const char *p)
  1152. {
  1153. long long i;
  1154. p=skip_space(p);
  1155. if(*p==';' || *p==',')
  1156. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1157. if(*p=='('){
  1158. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1159. ++syntax.curly[i].count;
  1160. p=parse_subexpr(p+1,-1);
  1161. p=skip_space(p);
  1162. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1163. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1164. ){
  1165. if( *p == ',' ){
  1166. syntax.curly[i].flag = ARGLIST_PAREN;
  1167. return p;
  1168. } else
  1169. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1170. }
  1171. if( *p != ')' )
  1172. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1173. ++p;
  1174. } else if(is_number(p)) {
  1175. char *np;
  1176. while(*p == '0' && ISDIGIT(p[1])) p++;
  1177. i=strtoll(p,&np,0);
  1178. if( i < INT_MIN ) {
  1179. i = INT_MIN;
  1180. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1181. } else if( i > INT_MAX ) {
  1182. i = INT_MAX;
  1183. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1184. }
  1185. add_scripti((int)i);
  1186. p=np;
  1187. } else if(*p=='"'){
  1188. add_scriptc(C_STR);
  1189. p++;
  1190. while( *p && *p != '"' ){
  1191. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1192. {
  1193. char buf[8];
  1194. size_t len = skip_escaped_c(p) - p;
  1195. size_t n = sv_unescape_c(buf, p, len);
  1196. if( n != 1 )
  1197. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1198. p += len;
  1199. add_scriptb(*buf);
  1200. continue;
  1201. }
  1202. else if( *p == '\n' )
  1203. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1204. add_scriptb(*p++);
  1205. }
  1206. if(!*p)
  1207. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1208. add_scriptb(0);
  1209. p++; //'"'
  1210. } else {
  1211. int l;
  1212. const char* pv;
  1213. // label , register , function etc
  1214. if(skip_word(p)==p)
  1215. disp_error_message("parse_simpleexpr: unexpected character",p);
  1216. l=add_word(p);
  1217. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1218. return parse_callfunc(p,1,0);
  1219. #ifdef SCRIPT_CALLFUNC_CHECK
  1220. else {
  1221. const char* name = get_str(l);
  1222. if( strdb_get(userfunc_db,name) != NULL ) {
  1223. return parse_callfunc(p,1,1);
  1224. }
  1225. }
  1226. #endif
  1227. if( (pv = parse_variable(p)) )
  1228. {// successfully processed a variable assignment
  1229. return pv;
  1230. }
  1231. p=skip_word(p);
  1232. if( *p == '[' ){
  1233. // array(name[i] => getelementofarray(name,i) )
  1234. add_scriptl(buildin_getelementofarray_ref);
  1235. add_scriptc(C_ARG);
  1236. add_scriptl(l);
  1237. p=parse_subexpr(p+1,-1);
  1238. p=skip_space(p);
  1239. if( *p != ']' )
  1240. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1241. ++p;
  1242. add_scriptc(C_FUNC);
  1243. }else
  1244. add_scriptl(l);
  1245. }
  1246. return p;
  1247. }
  1248. /*==========================================
  1249. * Analysis of the expression
  1250. *------------------------------------------*/
  1251. const char* parse_subexpr(const char* p,int limit)
  1252. {
  1253. int op,opl,len;
  1254. p=skip_space(p);
  1255. if( *p == '-' ){
  1256. const char* tmpp = skip_space(p+1);
  1257. if( *tmpp == ';' || *tmpp == ',' ){
  1258. add_scriptl(LABEL_NEXTLINE);
  1259. p++;
  1260. return p;
  1261. }
  1262. }
  1263. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1264. p = parse_variable(p);
  1265. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1266. p = parse_subexpr(p + 1, 11);
  1267. add_scriptc(op);
  1268. } else
  1269. p = parse_simpleexpr(p);
  1270. p = skip_space(p);
  1271. while((
  1272. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1273. (op=C_ADD,opl=9,len=1,*p=='+') ||
  1274. (op=C_SUB,opl=9,len=1,*p=='-') ||
  1275. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1276. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1277. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1278. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1279. (op=C_AND,opl=5,len=1,*p=='&') ||
  1280. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1281. (op=C_OR,opl=3,len=1,*p=='|') ||
  1282. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1283. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1284. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1285. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1286. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1287. (op=C_GT,opl=7,len=1,*p=='>') ||
  1288. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1289. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1290. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1291. p+=len;
  1292. if(op == C_OP3) {
  1293. p=parse_subexpr(p,-1);
  1294. p=skip_space(p);
  1295. if( *(p++) != ':')
  1296. disp_error_message("parse_subexpr: expected ':'", p-1);
  1297. p=parse_subexpr(p,-1);
  1298. } else {
  1299. p=parse_subexpr(p,opl);
  1300. }
  1301. add_scriptc(op);
  1302. p=skip_space(p);
  1303. }
  1304. return p; /* return first untreated operator */
  1305. }
  1306. /*==========================================
  1307. * Evaluation of the expression
  1308. *------------------------------------------*/
  1309. const char* parse_expr(const char *p)
  1310. {
  1311. switch(*p){
  1312. case ')': case ';': case ':': case '[': case ']':
  1313. case '}':
  1314. disp_error_message("parse_expr: unexpected character",p);
  1315. }
  1316. p=parse_subexpr(p,-1);
  1317. return p;
  1318. }
  1319. /*==========================================
  1320. * Analysis of the line
  1321. *------------------------------------------*/
  1322. const char* parse_line(const char* p)
  1323. {
  1324. const char* p2;
  1325. p=skip_space(p);
  1326. if(*p==';') {
  1327. //Close decision for if(); for(); while();
  1328. p = parse_syntax_close(p + 1);
  1329. return p;
  1330. }
  1331. if(*p==')' && parse_syntax_for_flag)
  1332. return p+1;
  1333. p = skip_space(p);
  1334. if(p[0] == '{') {
  1335. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1336. syntax.curly[syntax.curly_count].count = -1;
  1337. syntax.curly[syntax.curly_count].index = -1;
  1338. syntax.curly_count++;
  1339. return p + 1;
  1340. } else if(p[0] == '}') {
  1341. return parse_curly_close(p);
  1342. }
  1343. // Syntax-related processing
  1344. p2 = parse_syntax(p);
  1345. if(p2 != NULL)
  1346. return p2;
  1347. // attempt to process a variable assignment
  1348. p2 = parse_variable(p);
  1349. if( p2 != NULL )
  1350. {// variable assignment processed so leave the method
  1351. return parse_syntax_close(p2 + 1);
  1352. }
  1353. p = parse_callfunc(p,0,0);
  1354. p = skip_space(p);
  1355. if(parse_syntax_for_flag) {
  1356. if( *p != ')' )
  1357. disp_error_message("parse_line: expected ')'",p);
  1358. } else {
  1359. if( *p != ';' )
  1360. disp_error_message("parse_line: expected ';'",p);
  1361. }
  1362. //Binding decision for if(), for(), while()
  1363. p = parse_syntax_close(p+1);
  1364. return p;
  1365. }
  1366. // { ... } Closing process
  1367. const char* parse_curly_close(const char* p)
  1368. {
  1369. if(syntax.curly_count <= 0) {
  1370. disp_error_message("parse_curly_close: unexpected string",p);
  1371. return p + 1;
  1372. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1373. syntax.curly_count--;
  1374. //Close decision if, for , while
  1375. p = parse_syntax_close(p + 1);
  1376. return p;
  1377. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1378. //Closing switch()
  1379. int pos = syntax.curly_count-1;
  1380. char label[256];
  1381. int l;
  1382. // Remove temporary variables
  1383. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1384. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1385. parse_line(label);
  1386. syntax.curly_count--;
  1387. // Go to the end pointer unconditionally
  1388. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1389. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1390. parse_line(label);
  1391. syntax.curly_count--;
  1392. // You are here labeled
  1393. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1394. l=add_str(label);
  1395. set_label(l,script_pos, p);
  1396. if(syntax.curly[pos].flag) {
  1397. //Exists default
  1398. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1399. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1400. parse_line(label);
  1401. syntax.curly_count--;
  1402. }
  1403. // Label end
  1404. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1405. l=add_str(label);
  1406. set_label(l,script_pos, p);
  1407. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1408. syntax.curly_count--;
  1409. //Closing decision if, for , while
  1410. p = parse_syntax_close(p + 1);
  1411. return p;
  1412. } else {
  1413. disp_error_message("parse_curly_close: unexpected string",p);
  1414. return p + 1;
  1415. }
  1416. }
  1417. // Syntax-related processing
  1418. // break, case, continue, default, do, for, function,
  1419. // if, switch, while ? will handle this internally.
  1420. const char* parse_syntax(const char* p)
  1421. {
  1422. const char *p2 = skip_word(p);
  1423. switch(*p) {
  1424. case 'B':
  1425. case 'b':
  1426. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1427. // break Processing
  1428. char label[256];
  1429. int pos = syntax.curly_count - 1;
  1430. while(pos >= 0) {
  1431. if(syntax.curly[pos].type == TYPE_DO) {
  1432. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1433. break;
  1434. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1435. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1436. break;
  1437. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1438. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1439. break;
  1440. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1441. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1442. break;
  1443. }
  1444. pos--;
  1445. }
  1446. if(pos < 0) {
  1447. disp_error_message("parse_syntax: unexpected 'break'",p);
  1448. } else {
  1449. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1450. parse_line(label);
  1451. syntax.curly_count--;
  1452. }
  1453. p = skip_space(p2);
  1454. if(*p != ';')
  1455. disp_error_message("parse_syntax: expected ';'",p);
  1456. // Closing decision if, for , while
  1457. p = parse_syntax_close(p + 1);
  1458. return p;
  1459. }
  1460. break;
  1461. case 'c':
  1462. case 'C':
  1463. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1464. //Processing case
  1465. int pos = syntax.curly_count-1;
  1466. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1467. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1468. return p+1;
  1469. } else {
  1470. char label[256];
  1471. int l,v;
  1472. char *np;
  1473. if(syntax.curly[pos].count != 1) {
  1474. //Jump for FALLTHRU
  1475. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1476. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1477. parse_line(label);
  1478. syntax.curly_count--;
  1479. // You are here labeled
  1480. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1481. l=add_str(label);
  1482. set_label(l,script_pos, p);
  1483. }
  1484. //Decision statement switch
  1485. p = skip_space(p2);
  1486. if(p == p2) {
  1487. disp_error_message("parse_syntax: expected a space ' '",p);
  1488. }
  1489. // check whether case label is integer or not
  1490. if(is_number(p)) {
  1491. //Numeric value
  1492. v = (int)strtol(p,&np,0);
  1493. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1494. p++;
  1495. p = skip_word(p);
  1496. if(np != p)
  1497. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1498. } else {
  1499. //Check for constants
  1500. p2 = skip_word(p);
  1501. v = (int)(size_t) (p2-p); // length of word at p2
  1502. memcpy(label,p,v);
  1503. label[v]='\0';
  1504. if( !script_get_constant(label, &v) )
  1505. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1506. p = skip_word(p);
  1507. }
  1508. p = skip_space(p);
  1509. if(*p != ':')
  1510. disp_error_message("parse_syntax: expect ':'",p);
  1511. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1512. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1513. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1514. // Bad I do not parse twice
  1515. p2 = parse_line(label);
  1516. parse_line(p2);
  1517. syntax.curly_count--;
  1518. if(syntax.curly[pos].count != 1) {
  1519. // Label after the completion of FALLTHRU
  1520. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1521. l=add_str(label);
  1522. set_label(l,script_pos,p);
  1523. }
  1524. // check duplication of case label [Rayce]
  1525. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1526. disp_error_message("parse_syntax: dup 'case'",p);
  1527. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1528. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1529. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1530. parse_line(label);
  1531. syntax.curly_count--;
  1532. syntax.curly[pos].count++;
  1533. }
  1534. return p + 1;
  1535. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1536. // Processing continue
  1537. char label[256];
  1538. int pos = syntax.curly_count - 1;
  1539. while(pos >= 0) {
  1540. if(syntax.curly[pos].type == TYPE_DO) {
  1541. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1542. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1543. break;
  1544. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1545. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1546. break;
  1547. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1548. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1549. break;
  1550. }
  1551. pos--;
  1552. }
  1553. if(pos < 0) {
  1554. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1555. } else {
  1556. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1557. parse_line(label);
  1558. syntax.curly_count--;
  1559. }
  1560. p = skip_space(p2);
  1561. if(*p != ';')
  1562. disp_error_message("parse_syntax: expected ';'",p);
  1563. //Closing decision if, for , while
  1564. p = parse_syntax_close(p + 1);
  1565. return p;
  1566. }
  1567. break;
  1568. case 'd':
  1569. case 'D':
  1570. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1571. // Switch - default processing
  1572. int pos = syntax.curly_count-1;
  1573. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1574. disp_error_message("parse_syntax: unexpected 'default'",p);
  1575. } else if(syntax.curly[pos].flag) {
  1576. disp_error_message("parse_syntax: dup 'default'",p);
  1577. } else {
  1578. char label[256];
  1579. int l;
  1580. // Put the label location
  1581. p = skip_space(p2);
  1582. if(*p != ':') {
  1583. disp_error_message("parse_syntax: expected ':'",p);
  1584. }
  1585. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1586. l=add_str(label);
  1587. set_label(l,script_pos,p);
  1588. // Skip to the next link w/o condition
  1589. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1590. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1591. parse_line(label);
  1592. syntax.curly_count--;
  1593. // The default label
  1594. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1595. l=add_str(label);
  1596. set_label(l,script_pos,p);
  1597. syntax.curly[syntax.curly_count - 1].flag = 1;
  1598. syntax.curly[pos].count++;
  1599. }
  1600. return p + 1;
  1601. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1602. int l;
  1603. char label[256];
  1604. p=skip_space(p2);
  1605. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1606. syntax.curly[syntax.curly_count].count = 1;
  1607. syntax.curly[syntax.curly_count].index = syntax.index++;
  1608. syntax.curly[syntax.curly_count].flag = 0;
  1609. // Label of the (do) form here
  1610. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1611. l=add_str(label);
  1612. set_label(l,script_pos,p);
  1613. syntax.curly_count++;
  1614. return p;
  1615. }
  1616. break;
  1617. case 'f':
  1618. case 'F':
  1619. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1620. int l;
  1621. char label[256];
  1622. int pos = syntax.curly_count;
  1623. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1624. syntax.curly[syntax.curly_count].count = 1;
  1625. syntax.curly[syntax.curly_count].index = syntax.index++;
  1626. syntax.curly[syntax.curly_count].flag = 0;
  1627. syntax.curly_count++;
  1628. p=skip_space(p2);
  1629. if(*p != '(')
  1630. disp_error_message("parse_syntax: expected '('",p);
  1631. p++;
  1632. // Execute the initialization statement
  1633. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1634. p=parse_line(p);
  1635. syntax.curly_count--;
  1636. // Form the start of label decision
  1637. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1638. l=add_str(label);
  1639. set_label(l,script_pos,p);
  1640. p=skip_space(p);
  1641. if(*p == ';') {
  1642. // For (; Because the pattern of always true ;)
  1643. ;
  1644. } else {
  1645. // Skip to the end point if the condition is false
  1646. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1647. add_scriptl(add_str("jump_zero"));
  1648. add_scriptc(C_ARG);
  1649. p=parse_expr(p);
  1650. p=skip_space(p);
  1651. add_scriptl(add_str(label));
  1652. add_scriptc(C_FUNC);
  1653. }
  1654. if(*p != ';')
  1655. disp_error_message("parse_syntax: expected ';'",p);
  1656. p++;
  1657. // Skip to the beginning of the loop
  1658. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1659. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1660. parse_line(label);
  1661. syntax.curly_count--;
  1662. // Labels to form the next loop
  1663. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1664. l=add_str(label);
  1665. set_label(l,script_pos,p);
  1666. // Process the next time you enter the loop
  1667. // A ')' last for; flag to be treated as'
  1668. parse_syntax_for_flag = 1;
  1669. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1670. p=parse_line(p);
  1671. syntax.curly_count--;
  1672. parse_syntax_for_flag = 0;
  1673. // Skip to the determination process conditions
  1674. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1675. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1676. parse_line(label);
  1677. syntax.curly_count--;
  1678. // Loop start labeling
  1679. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1680. l=add_str(label);
  1681. set_label(l,script_pos,p);
  1682. return p;
  1683. }
  1684. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1685. {// internal script function
  1686. const char *func_name;
  1687. func_name = skip_space(p2);
  1688. p = skip_word(func_name);
  1689. if( p == func_name )
  1690. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1691. p2 = skip_space(p);
  1692. if( *p2 == ';' )
  1693. {// function <name> ;
  1694. // function declaration - just register the name
  1695. int l;
  1696. l = add_word(func_name);
  1697. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1698. str_data[l].type = C_USERFUNC;
  1699. else if( str_data[l].type == C_USERFUNC )
  1700. ; // already registered
  1701. else
  1702. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1703. // Close condition of if, for, while
  1704. p = parse_syntax_close(p2 + 1);
  1705. return p;
  1706. }
  1707. else if(*p2 == '{')
  1708. {// function <name> <line/block of code>
  1709. char label[256];
  1710. int l;
  1711. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1712. syntax.curly[syntax.curly_count].count = 1;
  1713. syntax.curly[syntax.curly_count].index = syntax.index++;
  1714. syntax.curly[syntax.curly_count].flag = 0;
  1715. ++syntax.curly_count;
  1716. // Jump over the function code
  1717. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1718. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1719. ++syntax.curly_count;
  1720. parse_line(label);
  1721. --syntax.curly_count;
  1722. // Set the position of the function (label)
  1723. l=add_word(func_name);
  1724. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1725. {
  1726. str_data[l].type = C_USERFUNC;
  1727. set_label(l, script_pos, p);
  1728. if( parse_options&SCRIPT_USE_LABEL_DB )
  1729. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1730. }
  1731. else
  1732. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1733. return skip_space(p);
  1734. }
  1735. else
  1736. {
  1737. disp_error_message("expect ';' or '{' at function syntax",p);
  1738. }
  1739. }
  1740. break;
  1741. case 'i':
  1742. case 'I':
  1743. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1744. // If process
  1745. char label[256];
  1746. p=skip_space(p2);
  1747. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1748. disp_error_message("need '('",p);
  1749. }
  1750. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1751. syntax.curly[syntax.curly_count].count = 1;
  1752. syntax.curly[syntax.curly_count].index = syntax.index++;
  1753. syntax.curly[syntax.curly_count].flag = 0;
  1754. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1755. syntax.curly_count++;
  1756. add_scriptl(add_str("jump_zero"));
  1757. add_scriptc(C_ARG);
  1758. p=parse_expr(p);
  1759. p=skip_space(p);
  1760. add_scriptl(add_str(label));
  1761. add_scriptc(C_FUNC);
  1762. return p;
  1763. }
  1764. break;
  1765. case 's':
  1766. case 'S':
  1767. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1768. // Processing of switch ()
  1769. char label[256];
  1770. p=skip_space(p2);
  1771. if(*p != '(') {
  1772. disp_error_message("need '('",p);
  1773. }
  1774. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1775. syntax.curly[syntax.curly_count].count = 1;
  1776. syntax.curly[syntax.curly_count].index = syntax.index++;
  1777. syntax.curly[syntax.curly_count].flag = 0;
  1778. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1779. syntax.curly_count++;
  1780. add_scriptl(add_str("set"));
  1781. add_scriptc(C_ARG);
  1782. add_scriptl(add_str(label));
  1783. p=parse_expr(p);
  1784. p=skip_space(p);
  1785. if(*p != '{') {
  1786. disp_error_message("parse_syntax: expected '{'",p);
  1787. }
  1788. add_scriptc(C_FUNC);
  1789. return p + 1;
  1790. }
  1791. break;
  1792. case 'w':
  1793. case 'W':
  1794. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1795. int l;
  1796. char label[256];
  1797. p=skip_space(p2);
  1798. if(*p != '(') {
  1799. disp_error_message("need '('",p);
  1800. }
  1801. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1802. syntax.curly[syntax.curly_count].count = 1;
  1803. syntax.curly[syntax.curly_count].index = syntax.index++;
  1804. syntax.curly[syntax.curly_count].flag = 0;
  1805. // Form the start of label decision
  1806. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1807. l=add_str(label);
  1808. set_label(l,script_pos,p);
  1809. // Skip to the end point if the condition is false
  1810. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1811. syntax.curly_count++;
  1812. add_scriptl(add_str("jump_zero"));
  1813. add_scriptc(C_ARG);
  1814. p=parse_expr(p);
  1815. p=skip_space(p);
  1816. add_scriptl(add_str(label));
  1817. add_scriptc(C_FUNC);
  1818. return p;
  1819. }
  1820. break;
  1821. }
  1822. return NULL;
  1823. }
  1824. const char* parse_syntax_close(const char *p) {
  1825. // If (...) for (...) hoge (); as to make sure closed closed once again
  1826. int flag;
  1827. do {
  1828. p = parse_syntax_close_sub(p,&flag);
  1829. } while(flag);
  1830. return p;
  1831. }
  1832. // Close judgment if, for, while, of do
  1833. // flag == 1 : closed
  1834. // flag == 0 : not closed
  1835. const char* parse_syntax_close_sub(const char* p,int* flag)
  1836. {
  1837. char label[256];
  1838. int pos = syntax.curly_count - 1;
  1839. int l;
  1840. *flag = 1;
  1841. if(syntax.curly_count <= 0) {
  1842. *flag = 0;
  1843. return p;
  1844. } else if(syntax.curly[pos].type == TYPE_IF) {
  1845. const char *bp = p;
  1846. const char *p2;
  1847. // if-block and else-block end is a new line
  1848. parse_nextline(false, p);
  1849. // Skip to the last location if
  1850. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1851. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1852. parse_line(label);
  1853. syntax.curly_count--;
  1854. // Put the label of the location
  1855. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1856. l=add_str(label);
  1857. set_label(l,script_pos,p);
  1858. syntax.curly[pos].count++;
  1859. p = skip_space(p);
  1860. p2 = skip_word(p);
  1861. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1862. // else or else - if
  1863. p = skip_space(p2);
  1864. p2 = skip_word(p);
  1865. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1866. // else - if
  1867. p=skip_space(p2);
  1868. if(*p != '(') {
  1869. disp_error_message("need '('",p);
  1870. }
  1871. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1872. add_scriptl(add_str("jump_zero"));
  1873. add_scriptc(C_ARG);
  1874. p=parse_expr(p);
  1875. p=skip_space(p);
  1876. add_scriptl(add_str(label));
  1877. add_scriptc(C_FUNC);
  1878. *flag = 0;
  1879. return p;
  1880. } else {
  1881. // else
  1882. if(!syntax.curly[pos].flag) {
  1883. syntax.curly[pos].flag = 1;
  1884. *flag = 0;
  1885. return p;
  1886. }
  1887. }
  1888. }
  1889. // Close if
  1890. syntax.curly_count--;
  1891. // Put the label of the final location
  1892. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1893. l=add_str(label);
  1894. set_label(l,script_pos,p);
  1895. if(syntax.curly[pos].flag == 1) {
  1896. // Because the position of the pointer is the same if not else for this
  1897. return bp;
  1898. }
  1899. return p;
  1900. } else if(syntax.curly[pos].type == TYPE_DO) {
  1901. int l2;
  1902. char label2[256];
  1903. const char *p2;
  1904. if(syntax.curly[pos].flag) {
  1905. // (Come here continue) to form the label here
  1906. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1907. l2=add_str(label2);
  1908. set_label(l2,script_pos,p);
  1909. }
  1910. // Skip to the end point if the condition is false
  1911. p = skip_space(p);
  1912. p2 = skip_word(p);
  1913. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1914. disp_error_message("parse_syntax: expected 'while'",p);
  1915. p = skip_space(p2);
  1916. if(*p != '(') {
  1917. disp_error_message("need '('",p);
  1918. }
  1919. // do-block end is a new line
  1920. parse_nextline(false, p);
  1921. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1922. add_scriptl(add_str("jump_zero"));
  1923. add_scriptc(C_ARG);
  1924. p=parse_expr(p);
  1925. p=skip_space(p);
  1926. add_scriptl(add_str(label2));
  1927. add_scriptc(C_FUNC);
  1928. // Skip to the starting point
  1929. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1930. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1931. parse_line(label2);
  1932. syntax.curly_count--;
  1933. // Form label of the end point conditions
  1934. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1935. l2=add_str(label2);
  1936. set_label(l2,script_pos,p);
  1937. p = skip_space(p);
  1938. if(*p != ';') {
  1939. disp_error_message("parse_syntax: expected ';'",p);
  1940. return p+1;
  1941. }
  1942. p++;
  1943. syntax.curly_count--;
  1944. return p;
  1945. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1946. // for-block end is a new line
  1947. parse_nextline(false, p);
  1948. // Skip to the next loop
  1949. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1950. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1951. parse_line(label);
  1952. syntax.curly_count--;
  1953. // End for labeling
  1954. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1955. l=add_str(label);
  1956. set_label(l,script_pos,p);
  1957. syntax.curly_count--;
  1958. return p;
  1959. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1960. // while-block end is a new line
  1961. parse_nextline(false, p);
  1962. // Skip to the decision while
  1963. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1964. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1965. parse_line(label);
  1966. syntax.curly_count--;
  1967. // End while labeling
  1968. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1969. l=add_str(label);
  1970. set_label(l,script_pos,p);
  1971. syntax.curly_count--;
  1972. return p;
  1973. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1974. int pos2 = syntax.curly_count-1;
  1975. char label2[256];
  1976. int l2;
  1977. // Back
  1978. sprintf(label2,"return;");
  1979. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1980. parse_line(label2);
  1981. syntax.curly_count--;
  1982. // Put the label of the location
  1983. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1984. l2=add_str(label2);
  1985. set_label(l2,script_pos,p);
  1986. syntax.curly_count--;
  1987. return p;
  1988. } else {
  1989. *flag = 0;
  1990. return p;
  1991. }
  1992. }
  1993. /*==========================================
  1994. * Added built-in functions
  1995. *------------------------------------------*/
  1996. static void add_buildin_func(void)
  1997. {
  1998. int i;
  1999. for( i = 0; buildin_func[i].func; i++ )
  2000. {
  2001. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  2002. // 'v' - value (either string or int or reference)
  2003. // 's' - string
  2004. // 'i' - int
  2005. // 'r' - reference (of a variable)
  2006. // 'l' - label
  2007. // '?' - one optional parameter
  2008. // '*' - unknown number of optional parameters
  2009. const char* p = buildin_func[i].arg;
  2010. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  2011. while( *p == '?' ) ++p;
  2012. if( *p == '*' ) ++p;
  2013. if( *p != 0){
  2014. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  2015. } else if( *skip_word(buildin_func[i].name) != 0 ){
  2016. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  2017. } else {
  2018. int n = add_str(buildin_func[i].name);
  2019. str_data[n].type = C_FUNC;
  2020. str_data[n].val = i;
  2021. str_data[n].func = buildin_func[i].func;
  2022. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  2023. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2024. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2025. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2026. }
  2027. }
  2028. }
  2029. /// Retrieves the value of a constant.
  2030. bool script_get_constant(const char* name, int* value)
  2031. {
  2032. int n = search_str(name);
  2033. if( n == -1 || str_data[n].type != C_INT )
  2034. {// not found or not a constant
  2035. return false;
  2036. }
  2037. value[0] = str_data[n].val;
  2038. return true;
  2039. }
  2040. /// Creates new constant or parameter with given value.
  2041. void script_set_constant(const char* name, int value, bool isparameter)
  2042. {
  2043. int n = add_str(name);
  2044. if( str_data[n].type == C_NOP )
  2045. {// new
  2046. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2047. str_data[n].val = value;
  2048. }
  2049. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2050. {// existing parameter or constant
  2051. 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);
  2052. }
  2053. else
  2054. {// existing name
  2055. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2056. }
  2057. }
  2058. /*==========================================
  2059. * Reading constant databases
  2060. * const.txt
  2061. *------------------------------------------*/
  2062. static void read_constdb(void)
  2063. {
  2064. FILE *fp;
  2065. char line[1024],name[1024],val[1024];
  2066. int type;
  2067. int entries=0, skipped=0, linenum=0;
  2068. sprintf(line, "%s/const.txt", db_path);
  2069. fp=fopen(line, "r");
  2070. if(fp==NULL){
  2071. ShowError("can't read %s\n", line);
  2072. return ;
  2073. }
  2074. while(fgets(line, sizeof(line), fp))
  2075. {
  2076. linenum++;
  2077. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  2078. continue;
  2079. if(line[0]=='/' && line[1]=='/') //ignore commented line
  2080. continue;
  2081. type=0;
  2082. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%11d",name,val,&type)>=2 ||
  2083. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d",name,val,&type)>=2){
  2084. entries++;
  2085. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2086. }
  2087. else {
  2088. skipped++;
  2089. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2090. }
  2091. }
  2092. fclose(fp);
  2093. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2094. if(skipped){
  2095. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2096. }
  2097. }
  2098. /**
  2099. * Sets source-end constants for NPC scripts to access.
  2100. **/
  2101. void script_hardcoded_constants(void) {
  2102. #include "script_constants.h"
  2103. }
  2104. /*==========================================
  2105. * Display emplacement line of script
  2106. *------------------------------------------*/
  2107. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2108. {
  2109. int i;
  2110. if( p == NULL || !p[0] ) return NULL;
  2111. if( line < 0 )
  2112. StringBuf_Printf(buf, "*% 5d : ", -line);
  2113. else
  2114. StringBuf_Printf(buf, " % 5d : ", line);
  2115. for(i=0;p[i] && p[i] != '\n';i++){
  2116. if(p + i != mark)
  2117. StringBuf_Printf(buf, "%c", p[i]);
  2118. else
  2119. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2120. }
  2121. StringBuf_AppendStr(buf, "\n");
  2122. return p+i+(p[i] == '\n' ? 1 : 0);
  2123. }
  2124. 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) {
  2125. // Find the line where the error occurred
  2126. int j;
  2127. int line = start_line;
  2128. const char *p;
  2129. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2130. for(p=src;p && *p;line++){
  2131. const char *lineend=strchr(p,'\n');
  2132. if(lineend==NULL || error_pos_cur<lineend){
  2133. break;
  2134. }
  2135. for( j = 0; j < 4; j++ ) {
  2136. linestart[j] = linestart[j+1];
  2137. }
  2138. linestart[4] = p;
  2139. p=lineend+1;
  2140. }
  2141. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2142. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2143. for(j = 0; j < 5; j++ ) {
  2144. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2145. }
  2146. p = script_print_line(buf, p, error_pos_cur, -line);
  2147. for(j = 0; j < 5; j++) {
  2148. p = script_print_line(buf, p, NULL, line + j + 1 );
  2149. }
  2150. }
  2151. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2152. StringBuf buf;
  2153. StringBuf_Init(&buf);
  2154. StringBuf_AppendStr(&buf, "\a\n");
  2155. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2156. ShowError("%s", StringBuf_Value(&buf));
  2157. StringBuf_Destroy(&buf);
  2158. }
  2159. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2160. StringBuf buf;
  2161. StringBuf_Init(&buf);
  2162. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2163. ShowWarning("%s", StringBuf_Value(&buf));
  2164. StringBuf_Destroy(&buf);
  2165. }
  2166. /*==========================================
  2167. * Analysis of the script
  2168. *------------------------------------------*/
  2169. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2170. {
  2171. const char *p,*tmpp;
  2172. int i;
  2173. struct script_code* code = NULL;
  2174. static int first=1;
  2175. char end;
  2176. bool unresolved_names = false;
  2177. parser_current_src = src;
  2178. parser_current_file = file;
  2179. parser_current_line = line;
  2180. if( src == NULL )
  2181. return NULL;// empty script
  2182. memset(&syntax,0,sizeof(syntax));
  2183. if(first){
  2184. add_buildin_func();
  2185. read_constdb();
  2186. script_hardcoded_constants();
  2187. first=0;
  2188. }
  2189. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2190. script_pos=0;
  2191. script_size=SCRIPT_BLOCK_SIZE;
  2192. parse_nextline(true, NULL);
  2193. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2194. if( options&SCRIPT_USE_LABEL_DB )
  2195. db_clear(scriptlabel_db);
  2196. parse_options = options;
  2197. if( setjmp( error_jump ) != 0 ) {
  2198. //Restore program state when script has problems. [from jA]
  2199. int j;
  2200. const int size = ARRAYLENGTH(syntax.curly);
  2201. if( error_report )
  2202. script_error(src,file,line,error_msg,error_pos);
  2203. aFree( error_msg );
  2204. aFree( script_buf );
  2205. script_pos = 0;
  2206. script_size = 0;
  2207. script_buf = NULL;
  2208. for(j=LABEL_START;j<str_num;j++)
  2209. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2210. for(j=0; j<size; j++)
  2211. linkdb_final(&syntax.curly[j].case_label);
  2212. return NULL;
  2213. }
  2214. parse_syntax_for_flag=0;
  2215. p=src;
  2216. p=skip_space(p);
  2217. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2218. {// does not require brackets around the script
  2219. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2220. {// empty script and can return NULL
  2221. aFree( script_buf );
  2222. script_pos = 0;
  2223. script_size = 0;
  2224. script_buf = NULL;
  2225. return NULL;
  2226. }
  2227. end = '\0';
  2228. }
  2229. else
  2230. {// requires brackets around the script
  2231. if( *p != '{' )
  2232. disp_error_message("not found '{'",p);
  2233. p = skip_space(p+1);
  2234. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2235. {// empty script and can return NULL
  2236. aFree( script_buf );
  2237. script_pos = 0;
  2238. script_size = 0;
  2239. script_buf = NULL;
  2240. return NULL;
  2241. }
  2242. end = '}';
  2243. }
  2244. // clear references of labels, variables and internal functions
  2245. for(i=LABEL_START;i<str_num;i++){
  2246. if(
  2247. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2248. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2249. ){
  2250. str_data[i].type=C_NOP;
  2251. str_data[i].backpatch=-1;
  2252. str_data[i].label=-1;
  2253. }
  2254. }
  2255. while( syntax.curly_count != 0 || *p != end )
  2256. {
  2257. if( *p == '\0' )
  2258. disp_error_message("unexpected end of script",p);
  2259. // Special handling only label
  2260. tmpp=skip_space(skip_word(p));
  2261. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2262. i=add_word(p);
  2263. set_label(i,script_pos,p);
  2264. if( parse_options&SCRIPT_USE_LABEL_DB )
  2265. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2266. p=tmpp+1;
  2267. p=skip_space(p);
  2268. continue;
  2269. }
  2270. // All other lumped
  2271. p=parse_line(p);
  2272. p=skip_space(p);
  2273. parse_nextline(false, p);
  2274. }
  2275. add_scriptc(C_NOP);
  2276. // trim code to size
  2277. script_size = script_pos;
  2278. RECREATE(script_buf,unsigned char,script_pos);
  2279. // default unknown references to variables
  2280. for(i=LABEL_START;i<str_num;i++){
  2281. if(str_data[i].type==C_NOP){
  2282. int j;
  2283. str_data[i].type=C_NAME;
  2284. str_data[i].label=i;
  2285. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2286. int next=GETVALUE(script_buf,j);
  2287. SETVALUE(script_buf,j,i);
  2288. j=next;
  2289. }
  2290. }
  2291. else if( str_data[i].type == C_USERFUNC )
  2292. {// 'function name;' without follow-up code
  2293. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2294. unresolved_names = true;
  2295. }
  2296. }
  2297. if( unresolved_names )
  2298. {
  2299. disp_error_message("parse_script: unresolved function references", p);
  2300. }
  2301. #ifdef DEBUG_DISP
  2302. for(i=0;i<script_pos;i++){
  2303. if((i&15)==0) ShowMessage("%04x : ",i);
  2304. ShowMessage("%02x ",script_buf[i]);
  2305. if((i&15)==15) ShowMessage("\n");
  2306. }
  2307. ShowMessage("\n");
  2308. #endif
  2309. #ifdef DEBUG_DISASM
  2310. {
  2311. int i = 0,j;
  2312. while(i < script_pos) {
  2313. c_op op = get_com(script_buf,&i);
  2314. ShowMessage("%06x %s", i, script_op2name(op));
  2315. j = i;
  2316. switch(op) {
  2317. case C_INT:
  2318. ShowMessage(" %d", get_num(script_buf,&i));
  2319. break;
  2320. case C_POS:
  2321. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2322. i += 3;
  2323. break;
  2324. case C_NAME:
  2325. j = (*(int*)(script_buf+i)&0xffffff);
  2326. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2327. i += 3;
  2328. break;
  2329. case C_STR:
  2330. j = strlen(script_buf + i);
  2331. ShowMessage(" %s", script_buf + i);
  2332. i += j+1;
  2333. break;
  2334. }
  2335. ShowMessage(CL_CLL"\n");
  2336. }
  2337. }
  2338. #endif
  2339. CREATE(code,struct script_code,1);
  2340. code->script_buf = script_buf;
  2341. code->script_size = script_size;
  2342. code->local.vars = NULL;
  2343. code->local.arrays = NULL;
  2344. return code;
  2345. }
  2346. /// Returns the player attached to this script, identified by the rid.
  2347. /// If there is no player attached, the script is terminated.
  2348. TBL_PC *script_rid2sd(struct script_state *st)
  2349. {
  2350. TBL_PC *sd=map_id2sd(st->rid);
  2351. if(!sd){
  2352. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2353. script_reportfunc(st);
  2354. script_reportsrc(st);
  2355. st->state = END;
  2356. }
  2357. return sd;
  2358. }
  2359. /**
  2360. * Dereferences a variable/constant, replacing it with a copy of the value.
  2361. * @param st Script state
  2362. * @param data Variable/constant
  2363. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2364. */
  2365. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2366. {
  2367. const char* name;
  2368. char prefix;
  2369. char postfix;
  2370. if( !data_isreference(data) )
  2371. return;// not a variable/constant
  2372. name = reference_getname(data);
  2373. prefix = name[0];
  2374. postfix = name[strlen(name) - 1];
  2375. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2376. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2377. if (sd == NULL)
  2378. sd = script_rid2sd(st);
  2379. if( sd == NULL ) {// needs player attached
  2380. if( postfix == '$' ) {// string variable
  2381. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2382. data->type = C_CONSTSTR;
  2383. data->u.str = "";
  2384. } else {// integer variable
  2385. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2386. data->type = C_INT;
  2387. data->u.num = 0;
  2388. }
  2389. return;
  2390. }
  2391. }
  2392. if( postfix == '$' ) {// string variable
  2393. switch( prefix ) {
  2394. case '@':
  2395. data->u.str = pc_readregstr(sd, data->u.num);
  2396. break;
  2397. case '$':
  2398. data->u.str = mapreg_readregstr(data->u.num);
  2399. break;
  2400. case '#':
  2401. if( name[1] == '#' )
  2402. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2403. else
  2404. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2405. break;
  2406. case '.':
  2407. {
  2408. struct DBMap* n = data->ref ?
  2409. data->ref->vars : name[1] == '@' ?
  2410. st->stack->scope.vars : // instance/scope variable
  2411. st->script->local.vars; // npc variable
  2412. if( n )
  2413. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2414. else
  2415. data->u.str = NULL;
  2416. }
  2417. break;
  2418. case '\'':
  2419. {
  2420. unsigned short instance_id = script_instancegetid(st);
  2421. if( instance_id )
  2422. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2423. else {
  2424. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2425. data->u.str = NULL;
  2426. }
  2427. }
  2428. break;
  2429. default:
  2430. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2431. break;
  2432. }
  2433. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2434. data->type = C_CONSTSTR;
  2435. data->u.str = "";
  2436. } else {// duplicate string
  2437. data->type = C_STR;
  2438. data->u.str = aStrdup(data->u.str);
  2439. }
  2440. } else {// integer variable
  2441. data->type = C_INT;
  2442. if( reference_toconstant(data) ) {
  2443. data->u.num = reference_getconstant(data);
  2444. } else if( reference_toparam(data) ) {
  2445. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2446. } else
  2447. switch( prefix ) {
  2448. case '@':
  2449. data->u.num = pc_readreg(sd, data->u.num);
  2450. break;
  2451. case '$':
  2452. data->u.num = mapreg_readreg(data->u.num);
  2453. break;
  2454. case '#':
  2455. if( name[1] == '#' )
  2456. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2457. else
  2458. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2459. break;
  2460. case '.':
  2461. {
  2462. struct DBMap* n = data->ref ?
  2463. data->ref->vars : name[1] == '@' ?
  2464. st->stack->scope.vars : // instance/scope variable
  2465. st->script->local.vars; // npc variable
  2466. if( n )
  2467. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2468. else
  2469. data->u.num = 0;
  2470. }
  2471. break;
  2472. case '\'':
  2473. {
  2474. unsigned short instance_id = script_instancegetid(st);
  2475. if( instance_id )
  2476. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2477. else {
  2478. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2479. data->u.num = 0;
  2480. }
  2481. }
  2482. break;
  2483. default:
  2484. data->u.num = pc_readglobalreg(sd, data->u.num);
  2485. break;
  2486. }
  2487. }
  2488. data->ref = NULL;
  2489. return;
  2490. }
  2491. void get_val(struct script_state* st, struct script_data* data)
  2492. {
  2493. get_val_(st,data,NULL);
  2494. }
  2495. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2496. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2497. /// The value is left in the top of the stack and needs to be removed manually.
  2498. /// @param st[in]: script state.
  2499. /// @param uid[in]: reference identifier.
  2500. /// @param ref[in]: the container to look up the reference into.
  2501. /// @return: the retrieved value of the reference.
  2502. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2503. {
  2504. struct script_data* data;
  2505. push_val2(st->stack, C_NAME, uid, ref);
  2506. data = script_getdatatop(st, -1);
  2507. get_val(st, data);
  2508. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2509. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2510. }
  2511. /**
  2512. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2513. * Will be gone as soon as undefined var feature is implemented
  2514. **/
  2515. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2516. {
  2517. const char *name = get_str(script_getvarid(uid));
  2518. // is here st can be null pointer and st->rid is wrong?
  2519. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2520. bool insert = false;
  2521. if (sd && !st) {
  2522. // when sd comes, st isn't available
  2523. insert = true;
  2524. } else {
  2525. if( is_string_variable(name) ) {
  2526. char* str = (char*)get_val2(st, uid, ref);
  2527. if( str && *str )
  2528. insert = true;
  2529. script_removetop(st, -1, 0);
  2530. } else {
  2531. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2532. if( num )
  2533. insert = true;
  2534. script_removetop(st, -1, 0);
  2535. }
  2536. }
  2537. if (src && src->arrays) {
  2538. struct script_array *sa = idb_get(src->arrays, script_getvarid(uid));
  2539. if (sa) {
  2540. unsigned int i;
  2541. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2542. if( i != sa->size ) {
  2543. if( !insert )
  2544. script_array_remove_member(src,sa,i);
  2545. return;
  2546. }
  2547. script_array_add_member(sa,0);
  2548. } else if (insert) {
  2549. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2550. }
  2551. }
  2552. }
  2553. /**
  2554. * Returns array size by ID
  2555. **/
  2556. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2557. {
  2558. struct script_array *sa = NULL;
  2559. struct reg_db *src = script_array_src(st, sd, name, ref);
  2560. if (src && src->arrays)
  2561. sa = idb_get(src->arrays, search_str(name));
  2562. return sa ? sa->size : 0;
  2563. }
  2564. /**
  2565. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2566. **/
  2567. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2568. {
  2569. struct script_array *sa = NULL;
  2570. struct reg_db *src = script_array_src(st, sd, name, ref);
  2571. if (src && src->arrays) {
  2572. int key = add_word(name);
  2573. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2574. if( ( sa = idb_get(src->arrays, key) ) ) {
  2575. unsigned int i, highest_key = 0;
  2576. for(i = 0; i < sa->size; i++) {
  2577. if( sa->members[i] > highest_key )
  2578. highest_key = sa->members[i];
  2579. }
  2580. return sa->size ? highest_key + 1 : 0;
  2581. }
  2582. }
  2583. return SCRIPT_CMD_SUCCESS;
  2584. }
  2585. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2586. {
  2587. struct script_array *sa = db_data2ptr(data);
  2588. aFree(sa->members);
  2589. ers_free(array_ers, sa);
  2590. return SCRIPT_CMD_SUCCESS;
  2591. }
  2592. /**
  2593. * Clears script_array and removes it from script->array_db
  2594. **/
  2595. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2596. {
  2597. aFree(sa->members);
  2598. idb_remove(src->arrays, sa->id);
  2599. ers_free(array_ers, sa);
  2600. }
  2601. /**
  2602. * Removes a member from a script_array list
  2603. *
  2604. * @param idx the index of the member in script_array struct list, not of the actual array member
  2605. **/
  2606. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2607. {
  2608. unsigned int i, cursor;
  2609. // it's the only member left, no need to do anything other than delete the array data
  2610. if( sa->size == 1 ) {
  2611. script_array_delete(src,sa);
  2612. return;
  2613. }
  2614. sa->members[idx] = UINT_MAX;
  2615. for(i = 0, cursor = 0; i < sa->size; i++) {
  2616. if( sa->members[i] == UINT_MAX )
  2617. continue;
  2618. if( i != cursor )
  2619. sa->members[cursor] = sa->members[i];
  2620. cursor++;
  2621. }
  2622. sa->size = cursor;
  2623. }
  2624. /**
  2625. * Appends a new array index to the list in script_array
  2626. *
  2627. * @param idx the index of the array member being inserted
  2628. **/
  2629. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2630. {
  2631. RECREATE(sa->members, unsigned int, ++sa->size);
  2632. sa->members[sa->size - 1] = idx;
  2633. }
  2634. /**
  2635. * Obtains the source of the array database for this type and scenario
  2636. * Initializes such database when not yet initialized.
  2637. **/
  2638. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2639. {
  2640. struct reg_db *src = NULL;
  2641. switch( name[0] ) {
  2642. // from player
  2643. default: // char reg
  2644. case '@': // temp char reg
  2645. case '#': // account reg
  2646. src = &sd->regs;
  2647. break;
  2648. case '$': // map reg
  2649. src = &regs;
  2650. break;
  2651. case '.': // npc/script
  2652. if (ref)
  2653. src = ref;
  2654. else
  2655. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2656. break;
  2657. case '\'': // instance
  2658. {
  2659. unsigned short instance_id = script_instancegetid(st);
  2660. if( instance_id ) {
  2661. src = &instance_data[instance_id].regs;
  2662. }
  2663. break;
  2664. }
  2665. }
  2666. if( src ) {
  2667. if( !src->arrays )
  2668. src->arrays = idb_alloc(DB_OPT_BASE);
  2669. return src;
  2670. }
  2671. return NULL;
  2672. }
  2673. /**
  2674. * Processes a array member modification, and update data accordingly
  2675. *
  2676. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2677. * @param num[in] Variable ID
  2678. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2679. **/
  2680. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2681. {
  2682. struct script_array *sa = NULL;
  2683. int id = script_getvarid(num);
  2684. unsigned int index = script_getvaridx(num);
  2685. if (!src->arrays) {
  2686. src->arrays = idb_alloc(DB_OPT_BASE);
  2687. } else {
  2688. sa = idb_get(src->arrays, id);
  2689. }
  2690. if( sa ) {
  2691. unsigned int i;
  2692. // search
  2693. for(i = 0; i < sa->size; i++) {
  2694. if( sa->members[i] == index )
  2695. break;
  2696. }
  2697. // if existent
  2698. if( i != sa->size ) {
  2699. // if empty, we gotta remove it
  2700. if( empty ) {
  2701. script_array_remove_member(src, sa, i);
  2702. }
  2703. } else if( !empty ) { /* new entry */
  2704. script_array_add_member(sa,index);
  2705. // we do nothing if its empty, no point in modifying array data for a new empty member
  2706. }
  2707. } else if ( !empty ) { // we only move to create if not empty
  2708. sa = ers_alloc(array_ers, struct script_array);
  2709. sa->id = id;
  2710. sa->members = NULL;
  2711. sa->size = 0;
  2712. script_array_add_member(sa,index);
  2713. idb_put(src->arrays, id, sa);
  2714. }
  2715. }
  2716. /**
  2717. * Stores the value of a script variable
  2718. *
  2719. * @param st: current script state.
  2720. * @param sd: current character data.
  2721. * @param num: variable identifier.
  2722. * @param name: variable name.
  2723. * @param value: new value.
  2724. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2725. * @return: 0 failure, 1 success.
  2726. *
  2727. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2728. *------------------------------------------*/
  2729. int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2730. {
  2731. char prefix = name[0];
  2732. if( is_string_variable(name) ) {// string variable
  2733. const char *str = (const char*)value;
  2734. switch (prefix) {
  2735. case '@':
  2736. pc_setregstr(sd, num, str);
  2737. return 1;
  2738. case '$':
  2739. return mapreg_setregstr(num, str);
  2740. case '#':
  2741. return (name[1] == '#') ?
  2742. pc_setaccountreg2str(sd, num, str) :
  2743. pc_setaccountregstr(sd, num, str);
  2744. case '.':
  2745. {
  2746. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2747. if( n ) {
  2748. if (str[0]) {
  2749. i64db_put(n->vars, num, aStrdup(str));
  2750. if( script_getvaridx(num) )
  2751. script_array_update(n, num, false);
  2752. } else {
  2753. i64db_remove(n->vars, num);
  2754. if( script_getvaridx(num) )
  2755. script_array_update(n, num, true);
  2756. }
  2757. }
  2758. }
  2759. return 1;
  2760. case '\'':
  2761. {
  2762. unsigned short instance_id = script_instancegetid(st);
  2763. if( instance_id ) {
  2764. if( str[0] ) {
  2765. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2766. if( script_getvaridx(num) )
  2767. script_array_update(&instance_data[instance_id].regs, num, false);
  2768. } else {
  2769. i64db_remove(instance_data[instance_id].regs.vars, num);
  2770. if (script_getvaridx(num))
  2771. script_array_update(&instance_data[instance_id].regs, num, true);
  2772. }
  2773. } else {
  2774. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2775. script_reportsrc(st);
  2776. }
  2777. return 1;
  2778. }
  2779. default:
  2780. return pc_setglobalreg_str(sd, num, str);
  2781. }
  2782. } else {// integer variable
  2783. int val = (int)__64BPRTSIZE(value);
  2784. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2785. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2786. if( st != NULL ) {
  2787. ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val);
  2788. script_reportsrc(st);
  2789. st->state = END;
  2790. }
  2791. return 0;
  2792. }
  2793. return 1;
  2794. }
  2795. switch (prefix) {
  2796. case '@':
  2797. pc_setreg(sd, num, val);
  2798. return 1;
  2799. case '$':
  2800. return mapreg_setreg(num, val);
  2801. case '#':
  2802. return (name[1] == '#') ?
  2803. pc_setaccountreg2(sd, num, val) :
  2804. pc_setaccountreg(sd, num, val);
  2805. case '.':
  2806. {
  2807. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2808. if( n ) {
  2809. if( val != 0 ) {
  2810. i64db_iput(n->vars, num, val);
  2811. if( script_getvaridx(num) )
  2812. script_array_update(n, num, false);
  2813. } else {
  2814. i64db_remove(n->vars, num);
  2815. if( script_getvaridx(num) )
  2816. script_array_update(n, num, true);
  2817. }
  2818. }
  2819. }
  2820. return 1;
  2821. case '\'':
  2822. {
  2823. unsigned short instance_id = script_instancegetid(st);
  2824. if( instance_id ) {
  2825. if( val != 0 ) {
  2826. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2827. if( script_getvaridx(num) )
  2828. script_array_update(&instance_data[instance_id].regs, num, false);
  2829. } else {
  2830. i64db_remove(instance_data[instance_id].regs.vars, num);
  2831. if (script_getvaridx(num))
  2832. script_array_update(&instance_data[instance_id].regs, num, true);
  2833. }
  2834. } else {
  2835. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2836. script_reportsrc(st);
  2837. }
  2838. return 1;
  2839. }
  2840. default:
  2841. return pc_setglobalreg(sd, num, val);
  2842. }
  2843. }
  2844. }
  2845. int set_var(struct map_session_data* sd, char* name, void* val)
  2846. {
  2847. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2848. }
  2849. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2850. {
  2851. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2852. }
  2853. /**
  2854. * Converts the data to a string
  2855. * @param st
  2856. * @param data
  2857. * @param sd
  2858. */
  2859. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2860. {
  2861. char* p;
  2862. get_val_(st, data, sd);
  2863. if( data_isstring(data) )
  2864. {// nothing to convert
  2865. }
  2866. else if( data_isint(data) )
  2867. {// int -> string
  2868. CREATE(p, char, ITEM_NAME_LENGTH);
  2869. snprintf(p, ITEM_NAME_LENGTH, "%"PRId64"", data->u.num);
  2870. p[ITEM_NAME_LENGTH-1] = '\0';
  2871. data->type = C_STR;
  2872. data->u.str = p;
  2873. }
  2874. else if( data_isreference(data) )
  2875. {// reference -> string
  2876. //##TODO when does this happen (check get_val) [FlavioJS]
  2877. data->type = C_CONSTSTR;
  2878. data->u.str = reference_getname(data);
  2879. }
  2880. else
  2881. {// unsupported data type
  2882. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2883. script_reportdata(data);
  2884. script_reportsrc(st);
  2885. data->type = C_CONSTSTR;
  2886. data->u.str = "";
  2887. }
  2888. return data->u.str;
  2889. }
  2890. const char* conv_str(struct script_state* st, struct script_data* data)
  2891. {
  2892. return conv_str_(st, data, NULL);
  2893. }
  2894. /**
  2895. * Converts the data to an int
  2896. * @param st
  2897. * @param data
  2898. * @param sd
  2899. */
  2900. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2901. {
  2902. get_val_(st, data, sd);
  2903. if( data_isint(data) )
  2904. {// nothing to convert
  2905. }
  2906. else if( data_isstring(data) )
  2907. {// string -> int
  2908. // the result does not overflow or underflow, it is capped instead
  2909. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2910. char* p = data->u.str;
  2911. long num;
  2912. errno = 0;
  2913. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2914. if( errno == ERANGE
  2915. #if LONG_MAX > INT_MAX
  2916. || num < INT_MIN || num > INT_MAX
  2917. #endif
  2918. )
  2919. {
  2920. if( num <= INT_MIN )
  2921. {
  2922. num = INT_MIN;
  2923. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2924. }
  2925. else//if( num >= INT_MAX )
  2926. {
  2927. num = INT_MAX;
  2928. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2929. }
  2930. script_reportdata(data);
  2931. script_reportsrc(st);
  2932. }
  2933. if( data->type == C_STR )
  2934. aFree(p);
  2935. data->type = C_INT;
  2936. data->u.num = (int)num;
  2937. }
  2938. #if 0
  2939. // FIXME this function is being used to retrieve the position of labels and
  2940. // probably other stuff [FlavioJS]
  2941. else
  2942. {// unsupported data type
  2943. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2944. script_reportdata(data);
  2945. script_reportsrc(st);
  2946. data->type = C_INT;
  2947. data->u.num = 0;
  2948. }
  2949. #endif
  2950. return (int)data->u.num;
  2951. }
  2952. int conv_num(struct script_state* st, struct script_data* data)
  2953. {
  2954. return conv_num_(st, data, NULL);
  2955. }
  2956. //
  2957. // Stack operations
  2958. //
  2959. /// Increases the size of the stack
  2960. void stack_expand(struct script_stack* stack)
  2961. {
  2962. stack->sp_max += 64;
  2963. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2964. stack->sp_max * sizeof(stack->stack_data[0]) );
  2965. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2966. 64 * sizeof(stack->stack_data[0]) );
  2967. }
  2968. /// Pushes a value into the stack
  2969. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2970. /// Pushes a value into the stack (with reference)
  2971. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  2972. {
  2973. if( stack->sp >= stack->sp_max )
  2974. stack_expand(stack);
  2975. stack->stack_data[stack->sp].type = type;
  2976. stack->stack_data[stack->sp].u.num = val;
  2977. stack->stack_data[stack->sp].ref = ref;
  2978. stack->sp++;
  2979. return &stack->stack_data[stack->sp-1];
  2980. }
  2981. /// Pushes a string into the stack
  2982. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2983. {
  2984. if( stack->sp >= stack->sp_max )
  2985. stack_expand(stack);
  2986. stack->stack_data[stack->sp].type = type;
  2987. stack->stack_data[stack->sp].u.str = str;
  2988. stack->stack_data[stack->sp].ref = NULL;
  2989. stack->sp++;
  2990. return &stack->stack_data[stack->sp-1];
  2991. }
  2992. /// Pushes a retinfo into the stack
  2993. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  2994. {
  2995. if( stack->sp >= stack->sp_max )
  2996. stack_expand(stack);
  2997. stack->stack_data[stack->sp].type = C_RETINFO;
  2998. stack->stack_data[stack->sp].u.ri = ri;
  2999. stack->stack_data[stack->sp].ref = ref;
  3000. stack->sp++;
  3001. return &stack->stack_data[stack->sp-1];
  3002. }
  3003. /// Pushes a copy of the target position into the stack
  3004. struct script_data* push_copy(struct script_stack* stack, int pos)
  3005. {
  3006. switch( stack->stack_data[pos].type ) {
  3007. case C_CONSTSTR:
  3008. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  3009. break;
  3010. case C_STR:
  3011. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  3012. break;
  3013. case C_RETINFO:
  3014. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  3015. exit(1);
  3016. break;
  3017. default:
  3018. return push_val2(
  3019. stack,stack->stack_data[pos].type,
  3020. stack->stack_data[pos].u.num,
  3021. stack->stack_data[pos].ref
  3022. );
  3023. break;
  3024. }
  3025. }
  3026. /// Removes the values in indexes [start,end] from the stack.
  3027. /// Adjusts all stack pointers.
  3028. void pop_stack(struct script_state* st, int start, int end)
  3029. {
  3030. struct script_stack* stack = st->stack;
  3031. struct script_data* data;
  3032. int i;
  3033. if( start < 0 )
  3034. start = 0;
  3035. if( end > stack->sp )
  3036. end = stack->sp;
  3037. if( start >= end )
  3038. return;// nothing to pop
  3039. // free stack elements
  3040. for( i = start; i < end; i++ )
  3041. {
  3042. data = &stack->stack_data[i];
  3043. if( data->type == C_STR )
  3044. aFree(data->u.str);
  3045. if( data->type == C_RETINFO ) {
  3046. struct script_retinfo* ri = data->u.ri;
  3047. if (ri->scope.vars) {
  3048. script_free_vars(ri->scope.vars);
  3049. ri->scope.vars = NULL;
  3050. }
  3051. if (ri->scope.arrays) {
  3052. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3053. ri->scope.arrays = NULL;
  3054. }
  3055. if( data->ref )
  3056. aFree(data->ref);
  3057. aFree(ri);
  3058. }
  3059. data->type = C_NOP;
  3060. }
  3061. // move the rest of the elements
  3062. if( stack->sp > end )
  3063. {
  3064. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3065. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3066. stack->stack_data[i].type = C_NOP;
  3067. }
  3068. // adjust stack pointers
  3069. if( st->start > end ) st->start -= end - start;
  3070. else if( st->start > start ) st->start = start;
  3071. if( st->end > end ) st->end -= end - start;
  3072. else if( st->end > start ) st->end = start;
  3073. if( stack->defsp > end ) stack->defsp -= end - start;
  3074. else if( stack->defsp > start ) stack->defsp = start;
  3075. stack->sp -= end - start;
  3076. }
  3077. ///
  3078. ///
  3079. ///
  3080. /*==========================================
  3081. * Release script dependent variable, dependent variable of function
  3082. *------------------------------------------*/
  3083. void script_free_vars(struct DBMap* storage)
  3084. {
  3085. if( storage ) {
  3086. // destroy the storage construct containing the variables
  3087. db_destroy(storage);
  3088. }
  3089. }
  3090. void script_free_code(struct script_code* code)
  3091. {
  3092. nullpo_retv(code);
  3093. if (code->instances)
  3094. script_stop_instances(code);
  3095. script_free_vars(code->local.vars);
  3096. if (code->local.arrays)
  3097. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3098. aFree(code->script_buf);
  3099. aFree(code);
  3100. }
  3101. /// Creates a new script state.
  3102. ///
  3103. /// @param script Script code
  3104. /// @param pos Position in the code
  3105. /// @param rid Who is running the script (attached player)
  3106. /// @param oid Where the code is being run (npc 'object')
  3107. /// @return Script state
  3108. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3109. {
  3110. struct script_state* st;
  3111. st = ers_alloc(st_ers, struct script_state);
  3112. st->stack = ers_alloc(stack_ers, struct script_stack);
  3113. st->stack->sp = 0;
  3114. st->stack->sp_max = 64;
  3115. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3116. st->stack->defsp = st->stack->sp;
  3117. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3118. st->stack->scope.arrays = NULL;
  3119. st->state = RUN;
  3120. st->script = rootscript;
  3121. st->pos = pos;
  3122. st->rid = rid;
  3123. st->oid = oid;
  3124. st->sleep.timer = INVALID_TIMER;
  3125. st->npc_item_flag = battle_config.item_enabled_npc;
  3126. if( st->script->instances != USHRT_MAX )
  3127. st->script->instances++;
  3128. else {
  3129. struct npc_data *nd = map_id2nd(oid);
  3130. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3131. }
  3132. if (!st->script->local.vars)
  3133. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3134. st->id = next_id++;
  3135. active_scripts++;
  3136. idb_put(st_db, st->id, st);
  3137. return st;
  3138. }
  3139. /// Frees a script state.
  3140. ///
  3141. /// @param st Script state
  3142. void script_free_state(struct script_state* st)
  3143. {
  3144. if (idb_exists(st_db, st->id)) {
  3145. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3146. if (st->bk_st) // backup was not restored
  3147. 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);
  3148. if (sd && sd->st == st) { // Current script is aborted.
  3149. if(sd->state.using_fake_npc) {
  3150. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3151. sd->state.using_fake_npc = 0;
  3152. }
  3153. sd->st = NULL;
  3154. sd->npc_id = 0;
  3155. }
  3156. if (st->sleep.timer != INVALID_TIMER)
  3157. delete_timer(st->sleep.timer, run_script_timer);
  3158. if (st->stack) {
  3159. script_free_vars(st->stack->scope.vars);
  3160. if (st->stack->scope.arrays)
  3161. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3162. pop_stack(st, 0, st->stack->sp);
  3163. aFree(st->stack->stack_data);
  3164. ers_free(stack_ers, st->stack);
  3165. st->stack = NULL;
  3166. }
  3167. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3168. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3169. script_free_vars(st->script->local.vars);
  3170. st->script->local.vars = NULL;
  3171. }
  3172. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3173. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3174. st->script->local.arrays = NULL;
  3175. }
  3176. }
  3177. st->pos = -1;
  3178. idb_remove(st_db, st->id);
  3179. ers_free(st_ers, st);
  3180. if (--active_scripts == 0)
  3181. next_id = 0;
  3182. }
  3183. }
  3184. //
  3185. // Main execution unit
  3186. //
  3187. /*==========================================
  3188. * Read command
  3189. *------------------------------------------*/
  3190. c_op get_com(unsigned char *script,int *pos)
  3191. {
  3192. int i = 0, j = 0;
  3193. if(script[*pos]>=0x80){
  3194. return C_INT;
  3195. }
  3196. while(script[*pos]>=0x40){
  3197. i=script[(*pos)++]<<j;
  3198. j+=6;
  3199. }
  3200. return (c_op)(i+(script[(*pos)++]<<j));
  3201. }
  3202. /*==========================================
  3203. * Income figures
  3204. *------------------------------------------*/
  3205. int get_num(unsigned char *script,int *pos)
  3206. {
  3207. int i,j;
  3208. i=0; j=0;
  3209. while(script[*pos]>=0xc0){
  3210. i+=(script[(*pos)++]&0x7f)<<j;
  3211. j+=6;
  3212. }
  3213. return i+((script[(*pos)++]&0x7f)<<j);
  3214. }
  3215. /// Ternary operators
  3216. /// test ? if_true : if_false
  3217. void op_3(struct script_state* st, int op)
  3218. {
  3219. struct script_data* data;
  3220. int flag = 0;
  3221. data = script_getdatatop(st, -3);
  3222. get_val(st, data);
  3223. if( data_isstring(data) )
  3224. flag = data->u.str[0];// "" -> false
  3225. else if( data_isint(data) )
  3226. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3227. else
  3228. {
  3229. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3230. script_reportdata(data);
  3231. script_reportsrc(st);
  3232. script_removetop(st, -3, 0);
  3233. script_pushnil(st);
  3234. return;
  3235. }
  3236. if( flag )
  3237. script_pushcopytop(st, -2);
  3238. else
  3239. script_pushcopytop(st, -1);
  3240. script_removetop(st, -4, -1);
  3241. }
  3242. /// Binary string operators
  3243. /// s1 EQ s2 -> i
  3244. /// s1 NE s2 -> i
  3245. /// s1 GT s2 -> i
  3246. /// s1 GE s2 -> i
  3247. /// s1 LT s2 -> i
  3248. /// s1 LE s2 -> i
  3249. /// s1 ADD s2 -> s
  3250. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3251. {
  3252. int a = 0;
  3253. switch(op){
  3254. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3255. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3256. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3257. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3258. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3259. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3260. case C_ADD:
  3261. {
  3262. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3263. strcpy(buf, s1);
  3264. strcat(buf, s2);
  3265. script_pushstr(st, buf);
  3266. return;
  3267. }
  3268. default:
  3269. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3270. script_reportsrc(st);
  3271. script_pushnil(st);
  3272. st->state = END;
  3273. return;
  3274. }
  3275. script_pushint(st,a);
  3276. }
  3277. /// Binary number operators
  3278. /// i OP i -> i
  3279. void op_2num(struct script_state* st, int op, int i1, int i2)
  3280. {
  3281. int ret;
  3282. double ret_double;
  3283. switch( op ) {
  3284. case C_AND: ret = i1 & i2; break;
  3285. case C_OR: ret = i1 | i2; break;
  3286. case C_XOR: ret = i1 ^ i2; break;
  3287. case C_LAND: ret = (i1 && i2); break;
  3288. case C_LOR: ret = (i1 || i2); break;
  3289. case C_EQ: ret = (i1 == i2); break;
  3290. case C_NE: ret = (i1 != i2); break;
  3291. case C_GT: ret = (i1 > i2); break;
  3292. case C_GE: ret = (i1 >= i2); break;
  3293. case C_LT: ret = (i1 < i2); break;
  3294. case C_LE: ret = (i1 <= i2); break;
  3295. case C_R_SHIFT: ret = i1>>i2; break;
  3296. case C_L_SHIFT: ret = i1<<i2; break;
  3297. case C_DIV:
  3298. case C_MOD:
  3299. if( i2 == 0 )
  3300. {
  3301. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3302. script_reportsrc(st);
  3303. script_pushnil(st);
  3304. st->state = END;
  3305. return;
  3306. }
  3307. else if( op == C_DIV )
  3308. ret = i1 / i2;
  3309. else//if( op == C_MOD )
  3310. ret = i1 % i2;
  3311. break;
  3312. default:
  3313. switch( op ) {// operators that can overflow/underflow
  3314. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3315. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3316. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3317. default:
  3318. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3319. script_reportsrc(st);
  3320. script_pushnil(st);
  3321. return;
  3322. }
  3323. if( ret_double < (double)INT_MIN ) {
  3324. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3325. script_reportsrc(st);
  3326. ret = INT_MIN;
  3327. }
  3328. else if( ret_double > (double)INT_MAX ) {
  3329. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3330. script_reportsrc(st);
  3331. ret = INT_MAX;
  3332. }
  3333. }
  3334. script_pushint(st, ret);
  3335. }
  3336. /// Binary operators
  3337. void op_2(struct script_state *st, int op)
  3338. {
  3339. struct script_data* left, leftref;
  3340. struct script_data* right;
  3341. leftref.type = C_NOP;
  3342. left = script_getdatatop(st, -2);
  3343. right = script_getdatatop(st, -1);
  3344. if (st->op2ref) {
  3345. if (data_isreference(left)) {
  3346. leftref = *left;
  3347. }
  3348. st->op2ref = 0;
  3349. }
  3350. get_val(st, left);
  3351. get_val(st, right);
  3352. // automatic conversions
  3353. switch( op )
  3354. {
  3355. case C_ADD:
  3356. if( data_isint(left) && data_isstring(right) )
  3357. {// convert int-string to string-string
  3358. conv_str(st, left);
  3359. }
  3360. else if( data_isstring(left) && data_isint(right) )
  3361. {// convert string-int to string-string
  3362. conv_str(st, right);
  3363. }
  3364. break;
  3365. }
  3366. if( data_isstring(left) && data_isstring(right) )
  3367. {// ss => op_2str
  3368. op_2str(st, op, left->u.str, right->u.str);
  3369. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3370. if (leftref.type != C_NOP)
  3371. {
  3372. if (left->type == C_STR) // don't free C_CONSTSTR
  3373. aFree(left->u.str);
  3374. *left = leftref;
  3375. }
  3376. }
  3377. else if( data_isint(left) && data_isint(right) )
  3378. {// ii => op_2num
  3379. int i1 = (int)left->u.num;
  3380. int i2 = (int)right->u.num;
  3381. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3382. op_2num(st, op, i1, i2);
  3383. if (leftref.type != C_NOP)
  3384. *left = leftref;
  3385. }
  3386. else
  3387. {// invalid argument
  3388. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3389. script_reportdata(left);
  3390. script_reportdata(right);
  3391. script_reportsrc(st);
  3392. script_removetop(st, -2, 0);
  3393. script_pushnil(st);
  3394. st->state = END;
  3395. }
  3396. }
  3397. /// Unary operators
  3398. /// NEG i -> i
  3399. /// NOT i -> i
  3400. /// LNOT i -> i
  3401. void op_1(struct script_state* st, int op)
  3402. {
  3403. struct script_data* data;
  3404. int i1;
  3405. data = script_getdatatop(st, -1);
  3406. get_val(st, data);
  3407. if( !data_isint(data) )
  3408. {// not a number
  3409. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3410. script_reportdata(data);
  3411. script_reportsrc(st);
  3412. script_pushnil(st);
  3413. st->state = END;
  3414. return;
  3415. }
  3416. i1 = (int)data->u.num;
  3417. script_removetop(st, -1, 0);
  3418. switch( op )
  3419. {
  3420. case C_NEG: i1 = -i1; break;
  3421. case C_NOT: i1 = ~i1; break;
  3422. case C_LNOT: i1 = !i1; break;
  3423. default:
  3424. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3425. script_reportsrc(st);
  3426. script_pushnil(st);
  3427. st->state = END;
  3428. return;
  3429. }
  3430. script_pushint(st, i1);
  3431. }
  3432. /// Checks the type of all arguments passed to a built-in function.
  3433. ///
  3434. /// @param st Script state whose stack arguments should be inspected.
  3435. /// @param func Built-in function for which the arguments are intended.
  3436. static void script_check_buildin_argtype(struct script_state* st, int func)
  3437. {
  3438. int idx, invalid = 0;
  3439. for( idx = 2; script_hasdata(st, idx); idx++ )
  3440. {
  3441. struct script_data* data = script_getdata(st, idx);
  3442. script_function* sf = &buildin_func[str_data[func].val];
  3443. char type = sf->arg[idx-2];
  3444. if( type == '?' || type == '*' )
  3445. {// optional argument or unknown number of optional parameters ( no types are after this )
  3446. break;
  3447. }
  3448. else if( type == 0 )
  3449. {// more arguments than necessary ( should not happen, as it is checked before )
  3450. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3451. invalid++;
  3452. break;
  3453. }
  3454. else
  3455. {
  3456. const char* name = NULL;
  3457. if( data_isreference(data) )
  3458. {// get name for variables to determine the type they refer to
  3459. name = reference_getname(data);
  3460. }
  3461. switch( type )
  3462. {
  3463. case 'v':
  3464. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3465. {// variant
  3466. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3467. script_reportdata(data);
  3468. invalid++;
  3469. }
  3470. break;
  3471. case 's':
  3472. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3473. {// string
  3474. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3475. script_reportdata(data);
  3476. invalid++;
  3477. }
  3478. break;
  3479. case 'i':
  3480. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3481. {// int ( params and constants are always int )
  3482. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3483. script_reportdata(data);
  3484. invalid++;
  3485. }
  3486. break;
  3487. case 'r':
  3488. if( !data_isreference(data) )
  3489. {// variables
  3490. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3491. script_reportdata(data);
  3492. invalid++;
  3493. }
  3494. break;
  3495. case 'l':
  3496. if( !data_islabel(data) && !data_isfunclabel(data) )
  3497. {// label
  3498. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3499. script_reportdata(data);
  3500. invalid++;
  3501. }
  3502. break;
  3503. }
  3504. }
  3505. }
  3506. if(invalid)
  3507. {
  3508. ShowDebug("Function: %s\n", get_str(func));
  3509. script_reportsrc(st);
  3510. }
  3511. }
  3512. /// Executes a buildin command.
  3513. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3514. int run_func(struct script_state *st)
  3515. {
  3516. struct script_data* data;
  3517. int i,start_sp,end_sp,func;
  3518. end_sp = st->stack->sp;// position after the last argument
  3519. for( i = end_sp-1; i > 0 ; --i )
  3520. if( st->stack->stack_data[i].type == C_ARG )
  3521. break;
  3522. if( i == 0 ) {
  3523. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3524. st->state = END;
  3525. script_reportsrc(st);
  3526. return 1;
  3527. }
  3528. start_sp = i-1;// C_NAME of the command
  3529. st->start = start_sp;
  3530. st->end = end_sp;
  3531. data = &st->stack->stack_data[st->start];
  3532. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3533. func = (int)data->u.num;
  3534. st->funcname = reference_getname(data);
  3535. } else {
  3536. ShowError("script:run_func: not a buildin command.\n");
  3537. script_reportdata(data);
  3538. script_reportsrc(st);
  3539. st->state = END;
  3540. return 1;
  3541. }
  3542. if( script_config.warn_func_mismatch_argtypes ) {
  3543. script_check_buildin_argtype(st, func);
  3544. }
  3545. if(str_data[func].func) {
  3546. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3547. script_reportsrc(st);
  3548. } else {
  3549. 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));
  3550. script_reportsrc(st);
  3551. st->state = END;
  3552. }
  3553. // Stack's datum are used when re-running functions [Eoe]
  3554. if( st->state == RERUNLINE )
  3555. return 0;
  3556. pop_stack(st, st->start, st->end);
  3557. if( st->state == RETFUNC ) {// return from a user-defined function
  3558. struct script_retinfo* ri;
  3559. int olddefsp = st->stack->defsp;
  3560. int nargs;
  3561. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3562. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3563. {
  3564. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3565. script_reportsrc(st);
  3566. st->state = END;
  3567. return 1;
  3568. }
  3569. script_free_vars(st->stack->scope.vars);
  3570. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3571. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3572. nargs = ri->nargs;
  3573. st->pos = ri->pos;
  3574. st->script = ri->script;
  3575. st->stack->scope.vars = ri->scope.vars;
  3576. st->stack->scope.arrays = ri->scope.arrays;
  3577. st->stack->defsp = ri->defsp;
  3578. memset(ri, 0, sizeof(struct script_retinfo));
  3579. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3580. st->state = GOTO;
  3581. }
  3582. return 0;
  3583. }
  3584. /*==========================================
  3585. * script execution
  3586. *------------------------------------------*/
  3587. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3588. {
  3589. struct script_state *st;
  3590. if( rootscript == NULL || pos < 0 )
  3591. return;
  3592. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3593. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3594. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3595. st = script_alloc_state(rootscript, pos, rid, oid);
  3596. run_script_main(st);
  3597. }
  3598. void script_stop_instances(struct script_code *code) {
  3599. DBIterator *iter;
  3600. struct script_state* st;
  3601. if( !active_scripts )
  3602. return; // Don't even bother.
  3603. iter = db_iterator(st_db);
  3604. for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) {
  3605. if( st->script == code )
  3606. script_free_state(st);
  3607. }
  3608. dbi_destroy(iter);
  3609. }
  3610. /*==========================================
  3611. * Timer function for sleep
  3612. *------------------------------------------*/
  3613. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3614. {
  3615. struct script_state *st = (struct script_state *)data;
  3616. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3617. // If it was a player before going to sleep and there is still a unit attached to the script
  3618. if( id != 0 && st->rid ){
  3619. struct map_session_data *sd = map_id2sd(st->rid);
  3620. // Attached player is offline or another unit type - should not happen
  3621. if( !sd ){
  3622. ShowWarning( "Script sleep timer called by an offline character or non player unit.\n" );
  3623. script_reportsrc(st);
  3624. st->rid = 0;
  3625. st->state = END;
  3626. // Character mismatch. Cancel execution.
  3627. }else if( sd->status.char_id != id ){
  3628. ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id );
  3629. script_reportsrc(st);
  3630. st->rid = 0;
  3631. st->state = END;
  3632. }
  3633. }
  3634. while (node && st->sleep.timer != INVALID_TIMER) {
  3635. if ((int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer) {
  3636. script_erase_sleepdb(node);
  3637. st->sleep.timer = INVALID_TIMER;
  3638. break;
  3639. }
  3640. node = node->next;
  3641. }
  3642. if(st->state != RERUNLINE)
  3643. st->sleep.tick = 0;
  3644. run_script_main(st);
  3645. return 0;
  3646. }
  3647. /**
  3648. * Remove sleep timers from the NPC
  3649. * @param id: NPC ID
  3650. */
  3651. void script_stop_sleeptimers(int id) {
  3652. for (;;) {
  3653. struct script_state *st = (struct script_state *)linkdb_erase(&sleep_db, (void *)__64BPRTSIZE(id));
  3654. if (!st)
  3655. break; // No more sleep timers
  3656. script_free_state(st);
  3657. }
  3658. }
  3659. /**
  3660. * Delete the specified node from sleep_db
  3661. * @param n: Linked list of sleep timers
  3662. */
  3663. struct linkdb_node *script_erase_sleepdb(struct linkdb_node *n) {
  3664. struct linkdb_node *retnode;
  3665. if (!n)
  3666. return NULL;
  3667. if (!n->prev)
  3668. sleep_db = n->next;
  3669. else
  3670. n->prev->next = n->next;
  3671. if (n->next)
  3672. n->next->prev = n->prev;
  3673. retnode = n->next;
  3674. aFree(n);
  3675. return retnode;
  3676. }
  3677. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3678. ///
  3679. /// @param st Script state to detach.
  3680. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3681. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3682. {
  3683. struct map_session_data* sd;
  3684. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3685. sd->st = st->bk_st;
  3686. sd->npc_id = st->bk_npcid;
  3687. sd->state.disable_atcommand_on_npc = 0;
  3688. if(st->bk_st) {
  3689. //Remove tag for removal.
  3690. st->bk_st = NULL;
  3691. st->bk_npcid = 0;
  3692. } else if(dequeue_event) {
  3693. #ifdef SECURE_NPCTIMEOUT
  3694. /**
  3695. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3696. **/
  3697. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3698. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3699. sd->npc_idle_timer = INVALID_TIMER;
  3700. }
  3701. #endif
  3702. npc_event_dequeue(sd);
  3703. }
  3704. }
  3705. else if(st->bk_st)
  3706. {// rid was set to 0, before detaching the script state
  3707. 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);
  3708. script_reportsrc(st->bk_st);
  3709. script_free_state(st->bk_st);
  3710. st->bk_st = NULL;
  3711. }
  3712. }
  3713. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3714. ///
  3715. /// @param st Script state to attach.
  3716. static void script_attach_state(struct script_state* st)
  3717. {
  3718. struct map_session_data* sd;
  3719. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3720. {
  3721. if(st!=sd->st)
  3722. {
  3723. if(st->bk_st)
  3724. {// there is already a backup
  3725. 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);
  3726. }
  3727. st->bk_st = sd->st;
  3728. st->bk_npcid = sd->npc_id;
  3729. }
  3730. sd->st = st;
  3731. sd->npc_id = st->oid;
  3732. sd->npc_item_flag = st->npc_item_flag; // load default.
  3733. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3734. #ifdef SECURE_NPCTIMEOUT
  3735. if( sd->npc_idle_timer == INVALID_TIMER )
  3736. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3737. sd->npc_idle_tick = gettick();
  3738. #endif
  3739. }
  3740. }
  3741. /*==========================================
  3742. * The main part of the script execution
  3743. *------------------------------------------*/
  3744. void run_script_main(struct script_state *st)
  3745. {
  3746. int cmdcount = script_config.check_cmdcount;
  3747. int gotocount = script_config.check_gotocount;
  3748. TBL_PC *sd;
  3749. struct script_stack *stack = st->stack;
  3750. script_attach_state(st);
  3751. if(st->state == RERUNLINE) {
  3752. run_func(st);
  3753. if(st->state == GOTO)
  3754. st->state = RUN;
  3755. } else if(st->state != END)
  3756. st->state = RUN;
  3757. while(st->state == RUN) {
  3758. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3759. switch(c){
  3760. case C_EOL:
  3761. if( stack->defsp > stack->sp )
  3762. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3763. else
  3764. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3765. break;
  3766. case C_INT:
  3767. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3768. break;
  3769. case C_POS:
  3770. case C_NAME:
  3771. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3772. st->pos+=3;
  3773. break;
  3774. case C_ARG:
  3775. push_val(stack,c,0);
  3776. break;
  3777. case C_STR:
  3778. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3779. while(st->script->script_buf[st->pos++]);
  3780. break;
  3781. case C_FUNC:
  3782. run_func(st);
  3783. if(st->state==GOTO){
  3784. st->state = RUN;
  3785. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3786. ShowError("script:run_script_main: infinity loop !\n");
  3787. script_reportsrc(st);
  3788. st->state=END;
  3789. }
  3790. }
  3791. break;
  3792. case C_REF:
  3793. st->op2ref = 1;
  3794. break;
  3795. case C_NEG:
  3796. case C_NOT:
  3797. case C_LNOT:
  3798. op_1(st ,c);
  3799. break;
  3800. case C_ADD:
  3801. case C_SUB:
  3802. case C_MUL:
  3803. case C_DIV:
  3804. case C_MOD:
  3805. case C_EQ:
  3806. case C_NE:
  3807. case C_GT:
  3808. case C_GE:
  3809. case C_LT:
  3810. case C_LE:
  3811. case C_AND:
  3812. case C_OR:
  3813. case C_XOR:
  3814. case C_LAND:
  3815. case C_LOR:
  3816. case C_R_SHIFT:
  3817. case C_L_SHIFT:
  3818. op_2(st, c);
  3819. break;
  3820. case C_OP3:
  3821. op_3(st, c);
  3822. break;
  3823. case C_NOP:
  3824. st->state=END;
  3825. break;
  3826. default:
  3827. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3828. st->state=END;
  3829. break;
  3830. }
  3831. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3832. ShowError("script:run_script_main: infinity loop !\n");
  3833. script_reportsrc(st);
  3834. st->state=END;
  3835. }
  3836. }
  3837. if(st->sleep.tick > 0) {
  3838. //Restore previous script
  3839. script_detach_state(st, false);
  3840. //Delay execution
  3841. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3842. st->sleep.charid = sd?sd->status.char_id:0;
  3843. st->sleep.timer = add_timer(gettick() + st->sleep.tick, run_script_timer, st->sleep.charid, (intptr_t)st);
  3844. linkdb_insert(&sleep_db, (void *)__64BPRTSIZE(st->oid), st);
  3845. } else if(st->state != END && st->rid) {
  3846. //Resume later (st is already attached to player).
  3847. if(st->bk_st) {
  3848. ShowWarning("Unable to restore stack! Double continuation!\n");
  3849. //Report BOTH scripts to see if that can help somehow.
  3850. ShowDebug("Previous script (lost):\n");
  3851. script_reportsrc(st->bk_st);
  3852. ShowDebug("Current script:\n");
  3853. script_reportsrc(st);
  3854. script_free_state(st->bk_st);
  3855. st->bk_st = NULL;
  3856. }
  3857. } else {
  3858. //Dispose of script.
  3859. if ((sd = map_id2sd(st->rid))!=NULL)
  3860. { //Restore previous stack and save char.
  3861. if(sd->state.using_fake_npc){
  3862. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3863. sd->state.using_fake_npc = 0;
  3864. }
  3865. //Restore previous script if any.
  3866. script_detach_state(st, true);
  3867. if (sd->vars_dirty)
  3868. intif_saveregistry(sd);
  3869. }
  3870. script_free_state(st);
  3871. }
  3872. }
  3873. int script_config_read(char *cfgName)
  3874. {
  3875. int i;
  3876. char line[1024],w1[1024],w2[1024];
  3877. FILE *fp;
  3878. fp=fopen(cfgName,"r");
  3879. if(fp==NULL){
  3880. ShowError("File not found: %s\n", cfgName);
  3881. return 1;
  3882. }
  3883. while(fgets(line, sizeof(line), fp))
  3884. {
  3885. if(line[0] == '/' && line[1] == '/')
  3886. continue;
  3887. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3888. if(i!=2)
  3889. continue;
  3890. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3891. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3892. }
  3893. else if(strcmpi(w1,"check_cmdcount")==0) {
  3894. script_config.check_cmdcount = config_switch(w2);
  3895. }
  3896. else if(strcmpi(w1,"check_gotocount")==0) {
  3897. script_config.check_gotocount = config_switch(w2);
  3898. }
  3899. else if(strcmpi(w1,"input_min_value")==0) {
  3900. script_config.input_min_value = config_switch(w2);
  3901. }
  3902. else if(strcmpi(w1,"input_max_value")==0) {
  3903. script_config.input_max_value = config_switch(w2);
  3904. }
  3905. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3906. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3907. }
  3908. else if(strcmpi(w1,"import")==0){
  3909. script_config_read(w2);
  3910. }
  3911. else {
  3912. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3913. }
  3914. }
  3915. fclose(fp);
  3916. return 0;
  3917. }
  3918. /**
  3919. * @see DBApply
  3920. */
  3921. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3922. {
  3923. struct script_code *code = db_data2ptr(data);
  3924. if (code)
  3925. script_free_code(code);
  3926. return 0;
  3927. }
  3928. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3929. {
  3930. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3931. if( script )
  3932. {
  3933. int j;
  3934. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  3935. if( j < EQI_MAX ) {
  3936. //Single item autobonus
  3937. current_equip_item_index = sd->equip_index[j];
  3938. current_equip_combo_pos = 0;
  3939. } else {
  3940. //Combo autobonus
  3941. current_equip_item_index = -1;
  3942. current_equip_combo_pos = pos;
  3943. }
  3944. run_script(script,0,sd->bl.id,0);
  3945. }
  3946. }
  3947. void script_add_autobonus(const char *autobonus)
  3948. {
  3949. if( strdb_get(autobonus_db, autobonus) == NULL )
  3950. {
  3951. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3952. if( script )
  3953. strdb_put(autobonus_db, autobonus, script);
  3954. }
  3955. }
  3956. /// resets a temporary character array variable to given value
  3957. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3958. {
  3959. struct script_array *sa = NULL;
  3960. struct reg_db *src = NULL;
  3961. unsigned int i, *list = NULL, size = 0;
  3962. int key;
  3963. key = add_str(varname);
  3964. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  3965. return;
  3966. if( value )
  3967. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  3968. if( !(sa = idb_get(src->arrays, key)) ) /* non-existent array, nothing to empty */
  3969. return;
  3970. size = sa->size;
  3971. list = script_array_cpy_list(sa);
  3972. for(i = 0; i < size; i++) {
  3973. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  3974. }
  3975. }
  3976. /// sets a temporary character array variable element idx to given value
  3977. /// @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.
  3978. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  3979. {
  3980. int key;
  3981. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  3982. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  3983. return;
  3984. }
  3985. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3986. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  3987. if( refcache ) {
  3988. // save to avoid repeated script->add_str calls
  3989. refcache[0] = key;
  3990. }
  3991. }
  3992. /**
  3993. * Clears persistent variables from memory
  3994. **/
  3995. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  3996. {
  3997. struct script_reg_state *src;
  3998. if( data->type != DB_DATA_PTR ) // got no need for those!
  3999. return 0;
  4000. src = db_data2ptr(data);
  4001. if( src->type ) {
  4002. struct script_reg_str *p = (struct script_reg_str *)src;
  4003. if( p->value )
  4004. aFree(p->value);
  4005. ers_free(str_reg_ers,p);
  4006. } else {
  4007. ers_free(num_reg_ers,(struct script_reg_num*)src);
  4008. }
  4009. return 0;
  4010. }
  4011. /**
  4012. * Clears a single persistent variable
  4013. **/
  4014. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  4015. {
  4016. i64db_remove(sd->regs.vars, reg);
  4017. if( data->type ) {
  4018. struct script_reg_str *p = (struct script_reg_str*)data;
  4019. if( p->value )
  4020. aFree(p->value);
  4021. ers_free(str_reg_ers,p);
  4022. } else {
  4023. ers_free(num_reg_ers,(struct script_reg_num*)data);
  4024. }
  4025. }
  4026. unsigned int *script_array_cpy_list(struct script_array *sa)
  4027. {
  4028. if( sa->size > generic_ui_array_size )
  4029. script_generic_ui_array_expand(sa->size);
  4030. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  4031. return generic_ui_array;
  4032. }
  4033. void script_generic_ui_array_expand (unsigned int plus)
  4034. {
  4035. generic_ui_array_size += plus + 100;
  4036. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  4037. }
  4038. int buildin_query_sql_sub(struct script_state *st, Sql *handle);
  4039. #ifdef BETA_THREAD_TEST
  4040. /* used to receive items the queryThread has already processed */
  4041. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  4042. int i, cursor = 0;
  4043. bool allOk = true;
  4044. EnterSpinLock(&queryThreadLock);
  4045. for( i = 0; i < queryThreadData.count; i++ ) {
  4046. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4047. if( !entry->ok ) {
  4048. allOk = false;
  4049. continue;
  4050. }
  4051. run_script_main(entry->st);
  4052. entry->st = NULL;/* empty entries */
  4053. aFree(entry);
  4054. queryThreadData.entry[i] = NULL;
  4055. }
  4056. if( allOk ) {
  4057. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  4058. delete_timer(queryThreadData.timer, queryThread_timer);
  4059. queryThreadData.timer = INVALID_TIMER;
  4060. }
  4061. /* now lets clear the mess. */
  4062. for( i = 0; i < queryThreadData.count; i++ ) {
  4063. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4064. if( entry == NULL )
  4065. continue;/* entry on hold */
  4066. /* move */
  4067. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  4068. cursor++;
  4069. }
  4070. queryThreadData.count = cursor;
  4071. LeaveSpinLock(&queryThreadLock);
  4072. return 0;
  4073. }
  4074. void queryThread_add(struct script_state *st, bool type) {
  4075. int idx = 0;
  4076. struct queryThreadEntry* entry = NULL;
  4077. EnterSpinLock(&queryThreadLock);
  4078. if( queryThreadData.count++ != 0 )
  4079. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  4080. idx = queryThreadData.count-1;
  4081. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  4082. entry = queryThreadData.entry[idx];
  4083. entry->st = st;
  4084. entry->ok = false;
  4085. entry->type = type;
  4086. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  4087. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  4088. }
  4089. LeaveSpinLock(&queryThreadLock);
  4090. /* unlock the queryThread */
  4091. racond_signal(queryThreadCond);
  4092. }
  4093. /* adds a new log to the queue */
  4094. void queryThread_log(char * entry, int length) {
  4095. int idx = logThreadData.count;
  4096. EnterSpinLock(&queryThreadLock);
  4097. if( logThreadData.count++ != 0 )
  4098. RECREATE(logThreadData.entry, char* , logThreadData.count);
  4099. CREATE(logThreadData.entry[idx], char, length + 1 );
  4100. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  4101. LeaveSpinLock(&queryThreadLock);
  4102. /* unlock the queryThread */
  4103. racond_signal(queryThreadCond);
  4104. }
  4105. /* queryThread_main */
  4106. static void *queryThread_main(void *x) {
  4107. Sql *queryThread_handle = Sql_Malloc();
  4108. int i;
  4109. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  4110. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4111. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  4112. Sql_ShowDebug(queryThread_handle);
  4113. Sql_Free(queryThread_handle);
  4114. exit(EXIT_FAILURE);
  4115. }
  4116. if( strlen(default_codepage) > 0 )
  4117. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  4118. Sql_ShowDebug(queryThread_handle);
  4119. if( log_config.sql_logs ) {
  4120. logmysql_handle = Sql_Malloc();
  4121. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  4122. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4123. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  4124. Sql_ShowDebug(logmysql_handle);
  4125. Sql_Free(logmysql_handle);
  4126. exit(EXIT_FAILURE);
  4127. }
  4128. if( strlen(default_codepage) > 0 )
  4129. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  4130. Sql_ShowDebug(logmysql_handle);
  4131. }
  4132. while( 1 ) {
  4133. if(queryThreadTerminate > 0)
  4134. break;
  4135. EnterSpinLock(&queryThreadLock);
  4136. /* mess with queryThreadData within the lock */
  4137. for( i = 0; i < queryThreadData.count; i++ ) {
  4138. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4139. if( entry->ok )
  4140. continue;
  4141. else if ( !entry->st || !entry->st->stack ) {
  4142. entry->ok = true;/* dispose */
  4143. continue;
  4144. }
  4145. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  4146. entry->ok = true;/* we're done with this */
  4147. }
  4148. /* also check for any logs in need to be sent */
  4149. if( log_config.sql_logs ) {
  4150. for( i = 0; i < logThreadData.count; i++ ) {
  4151. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  4152. Sql_ShowDebug(logmysql_handle);
  4153. aFree(logThreadData.entry[i]);
  4154. }
  4155. logThreadData.count = 0;
  4156. }
  4157. LeaveSpinLock(&queryThreadLock);
  4158. ramutex_lock( queryThreadMutex );
  4159. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  4160. ramutex_unlock( queryThreadMutex );
  4161. }
  4162. Sql_Free(queryThread_handle);
  4163. if( log_config.sql_logs ) {
  4164. Sql_Free(logmysql_handle);
  4165. }
  4166. return NULL;
  4167. }
  4168. #endif
  4169. /*==========================================
  4170. * Destructor
  4171. *------------------------------------------*/
  4172. void do_final_script() {
  4173. int i;
  4174. DBIterator *iter;
  4175. struct script_state *st;
  4176. #ifdef DEBUG_HASH
  4177. if (battle_config.etc_log)
  4178. {
  4179. FILE *fp = fopen("hash_dump.txt","wt");
  4180. if(fp) {
  4181. int count[SCRIPT_HASH_SIZE];
  4182. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4183. int n=0;
  4184. int min=INT_MAX,max=0,zero=0;
  4185. double mean=0.0f;
  4186. double median=0.0f;
  4187. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4188. memset(count, 0, sizeof(count));
  4189. fprintf(fp,"num : hash : data_name\n");
  4190. fprintf(fp,"---------------------------------------------------------------\n");
  4191. for(i=LABEL_START; i<str_num; i++) {
  4192. unsigned int h = calc_hash(get_str(i));
  4193. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4194. ++count[h];
  4195. }
  4196. fprintf(fp,"--------------------\n\n");
  4197. memset(count2, 0, sizeof(count2));
  4198. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4199. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4200. if(min > count[i])
  4201. min = count[i]; // minimun count of collision
  4202. if(max < count[i])
  4203. max = count[i]; // maximun count of collision
  4204. if(count[i] == 0)
  4205. zero++;
  4206. ++count2[count[i]];
  4207. }
  4208. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4209. for( i=min; i <= max; ++i ){
  4210. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4211. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4212. }
  4213. for( i=min; i <= max; ++i ){
  4214. n += count2[i];
  4215. if( n*2 >= SCRIPT_HASH_SIZE )
  4216. {
  4217. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4218. median = (i+i+1)/2.0f;
  4219. else
  4220. median = i;
  4221. break;
  4222. }
  4223. }
  4224. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4225. fclose(fp);
  4226. }
  4227. }
  4228. #endif
  4229. mapreg_final();
  4230. db_destroy(scriptlabel_db);
  4231. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4232. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4233. ers_destroy(array_ers);
  4234. if (generic_ui_array)
  4235. aFree(generic_ui_array);
  4236. iter = db_iterator(st_db);
  4237. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4238. script_free_state(st);
  4239. dbi_destroy(iter);
  4240. if (str_data)
  4241. aFree(str_data);
  4242. if (str_buf)
  4243. aFree(str_buf);
  4244. for( i = 0; i < atcmd_binding_count; i++ ) {
  4245. aFree(atcmd_binding[i]);
  4246. }
  4247. if( atcmd_binding_count != 0 )
  4248. aFree(atcmd_binding);
  4249. ers_destroy(st_ers);
  4250. ers_destroy(stack_ers);
  4251. db_destroy(st_db);
  4252. #ifdef BETA_THREAD_TEST
  4253. /* QueryThread */
  4254. InterlockedIncrement(&queryThreadTerminate);
  4255. racond_signal(queryThreadCond);
  4256. rathread_wait(queryThread, NULL);
  4257. // Destroy cond var and mutex.
  4258. racond_destroy( queryThreadCond );
  4259. ramutex_destroy( queryThreadMutex );
  4260. /* Clear missing vars */
  4261. for( i = 0; i < queryThreadData.count; i++ ) {
  4262. aFree(queryThreadData.entry[i]);
  4263. }
  4264. aFree(queryThreadData.entry);
  4265. for( i = 0; i < logThreadData.count; i++ ) {
  4266. aFree(logThreadData.entry[i]);
  4267. }
  4268. aFree(logThreadData.entry);
  4269. #endif
  4270. }
  4271. /*==========================================
  4272. * Initialization
  4273. *------------------------------------------*/
  4274. void do_init_script(void) {
  4275. st_db = idb_alloc(DB_OPT_BASE);
  4276. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4277. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4278. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4279. st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_CACHE_OPTIONS);
  4280. stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_FLEX_CHUNK);
  4281. 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
  4282. ers_chunk_size(st_ers, 10);
  4283. ers_chunk_size(stack_ers, 10);
  4284. active_scripts = 0;
  4285. next_id = 0;
  4286. mapreg_init();
  4287. #ifdef BETA_THREAD_TEST
  4288. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4289. queryThreadData.count = 0;
  4290. CREATE(logThreadData.entry, char *, 1);
  4291. logThreadData.count = 0;
  4292. /* QueryThread Start */
  4293. InitializeSpinLock(&queryThreadLock);
  4294. queryThreadData.timer = INVALID_TIMER;
  4295. queryThreadTerminate = 0;
  4296. queryThreadMutex = ramutex_create();
  4297. queryThreadCond = racond_create();
  4298. queryThread = rathread_create(queryThread_main, NULL);
  4299. if(queryThread == NULL){
  4300. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4301. exit(EXIT_FAILURE);
  4302. }
  4303. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4304. #endif
  4305. }
  4306. void script_reload(void) {
  4307. int i;
  4308. DBIterator *iter;
  4309. struct script_state *st;
  4310. #ifdef BETA_THREAD_TEST
  4311. /* we're reloading so any queries undergoing should be...exterminated. */
  4312. EnterSpinLock(&queryThreadLock);
  4313. for( i = 0; i < queryThreadData.count; i++ ) {
  4314. aFree(queryThreadData.entry[i]);
  4315. }
  4316. queryThreadData.count = 0;
  4317. if( queryThreadData.timer != INVALID_TIMER ) {
  4318. delete_timer(queryThreadData.timer, queryThread_timer);
  4319. queryThreadData.timer = INVALID_TIMER;
  4320. }
  4321. LeaveSpinLock(&queryThreadLock);
  4322. #endif
  4323. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4324. db_clear(scriptlabel_db);
  4325. // @commands (script based)
  4326. // Clear bindings
  4327. for( i = 0; i < atcmd_binding_count; i++ ) {
  4328. aFree(atcmd_binding[i]);
  4329. }
  4330. if( atcmd_binding_count != 0 )
  4331. aFree(atcmd_binding);
  4332. atcmd_binding_count = 0;
  4333. iter = db_iterator(st_db);
  4334. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4335. script_free_state(st);
  4336. dbi_destroy(iter);
  4337. db_clear(st_db);
  4338. mapreg_reload();
  4339. }
  4340. //-----------------------------------------------------------------------------
  4341. // buildin functions
  4342. //
  4343. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4344. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4345. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4346. /////////////////////////////////////////////////////////////////////
  4347. // NPC interaction
  4348. //
  4349. /// Appends a message to the npc dialog.
  4350. /// If a dialog doesn't exist yet, one is created.
  4351. ///
  4352. /// mes "<message>";
  4353. BUILDIN_FUNC(mes)
  4354. {
  4355. TBL_PC* sd = script_rid2sd(st);
  4356. if( sd == NULL )
  4357. return SCRIPT_CMD_SUCCESS;
  4358. if( !script_hasdata(st, 3) )
  4359. {// only a single line detected in the script
  4360. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4361. }
  4362. else
  4363. {// parse multiple lines as they exist
  4364. int i;
  4365. for( i = 2; script_hasdata(st, i); i++ )
  4366. {
  4367. // send the message to the client
  4368. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4369. }
  4370. }
  4371. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4372. return SCRIPT_CMD_SUCCESS;
  4373. }
  4374. /// Displays the button 'next' in the npc dialog.
  4375. /// The dialog text is cleared and the script continues when the button is pressed.
  4376. ///
  4377. /// next;
  4378. BUILDIN_FUNC(next)
  4379. {
  4380. TBL_PC* sd;
  4381. sd = script_rid2sd(st);
  4382. if( sd == NULL )
  4383. return SCRIPT_CMD_SUCCESS;
  4384. #ifdef SECURE_NPCTIMEOUT
  4385. sd->npc_idle_type = NPCT_WAIT;
  4386. #endif
  4387. st->state = STOP;
  4388. clif_scriptnext(sd, st->oid);
  4389. return SCRIPT_CMD_SUCCESS;
  4390. }
  4391. /// Ends the script and displays the button 'close' on the npc dialog.
  4392. /// The dialog is closed when the button is pressed.
  4393. ///
  4394. /// close;
  4395. BUILDIN_FUNC(close)
  4396. {
  4397. TBL_PC* sd;
  4398. sd = script_rid2sd(st);
  4399. if( sd == NULL )
  4400. return SCRIPT_CMD_SUCCESS;
  4401. if( !st->mes_active ) {
  4402. TBL_NPC* nd = map_id2nd(st->oid);
  4403. st->state = END; // Keep backwards compatibility.
  4404. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4405. } else {
  4406. st->state = CLOSE;
  4407. st->mes_active = 0;
  4408. }
  4409. clif_scriptclose(sd, st->oid);
  4410. return SCRIPT_CMD_SUCCESS;
  4411. }
  4412. /// Displays the button 'close' on the npc dialog.
  4413. /// The dialog is closed and the script continues when the button is pressed.
  4414. ///
  4415. /// close2;
  4416. BUILDIN_FUNC(close2)
  4417. {
  4418. TBL_PC* sd;
  4419. sd = script_rid2sd(st);
  4420. if( sd == NULL )
  4421. return SCRIPT_CMD_SUCCESS;
  4422. st->state = STOP;
  4423. if( st->mes_active )
  4424. st->mes_active = 0;
  4425. clif_scriptclose(sd, st->oid);
  4426. return SCRIPT_CMD_SUCCESS;
  4427. }
  4428. /// Counts the number of valid and total number of options in 'str'
  4429. /// If max_count > 0 the counting stops when that valid option is reached
  4430. /// total is incremented for each option (NULL is supported)
  4431. static int menu_countoptions(const char* str, int max_count, int* total)
  4432. {
  4433. int count = 0;
  4434. int bogus_total;
  4435. if( total == NULL )
  4436. total = &bogus_total;
  4437. ++(*total);
  4438. // initial empty options
  4439. while( *str == ':' )
  4440. {
  4441. ++str;
  4442. ++(*total);
  4443. }
  4444. // count menu options
  4445. while( *str != '\0' )
  4446. {
  4447. ++count;
  4448. --max_count;
  4449. if( max_count == 0 )
  4450. break;
  4451. while( *str != ':' && *str != '\0' )
  4452. ++str;
  4453. while( *str == ':' )
  4454. {
  4455. ++str;
  4456. ++(*total);
  4457. }
  4458. }
  4459. return count;
  4460. }
  4461. /// Displays a menu with options and goes to the target label.
  4462. /// The script is stopped if cancel is pressed.
  4463. /// Options with no text are not displayed in the client.
  4464. ///
  4465. /// Options can be grouped together, separated by the character ':' in the text:
  4466. /// ex: menu "A:B:C",L_target;
  4467. /// All these options go to the specified target label.
  4468. ///
  4469. /// The index of the selected option is put in the variable @menu.
  4470. /// Indexes start with 1 and are consistent with grouped and empty options.
  4471. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4472. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4473. ///
  4474. /// NOTE: the client closes the npc dialog when cancel is pressed
  4475. ///
  4476. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4477. BUILDIN_FUNC(menu)
  4478. {
  4479. int i;
  4480. const char* text;
  4481. TBL_PC* sd;
  4482. sd = script_rid2sd(st);
  4483. if( sd == NULL )
  4484. return SCRIPT_CMD_SUCCESS;
  4485. #ifdef SECURE_NPCTIMEOUT
  4486. sd->npc_idle_type = NPCT_MENU;
  4487. #endif
  4488. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4489. if( sd->state.menu_or_input == 0 )
  4490. {
  4491. struct StringBuf buf;
  4492. if( script_lastdata(st) % 2 == 0 )
  4493. {// argument count is not even (1st argument is at index 2)
  4494. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4495. st->state = END;
  4496. return SCRIPT_CMD_FAILURE;
  4497. }
  4498. StringBuf_Init(&buf);
  4499. sd->npc_menu = 0;
  4500. for( i = 2; i < script_lastdata(st); i += 2 )
  4501. {
  4502. struct script_data* data;
  4503. // menu options
  4504. text = script_getstr(st, i);
  4505. // target label
  4506. data = script_getdata(st, i+1);
  4507. if( !data_islabel(data) )
  4508. {// not a label
  4509. StringBuf_Destroy(&buf);
  4510. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4511. script_reportdata(data);
  4512. st->state = END;
  4513. return SCRIPT_CMD_FAILURE;
  4514. }
  4515. // append option(s)
  4516. if( text[0] == '\0' )
  4517. continue;// empty string, ignore
  4518. if( sd->npc_menu > 0 )
  4519. StringBuf_AppendStr(&buf, ":");
  4520. StringBuf_AppendStr(&buf, text);
  4521. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4522. }
  4523. st->state = RERUNLINE;
  4524. sd->state.menu_or_input = 1;
  4525. /**
  4526. * menus beyond this length crash the client (see bugreport:6402)
  4527. **/
  4528. if( StringBuf_Length(&buf) >= 2047 ) {
  4529. struct npc_data * nd = map_id2nd(st->oid);
  4530. char* menu;
  4531. CREATE(menu, char, 2048);
  4532. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4533. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4534. clif_scriptmenu(sd, st->oid, menu);
  4535. aFree(menu);
  4536. } else
  4537. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4538. StringBuf_Destroy(&buf);
  4539. if( sd->npc_menu >= 0xff )
  4540. {// 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
  4541. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4542. script_reportsrc(st);
  4543. }
  4544. }
  4545. else if( sd->npc_menu == 0xff )
  4546. {// Cancel was pressed
  4547. sd->state.menu_or_input = 0;
  4548. st->state = END;
  4549. }
  4550. else
  4551. {// goto target label
  4552. int menu = 0;
  4553. sd->state.menu_or_input = 0;
  4554. if( sd->npc_menu <= 0 )
  4555. {
  4556. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4557. st->state = END;
  4558. return SCRIPT_CMD_FAILURE;
  4559. }
  4560. // get target label
  4561. for( i = 2; i < script_lastdata(st); i += 2 )
  4562. {
  4563. text = script_getstr(st, i);
  4564. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4565. if( sd->npc_menu <= 0 )
  4566. break;// entry found
  4567. }
  4568. if( sd->npc_menu > 0 )
  4569. {// Invalid selection
  4570. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4571. st->state = END;
  4572. return SCRIPT_CMD_FAILURE;
  4573. }
  4574. if( !data_islabel(script_getdata(st, i + 1)) )
  4575. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4576. ShowError("buildin_menu: Unexpected data in label argument\n");
  4577. script_reportdata(script_getdata(st, i + 1));
  4578. st->state = END;
  4579. return SCRIPT_CMD_FAILURE;
  4580. }
  4581. pc_setreg(sd, add_str("@menu"), menu);
  4582. st->pos = script_getnum(st, i + 1);
  4583. st->state = GOTO;
  4584. }
  4585. return SCRIPT_CMD_SUCCESS;
  4586. }
  4587. /// Displays a menu with options and returns the selected option.
  4588. /// Behaves like 'menu' without the target labels.
  4589. ///
  4590. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4591. ///
  4592. /// @see menu
  4593. BUILDIN_FUNC(select)
  4594. {
  4595. int i;
  4596. const char* text;
  4597. TBL_PC* sd;
  4598. sd = script_rid2sd(st);
  4599. if( sd == NULL )
  4600. return SCRIPT_CMD_SUCCESS;
  4601. #ifdef SECURE_NPCTIMEOUT
  4602. sd->npc_idle_type = NPCT_MENU;
  4603. #endif
  4604. if( sd->state.menu_or_input == 0 ) {
  4605. struct StringBuf buf;
  4606. StringBuf_Init(&buf);
  4607. sd->npc_menu = 0;
  4608. for( i = 2; i <= script_lastdata(st); ++i ) {
  4609. text = script_getstr(st, i);
  4610. if( sd->npc_menu > 0 )
  4611. StringBuf_AppendStr(&buf, ":");
  4612. StringBuf_AppendStr(&buf, text);
  4613. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4614. }
  4615. st->state = RERUNLINE;
  4616. sd->state.menu_or_input = 1;
  4617. /**
  4618. * menus beyond this length crash the client (see bugreport:6402)
  4619. **/
  4620. if( StringBuf_Length(&buf) >= 2047 ) {
  4621. struct npc_data * nd = map_id2nd(st->oid);
  4622. char* menu;
  4623. CREATE(menu, char, 2048);
  4624. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4625. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4626. clif_scriptmenu(sd, st->oid, menu);
  4627. aFree(menu);
  4628. } else
  4629. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4630. StringBuf_Destroy(&buf);
  4631. if( sd->npc_menu >= 0xff ) {
  4632. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4633. script_reportsrc(st);
  4634. }
  4635. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4636. sd->state.menu_or_input = 0;
  4637. st->state = END;
  4638. } else {// return selected option
  4639. int menu = 0;
  4640. sd->state.menu_or_input = 0;
  4641. for( i = 2; i <= script_lastdata(st); ++i ) {
  4642. text = script_getstr(st, i);
  4643. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4644. if( sd->npc_menu <= 0 )
  4645. break;// entry found
  4646. }
  4647. pc_setreg(sd, add_str("@menu"), menu);
  4648. script_pushint(st, menu);
  4649. st->state = RUN;
  4650. }
  4651. return SCRIPT_CMD_SUCCESS;
  4652. }
  4653. /// Displays a menu with options and returns the selected option.
  4654. /// Behaves like 'menu' without the target labels, except when cancel is
  4655. /// pressed.
  4656. /// When cancel is pressed, the script continues and 255 is returned.
  4657. ///
  4658. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4659. ///
  4660. /// @see menu
  4661. BUILDIN_FUNC(prompt)
  4662. {
  4663. int i;
  4664. const char *text;
  4665. TBL_PC* sd;
  4666. sd = script_rid2sd(st);
  4667. if( sd == NULL )
  4668. return SCRIPT_CMD_SUCCESS;
  4669. #ifdef SECURE_NPCTIMEOUT
  4670. sd->npc_idle_type = NPCT_MENU;
  4671. #endif
  4672. if( sd->state.menu_or_input == 0 )
  4673. {
  4674. struct StringBuf buf;
  4675. StringBuf_Init(&buf);
  4676. sd->npc_menu = 0;
  4677. for( i = 2; i <= script_lastdata(st); ++i )
  4678. {
  4679. text = script_getstr(st, i);
  4680. if( sd->npc_menu > 0 )
  4681. StringBuf_AppendStr(&buf, ":");
  4682. StringBuf_AppendStr(&buf, text);
  4683. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4684. }
  4685. st->state = RERUNLINE;
  4686. sd->state.menu_or_input = 1;
  4687. /**
  4688. * menus beyond this length crash the client (see bugreport:6402)
  4689. **/
  4690. if( StringBuf_Length(&buf) >= 2047 ) {
  4691. struct npc_data * nd = map_id2nd(st->oid);
  4692. char* menu;
  4693. CREATE(menu, char, 2048);
  4694. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4695. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4696. clif_scriptmenu(sd, st->oid, menu);
  4697. aFree(menu);
  4698. } else
  4699. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4700. StringBuf_Destroy(&buf);
  4701. if( sd->npc_menu >= 0xff )
  4702. {
  4703. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4704. script_reportsrc(st);
  4705. }
  4706. }
  4707. else if( sd->npc_menu == 0xff )
  4708. {// Cancel was pressed
  4709. sd->state.menu_or_input = 0;
  4710. pc_setreg(sd, add_str("@menu"), 0xff);
  4711. script_pushint(st, 0xff);
  4712. st->state = RUN;
  4713. }
  4714. else
  4715. {// return selected option
  4716. int menu = 0;
  4717. sd->state.menu_or_input = 0;
  4718. for( i = 2; i <= script_lastdata(st); ++i )
  4719. {
  4720. text = script_getstr(st, i);
  4721. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4722. if( sd->npc_menu <= 0 )
  4723. break;// entry found
  4724. }
  4725. pc_setreg(sd, add_str("@menu"), menu);
  4726. script_pushint(st, menu);
  4727. st->state = RUN;
  4728. }
  4729. return SCRIPT_CMD_SUCCESS;
  4730. }
  4731. /////////////////////////////////////////////////////////////////////
  4732. // ...
  4733. //
  4734. /// Jumps to the target script label.
  4735. ///
  4736. /// goto <label>;
  4737. BUILDIN_FUNC(goto)
  4738. {
  4739. if( !data_islabel(script_getdata(st,2)) )
  4740. {
  4741. ShowError("buildin_goto: Not a label\n");
  4742. script_reportdata(script_getdata(st,2));
  4743. st->state = END;
  4744. return SCRIPT_CMD_FAILURE;
  4745. }
  4746. st->pos = script_getnum(st,2);
  4747. st->state = GOTO;
  4748. return SCRIPT_CMD_SUCCESS;
  4749. }
  4750. /*==========================================
  4751. * user-defined function call
  4752. *------------------------------------------*/
  4753. BUILDIN_FUNC(callfunc)
  4754. {
  4755. int i, j;
  4756. struct script_retinfo* ri;
  4757. struct script_code* scr;
  4758. const char* str = script_getstr(st,2);
  4759. struct reg_db *ref = NULL;
  4760. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4761. if(!scr) {
  4762. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4763. st->state = END;
  4764. return SCRIPT_CMD_FAILURE;
  4765. }
  4766. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4767. ref[0].vars = st->stack->scope.vars;
  4768. if (!st->stack->scope.arrays)
  4769. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4770. ref[0].arrays = st->stack->scope.arrays;
  4771. ref[1].vars = st->script->local.vars;
  4772. if (!st->script->local.arrays)
  4773. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4774. ref[1].arrays = st->script->local.arrays;
  4775. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4776. struct script_data* data = push_copy(st->stack,i);
  4777. if (data_isreference(data) && !data->ref) {
  4778. const char* name = reference_getname(data);
  4779. if (name[0] == '.')
  4780. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4781. }
  4782. }
  4783. CREATE(ri, struct script_retinfo, 1);
  4784. ri->script = st->script; // script code
  4785. ri->scope.vars = st->stack->scope.vars; // scope variables
  4786. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4787. ri->pos = st->pos; // script location
  4788. ri->nargs = j; // argument count
  4789. ri->defsp = st->stack->defsp; // default stack pointer
  4790. push_retinfo(st->stack, ri, ref);
  4791. st->pos = 0;
  4792. st->script = scr;
  4793. st->stack->defsp = st->stack->sp;
  4794. st->state = GOTO;
  4795. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4796. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4797. if (!st->script->local.vars)
  4798. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4799. return SCRIPT_CMD_SUCCESS;
  4800. }
  4801. /*==========================================
  4802. * subroutine call
  4803. *------------------------------------------*/
  4804. BUILDIN_FUNC(callsub)
  4805. {
  4806. int i,j;
  4807. struct script_retinfo* ri;
  4808. int pos = script_getnum(st,2);
  4809. struct reg_db *ref = NULL;
  4810. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4811. ShowError("buildin_callsub: Argument is not a label\n");
  4812. script_reportdata(script_getdata(st,2));
  4813. st->state = END;
  4814. return SCRIPT_CMD_FAILURE;
  4815. }
  4816. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4817. ref[0].vars = st->stack->scope.vars;
  4818. if (!st->stack->scope.arrays)
  4819. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4820. ref[0].arrays = st->stack->scope.arrays;
  4821. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4822. struct script_data* data = push_copy(st->stack,i);
  4823. if (data_isreference(data) && !data->ref) {
  4824. const char* name = reference_getname(data);
  4825. if (name[0] == '.' && name[1] == '@')
  4826. data->ref = &ref[0];
  4827. }
  4828. }
  4829. CREATE(ri, struct script_retinfo, 1);
  4830. ri->script = st->script; // script code
  4831. ri->scope.vars = st->stack->scope.vars; // scope variables
  4832. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4833. ri->pos = st->pos; // script location
  4834. ri->nargs = j; // argument count
  4835. ri->defsp = st->stack->defsp; // default stack pointer
  4836. push_retinfo(st->stack, ri, ref);
  4837. st->pos = pos;
  4838. st->stack->defsp = st->stack->sp;
  4839. st->state = GOTO;
  4840. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4841. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4842. return SCRIPT_CMD_SUCCESS;
  4843. }
  4844. /// Retrieves an argument provided to callfunc/callsub.
  4845. /// If the argument doesn't exist
  4846. ///
  4847. /// getarg(<index>{,<default_value>}) -> <value>
  4848. BUILDIN_FUNC(getarg)
  4849. {
  4850. struct script_retinfo* ri;
  4851. int idx;
  4852. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4853. {
  4854. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4855. st->state = END;
  4856. return SCRIPT_CMD_FAILURE;
  4857. }
  4858. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4859. idx = script_getnum(st,2);
  4860. if( idx >= 0 && idx < ri->nargs )
  4861. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4862. else if( script_hasdata(st,3) )
  4863. script_pushcopy(st, 3);
  4864. else
  4865. {
  4866. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4867. st->state = END;
  4868. return SCRIPT_CMD_FAILURE;
  4869. }
  4870. return SCRIPT_CMD_SUCCESS;
  4871. }
  4872. /// Returns from the current function, optionaly returning a value from the functions.
  4873. /// Don't use outside script functions.
  4874. ///
  4875. /// return;
  4876. /// return <value>;
  4877. BUILDIN_FUNC(return)
  4878. {
  4879. if( script_hasdata(st,2) )
  4880. {// return value
  4881. struct script_data* data;
  4882. script_pushcopy(st, 2);
  4883. data = script_getdatatop(st, -1);
  4884. if( data_isreference(data) ) {
  4885. const char* name = reference_getname(data);
  4886. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4887. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4888. get_val(st, data); // current scope, convert to value
  4889. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4890. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4891. } else if( name[0] == '.' && !data->ref ) { // script variable, link to current script
  4892. data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4893. data->ref->vars = st->script->local.vars;
  4894. if (!st->script->local.arrays)
  4895. st->script->local.arrays = idb_alloc(DB_OPT_BASE);
  4896. data->ref->arrays = st->script->local.arrays;
  4897. }
  4898. }
  4899. }
  4900. else
  4901. {// no return value
  4902. script_pushnil(st);
  4903. }
  4904. st->state = RETFUNC;
  4905. return SCRIPT_CMD_SUCCESS;
  4906. }
  4907. /// Returns a random number from 0 to <range>-1.
  4908. /// Or returns a random number from <min> to <max>.
  4909. /// If <min> is greater than <max>, their numbers are switched.
  4910. /// rand(<range>) -> <int>
  4911. /// rand(<min>,<max>) -> <int>
  4912. BUILDIN_FUNC(rand)
  4913. {
  4914. int range;
  4915. int min;
  4916. if( script_hasdata(st,3) )
  4917. {// min,max
  4918. int max = script_getnum(st,3);
  4919. min = script_getnum(st,2);
  4920. if( max < min )
  4921. swap(min, max);
  4922. range = max - min + 1;
  4923. }
  4924. else
  4925. {// range
  4926. min = 0;
  4927. range = script_getnum(st,2);
  4928. }
  4929. if( range <= 1 )
  4930. script_pushint(st, min);
  4931. else
  4932. script_pushint(st, rnd()%range + min);
  4933. return SCRIPT_CMD_SUCCESS;
  4934. }
  4935. /*==========================================
  4936. * Warp sd to str,x,y or Random or SavePoint/Save
  4937. *------------------------------------------*/
  4938. BUILDIN_FUNC(warp)
  4939. {
  4940. int ret;
  4941. int x,y;
  4942. const char* str;
  4943. struct map_session_data* sd;
  4944. if(!script_charid2sd(5, sd))
  4945. return SCRIPT_CMD_SUCCESS;
  4946. str = script_getstr(st,2);
  4947. x = script_getnum(st,3);
  4948. y = script_getnum(st,4);
  4949. if(strcmp(str,"Random")==0)
  4950. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4951. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4952. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4953. else
  4954. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4955. if( ret ) {
  4956. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4957. return SCRIPT_CMD_FAILURE;
  4958. }
  4959. return SCRIPT_CMD_SUCCESS;
  4960. }
  4961. /*==========================================
  4962. * Warp a specified area
  4963. *------------------------------------------*/
  4964. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4965. {
  4966. int x2,y2,x3,y3;
  4967. unsigned int index;
  4968. index = va_arg(ap,unsigned int);
  4969. x2 = va_arg(ap,int);
  4970. y2 = va_arg(ap,int);
  4971. x3 = va_arg(ap,int);
  4972. y3 = va_arg(ap,int);
  4973. if(index == 0)
  4974. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4975. else if(x3 && y3) {
  4976. int max, tx, ty, j = 0;
  4977. int16 m;
  4978. m = map_mapindex2mapid(index);
  4979. // choose a suitable max number of attempts
  4980. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4981. max = 1000;
  4982. // find a suitable map cell
  4983. do {
  4984. tx = rnd()%(x3-x2+1)+x2;
  4985. ty = rnd()%(y3-y2+1)+y2;
  4986. j++;
  4987. } while( map_getcell(m,tx,ty,CELL_CHKNOPASS) && j < max );
  4988. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4989. }
  4990. else
  4991. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4992. return 0;
  4993. }
  4994. BUILDIN_FUNC(areawarp)
  4995. {
  4996. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4997. unsigned int index;
  4998. const char *str;
  4999. const char *mapname;
  5000. mapname = script_getstr(st,2);
  5001. x0 = script_getnum(st,3);
  5002. y0 = script_getnum(st,4);
  5003. x1 = script_getnum(st,5);
  5004. y1 = script_getnum(st,6);
  5005. str = script_getstr(st,7);
  5006. x2 = script_getnum(st,8);
  5007. y2 = script_getnum(st,9);
  5008. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  5009. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  5010. x3 = 0;
  5011. y3 = 0;
  5012. } else if( x3 && y3 ) {
  5013. // normalize x3/y3 coordinates
  5014. if( x3 < x2 ) swap(x3,x2);
  5015. if( y3 < y2 ) swap(y3,y2);
  5016. }
  5017. }
  5018. if( (m = map_mapname2mapid(mapname)) < 0 )
  5019. return SCRIPT_CMD_FAILURE;
  5020. if( strcmp(str,"Random") == 0 )
  5021. index = 0;
  5022. else if( !(index=mapindex_name2id(str)) )
  5023. return SCRIPT_CMD_FAILURE;
  5024. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  5025. return SCRIPT_CMD_SUCCESS;
  5026. }
  5027. /*==========================================
  5028. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  5029. *------------------------------------------*/
  5030. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  5031. {
  5032. int hp, sp;
  5033. hp = va_arg(ap, int);
  5034. sp = va_arg(ap, int);
  5035. pc_percentheal((TBL_PC *)bl,hp,sp);
  5036. return 0;
  5037. }
  5038. BUILDIN_FUNC(areapercentheal)
  5039. {
  5040. int hp,sp,m;
  5041. const char *mapname;
  5042. int x0,y0,x1,y1;
  5043. mapname=script_getstr(st,2);
  5044. x0=script_getnum(st,3);
  5045. y0=script_getnum(st,4);
  5046. x1=script_getnum(st,5);
  5047. y1=script_getnum(st,6);
  5048. hp=script_getnum(st,7);
  5049. sp=script_getnum(st,8);
  5050. if( (m=map_mapname2mapid(mapname))< 0)
  5051. return SCRIPT_CMD_FAILURE;
  5052. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  5053. return SCRIPT_CMD_SUCCESS;
  5054. }
  5055. /*==========================================
  5056. * Warpparty - [Fredzilla] [Paradox924X]
  5057. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  5058. * If 'from_mapname' is specified, only the party members on that map will be warped
  5059. *------------------------------------------*/
  5060. BUILDIN_FUNC(warpparty)
  5061. {
  5062. TBL_PC *sd = NULL;
  5063. TBL_PC *pl_sd;
  5064. struct party_data* p;
  5065. int type;
  5066. int mapindex = 0, m = -1;
  5067. int i;
  5068. const char* str = script_getstr(st,2);
  5069. int x = script_getnum(st,3);
  5070. int y = script_getnum(st,4);
  5071. int p_id = script_getnum(st,5);
  5072. const char* str2 = NULL;
  5073. if ( script_hasdata(st,6) )
  5074. str2 = script_getstr(st,6);
  5075. p = party_search(p_id);
  5076. if(!p)
  5077. return SCRIPT_CMD_SUCCESS;
  5078. type = ( strcmp(str,"Random")==0 ) ? 0
  5079. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5080. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5081. : ( strcmp(str,"Leader")==0 ) ? 3
  5082. : 4;
  5083. switch (type)
  5084. {
  5085. case 3:
  5086. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5087. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5088. return SCRIPT_CMD_FAILURE;
  5089. pl_sd = p->data[i].sd;
  5090. mapindex = pl_sd->mapindex;
  5091. m = map_mapindex2mapid(mapindex);
  5092. x = pl_sd->bl.x;
  5093. y = pl_sd->bl.y;
  5094. break;
  5095. case 4:
  5096. mapindex = mapindex_name2id(str);
  5097. if (!mapindex) {// Invalid map
  5098. return SCRIPT_CMD_FAILURE;
  5099. }
  5100. m = map_mapindex2mapid(mapindex);
  5101. break;
  5102. case 2:
  5103. //"SavePoint" uses save point of the currently attached player
  5104. if (( sd = script_rid2sd(st) ) == NULL )
  5105. return SCRIPT_CMD_SUCCESS;
  5106. break;
  5107. }
  5108. for (i = 0; i < MAX_PARTY; i++)
  5109. {
  5110. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5111. continue;
  5112. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  5113. continue;
  5114. if( pc_isdead(pl_sd) )
  5115. continue;
  5116. switch( type )
  5117. {
  5118. case 0: // Random
  5119. if(!map[pl_sd->bl.m].flag.nowarp)
  5120. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5121. break;
  5122. case 1: // SavePointAll
  5123. if(!map[pl_sd->bl.m].flag.noreturn)
  5124. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5125. break;
  5126. case 2: // SavePoint
  5127. if(!map[pl_sd->bl.m].flag.noreturn)
  5128. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5129. break;
  5130. case 3: // Leader
  5131. case 4: // m,x,y
  5132. 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))
  5133. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5134. break;
  5135. }
  5136. }
  5137. return SCRIPT_CMD_SUCCESS;
  5138. }
  5139. /*==========================================
  5140. * Warpguild - [Fredzilla]
  5141. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5142. *------------------------------------------*/
  5143. BUILDIN_FUNC(warpguild)
  5144. {
  5145. TBL_PC *sd = NULL;
  5146. TBL_PC *pl_sd;
  5147. struct guild* g;
  5148. struct s_mapiterator* iter;
  5149. int type, mapindex = 0, m = -1;
  5150. const char* str = script_getstr(st,2);
  5151. int x = script_getnum(st,3);
  5152. int y = script_getnum(st,4);
  5153. int gid = script_getnum(st,5);
  5154. g = guild_search(gid);
  5155. if( g == NULL )
  5156. return SCRIPT_CMD_SUCCESS;
  5157. type = ( strcmp(str,"Random")==0 ) ? 0
  5158. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5159. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5160. : 3;
  5161. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  5162. {// "SavePoint" uses save point of the currently attached player
  5163. return SCRIPT_CMD_SUCCESS;
  5164. }
  5165. switch (type) {
  5166. case 3:
  5167. mapindex = mapindex_name2id(str);
  5168. if (!mapindex)
  5169. return SCRIPT_CMD_FAILURE;
  5170. m = map_mapindex2mapid(mapindex);
  5171. break;
  5172. }
  5173. iter = mapit_getallusers();
  5174. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5175. {
  5176. if( pl_sd->status.guild_id != gid )
  5177. continue;
  5178. switch( type )
  5179. {
  5180. case 0: // Random
  5181. if(!map[pl_sd->bl.m].flag.nowarp)
  5182. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5183. break;
  5184. case 1: // SavePointAll
  5185. if(!map[pl_sd->bl.m].flag.noreturn)
  5186. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5187. break;
  5188. case 2: // SavePoint
  5189. if(!map[pl_sd->bl.m].flag.noreturn)
  5190. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5191. break;
  5192. case 3: // m,x,y
  5193. 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))
  5194. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5195. break;
  5196. }
  5197. }
  5198. mapit_free(iter);
  5199. return SCRIPT_CMD_SUCCESS;
  5200. }
  5201. /*==========================================
  5202. * Force Heal a player (hp and sp)
  5203. *------------------------------------------*/
  5204. BUILDIN_FUNC(heal)
  5205. {
  5206. TBL_PC *sd;
  5207. int hp,sp;
  5208. if (!script_charid2sd(4,sd))
  5209. return SCRIPT_CMD_SUCCESS;
  5210. hp=script_getnum(st,2);
  5211. sp=script_getnum(st,3);
  5212. status_heal(&sd->bl, hp, sp, 1);
  5213. return SCRIPT_CMD_SUCCESS;
  5214. }
  5215. /*==========================================
  5216. * Heal a player by item (get vit bonus etc)
  5217. *------------------------------------------*/
  5218. BUILDIN_FUNC(itemheal)
  5219. {
  5220. TBL_PC *sd;
  5221. int hp,sp;
  5222. hp=script_getnum(st,2);
  5223. sp=script_getnum(st,3);
  5224. if(potion_flag==1) {
  5225. potion_hp = hp;
  5226. potion_sp = sp;
  5227. return SCRIPT_CMD_SUCCESS;
  5228. }
  5229. if (!script_charid2sd(4,sd))
  5230. return SCRIPT_CMD_SUCCESS;
  5231. pc_itemheal(sd,sd->itemid,hp,sp);
  5232. return SCRIPT_CMD_SUCCESS;
  5233. }
  5234. /*==========================================
  5235. *
  5236. *------------------------------------------*/
  5237. BUILDIN_FUNC(percentheal)
  5238. {
  5239. int hp,sp;
  5240. TBL_PC* sd;
  5241. hp=script_getnum(st,2);
  5242. sp=script_getnum(st,3);
  5243. if(potion_flag==1) {
  5244. potion_per_hp = hp;
  5245. potion_per_sp = sp;
  5246. return SCRIPT_CMD_SUCCESS;
  5247. }
  5248. if (!script_charid2sd(4,sd))
  5249. return SCRIPT_CMD_SUCCESS;
  5250. #ifdef RENEWAL
  5251. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5252. sp = 0;
  5253. #endif
  5254. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5255. hp = 0;
  5256. sp = 0;
  5257. }
  5258. if (sd->sc.data[SC_BITESCAR])
  5259. hp = 0;
  5260. pc_percentheal(sd,hp,sp);
  5261. return SCRIPT_CMD_SUCCESS;
  5262. }
  5263. /*==========================================
  5264. *
  5265. *------------------------------------------*/
  5266. BUILDIN_FUNC(jobchange)
  5267. {
  5268. int job, upper=-1;
  5269. job=script_getnum(st,2);
  5270. if( script_hasdata(st,3) )
  5271. upper=script_getnum(st,3);
  5272. if (pcdb_checkid(job))
  5273. {
  5274. TBL_PC* sd;
  5275. if (!script_charid2sd(4,sd))
  5276. return SCRIPT_CMD_SUCCESS;
  5277. pc_jobchange(sd, job, upper);
  5278. }
  5279. return SCRIPT_CMD_SUCCESS;
  5280. }
  5281. /*==========================================
  5282. *
  5283. *------------------------------------------*/
  5284. BUILDIN_FUNC(jobname)
  5285. {
  5286. int class_=script_getnum(st,2);
  5287. script_pushconststr(st, (char*)job_name(class_));
  5288. return SCRIPT_CMD_SUCCESS;
  5289. }
  5290. /// Get input from the player.
  5291. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5292. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5293. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5294. /// shorter than 'min' and 0 otherwise.
  5295. ///
  5296. /// input(<var>{,<min>{,<max>}}) -> <int>
  5297. BUILDIN_FUNC(input)
  5298. {
  5299. TBL_PC* sd;
  5300. struct script_data* data;
  5301. int64 uid;
  5302. const char* name;
  5303. int min;
  5304. int max;
  5305. sd = script_rid2sd(st);
  5306. if( sd == NULL )
  5307. return SCRIPT_CMD_SUCCESS;
  5308. data = script_getdata(st,2);
  5309. if( !data_isreference(data) ){
  5310. ShowError("script:input: not a variable\n");
  5311. script_reportdata(data);
  5312. st->state = END;
  5313. return SCRIPT_CMD_FAILURE;
  5314. }
  5315. uid = reference_getuid(data);
  5316. name = reference_getname(data);
  5317. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5318. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5319. #ifdef SECURE_NPCTIMEOUT
  5320. sd->npc_idle_type = NPCT_WAIT;
  5321. #endif
  5322. if( !sd->state.menu_or_input )
  5323. { // first invocation, display npc input box
  5324. sd->state.menu_or_input = 1;
  5325. st->state = RERUNLINE;
  5326. if( is_string_variable(name) )
  5327. clif_scriptinputstr(sd,st->oid);
  5328. else
  5329. clif_scriptinput(sd,st->oid);
  5330. }
  5331. else
  5332. { // take received text/value and store it in the designated variable
  5333. sd->state.menu_or_input = 0;
  5334. if( is_string_variable(name) )
  5335. {
  5336. int len = (int)strlen(sd->npc_str);
  5337. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5338. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5339. }
  5340. else
  5341. {
  5342. int amount = sd->npc_amount;
  5343. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5344. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5345. }
  5346. st->state = RUN;
  5347. }
  5348. return SCRIPT_CMD_SUCCESS;
  5349. }
  5350. // declare the copyarray method here for future reference
  5351. BUILDIN_FUNC(copyarray);
  5352. /// Sets the value of a variable.
  5353. /// The value is converted to the type of the variable.
  5354. ///
  5355. /// set(<variable>,<value>{,<charid>})
  5356. BUILDIN_FUNC(setr)
  5357. {
  5358. TBL_PC* sd = NULL;
  5359. struct script_data* data;
  5360. //struct script_data* datavalue;
  5361. int64 num;
  5362. uint8 pos = 4;
  5363. const char* name, *command = script_getfuncname(st);
  5364. char prefix;
  5365. data = script_getdata(st,2);
  5366. //datavalue = script_getdata(st,3);
  5367. if( !data_isreference(data) )
  5368. {
  5369. ShowError("script:set: not a variable\n");
  5370. script_reportdata(script_getdata(st,2));
  5371. st->state = END;
  5372. return SCRIPT_CMD_FAILURE;
  5373. }
  5374. num = reference_getuid(data);
  5375. name = reference_getname(data);
  5376. prefix = *name;
  5377. if (!strcmp(command, "setr"))
  5378. pos = 5;
  5379. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5380. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5381. return SCRIPT_CMD_FAILURE;
  5382. }
  5383. #if 0
  5384. if( data_isreference(datavalue) )
  5385. {// the value being referenced is a variable
  5386. const char* namevalue = reference_getname(datavalue);
  5387. if( !not_array_variable(*namevalue) )
  5388. {// array variable being copied into another array variable
  5389. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5390. {// player must be attached in order to copy a player variable
  5391. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5392. return SCRIPT_CMD_SUCCESS;
  5393. }
  5394. if( is_string_variable(namevalue) != is_string_variable(name) )
  5395. {// non-matching array value types
  5396. ShowWarning("script:set: two array variables do not match in type.\n");
  5397. return SCRIPT_CMD_SUCCESS;
  5398. }
  5399. // push the maximum number of array values to the stack
  5400. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5401. // call the copy array method directly
  5402. return buildin_copyarray(st);
  5403. }
  5404. }
  5405. #endif
  5406. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5407. if( is_string_variable(name) )
  5408. script_pushstrcopy(st,script_getstr(st, 4));
  5409. else
  5410. script_pushint(st,script_getnum(st, 4));
  5411. } else // Return a copy of the variable reference
  5412. script_pushcopy(st, 2);
  5413. if( is_string_variable(name) )
  5414. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5415. else
  5416. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5417. return SCRIPT_CMD_SUCCESS;
  5418. }
  5419. /////////////////////////////////////////////////////////////////////
  5420. /// Array variables
  5421. ///
  5422. /// Sets values of an array, from the starting index.
  5423. /// ex: setarray arr[1],1,2,3;
  5424. ///
  5425. /// setarray <array variable>,<value1>{,<value2>...};
  5426. BUILDIN_FUNC(setarray)
  5427. {
  5428. struct script_data* data;
  5429. const char* name;
  5430. uint32 start;
  5431. uint32 end;
  5432. int32 id;
  5433. int32 i;
  5434. TBL_PC* sd = NULL;
  5435. data = script_getdata(st, 2);
  5436. if( !data_isreference(data) )
  5437. {
  5438. ShowError("script:setarray: not a variable\n");
  5439. script_reportdata(data);
  5440. st->state = END;
  5441. return SCRIPT_CMD_FAILURE;// not a variable
  5442. }
  5443. id = reference_getid(data);
  5444. start = reference_getindex(data);
  5445. name = reference_getname(data);
  5446. if( not_server_variable(*name) )
  5447. {
  5448. sd = script_rid2sd(st);
  5449. if( sd == NULL )
  5450. return SCRIPT_CMD_SUCCESS;// no player attached
  5451. }
  5452. end = start + script_lastdata(st) - 2;
  5453. if( end > SCRIPT_MAX_ARRAYSIZE )
  5454. end = SCRIPT_MAX_ARRAYSIZE;
  5455. if( is_string_variable(name) )
  5456. {// string array
  5457. for( i = 3; start < end; ++start, ++i )
  5458. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5459. }
  5460. else
  5461. {// int array
  5462. for( i = 3; start < end; ++start, ++i )
  5463. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5464. }
  5465. return SCRIPT_CMD_SUCCESS;
  5466. }
  5467. /// Sets count values of an array, from the starting index.
  5468. /// ex: cleararray arr[0],0,1;
  5469. ///
  5470. /// cleararray <array variable>,<value>,<count>;
  5471. BUILDIN_FUNC(cleararray)
  5472. {
  5473. struct script_data* data;
  5474. const char* name;
  5475. uint32 start;
  5476. uint32 end;
  5477. int32 id;
  5478. void* v;
  5479. TBL_PC* sd = NULL;
  5480. data = script_getdata(st, 2);
  5481. if( !data_isreference(data) )
  5482. {
  5483. ShowError("script:cleararray: not a variable\n");
  5484. script_reportdata(data);
  5485. st->state = END;
  5486. return SCRIPT_CMD_FAILURE;// not a variable
  5487. }
  5488. id = reference_getid(data);
  5489. start = reference_getindex(data);
  5490. name = reference_getname(data);
  5491. if( not_server_variable(*name) )
  5492. {
  5493. sd = script_rid2sd(st);
  5494. if( sd == NULL )
  5495. return SCRIPT_CMD_SUCCESS;// no player attached
  5496. }
  5497. if( is_string_variable(name) )
  5498. v = (void*)script_getstr(st, 3);
  5499. else
  5500. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5501. end = start + script_getnum(st, 4);
  5502. if( end > SCRIPT_MAX_ARRAYSIZE )
  5503. end = SCRIPT_MAX_ARRAYSIZE;
  5504. for( ; start < end; ++start )
  5505. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5506. return SCRIPT_CMD_SUCCESS;
  5507. }
  5508. /// Copies data from one array to another.
  5509. /// ex: copyarray arr[0],arr[2],2;
  5510. ///
  5511. /// copyarray <destination array variable>,<source array variable>,<count>;
  5512. BUILDIN_FUNC(copyarray)
  5513. {
  5514. struct script_data* data1;
  5515. struct script_data* data2;
  5516. const char* name1;
  5517. const char* name2;
  5518. int32 idx1;
  5519. int32 idx2;
  5520. int32 id1;
  5521. int32 id2;
  5522. void* v;
  5523. int32 i;
  5524. uint32 count;
  5525. TBL_PC* sd = NULL;
  5526. data1 = script_getdata(st, 2);
  5527. data2 = script_getdata(st, 3);
  5528. if( !data_isreference(data1) || !data_isreference(data2) )
  5529. {
  5530. ShowError("script:copyarray: not a variable\n");
  5531. script_reportdata(data1);
  5532. script_reportdata(data2);
  5533. st->state = END;
  5534. return SCRIPT_CMD_FAILURE;// not a variable
  5535. }
  5536. id1 = reference_getid(data1);
  5537. id2 = reference_getid(data2);
  5538. idx1 = reference_getindex(data1);
  5539. idx2 = reference_getindex(data2);
  5540. name1 = reference_getname(data1);
  5541. name2 = reference_getname(data2);
  5542. if( is_string_variable(name1) != is_string_variable(name2) )
  5543. {
  5544. ShowError("script:copyarray: type mismatch\n");
  5545. script_reportdata(data1);
  5546. script_reportdata(data2);
  5547. st->state = END;
  5548. return SCRIPT_CMD_FAILURE;// data type mismatch
  5549. }
  5550. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5551. {
  5552. sd = script_rid2sd(st);
  5553. if( sd == NULL )
  5554. return SCRIPT_CMD_SUCCESS;// no player attached
  5555. }
  5556. count = script_getnum(st, 4);
  5557. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5558. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5559. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5560. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5561. if( id1 == id2 && idx1 > idx2 )
  5562. {// destination might be overlapping the source - copy in reverse order
  5563. for( i = count - 1; i >= 0; --i )
  5564. {
  5565. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5566. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5567. script_removetop(st, -1, 0);
  5568. }
  5569. }
  5570. else
  5571. {// normal copy
  5572. for( i = 0; i < count; ++i )
  5573. {
  5574. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5575. {
  5576. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5577. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5578. script_removetop(st, -1, 0);
  5579. }
  5580. else// out of range - assume ""/0
  5581. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5582. }
  5583. }
  5584. return SCRIPT_CMD_SUCCESS;
  5585. }
  5586. /// Returns the size of the array.
  5587. /// Assumes that everything before the starting index exists.
  5588. /// ex: getarraysize(arr[3])
  5589. ///
  5590. /// getarraysize(<array variable>) -> <int>
  5591. BUILDIN_FUNC(getarraysize)
  5592. {
  5593. struct script_data* data;
  5594. const char* name;
  5595. struct map_session_data* sd = NULL;
  5596. data = script_getdata(st, 2);
  5597. if( !data_isreference(data) )
  5598. {
  5599. ShowError("script:getarraysize: not a variable\n");
  5600. script_reportdata(data);
  5601. script_pushnil(st);
  5602. st->state = END;
  5603. return SCRIPT_CMD_FAILURE;// not a variable
  5604. }
  5605. name = reference_getname(data);
  5606. if( not_server_variable(*name) ){
  5607. sd = script_rid2sd(st);
  5608. if (sd == NULL)
  5609. return SCRIPT_CMD_SUCCESS;// no player attached
  5610. }
  5611. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5612. return SCRIPT_CMD_SUCCESS;
  5613. }
  5614. int script_array_index_cmp(const void *a, const void *b) {
  5615. return ( *(unsigned int*)a - *(unsigned int*)b );
  5616. }
  5617. /// Deletes count or all the elements in an array, from the starting index.
  5618. /// ex: deletearray arr[4],2;
  5619. ///
  5620. /// deletearray <array variable>;
  5621. /// deletearray <array variable>,<count>;
  5622. BUILDIN_FUNC(deletearray)
  5623. {
  5624. struct script_data* data;
  5625. const char* name;
  5626. unsigned int start, end, i;
  5627. int id;
  5628. TBL_PC *sd = NULL;
  5629. struct script_array *sa = NULL;
  5630. struct reg_db *src = NULL;
  5631. void *value;
  5632. data = script_getdata(st, 2);
  5633. if( !data_isreference(data) ) {
  5634. ShowError("script:deletearray: not a variable\n");
  5635. script_reportdata(data);
  5636. st->state = END;
  5637. return SCRIPT_CMD_FAILURE;// not a variable
  5638. }
  5639. id = reference_getid(data);
  5640. start = reference_getindex(data);
  5641. name = reference_getname(data);
  5642. if( not_server_variable(*name) ) {
  5643. sd = script_rid2sd(st);
  5644. if( sd == NULL )
  5645. return SCRIPT_CMD_SUCCESS;// no player attached
  5646. }
  5647. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5648. ShowError("script:deletearray: not a array\n");
  5649. script_reportdata(data);
  5650. st->state = END;
  5651. return SCRIPT_CMD_FAILURE;// not a variable
  5652. }
  5653. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5654. if ( !(sa = idb_get(src->arrays, id)) ) { // non-existent array, nothing to empty
  5655. return SCRIPT_CMD_SUCCESS;// not a variable
  5656. }
  5657. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5658. if( start >= end )
  5659. return SCRIPT_CMD_SUCCESS;// nothing to free
  5660. if( is_string_variable(name) )
  5661. value = (void *)"";
  5662. else
  5663. value = (void *)0;
  5664. if( script_hasdata(st,3) ) {
  5665. unsigned int count = script_getnum(st, 3);
  5666. if( count > end - start )
  5667. count = end - start;
  5668. if( count <= 0 )
  5669. return SCRIPT_CMD_SUCCESS;// nothing to free
  5670. if( end - start < sa->size ) {
  5671. // Better to iterate directly on the array, no speed-up from using sa
  5672. for( ; start + count < end; ++start ) {
  5673. // Compact and overwrite
  5674. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5675. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5676. script_removetop(st, -1, 0);
  5677. }
  5678. for( ; start < end; start++ ) {
  5679. // Clean up any leftovers that weren't overwritten
  5680. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5681. }
  5682. } else {
  5683. // using sa to speed up
  5684. unsigned int *list = NULL, size = 0;
  5685. list = script_array_cpy_list(sa);
  5686. size = sa->size;
  5687. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5688. ARR_FIND(0, size, i, list[i] >= start);
  5689. for( ; i < size && list[i] < start + count; i++ ) {
  5690. // Clear any entries between start and start+count, if they exist
  5691. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5692. }
  5693. for( ; i < size && list[i] < end; i++ ) {
  5694. // Move back count positions any entries between start+count to fill the gaps
  5695. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5696. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5697. script_removetop(st, -1, 0);
  5698. // Clear their originals
  5699. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5700. }
  5701. }
  5702. } else {
  5703. unsigned int *list = NULL, size = 0;
  5704. list = script_array_cpy_list(sa);
  5705. size = sa->size;
  5706. for(i = 0; i < size; i++) {
  5707. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5708. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5709. }
  5710. }
  5711. return SCRIPT_CMD_SUCCESS;
  5712. }
  5713. /// Returns a reference to the target index of the array variable.
  5714. /// Equivalent to var[index].
  5715. ///
  5716. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5717. BUILDIN_FUNC(getelementofarray)
  5718. {
  5719. struct script_data* data;
  5720. int32 id;
  5721. int64 i;
  5722. data = script_getdata(st, 2);
  5723. if( !data_isreference(data) )
  5724. {
  5725. ShowError("script:getelementofarray: not a variable\n");
  5726. script_reportdata(data);
  5727. script_pushnil(st);
  5728. st->state = END;
  5729. return SCRIPT_CMD_SUCCESS;// not a variable
  5730. }
  5731. id = reference_getid(data);
  5732. i = script_getnum(st, 3);
  5733. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5734. ShowWarning("script:getelementofarray: index out of range (%"PRId64")\n", i);
  5735. script_reportdata(data);
  5736. script_pushnil(st);
  5737. st->state = END;
  5738. return SCRIPT_CMD_FAILURE;// out of range
  5739. }
  5740. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5741. return SCRIPT_CMD_SUCCESS;
  5742. }
  5743. /////////////////////////////////////////////////////////////////////
  5744. /// ...
  5745. ///
  5746. /*==========================================
  5747. *
  5748. *------------------------------------------*/
  5749. BUILDIN_FUNC(setlook)
  5750. {
  5751. int type,val;
  5752. TBL_PC* sd;
  5753. type = script_getnum(st,2);
  5754. val = script_getnum(st,3);
  5755. if (!script_charid2sd(4,sd))
  5756. return SCRIPT_CMD_SUCCESS;
  5757. pc_changelook(sd,type,val);
  5758. return SCRIPT_CMD_SUCCESS;
  5759. }
  5760. BUILDIN_FUNC(changelook)
  5761. { // As setlook but only client side
  5762. int type,val;
  5763. TBL_PC* sd;
  5764. type = script_getnum(st,2);
  5765. val = script_getnum(st,3);
  5766. if (!script_charid2sd(4,sd))
  5767. return SCRIPT_CMD_SUCCESS;
  5768. clif_changelook(&sd->bl,type,val);
  5769. return SCRIPT_CMD_SUCCESS;
  5770. }
  5771. /*==========================================
  5772. *
  5773. *------------------------------------------*/
  5774. BUILDIN_FUNC(cutin)
  5775. {
  5776. TBL_PC* sd;
  5777. sd = script_rid2sd(st);
  5778. if( sd == NULL )
  5779. return SCRIPT_CMD_SUCCESS;
  5780. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5781. return SCRIPT_CMD_SUCCESS;
  5782. }
  5783. /*==========================================
  5784. *
  5785. *------------------------------------------*/
  5786. BUILDIN_FUNC(viewpoint)
  5787. {
  5788. int type,x,y,id,color;
  5789. TBL_PC* sd;
  5790. type=script_getnum(st,2);
  5791. x=script_getnum(st,3);
  5792. y=script_getnum(st,4);
  5793. id=script_getnum(st,5);
  5794. color=script_getnum(st,6);
  5795. sd = script_rid2sd(st);
  5796. if( sd == NULL )
  5797. return SCRIPT_CMD_SUCCESS;
  5798. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5799. return SCRIPT_CMD_SUCCESS;
  5800. }
  5801. /// Returns number of items in inventory/cart/storage
  5802. /// countitem <nameID>{,<accountID>});
  5803. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5804. /// cartcountitem <nameID>{,<accountID>});
  5805. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5806. /// storagecountitem <nameID>{,<accountID>});
  5807. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5808. /// guildstoragecountitem <nameID>{,<accountID>});
  5809. /// guildstoragecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5810. BUILDIN_FUNC(countitem)
  5811. {
  5812. int i = 0, aid = 3;
  5813. struct item_data* id = NULL;
  5814. struct script_data* data;
  5815. char *command = (char *)script_getfuncname(st);
  5816. uint8 loc = 0;
  5817. uint16 size, count = 0;
  5818. struct item *items;
  5819. TBL_PC *sd = NULL;
  5820. struct s_storage *gstor = NULL;
  5821. if( command[strlen(command)-1] == '2' ) {
  5822. i = 1;
  5823. aid = 10;
  5824. }
  5825. if( script_hasdata(st,aid) ) {
  5826. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5827. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5828. st->state = END;
  5829. return SCRIPT_CMD_FAILURE;
  5830. }
  5831. }
  5832. else {
  5833. if( !(sd = script_rid2sd(st)) )
  5834. return SCRIPT_CMD_SUCCESS;
  5835. }
  5836. if( !strncmp(command, "cart", 4) ) {
  5837. loc = TABLE_CART;
  5838. size = MAX_CART;
  5839. items = sd->cart.u.items_cart;
  5840. }
  5841. else if( !strncmp(command, "storage", 7) ) {
  5842. loc = TABLE_STORAGE;
  5843. size = MAX_STORAGE;
  5844. items = sd->storage.u.items_storage;
  5845. }
  5846. else if( !strncmp(command, "guildstorage", 12) ) {
  5847. gstor = guild2storage2(sd->status.guild_id);
  5848. if (gstor && !sd->state.storage_flag) {
  5849. loc = TABLE_GUILD_STORAGE;
  5850. size = MAX_GUILD_STORAGE;
  5851. items = gstor->u.items_guild;
  5852. } else {
  5853. script_pushint(st, -1);
  5854. return SCRIPT_CMD_SUCCESS;
  5855. }
  5856. }
  5857. else {
  5858. size = MAX_INVENTORY;
  5859. items = sd->inventory.u.items_inventory;
  5860. }
  5861. if( loc == TABLE_CART && !pc_iscarton(sd) ) {
  5862. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5863. script_pushint(st,-1);
  5864. return SCRIPT_CMD_SUCCESS;
  5865. }
  5866. data = script_getdata(st, 2);
  5867. get_val(st, data); // Convert into value in case of a variable
  5868. if( data_isstring(data) ) // item name
  5869. id = itemdb_searchname(conv_str(st, data));
  5870. else // item id
  5871. id = itemdb_exists(conv_num(st, data));
  5872. if( id == NULL ) {
  5873. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5874. script_pushint(st,0);
  5875. return SCRIPT_CMD_FAILURE;
  5876. }
  5877. if (loc == TABLE_GUILD_STORAGE)
  5878. gstor->lock = true;
  5879. if( !i ) { // For count/cart/storagecountitem function
  5880. unsigned short nameid = id->nameid;
  5881. for( i = 0; i < size; i++ )
  5882. if( &items[i] && items[i].nameid == nameid )
  5883. count += items[i].amount;
  5884. }
  5885. else { // For count/cart/storagecountitem2 function
  5886. unsigned short nameid;
  5887. int iden, ref, attr, c1, c2, c3, c4;
  5888. nameid = id->nameid;
  5889. iden = script_getnum(st,3);
  5890. ref = script_getnum(st,4);
  5891. attr = script_getnum(st,5);
  5892. c1 = script_getnum(st,6);
  5893. c2 = script_getnum(st,7);
  5894. c3 = script_getnum(st,8);
  5895. c4 = script_getnum(st,9);
  5896. for( i = 0; i < size; i++ )
  5897. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5898. items[i].amount > 0 && items[i].identify == iden &&
  5899. items[i].refine == ref && items[i].attribute == attr &&
  5900. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5901. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5902. {
  5903. count += items[i].amount;
  5904. }
  5905. }
  5906. if (loc == TABLE_GUILD_STORAGE) {
  5907. storage_guild_storageclose(sd);
  5908. gstor->lock = false;
  5909. }
  5910. script_pushint(st, count);
  5911. return SCRIPT_CMD_SUCCESS;
  5912. }
  5913. /*==========================================
  5914. * Check if item with this amount can fit in inventory
  5915. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  5916. * Return
  5917. * 0 : fail
  5918. * 1 : success (npc side only)
  5919. *------------------------------------------*/
  5920. BUILDIN_FUNC(checkweight)
  5921. {
  5922. int slots = 0;
  5923. unsigned short amount2 = 0;
  5924. unsigned int weight = 0, i, nbargs;
  5925. struct item_data* id = NULL;
  5926. struct map_session_data* sd;
  5927. if( ( sd = script_rid2sd(st) ) == NULL )
  5928. return SCRIPT_CMD_SUCCESS;
  5929. nbargs = script_lastdata(st)+1;
  5930. if(nbargs%2) {
  5931. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5932. script_pushint(st,0);
  5933. return SCRIPT_CMD_FAILURE;
  5934. }
  5935. slots = pc_inventoryblank(sd); //nb of empty slot
  5936. for (i = 2; i < nbargs; i += 2) {
  5937. unsigned short nameid, amount;
  5938. struct script_data* data = script_getdata(st,i);
  5939. get_val(st, data); // Convert into value in case of a variable
  5940. if( data_isstring(data) ) // item name
  5941. id = itemdb_searchname(conv_str(st, data));
  5942. else // item id
  5943. id = itemdb_exists(conv_num(st, data));
  5944. if( id == NULL ) {
  5945. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5946. script_pushint(st,0);
  5947. return SCRIPT_CMD_FAILURE;
  5948. }
  5949. nameid = id->nameid;
  5950. amount = script_getnum(st,i+1);
  5951. if( amount < 1 ) {
  5952. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5953. script_pushint(st,0);
  5954. return SCRIPT_CMD_FAILURE;
  5955. }
  5956. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  5957. if( weight + sd->weight > sd->max_weight )
  5958. {// too heavy
  5959. script_pushint(st,0);
  5960. return SCRIPT_CMD_SUCCESS;
  5961. }
  5962. switch( pc_checkadditem(sd, nameid, amount) ) {
  5963. case CHKADDITEM_EXIST:
  5964. // item is already in inventory, but there is still space for the requested amount
  5965. break;
  5966. case CHKADDITEM_NEW:
  5967. if( itemdb_isstackable(nameid) ) {
  5968. // stackable
  5969. amount2++;
  5970. if( slots < amount2 ) {
  5971. script_pushint(st,0);
  5972. return SCRIPT_CMD_SUCCESS;
  5973. }
  5974. } else {
  5975. // non-stackable
  5976. amount2 += amount;
  5977. if( slots < amount2) {
  5978. script_pushint(st,0);
  5979. return SCRIPT_CMD_SUCCESS;
  5980. }
  5981. }
  5982. break;
  5983. case CHKADDITEM_OVERAMOUNT:
  5984. script_pushint(st,0);
  5985. return SCRIPT_CMD_SUCCESS;
  5986. }
  5987. }
  5988. script_pushint(st,1);
  5989. return SCRIPT_CMD_SUCCESS;
  5990. }
  5991. BUILDIN_FUNC(checkweight2)
  5992. {
  5993. //variable sub checkweight
  5994. int i = 0, slots = 0, weight = 0;
  5995. short fail = 0;
  5996. unsigned short amount2 = 0;
  5997. //variable for array parsing
  5998. struct script_data* data_it;
  5999. struct script_data* data_nb;
  6000. const char* name_it;
  6001. const char* name_nb;
  6002. int32 id_it, id_nb;
  6003. int32 idx_it, idx_nb;
  6004. int nb_it, nb_nb; //array size
  6005. TBL_PC *sd = script_rid2sd(st);
  6006. nullpo_retr(1,sd);
  6007. data_it = script_getdata(st, 2);
  6008. data_nb = script_getdata(st, 3);
  6009. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  6010. ShowError("buildin_checkweight2: parameter not a variable\n");
  6011. script_pushint(st,0);
  6012. return SCRIPT_CMD_FAILURE;// not a variable
  6013. }
  6014. id_it = reference_getid(data_it);
  6015. id_nb = reference_getid(data_nb);
  6016. idx_it = reference_getindex(data_it);
  6017. idx_nb = reference_getindex(data_nb);
  6018. name_it = reference_getname(data_it);
  6019. name_nb = reference_getname(data_nb);
  6020. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  6021. ShowError("buildin_checkweight2: illegal type, need int\n");
  6022. script_pushint(st,0);
  6023. return SCRIPT_CMD_FAILURE;// not supported
  6024. }
  6025. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  6026. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  6027. if(nb_it != nb_nb) {
  6028. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  6029. fail = 1;
  6030. }
  6031. slots = pc_inventoryblank(sd);
  6032. for(i=0; i<nb_it; i++) {
  6033. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  6034. script_removetop(st, -1, 0);
  6035. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  6036. script_removetop(st, -1, 0);
  6037. if(fail)
  6038. continue; //cpntonie to depop rest
  6039. if(itemdb_exists(nameid) == NULL ) {
  6040. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  6041. fail=1;
  6042. continue;
  6043. }
  6044. if(amount < 0 ) {
  6045. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  6046. fail = 1;
  6047. continue;
  6048. }
  6049. weight += itemdb_weight(nameid)*amount;
  6050. if( weight + sd->weight > sd->max_weight ) {
  6051. fail = 1;
  6052. continue;
  6053. }
  6054. switch( pc_checkadditem(sd, nameid, amount) ) {
  6055. case CHKADDITEM_EXIST:
  6056. // item is already in inventory, but there is still space for the requested amount
  6057. break;
  6058. case CHKADDITEM_NEW:
  6059. if( itemdb_isstackable(nameid) ) {// stackable
  6060. amount2++;
  6061. if( slots < amount2 )
  6062. fail = 1;
  6063. } else {// non-stackable
  6064. amount2 += amount;
  6065. if( slots < amount2 ) {
  6066. fail = 1;
  6067. }
  6068. }
  6069. break;
  6070. case CHKADDITEM_OVERAMOUNT:
  6071. fail = 1;
  6072. } //end switch
  6073. } //end loop DO NOT break it prematurly we need to depop all stack
  6074. fail ? script_pushint(st,0) : script_pushint(st,1);
  6075. return SCRIPT_CMD_SUCCESS;
  6076. }
  6077. /*==========================================
  6078. * getitem <item id>,<amount>{,<account ID>};
  6079. * getitem "<item name>",<amount>{,<account ID>};
  6080. *
  6081. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6082. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6083. * Type:
  6084. * 0 - No bound
  6085. * 1 - Account Bound
  6086. * 2 - Guild Bound
  6087. * 3 - Party Bound
  6088. * 4 - Character Bound
  6089. *------------------------------------------*/
  6090. BUILDIN_FUNC(getitem)
  6091. {
  6092. int get_count, i;
  6093. unsigned short nameid, amount;
  6094. struct item it;
  6095. TBL_PC *sd;
  6096. struct script_data *data;
  6097. unsigned char flag = 0;
  6098. const char* command = script_getfuncname(st);
  6099. struct item_data *id = NULL;
  6100. data = script_getdata(st,2);
  6101. get_val(st,data);
  6102. if( data_isstring(data) ) {// "<item name>"
  6103. const char *name = conv_str(st,data);
  6104. id = itemdb_searchname(name);
  6105. if( id == NULL ){
  6106. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6107. return SCRIPT_CMD_FAILURE; //No item created.
  6108. }
  6109. nameid = id->nameid;
  6110. } else if( data_isint(data) ) {// <item id>
  6111. nameid = conv_num(st,data);
  6112. if( !(id = itemdb_exists(nameid)) ){
  6113. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  6114. return SCRIPT_CMD_FAILURE; //No item created.
  6115. }
  6116. } else {
  6117. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  6118. return SCRIPT_CMD_FAILURE;
  6119. }
  6120. // <amount>
  6121. if( (amount = script_getnum(st,3)) <= 0)
  6122. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6123. memset(&it,0,sizeof(it));
  6124. it.nameid = nameid;
  6125. it.identify = 1;
  6126. it.bound = BOUND_NONE;
  6127. if( !strcmp(command,"getitembound") ) {
  6128. char bound = script_getnum(st,4);
  6129. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6130. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6131. return SCRIPT_CMD_FAILURE;
  6132. }
  6133. if( script_hasdata(st,5) )
  6134. sd = map_id2sd(script_getnum(st,5));
  6135. else
  6136. sd = script_rid2sd(st); // Attached player
  6137. it.bound = bound;
  6138. } else if( script_hasdata(st,4) )
  6139. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  6140. else
  6141. sd = script_rid2sd(st); // Attached player
  6142. if( sd == NULL ) // no target
  6143. return SCRIPT_CMD_SUCCESS;
  6144. //Check if it's stackable.
  6145. if (!itemdb_isstackable2(id))
  6146. get_count = 1;
  6147. else
  6148. get_count = amount;
  6149. for (i = 0; i < amount; i += get_count)
  6150. {
  6151. // if not pet egg
  6152. if (!pet_create_egg(sd, nameid))
  6153. {
  6154. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6155. {
  6156. clif_additem(sd, 0, 0, flag);
  6157. if( pc_candrop(sd,&it) )
  6158. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6159. }
  6160. }
  6161. }
  6162. return SCRIPT_CMD_SUCCESS;
  6163. }
  6164. /*==========================================
  6165. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6166. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6167. *
  6168. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6169. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6170. * Type:
  6171. * 0 - No bound
  6172. * 1 - Account Bound
  6173. * 2 - Guild Bound
  6174. * 3 - Party Bound
  6175. * 4 - Character Bound
  6176. *------------------------------------------*/
  6177. BUILDIN_FUNC(getitem2)
  6178. {
  6179. unsigned short nameid, amount;
  6180. int iden, ref, attr;
  6181. unsigned short c1, c2, c3, c4;
  6182. char bound = BOUND_NONE;
  6183. struct item_data *item_data = NULL;
  6184. struct item item_tmp;
  6185. TBL_PC *sd;
  6186. struct script_data *data;
  6187. const char* command = script_getfuncname(st);
  6188. if( !strcmp(command,"getitembound2") ) {
  6189. bound = script_getnum(st,11);
  6190. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6191. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6192. return SCRIPT_CMD_FAILURE;
  6193. }
  6194. if( script_hasdata(st,12) )
  6195. sd = map_id2sd(script_getnum(st,12));
  6196. else
  6197. sd = script_rid2sd(st); // Attached player
  6198. } else if( script_hasdata(st,11) )
  6199. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  6200. else
  6201. sd = script_rid2sd(st); // Attached player
  6202. if( sd == NULL ) // no target
  6203. return SCRIPT_CMD_SUCCESS;
  6204. data = script_getdata(st,2);
  6205. get_val(st,data);
  6206. if( data_isstring(data) ) {
  6207. const char *name = conv_str(st,data);
  6208. if( (item_data = itemdb_searchname(name)) == NULL ){
  6209. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6210. return SCRIPT_CMD_FAILURE; //No item created.
  6211. }
  6212. nameid = item_data->nameid;
  6213. } else {
  6214. nameid = conv_num(st,data);
  6215. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6216. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6217. return SCRIPT_CMD_FAILURE; //No item created.
  6218. }
  6219. }
  6220. amount = script_getnum(st,3);
  6221. iden = script_getnum(st,4);
  6222. ref = script_getnum(st,5);
  6223. attr = script_getnum(st,6);
  6224. c1 = (unsigned short)script_getnum(st,7);
  6225. c2 = (unsigned short)script_getnum(st,8);
  6226. c3 = (unsigned short)script_getnum(st,9);
  6227. c4 = (unsigned short)script_getnum(st,10);
  6228. if( item_data ) {
  6229. int get_count = 0, i;
  6230. memset(&item_tmp,0,sizeof(item_tmp));
  6231. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6232. if(ref > MAX_REFINE)
  6233. ref = MAX_REFINE;
  6234. }
  6235. else if( item_data->type == IT_PETEGG ) {
  6236. iden = 1;
  6237. ref = 0;
  6238. }
  6239. else {
  6240. iden = 1;
  6241. ref = attr = 0;
  6242. }
  6243. item_tmp.nameid = nameid;
  6244. item_tmp.identify = iden;
  6245. item_tmp.refine = ref;
  6246. item_tmp.attribute = attr;
  6247. item_tmp.card[0] = c1;
  6248. item_tmp.card[1] = c2;
  6249. item_tmp.card[2] = c3;
  6250. item_tmp.card[3] = c4;
  6251. item_tmp.bound = bound;
  6252. //Check if it's stackable.
  6253. if (!itemdb_isstackable2(item_data))
  6254. get_count = 1;
  6255. else
  6256. get_count = amount;
  6257. for (i = 0; i < amount; i += get_count)
  6258. {
  6259. // if not pet egg
  6260. if (!pet_create_egg(sd, nameid))
  6261. {
  6262. unsigned char flag = 0;
  6263. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6264. {
  6265. clif_additem(sd, 0, 0, flag);
  6266. if( pc_candrop(sd,&item_tmp) )
  6267. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6268. }
  6269. }
  6270. }
  6271. }
  6272. return SCRIPT_CMD_SUCCESS;
  6273. }
  6274. /** Gives rental item to player
  6275. * rentitem <item id>,<seconds>{,<account_id>}
  6276. * rentitem "<item name>",<seconds>{,<account_id>}
  6277. */
  6278. BUILDIN_FUNC(rentitem) {
  6279. struct map_session_data *sd;
  6280. struct script_data *data;
  6281. struct item it;
  6282. int seconds;
  6283. unsigned short nameid = 0;
  6284. unsigned char flag = 0;
  6285. data = script_getdata(st,2);
  6286. get_val(st,data);
  6287. if (!script_accid2sd(4,sd))
  6288. return SCRIPT_CMD_FAILURE;
  6289. if( data_isstring(data) )
  6290. {
  6291. const char *name = conv_str(st,data);
  6292. struct item_data *itd = itemdb_searchname(name);
  6293. if( itd == NULL )
  6294. {
  6295. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6296. return SCRIPT_CMD_FAILURE;
  6297. }
  6298. nameid = itd->nameid;
  6299. }
  6300. else if( data_isint(data) )
  6301. {
  6302. nameid = conv_num(st,data);
  6303. if( nameid == 0 || !itemdb_exists(nameid) )
  6304. {
  6305. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6306. return SCRIPT_CMD_FAILURE;
  6307. }
  6308. }
  6309. else
  6310. {
  6311. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6312. return SCRIPT_CMD_FAILURE;
  6313. }
  6314. seconds = script_getnum(st,3);
  6315. memset(&it, 0, sizeof(it));
  6316. it.nameid = nameid;
  6317. it.identify = 1;
  6318. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6319. it.bound = BOUND_NONE;
  6320. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6321. {
  6322. clif_additem(sd, 0, 0, flag);
  6323. return SCRIPT_CMD_FAILURE;
  6324. }
  6325. return SCRIPT_CMD_SUCCESS;
  6326. }
  6327. /** Gives rental item to player with advanced option
  6328. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6329. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6330. */
  6331. BUILDIN_FUNC(rentitem2) {
  6332. struct map_session_data *sd;
  6333. struct script_data *data;
  6334. struct item it;
  6335. struct item_data *id;
  6336. int seconds;
  6337. unsigned short nameid = 0;
  6338. unsigned char flag = 0;
  6339. int iden,ref,attr,c1,c2,c3,c4;
  6340. data = script_getdata(st,2);
  6341. get_val(st,data);
  6342. if (!script_accid2sd(11,sd))
  6343. return SCRIPT_CMD_FAILURE;
  6344. if( data_isstring(data) ) {
  6345. const char *name = conv_str(st,data);
  6346. id = itemdb_searchname(name);
  6347. if( id == NULL ) {
  6348. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6349. return SCRIPT_CMD_FAILURE;
  6350. }
  6351. nameid = id->nameid;
  6352. }
  6353. else if( data_isint(data) ) {
  6354. nameid = conv_num(st,data);
  6355. if( !(id = itemdb_search(nameid))) {
  6356. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6357. return SCRIPT_CMD_FAILURE;
  6358. }
  6359. }
  6360. else {
  6361. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6362. return SCRIPT_CMD_FAILURE;
  6363. }
  6364. seconds = script_getnum(st,3);
  6365. iden = script_getnum(st,4);
  6366. ref = script_getnum(st,5);
  6367. attr = script_getnum(st,6);
  6368. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6369. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6370. }
  6371. else if (id->type==IT_PETEGG) {
  6372. iden = 1;
  6373. ref = 0;
  6374. }
  6375. else {
  6376. iden = 1;
  6377. ref = attr = 0;
  6378. }
  6379. c1 = (short)script_getnum(st,7);
  6380. c2 = (short)script_getnum(st,8);
  6381. c3 = (short)script_getnum(st,9);
  6382. c4 = (short)script_getnum(st,10);
  6383. memset(&it, 0, sizeof(it));
  6384. it.nameid = nameid;
  6385. it.identify = iden;
  6386. it.refine = ref;
  6387. it.attribute = attr;
  6388. it.card[0] = (short)c1;
  6389. it.card[1] = (short)c2;
  6390. it.card[2] = (short)c3;
  6391. it.card[3] = (short)c4;
  6392. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6393. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6394. clif_additem(sd, 0, 0, flag);
  6395. return SCRIPT_CMD_FAILURE;
  6396. }
  6397. return SCRIPT_CMD_SUCCESS;
  6398. }
  6399. /*==========================================
  6400. * gets an item with someone's name inscribed [Skotlex]
  6401. * getinscribeditem item_num, character_name
  6402. * Returned Qty is always 1, only works on equip-able
  6403. * equipment
  6404. *------------------------------------------*/
  6405. BUILDIN_FUNC(getnameditem)
  6406. {
  6407. unsigned short nameid;
  6408. struct item item_tmp;
  6409. TBL_PC *sd, *tsd;
  6410. struct script_data *data;
  6411. sd = script_rid2sd(st);
  6412. if (sd == NULL)
  6413. { //Player not attached!
  6414. script_pushint(st,0);
  6415. return SCRIPT_CMD_SUCCESS;
  6416. }
  6417. data=script_getdata(st,2);
  6418. get_val(st,data);
  6419. if( data_isstring(data) ){
  6420. const char *name=conv_str(st,data);
  6421. struct item_data *item_data = itemdb_searchname(name);
  6422. if( item_data == NULL)
  6423. { //Failed
  6424. script_pushint(st,0);
  6425. return SCRIPT_CMD_SUCCESS;
  6426. }
  6427. nameid = item_data->nameid;
  6428. }else
  6429. nameid = conv_num(st,data);
  6430. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6431. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6432. script_pushint(st,0);
  6433. return SCRIPT_CMD_SUCCESS;
  6434. }
  6435. data=script_getdata(st,3);
  6436. get_val(st,data);
  6437. if( data_isstring(data) ) //Char Name
  6438. tsd=map_nick2sd(conv_str(st,data),false);
  6439. else //Char Id was given
  6440. tsd=map_charid2sd(conv_num(st,data));
  6441. if( tsd == NULL )
  6442. { //Failed
  6443. script_pushint(st,0);
  6444. return SCRIPT_CMD_SUCCESS;
  6445. }
  6446. memset(&item_tmp,0,sizeof(item_tmp));
  6447. item_tmp.nameid=nameid;
  6448. item_tmp.amount=1;
  6449. item_tmp.identify=1;
  6450. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6451. item_tmp.card[2]=tsd->status.char_id;
  6452. item_tmp.card[3]=tsd->status.char_id >> 16;
  6453. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6454. script_pushint(st,0);
  6455. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6456. }
  6457. script_pushint(st,1);
  6458. return SCRIPT_CMD_SUCCESS;
  6459. }
  6460. /*==========================================
  6461. * gets a random item ID from an item group [Skotlex]
  6462. * groupranditem <group_num>{,<sub_group>};
  6463. *------------------------------------------*/
  6464. BUILDIN_FUNC(grouprandomitem) {
  6465. struct s_item_group_entry *entry = NULL;
  6466. int sub_group = 1;
  6467. FETCH(3, sub_group);
  6468. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6469. if (!entry) {
  6470. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6471. script_pushint(st,UNKNOWN_ITEM_ID);
  6472. return SCRIPT_CMD_FAILURE;
  6473. }
  6474. script_pushint(st,entry->nameid);
  6475. return SCRIPT_CMD_SUCCESS;
  6476. }
  6477. /**
  6478. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6479. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6480. */
  6481. BUILDIN_FUNC(makeitem) {
  6482. uint16 nameid, amount, flag = 0, x, y;
  6483. const char *mapname;
  6484. int m;
  6485. struct item item_tmp;
  6486. struct script_data *data;
  6487. data = script_getdata(st,2);
  6488. get_val(st,data);
  6489. if( data_isstring(data) ){
  6490. const char *name = conv_str(st,data);
  6491. struct item_data *item_data = itemdb_searchname(name);
  6492. if( item_data )
  6493. nameid = item_data->nameid;
  6494. else
  6495. nameid = UNKNOWN_ITEM_ID;
  6496. }
  6497. else
  6498. nameid = conv_num(st,data);
  6499. amount = script_getnum(st,3);
  6500. mapname = script_getstr(st,4);
  6501. x = script_getnum(st,5);
  6502. y = script_getnum(st,6);
  6503. if(strcmp(mapname,"this")==0) {
  6504. TBL_PC *sd;
  6505. sd = script_rid2sd(st);
  6506. if (!sd)
  6507. return SCRIPT_CMD_SUCCESS; //Failed...
  6508. m = sd->bl.m;
  6509. } else
  6510. m = map_mapname2mapid(mapname);
  6511. if(nameid<0) {
  6512. nameid = -nameid;
  6513. flag = 1;
  6514. }
  6515. if(nameid > 0) {
  6516. memset(&item_tmp,0,sizeof(item_tmp));
  6517. item_tmp.nameid = nameid;
  6518. if(!flag)
  6519. item_tmp.identify = 1;
  6520. else
  6521. item_tmp.identify = itemdb_isidentified(nameid);
  6522. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6523. }
  6524. return SCRIPT_CMD_SUCCESS;
  6525. }
  6526. /**
  6527. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6528. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6529. */
  6530. BUILDIN_FUNC(makeitem2) {
  6531. uint16 nameid, amount, x, y;
  6532. const char *mapname;
  6533. int m;
  6534. struct item item_tmp;
  6535. struct script_data *data;
  6536. struct item_data *id;
  6537. data = script_getdata(st,2);
  6538. get_val(st,data);
  6539. if( data_isstring(data) ){
  6540. const char *name = conv_str(st,data);
  6541. struct item_data *item_data = itemdb_searchname(name);
  6542. if( item_data )
  6543. nameid = item_data->nameid;
  6544. else
  6545. nameid = UNKNOWN_ITEM_ID;
  6546. }
  6547. else
  6548. nameid = conv_num(st,data);
  6549. amount = script_getnum(st,3);
  6550. mapname = script_getstr(st,4);
  6551. x = script_getnum(st,5);
  6552. y = script_getnum(st,6);
  6553. if (strcmp(mapname,"this")==0) {
  6554. TBL_PC *sd;
  6555. sd = script_rid2sd(st);
  6556. if (!sd)
  6557. return SCRIPT_CMD_SUCCESS; //Failed...
  6558. m = sd->bl.m;
  6559. }
  6560. else
  6561. m = map_mapname2mapid(mapname);
  6562. if ((id = itemdb_search(nameid))) {
  6563. char iden, ref, attr;
  6564. memset(&item_tmp,0,sizeof(item_tmp));
  6565. item_tmp.nameid = nameid;
  6566. iden = (char)script_getnum(st,7);
  6567. ref = (char)script_getnum(st,8);
  6568. attr = (char)script_getnum(st,9);
  6569. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6570. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6571. }
  6572. else if (id->type==IT_PETEGG) {
  6573. iden = 1;
  6574. ref = 0;
  6575. }
  6576. else {
  6577. iden = 1;
  6578. ref = attr = 0;
  6579. }
  6580. item_tmp.identify = iden;
  6581. item_tmp.refine = ref;
  6582. item_tmp.attribute = attr;
  6583. item_tmp.card[0] = script_getnum(st,10);
  6584. item_tmp.card[1] = script_getnum(st,11);
  6585. item_tmp.card[2] = script_getnum(st,12);
  6586. item_tmp.card[3] = script_getnum(st,13);
  6587. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6588. }
  6589. else
  6590. return SCRIPT_CMD_FAILURE;
  6591. return SCRIPT_CMD_SUCCESS;
  6592. }
  6593. /// Counts / deletes the current item given by idx.
  6594. /// Used by buildin_delitem_search
  6595. /// Relies on all input data being already fully valid.
  6596. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6597. {
  6598. int delamount;
  6599. struct item *itm = NULL;
  6600. struct s_storage *gstor = NULL;
  6601. switch(loc) {
  6602. case TABLE_CART:
  6603. itm = &sd->cart.u.items_cart[idx];
  6604. break;
  6605. case TABLE_STORAGE:
  6606. itm = &sd->storage.u.items_storage[idx];
  6607. break;
  6608. case TABLE_GUILD_STORAGE:
  6609. {
  6610. gstor = guild2storage2(sd->status.guild_id);
  6611. itm = &gstor->u.items_guild[idx];
  6612. }
  6613. break;
  6614. default: // TABLE_INVENTORY
  6615. itm = &sd->inventory.u.items_inventory[idx];
  6616. break;
  6617. }
  6618. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6619. if( delete_items )
  6620. {
  6621. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6622. {// delete associated pet
  6623. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6624. }
  6625. switch(loc) {
  6626. case TABLE_CART:
  6627. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6628. break;
  6629. case TABLE_STORAGE:
  6630. storage_delitem(sd,&sd->storage,idx,delamount);
  6631. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6632. break;
  6633. case TABLE_GUILD_STORAGE:
  6634. gstor->lock = true;
  6635. storage_guild_delitem(sd, gstor, idx, delamount);
  6636. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  6637. storage_guild_storageclose(sd);
  6638. gstor->lock = false;
  6639. break;
  6640. default: // TABLE_INVENTORY
  6641. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6642. break;
  6643. }
  6644. }
  6645. amount[0]-= delamount;
  6646. }
  6647. /// Searches for item(s) and checks, if there is enough of them.
  6648. /// Used by delitem and delitem2
  6649. /// Relies on all input data being already fully valid.
  6650. /// @param exact_match will also match item attributes and cards, not just name id
  6651. /// @return true when all items could be deleted, false when there were not enough items to delete
  6652. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6653. {
  6654. bool delete_items = false;
  6655. int i, amount, size;
  6656. struct item *items;
  6657. // prefer always non-equipped items
  6658. it->equip = 0;
  6659. // when searching for nameid only, prefer additionally
  6660. if( !exact_match )
  6661. {
  6662. // non-refined items
  6663. it->refine = 0;
  6664. // card-less items
  6665. memset(it->card, 0, sizeof(it->card));
  6666. }
  6667. switch(loc) {
  6668. case TABLE_CART:
  6669. size = MAX_CART;
  6670. items = sd->cart.u.items_cart;
  6671. break;
  6672. case TABLE_STORAGE:
  6673. size = MAX_STORAGE;
  6674. items = sd->storage.u.items_storage;
  6675. break;
  6676. case TABLE_GUILD_STORAGE:
  6677. {
  6678. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6679. size = MAX_GUILD_STORAGE;
  6680. items = gstor->u.items_guild;
  6681. }
  6682. break;
  6683. default: // TABLE_INVENTORY
  6684. size = MAX_INVENTORY;
  6685. items = sd->inventory.u.items_inventory;
  6686. break;
  6687. }
  6688. for(;;)
  6689. {
  6690. unsigned short important = 0;
  6691. amount = it->amount;
  6692. // 1st pass -- less important items / exact match
  6693. for( i = 0; amount && i < size; i++ )
  6694. {
  6695. struct item *itm = NULL;
  6696. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6697. {// wrong/invalid item
  6698. continue;
  6699. }
  6700. if( itm->equip != it->equip || itm->refine != it->refine )
  6701. {// not matching attributes
  6702. important++;
  6703. continue;
  6704. }
  6705. if( exact_match )
  6706. {
  6707. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6708. {// not matching exact attributes
  6709. continue;
  6710. }
  6711. }
  6712. else
  6713. {
  6714. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6715. {
  6716. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6717. {// pet which cannot be deleted
  6718. continue;
  6719. }
  6720. }
  6721. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6722. {// named/carded item
  6723. important++;
  6724. continue;
  6725. }
  6726. }
  6727. // count / delete item
  6728. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6729. }
  6730. // 2nd pass -- any matching item
  6731. if( amount == 0 || important == 0 )
  6732. {// either everything was already consumed or no items were skipped
  6733. ;
  6734. }
  6735. else for( i = 0; amount && i < size; i++ )
  6736. {
  6737. struct item *itm = NULL;
  6738. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6739. {// wrong/invalid item
  6740. continue;
  6741. }
  6742. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6743. {// pet which cannot be deleted
  6744. continue;
  6745. }
  6746. if( exact_match )
  6747. {
  6748. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6749. {// not matching attributes
  6750. continue;
  6751. }
  6752. }
  6753. // count / delete item
  6754. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6755. }
  6756. if( amount )
  6757. {// not enough items
  6758. return false;
  6759. }
  6760. else if( delete_items )
  6761. {// we are done with the work
  6762. return true;
  6763. }
  6764. else
  6765. {// get rid of the items now
  6766. delete_items = true;
  6767. }
  6768. }
  6769. }
  6770. /// Deletes items from the target/attached player.
  6771. /// Prioritizes ordinary items.
  6772. ///
  6773. /// delitem <item id>,<amount>{,<account id>}
  6774. /// delitem "<item name>",<amount>{,<account id>}
  6775. /// cartdelitem <item id>,<amount>{,<account id>}
  6776. /// cartdelitem "<item name>",<amount>{,<account id>}
  6777. /// storagedelitem <item id>,<amount>{,<account id>}
  6778. /// storagedelitem "<item name>",<amount>{,<account id>}
  6779. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  6780. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  6781. BUILDIN_FUNC(delitem)
  6782. {
  6783. TBL_PC *sd;
  6784. struct item it;
  6785. struct script_data *data;
  6786. uint8 loc = 0;
  6787. char* command = (char*)script_getfuncname(st);
  6788. if(!strncmp(command, "cart", 4))
  6789. loc = TABLE_CART;
  6790. else if(!strncmp(command, "storage", 7))
  6791. loc = TABLE_STORAGE;
  6792. else if(!strncmp(command, "guildstorage", 12))
  6793. loc = TABLE_GUILD_STORAGE;
  6794. if( script_hasdata(st,4) )
  6795. {
  6796. uint32 account_id = script_getnum(st,4);
  6797. sd = map_id2sd(account_id); // <account id>
  6798. if( sd == NULL )
  6799. {
  6800. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6801. st->state = END;
  6802. return SCRIPT_CMD_FAILURE;
  6803. }
  6804. }
  6805. else
  6806. {
  6807. sd = script_rid2sd(st);// attached player
  6808. if( sd == NULL )
  6809. return SCRIPT_CMD_SUCCESS;
  6810. }
  6811. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  6812. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6813. script_pushint(st, -1);
  6814. return SCRIPT_CMD_FAILURE;
  6815. }
  6816. if (loc == TABLE_GUILD_STORAGE) {
  6817. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6818. if (gstor == NULL || sd->state.storage_flag) {
  6819. script_pushint(st, -1);
  6820. return SCRIPT_CMD_FAILURE;
  6821. }
  6822. }
  6823. data = script_getdata(st,2);
  6824. get_val(st,data);
  6825. if( data_isstring(data) )
  6826. {
  6827. const char* item_name = conv_str(st,data);
  6828. struct item_data* id = itemdb_searchname(item_name);
  6829. if( id == NULL )
  6830. {
  6831. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6832. st->state = END;
  6833. return SCRIPT_CMD_FAILURE;
  6834. }
  6835. it.nameid = id->nameid;// "<item name>"
  6836. }
  6837. else
  6838. {
  6839. it.nameid = conv_num(st,data);// <item id>
  6840. if( !itemdb_exists( it.nameid ) )
  6841. {
  6842. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6843. st->state = END;
  6844. return SCRIPT_CMD_FAILURE;
  6845. }
  6846. }
  6847. it.amount=script_getnum(st,3);
  6848. if( it.amount <= 0 )
  6849. return SCRIPT_CMD_SUCCESS;// nothing to do
  6850. if( buildin_delitem_search(sd, &it, false, loc) )
  6851. {// success
  6852. return SCRIPT_CMD_SUCCESS;
  6853. }
  6854. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6855. st->state = END;
  6856. st->mes_active = 0;
  6857. clif_scriptclose(sd, st->oid);
  6858. return SCRIPT_CMD_FAILURE;
  6859. }
  6860. /// Deletes items from the target/attached player.
  6861. ///
  6862. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6863. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6864. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6865. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6866. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6867. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6868. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6869. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6870. BUILDIN_FUNC(delitem2)
  6871. {
  6872. TBL_PC *sd;
  6873. struct item it;
  6874. struct script_data *data;
  6875. uint8 loc = 0;
  6876. char* command = (char*)script_getfuncname(st);
  6877. if(!strncmp(command, "cart", 4))
  6878. loc = TABLE_CART;
  6879. else if(!strncmp(command, "storage", 7))
  6880. loc = TABLE_STORAGE;
  6881. else if(!strncmp(command, "guildstorage", 12))
  6882. loc = TABLE_GUILD_STORAGE;
  6883. if( script_hasdata(st,11) )
  6884. {
  6885. uint32 account_id = script_getnum(st,11);
  6886. sd = map_id2sd(account_id); // <account id>
  6887. if( sd == NULL )
  6888. {
  6889. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6890. st->state = END;
  6891. return SCRIPT_CMD_FAILURE;
  6892. }
  6893. }
  6894. else
  6895. {
  6896. sd = script_rid2sd(st);// attached player
  6897. if( sd == NULL )
  6898. return SCRIPT_CMD_SUCCESS;
  6899. }
  6900. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  6901. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6902. script_pushint(st,-1);
  6903. return SCRIPT_CMD_FAILURE;
  6904. }
  6905. if (loc == TABLE_GUILD_STORAGE) {
  6906. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6907. if (gstor == NULL || sd->state.storage_flag) {
  6908. script_pushint(st, -1);
  6909. return SCRIPT_CMD_FAILURE;
  6910. }
  6911. }
  6912. data = script_getdata(st,2);
  6913. get_val(st,data);
  6914. if( data_isstring(data) )
  6915. {
  6916. const char* item_name = conv_str(st,data);
  6917. struct item_data* id = itemdb_searchname(item_name);
  6918. if( id == NULL )
  6919. {
  6920. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6921. st->state = END;
  6922. return SCRIPT_CMD_FAILURE;
  6923. }
  6924. it.nameid = id->nameid;// "<item name>"
  6925. }
  6926. else
  6927. {
  6928. it.nameid = conv_num(st,data);// <item id>
  6929. if( !itemdb_exists( it.nameid ) )
  6930. {
  6931. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6932. st->state = END;
  6933. return SCRIPT_CMD_FAILURE;
  6934. }
  6935. }
  6936. it.amount=script_getnum(st,3);
  6937. it.identify=script_getnum(st,4);
  6938. it.refine=script_getnum(st,5);
  6939. it.attribute=script_getnum(st,6);
  6940. it.card[0]=(short)script_getnum(st,7);
  6941. it.card[1]=(short)script_getnum(st,8);
  6942. it.card[2]=(short)script_getnum(st,9);
  6943. it.card[3]=(short)script_getnum(st,10);
  6944. if( it.amount <= 0 )
  6945. return SCRIPT_CMD_SUCCESS;// nothing to do
  6946. if( buildin_delitem_search(sd, &it, true, loc) )
  6947. {// success
  6948. return SCRIPT_CMD_SUCCESS;
  6949. }
  6950. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6951. st->state = END;
  6952. st->mes_active = 0;
  6953. clif_scriptclose(sd, st->oid);
  6954. return SCRIPT_CMD_FAILURE;
  6955. }
  6956. /*==========================================
  6957. * Enables/Disables use of items while in an NPC [Skotlex]
  6958. *------------------------------------------*/
  6959. BUILDIN_FUNC(enableitemuse)
  6960. {
  6961. TBL_PC *sd;
  6962. sd=script_rid2sd(st);
  6963. if (sd)
  6964. st->npc_item_flag = sd->npc_item_flag = 1;
  6965. return SCRIPT_CMD_SUCCESS;
  6966. }
  6967. BUILDIN_FUNC(disableitemuse)
  6968. {
  6969. TBL_PC *sd;
  6970. sd=script_rid2sd(st);
  6971. if (sd)
  6972. st->npc_item_flag = sd->npc_item_flag = 0;
  6973. return SCRIPT_CMD_SUCCESS;
  6974. }
  6975. /*==========================================
  6976. * Returns a character's specified stat.
  6977. * Check pc_readparam for available options.
  6978. * readparam <param>{,"<nick>"}
  6979. * readparam <param>{,<char_id>}
  6980. *------------------------------------------*/
  6981. BUILDIN_FUNC(readparam)
  6982. {
  6983. int value;
  6984. struct script_data *data = script_getdata(st, 2);
  6985. TBL_PC *sd;
  6986. if( script_hasdata(st, 3) ){
  6987. if( script_isint(st, 3) ){
  6988. script_charid2sd(3, sd);
  6989. }else{
  6990. script_nick2sd(3, sd);
  6991. }
  6992. }else{
  6993. sd = script_rid2sd(st);
  6994. }
  6995. if( !sd ){
  6996. script_pushint(st, -1);
  6997. return SCRIPT_CMD_FAILURE;
  6998. }
  6999. // If you use a parameter, return the value behind it
  7000. if( reference_toparam(data) ){
  7001. get_val_(st, data, sd);
  7002. value = (int)data->u.num;
  7003. }else{
  7004. value = pc_readparam(sd,script_getnum(st, 2));
  7005. }
  7006. script_pushint(st,value);
  7007. return SCRIPT_CMD_SUCCESS;
  7008. }
  7009. /*==========================================
  7010. * Return charid identification
  7011. * return by @num :
  7012. * 0 : char_id
  7013. * 1 : party_id
  7014. * 2 : guild_id
  7015. * 3 : account_id
  7016. * 4 : bg_id
  7017. * 5 : clan_id
  7018. *------------------------------------------*/
  7019. BUILDIN_FUNC(getcharid)
  7020. {
  7021. int num;
  7022. TBL_PC *sd;
  7023. num = script_getnum(st,2);
  7024. if( !script_nick2sd(3,sd) ){
  7025. script_pushint(st,0); //return 0, according docs
  7026. return SCRIPT_CMD_SUCCESS;
  7027. }
  7028. switch( num ) {
  7029. case 0: script_pushint(st,sd->status.char_id); break;
  7030. case 1: script_pushint(st,sd->status.party_id); break;
  7031. case 2: script_pushint(st,sd->status.guild_id); break;
  7032. case 3: script_pushint(st,sd->status.account_id); break;
  7033. case 4: script_pushint(st,sd->bg_id); break;
  7034. case 5: script_pushint(st,sd->status.clan_id); break;
  7035. default:
  7036. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  7037. script_pushint(st,0);
  7038. break;
  7039. }
  7040. return SCRIPT_CMD_SUCCESS;
  7041. }
  7042. /*==========================================
  7043. * returns the GID of an NPC
  7044. *------------------------------------------*/
  7045. BUILDIN_FUNC(getnpcid)
  7046. {
  7047. int num = script_getnum(st,2);
  7048. struct npc_data* nd = NULL;
  7049. if( script_hasdata(st,3) )
  7050. {// unique npc name
  7051. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  7052. {
  7053. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  7054. script_pushint(st,0);
  7055. return SCRIPT_CMD_FAILURE;
  7056. }
  7057. }
  7058. switch (num) {
  7059. case 0:
  7060. script_pushint(st,nd ? nd->bl.id : st->oid);
  7061. break;
  7062. default:
  7063. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  7064. script_pushint(st,0);
  7065. return SCRIPT_CMD_FAILURE;
  7066. }
  7067. return SCRIPT_CMD_SUCCESS;
  7068. }
  7069. /*==========================================
  7070. * Return the name of the party_id
  7071. * null if not found
  7072. *------------------------------------------*/
  7073. BUILDIN_FUNC(getpartyname)
  7074. {
  7075. int party_id;
  7076. struct party_data* p;
  7077. party_id = script_getnum(st,2);
  7078. if( ( p = party_search(party_id) ) != NULL )
  7079. {
  7080. script_pushstrcopy(st,p->party.name);
  7081. }
  7082. else
  7083. {
  7084. script_pushconststr(st,"null");
  7085. }
  7086. return SCRIPT_CMD_SUCCESS;
  7087. }
  7088. /*==========================================
  7089. * Get the information of the members of a party by type
  7090. * @party_id, @type
  7091. * return by @type :
  7092. * - : nom des membres
  7093. * 1 : char_id des membres
  7094. * 2 : account_id des membres
  7095. *------------------------------------------*/
  7096. BUILDIN_FUNC(getpartymember)
  7097. {
  7098. struct party_data *p;
  7099. uint8 j = 0;
  7100. p = party_search(script_getnum(st,2));
  7101. if (p != NULL) {
  7102. uint8 i, type = 0;
  7103. struct script_data *data = NULL;
  7104. char *varname = NULL;
  7105. if (script_hasdata(st,3))
  7106. type = script_getnum(st,3);
  7107. if (script_hasdata(st,4)) {
  7108. data = script_getdata(st, 4);
  7109. if (!data_isreference(data)) {
  7110. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7111. return SCRIPT_CMD_FAILURE;
  7112. }
  7113. varname = reference_getname(data);
  7114. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7115. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7116. return SCRIPT_CMD_FAILURE;
  7117. }
  7118. }
  7119. for (i = 0; i < MAX_PARTY; i++) {
  7120. if (p->party.member[i].account_id) {
  7121. switch (type) {
  7122. case 2:
  7123. if (data)
  7124. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  7125. else
  7126. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7127. break;
  7128. case 1:
  7129. if (data)
  7130. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  7131. else
  7132. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7133. break;
  7134. default:
  7135. if (data)
  7136. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  7137. else
  7138. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7139. break;
  7140. }
  7141. j++;
  7142. }
  7143. }
  7144. }
  7145. mapreg_setreg(add_str("$@partymembercount"),j);
  7146. script_pushint(st, j);
  7147. return SCRIPT_CMD_SUCCESS;
  7148. }
  7149. /*==========================================
  7150. * Retrieves party leader. if flag is specified,
  7151. * return some of the leader data. Otherwise, return name.
  7152. *------------------------------------------*/
  7153. BUILDIN_FUNC(getpartyleader)
  7154. {
  7155. int party_id, type = 0, i=0;
  7156. struct party_data *p;
  7157. party_id=script_getnum(st,2);
  7158. if( script_hasdata(st,3) )
  7159. type=script_getnum(st,3);
  7160. p=party_search(party_id);
  7161. if (p) //Search leader
  7162. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7163. if (!p || i == MAX_PARTY) { //leader not found
  7164. if (type)
  7165. script_pushint(st,-1);
  7166. else
  7167. script_pushconststr(st,"null");
  7168. return SCRIPT_CMD_SUCCESS;
  7169. }
  7170. switch (type) {
  7171. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7172. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7173. case 3: script_pushint(st,p->party.member[i].class_); break;
  7174. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7175. case 5: script_pushint(st,p->party.member[i].lv); break;
  7176. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7177. }
  7178. return SCRIPT_CMD_SUCCESS;
  7179. }
  7180. /*==========================================
  7181. * Return the name of the @guild_id
  7182. * null if not found
  7183. *------------------------------------------*/
  7184. BUILDIN_FUNC(getguildname)
  7185. {
  7186. int guild_id;
  7187. struct guild* g;
  7188. guild_id = script_getnum(st,2);
  7189. if( ( g = guild_search(guild_id) ) != NULL )
  7190. script_pushstrcopy(st,g->name);
  7191. else
  7192. script_pushconststr(st,"null");
  7193. return SCRIPT_CMD_SUCCESS;
  7194. }
  7195. /*==========================================
  7196. * Return the name of the guild master of @guild_id
  7197. * null if not found
  7198. *------------------------------------------*/
  7199. BUILDIN_FUNC(getguildmaster)
  7200. {
  7201. int guild_id;
  7202. struct guild* g;
  7203. guild_id = script_getnum(st,2);
  7204. if( ( g = guild_search(guild_id) ) != NULL )
  7205. script_pushstrcopy(st,g->member[0].name);
  7206. else
  7207. script_pushconststr(st,"null");
  7208. return SCRIPT_CMD_SUCCESS;
  7209. }
  7210. BUILDIN_FUNC(getguildmasterid)
  7211. {
  7212. int guild_id;
  7213. struct guild* g;
  7214. guild_id = script_getnum(st,2);
  7215. if( ( g = guild_search(guild_id) ) != NULL )
  7216. script_pushint(st,g->member[0].char_id);
  7217. else
  7218. script_pushint(st,0);
  7219. return SCRIPT_CMD_SUCCESS;
  7220. }
  7221. /*==========================================
  7222. * Get char string information by type :
  7223. * Return by @type :
  7224. * 0 : char_name
  7225. * 1 : party_name or ""
  7226. * 2 : guild_name or ""
  7227. * 3 : map_name
  7228. * - : ""
  7229. * strcharinfo(<type>{,<char_id>})
  7230. *------------------------------------------*/
  7231. BUILDIN_FUNC(strcharinfo)
  7232. {
  7233. TBL_PC *sd;
  7234. int num;
  7235. struct guild* g;
  7236. struct party_data* p;
  7237. if (!script_charid2sd(3,sd)) {
  7238. script_pushconststr(st,"");
  7239. return SCRIPT_CMD_FAILURE;
  7240. }
  7241. num=script_getnum(st,2);
  7242. switch(num){
  7243. case 0:
  7244. script_pushstrcopy(st,sd->status.name);
  7245. break;
  7246. case 1:
  7247. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7248. script_pushstrcopy(st,p->party.name);
  7249. } else {
  7250. script_pushconststr(st,"");
  7251. }
  7252. break;
  7253. case 2:
  7254. if( ( g = sd->guild ) != NULL ) {
  7255. script_pushstrcopy(st,g->name);
  7256. } else {
  7257. script_pushconststr(st,"");
  7258. }
  7259. break;
  7260. case 3:
  7261. script_pushconststr(st,map[sd->bl.m].name);
  7262. break;
  7263. default:
  7264. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7265. script_pushconststr(st,"");
  7266. break;
  7267. }
  7268. return SCRIPT_CMD_SUCCESS;
  7269. }
  7270. /*==========================================
  7271. * Get npc string information by type
  7272. * return by @type:
  7273. * 0 : name
  7274. * 1 : str#
  7275. * 2 : #str
  7276. * 3 : ::str
  7277. * 4 : map name
  7278. *------------------------------------------*/
  7279. BUILDIN_FUNC(strnpcinfo)
  7280. {
  7281. TBL_NPC* nd;
  7282. int num;
  7283. char *buf,*name=NULL;
  7284. nd = map_id2nd(st->oid);
  7285. if (!nd) {
  7286. script_pushconststr(st, "");
  7287. return SCRIPT_CMD_SUCCESS;
  7288. }
  7289. num = script_getnum(st,2);
  7290. switch(num){
  7291. case 0: // display name
  7292. name = aStrdup(nd->name);
  7293. break;
  7294. case 1: // visible part of display name
  7295. if((buf = strchr(nd->name,'#')) != NULL)
  7296. {
  7297. name = aStrdup(nd->name);
  7298. name[buf - nd->name] = 0;
  7299. } else // Return the name, there is no '#' present
  7300. name = aStrdup(nd->name);
  7301. break;
  7302. case 2: // # fragment
  7303. if((buf = strchr(nd->name,'#')) != NULL)
  7304. name = aStrdup(buf+1);
  7305. break;
  7306. case 3: // unique name
  7307. name = aStrdup(nd->exname);
  7308. break;
  7309. case 4: // map name
  7310. name = aStrdup(map[nd->bl.m].name);
  7311. break;
  7312. }
  7313. if(name)
  7314. script_pushstr(st, name);
  7315. else
  7316. script_pushconststr(st, "");
  7317. return SCRIPT_CMD_SUCCESS;
  7318. }
  7319. /**
  7320. * getequipid(<equipment slot>{,<char_id>})
  7321. **/
  7322. BUILDIN_FUNC(getequipid)
  7323. {
  7324. int i, num;
  7325. TBL_PC* sd;
  7326. struct item_data* item;
  7327. if (!script_charid2sd(3, sd)) {
  7328. script_pushint(st,-1);
  7329. return SCRIPT_CMD_FAILURE;
  7330. }
  7331. num = script_getnum(st,2);
  7332. if( !equip_index_check(num) )
  7333. {
  7334. script_pushint(st,-1);
  7335. return SCRIPT_CMD_FAILURE;
  7336. }
  7337. // get inventory position of item
  7338. i = pc_checkequip(sd,equip_bitmask[num]);
  7339. if( i < 0 )
  7340. {
  7341. script_pushint(st,-1);
  7342. return SCRIPT_CMD_SUCCESS;
  7343. }
  7344. item = sd->inventory_data[i];
  7345. if( item != 0 )
  7346. script_pushint(st,item->nameid);
  7347. else
  7348. script_pushint(st,0);
  7349. return SCRIPT_CMD_SUCCESS;
  7350. }
  7351. /*==========================================
  7352. * getequipuniqueid(<equipment slot>{,<char_id>})
  7353. *------------------------------------------*/
  7354. BUILDIN_FUNC(getequipuniqueid)
  7355. {
  7356. int i, num;
  7357. TBL_PC* sd;
  7358. struct item* item;
  7359. if (!script_charid2sd(3, sd)) {
  7360. script_pushconststr(st, "");
  7361. return SCRIPT_CMD_FAILURE;
  7362. }
  7363. num = script_getnum(st,2);
  7364. if ( !equip_index_check(num) ) {
  7365. script_pushconststr(st, "");
  7366. return SCRIPT_CMD_FAILURE;
  7367. }
  7368. // get inventory position of item
  7369. i = pc_checkequip(sd,equip_bitmask[num]);
  7370. if (i < 0) {
  7371. script_pushconststr(st, "");
  7372. return SCRIPT_CMD_FAILURE;
  7373. }
  7374. item = &sd->inventory.u.items_inventory[i];
  7375. if (item != 0) {
  7376. int maxlen = 256;
  7377. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7378. memset(buf, 0, maxlen);
  7379. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7380. script_pushstr(st, buf);
  7381. } else
  7382. script_pushconststr(st, "");
  7383. return SCRIPT_CMD_SUCCESS;
  7384. }
  7385. /*==========================================
  7386. * Get the equipement name at pos
  7387. * return item jname or ""
  7388. * getequipname(<equipment slot>{,<char_id>})
  7389. *------------------------------------------*/
  7390. BUILDIN_FUNC(getequipname)
  7391. {
  7392. int i, num;
  7393. TBL_PC* sd;
  7394. struct item_data* item;
  7395. if (!script_charid2sd(3, sd)) {
  7396. script_pushconststr(st, "");
  7397. return SCRIPT_CMD_FAILURE;
  7398. }
  7399. num = script_getnum(st,2);
  7400. if( !equip_index_check(num) )
  7401. {
  7402. script_pushconststr(st,"");
  7403. return SCRIPT_CMD_SUCCESS;
  7404. }
  7405. // get inventory position of item
  7406. i = pc_checkequip(sd,equip_bitmask[num]);
  7407. if( i < 0 )
  7408. {
  7409. script_pushconststr(st,"");
  7410. return SCRIPT_CMD_SUCCESS;
  7411. }
  7412. item = sd->inventory_data[i];
  7413. if( item != 0 )
  7414. script_pushstrcopy(st,item->jname);
  7415. else
  7416. script_pushconststr(st,"");
  7417. return SCRIPT_CMD_SUCCESS;
  7418. }
  7419. /*==========================================
  7420. * getbrokenid [Valaris]
  7421. * getbrokenid(<number>{,<char_id>})
  7422. *------------------------------------------*/
  7423. BUILDIN_FUNC(getbrokenid)
  7424. {
  7425. int i,num,id = 0,brokencounter = 0;
  7426. TBL_PC *sd;
  7427. if (!script_charid2sd(3, sd)) {
  7428. script_pushint(st,0);
  7429. return SCRIPT_CMD_FAILURE;
  7430. }
  7431. num = script_getnum(st,2);
  7432. for(i = 0; i < MAX_INVENTORY; i++) {
  7433. if(sd->inventory.u.items_inventory[i].attribute) {
  7434. brokencounter++;
  7435. if(num == brokencounter){
  7436. id = sd->inventory.u.items_inventory[i].nameid;
  7437. break;
  7438. }
  7439. }
  7440. }
  7441. script_pushint(st,id);
  7442. return SCRIPT_CMD_SUCCESS;
  7443. }
  7444. /*==========================================
  7445. * repair [Valaris]
  7446. * repair <num>{,<char_id>};
  7447. *------------------------------------------*/
  7448. BUILDIN_FUNC(repair)
  7449. {
  7450. int i,num;
  7451. int repaircounter = 0;
  7452. TBL_PC *sd;
  7453. if (!script_charid2sd(3,sd))
  7454. return SCRIPT_CMD_FAILURE;
  7455. num = script_getnum(st,2);
  7456. for(i = 0; i < MAX_INVENTORY; i++) {
  7457. if(sd->inventory.u.items_inventory[i].attribute) {
  7458. repaircounter++;
  7459. if(num == repaircounter) {
  7460. sd->inventory.u.items_inventory[i].attribute = 0;
  7461. clif_equiplist(sd);
  7462. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  7463. clif_misceffect(&sd->bl, 3);
  7464. break;
  7465. }
  7466. }
  7467. }
  7468. return SCRIPT_CMD_SUCCESS;
  7469. }
  7470. /*==========================================
  7471. * repairall {<char_id>}
  7472. *------------------------------------------*/
  7473. BUILDIN_FUNC(repairall)
  7474. {
  7475. int i, repaircounter = 0;
  7476. TBL_PC *sd;
  7477. if (!script_charid2sd(2,sd))
  7478. return SCRIPT_CMD_FAILURE;
  7479. for(i = 0; i < MAX_INVENTORY; i++)
  7480. {
  7481. if(sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].attribute)
  7482. {
  7483. sd->inventory.u.items_inventory[i].attribute = 0;
  7484. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  7485. repaircounter++;
  7486. }
  7487. }
  7488. if(repaircounter)
  7489. {
  7490. clif_misceffect(&sd->bl, 3);
  7491. clif_equiplist(sd);
  7492. }
  7493. return SCRIPT_CMD_SUCCESS;
  7494. }
  7495. /*==========================================
  7496. * Chk if player have something equiped at pos
  7497. * getequipisequiped <pos>{,<char_id>}
  7498. *------------------------------------------*/
  7499. BUILDIN_FUNC(getequipisequiped)
  7500. {
  7501. int i = -1,num;
  7502. TBL_PC *sd;
  7503. num = script_getnum(st,2);
  7504. if (!script_charid2sd(3,sd))
  7505. return SCRIPT_CMD_FAILURE;
  7506. if ( equip_index_check(num) )
  7507. i=pc_checkequip(sd,equip_bitmask[num]);
  7508. if(i >= 0)
  7509. script_pushint(st,1);
  7510. else
  7511. script_pushint(st,0);
  7512. return SCRIPT_CMD_SUCCESS;
  7513. }
  7514. /*==========================================
  7515. * Chk if the player have something equiped at pos
  7516. * if so chk if this item ain't marked not refinable or rental
  7517. * return (npc)
  7518. * 1 : true
  7519. * 0 : false
  7520. * getequipisenableref(<equipment slot>{,<char_id>})
  7521. *------------------------------------------*/
  7522. BUILDIN_FUNC(getequipisenableref)
  7523. {
  7524. int i = -1,num;
  7525. TBL_PC *sd;
  7526. num = script_getnum(st,2);
  7527. if (!script_charid2sd(3, sd)) {
  7528. script_pushint(st,0);
  7529. return SCRIPT_CMD_FAILURE;
  7530. }
  7531. if( equip_index_check(num) )
  7532. i = pc_checkequip(sd,equip_bitmask[num]);
  7533. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  7534. script_pushint(st,1);
  7535. else
  7536. script_pushint(st,0);
  7537. return SCRIPT_CMD_SUCCESS;
  7538. }
  7539. /*==========================================
  7540. * Get the item refined value at pos
  7541. * return (npc)
  7542. * x : refine amount
  7543. * 0 : false (not refined)
  7544. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7545. *------------------------------------------*/
  7546. BUILDIN_FUNC(getequiprefinerycnt)
  7547. {
  7548. int i = -1,num;
  7549. TBL_PC *sd;
  7550. num = script_getnum(st,2);
  7551. if (!script_charid2sd(3, sd)) {
  7552. script_pushint(st,0);
  7553. return SCRIPT_CMD_FAILURE;
  7554. }
  7555. if (equip_index_check(num))
  7556. i=pc_checkequip(sd,equip_bitmask[num]);
  7557. if(i >= 0)
  7558. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  7559. else
  7560. script_pushint(st,0);
  7561. return SCRIPT_CMD_SUCCESS;
  7562. }
  7563. /*==========================================
  7564. * Get the weapon level value at pos
  7565. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7566. * return (npc)
  7567. * x : weapon level
  7568. * 0 : false
  7569. * getequipweaponlv(<equipment slot>{,<char_id>})
  7570. *------------------------------------------*/
  7571. BUILDIN_FUNC(getequipweaponlv)
  7572. {
  7573. int i = -1, num;
  7574. TBL_PC *sd;
  7575. num = script_getnum(st, 2);
  7576. if (!script_charid2sd(3, sd)) {
  7577. script_pushint(st, 0);
  7578. return SCRIPT_CMD_FAILURE;
  7579. }
  7580. if (num == -1)
  7581. i = current_equip_item_index;
  7582. else if (equip_index_check(num))
  7583. i = pc_checkequip(sd, equip_bitmask[num]);
  7584. if (i >= 0 && sd->inventory_data[i])
  7585. script_pushint(st, sd->inventory_data[i]->wlv);
  7586. else
  7587. script_pushint(st, 0);
  7588. return SCRIPT_CMD_SUCCESS;
  7589. }
  7590. /*==========================================
  7591. * Get the item refine chance (from refine.txt) for item at pos
  7592. * return (npc)
  7593. * x : refine chance
  7594. * 0 : false (max refine level or unequip..)
  7595. * getequippercentrefinery(<equipment slot>{,<char_id>})
  7596. *------------------------------------------*/
  7597. BUILDIN_FUNC(getequippercentrefinery)
  7598. {
  7599. int i = -1,num;
  7600. TBL_PC *sd;
  7601. num = script_getnum(st,2);
  7602. if (!script_charid2sd(3, sd)) {
  7603. script_pushint(st,0);
  7604. return SCRIPT_CMD_FAILURE;
  7605. }
  7606. if (equip_index_check(num))
  7607. i = pc_checkequip(sd,equip_bitmask[num]);
  7608. if(i >= 0 && sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].refine < MAX_REFINE)
  7609. 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));
  7610. else
  7611. script_pushint(st,0);
  7612. return SCRIPT_CMD_SUCCESS;
  7613. }
  7614. /*==========================================
  7615. * Refine +1 item at pos and log and display refine
  7616. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7617. *------------------------------------------*/
  7618. BUILDIN_FUNC(successrefitem) {
  7619. short i = -1, up = 1;
  7620. int pos;
  7621. TBL_PC *sd;
  7622. pos = script_getnum(st,2);
  7623. if (!script_charid2sd(4, sd)) {
  7624. script_pushint(st, -1);
  7625. return SCRIPT_CMD_FAILURE;
  7626. }
  7627. if (script_hasdata(st, 3))
  7628. up = script_getnum(st, 3);
  7629. if (equip_index_check(pos))
  7630. i = pc_checkequip(sd,equip_bitmask[pos]);
  7631. if (i >= 0) {
  7632. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7633. //Logs items, got from (N)PC scripts [Lupus]
  7634. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7635. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  7636. script_pushint(st, MAX_REFINE);
  7637. return SCRIPT_CMD_SUCCESS;
  7638. }
  7639. sd->inventory.u.items_inventory[i].refine += up;
  7640. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7641. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7642. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  7643. clif_delitem(sd,i,1,3);
  7644. //Logs items, got from (N)PC scripts [Lupus]
  7645. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7646. clif_additem(sd,i,1,0);
  7647. pc_equipitem(sd,i,ep);
  7648. clif_misceffect(&sd->bl,3);
  7649. if (sd->inventory.u.items_inventory[i].refine == MAX_REFINE &&
  7650. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  7651. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]))
  7652. { // Fame point system [DracoRPG]
  7653. switch (sd->inventory_data[i]->wlv){
  7654. case 1:
  7655. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7656. break;
  7657. case 2:
  7658. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7659. break;
  7660. case 3:
  7661. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7662. break;
  7663. }
  7664. }
  7665. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7666. return SCRIPT_CMD_SUCCESS;
  7667. }
  7668. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7669. script_pushint(st, -1);
  7670. return SCRIPT_CMD_FAILURE;
  7671. }
  7672. /*==========================================
  7673. * Show a failed Refine +1 attempt
  7674. * failedrefitem <equipment slot>{,<char_id>}
  7675. *------------------------------------------*/
  7676. BUILDIN_FUNC(failedrefitem) {
  7677. short i = -1;
  7678. int pos;
  7679. TBL_PC *sd;
  7680. pos = script_getnum(st,2);
  7681. if (!script_charid2sd(3, sd)) {
  7682. script_pushint(st, 0);
  7683. return SCRIPT_CMD_FAILURE;
  7684. }
  7685. if (equip_index_check(pos))
  7686. i = pc_checkequip(sd,equip_bitmask[pos]);
  7687. if (i >= 0) {
  7688. sd->inventory.u.items_inventory[i].refine = 0;
  7689. pc_unequipitem(sd,i,3); //recalculate bonus
  7690. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  7691. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7692. clif_misceffect(&sd->bl,2); // display failure effect
  7693. script_pushint(st, 1);
  7694. return SCRIPT_CMD_SUCCESS;
  7695. }
  7696. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7697. script_pushint(st, 0);
  7698. return SCRIPT_CMD_FAILURE;
  7699. }
  7700. /*==========================================
  7701. * Downgrades an Equipment Part by -1 . [Masao]
  7702. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7703. *------------------------------------------*/
  7704. BUILDIN_FUNC(downrefitem) {
  7705. short i = -1, down = 1;
  7706. int pos;
  7707. TBL_PC *sd;
  7708. if (!script_charid2sd(4, sd)) {
  7709. script_pushint(st, -1);
  7710. return SCRIPT_CMD_FAILURE;
  7711. }
  7712. pos = script_getnum(st,2);
  7713. if (script_hasdata(st, 3))
  7714. down = script_getnum(st, 3);
  7715. if (equip_index_check(pos))
  7716. i = pc_checkequip(sd,equip_bitmask[pos]);
  7717. if (i >= 0) {
  7718. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7719. //Logs items, got from (N)PC scripts [Lupus]
  7720. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7721. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7722. sd->inventory.u.items_inventory[i].refine -= down;
  7723. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7724. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  7725. clif_delitem(sd,i,1,3);
  7726. //Logs items, got from (N)PC scripts [Lupus]
  7727. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7728. clif_additem(sd,i,1,0);
  7729. pc_equipitem(sd,i,ep);
  7730. clif_misceffect(&sd->bl,2);
  7731. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7732. return SCRIPT_CMD_SUCCESS;
  7733. }
  7734. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7735. script_pushint(st, -1);
  7736. return SCRIPT_CMD_FAILURE;
  7737. }
  7738. /**
  7739. * Delete the item equipped at pos.
  7740. * delequip <equipment slot>{,<char_id>};
  7741. **/
  7742. BUILDIN_FUNC(delequip) {
  7743. short i = -1;
  7744. int pos;
  7745. int8 ret;
  7746. TBL_PC *sd;
  7747. pos = script_getnum(st,2);
  7748. if (!script_charid2sd(3,sd)) {
  7749. st->state = END;
  7750. return SCRIPT_CMD_FAILURE;
  7751. }
  7752. if (equip_index_check(pos))
  7753. i = pc_checkequip(sd,equip_bitmask[pos]);
  7754. if (i >= 0) {
  7755. pc_unequipitem(sd,i,3); //recalculate bonus
  7756. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7757. }
  7758. else {
  7759. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7760. st->state = END;
  7761. return SCRIPT_CMD_FAILURE;
  7762. }
  7763. script_pushint(st,ret);
  7764. return SCRIPT_CMD_SUCCESS;
  7765. }
  7766. /**
  7767. * Break the item equipped at pos.
  7768. * breakequip <equipment slot>{,<char_id>};
  7769. **/
  7770. BUILDIN_FUNC(breakequip) {
  7771. short i = -1;
  7772. int pos;
  7773. TBL_PC *sd;
  7774. pos = script_getnum(st,2);
  7775. if (!script_charid2sd(3,sd))
  7776. return SCRIPT_CMD_FAILURE;
  7777. if (equip_index_check(pos))
  7778. i = pc_checkequip(sd,equip_bitmask[pos]);
  7779. if (i >= 0) {
  7780. sd->inventory.u.items_inventory[i].attribute = 1;
  7781. pc_unequipitem(sd,i,3);
  7782. clif_equiplist(sd);
  7783. script_pushint(st,1);
  7784. return SCRIPT_CMD_SUCCESS;
  7785. }
  7786. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7787. script_pushint(st,0);
  7788. return SCRIPT_CMD_FAILURE;
  7789. }
  7790. /**
  7791. * statusup <stat>{,<char_id>};
  7792. **/
  7793. BUILDIN_FUNC(statusup)
  7794. {
  7795. int type;
  7796. TBL_PC *sd;
  7797. type = script_getnum(st,2);
  7798. if (!script_charid2sd(3, sd))
  7799. return SCRIPT_CMD_FAILURE;
  7800. pc_statusup(sd, type, 1);
  7801. return SCRIPT_CMD_SUCCESS;
  7802. }
  7803. /**
  7804. * statusup2 <stat>,<amount>{,<char_id>};
  7805. **/
  7806. BUILDIN_FUNC(statusup2)
  7807. {
  7808. int type,val;
  7809. TBL_PC *sd;
  7810. type=script_getnum(st,2);
  7811. val=script_getnum(st,3);
  7812. if (!script_charid2sd(4, sd))
  7813. return SCRIPT_CMD_FAILURE;
  7814. pc_statusup2(sd,type,val);
  7815. return SCRIPT_CMD_SUCCESS;
  7816. }
  7817. /// See 'doc/item_bonus.txt'
  7818. ///
  7819. /// bonus <bonus type>,<val1>;
  7820. /// bonus2 <bonus type>,<val1>,<val2>;
  7821. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7822. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7823. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7824. BUILDIN_FUNC(bonus)
  7825. {
  7826. int type;
  7827. int val1;
  7828. int val2 = 0;
  7829. int val3 = 0;
  7830. int val4 = 0;
  7831. int val5 = 0;
  7832. TBL_PC* sd;
  7833. struct script_data *data;
  7834. sd = script_rid2sd(st);
  7835. if( sd == NULL )
  7836. return SCRIPT_CMD_SUCCESS; // no player attached
  7837. type = script_getnum(st,2);
  7838. switch( type ) {
  7839. case SP_AUTOSPELL:
  7840. case SP_AUTOSPELL_WHENHIT:
  7841. case SP_AUTOSPELL_ONSKILL:
  7842. case SP_SKILL_ATK:
  7843. case SP_SKILL_HEAL:
  7844. case SP_SKILL_HEAL2:
  7845. case SP_ADD_SKILL_BLOW:
  7846. case SP_CASTRATE:
  7847. case SP_ADDEFF_ONSKILL:
  7848. case SP_SKILL_USE_SP_RATE:
  7849. case SP_SKILL_COOLDOWN:
  7850. case SP_SKILL_FIXEDCAST:
  7851. case SP_SKILL_VARIABLECAST:
  7852. case SP_VARCASTRATE:
  7853. case SP_FIXCASTRATE:
  7854. case SP_SKILL_USE_SP:
  7855. case SP_SUB_SKILL:
  7856. // these bonuses support skill names
  7857. data = script_getdata(st, 3);
  7858. get_val(st, data); // Convert into value in case of a variable
  7859. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7860. break;
  7861. default:
  7862. val1 = script_getnum(st,3);
  7863. break;
  7864. }
  7865. switch( script_lastdata(st)-2 ) {
  7866. case 1:
  7867. pc_bonus(sd, type, val1);
  7868. break;
  7869. case 2:
  7870. val2 = script_getnum(st,4);
  7871. pc_bonus2(sd, type, val1, val2);
  7872. break;
  7873. case 3:
  7874. val2 = script_getnum(st,4);
  7875. val3 = script_getnum(st,5);
  7876. pc_bonus3(sd, type, val1, val2, val3);
  7877. break;
  7878. case 4:
  7879. data = script_getdata(st, 4);
  7880. get_val(st, data); // Convert into value in case of a variable
  7881. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7882. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7883. else
  7884. val2 = script_getnum(st,4);
  7885. val3 = script_getnum(st,5);
  7886. val4 = script_getnum(st,6);
  7887. pc_bonus4(sd, type, val1, val2, val3, val4);
  7888. break;
  7889. case 5:
  7890. data = script_getdata(st, 4);
  7891. get_val(st, data); // Convert into value in case of a variable
  7892. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7893. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7894. else
  7895. val2 = script_getnum(st,4);
  7896. val3 = script_getnum(st,5);
  7897. val4 = script_getnum(st,6);
  7898. val5 = script_getnum(st,7);
  7899. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7900. break;
  7901. default:
  7902. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7903. break;
  7904. }
  7905. return SCRIPT_CMD_SUCCESS;
  7906. }
  7907. BUILDIN_FUNC(autobonus)
  7908. {
  7909. unsigned int dur, pos;
  7910. short rate;
  7911. short atk_type = 0;
  7912. TBL_PC* sd;
  7913. const char *bonus_script, *other_script = NULL;
  7914. sd = script_rid2sd(st);
  7915. if( sd == NULL )
  7916. return SCRIPT_CMD_SUCCESS; // no player attached
  7917. if (current_equip_combo_pos)
  7918. pos = current_equip_combo_pos;
  7919. else
  7920. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7921. if((sd->state.autobonus&pos) == pos)
  7922. return SCRIPT_CMD_SUCCESS;
  7923. rate = script_getnum(st,3);
  7924. dur = script_getnum(st,4);
  7925. bonus_script = script_getstr(st,2);
  7926. if( !rate || !dur || !pos || !bonus_script )
  7927. return SCRIPT_CMD_SUCCESS;
  7928. if( script_hasdata(st,5) )
  7929. atk_type = script_getnum(st,5);
  7930. if( script_hasdata(st,6) )
  7931. other_script = script_getstr(st,6);
  7932. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7933. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7934. {
  7935. script_add_autobonus(bonus_script);
  7936. if( other_script )
  7937. script_add_autobonus(other_script);
  7938. }
  7939. return SCRIPT_CMD_SUCCESS;
  7940. }
  7941. BUILDIN_FUNC(autobonus2)
  7942. {
  7943. unsigned int dur, pos;
  7944. short rate;
  7945. short atk_type = 0;
  7946. TBL_PC* sd;
  7947. const char *bonus_script, *other_script = NULL;
  7948. sd = script_rid2sd(st);
  7949. if( sd == NULL )
  7950. return SCRIPT_CMD_SUCCESS; // no player attached
  7951. if (current_equip_combo_pos)
  7952. pos = current_equip_combo_pos;
  7953. else
  7954. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7955. if((sd->state.autobonus&pos) == pos)
  7956. return SCRIPT_CMD_SUCCESS;
  7957. rate = script_getnum(st,3);
  7958. dur = script_getnum(st,4);
  7959. bonus_script = script_getstr(st,2);
  7960. if( !rate || !dur || !pos || !bonus_script )
  7961. return SCRIPT_CMD_SUCCESS;
  7962. if( script_hasdata(st,5) )
  7963. atk_type = script_getnum(st,5);
  7964. if( script_hasdata(st,6) )
  7965. other_script = script_getstr(st,6);
  7966. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7967. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7968. {
  7969. script_add_autobonus(bonus_script);
  7970. if( other_script )
  7971. script_add_autobonus(other_script);
  7972. }
  7973. return SCRIPT_CMD_SUCCESS;
  7974. }
  7975. BUILDIN_FUNC(autobonus3)
  7976. {
  7977. unsigned int dur, pos;
  7978. short rate,atk_type;
  7979. TBL_PC* sd;
  7980. const char *bonus_script, *other_script = NULL;
  7981. struct script_data *data;
  7982. sd = script_rid2sd(st);
  7983. if( sd == NULL )
  7984. return SCRIPT_CMD_SUCCESS; // no player attached
  7985. if (current_equip_combo_pos)
  7986. pos = current_equip_combo_pos;
  7987. else
  7988. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7989. if((sd->state.autobonus&pos) == pos)
  7990. return SCRIPT_CMD_SUCCESS;
  7991. rate = script_getnum(st,3);
  7992. dur = script_getnum(st,4);
  7993. data = script_getdata(st, 5);
  7994. get_val(st, data); // Convert into value in case of a variable
  7995. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7996. bonus_script = script_getstr(st,2);
  7997. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  7998. return SCRIPT_CMD_SUCCESS;
  7999. if( script_hasdata(st,6) )
  8000. other_script = script_getstr(st,6);
  8001. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  8002. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  8003. {
  8004. script_add_autobonus(bonus_script);
  8005. if( other_script )
  8006. script_add_autobonus(other_script);
  8007. }
  8008. return SCRIPT_CMD_SUCCESS;
  8009. }
  8010. /// Changes the level of a player skill.
  8011. /// <flag> defaults to 1
  8012. /// <flag>=0 : set the level of the skill
  8013. /// <flag>=1 : set the temporary level of the skill
  8014. /// <flag>=2 : add to the level of the skill
  8015. ///
  8016. /// skill <skill id>,<level>,<flag>
  8017. /// skill <skill id>,<level>
  8018. /// skill "<skill name>",<level>,<flag>
  8019. /// skill "<skill name>",<level>
  8020. BUILDIN_FUNC(skill)
  8021. {
  8022. int id;
  8023. int level;
  8024. int flag = ADDSKILL_TEMP;
  8025. TBL_PC* sd;
  8026. struct script_data *data;
  8027. const char* command = script_getfuncname(st);
  8028. sd = script_rid2sd(st);
  8029. if( sd == NULL )
  8030. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8031. if (strcmpi(command, "addtoskill") == 0)
  8032. flag = ADDSKILL_TEMP_ADDLEVEL;
  8033. data = script_getdata(st, 2);
  8034. get_val(st, data); // Convert into value in case of a variable
  8035. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8036. level = script_getnum(st,3);
  8037. if( script_hasdata(st,4) )
  8038. flag = script_getnum(st,4);
  8039. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8040. return SCRIPT_CMD_SUCCESS;
  8041. }
  8042. /// Increases the level of a guild skill.
  8043. ///
  8044. /// guildskill <skill id>,<amount>;
  8045. /// guildskill "<skill name>",<amount>;
  8046. BUILDIN_FUNC(guildskill)
  8047. {
  8048. int id;
  8049. int level;
  8050. TBL_PC* sd;
  8051. int i;
  8052. struct script_data *data;
  8053. sd = script_rid2sd(st);
  8054. if( sd == NULL )
  8055. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8056. data = script_getdata(st, 2);
  8057. get_val(st, data); // Convert into value in case of a variable
  8058. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8059. level = script_getnum(st,3);
  8060. for( i=0; i < level; i++ )
  8061. guild_skillup(sd, id);
  8062. return SCRIPT_CMD_SUCCESS;
  8063. }
  8064. /// Returns the level of the player skill.
  8065. ///
  8066. /// getskilllv(<skill id>) -> <level>
  8067. /// getskilllv("<skill name>") -> <level>
  8068. BUILDIN_FUNC(getskilllv)
  8069. {
  8070. int id;
  8071. TBL_PC* sd;
  8072. struct script_data *data;
  8073. sd = script_rid2sd(st);
  8074. if( sd == NULL )
  8075. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8076. data = script_getdata(st, 2);
  8077. get_val(st, data); // Convert into value in case of a variable
  8078. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8079. script_pushint(st, pc_checkskill(sd,id));
  8080. return SCRIPT_CMD_SUCCESS;
  8081. }
  8082. /// Returns the level of the guild skill.
  8083. ///
  8084. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8085. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8086. BUILDIN_FUNC(getgdskilllv)
  8087. {
  8088. int guild_id;
  8089. uint16 skill_id;
  8090. struct guild* g;
  8091. struct script_data *data;
  8092. guild_id = script_getnum(st,2);
  8093. data = script_getdata(st, 3);
  8094. get_val(st, data); // Convert into value in case of a variable
  8095. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8096. g = guild_search(guild_id);
  8097. if( g == NULL )
  8098. script_pushint(st, -1);
  8099. else
  8100. script_pushint(st, guild_checkskill(g,skill_id));
  8101. return SCRIPT_CMD_SUCCESS;
  8102. }
  8103. /// Returns the 'basic_skill_check' setting.
  8104. /// This config determines if the server checks the skill level of NV_BASIC
  8105. /// before allowing the basic actions.
  8106. ///
  8107. /// basicskillcheck() -> <bool>
  8108. BUILDIN_FUNC(basicskillcheck)
  8109. {
  8110. script_pushint(st, battle_config.basic_skill_check);
  8111. return SCRIPT_CMD_SUCCESS;
  8112. }
  8113. /// Returns the GM level of the player.
  8114. ///
  8115. /// getgmlevel({<char_id>}) -> <level>
  8116. BUILDIN_FUNC(getgmlevel)
  8117. {
  8118. TBL_PC* sd;
  8119. if (!script_charid2sd(2,sd))
  8120. return SCRIPT_CMD_FAILURE;
  8121. script_pushint(st, pc_get_group_level(sd));
  8122. return SCRIPT_CMD_SUCCESS;
  8123. }
  8124. /// Returns the group ID of the player.
  8125. ///
  8126. /// getgroupid({<char_id>}) -> <int>
  8127. BUILDIN_FUNC(getgroupid)
  8128. {
  8129. TBL_PC* sd;
  8130. if (!script_charid2sd(2,sd))
  8131. return SCRIPT_CMD_FAILURE;
  8132. script_pushint(st, pc_get_group_id(sd));
  8133. return SCRIPT_CMD_SUCCESS;
  8134. }
  8135. /// Terminates the execution of this script instance.
  8136. ///
  8137. /// end
  8138. BUILDIN_FUNC(end)
  8139. {
  8140. TBL_PC* sd;
  8141. sd = map_id2sd(st->rid);
  8142. st->state = END;
  8143. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  8144. int i;
  8145. for(i = 0; i < st->stack->sp; i++) {
  8146. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  8147. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  8148. st->script = ri->script;
  8149. break;
  8150. }
  8151. }
  8152. }
  8153. if( st->mes_active )
  8154. st->mes_active = 0;
  8155. if (sd){
  8156. if (sd->state.callshop == 0)
  8157. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8158. else
  8159. sd->state.callshop = 0;
  8160. }
  8161. return SCRIPT_CMD_SUCCESS;
  8162. }
  8163. /// Checks if the player has that effect state (option).
  8164. ///
  8165. /// checkoption(<option>{,<char_id>}) -> <bool>
  8166. BUILDIN_FUNC(checkoption)
  8167. {
  8168. int option;
  8169. TBL_PC* sd;
  8170. if (!script_charid2sd(3,sd))
  8171. return SCRIPT_CMD_FAILURE;
  8172. option = script_getnum(st,2);
  8173. if( sd->sc.option&option )
  8174. script_pushint(st, 1);
  8175. else
  8176. script_pushint(st, 0);
  8177. return SCRIPT_CMD_SUCCESS;
  8178. }
  8179. /// Checks if the player is in that body state (opt1).
  8180. ///
  8181. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8182. BUILDIN_FUNC(checkoption1)
  8183. {
  8184. int opt1;
  8185. TBL_PC* sd;
  8186. if (!script_charid2sd(3,sd))
  8187. return SCRIPT_CMD_FAILURE;
  8188. opt1 = script_getnum(st,2);
  8189. if( sd->sc.opt1 == opt1 )
  8190. script_pushint(st, 1);
  8191. else
  8192. script_pushint(st, 0);
  8193. return SCRIPT_CMD_SUCCESS;
  8194. }
  8195. /// Checks if the player has that health state (opt2).
  8196. ///
  8197. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8198. BUILDIN_FUNC(checkoption2)
  8199. {
  8200. int opt2;
  8201. TBL_PC* sd;
  8202. if (!script_charid2sd(3,sd))
  8203. return SCRIPT_CMD_FAILURE;
  8204. opt2 = script_getnum(st,2);
  8205. if( sd->sc.opt2&opt2 )
  8206. script_pushint(st, 1);
  8207. else
  8208. script_pushint(st, 0);
  8209. return SCRIPT_CMD_SUCCESS;
  8210. }
  8211. /// Changes the effect state (option) of the player.
  8212. /// <flag> defaults to 1
  8213. /// <flag>=0 : removes the option
  8214. /// <flag>=other : adds the option
  8215. ///
  8216. /// setoption <option>{,<flag>{,<char_id>}};
  8217. BUILDIN_FUNC(setoption)
  8218. {
  8219. int option;
  8220. int flag = 1;
  8221. TBL_PC* sd;
  8222. if (!script_charid2sd(4,sd))
  8223. return SCRIPT_CMD_FAILURE;
  8224. option = script_getnum(st,2);
  8225. if( script_hasdata(st,3) )
  8226. flag = script_getnum(st,3);
  8227. else if( !option ){// Request to remove everything.
  8228. flag = 0;
  8229. option = OPTION_FALCON|OPTION_RIDING;
  8230. #ifndef NEW_CARTS
  8231. option |= OPTION_CART;
  8232. #endif
  8233. }
  8234. if( flag ){// Add option
  8235. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8236. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8237. pc_setoption(sd, sd->sc.option|option);
  8238. } else// Remove option
  8239. pc_setoption(sd, sd->sc.option&~option);
  8240. return SCRIPT_CMD_SUCCESS;
  8241. }
  8242. /// Returns if the player has a cart.
  8243. ///
  8244. /// checkcart({char_id}) -> <bool>
  8245. ///
  8246. /// @author Valaris
  8247. BUILDIN_FUNC(checkcart)
  8248. {
  8249. TBL_PC* sd;
  8250. if (!script_charid2sd(2,sd))
  8251. return SCRIPT_CMD_FAILURE;
  8252. if( pc_iscarton(sd) )
  8253. script_pushint(st, 1);
  8254. else
  8255. script_pushint(st, 0);
  8256. return SCRIPT_CMD_SUCCESS;
  8257. }
  8258. /// Sets the cart of the player.
  8259. /// <type> defaults to 1
  8260. /// <type>=0 : removes the cart
  8261. /// <type>=1 : Normal cart
  8262. /// <type>=2 : Wooden cart
  8263. /// <type>=3 : Covered cart with flowers and ferns
  8264. /// <type>=4 : Wooden cart with a Panda doll on the back
  8265. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8266. ///
  8267. /// setcart {<type>{,<char_id>}};
  8268. BUILDIN_FUNC(setcart)
  8269. {
  8270. int type = 1;
  8271. TBL_PC* sd;
  8272. if (!script_charid2sd(3,sd))
  8273. return SCRIPT_CMD_FAILURE;
  8274. if( script_hasdata(st,2) )
  8275. type = script_getnum(st,2);
  8276. pc_setcart(sd, type);
  8277. return SCRIPT_CMD_SUCCESS;
  8278. }
  8279. /// Returns if the player has a falcon.
  8280. ///
  8281. /// checkfalcon({char_id}) -> <bool>
  8282. ///
  8283. /// @author Valaris
  8284. BUILDIN_FUNC(checkfalcon)
  8285. {
  8286. TBL_PC* sd;
  8287. if (!script_charid2sd(2,sd))
  8288. return SCRIPT_CMD_FAILURE;
  8289. if( pc_isfalcon(sd) )
  8290. script_pushint(st, 1);
  8291. else
  8292. script_pushint(st, 0);
  8293. return SCRIPT_CMD_SUCCESS;
  8294. }
  8295. /// Sets if the player has a falcon or not.
  8296. /// <flag> defaults to 1
  8297. ///
  8298. /// setfalcon {<flag>{,<char_id>}};
  8299. BUILDIN_FUNC(setfalcon)
  8300. {
  8301. int flag = 1;
  8302. TBL_PC* sd;
  8303. if (!script_charid2sd(3,sd))
  8304. return SCRIPT_CMD_FAILURE;
  8305. if( script_hasdata(st,2) )
  8306. flag = script_getnum(st,2);
  8307. pc_setfalcon(sd, flag);
  8308. return SCRIPT_CMD_SUCCESS;
  8309. }
  8310. /// Returns if the player is riding.
  8311. ///
  8312. /// checkriding({char_id}) -> <bool>
  8313. ///
  8314. /// @author Valaris
  8315. BUILDIN_FUNC(checkriding)
  8316. {
  8317. TBL_PC* sd;
  8318. if (!script_charid2sd(2,sd))
  8319. return SCRIPT_CMD_FAILURE;
  8320. if( pc_isriding(sd) )
  8321. script_pushint(st, 1);
  8322. else
  8323. script_pushint(st, 0);
  8324. return SCRIPT_CMD_SUCCESS;
  8325. }
  8326. /// Sets if the player is riding.
  8327. /// <flag> defaults to 1
  8328. ///
  8329. /// setriding {<flag>{,<char_id>}};
  8330. BUILDIN_FUNC(setriding)
  8331. {
  8332. int flag = 1;
  8333. TBL_PC* sd;
  8334. if (!script_charid2sd(3,sd))
  8335. return SCRIPT_CMD_FAILURE;
  8336. if( script_hasdata(st,2) )
  8337. flag = script_getnum(st,2);
  8338. pc_setriding(sd, flag);
  8339. return SCRIPT_CMD_SUCCESS;
  8340. }
  8341. /// Returns if the player has a warg.
  8342. ///
  8343. /// checkwug({char_id}) -> <bool>
  8344. ///
  8345. BUILDIN_FUNC(checkwug)
  8346. {
  8347. TBL_PC* sd;
  8348. if (!script_charid2sd(2,sd))
  8349. return SCRIPT_CMD_FAILURE;
  8350. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8351. script_pushint(st, 1);
  8352. else
  8353. script_pushint(st, 0);
  8354. return SCRIPT_CMD_SUCCESS;
  8355. }
  8356. /// Returns if the player is wearing MADO Gear.
  8357. ///
  8358. /// checkmadogear({<char_id>}) -> <bool>
  8359. ///
  8360. BUILDIN_FUNC(checkmadogear)
  8361. {
  8362. TBL_PC* sd;
  8363. if (!script_charid2sd(2,sd))
  8364. return SCRIPT_CMD_FAILURE;
  8365. if( pc_ismadogear(sd) )
  8366. script_pushint(st, 1);
  8367. else
  8368. script_pushint(st, 0);
  8369. return SCRIPT_CMD_SUCCESS;
  8370. }
  8371. /// Sets if the player is riding MADO Gear.
  8372. /// <flag> defaults to 1
  8373. ///
  8374. /// setmadogear {<flag>{,<char_id>}};
  8375. BUILDIN_FUNC(setmadogear)
  8376. {
  8377. int flag = 1;
  8378. TBL_PC* sd;
  8379. if (!script_charid2sd(3,sd))
  8380. return SCRIPT_CMD_FAILURE;
  8381. if( script_hasdata(st,2) )
  8382. flag = script_getnum(st,2);
  8383. pc_setmadogear(sd, flag);
  8384. return SCRIPT_CMD_SUCCESS;
  8385. }
  8386. /// Sets the save point of the player.
  8387. ///
  8388. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8389. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8390. BUILDIN_FUNC(savepoint)
  8391. {
  8392. int x, y, m, cid_pos = 5;
  8393. unsigned short map_idx;
  8394. const char* str;
  8395. TBL_PC* sd;
  8396. if (script_lastdata(st) > 5)
  8397. cid_pos = 7;
  8398. if (!script_charid2sd(cid_pos,sd))
  8399. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8400. str = script_getstr(st, 2);
  8401. map_idx = mapindex_name2id(str);
  8402. if( !map_idx )
  8403. return SCRIPT_CMD_FAILURE;
  8404. x = script_getnum(st,3);
  8405. y = script_getnum(st,4);
  8406. m = map_mapindex2mapid(map_idx);
  8407. if (cid_pos == 7) {
  8408. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  8409. x1 = x + dx, y1 = y + dy,
  8410. x0 = x - dx, y0 = y - dy;
  8411. uint8 n = 10;
  8412. do {
  8413. x = x0 + rnd()%(x1-x0+1);
  8414. y = y0 + rnd()%(y1-y0+1);
  8415. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  8416. }
  8417. // Check for valid coordinates if map in local map-server
  8418. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  8419. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  8420. return SCRIPT_CMD_FAILURE;
  8421. }
  8422. pc_setsavepoint(sd, map_idx, x, y);
  8423. return SCRIPT_CMD_SUCCESS;
  8424. }
  8425. /*==========================================
  8426. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  8427. *------------------------------------------*/
  8428. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8429. {
  8430. int type;
  8431. time_t timer;
  8432. struct tm *t;
  8433. type=script_getnum(st,2);
  8434. switch(type){
  8435. case 2:
  8436. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8437. // from the system clock.)
  8438. script_pushint(st,(int)time(NULL));
  8439. break;
  8440. case 1:
  8441. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8442. time(&timer);
  8443. t=localtime(&timer);
  8444. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8445. break;
  8446. case 0:
  8447. default:
  8448. //type 0:(System Ticks)
  8449. script_pushint(st,gettick());
  8450. break;
  8451. }
  8452. return SCRIPT_CMD_SUCCESS;
  8453. }
  8454. /*==========================================
  8455. * GetTime(Type)
  8456. *
  8457. * Returns the current value of a certain date type.
  8458. * Possible types are:
  8459. * - DT_SECOND Current seconds
  8460. * - DT_MINUTE Current minute
  8461. * - DT_HOUR Current hour
  8462. * - DT_DAYOFWEEK Day of current week
  8463. * - DT_DAYOFMONTH Day of current month
  8464. * - DT_MONTH Current month
  8465. * - DT_YEAR Current year
  8466. * - DT_DAYOFYEAR Day of current year
  8467. *
  8468. * If none of the above types is supplied -1 will be returned to the script
  8469. * and the script source will be reported into the mapserver console.
  8470. *------------------------------------------*/
  8471. BUILDIN_FUNC(gettime)
  8472. {
  8473. int type;
  8474. type = script_getnum(st,2);
  8475. if( type <= DT_MIN || type >= DT_MAX ){
  8476. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  8477. script_reportsrc(st);
  8478. script_pushint(st,-1);
  8479. }else{
  8480. script_pushint(st,date_get((enum e_date_type)type));
  8481. }
  8482. return SCRIPT_CMD_SUCCESS;
  8483. }
  8484. /*==========================================
  8485. * GetTimeStr("TimeFMT", Length);
  8486. *------------------------------------------*/
  8487. BUILDIN_FUNC(gettimestr)
  8488. {
  8489. char *tmpstr;
  8490. const char *fmtstr;
  8491. int maxlen;
  8492. time_t now = time(NULL);
  8493. fmtstr=script_getstr(st,2);
  8494. maxlen=script_getnum(st,3);
  8495. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8496. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8497. tmpstr[maxlen]='\0';
  8498. script_pushstr(st,tmpstr);
  8499. return SCRIPT_CMD_SUCCESS;
  8500. }
  8501. /*==========================================
  8502. * Open player storage
  8503. *------------------------------------------*/
  8504. BUILDIN_FUNC(openstorage)
  8505. {
  8506. TBL_PC* sd;
  8507. sd = script_rid2sd(st);
  8508. if( sd == NULL )
  8509. return SCRIPT_CMD_SUCCESS;
  8510. storage_storageopen(sd);
  8511. return SCRIPT_CMD_SUCCESS;
  8512. }
  8513. BUILDIN_FUNC(guildopenstorage)
  8514. {
  8515. TBL_PC* sd;
  8516. int ret;
  8517. sd = script_rid2sd(st);
  8518. if( sd == NULL )
  8519. return SCRIPT_CMD_SUCCESS;
  8520. ret = storage_guild_storageopen(sd);
  8521. script_pushint(st,ret);
  8522. return SCRIPT_CMD_SUCCESS;
  8523. }
  8524. /*==========================================
  8525. * Make player use a skill trought item usage
  8526. *------------------------------------------*/
  8527. /// itemskill <skill id>,<level>
  8528. /// itemskill "<skill name>",<level>
  8529. BUILDIN_FUNC(itemskill)
  8530. {
  8531. int id;
  8532. int lv;
  8533. bool keep_requirement;
  8534. TBL_PC* sd;
  8535. struct script_data *data;
  8536. sd = script_rid2sd(st);
  8537. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8538. return SCRIPT_CMD_SUCCESS;
  8539. data = script_getdata(st, 2);
  8540. get_val(st, data); // Convert into value in case of a variable
  8541. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8542. lv = script_getnum(st,3);
  8543. if (script_hasdata(st, 4)) {
  8544. keep_requirement = (script_getnum(st, 4) != 0);
  8545. } else {
  8546. keep_requirement = false;
  8547. }
  8548. sd->skillitem=id;
  8549. sd->skillitemlv=lv;
  8550. sd->skillitem_keep_requirement = keep_requirement;
  8551. clif_item_skill(sd,id,lv);
  8552. return SCRIPT_CMD_SUCCESS;
  8553. }
  8554. /*==========================================
  8555. * Attempt to create an item
  8556. *------------------------------------------*/
  8557. BUILDIN_FUNC(produce)
  8558. {
  8559. int trigger;
  8560. TBL_PC* sd;
  8561. sd = script_rid2sd(st);
  8562. if( sd == NULL )
  8563. return SCRIPT_CMD_SUCCESS;
  8564. trigger=script_getnum(st,2);
  8565. clif_skill_produce_mix_list(sd, -1, trigger);
  8566. return SCRIPT_CMD_SUCCESS;
  8567. }
  8568. /*==========================================
  8569. *
  8570. *------------------------------------------*/
  8571. BUILDIN_FUNC(cooking)
  8572. {
  8573. int trigger;
  8574. TBL_PC* sd;
  8575. sd = script_rid2sd(st);
  8576. if( sd == NULL )
  8577. return SCRIPT_CMD_SUCCESS;
  8578. trigger=script_getnum(st,2);
  8579. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8580. return SCRIPT_CMD_SUCCESS;
  8581. }
  8582. /*==========================================
  8583. * Create a pet
  8584. *------------------------------------------*/
  8585. BUILDIN_FUNC(makepet)
  8586. {
  8587. TBL_PC* sd;
  8588. int id,pet_id;
  8589. id=script_getnum(st,2);
  8590. sd = script_rid2sd(st);
  8591. if( sd == NULL )
  8592. return SCRIPT_CMD_SUCCESS;
  8593. pet_id = search_petDB_index(id, PET_CLASS);
  8594. if (pet_id < 0)
  8595. pet_id = search_petDB_index(id, PET_EGG);
  8596. if (pet_id >= 0 && sd) {
  8597. sd->catch_target_class = pet_db[pet_id].class_;
  8598. intif_create_pet(
  8599. sd->status.account_id, sd->status.char_id,
  8600. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8601. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8602. 100, 0, 1, pet_db[pet_id].jname);
  8603. }
  8604. return SCRIPT_CMD_SUCCESS;
  8605. }
  8606. /**
  8607. * Give player exp base,job * quest_exp_rate/100
  8608. * getexp <base xp>,<job xp>{,<char_id>};
  8609. **/
  8610. BUILDIN_FUNC(getexp)
  8611. {
  8612. TBL_PC* sd;
  8613. int base=0,job=0;
  8614. double bonus;
  8615. if (!script_charid2sd(4,sd))
  8616. return SCRIPT_CMD_FAILURE;
  8617. base=script_getnum(st,2);
  8618. job =script_getnum(st,3);
  8619. if(base<0 || job<0)
  8620. return SCRIPT_CMD_SUCCESS;
  8621. // bonus for npc-given exp
  8622. bonus = battle_config.quest_exp_rate / 100.;
  8623. if (base)
  8624. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8625. if (job)
  8626. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8627. pc_gainexp(sd, NULL, base, job, 1);
  8628. return SCRIPT_CMD_SUCCESS;
  8629. }
  8630. /*==========================================
  8631. * Gain guild exp [Celest]
  8632. *------------------------------------------*/
  8633. BUILDIN_FUNC(guildgetexp)
  8634. {
  8635. TBL_PC* sd;
  8636. int exp;
  8637. sd = script_rid2sd(st);
  8638. if( sd == NULL )
  8639. return SCRIPT_CMD_SUCCESS;
  8640. exp = script_getnum(st,2);
  8641. if(exp < 0)
  8642. return SCRIPT_CMD_SUCCESS;
  8643. if(sd && sd->status.guild_id > 0)
  8644. guild_getexp (sd, exp);
  8645. return SCRIPT_CMD_SUCCESS;
  8646. }
  8647. /*==========================================
  8648. * Changes the guild master of a guild [Skotlex]
  8649. *------------------------------------------*/
  8650. BUILDIN_FUNC(guildchangegm)
  8651. {
  8652. TBL_PC *sd;
  8653. int guild_id;
  8654. const char *name;
  8655. guild_id = script_getnum(st,2);
  8656. name = script_getstr(st,3);
  8657. sd=map_nick2sd(name,false);
  8658. if (!sd)
  8659. script_pushint(st,0);
  8660. else
  8661. script_pushint(st,guild_gm_change(guild_id, sd));
  8662. return SCRIPT_CMD_SUCCESS;
  8663. }
  8664. /*==========================================
  8665. * Spawn a monster:
  8666. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8667. *------------------------------------------*/
  8668. BUILDIN_FUNC(monster)
  8669. {
  8670. const char* mapn = script_getstr(st,2);
  8671. int x = script_getnum(st,3);
  8672. int y = script_getnum(st,4);
  8673. const char* str = script_getstr(st,5);
  8674. int class_ = script_getnum(st,6);
  8675. int amount = script_getnum(st,7);
  8676. const char* event = "";
  8677. unsigned int size = SZ_SMALL;
  8678. unsigned int ai = AI_NONE;
  8679. struct map_session_data* sd;
  8680. int16 m;
  8681. int i;
  8682. if (script_hasdata(st, 8)) {
  8683. event = script_getstr(st, 8);
  8684. check_event(st, event);
  8685. }
  8686. if (script_hasdata(st, 9)) {
  8687. size = script_getnum(st, 9);
  8688. if (size > SZ_BIG) {
  8689. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8690. return SCRIPT_CMD_FAILURE;
  8691. }
  8692. }
  8693. if (script_hasdata(st, 10)) {
  8694. ai = script_getnum(st, 10);
  8695. if (ai >= AI_MAX) {
  8696. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8697. return SCRIPT_CMD_FAILURE;
  8698. }
  8699. }
  8700. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8701. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8702. return SCRIPT_CMD_FAILURE;
  8703. }
  8704. sd = map_id2sd(st->rid);
  8705. if (sd && strcmp(mapn, "this") == 0)
  8706. m = sd->bl.m;
  8707. else
  8708. m = map_mapname2mapid(mapn);
  8709. for(i = 0; i < amount; i++) { //not optimised
  8710. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8711. if (mobid)
  8712. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8713. }
  8714. return SCRIPT_CMD_SUCCESS;
  8715. }
  8716. /*==========================================
  8717. * Request List of Monster Drops
  8718. *------------------------------------------*/
  8719. BUILDIN_FUNC(getmobdrops)
  8720. {
  8721. int class_ = script_getnum(st,2);
  8722. int i, j = 0;
  8723. struct mob_db *mob;
  8724. if( !mobdb_checkid(class_) )
  8725. {
  8726. script_pushint(st, 0);
  8727. return SCRIPT_CMD_SUCCESS;
  8728. }
  8729. mob = mob_db(class_);
  8730. for( i = 0; i < MAX_MOB_DROP; i++ )
  8731. {
  8732. if( mob->dropitem[i].nameid < 1 )
  8733. continue;
  8734. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8735. continue;
  8736. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8737. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8738. j++;
  8739. }
  8740. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8741. script_pushint(st, 1);
  8742. return SCRIPT_CMD_SUCCESS;
  8743. }
  8744. /*==========================================
  8745. * Spawn a monster in a random location
  8746. * in x0,x1,y0,y1 area.
  8747. *------------------------------------------*/
  8748. BUILDIN_FUNC(areamonster)
  8749. {
  8750. const char* mapn = script_getstr(st,2);
  8751. int x0 = script_getnum(st,3);
  8752. int y0 = script_getnum(st,4);
  8753. int x1 = script_getnum(st,5);
  8754. int y1 = script_getnum(st,6);
  8755. const char* str = script_getstr(st,7);
  8756. int class_ = script_getnum(st,8);
  8757. int amount = script_getnum(st,9);
  8758. const char* event = "";
  8759. unsigned int size = SZ_SMALL;
  8760. unsigned int ai = AI_NONE;
  8761. struct map_session_data* sd;
  8762. int16 m;
  8763. int i;
  8764. if (script_hasdata(st,10)) {
  8765. event = script_getstr(st, 10);
  8766. check_event(st, event);
  8767. }
  8768. if (script_hasdata(st, 11)) {
  8769. size = script_getnum(st, 11);
  8770. if (size > 3) {
  8771. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8772. return SCRIPT_CMD_FAILURE;
  8773. }
  8774. }
  8775. if (script_hasdata(st, 12)) {
  8776. ai = script_getnum(st, 12);
  8777. if (ai >= AI_MAX) {
  8778. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8779. return SCRIPT_CMD_FAILURE;
  8780. }
  8781. }
  8782. sd = map_id2sd(st->rid);
  8783. if (sd && strcmp(mapn, "this") == 0)
  8784. m = sd->bl.m;
  8785. else
  8786. m = map_mapname2mapid(mapn);
  8787. for(i = 0; i < amount; i++) { //not optimised
  8788. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8789. if (mobid)
  8790. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8791. }
  8792. return SCRIPT_CMD_SUCCESS;
  8793. }
  8794. /*==========================================
  8795. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8796. *------------------------------------------*/
  8797. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8798. { //same fix but with killmonster instead - stripping events from mobs.
  8799. TBL_MOB* md = (TBL_MOB*)bl;
  8800. char *event=va_arg(ap,char *);
  8801. int allflag=va_arg(ap,int);
  8802. md->state.npc_killmonster = 1;
  8803. if(!allflag){
  8804. if(strcmp(event,md->npc_event)==0)
  8805. status_kill(bl);
  8806. }else{
  8807. if(!md->spawn)
  8808. status_kill(bl);
  8809. }
  8810. md->state.npc_killmonster = 0;
  8811. return SCRIPT_CMD_SUCCESS;
  8812. }
  8813. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8814. {
  8815. TBL_MOB* md = (TBL_MOB*)bl;
  8816. char *event=va_arg(ap,char *);
  8817. int allflag=va_arg(ap,int);
  8818. if(!allflag){
  8819. if(strcmp(event,md->npc_event)==0)
  8820. status_kill(bl);
  8821. }else{
  8822. if(!md->spawn)
  8823. status_kill(bl);
  8824. }
  8825. return SCRIPT_CMD_SUCCESS;
  8826. }
  8827. BUILDIN_FUNC(killmonster)
  8828. {
  8829. const char *mapname,*event;
  8830. int16 m,allflag=0;
  8831. mapname=script_getstr(st,2);
  8832. event=script_getstr(st,3);
  8833. if(strcmp(event,"All")==0)
  8834. allflag = 1;
  8835. else
  8836. check_event(st, event);
  8837. if( (m=map_mapname2mapid(mapname))<0 )
  8838. return SCRIPT_CMD_SUCCESS;
  8839. if( script_hasdata(st,4) ) {
  8840. if ( script_getnum(st,4) == 1 ) {
  8841. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8842. return SCRIPT_CMD_SUCCESS;
  8843. }
  8844. }
  8845. map_freeblock_lock();
  8846. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8847. map_freeblock_unlock();
  8848. return SCRIPT_CMD_SUCCESS;
  8849. }
  8850. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8851. { //Strips the event from the mob if it's killed the old method.
  8852. struct mob_data *md;
  8853. md = BL_CAST(BL_MOB, bl);
  8854. if (md->npc_event[0])
  8855. md->npc_event[0] = 0;
  8856. status_kill(bl);
  8857. return 0;
  8858. }
  8859. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8860. {
  8861. status_kill(bl);
  8862. return 0;
  8863. }
  8864. BUILDIN_FUNC(killmonsterall)
  8865. {
  8866. const char *mapname;
  8867. int16 m;
  8868. mapname=script_getstr(st,2);
  8869. if( (m = map_mapname2mapid(mapname))<0 )
  8870. return SCRIPT_CMD_SUCCESS;
  8871. if( script_hasdata(st,3) ) {
  8872. if ( script_getnum(st,3) == 1 ) {
  8873. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8874. return SCRIPT_CMD_SUCCESS;
  8875. }
  8876. }
  8877. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8878. return SCRIPT_CMD_SUCCESS;
  8879. }
  8880. /*==========================================
  8881. * Creates a clone of a player.
  8882. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8883. *------------------------------------------*/
  8884. BUILDIN_FUNC(clone)
  8885. {
  8886. TBL_PC *sd, *msd=NULL;
  8887. uint32 char_id, master_id = 0, x, y, flag = 0, m;
  8888. enum e_mode mode = 0;
  8889. unsigned int duration = 0;
  8890. const char *mapname,*event;
  8891. mapname=script_getstr(st,2);
  8892. x=script_getnum(st,3);
  8893. y=script_getnum(st,4);
  8894. event=script_getstr(st,5);
  8895. char_id=script_getnum(st,6);
  8896. if( script_hasdata(st,7) )
  8897. master_id=script_getnum(st,7);
  8898. if( script_hasdata(st,8) )
  8899. mode=script_getnum(st,8);
  8900. if( script_hasdata(st,9) )
  8901. flag=script_getnum(st,9);
  8902. if( script_hasdata(st,10) )
  8903. duration=script_getnum(st,10);
  8904. check_event(st, event);
  8905. m = map_mapname2mapid(mapname);
  8906. if (m < 0)
  8907. return SCRIPT_CMD_SUCCESS;
  8908. sd = map_charid2sd(char_id);
  8909. if (master_id) {
  8910. msd = map_charid2sd(master_id);
  8911. if (msd)
  8912. master_id = msd->bl.id;
  8913. else
  8914. master_id = 0;
  8915. }
  8916. if (sd) //Return ID of newly crafted clone.
  8917. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8918. else //Failed to create clone.
  8919. script_pushint(st,0);
  8920. return SCRIPT_CMD_SUCCESS;
  8921. }
  8922. /*==========================================
  8923. *------------------------------------------*/
  8924. BUILDIN_FUNC(doevent)
  8925. {
  8926. const char* event = script_getstr(st,2);
  8927. struct map_session_data* sd;
  8928. if( ( sd = script_rid2sd(st) ) == NULL )
  8929. {
  8930. return SCRIPT_CMD_SUCCESS;
  8931. }
  8932. check_event(st, event);
  8933. npc_event(sd, event, 0);
  8934. return SCRIPT_CMD_SUCCESS;
  8935. }
  8936. /*==========================================
  8937. *------------------------------------------*/
  8938. BUILDIN_FUNC(donpcevent)
  8939. {
  8940. const char* event = script_getstr(st,2);
  8941. check_event(st, event);
  8942. if( !npc_event_do(event) ) {
  8943. struct npc_data * nd = map_id2nd(st->oid);
  8944. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8945. script_pushint(st, 0);
  8946. } else
  8947. script_pushint(st, 1);
  8948. return SCRIPT_CMD_SUCCESS;
  8949. }
  8950. /// for Aegis compatibility
  8951. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8952. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8953. {
  8954. const char* npc = script_getstr(st,2);
  8955. const char* command = script_getstr(st,3);
  8956. char event[EVENT_NAME_LENGTH];
  8957. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  8958. check_event(st, event);
  8959. if( npc_event_do(event) ){
  8960. script_pushint(st, true);
  8961. }else{
  8962. struct npc_data * nd = map_id2nd(st->oid);
  8963. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  8964. script_pushint(st, false);
  8965. }
  8966. return SCRIPT_CMD_SUCCESS;
  8967. }
  8968. /*==========================================
  8969. *------------------------------------------*/
  8970. BUILDIN_FUNC(addtimer)
  8971. {
  8972. int tick = script_getnum(st,2);
  8973. const char* event = script_getstr(st, 3);
  8974. TBL_PC* sd;
  8975. check_event(st, event);
  8976. sd = script_rid2sd(st);
  8977. if( sd == NULL )
  8978. return SCRIPT_CMD_SUCCESS;
  8979. if (!pc_addeventtimer(sd,tick,event)) {
  8980. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8981. return SCRIPT_CMD_FAILURE;
  8982. }
  8983. return SCRIPT_CMD_SUCCESS;
  8984. }
  8985. /*==========================================
  8986. *------------------------------------------*/
  8987. BUILDIN_FUNC(deltimer)
  8988. {
  8989. const char *event;
  8990. TBL_PC* sd;
  8991. event=script_getstr(st, 2);
  8992. sd = script_rid2sd(st);
  8993. if( sd == NULL )
  8994. return SCRIPT_CMD_SUCCESS;
  8995. check_event(st, event);
  8996. pc_deleventtimer(sd,event);
  8997. return SCRIPT_CMD_SUCCESS;
  8998. }
  8999. /*==========================================
  9000. *------------------------------------------*/
  9001. BUILDIN_FUNC(addtimercount)
  9002. {
  9003. const char *event;
  9004. int tick;
  9005. TBL_PC* sd;
  9006. tick=script_getnum(st,2);
  9007. event=script_getstr(st,3);
  9008. sd = script_rid2sd(st);
  9009. if( sd == NULL )
  9010. return SCRIPT_CMD_SUCCESS;
  9011. check_event(st, event);
  9012. pc_addeventtimercount(sd,event,tick);
  9013. return SCRIPT_CMD_SUCCESS;
  9014. }
  9015. /*==========================================
  9016. *------------------------------------------*/
  9017. BUILDIN_FUNC(initnpctimer)
  9018. {
  9019. struct npc_data *nd;
  9020. int flag = 0;
  9021. if( script_hasdata(st,3) )
  9022. { //Two arguments: NPC name and attach flag.
  9023. nd = npc_name2id(script_getstr(st, 2));
  9024. flag = script_getnum(st,3);
  9025. }
  9026. else if( script_hasdata(st,2) )
  9027. { //Check if argument is numeric (flag) or string (npc name)
  9028. struct script_data *data;
  9029. data = script_getdata(st,2);
  9030. get_val(st,data);
  9031. if( data_isstring(data) ) //NPC name
  9032. nd = npc_name2id(conv_str(st, data));
  9033. else if( data_isint(data) ) //Flag
  9034. {
  9035. nd = (struct npc_data *)map_id2bl(st->oid);
  9036. flag = conv_num(st,data);
  9037. }
  9038. else
  9039. {
  9040. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9041. return SCRIPT_CMD_FAILURE;
  9042. }
  9043. }
  9044. else
  9045. nd = (struct npc_data *)map_id2bl(st->oid);
  9046. if( !nd )
  9047. return SCRIPT_CMD_SUCCESS;
  9048. if( flag ) //Attach
  9049. {
  9050. TBL_PC* sd = script_rid2sd(st);
  9051. if( sd == NULL )
  9052. return SCRIPT_CMD_SUCCESS;
  9053. nd->u.scr.rid = sd->bl.id;
  9054. }
  9055. nd->u.scr.timertick = 0;
  9056. npc_settimerevent_tick(nd,0);
  9057. npc_timerevent_start(nd, st->rid);
  9058. return SCRIPT_CMD_SUCCESS;
  9059. }
  9060. /*==========================================
  9061. *------------------------------------------*/
  9062. BUILDIN_FUNC(startnpctimer)
  9063. {
  9064. struct npc_data *nd;
  9065. int flag = 0;
  9066. if( script_hasdata(st,3) )
  9067. { //Two arguments: NPC name and attach flag.
  9068. nd = npc_name2id(script_getstr(st, 2));
  9069. flag = script_getnum(st,3);
  9070. }
  9071. else if( script_hasdata(st,2) )
  9072. { //Check if argument is numeric (flag) or string (npc name)
  9073. struct script_data *data;
  9074. data = script_getdata(st,2);
  9075. get_val(st,data);
  9076. if( data_isstring(data) ) //NPC name
  9077. nd = npc_name2id(conv_str(st, data));
  9078. else if( data_isint(data) ) //Flag
  9079. {
  9080. nd = (struct npc_data *)map_id2bl(st->oid);
  9081. flag = conv_num(st,data);
  9082. }
  9083. else
  9084. {
  9085. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9086. return SCRIPT_CMD_FAILURE;
  9087. }
  9088. }
  9089. else
  9090. nd=(struct npc_data *)map_id2bl(st->oid);
  9091. if( !nd )
  9092. return SCRIPT_CMD_SUCCESS;
  9093. if( flag ) //Attach
  9094. {
  9095. TBL_PC* sd = script_rid2sd(st);
  9096. if( sd == NULL )
  9097. return SCRIPT_CMD_SUCCESS;
  9098. nd->u.scr.rid = sd->bl.id;
  9099. }
  9100. npc_timerevent_start(nd, st->rid);
  9101. return SCRIPT_CMD_SUCCESS;
  9102. }
  9103. /*==========================================
  9104. *------------------------------------------*/
  9105. BUILDIN_FUNC(stopnpctimer)
  9106. {
  9107. struct npc_data *nd;
  9108. int flag = 0;
  9109. if( script_hasdata(st,3) )
  9110. { //Two arguments: NPC name and attach flag.
  9111. nd = npc_name2id(script_getstr(st, 2));
  9112. flag = script_getnum(st,3);
  9113. }
  9114. else if( script_hasdata(st,2) )
  9115. { //Check if argument is numeric (flag) or string (npc name)
  9116. struct script_data *data;
  9117. data = script_getdata(st,2);
  9118. get_val(st,data);
  9119. if( data_isstring(data) ) //NPC name
  9120. nd = npc_name2id(conv_str(st, data));
  9121. else if( data_isint(data) ) //Flag
  9122. {
  9123. nd = (struct npc_data *)map_id2bl(st->oid);
  9124. flag = conv_num(st,data);
  9125. }
  9126. else
  9127. {
  9128. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9129. return SCRIPT_CMD_FAILURE;
  9130. }
  9131. }
  9132. else
  9133. nd=(struct npc_data *)map_id2bl(st->oid);
  9134. if( !nd )
  9135. return SCRIPT_CMD_SUCCESS;
  9136. if( flag ) //Detach
  9137. nd->u.scr.rid = 0;
  9138. npc_timerevent_stop(nd);
  9139. return SCRIPT_CMD_SUCCESS;
  9140. }
  9141. /*==========================================
  9142. *------------------------------------------*/
  9143. BUILDIN_FUNC(getnpctimer)
  9144. {
  9145. struct npc_data *nd;
  9146. TBL_PC *sd;
  9147. int type = script_getnum(st,2);
  9148. int val = 0;
  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,0);
  9156. ShowError("getnpctimer: Invalid NPC.\n");
  9157. return SCRIPT_CMD_FAILURE;
  9158. }
  9159. switch( type )
  9160. {
  9161. case 0: val = npc_gettimerevent_tick(nd); break;
  9162. case 1:
  9163. if( nd->u.scr.rid )
  9164. {
  9165. sd = map_id2sd(nd->u.scr.rid);
  9166. if( !sd )
  9167. {
  9168. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9169. break;
  9170. }
  9171. val = (sd->npc_timer_id != INVALID_TIMER);
  9172. }
  9173. else
  9174. val = (nd->u.scr.timerid != INVALID_TIMER);
  9175. break;
  9176. case 2: val = nd->u.scr.timeramount; break;
  9177. }
  9178. script_pushint(st,val);
  9179. return SCRIPT_CMD_SUCCESS;
  9180. }
  9181. /*==========================================
  9182. *------------------------------------------*/
  9183. BUILDIN_FUNC(setnpctimer)
  9184. {
  9185. int tick;
  9186. struct npc_data *nd;
  9187. tick = script_getnum(st,2);
  9188. if( script_hasdata(st,3) )
  9189. nd = npc_name2id(script_getstr(st,3));
  9190. else
  9191. nd = (struct npc_data *)map_id2bl(st->oid);
  9192. if( !nd || nd->bl.type != BL_NPC )
  9193. {
  9194. script_pushint(st,1);
  9195. ShowError("setnpctimer: Invalid NPC.\n");
  9196. return SCRIPT_CMD_FAILURE;
  9197. }
  9198. npc_settimerevent_tick(nd,tick);
  9199. script_pushint(st,0);
  9200. return SCRIPT_CMD_SUCCESS;
  9201. }
  9202. /*==========================================
  9203. * attaches the player rid to the timer [Celest]
  9204. *------------------------------------------*/
  9205. BUILDIN_FUNC(attachnpctimer)
  9206. {
  9207. TBL_PC *sd;
  9208. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9209. if( !nd || nd->bl.type != BL_NPC )
  9210. {
  9211. script_pushint(st,1);
  9212. ShowError("setnpctimer: Invalid NPC.\n");
  9213. return SCRIPT_CMD_FAILURE;
  9214. }
  9215. if( !script_nick2sd(2,sd) ){
  9216. script_pushint(st,1);
  9217. ShowWarning("attachnpctimer: Invalid player.\n");
  9218. return SCRIPT_CMD_FAILURE;
  9219. }
  9220. nd->u.scr.rid = sd->bl.id;
  9221. script_pushint(st,0);
  9222. return SCRIPT_CMD_SUCCESS;
  9223. }
  9224. /*==========================================
  9225. * detaches a player rid from the timer [Celest]
  9226. *------------------------------------------*/
  9227. BUILDIN_FUNC(detachnpctimer)
  9228. {
  9229. struct npc_data *nd;
  9230. if( script_hasdata(st,2) )
  9231. nd = npc_name2id(script_getstr(st,2));
  9232. else
  9233. nd = (struct npc_data *)map_id2bl(st->oid);
  9234. if( !nd || nd->bl.type != BL_NPC )
  9235. {
  9236. script_pushint(st,1);
  9237. ShowError("detachnpctimer: Invalid NPC.\n");
  9238. return SCRIPT_CMD_FAILURE;
  9239. }
  9240. nd->u.scr.rid = 0;
  9241. script_pushint(st,0);
  9242. return SCRIPT_CMD_SUCCESS;
  9243. }
  9244. /*==========================================
  9245. * To avoid "player not attached" script errors, this function is provided,
  9246. * it checks if there is a player attached to the current script. [Skotlex]
  9247. * If no, returns 0, if yes, returns the account_id of the attached player.
  9248. *------------------------------------------*/
  9249. BUILDIN_FUNC(playerattached)
  9250. {
  9251. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9252. script_pushint(st,0);
  9253. else
  9254. script_pushint(st,st->rid);
  9255. return SCRIPT_CMD_SUCCESS;
  9256. }
  9257. /*==========================================
  9258. *------------------------------------------*/
  9259. BUILDIN_FUNC(announce)
  9260. {
  9261. const char *mes = script_getstr(st,2);
  9262. int flag = script_getnum(st,3);
  9263. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9264. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9265. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9266. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9267. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9268. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9269. {
  9270. send_target target;
  9271. 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
  9272. if (bl == NULL)
  9273. return SCRIPT_CMD_SUCCESS;
  9274. switch (flag&BC_TARGET_MASK) {
  9275. case BC_MAP: target = ALL_SAMEMAP; break;
  9276. case BC_AREA: target = AREA; break;
  9277. case BC_SELF: target = SELF; break;
  9278. default: target = ALL_CLIENT; break; // BC_ALL
  9279. }
  9280. if (fontColor)
  9281. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9282. else
  9283. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9284. }
  9285. else
  9286. {
  9287. if (fontColor)
  9288. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9289. else
  9290. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9291. }
  9292. return SCRIPT_CMD_SUCCESS;
  9293. }
  9294. /*==========================================
  9295. *------------------------------------------*/
  9296. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9297. {
  9298. char *mes = va_arg(ap, char *);
  9299. int len = va_arg(ap, int);
  9300. int type = va_arg(ap, int);
  9301. char *fontColor = va_arg(ap, char *);
  9302. short fontType = (short)va_arg(ap, int);
  9303. short fontSize = (short)va_arg(ap, int);
  9304. short fontAlign = (short)va_arg(ap, int);
  9305. short fontY = (short)va_arg(ap, int);
  9306. if (fontColor)
  9307. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9308. else
  9309. clif_broadcast(bl, mes, len, type, SELF);
  9310. return SCRIPT_CMD_SUCCESS;
  9311. }
  9312. BUILDIN_FUNC(mapannounce)
  9313. {
  9314. const char *mapname = script_getstr(st,2);
  9315. const char *mes = script_getstr(st,3);
  9316. int flag = script_getnum(st,4);
  9317. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9318. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9319. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9320. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9321. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9322. int16 m;
  9323. if ((m = map_mapname2mapid(mapname)) < 0)
  9324. return SCRIPT_CMD_SUCCESS;
  9325. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9326. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9327. return SCRIPT_CMD_SUCCESS;
  9328. }
  9329. /*==========================================
  9330. *------------------------------------------*/
  9331. BUILDIN_FUNC(areaannounce)
  9332. {
  9333. const char *mapname = script_getstr(st,2);
  9334. int x0 = script_getnum(st,3);
  9335. int y0 = script_getnum(st,4);
  9336. int x1 = script_getnum(st,5);
  9337. int y1 = script_getnum(st,6);
  9338. const char *mes = script_getstr(st,7);
  9339. int flag = script_getnum(st,8);
  9340. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9341. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9342. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9343. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9344. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9345. int16 m;
  9346. if ((m = map_mapname2mapid(mapname)) < 0)
  9347. return SCRIPT_CMD_SUCCESS;
  9348. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9349. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9350. return SCRIPT_CMD_SUCCESS;
  9351. }
  9352. /*==========================================
  9353. *------------------------------------------*/
  9354. BUILDIN_FUNC(getusers)
  9355. {
  9356. int flag, val = 0;
  9357. struct map_session_data* sd;
  9358. struct block_list* bl = NULL;
  9359. flag = script_getnum(st,2);
  9360. switch(flag&0x07)
  9361. {
  9362. case 0:
  9363. if(flag&0x8)
  9364. {// npc
  9365. bl = map_id2bl(st->oid);
  9366. }
  9367. else if((sd = script_rid2sd(st))!=NULL)
  9368. {// pc
  9369. bl = &sd->bl;
  9370. }
  9371. if(bl)
  9372. {
  9373. val = map[bl->m].users;
  9374. }
  9375. break;
  9376. case 1:
  9377. val = map_getusers();
  9378. break;
  9379. default:
  9380. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9381. script_pushint(st,0);
  9382. return SCRIPT_CMD_FAILURE;
  9383. }
  9384. script_pushint(st,val);
  9385. return SCRIPT_CMD_SUCCESS;
  9386. }
  9387. /*==========================================
  9388. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9389. *------------------------------------------*/
  9390. BUILDIN_FUNC(getmapguildusers)
  9391. {
  9392. const char *str;
  9393. int16 m;
  9394. int gid;
  9395. int c=0;
  9396. struct guild *g = NULL;
  9397. str=script_getstr(st,2);
  9398. gid=script_getnum(st,3);
  9399. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9400. script_pushint(st,-1);
  9401. return SCRIPT_CMD_SUCCESS;
  9402. }
  9403. g = guild_search(gid);
  9404. if (g){
  9405. unsigned short i;
  9406. for(i = 0; i < g->max_member; i++)
  9407. {
  9408. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9409. c++;
  9410. }
  9411. }
  9412. script_pushint(st,c);
  9413. return SCRIPT_CMD_SUCCESS;
  9414. }
  9415. /*==========================================
  9416. *------------------------------------------*/
  9417. BUILDIN_FUNC(getmapusers)
  9418. {
  9419. const char *str;
  9420. int16 m;
  9421. str=script_getstr(st,2);
  9422. if( (m=map_mapname2mapid(str))< 0){
  9423. script_pushint(st,-1);
  9424. return SCRIPT_CMD_SUCCESS;
  9425. }
  9426. script_pushint(st,map[m].users);
  9427. return SCRIPT_CMD_SUCCESS;
  9428. }
  9429. /*==========================================
  9430. *------------------------------------------*/
  9431. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9432. {
  9433. int *users=va_arg(ap,int *);
  9434. (*users)++;
  9435. return SCRIPT_CMD_SUCCESS;
  9436. }
  9437. BUILDIN_FUNC(getareausers)
  9438. {
  9439. const char *str;
  9440. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9441. str=script_getstr(st,2);
  9442. x0=script_getnum(st,3);
  9443. y0=script_getnum(st,4);
  9444. x1=script_getnum(st,5);
  9445. y1=script_getnum(st,6);
  9446. if( (m=map_mapname2mapid(str))< 0){
  9447. script_pushint(st,-1);
  9448. return SCRIPT_CMD_SUCCESS;
  9449. }
  9450. map_foreachinarea(buildin_getareausers_sub,
  9451. m,x0,y0,x1,y1,BL_PC,&users);
  9452. script_pushint(st,users);
  9453. return SCRIPT_CMD_SUCCESS;
  9454. }
  9455. /*==========================================
  9456. *------------------------------------------*/
  9457. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9458. {
  9459. unsigned short nameid = (unsigned short)va_arg(ap, int);
  9460. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  9461. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9462. if(drop->item.nameid==nameid)
  9463. (*amount)+=drop->item.amount;
  9464. return SCRIPT_CMD_SUCCESS;
  9465. }
  9466. BUILDIN_FUNC(getareadropitem)
  9467. {
  9468. const char *str;
  9469. int16 m,x0,y0,x1,y1;
  9470. unsigned short nameid, amount = 0;
  9471. struct script_data *data;
  9472. str=script_getstr(st,2);
  9473. x0=script_getnum(st,3);
  9474. y0=script_getnum(st,4);
  9475. x1=script_getnum(st,5);
  9476. y1=script_getnum(st,6);
  9477. data=script_getdata(st,7);
  9478. get_val(st,data);
  9479. if( data_isstring(data) ){
  9480. const char *name=conv_str(st,data);
  9481. struct item_data *item_data = itemdb_searchname(name);
  9482. nameid=UNKNOWN_ITEM_ID;
  9483. if( item_data )
  9484. nameid=item_data->nameid;
  9485. }else
  9486. nameid=conv_num(st,data);
  9487. if( (m=map_mapname2mapid(str))< 0){
  9488. script_pushint(st,-1);
  9489. return SCRIPT_CMD_SUCCESS;
  9490. }
  9491. map_foreachinarea(buildin_getareadropitem_sub,
  9492. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9493. script_pushint(st,amount);
  9494. return SCRIPT_CMD_SUCCESS;
  9495. }
  9496. /*==========================================
  9497. *------------------------------------------*/
  9498. BUILDIN_FUNC(enablenpc)
  9499. {
  9500. const char *str;
  9501. str=script_getstr(st,2);
  9502. npc_enable(str,1);
  9503. return SCRIPT_CMD_SUCCESS;
  9504. }
  9505. /*==========================================
  9506. *------------------------------------------*/
  9507. BUILDIN_FUNC(disablenpc)
  9508. {
  9509. const char *str;
  9510. str=script_getstr(st,2);
  9511. npc_enable(str,0);
  9512. return SCRIPT_CMD_SUCCESS;
  9513. }
  9514. /*==========================================
  9515. *------------------------------------------*/
  9516. BUILDIN_FUNC(hideoffnpc)
  9517. {
  9518. const char *str;
  9519. str=script_getstr(st,2);
  9520. npc_enable(str,2);
  9521. return SCRIPT_CMD_SUCCESS;
  9522. }
  9523. /*==========================================
  9524. *------------------------------------------*/
  9525. BUILDIN_FUNC(hideonnpc)
  9526. {
  9527. const char *str;
  9528. str=script_getstr(st,2);
  9529. npc_enable(str,4);
  9530. return SCRIPT_CMD_SUCCESS;
  9531. }
  9532. /* Starts a status effect on the target unit or on the attached player.
  9533. *
  9534. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9535. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9536. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9537. * <flag>: enum e_status_change_start_flags
  9538. */
  9539. BUILDIN_FUNC(sc_start)
  9540. {
  9541. TBL_NPC * nd = map_id2nd(st->oid);
  9542. struct block_list* bl;
  9543. enum sc_type type;
  9544. int tick, val1, val2, val3, val4=0, rate, flag;
  9545. char start_type;
  9546. const char* command = script_getfuncname(st);
  9547. if(strstr(command, "4"))
  9548. start_type = 4;
  9549. else if(strstr(command, "2"))
  9550. start_type = 2;
  9551. else
  9552. start_type = 1;
  9553. type = (sc_type)script_getnum(st,2);
  9554. tick = script_getnum(st,3);
  9555. val1 = script_getnum(st,4);
  9556. //If from NPC we make default flag 1 to be unavoidable
  9557. if(nd && nd->bl.id == fake_nd->bl.id)
  9558. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9559. else
  9560. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9561. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9562. if(script_hasdata(st,(6+start_type)))
  9563. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9564. else
  9565. bl = map_id2bl(st->rid);
  9566. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9567. {// When there isn't a duration specified, try to get it from the skill_db
  9568. tick = skill_get_time(status_sc2skill(type), val1);
  9569. }
  9570. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9571. bl = map_id2bl(potion_target);
  9572. tick /= 2;// Thrown potions only last half.
  9573. val4 = 1;// Mark that this was a thrown sc_effect
  9574. }
  9575. if(!bl)
  9576. return SCRIPT_CMD_SUCCESS;
  9577. switch(start_type) {
  9578. case 1:
  9579. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9580. break;
  9581. case 2:
  9582. val2 = script_getnum(st,5);
  9583. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9584. break;
  9585. case 4:
  9586. val2 = script_getnum(st,5);
  9587. val3 = script_getnum(st,6);
  9588. val4 = script_getnum(st,7);
  9589. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9590. break;
  9591. }
  9592. return SCRIPT_CMD_SUCCESS;
  9593. }
  9594. /// Ends one or all status effects on the target unit or on the attached player.
  9595. ///
  9596. /// sc_end <effect_id>{,<unit_id>};
  9597. BUILDIN_FUNC(sc_end)
  9598. {
  9599. struct block_list* bl;
  9600. int type;
  9601. type = script_getnum(st, 2);
  9602. if (script_hasdata(st, 3))
  9603. bl = map_id2bl(script_getnum(st, 3));
  9604. else
  9605. bl = map_id2bl(st->rid);
  9606. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9607. bl = map_id2bl(potion_target);
  9608. if (!bl)
  9609. return SCRIPT_CMD_SUCCESS;
  9610. if (type >= 0 && type < SC_MAX) {
  9611. struct status_change *sc = status_get_sc(bl);
  9612. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9613. if (!sce)
  9614. return SCRIPT_CMD_SUCCESS;
  9615. switch (type) {
  9616. case SC_WEIGHT50:
  9617. case SC_WEIGHT90:
  9618. case SC_NOCHAT:
  9619. case SC_PUSH_CART:
  9620. case SC_ALL_RIDING:
  9621. case SC_STYLE_CHANGE:
  9622. case SC_MONSTER_TRANSFORM:
  9623. case SC_ACTIVE_MONSTER_TRANSFORM:
  9624. case SC_MTF_ASPD:
  9625. case SC_MTF_RANGEATK:
  9626. case SC_MTF_MATK:
  9627. case SC_MTF_MLEATKED:
  9628. case SC_MTF_CRIDAMAGE:
  9629. case SC_MTF_ASPD2:
  9630. case SC_MTF_RANGEATK2:
  9631. case SC_MTF_MATK2:
  9632. case SC_MTF_MHP:
  9633. case SC_MTF_MSP:
  9634. case SC_MTF_PUMPKIN:
  9635. case SC_MTF_HITFLEE:
  9636. case SC_ATTHASTE_CASH:
  9637. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  9638. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  9639. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  9640. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  9641. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  9642. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  9643. return SCRIPT_CMD_SUCCESS;
  9644. default:
  9645. break;
  9646. }
  9647. //This should help status_change_end force disabling the SC in case it has no limit.
  9648. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9649. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9650. } else
  9651. status_change_clear(bl, 3); // remove all effects
  9652. return SCRIPT_CMD_SUCCESS;
  9653. }
  9654. /*==========================================
  9655. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9656. *------------------------------------------*/
  9657. BUILDIN_FUNC(getscrate)
  9658. {
  9659. struct block_list *bl;
  9660. int type,rate;
  9661. type=script_getnum(st,2);
  9662. rate=script_getnum(st,3);
  9663. if( script_hasdata(st,4) ) //get for the bl assigned
  9664. bl = map_id2bl(script_getnum(st,4));
  9665. else
  9666. bl = map_id2bl(st->rid);
  9667. if (bl)
  9668. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9669. script_pushint(st,rate);
  9670. return SCRIPT_CMD_SUCCESS;
  9671. }
  9672. /**
  9673. * getstatus(<effect type>{,<type>{,<char_id>}});
  9674. **/
  9675. BUILDIN_FUNC(getstatus)
  9676. {
  9677. int id, type;
  9678. struct map_session_data* sd;
  9679. if (!script_charid2sd(4,sd))
  9680. return SCRIPT_CMD_FAILURE;
  9681. id = script_getnum(st, 2);
  9682. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9683. if( id <= SC_NONE || id >= SC_MAX )
  9684. {// invalid status type given
  9685. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9686. return SCRIPT_CMD_SUCCESS;
  9687. }
  9688. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9689. {// no status is active
  9690. script_pushint(st, 0);
  9691. return SCRIPT_CMD_SUCCESS;
  9692. }
  9693. switch( type )
  9694. {
  9695. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9696. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9697. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9698. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9699. case 5:
  9700. {
  9701. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9702. if( timer )
  9703. {// return the amount of time remaining
  9704. script_pushint(st, timer->tick - gettick());
  9705. } else {
  9706. script_pushint(st, -1);
  9707. }
  9708. }
  9709. break;
  9710. default: script_pushint(st, 1); break;
  9711. }
  9712. return SCRIPT_CMD_SUCCESS;
  9713. }
  9714. /*==========================================
  9715. *
  9716. *------------------------------------------*/
  9717. BUILDIN_FUNC(debugmes)
  9718. {
  9719. const char *str;
  9720. str=script_getstr(st,2);
  9721. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9722. return SCRIPT_CMD_SUCCESS;
  9723. }
  9724. /*==========================================
  9725. *------------------------------------------*/
  9726. BUILDIN_FUNC(catchpet)
  9727. {
  9728. int pet_id;
  9729. TBL_PC *sd;
  9730. pet_id= script_getnum(st,2);
  9731. sd=script_rid2sd(st);
  9732. if( sd == NULL )
  9733. return SCRIPT_CMD_SUCCESS;
  9734. pet_catch_process1(sd,pet_id);
  9735. return SCRIPT_CMD_SUCCESS;
  9736. }
  9737. /*==========================================
  9738. * [orn]
  9739. *------------------------------------------*/
  9740. BUILDIN_FUNC(homunculus_evolution)
  9741. {
  9742. TBL_PC *sd;
  9743. sd=script_rid2sd(st);
  9744. if( sd == NULL )
  9745. return SCRIPT_CMD_SUCCESS;
  9746. if(hom_is_active(sd->hd))
  9747. {
  9748. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9749. hom_evolution(sd->hd);
  9750. else
  9751. clif_emotion(&sd->hd->bl, E_SWT);
  9752. }
  9753. return SCRIPT_CMD_SUCCESS;
  9754. }
  9755. /*==========================================
  9756. * Checks for vaporized morph state
  9757. * and deletes ITEMID_STRANGE_EMBRYO.
  9758. *------------------------------------------*/
  9759. BUILDIN_FUNC(homunculus_mutate)
  9760. {
  9761. int homun_id;
  9762. TBL_PC *sd;
  9763. sd = script_rid2sd(st);
  9764. if( sd == NULL || sd->hd == NULL )
  9765. return SCRIPT_CMD_SUCCESS;
  9766. if(script_hasdata(st,2))
  9767. homun_id = script_getnum(st,2);
  9768. else
  9769. homun_id = 6048 + (rnd() % 4);
  9770. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9771. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9772. int m_id = hom_class2mapid(homun_id);
  9773. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9774. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9775. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9776. hom_call(sd); // Respawn homunculus.
  9777. hom_mutate(sd->hd, homun_id);
  9778. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9779. script_pushint(st, 1);
  9780. return SCRIPT_CMD_SUCCESS;
  9781. } else
  9782. clif_emotion(&sd->bl, E_SWT);
  9783. } else
  9784. clif_emotion(&sd->bl, E_SWT);
  9785. script_pushint(st, 0);
  9786. return SCRIPT_CMD_SUCCESS;
  9787. }
  9788. /*==========================================
  9789. * Puts homunculus into morph state
  9790. * and gives ITEMID_STRANGE_EMBRYO.
  9791. *------------------------------------------*/
  9792. BUILDIN_FUNC(morphembryo)
  9793. {
  9794. struct item item_tmp;
  9795. TBL_PC *sd;
  9796. sd = script_rid2sd(st);
  9797. if( sd == NULL || sd->hd == NULL )
  9798. return SCRIPT_CMD_SUCCESS;
  9799. if( hom_is_active(sd->hd) ) {
  9800. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9801. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9802. char i;
  9803. memset(&item_tmp, 0, sizeof(item_tmp));
  9804. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9805. item_tmp.identify = 1;
  9806. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9807. clif_additem(sd, 0, 0, i);
  9808. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9809. } else {
  9810. hom_vaporize(sd, HOM_ST_MORPH);
  9811. script_pushint(st, 1);
  9812. return SCRIPT_CMD_SUCCESS;
  9813. }
  9814. } else
  9815. clif_emotion(&sd->hd->bl, E_SWT);
  9816. } else
  9817. clif_emotion(&sd->bl, E_SWT);
  9818. script_pushint(st, 0);
  9819. return SCRIPT_CMD_SUCCESS;
  9820. }
  9821. // [Zephyrus]
  9822. BUILDIN_FUNC(homunculus_shuffle)
  9823. {
  9824. TBL_PC *sd;
  9825. sd=script_rid2sd(st);
  9826. if( sd == NULL )
  9827. return SCRIPT_CMD_SUCCESS;
  9828. if(hom_is_active(sd->hd))
  9829. hom_shuffle(sd->hd);
  9830. return SCRIPT_CMD_SUCCESS;
  9831. }
  9832. /*==========================================
  9833. * Check for homunculus state.
  9834. * Return: -1 = No homunculus
  9835. * 0 = Homunculus is active
  9836. * 1 = Homunculus is vaporized (rest)
  9837. * 2 = Homunculus is in morph state
  9838. *------------------------------------------*/
  9839. BUILDIN_FUNC(checkhomcall)
  9840. {
  9841. TBL_PC *sd = script_rid2sd(st);
  9842. TBL_HOM *hd;
  9843. if( sd == NULL )
  9844. return SCRIPT_CMD_SUCCESS;
  9845. hd = sd->hd;
  9846. if( !hd )
  9847. script_pushint(st, -1);
  9848. else
  9849. script_pushint(st, hd->homunculus.vaporize);
  9850. return SCRIPT_CMD_SUCCESS;
  9851. }
  9852. //These two functions bring the eA MAPID_* class functionality to scripts.
  9853. BUILDIN_FUNC(eaclass)
  9854. {
  9855. int class_;
  9856. if( script_hasdata(st,2) )
  9857. class_ = script_getnum(st,2);
  9858. else {
  9859. TBL_PC *sd;
  9860. if (!script_charid2sd(3,sd)) {
  9861. script_pushint(st,-1);
  9862. return SCRIPT_CMD_SUCCESS;
  9863. }
  9864. class_ = sd->status.class_;
  9865. }
  9866. script_pushint(st,pc_jobid2mapid(class_));
  9867. return SCRIPT_CMD_SUCCESS;
  9868. }
  9869. BUILDIN_FUNC(roclass)
  9870. {
  9871. int class_ =script_getnum(st,2);
  9872. int sex;
  9873. if( script_hasdata(st,3) )
  9874. sex = script_getnum(st,3);
  9875. else {
  9876. TBL_PC *sd;
  9877. if (st->rid && (sd=script_rid2sd(st)))
  9878. sex = sd->status.sex;
  9879. else
  9880. sex = 1; //Just use male when not found.
  9881. }
  9882. script_pushint(st,pc_mapid2jobid(class_, sex));
  9883. return SCRIPT_CMD_SUCCESS;
  9884. }
  9885. /*==========================================
  9886. * Tells client to open a hatching window, used for pet incubator
  9887. *------------------------------------------*/
  9888. BUILDIN_FUNC(birthpet)
  9889. {
  9890. TBL_PC *sd;
  9891. sd=script_rid2sd(st);
  9892. if( sd == NULL )
  9893. return SCRIPT_CMD_SUCCESS;
  9894. if( sd->status.pet_id )
  9895. {// do not send egg list, when you already have a pet
  9896. return SCRIPT_CMD_SUCCESS;
  9897. }
  9898. clif_sendegg(sd);
  9899. return SCRIPT_CMD_SUCCESS;
  9900. }
  9901. /**
  9902. * resetlvl <action type>{,<char_id>};
  9903. * @param action_type:
  9904. * 1 : make like after rebirth
  9905. * 2 : blvl,jlvl=1, skillpoint=0
  9906. * 3 : don't reset skill, blvl=1
  9907. * 4 : jlvl=0
  9908. * @author AppleGirl
  9909. **/
  9910. BUILDIN_FUNC(resetlvl)
  9911. {
  9912. TBL_PC *sd;
  9913. int type=script_getnum(st,2);
  9914. if (!script_charid2sd(3,sd))
  9915. return SCRIPT_CMD_FAILURE;
  9916. pc_resetlvl(sd,type);
  9917. return SCRIPT_CMD_SUCCESS;
  9918. }
  9919. /**
  9920. * Reset a player status point
  9921. * resetstatus({<char_id>});
  9922. **/
  9923. BUILDIN_FUNC(resetstatus)
  9924. {
  9925. TBL_PC *sd;
  9926. if (!script_charid2sd(2,sd))
  9927. return SCRIPT_CMD_FAILURE;
  9928. pc_resetstate(sd);
  9929. return SCRIPT_CMD_SUCCESS;
  9930. }
  9931. /**
  9932. * Reset player's skill
  9933. * resetskill({<char_id>});
  9934. **/
  9935. BUILDIN_FUNC(resetskill)
  9936. {
  9937. TBL_PC *sd;
  9938. if (!script_charid2sd(2,sd))
  9939. return SCRIPT_CMD_FAILURE;
  9940. pc_resetskill(sd,1);
  9941. return SCRIPT_CMD_SUCCESS;
  9942. }
  9943. /**
  9944. * Counts total amount of skill points.
  9945. * skillpointcount({<char_id>})
  9946. **/
  9947. BUILDIN_FUNC(skillpointcount)
  9948. {
  9949. TBL_PC *sd;
  9950. if (!script_charid2sd(2,sd))
  9951. return SCRIPT_CMD_FAILURE;
  9952. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9953. return SCRIPT_CMD_SUCCESS;
  9954. }
  9955. /*==========================================
  9956. *
  9957. *------------------------------------------*/
  9958. BUILDIN_FUNC(changebase)
  9959. {
  9960. TBL_PC *sd=NULL;
  9961. int vclass;
  9962. if( script_hasdata(st,3) )
  9963. sd=map_id2sd(script_getnum(st,3));
  9964. else
  9965. sd=script_rid2sd(st);
  9966. if(sd == NULL)
  9967. return SCRIPT_CMD_SUCCESS;
  9968. vclass = script_getnum(st,2);
  9969. if(vclass == JOB_WEDDING)
  9970. {
  9971. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9972. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9973. )
  9974. return SCRIPT_CMD_SUCCESS;
  9975. }
  9976. if(!sd->disguise && vclass != sd->vd.class_) {
  9977. status_set_viewdata(&sd->bl, vclass);
  9978. //Updated client view. Base, Weapon and Cloth Colors.
  9979. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9980. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9981. if (sd->vd.cloth_color)
  9982. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9983. if (sd->vd.body_style)
  9984. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9985. clif_skillinfoblock(sd);
  9986. }
  9987. return SCRIPT_CMD_SUCCESS;
  9988. }
  9989. /**
  9990. * Change account sex and unequip all item and request for a changesex to char-serv
  9991. * changesex({<char_id>});
  9992. */
  9993. BUILDIN_FUNC(changesex)
  9994. {
  9995. int i;
  9996. TBL_PC *sd = NULL;
  9997. if (!script_charid2sd(2,sd))
  9998. return SCRIPT_CMD_FAILURE;
  9999. pc_resetskill(sd,4);
  10000. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10001. for(i = 0; i < EQI_MAX; i++) {
  10002. if (sd->equip_index[i] >= 0)
  10003. pc_unequipitem(sd, sd->equip_index[i], 3);
  10004. }
  10005. chrif_changesex(sd, true);
  10006. return SCRIPT_CMD_SUCCESS;
  10007. }
  10008. /**
  10009. * Change character's sex and unequip all item and request for a changesex to char-serv
  10010. * changecharsex({<char_id>});
  10011. */
  10012. BUILDIN_FUNC(changecharsex)
  10013. {
  10014. #if PACKETVER >= 20141016
  10015. int i;
  10016. TBL_PC *sd = NULL;
  10017. if (!script_charid2sd(2,sd))
  10018. return SCRIPT_CMD_FAILURE;
  10019. pc_resetskill(sd,4);
  10020. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10021. for (i = 0; i < EQI_MAX; i++) {
  10022. if (sd->equip_index[i] >= 0)
  10023. pc_unequipitem(sd, sd->equip_index[i], 3);
  10024. }
  10025. chrif_changesex(sd, false);
  10026. return SCRIPT_CMD_SUCCESS;
  10027. #else
  10028. return SCRIPT_CMD_FAILURE;
  10029. #endif
  10030. }
  10031. /*==========================================
  10032. * Works like 'announce' but outputs in the common chat window
  10033. *------------------------------------------*/
  10034. BUILDIN_FUNC(globalmes)
  10035. {
  10036. struct block_list *bl = map_id2bl(st->oid);
  10037. struct npc_data *nd = (struct npc_data *)bl;
  10038. const char *name=NULL,*mes;
  10039. mes=script_getstr(st,2);
  10040. if(mes==NULL)
  10041. return SCRIPT_CMD_SUCCESS;
  10042. if(script_hasdata(st,3)){ // npc name to display
  10043. name=script_getstr(st,3);
  10044. } else {
  10045. name=nd->name; //use current npc name
  10046. }
  10047. npc_globalmessage(name,mes); // broadcast to all players connected
  10048. return SCRIPT_CMD_SUCCESS;
  10049. }
  10050. /////////////////////////////////////////////////////////////////////
  10051. // NPC waiting room (chat room)
  10052. //
  10053. /// Creates a waiting room (chat room) for this npc.
  10054. ///
  10055. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  10056. BUILDIN_FUNC(waitingroom)
  10057. {
  10058. struct npc_data* nd;
  10059. const char* title = script_getstr(st, 2);
  10060. int limit = script_getnum(st, 3);
  10061. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  10062. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  10063. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  10064. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  10065. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  10066. nd = (struct npc_data *)map_id2bl(st->oid);
  10067. if( nd != NULL )
  10068. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  10069. return SCRIPT_CMD_SUCCESS;
  10070. }
  10071. /// Removes the waiting room of the current or target npc.
  10072. ///
  10073. /// delwaitingroom "<npc_name>";
  10074. /// delwaitingroom;
  10075. BUILDIN_FUNC(delwaitingroom)
  10076. {
  10077. struct npc_data* nd;
  10078. if( script_hasdata(st,2) )
  10079. nd = npc_name2id(script_getstr(st, 2));
  10080. else
  10081. nd = (struct npc_data *)map_id2bl(st->oid);
  10082. if( nd != NULL )
  10083. chat_deletenpcchat(nd);
  10084. return SCRIPT_CMD_SUCCESS;
  10085. }
  10086. /// Kick the specified player from the waiting room of the target npc.
  10087. ///
  10088. /// waitingroomkick "<npc_name>", <kickusername>;
  10089. BUILDIN_FUNC(waitingroomkick)
  10090. {
  10091. struct npc_data* nd;
  10092. struct chat_data* cd;
  10093. const char* kickusername;
  10094. nd = npc_name2id(script_getstr(st,2));
  10095. kickusername = script_getstr(st,3);
  10096. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10097. chat_npckickchat(cd, kickusername);
  10098. return SCRIPT_CMD_SUCCESS;
  10099. }
  10100. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  10101. /// Num users stored in .@waitingroom_usercount
  10102. ///
  10103. /// getwaitingroomusers "<npc_name>";
  10104. BUILDIN_FUNC(getwaitingroomusers)
  10105. {
  10106. struct npc_data* nd;
  10107. struct chat_data* cd;
  10108. int i, j=0;
  10109. if( script_hasdata(st,2) )
  10110. nd = npc_name2id(script_getstr(st, 2));
  10111. else
  10112. nd = (struct npc_data *)map_id2bl(st->oid);
  10113. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  10114. for(i = 0; i < cd->users; ++i) {
  10115. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  10116. j++;
  10117. }
  10118. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  10119. }
  10120. return SCRIPT_CMD_SUCCESS;
  10121. }
  10122. /// Kicks all the players from the waiting room of the current or target npc.
  10123. ///
  10124. /// kickwaitingroomall "<npc_name>";
  10125. /// kickwaitingroomall;
  10126. BUILDIN_FUNC(waitingroomkickall)
  10127. {
  10128. struct npc_data* nd;
  10129. struct chat_data* cd;
  10130. if( script_hasdata(st,2) )
  10131. nd = npc_name2id(script_getstr(st,2));
  10132. else
  10133. nd = (struct npc_data *)map_id2bl(st->oid);
  10134. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10135. chat_npckickall(cd);
  10136. return SCRIPT_CMD_SUCCESS;
  10137. }
  10138. /// Enables the waiting room event of the current or target npc.
  10139. ///
  10140. /// enablewaitingroomevent "<npc_name>";
  10141. /// enablewaitingroomevent;
  10142. BUILDIN_FUNC(enablewaitingroomevent)
  10143. {
  10144. struct npc_data* nd;
  10145. struct chat_data* cd;
  10146. if( script_hasdata(st,2) )
  10147. nd = npc_name2id(script_getstr(st, 2));
  10148. else
  10149. nd = (struct npc_data *)map_id2bl(st->oid);
  10150. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10151. chat_enableevent(cd);
  10152. return SCRIPT_CMD_SUCCESS;
  10153. }
  10154. /// Disables the waiting room event of the current or target npc.
  10155. ///
  10156. /// disablewaitingroomevent "<npc_name>";
  10157. /// disablewaitingroomevent;
  10158. BUILDIN_FUNC(disablewaitingroomevent)
  10159. {
  10160. struct npc_data *nd;
  10161. struct chat_data *cd;
  10162. if( script_hasdata(st,2) )
  10163. nd = npc_name2id(script_getstr(st, 2));
  10164. else
  10165. nd = (struct npc_data *)map_id2bl(st->oid);
  10166. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10167. chat_disableevent(cd);
  10168. return SCRIPT_CMD_SUCCESS;
  10169. }
  10170. /// Returns info on the waiting room of the current or target npc.
  10171. /// Returns -1 if the type unknown
  10172. /// <type>=0 : current number of users
  10173. /// <type>=1 : maximum number of users allowed
  10174. /// <type>=2 : the number of users that trigger the event
  10175. /// <type>=3 : if the trigger is disabled
  10176. /// <type>=4 : the title of the waiting room
  10177. /// <type>=5 : the password of the waiting room
  10178. /// <type>=16 : the name of the waiting room event
  10179. /// <type>=32 : if the waiting room is full
  10180. /// <type>=33 : if there are enough users to trigger the event
  10181. ///
  10182. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10183. /// getwaitingroomstate(<type>) -> <info>
  10184. BUILDIN_FUNC(getwaitingroomstate)
  10185. {
  10186. struct npc_data *nd;
  10187. struct chat_data *cd;
  10188. int type;
  10189. type = script_getnum(st,2);
  10190. if( script_hasdata(st,3) )
  10191. nd = npc_name2id(script_getstr(st, 3));
  10192. else
  10193. nd = (struct npc_data *)map_id2bl(st->oid);
  10194. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10195. {
  10196. script_pushint(st, -1);
  10197. return SCRIPT_CMD_SUCCESS;
  10198. }
  10199. switch(type)
  10200. {
  10201. case 0: script_pushint(st, cd->users); break;
  10202. case 1: script_pushint(st, cd->limit); break;
  10203. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10204. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10205. case 4: script_pushstrcopy(st, cd->title); break;
  10206. case 5: script_pushstrcopy(st, cd->pass); break;
  10207. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10208. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10209. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10210. default: script_pushint(st, -1); break;
  10211. }
  10212. return SCRIPT_CMD_SUCCESS;
  10213. }
  10214. /// Warps the trigger or target amount of players to the target map and position.
  10215. /// Players are automatically removed from the waiting room.
  10216. /// Those waiting the longest will get warped first.
  10217. /// The target map can be "Random" for a random position in the current map,
  10218. /// and "SavePoint" for the savepoint map+position.
  10219. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10220. ///
  10221. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10222. /// The total number of teleported players is put into $@warpwaitingpcnum
  10223. ///
  10224. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10225. /// warpwaitingpc "<map name>",<x>,<y>;
  10226. BUILDIN_FUNC(warpwaitingpc)
  10227. {
  10228. int x;
  10229. int y;
  10230. int i;
  10231. int n;
  10232. const char* map_name;
  10233. struct npc_data* nd;
  10234. struct chat_data* cd;
  10235. nd = (struct npc_data *)map_id2bl(st->oid);
  10236. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10237. return SCRIPT_CMD_SUCCESS;
  10238. map_name = script_getstr(st,2);
  10239. x = script_getnum(st,3);
  10240. y = script_getnum(st,4);
  10241. n = cd->trigger&0x7f;
  10242. if( script_hasdata(st,5) )
  10243. n = script_getnum(st,5);
  10244. for( i = 0; i < n && cd->users > 0; i++ )
  10245. {
  10246. TBL_PC* sd = cd->usersd[0];
  10247. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  10248. {// can't teleport on this map
  10249. break;
  10250. }
  10251. if( cd->zeny )
  10252. {// fee set
  10253. if( (uint32)sd->status.zeny < cd->zeny )
  10254. {// no zeny to cover set fee
  10255. break;
  10256. }
  10257. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10258. }
  10259. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10260. if( strcmp(map_name,"Random") == 0 )
  10261. pc_randomwarp(sd,CLR_TELEPORT);
  10262. else if( strcmp(map_name,"SavePoint") == 0 )
  10263. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10264. else
  10265. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10266. }
  10267. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10268. return SCRIPT_CMD_SUCCESS;
  10269. }
  10270. /////////////////////////////////////////////////////////////////////
  10271. // ...
  10272. //
  10273. /// Detaches a character from a script.
  10274. ///
  10275. /// @param st Script state to detach the character from.
  10276. static void script_detach_rid(struct script_state* st)
  10277. {
  10278. if(st->rid)
  10279. {
  10280. script_detach_state(st, false);
  10281. st->rid = 0;
  10282. }
  10283. }
  10284. /*=========================================================================
  10285. * Attaches a set of RIDs to the current script. [digitalhamster]
  10286. * addrid(<type>{,<flag>{,<parameters>}});
  10287. * <type>:
  10288. * 0 : All players in the server.
  10289. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10290. * 2 : Party members of a specified party ID.
  10291. * [ Parameters: <party id> ]
  10292. * 3 : Guild members of a specified guild ID.
  10293. * [ Parameters: <guild id> ]
  10294. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10295. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10296. * 5 : All players in the map.
  10297. * [ Parameters: "<map name>" ]
  10298. * Account ID: The specified account ID.
  10299. * <flag>:
  10300. * 0 : Players are always attached. (default)
  10301. * 1 : Players currently running another script will not be attached.
  10302. *-------------------------------------------------------------------------*/
  10303. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10304. {
  10305. int forceflag;
  10306. struct map_session_data *sd = (TBL_PC *)bl;
  10307. struct script_state* st;
  10308. st = va_arg(ap,struct script_state*);
  10309. forceflag = va_arg(ap,int);
  10310. if(!forceflag || !sd->st)
  10311. if(sd->status.account_id != st->rid)
  10312. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10313. return 0;
  10314. }
  10315. BUILDIN_FUNC(addrid)
  10316. {
  10317. struct s_mapiterator* iter;
  10318. struct block_list *bl;
  10319. TBL_PC *sd;
  10320. if(st->rid < 1) {
  10321. st->state = END;
  10322. bl = map_id2bl(st->oid);
  10323. } else
  10324. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10325. iter = mapit_getallusers();
  10326. switch(script_getnum(st,2)) {
  10327. case 0:
  10328. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10329. if(!script_getnum(st,3) || !sd->st)
  10330. if(sd->status.account_id != st->rid) //attached player already runs.
  10331. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10332. }
  10333. break;
  10334. case 1:
  10335. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10336. if(!script_getnum(st,3) || !sd->st)
  10337. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10338. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10339. }
  10340. break;
  10341. case 2:
  10342. if(script_getnum(st,4) == 0) {
  10343. script_pushint(st,0);
  10344. return SCRIPT_CMD_SUCCESS;
  10345. }
  10346. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10347. if(!script_getnum(st,3) || !sd->st)
  10348. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10349. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10350. }
  10351. break;
  10352. case 3:
  10353. if(script_getnum(st,4) == 0) {
  10354. script_pushint(st,0);
  10355. return SCRIPT_CMD_SUCCESS;
  10356. }
  10357. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10358. if(!script_getnum(st,3) || !sd->st)
  10359. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10360. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10361. }
  10362. break;
  10363. case 4:
  10364. map_foreachinarea(buildin_addrid_sub,
  10365. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10366. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10367. break;
  10368. case 5:
  10369. if (script_getstr(st, 4) == NULL) {
  10370. script_pushint(st, 0);
  10371. return SCRIPT_CMD_FAILURE;
  10372. }
  10373. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  10374. script_pushint(st, 0);
  10375. return SCRIPT_CMD_FAILURE;
  10376. }
  10377. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  10378. break;
  10379. default:
  10380. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10381. script_pushint(st,0);
  10382. return SCRIPT_CMD_SUCCESS;
  10383. }
  10384. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10385. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10386. script_pushint(st,1);
  10387. }
  10388. return SCRIPT_CMD_SUCCESS;
  10389. }
  10390. mapit_free(iter);
  10391. script_pushint(st,1);
  10392. return SCRIPT_CMD_SUCCESS;
  10393. }
  10394. /*==========================================
  10395. * Attach sd char id to script and detach current one if any
  10396. *------------------------------------------*/
  10397. BUILDIN_FUNC(attachrid)
  10398. {
  10399. int rid = script_getnum(st,2);
  10400. if (map_id2sd(rid) != NULL) {
  10401. script_detach_rid(st);
  10402. st->rid = rid;
  10403. script_attach_state(st);
  10404. script_pushint(st,1);
  10405. } else
  10406. script_pushint(st,0);
  10407. return SCRIPT_CMD_SUCCESS;
  10408. }
  10409. /*==========================================
  10410. * Detach script to rid
  10411. *------------------------------------------*/
  10412. BUILDIN_FUNC(detachrid)
  10413. {
  10414. script_detach_rid(st);
  10415. return SCRIPT_CMD_SUCCESS;
  10416. }
  10417. /*==========================================
  10418. * Chk if account connected, (and charid from account if specified)
  10419. *------------------------------------------*/
  10420. BUILDIN_FUNC(isloggedin)
  10421. {
  10422. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10423. if (script_hasdata(st,3) && sd &&
  10424. sd->status.char_id != script_getnum(st,3))
  10425. sd = NULL;
  10426. push_val(st->stack,C_INT,sd!=NULL);
  10427. return SCRIPT_CMD_SUCCESS;
  10428. }
  10429. /*==========================================
  10430. *
  10431. *------------------------------------------*/
  10432. BUILDIN_FUNC(setmapflagnosave)
  10433. {
  10434. int16 m,x,y;
  10435. unsigned short mapindex;
  10436. const char *str,*str2;
  10437. str=script_getstr(st,2);
  10438. str2=script_getstr(st,3);
  10439. x=script_getnum(st,4);
  10440. y=script_getnum(st,5);
  10441. m = map_mapname2mapid(str);
  10442. mapindex = mapindex_name2id(str2);
  10443. if(m >= 0 && mapindex) {
  10444. map[m].flag.nosave=1;
  10445. map[m].save.map=mapindex;
  10446. map[m].save.x=x;
  10447. map[m].save.y=y;
  10448. }
  10449. return SCRIPT_CMD_SUCCESS;
  10450. }
  10451. BUILDIN_FUNC(getmapflag)
  10452. {
  10453. int16 m,i;
  10454. const char *str;
  10455. str=script_getstr(st,2);
  10456. i=script_getnum(st,3);
  10457. m = map_mapname2mapid(str);
  10458. if(m >= 0) {
  10459. switch(i) {
  10460. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10461. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10462. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10463. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10464. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10465. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10466. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10467. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10468. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10469. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10470. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10471. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10472. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10473. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10474. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10475. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10476. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10477. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10478. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10479. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10480. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10481. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10482. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10483. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10484. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10485. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10486. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10487. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10488. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10489. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10490. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10491. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10492. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10493. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10494. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10495. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10496. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10497. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10498. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10499. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10500. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10501. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10502. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10503. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10504. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10505. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10506. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10507. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10508. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10509. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10510. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10511. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10512. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10513. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10514. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10515. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10516. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10517. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10518. case MF_NOCOSTUME: script_pushint(st,map[m].flag.nocostume); break;
  10519. case MF_GVG_TE_CASTLE: script_pushint(st,map[m].flag.gvg_te_castle); break;
  10520. case MF_GVG_TE: script_pushint(st,map[m].flag.gvg_te); break;
  10521. case MF_HIDEMOBHPBAR: script_pushint(st,map[m].flag.hidemobhpbar); break;
  10522. #ifdef ADJUST_SKILL_DAMAGE
  10523. case MF_SKILL_DAMAGE:
  10524. {
  10525. int ret_val=0, type=0;
  10526. FETCH(4,type);
  10527. switch (type) {
  10528. case 1: ret_val = map[m].adjust.damage.pc; break;
  10529. case 2: ret_val = map[m].adjust.damage.mob; break;
  10530. case 3: ret_val = map[m].adjust.damage.boss; break;
  10531. case 4: ret_val = map[m].adjust.damage.other; break;
  10532. case 5: ret_val = map[m].adjust.damage.caster; break;
  10533. default: ret_val = map[m].flag.skill_damage; break;
  10534. }
  10535. script_pushint(st,ret_val); break;
  10536. } break;
  10537. #endif
  10538. }
  10539. }
  10540. return SCRIPT_CMD_SUCCESS;
  10541. }
  10542. /* pvp timer handling */
  10543. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10544. TBL_PC* sd = (TBL_PC*)bl;
  10545. if (sd->pvp_timer == INVALID_TIMER) {
  10546. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10547. sd->pvp_rank = 0;
  10548. sd->pvp_lastusers = 0;
  10549. sd->pvp_point = 5;
  10550. sd->pvp_won = 0;
  10551. sd->pvp_lost = 0;
  10552. }
  10553. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE,SELF);
  10554. return 0;
  10555. }
  10556. BUILDIN_FUNC(setmapflag)
  10557. {
  10558. int16 m,i;
  10559. const char *str;
  10560. int val=0;
  10561. str=script_getstr(st,2);
  10562. i=script_getnum(st,3);
  10563. FETCH(4,val);
  10564. m = map_mapname2mapid(str);
  10565. if(m >= 0) {
  10566. switch(i) {
  10567. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10568. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10569. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10570. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10571. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10572. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10573. case MF_PVP:
  10574. map[m].flag.pvp = 1;
  10575. if( !battle_config.pk_mode ) {
  10576. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10577. }
  10578. break;
  10579. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10580. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10581. case MF_GVG:
  10582. map[m].flag.gvg = 1;
  10583. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10584. break;
  10585. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10586. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10587. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10588. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10589. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10590. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10591. case MF_SNOW: map[m].flag.snow = 1; break;
  10592. case MF_FOG: map[m].flag.fog = 1; break;
  10593. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10594. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10595. case MF_NOGO: map[m].flag.nogo = 1; break;
  10596. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10597. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10598. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10599. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10600. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10601. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10602. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10603. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10604. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10605. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10606. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10607. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10608. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10609. case MF_RESTRICTED:
  10610. map[m].zone |= 1<<(val+1);
  10611. map[m].flag.restricted=1;
  10612. break;
  10613. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10614. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10615. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10616. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10617. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10618. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10619. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10620. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10621. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10622. case MF_TOWN: map[m].flag.town = 1; break;
  10623. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10624. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10625. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10626. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10627. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10628. case MF_RESET: map[m].flag.reset = 1; break;
  10629. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10630. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10631. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10632. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10633. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10634. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10635. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10636. case MF_NOCOSTUME: map[m].flag.nocostume = 1; break;
  10637. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 1; break;
  10638. case MF_GVG_TE:
  10639. map[m].flag.gvg_te = 1;
  10640. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10641. break;
  10642. case MF_HIDEMOBHPBAR: map[m].flag.hidemobhpbar = 1; break;
  10643. #ifdef ADJUST_SKILL_DAMAGE
  10644. case MF_SKILL_DAMAGE:
  10645. {
  10646. int type=0;
  10647. FETCH(5,type);
  10648. switch (type) {
  10649. case 1: map[m].adjust.damage.pc = val; break;
  10650. case 2: map[m].adjust.damage.mob = val; break;
  10651. case 3: map[m].adjust.damage.boss = val; break;
  10652. case 4: map[m].adjust.damage.other = val; break;
  10653. case 5: map[m].adjust.damage.caster = val; break;
  10654. }
  10655. map[m].flag.skill_damage = 1;
  10656. } break;
  10657. #endif
  10658. }
  10659. }
  10660. return SCRIPT_CMD_SUCCESS;
  10661. }
  10662. BUILDIN_FUNC(removemapflag)
  10663. {
  10664. int16 m,i;
  10665. const char *str;
  10666. int val=0;
  10667. str=script_getstr(st,2);
  10668. i=script_getnum(st,3);
  10669. FETCH(4,val);
  10670. m = map_mapname2mapid(str);
  10671. if(m >= 0) {
  10672. switch(i) {
  10673. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10674. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10675. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10676. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10677. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10678. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10679. case MF_PVP:
  10680. map[m].flag.pvp = 0;
  10681. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10682. break;
  10683. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10684. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10685. case MF_GVG:
  10686. map[m].flag.gvg = 0;
  10687. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10688. break;
  10689. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10690. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10691. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10692. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10693. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10694. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10695. case MF_SNOW: map[m].flag.snow = 0; break;
  10696. case MF_FOG: map[m].flag.fog = 0; break;
  10697. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10698. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10699. case MF_NOGO: map[m].flag.nogo = 0; break;
  10700. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10701. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10702. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10703. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10704. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10705. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10706. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10707. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10708. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10709. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10710. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10711. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10712. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10713. case MF_RESTRICTED:
  10714. map[m].zone ^= 1<<(val+1);
  10715. if (map[m].zone == 0){
  10716. map[m].flag.restricted=0;
  10717. }
  10718. break;
  10719. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10720. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10721. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10722. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10723. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10724. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10725. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10726. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10727. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10728. case MF_TOWN: map[m].flag.town = 0; break;
  10729. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10730. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10731. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10732. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10733. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10734. case MF_RESET: map[m].flag.reset = 0; break;
  10735. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10736. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10737. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10738. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10739. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10740. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10741. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10742. case MF_NOCOSTUME: map[m].flag.nocostume = 0; break;
  10743. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 0; break;
  10744. case MF_GVG_TE:
  10745. map[m].flag.gvg_te = 0;
  10746. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10747. break;
  10748. case MF_HIDEMOBHPBAR: map[m].flag.hidemobhpbar = 0; break;
  10749. #ifdef ADJUST_SKILL_DAMAGE
  10750. case MF_SKILL_DAMAGE:
  10751. {
  10752. map[m].flag.skill_damage = 0;
  10753. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10754. if (map[m].skill_damage.count)
  10755. map_skill_damage_free(&map[m]);
  10756. } break;
  10757. #endif
  10758. }
  10759. }
  10760. return SCRIPT_CMD_SUCCESS;
  10761. }
  10762. BUILDIN_FUNC(pvpon)
  10763. {
  10764. int16 m;
  10765. const char *str;
  10766. TBL_PC* sd = NULL;
  10767. struct s_mapiterator* iter;
  10768. str = script_getstr(st,2);
  10769. m = map_mapname2mapid(str);
  10770. if( m < 0 || map[m].flag.pvp )
  10771. return SCRIPT_CMD_SUCCESS; // nothing to do
  10772. map[m].flag.pvp = 1;
  10773. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10774. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10775. return SCRIPT_CMD_SUCCESS;
  10776. iter = mapit_getallusers();
  10777. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10778. {
  10779. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10780. continue; // not applicable
  10781. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10782. sd->pvp_rank = 0;
  10783. sd->pvp_lastusers = 0;
  10784. sd->pvp_point = 5;
  10785. sd->pvp_won = 0;
  10786. sd->pvp_lost = 0;
  10787. }
  10788. mapit_free(iter);
  10789. return SCRIPT_CMD_SUCCESS;
  10790. }
  10791. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10792. {
  10793. TBL_PC* sd = (TBL_PC*)bl;
  10794. clif_pvpset(sd, 0, 0, 2);
  10795. if (sd->pvp_timer != INVALID_TIMER) {
  10796. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10797. sd->pvp_timer = INVALID_TIMER;
  10798. }
  10799. return 0;
  10800. }
  10801. BUILDIN_FUNC(pvpoff)
  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.pvp)
  10808. return SCRIPT_CMD_SUCCESS; //fixed Lupus
  10809. map[m].flag.pvp = 0;
  10810. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10811. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10812. return SCRIPT_CMD_SUCCESS;
  10813. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10814. return SCRIPT_CMD_SUCCESS;
  10815. }
  10816. BUILDIN_FUNC(gvgon)
  10817. {
  10818. int16 m;
  10819. const char *str;
  10820. str=script_getstr(st,2);
  10821. m = map_mapname2mapid(str);
  10822. if(m >= 0 && !map[m].flag.gvg) {
  10823. map[m].flag.gvg = 1;
  10824. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10825. }
  10826. return SCRIPT_CMD_SUCCESS;
  10827. }
  10828. BUILDIN_FUNC(gvgoff)
  10829. {
  10830. int16 m;
  10831. const char *str;
  10832. str=script_getstr(st,2);
  10833. m = map_mapname2mapid(str);
  10834. if(m >= 0 && map[m].flag.gvg) {
  10835. map[m].flag.gvg = 0;
  10836. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10837. }
  10838. return SCRIPT_CMD_SUCCESS;
  10839. }
  10840. BUILDIN_FUNC(gvgon3)
  10841. {
  10842. int16 m;
  10843. const char *str;
  10844. str = script_getstr(st,2);
  10845. m = map_mapname2mapid(str);
  10846. if (m >= 0 && !map[m].flag.gvg_te) {
  10847. map[m].flag.gvg_te = 1;
  10848. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10849. }
  10850. return SCRIPT_CMD_SUCCESS;
  10851. }
  10852. BUILDIN_FUNC(gvgoff3)
  10853. {
  10854. int16 m;
  10855. const char *str;
  10856. str = script_getstr(st,2);
  10857. m = map_mapname2mapid(str);
  10858. if (m >= 0 && map[m].flag.gvg_te) {
  10859. map[m].flag.gvg_te = 0;
  10860. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10861. }
  10862. return SCRIPT_CMD_SUCCESS;
  10863. }
  10864. /*==========================================
  10865. * Shows an emoticon on top of the player/npc
  10866. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10867. *------------------------------------------*/
  10868. //Optional second parameter added by [Skotlex]
  10869. BUILDIN_FUNC(emotion)
  10870. {
  10871. int type;
  10872. int player=0;
  10873. type=script_getnum(st,2);
  10874. if(type < 0 || type > 100)
  10875. return SCRIPT_CMD_SUCCESS;
  10876. if( script_hasdata(st,3) )
  10877. player=script_getnum(st,3);
  10878. if (player) {
  10879. TBL_PC *sd = NULL;
  10880. if( script_nick2sd(4,sd) ){
  10881. clif_emotion(&sd->bl, type);
  10882. }
  10883. } else
  10884. if( script_hasdata(st,4) )
  10885. {
  10886. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10887. if(nd)
  10888. clif_emotion(&nd->bl,type);
  10889. }
  10890. else
  10891. clif_emotion(map_id2bl(st->oid),type);
  10892. return SCRIPT_CMD_SUCCESS;
  10893. }
  10894. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10895. {
  10896. int16 m=va_arg(ap,int);
  10897. int g_id=va_arg(ap,int);
  10898. int flag=va_arg(ap,int);
  10899. if(!sd || sd->bl.m != m)
  10900. return 0;
  10901. if(
  10902. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10903. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10904. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10905. )
  10906. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10907. return 1;
  10908. }
  10909. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10910. {
  10911. struct mob_data *md=(struct mob_data *)bl;
  10912. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  10913. status_kill(bl);
  10914. return 1;
  10915. }
  10916. /*
  10917. * Function to kick guild members out of a map and to their save points.
  10918. * m : mapid
  10919. * g_id : owner guild id
  10920. * flag & 1 : Warp out owners
  10921. * flag & 2 : Warp out outsiders
  10922. * flag & 4 : reserved for mobs
  10923. * */
  10924. BUILDIN_FUNC(maprespawnguildid)
  10925. {
  10926. const char *mapname=script_getstr(st,2);
  10927. int g_id=script_getnum(st,3);
  10928. int flag=script_getnum(st,4);
  10929. int16 m=map_mapname2mapid(mapname);
  10930. if(m == -1)
  10931. return SCRIPT_CMD_SUCCESS;
  10932. //Catch ALL players (in case some are 'between maps' on execution time)
  10933. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10934. if (flag&4) //Remove script mobs.
  10935. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10936. return SCRIPT_CMD_SUCCESS;
  10937. }
  10938. /// Siege commands
  10939. /**
  10940. * Start WoE:FE
  10941. * agitstart();
  10942. */
  10943. BUILDIN_FUNC(agitstart)
  10944. {
  10945. guild_agit_start();
  10946. return SCRIPT_CMD_SUCCESS;
  10947. }
  10948. /**
  10949. * End WoE:FE
  10950. * agitend();
  10951. */
  10952. BUILDIN_FUNC(agitend)
  10953. {
  10954. guild_agit_end();
  10955. return SCRIPT_CMD_SUCCESS;
  10956. }
  10957. /**
  10958. * Start WoE:SE
  10959. * agitstart2();
  10960. */
  10961. BUILDIN_FUNC(agitstart2)
  10962. {
  10963. guild_agit2_start();
  10964. return SCRIPT_CMD_SUCCESS;
  10965. }
  10966. /**
  10967. * End WoE:SE
  10968. * agitend();
  10969. */
  10970. BUILDIN_FUNC(agitend2)
  10971. {
  10972. guild_agit2_end();
  10973. return SCRIPT_CMD_SUCCESS;
  10974. }
  10975. /**
  10976. * Start WoE:TE
  10977. * agitstart3();
  10978. */
  10979. BUILDIN_FUNC(agitstart3)
  10980. {
  10981. guild_agit3_start();
  10982. return SCRIPT_CMD_SUCCESS;
  10983. }
  10984. /**
  10985. * End WoE:TE
  10986. * agitend3();
  10987. */
  10988. BUILDIN_FUNC(agitend3)
  10989. {
  10990. guild_agit3_end();
  10991. return SCRIPT_CMD_SUCCESS;
  10992. }
  10993. /**
  10994. * Returns whether WoE:FE is on or off.
  10995. * agitcheck();
  10996. */
  10997. BUILDIN_FUNC(agitcheck)
  10998. {
  10999. script_pushint(st, agit_flag);
  11000. return SCRIPT_CMD_SUCCESS;
  11001. }
  11002. /**
  11003. * Returns whether WoE:SE is on or off.
  11004. * agitcheck2();
  11005. */
  11006. BUILDIN_FUNC(agitcheck2)
  11007. {
  11008. script_pushint(st, agit2_flag);
  11009. return SCRIPT_CMD_SUCCESS;
  11010. }
  11011. /**
  11012. * Returns whether WoE:TE is on or off.
  11013. * agitcheck3();
  11014. */
  11015. BUILDIN_FUNC(agitcheck3)
  11016. {
  11017. script_pushint(st, agit3_flag);
  11018. return SCRIPT_CMD_SUCCESS;
  11019. }
  11020. /// Sets the guild_id of this npc.
  11021. ///
  11022. /// flagemblem <guild_id>;
  11023. BUILDIN_FUNC(flagemblem)
  11024. {
  11025. TBL_NPC* nd;
  11026. int g_id = script_getnum(st,2);
  11027. if(g_id < 0)
  11028. return SCRIPT_CMD_SUCCESS;
  11029. nd = (TBL_NPC*)map_id2nd(st->oid);
  11030. if( nd == NULL ) {
  11031. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  11032. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  11033. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  11034. } else {
  11035. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11036. nd->u.scr.guild_id = g_id;
  11037. clif_guild_emblem_area(&nd->bl);
  11038. /* guild flag caching */
  11039. if( g_id ) /* adding a id */
  11040. guild_flag_add(nd);
  11041. else if( changed ) /* removing a flag */
  11042. guild_flag_remove(nd);
  11043. }
  11044. return SCRIPT_CMD_SUCCESS;
  11045. }
  11046. BUILDIN_FUNC(getcastlename)
  11047. {
  11048. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11049. struct guild_castle* gc = guild_mapname2gc(mapname);
  11050. const char* name = (gc) ? gc->castle_name : "";
  11051. script_pushstrcopy(st,name);
  11052. return SCRIPT_CMD_SUCCESS;
  11053. }
  11054. BUILDIN_FUNC(getcastledata)
  11055. {
  11056. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11057. int index = script_getnum(st,3);
  11058. struct guild_castle *gc = guild_mapname2gc(mapname);
  11059. if (gc == NULL) {
  11060. script_pushint(st,0);
  11061. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11062. return SCRIPT_CMD_FAILURE;
  11063. }
  11064. switch (index) {
  11065. case 1:
  11066. script_pushint(st,gc->guild_id); break;
  11067. case 2:
  11068. script_pushint(st,gc->economy); break;
  11069. case 3:
  11070. script_pushint(st,gc->defense); break;
  11071. case 4:
  11072. script_pushint(st,gc->triggerE); break;
  11073. case 5:
  11074. script_pushint(st,gc->triggerD); break;
  11075. case 6:
  11076. script_pushint(st,gc->nextTime); break;
  11077. case 7:
  11078. script_pushint(st,gc->payTime); break;
  11079. case 8:
  11080. script_pushint(st,gc->createTime); break;
  11081. case 9:
  11082. script_pushint(st,gc->visibleC); break;
  11083. default:
  11084. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  11085. script_pushint(st,gc->guardian[index-10].visible);
  11086. break;
  11087. }
  11088. script_pushint(st,0);
  11089. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11090. return SCRIPT_CMD_FAILURE;
  11091. }
  11092. return SCRIPT_CMD_SUCCESS;
  11093. }
  11094. BUILDIN_FUNC(setcastledata)
  11095. {
  11096. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11097. int index = script_getnum(st,3);
  11098. int value = script_getnum(st,4);
  11099. struct guild_castle *gc = guild_mapname2gc(mapname);
  11100. if (gc == NULL) {
  11101. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11102. return SCRIPT_CMD_FAILURE;
  11103. }
  11104. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  11105. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11106. return SCRIPT_CMD_FAILURE;
  11107. }
  11108. guild_castledatasave(gc->castle_id, index, value);
  11109. return SCRIPT_CMD_SUCCESS;
  11110. }
  11111. /* =====================================================================
  11112. * ---------------------------------------------------------------------*/
  11113. BUILDIN_FUNC(requestguildinfo)
  11114. {
  11115. int guild_id=script_getnum(st,2);
  11116. const char *event=NULL;
  11117. if( script_hasdata(st,3) ){
  11118. event=script_getstr(st,3);
  11119. check_event(st, event);
  11120. }
  11121. if(guild_id>0)
  11122. guild_npc_request_info(guild_id,event);
  11123. return SCRIPT_CMD_SUCCESS;
  11124. }
  11125. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11126. /// getequipcardcnt(<equipment slot>);
  11127. BUILDIN_FUNC(getequipcardcnt)
  11128. {
  11129. int i=-1,j,num;
  11130. TBL_PC *sd;
  11131. int count;
  11132. num=script_getnum(st,2);
  11133. sd=script_rid2sd(st);
  11134. if (equip_index_check(num))
  11135. i=pc_checkequip(sd,equip_bitmask[num]);
  11136. if (i < 0 || !sd->inventory_data[i]) {
  11137. script_pushint(st,0);
  11138. return SCRIPT_CMD_SUCCESS;
  11139. }
  11140. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11141. {
  11142. script_pushint(st,0);
  11143. return SCRIPT_CMD_SUCCESS;
  11144. }
  11145. count = 0;
  11146. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  11147. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11148. count++;
  11149. script_pushint(st,count);
  11150. return SCRIPT_CMD_SUCCESS;
  11151. }
  11152. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11153. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11154. /// successremovecards <slot>;
  11155. BUILDIN_FUNC(successremovecards) {
  11156. int i=-1,c,cardflag=0;
  11157. TBL_PC* sd = script_rid2sd(st);
  11158. int num = script_getnum(st,2);
  11159. if (equip_index_check(num))
  11160. i=pc_checkequip(sd,equip_bitmask[num]);
  11161. if (i < 0 || !sd->inventory_data[i]) {
  11162. return SCRIPT_CMD_SUCCESS;
  11163. }
  11164. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11165. return SCRIPT_CMD_SUCCESS;
  11166. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11167. 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
  11168. unsigned char flag = 0;
  11169. struct item item_tmp;
  11170. memset(&item_tmp,0,sizeof(item_tmp));
  11171. cardflag = 1;
  11172. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11173. item_tmp.identify = 1;
  11174. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11175. clif_additem(sd,0,0,flag);
  11176. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11177. }
  11178. }
  11179. }
  11180. if(cardflag == 1) {//if card was remove remplace item with no card
  11181. unsigned char flag = 0, j;
  11182. struct item item_tmp;
  11183. memset(&item_tmp,0,sizeof(item_tmp));
  11184. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11185. item_tmp.identify = 1;
  11186. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11187. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11188. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11189. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11190. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11191. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11192. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11193. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11194. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11195. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11196. }
  11197. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11198. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11199. clif_additem(sd,0,0,flag);
  11200. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11201. }
  11202. clif_misceffect(&sd->bl,3);
  11203. }
  11204. return SCRIPT_CMD_SUCCESS;
  11205. }
  11206. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11207. /// failedremovecards <slot>, <type>;
  11208. /// <type>=0 : will destroy both the item and the cards.
  11209. /// <type>=1 : will keep the item, but destroy the cards.
  11210. /// <type>=2 : will keep the cards, but destroy the item.
  11211. /// <type>=? : will just display the failure effect.
  11212. BUILDIN_FUNC(failedremovecards) {
  11213. int i=-1,c,cardflag=0;
  11214. TBL_PC* sd = script_rid2sd(st);
  11215. int num = script_getnum(st,2);
  11216. int typefail = script_getnum(st,3);
  11217. if (equip_index_check(num))
  11218. i=pc_checkequip(sd,equip_bitmask[num]);
  11219. if (i < 0 || !sd->inventory_data[i])
  11220. return SCRIPT_CMD_SUCCESS;
  11221. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11222. return SCRIPT_CMD_SUCCESS;
  11223. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11224. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11225. cardflag = 1;
  11226. if(typefail == 2) {// add cards to inventory, clear
  11227. unsigned char flag = 0;
  11228. struct item item_tmp;
  11229. memset(&item_tmp,0,sizeof(item_tmp));
  11230. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11231. item_tmp.identify = 1;
  11232. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11233. clif_additem(sd,0,0,flag);
  11234. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11235. }
  11236. }
  11237. }
  11238. }
  11239. if(cardflag == 1) {
  11240. if(typefail == 0 || typefail == 2){ // destroy the item
  11241. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11242. }else if(typefail == 1){ // destroy the card
  11243. unsigned char flag = 0, j;
  11244. struct item item_tmp;
  11245. memset(&item_tmp,0,sizeof(item_tmp));
  11246. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11247. item_tmp.identify = 1;
  11248. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11249. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11250. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11251. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11252. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11253. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11254. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11255. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11256. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11257. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11258. }
  11259. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11260. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11261. clif_additem(sd,0,0,flag);
  11262. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11263. }
  11264. }
  11265. clif_misceffect(&sd->bl,2);
  11266. }
  11267. return SCRIPT_CMD_SUCCESS;
  11268. }
  11269. /* ================================================================
  11270. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11271. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11272. * improved by [Lance]
  11273. * ================================================================*/
  11274. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11275. {
  11276. int x,y,m,check_val=0,check_ID=0,i=0;
  11277. struct guild *g = NULL;
  11278. struct party_data *p = NULL;
  11279. const char *str;
  11280. const char *mapname;
  11281. unsigned int index;
  11282. mapname=script_getstr(st,2);
  11283. str=script_getstr(st,3);
  11284. x=script_getnum(st,4);
  11285. y=script_getnum(st,5);
  11286. if(script_hasdata(st,7)){
  11287. check_val=script_getnum(st,6);
  11288. check_ID=script_getnum(st,7);
  11289. }
  11290. if((m=map_mapname2mapid(mapname))< 0)
  11291. return SCRIPT_CMD_SUCCESS;
  11292. if(!(index=mapindex_name2id(str)))
  11293. return SCRIPT_CMD_SUCCESS;
  11294. switch(check_val){
  11295. case 1:
  11296. g = guild_search(check_ID);
  11297. if (g){
  11298. for( i=0; i < g->max_member; i++)
  11299. {
  11300. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11301. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11302. }
  11303. }
  11304. }
  11305. break;
  11306. case 2:
  11307. p = party_search(check_ID);
  11308. if(p){
  11309. for(i=0;i<MAX_PARTY; i++){
  11310. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11311. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11312. }
  11313. }
  11314. }
  11315. break;
  11316. default:
  11317. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11318. break;
  11319. }
  11320. return SCRIPT_CMD_SUCCESS;
  11321. }
  11322. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11323. {
  11324. char *event=va_arg(ap,char *);
  11325. struct mob_data *md = ((struct mob_data *)bl);
  11326. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11327. return 1;
  11328. return SCRIPT_CMD_SUCCESS;
  11329. }
  11330. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11331. {
  11332. const char *mapname,*event;
  11333. int16 m;
  11334. mapname=script_getstr(st,2);
  11335. event=script_getstr(st,3);
  11336. if( strcmp(event, "all") == 0 )
  11337. event = NULL;
  11338. else
  11339. check_event(st, event);
  11340. if( strcmp(mapname, "this") == 0 ) {
  11341. struct map_session_data *sd = script_rid2sd(st);
  11342. if( sd )
  11343. m = sd->bl.m;
  11344. else {
  11345. script_pushint(st,-1);
  11346. return SCRIPT_CMD_SUCCESS;
  11347. }
  11348. }
  11349. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11350. script_pushint(st,-1);
  11351. return SCRIPT_CMD_SUCCESS;
  11352. }
  11353. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11354. return SCRIPT_CMD_SUCCESS;
  11355. }
  11356. BUILDIN_FUNC(marriage)
  11357. {
  11358. const char *partner=script_getstr(st,2);
  11359. TBL_PC *sd=script_rid2sd(st);
  11360. TBL_PC *p_sd=map_nick2sd(partner,false);
  11361. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  11362. script_pushint(st,0);
  11363. return SCRIPT_CMD_SUCCESS;
  11364. }
  11365. script_pushint(st,1);
  11366. return SCRIPT_CMD_SUCCESS;
  11367. }
  11368. BUILDIN_FUNC(wedding_effect)
  11369. {
  11370. TBL_PC *sd=script_rid2sd(st);
  11371. struct block_list *bl;
  11372. if(sd==NULL) {
  11373. bl=map_id2bl(st->oid);
  11374. } else
  11375. bl=&sd->bl;
  11376. clif_wedding_effect(bl);
  11377. return SCRIPT_CMD_SUCCESS;
  11378. }
  11379. /**
  11380. * divorce({<char_id>})
  11381. **/
  11382. BUILDIN_FUNC(divorce)
  11383. {
  11384. TBL_PC *sd;
  11385. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11386. script_pushint(st,0);
  11387. return SCRIPT_CMD_FAILURE;
  11388. }
  11389. script_pushint(st,1);
  11390. return SCRIPT_CMD_SUCCESS;
  11391. }
  11392. /**
  11393. * ispartneron({<char_id>})
  11394. **/
  11395. BUILDIN_FUNC(ispartneron)
  11396. {
  11397. TBL_PC *sd;
  11398. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11399. map_charid2sd(sd->status.partner_id) == NULL)
  11400. {
  11401. script_pushint(st,0);
  11402. return SCRIPT_CMD_FAILURE;
  11403. }
  11404. script_pushint(st,1);
  11405. return SCRIPT_CMD_SUCCESS;
  11406. }
  11407. /**
  11408. * getpartnerid({<char_id>})
  11409. **/
  11410. BUILDIN_FUNC(getpartnerid)
  11411. {
  11412. TBL_PC *sd;
  11413. if (!script_charid2sd(2,sd)) {
  11414. script_pushint(st,0);
  11415. return SCRIPT_CMD_FAILURE;
  11416. }
  11417. script_pushint(st,sd->status.partner_id);
  11418. return SCRIPT_CMD_SUCCESS;
  11419. }
  11420. /**
  11421. * getchildid({<char_id>})
  11422. **/
  11423. BUILDIN_FUNC(getchildid)
  11424. {
  11425. TBL_PC *sd;
  11426. if (!script_charid2sd(2,sd)) {
  11427. script_pushint(st,0);
  11428. return SCRIPT_CMD_FAILURE;
  11429. }
  11430. script_pushint(st,sd->status.child);
  11431. return SCRIPT_CMD_SUCCESS;
  11432. }
  11433. /**
  11434. * getmotherid({<char_id>})
  11435. **/
  11436. BUILDIN_FUNC(getmotherid)
  11437. {
  11438. TBL_PC *sd;
  11439. if (!script_charid2sd(2,sd)) {
  11440. script_pushint(st,0);
  11441. return SCRIPT_CMD_FAILURE;
  11442. }
  11443. script_pushint(st,sd->status.mother);
  11444. return SCRIPT_CMD_SUCCESS;
  11445. }
  11446. /**
  11447. * getfatherid({<char_id>})
  11448. **/
  11449. BUILDIN_FUNC(getfatherid)
  11450. {
  11451. TBL_PC *sd;
  11452. if (!script_charid2sd(2,sd)) {
  11453. script_pushint(st,0);
  11454. return SCRIPT_CMD_FAILURE;
  11455. }
  11456. script_pushint(st,sd->status.father);
  11457. return SCRIPT_CMD_SUCCESS;
  11458. }
  11459. BUILDIN_FUNC(warppartner)
  11460. {
  11461. int x,y;
  11462. unsigned short mapindex;
  11463. const char *str;
  11464. TBL_PC *sd=script_rid2sd(st);
  11465. TBL_PC *p_sd=NULL;
  11466. if(sd==NULL || !pc_ismarried(sd) ||
  11467. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11468. script_pushint(st,0);
  11469. return SCRIPT_CMD_SUCCESS;
  11470. }
  11471. str=script_getstr(st,2);
  11472. x=script_getnum(st,3);
  11473. y=script_getnum(st,4);
  11474. mapindex = mapindex_name2id(str);
  11475. if (mapindex) {
  11476. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11477. script_pushint(st,1);
  11478. } else
  11479. script_pushint(st,0);
  11480. return SCRIPT_CMD_SUCCESS;
  11481. }
  11482. /*================================================
  11483. * Script for Displaying MOB Information [Valaris]
  11484. *------------------------------------------------*/
  11485. BUILDIN_FUNC(strmobinfo)
  11486. {
  11487. int num=script_getnum(st,2);
  11488. int class_=script_getnum(st,3);
  11489. if(!mobdb_checkid(class_))
  11490. {
  11491. if (num < 3) //requested a string
  11492. script_pushconststr(st,"");
  11493. else
  11494. script_pushint(st,0);
  11495. return SCRIPT_CMD_SUCCESS;
  11496. }
  11497. switch (num) {
  11498. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11499. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11500. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11501. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11502. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11503. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11504. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11505. default:
  11506. script_pushint(st,0);
  11507. break;
  11508. }
  11509. return SCRIPT_CMD_SUCCESS;
  11510. }
  11511. /*==========================================
  11512. * Summon guardians [Valaris]
  11513. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11514. *------------------------------------------*/
  11515. BUILDIN_FUNC(guardian)
  11516. {
  11517. int class_=0,x=0,y=0,guardian=0;
  11518. const char *str,*mapname,*evt="";
  11519. bool has_index = false;
  11520. mapname =script_getstr(st,2);
  11521. x =script_getnum(st,3);
  11522. y =script_getnum(st,4);
  11523. str =script_getstr(st,5);
  11524. class_=script_getnum(st,6);
  11525. if( script_hasdata(st,8) )
  11526. {// "<event label>",<guardian index>
  11527. evt=script_getstr(st,7);
  11528. guardian=script_getnum(st,8);
  11529. has_index = true;
  11530. } else if( script_hasdata(st,7) ){
  11531. struct script_data *data = script_getdata(st,7);
  11532. get_val(st,data);
  11533. if( data_isstring(data) )
  11534. {// "<event label>"
  11535. evt=script_getstr(st,7);
  11536. } else if( data_isint(data) )
  11537. {// <guardian index>
  11538. guardian=script_getnum(st,7);
  11539. has_index = true;
  11540. } else {
  11541. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11542. script_reportdata(data);
  11543. return SCRIPT_CMD_FAILURE;
  11544. }
  11545. }
  11546. check_event(st, evt);
  11547. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11548. return SCRIPT_CMD_SUCCESS;
  11549. }
  11550. /*==========================================
  11551. * Invisible Walls [Zephyrus]
  11552. *------------------------------------------*/
  11553. BUILDIN_FUNC(setwall)
  11554. {
  11555. const char *mapname, *name;
  11556. int x, y, m, size, dir;
  11557. bool shootable;
  11558. mapname = script_getstr(st,2);
  11559. x = script_getnum(st,3);
  11560. y = script_getnum(st,4);
  11561. size = script_getnum(st,5);
  11562. dir = script_getnum(st,6);
  11563. shootable = script_getnum(st,7);
  11564. name = script_getstr(st,8);
  11565. if( (m = map_mapname2mapid(mapname)) < 0 )
  11566. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11567. map_iwall_set(m, x, y, size, dir, shootable, name);
  11568. return SCRIPT_CMD_SUCCESS;
  11569. }
  11570. BUILDIN_FUNC(delwall)
  11571. {
  11572. const char *name = script_getstr(st,2);
  11573. map_iwall_remove(name);
  11574. return SCRIPT_CMD_SUCCESS;
  11575. }
  11576. /// Retrieves various information about the specified guardian.
  11577. ///
  11578. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11579. /// type: 0 - whether it is deployed or not
  11580. /// 1 - maximum hp
  11581. /// 2 - current hp
  11582. ///
  11583. BUILDIN_FUNC(guardianinfo)
  11584. {
  11585. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11586. int id = script_getnum(st,3);
  11587. int type = script_getnum(st,4);
  11588. struct guild_castle* gc = guild_mapname2gc(mapname);
  11589. struct mob_data* gd;
  11590. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11591. {
  11592. script_pushint(st,-1);
  11593. return SCRIPT_CMD_SUCCESS;
  11594. }
  11595. if( type == 0 )
  11596. script_pushint(st, gc->guardian[id].visible);
  11597. else
  11598. if( !gc->guardian[id].visible )
  11599. script_pushint(st,-1);
  11600. else
  11601. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11602. script_pushint(st,-1);
  11603. else
  11604. {
  11605. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11606. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11607. else
  11608. script_pushint(st,-1);
  11609. }
  11610. return SCRIPT_CMD_SUCCESS;
  11611. }
  11612. /*==========================================
  11613. * Get the item name by item_id or null
  11614. *------------------------------------------*/
  11615. BUILDIN_FUNC(getitemname)
  11616. {
  11617. unsigned short item_id = 0;
  11618. struct item_data *i_data;
  11619. char *item_name;
  11620. struct script_data *data;
  11621. data=script_getdata(st,2);
  11622. get_val(st,data);
  11623. if( data_isstring(data) ){
  11624. const char *name=conv_str(st,data);
  11625. struct item_data *item_data = itemdb_searchname(name);
  11626. if( item_data )
  11627. item_id=item_data->nameid;
  11628. }else
  11629. item_id=conv_num(st,data);
  11630. i_data = itemdb_exists(item_id);
  11631. if (i_data == NULL)
  11632. {
  11633. script_pushconststr(st,"null");
  11634. return SCRIPT_CMD_SUCCESS;
  11635. }
  11636. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11637. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11638. script_pushstr(st,item_name);
  11639. return SCRIPT_CMD_SUCCESS;
  11640. }
  11641. /*==========================================
  11642. * Returns number of slots an item has. [Skotlex]
  11643. *------------------------------------------*/
  11644. BUILDIN_FUNC(getitemslots)
  11645. {
  11646. unsigned short item_id;
  11647. struct item_data *i_data;
  11648. item_id=script_getnum(st,2);
  11649. i_data = itemdb_exists(item_id);
  11650. if (i_data)
  11651. script_pushint(st,i_data->slot);
  11652. else
  11653. script_pushint(st,-1);
  11654. return SCRIPT_CMD_SUCCESS;
  11655. }
  11656. // TODO: add matk here if needed/once we get rid of RENEWAL
  11657. /*==========================================
  11658. * Returns some values of an item [Lupus]
  11659. * Price, Weight, etc...
  11660. getiteminfo(itemID,n), where n
  11661. 0 value_buy;
  11662. 1 value_sell;
  11663. 2 type;
  11664. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11665. if = 0, then monsters don't drop it at all (rare or a quest item)
  11666. if = -1, then this item is sold in NPC shops only
  11667. 4 sex;
  11668. 5 equip;
  11669. 6 weight;
  11670. 7 atk;
  11671. 8 def;
  11672. 9 range;
  11673. 10 slot;
  11674. 11 look;
  11675. 12 elv;
  11676. 13 wlv;
  11677. 14 view id
  11678. 15 eLvmax
  11679. 16 matk (renewal)
  11680. *------------------------------------------*/
  11681. BUILDIN_FUNC(getiteminfo)
  11682. {
  11683. unsigned short item_id,n;
  11684. struct item_data *i_data;
  11685. item_id = script_getnum(st,2);
  11686. n = script_getnum(st,3);
  11687. i_data = itemdb_exists(item_id);
  11688. if (i_data && n <= 16) {
  11689. int *item_arr = (int*)&i_data->value_buy;
  11690. #ifndef RENEWAL
  11691. if (n == 16)
  11692. script_pushint(st,0);
  11693. else
  11694. #endif
  11695. script_pushint(st,item_arr[n]);
  11696. } else
  11697. script_pushint(st,-1);
  11698. return SCRIPT_CMD_SUCCESS;
  11699. }
  11700. /*==========================================
  11701. * Set some values of an item [Lupus]
  11702. * Price, Weight, etc...
  11703. setiteminfo(itemID,n,Value), where n
  11704. 0 value_buy;
  11705. 1 value_sell;
  11706. 2 type;
  11707. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11708. if = 0, then monsters don't drop it at all (rare or a quest item)
  11709. if = -1, then this item is sold in NPC shops only
  11710. 4 sex;
  11711. 5 equip;
  11712. 6 weight;
  11713. 7 atk;
  11714. 8 def;
  11715. 9 range;
  11716. 10 slot;
  11717. 11 look;
  11718. 12 elv;
  11719. 13 wlv;
  11720. 14 view id
  11721. * Returns Value or -1 if the wrong field's been set
  11722. *------------------------------------------*/
  11723. BUILDIN_FUNC(setiteminfo)
  11724. {
  11725. unsigned short item_id;
  11726. int n,value;
  11727. struct item_data *i_data;
  11728. item_id = script_getnum(st,2);
  11729. n = script_getnum(st,3);
  11730. value = script_getnum(st,4);
  11731. i_data = itemdb_exists(item_id);
  11732. if (i_data && n>=0 && n<=14) {
  11733. int *item_arr = (int*)&i_data->value_buy;
  11734. item_arr[n] = value;
  11735. script_pushint(st,value);
  11736. } else
  11737. script_pushint(st,-1);
  11738. return SCRIPT_CMD_SUCCESS;
  11739. }
  11740. /*==========================================
  11741. * Returns value from equipped item slot n [Lupus]
  11742. getequipcardid(num,slot)
  11743. where
  11744. num = eqip position slot
  11745. slot = 0,1,2,3 (Card Slot N)
  11746. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11747. it's useful when you want to check item cards or if it's signed
  11748. Useful for such quests as "Sign this refined item with players name" etc
  11749. Hat[0] +4 -> Player's Hat[0] +4
  11750. *------------------------------------------*/
  11751. BUILDIN_FUNC(getequipcardid)
  11752. {
  11753. int i=-1,num,slot;
  11754. TBL_PC *sd;
  11755. num=script_getnum(st,2);
  11756. slot=script_getnum(st,3);
  11757. sd=script_rid2sd(st);
  11758. if (equip_index_check(num))
  11759. i=pc_checkequip(sd,equip_bitmask[num]);
  11760. if(i >= 0 && slot>=0 && slot<4)
  11761. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  11762. else
  11763. script_pushint(st,0);
  11764. return SCRIPT_CMD_SUCCESS;
  11765. }
  11766. /*==========================================
  11767. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11768. *------------------------------------------*/
  11769. BUILDIN_FUNC(petskillbonus)
  11770. {
  11771. struct pet_data *pd;
  11772. TBL_PC *sd = script_rid2sd(st);
  11773. if(sd == NULL || sd->pd == NULL)
  11774. return SCRIPT_CMD_FAILURE;
  11775. pd = sd->pd;
  11776. if (pd->bonus)
  11777. { //Clear previous bonus
  11778. if (pd->bonus->timer != INVALID_TIMER)
  11779. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11780. } else //init
  11781. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11782. pd->bonus->type = script_getnum(st,2);
  11783. pd->bonus->val = script_getnum(st,3);
  11784. pd->bonus->duration = script_getnum(st,4);
  11785. pd->bonus->delay = script_getnum(st,5);
  11786. if (pd->state.skillbonus == 1)
  11787. pd->state.skillbonus = 0; // waiting state
  11788. // wait for timer to start
  11789. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11790. pd->bonus->timer = INVALID_TIMER;
  11791. else
  11792. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11793. return SCRIPT_CMD_SUCCESS;
  11794. }
  11795. /*==========================================
  11796. * pet looting [Valaris] //Rewritten by [Skotlex]
  11797. *------------------------------------------*/
  11798. BUILDIN_FUNC(petloot)
  11799. {
  11800. int max;
  11801. struct pet_data *pd;
  11802. TBL_PC *sd=script_rid2sd(st);
  11803. if(sd==NULL || sd->pd==NULL)
  11804. return SCRIPT_CMD_SUCCESS;
  11805. max=script_getnum(st,2);
  11806. if(max < 1)
  11807. max = 1; //Let'em loot at least 1 item.
  11808. else if (max > MAX_PETLOOT_SIZE)
  11809. max = MAX_PETLOOT_SIZE;
  11810. pd = sd->pd;
  11811. if (pd->loot != NULL)
  11812. { //Release whatever was there already and reallocate memory
  11813. pet_lootitem_drop(pd, pd->master);
  11814. aFree(pd->loot->item);
  11815. }
  11816. else
  11817. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11818. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11819. pd->loot->max=max;
  11820. pd->loot->count = 0;
  11821. pd->loot->weight = 0;
  11822. return SCRIPT_CMD_SUCCESS;
  11823. }
  11824. /*==========================================
  11825. * Set arrays with info of all sd inventory :
  11826. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11827. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11828. * @inventorylist_card(0..3), @inventorylist_expire
  11829. * @inventorylist_count = scalar
  11830. *------------------------------------------*/
  11831. BUILDIN_FUNC(getinventorylist)
  11832. {
  11833. TBL_PC *sd;
  11834. char card_var[NAME_LENGTH];
  11835. int i,j=0,k;
  11836. if (!script_charid2sd(2,sd))
  11837. return SCRIPT_CMD_FAILURE;
  11838. for(i=0;i<MAX_INVENTORY;i++){
  11839. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  11840. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  11841. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  11842. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  11843. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  11844. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  11845. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  11846. for (k = 0; k < MAX_SLOTS; k++)
  11847. {
  11848. sprintf(card_var, "@inventorylist_card%d",k+1);
  11849. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  11850. }
  11851. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  11852. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  11853. j++;
  11854. }
  11855. }
  11856. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11857. return SCRIPT_CMD_SUCCESS;
  11858. }
  11859. /**
  11860. * getskilllist ({<char_id>});
  11861. **/
  11862. BUILDIN_FUNC(getskilllist)
  11863. {
  11864. TBL_PC *sd;
  11865. int i,j=0;
  11866. if (!script_charid2sd(2,sd))
  11867. return SCRIPT_CMD_FAILURE;
  11868. for(i=0;i<MAX_SKILL;i++){
  11869. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11870. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11871. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11872. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11873. j++;
  11874. }
  11875. }
  11876. pc_setreg(sd,add_str("@skilllist_count"),j);
  11877. return SCRIPT_CMD_SUCCESS;
  11878. }
  11879. /**
  11880. * clearitem ({<char_id>});
  11881. **/
  11882. BUILDIN_FUNC(clearitem)
  11883. {
  11884. TBL_PC *sd;
  11885. int i;
  11886. if (!script_charid2sd(2,sd))
  11887. return SCRIPT_CMD_FAILURE;
  11888. for (i=0; i<MAX_INVENTORY; i++) {
  11889. if (sd->inventory.u.items_inventory[i].amount) {
  11890. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11891. }
  11892. }
  11893. return SCRIPT_CMD_SUCCESS;
  11894. }
  11895. /**
  11896. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11897. * disguise <Monster ID>{,<char_id>};
  11898. **/
  11899. BUILDIN_FUNC(disguise)
  11900. {
  11901. int id;
  11902. TBL_PC* sd;
  11903. if (!script_charid2sd(3,sd))
  11904. return SCRIPT_CMD_FAILURE;
  11905. id = script_getnum(st,2);
  11906. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11907. pc_disguise(sd, id);
  11908. script_pushint(st,id);
  11909. } else
  11910. script_pushint(st,0);
  11911. return SCRIPT_CMD_SUCCESS;
  11912. }
  11913. /**
  11914. * Undisguise Player (returns 1 if success, 0 on fail)
  11915. * undisguise {<char_id>};
  11916. **/
  11917. BUILDIN_FUNC(undisguise)
  11918. {
  11919. TBL_PC* sd;
  11920. if (!script_charid2sd(2,sd))
  11921. return SCRIPT_CMD_FAILURE;
  11922. if (sd->disguise) {
  11923. pc_disguise(sd, 0);
  11924. script_pushint(st,0);
  11925. } else {
  11926. script_pushint(st,1);
  11927. }
  11928. return SCRIPT_CMD_SUCCESS;
  11929. }
  11930. /*==========================================
  11931. * Transform a bl to another _class,
  11932. * @type unused
  11933. *------------------------------------------*/
  11934. BUILDIN_FUNC(classchange)
  11935. {
  11936. int _class,type;
  11937. struct block_list *bl=map_id2bl(st->oid);
  11938. if (bl==NULL)
  11939. return SCRIPT_CMD_FAILURE;
  11940. _class=script_getnum(st,2);
  11941. type=script_getnum(st,3);
  11942. clif_class_change(bl,_class,type);
  11943. return SCRIPT_CMD_SUCCESS;
  11944. }
  11945. /*==========================================
  11946. * Display an effect
  11947. *------------------------------------------*/
  11948. BUILDIN_FUNC(misceffect)
  11949. {
  11950. int type;
  11951. type=script_getnum(st,2);
  11952. if(st->oid && st->oid != fake_nd->bl.id) {
  11953. struct block_list *bl = map_id2bl(st->oid);
  11954. if (bl)
  11955. clif_specialeffect(bl,type,AREA);
  11956. } else{
  11957. TBL_PC *sd=script_rid2sd(st);
  11958. if(sd)
  11959. clif_specialeffect(&sd->bl,type,AREA);
  11960. }
  11961. return SCRIPT_CMD_SUCCESS;
  11962. }
  11963. /*==========================================
  11964. * Play a BGM on a single client [Rikter/Yommy]
  11965. *------------------------------------------*/
  11966. BUILDIN_FUNC(playBGM)
  11967. {
  11968. struct map_session_data* sd;
  11969. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11970. clif_playBGM(sd, script_getstr(st,2));
  11971. }
  11972. return SCRIPT_CMD_SUCCESS;
  11973. }
  11974. static int playBGM_sub(struct block_list* bl,va_list ap)
  11975. {
  11976. const char* name = va_arg(ap,const char*);
  11977. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11978. return 0;
  11979. }
  11980. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11981. {
  11982. const char* name = va_arg(args, const char*);
  11983. clif_playBGM(sd, name);
  11984. return 0;
  11985. }
  11986. /*==========================================
  11987. * Play a BGM on multiple client [Rikter/Yommy]
  11988. *------------------------------------------*/
  11989. BUILDIN_FUNC(playBGMall)
  11990. {
  11991. const char* name;
  11992. name = script_getstr(st,2);
  11993. if( script_hasdata(st,7) ) {// specified part of map
  11994. const char* mapname = script_getstr(st,3);
  11995. int x0 = script_getnum(st,4);
  11996. int y0 = script_getnum(st,5);
  11997. int x1 = script_getnum(st,6);
  11998. int y1 = script_getnum(st,7);
  11999. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  12000. }
  12001. else if( script_hasdata(st,3) ) {// entire map
  12002. const char* mapname = script_getstr(st,3);
  12003. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  12004. }
  12005. else {// entire server
  12006. map_foreachpc(&playBGM_foreachpc_sub, name);
  12007. }
  12008. return SCRIPT_CMD_SUCCESS;
  12009. }
  12010. /*==========================================
  12011. * Play a .wav sound for sd
  12012. *------------------------------------------*/
  12013. BUILDIN_FUNC(soundeffect)
  12014. {
  12015. TBL_PC* sd = script_rid2sd(st);
  12016. const char* name = script_getstr(st,2);
  12017. int type = script_getnum(st,3);
  12018. if(sd) {
  12019. clif_soundeffect(sd,&sd->bl,name,type);
  12020. }
  12021. return SCRIPT_CMD_SUCCESS;
  12022. }
  12023. int soundeffect_sub(struct block_list* bl,va_list ap)
  12024. {
  12025. char* name = va_arg(ap,char*);
  12026. int type = va_arg(ap,int);
  12027. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  12028. return 0;
  12029. }
  12030. /*==========================================
  12031. * Play a sound effect (.wav) on multiple clients
  12032. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  12033. *------------------------------------------*/
  12034. BUILDIN_FUNC(soundeffectall)
  12035. {
  12036. struct block_list* bl;
  12037. const char* name;
  12038. int type;
  12039. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  12040. if (!bl)
  12041. return SCRIPT_CMD_SUCCESS;
  12042. name = script_getstr(st,2);
  12043. type = script_getnum(st,3);
  12044. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12045. if(!script_hasdata(st,4))
  12046. { // area around
  12047. clif_soundeffectall(bl, name, type, AREA);
  12048. }
  12049. else
  12050. if(!script_hasdata(st,5))
  12051. { // entire map
  12052. const char* mapname = script_getstr(st,4);
  12053. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  12054. }
  12055. else
  12056. if(script_hasdata(st,8))
  12057. { // specified part of map
  12058. const char* mapname = script_getstr(st,4);
  12059. int x0 = script_getnum(st,5);
  12060. int y0 = script_getnum(st,6);
  12061. int x1 = script_getnum(st,7);
  12062. int y1 = script_getnum(st,8);
  12063. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  12064. }
  12065. else
  12066. {
  12067. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  12068. }
  12069. return SCRIPT_CMD_SUCCESS;
  12070. }
  12071. /*==========================================
  12072. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  12073. *------------------------------------------*/
  12074. BUILDIN_FUNC(petrecovery)
  12075. {
  12076. struct pet_data *pd;
  12077. TBL_PC *sd=script_rid2sd(st);
  12078. int sc;
  12079. if(sd == NULL || sd->pd == NULL)
  12080. return SCRIPT_CMD_FAILURE;
  12081. sc = script_getnum(st,2);
  12082. if (sc <= SC_NONE || sc >= SC_MAX) {
  12083. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  12084. return SCRIPT_CMD_FAILURE;
  12085. }
  12086. pd = sd->pd;
  12087. if (pd->recovery)
  12088. { //Halt previous bonus
  12089. if (pd->recovery->timer != INVALID_TIMER)
  12090. delete_timer(pd->recovery->timer, pet_recovery_timer);
  12091. } else //Init
  12092. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  12093. pd->recovery->type = (sc_type)sc;
  12094. pd->recovery->delay = script_getnum(st,3);
  12095. pd->recovery->timer = INVALID_TIMER;
  12096. return SCRIPT_CMD_SUCCESS;
  12097. }
  12098. /*==========================================
  12099. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12100. *------------------------------------------*/
  12101. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12102. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12103. BUILDIN_FUNC(petskillattack)
  12104. {
  12105. struct pet_data *pd;
  12106. struct script_data *data;
  12107. TBL_PC *sd = script_rid2sd(st);
  12108. int id = 0;
  12109. if(sd == NULL || sd->pd == NULL)
  12110. return SCRIPT_CMD_FAILURE;
  12111. data = script_getdata(st, 2);
  12112. get_val(st, data);
  12113. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12114. if (!id) {
  12115. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12116. return SCRIPT_CMD_FAILURE;
  12117. }
  12118. pd = sd->pd;
  12119. if (pd->a_skill == NULL)
  12120. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12121. pd->a_skill->id = id;
  12122. pd->a_skill->damage = 0;
  12123. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12124. pd->a_skill->div_ = 0;
  12125. pd->a_skill->rate = script_getnum(st,4);
  12126. pd->a_skill->bonusrate = script_getnum(st,5);
  12127. return SCRIPT_CMD_SUCCESS;
  12128. }
  12129. /*==========================================
  12130. * pet attack skills [Valaris]
  12131. *------------------------------------------*/
  12132. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12133. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12134. BUILDIN_FUNC(petskillattack2)
  12135. {
  12136. struct pet_data *pd;
  12137. struct script_data *data;
  12138. TBL_PC *sd = script_rid2sd(st);
  12139. int id = 0;
  12140. if(sd == NULL || sd->pd == NULL)
  12141. return SCRIPT_CMD_FAILURE;
  12142. data = script_getdata(st, 2);
  12143. get_val(st, data);
  12144. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12145. if (!id) {
  12146. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12147. return SCRIPT_CMD_FAILURE;
  12148. }
  12149. pd = sd->pd;
  12150. if (pd->a_skill == NULL)
  12151. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12152. pd->a_skill->id = id;
  12153. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12154. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12155. pd->a_skill->div_ = script_getnum(st,4);
  12156. pd->a_skill->rate = script_getnum(st,5);
  12157. pd->a_skill->bonusrate = script_getnum(st,6);
  12158. return SCRIPT_CMD_SUCCESS;
  12159. }
  12160. /*==========================================
  12161. * pet support skills [Skotlex]
  12162. *------------------------------------------*/
  12163. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12164. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12165. BUILDIN_FUNC(petskillsupport)
  12166. {
  12167. struct pet_data *pd;
  12168. struct script_data *data;
  12169. TBL_PC *sd = script_rid2sd(st);
  12170. int id = 0;
  12171. if(sd == NULL || sd->pd == NULL)
  12172. return SCRIPT_CMD_FAILURE;
  12173. data = script_getdata(st, 2);
  12174. get_val(st, data);
  12175. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12176. if (!id) {
  12177. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12178. return SCRIPT_CMD_FAILURE;
  12179. }
  12180. pd = sd->pd;
  12181. if (pd->s_skill)
  12182. { //Clear previous skill
  12183. if (pd->s_skill->timer != INVALID_TIMER)
  12184. {
  12185. if (pd->s_skill->id)
  12186. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12187. else
  12188. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12189. }
  12190. } else //init memory
  12191. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12192. pd->s_skill->id = id;
  12193. pd->s_skill->lv = script_getnum(st,3);
  12194. pd->s_skill->delay = script_getnum(st,4);
  12195. pd->s_skill->hp = script_getnum(st,5);
  12196. pd->s_skill->sp = script_getnum(st,6);
  12197. //Use delay as initial offset to avoid skill/heal exploits
  12198. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12199. pd->s_skill->timer = INVALID_TIMER;
  12200. else
  12201. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12202. return SCRIPT_CMD_SUCCESS;
  12203. }
  12204. /*==========================================
  12205. * Scripted skill effects [Celest]
  12206. *------------------------------------------*/
  12207. /// skilleffect <skill id>,<level>
  12208. /// skilleffect "<skill name>",<level>
  12209. BUILDIN_FUNC(skilleffect)
  12210. {
  12211. TBL_PC *sd;
  12212. uint16 skill_id, skill_lv;
  12213. struct script_data *data = script_getdata(st, 2);
  12214. get_val(st, data); // Convert into value in case of a variable
  12215. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12216. skill_lv = script_getnum(st,3);
  12217. sd = script_rid2sd(st);
  12218. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12219. * which leaves the server thinking it still is sitting. */
  12220. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12221. skill_sit(sd, 0);
  12222. clif_standing(&sd->bl);
  12223. }
  12224. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  12225. return SCRIPT_CMD_SUCCESS;
  12226. }
  12227. /*==========================================
  12228. * NPC skill effects [Valaris]
  12229. *------------------------------------------*/
  12230. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12231. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12232. BUILDIN_FUNC(npcskilleffect)
  12233. {
  12234. struct block_list *bl= map_id2bl(st->oid);
  12235. uint16 skill_id, skill_lv;
  12236. int x, y;
  12237. struct script_data *data = script_getdata(st, 2);
  12238. get_val(st, data); // Convert into value in case of a variable
  12239. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12240. skill_lv=script_getnum(st,3);
  12241. x=script_getnum(st,4);
  12242. y=script_getnum(st,5);
  12243. if (bl)
  12244. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12245. return SCRIPT_CMD_SUCCESS;
  12246. }
  12247. /*==========================================
  12248. * Special effects [Valaris]
  12249. *------------------------------------------*/
  12250. BUILDIN_FUNC(specialeffect)
  12251. {
  12252. struct block_list *bl=map_id2bl(st->oid);
  12253. int type = script_getnum(st,2);
  12254. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12255. if(bl==NULL)
  12256. return SCRIPT_CMD_SUCCESS;
  12257. if( script_hasdata(st,4) )
  12258. {
  12259. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12260. if(nd)
  12261. clif_specialeffect(&nd->bl, type, target);
  12262. }
  12263. else
  12264. {
  12265. if (target == SELF) {
  12266. TBL_PC *sd=script_rid2sd(st);
  12267. if (sd)
  12268. clif_specialeffect_single(bl,type,sd->fd);
  12269. } else {
  12270. clif_specialeffect(bl, type, target);
  12271. }
  12272. }
  12273. return SCRIPT_CMD_SUCCESS;
  12274. }
  12275. BUILDIN_FUNC(specialeffect2)
  12276. {
  12277. TBL_PC *sd=script_rid2sd(st);
  12278. int type = script_getnum(st,2);
  12279. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12280. if( script_nick2sd(4,sd) )
  12281. clif_specialeffect(&sd->bl, type, target);
  12282. return SCRIPT_CMD_SUCCESS;
  12283. }
  12284. /**
  12285. * nude({<char_id>});
  12286. * @author [Valaris]
  12287. **/
  12288. BUILDIN_FUNC(nude)
  12289. {
  12290. TBL_PC *sd;
  12291. int i, calcflag = 0;
  12292. if (!script_charid2sd(2,sd))
  12293. return SCRIPT_CMD_FAILURE;
  12294. for( i = 0 ; i < EQI_MAX; i++ ) {
  12295. if( sd->equip_index[ i ] >= 0 ) {
  12296. if( !calcflag )
  12297. calcflag = 1;
  12298. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12299. }
  12300. }
  12301. if( calcflag )
  12302. status_calc_pc(sd,SCO_NONE);
  12303. return SCRIPT_CMD_SUCCESS;
  12304. }
  12305. int atcommand_sub(struct script_state* st,int type) {
  12306. TBL_PC *sd, dummy_sd;
  12307. int fd;
  12308. const char *cmd;
  12309. cmd = script_getstr(st,2);
  12310. if (st->rid) {
  12311. sd = script_rid2sd(st);
  12312. fd = sd->fd;
  12313. } else { //Use a dummy character.
  12314. sd = &dummy_sd;
  12315. fd = 0;
  12316. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12317. if (st->oid) {
  12318. struct block_list* bl = map_id2bl(st->oid);
  12319. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12320. if (bl->type == BL_NPC)
  12321. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12322. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : mapindex_name2id(map_default.mapname);
  12323. }
  12324. // Init Group ID, Level, & permissions
  12325. sd->group_id = sd->group_level = 99;
  12326. sd->permissions |= PC_PERM_ALLPERMISSION;
  12327. }
  12328. if (!is_atcommand(fd, sd, cmd, type)) {
  12329. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12330. script_reportsrc(st);
  12331. return SCRIPT_CMD_FAILURE;
  12332. }
  12333. return SCRIPT_CMD_SUCCESS;
  12334. }
  12335. /*==========================================
  12336. * gmcommand [MouseJstr]
  12337. *------------------------------------------*/
  12338. BUILDIN_FUNC(atcommand) {
  12339. return atcommand_sub(st,0);
  12340. }
  12341. /** Displays a message for the player only (like system messages like "you got an apple" )
  12342. * dispbottom("<message>"{,<color>{,<char_id>}})
  12343. * @param message
  12344. * @param color Hex color default (Green)
  12345. */
  12346. BUILDIN_FUNC(dispbottom)
  12347. {
  12348. TBL_PC *sd;
  12349. int color = 0;
  12350. const char *message;
  12351. if (!script_charid2sd(4,sd))
  12352. return SCRIPT_CMD_FAILURE;
  12353. message = script_getstr(st,2);
  12354. if (script_hasdata(st,3))
  12355. color = script_getnum(st,3); // <color>
  12356. if(sd) {
  12357. if (script_hasdata(st,3))
  12358. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  12359. else
  12360. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  12361. }
  12362. return SCRIPT_CMD_SUCCESS;
  12363. }
  12364. /*===================================
  12365. * Heal portion of recovery command
  12366. *-----------------------------------*/
  12367. int recovery_sub(struct map_session_data* sd, int revive)
  12368. {
  12369. if(revive&(1|4) && pc_isdead(sd)) {
  12370. status_revive(&sd->bl, 100, 100);
  12371. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12372. clif_specialeffect(&sd->bl, 77, AREA);
  12373. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12374. status_percent_heal(&sd->bl, 100, 100);
  12375. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12376. }
  12377. return SCRIPT_CMD_SUCCESS;
  12378. }
  12379. /*=========================================================================
  12380. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12381. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12382. * <type> determines <option>:
  12383. * 0 : char_id
  12384. * 1 : party_id
  12385. * 2 : guild_id
  12386. * 3 : map_name
  12387. * 4 : all characters
  12388. * <revive_flag>:
  12389. * 1 : Revive and heal all players (default)
  12390. * 2 : Heal living players only
  12391. * 4 : Revive dead players only
  12392. * <map name>:
  12393. * for types 1-2 : map_name (null = all maps)
  12394. *-------------------------------------------------------------------------*/
  12395. BUILDIN_FUNC(recovery)
  12396. {
  12397. TBL_PC *sd;
  12398. int map_idx = 0, type = 0, revive = 1;
  12399. type = script_getnum(st,2);
  12400. if(script_hasdata(st,4))
  12401. revive = script_getnum(st,4);
  12402. switch(type) {
  12403. case 0:
  12404. if(script_hasdata(st,3))
  12405. sd=map_charid2sd(script_getnum(st,3));
  12406. else if((sd = script_rid2sd(st)) == NULL)
  12407. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12408. recovery_sub(sd, revive);
  12409. break;
  12410. case 1:
  12411. {
  12412. struct party_data* p;
  12413. //When no party given, we use invoker party
  12414. int p_id = 0, i;
  12415. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12416. map_idx = map_mapname2mapid(script_getstr(st,5));
  12417. if(map_idx < 1) { //But we'll check anyways
  12418. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12419. return SCRIPT_CMD_FAILURE;
  12420. }
  12421. }
  12422. if(script_hasdata(st,3))
  12423. p_id = script_getnum(st,3);
  12424. else if((sd = script_rid2sd(st)))
  12425. p_id = sd->status.party_id;
  12426. p = party_search(p_id);
  12427. if(p == NULL)
  12428. return SCRIPT_CMD_SUCCESS;
  12429. for (i = 0; i < MAX_PARTY; i++) {
  12430. struct map_session_data* pl_sd;
  12431. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12432. || (map_idx && pl_sd->bl.m != map_idx))
  12433. continue;
  12434. recovery_sub(pl_sd, revive);
  12435. }
  12436. break;
  12437. }
  12438. case 2:
  12439. {
  12440. struct guild* g;
  12441. //When no guild given, we use invoker guild
  12442. int g_id = 0, i;
  12443. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12444. map_idx = map_mapname2mapid(script_getstr(st,5));
  12445. if(map_idx < 1) { //But we'll check anyways
  12446. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12447. return SCRIPT_CMD_FAILURE;
  12448. }
  12449. }
  12450. if(script_hasdata(st,3))
  12451. g_id = script_getnum(st,3);
  12452. else if((sd = script_rid2sd(st)))
  12453. g_id = sd->status.guild_id;
  12454. g = guild_search(g_id);
  12455. if(g == NULL)
  12456. return SCRIPT_CMD_SUCCESS;
  12457. for (i = 0; i < MAX_GUILD; i++) {
  12458. struct map_session_data* pl_sd;
  12459. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12460. || (map_idx && pl_sd->bl.m != map_idx))
  12461. continue;
  12462. recovery_sub(pl_sd, revive);
  12463. }
  12464. break;
  12465. }
  12466. case 3:
  12467. if(script_hasdata(st,3))
  12468. map_idx = map_mapname2mapid(script_getstr(st,3));
  12469. else if((sd = script_rid2sd(st)))
  12470. map_idx = sd->bl.m;
  12471. if(map_idx < 1)
  12472. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12473. case 4:
  12474. {
  12475. struct s_mapiterator *iter;
  12476. struct script_data *data = script_getdata(st, 3);
  12477. get_val(st, data); // Convert into value in case of a variable
  12478. if(script_hasdata(st,3) && !data_isstring(data))
  12479. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12480. iter = mapit_getallusers();
  12481. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12482. if(type == 3 && sd->bl.m != map_idx)
  12483. continue;
  12484. recovery_sub(sd, revive);
  12485. }
  12486. mapit_free(iter);
  12487. break;
  12488. }
  12489. default:
  12490. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12491. script_pushint(st,-1);
  12492. return SCRIPT_CMD_FAILURE;
  12493. }
  12494. script_pushint(st,1); //Successfully executed without errors
  12495. return SCRIPT_CMD_SUCCESS;
  12496. }
  12497. /*==========================================
  12498. * Get your pet info: getpetinfo <type>{,<char_id>}
  12499. * n -> 0:pet_id 1:pet_class 2:pet_name
  12500. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12501. * 7:game id
  12502. *------------------------------------------*/
  12503. BUILDIN_FUNC(getpetinfo)
  12504. {
  12505. TBL_PC *sd=script_rid2sd(st);
  12506. TBL_PET *pd;
  12507. int type = script_getnum(st,2);
  12508. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12509. if (type == 2)
  12510. script_pushconststr(st,"null");
  12511. else
  12512. script_pushint(st,0);
  12513. return SCRIPT_CMD_SUCCESS;
  12514. }
  12515. switch(type){
  12516. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  12517. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  12518. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  12519. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  12520. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  12521. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  12522. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  12523. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  12524. default:
  12525. script_pushint(st,0);
  12526. break;
  12527. }
  12528. return SCRIPT_CMD_SUCCESS;
  12529. }
  12530. /*==========================================
  12531. * Get your homunculus info: gethominfo <type>{,<char_id>};
  12532. * type -> 0:hom_id 1:class 2:name
  12533. * 3:friendly 4:hungry, 5: rename flag.
  12534. * 6: level, 7: game id
  12535. *------------------------------------------*/
  12536. BUILDIN_FUNC(gethominfo)
  12537. {
  12538. TBL_PC *sd=script_rid2sd(st);
  12539. TBL_HOM *hd;
  12540. int type=script_getnum(st,2);
  12541. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  12542. if (type == 2)
  12543. script_pushconststr(st,"null");
  12544. else
  12545. script_pushint(st,0);
  12546. return SCRIPT_CMD_SUCCESS;
  12547. }
  12548. switch(type){
  12549. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12550. case 1: script_pushint(st,hd->homunculus.class_); break;
  12551. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12552. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12553. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12554. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12555. case 6: script_pushint(st,hd->homunculus.level); break;
  12556. case 7: script_pushint(st,hd->bl.id); break;
  12557. default:
  12558. script_pushint(st,0);
  12559. break;
  12560. }
  12561. return SCRIPT_CMD_SUCCESS;
  12562. }
  12563. /// Retrieves information about character's mercenary
  12564. /// getmercinfo <type>[,<char id>];
  12565. BUILDIN_FUNC(getmercinfo)
  12566. {
  12567. int type;
  12568. struct map_session_data* sd;
  12569. struct mercenary_data* md;
  12570. type = script_getnum(st,2);
  12571. if( script_hasdata(st,3) )
  12572. {
  12573. uint32 char_id = script_getnum(st,3);
  12574. if( ( sd = map_charid2sd(char_id) ) == NULL )
  12575. {
  12576. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  12577. script_pushnil(st);
  12578. return SCRIPT_CMD_FAILURE;
  12579. }
  12580. }
  12581. else
  12582. {
  12583. if( ( sd = script_rid2sd(st) ) == NULL )
  12584. {
  12585. script_pushnil(st);
  12586. return SCRIPT_CMD_SUCCESS;
  12587. }
  12588. }
  12589. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12590. switch( type )
  12591. {
  12592. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12593. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12594. case 2:
  12595. if( md )
  12596. script_pushstrcopy(st,md->db->name);
  12597. else
  12598. script_pushconststr(st,"");
  12599. break;
  12600. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12601. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12602. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12603. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12604. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12605. case 8: script_pushint(st,md ? md->bl.id : 0); break;
  12606. default:
  12607. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12608. script_pushnil(st);
  12609. return SCRIPT_CMD_FAILURE;
  12610. }
  12611. return SCRIPT_CMD_SUCCESS;
  12612. }
  12613. /*==========================================
  12614. * Shows wether your inventory(and equips) contain
  12615. selected card or not.
  12616. checkequipedcard(4001);
  12617. *------------------------------------------*/
  12618. BUILDIN_FUNC(checkequipedcard)
  12619. {
  12620. TBL_PC *sd=script_rid2sd(st);
  12621. if(sd){
  12622. int n,i,c=0;
  12623. c=script_getnum(st,2);
  12624. for(i=0;i<MAX_INVENTORY;i++){
  12625. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  12626. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12627. continue;
  12628. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12629. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  12630. script_pushint(st,1);
  12631. return SCRIPT_CMD_SUCCESS;
  12632. }
  12633. }
  12634. }
  12635. }
  12636. }
  12637. script_pushint(st,0);
  12638. return SCRIPT_CMD_SUCCESS;
  12639. }
  12640. BUILDIN_FUNC(jump_zero)
  12641. {
  12642. int sel;
  12643. sel=script_getnum(st,2);
  12644. if(!sel) {
  12645. int pos;
  12646. if( !data_islabel(script_getdata(st,3)) ){
  12647. ShowError("script: jump_zero: not label !\n");
  12648. st->state=END;
  12649. return SCRIPT_CMD_FAILURE;
  12650. }
  12651. pos=script_getnum(st,3);
  12652. st->pos=pos;
  12653. st->state=GOTO;
  12654. }
  12655. return SCRIPT_CMD_SUCCESS;
  12656. }
  12657. /*==========================================
  12658. * movenpc [MouseJstr]
  12659. *------------------------------------------*/
  12660. BUILDIN_FUNC(movenpc)
  12661. {
  12662. TBL_NPC *nd = NULL;
  12663. const char *npc;
  12664. int x,y;
  12665. npc = script_getstr(st,2);
  12666. x = script_getnum(st,3);
  12667. y = script_getnum(st,4);
  12668. if ((nd = npc_name2id(npc)) == NULL){
  12669. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12670. return -1;
  12671. }
  12672. if (script_hasdata(st,5))
  12673. nd->ud.dir = script_getnum(st,5) % 8;
  12674. npc_movenpc(nd, x, y);
  12675. return SCRIPT_CMD_SUCCESS;
  12676. }
  12677. /*==========================================
  12678. * message [MouseJstr]
  12679. *------------------------------------------*/
  12680. BUILDIN_FUNC(message)
  12681. {
  12682. const char *msg,*player;
  12683. TBL_PC *pl_sd = NULL;
  12684. player = script_getstr(st,2);
  12685. msg = script_getstr(st,3);
  12686. if((pl_sd=map_nick2sd((char *) player,false)) == NULL)
  12687. return SCRIPT_CMD_SUCCESS;
  12688. clif_displaymessage(pl_sd->fd, msg);
  12689. return SCRIPT_CMD_SUCCESS;
  12690. }
  12691. /*==========================================
  12692. * npctalk (sends message to surrounding area)
  12693. *------------------------------------------*/
  12694. BUILDIN_FUNC(npctalk)
  12695. {
  12696. struct npc_data* nd = NULL;
  12697. const char* str = script_getstr(st,2);
  12698. if (script_hasdata(st, 3))
  12699. nd = npc_name2id(script_getstr(st, 3));
  12700. else
  12701. nd = (struct npc_data *)map_id2bl(st->oid);
  12702. if (nd != NULL) {
  12703. char message[256];
  12704. safesnprintf(message, sizeof(message), "%s", str);
  12705. clif_disp_overhead(&nd->bl, message);
  12706. }
  12707. return SCRIPT_CMD_SUCCESS;
  12708. }
  12709. // change npc walkspeed [Valaris]
  12710. BUILDIN_FUNC(npcspeed)
  12711. {
  12712. struct npc_data* nd;
  12713. int speed;
  12714. speed = script_getnum(st,2);
  12715. nd =(struct npc_data *)map_id2bl(st->oid);
  12716. if( nd ) {
  12717. nd->speed = speed;
  12718. nd->ud.state.speed_changed = 1;
  12719. }
  12720. return SCRIPT_CMD_SUCCESS;
  12721. }
  12722. // make an npc walk to a position [Valaris]
  12723. BUILDIN_FUNC(npcwalkto)
  12724. {
  12725. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12726. int x=0,y=0;
  12727. x=script_getnum(st,2);
  12728. y=script_getnum(st,3);
  12729. if(nd) {
  12730. if (!nd->status.hp)
  12731. status_calc_npc(nd, SCO_FIRST);
  12732. else
  12733. status_calc_npc(nd, SCO_NONE);
  12734. unit_walktoxy(&nd->bl,x,y,0);
  12735. }
  12736. return SCRIPT_CMD_SUCCESS;
  12737. }
  12738. // stop an npc's movement [Valaris]
  12739. BUILDIN_FUNC(npcstop)
  12740. {
  12741. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12742. if(nd) {
  12743. unit_stop_walking(&nd->bl,1|4);
  12744. }
  12745. return SCRIPT_CMD_SUCCESS;
  12746. }
  12747. /**
  12748. * getlook(<type>{,<char_id>})
  12749. **/
  12750. BUILDIN_FUNC(getlook)
  12751. {
  12752. int type,val;
  12753. TBL_PC *sd;
  12754. if (!script_charid2sd(3,sd)) {
  12755. script_pushint(st,-1);
  12756. return SCRIPT_CMD_FAILURE;
  12757. }
  12758. type=script_getnum(st,2);
  12759. val=-1;
  12760. switch(type) {
  12761. // TODO: implement LOOK_BASE as stated in script doc
  12762. case LOOK_HAIR: val=sd->status.hair; break; //1
  12763. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12764. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12765. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12766. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12767. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12768. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12769. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12770. case LOOK_SHOES: break; //9
  12771. case LOOK_ROBE: val=sd->status.robe; break; //12
  12772. case LOOK_BODY2: val=sd->status.body; break; //13
  12773. }
  12774. script_pushint(st,val);
  12775. return SCRIPT_CMD_SUCCESS;
  12776. }
  12777. /**
  12778. * getsavepoint(<information type>{,<char_id>})
  12779. * @param type 0- map name, 1- x, 2- y
  12780. **/
  12781. BUILDIN_FUNC(getsavepoint)
  12782. {
  12783. TBL_PC* sd;
  12784. int type;
  12785. if (!script_charid2sd(3,sd)) {
  12786. script_pushint(st,0);
  12787. return SCRIPT_CMD_SUCCESS;
  12788. }
  12789. type = script_getnum(st,2);
  12790. switch(type) {
  12791. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12792. case 1: script_pushint(st,sd->status.save_point.x); break;
  12793. case 2: script_pushint(st,sd->status.save_point.y); break;
  12794. default:
  12795. script_pushint(st,0);
  12796. break;
  12797. }
  12798. return SCRIPT_CMD_SUCCESS;
  12799. }
  12800. /**
  12801. * Get position for BL objects.
  12802. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  12803. * @param mapname: String variable for output map name
  12804. * @param x: Integer variable for output coord X
  12805. * @param y: Integer variable for output coord Y
  12806. * @param type: Type of object
  12807. * UNITTYPE_PC - Character coord
  12808. * UNITTYPE_NPC - NPC coord
  12809. * UNITTYPE_PET - Pet coord
  12810. * UNITTYPE_HOM - Homun coord
  12811. * UNITTYPE_MER - Mercenary coord
  12812. * UNITTYPE_ELEM - Elemental coord
  12813. * @param charname: Name object. If empty or "this" use the current object
  12814. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  12815. */
  12816. BUILDIN_FUNC(getmapxy)
  12817. {
  12818. struct block_list *bl = NULL;
  12819. TBL_PC *sd=NULL;
  12820. int64 num;
  12821. const char *name;
  12822. char prefix;
  12823. int x,y,type;
  12824. char mapname[MAP_NAME_LENGTH];
  12825. if( !data_isreference(script_getdata(st,2)) ) {
  12826. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  12827. script_pushint(st,-1);
  12828. return SCRIPT_CMD_FAILURE;
  12829. }
  12830. if( !data_isreference(script_getdata(st,3)) ) {
  12831. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  12832. script_pushint(st,-1);
  12833. return SCRIPT_CMD_FAILURE;
  12834. }
  12835. if( !data_isreference(script_getdata(st,4)) ) {
  12836. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  12837. script_pushint(st,-1);
  12838. return SCRIPT_CMD_FAILURE;
  12839. }
  12840. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  12841. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  12842. script_pushint(st,-1);
  12843. return SCRIPT_CMD_FAILURE;
  12844. }
  12845. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  12846. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  12847. script_pushint(st,-1);
  12848. return SCRIPT_CMD_FAILURE;
  12849. }
  12850. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  12851. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  12852. script_pushint(st,-1);
  12853. return SCRIPT_CMD_FAILURE;
  12854. }
  12855. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12856. type=script_getnum(st,5);
  12857. switch (type) {
  12858. case UNITTYPE_PC: //Get Character Position
  12859. if( script_nick2sd(6,sd) )
  12860. bl = &sd->bl;
  12861. break;
  12862. case UNITTYPE_NPC: //Get NPC Position
  12863. if( script_hasdata(st,6) )
  12864. {
  12865. struct npc_data *nd;
  12866. nd=npc_name2id(script_getstr(st,6));
  12867. if (nd)
  12868. bl = &nd->bl;
  12869. } else //In case the origin is not an npc?
  12870. bl=map_id2bl(st->oid);
  12871. break;
  12872. case UNITTYPE_PET: //Get Pet Position
  12873. if( script_nick2sd(6, sd) && sd->pd )
  12874. bl = &sd->pd->bl;
  12875. break;
  12876. case UNITTYPE_HOM: //Get Homun Position
  12877. if( script_nick2sd(6, sd) && sd->hd )
  12878. bl = &sd->hd->bl;
  12879. break;
  12880. case UNITTYPE_MER: //Get Mercenary Position
  12881. if( script_nick2sd(6, sd) && sd->md )
  12882. bl = &sd->md->bl;
  12883. break;
  12884. case UNITTYPE_ELEM: //Get Elemental Position
  12885. if( script_nick2sd(6, sd) && sd->ed )
  12886. bl = &sd->ed->bl;
  12887. break;
  12888. default:
  12889. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  12890. script_pushint(st,-1);
  12891. return SCRIPT_CMD_FAILURE;
  12892. }
  12893. if (!bl) { //No object found.
  12894. script_pushint(st,-1);
  12895. return SCRIPT_CMD_SUCCESS;
  12896. }
  12897. x= bl->x;
  12898. y= bl->y;
  12899. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12900. //Set MapName$
  12901. num=st->stack->stack_data[st->start+2].u.num;
  12902. name=get_str(script_getvarid(num));
  12903. prefix=*name;
  12904. if(not_server_variable(prefix))
  12905. sd=script_rid2sd(st);
  12906. else
  12907. sd=NULL;
  12908. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12909. //Set MapX
  12910. num=st->stack->stack_data[st->start+3].u.num;
  12911. name=get_str(script_getvarid(num));
  12912. prefix=*name;
  12913. if(not_server_variable(prefix))
  12914. sd=script_rid2sd(st);
  12915. else
  12916. sd=NULL;
  12917. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12918. //Set MapY
  12919. num=st->stack->stack_data[st->start+4].u.num;
  12920. name=get_str(script_getvarid(num));
  12921. prefix=*name;
  12922. if(not_server_variable(prefix))
  12923. sd=script_rid2sd(st);
  12924. else
  12925. sd=NULL;
  12926. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12927. //Return Success value
  12928. script_pushint(st,0);
  12929. return SCRIPT_CMD_SUCCESS;
  12930. }
  12931. /// Returns the map name of given map ID.
  12932. ///
  12933. /// mapid2name <map ID>;
  12934. BUILDIN_FUNC(mapid2name)
  12935. {
  12936. uint16 m = script_getnum(st, 2);
  12937. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  12938. script_pushstr(st, "");
  12939. return SCRIPT_CMD_FAILURE;
  12940. }
  12941. script_pushstrcopy(st, map_mapid2mapname(m));
  12942. return SCRIPT_CMD_SUCCESS;
  12943. }
  12944. /*==========================================
  12945. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12946. *------------------------------------------*/
  12947. BUILDIN_FUNC(logmes)
  12948. {
  12949. const char *str;
  12950. TBL_PC* sd;
  12951. sd = script_rid2sd(st);
  12952. if( sd == NULL )
  12953. return SCRIPT_CMD_FAILURE;
  12954. str = script_getstr(st,2);
  12955. log_npc(sd,str);
  12956. return SCRIPT_CMD_SUCCESS;
  12957. }
  12958. BUILDIN_FUNC(summon)
  12959. {
  12960. int _class, timeout=0;
  12961. const char *str,*event="";
  12962. TBL_PC *sd;
  12963. struct mob_data *md;
  12964. int tick = gettick();
  12965. sd=script_rid2sd(st);
  12966. if (!sd)
  12967. return SCRIPT_CMD_SUCCESS;
  12968. str =script_getstr(st,2);
  12969. _class=script_getnum(st,3);
  12970. if( script_hasdata(st,4) )
  12971. timeout=script_getnum(st,4);
  12972. if( script_hasdata(st,5) ){
  12973. event=script_getstr(st,5);
  12974. check_event(st, event);
  12975. }
  12976. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12977. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12978. if (md) {
  12979. md->master_id=sd->bl.id;
  12980. md->special_state.ai = AI_ATTACK;
  12981. if( md->deletetimer != INVALID_TIMER )
  12982. delete_timer(md->deletetimer, mob_timer_delete);
  12983. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12984. mob_spawn (md); //Now it is ready for spawning.
  12985. clif_specialeffect(&md->bl,344,AREA);
  12986. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12987. }
  12988. script_pushint(st, md->bl.id);
  12989. return SCRIPT_CMD_SUCCESS;
  12990. }
  12991. /*==========================================
  12992. * Checks whether it is daytime/nighttime
  12993. *------------------------------------------*/
  12994. BUILDIN_FUNC(isnight)
  12995. {
  12996. script_pushint(st,(night_flag == 1));
  12997. return SCRIPT_CMD_SUCCESS;
  12998. }
  12999. BUILDIN_FUNC(isday)
  13000. {
  13001. script_pushint(st,(night_flag == 0));
  13002. return SCRIPT_CMD_SUCCESS;
  13003. }
  13004. /*================================================
  13005. * Check how many items/cards in the list are
  13006. * equipped - used for 2/15's cards patch [celest]
  13007. *------------------------------------------------*/
  13008. BUILDIN_FUNC(isequippedcnt)
  13009. {
  13010. TBL_PC *sd;
  13011. int i, id = 1;
  13012. int ret = 0;
  13013. sd = script_rid2sd(st);
  13014. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13015. script_pushint(st,0);
  13016. return SCRIPT_CMD_SUCCESS;
  13017. }
  13018. for (i=0; id!=0; i++) {
  13019. short j;
  13020. FETCH (i+2, id) else id = 0;
  13021. if (id <= 0)
  13022. continue;
  13023. for (j=0; j<EQI_MAX; j++) {
  13024. short index = sd->equip_index[j];
  13025. if(index < 0)
  13026. continue;
  13027. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  13028. continue;
  13029. if(!sd->inventory_data[index])
  13030. continue;
  13031. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  13032. if (sd->inventory_data[index]->nameid == id)
  13033. ret+= sd->inventory.u.items_inventory[index].amount;
  13034. } else { //Count cards.
  13035. short k;
  13036. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13037. continue; //No cards
  13038. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13039. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13040. ret++; //[Lupus]
  13041. }
  13042. }
  13043. }
  13044. }
  13045. script_pushint(st,ret);
  13046. return SCRIPT_CMD_SUCCESS;
  13047. }
  13048. /*================================================
  13049. * Check whether another card has been
  13050. * equipped - used for 2/15's cards patch [celest]
  13051. * -- Items checked cannot be reused in another
  13052. * card set to prevent exploits
  13053. *------------------------------------------------*/
  13054. BUILDIN_FUNC(isequipped)
  13055. {
  13056. TBL_PC *sd;
  13057. int i, id = 1;
  13058. int ret = -1;
  13059. //Original hash to reverse it when full check fails.
  13060. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  13061. sd = script_rid2sd(st);
  13062. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13063. script_pushint(st,0);
  13064. return SCRIPT_CMD_SUCCESS;
  13065. }
  13066. setitem_hash = sd->bonus.setitem_hash;
  13067. setitem_hash2 = sd->bonus.setitem_hash2;
  13068. for (i=0; id!=0; i++) {
  13069. int flag = 0;
  13070. short j;
  13071. FETCH (i+2, id) else id = 0;
  13072. if (id <= 0)
  13073. continue;
  13074. for (j=0; j<EQI_MAX; j++) {
  13075. short index = sd->equip_index[j];
  13076. if(index < 0)
  13077. continue;
  13078. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  13079. continue;
  13080. if(!sd->inventory_data[index])
  13081. continue;
  13082. if (itemdb_type(id) != IT_CARD) {
  13083. if (sd->inventory_data[index]->nameid != id)
  13084. continue;
  13085. flag = 1;
  13086. break;
  13087. } else { //Cards
  13088. short k;
  13089. if (sd->inventory_data[index]->slot == 0 ||
  13090. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13091. continue;
  13092. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  13093. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13094. unsigned int hash = 0;
  13095. if (sd->inventory.u.items_inventory[index].card[k] != id)
  13096. continue;
  13097. hash = 1<<((j<5?j:j-5)*4 + k);
  13098. // check if card is already used by another set
  13099. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13100. continue;
  13101. // We have found a match
  13102. flag = 1;
  13103. // Set hash so this card cannot be used by another
  13104. if (j<5)
  13105. sd->bonus.setitem_hash |= hash;
  13106. else
  13107. sd->bonus.setitem_hash2 |= hash;
  13108. break;
  13109. }
  13110. }
  13111. if (flag) break; //Card found
  13112. }
  13113. if (ret == -1)
  13114. ret = flag;
  13115. else
  13116. ret &= flag;
  13117. if (!ret) break;
  13118. }
  13119. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13120. sd->bonus.setitem_hash = setitem_hash;
  13121. sd->bonus.setitem_hash2 = setitem_hash2;
  13122. }
  13123. script_pushint(st,ret);
  13124. return SCRIPT_CMD_SUCCESS;
  13125. }
  13126. /*================================================
  13127. * Check how many given inserted cards in the CURRENT
  13128. * weapon - used for 2/15's cards patch [Lupus]
  13129. *------------------------------------------------*/
  13130. BUILDIN_FUNC(cardscnt)
  13131. {
  13132. TBL_PC *sd;
  13133. int i, k, id = 1;
  13134. int ret = 0;
  13135. int index;
  13136. sd = script_rid2sd(st);
  13137. for (i=0; id!=0; i++) {
  13138. FETCH (i+2, id) else id = 0;
  13139. if (id <= 0)
  13140. continue;
  13141. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  13142. if(index < 0) continue;
  13143. if(!sd->inventory_data[index])
  13144. continue;
  13145. if(itemdb_type(id) != IT_CARD) {
  13146. if (sd->inventory_data[index]->nameid == id)
  13147. ret+= sd->inventory.u.items_inventory[index].amount;
  13148. } else {
  13149. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13150. continue;
  13151. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13152. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13153. ret++;
  13154. }
  13155. }
  13156. }
  13157. script_pushint(st,ret);
  13158. // script_pushint(st,current_equip_item_index);
  13159. return SCRIPT_CMD_SUCCESS;
  13160. }
  13161. /*=======================================================
  13162. * Returns the refined number of the current item, or an
  13163. * item with inventory index specified
  13164. *-------------------------------------------------------*/
  13165. BUILDIN_FUNC(getrefine)
  13166. {
  13167. TBL_PC *sd;
  13168. if ((sd = script_rid2sd(st))!= NULL)
  13169. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13170. else
  13171. script_pushint(st,0);
  13172. return SCRIPT_CMD_SUCCESS;
  13173. }
  13174. /*=======================================================
  13175. * Day/Night controls
  13176. *-------------------------------------------------------*/
  13177. BUILDIN_FUNC(night)
  13178. {
  13179. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13180. return SCRIPT_CMD_SUCCESS;
  13181. }
  13182. BUILDIN_FUNC(day)
  13183. {
  13184. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13185. return SCRIPT_CMD_SUCCESS;
  13186. }
  13187. /**
  13188. * unequip <equipment slot>{,<char_id>};
  13189. * @author [Spectre]
  13190. **/
  13191. BUILDIN_FUNC(unequip) {
  13192. int pos;
  13193. TBL_PC *sd;
  13194. if (!script_charid2sd(3,sd))
  13195. return SCRIPT_CMD_FAILURE;
  13196. pos = script_getnum(st,2);
  13197. if (equip_index_check(pos)) {
  13198. short i = pc_checkequip(sd,equip_bitmask[pos]);
  13199. if (i >= 0) {
  13200. pc_unequipitem(sd,i,1|2);
  13201. script_pushint(st, 1);
  13202. return SCRIPT_CMD_SUCCESS;
  13203. }
  13204. }
  13205. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13206. script_pushint(st, 0);
  13207. return SCRIPT_CMD_FAILURE;
  13208. }
  13209. /**
  13210. * equip <item id>{,<char_id>};
  13211. **/
  13212. BUILDIN_FUNC(equip) {
  13213. unsigned short nameid = 0;
  13214. TBL_PC *sd;
  13215. struct item_data *item_data;
  13216. if (!script_charid2sd(3,sd))
  13217. return SCRIPT_CMD_FAILURE;
  13218. nameid = script_getnum(st,2);
  13219. if ((item_data = itemdb_exists(nameid))) {
  13220. int i;
  13221. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  13222. if (i < MAX_INVENTORY) {
  13223. pc_equipitem(sd,i,item_data->equip);
  13224. script_pushint(st,1);
  13225. return SCRIPT_CMD_SUCCESS;
  13226. }
  13227. }
  13228. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13229. script_pushint(st,0);
  13230. return SCRIPT_CMD_FAILURE;
  13231. }
  13232. BUILDIN_FUNC(autoequip)
  13233. {
  13234. unsigned short nameid;
  13235. int flag;
  13236. struct item_data *item_data;
  13237. nameid=script_getnum(st,2);
  13238. flag=script_getnum(st,3);
  13239. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13240. {
  13241. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13242. return SCRIPT_CMD_FAILURE;
  13243. }
  13244. if( !itemdb_isequip2(item_data) )
  13245. {
  13246. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13247. return SCRIPT_CMD_FAILURE;
  13248. }
  13249. item_data->flag.autoequip = flag>0?1:0;
  13250. return SCRIPT_CMD_SUCCESS;
  13251. }
  13252. /**
  13253. * Set the value of a given battle config
  13254. * setbattleflag "<battle flag>",<value>{,<reload>};
  13255. */
  13256. BUILDIN_FUNC(setbattleflag)
  13257. {
  13258. const char *flag, *value;
  13259. flag = script_getstr(st,2);
  13260. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13261. if (battle_set_value(flag, value) == 0)
  13262. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13263. else {
  13264. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13265. if (script_hasdata(st, 4) && script_getnum(st, 4)) { // Only attempt to reload monster data if told to
  13266. const char *expdrop_flags[] = { // Only certain flags require a reload, check for those types
  13267. "base_exp_rate", "job_exp_rate", "mvp_exp_rate", "quest_exp_rate", "heal_exp", "resurrection_exp",
  13268. "item_rate_common", "item_rate_common_boss", "item_rate_common_mvp", "item_drop_common_min", "item_drop_common_max",
  13269. "item_rate_heal", "item_rate_heal_boss", "item_rate_heal_mvp", "item_rate_heal_min", "item_rate_heal_max",
  13270. "item_rate_use", "item_rate_use_boss", "item_rate_use_mvp", "item_rate_use_min", "item_rate_use_max",
  13271. "item_rate_equip", "item_rate_equip_boss", "item_rate_equip_mvp", "item_rate_equip_min", "item_rate_equip_max",
  13272. "item_rate_card", "item_rate_card_boss", "item_rate_card_mvp", "item_rate_card_min", "item_rate_card_max",
  13273. "item_rate_mvp", "item_rate_mvp_min", "item_rate_mvp_max", "item_rate_mvp_mode",
  13274. "item_rate_treasure", "item_rate_treasure_min", "item_rate_treasure_max",
  13275. "item_logarithmic_drops", "drop_rate0item", "drop_rateincrease",
  13276. };
  13277. uint8 i;
  13278. for (i = 0; i < ARRAYLENGTH(expdrop_flags); i++) {
  13279. if (!strcmpi(flag, expdrop_flags[i])) {
  13280. mob_reload();
  13281. break;
  13282. }
  13283. }
  13284. }
  13285. }
  13286. return SCRIPT_CMD_SUCCESS;
  13287. }
  13288. /**
  13289. * Get the value of a given battle config
  13290. * getbattleflag("<battle flag>")
  13291. */
  13292. BUILDIN_FUNC(getbattleflag)
  13293. {
  13294. const char *flag = script_getstr(st,2);
  13295. script_pushint(st,battle_get_value(flag));
  13296. return SCRIPT_CMD_SUCCESS;
  13297. }
  13298. //=======================================================
  13299. // strlen [Valaris]
  13300. //-------------------------------------------------------
  13301. BUILDIN_FUNC(getstrlen)
  13302. {
  13303. const char *str = script_getstr(st,2);
  13304. int len = (str) ? (int)strlen(str) : 0;
  13305. script_pushint(st,len);
  13306. return SCRIPT_CMD_SUCCESS;
  13307. }
  13308. //=======================================================
  13309. // isalpha [Valaris]
  13310. //-------------------------------------------------------
  13311. BUILDIN_FUNC(charisalpha)
  13312. {
  13313. const char *str=script_getstr(st,2);
  13314. int pos=script_getnum(st,3);
  13315. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13316. script_pushint(st,val);
  13317. return SCRIPT_CMD_SUCCESS;
  13318. }
  13319. //=======================================================
  13320. // charisupper <str>, <index>
  13321. //-------------------------------------------------------
  13322. BUILDIN_FUNC(charisupper)
  13323. {
  13324. const char *str = script_getstr(st,2);
  13325. int pos = script_getnum(st,3);
  13326. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13327. script_pushint(st,val);
  13328. return SCRIPT_CMD_SUCCESS;
  13329. }
  13330. //=======================================================
  13331. // charislower <str>, <index>
  13332. //-------------------------------------------------------
  13333. BUILDIN_FUNC(charislower)
  13334. {
  13335. const char *str = script_getstr(st,2);
  13336. int pos = script_getnum(st,3);
  13337. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13338. script_pushint(st,val);
  13339. return SCRIPT_CMD_SUCCESS;
  13340. }
  13341. //=======================================================
  13342. // charat <str>, <index>
  13343. //-------------------------------------------------------
  13344. BUILDIN_FUNC(charat) {
  13345. const char *str = script_getstr(st,2);
  13346. int pos = script_getnum(st,3);
  13347. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13348. char output[2];
  13349. output[0] = str[pos];
  13350. output[1] = '\0';
  13351. script_pushstrcopy(st, output);
  13352. } else
  13353. script_pushconststr(st, "");
  13354. return SCRIPT_CMD_SUCCESS;
  13355. }
  13356. //=======================================================
  13357. // setchar <string>, <char>, <index>
  13358. //-------------------------------------------------------
  13359. BUILDIN_FUNC(setchar)
  13360. {
  13361. const char *str = script_getstr(st,2);
  13362. const char *c = script_getstr(st,3);
  13363. int index = script_getnum(st,4);
  13364. char *output = aStrdup(str);
  13365. if(index >= 0 && index < strlen(output))
  13366. output[index] = *c;
  13367. script_pushstr(st, output);
  13368. return SCRIPT_CMD_SUCCESS;
  13369. }
  13370. //=======================================================
  13371. // insertchar <string>, <char>, <index>
  13372. //-------------------------------------------------------
  13373. BUILDIN_FUNC(insertchar)
  13374. {
  13375. const char *str = script_getstr(st,2);
  13376. const char *c = script_getstr(st,3);
  13377. int index = script_getnum(st,4);
  13378. char *output;
  13379. size_t len = strlen(str);
  13380. if(index < 0)
  13381. index = 0;
  13382. else if(index > len)
  13383. index = len;
  13384. output = (char*)aMalloc(len + 2);
  13385. memcpy(output, str, index);
  13386. output[index] = c[0];
  13387. memcpy(&output[index+1], &str[index], len - index);
  13388. output[len+1] = '\0';
  13389. script_pushstr(st, output);
  13390. return SCRIPT_CMD_SUCCESS;
  13391. }
  13392. //=======================================================
  13393. // delchar <string>, <index>
  13394. //-------------------------------------------------------
  13395. BUILDIN_FUNC(delchar)
  13396. {
  13397. const char *str = script_getstr(st,2);
  13398. int index = script_getnum(st,3);
  13399. char *output;
  13400. size_t len = strlen(str);
  13401. if(index < 0 || index > len) {
  13402. //return original
  13403. output = aStrdup(str);
  13404. script_pushstr(st, output);
  13405. return SCRIPT_CMD_SUCCESS;
  13406. }
  13407. output = (char*)aMalloc(len);
  13408. memcpy(output, str, index);
  13409. memcpy(&output[index], &str[index+1], len - index);
  13410. script_pushstr(st, output);
  13411. return SCRIPT_CMD_SUCCESS;
  13412. }
  13413. //=======================================================
  13414. // strtoupper <str>
  13415. //-------------------------------------------------------
  13416. BUILDIN_FUNC(strtoupper)
  13417. {
  13418. const char *str = script_getstr(st,2);
  13419. char *output = aStrdup(str);
  13420. char *cursor = output;
  13421. while (*cursor != '\0') {
  13422. *cursor = TOUPPER(*cursor);
  13423. cursor++;
  13424. }
  13425. script_pushstr(st, output);
  13426. return SCRIPT_CMD_SUCCESS;
  13427. }
  13428. //=======================================================
  13429. // strtolower <str>
  13430. //-------------------------------------------------------
  13431. BUILDIN_FUNC(strtolower)
  13432. {
  13433. const char *str = script_getstr(st,2);
  13434. char *output = aStrdup(str);
  13435. char *cursor = output;
  13436. while (*cursor != '\0') {
  13437. *cursor = TOLOWER(*cursor);
  13438. cursor++;
  13439. }
  13440. script_pushstr(st, output);
  13441. return SCRIPT_CMD_SUCCESS;
  13442. }
  13443. //=======================================================
  13444. // substr <str>, <start>, <end>
  13445. //-------------------------------------------------------
  13446. BUILDIN_FUNC(substr)
  13447. {
  13448. const char *str = script_getstr(st,2);
  13449. char *output;
  13450. int start = script_getnum(st,3);
  13451. int end = script_getnum(st,4);
  13452. int len = 0;
  13453. if(start >= 0 && end < strlen(str) && start <= end) {
  13454. len = end - start + 1;
  13455. output = (char*)aMalloc(len + 1);
  13456. memcpy(output, &str[start], len);
  13457. } else
  13458. output = (char*)aMalloc(1);
  13459. output[len] = '\0';
  13460. script_pushstr(st, output);
  13461. return SCRIPT_CMD_SUCCESS;
  13462. }
  13463. //=======================================================
  13464. // explode <dest_string_array>, <str>, <delimiter>
  13465. // Note: delimiter is limited to 1 char
  13466. //-------------------------------------------------------
  13467. BUILDIN_FUNC(explode)
  13468. {
  13469. struct script_data* data = script_getdata(st, 2);
  13470. const char *str = script_getstr(st,3);
  13471. const char delimiter = script_getstr(st, 4)[0];
  13472. int32 id;
  13473. size_t len = strlen(str);
  13474. int i = 0, j = 0;
  13475. int start;
  13476. char *temp;
  13477. const char* name;
  13478. TBL_PC* sd = NULL;
  13479. temp = (char*)aMalloc(len + 1);
  13480. if( !data_isreference(data) ) {
  13481. ShowError("script:explode: not a variable\n");
  13482. script_reportdata(data);
  13483. st->state = END;
  13484. return SCRIPT_CMD_FAILURE;// not a variable
  13485. }
  13486. id = reference_getid(data);
  13487. start = reference_getindex(data);
  13488. name = reference_getname(data);
  13489. if( !is_string_variable(name) ) {
  13490. ShowError("script:explode: not string array\n");
  13491. script_reportdata(data);
  13492. st->state = END;
  13493. return SCRIPT_CMD_FAILURE;// data type mismatch
  13494. }
  13495. if( not_server_variable(*name) ) {
  13496. sd = script_rid2sd(st);
  13497. if( sd == NULL )
  13498. return SCRIPT_CMD_SUCCESS;// no player attached
  13499. }
  13500. while(str[i] != '\0') {
  13501. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13502. temp[j] = '\0';
  13503. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13504. j = 0;
  13505. ++i;
  13506. } else {
  13507. temp[j++] = str[i++];
  13508. }
  13509. }
  13510. //set last string
  13511. temp[j] = '\0';
  13512. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13513. aFree(temp);
  13514. return SCRIPT_CMD_SUCCESS;
  13515. }
  13516. //=======================================================
  13517. // implode <string_array>
  13518. // implode <string_array>, <glue>
  13519. //-------------------------------------------------------
  13520. BUILDIN_FUNC(implode)
  13521. {
  13522. struct script_data* data = script_getdata(st, 2);
  13523. const char *name;
  13524. uint32 glue_len = 0, array_size, id;
  13525. char *output;
  13526. TBL_PC* sd = NULL;
  13527. if( !data_isreference(data) ) {
  13528. ShowError("script:implode: not a variable\n");
  13529. script_reportdata(data);
  13530. st->state = END;
  13531. return SCRIPT_CMD_FAILURE;// not a variable
  13532. }
  13533. id = reference_getid(data);
  13534. name = reference_getname(data);
  13535. if( !is_string_variable(name) ) {
  13536. ShowError("script:implode: not string array\n");
  13537. script_reportdata(data);
  13538. st->state = END;
  13539. return SCRIPT_CMD_FAILURE;// data type mismatch
  13540. }
  13541. if( not_server_variable(*name) ) {
  13542. sd = script_rid2sd(st);
  13543. if( sd == NULL )
  13544. return SCRIPT_CMD_SUCCESS;// no player attached
  13545. }
  13546. //count chars
  13547. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  13548. if(array_size == -1) { //empty array check (AmsTaff)
  13549. ShowWarning("script:implode: array length = 0\n");
  13550. output = (char*)aMalloc(sizeof(char)*5);
  13551. sprintf(output,"%s","NULL");
  13552. } else {
  13553. const char *glue = NULL, *temp;
  13554. size_t len = 0;
  13555. int i, k = 0;
  13556. for(i = 0; i <= array_size; ++i) {
  13557. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13558. len += strlen(temp);
  13559. script_removetop(st, -1, 0);
  13560. }
  13561. //allocate mem
  13562. if( script_hasdata(st,3) ) {
  13563. glue = script_getstr(st,3);
  13564. glue_len = strlen(glue);
  13565. len += glue_len * (array_size);
  13566. }
  13567. output = (char*)aMalloc(len + 1);
  13568. //build output
  13569. for(i = 0; i < array_size; ++i) {
  13570. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13571. len = strlen(temp);
  13572. memcpy(&output[k], temp, len);
  13573. k += len;
  13574. if(glue_len != 0) {
  13575. memcpy(&output[k], glue, glue_len);
  13576. k += glue_len;
  13577. }
  13578. script_removetop(st, -1, 0);
  13579. }
  13580. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13581. len = strlen(temp);
  13582. memcpy(&output[k], temp, len);
  13583. k += len;
  13584. script_removetop(st, -1, 0);
  13585. output[k] = '\0';
  13586. }
  13587. script_pushstr(st, output);
  13588. return SCRIPT_CMD_SUCCESS;
  13589. }
  13590. //=======================================================
  13591. // sprintf(<format>, ...);
  13592. // Implements C sprintf, except format %n. The resulting string is
  13593. // returned, instead of being saved in variable by reference.
  13594. //-------------------------------------------------------
  13595. BUILDIN_FUNC(sprintf)
  13596. {
  13597. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13598. const char* format;
  13599. char* p;
  13600. char* q;
  13601. char* buf = NULL;
  13602. char* buf2 = NULL;
  13603. struct script_data* data;
  13604. StringBuf final_buf;
  13605. // Fetch init data
  13606. format = script_getstr(st, 2);
  13607. argc = script_lastdata(st)-2;
  13608. len = strlen(format);
  13609. // Skip parsing, where no parsing is required.
  13610. if(len == 0) {
  13611. script_pushconststr(st,"");
  13612. return SCRIPT_CMD_SUCCESS;
  13613. }
  13614. // Pessimistic alloc
  13615. CREATE(buf, char, len+1);
  13616. // Need not be parsed, just solve stuff like %%.
  13617. if(argc == 0) {
  13618. memcpy(buf,format,len+1);
  13619. script_pushstrcopy(st, buf);
  13620. aFree(buf);
  13621. return SCRIPT_CMD_SUCCESS;
  13622. }
  13623. safestrncpy(buf, format, len+1);
  13624. // Issue sprintf for each parameter
  13625. StringBuf_Init(&final_buf);
  13626. q = buf;
  13627. while((p = strchr(q, '%')) != NULL) {
  13628. if(p != q) {
  13629. len = p - q + 1;
  13630. if(buf2_len < len) {
  13631. RECREATE(buf2, char, len);
  13632. buf2_len = len;
  13633. }
  13634. safestrncpy(buf2, q, len);
  13635. StringBuf_AppendStr(&final_buf, buf2);
  13636. q = p;
  13637. }
  13638. p = q + 1;
  13639. if(*p == '%') { // %%
  13640. StringBuf_AppendStr(&final_buf, "%");
  13641. q+=2;
  13642. continue;
  13643. }
  13644. if(*p == 'n') { // %n
  13645. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13646. script_reportsrc(st);
  13647. q+=2;
  13648. continue;
  13649. }
  13650. if(arg >= argc) {
  13651. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13652. if(buf) aFree(buf);
  13653. if(buf2) aFree(buf2);
  13654. StringBuf_Destroy(&final_buf);
  13655. script_pushconststr(st,"");
  13656. return SCRIPT_CMD_FAILURE;
  13657. }
  13658. if((p = strchr(q+1, '%')) == NULL)
  13659. p = strchr(q, 0); // EOS
  13660. len = p - q + 1;
  13661. if(buf2_len < len) {
  13662. RECREATE(buf2, char, len);
  13663. buf2_len = len;
  13664. }
  13665. safestrncpy(buf2, q, len);
  13666. q = p;
  13667. // Note: This assumes the passed value being the correct
  13668. // type to the current format specifier. If not, the server
  13669. // probably crashes or returns anything else, than expected,
  13670. // but it would behave in normal code the same way so it's
  13671. // the scripter's responsibility.
  13672. data = script_getdata(st, arg+3);
  13673. if(data_isstring(data)) // String
  13674. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13675. else if(data_isint(data)) // Number
  13676. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13677. else if(data_isreference(data)) { // Variable
  13678. char* name = reference_getname(data);
  13679. if(name[strlen(name)-1]=='$') // var Str
  13680. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13681. else // var Int
  13682. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13683. } else { // Unsupported type
  13684. ShowError("buildin_sprintf: Unknown argument type!\n");
  13685. if(buf) aFree(buf);
  13686. if(buf2) aFree(buf2);
  13687. StringBuf_Destroy(&final_buf);
  13688. script_pushconststr(st,"");
  13689. return SCRIPT_CMD_FAILURE;
  13690. }
  13691. arg++;
  13692. }
  13693. // Append anything left
  13694. if(*q)
  13695. StringBuf_AppendStr(&final_buf, q);
  13696. // Passed more, than needed
  13697. if(arg < argc) {
  13698. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13699. script_reportsrc(st);
  13700. }
  13701. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13702. if(buf) aFree(buf);
  13703. if(buf2) aFree(buf2);
  13704. StringBuf_Destroy(&final_buf);
  13705. return SCRIPT_CMD_SUCCESS;
  13706. }
  13707. //=======================================================
  13708. // sscanf(<str>, <format>, ...);
  13709. // Implements C sscanf.
  13710. //-------------------------------------------------------
  13711. BUILDIN_FUNC(sscanf){
  13712. unsigned int argc, arg = 0, len;
  13713. struct script_data* data;
  13714. struct map_session_data* sd = NULL;
  13715. const char* str;
  13716. const char* format;
  13717. const char* p;
  13718. const char* q;
  13719. char* buf = NULL;
  13720. char* buf_p;
  13721. char* ref_str = NULL;
  13722. int ref_int;
  13723. // Get data
  13724. str = script_getstr(st, 2);
  13725. format = script_getstr(st, 3);
  13726. argc = script_lastdata(st)-3;
  13727. len = strlen(format);
  13728. if (len != 0 && strlen(str) == 0) {
  13729. // If the source string is empty but the format string is not, we return -1
  13730. // according to the C specs. (if the format string is also empty, we shall
  13731. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13732. script_pushint(st, -1);
  13733. return SCRIPT_CMD_SUCCESS;
  13734. }
  13735. CREATE(buf, char, len*2+1);
  13736. // Issue sscanf for each parameter
  13737. *buf = 0;
  13738. q = format;
  13739. while((p = strchr(q, '%'))){
  13740. if(p!=q){
  13741. strncat(buf, q, (size_t)(p-q));
  13742. q = p;
  13743. }
  13744. p = q+1;
  13745. if(*p=='*' || *p=='%'){ // Skip
  13746. strncat(buf, q, 2);
  13747. q+=2;
  13748. continue;
  13749. }
  13750. if(arg>=argc){
  13751. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13752. script_pushint(st, -1);
  13753. if(buf) aFree(buf);
  13754. if(ref_str) aFree(ref_str);
  13755. return SCRIPT_CMD_FAILURE;
  13756. }
  13757. if((p = strchr(q+1, '%'))==NULL){
  13758. p = strchr(q, 0); // EOS
  13759. }
  13760. len = p-q;
  13761. strncat(buf, q, len);
  13762. q = p;
  13763. // Validate output
  13764. data = script_getdata(st, arg+4);
  13765. if(!data_isreference(data) || !reference_tovariable(data)){
  13766. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13767. script_pushint(st, -1);
  13768. if(buf) aFree(buf);
  13769. if(ref_str) aFree(ref_str);
  13770. return SCRIPT_CMD_FAILURE;
  13771. }
  13772. buf_p = reference_getname(data);
  13773. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13774. script_pushint(st, -1);
  13775. if(buf) aFree(buf);
  13776. if(ref_str) aFree(ref_str);
  13777. return SCRIPT_CMD_SUCCESS;
  13778. }
  13779. // Save value if any
  13780. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13781. if(ref_str==NULL){
  13782. CREATE(ref_str, char, strlen(str)+1);
  13783. }
  13784. if(sscanf(str, buf, ref_str)==0){
  13785. break;
  13786. }
  13787. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13788. } else { // Number
  13789. if(sscanf(str, buf, &ref_int)==0){
  13790. break;
  13791. }
  13792. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13793. }
  13794. arg++;
  13795. // Disable used format (%... -> %*...)
  13796. buf_p = strchr(buf, 0);
  13797. memmove(buf_p-len+2, buf_p-len+1, len);
  13798. *(buf_p-len+1) = '*';
  13799. }
  13800. script_pushint(st, arg);
  13801. if(buf) aFree(buf);
  13802. if(ref_str) aFree(ref_str);
  13803. return SCRIPT_CMD_SUCCESS;
  13804. }
  13805. //=======================================================
  13806. // strpos(<haystack>, <needle>)
  13807. // strpos(<haystack>, <needle>, <offset>)
  13808. //
  13809. // Implements PHP style strpos. Adapted from code from
  13810. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13811. //-------------------------------------------------------
  13812. BUILDIN_FUNC(strpos) {
  13813. const char *haystack = script_getstr(st,2);
  13814. const char *needle = script_getstr(st,3);
  13815. int i;
  13816. size_t len;
  13817. if( script_hasdata(st,4) )
  13818. i = script_getnum(st,4);
  13819. else
  13820. i = 0;
  13821. if (needle[0] == '\0') {
  13822. script_pushint(st, -1);
  13823. return SCRIPT_CMD_SUCCESS;
  13824. }
  13825. len = strlen(haystack);
  13826. for ( ; i < len; ++i ) {
  13827. if ( haystack[i] == *needle ) {
  13828. // matched starting char -- loop through remaining chars
  13829. const char *h, *n;
  13830. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13831. if ( *h != *n ) {
  13832. break;
  13833. }
  13834. }
  13835. if ( !*n ) { // matched all of 'needle' to null termination
  13836. script_pushint(st, i);
  13837. return SCRIPT_CMD_SUCCESS;
  13838. }
  13839. }
  13840. }
  13841. script_pushint(st, -1);
  13842. return SCRIPT_CMD_SUCCESS;
  13843. }
  13844. //===============================================================
  13845. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13846. //
  13847. // Note: Finds all instances of <search> in <input> and replaces
  13848. // with <replace>. If specified will only replace as many
  13849. // instances as specified in <count>. By default will be case
  13850. // sensitive.
  13851. //---------------------------------------------------------------
  13852. BUILDIN_FUNC(replacestr)
  13853. {
  13854. const char *input = script_getstr(st, 2);
  13855. const char *find = script_getstr(st, 3);
  13856. const char *replace = script_getstr(st, 4);
  13857. size_t inputlen = strlen(input);
  13858. size_t findlen = strlen(find);
  13859. struct StringBuf output;
  13860. bool usecase = true;
  13861. int count = 0;
  13862. int numFinds = 0;
  13863. int i = 0, f = 0;
  13864. if(findlen == 0) {
  13865. ShowError("script:replacestr: Invalid search length.\n");
  13866. st->state = END;
  13867. return SCRIPT_CMD_FAILURE;
  13868. }
  13869. if(script_hasdata(st, 5)) {
  13870. struct script_data *data = script_getdata(st, 5);
  13871. get_val(st, data); // Convert into value in case of a variable
  13872. if( !data_isstring(data) )
  13873. usecase = script_getnum(st, 5) != 0;
  13874. else {
  13875. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13876. st->state = END;
  13877. return SCRIPT_CMD_FAILURE;
  13878. }
  13879. }
  13880. if(script_hasdata(st, 6)) {
  13881. count = script_getnum(st, 6);
  13882. if(count == 0) {
  13883. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13884. st->state = END;
  13885. return SCRIPT_CMD_FAILURE;
  13886. }
  13887. }
  13888. StringBuf_Init(&output);
  13889. for(; i < inputlen; i++) {
  13890. if(count && count == numFinds) { //found enough, stop looking
  13891. break;
  13892. }
  13893. for(f = 0; f <= findlen; f++) {
  13894. if(f == findlen) { //complete match
  13895. numFinds++;
  13896. StringBuf_AppendStr(&output, replace);
  13897. i += findlen - 1;
  13898. break;
  13899. } else {
  13900. if(usecase) {
  13901. if((i + f) > inputlen || input[i + f] != find[f]) {
  13902. StringBuf_Printf(&output, "%c", input[i]);
  13903. break;
  13904. }
  13905. } else {
  13906. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13907. StringBuf_Printf(&output, "%c", input[i]);
  13908. break;
  13909. }
  13910. }
  13911. }
  13912. }
  13913. }
  13914. //append excess after enough found
  13915. if(i < inputlen)
  13916. StringBuf_AppendStr(&output, &(input[i]));
  13917. script_pushstrcopy(st, StringBuf_Value(&output));
  13918. StringBuf_Destroy(&output);
  13919. return SCRIPT_CMD_SUCCESS;
  13920. }
  13921. //========================================================
  13922. // countstr <input>, <search>{, <usecase>}
  13923. //
  13924. // Note: Counts the number of times <search> occurs in
  13925. // <input>. By default will be case sensitive.
  13926. //--------------------------------------------------------
  13927. BUILDIN_FUNC(countstr)
  13928. {
  13929. const char *input = script_getstr(st, 2);
  13930. const char *find = script_getstr(st, 3);
  13931. size_t inputlen = strlen(input);
  13932. size_t findlen = strlen(find);
  13933. bool usecase = true;
  13934. int numFinds = 0;
  13935. int i = 0, f = 0;
  13936. if(findlen == 0) {
  13937. ShowError("script:countstr: Invalid search length.\n");
  13938. st->state = END;
  13939. return SCRIPT_CMD_FAILURE;
  13940. }
  13941. if(script_hasdata(st, 4)) {
  13942. struct script_data *data;
  13943. data = script_getdata(st, 4);
  13944. get_val(st, data); // Convert into value in case of a variable
  13945. if( !data_isstring(data) )
  13946. usecase = script_getnum(st, 4) != 0;
  13947. else {
  13948. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13949. st->state = END;
  13950. return SCRIPT_CMD_FAILURE;
  13951. }
  13952. }
  13953. for(; i < inputlen; i++) {
  13954. for(f = 0; f <= findlen; f++) {
  13955. if(f == findlen) { //complete match
  13956. numFinds++;
  13957. i += findlen - 1;
  13958. break;
  13959. } else {
  13960. if(usecase) {
  13961. if((i + f) > inputlen || input[i + f] != find[f]) {
  13962. break;
  13963. }
  13964. } else {
  13965. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13966. break;
  13967. }
  13968. }
  13969. }
  13970. }
  13971. }
  13972. script_pushint(st, numFinds);
  13973. return SCRIPT_CMD_SUCCESS;
  13974. }
  13975. /// Changes the display name and/or display class of the npc.
  13976. /// Returns 0 is successful, 1 if the npc does not exist.
  13977. ///
  13978. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13979. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13980. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13981. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13982. BUILDIN_FUNC(setnpcdisplay)
  13983. {
  13984. const char* name;
  13985. const char* newname = NULL;
  13986. int class_ = -1, size = -1;
  13987. struct script_data* data;
  13988. struct npc_data* nd;
  13989. name = script_getstr(st,2);
  13990. data = script_getdata(st,3);
  13991. if( script_hasdata(st,4) )
  13992. class_ = script_getnum(st,4);
  13993. if( script_hasdata(st,5) )
  13994. size = script_getnum(st,5);
  13995. get_val(st, data);
  13996. if( data_isstring(data) )
  13997. newname = conv_str(st,data);
  13998. else if( data_isint(data) )
  13999. class_ = conv_num(st,data);
  14000. else
  14001. {
  14002. ShowError("script:setnpcdisplay: expected string or number\n");
  14003. script_reportdata(data);
  14004. return SCRIPT_CMD_FAILURE;
  14005. }
  14006. nd = npc_name2id(name);
  14007. if( nd == NULL )
  14008. {// not found
  14009. script_pushint(st,1);
  14010. return SCRIPT_CMD_SUCCESS;
  14011. }
  14012. // update npc
  14013. if( newname )
  14014. npc_setdisplayname(nd, newname);
  14015. if( size != -1 && size != (int)nd->size )
  14016. nd->size = size;
  14017. else
  14018. size = -1;
  14019. if( class_ != -1 && nd->class_ != class_ )
  14020. npc_setclass(nd, class_);
  14021. else if( size != -1 )
  14022. { // Required to update the visual size
  14023. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  14024. clif_spawn(&nd->bl);
  14025. }
  14026. script_pushint(st,0);
  14027. return SCRIPT_CMD_SUCCESS;
  14028. }
  14029. BUILDIN_FUNC(atoi)
  14030. {
  14031. const char *value;
  14032. value = script_getstr(st,2);
  14033. script_pushint(st,atoi(value));
  14034. return SCRIPT_CMD_SUCCESS;
  14035. }
  14036. BUILDIN_FUNC(axtoi)
  14037. {
  14038. const char *hex = script_getstr(st,2);
  14039. long value = strtol(hex, NULL, 16);
  14040. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14041. value = cap_value(value, INT_MIN, INT_MAX);
  14042. #endif
  14043. script_pushint(st, (int)value);
  14044. return SCRIPT_CMD_SUCCESS;
  14045. }
  14046. BUILDIN_FUNC(strtol)
  14047. {
  14048. const char *string = script_getstr(st, 2);
  14049. int base = script_getnum(st, 3);
  14050. long value = strtol(string, NULL, base);
  14051. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14052. value = cap_value(value, INT_MIN, INT_MAX);
  14053. #endif
  14054. script_pushint(st, (int)value);
  14055. return SCRIPT_CMD_SUCCESS;
  14056. }
  14057. // case-insensitive substring search [lordalfa]
  14058. BUILDIN_FUNC(compare)
  14059. {
  14060. const char *message;
  14061. const char *cmpstring;
  14062. message = script_getstr(st,2);
  14063. cmpstring = script_getstr(st,3);
  14064. script_pushint(st,(stristr(message,cmpstring) != NULL));
  14065. return SCRIPT_CMD_SUCCESS;
  14066. }
  14067. BUILDIN_FUNC(strcmp)
  14068. {
  14069. const char *str1;
  14070. const char *str2;
  14071. str1 = script_getstr(st,2);
  14072. str2 = script_getstr(st,3);
  14073. script_pushint(st,strcmp(str1, str2));
  14074. return SCRIPT_CMD_SUCCESS;
  14075. }
  14076. // [zBuffer] List of mathematics commands --->
  14077. BUILDIN_FUNC(sqrt)
  14078. {
  14079. double i, a;
  14080. i = script_getnum(st,2);
  14081. a = sqrt(i);
  14082. script_pushint(st,(int)a);
  14083. return SCRIPT_CMD_SUCCESS;
  14084. }
  14085. BUILDIN_FUNC(pow)
  14086. {
  14087. double i, a, b;
  14088. a = script_getnum(st,2);
  14089. b = script_getnum(st,3);
  14090. i = pow(a,b);
  14091. script_pushint(st,(int)i);
  14092. return SCRIPT_CMD_SUCCESS;
  14093. }
  14094. BUILDIN_FUNC(distance)
  14095. {
  14096. int x0, y0, x1, y1;
  14097. x0 = script_getnum(st,2);
  14098. y0 = script_getnum(st,3);
  14099. x1 = script_getnum(st,4);
  14100. y1 = script_getnum(st,5);
  14101. script_pushint(st,distance_xy(x0,y0,x1,y1));
  14102. return SCRIPT_CMD_SUCCESS;
  14103. }
  14104. // <--- [zBuffer] List of mathematics commands
  14105. BUILDIN_FUNC(md5)
  14106. {
  14107. const char *tmpstr;
  14108. char *md5str;
  14109. tmpstr = script_getstr(st,2);
  14110. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14111. MD5_String(tmpstr, md5str);
  14112. script_pushstr(st, md5str);
  14113. return SCRIPT_CMD_SUCCESS;
  14114. }
  14115. // [zBuffer] List of dynamic var commands --->
  14116. /**
  14117. * setd "<variable name>",<value>{,<char_id>};
  14118. **/
  14119. BUILDIN_FUNC(setd)
  14120. {
  14121. TBL_PC *sd = NULL;
  14122. char varname[100];
  14123. const char *buffer;
  14124. int elem;
  14125. buffer = script_getstr(st, 2);
  14126. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14127. elem = 0;
  14128. if( not_server_variable(*varname) ) {
  14129. if (!script_charid2sd(4,sd))
  14130. return SCRIPT_CMD_FAILURE;
  14131. }
  14132. if( is_string_variable(varname) ) {
  14133. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  14134. } else {
  14135. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  14136. }
  14137. return SCRIPT_CMD_SUCCESS;
  14138. }
  14139. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14140. {
  14141. int i, j;
  14142. TBL_PC* sd = NULL;
  14143. const char* query;
  14144. struct script_data* data;
  14145. const char* name;
  14146. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14147. int num_vars;
  14148. int num_cols;
  14149. // check target variables
  14150. for( i = 3; script_hasdata(st,i); ++i ) {
  14151. data = script_getdata(st, i);
  14152. if( data_isreference(data) ) { // it's a variable
  14153. name = reference_getname(data);
  14154. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14155. sd = script_rid2sd(st);
  14156. if( sd == NULL ) { // no player attached
  14157. script_reportdata(data);
  14158. st->state = END;
  14159. return SCRIPT_CMD_FAILURE;
  14160. }
  14161. }
  14162. } else {
  14163. ShowError("script:query_sql: not a variable\n");
  14164. script_reportdata(data);
  14165. st->state = END;
  14166. return SCRIPT_CMD_FAILURE;
  14167. }
  14168. }
  14169. num_vars = i - 3;
  14170. // Execute the query
  14171. query = script_getstr(st,2);
  14172. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14173. Sql_ShowDebug(handle);
  14174. script_pushint(st, -1);
  14175. return SCRIPT_CMD_FAILURE;
  14176. }
  14177. if( Sql_NumRows(handle) == 0 ) { // No data received
  14178. Sql_FreeResult(handle);
  14179. script_pushint(st, 0);
  14180. return SCRIPT_CMD_SUCCESS;
  14181. }
  14182. // Count the number of columns to store
  14183. num_cols = Sql_NumColumns(handle);
  14184. if( num_vars < num_cols ) {
  14185. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14186. script_reportsrc(st);
  14187. } else if( num_vars > num_cols ) {
  14188. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14189. script_reportsrc(st);
  14190. }
  14191. // Store data
  14192. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14193. for( j = 0; j < num_vars; ++j ) {
  14194. char* str = NULL;
  14195. if( j < num_cols )
  14196. Sql_GetData(handle, j, &str, NULL);
  14197. data = script_getdata(st, j+3);
  14198. name = reference_getname(data);
  14199. if( is_string_variable(name) )
  14200. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  14201. else
  14202. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  14203. }
  14204. }
  14205. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14206. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14207. script_reportsrc(st);
  14208. }
  14209. // Free data
  14210. Sql_FreeResult(handle);
  14211. script_pushint(st, i);
  14212. return SCRIPT_CMD_SUCCESS;
  14213. }
  14214. BUILDIN_FUNC(query_sql) {
  14215. #ifdef BETA_THREAD_TEST
  14216. if( st->state != RERUNLINE ) {
  14217. queryThread_add(st,false);
  14218. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14219. } else
  14220. st->state = RUN;
  14221. return SCRIPT_CMD_SUCCESS;
  14222. #else
  14223. return buildin_query_sql_sub(st, qsmysql_handle);
  14224. #endif
  14225. }
  14226. BUILDIN_FUNC(query_logsql) {
  14227. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14228. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14229. script_pushint(st,-1);
  14230. return SCRIPT_CMD_FAILURE;
  14231. }
  14232. #ifdef BETA_THREAD_TEST
  14233. if( st->state != RERUNLINE ) {
  14234. queryThread_add(st,true);
  14235. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14236. } else
  14237. st->state = RUN;
  14238. return SCRIPT_CMD_SUCCESS;
  14239. #else
  14240. return buildin_query_sql_sub(st, logmysql_handle);
  14241. #endif
  14242. }
  14243. //Allows escaping of a given string.
  14244. BUILDIN_FUNC(escape_sql)
  14245. {
  14246. const char *str;
  14247. char *esc_str;
  14248. size_t len;
  14249. str = script_getstr(st,2);
  14250. len = strlen(str);
  14251. esc_str = (char*)aMalloc(len*2+1);
  14252. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14253. script_pushstr(st, esc_str);
  14254. return SCRIPT_CMD_SUCCESS;
  14255. }
  14256. BUILDIN_FUNC(getd)
  14257. {
  14258. char varname[100];
  14259. const char *buffer;
  14260. int elem;
  14261. buffer = script_getstr(st, 2);
  14262. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14263. elem = 0;
  14264. // Push the 'pointer' so it's more flexible [Lance]
  14265. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14266. return SCRIPT_CMD_SUCCESS;
  14267. }
  14268. BUILDIN_FUNC(callshop)
  14269. {
  14270. TBL_PC *sd = NULL;
  14271. struct npc_data *nd;
  14272. const char *shopname;
  14273. int flag = 0;
  14274. sd = script_rid2sd(st);
  14275. if (!sd) {
  14276. script_pushint(st,0);
  14277. return SCRIPT_CMD_SUCCESS;
  14278. }
  14279. shopname = script_getstr(st, 2);
  14280. if (script_hasdata(st,3))
  14281. flag = script_getnum(st,3);
  14282. nd = npc_name2id(shopname);
  14283. 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) ) {
  14284. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14285. script_pushint(st,0);
  14286. return SCRIPT_CMD_FAILURE;
  14287. }
  14288. if (nd->subtype == NPCTYPE_SHOP) {
  14289. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14290. sd->state.callshop = 1;
  14291. switch (flag) {
  14292. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14293. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14294. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14295. }
  14296. }
  14297. #if PACKETVER >= 20131223
  14298. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14299. unsigned short i;
  14300. for (i = 0; i < nd->u.shop.count; i++) {
  14301. if (nd->u.shop.shop_item[i].qty)
  14302. break;
  14303. }
  14304. if (i == nd->u.shop.count) {
  14305. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  14306. return SCRIPT_CMD_FAILURE;
  14307. }
  14308. sd->npc_shopid = nd->bl.id;
  14309. clif_npc_market_open(sd, nd);
  14310. script_pushint(st,1);
  14311. return SCRIPT_CMD_SUCCESS;
  14312. }
  14313. #endif
  14314. else
  14315. clif_cashshop_show(sd, nd);
  14316. sd->npc_shopid = nd->bl.id;
  14317. script_pushint(st,1);
  14318. return SCRIPT_CMD_SUCCESS;
  14319. }
  14320. BUILDIN_FUNC(npcshopitem)
  14321. {
  14322. const char* npcname = script_getstr(st, 2);
  14323. struct npc_data* nd = npc_name2id(npcname);
  14324. int n, i;
  14325. int amount;
  14326. uint16 offs = 2;
  14327. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14328. script_pushint(st,0);
  14329. return SCRIPT_CMD_SUCCESS;
  14330. }
  14331. #if PACKETVER >= 20131223
  14332. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14333. offs = 3;
  14334. npc_market_delfromsql_(nd->exname, 0, true);
  14335. }
  14336. #endif
  14337. // get the count of new entries
  14338. amount = (script_lastdata(st)-2)/offs;
  14339. // generate new shop item list
  14340. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14341. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14342. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14343. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14344. #if PACKETVER >= 20131223
  14345. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14346. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14347. nd->u.shop.shop_item[n].flag = 1;
  14348. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14349. }
  14350. #endif
  14351. }
  14352. nd->u.shop.count = n;
  14353. script_pushint(st,1);
  14354. return SCRIPT_CMD_SUCCESS;
  14355. }
  14356. BUILDIN_FUNC(npcshopadditem)
  14357. {
  14358. const char* npcname = script_getstr(st,2);
  14359. struct npc_data* nd = npc_name2id(npcname);
  14360. int n, i;
  14361. uint16 offs = 2, amount;
  14362. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14363. script_pushint(st,0);
  14364. return SCRIPT_CMD_SUCCESS;
  14365. }
  14366. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14367. offs = 3;
  14368. // get the count of new entries
  14369. amount = (script_lastdata(st)-2)/offs;
  14370. #if PACKETVER >= 20131223
  14371. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14372. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14373. uint16 nameid = script_getnum(st,i), j;
  14374. // Check existing entries
  14375. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14376. if (j == nd->u.shop.count) {
  14377. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14378. j = nd->u.shop.count;
  14379. nd->u.shop.shop_item[j].flag = 1;
  14380. nd->u.shop.count++;
  14381. }
  14382. nd->u.shop.shop_item[j].nameid = nameid;
  14383. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14384. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14385. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14386. }
  14387. script_pushint(st,1);
  14388. return SCRIPT_CMD_SUCCESS;
  14389. }
  14390. #endif
  14391. // append new items to existing shop item list
  14392. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14393. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14394. {
  14395. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14396. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14397. }
  14398. nd->u.shop.count = n;
  14399. script_pushint(st,1);
  14400. return SCRIPT_CMD_SUCCESS;
  14401. }
  14402. BUILDIN_FUNC(npcshopdelitem)
  14403. {
  14404. const char* npcname = script_getstr(st,2);
  14405. struct npc_data* nd = npc_name2id(npcname);
  14406. int n, i, size;
  14407. unsigned short amount;
  14408. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14409. script_pushint(st,0);
  14410. return SCRIPT_CMD_SUCCESS;
  14411. }
  14412. amount = script_lastdata(st)-2;
  14413. size = nd->u.shop.count;
  14414. // remove specified items from the shop item list
  14415. for( i = 3; i < 3 + amount; i++ ) {
  14416. unsigned short nameid = script_getnum(st,i);
  14417. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14418. if( n < size ) {
  14419. if (n+1 != size)
  14420. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14421. #if PACKETVER >= 20131223
  14422. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14423. npc_market_delfromsql_(nd->exname, nameid, false);
  14424. #endif
  14425. size--;
  14426. }
  14427. }
  14428. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14429. nd->u.shop.count = size;
  14430. script_pushint(st,1);
  14431. return SCRIPT_CMD_SUCCESS;
  14432. }
  14433. /**
  14434. * Sets a script to attach to a shop npc.
  14435. * npcshopattach "<npc_name>";
  14436. **/
  14437. BUILDIN_FUNC(npcshopattach)
  14438. {
  14439. const char* npcname = script_getstr(st,2);
  14440. struct npc_data* nd = npc_name2id(npcname);
  14441. int flag = 1;
  14442. if( script_hasdata(st,3) )
  14443. flag = script_getnum(st,3);
  14444. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14445. script_pushint(st,0);
  14446. return SCRIPT_CMD_SUCCESS;
  14447. }
  14448. if (flag)
  14449. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14450. else
  14451. nd->master_nd = NULL;
  14452. script_pushint(st,1);
  14453. return SCRIPT_CMD_SUCCESS;
  14454. }
  14455. /*==========================================
  14456. * Returns some values of an item [Lupus]
  14457. * Price, Weight, etc...
  14458. setitemscript(itemID,"{new item bonus script}",[n]);
  14459. Where n:
  14460. 0 - script
  14461. 1 - Equip script
  14462. 2 - Unequip script
  14463. *------------------------------------------*/
  14464. BUILDIN_FUNC(setitemscript)
  14465. {
  14466. unsigned short item_id;
  14467. int n = 0;
  14468. const char *script;
  14469. struct item_data *i_data;
  14470. struct script_code **dstscript;
  14471. item_id = script_getnum(st,2);
  14472. script = script_getstr(st,3);
  14473. if( script_hasdata(st,4) )
  14474. n=script_getnum(st,4);
  14475. i_data = itemdb_exists(item_id);
  14476. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14477. script_pushint(st,0);
  14478. return SCRIPT_CMD_SUCCESS;
  14479. }
  14480. switch (n) {
  14481. case 2:
  14482. dstscript = &i_data->unequip_script;
  14483. break;
  14484. case 1:
  14485. dstscript = &i_data->equip_script;
  14486. break;
  14487. default:
  14488. dstscript = &i_data->script;
  14489. break;
  14490. }
  14491. if(*dstscript)
  14492. script_free_code(*dstscript);
  14493. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14494. script_pushint(st,1);
  14495. return SCRIPT_CMD_SUCCESS;
  14496. }
  14497. /*=======================================================
  14498. * Add or Update a mob drop temporarily [Akinari]
  14499. * Original Idea By: [Lupus]
  14500. *
  14501. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14502. *
  14503. * If given an item the mob already drops, the rate
  14504. * is updated to the new rate. Rate cannot exceed 10000
  14505. * Returns 1 if succeeded (added/updated a mob drop)
  14506. *-------------------------------------------------------*/
  14507. BUILDIN_FUNC(addmonsterdrop)
  14508. {
  14509. struct mob_db *mob;
  14510. struct script_data *data;
  14511. unsigned short item_id;
  14512. int rate;
  14513. data = script_getdata(st, 2);
  14514. get_val(st, data); // Convert into value in case of a variable
  14515. if(data_isstring(data))
  14516. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14517. else
  14518. mob = mob_db(script_getnum(st,2));
  14519. item_id=script_getnum(st,3);
  14520. rate=script_getnum(st,4);
  14521. if(!itemdb_exists(item_id)){
  14522. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14523. return SCRIPT_CMD_FAILURE;
  14524. }
  14525. if(mob) { //We got a valid monster, check for available drop slot
  14526. unsigned char i, c = 0;
  14527. for(i = 0; i < MAX_MOB_DROP; i++) {
  14528. if(mob->dropitem[i].nameid) {
  14529. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14530. c = i;
  14531. break;
  14532. }
  14533. continue;
  14534. }
  14535. if(!c) //Accept first available slot only
  14536. c = i;
  14537. }
  14538. if(c) { //Fill in the slot with the item and rate
  14539. mob->dropitem[c].nameid = item_id;
  14540. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14541. script_pushint(st,1);
  14542. } else //No place to put the new drop
  14543. script_pushint(st,0);
  14544. } else {
  14545. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14546. return SCRIPT_CMD_FAILURE;
  14547. }
  14548. return SCRIPT_CMD_SUCCESS;
  14549. }
  14550. /*=======================================================
  14551. * Delete a mob drop temporarily [Akinari]
  14552. * Original Idea By: [Lupus]
  14553. *
  14554. * delmonsterdrop <mob_id or name>,<item_id>;
  14555. *
  14556. * Returns 1 if succeeded (deleted a mob drop)
  14557. *-------------------------------------------------------*/
  14558. BUILDIN_FUNC(delmonsterdrop)
  14559. {
  14560. struct mob_db *mob;
  14561. struct script_data *data;
  14562. unsigned short item_id;
  14563. data = script_getdata(st, 2);
  14564. get_val(st, data); // Convert into value in case of a variable
  14565. if(data_isstring(data))
  14566. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14567. else
  14568. mob = mob_db(script_getnum(st,2));
  14569. item_id=script_getnum(st,3);
  14570. if(!itemdb_exists(item_id)){
  14571. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14572. return SCRIPT_CMD_FAILURE;
  14573. }
  14574. if(mob) { //We got a valid monster, check for item drop on monster
  14575. unsigned char i;
  14576. for(i = 0; i < MAX_MOB_DROP; i++) {
  14577. if(mob->dropitem[i].nameid == item_id) {
  14578. mob->dropitem[i].nameid = 0;
  14579. mob->dropitem[i].p = 0;
  14580. script_pushint(st,1);
  14581. return SCRIPT_CMD_SUCCESS;
  14582. }
  14583. }
  14584. //No drop on that monster
  14585. script_pushint(st,0);
  14586. } else {
  14587. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14588. return SCRIPT_CMD_FAILURE;
  14589. }
  14590. return SCRIPT_CMD_SUCCESS;
  14591. }
  14592. /*==========================================
  14593. * Returns some values of a monster [Lupus]
  14594. * Name, Level, race, size, etc...
  14595. getmonsterinfo(monsterID,queryIndex);
  14596. *------------------------------------------*/
  14597. BUILDIN_FUNC(getmonsterinfo)
  14598. {
  14599. struct mob_db *mob;
  14600. int mob_id;
  14601. mob_id = script_getnum(st,2);
  14602. if (!mobdb_checkid(mob_id)) {
  14603. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14604. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  14605. script_pushconststr(st,"null");
  14606. else
  14607. script_pushint(st,-1);
  14608. return SCRIPT_CMD_SUCCESS;
  14609. }
  14610. mob = mob_db(mob_id);
  14611. switch ( script_getnum(st,3) ) {
  14612. case MOB_NAME: script_pushstrcopy(st,mob->jname); break;
  14613. case MOB_LV: script_pushint(st,mob->lv); break;
  14614. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  14615. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  14616. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  14617. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  14618. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  14619. case MOB_DEF: script_pushint(st,mob->status.def); break;
  14620. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  14621. case MOB_STR: script_pushint(st,mob->status.str); break;
  14622. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  14623. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  14624. case MOB_INT: script_pushint(st,mob->status.int_); break;
  14625. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  14626. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  14627. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  14628. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  14629. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  14630. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  14631. case MOB_RACE: script_pushint(st,mob->status.race); break;
  14632. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  14633. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  14634. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  14635. default: script_pushint(st,-1); //wrong Index
  14636. }
  14637. return SCRIPT_CMD_SUCCESS;
  14638. }
  14639. /**
  14640. * Check player's vending/buyingstore/autotrade state
  14641. * checkvending({"<Player Name>"})
  14642. * @author [Nab4]
  14643. */
  14644. BUILDIN_FUNC(checkvending) {
  14645. TBL_PC *sd = NULL;
  14646. if (!script_nick2sd(2,sd) ) {
  14647. script_pushint(st,0);
  14648. return SCRIPT_CMD_SUCCESS;
  14649. }
  14650. else {
  14651. int8 ret = 0;
  14652. if (sd->state.vending)
  14653. ret = 1;
  14654. else if (sd->state.buyingstore)
  14655. ret = 4;
  14656. if (sd->state.autotrade)
  14657. ret |= 2;
  14658. script_pushint(st, ret);
  14659. }
  14660. return SCRIPT_CMD_SUCCESS;
  14661. }
  14662. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14663. {
  14664. TBL_PC *sd = NULL;
  14665. if( script_nick2sd(2,sd) )
  14666. script_pushint(st,(sd->chatID != 0));
  14667. else
  14668. script_pushint(st,0);
  14669. return SCRIPT_CMD_SUCCESS;
  14670. }
  14671. BUILDIN_FUNC(checkidle)
  14672. {
  14673. TBL_PC *sd = NULL;
  14674. if( script_nick2sd(2,sd) )
  14675. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14676. else
  14677. script_pushint(st, 0);
  14678. return SCRIPT_CMD_SUCCESS;
  14679. }
  14680. BUILDIN_FUNC(searchitem)
  14681. {
  14682. struct script_data* data = script_getdata(st, 2);
  14683. const char *itemname = script_getstr(st,3);
  14684. struct item_data *items[MAX_SEARCH];
  14685. int count;
  14686. char* name;
  14687. int32 start;
  14688. int32 id;
  14689. int32 i;
  14690. TBL_PC* sd = NULL;
  14691. if ((items[0] = itemdb_exists(atoi(itemname))))
  14692. count = 1;
  14693. else
  14694. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14695. if (!count) {
  14696. script_pushint(st, 0);
  14697. return SCRIPT_CMD_SUCCESS;
  14698. }
  14699. if( !data_isreference(data) )
  14700. {
  14701. ShowError("script:searchitem: not a variable\n");
  14702. script_reportdata(data);
  14703. st->state = END;
  14704. return SCRIPT_CMD_FAILURE;// not a variable
  14705. }
  14706. id = reference_getid(data);
  14707. start = reference_getindex(data);
  14708. name = reference_getname(data);
  14709. if( not_server_variable(*name) )
  14710. {
  14711. sd = script_rid2sd(st);
  14712. if( sd == NULL )
  14713. return SCRIPT_CMD_SUCCESS;// no player attached
  14714. }
  14715. if( is_string_variable(name) )
  14716. {// string array
  14717. ShowError("script:searchitem: not an integer array reference\n");
  14718. script_reportdata(data);
  14719. st->state = END;
  14720. return SCRIPT_CMD_FAILURE;// not supported
  14721. }
  14722. for( i = 0; i < count; ++start, ++i )
  14723. {// Set array
  14724. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14725. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14726. }
  14727. script_pushint(st, count);
  14728. return SCRIPT_CMD_SUCCESS;
  14729. }
  14730. // [zBuffer] List of player cont commands --->
  14731. BUILDIN_FUNC(rid2name)
  14732. {
  14733. struct block_list *bl = NULL;
  14734. int rid = script_getnum(st,2);
  14735. if((bl = map_id2bl(rid)))
  14736. {
  14737. switch(bl->type) {
  14738. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14739. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14740. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14741. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14742. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14743. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14744. default:
  14745. ShowError("buildin_rid2name: BL type unknown.\n");
  14746. script_pushconststr(st,"");
  14747. break;
  14748. }
  14749. } else {
  14750. ShowError("buildin_rid2name: invalid RID\n");
  14751. script_pushconststr(st,"(null)");
  14752. }
  14753. return SCRIPT_CMD_SUCCESS;
  14754. }
  14755. /**
  14756. * Toggle a unit from moving.
  14757. * pcblockmove(<unit_id>,<option>);
  14758. */
  14759. BUILDIN_FUNC(pcblockmove)
  14760. {
  14761. struct block_list *bl = NULL;
  14762. if (script_getnum(st, 2))
  14763. bl = map_id2bl(script_getnum(st,2));
  14764. else
  14765. bl = map_id2bl(st->rid);
  14766. if (bl) {
  14767. struct unit_data *ud = unit_bl2ud(bl);
  14768. if (ud)
  14769. ud->state.blockedmove = script_getnum(st,3) > 0;
  14770. }
  14771. return SCRIPT_CMD_SUCCESS;
  14772. }
  14773. /**
  14774. * Toggle a unit from casting skills.
  14775. * pcblockskill(<unit_id>,<option>);
  14776. */
  14777. BUILDIN_FUNC(pcblockskill)
  14778. {
  14779. struct block_list *bl = NULL;
  14780. if (script_getnum(st, 2))
  14781. bl = map_id2bl(script_getnum(st,2));
  14782. else
  14783. bl = map_id2bl(st->rid);
  14784. if (bl) {
  14785. struct unit_data *ud = unit_bl2ud(bl);
  14786. if (ud)
  14787. ud->state.blockedskill = script_getnum(st, 3) > 0;
  14788. }
  14789. return SCRIPT_CMD_SUCCESS;
  14790. }
  14791. BUILDIN_FUNC(pcfollow)
  14792. {
  14793. int id, targetid;
  14794. TBL_PC *sd = NULL;
  14795. id = script_getnum(st,2);
  14796. targetid = script_getnum(st,3);
  14797. if(id)
  14798. sd = map_id2sd(id);
  14799. else
  14800. sd = script_rid2sd(st);
  14801. if(sd)
  14802. pc_follow(sd, targetid);
  14803. return SCRIPT_CMD_SUCCESS;
  14804. }
  14805. BUILDIN_FUNC(pcstopfollow)
  14806. {
  14807. int id;
  14808. TBL_PC *sd = NULL;
  14809. id = script_getnum(st,2);
  14810. if(id)
  14811. sd = map_id2sd(id);
  14812. else
  14813. sd = script_rid2sd(st);
  14814. if(sd)
  14815. pc_stop_following(sd);
  14816. return SCRIPT_CMD_SUCCESS;
  14817. }
  14818. // <--- [zBuffer] List of player cont commands
  14819. // [zBuffer] List of unit control commands --->
  14820. /// Checks to see if the unit exists.
  14821. ///
  14822. /// unitexists <unit id>;
  14823. BUILDIN_FUNC(unitexists)
  14824. {
  14825. struct block_list* bl;
  14826. bl = map_id2bl(script_getnum(st, 2));
  14827. if (!bl)
  14828. script_pushint(st, false);
  14829. else
  14830. script_pushint(st, true);
  14831. return SCRIPT_CMD_SUCCESS;
  14832. }
  14833. /// Gets the type of the given Game ID.
  14834. ///
  14835. /// getunittype <unit id>;
  14836. BUILDIN_FUNC(getunittype)
  14837. {
  14838. struct block_list* bl;
  14839. uint8 value = 0;
  14840. if(!script_rid2bl(2,bl))
  14841. {
  14842. script_pushint(st, -1);
  14843. return SCRIPT_CMD_FAILURE;
  14844. }
  14845. switch (bl->type) {
  14846. case BL_PC: value = UNITTYPE_PC; break;
  14847. case BL_NPC: value = UNITTYPE_NPC; break;
  14848. case BL_PET: value = UNITTYPE_PET; break;
  14849. case BL_MOB: value = UNITTYPE_MOB; break;
  14850. case BL_HOM: value = UNITTYPE_HOM; break;
  14851. case BL_MER: value = UNITTYPE_MER; break;
  14852. case BL_ELEM: value = UNITTYPE_ELEM; break;
  14853. default: value = -1; break;
  14854. }
  14855. script_pushint(st, value);
  14856. return SCRIPT_CMD_SUCCESS;
  14857. }
  14858. /// Gets specific live information of a bl.
  14859. ///
  14860. /// getunitdata <unit id>,<arrayname>;
  14861. BUILDIN_FUNC(getunitdata)
  14862. {
  14863. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14864. struct block_list* bl;
  14865. TBL_MOB* md = NULL;
  14866. TBL_HOM* hd = NULL;
  14867. TBL_MER* mc = NULL;
  14868. TBL_PET* pd = NULL;
  14869. TBL_ELEM* ed = NULL;
  14870. TBL_NPC* nd = NULL;
  14871. char* name;
  14872. struct script_data *data = script_getdata(st, 3);
  14873. if (!data_isreference(data)) {
  14874. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14875. return SCRIPT_CMD_FAILURE;
  14876. }
  14877. if(!script_rid2bl(2,bl))
  14878. {
  14879. script_pushint(st, -1);
  14880. return SCRIPT_CMD_FAILURE;
  14881. }
  14882. switch (bl->type) {
  14883. case BL_MOB: md = map_id2md(bl->id); break;
  14884. case BL_HOM: hd = map_id2hd(bl->id); break;
  14885. case BL_PET: pd = map_id2pd(bl->id); break;
  14886. case BL_MER: mc = map_id2mc(bl->id); break;
  14887. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14888. case BL_NPC: nd = map_id2nd(bl->id); break;
  14889. }
  14890. name = reference_getname(data);
  14891. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  14892. switch(bl->type) {
  14893. case BL_MOB:
  14894. if (!md) {
  14895. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14896. return SCRIPT_CMD_FAILURE;
  14897. }
  14898. getunitdata_sub(UMOB_SIZE, md->status.size);
  14899. getunitdata_sub(UMOB_LEVEL, md->level);
  14900. getunitdata_sub(UMOB_HP, md->status.hp);
  14901. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  14902. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  14903. getunitdata_sub(UMOB_MAPID, md->bl.m);
  14904. getunitdata_sub(UMOB_X, md->bl.x);
  14905. getunitdata_sub(UMOB_Y, md->bl.y);
  14906. getunitdata_sub(UMOB_SPEED, md->status.speed);
  14907. getunitdata_sub(UMOB_MODE, md->status.mode);
  14908. getunitdata_sub(UMOB_AI, md->special_state.ai);
  14909. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  14910. getunitdata_sub(UMOB_SEX, md->vd->sex);
  14911. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  14912. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  14913. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  14914. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  14915. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  14916. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  14917. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  14918. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  14919. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  14920. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  14921. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  14922. getunitdata_sub(UMOB_STR, md->status.str);
  14923. getunitdata_sub(UMOB_AGI, md->status.agi);
  14924. getunitdata_sub(UMOB_VIT, md->status.vit);
  14925. getunitdata_sub(UMOB_INT, md->status.int_);
  14926. getunitdata_sub(UMOB_DEX, md->status.dex);
  14927. getunitdata_sub(UMOB_LUK, md->status.luk);
  14928. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  14929. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  14930. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  14931. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  14932. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  14933. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  14934. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  14935. getunitdata_sub(UMOB_DEF, md->status.def);
  14936. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  14937. getunitdata_sub(UMOB_HIT, md->status.hit);
  14938. getunitdata_sub(UMOB_FLEE, md->status.flee);
  14939. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  14940. getunitdata_sub(UMOB_CRIT, md->status.cri);
  14941. getunitdata_sub(UMOB_RACE, md->status.race);
  14942. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  14943. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  14944. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  14945. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  14946. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  14947. break;
  14948. case BL_HOM:
  14949. if (!hd) {
  14950. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  14951. return SCRIPT_CMD_FAILURE;
  14952. }
  14953. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  14954. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  14955. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  14956. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  14957. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  14958. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  14959. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  14960. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  14961. getunitdata_sub(UHOM_X, hd->bl.x);
  14962. getunitdata_sub(UHOM_Y, hd->bl.y);
  14963. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  14964. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  14965. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  14966. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  14967. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  14968. getunitdata_sub(UHOM_STR, hd->base_status.str);
  14969. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  14970. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  14971. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  14972. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  14973. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  14974. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  14975. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  14976. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  14977. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  14978. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  14979. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  14980. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  14981. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  14982. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  14983. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  14984. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  14985. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  14986. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  14987. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  14988. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  14989. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  14990. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  14991. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  14992. break;
  14993. case BL_PET:
  14994. if (!pd) {
  14995. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  14996. return SCRIPT_CMD_FAILURE;
  14997. }
  14998. getunitdata_sub(UPET_SIZE, pd->status.size);
  14999. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  15000. getunitdata_sub(UPET_HP, pd->status.hp);
  15001. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  15002. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  15003. getunitdata_sub(UPET_MAPID, pd->bl.m);
  15004. getunitdata_sub(UPET_X, pd->bl.x);
  15005. getunitdata_sub(UPET_Y, pd->bl.y);
  15006. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  15007. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  15008. getunitdata_sub(UPET_SPEED, pd->status.speed);
  15009. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  15010. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  15011. getunitdata_sub(UPET_STR, pd->status.str);
  15012. getunitdata_sub(UPET_AGI, pd->status.agi);
  15013. getunitdata_sub(UPET_VIT, pd->status.vit);
  15014. getunitdata_sub(UPET_INT, pd->status.int_);
  15015. getunitdata_sub(UPET_DEX, pd->status.dex);
  15016. getunitdata_sub(UPET_LUK, pd->status.luk);
  15017. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  15018. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  15019. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  15020. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  15021. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  15022. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  15023. getunitdata_sub(UPET_DEF, pd->status.def);
  15024. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  15025. getunitdata_sub(UPET_HIT, pd->status.hit);
  15026. getunitdata_sub(UPET_FLEE, pd->status.flee);
  15027. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  15028. getunitdata_sub(UPET_CRIT, pd->status.cri);
  15029. getunitdata_sub(UPET_RACE, pd->status.race);
  15030. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  15031. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  15032. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  15033. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  15034. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  15035. break;
  15036. case BL_MER:
  15037. if (!mc) {
  15038. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  15039. return SCRIPT_CMD_FAILURE;
  15040. }
  15041. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  15042. getunitdata_sub(UMER_HP, mc->base_status.hp);
  15043. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  15044. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  15045. getunitdata_sub(UMER_MAPID, mc->bl.m);
  15046. getunitdata_sub(UMER_X, mc->bl.x);
  15047. getunitdata_sub(UMER_Y, mc->bl.y);
  15048. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  15049. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  15050. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  15051. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  15052. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  15053. getunitdata_sub(UMER_STR, mc->base_status.str);
  15054. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  15055. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  15056. getunitdata_sub(UMER_INT, mc->base_status.int_);
  15057. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  15058. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  15059. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  15060. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  15061. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  15062. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  15063. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  15064. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  15065. getunitdata_sub(UMER_DEF, mc->base_status.def);
  15066. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  15067. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  15068. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  15069. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  15070. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  15071. getunitdata_sub(UMER_RACE, mc->base_status.race);
  15072. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  15073. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  15074. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  15075. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  15076. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  15077. break;
  15078. case BL_ELEM:
  15079. if (!ed) {
  15080. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  15081. return SCRIPT_CMD_FAILURE;
  15082. }
  15083. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  15084. getunitdata_sub(UELE_HP, ed->elemental.hp);
  15085. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  15086. getunitdata_sub(UELE_SP, ed->elemental.sp);
  15087. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  15088. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  15089. getunitdata_sub(UELE_MAPID, ed->bl.m);
  15090. getunitdata_sub(UELE_X, ed->bl.x);
  15091. getunitdata_sub(UELE_Y, ed->bl.y);
  15092. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  15093. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  15094. getunitdata_sub(UELE_SP, ed->base_status.speed);
  15095. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  15096. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  15097. getunitdata_sub(UELE_STR, ed->base_status.str);
  15098. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  15099. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  15100. getunitdata_sub(UELE_INT, ed->base_status.int_);
  15101. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  15102. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  15103. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  15104. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  15105. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  15106. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  15107. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  15108. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  15109. getunitdata_sub(UELE_DEF, ed->base_status.def);
  15110. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  15111. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  15112. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  15113. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  15114. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  15115. getunitdata_sub(UELE_RACE, ed->base_status.race);
  15116. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  15117. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  15118. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  15119. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  15120. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  15121. break;
  15122. case BL_NPC:
  15123. if (!nd) {
  15124. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  15125. return SCRIPT_CMD_FAILURE;
  15126. }
  15127. getunitdata_sub(UNPC_DISPLAY, nd->class_);
  15128. getunitdata_sub(UNPC_LEVEL, nd->level);
  15129. getunitdata_sub(UNPC_HP, nd->status.hp);
  15130. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  15131. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  15132. getunitdata_sub(UNPC_X, nd->bl.x);
  15133. getunitdata_sub(UNPC_Y, nd->bl.y);
  15134. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  15135. getunitdata_sub(UNPC_STR, nd->status.str);
  15136. getunitdata_sub(UNPC_AGI, nd->status.agi);
  15137. getunitdata_sub(UNPC_VIT, nd->status.vit);
  15138. getunitdata_sub(UNPC_INT, nd->status.int_);
  15139. getunitdata_sub(UNPC_DEX, nd->status.dex);
  15140. getunitdata_sub(UNPC_LUK, nd->status.luk);
  15141. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  15142. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  15143. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  15144. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  15145. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  15146. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  15147. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  15148. getunitdata_sub(UNPC_DEF, nd->status.def);
  15149. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  15150. getunitdata_sub(UNPC_HIT, nd->status.hit);
  15151. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  15152. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  15153. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  15154. getunitdata_sub(UNPC_RACE, nd->status.race);
  15155. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  15156. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  15157. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  15158. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  15159. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  15160. break;
  15161. default:
  15162. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  15163. return SCRIPT_CMD_FAILURE;
  15164. }
  15165. #undef getunitdata_sub
  15166. return SCRIPT_CMD_SUCCESS;
  15167. }
  15168. /// Changes the live data of a bl.
  15169. ///
  15170. /// setunitdata <unit id>,<type>,<value>;
  15171. BUILDIN_FUNC(setunitdata)
  15172. {
  15173. struct block_list* bl = NULL;
  15174. struct script_data* data;
  15175. const char *mapname = NULL;
  15176. TBL_MOB* md = NULL;
  15177. TBL_HOM* hd = NULL;
  15178. TBL_MER* mc = NULL;
  15179. TBL_PET* pd = NULL;
  15180. TBL_ELEM* ed = NULL;
  15181. TBL_NPC* nd = NULL;
  15182. int type, value = 0;
  15183. bool calc_status = false;
  15184. if(!script_rid2bl(2,bl))
  15185. {
  15186. script_pushint(st, -1);
  15187. return SCRIPT_CMD_FAILURE;
  15188. }
  15189. switch (bl->type) {
  15190. case BL_MOB: md = map_id2md(bl->id); break;
  15191. case BL_HOM: hd = map_id2hd(bl->id); break;
  15192. case BL_PET: pd = map_id2pd(bl->id); break;
  15193. case BL_MER: mc = map_id2mc(bl->id); break;
  15194. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15195. case BL_NPC:
  15196. nd = map_id2nd(bl->id);
  15197. if (!nd->status.hp)
  15198. status_calc_npc(nd, SCO_FIRST);
  15199. else
  15200. status_calc_npc(nd, SCO_NONE);
  15201. break;
  15202. default:
  15203. ShowError("buildin_setunitdata: Invalid object!\n");
  15204. return SCRIPT_CMD_FAILURE;
  15205. }
  15206. type = script_getnum(st, 3);
  15207. data = script_getdata(st, 4);
  15208. get_val(st, data);
  15209. if (type == 5 && data_isstring(data))
  15210. mapname = conv_str(st, data);
  15211. else if (data_isint(data))
  15212. value = conv_num(st, data);
  15213. else {
  15214. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).\n", data->type);
  15215. return SCRIPT_CMD_FAILURE;
  15216. }
  15217. switch (bl->type) {
  15218. case BL_MOB:
  15219. if (!md) {
  15220. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  15221. return SCRIPT_CMD_FAILURE;
  15222. }
  15223. if (!md->base_status) {
  15224. md->base_status = (struct status_data*)aCalloc(1, sizeof(struct status_data));
  15225. memcpy(md->base_status, &md->db->status, sizeof(struct status_data));
  15226. }
  15227. switch (type) {
  15228. case UMOB_SIZE: md->base_status->size = (unsigned char)value; calc_status = true; break;
  15229. case UMOB_LEVEL: md->level = (unsigned short)value; break;
  15230. case UMOB_HP: md->base_status->hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15231. case UMOB_MAXHP: md->base_status->max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15232. case UMOB_MASTERAID: md->master_id = value; break;
  15233. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15234. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15235. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  15236. case UMOB_SPEED: md->base_status->speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15237. case UMOB_MODE: md->base_status->mode = (enum e_mode)value; calc_status = true; break;
  15238. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15239. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15240. case UMOB_SEX: md->vd->sex = (char)value; break;
  15241. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); break;
  15242. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15243. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15244. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15245. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15246. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15247. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15248. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15249. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15250. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15251. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15252. case UMOB_STR: md->base_status->str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15253. case UMOB_AGI: md->base_status->agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15254. case UMOB_VIT: md->base_status->vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15255. case UMOB_INT: md->base_status->int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15256. case UMOB_DEX: md->base_status->dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15257. case UMOB_LUK: md->base_status->luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15258. case UMOB_SLAVECPYMSTRMD:
  15259. if (value > 0) {
  15260. TBL_MOB *md2 = NULL;
  15261. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15262. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15263. break;
  15264. }
  15265. md->base_status->mode = md2->status.mode;
  15266. md->state.copy_master_mode = 1;
  15267. } else
  15268. md->state.copy_master_mode = 0;
  15269. calc_status = true;
  15270. break;
  15271. case UMOB_DMGIMMUNE: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15272. case UMOB_ATKRANGE: md->base_status->rhw.range = (unsigned short)value; calc_status = true; break;
  15273. case UMOB_ATKMIN: md->base_status->rhw.atk = (unsigned short)value; calc_status = true; break;
  15274. case UMOB_ATKMAX: md->base_status->rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15275. case UMOB_MATKMIN: md->base_status->matk_min = (unsigned short)value; calc_status = true; break;
  15276. case UMOB_MATKMAX: md->base_status->matk_max = (unsigned short)value; calc_status = true; break;
  15277. case UMOB_DEF: md->base_status->def = (defType)value; calc_status = true; break;
  15278. case UMOB_MDEF: md->base_status->mdef = (defType)value; calc_status = true; break;
  15279. case UMOB_HIT: md->base_status->hit = (short)value; calc_status = true; break;
  15280. case UMOB_FLEE: md->base_status->flee = (short)value; calc_status = true; break;
  15281. case UMOB_PDODGE: md->base_status->flee2 = (short)value; calc_status = true; break;
  15282. case UMOB_CRIT: md->base_status->cri = (short)value; calc_status = true; break;
  15283. case UMOB_RACE: md->base_status->race = (unsigned char)value; calc_status = true; break;
  15284. case UMOB_ELETYPE: md->base_status->def_ele = (unsigned char)value; calc_status = true; break;
  15285. case UMOB_ELELEVEL: md->base_status->ele_lv = (unsigned char)value; calc_status = true; break;
  15286. case UMOB_AMOTION: md->base_status->amotion = (short)value; calc_status = true; break;
  15287. case UMOB_ADELAY: md->base_status->adelay = (short)value; calc_status = true; break;
  15288. case UMOB_DMOTION: md->base_status->dmotion = (short)value; calc_status = true; break;
  15289. default:
  15290. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15291. return SCRIPT_CMD_FAILURE;
  15292. }
  15293. if (calc_status)
  15294. status_calc_bl(&md->bl, SCB_BATTLE);
  15295. break;
  15296. case BL_HOM:
  15297. if (!hd) {
  15298. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15299. return SCRIPT_CMD_FAILURE;
  15300. }
  15301. switch (type) {
  15302. case UHOM_SIZE: hd->base_status.size = (unsigned char)value; calc_status = true; break;
  15303. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15304. case UHOM_HP: hd->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15305. case UHOM_MAXHP: hd->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15306. case UHOM_SP: hd->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  15307. case UHOM_MAXSP: hd->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  15308. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15309. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15310. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15311. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15312. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15313. 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;
  15314. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15315. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15316. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15317. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15318. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15319. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15320. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15321. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15322. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15323. case UHOM_DMGIMMUNE: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15324. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15325. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15326. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15327. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15328. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15329. case UHOM_DEF: hd->base_status.def = (defType)value; calc_status = true; break;
  15330. case UHOM_MDEF: hd->base_status.mdef = (defType)value; calc_status = true; break;
  15331. case UHOM_HIT: hd->base_status.hit = (short)value; calc_status = true; break;
  15332. case UHOM_FLEE: hd->base_status.flee = (short)value; calc_status = true; break;
  15333. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; calc_status = true; break;
  15334. case UHOM_CRIT: hd->base_status.cri = (short)value; calc_status = true; break;
  15335. case UHOM_RACE: hd->base_status.race = (unsigned char)value; calc_status = true; break;
  15336. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15337. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15338. case UHOM_AMOTION: hd->base_status.amotion = (short)value; calc_status = true; break;
  15339. case UHOM_ADELAY: hd->base_status.adelay = (short)value; calc_status = true; break;
  15340. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; calc_status = true; break;
  15341. default:
  15342. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15343. return SCRIPT_CMD_FAILURE;
  15344. }
  15345. if (calc_status)
  15346. status_calc_bl(&hd->bl, SCB_BATTLE);
  15347. break;
  15348. case BL_PET:
  15349. if (!pd) {
  15350. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15351. return SCRIPT_CMD_FAILURE;
  15352. }
  15353. switch (type) {
  15354. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15355. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15356. case UPET_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15357. case UPET_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15358. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15359. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15360. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15361. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15362. case UPET_HUNGER: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  15363. 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;
  15364. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15365. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15366. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15367. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15368. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15369. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15370. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15371. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15372. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15373. case UPET_DMGIMMUNE: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15374. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15375. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15376. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15377. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15378. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15379. case UPET_DEF: pd->status.def = (defType)value; break;
  15380. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15381. case UPET_HIT: pd->status.hit = (short)value; break;
  15382. case UPET_FLEE: pd->status.flee = (short)value; break;
  15383. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  15384. case UPET_CRIT: pd->status.cri = (short)value; break;
  15385. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  15386. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  15387. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  15388. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  15389. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  15390. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  15391. default:
  15392. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15393. return SCRIPT_CMD_FAILURE;
  15394. }
  15395. break;
  15396. case BL_MER:
  15397. if (!mc) {
  15398. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15399. return SCRIPT_CMD_FAILURE;
  15400. }
  15401. switch (type) {
  15402. case UMER_SIZE: mc->base_status.size = (unsigned char)value; calc_status = true; break;
  15403. case UMER_HP: mc->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15404. case UMER_MAXHP: mc->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15405. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  15406. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15407. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15408. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15409. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  15410. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  15411. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15412. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15413. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15414. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15415. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15416. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15417. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15418. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15419. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15420. case UMER_DMGIMMUNE: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15421. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15422. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15423. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15424. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15425. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15426. case UMER_DEF: mc->base_status.def = (defType)value; calc_status = true; break;
  15427. case UMER_MDEF: mc->base_status.mdef = (defType)value; calc_status = true; break;
  15428. case UMER_HIT: mc->base_status.hit = (short)value; calc_status = true; break;
  15429. case UMER_FLEE: mc->base_status.flee = (short)value; calc_status = true; break;
  15430. case UMER_PDODGE: mc->base_status.flee2 = (short)value; calc_status = true; break;
  15431. case UMER_CRIT: mc->base_status.cri = (short)value; calc_status = true; break;
  15432. case UMER_RACE: mc->base_status.race = (unsigned char)value; calc_status = true; break;
  15433. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15434. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15435. case UMER_AMOTION: mc->base_status.amotion = (short)value; calc_status = true; break;
  15436. case UMER_ADELAY: mc->base_status.adelay = (short)value; calc_status = true; break;
  15437. case UMER_DMOTION: mc->base_status.dmotion = (short)value; calc_status = true; break;
  15438. default:
  15439. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  15440. return SCRIPT_CMD_FAILURE;
  15441. }
  15442. if (calc_status)
  15443. status_calc_bl(&mc->bl, SCB_BATTLE);
  15444. break;
  15445. case BL_ELEM:
  15446. if (!ed) {
  15447. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  15448. return SCRIPT_CMD_FAILURE;
  15449. }
  15450. switch (type) {
  15451. case UELE_SIZE: ed->base_status.size = (unsigned char)value; calc_status = true; break;
  15452. case UELE_HP: ed->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15453. case UELE_MAXHP: ed->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15454. case UELE_SP: ed->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  15455. case UELE_MAXSP: ed->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  15456. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  15457. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15458. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  15459. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  15460. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  15461. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; calc_status = true; break;
  15462. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15463. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15464. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15465. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15466. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15467. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15468. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15469. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15470. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15471. case UELE_DMGIMMUNE: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15472. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15473. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15474. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15475. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15476. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15477. case UELE_DEF: ed->base_status.def = (defType)value; calc_status = true; break;
  15478. case UELE_MDEF: ed->base_status.mdef = (defType)value; calc_status = true; break;
  15479. case UELE_HIT: ed->base_status.hit = (short)value; calc_status = true; break;
  15480. case UELE_FLEE: ed->base_status.flee = (short)value; calc_status = true; break;
  15481. case UELE_PDODGE: ed->base_status.flee2 = (short)value; calc_status = true; break;
  15482. case UELE_CRIT: ed->base_status.cri = (short)value; calc_status = true; break;
  15483. case UELE_RACE: ed->base_status.race = (unsigned char)value; calc_status = true; break;
  15484. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15485. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15486. case UELE_AMOTION: ed->base_status.amotion = (short)value; calc_status = true; break;
  15487. case UELE_ADELAY: ed->base_status.adelay = (short)value; calc_status = true; break;
  15488. case UELE_DMOTION: ed->base_status.dmotion = (short)value; calc_status = true; break;
  15489. default:
  15490. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  15491. return SCRIPT_CMD_FAILURE;
  15492. }
  15493. if (calc_status)
  15494. status_calc_bl(&ed->bl, SCB_BATTLE);
  15495. break;
  15496. case BL_NPC:
  15497. if (!nd) {
  15498. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  15499. return SCRIPT_CMD_FAILURE;
  15500. }
  15501. switch (type) {
  15502. case UNPC_DISPLAY: status_set_viewdata(bl, (unsigned short)value); break;
  15503. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  15504. case UNPC_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15505. case UNPC_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15506. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15507. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  15508. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  15509. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15510. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15511. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15512. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15513. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15514. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15515. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15516. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  15517. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  15518. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  15519. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  15520. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  15521. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  15522. case UNPC_DEF: nd->status.def = (defType)value; break;
  15523. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  15524. case UNPC_HIT: nd->status.hit = (short)value; break;
  15525. case UNPC_FLEE: nd->status.flee = (short)value; break;
  15526. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  15527. case UNPC_CRIT: nd->status.cri = (short)value; break;
  15528. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  15529. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  15530. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  15531. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  15532. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  15533. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  15534. default:
  15535. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  15536. return SCRIPT_CMD_FAILURE;
  15537. }
  15538. break;
  15539. default:
  15540. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  15541. return SCRIPT_CMD_FAILURE;
  15542. }
  15543. // Client information updates
  15544. switch (bl->type) {
  15545. case BL_HOM:
  15546. clif_send_homdata(hd->master, SP_ACK, 0);
  15547. break;
  15548. case BL_PET:
  15549. clif_send_petstatus(pd->master);
  15550. break;
  15551. case BL_MER:
  15552. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  15553. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  15554. break;
  15555. case BL_ELEM:
  15556. clif_elemental_info(ed->master);
  15557. break;
  15558. }
  15559. return SCRIPT_CMD_SUCCESS;
  15560. }
  15561. /// Gets the name of a bl.
  15562. /// Supported types are [MOB|HOM|PET|NPC].
  15563. /// MER and ELEM don't support custom names.
  15564. ///
  15565. /// getunitname <unit id>;
  15566. BUILDIN_FUNC(getunitname)
  15567. {
  15568. struct block_list* bl = NULL;
  15569. if(!script_rid2bl(2,bl)){
  15570. script_pushconststr(st, "Unknown");
  15571. return SCRIPT_CMD_FAILURE;
  15572. }
  15573. script_pushstrcopy(st, status_get_name(bl));
  15574. return SCRIPT_CMD_SUCCESS;
  15575. }
  15576. /// Changes the name of a bl.
  15577. /// Supported types are [MOB|HOM|PET].
  15578. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  15579. ///
  15580. /// setunitname <unit id>,<name>;
  15581. BUILDIN_FUNC(setunitname)
  15582. {
  15583. struct block_list* bl = NULL;
  15584. TBL_MOB* md = NULL;
  15585. TBL_HOM* hd = NULL;
  15586. TBL_PET* pd = NULL;
  15587. if(!script_rid2bl(2,bl))
  15588. {
  15589. script_pushconststr(st, "Unknown");
  15590. return SCRIPT_CMD_FAILURE;
  15591. }
  15592. switch (bl->type) {
  15593. case BL_MOB: md = map_id2md(bl->id); break;
  15594. case BL_HOM: hd = map_id2hd(bl->id); break;
  15595. case BL_PET: pd = map_id2pd(bl->id); break;
  15596. }
  15597. switch (bl->type) {
  15598. case BL_MOB:
  15599. if (!md) {
  15600. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  15601. return SCRIPT_CMD_FAILURE;
  15602. }
  15603. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  15604. break;
  15605. case BL_HOM:
  15606. if (!hd) {
  15607. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  15608. return SCRIPT_CMD_FAILURE;
  15609. }
  15610. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  15611. break;
  15612. case BL_PET:
  15613. if (!pd) {
  15614. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  15615. return SCRIPT_CMD_FAILURE;
  15616. }
  15617. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  15618. break;
  15619. default:
  15620. ShowWarning("buildin_setunitname: Unknown object type!\n");
  15621. return SCRIPT_CMD_FAILURE;
  15622. }
  15623. clif_name_area(bl); // Send update to client.
  15624. return SCRIPT_CMD_SUCCESS;
  15625. }
  15626. /// Makes the unit walk to target position or map.
  15627. /// Returns if it was successful.
  15628. ///
  15629. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  15630. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  15631. BUILDIN_FUNC(unitwalk)
  15632. {
  15633. struct block_list* bl;
  15634. struct unit_data *ud = NULL;
  15635. const char *cmd = script_getfuncname(st), *done_label = "";
  15636. uint8 off = 5;
  15637. if(!script_rid2bl(2,bl))
  15638. {
  15639. script_pushint(st, 0);
  15640. return SCRIPT_CMD_FAILURE;
  15641. }
  15642. ud = unit_bl2ud(bl);
  15643. if (!strcmp(cmd,"unitwalk")) {
  15644. int x = script_getnum(st,3);
  15645. int y = script_getnum(st,4);
  15646. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  15647. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  15648. } else {
  15649. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  15650. if (!tbl) {
  15651. ShowError("buildin_unitwalk: Bad target destination.\n");
  15652. script_pushint(st, 0);
  15653. return SCRIPT_CMD_FAILURE;
  15654. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  15655. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  15656. off = 4;
  15657. }
  15658. if (ud && script_hasdata(st, off)) {
  15659. done_label = script_getstr(st, off);
  15660. check_event(st, done_label);
  15661. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  15662. }
  15663. return SCRIPT_CMD_SUCCESS;
  15664. }
  15665. /// Kills the unit.
  15666. ///
  15667. /// unitkill <unit_id>;
  15668. BUILDIN_FUNC(unitkill)
  15669. {
  15670. struct block_list* bl;
  15671. if(script_rid2bl(2,bl))
  15672. status_kill(bl);
  15673. return SCRIPT_CMD_SUCCESS;
  15674. }
  15675. /// Warps the unit to the target position in the target map.
  15676. /// Returns if it was successful.
  15677. ///
  15678. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  15679. BUILDIN_FUNC(unitwarp)
  15680. {
  15681. int map_idx;
  15682. short x;
  15683. short y;
  15684. struct block_list* bl;
  15685. const char *mapname;
  15686. mapname = script_getstr(st, 3);
  15687. x = (short)script_getnum(st,4);
  15688. y = (short)script_getnum(st,5);
  15689. if(!script_rid2bl(2,bl))
  15690. {
  15691. script_pushint(st, 0);
  15692. return SCRIPT_CMD_SUCCESS;
  15693. }
  15694. if (!strcmp(mapname,"this"))
  15695. map_idx = bl?bl->m:-1;
  15696. else
  15697. map_idx = map_mapname2mapid(mapname);
  15698. if (map_idx >= 0 && bl != NULL)
  15699. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15700. else
  15701. script_pushint(st, 0);
  15702. return SCRIPT_CMD_SUCCESS;
  15703. }
  15704. /// Makes the unit attack the target.
  15705. /// If the unit is a player and <action type> is not 0, it does a continuous
  15706. /// attack instead of a single attack.
  15707. /// Returns if the request was successful.
  15708. ///
  15709. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15710. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15711. BUILDIN_FUNC(unitattack)
  15712. {
  15713. struct block_list* unit_bl;
  15714. struct block_list* target_bl = NULL;
  15715. struct script_data* data;
  15716. int actiontype = 0;
  15717. if (!script_rid2bl(2,unit_bl)) {
  15718. script_pushint(st, 0);
  15719. return SCRIPT_CMD_FAILURE;
  15720. }
  15721. data = script_getdata(st, 3);
  15722. get_val(st, data);
  15723. if (data_isstring(data)) {
  15724. TBL_PC* sd = map_nick2sd(conv_str(st, data),false);
  15725. if( sd != NULL )
  15726. target_bl = &sd->bl;
  15727. } else
  15728. target_bl = map_id2bl(conv_num(st, data));
  15729. if (!target_bl) {
  15730. script_pushint(st, 0);
  15731. return SCRIPT_CMD_FAILURE;
  15732. }
  15733. if (script_hasdata(st,4))
  15734. actiontype = script_getnum(st,4);
  15735. switch(unit_bl->type) {
  15736. case BL_PC:
  15737. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15738. script_pushint(st, 1);
  15739. return SCRIPT_CMD_SUCCESS;
  15740. case BL_MOB:
  15741. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15742. break;
  15743. case BL_PET:
  15744. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15745. break;
  15746. default:
  15747. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  15748. script_pushint(st, 0);
  15749. return SCRIPT_CMD_FAILURE;
  15750. }
  15751. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  15752. return SCRIPT_CMD_SUCCESS;
  15753. }
  15754. /// Makes the unit stop attacking.
  15755. ///
  15756. /// unitstopattack <unit_id>;
  15757. BUILDIN_FUNC(unitstopattack)
  15758. {
  15759. struct block_list* bl;
  15760. if(script_rid2bl(2,bl))
  15761. {
  15762. unit_stop_attack(bl);
  15763. if (bl->type == BL_MOB)
  15764. ((TBL_MOB*)bl)->target_id = 0;
  15765. }
  15766. return SCRIPT_CMD_SUCCESS;
  15767. }
  15768. /// Makes the unit stop walking.
  15769. ///
  15770. /// unitstopwalk <unit_id>;
  15771. BUILDIN_FUNC(unitstopwalk)
  15772. {
  15773. struct block_list* bl;
  15774. if(script_rid2bl(2,bl))
  15775. unit_stop_walking(bl, 0);
  15776. return SCRIPT_CMD_SUCCESS;
  15777. }
  15778. /// Makes the unit say the given message.
  15779. ///
  15780. /// unittalk <unit_id>,"<message>";
  15781. BUILDIN_FUNC(unittalk)
  15782. {
  15783. const char* message;
  15784. struct block_list* bl;
  15785. message = script_getstr(st, 3);
  15786. if(script_rid2bl(2,bl))
  15787. {
  15788. struct StringBuf sbuf;
  15789. StringBuf_Init(&sbuf);
  15790. StringBuf_Printf(&sbuf, "%s", message);
  15791. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  15792. StringBuf_Destroy(&sbuf);
  15793. }
  15794. return SCRIPT_CMD_SUCCESS;
  15795. }
  15796. /// Makes the unit do an emotion.
  15797. ///
  15798. /// unitemote <unit_id>,<emotion>;
  15799. ///
  15800. /// @see e_* in db/const.txt
  15801. BUILDIN_FUNC(unitemote)
  15802. {
  15803. int emotion;
  15804. struct block_list* bl;
  15805. emotion = script_getnum(st,3);
  15806. if(script_rid2bl(2,bl))
  15807. clif_emotion(bl, emotion);
  15808. return SCRIPT_CMD_SUCCESS;
  15809. }
  15810. /// Makes the unit cast the skill on the target or self if no target is specified.
  15811. ///
  15812. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  15813. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  15814. BUILDIN_FUNC(unitskilluseid)
  15815. {
  15816. int unit_id, target_id, casttime;
  15817. uint16 skill_id, skill_lv;
  15818. struct block_list* bl;
  15819. struct script_data *data;
  15820. unit_id = script_getnum(st,2);
  15821. data = script_getdata(st, 3);
  15822. get_val(st, data); // Convert into value in case of a variable
  15823. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15824. skill_lv = script_getnum(st,4);
  15825. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  15826. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  15827. if(script_rid2bl(2,bl)){
  15828. if (bl->type == BL_NPC) {
  15829. if (!((TBL_NPC*)bl)->status.hp)
  15830. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15831. else
  15832. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15833. }
  15834. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  15835. }
  15836. return SCRIPT_CMD_SUCCESS;
  15837. }
  15838. /// Makes the unit cast the skill on the target position.
  15839. ///
  15840. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15841. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15842. BUILDIN_FUNC(unitskillusepos)
  15843. {
  15844. int skill_x, skill_y, casttime;
  15845. uint16 skill_id, skill_lv;
  15846. struct block_list* bl;
  15847. struct script_data *data;
  15848. data = script_getdata(st, 3);
  15849. get_val(st, data); // Convert into value in case of a variable
  15850. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15851. skill_lv = script_getnum(st,4);
  15852. skill_x = script_getnum(st,5);
  15853. skill_y = script_getnum(st,6);
  15854. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  15855. if(script_rid2bl(2,bl)){
  15856. if (bl->type == BL_NPC) {
  15857. if (!((TBL_NPC*)bl)->status.hp)
  15858. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15859. else
  15860. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15861. }
  15862. 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));
  15863. }
  15864. return SCRIPT_CMD_SUCCESS;
  15865. }
  15866. // <--- [zBuffer] List of unit control commands
  15867. /// Pauses the execution of the script, detaching the player
  15868. ///
  15869. /// sleep <mili seconds>;
  15870. BUILDIN_FUNC(sleep)
  15871. {
  15872. int ticks;
  15873. ticks = script_getnum(st,2);
  15874. // detach the player
  15875. script_detach_rid(st);
  15876. if( ticks <= 0 )
  15877. {// do nothing
  15878. }
  15879. else if( st->sleep.tick == 0 )
  15880. {// sleep for the target amount of time
  15881. st->state = RERUNLINE;
  15882. st->sleep.tick = ticks;
  15883. }
  15884. else
  15885. {// sleep time is over
  15886. st->state = RUN;
  15887. st->sleep.tick = 0;
  15888. }
  15889. return SCRIPT_CMD_SUCCESS;
  15890. }
  15891. /// Pauses the execution of the script, keeping the unit attached
  15892. /// Returns if the unit is still attached
  15893. ///
  15894. /// sleep2(<mili secconds>) -> <bool>
  15895. BUILDIN_FUNC(sleep2)
  15896. {
  15897. int ticks;
  15898. ticks = script_getnum(st,2);
  15899. if( ticks <= 0 )
  15900. {// do nothing
  15901. script_pushint(st, (map_id2bl(st->rid)!=NULL));
  15902. }
  15903. else if( !st->sleep.tick )
  15904. {// sleep for the target amount of time
  15905. st->state = RERUNLINE;
  15906. st->sleep.tick = ticks;
  15907. }
  15908. else
  15909. {// sleep time is over
  15910. st->state = RUN;
  15911. st->sleep.tick = 0;
  15912. script_pushint(st, (map_id2bl(st->rid)!=NULL));
  15913. }
  15914. return SCRIPT_CMD_SUCCESS;
  15915. }
  15916. /// Awakes all the sleep timers of the target npc
  15917. ///
  15918. /// awake "<npc name>";
  15919. BUILDIN_FUNC(awake)
  15920. {
  15921. DBIterator *iter;
  15922. struct script_state *tst;
  15923. struct npc_data* nd;
  15924. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  15925. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  15926. return SCRIPT_CMD_FAILURE;
  15927. }
  15928. iter = db_iterator(st_db);
  15929. for (tst = dbi_first(iter); dbi_exists(iter); tst = dbi_next(iter)) {
  15930. if (tst->oid == nd->bl.id) {
  15931. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  15932. continue;
  15933. }
  15934. delete_timer(tst->sleep.timer, run_script_timer);
  15935. // Trigger the timer function
  15936. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  15937. }
  15938. }
  15939. dbi_destroy(iter);
  15940. return SCRIPT_CMD_SUCCESS;
  15941. }
  15942. /// Returns a reference to a variable of the target NPC.
  15943. /// Returns 0 if an error occurs.
  15944. ///
  15945. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  15946. BUILDIN_FUNC(getvariableofnpc)
  15947. {
  15948. struct script_data* data;
  15949. const char* name;
  15950. struct npc_data* nd;
  15951. data = script_getdata(st,2);
  15952. if( !data_isreference(data) )
  15953. {// Not a reference (aka varaible name)
  15954. ShowError("buildin_getvariableofnpc: not a variable\n");
  15955. script_reportdata(data);
  15956. script_pushnil(st);
  15957. st->state = END;
  15958. return SCRIPT_CMD_FAILURE;
  15959. }
  15960. name = reference_getname(data);
  15961. if( *name != '.' || name[1] == '@' )
  15962. {// not a npc variable
  15963. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  15964. script_reportdata(data);
  15965. script_pushnil(st);
  15966. st->state = END;
  15967. return SCRIPT_CMD_FAILURE;
  15968. }
  15969. nd = npc_name2id(script_getstr(st,3));
  15970. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  15971. {// NPC not found or has no script
  15972. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  15973. script_pushnil(st);
  15974. st->state = END;
  15975. return SCRIPT_CMD_FAILURE;
  15976. }
  15977. if (!nd->u.scr.script->local.vars)
  15978. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  15979. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  15980. return SCRIPT_CMD_SUCCESS;
  15981. }
  15982. /// Opens a warp portal.
  15983. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  15984. ///
  15985. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  15986. ///
  15987. /// @author blackhole89
  15988. BUILDIN_FUNC(warpportal)
  15989. {
  15990. int spx;
  15991. int spy;
  15992. unsigned short mapindex;
  15993. int tpx;
  15994. int tpy;
  15995. struct skill_unit_group* group;
  15996. struct block_list* bl;
  15997. bl = map_id2bl(st->oid);
  15998. if( bl == NULL ) {
  15999. ShowError("buildin_warpportal: NPC is needed\n");
  16000. return SCRIPT_CMD_FAILURE;
  16001. }
  16002. spx = script_getnum(st,2);
  16003. spy = script_getnum(st,3);
  16004. mapindex = mapindex_name2id(script_getstr(st, 4));
  16005. tpx = script_getnum(st,5);
  16006. tpy = script_getnum(st,6);
  16007. if( mapindex == 0 ) {
  16008. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  16009. return SCRIPT_CMD_FAILURE;
  16010. }
  16011. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  16012. if( group == NULL )
  16013. return SCRIPT_CMD_FAILURE;// failed
  16014. group->val1 = (group->val1<<16)|(short)0;
  16015. group->val2 = (tpx<<16) | tpy;
  16016. group->val3 = mapindex;
  16017. return SCRIPT_CMD_SUCCESS;
  16018. }
  16019. /**
  16020. * openmail({<char_id>});
  16021. **/
  16022. BUILDIN_FUNC(openmail)
  16023. {
  16024. TBL_PC* sd;
  16025. if (!script_charid2sd(2,sd))
  16026. return SCRIPT_CMD_FAILURE;
  16027. mail_openmail(sd);
  16028. return SCRIPT_CMD_SUCCESS;
  16029. }
  16030. /**
  16031. * openauction({<char_id>});
  16032. **/
  16033. BUILDIN_FUNC(openauction)
  16034. {
  16035. TBL_PC* sd;
  16036. if (!script_charid2sd(2,sd))
  16037. return SCRIPT_CMD_FAILURE;
  16038. if( !battle_config.feature_auction ) {
  16039. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  16040. return SCRIPT_CMD_SUCCESS;
  16041. }
  16042. clif_Auction_openwindow(sd);
  16043. return SCRIPT_CMD_SUCCESS;
  16044. }
  16045. /// Retrieves the value of the specified flag of the specified cell.
  16046. ///
  16047. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  16048. ///
  16049. /// @see cell_chk* constants in const.txt for the types
  16050. BUILDIN_FUNC(checkcell)
  16051. {
  16052. int16 m = map_mapname2mapid(script_getstr(st,2));
  16053. int16 x = script_getnum(st,3);
  16054. int16 y = script_getnum(st,4);
  16055. cell_chk type = (cell_chk)script_getnum(st,5);
  16056. script_pushint(st, map_getcell(m, x, y, type));
  16057. return SCRIPT_CMD_SUCCESS;
  16058. }
  16059. /// Modifies flags of cells in the specified area.
  16060. ///
  16061. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  16062. ///
  16063. /// @see cell_* constants in const.txt for the types
  16064. BUILDIN_FUNC(setcell)
  16065. {
  16066. int16 m = map_mapname2mapid(script_getstr(st,2));
  16067. int16 x1 = script_getnum(st,3);
  16068. int16 y1 = script_getnum(st,4);
  16069. int16 x2 = script_getnum(st,5);
  16070. int16 y2 = script_getnum(st,6);
  16071. cell_t type = (cell_t)script_getnum(st,7);
  16072. bool flag = (bool)script_getnum(st,8);
  16073. int x,y;
  16074. if( x1 > x2 ) swap(x1,x2);
  16075. if( y1 > y2 ) swap(y1,y2);
  16076. for( y = y1; y <= y2; ++y )
  16077. for( x = x1; x <= x2; ++x )
  16078. map_setcell(m, x, y, type, flag);
  16079. return SCRIPT_CMD_SUCCESS;
  16080. }
  16081. /*==========================================
  16082. * Mercenary Commands
  16083. *------------------------------------------*/
  16084. BUILDIN_FUNC(mercenary_create)
  16085. {
  16086. struct map_session_data *sd;
  16087. int class_, contract_time;
  16088. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  16089. return SCRIPT_CMD_SUCCESS;
  16090. class_ = script_getnum(st,2);
  16091. if( !mercenary_class(class_) )
  16092. return SCRIPT_CMD_SUCCESS;
  16093. contract_time = script_getnum(st,3);
  16094. mercenary_create(sd, class_, contract_time);
  16095. return SCRIPT_CMD_SUCCESS;
  16096. }
  16097. BUILDIN_FUNC(mercenary_heal)
  16098. {
  16099. struct map_session_data *sd = script_rid2sd(st);
  16100. int hp, sp;
  16101. if( sd == NULL || sd->md == NULL )
  16102. return SCRIPT_CMD_SUCCESS;
  16103. hp = script_getnum(st,2);
  16104. sp = script_getnum(st,3);
  16105. status_heal(&sd->md->bl, hp, sp, 0);
  16106. return SCRIPT_CMD_SUCCESS;
  16107. }
  16108. BUILDIN_FUNC(mercenary_sc_start)
  16109. {
  16110. struct map_session_data *sd = script_rid2sd(st);
  16111. enum sc_type type;
  16112. int tick, val1;
  16113. if( sd == NULL || sd->md == NULL )
  16114. return SCRIPT_CMD_SUCCESS;
  16115. type = (sc_type)script_getnum(st,2);
  16116. tick = script_getnum(st,3);
  16117. val1 = script_getnum(st,4);
  16118. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  16119. return SCRIPT_CMD_SUCCESS;
  16120. }
  16121. BUILDIN_FUNC(mercenary_get_calls)
  16122. {
  16123. struct map_session_data *sd = script_rid2sd(st);
  16124. int guild;
  16125. if( sd == NULL )
  16126. return SCRIPT_CMD_SUCCESS;
  16127. guild = script_getnum(st,2);
  16128. switch( guild )
  16129. {
  16130. case ARCH_MERC_GUILD:
  16131. script_pushint(st,sd->status.arch_calls);
  16132. break;
  16133. case SPEAR_MERC_GUILD:
  16134. script_pushint(st,sd->status.spear_calls);
  16135. break;
  16136. case SWORD_MERC_GUILD:
  16137. script_pushint(st,sd->status.sword_calls);
  16138. break;
  16139. default:
  16140. script_pushint(st,0);
  16141. break;
  16142. }
  16143. return SCRIPT_CMD_SUCCESS;
  16144. }
  16145. BUILDIN_FUNC(mercenary_set_calls)
  16146. {
  16147. struct map_session_data *sd = script_rid2sd(st);
  16148. int guild, value, *calls;
  16149. if( sd == NULL )
  16150. return SCRIPT_CMD_SUCCESS;
  16151. guild = script_getnum(st,2);
  16152. value = script_getnum(st,3);
  16153. switch( guild )
  16154. {
  16155. case ARCH_MERC_GUILD:
  16156. calls = &sd->status.arch_calls;
  16157. break;
  16158. case SPEAR_MERC_GUILD:
  16159. calls = &sd->status.spear_calls;
  16160. break;
  16161. case SWORD_MERC_GUILD:
  16162. calls = &sd->status.sword_calls;
  16163. break;
  16164. default:
  16165. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  16166. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16167. }
  16168. *calls += value;
  16169. *calls = cap_value(*calls, 0, INT_MAX);
  16170. return SCRIPT_CMD_SUCCESS;
  16171. }
  16172. BUILDIN_FUNC(mercenary_get_faith)
  16173. {
  16174. struct map_session_data *sd = script_rid2sd(st);
  16175. int guild;
  16176. if( sd == NULL )
  16177. return SCRIPT_CMD_SUCCESS;
  16178. guild = script_getnum(st,2);
  16179. switch( guild )
  16180. {
  16181. case ARCH_MERC_GUILD:
  16182. script_pushint(st,sd->status.arch_faith);
  16183. break;
  16184. case SPEAR_MERC_GUILD:
  16185. script_pushint(st,sd->status.spear_faith);
  16186. break;
  16187. case SWORD_MERC_GUILD:
  16188. script_pushint(st,sd->status.sword_faith);
  16189. break;
  16190. default:
  16191. script_pushint(st,0);
  16192. break;
  16193. }
  16194. return SCRIPT_CMD_SUCCESS;
  16195. }
  16196. BUILDIN_FUNC(mercenary_set_faith)
  16197. {
  16198. struct map_session_data *sd = script_rid2sd(st);
  16199. int guild, value, *calls;
  16200. if( sd == NULL )
  16201. return SCRIPT_CMD_SUCCESS;
  16202. guild = script_getnum(st,2);
  16203. value = script_getnum(st,3);
  16204. switch( guild )
  16205. {
  16206. case ARCH_MERC_GUILD:
  16207. calls = &sd->status.arch_faith;
  16208. break;
  16209. case SPEAR_MERC_GUILD:
  16210. calls = &sd->status.spear_faith;
  16211. break;
  16212. case SWORD_MERC_GUILD:
  16213. calls = &sd->status.sword_faith;
  16214. break;
  16215. default:
  16216. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  16217. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16218. }
  16219. *calls += value;
  16220. *calls = cap_value(*calls, 0, INT_MAX);
  16221. if( mercenary_get_guild(sd->md) == guild )
  16222. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  16223. return SCRIPT_CMD_SUCCESS;
  16224. }
  16225. /*------------------------------------------
  16226. * Book Reading
  16227. *------------------------------------------*/
  16228. BUILDIN_FUNC(readbook)
  16229. {
  16230. struct map_session_data *sd;
  16231. int book_id, page;
  16232. if( (sd = script_rid2sd(st)) == NULL )
  16233. return SCRIPT_CMD_SUCCESS;
  16234. book_id = script_getnum(st,2);
  16235. page = script_getnum(st,3);
  16236. clif_readbook(sd->fd, book_id, page);
  16237. return SCRIPT_CMD_SUCCESS;
  16238. }
  16239. /******************
  16240. Questlog script commands
  16241. *******************/
  16242. /**
  16243. * Add job criteria to questinfo
  16244. * @param qi Quest Info
  16245. * @param job
  16246. * @author [Cydh]
  16247. **/
  16248. static void buildin_questinfo_setjob(struct questinfo *qi, int job) {
  16249. RECREATE(qi->jobid, unsigned short, qi->jobid_count+1);
  16250. qi->jobid[qi->jobid_count++] = job;
  16251. }
  16252. /**
  16253. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  16254. **/
  16255. BUILDIN_FUNC(questinfo)
  16256. {
  16257. TBL_NPC* nd = map_id2nd(st->oid);
  16258. int quest_id, icon;
  16259. struct questinfo qi, *q2;
  16260. if( nd == NULL || nd->bl.m == -1 ) {
  16261. ShowError("buildin_questinfo: No NPC attached.\n");
  16262. return SCRIPT_CMD_FAILURE;
  16263. }
  16264. quest_id = script_getnum(st, 2);
  16265. icon = script_getnum(st, 3);
  16266. #if PACKETVER >= 20120410
  16267. switch(icon){
  16268. case QTYPE_QUEST:
  16269. case QTYPE_QUEST2:
  16270. case QTYPE_JOB:
  16271. case QTYPE_JOB2:
  16272. case QTYPE_EVENT:
  16273. case QTYPE_EVENT2:
  16274. case QTYPE_WARG:
  16275. case QTYPE_WARG2:
  16276. // Leave everything as it is
  16277. break;
  16278. case QTYPE_NONE:
  16279. default:
  16280. // Default to nothing if icon id is invalid.
  16281. icon = QTYPE_NONE;
  16282. break;
  16283. }
  16284. #else
  16285. if(icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  16286. icon = QTYPE_QUEST;
  16287. else
  16288. icon = icon + 1;
  16289. #endif
  16290. qi.quest_id = quest_id;
  16291. qi.icon = (unsigned char)icon;
  16292. qi.nd = nd;
  16293. if( script_hasdata(st, 4) ) {
  16294. int color = script_getnum(st, 4);
  16295. if( color < 0 || color > 3 ) {
  16296. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  16297. script_reportfunc(st);
  16298. color = 0;
  16299. }
  16300. qi.color = (unsigned char)color;
  16301. }
  16302. qi.min_level = 1;
  16303. qi.max_level = MAX_LEVEL;
  16304. q2 = map_add_questinfo(nd->bl.m, &qi);
  16305. q2->req = NULL;
  16306. q2->req_count = 0;
  16307. q2->jobid = NULL;
  16308. q2->jobid_count = 0;
  16309. if(script_hasdata(st, 5)) {
  16310. int job = script_getnum(st, 5);
  16311. if (!pcdb_checkid(job))
  16312. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  16313. else {
  16314. buildin_questinfo_setjob(q2, job);
  16315. }
  16316. }
  16317. return SCRIPT_CMD_SUCCESS;
  16318. }
  16319. /**
  16320. * setquest <ID>{,<char_id>};
  16321. **/
  16322. BUILDIN_FUNC(setquest)
  16323. {
  16324. struct map_session_data *sd;
  16325. int quest_id;
  16326. quest_id = script_getnum(st, 2);
  16327. if (!script_charid2sd(3,sd))
  16328. return SCRIPT_CMD_FAILURE;
  16329. if( quest_add(sd, quest_id) == -1 ){
  16330. script_reportsrc(st);
  16331. script_reportfunc(st);
  16332. }
  16333. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  16334. pc_show_questinfo(sd);
  16335. return SCRIPT_CMD_SUCCESS;
  16336. }
  16337. /**
  16338. * erasequest <ID>{,<char_id>};
  16339. **/
  16340. BUILDIN_FUNC(erasequest)
  16341. {
  16342. struct map_session_data *sd;
  16343. if (!script_charid2sd(3,sd))
  16344. return SCRIPT_CMD_FAILURE;
  16345. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  16346. script_reportsrc(st);
  16347. script_reportfunc(st);
  16348. }
  16349. return SCRIPT_CMD_SUCCESS;
  16350. }
  16351. /**
  16352. * completequest <ID>{,<char_id>};
  16353. **/
  16354. BUILDIN_FUNC(completequest)
  16355. {
  16356. struct map_session_data *sd;
  16357. if (!script_charid2sd(3,sd))
  16358. return SCRIPT_CMD_FAILURE;
  16359. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  16360. //20120410 or 20090218
  16361. pc_show_questinfo(sd);
  16362. return SCRIPT_CMD_SUCCESS;
  16363. }
  16364. /**
  16365. * changequest <ID>,<ID2>{,<char_id>};
  16366. **/
  16367. BUILDIN_FUNC(changequest)
  16368. {
  16369. struct map_session_data *sd;
  16370. if (!script_charid2sd(4,sd))
  16371. return SCRIPT_CMD_FAILURE;
  16372. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  16373. script_reportsrc(st);
  16374. script_reportfunc(st);
  16375. }
  16376. //20120410 or 20090218
  16377. pc_show_questinfo(sd);
  16378. return SCRIPT_CMD_SUCCESS;
  16379. }
  16380. /**
  16381. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  16382. **/
  16383. BUILDIN_FUNC(checkquest)
  16384. {
  16385. struct map_session_data *sd;
  16386. enum quest_check_type type = HAVEQUEST;
  16387. if( script_hasdata(st, 3) )
  16388. type = (enum quest_check_type)script_getnum(st, 3);
  16389. if (!script_charid2sd(4,sd))
  16390. return SCRIPT_CMD_FAILURE;
  16391. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  16392. return SCRIPT_CMD_SUCCESS;
  16393. }
  16394. /**
  16395. * isbegin_quest(<ID>{,<char_id>})
  16396. **/
  16397. BUILDIN_FUNC(isbegin_quest)
  16398. {
  16399. struct map_session_data *sd;
  16400. int i;
  16401. if (!script_charid2sd(3,sd))
  16402. return SCRIPT_CMD_FAILURE;
  16403. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  16404. script_pushint(st, i + (i < 1));
  16405. return SCRIPT_CMD_SUCCESS;
  16406. }
  16407. /**
  16408. * showevent <icon>{,<mark color>{,<char_id>}}
  16409. **/
  16410. BUILDIN_FUNC(showevent)
  16411. {
  16412. TBL_PC *sd;
  16413. struct npc_data *nd = map_id2nd(st->oid);
  16414. int icon, color = 0;
  16415. if (!script_charid2sd(4,sd))
  16416. return SCRIPT_CMD_FAILURE;
  16417. if( sd == NULL || nd == NULL )
  16418. return SCRIPT_CMD_SUCCESS;
  16419. icon = script_getnum(st, 2);
  16420. if( script_hasdata(st, 3) ) {
  16421. color = script_getnum(st, 3);
  16422. if( color < 0 || color > 3 ) {
  16423. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  16424. script_reportfunc(st);
  16425. color = 0;
  16426. }
  16427. }
  16428. #if PACKETVER >= 20120410
  16429. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  16430. icon = 9999; // Default to nothing if icon id is invalid.
  16431. #else
  16432. if(icon < 0 || icon > 7)
  16433. icon = 0;
  16434. else
  16435. icon = icon + 1;
  16436. #endif
  16437. clif_quest_show_event(sd, &nd->bl, icon, color);
  16438. return SCRIPT_CMD_SUCCESS;
  16439. }
  16440. /*==========================================
  16441. * BattleGround System
  16442. *------------------------------------------*/
  16443. BUILDIN_FUNC(waitingroom2bg)
  16444. {
  16445. struct npc_data *nd;
  16446. struct chat_data *cd;
  16447. const char *map_name, *ev = "", *dev = "";
  16448. int x, y, mapindex = 0, bg_id;
  16449. unsigned char i,c=0;
  16450. if( script_hasdata(st,7) )
  16451. nd = npc_name2id(script_getstr(st,7));
  16452. else
  16453. nd = (struct npc_data *)map_id2bl(st->oid);
  16454. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  16455. {
  16456. script_pushint(st,0);
  16457. return SCRIPT_CMD_SUCCESS;
  16458. }
  16459. map_name = script_getstr(st,2);
  16460. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16461. { // Invalid Map
  16462. script_pushint(st, 0);
  16463. return SCRIPT_CMD_SUCCESS;
  16464. }
  16465. x = script_getnum(st,3);
  16466. y = script_getnum(st,4);
  16467. if(script_hasdata(st,5))
  16468. ev = script_getstr(st,5); // Logout Event
  16469. if(script_hasdata(st,6))
  16470. dev = script_getstr(st,6); // Die Event
  16471. check_event(st, ev);
  16472. check_event(st, dev);
  16473. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  16474. { // Creation failed
  16475. script_pushint(st,0);
  16476. return SCRIPT_CMD_SUCCESS;
  16477. }
  16478. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  16479. struct map_session_data *sd;
  16480. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ){
  16481. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  16482. ++c;
  16483. }
  16484. }
  16485. mapreg_setreg(add_str("$@arenamembersnum"), c);
  16486. script_pushint(st,bg_id);
  16487. return SCRIPT_CMD_SUCCESS;
  16488. }
  16489. BUILDIN_FUNC(waitingroom2bg_single)
  16490. {
  16491. const char* map_name;
  16492. struct npc_data *nd;
  16493. struct chat_data *cd;
  16494. struct map_session_data *sd;
  16495. struct battleground_data *bg;
  16496. int x, y, mapindex, bg_id;
  16497. bg_id = script_getnum(st,2);
  16498. if ((bg = bg_team_search(bg_id)) == NULL) {
  16499. script_pushint(st, false);
  16500. return SCRIPT_CMD_SUCCESS;
  16501. }
  16502. if (script_hasdata(st, 3)) {
  16503. map_name = script_getstr(st, 3);
  16504. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16505. script_pushint(st, false);
  16506. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16507. }
  16508. x = script_getnum(st, 4);
  16509. y = script_getnum(st, 5);
  16510. }
  16511. else {
  16512. mapindex = bg->mapindex;
  16513. x = bg->x;
  16514. y = bg->y;
  16515. }
  16516. nd = npc_name2id(script_getstr(st,6));
  16517. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  16518. return SCRIPT_CMD_SUCCESS;
  16519. if( (sd = cd->usersd[0]) == NULL )
  16520. return SCRIPT_CMD_SUCCESS;
  16521. if( bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16522. {
  16523. script_pushint(st, true);
  16524. }
  16525. else
  16526. script_pushint(st, false);
  16527. return SCRIPT_CMD_SUCCESS;
  16528. }
  16529. /// Creates an instance of battleground battle group.
  16530. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  16531. /// @author [secretdataz]
  16532. BUILDIN_FUNC(bg_create) {
  16533. const char *map_name, *ev = "", *dev = "";
  16534. int x, y, mapindex = 0;
  16535. map_name = script_getstr(st, 2);
  16536. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16537. { // Invalid Map
  16538. script_pushint(st, 0);
  16539. return SCRIPT_CMD_SUCCESS;
  16540. }
  16541. x = script_getnum(st, 3);
  16542. y = script_getnum(st, 4);
  16543. if(script_hasdata(st, 5))
  16544. ev = script_getstr(st, 5); // Logout Event
  16545. if(script_hasdata(st, 6))
  16546. dev = script_getstr(st, 6); // Die Event
  16547. check_event(st, ev);
  16548. check_event(st, dev);
  16549. script_pushint(st, bg_create(mapindex, x, y, ev, dev));
  16550. return SCRIPT_CMD_SUCCESS;
  16551. }
  16552. /// Adds attached player or <char id> (if specified) to an existing
  16553. /// battleground group and warps it to the specified coordinates on
  16554. /// the given map.
  16555. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  16556. /// @author [secretdataz]
  16557. BUILDIN_FUNC(bg_join) {
  16558. const char* map_name;
  16559. struct map_session_data *sd;
  16560. struct battleground_data *bg;
  16561. int x, y, bg_id, mapindex;
  16562. bg_id = script_getnum(st, 2);
  16563. if ((bg = bg_team_search(bg_id)) == NULL) {
  16564. script_pushint(st, false);
  16565. return SCRIPT_CMD_SUCCESS;
  16566. }
  16567. if (script_hasdata(st, 3)) {
  16568. map_name = script_getstr(st, 3);
  16569. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16570. script_pushint(st, false);
  16571. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16572. }
  16573. x = script_getnum(st, 4);
  16574. y = script_getnum(st, 5);
  16575. } else {
  16576. mapindex = bg->mapindex;
  16577. x = bg->x;
  16578. y = bg->y;
  16579. }
  16580. if (!script_charid2sd(6, sd)) {
  16581. script_pushint(st, false);
  16582. return SCRIPT_CMD_FAILURE;
  16583. }
  16584. if (bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16585. {
  16586. script_pushint(st, true);
  16587. }
  16588. else
  16589. script_pushint(st, false);
  16590. return SCRIPT_CMD_SUCCESS;
  16591. }
  16592. BUILDIN_FUNC(bg_team_setxy)
  16593. {
  16594. struct battleground_data *bg;
  16595. int bg_id;
  16596. bg_id = script_getnum(st,2);
  16597. if( (bg = bg_team_search(bg_id)) == NULL )
  16598. return SCRIPT_CMD_SUCCESS;
  16599. bg->x = script_getnum(st,3);
  16600. bg->y = script_getnum(st,4);
  16601. return SCRIPT_CMD_SUCCESS;
  16602. }
  16603. BUILDIN_FUNC(bg_warp)
  16604. {
  16605. int x, y, mapindex, bg_id;
  16606. const char* map_name;
  16607. bg_id = script_getnum(st,2);
  16608. map_name = script_getstr(st,3);
  16609. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16610. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16611. x = script_getnum(st,4);
  16612. y = script_getnum(st,5);
  16613. bg_team_warp(bg_id, mapindex, x, y);
  16614. return SCRIPT_CMD_SUCCESS;
  16615. }
  16616. BUILDIN_FUNC(bg_monster)
  16617. {
  16618. int class_ = 0, x = 0, y = 0, bg_id = 0;
  16619. const char *str,*mapname, *evt="";
  16620. bg_id = script_getnum(st,2);
  16621. mapname = script_getstr(st,3);
  16622. x = script_getnum(st,4);
  16623. y = script_getnum(st,5);
  16624. str = script_getstr(st,6);
  16625. class_ = script_getnum(st,7);
  16626. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  16627. check_event(st, evt);
  16628. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  16629. return SCRIPT_CMD_SUCCESS;
  16630. }
  16631. BUILDIN_FUNC(bg_monster_set_team)
  16632. {
  16633. struct mob_data *md;
  16634. struct block_list *mbl;
  16635. int id = script_getnum(st,2),
  16636. bg_id = script_getnum(st,3);
  16637. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  16638. return SCRIPT_CMD_SUCCESS;
  16639. md = (TBL_MOB *)mbl;
  16640. md->bg_id = bg_id;
  16641. mob_stop_attack(md);
  16642. mob_stop_walking(md, 0);
  16643. md->target_id = md->attacked_id = 0;
  16644. clif_name_area(&md->bl);
  16645. return SCRIPT_CMD_SUCCESS;
  16646. }
  16647. BUILDIN_FUNC(bg_leave)
  16648. {
  16649. struct map_session_data *sd = NULL;
  16650. if( !script_charid2sd(2,sd) || !sd->bg_id )
  16651. return SCRIPT_CMD_SUCCESS;
  16652. bg_team_leave(sd,0);
  16653. return SCRIPT_CMD_SUCCESS;
  16654. }
  16655. BUILDIN_FUNC(bg_destroy)
  16656. {
  16657. int bg_id = script_getnum(st,2);
  16658. bg_team_delete(bg_id);
  16659. return SCRIPT_CMD_SUCCESS;
  16660. }
  16661. BUILDIN_FUNC(bg_getareausers)
  16662. {
  16663. const char *str;
  16664. int16 m, x0, y0, x1, y1;
  16665. int bg_id;
  16666. int i = 0, c = 0;
  16667. struct battleground_data *bg = NULL;
  16668. bg_id = script_getnum(st,2);
  16669. str = script_getstr(st,3);
  16670. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  16671. {
  16672. script_pushint(st,0);
  16673. return SCRIPT_CMD_SUCCESS;
  16674. }
  16675. x0 = script_getnum(st,4);
  16676. y0 = script_getnum(st,5);
  16677. x1 = script_getnum(st,6);
  16678. y1 = script_getnum(st,7);
  16679. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  16680. {
  16681. struct map_session_data *sd;
  16682. if( (sd = bg->members[i].sd) == NULL )
  16683. continue;
  16684. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  16685. continue;
  16686. c++;
  16687. }
  16688. script_pushint(st,c);
  16689. return SCRIPT_CMD_SUCCESS;
  16690. }
  16691. BUILDIN_FUNC(bg_updatescore)
  16692. {
  16693. const char *str;
  16694. int16 m;
  16695. str = script_getstr(st,2);
  16696. if( (m = map_mapname2mapid(str)) < 0 )
  16697. return SCRIPT_CMD_SUCCESS;
  16698. map[m].bgscore_lion = script_getnum(st,3);
  16699. map[m].bgscore_eagle = script_getnum(st,4);
  16700. clif_bg_updatescore(m);
  16701. return SCRIPT_CMD_SUCCESS;
  16702. }
  16703. BUILDIN_FUNC(bg_get_data)
  16704. {
  16705. struct battleground_data *bg;
  16706. int bg_id = script_getnum(st,2),
  16707. type = script_getnum(st,3);
  16708. if( (bg = bg_team_search(bg_id)) == NULL )
  16709. {
  16710. script_pushint(st,0);
  16711. return SCRIPT_CMD_SUCCESS;
  16712. }
  16713. switch( type )
  16714. {
  16715. case 0: script_pushint(st, bg->count); break;
  16716. default:
  16717. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  16718. break;
  16719. }
  16720. return SCRIPT_CMD_SUCCESS;
  16721. }
  16722. /*==========================================
  16723. * Instancing System
  16724. *------------------------------------------*/
  16725. //Returns an Instance ID
  16726. //Checks NPC first, then if player is attached we check
  16727. unsigned short script_instancegetid(struct script_state* st)
  16728. {
  16729. unsigned short instance_id = 0;
  16730. struct npc_data *nd;
  16731. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  16732. instance_id = nd->instance_id;
  16733. else {
  16734. struct map_session_data *sd = NULL;
  16735. struct party_data *p = NULL;
  16736. struct guild *g = NULL;
  16737. if ((sd = script_rid2sd(st)) != NULL) {
  16738. if (sd->instance_id)
  16739. instance_id = sd->instance_id;
  16740. if (instance_id == 0 && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id)
  16741. instance_id = p->instance_id;
  16742. if (instance_id == 0 && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
  16743. instance_id = g->instance_id;
  16744. }
  16745. }
  16746. return instance_id;
  16747. }
  16748. /*==========================================
  16749. * Creates the instance
  16750. * Returns Instance ID if created successfully
  16751. *------------------------------------------*/
  16752. BUILDIN_FUNC(instance_create)
  16753. {
  16754. enum instance_mode mode = IM_PARTY;
  16755. int owner_id = 0;
  16756. if (script_hasdata(st, 3)) {
  16757. mode = script_getnum(st, 3);
  16758. if (mode < IM_NONE || mode >= IM_MAX) {
  16759. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  16760. return SCRIPT_CMD_FAILURE;
  16761. }
  16762. }
  16763. if (script_hasdata(st, 4))
  16764. owner_id = script_getnum(st, 4);
  16765. else {
  16766. struct map_session_data *sd = NULL;
  16767. sd = script_rid2sd(st); // If sd is NULL, instance_create will return -2.
  16768. switch(mode) {
  16769. case IM_NONE:
  16770. owner_id = st->oid;
  16771. break;
  16772. case IM_CHAR:
  16773. if (sd)
  16774. owner_id = sd->status.char_id;
  16775. break;
  16776. case IM_PARTY:
  16777. if (sd)
  16778. owner_id = sd->status.party_id;
  16779. break;
  16780. case IM_GUILD:
  16781. if (sd)
  16782. owner_id = sd->status.guild_id;
  16783. break;
  16784. default:
  16785. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  16786. return SCRIPT_CMD_FAILURE;
  16787. }
  16788. }
  16789. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  16790. return SCRIPT_CMD_SUCCESS;
  16791. }
  16792. /*==========================================
  16793. * Destroys an instance (unofficial)
  16794. * Officially instances are only destroyed by timeout
  16795. *
  16796. * instance_destroy {<instance_id>};
  16797. *------------------------------------------*/
  16798. BUILDIN_FUNC(instance_destroy)
  16799. {
  16800. unsigned short instance_id;
  16801. if( script_hasdata(st,2) )
  16802. instance_id = script_getnum(st,2);
  16803. else
  16804. instance_id = script_instancegetid(st);
  16805. if( instance_id == 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  16806. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %hu.\n", instance_id);
  16807. return SCRIPT_CMD_FAILURE;
  16808. }
  16809. instance_destroy(instance_id);
  16810. return SCRIPT_CMD_SUCCESS;
  16811. }
  16812. /*==========================================
  16813. * Warps player to instance
  16814. * Results:
  16815. * IE_OK: Success
  16816. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  16817. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  16818. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  16819. *------------------------------------------*/
  16820. BUILDIN_FUNC(instance_enter)
  16821. {
  16822. struct map_session_data *sd = NULL;
  16823. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  16824. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  16825. unsigned short instance_id;
  16826. if (script_hasdata(st, 6))
  16827. instance_id = script_getnum(st, 6);
  16828. else
  16829. instance_id = script_instancegetid(st);
  16830. if (!script_charid2sd(5,sd))
  16831. return SCRIPT_CMD_FAILURE;
  16832. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  16833. return SCRIPT_CMD_SUCCESS;
  16834. }
  16835. /*==========================================
  16836. * Returns the name of a duplicated NPC
  16837. *
  16838. * instance_npcname <npc_name>{,<instance_id>};
  16839. * <npc_name> is the full name of an NPC.
  16840. *------------------------------------------*/
  16841. BUILDIN_FUNC(instance_npcname)
  16842. {
  16843. const char *str;
  16844. unsigned short instance_id = 0;
  16845. struct npc_data *nd;
  16846. str = script_getstr(st,2);
  16847. if( script_hasdata(st,3) )
  16848. instance_id = script_getnum(st,3);
  16849. else
  16850. instance_id = script_instancegetid(st);
  16851. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  16852. static char npcname[NAME_LENGTH];
  16853. snprintf(npcname, sizeof(npcname), "dup_%hu_%d", instance_id, nd->bl.id);
  16854. script_pushconststr(st,npcname);
  16855. } else {
  16856. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %hu, NPC name: \"%s\".)\n", instance_id, str);
  16857. st->state = END;
  16858. return SCRIPT_CMD_FAILURE;
  16859. }
  16860. return SCRIPT_CMD_SUCCESS;
  16861. }
  16862. /*==========================================
  16863. * Returns the name of a duplicated map
  16864. *
  16865. * instance_mapname <map_name>{,<instance_id>};
  16866. *------------------------------------------*/
  16867. BUILDIN_FUNC(instance_mapname)
  16868. {
  16869. const char *str;
  16870. int16 m;
  16871. unsigned short instance_id = 0;
  16872. str = script_getstr(st,2);
  16873. if( script_hasdata(st,3) )
  16874. instance_id = script_getnum(st,3);
  16875. else
  16876. instance_id = script_instancegetid(st);
  16877. // Check that instance mapname is a valid map
  16878. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  16879. script_pushconststr(st, "");
  16880. else
  16881. script_pushconststr(st, map[m].name);
  16882. return SCRIPT_CMD_SUCCESS;
  16883. }
  16884. /*==========================================
  16885. * Returns an Instance ID
  16886. *------------------------------------------*/
  16887. BUILDIN_FUNC(instance_id)
  16888. {
  16889. script_pushint(st, script_instancegetid(st));
  16890. return SCRIPT_CMD_SUCCESS;
  16891. }
  16892. /*==========================================
  16893. * Warps all players inside an instance
  16894. *
  16895. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  16896. *------------------------------------------*/
  16897. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  16898. {
  16899. unsigned int m = va_arg(ap,unsigned int);
  16900. int x = va_arg(ap,int);
  16901. int y = va_arg(ap,int);
  16902. unsigned short instance_id = va_arg(ap,unsigned int);
  16903. struct map_session_data *sd = NULL;
  16904. int owner_id = 0;
  16905. nullpo_retr(0, bl);
  16906. if (bl->type != BL_PC)
  16907. return 0;
  16908. sd = (TBL_PC *)bl;
  16909. owner_id = instance_data[instance_id].owner_id;
  16910. switch(instance_data[instance_id].mode) {
  16911. case IM_NONE:
  16912. break;
  16913. case IM_CHAR:
  16914. if (sd->status.char_id != owner_id)
  16915. return 0;
  16916. break;
  16917. case IM_PARTY:
  16918. if (sd->status.party_id != owner_id)
  16919. return 0;
  16920. break;
  16921. case IM_GUILD:
  16922. if (sd->status.guild_id != owner_id)
  16923. return 0;
  16924. }
  16925. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  16926. return 1;
  16927. }
  16928. BUILDIN_FUNC(instance_warpall)
  16929. {
  16930. int16 m, i;
  16931. unsigned short instance_id;
  16932. const char *mapn;
  16933. int x, y;
  16934. mapn = script_getstr(st,2);
  16935. x = script_getnum(st,3);
  16936. y = script_getnum(st,4);
  16937. if( script_hasdata(st,5) )
  16938. instance_id = script_getnum(st,5);
  16939. else
  16940. instance_id = script_instancegetid(st);
  16941. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  16942. return SCRIPT_CMD_FAILURE;
  16943. for(i = 0; i < instance_data[instance_id].cnt_map; i++)
  16944. map_foreachinmap(buildin_instance_warpall_sub, instance_data[instance_id].map[i]->m, BL_PC, map_id2index(m), x, y, instance_id);
  16945. return SCRIPT_CMD_SUCCESS;
  16946. }
  16947. /*==========================================
  16948. * Broadcasts to all maps inside an instance
  16949. *
  16950. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  16951. * Using 0 for <instance id> will auto-detect the id.
  16952. *------------------------------------------*/
  16953. BUILDIN_FUNC(instance_announce) {
  16954. unsigned short instance_id = script_getnum(st,2);
  16955. const char *mes = script_getstr(st,3);
  16956. int flag = script_getnum(st,4);
  16957. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  16958. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  16959. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  16960. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  16961. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  16962. int i;
  16963. if( instance_id == 0 ) {
  16964. instance_id = script_instancegetid(st);
  16965. }
  16966. if( !instance_id && &instance_data[instance_id] != NULL) {
  16967. ShowError("buildin_instance_announce: Intance is not found.\n");
  16968. return SCRIPT_CMD_FAILURE;
  16969. }
  16970. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  16971. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i]->m, BL_PC,
  16972. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  16973. return SCRIPT_CMD_SUCCESS;
  16974. }
  16975. /*==========================================
  16976. * instance_check_party [malufett]
  16977. * Values:
  16978. * party_id : Party ID of the invoking character. [Required Parameter]
  16979. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  16980. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16981. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16982. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  16983. * Example 2: instance_check_party (getcharid(1),1,1,99);
  16984. *------------------------------------------*/
  16985. BUILDIN_FUNC(instance_check_party)
  16986. {
  16987. int amount, min, max, i, party_id, c = 0;
  16988. struct party_data *p = NULL;
  16989. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  16990. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16991. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16992. if( min < 1 || min > MAX_LEVEL) {
  16993. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  16994. return SCRIPT_CMD_FAILURE;
  16995. } else if( max < 1 || max > MAX_LEVEL) {
  16996. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  16997. return SCRIPT_CMD_FAILURE;
  16998. }
  16999. if( script_hasdata(st,2) )
  17000. party_id = script_getnum(st,2);
  17001. else return SCRIPT_CMD_FAILURE;
  17002. if( !(p = party_search(party_id)) ) {
  17003. script_pushint(st, 0); // Returns false if party does not exist.
  17004. return SCRIPT_CMD_FAILURE;
  17005. }
  17006. for( i = 0; i < MAX_PARTY; i++ ) {
  17007. struct map_session_data *pl_sd;
  17008. if( (pl_sd = p->data[i].sd) )
  17009. if(map_id2bl(pl_sd->bl.id)) {
  17010. if(pl_sd->status.base_level < min) {
  17011. script_pushint(st, 0);
  17012. return SCRIPT_CMD_SUCCESS;
  17013. } else if(pl_sd->status.base_level > max) {
  17014. script_pushint(st, 0);
  17015. return SCRIPT_CMD_SUCCESS;
  17016. }
  17017. c++;
  17018. }
  17019. }
  17020. if(c < amount)
  17021. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  17022. else
  17023. script_pushint(st, 1);
  17024. return SCRIPT_CMD_SUCCESS;
  17025. }
  17026. /*==========================================
  17027. * instance_check_guild
  17028. * Values:
  17029. * guild_id : Guild ID of the invoking character. [Required Parameter]
  17030. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  17031. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17032. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17033. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  17034. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  17035. *------------------------------------------*/
  17036. BUILDIN_FUNC(instance_check_guild)
  17037. {
  17038. int amount, min, max, i, guild_id = 0, c = 0;
  17039. struct guild *g = NULL;
  17040. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  17041. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17042. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17043. if (min < 1 || min > MAX_LEVEL) {
  17044. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  17045. return SCRIPT_CMD_FAILURE;
  17046. } else if (max < 1 || max > MAX_LEVEL) {
  17047. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  17048. return SCRIPT_CMD_FAILURE;
  17049. }
  17050. if (script_hasdata(st,2))
  17051. guild_id = script_getnum(st,2);
  17052. else
  17053. return SCRIPT_CMD_FAILURE;
  17054. if (!(g = guild_search(guild_id))) {
  17055. script_pushint(st, 0); // Returns false if guild does not exist.
  17056. return SCRIPT_CMD_FAILURE;
  17057. }
  17058. for(i = 0; i < MAX_GUILD; i++) {
  17059. struct map_session_data *pl_sd;
  17060. if ((pl_sd = g->member[i].sd)) {
  17061. if (map_id2bl(pl_sd->bl.id)) {
  17062. if (pl_sd->status.base_level < min) {
  17063. script_pushint(st, 0);
  17064. return SCRIPT_CMD_SUCCESS;
  17065. } else if (pl_sd->status.base_level > max) {
  17066. script_pushint(st, 0);
  17067. return SCRIPT_CMD_SUCCESS;
  17068. }
  17069. c++;
  17070. }
  17071. }
  17072. }
  17073. if (c < amount)
  17074. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  17075. else
  17076. script_pushint(st, 1);
  17077. return SCRIPT_CMD_SUCCESS;
  17078. }
  17079. /*==========================================
  17080. * instance_info
  17081. * Values:
  17082. * name : name of the instance you want to look up. [Required Parameter]
  17083. * type : type of information you want to look up for the specified instance. [Required Parameter]
  17084. * index : index of the map in the instance. [Optional Parameter]
  17085. *------------------------------------------*/
  17086. BUILDIN_FUNC(instance_info)
  17087. {
  17088. const char* name = script_getstr(st, 2);
  17089. int type = script_getnum(st, 3);
  17090. int index = 0;
  17091. struct instance_db *db = instance_searchname_db(name);
  17092. if( !db ){
  17093. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  17094. script_pushint(st, -1);
  17095. return SCRIPT_CMD_FAILURE;
  17096. }
  17097. switch( type ){
  17098. case IIT_ID:
  17099. script_pushint(st, db->id);
  17100. break;
  17101. case IIT_TIME_LIMIT:
  17102. script_pushint(st, db->limit);
  17103. break;
  17104. case IIT_IDLE_TIMEOUT:
  17105. script_pushint(st, db->timeout);
  17106. break;
  17107. case IIT_ENTER_MAP:
  17108. script_pushstrcopy(st, StringBuf_Value(db->enter.mapname));
  17109. break;
  17110. case IIT_ENTER_X:
  17111. script_pushint(st, db->enter.x);
  17112. break;
  17113. case IIT_ENTER_Y:
  17114. script_pushint(st, db->enter.y);
  17115. break;
  17116. case IIT_MAPCOUNT:
  17117. script_pushint(st, db->maplist_count);
  17118. break;
  17119. case IIT_MAP:
  17120. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  17121. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  17122. script_pushstr(st, "");
  17123. return SCRIPT_CMD_FAILURE;
  17124. }
  17125. index = script_getnum(st, 4);
  17126. if( index < 0 ){
  17127. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  17128. script_pushstr(st, "");
  17129. return SCRIPT_CMD_FAILURE;
  17130. }
  17131. if( index > UINT8_MAX ){
  17132. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  17133. script_pushstr(st, "");
  17134. return SCRIPT_CMD_FAILURE;
  17135. }
  17136. script_pushstrcopy(st, StringBuf_Value(db->maplist[index]));
  17137. break;
  17138. default:
  17139. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  17140. script_pushint(st, -1);
  17141. return SCRIPT_CMD_FAILURE;
  17142. }
  17143. return SCRIPT_CMD_SUCCESS;
  17144. }
  17145. /*==========================================
  17146. * Custom Fonts
  17147. *------------------------------------------*/
  17148. BUILDIN_FUNC(setfont)
  17149. {
  17150. struct map_session_data *sd = script_rid2sd(st);
  17151. int font = script_getnum(st,2);
  17152. if( sd == NULL )
  17153. return SCRIPT_CMD_SUCCESS;
  17154. if( sd->status.font != font )
  17155. sd->status.font = font;
  17156. else
  17157. sd->status.font = 0;
  17158. clif_font(sd);
  17159. return SCRIPT_CMD_SUCCESS;
  17160. }
  17161. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  17162. {
  17163. TBL_MOB* md = (TBL_MOB*)bl;
  17164. struct block_list *tbl;
  17165. int mobid = va_arg(ap,int);
  17166. uint16 skill_id = va_arg(ap,int);
  17167. uint16 skill_lv = va_arg(ap,int);
  17168. int casttime = va_arg(ap,int);
  17169. int cancel = va_arg(ap,int);
  17170. int emotion = va_arg(ap,int);
  17171. int target = va_arg(ap,int);
  17172. if( md->mob_id != mobid )
  17173. return 0;
  17174. // 0:self, 1:target, 2:master, default:random
  17175. switch( target ) {
  17176. case 0: tbl = map_id2bl(md->bl.id); break;
  17177. case 1: tbl = map_id2bl(md->target_id); break;
  17178. case 2: tbl = map_id2bl(md->master_id); break;
  17179. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  17180. }
  17181. if( !tbl )
  17182. return 0;
  17183. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  17184. unit_skillcastcancel(bl,0);
  17185. if( skill_get_casttype(skill_id) == CAST_GROUND )
  17186. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  17187. else
  17188. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  17189. clif_emotion(&md->bl, emotion);
  17190. return 1;
  17191. }
  17192. /*==========================================
  17193. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  17194. *------------------------------------------*/
  17195. BUILDIN_FUNC(areamobuseskill)
  17196. {
  17197. struct block_list center;
  17198. struct script_data *data;
  17199. int16 m;
  17200. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  17201. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  17202. ShowError("areamobuseskill: invalid map name.\n");
  17203. return SCRIPT_CMD_SUCCESS;
  17204. }
  17205. center.m = m;
  17206. center.x = script_getnum(st,3);
  17207. center.y = script_getnum(st,4);
  17208. range = script_getnum(st,5);
  17209. mobid = script_getnum(st,6);
  17210. data = script_getdata(st, 7);
  17211. get_val(st, data); // Convert into value in case of a variable
  17212. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  17213. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  17214. skill_lv = battle_config.mob_max_skilllvl;
  17215. casttime = script_getnum(st,9);
  17216. cancel = script_getnum(st,10);
  17217. emotion = script_getnum(st,11);
  17218. target = script_getnum(st,12);
  17219. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  17220. return SCRIPT_CMD_SUCCESS;
  17221. }
  17222. BUILDIN_FUNC(progressbar)
  17223. {
  17224. struct map_session_data * sd = script_rid2sd(st);
  17225. const char * color;
  17226. unsigned int second;
  17227. if( !st || !sd )
  17228. return SCRIPT_CMD_SUCCESS;
  17229. st->state = STOP;
  17230. color = script_getstr(st,2);
  17231. second = script_getnum(st,3);
  17232. sd->progressbar.npc_id = st->oid;
  17233. sd->progressbar.timeout = gettick() + second*1000;
  17234. sd->state.workinprogress = WIP_DISABLE_ALL;
  17235. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  17236. return SCRIPT_CMD_SUCCESS;
  17237. }
  17238. BUILDIN_FUNC(pushpc)
  17239. {
  17240. uint8 dir;
  17241. int cells, dx, dy;
  17242. struct map_session_data* sd;
  17243. if((sd = script_rid2sd(st))==NULL)
  17244. {
  17245. return SCRIPT_CMD_SUCCESS;
  17246. }
  17247. dir = script_getnum(st,2);
  17248. cells = script_getnum(st,3);
  17249. if(dir >= DIR_MAX)
  17250. {
  17251. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  17252. script_reportsrc(st);
  17253. dir%= DIR_MAX; // trim spin-over
  17254. }
  17255. if(!cells)
  17256. {// zero distance
  17257. return SCRIPT_CMD_SUCCESS;
  17258. }
  17259. else if(cells<0)
  17260. {// pushing backwards
  17261. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  17262. cells = -cells;
  17263. }
  17264. dx = dirx[dir];
  17265. dy = diry[dir];
  17266. unit_blown(&sd->bl, dx, dy, cells, 0);
  17267. return SCRIPT_CMD_SUCCESS;
  17268. }
  17269. /// Invokes buying store preparation window
  17270. /// buyingstore <slots>;
  17271. BUILDIN_FUNC(buyingstore)
  17272. {
  17273. struct map_session_data* sd;
  17274. if( ( sd = script_rid2sd(st) ) == NULL )
  17275. {
  17276. return SCRIPT_CMD_SUCCESS;
  17277. }
  17278. if( npc_isnear(&sd->bl) ) {
  17279. char output[150];
  17280. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  17281. clif_displaymessage(sd->fd, output);
  17282. return SCRIPT_CMD_SUCCESS;
  17283. }
  17284. buyingstore_setup(sd, script_getnum(st,2));
  17285. return SCRIPT_CMD_SUCCESS;
  17286. }
  17287. /// Invokes search store info window
  17288. /// searchstores <uses>,<effect>;
  17289. BUILDIN_FUNC(searchstores)
  17290. {
  17291. unsigned short effect;
  17292. unsigned int uses;
  17293. struct map_session_data* sd;
  17294. if( ( sd = script_rid2sd(st) ) == NULL )
  17295. {
  17296. return SCRIPT_CMD_SUCCESS;
  17297. }
  17298. uses = script_getnum(st,2);
  17299. effect = script_getnum(st,3);
  17300. if( !uses )
  17301. {
  17302. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  17303. return SCRIPT_CMD_FAILURE;
  17304. }
  17305. if( effect > 1 )
  17306. {
  17307. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  17308. return SCRIPT_CMD_FAILURE;
  17309. }
  17310. searchstore_open(sd, uses, effect);
  17311. return SCRIPT_CMD_SUCCESS;
  17312. }
  17313. /// Displays a number as large digital clock.
  17314. /// showdigit <value>[,<type>];
  17315. BUILDIN_FUNC(showdigit)
  17316. {
  17317. unsigned int type = 0;
  17318. int value;
  17319. struct map_session_data* sd;
  17320. if( ( sd = script_rid2sd(st) ) == NULL )
  17321. {
  17322. return SCRIPT_CMD_SUCCESS;
  17323. }
  17324. value = script_getnum(st,2);
  17325. if( script_hasdata(st,3) ){
  17326. type = script_getnum(st,3);
  17327. }
  17328. switch( type ){
  17329. case 0:
  17330. break;
  17331. case 1:
  17332. case 2:
  17333. // Use absolute value and then the negative value of it as starting value
  17334. // This is what gravity's client does for these counters
  17335. value = -abs(value);
  17336. break;
  17337. case 3:
  17338. value = abs(value);
  17339. if( value > 99 ){
  17340. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  17341. script_reportsrc(st);
  17342. value = 99;
  17343. }
  17344. break;
  17345. default:
  17346. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  17347. return SCRIPT_CMD_FAILURE;
  17348. }
  17349. clif_showdigit(sd, (unsigned char)type, value);
  17350. return SCRIPT_CMD_SUCCESS;
  17351. }
  17352. /**
  17353. * Rune Knight
  17354. * makerune <% success bonus>{,<char_id>};
  17355. **/
  17356. BUILDIN_FUNC(makerune) {
  17357. TBL_PC* sd;
  17358. if (!script_charid2sd(3,sd))
  17359. return SCRIPT_CMD_FAILURE;
  17360. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  17361. sd->itemid = script_getnum(st,2);
  17362. return SCRIPT_CMD_SUCCESS;
  17363. }
  17364. /**
  17365. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  17366. **/
  17367. BUILDIN_FUNC(checkdragon) {
  17368. TBL_PC* sd;
  17369. if (!script_charid2sd(2,sd))
  17370. return SCRIPT_CMD_FAILURE;
  17371. if( pc_isridingdragon(sd) )
  17372. script_pushint(st,1);
  17373. else
  17374. script_pushint(st,0);
  17375. return SCRIPT_CMD_SUCCESS;
  17376. }
  17377. /**
  17378. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  17379. * - Toggles the dragon on a RK if he can mount;
  17380. * @param Color - when not provided uses the green dragon;
  17381. * - 1 : Green Dragon
  17382. * - 2 : Brown Dragon
  17383. * - 3 : Gray Dragon
  17384. * - 4 : Blue Dragon
  17385. * - 5 : Red Dragon
  17386. **/
  17387. BUILDIN_FUNC(setdragon) {
  17388. TBL_PC* sd;
  17389. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  17390. if (!script_charid2sd(3,sd))
  17391. return SCRIPT_CMD_FAILURE;
  17392. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  17393. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  17394. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  17395. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  17396. script_pushint(st,1);
  17397. } else {//Not mounted; Mount now.
  17398. unsigned int option = OPTION_DRAGON1;
  17399. if( color ) {
  17400. option = ( color == 1 ? OPTION_DRAGON1 :
  17401. color == 2 ? OPTION_DRAGON2 :
  17402. color == 3 ? OPTION_DRAGON3 :
  17403. color == 4 ? OPTION_DRAGON4 :
  17404. color == 5 ? OPTION_DRAGON5 : 0);
  17405. if( !option ) {
  17406. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  17407. option = OPTION_DRAGON1;
  17408. }
  17409. }
  17410. pc_setoption(sd, sd->sc.option|option);
  17411. script_pushint(st,1);
  17412. }
  17413. return SCRIPT_CMD_SUCCESS;
  17414. }
  17415. /**
  17416. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  17417. **/
  17418. BUILDIN_FUNC(ismounting) {
  17419. TBL_PC* sd;
  17420. if (!script_charid2sd(2,sd))
  17421. return SCRIPT_CMD_FAILURE;
  17422. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17423. script_pushint(st,1);
  17424. else
  17425. script_pushint(st,0);
  17426. return SCRIPT_CMD_SUCCESS;
  17427. }
  17428. /**
  17429. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  17430. * - Toggles new mounts on a player when he can mount
  17431. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  17432. * - Will unmount the player is he is already mounting
  17433. **/
  17434. BUILDIN_FUNC(setmounting) {
  17435. TBL_PC* sd;
  17436. if (!script_charid2sd(2,sd))
  17437. return SCRIPT_CMD_FAILURE;
  17438. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  17439. clif_msg(sd, NEED_REINS_OF_MOUNT);
  17440. script_pushint(st,0); //can't mount with one of these
  17441. } else {
  17442. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17443. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  17444. else
  17445. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  17446. script_pushint(st,1);//in both cases, return 1.
  17447. }
  17448. return SCRIPT_CMD_SUCCESS;
  17449. }
  17450. /**
  17451. * Retrieves quantity of arguments provided to callfunc/callsub.
  17452. * getargcount() -> amount of arguments received in a function
  17453. **/
  17454. BUILDIN_FUNC(getargcount) {
  17455. struct script_retinfo* ri;
  17456. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  17457. ShowError("script:getargcount: used out of function or callsub label!\n");
  17458. st->state = END;
  17459. return SCRIPT_CMD_FAILURE;
  17460. }
  17461. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  17462. script_pushint(st, ri->nargs);
  17463. return SCRIPT_CMD_SUCCESS;
  17464. }
  17465. /**
  17466. * getcharip(<account ID>/<character ID>/<character name>)
  17467. **/
  17468. BUILDIN_FUNC(getcharip)
  17469. {
  17470. struct map_session_data* sd = NULL;
  17471. /* check if a character name is specified */
  17472. if( script_hasdata(st, 2) )
  17473. {
  17474. struct script_data *data;
  17475. data = script_getdata(st, 2);
  17476. get_val(st, data); // Convert into value in case of a variable
  17477. if (data_isstring(data))
  17478. sd = map_nick2sd(script_getstr(st, 2),false);
  17479. else if (data_isint(data) || script_getnum(st, 2))
  17480. {
  17481. int id = 0;
  17482. id = script_getnum(st, 2);
  17483. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  17484. }
  17485. }
  17486. else
  17487. sd = script_rid2sd(st);
  17488. /* check for sd and IP */
  17489. if (!sd || !session[sd->fd]->client_addr)
  17490. {
  17491. script_pushconststr(st, "");
  17492. return SCRIPT_CMD_SUCCESS;
  17493. }
  17494. /* return the client ip_addr converted for output */
  17495. if (sd && sd->fd && session[sd->fd])
  17496. {
  17497. /* initiliaze */
  17498. const char *ip_addr = NULL;
  17499. uint32 ip;
  17500. /* set ip, ip_addr and convert to ip and push str */
  17501. ip = session[sd->fd]->client_addr;
  17502. ip_addr = ip2str(ip, NULL);
  17503. script_pushstrcopy(st, ip_addr);
  17504. }
  17505. return SCRIPT_CMD_SUCCESS;
  17506. }
  17507. /**
  17508. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  17509. **/
  17510. BUILDIN_FUNC(is_function) {
  17511. const char* str = script_getstr(st,2);
  17512. if( strdb_exists(userfunc_db, str) )
  17513. script_pushint(st,1);
  17514. else
  17515. script_pushint(st,0);
  17516. return SCRIPT_CMD_SUCCESS;
  17517. }
  17518. /**
  17519. * get_revision() -> retrieves the current svn revision (if available)
  17520. **/
  17521. BUILDIN_FUNC(get_revision) {
  17522. const char *svn = get_svn_revision();
  17523. if ( svn[0] != UNKNOWN_VERSION )
  17524. script_pushint(st,atoi(svn));
  17525. else
  17526. script_pushint(st,-1); //unknown
  17527. return SCRIPT_CMD_SUCCESS;
  17528. }
  17529. /* get_hash() -> retrieves the current git hash (if available)*/
  17530. BUILDIN_FUNC(get_githash) {
  17531. const char* git = get_git_hash();
  17532. char buf[CHAT_SIZE_MAX];
  17533. safestrncpy(buf,git,strlen(git)+1);
  17534. if ( git[0] != UNKNOWN_VERSION )
  17535. script_pushstr(st,buf);
  17536. else
  17537. script_pushstr(st,"Unknown"); //unknown
  17538. return SCRIPT_CMD_SUCCESS;
  17539. }
  17540. /**
  17541. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  17542. **/
  17543. BUILDIN_FUNC(freeloop) {
  17544. if( script_hasdata(st,2) ) {
  17545. if( script_getnum(st,2) )
  17546. st->freeloop = 1;
  17547. else
  17548. st->freeloop = 0;
  17549. }
  17550. script_pushint(st, st->freeloop);
  17551. return SCRIPT_CMD_SUCCESS;
  17552. }
  17553. /**
  17554. * @commands (script based)
  17555. **/
  17556. BUILDIN_FUNC(bindatcmd) {
  17557. const char* atcmd;
  17558. const char* eventName;
  17559. int i, level = 0, level2 = 0;
  17560. bool create = false;
  17561. atcmd = script_getstr(st,2);
  17562. eventName = script_getstr(st,3);
  17563. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17564. atcmd++;
  17565. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  17566. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  17567. if( atcmd_binding_count == 0 ) {
  17568. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  17569. create = true;
  17570. } else {
  17571. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  17572. if( i < atcmd_binding_count ) {/* update existent entry */
  17573. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  17574. atcmd_binding[i]->level = level;
  17575. atcmd_binding[i]->level2 = level2;
  17576. } else
  17577. create = true;
  17578. }
  17579. if( create ) {
  17580. i = atcmd_binding_count;
  17581. if( atcmd_binding_count++ != 0 )
  17582. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  17583. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  17584. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  17585. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  17586. atcmd_binding[i]->level = level;
  17587. atcmd_binding[i]->level2 = level2;
  17588. }
  17589. return SCRIPT_CMD_SUCCESS;
  17590. }
  17591. BUILDIN_FUNC(unbindatcmd) {
  17592. const char* atcmd;
  17593. int i = 0;
  17594. atcmd = script_getstr(st, 2);
  17595. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17596. atcmd++;
  17597. if( atcmd_binding_count == 0 ) {
  17598. script_pushint(st, 0);
  17599. return SCRIPT_CMD_SUCCESS;
  17600. }
  17601. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  17602. if( i < atcmd_binding_count ) {
  17603. int cursor = 0;
  17604. aFree(atcmd_binding[i]);
  17605. atcmd_binding[i] = NULL;
  17606. /* compact the list now that we freed a slot somewhere */
  17607. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  17608. if( atcmd_binding[i] == NULL )
  17609. continue;
  17610. if( cursor != i ) {
  17611. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  17612. }
  17613. cursor++;
  17614. }
  17615. if( (atcmd_binding_count = cursor) == 0 )
  17616. aFree(atcmd_binding);
  17617. script_pushint(st, 1);
  17618. } else
  17619. script_pushint(st, 0);/* not found */
  17620. return SCRIPT_CMD_SUCCESS;
  17621. }
  17622. BUILDIN_FUNC(useatcmd) {
  17623. return atcommand_sub(st,3);
  17624. }
  17625. BUILDIN_FUNC(checkre)
  17626. {
  17627. int num;
  17628. num=script_getnum(st,2);
  17629. switch(num){
  17630. case 0:
  17631. #ifdef RENEWAL
  17632. script_pushint(st, 1);
  17633. #else
  17634. script_pushint(st, 0);
  17635. #endif
  17636. break;
  17637. case 1:
  17638. #ifdef RENEWAL_CAST
  17639. script_pushint(st, 1);
  17640. #else
  17641. script_pushint(st, 0);
  17642. #endif
  17643. break;
  17644. case 2:
  17645. #ifdef RENEWAL_DROP
  17646. script_pushint(st, 1);
  17647. #else
  17648. script_pushint(st, 0);
  17649. #endif
  17650. break;
  17651. case 3:
  17652. #ifdef RENEWAL_EXP
  17653. script_pushint(st, 1);
  17654. #else
  17655. script_pushint(st, 0);
  17656. #endif
  17657. break;
  17658. case 4:
  17659. #ifdef RENEWAL_LVDMG
  17660. script_pushint(st, 1);
  17661. #else
  17662. script_pushint(st, 0);
  17663. #endif
  17664. break;
  17665. case 5:
  17666. #ifdef RENEWAL_ASPD
  17667. script_pushint(st, 1);
  17668. #else
  17669. script_pushint(st, 0);
  17670. #endif
  17671. break;
  17672. default:
  17673. ShowWarning("buildin_checkre: unknown parameter.\n");
  17674. break;
  17675. }
  17676. return SCRIPT_CMD_SUCCESS;
  17677. }
  17678. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>}} */
  17679. BUILDIN_FUNC(getrandgroupitem) {
  17680. TBL_PC* sd;
  17681. int i, get_count = 0;
  17682. uint16 group = script_getnum(st,2), qty = 0;
  17683. uint8 sub_group = 1;
  17684. struct item item_tmp;
  17685. struct s_item_group_entry *entry = NULL;
  17686. if (!(sd = script_rid2sd(st)))
  17687. return SCRIPT_CMD_SUCCESS;
  17688. if (!group) {
  17689. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  17690. return SCRIPT_CMD_FAILURE;
  17691. }
  17692. FETCH(3, qty);
  17693. FETCH(4, sub_group);
  17694. entry = itemdb_get_randgroupitem(group,sub_group);
  17695. if (!entry)
  17696. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  17697. memset(&item_tmp,0,sizeof(item_tmp));
  17698. item_tmp.nameid = entry->nameid;
  17699. item_tmp.identify = itemdb_isidentified(entry->nameid);
  17700. if (!qty)
  17701. qty = entry->amount;
  17702. //Check if it's stackable.
  17703. if (!itemdb_isstackable(entry->nameid)) {
  17704. item_tmp.amount = 1;
  17705. get_count = qty;
  17706. }
  17707. else {
  17708. item_tmp.amount = qty;
  17709. get_count = 1;
  17710. }
  17711. for (i = 0; i < get_count; i++) {
  17712. // if not pet egg
  17713. if (!pet_create_egg(sd, entry->nameid)) {
  17714. unsigned char flag = 0;
  17715. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  17716. clif_additem(sd,0,0,flag);
  17717. if (pc_candrop(sd,&item_tmp))
  17718. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  17719. }
  17720. }
  17721. }
  17722. return SCRIPT_CMD_SUCCESS;
  17723. }
  17724. /* getgroupitem <group_id>{,<char_id>};
  17725. * Gives item(s) to the attached player based on item group contents
  17726. */
  17727. BUILDIN_FUNC(getgroupitem) {
  17728. TBL_PC *sd;
  17729. int group_id = script_getnum(st,2);
  17730. if (!script_charid2sd(3,sd))
  17731. return SCRIPT_CMD_SUCCESS;
  17732. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  17733. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  17734. return SCRIPT_CMD_FAILURE;
  17735. }
  17736. return SCRIPT_CMD_SUCCESS;
  17737. }
  17738. /* cleanmap <map_name>;
  17739. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  17740. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  17741. {
  17742. nullpo_ret(bl);
  17743. map_clearflooritem(bl);
  17744. return SCRIPT_CMD_SUCCESS;
  17745. }
  17746. BUILDIN_FUNC(cleanmap)
  17747. {
  17748. const char *mapname;
  17749. int16 m;
  17750. mapname = script_getstr(st, 2);
  17751. m = map_mapname2mapid(mapname);
  17752. if (!m)
  17753. return SCRIPT_CMD_FAILURE;
  17754. if ((script_lastdata(st) - 2) < 4) {
  17755. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  17756. } else {
  17757. int16 x0 = script_getnum(st, 3);
  17758. int16 y0 = script_getnum(st, 4);
  17759. int16 x1 = script_getnum(st, 5);
  17760. int16 y1 = script_getnum(st, 6);
  17761. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  17762. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  17763. } else {
  17764. ShowError("cleanarea: invalid coordinate defined!\n");
  17765. return SCRIPT_CMD_FAILURE;
  17766. }
  17767. }
  17768. return SCRIPT_CMD_SUCCESS;
  17769. }
  17770. /* Cast a skill on the attached player.
  17771. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  17772. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  17773. BUILDIN_FUNC(npcskill)
  17774. {
  17775. uint16 skill_id;
  17776. unsigned short skill_level;
  17777. unsigned int stat_point;
  17778. unsigned int npc_level;
  17779. struct npc_data *nd;
  17780. struct map_session_data *sd;
  17781. struct script_data *data;
  17782. data = script_getdata(st, 2);
  17783. get_val(st, data); // Convert into value in case of a variable
  17784. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  17785. skill_level = script_getnum(st, 3);
  17786. stat_point = script_getnum(st, 4);
  17787. npc_level = script_getnum(st, 5);
  17788. sd = script_rid2sd(st);
  17789. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  17790. if (stat_point > battle_config.max_third_parameter) {
  17791. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  17792. return SCRIPT_CMD_FAILURE;
  17793. }
  17794. if (npc_level > MAX_LEVEL) {
  17795. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  17796. return SCRIPT_CMD_FAILURE;
  17797. }
  17798. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  17799. return SCRIPT_CMD_FAILURE;
  17800. }
  17801. nd->level = npc_level;
  17802. nd->stat_point = stat_point;
  17803. if (!nd->status.hp)
  17804. status_calc_npc(nd, SCO_FIRST);
  17805. else
  17806. status_calc_npc(nd, SCO_NONE);
  17807. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  17808. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  17809. else
  17810. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  17811. return SCRIPT_CMD_SUCCESS;
  17812. }
  17813. /* Consumes an item.
  17814. * consumeitem <item id>{,<char_id>};
  17815. * consumeitem "<item name>"{,<char_id>};
  17816. * @param item: Item ID or name
  17817. */
  17818. BUILDIN_FUNC(consumeitem)
  17819. {
  17820. TBL_PC *sd;
  17821. struct script_data *data;
  17822. struct item_data *item_data;
  17823. if (!script_charid2sd(3, sd))
  17824. return SCRIPT_CMD_FAILURE;
  17825. data = script_getdata( st, 2 );
  17826. get_val( st, data );
  17827. if( data_isstring( data ) ){
  17828. const char *name = conv_str( st, data );
  17829. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  17830. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  17831. return SCRIPT_CMD_FAILURE;
  17832. }
  17833. }else if( data_isint( data ) ){
  17834. unsigned short nameid = conv_num( st, data );
  17835. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  17836. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  17837. return SCRIPT_CMD_FAILURE;
  17838. }
  17839. }else{
  17840. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  17841. return SCRIPT_CMD_FAILURE;
  17842. }
  17843. run_script( item_data->script, 0, sd->bl.id, 0 );
  17844. return SCRIPT_CMD_SUCCESS;
  17845. }
  17846. /** Makes a player sit/stand.
  17847. * sit {"<character name>"};
  17848. * stand {"<character name>"};
  17849. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  17850. * @param name: Player name that will be invoked
  17851. */
  17852. BUILDIN_FUNC(sit)
  17853. {
  17854. TBL_PC *sd;
  17855. if( !script_nick2sd(2,sd) )
  17856. return SCRIPT_CMD_FAILURE;
  17857. if( !pc_issit(sd) ) {
  17858. pc_setsit(sd);
  17859. skill_sit(sd, 1);
  17860. clif_sitting(&sd->bl);
  17861. }
  17862. return SCRIPT_CMD_SUCCESS;
  17863. }
  17864. /** Makes player to stand
  17865. * @param name: Player name that will be set
  17866. */
  17867. BUILDIN_FUNC(stand)
  17868. {
  17869. TBL_PC *sd;
  17870. if( !script_nick2sd(2,sd) )
  17871. return SCRIPT_CMD_FAILURE;
  17872. if( pc_issit(sd) && pc_setstand(sd, false)) {
  17873. skill_sit(sd, 0);
  17874. clif_standing(&sd->bl);
  17875. }
  17876. return SCRIPT_CMD_SUCCESS;
  17877. }
  17878. /** Creates an array of bounded item IDs
  17879. * countbound {<type>{,<char_id>}};
  17880. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  17881. * @return amt: Amount of items found
  17882. */
  17883. BUILDIN_FUNC(countbound)
  17884. {
  17885. int i, type, j = 0, k = 0;
  17886. TBL_PC *sd;
  17887. if (!script_charid2sd(3,sd))
  17888. return SCRIPT_CMD_FAILURE;
  17889. type = script_getnum(st,2);
  17890. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  17891. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  17892. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  17893. ))
  17894. {
  17895. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  17896. k++;
  17897. j += sd->inventory.u.items_inventory[i].amount;
  17898. }
  17899. }
  17900. script_pushint(st,j);
  17901. return SCRIPT_CMD_SUCCESS;
  17902. }
  17903. /** Creates new party
  17904. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  17905. * @param party_name: String of party name that will be created
  17906. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  17907. * @param item_share: 0-Each Take. 1-Party Share
  17908. * @param item_share_type: 0-Each Take. 1-Even Share
  17909. * @return val: Result value
  17910. * -3 - party name is exist
  17911. * -2 - player is in party already
  17912. * -1 - player is not found
  17913. * 0 - unknown error
  17914. * 1 - success, will return party id $@party_create_id
  17915. */
  17916. BUILDIN_FUNC(party_create)
  17917. {
  17918. char party_name[NAME_LENGTH];
  17919. int item1 = 0, item2 = 0;
  17920. TBL_PC *sd = NULL;
  17921. if (!script_charid2sd(3, sd)) {
  17922. script_pushint(st,-1);
  17923. return SCRIPT_CMD_FAILURE;
  17924. }
  17925. if( sd->status.party_id ) {
  17926. script_pushint(st,-2);
  17927. return SCRIPT_CMD_FAILURE;
  17928. }
  17929. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  17930. trim(party_name);
  17931. if( party_searchname(party_name) ) {
  17932. script_pushint(st,-3);
  17933. return SCRIPT_CMD_FAILURE;
  17934. }
  17935. if( script_getnum(st,4) )
  17936. item1 = 1;
  17937. if( script_getnum(st,5) )
  17938. item2 = 1;
  17939. party_create_byscript = 1;
  17940. script_pushint(st,party_create(sd,party_name,item1,item2));
  17941. return SCRIPT_CMD_SUCCESS;
  17942. }
  17943. /** Adds player to specified party
  17944. * party_addmember <party id>,<char id>;
  17945. * @param party_id: The party that will be entered by player
  17946. * @param char_id: Char id of player that will be joined to the party
  17947. * @return val: Result value
  17948. * -5 - another character of the same account is in the party
  17949. * -4 - party is full
  17950. * -3 - party is not found
  17951. * -2 - player is in party already
  17952. * -1 - player is not found
  17953. * 0 - unknown error
  17954. * 1 - success
  17955. */
  17956. BUILDIN_FUNC(party_addmember)
  17957. {
  17958. int party_id = script_getnum(st,2);
  17959. TBL_PC *sd;
  17960. struct party_data *party;
  17961. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  17962. script_pushint(st,-1);
  17963. return SCRIPT_CMD_FAILURE;
  17964. }
  17965. if( sd->status.party_id ) {
  17966. script_pushint(st,-2);
  17967. return SCRIPT_CMD_FAILURE;
  17968. }
  17969. if( !(party = party_search(party_id)) ) {
  17970. script_pushint(st,-3);
  17971. return SCRIPT_CMD_FAILURE;
  17972. }
  17973. if (battle_config.block_account_in_same_party) {
  17974. int i;
  17975. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  17976. if (i < MAX_PARTY) {
  17977. script_pushint(st,-5);
  17978. return SCRIPT_CMD_FAILURE;
  17979. }
  17980. }
  17981. if( party->party.count >= MAX_PARTY ) {
  17982. script_pushint(st,-4);
  17983. return SCRIPT_CMD_FAILURE;
  17984. }
  17985. sd->party_invite = party_id;
  17986. script_pushint(st,party_add_member(party_id,sd));
  17987. return SCRIPT_CMD_SUCCESS;
  17988. }
  17989. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  17990. * party_delmember {<char id>,<party_id>};
  17991. * @param: char_id
  17992. * @param: party_id
  17993. * @return val: Result value
  17994. * -3 - player is not in party
  17995. * -2 - party is not found
  17996. * -1 - player is not found
  17997. * 0 - unknown error
  17998. * 1 - success
  17999. */
  18000. BUILDIN_FUNC(party_delmember)
  18001. {
  18002. TBL_PC *sd = NULL;
  18003. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  18004. script_pushint(st,-1);
  18005. return SCRIPT_CMD_FAILURE;
  18006. }
  18007. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  18008. script_pushint(st,party_removemember2(sd,0,0));
  18009. else
  18010. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  18011. return SCRIPT_CMD_SUCCESS;
  18012. }
  18013. /** Changes party leader of specified party (even the leader is offline)
  18014. * party_changeleader <party id>,<char id>;
  18015. * @param party_id: ID of party
  18016. * @param char_id: Char ID of new leader
  18017. * @return val: Result value
  18018. * -4 - player is party leader already
  18019. * -3 - player is not in this party
  18020. * -2 - player is not found
  18021. * -1 - party is not found
  18022. * 0 - unknown error
  18023. * 1 - success */
  18024. BUILDIN_FUNC(party_changeleader)
  18025. {
  18026. int i, party_id = script_getnum(st,2);
  18027. TBL_PC *sd = NULL;
  18028. TBL_PC *tsd = NULL;
  18029. struct party_data *party = NULL;
  18030. if( !(party = party_search(party_id)) ) {
  18031. script_pushint(st,-1);
  18032. return SCRIPT_CMD_FAILURE;
  18033. }
  18034. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  18035. script_pushint(st,-2);
  18036. return SCRIPT_CMD_FAILURE;
  18037. }
  18038. if( tsd->status.party_id != party_id ) {
  18039. script_pushint(st,-3);
  18040. return SCRIPT_CMD_FAILURE;
  18041. }
  18042. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  18043. if( i >= MAX_PARTY ) { //this is should impossible!
  18044. script_pushint(st,0);
  18045. return SCRIPT_CMD_FAILURE;
  18046. }
  18047. if( party->data[i].sd == tsd ) {
  18048. script_pushint(st,-4);
  18049. return SCRIPT_CMD_FAILURE;
  18050. }
  18051. script_pushint(st,party_changeleader(sd,tsd,party));
  18052. return SCRIPT_CMD_SUCCESS;
  18053. }
  18054. /** Changes party option
  18055. * party_changeoption <party id>,<option>,<flag>;
  18056. * @param party_id: ID of party that will be changed
  18057. * @param option: Type of option
  18058. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  18059. */
  18060. BUILDIN_FUNC(party_changeoption)
  18061. {
  18062. struct party_data *party;
  18063. if( !(party = party_search(script_getnum(st,2))) ) {
  18064. script_pushint(st,-1);
  18065. return SCRIPT_CMD_FAILURE;
  18066. }
  18067. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  18068. return SCRIPT_CMD_SUCCESS;
  18069. }
  18070. /** Destroys party with party id.
  18071. * party_destroy <party id>;
  18072. * @param party_id: ID of party that will be destroyed
  18073. */
  18074. BUILDIN_FUNC(party_destroy)
  18075. {
  18076. int i;
  18077. struct party_data *party;
  18078. if( !(party = party_search(script_getnum(st,2))) ) {
  18079. script_pushint(st,0);
  18080. return SCRIPT_CMD_FAILURE;
  18081. }
  18082. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  18083. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  18084. int j;
  18085. for( j = 0; j < MAX_PARTY; j++ ) {
  18086. TBL_PC *sd = party->data[j].sd;
  18087. if(sd)
  18088. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  18089. else if( party->party.member[j].char_id )
  18090. 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);
  18091. }
  18092. party_broken(party->party.party_id);
  18093. script_pushint(st,1);
  18094. }
  18095. else //leader leave = party broken
  18096. script_pushint(st,party_leave(party->data[i].sd));
  18097. return SCRIPT_CMD_SUCCESS;
  18098. }
  18099. /** Checks if a player's client version meets a required version or date.
  18100. * @param type: 0 - check by version number; 1 - check by date
  18101. * @param data: Input
  18102. */
  18103. BUILDIN_FUNC(is_clientver) {
  18104. TBL_PC *sd = NULL;
  18105. int type = script_getnum(st,2);
  18106. int data = script_getnum(st,3);
  18107. int ret = 0;
  18108. if (script_hasdata(st,4))
  18109. sd = map_charid2sd(script_getnum(st,4));
  18110. else
  18111. sd = script_rid2sd(st);
  18112. if (sd == NULL) {
  18113. script_pushint(st,0);
  18114. return SCRIPT_CMD_FAILURE;
  18115. }
  18116. switch(type){
  18117. case 0:
  18118. ret = (sd->packet_ver >= data)?1:0;
  18119. break;
  18120. case 1:
  18121. ret = (sd->packet_ver >= date2version(data))?1:0;
  18122. break;
  18123. }
  18124. script_pushint(st,ret);
  18125. return SCRIPT_CMD_SUCCESS;
  18126. }
  18127. /** Returns various information about a player's VIP status. Need to enable VIP system
  18128. * vip_status <type>,{"<character name>"};
  18129. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  18130. * @param name: Character name (Optional)
  18131. */
  18132. BUILDIN_FUNC(vip_status) {
  18133. #ifdef VIP_ENABLE
  18134. TBL_PC *sd;
  18135. char vip_str[26];
  18136. time_t now = time(NULL);
  18137. int type = script_getnum(st, 2);
  18138. if( !script_nick2sd(3,sd) )
  18139. return SCRIPT_CMD_FAILURE;
  18140. switch(type) {
  18141. case 1: // Get VIP status.
  18142. script_pushint(st, pc_isvip(sd));
  18143. break;
  18144. case 2: // Get VIP expire date.
  18145. if (pc_isvip(sd)) {
  18146. time_t viptime = sd->vip.time;
  18147. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  18148. vip_str[25] = '\0';
  18149. script_pushstrcopy(st, vip_str);
  18150. } else
  18151. script_pushint(st, 0);
  18152. break;
  18153. case 3: // Get remaining time.
  18154. if (pc_isvip(sd)) {
  18155. time_t viptime_remain = sd->vip.time - now;
  18156. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  18157. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  18158. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  18159. script_pushstrcopy(st, vip_str);
  18160. } else
  18161. script_pushint(st, 0);
  18162. break;
  18163. }
  18164. #else
  18165. script_pushint(st, 0);
  18166. #endif
  18167. return SCRIPT_CMD_SUCCESS;
  18168. }
  18169. /** Adds or removes VIP time in minutes. Need to enable VIP system
  18170. * vip_time <time in mn>,{"<character name>"};
  18171. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  18172. * @param name: Character name (optional)
  18173. */
  18174. BUILDIN_FUNC(vip_time) {
  18175. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  18176. TBL_PC *sd;
  18177. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  18178. if( !script_nick2sd(3,sd) )
  18179. return SCRIPT_CMD_FAILURE;
  18180. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  18181. #endif
  18182. return SCRIPT_CMD_SUCCESS;
  18183. }
  18184. /**
  18185. * Turns a player into a monster and optionally can grant a SC attribute effect.
  18186. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18187. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18188. * @param monster: Monster ID or name
  18189. * @param duration: Transform duration in millisecond (ms)
  18190. * @param sc_type: Type of SC that will be affected during the transformation
  18191. * @param val1: Value for SC
  18192. * @param val2: Value for SC
  18193. * @param val3: Value for SC
  18194. * @param val4: Value for SC
  18195. * @author: malufett
  18196. */
  18197. BUILDIN_FUNC(montransform) {
  18198. TBL_PC *sd;
  18199. enum sc_type type;
  18200. int tick, mob_id, val1, val2, val3, val4;
  18201. struct script_data *data;
  18202. val1 = val2 = val3 = val4 = 0;
  18203. if( (sd = script_rid2sd(st)) == NULL )
  18204. return SCRIPT_CMD_FAILURE;
  18205. data = script_getdata(st, 2);
  18206. get_val(st, data); // Convert into value in case of a variable
  18207. if( data_isstring(data) )
  18208. mob_id = mobdb_searchname(script_getstr(st, 2));
  18209. else
  18210. mob_id = mobdb_checkid(script_getnum(st, 2));
  18211. tick = script_getnum(st, 3);
  18212. if (script_hasdata(st, 4))
  18213. type = (sc_type)script_getnum(st, 4);
  18214. else
  18215. type = SC_NONE;
  18216. if (mob_id == 0) {
  18217. if( data_isstring(data) )
  18218. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  18219. else
  18220. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  18221. return SCRIPT_CMD_FAILURE;
  18222. }
  18223. if (mob_id == MOBID_EMPERIUM) {
  18224. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  18225. return SCRIPT_CMD_FAILURE;
  18226. }
  18227. if (!(type >= SC_NONE && type < SC_MAX)) {
  18228. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  18229. return SCRIPT_CMD_FAILURE;
  18230. }
  18231. if (script_hasdata(st, 5))
  18232. val1 = script_getnum(st, 5);
  18233. if (script_hasdata(st, 6))
  18234. val2 = script_getnum(st, 6);
  18235. if (script_hasdata(st, 7))
  18236. val3 = script_getnum(st, 7);
  18237. if (script_hasdata(st, 8))
  18238. val4 = script_getnum(st, 8);
  18239. if (tick != 0) {
  18240. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  18241. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  18242. return SCRIPT_CMD_FAILURE;
  18243. }
  18244. if (sd->disguise){
  18245. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  18246. return SCRIPT_CMD_FAILURE;
  18247. }
  18248. if (!strcmp(script_getfuncname(st), "active_transform")) {
  18249. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18250. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18251. } else {
  18252. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18253. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18254. }
  18255. if (type != SC_NONE)
  18256. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  18257. }
  18258. return SCRIPT_CMD_SUCCESS;
  18259. }
  18260. /**
  18261. * Attach script to player for certain duration
  18262. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  18263. * @param "script code"
  18264. * @param duration
  18265. * @param flag
  18266. * @param icon
  18267. * @param char_id
  18268. * @author [Cydh]
  18269. **/
  18270. BUILDIN_FUNC(bonus_script) {
  18271. uint16 flag = 0;
  18272. int16 icon = SI_BLANK;
  18273. uint32 dur;
  18274. uint8 type = 0;
  18275. TBL_PC* sd;
  18276. const char *script_str = NULL;
  18277. struct s_bonus_script_entry *entry = NULL;
  18278. if (script_hasdata(st,7)) {
  18279. if (!(sd = map_charid2sd(script_getnum(st,7)))) {
  18280. ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7));
  18281. return SCRIPT_CMD_FAILURE;
  18282. }
  18283. }
  18284. else
  18285. sd = script_rid2sd(st);
  18286. if (sd == NULL)
  18287. return SCRIPT_CMD_FAILURE;
  18288. script_str = script_getstr(st,2);
  18289. dur = 1000 * abs(script_getnum(st,3));
  18290. FETCH(4, flag);
  18291. FETCH(5, type);
  18292. FETCH(6, icon);
  18293. // No Script string, No Duration!
  18294. if (script_str[0] == '\0' || !dur) {
  18295. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  18296. return SCRIPT_CMD_FAILURE;
  18297. }
  18298. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  18299. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  18300. return SCRIPT_CMD_FAILURE;
  18301. }
  18302. if (icon <= SI_BLANK || icon >= SI_MAX)
  18303. icon = SI_BLANK;
  18304. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  18305. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  18306. status_calc_pc(sd,SCO_NONE);
  18307. }
  18308. return SCRIPT_CMD_SUCCESS;
  18309. }
  18310. /**
  18311. * Removes all bonus script from player
  18312. * bonus_script_clear {<flag>,{<char_id>}};
  18313. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  18314. * @param char_id Clear script from this character
  18315. * @author [Cydh]
  18316. */
  18317. BUILDIN_FUNC(bonus_script_clear) {
  18318. TBL_PC* sd;
  18319. bool flag = false;
  18320. if (script_hasdata(st,2))
  18321. flag = script_getnum(st,2);
  18322. if (script_hasdata(st,3))
  18323. sd = map_charid2sd(script_getnum(st,3));
  18324. else
  18325. sd = script_rid2sd(st);
  18326. if (sd == NULL)
  18327. return SCRIPT_CMD_FAILURE;
  18328. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  18329. return SCRIPT_CMD_SUCCESS;
  18330. }
  18331. /** Allows player to use atcommand while talking with NPC
  18332. * enable_command;
  18333. * @author [Cydh], [Kichi]
  18334. */
  18335. BUILDIN_FUNC(enable_command) {
  18336. TBL_PC* sd = script_rid2sd(st);
  18337. if (!sd)
  18338. return SCRIPT_CMD_FAILURE;
  18339. sd->state.disable_atcommand_on_npc = 0;
  18340. return SCRIPT_CMD_SUCCESS;
  18341. }
  18342. /** Prevents player to use atcommand while talking with NPC
  18343. * disable_command;
  18344. * @author [Cydh], [Kichi]
  18345. */
  18346. BUILDIN_FUNC(disable_command) {
  18347. TBL_PC* sd = script_rid2sd(st);
  18348. if (!sd)
  18349. return SCRIPT_CMD_FAILURE;
  18350. sd->state.disable_atcommand_on_npc = 1;
  18351. return SCRIPT_CMD_SUCCESS;
  18352. }
  18353. /** Get the information of the members of a guild by type.
  18354. * getguildmember <guild_id>{,<type>};
  18355. * @param guild_id: ID of guild
  18356. * @param type: Type of option (optional)
  18357. */
  18358. BUILDIN_FUNC(getguildmember)
  18359. {
  18360. struct guild *g = NULL;
  18361. uint8 j = 0;
  18362. g = guild_search(script_getnum(st,2));
  18363. if (g) {
  18364. uint8 i, type = 0;
  18365. struct script_data *data = NULL;
  18366. char *varname = NULL;
  18367. if (script_hasdata(st,3))
  18368. type = script_getnum(st,3);
  18369. if (script_hasdata(st,4)) {
  18370. data = script_getdata(st, 4);
  18371. if (!data_isreference(data)) {
  18372. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  18373. return SCRIPT_CMD_FAILURE;
  18374. }
  18375. varname = reference_getname(data);
  18376. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  18377. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  18378. return SCRIPT_CMD_FAILURE;
  18379. }
  18380. }
  18381. for (i = 0; i < MAX_GUILD; i++) {
  18382. if (g->member[i].account_id) {
  18383. switch (type) {
  18384. case 2:
  18385. if (data)
  18386. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  18387. else
  18388. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  18389. break;
  18390. case 1:
  18391. if (data)
  18392. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  18393. else
  18394. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  18395. break;
  18396. default:
  18397. if (data)
  18398. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  18399. else
  18400. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  18401. break;
  18402. }
  18403. j++;
  18404. }
  18405. }
  18406. }
  18407. mapreg_setreg(add_str("$@guildmembercount"), j);
  18408. script_pushint(st, j);
  18409. return SCRIPT_CMD_SUCCESS;
  18410. }
  18411. /** Adds spirit ball to player for 'duration' in milisecond
  18412. * addspiritball <count>,<duration>{,<char_id>};
  18413. * @param count How many spirit ball will be added
  18414. * @param duration How long spiritball is active until it disappears
  18415. * @param char_id Target player (Optional)
  18416. * @author [Cydh]
  18417. */
  18418. BUILDIN_FUNC(addspiritball) {
  18419. uint8 i, count = script_getnum(st,2);
  18420. uint16 duration = script_getnum(st,3);
  18421. struct map_session_data *sd = NULL;
  18422. if (count == 0)
  18423. return SCRIPT_CMD_SUCCESS;
  18424. if (script_hasdata(st,4)) {
  18425. if (!script_isstring(st,4))
  18426. sd = map_charid2sd(script_getnum(st,4));
  18427. else
  18428. sd = map_nick2sd(script_getstr(st,4),false);
  18429. }
  18430. else
  18431. sd = script_rid2sd(st);
  18432. if (!sd)
  18433. return SCRIPT_CMD_FAILURE;
  18434. for (i = 0; i < count; i++)
  18435. pc_addspiritball(sd,duration,10);
  18436. return SCRIPT_CMD_SUCCESS;
  18437. }
  18438. /** Deletes the spirit ball(s) from player
  18439. * delspiritball <count>{,<char_id>};
  18440. * @param count How many spirit ball will be deleted
  18441. * @param char_id Target player (Optional)
  18442. * @author [Cydh]
  18443. */
  18444. BUILDIN_FUNC(delspiritball) {
  18445. uint8 count = script_getnum(st,2);
  18446. struct map_session_data *sd = NULL;
  18447. if (count == 0)
  18448. count = 1;
  18449. if (script_hasdata(st,3)) {
  18450. if (!script_isstring(st,3))
  18451. sd = map_charid2sd(script_getnum(st,3));
  18452. else
  18453. sd = map_nick2sd(script_getstr(st,3),false);
  18454. }
  18455. else
  18456. sd = script_rid2sd(st);
  18457. if (!sd)
  18458. return SCRIPT_CMD_FAILURE;
  18459. pc_delspiritball(sd,count,0);
  18460. return SCRIPT_CMD_SUCCESS;
  18461. }
  18462. /** Counts the spirit ball that player has
  18463. * countspiritball {<char_id>};
  18464. * @param char_id Target player (Optional)
  18465. * @author [Cydh]
  18466. */
  18467. BUILDIN_FUNC(countspiritball) {
  18468. struct map_session_data *sd;
  18469. if (script_hasdata(st,2)) {
  18470. if (!script_isstring(st,2))
  18471. sd = map_charid2sd(script_getnum(st,2));
  18472. else
  18473. sd = map_nick2sd(script_getstr(st,2),false);
  18474. }
  18475. else
  18476. sd = script_rid2sd(st);
  18477. if (!sd)
  18478. return SCRIPT_CMD_FAILURE;
  18479. script_pushint(st,sd->spiritball);
  18480. return SCRIPT_CMD_SUCCESS;
  18481. }
  18482. /** Merges separated stackable items because of guid
  18483. * mergeitem {<char_id>};
  18484. * @param char_id Char ID (Optional)
  18485. * @author [Cydh]
  18486. */
  18487. BUILDIN_FUNC(mergeitem) {
  18488. struct map_session_data *sd;
  18489. if (!script_charid2sd(2, sd))
  18490. return SCRIPT_CMD_FAILURE;
  18491. clif_merge_item_open(sd);
  18492. return SCRIPT_CMD_SUCCESS;
  18493. }
  18494. /** Merges separated stackable items because of guid (Unofficial)
  18495. * mergeitem2 {<item_id>,{<char_id>}};
  18496. * mergeitem2 {"<item name>",{<char_id>}};
  18497. * @param item Item ID/Name for merging specific item (Optional)
  18498. * @author [Cydh]
  18499. */
  18500. BUILDIN_FUNC(mergeitem2) {
  18501. struct map_session_data *sd;
  18502. struct item *items = NULL;
  18503. uint16 i, count = 0, nameid = 0;
  18504. if (!script_charid2sd(3, sd))
  18505. return SCRIPT_CMD_FAILURE;
  18506. if (script_hasdata(st, 2)) {
  18507. struct script_data *data = script_getdata(st, 2);
  18508. get_val(st, data);
  18509. if (data_isstring(data)) {// "<item name>"
  18510. const char *name = conv_str(st,data);
  18511. struct item_data *id;
  18512. if (!(id = itemdb_searchname(name))) {
  18513. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  18514. script_pushint(st, count);
  18515. return SCRIPT_CMD_FAILURE;
  18516. }
  18517. nameid = id->nameid;
  18518. }
  18519. else if (data_isint(data)) {// <item id>
  18520. nameid = conv_num(st,data);
  18521. if (!itemdb_exists(nameid)) {
  18522. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  18523. script_pushint(st, count);
  18524. return SCRIPT_CMD_FAILURE;
  18525. }
  18526. }
  18527. }
  18528. for (i = 0; i < MAX_INVENTORY; i++) {
  18529. struct item *it = &sd->inventory.u.items_inventory[i];
  18530. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  18531. continue;
  18532. if ((!nameid || (nameid == it->nameid))) {
  18533. uint8 k;
  18534. if (!count) {
  18535. CREATE(items, struct item, 1);
  18536. memcpy(&items[count++], it, sizeof(struct item));
  18537. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18538. continue;
  18539. }
  18540. for (k = 0; k < count; k++) {
  18541. // Find Match
  18542. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  18543. items[k].amount += it->amount;
  18544. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18545. break;
  18546. }
  18547. }
  18548. if (k >= count) {
  18549. // New entry
  18550. RECREATE(items, struct item, count+1);
  18551. memcpy(&items[count++], it, sizeof(struct item));
  18552. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18553. }
  18554. }
  18555. }
  18556. if (!items) // Nothing todo here
  18557. return SCRIPT_CMD_SUCCESS;
  18558. // Retrieve the items
  18559. for (i = 0; i < count; i++) {
  18560. uint8 flag = 0;
  18561. if (!&items[i])
  18562. continue;
  18563. items[i].id = 0;
  18564. items[i].unique_id = 0;
  18565. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  18566. clif_additem(sd, i, items[i].amount, flag);
  18567. }
  18568. aFree(items);
  18569. script_pushint(st, count);
  18570. return SCRIPT_CMD_SUCCESS;
  18571. }
  18572. /**
  18573. * Update an entry from NPC shop.
  18574. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  18575. **/
  18576. BUILDIN_FUNC(npcshopupdate) {
  18577. const char* npcname = script_getstr(st, 2);
  18578. struct npc_data* nd = npc_name2id(npcname);
  18579. uint16 nameid = script_getnum(st, 3);
  18580. int price = script_getnum(st, 4);
  18581. #if PACKETVER >= 20131223
  18582. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  18583. #endif
  18584. int i;
  18585. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  18586. script_pushint(st,0);
  18587. return SCRIPT_CMD_FAILURE;
  18588. }
  18589. if (!nd->u.shop.count) {
  18590. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  18591. script_pushint(st,0);
  18592. return SCRIPT_CMD_FAILURE;
  18593. }
  18594. for (i = 0; i < nd->u.shop.count; i++) {
  18595. if (nd->u.shop.shop_item[i].nameid == nameid) {
  18596. if (price != 0)
  18597. nd->u.shop.shop_item[i].value = price;
  18598. #if PACKETVER >= 20131223
  18599. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  18600. nd->u.shop.shop_item[i].qty = stock;
  18601. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  18602. }
  18603. #endif
  18604. }
  18605. }
  18606. script_pushint(st,1);
  18607. return SCRIPT_CMD_SUCCESS;
  18608. }
  18609. // Clan System
  18610. BUILDIN_FUNC(clan_join){
  18611. struct map_session_data *sd;
  18612. int clan_id = script_getnum(st,2);
  18613. if( !script_charid2sd( 3, sd ) ){
  18614. script_pushint(st, false);
  18615. return SCRIPT_CMD_FAILURE;
  18616. }
  18617. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  18618. script_pushint(st, true);
  18619. else
  18620. script_pushint(st, false);
  18621. return SCRIPT_CMD_SUCCESS;
  18622. }
  18623. BUILDIN_FUNC(clan_leave){
  18624. struct map_session_data *sd;
  18625. if( !script_charid2sd( 2, sd ) ){
  18626. script_pushint(st, false);
  18627. return SCRIPT_CMD_FAILURE;
  18628. }
  18629. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  18630. script_pushint(st, true);
  18631. else
  18632. script_pushint(st, false);
  18633. return SCRIPT_CMD_SUCCESS;
  18634. }
  18635. /**
  18636. * Get rid from running script.
  18637. * getattachedrid();
  18638. **/
  18639. BUILDIN_FUNC(getattachedrid) {
  18640. script_pushint(st, st->rid);
  18641. return SCRIPT_CMD_SUCCESS;
  18642. }
  18643. /**
  18644. * Get variable from a Player
  18645. * getvar <variable>,<char_id>;
  18646. */
  18647. BUILDIN_FUNC(getvar) {
  18648. int char_id = script_getnum(st, 3);
  18649. struct map_session_data *sd = map_charid2sd(char_id);
  18650. struct script_data *data = NULL;
  18651. const char *name = NULL;
  18652. if (!sd) {
  18653. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  18654. return SCRIPT_CMD_FAILURE;
  18655. }
  18656. data = script_getdata(st, 2);
  18657. if (!data_isreference(data)) {
  18658. ShowError("buildin_getvar: Not a variable\n");
  18659. script_reportdata(data);
  18660. script_pushnil(st);
  18661. st->state = END;
  18662. return SCRIPT_CMD_FAILURE;
  18663. }
  18664. name = reference_getname(data);
  18665. if (reference_toparam(data)) {
  18666. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  18667. script_reportdata(data);
  18668. script_pushnil(st);
  18669. st->state = END;
  18670. return SCRIPT_CMD_FAILURE;
  18671. }
  18672. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  18673. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  18674. script_reportdata(data);
  18675. script_pushnil(st);
  18676. st->state = END;
  18677. return SCRIPT_CMD_FAILURE;
  18678. }
  18679. get_val_(st, data, sd);
  18680. if (data_isint(data))
  18681. script_pushint(st, conv_num_(st, data, sd));
  18682. else
  18683. script_pushstrcopy(st, conv_str_(st, data, sd));
  18684. push_val2(st->stack, C_NAME, reference_getuid(data), reference_getref(data));
  18685. return SCRIPT_CMD_SUCCESS;
  18686. }
  18687. /**
  18688. * Display script message
  18689. * showscript "<message>"{,<GID>};
  18690. **/
  18691. BUILDIN_FUNC(showscript) {
  18692. struct block_list *bl = NULL;
  18693. const char *msg = script_getstr(st,2);
  18694. int id = 0;
  18695. if (script_hasdata(st,3)) {
  18696. id = script_getnum(st,3);
  18697. bl = map_id2bl(id);
  18698. }
  18699. else {
  18700. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  18701. }
  18702. if (!bl) {
  18703. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  18704. script_pushint(st,0);
  18705. return SCRIPT_CMD_FAILURE;
  18706. }
  18707. clif_showscript(bl, msg);
  18708. script_pushint(st,1);
  18709. return SCRIPT_CMD_SUCCESS;
  18710. }
  18711. /**
  18712. * Ignore the SECURE_NPCTIMEOUT function.
  18713. * ignoretimeout <flag>{,<char_id>};
  18714. */
  18715. BUILDIN_FUNC(ignoretimeout)
  18716. {
  18717. #ifdef SECURE_NPCTIMEOUT
  18718. struct map_session_data *sd = NULL;
  18719. if (script_hasdata(st,3)) {
  18720. if (!script_isstring(st,3))
  18721. sd = map_charid2sd(script_getnum(st,3));
  18722. else
  18723. sd = map_nick2sd(script_getstr(st,3),false);
  18724. } else
  18725. sd = script_rid2sd(st);
  18726. if (!sd)
  18727. return SCRIPT_CMD_FAILURE;
  18728. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  18729. #endif
  18730. return SCRIPT_CMD_SUCCESS;
  18731. }
  18732. /**
  18733. * geteleminfo <type>{,<char_id>};
  18734. **/
  18735. BUILDIN_FUNC(geteleminfo) {
  18736. TBL_ELEM *ed = NULL;
  18737. TBL_PC *sd = NULL;
  18738. int type = script_getnum(st,2);
  18739. if (!script_charid2sd(3, sd)) {
  18740. script_pushint(st, 0);
  18741. return SCRIPT_CMD_SUCCESS;
  18742. }
  18743. if (!(ed = sd->ed)) {
  18744. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  18745. script_pushint(st, 0);
  18746. return SCRIPT_CMD_SUCCESS;
  18747. }
  18748. switch (type) {
  18749. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  18750. case 1: script_pushint(st, ed->bl.id); break;
  18751. default:
  18752. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  18753. script_pushint(st, 0);
  18754. return SCRIPT_CMD_FAILURE;
  18755. }
  18756. return SCRIPT_CMD_SUCCESS;
  18757. }
  18758. /**
  18759. * Set the quest info of quest_id only showed on player in level range.
  18760. * setquestinfo_level <quest_id>,<min_level>,<max_level>
  18761. * @author [Cydh]
  18762. **/
  18763. BUILDIN_FUNC(setquestinfo_level) {
  18764. TBL_NPC* nd = map_id2nd(st->oid);
  18765. int quest_id = script_getnum(st, 2);
  18766. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18767. if (!qi) {
  18768. ShowError("buildin_setquestinfo_level: Quest with ID '%d' is not defined yet.\n", quest_id);
  18769. return SCRIPT_CMD_FAILURE;
  18770. }
  18771. qi->min_level = script_getnum(st, 3);
  18772. qi->max_level = script_getnum(st, 4);
  18773. if (!qi->max_level)
  18774. qi->max_level = MAX_LEVEL;
  18775. return SCRIPT_CMD_SUCCESS;
  18776. }
  18777. /**
  18778. * Set the quest info of quest_id only showed for player that has quest criteria
  18779. * setquestinfo_req <quest_id>,<quest_req_id>,<state>{,<quest_req_id>,<state>,...};
  18780. * @author [Cydh]
  18781. **/
  18782. BUILDIN_FUNC(setquestinfo_req) {
  18783. TBL_NPC* nd = map_id2nd(st->oid);
  18784. int quest_id = script_getnum(st, 2);
  18785. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18786. uint8 i = 0;
  18787. uint8 num = script_lastdata(st);
  18788. if (!qi) {
  18789. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not defined yet.\n", quest_id);
  18790. return SCRIPT_CMD_FAILURE;
  18791. }
  18792. if (quest_search(quest_id) == &quest_dummy) {
  18793. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not found in Quest DB.\n", quest_id);
  18794. return SCRIPT_CMD_FAILURE;
  18795. }
  18796. if (num%2) {
  18797. ShowError("buildin_setquestinfo_req: Odd number of parameters(%d) - pairs of requirements are expected.\n", num-2);
  18798. return SCRIPT_CMD_FAILURE;
  18799. }
  18800. for (i = 3; i <= num; i += 2) {
  18801. RECREATE(qi->req, struct questinfo_req, qi->req_count+1);
  18802. qi->req[qi->req_count].quest_id = script_getnum(st, i);
  18803. qi->req[qi->req_count].state = (script_getnum(st, i+1) >= 2) ? 2 : (script_getnum(st, i+1) <= 0) ? 0 : 1;
  18804. qi->req_count++;
  18805. }
  18806. return SCRIPT_CMD_SUCCESS;
  18807. }
  18808. /**
  18809. * Set the quest info of quest_id only showed for player that has specified Job
  18810. * setquestinfo_job <quest_id>,<job>{,<job>...};
  18811. * @author [Cydh]
  18812. **/
  18813. BUILDIN_FUNC(setquestinfo_job) {
  18814. TBL_NPC* nd = map_id2nd(st->oid);
  18815. int quest_id = script_getnum(st, 2);
  18816. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18817. int job_id = 0;
  18818. uint8 i = 0;
  18819. uint8 num = script_lastdata(st)+1;
  18820. if (!qi) {
  18821. ShowError("buildin_setquestinfo_job: Quest with ID '%d' is not defined yet.\n", quest_id);
  18822. return SCRIPT_CMD_FAILURE;
  18823. }
  18824. for (i = 3; i < num; i++) {
  18825. job_id = script_getnum(st, i);
  18826. if (!pcdb_checkid(job_id)) {
  18827. ShowError("buildin_setquestinfo_job: Invalid job id '%d' in Quest with ID %d.\n", job_id, quest_id);
  18828. continue;
  18829. }
  18830. buildin_questinfo_setjob(qi, job_id);
  18831. }
  18832. return SCRIPT_CMD_SUCCESS;
  18833. }
  18834. /**
  18835. * opendressroom(<flag>{,<char_id>});
  18836. */
  18837. BUILDIN_FUNC(opendressroom)
  18838. {
  18839. #if PACKETVER >= 20150513
  18840. int flag = 1;
  18841. TBL_PC* sd;
  18842. if( script_hasdata(st,2) )
  18843. flag = script_getnum(st,2);
  18844. if (!script_charid2sd(3, sd))
  18845. return SCRIPT_CMD_FAILURE;
  18846. clif_dressing_room(sd, flag);
  18847. return SCRIPT_CMD_SUCCESS;
  18848. #else
  18849. return SCRIPT_CMD_FAILURE;
  18850. #endif
  18851. }
  18852. /**
  18853. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  18854. */
  18855. BUILDIN_FUNC(navigateto){
  18856. #if PACKETVER >= 20111010
  18857. TBL_PC* sd;
  18858. const char *mapname;
  18859. uint16 x = 0, y = 0, monster_id = 0;
  18860. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  18861. bool hideWindow = true;
  18862. mapname = script_getstr(st,2);
  18863. if( script_hasdata(st,3) )
  18864. x = script_getnum(st,3);
  18865. if( script_hasdata(st,4) )
  18866. y = script_getnum(st,4);
  18867. if( script_hasdata(st,5) )
  18868. flag = (uint8)script_getnum(st,5);
  18869. if( script_hasdata(st,6) )
  18870. hideWindow = script_getnum(st,6) ? true : false;
  18871. if( script_hasdata(st,7) )
  18872. monster_id = script_getnum(st,7);
  18873. if (!script_charid2sd(8, sd))
  18874. return SCRIPT_CMD_FAILURE;
  18875. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  18876. return SCRIPT_CMD_SUCCESS;
  18877. #else
  18878. return SCRIPT_CMD_FAILURE;
  18879. #endif
  18880. }
  18881. /**
  18882. * adopt("<parent_name>","<baby_name>");
  18883. * adopt(<parent_id>,<baby_id>);
  18884. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  18885. */
  18886. BUILDIN_FUNC(adopt)
  18887. {
  18888. TBL_PC *sd, *b_sd;
  18889. struct script_data *data;
  18890. enum adopt_responses response;
  18891. data = script_getdata(st, 2);
  18892. get_val(st, data);
  18893. if (data_isstring(data)) {
  18894. const char *name = conv_str(st, data);
  18895. sd = map_nick2sd(name,false);
  18896. if (sd == NULL) {
  18897. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  18898. return SCRIPT_CMD_FAILURE;
  18899. }
  18900. } else if (data_isint(data)) {
  18901. uint32 char_id = conv_num(st, data);
  18902. sd = map_charid2sd(char_id);
  18903. if (sd == NULL) {
  18904. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  18905. return SCRIPT_CMD_FAILURE;
  18906. }
  18907. } else {
  18908. ShowError("buildin_adopt: Invalid data type for argument #1 (%d).", data->type);
  18909. return SCRIPT_CMD_FAILURE;
  18910. }
  18911. data = script_getdata(st, 3);
  18912. get_val(st, data);
  18913. if (data_isstring(data)) {
  18914. const char *name = conv_str(st, data);
  18915. b_sd = map_nick2sd(name,false);
  18916. if (b_sd == NULL) {
  18917. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  18918. return SCRIPT_CMD_FAILURE;
  18919. }
  18920. } else if (data_isint(data)) {
  18921. uint32 char_id = conv_num(st, data);
  18922. b_sd = map_charid2sd(char_id);
  18923. if (b_sd == NULL) {
  18924. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  18925. return SCRIPT_CMD_FAILURE;
  18926. }
  18927. } else {
  18928. ShowError("buildin_adopt: Invalid data type for argument #2 (%d).", data->type);
  18929. return SCRIPT_CMD_FAILURE;
  18930. }
  18931. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  18932. if (response == ADOPT_ALLOWED) {
  18933. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  18934. b_sd->adopt_invite = sd->status.account_id;
  18935. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  18936. script_pushint(st, ADOPT_ALLOWED);
  18937. return SCRIPT_CMD_SUCCESS;
  18938. }
  18939. script_pushint(st, response);
  18940. return SCRIPT_CMD_FAILURE;
  18941. }
  18942. /**
  18943. * Returns the minimum or maximum of all the given parameters for integer variables.
  18944. *
  18945. * min( <value or array>{,value or array 2,...} );
  18946. * minimum( <value or array>{,value or array 2,...} );
  18947. * max( <value or array>{,value or array 2,...} );
  18948. * maximum( <value or array>{,value or array 2,...} );
  18949. */
  18950. BUILDIN_FUNC(minmax){
  18951. char *functionname;
  18952. unsigned int i;
  18953. int value;
  18954. // Function pointer for our comparison function (either min or max at the moment)
  18955. int32 (*func)(int32, int32);
  18956. // Get the real function name
  18957. functionname = script_getfuncname(st);
  18958. // Our data should start at offset 2
  18959. i = 2;
  18960. if( !script_hasdata( st, i ) ){
  18961. ShowError( "buildin_%s: no arguments given!\n", functionname );
  18962. st->state = END;
  18963. return SCRIPT_CMD_FAILURE;
  18964. }
  18965. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  18966. value = INT_MAX;
  18967. func = i32min;
  18968. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  18969. value = INT_MIN;
  18970. func = i32max;
  18971. }else{
  18972. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  18973. st->state = END;
  18974. return SCRIPT_CMD_FAILURE;
  18975. }
  18976. // As long as we have data on our script stack
  18977. while( script_hasdata(st,i) ){
  18978. struct script_data *data;
  18979. // Get the next piece of data from the script stack
  18980. data = script_getdata( st, i );
  18981. // Is the current parameter a single integer?
  18982. if( data_isint( data ) ){
  18983. value = func( value, script_getnum( st, i ) );
  18984. // Is the current parameter an array variable?
  18985. }else if( data_isreference( data ) ){
  18986. const char *name;
  18987. struct map_session_data* sd;
  18988. unsigned int start, end;
  18989. // Get the name of the variable
  18990. name = reference_getname(data);
  18991. // Check if it's a string variable
  18992. if( is_string_variable( name ) ){
  18993. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  18994. script_reportdata( data );
  18995. st->state = END;
  18996. return SCRIPT_CMD_FAILURE;
  18997. }
  18998. // Get the session data, if a player is attached
  18999. sd = st->rid ? map_id2sd(st->rid) : NULL;
  19000. // Try to find the array's source pointer
  19001. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  19002. ShowError( "buildin_%s: not a array!\n", functionname );
  19003. script_reportdata( data );
  19004. st->state = END;
  19005. return SCRIPT_CMD_FAILURE;
  19006. }
  19007. // Get the start and end indices of the array
  19008. start = reference_getindex( data );
  19009. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  19010. // Skip empty arrays
  19011. if( start < end ){
  19012. int id;
  19013. // For getting the values we need the id of the array
  19014. id = reference_getid( data );
  19015. // Loop through each value stored in the array
  19016. for( ; start < end; start++ ){
  19017. value = func( value, (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) ) );
  19018. script_removetop( st, -1, 0 );
  19019. }
  19020. }
  19021. }else{
  19022. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  19023. script_reportdata( data );
  19024. st->state = END;
  19025. return SCRIPT_CMD_FAILURE;
  19026. }
  19027. // Continue with the next stack entry
  19028. i++;
  19029. }
  19030. script_pushint( st, value );
  19031. return SCRIPT_CMD_SUCCESS;
  19032. }
  19033. /**
  19034. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  19035. * Unlike `getexp` that affected by some adjustments.
  19036. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  19037. * @author [Cydh]
  19038. **/
  19039. BUILDIN_FUNC(getexp2) {
  19040. TBL_PC *sd = NULL;
  19041. int base_exp = script_getnum(st, 2);
  19042. int job_exp = script_getnum(st, 3);
  19043. if (!script_charid2sd(4, sd))
  19044. return SCRIPT_CMD_FAILURE;
  19045. if (base_exp == 0 && job_exp == 0)
  19046. return SCRIPT_CMD_SUCCESS;
  19047. if (base_exp > 0)
  19048. pc_gainexp(sd, NULL, base_exp, 0, 2);
  19049. else if (base_exp < 0)
  19050. pc_lostexp(sd, base_exp * -1, 0);
  19051. if (job_exp > 0)
  19052. pc_gainexp(sd, NULL, 0, job_exp, 2);
  19053. else if (job_exp < 0)
  19054. pc_lostexp(sd, 0, job_exp * -1);
  19055. return SCRIPT_CMD_SUCCESS;
  19056. }
  19057. /**
  19058. * Force stat recalculation of sd
  19059. * recalculatestat;
  19060. * @author [secretdataz]
  19061. **/
  19062. BUILDIN_FUNC(recalculatestat) {
  19063. TBL_PC* sd = script_rid2sd(st);
  19064. if (sd == NULL)
  19065. return SCRIPT_CMD_FAILURE;
  19066. status_calc_pc(sd, SCO_FORCE);
  19067. return SCRIPT_CMD_SUCCESS;
  19068. }
  19069. BUILDIN_FUNC(hateffect){
  19070. #if PACKETVER >= 20150513
  19071. struct map_session_data* sd = script_rid2sd(st);
  19072. bool enable;
  19073. int i, effectID;
  19074. if( sd == NULL )
  19075. return SCRIPT_CMD_FAILURE;
  19076. effectID = script_getnum(st,2);
  19077. enable = script_getnum(st,3) ? true : false;
  19078. ARR_FIND( 0, sd->hatEffectCount, i, sd->hatEffectIDs[i] == effectID );
  19079. if( enable ){
  19080. if( i < sd->hatEffectCount ){
  19081. return SCRIPT_CMD_SUCCESS;
  19082. }
  19083. RECREATE(sd->hatEffectIDs,uint32,sd->hatEffectCount+1);
  19084. sd->hatEffectIDs[sd->hatEffectCount] = effectID;
  19085. sd->hatEffectCount++;
  19086. }else{
  19087. if( i == sd->hatEffectCount ){
  19088. return SCRIPT_CMD_SUCCESS;
  19089. }
  19090. for( ; i < sd->hatEffectCount - 1; i++ ){
  19091. sd->hatEffectIDs[i] = sd->hatEffectIDs[i+1];
  19092. }
  19093. sd->hatEffectCount--;
  19094. if( !sd->hatEffectCount ){
  19095. aFree(sd->hatEffectIDs);
  19096. sd->hatEffectIDs = NULL;
  19097. }
  19098. }
  19099. if( !sd->state.connect_new ){
  19100. clif_hat_effect_single( sd, effectID, enable );
  19101. }
  19102. #endif
  19103. return SCRIPT_CMD_SUCCESS;
  19104. }
  19105. /**
  19106. * Retrieves param of current random option. Intended for random option script only.
  19107. * getrandomoptinfo(<type>);
  19108. * @author [secretdataz]
  19109. **/
  19110. BUILDIN_FUNC(getrandomoptinfo) {
  19111. struct map_session_data *sd;
  19112. int val;
  19113. int param = script_getnum(st, 2);
  19114. 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) {
  19115. switch (param) {
  19116. case ROA_ID:
  19117. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  19118. break;
  19119. case ROA_VALUE:
  19120. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  19121. break;
  19122. case ROA_PARAM:
  19123. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  19124. break;
  19125. default:
  19126. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  19127. val = 0;
  19128. break;
  19129. }
  19130. script_pushint(st, val);
  19131. }
  19132. else {
  19133. script_pushint(st, 0);
  19134. }
  19135. return SCRIPT_CMD_SUCCESS;
  19136. }
  19137. /**
  19138. * Retrieves a random option on an equipped item.
  19139. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  19140. * @author [secretdataz]
  19141. */
  19142. BUILDIN_FUNC(getequiprandomoption) {
  19143. struct map_session_data *sd;
  19144. int val;
  19145. short i = -1;
  19146. int pos = script_getnum(st, 2);
  19147. int index = script_getnum(st, 3);
  19148. int type = script_getnum(st, 4);
  19149. if (!script_charid2sd(5, sd)) {
  19150. script_pushint(st, -1);
  19151. return SCRIPT_CMD_FAILURE;
  19152. }
  19153. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19154. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  19155. script_pushint(st, -1);
  19156. return SCRIPT_CMD_FAILURE;
  19157. }
  19158. if (equip_index_check(pos))
  19159. i = pc_checkequip(sd, equip_bitmask[pos]);
  19160. if (i < 0) {
  19161. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19162. script_pushint(st, -1);
  19163. return SCRIPT_CMD_FAILURE;
  19164. }
  19165. switch (type) {
  19166. case ROA_ID:
  19167. val = sd->inventory.u.items_inventory[i].option[index].id;
  19168. break;
  19169. case ROA_VALUE:
  19170. val = sd->inventory.u.items_inventory[i].option[index].value;
  19171. break;
  19172. case ROA_PARAM:
  19173. val = sd->inventory.u.items_inventory[i].option[index].param;
  19174. break;
  19175. default:
  19176. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  19177. val = 0;
  19178. break;
  19179. }
  19180. script_pushint(st, val);
  19181. return SCRIPT_CMD_SUCCESS;
  19182. }
  19183. /**
  19184. * Adds a random option on a random option slot on an equipped item and overwrites
  19185. * existing random option in the process.
  19186. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  19187. * @author [secretdataz]
  19188. */
  19189. BUILDIN_FUNC(setrandomoption) {
  19190. struct map_session_data *sd;
  19191. struct s_random_opt_data *opt;
  19192. int pos, index, id, value, param, ep;
  19193. int i = -1;
  19194. if (!script_charid2sd(7, sd))
  19195. return SCRIPT_CMD_FAILURE;
  19196. pos = script_getnum(st, 2);
  19197. index = script_getnum(st, 3);
  19198. id = script_getnum(st, 4);
  19199. value = script_getnum(st, 5);
  19200. param = script_getnum(st, 6);
  19201. if ((opt = itemdb_randomopt_exists((short)id)) == NULL) {
  19202. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  19203. script_pushint(st, 0);
  19204. return SCRIPT_CMD_FAILURE;
  19205. }
  19206. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19207. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  19208. script_pushint(st, 0);
  19209. return SCRIPT_CMD_FAILURE;
  19210. }
  19211. if (equip_index_check(pos))
  19212. i = pc_checkequip(sd, equip_bitmask[pos]);
  19213. if (i >= 0) {
  19214. ep = sd->inventory.u.items_inventory[i].equip;
  19215. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19216. pc_unequipitem(sd, i, 2);
  19217. sd->inventory.u.items_inventory[i].option[index].id = id;
  19218. sd->inventory.u.items_inventory[i].option[index].value = value;
  19219. sd->inventory.u.items_inventory[i].option[index].param = param;
  19220. clif_delitem(sd, i, 1, 3);
  19221. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19222. clif_additem(sd, i, 1, 0);
  19223. pc_equipitem(sd, i, ep);
  19224. script_pushint(st, 1);
  19225. return SCRIPT_CMD_SUCCESS;
  19226. }
  19227. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19228. script_pushint(st, 0);
  19229. return SCRIPT_CMD_FAILURE;
  19230. }
  19231. /// Returns the number of stat points needed to change the specified stat by val.
  19232. /// If val is negative, returns the number of stat points that would be needed to
  19233. /// raise the specified stat from (current value - val) to current value.
  19234. /// *needed_status_point(<type>,<val>{,<char id>});
  19235. /// @author [secretdataz]
  19236. BUILDIN_FUNC(needed_status_point) {
  19237. struct map_session_data *sd;
  19238. int type, val;
  19239. if (!script_charid2sd(4, sd))
  19240. return SCRIPT_CMD_FAILURE;
  19241. type = script_getnum(st, 2);
  19242. val = script_getnum(st, 3);
  19243. script_pushint(st, pc_need_status_point(sd, type, val));
  19244. return SCRIPT_CMD_SUCCESS;
  19245. }
  19246. /**
  19247. * jobcanentermap("<mapname>"{,<JobID>});
  19248. * Check if (player with) JobID can enter the map.
  19249. * @param mapname Map name
  19250. * @param JobID Player's JobID (optional)
  19251. **/
  19252. BUILDIN_FUNC(jobcanentermap) {
  19253. const char *mapname = script_getstr(st, 2);
  19254. int mapidx = mapindex_name2id(mapname), m = -1;
  19255. int jobid = 0;
  19256. TBL_PC *sd = NULL;
  19257. if (!mapidx) {// Invalid map
  19258. script_pushint(st, false);
  19259. return SCRIPT_CMD_FAILURE;
  19260. }
  19261. m = map_mapindex2mapid(mapidx);
  19262. if (m == -1) { // Map is on different map server
  19263. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  19264. script_pushint(st, false);
  19265. return SCRIPT_CMD_FAILURE;
  19266. }
  19267. if (script_hasdata(st, 3)) {
  19268. jobid = script_getnum(st, 3);
  19269. } else {
  19270. if (!(sd = script_rid2sd(st))) {
  19271. script_pushint(st, false);
  19272. return SCRIPT_CMD_FAILURE;
  19273. }
  19274. jobid = sd->status.class_;
  19275. }
  19276. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? sd->group_level : 0));
  19277. return SCRIPT_CMD_SUCCESS;
  19278. }
  19279. /**
  19280. * Return alliance information between the two guilds.
  19281. * getguildalliance(<guild id1>,<guild id2>);
  19282. * Return values:
  19283. * -2 - Guild ID1 does not exist
  19284. * -1 - Guild ID2 does not exist
  19285. * 0 - Both guilds have no relation OR guild ID aren't given
  19286. * 1 - Both guilds are allies
  19287. * 2 - Both guilds are antagonists
  19288. */
  19289. BUILDIN_FUNC(getguildalliance)
  19290. {
  19291. struct guild *guild_data1, *guild_data2;
  19292. int guild_id1, guild_id2, i = 0;
  19293. guild_id1 = script_getnum(st,2);
  19294. guild_id2 = script_getnum(st,3);
  19295. if (guild_id1 < 1 || guild_id2 < 1) {
  19296. script_pushint(st, 0);
  19297. return SCRIPT_CMD_SUCCESS;
  19298. }
  19299. if (guild_id1 == guild_id2) {
  19300. script_pushint(st, 1);
  19301. return SCRIPT_CMD_SUCCESS;
  19302. }
  19303. guild_data1 = guild_search(guild_id1);
  19304. guild_data2 = guild_search(guild_id2);
  19305. if (guild_data1 == NULL) {
  19306. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  19307. script_pushint(st, -2);
  19308. return SCRIPT_CMD_FAILURE;
  19309. }
  19310. if (guild_data2 == NULL) {
  19311. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  19312. script_pushint(st, -1);
  19313. return SCRIPT_CMD_FAILURE;
  19314. }
  19315. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  19316. if (i == MAX_GUILDALLIANCE) {
  19317. script_pushint(st, 0);
  19318. return SCRIPT_CMD_SUCCESS;
  19319. }
  19320. if (guild_data1->alliance[i].opposition)
  19321. script_pushint(st, 2);
  19322. else
  19323. script_pushint(st, 1);
  19324. return SCRIPT_CMD_SUCCESS;
  19325. }
  19326. /*
  19327. * openstorage2 <storage_id>,<mode>{,<account_id>}
  19328. * mode @see enum e_storage_mode
  19329. **/
  19330. BUILDIN_FUNC(openstorage2) {
  19331. int stor_id = script_getnum(st, 2);
  19332. TBL_PC *sd = NULL;
  19333. if (!script_accid2sd(4, sd)) {
  19334. script_pushint(st, 0);
  19335. return SCRIPT_CMD_FAILURE;
  19336. }
  19337. if (!storage_exists(stor_id)) {
  19338. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  19339. return SCRIPT_CMD_FAILURE;
  19340. }
  19341. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  19342. return SCRIPT_CMD_SUCCESS;
  19343. }
  19344. #include "../custom/script.inc"
  19345. // declarations that were supposed to be exported from npc_chat.c
  19346. #ifdef PCRE_SUPPORT
  19347. BUILDIN_FUNC(defpattern);
  19348. BUILDIN_FUNC(activatepset);
  19349. BUILDIN_FUNC(deactivatepset);
  19350. BUILDIN_FUNC(deletepset);
  19351. #endif
  19352. /** Regular expression matching
  19353. * preg_match(<pattern>,<string>{,<offset>})
  19354. */
  19355. BUILDIN_FUNC(preg_match) {
  19356. #ifdef PCRE_SUPPORT
  19357. pcre *re;
  19358. pcre_extra *pcreExtra;
  19359. const char *error;
  19360. int erroffset, r, offset = 0;
  19361. int subStrVec[30];
  19362. const char* pattern = script_getstr(st,2);
  19363. const char* subject = script_getstr(st,3);
  19364. if (script_hasdata(st,4))
  19365. offset = script_getnum(st,4);
  19366. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  19367. pcreExtra = pcre_study(re, 0, &error);
  19368. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  19369. pcre_free(re);
  19370. if (pcreExtra != NULL)
  19371. pcre_free(pcreExtra);
  19372. if (r < 0)
  19373. script_pushint(st,0);
  19374. else
  19375. script_pushint(st,(r > 0) ? r : 30 / 3);
  19376. return SCRIPT_CMD_SUCCESS;
  19377. #else
  19378. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  19379. script_pushint(st,0);
  19380. return SCRIPT_CMD_SUCCESS;
  19381. #endif
  19382. }
  19383. /// script command definitions
  19384. /// for an explanation on args, see add_buildin_func
  19385. struct script_function buildin_func[] = {
  19386. // NPC interaction
  19387. BUILDIN_DEF(mes,"s*"),
  19388. BUILDIN_DEF(next,""),
  19389. BUILDIN_DEF(close,""),
  19390. BUILDIN_DEF(close2,""),
  19391. BUILDIN_DEF(menu,"sl*"),
  19392. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  19393. BUILDIN_DEF(prompt,"s*"),
  19394. //
  19395. BUILDIN_DEF(goto,"l"),
  19396. BUILDIN_DEF(callsub,"l*"),
  19397. BUILDIN_DEF(callfunc,"s*"),
  19398. BUILDIN_DEF(return,"?"),
  19399. BUILDIN_DEF(getarg,"i?"),
  19400. BUILDIN_DEF(jobchange,"i??"),
  19401. BUILDIN_DEF(jobname,"i"),
  19402. BUILDIN_DEF(input,"r??"),
  19403. BUILDIN_DEF(warp,"sii?"),
  19404. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  19405. BUILDIN_DEF(areawarp,"siiiisii??"),
  19406. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  19407. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  19408. BUILDIN_DEF(setlook,"ii?"),
  19409. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  19410. BUILDIN_DEF2(setr,"set","rv?"),
  19411. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  19412. BUILDIN_DEF(setarray,"rv*"),
  19413. BUILDIN_DEF(cleararray,"rvi"),
  19414. BUILDIN_DEF(copyarray,"rri"),
  19415. BUILDIN_DEF(getarraysize,"r"),
  19416. BUILDIN_DEF(deletearray,"r?"),
  19417. BUILDIN_DEF(getelementofarray,"ri"),
  19418. BUILDIN_DEF(getitem,"vi?"),
  19419. BUILDIN_DEF(rentitem,"vi?"),
  19420. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  19421. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  19422. BUILDIN_DEF(getnameditem,"vv"),
  19423. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  19424. BUILDIN_DEF(makeitem,"visii"),
  19425. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  19426. BUILDIN_DEF(delitem,"vi?"),
  19427. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  19428. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  19429. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  19430. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  19431. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  19432. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  19433. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  19434. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  19435. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  19436. BUILDIN_DEF(cutin,"si"),
  19437. BUILDIN_DEF(viewpoint,"iiiii"),
  19438. BUILDIN_DEF(heal,"ii?"),
  19439. BUILDIN_DEF(itemheal,"ii?"),
  19440. BUILDIN_DEF(percentheal,"ii?"),
  19441. BUILDIN_DEF(rand,"i?"),
  19442. BUILDIN_DEF(countitem,"v?"),
  19443. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  19444. BUILDIN_DEF2(countitem,"guildstoragecountitem","v?"),
  19445. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  19446. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  19447. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  19448. BUILDIN_DEF2(countitem,"guildstoragecountitem2","viiiiiii?"),
  19449. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  19450. BUILDIN_DEF(checkweight,"vi*"),
  19451. BUILDIN_DEF(checkweight2,"rr"),
  19452. BUILDIN_DEF(readparam,"i?"),
  19453. BUILDIN_DEF(getcharid,"i?"),
  19454. BUILDIN_DEF(getnpcid,"i?"),
  19455. BUILDIN_DEF(getpartyname,"i"),
  19456. BUILDIN_DEF(getpartymember,"i??"),
  19457. BUILDIN_DEF(getpartyleader,"i?"),
  19458. BUILDIN_DEF(getguildname,"i"),
  19459. BUILDIN_DEF(getguildmaster,"i"),
  19460. BUILDIN_DEF(getguildmasterid,"i"),
  19461. BUILDIN_DEF(strcharinfo,"i?"),
  19462. BUILDIN_DEF(strnpcinfo,"i"),
  19463. BUILDIN_DEF(getequipid,"i?"),
  19464. BUILDIN_DEF(getequipuniqueid,"i?"),
  19465. BUILDIN_DEF(getequipname,"i?"),
  19466. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  19467. BUILDIN_DEF(repair,"i?"), // [Valaris]
  19468. BUILDIN_DEF(repairall,"?"),
  19469. BUILDIN_DEF(getequipisequiped,"i?"),
  19470. BUILDIN_DEF(getequipisenableref,"i?"),
  19471. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  19472. BUILDIN_DEF(getequipweaponlv,"i?"),
  19473. BUILDIN_DEF(getequippercentrefinery,"i?"),
  19474. BUILDIN_DEF(successrefitem,"i??"),
  19475. BUILDIN_DEF(failedrefitem,"i?"),
  19476. BUILDIN_DEF(downrefitem,"i??"),
  19477. BUILDIN_DEF(statusup,"i?"),
  19478. BUILDIN_DEF(statusup2,"ii?"),
  19479. BUILDIN_DEF(bonus,"iv"),
  19480. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  19481. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  19482. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  19483. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  19484. BUILDIN_DEF(autobonus,"sii??"),
  19485. BUILDIN_DEF(autobonus2,"sii??"),
  19486. BUILDIN_DEF(autobonus3,"siiv?"),
  19487. BUILDIN_DEF(skill,"vi?"),
  19488. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  19489. BUILDIN_DEF(guildskill,"vi"),
  19490. BUILDIN_DEF(getskilllv,"v"),
  19491. BUILDIN_DEF(getgdskilllv,"iv"),
  19492. BUILDIN_DEF(basicskillcheck,""),
  19493. BUILDIN_DEF(getgmlevel,"?"),
  19494. BUILDIN_DEF(getgroupid,"?"),
  19495. BUILDIN_DEF(end,""),
  19496. BUILDIN_DEF(checkoption,"i?"),
  19497. BUILDIN_DEF(setoption,"i??"),
  19498. BUILDIN_DEF(setcart,"??"),
  19499. BUILDIN_DEF(checkcart,"?"),
  19500. BUILDIN_DEF(setfalcon,"??"),
  19501. BUILDIN_DEF(checkfalcon,"?"),
  19502. BUILDIN_DEF(setriding,"??"),
  19503. BUILDIN_DEF(checkriding,"?"),
  19504. BUILDIN_DEF(checkwug,"?"),
  19505. BUILDIN_DEF(checkmadogear,"?"),
  19506. BUILDIN_DEF(setmadogear,"??"),
  19507. BUILDIN_DEF2(savepoint,"save","sii???"),
  19508. BUILDIN_DEF(savepoint,"sii???"),
  19509. BUILDIN_DEF(gettimetick,"i"),
  19510. BUILDIN_DEF(gettime,"i"),
  19511. BUILDIN_DEF(gettimestr,"si"),
  19512. BUILDIN_DEF(openstorage,""),
  19513. BUILDIN_DEF(guildopenstorage,""),
  19514. BUILDIN_DEF(itemskill,"vi?"),
  19515. BUILDIN_DEF(produce,"i"),
  19516. BUILDIN_DEF(cooking,"i"),
  19517. BUILDIN_DEF(monster,"siisii???"),
  19518. BUILDIN_DEF(getmobdrops,"i"),
  19519. BUILDIN_DEF(areamonster,"siiiisii???"),
  19520. BUILDIN_DEF(killmonster,"ss?"),
  19521. BUILDIN_DEF(killmonsterall,"s?"),
  19522. BUILDIN_DEF(clone,"siisi????"),
  19523. BUILDIN_DEF(doevent,"s"),
  19524. BUILDIN_DEF(donpcevent,"s"),
  19525. BUILDIN_DEF(cmdothernpc,"ss"),
  19526. BUILDIN_DEF(addtimer,"is"),
  19527. BUILDIN_DEF(deltimer,"s"),
  19528. BUILDIN_DEF(addtimercount,"is"),
  19529. BUILDIN_DEF(initnpctimer,"??"),
  19530. BUILDIN_DEF(stopnpctimer,"??"),
  19531. BUILDIN_DEF(startnpctimer,"??"),
  19532. BUILDIN_DEF(setnpctimer,"i?"),
  19533. BUILDIN_DEF(getnpctimer,"i?"),
  19534. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  19535. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  19536. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  19537. BUILDIN_DEF(announce,"si?????"),
  19538. BUILDIN_DEF(mapannounce,"ssi?????"),
  19539. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  19540. BUILDIN_DEF(getusers,"i"),
  19541. BUILDIN_DEF(getmapguildusers,"si"),
  19542. BUILDIN_DEF(getmapusers,"s"),
  19543. BUILDIN_DEF(getareausers,"siiii"),
  19544. BUILDIN_DEF(getareadropitem,"siiiiv"),
  19545. BUILDIN_DEF(enablenpc,"s"),
  19546. BUILDIN_DEF(disablenpc,"s"),
  19547. BUILDIN_DEF(hideoffnpc,"s"),
  19548. BUILDIN_DEF(hideonnpc,"s"),
  19549. BUILDIN_DEF(sc_start,"iii???"),
  19550. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  19551. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  19552. BUILDIN_DEF(sc_end,"i?"),
  19553. BUILDIN_DEF(getstatus, "i??"),
  19554. BUILDIN_DEF(getscrate,"ii?"),
  19555. BUILDIN_DEF(debugmes,"s"),
  19556. BUILDIN_DEF2(catchpet,"pet","i"),
  19557. BUILDIN_DEF2(birthpet,"bpet",""),
  19558. BUILDIN_DEF(resetlvl,"i?"),
  19559. BUILDIN_DEF(resetstatus,"?"),
  19560. BUILDIN_DEF(resetskill,"?"),
  19561. BUILDIN_DEF(skillpointcount,"?"),
  19562. BUILDIN_DEF(changebase,"i?"),
  19563. BUILDIN_DEF(changesex,"?"),
  19564. BUILDIN_DEF(changecharsex,"?"),
  19565. BUILDIN_DEF(waitingroom,"si?????"),
  19566. BUILDIN_DEF(delwaitingroom,"?"),
  19567. BUILDIN_DEF(waitingroomkick,"ss"),
  19568. BUILDIN_DEF(getwaitingroomusers, "?"),
  19569. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  19570. BUILDIN_DEF(enablewaitingroomevent,"?"),
  19571. BUILDIN_DEF(disablewaitingroomevent,"?"),
  19572. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  19573. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  19574. BUILDIN_DEF(getwaitingroomstate,"i?"),
  19575. BUILDIN_DEF(warpwaitingpc,"sii?"),
  19576. BUILDIN_DEF(attachrid,"i"),
  19577. BUILDIN_DEF(addrid,"i?????"),
  19578. BUILDIN_DEF(detachrid,""),
  19579. BUILDIN_DEF(isloggedin,"i?"),
  19580. BUILDIN_DEF(setmapflagnosave,"ssii"),
  19581. BUILDIN_DEF(getmapflag,"si?"),
  19582. BUILDIN_DEF(setmapflag,"si??"),
  19583. BUILDIN_DEF(removemapflag,"si?"),
  19584. BUILDIN_DEF(pvpon,"s"),
  19585. BUILDIN_DEF(pvpoff,"s"),
  19586. BUILDIN_DEF(gvgon,"s"),
  19587. BUILDIN_DEF(gvgoff,"s"),
  19588. BUILDIN_DEF(emotion,"i??"),
  19589. BUILDIN_DEF(maprespawnguildid,"sii"),
  19590. BUILDIN_DEF(agitstart,""), // <Agit>
  19591. BUILDIN_DEF(agitend,""),
  19592. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  19593. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  19594. BUILDIN_DEF(getcastlename,"s"),
  19595. BUILDIN_DEF(getcastledata,"si"),
  19596. BUILDIN_DEF(setcastledata,"sii"),
  19597. BUILDIN_DEF(requestguildinfo,"i?"),
  19598. BUILDIN_DEF(getequipcardcnt,"i"),
  19599. BUILDIN_DEF(successremovecards,"i"),
  19600. BUILDIN_DEF(failedremovecards,"ii"),
  19601. BUILDIN_DEF(marriage,"s"),
  19602. BUILDIN_DEF2(wedding_effect,"wedding",""),
  19603. BUILDIN_DEF(divorce,"?"),
  19604. BUILDIN_DEF(ispartneron,"?"),
  19605. BUILDIN_DEF(getpartnerid,"?"),
  19606. BUILDIN_DEF(getchildid,"?"),
  19607. BUILDIN_DEF(getmotherid,"?"),
  19608. BUILDIN_DEF(getfatherid,"?"),
  19609. BUILDIN_DEF(warppartner,"sii"),
  19610. BUILDIN_DEF(getitemname,"v"),
  19611. BUILDIN_DEF(getitemslots,"i"),
  19612. BUILDIN_DEF(makepet,"i"),
  19613. BUILDIN_DEF(getexp,"ii?"),
  19614. BUILDIN_DEF(getinventorylist,"?"),
  19615. BUILDIN_DEF(getskilllist,"?"),
  19616. BUILDIN_DEF(clearitem,"?"),
  19617. BUILDIN_DEF(classchange,"ii"),
  19618. BUILDIN_DEF(misceffect,"i"),
  19619. BUILDIN_DEF(playBGM,"s"),
  19620. BUILDIN_DEF(playBGMall,"s?????"),
  19621. BUILDIN_DEF(soundeffect,"si"),
  19622. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  19623. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  19624. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  19625. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  19626. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  19627. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  19628. BUILDIN_DEF(petloot,"i"), // [Valaris]
  19629. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  19630. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  19631. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  19632. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  19633. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  19634. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  19635. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  19636. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  19637. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  19638. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  19639. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  19640. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  19641. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  19642. BUILDIN_DEF(npctalk,"s?"), // [Valaris]
  19643. BUILDIN_DEF(mobcount,"ss"),
  19644. BUILDIN_DEF(getlook,"i?"),
  19645. BUILDIN_DEF(getsavepoint,"i?"),
  19646. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  19647. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  19648. BUILDIN_DEF(npcstop,""), // [Valaris]
  19649. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  19650. BUILDIN_DEF(mapid2name,"i"),
  19651. BUILDIN_DEF(checkoption1,"i?"),
  19652. BUILDIN_DEF(checkoption2,"i?"),
  19653. BUILDIN_DEF(guildgetexp,"i"),
  19654. BUILDIN_DEF(guildchangegm,"is"),
  19655. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  19656. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  19657. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  19658. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  19659. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  19660. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  19661. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  19662. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  19663. BUILDIN_DEF(night,""), // sets the server to night time
  19664. BUILDIN_DEF(day,""), // sets the server to day time
  19665. #ifdef PCRE_SUPPORT
  19666. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  19667. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  19668. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  19669. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  19670. #endif
  19671. BUILDIN_DEF(preg_match,"ss?"),
  19672. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  19673. BUILDIN_DEF(recovery,"i???"),
  19674. BUILDIN_DEF(getpetinfo,"i?"),
  19675. BUILDIN_DEF(gethominfo,"i?"),
  19676. BUILDIN_DEF(getmercinfo,"i?"),
  19677. BUILDIN_DEF(checkequipedcard,"i"),
  19678. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  19679. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  19680. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  19681. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  19682. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  19683. BUILDIN_DEF(charat,"si"),
  19684. BUILDIN_DEF(setchar,"ssi"),
  19685. BUILDIN_DEF(insertchar,"ssi"),
  19686. BUILDIN_DEF(delchar,"si"),
  19687. BUILDIN_DEF(strtoupper,"s"),
  19688. BUILDIN_DEF(strtolower,"s"),
  19689. BUILDIN_DEF(charisupper, "si"),
  19690. BUILDIN_DEF(charislower, "si"),
  19691. BUILDIN_DEF(substr,"sii"),
  19692. BUILDIN_DEF(explode, "rss"),
  19693. BUILDIN_DEF(implode, "r?"),
  19694. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  19695. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  19696. BUILDIN_DEF(strpos,"ss?"),
  19697. BUILDIN_DEF(replacestr,"sss??"),
  19698. BUILDIN_DEF(countstr,"ss?"),
  19699. BUILDIN_DEF(setnpcdisplay,"sv??"),
  19700. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  19701. BUILDIN_DEF(strcmp,"ss"),
  19702. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  19703. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  19704. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  19705. // [zBuffer] List of mathematics commands --->
  19706. BUILDIN_DEF(sqrt,"i"),
  19707. BUILDIN_DEF(pow,"ii"),
  19708. BUILDIN_DEF(distance,"iiii"),
  19709. BUILDIN_DEF2(minmax,"min", "*"),
  19710. BUILDIN_DEF2(minmax,"minimum", "*"),
  19711. BUILDIN_DEF2(minmax,"max", "*"),
  19712. BUILDIN_DEF2(minmax,"maximum", "*"),
  19713. // <--- [zBuffer] List of mathematics commands
  19714. BUILDIN_DEF(md5,"s"),
  19715. // [zBuffer] List of dynamic var commands --->
  19716. BUILDIN_DEF(getd,"s"),
  19717. BUILDIN_DEF(setd,"sv?"),
  19718. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  19719. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  19720. BUILDIN_DEF(npcshopadditem,"sii*"),
  19721. BUILDIN_DEF(npcshopdelitem,"si*"),
  19722. BUILDIN_DEF(npcshopattach,"s?"),
  19723. BUILDIN_DEF(equip,"i?"),
  19724. BUILDIN_DEF(autoequip,"ii"),
  19725. BUILDIN_DEF(setbattleflag,"si?"),
  19726. BUILDIN_DEF(getbattleflag,"s"),
  19727. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  19728. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  19729. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  19730. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  19731. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  19732. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  19733. BUILDIN_DEF(axtoi,"s"),
  19734. BUILDIN_DEF(query_sql,"s*"),
  19735. BUILDIN_DEF(query_logsql,"s*"),
  19736. BUILDIN_DEF(escape_sql,"v"),
  19737. BUILDIN_DEF(atoi,"s"),
  19738. BUILDIN_DEF(strtol,"si"),
  19739. // [zBuffer] List of player cont commands --->
  19740. BUILDIN_DEF(rid2name,"i"),
  19741. BUILDIN_DEF(pcfollow,"ii"),
  19742. BUILDIN_DEF(pcstopfollow,"i"),
  19743. BUILDIN_DEF(pcblockmove,"ii"),
  19744. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  19745. BUILDIN_DEF(pcblockskill,"ii"),
  19746. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  19747. // <--- [zBuffer] List of player cont commands
  19748. // [zBuffer] List of unit control commands --->
  19749. BUILDIN_DEF(unitexists,"i"),
  19750. BUILDIN_DEF(getunittype,"i"),
  19751. BUILDIN_DEF(getunitname,"i"),
  19752. BUILDIN_DEF(setunitname,"is"),
  19753. BUILDIN_DEF(getunitdata,"i*"),
  19754. BUILDIN_DEF(setunitdata,"iii"),
  19755. BUILDIN_DEF(unitwalk,"iii?"),
  19756. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  19757. BUILDIN_DEF(unitkill,"i"),
  19758. BUILDIN_DEF(unitwarp,"isii"),
  19759. BUILDIN_DEF(unitattack,"iv?"),
  19760. BUILDIN_DEF(unitstopattack,"i"),
  19761. BUILDIN_DEF(unitstopwalk,"i"),
  19762. BUILDIN_DEF(unittalk,"is"),
  19763. BUILDIN_DEF(unitemote,"ii"),
  19764. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  19765. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  19766. // <--- [zBuffer] List of unit control commands
  19767. BUILDIN_DEF(sleep,"i"),
  19768. BUILDIN_DEF(sleep2,"i"),
  19769. BUILDIN_DEF(awake,"s"),
  19770. BUILDIN_DEF(getvariableofnpc,"rs"),
  19771. BUILDIN_DEF(warpportal,"iisii"),
  19772. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  19773. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  19774. BUILDIN_DEF(morphembryo,""),
  19775. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  19776. BUILDIN_DEF(checkhomcall,""),
  19777. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  19778. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  19779. BUILDIN_DEF(checkvending,"?"),
  19780. BUILDIN_DEF(checkchatting,"?"),
  19781. BUILDIN_DEF(checkidle,"?"),
  19782. BUILDIN_DEF(openmail,"?"),
  19783. BUILDIN_DEF(openauction,"?"),
  19784. BUILDIN_DEF(checkcell,"siii"),
  19785. BUILDIN_DEF(setcell,"siiiiii"),
  19786. BUILDIN_DEF(setwall,"siiiiis"),
  19787. BUILDIN_DEF(delwall,"s"),
  19788. BUILDIN_DEF(searchitem,"rs"),
  19789. BUILDIN_DEF(mercenary_create,"ii"),
  19790. BUILDIN_DEF(mercenary_heal,"ii"),
  19791. BUILDIN_DEF(mercenary_sc_start,"iii"),
  19792. BUILDIN_DEF(mercenary_get_calls,"i"),
  19793. BUILDIN_DEF(mercenary_get_faith,"i"),
  19794. BUILDIN_DEF(mercenary_set_calls,"ii"),
  19795. BUILDIN_DEF(mercenary_set_faith,"ii"),
  19796. BUILDIN_DEF(readbook,"ii"),
  19797. BUILDIN_DEF(setfont,"i"),
  19798. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  19799. BUILDIN_DEF(progressbar,"si"),
  19800. BUILDIN_DEF(pushpc,"ii"),
  19801. BUILDIN_DEF(buyingstore,"i"),
  19802. BUILDIN_DEF(searchstores,"ii"),
  19803. BUILDIN_DEF(showdigit,"i?"),
  19804. // WoE SE
  19805. BUILDIN_DEF(agitstart2,""),
  19806. BUILDIN_DEF(agitend2,""),
  19807. BUILDIN_DEF(agitcheck2,""),
  19808. // BattleGround
  19809. BUILDIN_DEF(waitingroom2bg,"sii???"),
  19810. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  19811. BUILDIN_DEF(bg_team_setxy,"iii"),
  19812. BUILDIN_DEF(bg_warp,"isii"),
  19813. BUILDIN_DEF(bg_monster,"isiisi?"),
  19814. BUILDIN_DEF(bg_monster_set_team,"ii"),
  19815. BUILDIN_DEF(bg_leave,"?"),
  19816. BUILDIN_DEF(bg_destroy,"i"),
  19817. BUILDIN_DEF(areapercentheal,"siiiiii"),
  19818. BUILDIN_DEF(bg_get_data,"ii"),
  19819. BUILDIN_DEF(bg_getareausers,"isiiii"),
  19820. BUILDIN_DEF(bg_updatescore,"sii"),
  19821. BUILDIN_DEF(bg_join,"i????"),
  19822. BUILDIN_DEF(bg_create,"sii??"),
  19823. // Instancing
  19824. BUILDIN_DEF(instance_create,"s??"),
  19825. BUILDIN_DEF(instance_destroy,"?"),
  19826. BUILDIN_DEF(instance_id,""),
  19827. BUILDIN_DEF(instance_enter,"s????"),
  19828. BUILDIN_DEF(instance_npcname,"s?"),
  19829. BUILDIN_DEF(instance_mapname,"s?"),
  19830. BUILDIN_DEF(instance_warpall,"sii?"),
  19831. BUILDIN_DEF(instance_announce,"isi?????"),
  19832. BUILDIN_DEF(instance_check_party,"i???"),
  19833. BUILDIN_DEF(instance_check_guild,"i???"),
  19834. BUILDIN_DEF(instance_info,"si?"),
  19835. /**
  19836. * 3rd-related
  19837. **/
  19838. BUILDIN_DEF(makerune,"i?"),
  19839. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  19840. BUILDIN_DEF(setdragon,"??"),//[Ind]
  19841. BUILDIN_DEF(ismounting,"?"),//[Ind]
  19842. BUILDIN_DEF(setmounting,"?"),//[Ind]
  19843. BUILDIN_DEF(checkre,"i"),
  19844. /**
  19845. * rAthena and beyond!
  19846. **/
  19847. BUILDIN_DEF(getargcount,""),
  19848. BUILDIN_DEF(getcharip,"?"),
  19849. BUILDIN_DEF(is_function,"s"),
  19850. BUILDIN_DEF(get_revision,""),
  19851. BUILDIN_DEF(get_githash,""),
  19852. BUILDIN_DEF(freeloop,"?"),
  19853. BUILDIN_DEF(getrandgroupitem,"i??"),
  19854. BUILDIN_DEF(cleanmap,"s"),
  19855. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  19856. BUILDIN_DEF(npcskill,"viii"),
  19857. BUILDIN_DEF(consumeitem,"v?"),
  19858. BUILDIN_DEF(delequip,"i?"),
  19859. BUILDIN_DEF(breakequip,"i?"),
  19860. BUILDIN_DEF(sit,"?"),
  19861. BUILDIN_DEF(stand,"?"),
  19862. //@commands (script based)
  19863. BUILDIN_DEF(bindatcmd, "ss??"),
  19864. BUILDIN_DEF(unbindatcmd, "s"),
  19865. BUILDIN_DEF(useatcmd, "s"),
  19866. //Quest Log System [Inkfish]
  19867. BUILDIN_DEF(questinfo, "ii??"),
  19868. BUILDIN_DEF(setquest, "i?"),
  19869. BUILDIN_DEF(erasequest, "i?"),
  19870. BUILDIN_DEF(completequest, "i?"),
  19871. BUILDIN_DEF(checkquest, "i??"),
  19872. BUILDIN_DEF(isbegin_quest,"i?"),
  19873. BUILDIN_DEF(changequest, "ii?"),
  19874. BUILDIN_DEF(showevent, "i??"),
  19875. //Bound items [Xantara] & [Akinari]
  19876. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  19877. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  19878. BUILDIN_DEF(countbound, "??"),
  19879. // Party related
  19880. BUILDIN_DEF(party_create,"s???"),
  19881. BUILDIN_DEF(party_addmember,"ii"),
  19882. BUILDIN_DEF(party_delmember,"??"),
  19883. BUILDIN_DEF(party_changeleader,"ii"),
  19884. BUILDIN_DEF(party_changeoption,"iii"),
  19885. BUILDIN_DEF(party_destroy,"i"),
  19886. // Clan system
  19887. BUILDIN_DEF(clan_join,"i?"),
  19888. BUILDIN_DEF(clan_leave,"?"),
  19889. BUILDIN_DEF(is_clientver,"ii?"),
  19890. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  19891. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  19892. BUILDIN_DEF(vip_status,"i?"),
  19893. BUILDIN_DEF(vip_time,"i?"),
  19894. BUILDIN_DEF(bonus_script,"si????"),
  19895. BUILDIN_DEF(bonus_script_clear,"??"),
  19896. BUILDIN_DEF(getgroupitem,"i?"),
  19897. BUILDIN_DEF(enable_command,""),
  19898. BUILDIN_DEF(disable_command,""),
  19899. BUILDIN_DEF(getguildmember,"i??"),
  19900. BUILDIN_DEF(addspiritball,"ii?"),
  19901. BUILDIN_DEF(delspiritball,"i?"),
  19902. BUILDIN_DEF(countspiritball,"?"),
  19903. BUILDIN_DEF(mergeitem,"?"),
  19904. BUILDIN_DEF(mergeitem2,"??"),
  19905. BUILDIN_DEF(npcshopupdate,"sii?"),
  19906. BUILDIN_DEF(getattachedrid,""),
  19907. BUILDIN_DEF(getvar,"vi"),
  19908. BUILDIN_DEF(showscript,"s?"),
  19909. BUILDIN_DEF(ignoretimeout,"i?"),
  19910. BUILDIN_DEF(geteleminfo,"i?"),
  19911. BUILDIN_DEF(setquestinfo_level,"iii"),
  19912. BUILDIN_DEF(setquestinfo_req,"iii*"),
  19913. BUILDIN_DEF(setquestinfo_job,"ii*"),
  19914. BUILDIN_DEF(opendressroom,"i?"),
  19915. BUILDIN_DEF(navigateto,"s???????"),
  19916. BUILDIN_DEF(getguildalliance,"ii"),
  19917. BUILDIN_DEF(adopt,"vv"),
  19918. BUILDIN_DEF(getexp2,"ii?"),
  19919. BUILDIN_DEF(recalculatestat,""),
  19920. BUILDIN_DEF(hateffect,"ii"),
  19921. BUILDIN_DEF(getrandomoptinfo, "i"),
  19922. BUILDIN_DEF(getequiprandomoption, "iii?"),
  19923. BUILDIN_DEF(setrandomoption,"iiiii?"),
  19924. BUILDIN_DEF(needed_status_point,"ii?"),
  19925. BUILDIN_DEF(jobcanentermap,"s?"),
  19926. BUILDIN_DEF(openstorage2,"ii?"),
  19927. // WoE TE
  19928. BUILDIN_DEF(agitstart3,""),
  19929. BUILDIN_DEF(agitend3,""),
  19930. BUILDIN_DEF(agitcheck3,""),
  19931. BUILDIN_DEF(gvgon3,"s"),
  19932. BUILDIN_DEF(gvgoff3,"s"),
  19933. #include "../custom/script_def.inc"
  19934. {NULL,NULL,NULL},
  19935. };