script.c 650 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009
  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 "channel.h"
  49. #include "achievement.h"
  50. #include <math.h>
  51. #include <stdlib.h> // atoi, strtol, strtoll, exit
  52. #include <setjmp.h>
  53. #include <errno.h>
  54. static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func );
  55. /**
  56. * Get `sd` from a account id in `loc` param instead of attached rid
  57. * @param st Script
  58. * @param loc Location to look account id in script parameter
  59. * @param sd Variable that will be assigned
  60. * @return True if `sd` is assigned, false otherwise
  61. **/
  62. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  63. if (script_hasdata(st, loc)) {
  64. int id_ = script_getnum(st, loc);
  65. if (!(*sd = map_id2sd(id_))){
  66. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  67. return false;
  68. }else{
  69. return true;
  70. }
  71. }
  72. else
  73. return script_rid2sd_(st,sd,func);
  74. }
  75. /**
  76. * Get `sd` from a char id in `loc` param instead of attached rid
  77. * @param st Script
  78. * @param loc Location to look char id in script parameter
  79. * @param sd Variable that will be assigned
  80. * @return True if `sd` is assigned, false otherwise
  81. **/
  82. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  83. if (script_hasdata(st, loc)) {
  84. int id_ = script_getnum(st, loc);
  85. if (!(*sd = map_charid2sd(id_))){
  86. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  87. return false;
  88. }else{
  89. return true;
  90. }
  91. }
  92. else
  93. return script_rid2sd_(st,sd,func);
  94. }
  95. /**
  96. * Get `sd` from a nick in `loc` param instead of attached rid
  97. * @param st Script
  98. * @param loc Location to look nick in script parameter
  99. * @param sd Variable that will be assigned
  100. * @return True if `sd` is assigned, false otherwise
  101. **/
  102. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  103. if (script_hasdata(st, loc)) {
  104. const char *name_ = script_getstr(st, loc);
  105. if (!(*sd = map_nick2sd(name_,false))){
  106. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  107. return false;
  108. }else{
  109. return true;
  110. }
  111. }
  112. else
  113. return script_rid2sd_(st,sd,func);
  114. }
  115. /**
  116. * Get `sd` from a mapid in `loc` param instead of attached rid
  117. * @param st Script
  118. * @param loc Location to look mapid in script parameter
  119. * @param sd Variable that will be assigned
  120. * @return True if `sd` is assigned, false otherwise
  121. **/
  122. static bool script_mapid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  123. if (script_hasdata(st, loc)) {
  124. int id_ = script_getnum(st, loc);
  125. if (!(*sd = map_id2sd(id_))){
  126. ShowError("%s: Player with map id '%d' is not found.\n", func, id_);
  127. return false;
  128. }else{
  129. return true;
  130. }
  131. }
  132. else
  133. return script_rid2sd_(st,sd,func);
  134. }
  135. /**
  136. * Get `bl` from an ID in `loc` param, if ID is 0 will return the `bl` of the script's activator.
  137. * @param st Script
  138. * @param loc Location to look for ID in script parameter
  139. * @param bl Variable that will be assigned
  140. * @return True if `bl` is assigned, false otherwise
  141. **/
  142. static bool script_rid2bl_(struct script_state *st, uint8 loc, struct block_list **bl, const char *func) {
  143. int unit_id;
  144. if ( !script_hasdata(st, loc) || ( unit_id = script_getnum(st, loc) ) == 0)
  145. unit_id = st->rid;
  146. *bl = map_id2bl(unit_id);
  147. if ( *bl )
  148. return true;
  149. else {
  150. ShowError("%s: Unit with ID '%d' is not found.\n", func, unit_id);
  151. return false;
  152. }
  153. }
  154. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  155. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  156. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  157. #define script_mapid2sd(loc,sd) script_mapid2sd_(st,(loc),&(sd),__FUNCTION__)
  158. #define script_rid2sd(sd) script_rid2sd_(st,&(sd),__FUNCTION__)
  159. #define script_rid2bl(loc,bl) script_rid2bl_(st,(loc),&(bl),__FUNCTION__)
  160. /// temporary buffer for passing around compiled bytecode
  161. /// @see add_scriptb, set_label, parse_script
  162. static unsigned char* script_buf = NULL;
  163. static int script_pos = 0, script_size = 0;
  164. static inline int GETVALUE(const unsigned char* buf, int i)
  165. {
  166. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  167. }
  168. static inline void SETVALUE(unsigned char* buf, int i, int n)
  169. {
  170. buf[i] = GetByte(n, 0);
  171. buf[i+1] = GetByte(n, 1);
  172. buf[i+2] = GetByte(n, 2);
  173. }
  174. // String buffer structures.
  175. // str_data stores string information
  176. static struct str_data_struct {
  177. enum c_op type;
  178. int str;
  179. int backpatch;
  180. int label;
  181. int (*func)(struct script_state *st);
  182. int val;
  183. int next;
  184. bool deprecated;
  185. } *str_data = NULL;
  186. static int str_data_size = 0; // size of the data
  187. static int str_num = LABEL_START; // next id to be assigned
  188. // str_buf holds the strings themselves
  189. static char *str_buf;
  190. static int str_size = 0; // size of the buffer
  191. static int str_pos = 0; // next position to be assigned
  192. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  193. #define SCRIPT_HASH_SIZE 1021
  194. int str_hash[SCRIPT_HASH_SIZE];
  195. // Specifies which string hashing method to use
  196. //#define SCRIPT_HASH_DJB2
  197. //#define SCRIPT_HASH_SDBM
  198. #define SCRIPT_HASH_ELF
  199. static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
  200. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  201. static int parse_options = 0;
  202. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  203. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  204. // important buildin function references for usage in scripts
  205. static int buildin_set_ref = 0;
  206. static int buildin_callsub_ref = 0;
  207. static int buildin_callfunc_ref = 0;
  208. static int buildin_getelementofarray_ref = 0;
  209. // Caches compiled autoscript item code.
  210. // Note: This is not cleared when reloading itemdb.
  211. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
  212. struct Script_Config script_config = {
  213. 1, // warn_func_mismatch_argtypes
  214. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  215. 0, INT_MAX, // input_min_value/input_max_value
  216. // NOTE: None of these event labels should be longer than <EVENT_NAME_LENGTH> characters
  217. // PC related
  218. "OnPCDieEvent", //die_event_name
  219. "OnPCKillEvent", //kill_pc_event_name
  220. "OnNPCKillEvent", //kill_mob_event_name
  221. "OnPCLoginEvent", //login_event_name
  222. "OnPCLogoutEvent", //logout_event_name
  223. "OnPCLoadMapEvent", //loadmap_event_name
  224. "OnPCBaseLvUpEvent", //baselvup_event_name
  225. "OnPCJobLvUpEvent", //joblvup_event_name
  226. "OnPCStatCalcEvent", //stat_calc_event_name
  227. // NPC related
  228. "OnTouch_", //ontouch_event_name (runs on first visible char to enter area, picks another char if the first char leaves)
  229. "OnTouch", //ontouch2_event_name (run whenever a char walks into the OnTouch area)
  230. "OnTouchNPC", //ontouchnpc_event_name (run whenever a monster walks into the OnTouch area)
  231. "OnWhisperGlobal", //onwhisper_event_name (is executed when a player sends a whisper message to the NPC)
  232. "OnCommand", //oncommand_event_name (is executed by script command cmdothernpc)
  233. // Init related
  234. "OnInit", //init_event_name (is executed on all npcs when all npcs were loaded)
  235. "OnInterIfInit", //inter_init_event_name (is executed on inter server connection)
  236. "OnInterIfInitOnce", //inter_init_once_event_name (is only executed on the first inter server connection)
  237. // Guild related
  238. "OnGuildBreak", //guild_break_event_name (is executed on all castles of the guild that is broken)
  239. "OnAgitStart", //agit_start_event_name (is executed when WoE FE is started)
  240. "OnAgitInit", //agit_init_event_name (is executed after all castle owning guilds have been loaded)
  241. "OnAgitEnd", //agit_end_event_name (is executed when WoE FE has ended)
  242. "OnAgitStart2", //agit_start2_event_name (is executed when WoE SE is started)
  243. "OnAgitInit2", //agit_init2_event_name (is executed after all castle owning guilds have been loaded)
  244. "OnAgitEnd2", //agit_end2_event_name (is executed when WoE SE has ended)
  245. "OnAgitStart3", //agit_start3_event_name (is executed when WoE TE is started)
  246. "OnAgitInit3", //agit_init3_event_name (is executed after all castle owning guilds have been loaded)
  247. "OnAgitEnd3", //agit_end3_event_name (is executed when WoE TE has ended)
  248. // Timer related
  249. "OnTimer", //timer_event_name (is executed by a timer at the specific second)
  250. "OnMinute", //timer_minute_event_name (is executed by a timer at the specific minute)
  251. "OnHour", //timer_hour_event_name (is executed by a timer at the specific hour)
  252. "OnClock", //timer_clock_event_name (is executed by a timer at the specific hour and minute)
  253. "OnDay", //timer_day_event_name (is executed by a timer at the specific month and day)
  254. "OnSun", //timer_sunday_event_name (is executed by a timer on sunday at the specific hour and minute)
  255. "OnMon", //timer_monday_event_name (is executed by a timer on monday at the specific hour and minute)
  256. "OnTue", //timer_tuesday_event_name (is executed by a timer on tuesday at the specific hour and minute)
  257. "OnWed", //timer_wednesday_event_name (is executed by a timer on wednesday at the specific hour and minute)
  258. "OnThu", //timer_thursday_event_name (is executed by a timer on thursday at the specific hour and minute)
  259. "OnFri", //timer_friday_event_name (is executed by a timer on friday at the specific hour and minute)
  260. "OnSat", //timer_saturday_event_name (is executed by a timer on saturday at the specific hour and minute)
  261. // Instance related
  262. "OnInstanceInit", //instance_init_event_name (is executed right after instance creation)
  263. "OnInstanceDestroy", //instance_destroy_event_name (is executed right before instance destruction)
  264. };
  265. static jmp_buf error_jump;
  266. static char* error_msg;
  267. static const char* error_pos;
  268. static int error_report; // if the error should produce output
  269. // Used by disp_warning_message
  270. static const char* parser_current_src;
  271. static const char* parser_current_file;
  272. static int parser_current_line;
  273. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  274. // [Eoe / jA 1080, 1081, 1094, 1164]
  275. enum curly_type {
  276. TYPE_NULL = 0,
  277. TYPE_IF,
  278. TYPE_SWITCH,
  279. TYPE_WHILE,
  280. TYPE_FOR,
  281. TYPE_DO,
  282. TYPE_USERFUNC,
  283. TYPE_ARGLIST // function argument list
  284. };
  285. enum e_arglist
  286. {
  287. ARGLIST_UNDEFINED = 0,
  288. ARGLIST_NO_PAREN = 1,
  289. ARGLIST_PAREN = 2,
  290. };
  291. static struct {
  292. struct {
  293. enum curly_type type;
  294. int index;
  295. int count;
  296. int flag;
  297. struct linkdb_node *case_label;
  298. } curly[256]; // Information right parenthesis
  299. int curly_count; // The number of right brackets
  300. int index; // Number of the syntax used in the script
  301. } syntax;
  302. const char* parse_curly_close(const char* p);
  303. const char* parse_syntax_close(const char* p);
  304. const char* parse_syntax_close_sub(const char* p,int* flag);
  305. const char* parse_syntax(const char* p);
  306. static int parse_syntax_for_flag = 0;
  307. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  308. extern unsigned int current_equip_combo_pos;
  309. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  310. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  311. int potion_target = 0;
  312. unsigned int *generic_ui_array = NULL;
  313. unsigned int generic_ui_array_size = 0;
  314. c_op get_com(unsigned char *script,int *pos);
  315. int get_num(unsigned char *script,int *pos);
  316. typedef struct script_function {
  317. int (*func)(struct script_state *st);
  318. const char *name;
  319. const char *arg;
  320. const char *deprecated;
  321. } script_function;
  322. extern script_function buildin_func[];
  323. static struct linkdb_node *sleep_db; // int oid -> struct script_state *
  324. #ifdef BETA_THREAD_TEST
  325. /**
  326. * MySQL Query Slave
  327. **/
  328. static SPIN_LOCK queryThreadLock;
  329. static rAthread queryThread = NULL;
  330. static ramutex queryThreadMutex = NULL;
  331. static racond queryThreadCond = NULL;
  332. static volatile int32 queryThreadTerminate = 0;
  333. struct queryThreadEntry {
  334. bool ok;
  335. bool type; /* main db or log db? */
  336. struct script_state *st;
  337. };
  338. /* Ladies and Gentleman the Manager! */
  339. struct {
  340. struct queryThreadEntry **entry;/* array of structs */
  341. int count;
  342. int timer;/* used to receive processed entries */
  343. } queryThreadData;
  344. #endif
  345. /*==========================================
  346. * (Only those needed) local declaration prototype
  347. *------------------------------------------*/
  348. const char* parse_subexpr(const char* p,int limit);
  349. int run_func(struct script_state *st);
  350. unsigned short script_instancegetid(struct script_state *st);
  351. enum {
  352. MF_NOMEMO, //0
  353. MF_NOTELEPORT,
  354. MF_NOSAVE,
  355. MF_NOBRANCH,
  356. MF_NOPENALTY,
  357. MF_NOZENYPENALTY,
  358. MF_PVP,
  359. MF_PVP_NOPARTY,
  360. MF_PVP_NOGUILD,
  361. MF_GVG,
  362. MF_GVG_NOPARTY, //10
  363. MF_NOTRADE,
  364. MF_NOSKILL,
  365. MF_NOWARP,
  366. MF_PARTYLOCK,
  367. MF_NOICEWALL,
  368. MF_SNOW,
  369. MF_FOG,
  370. MF_SAKURA,
  371. MF_LEAVES,
  372. /**
  373. * No longer available, keeping here just in case it's back someday. [Ind]
  374. **/
  375. //MF_RAIN, //20
  376. // 21 free
  377. MF_NOGO = 22,
  378. MF_CLOUDS,
  379. MF_CLOUDS2,
  380. MF_FIREWORKS,
  381. MF_GVG_CASTLE,
  382. MF_GVG_DUNGEON,
  383. MF_NIGHTENABLED,
  384. MF_NOBASEEXP,
  385. MF_NOJOBEXP, //30
  386. MF_NOMOBLOOT,
  387. MF_NOMVPLOOT,
  388. MF_NORETURN,
  389. MF_NOWARPTO,
  390. MF_NIGHTMAREDROP,
  391. MF_RESTRICTED,
  392. MF_NOCOMMAND,
  393. MF_NODROP,
  394. MF_JEXP,
  395. MF_BEXP, //40
  396. MF_NOVENDING,
  397. MF_LOADEVENT,
  398. MF_NOCHAT,
  399. MF_NOEXPPENALTY,
  400. MF_GUILDLOCK,
  401. MF_TOWN,
  402. MF_AUTOTRADE,
  403. MF_ALLOWKS,
  404. MF_MONSTER_NOTELEPORT,
  405. MF_PVP_NOCALCRANK, //50
  406. MF_BATTLEGROUND,
  407. MF_RESET,
  408. MF_CHANNELAUTOJOIN,
  409. MF_NOUSECART,
  410. MF_NOITEMCONSUMPTION,
  411. MF_SUMSTARTMIRACLE,
  412. MF_NOMINEEFFECT,
  413. MF_NOLOCKON,
  414. MF_NOTOMB,
  415. MF_SKILL_DAMAGE, //60
  416. MF_NOCOSTUME,
  417. MF_GVG_TE_CASTLE,
  418. MF_GVG_TE,
  419. MF_HIDEMOBHPBAR,
  420. };
  421. const char* script_op2name(int op)
  422. {
  423. #define RETURN_OP_NAME(type) case type: return #type
  424. switch( op )
  425. {
  426. RETURN_OP_NAME(C_NOP);
  427. RETURN_OP_NAME(C_POS);
  428. RETURN_OP_NAME(C_INT);
  429. RETURN_OP_NAME(C_PARAM);
  430. RETURN_OP_NAME(C_FUNC);
  431. RETURN_OP_NAME(C_STR);
  432. RETURN_OP_NAME(C_CONSTSTR);
  433. RETURN_OP_NAME(C_ARG);
  434. RETURN_OP_NAME(C_NAME);
  435. RETURN_OP_NAME(C_EOL);
  436. RETURN_OP_NAME(C_RETINFO);
  437. RETURN_OP_NAME(C_USERFUNC);
  438. RETURN_OP_NAME(C_USERFUNC_POS);
  439. RETURN_OP_NAME(C_REF);
  440. // operators
  441. RETURN_OP_NAME(C_OP3);
  442. RETURN_OP_NAME(C_LOR);
  443. RETURN_OP_NAME(C_LAND);
  444. RETURN_OP_NAME(C_LE);
  445. RETURN_OP_NAME(C_LT);
  446. RETURN_OP_NAME(C_GE);
  447. RETURN_OP_NAME(C_GT);
  448. RETURN_OP_NAME(C_EQ);
  449. RETURN_OP_NAME(C_NE);
  450. RETURN_OP_NAME(C_XOR);
  451. RETURN_OP_NAME(C_OR);
  452. RETURN_OP_NAME(C_AND);
  453. RETURN_OP_NAME(C_ADD);
  454. RETURN_OP_NAME(C_SUB);
  455. RETURN_OP_NAME(C_MUL);
  456. RETURN_OP_NAME(C_DIV);
  457. RETURN_OP_NAME(C_MOD);
  458. RETURN_OP_NAME(C_NEG);
  459. RETURN_OP_NAME(C_LNOT);
  460. RETURN_OP_NAME(C_NOT);
  461. RETURN_OP_NAME(C_R_SHIFT);
  462. RETURN_OP_NAME(C_L_SHIFT);
  463. RETURN_OP_NAME(C_ADD_POST);
  464. RETURN_OP_NAME(C_SUB_POST);
  465. RETURN_OP_NAME(C_ADD_PRE);
  466. RETURN_OP_NAME(C_SUB_PRE);
  467. default:
  468. ShowDebug("script_op2name: unexpected op=%d\n", op);
  469. return "???";
  470. }
  471. #undef RETURN_OP_NAME
  472. }
  473. #ifdef DEBUG_DUMP_STACK
  474. static void script_dump_stack(struct script_state* st)
  475. {
  476. int i;
  477. ShowMessage("\tstart = %d\n", st->start);
  478. ShowMessage("\tend = %d\n", st->end);
  479. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  480. ShowMessage("\tsp = %d\n", st->stack->sp);
  481. for( i = 0; i < st->stack->sp; ++i )
  482. {
  483. struct script_data* data = &st->stack->stack_data[i];
  484. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  485. switch( data->type )
  486. {
  487. case C_INT:
  488. case C_POS:
  489. ShowMessage(" %d\n", data->u.num);
  490. break;
  491. case C_STR:
  492. case C_CONSTSTR:
  493. ShowMessage(" \"%s\"\n", data->u.str);
  494. break;
  495. case C_NAME:
  496. 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));
  497. break;
  498. case C_RETINFO:
  499. {
  500. struct script_retinfo* ri = data->u.ri;
  501. 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);
  502. }
  503. break;
  504. default:
  505. ShowMessage("\n");
  506. break;
  507. }
  508. }
  509. }
  510. #endif
  511. /// Reports on the console the src of a script error.
  512. static void script_reportsrc(struct script_state *st)
  513. {
  514. struct block_list* bl;
  515. if( st->oid == 0 )
  516. return; //Can't report source.
  517. bl = map_id2bl(st->oid);
  518. if( bl == NULL )
  519. return;
  520. switch( bl->type ) {
  521. case BL_NPC:
  522. if( bl->m >= 0 )
  523. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  524. else
  525. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  526. break;
  527. default:
  528. if( bl->m >= 0 )
  529. 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);
  530. else
  531. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  532. break;
  533. }
  534. }
  535. /// Reports on the console information about the script data.
  536. static void script_reportdata(struct script_data* data)
  537. {
  538. if( data == NULL )
  539. return;
  540. switch( data->type ) {
  541. case C_NOP:// no value
  542. ShowDebug("Data: nothing (nil)\n");
  543. break;
  544. case C_INT:// number
  545. ShowDebug("Data: number value=%"PRId64"\n", data->u.num);
  546. break;
  547. case C_STR:
  548. case C_CONSTSTR:// string
  549. if( data->u.str ) {
  550. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  551. } else {
  552. ShowDebug("Data: string value=NULL\n");
  553. }
  554. break;
  555. case C_NAME:// reference
  556. if( reference_tovariable(data) ) {// variable
  557. const char* name = reference_getname(data);
  558. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  559. } else if( reference_toconstant(data) ) {// constant
  560. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  561. } else if( reference_toparam(data) ) {// param
  562. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  563. } else {// ???
  564. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  565. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  566. }
  567. break;
  568. case C_POS:// label
  569. ShowDebug("Data: label pos=%"PRId64"\n", data->u.num);
  570. break;
  571. default:
  572. ShowDebug("Data: %s\n", script_op2name(data->type));
  573. break;
  574. }
  575. }
  576. /// Reports on the console information about the current built-in function.
  577. static void script_reportfunc(struct script_state* st)
  578. {
  579. int params, id;
  580. struct script_data* data;
  581. if( !script_hasdata(st,0) )
  582. {// no stack
  583. return;
  584. }
  585. data = script_getdata(st,0);
  586. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  587. {// script currently not executing a built-in function or corrupt stack
  588. return;
  589. }
  590. id = reference_getid(data);
  591. params = script_lastdata(st)-1;
  592. if( params > 0 )
  593. {
  594. int i;
  595. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  596. for( i = 2; i <= script_lastdata(st); i++ )
  597. {
  598. script_reportdata(script_getdata(st,i));
  599. }
  600. }
  601. else
  602. {
  603. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  604. }
  605. }
  606. /*==========================================
  607. * Output error message
  608. *------------------------------------------*/
  609. static void disp_error_message2(const char *mes,const char *pos,int report)
  610. {
  611. error_msg = aStrdup(mes);
  612. error_pos = pos;
  613. error_report = report;
  614. longjmp( error_jump, 1 );
  615. }
  616. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  617. static void disp_warning_message(const char *mes, const char *pos) {
  618. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  619. }
  620. /// Checks event parameter validity
  621. static void check_event(struct script_state *st, const char *evt)
  622. {
  623. if( evt && evt[0] && !stristr(evt, "::On") )
  624. {
  625. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  626. script_reportsrc(st);
  627. }
  628. }
  629. /*==========================================
  630. * Hashes the input string
  631. *------------------------------------------*/
  632. static unsigned int calc_hash(const char* p)
  633. {
  634. unsigned int h;
  635. #if defined(SCRIPT_HASH_DJB2)
  636. h = 5381;
  637. while( *p ) // hash*33 + c
  638. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  639. #elif defined(SCRIPT_HASH_SDBM)
  640. h = 0;
  641. while( *p ) // hash*65599 + c
  642. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  643. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  644. h = 0;
  645. while( *p ){
  646. unsigned int g;
  647. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  648. g = h & 0xF0000000;
  649. if( g )
  650. {
  651. h ^= g >> 24;
  652. h &= ~g;
  653. }
  654. }
  655. #else // athena hash
  656. h = 0;
  657. while( *p )
  658. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  659. #endif
  660. return h % SCRIPT_HASH_SIZE;
  661. }
  662. /*==========================================
  663. * str_data manipulation functions
  664. *------------------------------------------*/
  665. /// Looks up string using the provided id.
  666. const char* get_str(int id)
  667. {
  668. Assert( id >= LABEL_START && id < str_size );
  669. return str_buf+str_data[id].str;
  670. }
  671. /// Returns the uid of the string, or -1.
  672. static int search_str(const char* p)
  673. {
  674. int i;
  675. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  676. if( strcasecmp(get_str(i),p) == 0 )
  677. return i;
  678. return -1;
  679. }
  680. /// Stores a copy of the string and returns its id.
  681. /// If an identical string is already present, returns its id instead.
  682. int add_str(const char* p)
  683. {
  684. int h;
  685. int len;
  686. h = calc_hash(p);
  687. if( str_hash[h] == 0 )
  688. {// empty bucket, add new node here
  689. str_hash[h] = str_num;
  690. }
  691. else
  692. {// scan for end of list, or occurence of identical string
  693. int i;
  694. for( i = str_hash[h]; ; i = str_data[i].next )
  695. {
  696. if( strcasecmp(get_str(i),p) == 0 )
  697. return i; // string already in list
  698. if( str_data[i].next == 0 )
  699. break; // reached the end
  700. }
  701. // append node to end of list
  702. str_data[i].next = str_num;
  703. }
  704. // grow list if neccessary
  705. if( str_num >= str_data_size )
  706. {
  707. str_data_size += 128;
  708. RECREATE(str_data,struct str_data_struct,str_data_size);
  709. memset(str_data + (str_data_size - 128), '\0', 128);
  710. }
  711. len=(int)strlen(p);
  712. // grow string buffer if neccessary
  713. while( str_pos+len+1 >= str_size )
  714. {
  715. str_size += 256;
  716. RECREATE(str_buf,char,str_size);
  717. memset(str_buf + (str_size - 256), '\0', 256);
  718. }
  719. safestrncpy(str_buf+str_pos, p, len+1);
  720. str_data[str_num].type = C_NOP;
  721. str_data[str_num].str = str_pos;
  722. str_data[str_num].next = 0;
  723. str_data[str_num].func = NULL;
  724. str_data[str_num].backpatch = -1;
  725. str_data[str_num].label = -1;
  726. str_pos += len+1;
  727. return str_num++;
  728. }
  729. /// Appends 1 byte to the script buffer.
  730. static void add_scriptb(int a)
  731. {
  732. if( script_pos+1 >= script_size )
  733. {
  734. script_size += SCRIPT_BLOCK_SIZE;
  735. RECREATE(script_buf,unsigned char,script_size);
  736. }
  737. script_buf[script_pos++] = (uint8)(a);
  738. }
  739. /// Appends a c_op value to the script buffer.
  740. /// The value is variable-length encoded into 8-bit blocks.
  741. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  742. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  743. static void add_scriptc(int a)
  744. {
  745. while( a >= 0x40 )
  746. {
  747. add_scriptb((a&0x3f)|0x40);
  748. a = (a - 0x40) >> 6;
  749. }
  750. add_scriptb(a);
  751. }
  752. /// Appends an integer value to the script buffer.
  753. /// The value is variable-length encoded into 8-bit blocks.
  754. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  755. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  756. static void add_scripti(int a)
  757. {
  758. while( a >= 0x40 )
  759. {
  760. add_scriptb((a&0x3f)|0xc0);
  761. a = (a - 0x40) >> 6;
  762. }
  763. add_scriptb(a|0x80);
  764. }
  765. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  766. ///
  767. /// @param l The id of the str_data entry
  768. // Maximum up to 16M
  769. static void add_scriptl(int l)
  770. {
  771. int backpatch = str_data[l].backpatch;
  772. switch(str_data[l].type){
  773. case C_POS:
  774. case C_USERFUNC_POS:
  775. add_scriptc(C_POS);
  776. add_scriptb(str_data[l].label);
  777. add_scriptb(str_data[l].label>>8);
  778. add_scriptb(str_data[l].label>>16);
  779. break;
  780. case C_NOP:
  781. case C_USERFUNC:
  782. // Embedded data backpatch there is a possibility of label
  783. add_scriptc(C_NAME);
  784. str_data[l].backpatch = script_pos;
  785. add_scriptb(backpatch);
  786. add_scriptb(backpatch>>8);
  787. add_scriptb(backpatch>>16);
  788. break;
  789. case C_INT:
  790. add_scripti(abs(str_data[l].val));
  791. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  792. add_scriptc(C_NEG);
  793. break;
  794. default: // assume C_NAME
  795. add_scriptc(C_NAME);
  796. add_scriptb(l);
  797. add_scriptb(l>>8);
  798. add_scriptb(l>>16);
  799. break;
  800. }
  801. }
  802. /*==========================================
  803. * Resolve the label
  804. *------------------------------------------*/
  805. void set_label(int l,int pos, const char* script_pos_cur)
  806. {
  807. int i;
  808. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  809. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  810. disp_error_message("set_label: invalid label name",script_pos_cur);
  811. return;
  812. }
  813. if(str_data[l].label!=-1){
  814. disp_error_message("set_label: dup label ",script_pos_cur);
  815. return;
  816. }
  817. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  818. str_data[l].label=pos;
  819. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  820. int next=GETVALUE(script_buf,i);
  821. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  822. SETVALUE(script_buf,i,pos);
  823. i=next;
  824. }
  825. }
  826. /// Skips spaces and/or comments.
  827. const char* skip_space(const char* p)
  828. {
  829. if( p == NULL )
  830. return NULL;
  831. for(;;)
  832. {
  833. while( ISSPACE(*p) )
  834. ++p;
  835. if( *p == '/' && p[1] == '/' )
  836. {// line comment
  837. while(*p && *p!='\n')
  838. ++p;
  839. }
  840. else if( *p == '/' && p[1] == '*' )
  841. {// block comment
  842. p += 2;
  843. for(;;)
  844. {
  845. if( *p == '\0' ) {
  846. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  847. return p;
  848. }
  849. if( *p == '*' && p[1] == '/' )
  850. {// end of block comment
  851. p += 2;
  852. break;
  853. }
  854. ++p;
  855. }
  856. }
  857. else
  858. break;
  859. }
  860. return p;
  861. }
  862. /// Skips a word.
  863. /// A word consists of undercores and/or alphanumeric characters,
  864. /// and valid variable prefixes/postfixes.
  865. static const char* skip_word(const char* p)
  866. {
  867. // prefix
  868. switch( *p ) {
  869. case '@':// temporary char variable
  870. ++p; break;
  871. case '#':// account variable
  872. p += ( p[1] == '#' ? 2 : 1 ); break;
  873. case '\'':// instance variable
  874. ++p; break;
  875. case '.':// npc variable
  876. p += ( p[1] == '@' ? 2 : 1 ); break;
  877. case '$':// global variable
  878. p += ( p[1] == '@' ? 2 : 1 ); break;
  879. }
  880. while( ISALNUM(*p) || *p == '_' )
  881. ++p;
  882. // postfix
  883. if( *p == '$' )// string
  884. p++;
  885. return p;
  886. }
  887. /// Adds a word to str_data.
  888. /// @see skip_word
  889. /// @see add_str
  890. static int add_word(const char* p)
  891. {
  892. char* word;
  893. int len;
  894. int i;
  895. // Check for a word
  896. len = skip_word(p) - p;
  897. if( len == 0 )
  898. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  899. // Duplicate the word
  900. word = (char*)aMalloc(len+1);
  901. memcpy(word, p, len);
  902. word[len] = 0;
  903. // add the word
  904. i = add_str(word);
  905. aFree(word);
  906. return i;
  907. }
  908. /// Parses a function call.
  909. /// The argument list can have parenthesis or not.
  910. /// The number of arguments is checked.
  911. static
  912. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  913. {
  914. const char* p2;
  915. const char* arg=NULL;
  916. int func;
  917. func = add_word(p);
  918. if( str_data[func].type == C_FUNC ){
  919. // buildin function
  920. add_scriptl(func);
  921. add_scriptc(C_ARG);
  922. arg = buildin_func[str_data[func].val].arg;
  923. #if defined(SCRIPT_COMMAND_DEPRECATION)
  924. if( str_data[func].deprecated ){
  925. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  926. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  927. }
  928. #endif
  929. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  930. // script defined function
  931. add_scriptl(buildin_callsub_ref);
  932. add_scriptc(C_ARG);
  933. add_scriptl(func);
  934. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  935. if( *arg == 0 )
  936. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  937. if( *arg != '*' )
  938. ++arg; // count func as argument
  939. } else {
  940. #ifdef SCRIPT_CALLFUNC_CHECK
  941. const char* name = get_str(func);
  942. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  943. #endif
  944. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  945. #ifdef SCRIPT_CALLFUNC_CHECK
  946. } else {;
  947. add_scriptl(buildin_callfunc_ref);
  948. add_scriptc(C_ARG);
  949. add_scriptc(C_STR);
  950. while( *name ) add_scriptb(*name ++);
  951. add_scriptb(0);
  952. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  953. if( *arg != '*' ) ++ arg;
  954. }
  955. #endif
  956. }
  957. p = skip_word(p);
  958. p = skip_space(p);
  959. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  960. syntax.curly[syntax.curly_count].count = 0;
  961. if( *p == ';' )
  962. {// <func name> ';'
  963. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  964. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  965. {// <func name> '(' ')'
  966. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  967. p = p2;
  968. /*
  969. } else if( 0 && require_paren && *p != '(' )
  970. {// <func name>
  971. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  972. */
  973. } else
  974. {// <func name> <arg list>
  975. if( require_paren ){
  976. if( *p != '(' )
  977. disp_error_message("need '('",p);
  978. ++p; // skip '('
  979. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  980. } else if( *p == '(' ){
  981. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  982. } else {
  983. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  984. }
  985. ++syntax.curly_count;
  986. while( *arg ) {
  987. p2=parse_subexpr(p,-1);
  988. if( p == p2 )
  989. break; // not an argument
  990. if( *arg != '*' )
  991. ++arg; // next argument
  992. p=skip_space(p2);
  993. if( *arg == 0 || *p != ',' )
  994. break; // no more arguments
  995. ++p; // skip comma
  996. }
  997. --syntax.curly_count;
  998. }
  999. if( *arg && *arg != '?' && *arg != '*' )
  1000. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  1001. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  1002. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  1003. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  1004. if( *p != ')' )
  1005. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  1006. ++p;
  1007. }
  1008. add_scriptc(C_FUNC);
  1009. return p;
  1010. }
  1011. /// Processes end of logical script line.
  1012. /// @param first When true, only fix up scheduling data is initialized
  1013. /// @param p Script position for error reporting in set_label
  1014. static void parse_nextline(bool first, const char* p)
  1015. {
  1016. if( !first )
  1017. {
  1018. add_scriptc(C_EOL); // mark end of line for stack cleanup
  1019. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  1020. }
  1021. // initialize data for new '-' label fix up scheduling
  1022. str_data[LABEL_NEXTLINE].type = C_NOP;
  1023. str_data[LABEL_NEXTLINE].backpatch = -1;
  1024. str_data[LABEL_NEXTLINE].label = -1;
  1025. }
  1026. /**
  1027. * Pushes a variable into stack, processing its array index if needed.
  1028. * @see parse_variable
  1029. */
  1030. void parse_variable_sub_push(int word, const char *p2)
  1031. {
  1032. if( p2 ) { // Process the variable index
  1033. const char *p3 = NULL;
  1034. // Push the getelementofarray method into the stack
  1035. add_scriptl(buildin_getelementofarray_ref);
  1036. add_scriptc(C_ARG);
  1037. add_scriptl(word);
  1038. // Process the sub-expression for this assignment
  1039. p3 = parse_subexpr(p2 + 1, 1);
  1040. p3 = skip_space(p3);
  1041. if( *p3 != ']' ) // Closing parenthesis is required for this script
  1042. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1043. // Push the closing function stack operator onto the stack
  1044. add_scriptc(C_FUNC);
  1045. p3++;
  1046. } else // No array index, simply push the variable or value onto the stack
  1047. add_scriptl(word);
  1048. }
  1049. /// Parse a variable assignment using the direct equals operator
  1050. /// @param p script position where the function should run from
  1051. /// @return NULL if not a variable assignment, the new position otherwise
  1052. const char* parse_variable(const char* p) {
  1053. int word;
  1054. c_op type = C_NOP;
  1055. const char *p2 = NULL;
  1056. const char *var = p;
  1057. if ((p[0] == '+' && p[1] == '+' && (type = C_ADD_PRE)) // pre ++
  1058. || (p[0] == '-' && p[1] == '-' && (type = C_SUB_PRE))) // pre --
  1059. var = p = skip_space(&p[2]);
  1060. // skip the variable where applicable
  1061. p = skip_word(p);
  1062. p = skip_space(p);
  1063. if( p == NULL ) {// end of the line or invalid buffer
  1064. return NULL;
  1065. }
  1066. if( *p == '[' ) {// array variable so process the array as appropriate
  1067. int i,j;
  1068. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1069. if( *p ++ == ']' && --(j) == 0 ) break;
  1070. if( *p == '[' ) ++ j;
  1071. }
  1072. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1073. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1074. }
  1075. }
  1076. if( type == C_NOP &&
  1077. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1078. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1079. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1080. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1081. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1082. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1083. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1084. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1085. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1086. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1087. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_POST) ) // post ++
  1088. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_POST) ) // post --
  1089. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1090. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1091. ) )
  1092. {// failed to find a matching operator combination so invalid
  1093. return NULL;
  1094. }
  1095. switch( type ) {
  1096. case C_ADD_PRE: // pre ++
  1097. case C_SUB_PRE: // pre --
  1098. // Nothing more to skip
  1099. break;
  1100. case C_EQ: {// incremental modifier
  1101. p = skip_space( &p[1] );
  1102. }
  1103. break;
  1104. case C_L_SHIFT:
  1105. case C_R_SHIFT: {// left or right shift modifier
  1106. p = skip_space( &p[3] );
  1107. }
  1108. break;
  1109. default: {// normal incremental command
  1110. p = skip_space( &p[2] );
  1111. }
  1112. }
  1113. if( p == NULL ) {// end of line or invalid buffer
  1114. return NULL;
  1115. }
  1116. // push the set function onto the stack
  1117. add_scriptl(buildin_set_ref);
  1118. add_scriptc(C_ARG);
  1119. // always append parenthesis to avoid errors
  1120. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1121. syntax.curly[syntax.curly_count].count = 0;
  1122. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1123. // increment the total curly count for the position in the script
  1124. ++ syntax.curly_count;
  1125. // parse the variable currently being modified
  1126. word = add_word(var);
  1127. 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
  1128. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1129. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1130. if( type != C_EQ )
  1131. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1132. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1133. add_scripti(1);
  1134. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1135. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1136. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1137. add_scripti(1);
  1138. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1139. } else { // Process the value as an expression
  1140. p = parse_subexpr(p, -1);
  1141. if( type != C_EQ )
  1142. {// push the type of modifier onto the stack
  1143. add_scriptc(type);
  1144. }
  1145. }
  1146. // decrement the curly count for the position within the script
  1147. -- syntax.curly_count;
  1148. // close the script by appending the function operator
  1149. add_scriptc(C_FUNC);
  1150. // push the buffer from the method
  1151. return p;
  1152. }
  1153. /*
  1154. * Checks whether the gives string is a number literal
  1155. *
  1156. * Mainly necessary to differentiate between number literals and NPC name
  1157. * constants, since several of those start with a digit.
  1158. *
  1159. * All this does is to check if the string begins with an optional + or - sign,
  1160. * followed by a hexadecimal or decimal number literal literal and is NOT
  1161. * followed by a underscore or letter.
  1162. *
  1163. * @author : Hercules.ws
  1164. * @param p Pointer to the string to check
  1165. * @return Whether the string is a number literal
  1166. */
  1167. bool is_number(const char *p) {
  1168. const char *np;
  1169. if (!p)
  1170. return false;
  1171. if (*p == '-' || *p == '+')
  1172. p++;
  1173. np = p;
  1174. if (*p == '0' && p[1] == 'x') {
  1175. p+=2;
  1176. np = p;
  1177. // Hexadecimal
  1178. while (ISXDIGIT(*np))
  1179. np++;
  1180. } else {
  1181. // Decimal
  1182. while (ISDIGIT(*np))
  1183. np++;
  1184. }
  1185. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1186. return true;
  1187. return false;
  1188. }
  1189. /*==========================================
  1190. * Analysis section
  1191. *------------------------------------------*/
  1192. const char* parse_simpleexpr(const char *p)
  1193. {
  1194. long long i;
  1195. p=skip_space(p);
  1196. if(*p==';' || *p==',')
  1197. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1198. if(*p=='('){
  1199. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1200. ++syntax.curly[i].count;
  1201. p=parse_subexpr(p+1,-1);
  1202. p=skip_space(p);
  1203. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1204. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1205. ){
  1206. if( *p == ',' ){
  1207. syntax.curly[i].flag = ARGLIST_PAREN;
  1208. return p;
  1209. } else
  1210. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1211. }
  1212. if( *p != ')' )
  1213. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1214. ++p;
  1215. } else if(is_number(p)) {
  1216. char *np;
  1217. while(*p == '0' && ISDIGIT(p[1])) p++;
  1218. i=strtoll(p,&np,0);
  1219. if( i < INT_MIN ) {
  1220. i = INT_MIN;
  1221. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1222. } else if( i > INT_MAX ) {
  1223. i = INT_MAX;
  1224. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1225. }
  1226. add_scripti((int)i);
  1227. p=np;
  1228. } else if(*p=='"'){
  1229. add_scriptc(C_STR);
  1230. p++;
  1231. while( *p && *p != '"' ){
  1232. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1233. {
  1234. char buf[8];
  1235. size_t len = skip_escaped_c(p) - p;
  1236. size_t n = sv_unescape_c(buf, p, len);
  1237. if( n != 1 )
  1238. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1239. p += len;
  1240. add_scriptb(*buf);
  1241. continue;
  1242. }
  1243. else if( *p == '\n' )
  1244. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1245. add_scriptb(*p++);
  1246. }
  1247. if(!*p)
  1248. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1249. add_scriptb(0);
  1250. p++; //'"'
  1251. } else {
  1252. int l;
  1253. const char* pv;
  1254. // label , register , function etc
  1255. if(skip_word(p)==p)
  1256. disp_error_message("parse_simpleexpr: unexpected character",p);
  1257. l=add_word(p);
  1258. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1259. return parse_callfunc(p,1,0);
  1260. #ifdef SCRIPT_CALLFUNC_CHECK
  1261. else {
  1262. const char* name = get_str(l);
  1263. if( strdb_get(userfunc_db,name) != NULL ) {
  1264. return parse_callfunc(p,1,1);
  1265. }
  1266. }
  1267. #endif
  1268. if( (pv = parse_variable(p)) )
  1269. {// successfully processed a variable assignment
  1270. return pv;
  1271. }
  1272. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  1273. if( str_data[l].type == C_INT && str_data[l].deprecated ){
  1274. ShowWarning( "Usage of deprecated constant '%s'.\n", get_str(l) );
  1275. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  1276. }
  1277. #endif
  1278. p=skip_word(p);
  1279. if( *p == '[' ){
  1280. // array(name[i] => getelementofarray(name,i) )
  1281. add_scriptl(buildin_getelementofarray_ref);
  1282. add_scriptc(C_ARG);
  1283. add_scriptl(l);
  1284. p=parse_subexpr(p+1,-1);
  1285. p=skip_space(p);
  1286. if( *p != ']' )
  1287. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1288. ++p;
  1289. add_scriptc(C_FUNC);
  1290. }else
  1291. add_scriptl(l);
  1292. }
  1293. return p;
  1294. }
  1295. /*==========================================
  1296. * Analysis of the expression
  1297. *------------------------------------------*/
  1298. const char* parse_subexpr(const char* p,int limit)
  1299. {
  1300. int op,opl,len;
  1301. p=skip_space(p);
  1302. if( *p == '-' ){
  1303. const char* tmpp = skip_space(p+1);
  1304. if( *tmpp == ';' || *tmpp == ',' ){
  1305. add_scriptl(LABEL_NEXTLINE);
  1306. p++;
  1307. return p;
  1308. }
  1309. }
  1310. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1311. p = parse_variable(p);
  1312. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1313. p = parse_subexpr(p + 1, 11);
  1314. add_scriptc(op);
  1315. } else
  1316. p = parse_simpleexpr(p);
  1317. p = skip_space(p);
  1318. while((
  1319. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1320. (op=C_ADD,opl=9,len=1,*p=='+') ||
  1321. (op=C_SUB,opl=9,len=1,*p=='-') ||
  1322. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1323. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1324. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1325. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1326. (op=C_AND,opl=5,len=1,*p=='&') ||
  1327. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1328. (op=C_OR,opl=3,len=1,*p=='|') ||
  1329. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1330. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1331. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1332. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1333. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1334. (op=C_GT,opl=7,len=1,*p=='>') ||
  1335. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1336. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1337. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1338. p+=len;
  1339. if(op == C_OP3) {
  1340. p=parse_subexpr(p,-1);
  1341. p=skip_space(p);
  1342. if( *(p++) != ':')
  1343. disp_error_message("parse_subexpr: expected ':'", p-1);
  1344. p=parse_subexpr(p,-1);
  1345. } else {
  1346. p=parse_subexpr(p,opl);
  1347. }
  1348. add_scriptc(op);
  1349. p=skip_space(p);
  1350. }
  1351. return p; /* return first untreated operator */
  1352. }
  1353. /*==========================================
  1354. * Evaluation of the expression
  1355. *------------------------------------------*/
  1356. const char* parse_expr(const char *p)
  1357. {
  1358. switch(*p){
  1359. case ')': case ';': case ':': case '[': case ']':
  1360. case '}':
  1361. disp_error_message("parse_expr: unexpected character",p);
  1362. }
  1363. p=parse_subexpr(p,-1);
  1364. return p;
  1365. }
  1366. /*==========================================
  1367. * Analysis of the line
  1368. *------------------------------------------*/
  1369. const char* parse_line(const char* p)
  1370. {
  1371. const char* p2;
  1372. p=skip_space(p);
  1373. if(*p==';') {
  1374. //Close decision for if(); for(); while();
  1375. p = parse_syntax_close(p + 1);
  1376. return p;
  1377. }
  1378. if(*p==')' && parse_syntax_for_flag)
  1379. return p+1;
  1380. p = skip_space(p);
  1381. if(p[0] == '{') {
  1382. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1383. syntax.curly[syntax.curly_count].count = -1;
  1384. syntax.curly[syntax.curly_count].index = -1;
  1385. syntax.curly_count++;
  1386. return p + 1;
  1387. } else if(p[0] == '}') {
  1388. return parse_curly_close(p);
  1389. }
  1390. // Syntax-related processing
  1391. p2 = parse_syntax(p);
  1392. if(p2 != NULL)
  1393. return p2;
  1394. // attempt to process a variable assignment
  1395. p2 = parse_variable(p);
  1396. if( p2 != NULL )
  1397. {// variable assignment processed so leave the method
  1398. return parse_syntax_close(p2 + 1);
  1399. }
  1400. p = parse_callfunc(p,0,0);
  1401. p = skip_space(p);
  1402. if(parse_syntax_for_flag) {
  1403. if( *p != ')' )
  1404. disp_error_message("parse_line: expected ')'",p);
  1405. } else {
  1406. if( *p != ';' )
  1407. disp_error_message("parse_line: expected ';'",p);
  1408. }
  1409. //Binding decision for if(), for(), while()
  1410. p = parse_syntax_close(p+1);
  1411. return p;
  1412. }
  1413. // { ... } Closing process
  1414. const char* parse_curly_close(const char* p)
  1415. {
  1416. if(syntax.curly_count <= 0) {
  1417. disp_error_message("parse_curly_close: unexpected string",p);
  1418. return p + 1;
  1419. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1420. syntax.curly_count--;
  1421. //Close decision if, for , while
  1422. p = parse_syntax_close(p + 1);
  1423. return p;
  1424. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1425. //Closing switch()
  1426. int pos = syntax.curly_count-1;
  1427. char label[256];
  1428. int l;
  1429. // Remove temporary variables
  1430. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1431. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1432. parse_line(label);
  1433. syntax.curly_count--;
  1434. // Go to the end pointer unconditionally
  1435. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1436. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1437. parse_line(label);
  1438. syntax.curly_count--;
  1439. // You are here labeled
  1440. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1441. l=add_str(label);
  1442. set_label(l,script_pos, p);
  1443. if(syntax.curly[pos].flag) {
  1444. //Exists default
  1445. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1446. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1447. parse_line(label);
  1448. syntax.curly_count--;
  1449. }
  1450. // Label end
  1451. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1452. l=add_str(label);
  1453. set_label(l,script_pos, p);
  1454. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1455. syntax.curly_count--;
  1456. //Closing decision if, for , while
  1457. p = parse_syntax_close(p + 1);
  1458. return p;
  1459. } else {
  1460. disp_error_message("parse_curly_close: unexpected string",p);
  1461. return p + 1;
  1462. }
  1463. }
  1464. // Syntax-related processing
  1465. // break, case, continue, default, do, for, function,
  1466. // if, switch, while ? will handle this internally.
  1467. const char* parse_syntax(const char* p)
  1468. {
  1469. const char *p2 = skip_word(p);
  1470. switch(*p) {
  1471. case 'B':
  1472. case 'b':
  1473. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1474. // break Processing
  1475. char label[256];
  1476. int pos = syntax.curly_count - 1;
  1477. while(pos >= 0) {
  1478. if(syntax.curly[pos].type == TYPE_DO) {
  1479. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1480. break;
  1481. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1482. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1483. break;
  1484. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1485. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1486. break;
  1487. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1488. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1489. break;
  1490. }
  1491. pos--;
  1492. }
  1493. if(pos < 0) {
  1494. disp_error_message("parse_syntax: unexpected 'break'",p);
  1495. } else {
  1496. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1497. parse_line(label);
  1498. syntax.curly_count--;
  1499. }
  1500. p = skip_space(p2);
  1501. if(*p != ';')
  1502. disp_error_message("parse_syntax: expected ';'",p);
  1503. // Closing decision if, for , while
  1504. p = parse_syntax_close(p + 1);
  1505. return p;
  1506. }
  1507. break;
  1508. case 'c':
  1509. case 'C':
  1510. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1511. //Processing case
  1512. int pos = syntax.curly_count-1;
  1513. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1514. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1515. return p+1;
  1516. } else {
  1517. char label[256];
  1518. int l,v;
  1519. char *np;
  1520. if(syntax.curly[pos].count != 1) {
  1521. //Jump for FALLTHRU
  1522. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1523. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1524. parse_line(label);
  1525. syntax.curly_count--;
  1526. // You are here labeled
  1527. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1528. l=add_str(label);
  1529. set_label(l,script_pos, p);
  1530. }
  1531. //Decision statement switch
  1532. p = skip_space(p2);
  1533. if(p == p2) {
  1534. disp_error_message("parse_syntax: expected a space ' '",p);
  1535. }
  1536. // check whether case label is integer or not
  1537. if(is_number(p)) {
  1538. //Numeric value
  1539. v = (int)strtol(p,&np,0);
  1540. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1541. p++;
  1542. p = skip_word(p);
  1543. if(np != p)
  1544. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1545. } else {
  1546. //Check for constants
  1547. p2 = skip_word(p);
  1548. v = (int)(size_t) (p2-p); // length of word at p2
  1549. memcpy(label,p,v);
  1550. label[v]='\0';
  1551. if( !script_get_constant(label, &v) )
  1552. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1553. p = skip_word(p);
  1554. }
  1555. p = skip_space(p);
  1556. if(*p != ':')
  1557. disp_error_message("parse_syntax: expect ':'",p);
  1558. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1559. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1560. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1561. // Bad I do not parse twice
  1562. p2 = parse_line(label);
  1563. parse_line(p2);
  1564. syntax.curly_count--;
  1565. if(syntax.curly[pos].count != 1) {
  1566. // Label after the completion of FALLTHRU
  1567. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1568. l=add_str(label);
  1569. set_label(l,script_pos,p);
  1570. }
  1571. // check duplication of case label [Rayce]
  1572. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1573. disp_error_message("parse_syntax: dup 'case'",p);
  1574. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1575. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1576. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1577. parse_line(label);
  1578. syntax.curly_count--;
  1579. syntax.curly[pos].count++;
  1580. }
  1581. return p + 1;
  1582. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1583. // Processing continue
  1584. char label[256];
  1585. int pos = syntax.curly_count - 1;
  1586. while(pos >= 0) {
  1587. if(syntax.curly[pos].type == TYPE_DO) {
  1588. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1589. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1590. break;
  1591. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1592. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1593. break;
  1594. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1595. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1596. break;
  1597. }
  1598. pos--;
  1599. }
  1600. if(pos < 0) {
  1601. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1602. } else {
  1603. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1604. parse_line(label);
  1605. syntax.curly_count--;
  1606. }
  1607. p = skip_space(p2);
  1608. if(*p != ';')
  1609. disp_error_message("parse_syntax: expected ';'",p);
  1610. //Closing decision if, for , while
  1611. p = parse_syntax_close(p + 1);
  1612. return p;
  1613. }
  1614. break;
  1615. case 'd':
  1616. case 'D':
  1617. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1618. // Switch - default processing
  1619. int pos = syntax.curly_count-1;
  1620. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1621. disp_error_message("parse_syntax: unexpected 'default'",p);
  1622. } else if(syntax.curly[pos].flag) {
  1623. disp_error_message("parse_syntax: dup 'default'",p);
  1624. } else {
  1625. char label[256];
  1626. int l;
  1627. // Put the label location
  1628. p = skip_space(p2);
  1629. if(*p != ':') {
  1630. disp_error_message("parse_syntax: expected ':'",p);
  1631. }
  1632. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1633. l=add_str(label);
  1634. set_label(l,script_pos,p);
  1635. // Skip to the next link w/o condition
  1636. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1637. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1638. parse_line(label);
  1639. syntax.curly_count--;
  1640. // The default label
  1641. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1642. l=add_str(label);
  1643. set_label(l,script_pos,p);
  1644. syntax.curly[syntax.curly_count - 1].flag = 1;
  1645. syntax.curly[pos].count++;
  1646. }
  1647. return p + 1;
  1648. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1649. int l;
  1650. char label[256];
  1651. p=skip_space(p2);
  1652. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1653. syntax.curly[syntax.curly_count].count = 1;
  1654. syntax.curly[syntax.curly_count].index = syntax.index++;
  1655. syntax.curly[syntax.curly_count].flag = 0;
  1656. // Label of the (do) form here
  1657. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1658. l=add_str(label);
  1659. set_label(l,script_pos,p);
  1660. syntax.curly_count++;
  1661. return p;
  1662. }
  1663. break;
  1664. case 'f':
  1665. case 'F':
  1666. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1667. int l;
  1668. char label[256];
  1669. int pos = syntax.curly_count;
  1670. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1671. syntax.curly[syntax.curly_count].count = 1;
  1672. syntax.curly[syntax.curly_count].index = syntax.index++;
  1673. syntax.curly[syntax.curly_count].flag = 0;
  1674. syntax.curly_count++;
  1675. p=skip_space(p2);
  1676. if(*p != '(')
  1677. disp_error_message("parse_syntax: expected '('",p);
  1678. p++;
  1679. // Execute the initialization statement
  1680. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1681. p=parse_line(p);
  1682. syntax.curly_count--;
  1683. // Form the start of label decision
  1684. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1685. l=add_str(label);
  1686. set_label(l,script_pos,p);
  1687. p=skip_space(p);
  1688. if(*p == ';') {
  1689. // For (; Because the pattern of always true ;)
  1690. ;
  1691. } else {
  1692. // Skip to the end point if the condition is false
  1693. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1694. add_scriptl(add_str("jump_zero"));
  1695. add_scriptc(C_ARG);
  1696. p=parse_expr(p);
  1697. p=skip_space(p);
  1698. add_scriptl(add_str(label));
  1699. add_scriptc(C_FUNC);
  1700. }
  1701. if(*p != ';')
  1702. disp_error_message("parse_syntax: expected ';'",p);
  1703. p++;
  1704. // Skip to the beginning of the loop
  1705. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1706. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1707. parse_line(label);
  1708. syntax.curly_count--;
  1709. // Labels to form the next loop
  1710. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1711. l=add_str(label);
  1712. set_label(l,script_pos,p);
  1713. // Process the next time you enter the loop
  1714. // A ')' last for; flag to be treated as'
  1715. parse_syntax_for_flag = 1;
  1716. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1717. p=parse_line(p);
  1718. syntax.curly_count--;
  1719. parse_syntax_for_flag = 0;
  1720. // Skip to the determination process conditions
  1721. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1722. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1723. parse_line(label);
  1724. syntax.curly_count--;
  1725. // Loop start labeling
  1726. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1727. l=add_str(label);
  1728. set_label(l,script_pos,p);
  1729. return p;
  1730. }
  1731. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1732. {// internal script function
  1733. const char *func_name;
  1734. func_name = skip_space(p2);
  1735. p = skip_word(func_name);
  1736. if( p == func_name )
  1737. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1738. p2 = skip_space(p);
  1739. if( *p2 == ';' )
  1740. {// function <name> ;
  1741. // function declaration - just register the name
  1742. int l;
  1743. l = add_word(func_name);
  1744. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1745. str_data[l].type = C_USERFUNC;
  1746. else if( str_data[l].type == C_USERFUNC )
  1747. ; // already registered
  1748. else
  1749. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1750. // Close condition of if, for, while
  1751. p = parse_syntax_close(p2 + 1);
  1752. return p;
  1753. }
  1754. else if(*p2 == '{')
  1755. {// function <name> <line/block of code>
  1756. char label[256];
  1757. int l;
  1758. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1759. syntax.curly[syntax.curly_count].count = 1;
  1760. syntax.curly[syntax.curly_count].index = syntax.index++;
  1761. syntax.curly[syntax.curly_count].flag = 0;
  1762. ++syntax.curly_count;
  1763. // Jump over the function code
  1764. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1765. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1766. ++syntax.curly_count;
  1767. parse_line(label);
  1768. --syntax.curly_count;
  1769. // Set the position of the function (label)
  1770. l=add_word(func_name);
  1771. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1772. {
  1773. str_data[l].type = C_USERFUNC;
  1774. set_label(l, script_pos, p);
  1775. if( parse_options&SCRIPT_USE_LABEL_DB )
  1776. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1777. }
  1778. else
  1779. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1780. return skip_space(p);
  1781. }
  1782. else
  1783. {
  1784. disp_error_message("expect ';' or '{' at function syntax",p);
  1785. }
  1786. }
  1787. break;
  1788. case 'i':
  1789. case 'I':
  1790. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1791. // If process
  1792. char label[256];
  1793. p=skip_space(p2);
  1794. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1795. disp_error_message("need '('",p);
  1796. }
  1797. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1798. syntax.curly[syntax.curly_count].count = 1;
  1799. syntax.curly[syntax.curly_count].index = syntax.index++;
  1800. syntax.curly[syntax.curly_count].flag = 0;
  1801. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1802. syntax.curly_count++;
  1803. add_scriptl(add_str("jump_zero"));
  1804. add_scriptc(C_ARG);
  1805. p=parse_expr(p);
  1806. p=skip_space(p);
  1807. add_scriptl(add_str(label));
  1808. add_scriptc(C_FUNC);
  1809. return p;
  1810. }
  1811. break;
  1812. case 's':
  1813. case 'S':
  1814. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1815. // Processing of switch ()
  1816. char label[256];
  1817. p=skip_space(p2);
  1818. if(*p != '(') {
  1819. disp_error_message("need '('",p);
  1820. }
  1821. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1822. syntax.curly[syntax.curly_count].count = 1;
  1823. syntax.curly[syntax.curly_count].index = syntax.index++;
  1824. syntax.curly[syntax.curly_count].flag = 0;
  1825. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1826. syntax.curly_count++;
  1827. add_scriptl(add_str("set"));
  1828. add_scriptc(C_ARG);
  1829. add_scriptl(add_str(label));
  1830. p=parse_expr(p);
  1831. p=skip_space(p);
  1832. if(*p != '{') {
  1833. disp_error_message("parse_syntax: expected '{'",p);
  1834. }
  1835. add_scriptc(C_FUNC);
  1836. return p + 1;
  1837. }
  1838. break;
  1839. case 'w':
  1840. case 'W':
  1841. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1842. int l;
  1843. char label[256];
  1844. p=skip_space(p2);
  1845. if(*p != '(') {
  1846. disp_error_message("need '('",p);
  1847. }
  1848. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1849. syntax.curly[syntax.curly_count].count = 1;
  1850. syntax.curly[syntax.curly_count].index = syntax.index++;
  1851. syntax.curly[syntax.curly_count].flag = 0;
  1852. // Form the start of label decision
  1853. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1854. l=add_str(label);
  1855. set_label(l,script_pos,p);
  1856. // Skip to the end point if the condition is false
  1857. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1858. syntax.curly_count++;
  1859. add_scriptl(add_str("jump_zero"));
  1860. add_scriptc(C_ARG);
  1861. p=parse_expr(p);
  1862. p=skip_space(p);
  1863. add_scriptl(add_str(label));
  1864. add_scriptc(C_FUNC);
  1865. return p;
  1866. }
  1867. break;
  1868. }
  1869. return NULL;
  1870. }
  1871. const char* parse_syntax_close(const char *p) {
  1872. // If (...) for (...) hoge (); as to make sure closed closed once again
  1873. int flag;
  1874. do {
  1875. p = parse_syntax_close_sub(p,&flag);
  1876. } while(flag);
  1877. return p;
  1878. }
  1879. // Close judgment if, for, while, of do
  1880. // flag == 1 : closed
  1881. // flag == 0 : not closed
  1882. const char* parse_syntax_close_sub(const char* p,int* flag)
  1883. {
  1884. char label[256];
  1885. int pos = syntax.curly_count - 1;
  1886. int l;
  1887. *flag = 1;
  1888. if(syntax.curly_count <= 0) {
  1889. *flag = 0;
  1890. return p;
  1891. } else if(syntax.curly[pos].type == TYPE_IF) {
  1892. const char *bp = p;
  1893. const char *p2;
  1894. // if-block and else-block end is a new line
  1895. parse_nextline(false, p);
  1896. // Skip to the last location if
  1897. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1898. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1899. parse_line(label);
  1900. syntax.curly_count--;
  1901. // Put the label of the location
  1902. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1903. l=add_str(label);
  1904. set_label(l,script_pos,p);
  1905. syntax.curly[pos].count++;
  1906. p = skip_space(p);
  1907. p2 = skip_word(p);
  1908. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1909. // else or else - if
  1910. p = skip_space(p2);
  1911. p2 = skip_word(p);
  1912. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1913. // else - if
  1914. p=skip_space(p2);
  1915. if(*p != '(') {
  1916. disp_error_message("need '('",p);
  1917. }
  1918. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1919. add_scriptl(add_str("jump_zero"));
  1920. add_scriptc(C_ARG);
  1921. p=parse_expr(p);
  1922. p=skip_space(p);
  1923. add_scriptl(add_str(label));
  1924. add_scriptc(C_FUNC);
  1925. *flag = 0;
  1926. return p;
  1927. } else {
  1928. // else
  1929. if(!syntax.curly[pos].flag) {
  1930. syntax.curly[pos].flag = 1;
  1931. *flag = 0;
  1932. return p;
  1933. }
  1934. }
  1935. }
  1936. // Close if
  1937. syntax.curly_count--;
  1938. // Put the label of the final location
  1939. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1940. l=add_str(label);
  1941. set_label(l,script_pos,p);
  1942. if(syntax.curly[pos].flag == 1) {
  1943. // Because the position of the pointer is the same if not else for this
  1944. return bp;
  1945. }
  1946. return p;
  1947. } else if(syntax.curly[pos].type == TYPE_DO) {
  1948. int l2;
  1949. char label2[256];
  1950. const char *p2;
  1951. if(syntax.curly[pos].flag) {
  1952. // (Come here continue) to form the label here
  1953. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1954. l2=add_str(label2);
  1955. set_label(l2,script_pos,p);
  1956. }
  1957. // Skip to the end point if the condition is false
  1958. p = skip_space(p);
  1959. p2 = skip_word(p);
  1960. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1961. disp_error_message("parse_syntax: expected 'while'",p);
  1962. p = skip_space(p2);
  1963. if(*p != '(') {
  1964. disp_error_message("need '('",p);
  1965. }
  1966. // do-block end is a new line
  1967. parse_nextline(false, p);
  1968. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1969. add_scriptl(add_str("jump_zero"));
  1970. add_scriptc(C_ARG);
  1971. p=parse_expr(p);
  1972. p=skip_space(p);
  1973. add_scriptl(add_str(label2));
  1974. add_scriptc(C_FUNC);
  1975. // Skip to the starting point
  1976. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1977. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1978. parse_line(label2);
  1979. syntax.curly_count--;
  1980. // Form label of the end point conditions
  1981. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1982. l2=add_str(label2);
  1983. set_label(l2,script_pos,p);
  1984. p = skip_space(p);
  1985. if(*p != ';') {
  1986. disp_error_message("parse_syntax: expected ';'",p);
  1987. return p+1;
  1988. }
  1989. p++;
  1990. syntax.curly_count--;
  1991. return p;
  1992. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1993. // for-block end is a new line
  1994. parse_nextline(false, p);
  1995. // Skip to the next loop
  1996. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1997. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1998. parse_line(label);
  1999. syntax.curly_count--;
  2000. // End for labeling
  2001. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  2002. l=add_str(label);
  2003. set_label(l,script_pos,p);
  2004. syntax.curly_count--;
  2005. return p;
  2006. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  2007. // while-block end is a new line
  2008. parse_nextline(false, p);
  2009. // Skip to the decision while
  2010. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  2011. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  2012. parse_line(label);
  2013. syntax.curly_count--;
  2014. // End while labeling
  2015. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  2016. l=add_str(label);
  2017. set_label(l,script_pos,p);
  2018. syntax.curly_count--;
  2019. return p;
  2020. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  2021. int pos2 = syntax.curly_count-1;
  2022. char label2[256];
  2023. int l2;
  2024. // Back
  2025. sprintf(label2,"return;");
  2026. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  2027. parse_line(label2);
  2028. syntax.curly_count--;
  2029. // Put the label of the location
  2030. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  2031. l2=add_str(label2);
  2032. set_label(l2,script_pos,p);
  2033. syntax.curly_count--;
  2034. return p;
  2035. } else {
  2036. *flag = 0;
  2037. return p;
  2038. }
  2039. }
  2040. /*==========================================
  2041. * Added built-in functions
  2042. *------------------------------------------*/
  2043. static void add_buildin_func(void)
  2044. {
  2045. int i;
  2046. for( i = 0; buildin_func[i].func; i++ )
  2047. {
  2048. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  2049. // 'v' - value (either string or int or reference)
  2050. // 's' - string
  2051. // 'i' - int
  2052. // 'r' - reference (of a variable)
  2053. // 'l' - label
  2054. // '?' - one optional parameter
  2055. // '*' - unknown number of optional parameters
  2056. const char* p = buildin_func[i].arg;
  2057. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  2058. while( *p == '?' ) ++p;
  2059. if( *p == '*' ) ++p;
  2060. if( *p != 0){
  2061. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  2062. } else if( *skip_word(buildin_func[i].name) != 0 ){
  2063. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  2064. } else {
  2065. int n = add_str(buildin_func[i].name);
  2066. str_data[n].type = C_FUNC;
  2067. str_data[n].val = i;
  2068. str_data[n].func = buildin_func[i].func;
  2069. str_data[n].deprecated = (buildin_func[i].deprecated != NULL);
  2070. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  2071. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2072. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2073. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2074. }
  2075. }
  2076. }
  2077. /// Retrieves the value of a constant parameter.
  2078. bool script_get_parameter(const char* name, int* value)
  2079. {
  2080. int n = search_str(name);
  2081. if (n == -1 || str_data[n].type != C_PARAM)
  2082. {// not found or not a parameter
  2083. return false;
  2084. }
  2085. value[0] = str_data[n].val;
  2086. return true;
  2087. }
  2088. /// Retrieves the value of a constant.
  2089. bool script_get_constant(const char* name, int* value)
  2090. {
  2091. int n = search_str(name);
  2092. if( n == -1 || str_data[n].type != C_INT )
  2093. {// not found or not a constant
  2094. return false;
  2095. }
  2096. value[0] = str_data[n].val;
  2097. #if defined(SCRIPT_CONSTANT_DEPRECATION)
  2098. if( str_data[n].deprecated ){
  2099. ShowWarning( "Usage of deprecated constant '%s'.\n", name );
  2100. ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" );
  2101. }
  2102. #endif
  2103. return true;
  2104. }
  2105. /// Creates new constant or parameter with given value.
  2106. void script_set_constant(const char* name, int value, bool isparameter, bool deprecated)
  2107. {
  2108. int n = add_str(name);
  2109. if( str_data[n].type == C_NOP )
  2110. {// new
  2111. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2112. str_data[n].val = value;
  2113. str_data[n].deprecated = deprecated;
  2114. }
  2115. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2116. {// existing parameter or constant
  2117. 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);
  2118. }
  2119. else
  2120. {// existing name
  2121. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2122. }
  2123. }
  2124. /*==========================================
  2125. * Reading constant databases
  2126. * const.txt
  2127. *------------------------------------------*/
  2128. static void read_constdb(void)
  2129. {
  2130. FILE *fp;
  2131. char line[1024],name[1024],val[1024];
  2132. int type;
  2133. int entries=0, skipped=0, linenum=0;
  2134. sprintf(line, "%s/const.txt", db_path);
  2135. fp=fopen(line, "r");
  2136. if(fp==NULL){
  2137. ShowError("can't read %s\n", line);
  2138. return ;
  2139. }
  2140. while(fgets(line, sizeof(line), fp))
  2141. {
  2142. linenum++;
  2143. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  2144. continue;
  2145. if(line[0]=='/' && line[1]=='/') //ignore commented line
  2146. continue;
  2147. type=0;
  2148. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%11d",name,val,&type)>=2 ||
  2149. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d",name,val,&type)>=2){
  2150. entries++;
  2151. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type, false);
  2152. }
  2153. else {
  2154. skipped++;
  2155. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalid constant definition\n",linenum);
  2156. }
  2157. }
  2158. fclose(fp);
  2159. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2160. if(skipped){
  2161. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2162. }
  2163. }
  2164. /**
  2165. * Sets source-end constants for NPC scripts to access.
  2166. **/
  2167. void script_hardcoded_constants(void) {
  2168. #include "script_constants.h"
  2169. }
  2170. /*==========================================
  2171. * Display emplacement line of script
  2172. *------------------------------------------*/
  2173. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2174. {
  2175. int i;
  2176. if( p == NULL || !p[0] ) return NULL;
  2177. if( line < 0 )
  2178. StringBuf_Printf(buf, "*% 5d : ", -line);
  2179. else
  2180. StringBuf_Printf(buf, " % 5d : ", line);
  2181. for(i=0;p[i] && p[i] != '\n';i++){
  2182. if(p + i != mark)
  2183. StringBuf_Printf(buf, "%c", p[i]);
  2184. else
  2185. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2186. }
  2187. StringBuf_AppendStr(buf, "\n");
  2188. return p+i+(p[i] == '\n' ? 1 : 0);
  2189. }
  2190. 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) {
  2191. // Find the line where the error occurred
  2192. int j;
  2193. int line = start_line;
  2194. const char *p;
  2195. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2196. for(p=src;p && *p;line++){
  2197. const char *lineend=strchr(p,'\n');
  2198. if(lineend==NULL || error_pos_cur<lineend){
  2199. break;
  2200. }
  2201. for( j = 0; j < 4; j++ ) {
  2202. linestart[j] = linestart[j+1];
  2203. }
  2204. linestart[4] = p;
  2205. p=lineend+1;
  2206. }
  2207. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2208. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2209. for(j = 0; j < 5; j++ ) {
  2210. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2211. }
  2212. p = script_print_line(buf, p, error_pos_cur, -line);
  2213. for(j = 0; j < 5; j++) {
  2214. p = script_print_line(buf, p, NULL, line + j + 1 );
  2215. }
  2216. }
  2217. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2218. StringBuf buf;
  2219. StringBuf_Init(&buf);
  2220. StringBuf_AppendStr(&buf, "\a\n");
  2221. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2222. ShowError("%s", StringBuf_Value(&buf));
  2223. StringBuf_Destroy(&buf);
  2224. }
  2225. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2226. StringBuf buf;
  2227. StringBuf_Init(&buf);
  2228. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2229. ShowWarning("%s", StringBuf_Value(&buf));
  2230. StringBuf_Destroy(&buf);
  2231. }
  2232. /*==========================================
  2233. * Analysis of the script
  2234. *------------------------------------------*/
  2235. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2236. {
  2237. const char *p,*tmpp;
  2238. int i;
  2239. struct script_code* code = NULL;
  2240. static int first=1;
  2241. char end;
  2242. bool unresolved_names = false;
  2243. parser_current_src = src;
  2244. parser_current_file = file;
  2245. parser_current_line = line;
  2246. if( src == NULL )
  2247. return NULL;// empty script
  2248. memset(&syntax,0,sizeof(syntax));
  2249. if(first){
  2250. add_buildin_func();
  2251. read_constdb();
  2252. script_hardcoded_constants();
  2253. first=0;
  2254. }
  2255. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2256. script_pos=0;
  2257. script_size=SCRIPT_BLOCK_SIZE;
  2258. parse_nextline(true, NULL);
  2259. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2260. if( options&SCRIPT_USE_LABEL_DB )
  2261. db_clear(scriptlabel_db);
  2262. parse_options = options;
  2263. if( setjmp( error_jump ) != 0 ) {
  2264. //Restore program state when script has problems. [from jA]
  2265. int j;
  2266. const int size = ARRAYLENGTH(syntax.curly);
  2267. if( error_report )
  2268. script_error(src,file,line,error_msg,error_pos);
  2269. aFree( error_msg );
  2270. aFree( script_buf );
  2271. script_pos = 0;
  2272. script_size = 0;
  2273. script_buf = NULL;
  2274. for(j=LABEL_START;j<str_num;j++)
  2275. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2276. for(j=0; j<size; j++)
  2277. linkdb_final(&syntax.curly[j].case_label);
  2278. return NULL;
  2279. }
  2280. parse_syntax_for_flag=0;
  2281. p=src;
  2282. p=skip_space(p);
  2283. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2284. {// does not require brackets around the script
  2285. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2286. {// empty script and can return NULL
  2287. aFree( script_buf );
  2288. script_pos = 0;
  2289. script_size = 0;
  2290. script_buf = NULL;
  2291. return NULL;
  2292. }
  2293. end = '\0';
  2294. }
  2295. else
  2296. {// requires brackets around the script
  2297. if( *p != '{' )
  2298. disp_error_message("not found '{'",p);
  2299. p = skip_space(p+1);
  2300. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2301. {// empty script and can return NULL
  2302. aFree( script_buf );
  2303. script_pos = 0;
  2304. script_size = 0;
  2305. script_buf = NULL;
  2306. return NULL;
  2307. }
  2308. end = '}';
  2309. }
  2310. // clear references of labels, variables and internal functions
  2311. for(i=LABEL_START;i<str_num;i++){
  2312. if(
  2313. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2314. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2315. ){
  2316. str_data[i].type=C_NOP;
  2317. str_data[i].backpatch=-1;
  2318. str_data[i].label=-1;
  2319. }
  2320. }
  2321. while( syntax.curly_count != 0 || *p != end )
  2322. {
  2323. if( *p == '\0' )
  2324. disp_error_message("unexpected end of script",p);
  2325. // Special handling only label
  2326. tmpp=skip_space(skip_word(p));
  2327. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2328. i=add_word(p);
  2329. set_label(i,script_pos,p);
  2330. if( parse_options&SCRIPT_USE_LABEL_DB )
  2331. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2332. p=tmpp+1;
  2333. p=skip_space(p);
  2334. continue;
  2335. }
  2336. // All other lumped
  2337. p=parse_line(p);
  2338. p=skip_space(p);
  2339. parse_nextline(false, p);
  2340. }
  2341. add_scriptc(C_NOP);
  2342. // trim code to size
  2343. script_size = script_pos;
  2344. RECREATE(script_buf,unsigned char,script_pos);
  2345. // default unknown references to variables
  2346. for(i=LABEL_START;i<str_num;i++){
  2347. if(str_data[i].type==C_NOP){
  2348. int j;
  2349. str_data[i].type=C_NAME;
  2350. str_data[i].label=i;
  2351. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2352. int next=GETVALUE(script_buf,j);
  2353. SETVALUE(script_buf,j,i);
  2354. j=next;
  2355. }
  2356. }
  2357. else if( str_data[i].type == C_USERFUNC )
  2358. {// 'function name;' without follow-up code
  2359. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2360. unresolved_names = true;
  2361. }
  2362. }
  2363. if( unresolved_names )
  2364. {
  2365. disp_error_message("parse_script: unresolved function references", p);
  2366. }
  2367. #ifdef DEBUG_DISP
  2368. for(i=0;i<script_pos;i++){
  2369. if((i&15)==0) ShowMessage("%04x : ",i);
  2370. ShowMessage("%02x ",script_buf[i]);
  2371. if((i&15)==15) ShowMessage("\n");
  2372. }
  2373. ShowMessage("\n");
  2374. #endif
  2375. #ifdef DEBUG_DISASM
  2376. {
  2377. int i = 0,j;
  2378. while(i < script_pos) {
  2379. c_op op = get_com(script_buf,&i);
  2380. ShowMessage("%06x %s", i, script_op2name(op));
  2381. j = i;
  2382. switch(op) {
  2383. case C_INT:
  2384. ShowMessage(" %d", get_num(script_buf,&i));
  2385. break;
  2386. case C_POS:
  2387. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2388. i += 3;
  2389. break;
  2390. case C_NAME:
  2391. j = (*(int*)(script_buf+i)&0xffffff);
  2392. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2393. i += 3;
  2394. break;
  2395. case C_STR:
  2396. j = strlen(script_buf + i);
  2397. ShowMessage(" %s", script_buf + i);
  2398. i += j+1;
  2399. break;
  2400. }
  2401. ShowMessage(CL_CLL"\n");
  2402. }
  2403. }
  2404. #endif
  2405. CREATE(code,struct script_code,1);
  2406. code->script_buf = script_buf;
  2407. code->script_size = script_size;
  2408. code->local.vars = NULL;
  2409. code->local.arrays = NULL;
  2410. return code;
  2411. }
  2412. /// Returns the player attached to this script, identified by the rid.
  2413. /// If there is no player attached, the script is terminated.
  2414. static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func ){
  2415. *sd = map_id2sd( st->rid );
  2416. if( *sd ){
  2417. return true;
  2418. }else{
  2419. ShowError("%s: fatal error ! player not attached!\n",func);
  2420. script_reportfunc(st);
  2421. script_reportsrc(st);
  2422. st->state = END;
  2423. return false;
  2424. }
  2425. }
  2426. /**
  2427. * Dereferences a variable/constant, replacing it with a copy of the value.
  2428. * @param st Script state
  2429. * @param data Variable/constant
  2430. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2431. */
  2432. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2433. {
  2434. const char* name;
  2435. char prefix;
  2436. char postfix;
  2437. if( !data_isreference(data) )
  2438. return;// not a variable/constant
  2439. name = reference_getname(data);
  2440. prefix = name[0];
  2441. postfix = name[strlen(name) - 1];
  2442. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2443. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2444. if( sd == NULL && !script_rid2sd(sd) ) {// needs player attached
  2445. if( postfix == '$' ) {// string variable
  2446. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2447. data->type = C_CONSTSTR;
  2448. data->u.str = "";
  2449. } else {// integer variable
  2450. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2451. data->type = C_INT;
  2452. data->u.num = 0;
  2453. }
  2454. return;
  2455. }
  2456. }
  2457. if( postfix == '$' ) {// string variable
  2458. switch( prefix ) {
  2459. case '@':
  2460. data->u.str = pc_readregstr(sd, data->u.num);
  2461. break;
  2462. case '$':
  2463. data->u.str = mapreg_readregstr(data->u.num);
  2464. break;
  2465. case '#':
  2466. if( name[1] == '#' )
  2467. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2468. else
  2469. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2470. break;
  2471. case '.':
  2472. {
  2473. struct DBMap* n = data->ref ?
  2474. data->ref->vars : name[1] == '@' ?
  2475. st->stack->scope.vars : // instance/scope variable
  2476. st->script->local.vars; // npc variable
  2477. if( n )
  2478. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2479. else
  2480. data->u.str = NULL;
  2481. }
  2482. break;
  2483. case '\'':
  2484. {
  2485. unsigned short instance_id = script_instancegetid(st);
  2486. if( instance_id )
  2487. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2488. else {
  2489. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2490. data->u.str = NULL;
  2491. }
  2492. }
  2493. break;
  2494. default:
  2495. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2496. break;
  2497. }
  2498. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2499. data->type = C_CONSTSTR;
  2500. data->u.str = "";
  2501. } else {// duplicate string
  2502. data->type = C_STR;
  2503. data->u.str = aStrdup(data->u.str);
  2504. }
  2505. } else {// integer variable
  2506. data->type = C_INT;
  2507. if( reference_toconstant(data) ) {
  2508. data->u.num = reference_getconstant(data);
  2509. } else if( reference_toparam(data) ) {
  2510. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2511. } else
  2512. switch( prefix ) {
  2513. case '@':
  2514. data->u.num = pc_readreg(sd, data->u.num);
  2515. break;
  2516. case '$':
  2517. data->u.num = mapreg_readreg(data->u.num);
  2518. break;
  2519. case '#':
  2520. if( name[1] == '#' )
  2521. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2522. else
  2523. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2524. break;
  2525. case '.':
  2526. {
  2527. struct DBMap* n = data->ref ?
  2528. data->ref->vars : name[1] == '@' ?
  2529. st->stack->scope.vars : // instance/scope variable
  2530. st->script->local.vars; // npc variable
  2531. if( n )
  2532. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2533. else
  2534. data->u.num = 0;
  2535. }
  2536. break;
  2537. case '\'':
  2538. {
  2539. unsigned short instance_id = script_instancegetid(st);
  2540. if( instance_id )
  2541. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2542. else {
  2543. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2544. data->u.num = 0;
  2545. }
  2546. }
  2547. break;
  2548. default:
  2549. data->u.num = pc_readglobalreg(sd, data->u.num);
  2550. break;
  2551. }
  2552. }
  2553. data->ref = NULL;
  2554. return;
  2555. }
  2556. void get_val(struct script_state* st, struct script_data* data)
  2557. {
  2558. get_val_(st,data,NULL);
  2559. }
  2560. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2561. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2562. /// The value is left in the top of the stack and needs to be removed manually.
  2563. /// @param st[in]: script state.
  2564. /// @param uid[in]: reference identifier.
  2565. /// @param ref[in]: the container to look up the reference into.
  2566. /// @return: the retrieved value of the reference.
  2567. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2568. {
  2569. struct script_data* data;
  2570. push_val2(st->stack, C_NAME, uid, ref);
  2571. data = script_getdatatop(st, -1);
  2572. get_val(st, data);
  2573. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2574. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2575. }
  2576. /**
  2577. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2578. * Will be gone as soon as undefined var feature is implemented
  2579. **/
  2580. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2581. {
  2582. const char *name = get_str(script_getvarid(uid));
  2583. // is here st can be null pointer and st->rid is wrong?
  2584. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2585. bool insert = false;
  2586. if (sd && !st) {
  2587. // when sd comes, st isn't available
  2588. insert = true;
  2589. } else {
  2590. if( is_string_variable(name) ) {
  2591. char* str = (char*)get_val2(st, uid, ref);
  2592. if( str && *str )
  2593. insert = true;
  2594. script_removetop(st, -1, 0);
  2595. } else {
  2596. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2597. if( num )
  2598. insert = true;
  2599. script_removetop(st, -1, 0);
  2600. }
  2601. }
  2602. if (src && src->arrays) {
  2603. struct script_array *sa = idb_get(src->arrays, script_getvarid(uid));
  2604. if (sa) {
  2605. unsigned int i;
  2606. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2607. if( i != sa->size ) {
  2608. if( !insert )
  2609. script_array_remove_member(src,sa,i);
  2610. return;
  2611. }
  2612. script_array_add_member(sa,0);
  2613. } else if (insert) {
  2614. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2615. }
  2616. }
  2617. }
  2618. /**
  2619. * Returns array size by ID
  2620. **/
  2621. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2622. {
  2623. struct script_array *sa = NULL;
  2624. struct reg_db *src = script_array_src(st, sd, name, ref);
  2625. if (src && src->arrays)
  2626. sa = idb_get(src->arrays, search_str(name));
  2627. return sa ? sa->size : 0;
  2628. }
  2629. /**
  2630. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2631. **/
  2632. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2633. {
  2634. struct script_array *sa = NULL;
  2635. struct reg_db *src = script_array_src(st, sd, name, ref);
  2636. if (src && src->arrays) {
  2637. int key = add_word(name);
  2638. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2639. if( ( sa = idb_get(src->arrays, key) ) ) {
  2640. unsigned int i, highest_key = 0;
  2641. for(i = 0; i < sa->size; i++) {
  2642. if( sa->members[i] > highest_key )
  2643. highest_key = sa->members[i];
  2644. }
  2645. return sa->size ? highest_key + 1 : 0;
  2646. }
  2647. }
  2648. return SCRIPT_CMD_SUCCESS;
  2649. }
  2650. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2651. {
  2652. struct script_array *sa = db_data2ptr(data);
  2653. aFree(sa->members);
  2654. ers_free(array_ers, sa);
  2655. return SCRIPT_CMD_SUCCESS;
  2656. }
  2657. /**
  2658. * Clears script_array and removes it from script->array_db
  2659. **/
  2660. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2661. {
  2662. aFree(sa->members);
  2663. idb_remove(src->arrays, sa->id);
  2664. ers_free(array_ers, sa);
  2665. }
  2666. /**
  2667. * Removes a member from a script_array list
  2668. *
  2669. * @param idx the index of the member in script_array struct list, not of the actual array member
  2670. **/
  2671. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2672. {
  2673. unsigned int i, cursor;
  2674. // it's the only member left, no need to do anything other than delete the array data
  2675. if( sa->size == 1 ) {
  2676. script_array_delete(src,sa);
  2677. return;
  2678. }
  2679. sa->members[idx] = UINT_MAX;
  2680. for(i = 0, cursor = 0; i < sa->size; i++) {
  2681. if( sa->members[i] == UINT_MAX )
  2682. continue;
  2683. if( i != cursor )
  2684. sa->members[cursor] = sa->members[i];
  2685. cursor++;
  2686. }
  2687. sa->size = cursor;
  2688. }
  2689. /**
  2690. * Appends a new array index to the list in script_array
  2691. *
  2692. * @param idx the index of the array member being inserted
  2693. **/
  2694. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2695. {
  2696. RECREATE(sa->members, unsigned int, ++sa->size);
  2697. sa->members[sa->size - 1] = idx;
  2698. }
  2699. /**
  2700. * Obtains the source of the array database for this type and scenario
  2701. * Initializes such database when not yet initialized.
  2702. **/
  2703. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2704. {
  2705. struct reg_db *src = NULL;
  2706. switch( name[0] ) {
  2707. // from player
  2708. default: // char reg
  2709. case '@': // temp char reg
  2710. case '#': // account reg
  2711. src = &sd->regs;
  2712. break;
  2713. case '$': // map reg
  2714. src = &regs;
  2715. break;
  2716. case '.': // npc/script
  2717. if (ref)
  2718. src = ref;
  2719. else
  2720. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2721. break;
  2722. case '\'': // instance
  2723. {
  2724. unsigned short instance_id = script_instancegetid(st);
  2725. if( instance_id ) {
  2726. src = &instance_data[instance_id].regs;
  2727. }
  2728. break;
  2729. }
  2730. }
  2731. if( src ) {
  2732. if( !src->arrays )
  2733. src->arrays = idb_alloc(DB_OPT_BASE);
  2734. return src;
  2735. }
  2736. return NULL;
  2737. }
  2738. /**
  2739. * Processes a array member modification, and update data accordingly
  2740. *
  2741. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2742. * @param num[in] Variable ID
  2743. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2744. **/
  2745. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2746. {
  2747. struct script_array *sa = NULL;
  2748. int id = script_getvarid(num);
  2749. unsigned int index = script_getvaridx(num);
  2750. if (!src->arrays) {
  2751. src->arrays = idb_alloc(DB_OPT_BASE);
  2752. } else {
  2753. sa = idb_get(src->arrays, id);
  2754. }
  2755. if( sa ) {
  2756. unsigned int i;
  2757. // search
  2758. for(i = 0; i < sa->size; i++) {
  2759. if( sa->members[i] == index )
  2760. break;
  2761. }
  2762. // if existent
  2763. if( i != sa->size ) {
  2764. // if empty, we gotta remove it
  2765. if( empty ) {
  2766. script_array_remove_member(src, sa, i);
  2767. }
  2768. } else if( !empty ) { /* new entry */
  2769. script_array_add_member(sa,index);
  2770. // we do nothing if its empty, no point in modifying array data for a new empty member
  2771. }
  2772. } else if ( !empty ) { // we only move to create if not empty
  2773. sa = ers_alloc(array_ers, struct script_array);
  2774. sa->id = id;
  2775. sa->members = NULL;
  2776. sa->size = 0;
  2777. script_array_add_member(sa,index);
  2778. idb_put(src->arrays, id, sa);
  2779. }
  2780. }
  2781. /**
  2782. * Stores the value of a script variable
  2783. *
  2784. * @param st: current script state.
  2785. * @param sd: current character data.
  2786. * @param num: variable identifier.
  2787. * @param name: variable name.
  2788. * @param value: new value.
  2789. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2790. * @return: 0 failure, 1 success.
  2791. *
  2792. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2793. *------------------------------------------*/
  2794. int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2795. {
  2796. char prefix = name[0];
  2797. if( is_string_variable(name) ) {// string variable
  2798. const char *str = (const char*)value;
  2799. switch (prefix) {
  2800. case '@':
  2801. pc_setregstr(sd, num, str);
  2802. return 1;
  2803. case '$':
  2804. return mapreg_setregstr(num, str);
  2805. case '#':
  2806. return (name[1] == '#') ?
  2807. pc_setaccountreg2str(sd, num, str) :
  2808. pc_setaccountregstr(sd, num, str);
  2809. case '.':
  2810. {
  2811. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2812. if( n ) {
  2813. if (str[0]) {
  2814. i64db_put(n->vars, num, aStrdup(str));
  2815. if( script_getvaridx(num) )
  2816. script_array_update(n, num, false);
  2817. } else {
  2818. i64db_remove(n->vars, num);
  2819. if( script_getvaridx(num) )
  2820. script_array_update(n, num, true);
  2821. }
  2822. }
  2823. }
  2824. return 1;
  2825. case '\'':
  2826. {
  2827. unsigned short instance_id = script_instancegetid(st);
  2828. if( instance_id ) {
  2829. if( str[0] ) {
  2830. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2831. if( script_getvaridx(num) )
  2832. script_array_update(&instance_data[instance_id].regs, num, false);
  2833. } else {
  2834. i64db_remove(instance_data[instance_id].regs.vars, num);
  2835. if (script_getvaridx(num))
  2836. script_array_update(&instance_data[instance_id].regs, num, true);
  2837. }
  2838. } else {
  2839. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2840. script_reportsrc(st);
  2841. }
  2842. return 1;
  2843. }
  2844. default:
  2845. return pc_setglobalreg_str(sd, num, str);
  2846. }
  2847. } else {// integer variable
  2848. int val = (int)__64BPRTSIZE(value);
  2849. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2850. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2851. if( st != NULL ) {
  2852. ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val);
  2853. script_reportsrc(st);
  2854. st->state = END;
  2855. }
  2856. return 0;
  2857. }
  2858. return 1;
  2859. }
  2860. switch (prefix) {
  2861. case '@':
  2862. pc_setreg(sd, num, val);
  2863. return 1;
  2864. case '$':
  2865. return mapreg_setreg(num, val);
  2866. case '#':
  2867. return (name[1] == '#') ?
  2868. pc_setaccountreg2(sd, num, val) :
  2869. pc_setaccountreg(sd, num, val);
  2870. case '.':
  2871. {
  2872. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2873. if( n ) {
  2874. if( val != 0 ) {
  2875. i64db_iput(n->vars, num, val);
  2876. if( script_getvaridx(num) )
  2877. script_array_update(n, num, false);
  2878. } else {
  2879. i64db_remove(n->vars, num);
  2880. if( script_getvaridx(num) )
  2881. script_array_update(n, num, true);
  2882. }
  2883. }
  2884. }
  2885. return 1;
  2886. case '\'':
  2887. {
  2888. unsigned short instance_id = script_instancegetid(st);
  2889. if( instance_id ) {
  2890. if( val != 0 ) {
  2891. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2892. if( script_getvaridx(num) )
  2893. script_array_update(&instance_data[instance_id].regs, num, false);
  2894. } else {
  2895. i64db_remove(instance_data[instance_id].regs.vars, num);
  2896. if (script_getvaridx(num))
  2897. script_array_update(&instance_data[instance_id].regs, num, true);
  2898. }
  2899. } else {
  2900. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2901. script_reportsrc(st);
  2902. }
  2903. return 1;
  2904. }
  2905. default:
  2906. return pc_setglobalreg(sd, num, val);
  2907. }
  2908. }
  2909. }
  2910. int set_var(struct map_session_data* sd, char* name, void* val)
  2911. {
  2912. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2913. }
  2914. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2915. {
  2916. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2917. }
  2918. /**
  2919. * Converts the data to a string
  2920. * @param st
  2921. * @param data
  2922. * @param sd
  2923. */
  2924. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2925. {
  2926. char* p;
  2927. get_val_(st, data, sd);
  2928. if( data_isstring(data) )
  2929. {// nothing to convert
  2930. }
  2931. else if( data_isint(data) )
  2932. {// int -> string
  2933. CREATE(p, char, ITEM_NAME_LENGTH);
  2934. snprintf(p, ITEM_NAME_LENGTH, "%"PRId64"", data->u.num);
  2935. p[ITEM_NAME_LENGTH-1] = '\0';
  2936. data->type = C_STR;
  2937. data->u.str = p;
  2938. }
  2939. else if( data_isreference(data) )
  2940. {// reference -> string
  2941. //##TODO when does this happen (check get_val) [FlavioJS]
  2942. data->type = C_CONSTSTR;
  2943. data->u.str = reference_getname(data);
  2944. }
  2945. else
  2946. {// unsupported data type
  2947. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2948. script_reportdata(data);
  2949. script_reportsrc(st);
  2950. data->type = C_CONSTSTR;
  2951. data->u.str = "";
  2952. }
  2953. return data->u.str;
  2954. }
  2955. const char* conv_str(struct script_state* st, struct script_data* data)
  2956. {
  2957. return conv_str_(st, data, NULL);
  2958. }
  2959. /**
  2960. * Converts the data to an int
  2961. * @param st
  2962. * @param data
  2963. * @param sd
  2964. */
  2965. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2966. {
  2967. get_val_(st, data, sd);
  2968. if( data_isint(data) )
  2969. {// nothing to convert
  2970. }
  2971. else if( data_isstring(data) )
  2972. {// string -> int
  2973. // the result does not overflow or underflow, it is capped instead
  2974. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2975. char* p = data->u.str;
  2976. long num;
  2977. errno = 0;
  2978. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2979. if( errno == ERANGE
  2980. #if LONG_MAX > INT_MAX
  2981. || num < INT_MIN || num > INT_MAX
  2982. #endif
  2983. )
  2984. {
  2985. if( num <= INT_MIN )
  2986. {
  2987. num = INT_MIN;
  2988. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2989. }
  2990. else//if( num >= INT_MAX )
  2991. {
  2992. num = INT_MAX;
  2993. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2994. }
  2995. script_reportdata(data);
  2996. script_reportsrc(st);
  2997. }
  2998. if( data->type == C_STR )
  2999. aFree(p);
  3000. data->type = C_INT;
  3001. data->u.num = (int)num;
  3002. }
  3003. #if 0
  3004. // FIXME this function is being used to retrieve the position of labels and
  3005. // probably other stuff [FlavioJS]
  3006. else
  3007. {// unsupported data type
  3008. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  3009. script_reportdata(data);
  3010. script_reportsrc(st);
  3011. data->type = C_INT;
  3012. data->u.num = 0;
  3013. }
  3014. #endif
  3015. return (int)data->u.num;
  3016. }
  3017. int conv_num(struct script_state* st, struct script_data* data)
  3018. {
  3019. return conv_num_(st, data, NULL);
  3020. }
  3021. //
  3022. // Stack operations
  3023. //
  3024. /// Increases the size of the stack
  3025. void stack_expand(struct script_stack* stack)
  3026. {
  3027. stack->sp_max += 64;
  3028. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  3029. stack->sp_max * sizeof(stack->stack_data[0]) );
  3030. memset(stack->stack_data + (stack->sp_max - 64), 0,
  3031. 64 * sizeof(stack->stack_data[0]) );
  3032. }
  3033. /// Pushes a value into the stack
  3034. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  3035. /// Pushes a value into the stack (with reference)
  3036. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  3037. {
  3038. if( stack->sp >= stack->sp_max )
  3039. stack_expand(stack);
  3040. stack->stack_data[stack->sp].type = type;
  3041. stack->stack_data[stack->sp].u.num = val;
  3042. stack->stack_data[stack->sp].ref = ref;
  3043. stack->sp++;
  3044. return &stack->stack_data[stack->sp-1];
  3045. }
  3046. /// Pushes a string into the stack
  3047. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  3048. {
  3049. if( stack->sp >= stack->sp_max )
  3050. stack_expand(stack);
  3051. stack->stack_data[stack->sp].type = type;
  3052. stack->stack_data[stack->sp].u.str = str;
  3053. stack->stack_data[stack->sp].ref = NULL;
  3054. stack->sp++;
  3055. return &stack->stack_data[stack->sp-1];
  3056. }
  3057. /// Pushes a retinfo into the stack
  3058. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  3059. {
  3060. if( stack->sp >= stack->sp_max )
  3061. stack_expand(stack);
  3062. stack->stack_data[stack->sp].type = C_RETINFO;
  3063. stack->stack_data[stack->sp].u.ri = ri;
  3064. stack->stack_data[stack->sp].ref = ref;
  3065. stack->sp++;
  3066. return &stack->stack_data[stack->sp-1];
  3067. }
  3068. /// Pushes a copy of the target position into the stack
  3069. struct script_data* push_copy(struct script_stack* stack, int pos)
  3070. {
  3071. switch( stack->stack_data[pos].type ) {
  3072. case C_CONSTSTR:
  3073. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  3074. break;
  3075. case C_STR:
  3076. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  3077. break;
  3078. case C_RETINFO:
  3079. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  3080. exit(1);
  3081. break;
  3082. default:
  3083. return push_val2(
  3084. stack,stack->stack_data[pos].type,
  3085. stack->stack_data[pos].u.num,
  3086. stack->stack_data[pos].ref
  3087. );
  3088. break;
  3089. }
  3090. }
  3091. /// Removes the values in indexes [start,end] from the stack.
  3092. /// Adjusts all stack pointers.
  3093. void pop_stack(struct script_state* st, int start, int end)
  3094. {
  3095. struct script_stack* stack = st->stack;
  3096. struct script_data* data;
  3097. int i;
  3098. if( start < 0 )
  3099. start = 0;
  3100. if( end > stack->sp )
  3101. end = stack->sp;
  3102. if( start >= end )
  3103. return;// nothing to pop
  3104. // free stack elements
  3105. for( i = start; i < end; i++ )
  3106. {
  3107. data = &stack->stack_data[i];
  3108. if( data->type == C_STR )
  3109. aFree(data->u.str);
  3110. if( data->type == C_RETINFO ) {
  3111. struct script_retinfo* ri = data->u.ri;
  3112. if (ri->scope.vars) {
  3113. script_free_vars(ri->scope.vars);
  3114. ri->scope.vars = NULL;
  3115. }
  3116. if (ri->scope.arrays) {
  3117. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3118. ri->scope.arrays = NULL;
  3119. }
  3120. if( data->ref )
  3121. aFree(data->ref);
  3122. aFree(ri);
  3123. }
  3124. data->type = C_NOP;
  3125. }
  3126. // move the rest of the elements
  3127. if( stack->sp > end )
  3128. {
  3129. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3130. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3131. stack->stack_data[i].type = C_NOP;
  3132. }
  3133. // adjust stack pointers
  3134. if( st->start > end ) st->start -= end - start;
  3135. else if( st->start > start ) st->start = start;
  3136. if( st->end > end ) st->end -= end - start;
  3137. else if( st->end > start ) st->end = start;
  3138. if( stack->defsp > end ) stack->defsp -= end - start;
  3139. else if( stack->defsp > start ) stack->defsp = start;
  3140. stack->sp -= end - start;
  3141. }
  3142. ///
  3143. ///
  3144. ///
  3145. /*==========================================
  3146. * Release script dependent variable, dependent variable of function
  3147. *------------------------------------------*/
  3148. void script_free_vars(struct DBMap* storage)
  3149. {
  3150. if( storage ) {
  3151. // destroy the storage construct containing the variables
  3152. db_destroy(storage);
  3153. }
  3154. }
  3155. void script_free_code(struct script_code* code)
  3156. {
  3157. nullpo_retv(code);
  3158. if (code->instances)
  3159. script_stop_scriptinstances(code);
  3160. script_free_vars(code->local.vars);
  3161. if (code->local.arrays)
  3162. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3163. aFree(code->script_buf);
  3164. aFree(code);
  3165. }
  3166. /// Creates a new script state.
  3167. ///
  3168. /// @param script Script code
  3169. /// @param pos Position in the code
  3170. /// @param rid Who is running the script (attached player)
  3171. /// @param oid Where the code is being run (npc 'object')
  3172. /// @return Script state
  3173. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3174. {
  3175. struct script_state* st;
  3176. st = ers_alloc(st_ers, struct script_state);
  3177. st->stack = ers_alloc(stack_ers, struct script_stack);
  3178. st->stack->sp = 0;
  3179. st->stack->sp_max = 64;
  3180. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3181. st->stack->defsp = st->stack->sp;
  3182. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3183. st->stack->scope.arrays = NULL;
  3184. st->state = RUN;
  3185. st->script = rootscript;
  3186. st->pos = pos;
  3187. st->rid = rid;
  3188. st->oid = oid;
  3189. st->sleep.timer = INVALID_TIMER;
  3190. st->npc_item_flag = battle_config.item_enabled_npc;
  3191. if( st->script->instances != USHRT_MAX )
  3192. st->script->instances++;
  3193. else {
  3194. struct npc_data *nd = map_id2nd(oid);
  3195. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3196. }
  3197. if (!st->script->local.vars)
  3198. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3199. st->id = next_id++;
  3200. active_scripts++;
  3201. idb_put(st_db, st->id, st);
  3202. return st;
  3203. }
  3204. /// Frees a script state.
  3205. ///
  3206. /// @param st Script state
  3207. void script_free_state(struct script_state* st)
  3208. {
  3209. if (idb_exists(st_db, st->id)) {
  3210. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3211. if (st->bk_st) // backup was not restored
  3212. 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);
  3213. if (sd && sd->st == st) { // Current script is aborted.
  3214. if(sd->state.using_fake_npc) {
  3215. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3216. sd->state.using_fake_npc = 0;
  3217. }
  3218. sd->st = NULL;
  3219. sd->npc_id = 0;
  3220. }
  3221. if (st->sleep.timer != INVALID_TIMER)
  3222. delete_timer(st->sleep.timer, run_script_timer);
  3223. if (st->stack) {
  3224. script_free_vars(st->stack->scope.vars);
  3225. if (st->stack->scope.arrays)
  3226. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3227. pop_stack(st, 0, st->stack->sp);
  3228. aFree(st->stack->stack_data);
  3229. ers_free(stack_ers, st->stack);
  3230. st->stack = NULL;
  3231. }
  3232. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3233. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3234. script_free_vars(st->script->local.vars);
  3235. st->script->local.vars = NULL;
  3236. }
  3237. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3238. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3239. st->script->local.arrays = NULL;
  3240. }
  3241. }
  3242. st->pos = -1;
  3243. idb_remove(st_db, st->id);
  3244. ers_free(st_ers, st);
  3245. if (--active_scripts == 0)
  3246. next_id = 0;
  3247. }
  3248. }
  3249. //
  3250. // Main execution unit
  3251. //
  3252. /*==========================================
  3253. * Read command
  3254. *------------------------------------------*/
  3255. c_op get_com(unsigned char *script,int *pos)
  3256. {
  3257. int i = 0, j = 0;
  3258. if(script[*pos]>=0x80){
  3259. return C_INT;
  3260. }
  3261. while(script[*pos]>=0x40){
  3262. i=script[(*pos)++]<<j;
  3263. j+=6;
  3264. }
  3265. return (c_op)(i+(script[(*pos)++]<<j));
  3266. }
  3267. /*==========================================
  3268. * Income figures
  3269. *------------------------------------------*/
  3270. int get_num(unsigned char *script,int *pos)
  3271. {
  3272. int i,j;
  3273. i=0; j=0;
  3274. while(script[*pos]>=0xc0){
  3275. i+=(script[(*pos)++]&0x7f)<<j;
  3276. j+=6;
  3277. }
  3278. return i+((script[(*pos)++]&0x7f)<<j);
  3279. }
  3280. /// Ternary operators
  3281. /// test ? if_true : if_false
  3282. void op_3(struct script_state* st, int op)
  3283. {
  3284. struct script_data* data;
  3285. int flag = 0;
  3286. data = script_getdatatop(st, -3);
  3287. get_val(st, data);
  3288. if( data_isstring(data) )
  3289. flag = data->u.str[0];// "" -> false
  3290. else if( data_isint(data) )
  3291. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3292. else
  3293. {
  3294. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3295. script_reportdata(data);
  3296. script_reportsrc(st);
  3297. script_removetop(st, -3, 0);
  3298. script_pushnil(st);
  3299. return;
  3300. }
  3301. if( flag )
  3302. script_pushcopytop(st, -2);
  3303. else
  3304. script_pushcopytop(st, -1);
  3305. script_removetop(st, -4, -1);
  3306. }
  3307. /// Binary string operators
  3308. /// s1 EQ s2 -> i
  3309. /// s1 NE s2 -> i
  3310. /// s1 GT s2 -> i
  3311. /// s1 GE s2 -> i
  3312. /// s1 LT s2 -> i
  3313. /// s1 LE s2 -> i
  3314. /// s1 ADD s2 -> s
  3315. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3316. {
  3317. int a = 0;
  3318. switch(op){
  3319. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3320. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3321. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3322. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3323. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3324. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3325. case C_ADD:
  3326. {
  3327. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3328. strcpy(buf, s1);
  3329. strcat(buf, s2);
  3330. script_pushstr(st, buf);
  3331. return;
  3332. }
  3333. default:
  3334. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3335. script_reportsrc(st);
  3336. script_pushnil(st);
  3337. st->state = END;
  3338. return;
  3339. }
  3340. script_pushint(st,a);
  3341. }
  3342. /// Binary number operators
  3343. /// i OP i -> i
  3344. void op_2num(struct script_state* st, int op, int i1, int i2)
  3345. {
  3346. int ret;
  3347. double ret_double;
  3348. switch( op ) {
  3349. case C_AND: ret = i1 & i2; break;
  3350. case C_OR: ret = i1 | i2; break;
  3351. case C_XOR: ret = i1 ^ i2; break;
  3352. case C_LAND: ret = (i1 && i2); break;
  3353. case C_LOR: ret = (i1 || i2); break;
  3354. case C_EQ: ret = (i1 == i2); break;
  3355. case C_NE: ret = (i1 != i2); break;
  3356. case C_GT: ret = (i1 > i2); break;
  3357. case C_GE: ret = (i1 >= i2); break;
  3358. case C_LT: ret = (i1 < i2); break;
  3359. case C_LE: ret = (i1 <= i2); break;
  3360. case C_R_SHIFT: ret = i1>>i2; break;
  3361. case C_L_SHIFT: ret = i1<<i2; break;
  3362. case C_DIV:
  3363. case C_MOD:
  3364. if( i2 == 0 )
  3365. {
  3366. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3367. script_reportsrc(st);
  3368. script_pushnil(st);
  3369. st->state = END;
  3370. return;
  3371. }
  3372. else if( op == C_DIV )
  3373. ret = i1 / i2;
  3374. else//if( op == C_MOD )
  3375. ret = i1 % i2;
  3376. break;
  3377. default:
  3378. switch( op ) {// operators that can overflow/underflow
  3379. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3380. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3381. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3382. default:
  3383. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3384. script_reportsrc(st);
  3385. script_pushnil(st);
  3386. return;
  3387. }
  3388. if( ret_double < (double)INT_MIN ) {
  3389. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3390. script_reportsrc(st);
  3391. ret = INT_MIN;
  3392. }
  3393. else if( ret_double > (double)INT_MAX ) {
  3394. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3395. script_reportsrc(st);
  3396. ret = INT_MAX;
  3397. }
  3398. }
  3399. script_pushint(st, ret);
  3400. }
  3401. /// Binary operators
  3402. void op_2(struct script_state *st, int op)
  3403. {
  3404. struct script_data* left, leftref;
  3405. struct script_data* right;
  3406. leftref.type = C_NOP;
  3407. left = script_getdatatop(st, -2);
  3408. right = script_getdatatop(st, -1);
  3409. if (st->op2ref) {
  3410. if (data_isreference(left)) {
  3411. leftref = *left;
  3412. }
  3413. st->op2ref = 0;
  3414. }
  3415. get_val(st, left);
  3416. get_val(st, right);
  3417. // automatic conversions
  3418. switch( op )
  3419. {
  3420. case C_ADD:
  3421. if( data_isint(left) && data_isstring(right) )
  3422. {// convert int-string to string-string
  3423. conv_str(st, left);
  3424. }
  3425. else if( data_isstring(left) && data_isint(right) )
  3426. {// convert string-int to string-string
  3427. conv_str(st, right);
  3428. }
  3429. break;
  3430. }
  3431. if( data_isstring(left) && data_isstring(right) )
  3432. {// ss => op_2str
  3433. op_2str(st, op, left->u.str, right->u.str);
  3434. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3435. if (leftref.type != C_NOP)
  3436. {
  3437. if (left->type == C_STR) // don't free C_CONSTSTR
  3438. aFree(left->u.str);
  3439. *left = leftref;
  3440. }
  3441. }
  3442. else if( data_isint(left) && data_isint(right) )
  3443. {// ii => op_2num
  3444. int i1 = (int)left->u.num;
  3445. int i2 = (int)right->u.num;
  3446. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3447. op_2num(st, op, i1, i2);
  3448. if (leftref.type != C_NOP)
  3449. *left = leftref;
  3450. }
  3451. else
  3452. {// invalid argument
  3453. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3454. script_reportdata(left);
  3455. script_reportdata(right);
  3456. script_reportsrc(st);
  3457. script_removetop(st, -2, 0);
  3458. script_pushnil(st);
  3459. st->state = END;
  3460. }
  3461. }
  3462. /// Unary operators
  3463. /// NEG i -> i
  3464. /// NOT i -> i
  3465. /// LNOT i -> i
  3466. void op_1(struct script_state* st, int op)
  3467. {
  3468. struct script_data* data;
  3469. int i1;
  3470. data = script_getdatatop(st, -1);
  3471. get_val(st, data);
  3472. if( !data_isint(data) )
  3473. {// not a number
  3474. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3475. script_reportdata(data);
  3476. script_reportsrc(st);
  3477. script_pushnil(st);
  3478. st->state = END;
  3479. return;
  3480. }
  3481. i1 = (int)data->u.num;
  3482. script_removetop(st, -1, 0);
  3483. switch( op )
  3484. {
  3485. case C_NEG: i1 = -i1; break;
  3486. case C_NOT: i1 = ~i1; break;
  3487. case C_LNOT: i1 = !i1; break;
  3488. default:
  3489. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3490. script_reportsrc(st);
  3491. script_pushnil(st);
  3492. st->state = END;
  3493. return;
  3494. }
  3495. script_pushint(st, i1);
  3496. }
  3497. /// Checks the type of all arguments passed to a built-in function.
  3498. ///
  3499. /// @param st Script state whose stack arguments should be inspected.
  3500. /// @param func Built-in function for which the arguments are intended.
  3501. static void script_check_buildin_argtype(struct script_state* st, int func)
  3502. {
  3503. int idx, invalid = 0;
  3504. for( idx = 2; script_hasdata(st, idx); idx++ )
  3505. {
  3506. struct script_data* data = script_getdata(st, idx);
  3507. script_function* sf = &buildin_func[str_data[func].val];
  3508. char type = sf->arg[idx-2];
  3509. if( type == '?' || type == '*' )
  3510. {// optional argument or unknown number of optional parameters ( no types are after this )
  3511. break;
  3512. }
  3513. else if( type == 0 )
  3514. {// more arguments than necessary ( should not happen, as it is checked before )
  3515. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3516. invalid++;
  3517. break;
  3518. }
  3519. else
  3520. {
  3521. const char* name = NULL;
  3522. if( data_isreference(data) )
  3523. {// get name for variables to determine the type they refer to
  3524. name = reference_getname(data);
  3525. }
  3526. switch( type )
  3527. {
  3528. case 'v':
  3529. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3530. {// variant
  3531. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3532. script_reportdata(data);
  3533. invalid++;
  3534. }
  3535. break;
  3536. case 's':
  3537. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3538. {// string
  3539. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3540. script_reportdata(data);
  3541. invalid++;
  3542. }
  3543. break;
  3544. case 'i':
  3545. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3546. {// int ( params and constants are always int )
  3547. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3548. script_reportdata(data);
  3549. invalid++;
  3550. }
  3551. break;
  3552. case 'r':
  3553. if( !data_isreference(data) )
  3554. {// variables
  3555. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3556. script_reportdata(data);
  3557. invalid++;
  3558. }
  3559. break;
  3560. case 'l':
  3561. if( !data_islabel(data) && !data_isfunclabel(data) )
  3562. {// label
  3563. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3564. script_reportdata(data);
  3565. invalid++;
  3566. }
  3567. break;
  3568. }
  3569. }
  3570. }
  3571. if(invalid)
  3572. {
  3573. ShowDebug("Function: %s\n", get_str(func));
  3574. script_reportsrc(st);
  3575. }
  3576. }
  3577. /// Executes a buildin command.
  3578. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3579. int run_func(struct script_state *st)
  3580. {
  3581. struct script_data* data;
  3582. int i,start_sp,end_sp,func;
  3583. end_sp = st->stack->sp;// position after the last argument
  3584. for( i = end_sp-1; i > 0 ; --i )
  3585. if( st->stack->stack_data[i].type == C_ARG )
  3586. break;
  3587. if( i == 0 ) {
  3588. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3589. st->state = END;
  3590. script_reportsrc(st);
  3591. return 1;
  3592. }
  3593. start_sp = i-1;// C_NAME of the command
  3594. st->start = start_sp;
  3595. st->end = end_sp;
  3596. data = &st->stack->stack_data[st->start];
  3597. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3598. func = (int)data->u.num;
  3599. st->funcname = reference_getname(data);
  3600. } else {
  3601. ShowError("script:run_func: not a buildin command.\n");
  3602. script_reportdata(data);
  3603. script_reportsrc(st);
  3604. st->state = END;
  3605. return 1;
  3606. }
  3607. if( script_config.warn_func_mismatch_argtypes ) {
  3608. script_check_buildin_argtype(st, func);
  3609. }
  3610. if(str_data[func].func) {
  3611. #if defined(SCRIPT_COMMAND_DEPRECATION)
  3612. if( buildin_func[str_data[func].val].deprecated ){
  3613. ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) );
  3614. ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated );
  3615. script_reportsrc(st);
  3616. }
  3617. #endif
  3618. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3619. script_reportsrc(st);
  3620. } else {
  3621. 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));
  3622. script_reportsrc(st);
  3623. st->state = END;
  3624. }
  3625. // Stack's datum are used when re-running functions [Eoe]
  3626. if( st->state == RERUNLINE )
  3627. return 0;
  3628. pop_stack(st, st->start, st->end);
  3629. if( st->state == RETFUNC ) {// return from a user-defined function
  3630. struct script_retinfo* ri;
  3631. int olddefsp = st->stack->defsp;
  3632. int nargs;
  3633. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3634. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3635. {
  3636. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3637. script_reportsrc(st);
  3638. st->state = END;
  3639. return 1;
  3640. }
  3641. script_free_vars(st->stack->scope.vars);
  3642. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3643. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3644. nargs = ri->nargs;
  3645. st->pos = ri->pos;
  3646. st->script = ri->script;
  3647. st->stack->scope.vars = ri->scope.vars;
  3648. st->stack->scope.arrays = ri->scope.arrays;
  3649. st->stack->defsp = ri->defsp;
  3650. memset(ri, 0, sizeof(struct script_retinfo));
  3651. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3652. st->state = GOTO;
  3653. }
  3654. return 0;
  3655. }
  3656. /*==========================================
  3657. * script execution
  3658. *------------------------------------------*/
  3659. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3660. {
  3661. struct script_state *st;
  3662. if( rootscript == NULL || pos < 0 )
  3663. return;
  3664. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3665. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3666. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3667. st = script_alloc_state(rootscript, pos, rid, oid);
  3668. run_script_main(st);
  3669. }
  3670. /**
  3671. * Free all related script code
  3672. * @param code: Script code to free
  3673. */
  3674. void script_stop_scriptinstances(struct script_code *code) {
  3675. DBIterator *iter;
  3676. struct script_state* st;
  3677. if( !active_scripts )
  3678. return; // Don't even bother.
  3679. iter = db_iterator(st_db);
  3680. for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) {
  3681. if( st->script == code )
  3682. script_free_state(st);
  3683. }
  3684. dbi_destroy(iter);
  3685. }
  3686. /*==========================================
  3687. * Timer function for sleep
  3688. *------------------------------------------*/
  3689. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3690. {
  3691. struct script_state *st = (struct script_state *)data;
  3692. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3693. // If it was a player before going to sleep and there is still a unit attached to the script
  3694. if( id != 0 && st->rid ){
  3695. struct map_session_data *sd = map_id2sd(st->rid);
  3696. // Attached player is offline(logout) or another unit type(should not happen)
  3697. if( !sd ){
  3698. st->rid = 0;
  3699. st->state = END;
  3700. // Character mismatch. Cancel execution.
  3701. }else if( sd->status.char_id != id ){
  3702. ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id );
  3703. script_reportsrc(st);
  3704. st->rid = 0;
  3705. st->state = END;
  3706. }
  3707. }
  3708. while (node && st->sleep.timer != INVALID_TIMER) {
  3709. if ((int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer) {
  3710. script_erase_sleepdb(node);
  3711. st->sleep.timer = INVALID_TIMER;
  3712. break;
  3713. }
  3714. node = node->next;
  3715. }
  3716. if(st->state != RERUNLINE)
  3717. st->sleep.tick = 0;
  3718. run_script_main(st);
  3719. return 0;
  3720. }
  3721. /**
  3722. * Remove sleep timers from the NPC
  3723. * @param id: NPC ID
  3724. */
  3725. void script_stop_sleeptimers(int id) {
  3726. for (;;) {
  3727. struct script_state *st = (struct script_state *)linkdb_erase(&sleep_db, (void *)__64BPRTSIZE(id));
  3728. if (!st)
  3729. break; // No more sleep timers
  3730. if (st->oid == id)
  3731. script_free_state(st);
  3732. }
  3733. }
  3734. /**
  3735. * Delete the specified node from sleep_db
  3736. * @param n: Linked list of sleep timers
  3737. */
  3738. struct linkdb_node *script_erase_sleepdb(struct linkdb_node *n) {
  3739. struct linkdb_node *retnode;
  3740. if (!n)
  3741. return NULL;
  3742. if (!n->prev)
  3743. sleep_db = n->next;
  3744. else
  3745. n->prev->next = n->next;
  3746. if (n->next)
  3747. n->next->prev = n->prev;
  3748. retnode = n->next;
  3749. aFree(n);
  3750. return retnode;
  3751. }
  3752. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3753. ///
  3754. /// @param st Script state to detach.
  3755. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3756. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3757. {
  3758. struct map_session_data* sd;
  3759. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3760. sd->st = st->bk_st;
  3761. sd->npc_id = st->bk_npcid;
  3762. sd->state.disable_atcommand_on_npc = 0;
  3763. if(st->bk_st) {
  3764. //Remove tag for removal.
  3765. st->bk_st = NULL;
  3766. st->bk_npcid = 0;
  3767. } else if(dequeue_event) {
  3768. #ifdef SECURE_NPCTIMEOUT
  3769. /**
  3770. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3771. **/
  3772. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3773. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3774. sd->npc_idle_timer = INVALID_TIMER;
  3775. }
  3776. #endif
  3777. npc_event_dequeue(sd);
  3778. }
  3779. }
  3780. else if(st->bk_st)
  3781. {// rid was set to 0, before detaching the script state
  3782. 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);
  3783. script_reportsrc(st->bk_st);
  3784. script_free_state(st->bk_st);
  3785. st->bk_st = NULL;
  3786. }
  3787. }
  3788. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3789. ///
  3790. /// @param st Script state to attach.
  3791. static void script_attach_state(struct script_state* st)
  3792. {
  3793. struct map_session_data* sd;
  3794. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3795. {
  3796. if(st!=sd->st)
  3797. {
  3798. if(st->bk_st)
  3799. {// there is already a backup
  3800. 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);
  3801. }
  3802. st->bk_st = sd->st;
  3803. st->bk_npcid = sd->npc_id;
  3804. }
  3805. sd->st = st;
  3806. sd->npc_id = st->oid;
  3807. sd->npc_item_flag = st->npc_item_flag; // load default.
  3808. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3809. #ifdef SECURE_NPCTIMEOUT
  3810. if( sd->npc_idle_timer == INVALID_TIMER )
  3811. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3812. sd->npc_idle_tick = gettick();
  3813. #endif
  3814. }
  3815. }
  3816. /*==========================================
  3817. * The main part of the script execution
  3818. *------------------------------------------*/
  3819. void run_script_main(struct script_state *st)
  3820. {
  3821. int cmdcount = script_config.check_cmdcount;
  3822. int gotocount = script_config.check_gotocount;
  3823. TBL_PC *sd;
  3824. struct script_stack *stack = st->stack;
  3825. script_attach_state(st);
  3826. if(st->state == RERUNLINE) {
  3827. run_func(st);
  3828. if(st->state == GOTO)
  3829. st->state = RUN;
  3830. } else if(st->state != END)
  3831. st->state = RUN;
  3832. while(st->state == RUN) {
  3833. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3834. switch(c){
  3835. case C_EOL:
  3836. if( stack->defsp > stack->sp )
  3837. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3838. else
  3839. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3840. break;
  3841. case C_INT:
  3842. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3843. break;
  3844. case C_POS:
  3845. case C_NAME:
  3846. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3847. st->pos+=3;
  3848. break;
  3849. case C_ARG:
  3850. push_val(stack,c,0);
  3851. break;
  3852. case C_STR:
  3853. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3854. while(st->script->script_buf[st->pos++]);
  3855. break;
  3856. case C_FUNC:
  3857. run_func(st);
  3858. if(st->state==GOTO){
  3859. st->state = RUN;
  3860. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3861. ShowError("script:run_script_main: infinity loop !\n");
  3862. script_reportsrc(st);
  3863. st->state=END;
  3864. }
  3865. }
  3866. break;
  3867. case C_REF:
  3868. st->op2ref = 1;
  3869. break;
  3870. case C_NEG:
  3871. case C_NOT:
  3872. case C_LNOT:
  3873. op_1(st ,c);
  3874. break;
  3875. case C_ADD:
  3876. case C_SUB:
  3877. case C_MUL:
  3878. case C_DIV:
  3879. case C_MOD:
  3880. case C_EQ:
  3881. case C_NE:
  3882. case C_GT:
  3883. case C_GE:
  3884. case C_LT:
  3885. case C_LE:
  3886. case C_AND:
  3887. case C_OR:
  3888. case C_XOR:
  3889. case C_LAND:
  3890. case C_LOR:
  3891. case C_R_SHIFT:
  3892. case C_L_SHIFT:
  3893. op_2(st, c);
  3894. break;
  3895. case C_OP3:
  3896. op_3(st, c);
  3897. break;
  3898. case C_NOP:
  3899. st->state=END;
  3900. break;
  3901. default:
  3902. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3903. st->state=END;
  3904. break;
  3905. }
  3906. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3907. ShowError("script:run_script_main: infinity loop !\n");
  3908. script_reportsrc(st);
  3909. st->state=END;
  3910. }
  3911. }
  3912. if(st->sleep.tick > 0) {
  3913. //Restore previous script
  3914. script_detach_state(st, false);
  3915. //Delay execution
  3916. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3917. st->sleep.charid = sd?sd->status.char_id:0;
  3918. st->sleep.timer = add_timer(gettick() + st->sleep.tick, run_script_timer, st->sleep.charid, (intptr_t)st);
  3919. linkdb_insert(&sleep_db, (void *)__64BPRTSIZE(st->oid), st);
  3920. } else if(st->state != END && st->rid) {
  3921. //Resume later (st is already attached to player).
  3922. if(st->bk_st) {
  3923. ShowWarning("Unable to restore stack! Double continuation!\n");
  3924. //Report BOTH scripts to see if that can help somehow.
  3925. ShowDebug("Previous script (lost):\n");
  3926. script_reportsrc(st->bk_st);
  3927. ShowDebug("Current script:\n");
  3928. script_reportsrc(st);
  3929. script_free_state(st->bk_st);
  3930. st->bk_st = NULL;
  3931. }
  3932. } else {
  3933. //Dispose of script.
  3934. if ((sd = map_id2sd(st->rid))!=NULL)
  3935. { //Restore previous stack and save char.
  3936. if(sd->state.using_fake_npc){
  3937. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3938. sd->state.using_fake_npc = 0;
  3939. }
  3940. //Restore previous script if any.
  3941. script_detach_state(st, true);
  3942. if (sd->vars_dirty)
  3943. intif_saveregistry(sd);
  3944. }
  3945. script_free_state(st);
  3946. }
  3947. }
  3948. int script_config_read(const char *cfgName)
  3949. {
  3950. int i;
  3951. char line[1024],w1[1024],w2[1024];
  3952. FILE *fp;
  3953. fp=fopen(cfgName,"r");
  3954. if(fp==NULL){
  3955. ShowError("File not found: %s\n", cfgName);
  3956. return 1;
  3957. }
  3958. while(fgets(line, sizeof(line), fp))
  3959. {
  3960. if(line[0] == '/' && line[1] == '/')
  3961. continue;
  3962. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3963. if(i!=2)
  3964. continue;
  3965. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3966. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3967. }
  3968. else if(strcmpi(w1,"check_cmdcount")==0) {
  3969. script_config.check_cmdcount = config_switch(w2);
  3970. }
  3971. else if(strcmpi(w1,"check_gotocount")==0) {
  3972. script_config.check_gotocount = config_switch(w2);
  3973. }
  3974. else if(strcmpi(w1,"input_min_value")==0) {
  3975. script_config.input_min_value = config_switch(w2);
  3976. }
  3977. else if(strcmpi(w1,"input_max_value")==0) {
  3978. script_config.input_max_value = config_switch(w2);
  3979. }
  3980. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3981. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3982. }
  3983. else if(strcmpi(w1,"import")==0){
  3984. script_config_read(w2);
  3985. }
  3986. else {
  3987. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3988. }
  3989. }
  3990. fclose(fp);
  3991. return 0;
  3992. }
  3993. /**
  3994. * @see DBApply
  3995. */
  3996. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3997. {
  3998. struct script_code *code = db_data2ptr(data);
  3999. if (code)
  4000. script_free_code(code);
  4001. return 0;
  4002. }
  4003. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  4004. {
  4005. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  4006. if( script )
  4007. {
  4008. int j;
  4009. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  4010. if( j < EQI_MAX ) {
  4011. //Single item autobonus
  4012. current_equip_item_index = sd->equip_index[j];
  4013. current_equip_combo_pos = 0;
  4014. } else {
  4015. //Combo autobonus
  4016. current_equip_item_index = -1;
  4017. current_equip_combo_pos = pos;
  4018. }
  4019. run_script(script,0,sd->bl.id,0);
  4020. }
  4021. }
  4022. void script_add_autobonus(const char *autobonus)
  4023. {
  4024. if( strdb_get(autobonus_db, autobonus) == NULL )
  4025. {
  4026. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  4027. if( script )
  4028. strdb_put(autobonus_db, autobonus, script);
  4029. }
  4030. }
  4031. /// resets a temporary character array variable to given value
  4032. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  4033. {
  4034. struct script_array *sa = NULL;
  4035. struct reg_db *src = NULL;
  4036. unsigned int i, *list = NULL, size = 0;
  4037. int key;
  4038. key = add_str(varname);
  4039. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  4040. return;
  4041. if( value )
  4042. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  4043. if( !(sa = idb_get(src->arrays, key)) ) /* non-existent array, nothing to empty */
  4044. return;
  4045. size = sa->size;
  4046. list = script_array_cpy_list(sa);
  4047. for(i = 0; i < size; i++) {
  4048. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  4049. }
  4050. }
  4051. /// sets a temporary character array variable element idx to given value
  4052. /// @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.
  4053. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  4054. {
  4055. int key;
  4056. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  4057. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  4058. return;
  4059. }
  4060. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  4061. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  4062. if( refcache ) {
  4063. // save to avoid repeated script->add_str calls
  4064. refcache[0] = key;
  4065. }
  4066. }
  4067. /**
  4068. * Clears persistent variables from memory
  4069. **/
  4070. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  4071. {
  4072. struct script_reg_state *src;
  4073. if( data->type != DB_DATA_PTR ) // got no need for those!
  4074. return 0;
  4075. src = db_data2ptr(data);
  4076. if( src->type ) {
  4077. struct script_reg_str *p = (struct script_reg_str *)src;
  4078. if( p->value )
  4079. aFree(p->value);
  4080. ers_free(str_reg_ers,p);
  4081. } else {
  4082. ers_free(num_reg_ers,(struct script_reg_num*)src);
  4083. }
  4084. return 0;
  4085. }
  4086. /**
  4087. * Clears a single persistent variable
  4088. **/
  4089. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  4090. {
  4091. i64db_remove(sd->regs.vars, reg);
  4092. if( data->type ) {
  4093. struct script_reg_str *p = (struct script_reg_str*)data;
  4094. if( p->value )
  4095. aFree(p->value);
  4096. ers_free(str_reg_ers,p);
  4097. } else {
  4098. ers_free(num_reg_ers,(struct script_reg_num*)data);
  4099. }
  4100. }
  4101. unsigned int *script_array_cpy_list(struct script_array *sa)
  4102. {
  4103. if( sa->size > generic_ui_array_size )
  4104. script_generic_ui_array_expand(sa->size);
  4105. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  4106. return generic_ui_array;
  4107. }
  4108. void script_generic_ui_array_expand (unsigned int plus)
  4109. {
  4110. generic_ui_array_size += plus + 100;
  4111. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  4112. }
  4113. int buildin_query_sql_sub(struct script_state *st, Sql *handle);
  4114. #ifdef BETA_THREAD_TEST
  4115. /* used to receive items the queryThread has already processed */
  4116. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  4117. int i, cursor = 0;
  4118. bool allOk = true;
  4119. EnterSpinLock(&queryThreadLock);
  4120. for( i = 0; i < queryThreadData.count; i++ ) {
  4121. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4122. if( !entry->ok ) {
  4123. allOk = false;
  4124. continue;
  4125. }
  4126. run_script_main(entry->st);
  4127. entry->st = NULL;/* empty entries */
  4128. aFree(entry);
  4129. queryThreadData.entry[i] = NULL;
  4130. }
  4131. if( allOk ) {
  4132. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  4133. delete_timer(queryThreadData.timer, queryThread_timer);
  4134. queryThreadData.timer = INVALID_TIMER;
  4135. }
  4136. /* now lets clear the mess. */
  4137. for( i = 0; i < queryThreadData.count; i++ ) {
  4138. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4139. if( entry == NULL )
  4140. continue;/* entry on hold */
  4141. /* move */
  4142. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  4143. cursor++;
  4144. }
  4145. queryThreadData.count = cursor;
  4146. LeaveSpinLock(&queryThreadLock);
  4147. return 0;
  4148. }
  4149. void queryThread_add(struct script_state *st, bool type) {
  4150. int idx = 0;
  4151. struct queryThreadEntry* entry = NULL;
  4152. EnterSpinLock(&queryThreadLock);
  4153. if( queryThreadData.count++ != 0 )
  4154. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  4155. idx = queryThreadData.count-1;
  4156. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  4157. entry = queryThreadData.entry[idx];
  4158. entry->st = st;
  4159. entry->ok = false;
  4160. entry->type = type;
  4161. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  4162. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  4163. }
  4164. LeaveSpinLock(&queryThreadLock);
  4165. /* unlock the queryThread */
  4166. racond_signal(queryThreadCond);
  4167. }
  4168. /* adds a new log to the queue */
  4169. void queryThread_log(char * entry, int length) {
  4170. int idx = logThreadData.count;
  4171. EnterSpinLock(&queryThreadLock);
  4172. if( logThreadData.count++ != 0 )
  4173. RECREATE(logThreadData.entry, char* , logThreadData.count);
  4174. CREATE(logThreadData.entry[idx], char, length + 1 );
  4175. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  4176. LeaveSpinLock(&queryThreadLock);
  4177. /* unlock the queryThread */
  4178. racond_signal(queryThreadCond);
  4179. }
  4180. /* queryThread_main */
  4181. static void *queryThread_main(void *x) {
  4182. Sql *queryThread_handle = Sql_Malloc();
  4183. int i;
  4184. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  4185. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4186. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  4187. Sql_ShowDebug(queryThread_handle);
  4188. Sql_Free(queryThread_handle);
  4189. exit(EXIT_FAILURE);
  4190. }
  4191. if( strlen(default_codepage) > 0 )
  4192. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  4193. Sql_ShowDebug(queryThread_handle);
  4194. if( log_config.sql_logs ) {
  4195. logmysql_handle = Sql_Malloc();
  4196. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  4197. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4198. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  4199. Sql_ShowDebug(logmysql_handle);
  4200. Sql_Free(logmysql_handle);
  4201. exit(EXIT_FAILURE);
  4202. }
  4203. if( strlen(default_codepage) > 0 )
  4204. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  4205. Sql_ShowDebug(logmysql_handle);
  4206. }
  4207. while( 1 ) {
  4208. if(queryThreadTerminate > 0)
  4209. break;
  4210. EnterSpinLock(&queryThreadLock);
  4211. /* mess with queryThreadData within the lock */
  4212. for( i = 0; i < queryThreadData.count; i++ ) {
  4213. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4214. if( entry->ok )
  4215. continue;
  4216. else if ( !entry->st || !entry->st->stack ) {
  4217. entry->ok = true;/* dispose */
  4218. continue;
  4219. }
  4220. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  4221. entry->ok = true;/* we're done with this */
  4222. }
  4223. /* also check for any logs in need to be sent */
  4224. if( log_config.sql_logs ) {
  4225. for( i = 0; i < logThreadData.count; i++ ) {
  4226. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  4227. Sql_ShowDebug(logmysql_handle);
  4228. aFree(logThreadData.entry[i]);
  4229. }
  4230. logThreadData.count = 0;
  4231. }
  4232. LeaveSpinLock(&queryThreadLock);
  4233. ramutex_lock( queryThreadMutex );
  4234. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  4235. ramutex_unlock( queryThreadMutex );
  4236. }
  4237. Sql_Free(queryThread_handle);
  4238. if( log_config.sql_logs ) {
  4239. Sql_Free(logmysql_handle);
  4240. }
  4241. return NULL;
  4242. }
  4243. #endif
  4244. /*==========================================
  4245. * Destructor
  4246. *------------------------------------------*/
  4247. void do_final_script() {
  4248. int i;
  4249. DBIterator *iter;
  4250. struct script_state *st;
  4251. #ifdef DEBUG_HASH
  4252. if (battle_config.etc_log)
  4253. {
  4254. FILE *fp = fopen("hash_dump.txt","wt");
  4255. if(fp) {
  4256. int count[SCRIPT_HASH_SIZE];
  4257. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4258. int n=0;
  4259. int min=INT_MAX,max=0,zero=0;
  4260. double mean=0.0f;
  4261. double median=0.0f;
  4262. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4263. memset(count, 0, sizeof(count));
  4264. fprintf(fp,"num : hash : data_name\n");
  4265. fprintf(fp,"---------------------------------------------------------------\n");
  4266. for(i=LABEL_START; i<str_num; i++) {
  4267. unsigned int h = calc_hash(get_str(i));
  4268. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4269. ++count[h];
  4270. }
  4271. fprintf(fp,"--------------------\n\n");
  4272. memset(count2, 0, sizeof(count2));
  4273. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4274. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4275. if(min > count[i])
  4276. min = count[i]; // minimun count of collision
  4277. if(max < count[i])
  4278. max = count[i]; // maximun count of collision
  4279. if(count[i] == 0)
  4280. zero++;
  4281. ++count2[count[i]];
  4282. }
  4283. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4284. for( i=min; i <= max; ++i ){
  4285. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4286. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4287. }
  4288. for( i=min; i <= max; ++i ){
  4289. n += count2[i];
  4290. if( n*2 >= SCRIPT_HASH_SIZE )
  4291. {
  4292. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4293. median = (i+i+1)/2.0f;
  4294. else
  4295. median = i;
  4296. break;
  4297. }
  4298. }
  4299. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4300. fclose(fp);
  4301. }
  4302. }
  4303. #endif
  4304. mapreg_final();
  4305. db_destroy(scriptlabel_db);
  4306. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4307. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4308. ers_destroy(array_ers);
  4309. if (generic_ui_array)
  4310. aFree(generic_ui_array);
  4311. iter = db_iterator(st_db);
  4312. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4313. script_free_state(st);
  4314. dbi_destroy(iter);
  4315. if (str_data)
  4316. aFree(str_data);
  4317. if (str_buf)
  4318. aFree(str_buf);
  4319. for( i = 0; i < atcmd_binding_count; i++ ) {
  4320. aFree(atcmd_binding[i]);
  4321. }
  4322. if( atcmd_binding_count != 0 )
  4323. aFree(atcmd_binding);
  4324. ers_destroy(st_ers);
  4325. ers_destroy(stack_ers);
  4326. db_destroy(st_db);
  4327. #ifdef BETA_THREAD_TEST
  4328. /* QueryThread */
  4329. InterlockedIncrement(&queryThreadTerminate);
  4330. racond_signal(queryThreadCond);
  4331. rathread_wait(queryThread, NULL);
  4332. // Destroy cond var and mutex.
  4333. racond_destroy( queryThreadCond );
  4334. ramutex_destroy( queryThreadMutex );
  4335. /* Clear missing vars */
  4336. for( i = 0; i < queryThreadData.count; i++ ) {
  4337. aFree(queryThreadData.entry[i]);
  4338. }
  4339. aFree(queryThreadData.entry);
  4340. for( i = 0; i < logThreadData.count; i++ ) {
  4341. aFree(logThreadData.entry[i]);
  4342. }
  4343. aFree(logThreadData.entry);
  4344. #endif
  4345. }
  4346. /*==========================================
  4347. * Initialization
  4348. *------------------------------------------*/
  4349. void do_init_script(void) {
  4350. st_db = idb_alloc(DB_OPT_BASE);
  4351. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4352. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4353. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4354. st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_CACHE_OPTIONS);
  4355. stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_FLEX_CHUNK);
  4356. 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
  4357. ers_chunk_size(st_ers, 10);
  4358. ers_chunk_size(stack_ers, 10);
  4359. active_scripts = 0;
  4360. next_id = 0;
  4361. mapreg_init();
  4362. #ifdef BETA_THREAD_TEST
  4363. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4364. queryThreadData.count = 0;
  4365. CREATE(logThreadData.entry, char *, 1);
  4366. logThreadData.count = 0;
  4367. /* QueryThread Start */
  4368. InitializeSpinLock(&queryThreadLock);
  4369. queryThreadData.timer = INVALID_TIMER;
  4370. queryThreadTerminate = 0;
  4371. queryThreadMutex = ramutex_create();
  4372. queryThreadCond = racond_create();
  4373. queryThread = rathread_create(queryThread_main, NULL);
  4374. if(queryThread == NULL){
  4375. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4376. exit(EXIT_FAILURE);
  4377. }
  4378. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4379. #endif
  4380. }
  4381. void script_reload(void) {
  4382. int i;
  4383. DBIterator *iter;
  4384. struct script_state *st;
  4385. #ifdef BETA_THREAD_TEST
  4386. /* we're reloading so any queries undergoing should be...exterminated. */
  4387. EnterSpinLock(&queryThreadLock);
  4388. for( i = 0; i < queryThreadData.count; i++ ) {
  4389. aFree(queryThreadData.entry[i]);
  4390. }
  4391. queryThreadData.count = 0;
  4392. if( queryThreadData.timer != INVALID_TIMER ) {
  4393. delete_timer(queryThreadData.timer, queryThread_timer);
  4394. queryThreadData.timer = INVALID_TIMER;
  4395. }
  4396. LeaveSpinLock(&queryThreadLock);
  4397. #endif
  4398. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4399. db_clear(scriptlabel_db);
  4400. // @commands (script based)
  4401. // Clear bindings
  4402. for( i = 0; i < atcmd_binding_count; i++ ) {
  4403. aFree(atcmd_binding[i]);
  4404. }
  4405. if( atcmd_binding_count != 0 )
  4406. aFree(atcmd_binding);
  4407. atcmd_binding_count = 0;
  4408. iter = db_iterator(st_db);
  4409. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4410. script_free_state(st);
  4411. dbi_destroy(iter);
  4412. db_clear(st_db);
  4413. mapreg_reload();
  4414. }
  4415. //-----------------------------------------------------------------------------
  4416. // buildin functions
  4417. //
  4418. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args, NULL }
  4419. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args, NULL }
  4420. #define BUILDIN_DEF_DEPRECATED(x,args,deprecationdate) { buildin_ ## x , #x , args, deprecationdate }
  4421. #define BUILDIN_DEF2_DEPRECATED(x,x2,args,deprecationdate) { buildin_ ## x , x2 , args, deprecationdate }
  4422. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4423. /////////////////////////////////////////////////////////////////////
  4424. // NPC interaction
  4425. //
  4426. /// Appends a message to the npc dialog.
  4427. /// If a dialog doesn't exist yet, one is created.
  4428. ///
  4429. /// mes "<message>";
  4430. BUILDIN_FUNC(mes)
  4431. {
  4432. TBL_PC* sd;
  4433. if( !script_rid2sd(sd) )
  4434. return SCRIPT_CMD_SUCCESS;
  4435. if( !script_hasdata(st, 3) )
  4436. {// only a single line detected in the script
  4437. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4438. }
  4439. else
  4440. {// parse multiple lines as they exist
  4441. int i;
  4442. for( i = 2; script_hasdata(st, i); i++ )
  4443. {
  4444. // send the message to the client
  4445. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4446. }
  4447. }
  4448. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4449. return SCRIPT_CMD_SUCCESS;
  4450. }
  4451. /// Displays the button 'next' in the npc dialog.
  4452. /// The dialog text is cleared and the script continues when the button is pressed.
  4453. ///
  4454. /// next;
  4455. BUILDIN_FUNC(next)
  4456. {
  4457. TBL_PC* sd;
  4458. if( !script_rid2sd(sd) )
  4459. return SCRIPT_CMD_SUCCESS;
  4460. #ifdef SECURE_NPCTIMEOUT
  4461. sd->npc_idle_type = NPCT_WAIT;
  4462. #endif
  4463. st->state = STOP;
  4464. clif_scriptnext(sd, st->oid);
  4465. return SCRIPT_CMD_SUCCESS;
  4466. }
  4467. /// Ends the script and displays the button 'close' on the npc dialog.
  4468. /// The dialog is closed when the button is pressed.
  4469. ///
  4470. /// close;
  4471. BUILDIN_FUNC(close)
  4472. {
  4473. TBL_PC* sd;
  4474. if( !script_rid2sd(sd) )
  4475. return SCRIPT_CMD_SUCCESS;
  4476. if( !st->mes_active ) {
  4477. TBL_NPC* nd = map_id2nd(st->oid);
  4478. st->state = END; // Keep backwards compatibility.
  4479. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4480. } else {
  4481. st->state = CLOSE;
  4482. st->mes_active = 0;
  4483. }
  4484. clif_scriptclose(sd, st->oid);
  4485. return SCRIPT_CMD_SUCCESS;
  4486. }
  4487. /// Displays the button 'close' on the npc dialog.
  4488. /// The dialog is closed and the script continues when the button is pressed.
  4489. ///
  4490. /// close2;
  4491. BUILDIN_FUNC(close2)
  4492. {
  4493. TBL_PC* sd;
  4494. if( !script_rid2sd(sd) )
  4495. return SCRIPT_CMD_SUCCESS;
  4496. st->state = STOP;
  4497. if( st->mes_active )
  4498. st->mes_active = 0;
  4499. clif_scriptclose(sd, st->oid);
  4500. return SCRIPT_CMD_SUCCESS;
  4501. }
  4502. /// Counts the number of valid and total number of options in 'str'
  4503. /// If max_count > 0 the counting stops when that valid option is reached
  4504. /// total is incremented for each option (NULL is supported)
  4505. static int menu_countoptions(const char* str, int max_count, int* total)
  4506. {
  4507. int count = 0;
  4508. int bogus_total;
  4509. if( total == NULL )
  4510. total = &bogus_total;
  4511. ++(*total);
  4512. // initial empty options
  4513. while( *str == ':' )
  4514. {
  4515. ++str;
  4516. ++(*total);
  4517. }
  4518. // count menu options
  4519. while( *str != '\0' )
  4520. {
  4521. ++count;
  4522. --max_count;
  4523. if( max_count == 0 )
  4524. break;
  4525. while( *str != ':' && *str != '\0' )
  4526. ++str;
  4527. while( *str == ':' )
  4528. {
  4529. ++str;
  4530. ++(*total);
  4531. }
  4532. }
  4533. return count;
  4534. }
  4535. /// Displays a menu with options and goes to the target label.
  4536. /// The script is stopped if cancel is pressed.
  4537. /// Options with no text are not displayed in the client.
  4538. ///
  4539. /// Options can be grouped together, separated by the character ':' in the text:
  4540. /// ex: menu "A:B:C",L_target;
  4541. /// All these options go to the specified target label.
  4542. ///
  4543. /// The index of the selected option is put in the variable @menu.
  4544. /// Indexes start with 1 and are consistent with grouped and empty options.
  4545. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4546. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4547. ///
  4548. /// NOTE: the client closes the npc dialog when cancel is pressed
  4549. ///
  4550. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4551. BUILDIN_FUNC(menu)
  4552. {
  4553. int i;
  4554. const char* text;
  4555. TBL_PC* sd;
  4556. if( !script_rid2sd(sd) )
  4557. return SCRIPT_CMD_SUCCESS;
  4558. #ifdef SECURE_NPCTIMEOUT
  4559. sd->npc_idle_type = NPCT_MENU;
  4560. #endif
  4561. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4562. if( sd->state.menu_or_input == 0 )
  4563. {
  4564. struct StringBuf buf;
  4565. if( script_lastdata(st) % 2 == 0 )
  4566. {// argument count is not even (1st argument is at index 2)
  4567. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4568. st->state = END;
  4569. return SCRIPT_CMD_FAILURE;
  4570. }
  4571. StringBuf_Init(&buf);
  4572. sd->npc_menu = 0;
  4573. for( i = 2; i < script_lastdata(st); i += 2 )
  4574. {
  4575. struct script_data* data;
  4576. // menu options
  4577. text = script_getstr(st, i);
  4578. // target label
  4579. data = script_getdata(st, i+1);
  4580. if( !data_islabel(data) )
  4581. {// not a label
  4582. StringBuf_Destroy(&buf);
  4583. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4584. script_reportdata(data);
  4585. st->state = END;
  4586. return SCRIPT_CMD_FAILURE;
  4587. }
  4588. // append option(s)
  4589. if( text[0] == '\0' )
  4590. continue;// empty string, ignore
  4591. if( sd->npc_menu > 0 )
  4592. StringBuf_AppendStr(&buf, ":");
  4593. StringBuf_AppendStr(&buf, text);
  4594. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4595. }
  4596. st->state = RERUNLINE;
  4597. sd->state.menu_or_input = 1;
  4598. /**
  4599. * menus beyond this length crash the client (see bugreport:6402)
  4600. **/
  4601. if( StringBuf_Length(&buf) >= 2047 ) {
  4602. struct npc_data * nd = map_id2nd(st->oid);
  4603. char* menu;
  4604. CREATE(menu, char, 2048);
  4605. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4606. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4607. clif_scriptmenu(sd, st->oid, menu);
  4608. aFree(menu);
  4609. } else
  4610. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4611. StringBuf_Destroy(&buf);
  4612. if( sd->npc_menu >= 0xff )
  4613. {// 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
  4614. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4615. script_reportsrc(st);
  4616. }
  4617. }
  4618. else if( sd->npc_menu == 0xff )
  4619. {// Cancel was pressed
  4620. sd->state.menu_or_input = 0;
  4621. st->state = END;
  4622. }
  4623. else
  4624. {// goto target label
  4625. int menu = 0;
  4626. sd->state.menu_or_input = 0;
  4627. if( sd->npc_menu <= 0 )
  4628. {
  4629. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4630. st->state = END;
  4631. return SCRIPT_CMD_FAILURE;
  4632. }
  4633. // get target label
  4634. for( i = 2; i < script_lastdata(st); i += 2 )
  4635. {
  4636. text = script_getstr(st, i);
  4637. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4638. if( sd->npc_menu <= 0 )
  4639. break;// entry found
  4640. }
  4641. if( sd->npc_menu > 0 )
  4642. {// Invalid selection
  4643. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4644. st->state = END;
  4645. return SCRIPT_CMD_FAILURE;
  4646. }
  4647. if( !data_islabel(script_getdata(st, i + 1)) )
  4648. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4649. ShowError("buildin_menu: Unexpected data in label argument\n");
  4650. script_reportdata(script_getdata(st, i + 1));
  4651. st->state = END;
  4652. return SCRIPT_CMD_FAILURE;
  4653. }
  4654. pc_setreg(sd, add_str("@menu"), menu);
  4655. st->pos = script_getnum(st, i + 1);
  4656. st->state = GOTO;
  4657. }
  4658. return SCRIPT_CMD_SUCCESS;
  4659. }
  4660. /// Displays a menu with options and returns the selected option.
  4661. /// Behaves like 'menu' without the target labels.
  4662. ///
  4663. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4664. ///
  4665. /// @see menu
  4666. BUILDIN_FUNC(select)
  4667. {
  4668. int i;
  4669. const char* text;
  4670. TBL_PC* sd;
  4671. if( !script_rid2sd(sd) )
  4672. return SCRIPT_CMD_SUCCESS;
  4673. #ifdef SECURE_NPCTIMEOUT
  4674. sd->npc_idle_type = NPCT_MENU;
  4675. #endif
  4676. if( sd->state.menu_or_input == 0 ) {
  4677. struct StringBuf buf;
  4678. StringBuf_Init(&buf);
  4679. sd->npc_menu = 0;
  4680. for( i = 2; i <= script_lastdata(st); ++i ) {
  4681. text = script_getstr(st, i);
  4682. if( sd->npc_menu > 0 )
  4683. StringBuf_AppendStr(&buf, ":");
  4684. StringBuf_AppendStr(&buf, text);
  4685. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4686. }
  4687. st->state = RERUNLINE;
  4688. sd->state.menu_or_input = 1;
  4689. /**
  4690. * menus beyond this length crash the client (see bugreport:6402)
  4691. **/
  4692. if( StringBuf_Length(&buf) >= 2047 ) {
  4693. struct npc_data * nd = map_id2nd(st->oid);
  4694. char* menu;
  4695. CREATE(menu, char, 2048);
  4696. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4697. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4698. clif_scriptmenu(sd, st->oid, menu);
  4699. aFree(menu);
  4700. } else
  4701. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4702. StringBuf_Destroy(&buf);
  4703. if( sd->npc_menu >= 0xff ) {
  4704. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4705. script_reportsrc(st);
  4706. }
  4707. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4708. sd->state.menu_or_input = 0;
  4709. st->state = END;
  4710. } else {// return selected option
  4711. int menu = 0;
  4712. sd->state.menu_or_input = 0;
  4713. for( i = 2; i <= script_lastdata(st); ++i ) {
  4714. text = script_getstr(st, i);
  4715. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4716. if( sd->npc_menu <= 0 )
  4717. break;// entry found
  4718. }
  4719. pc_setreg(sd, add_str("@menu"), menu);
  4720. script_pushint(st, menu);
  4721. st->state = RUN;
  4722. }
  4723. return SCRIPT_CMD_SUCCESS;
  4724. }
  4725. /// Displays a menu with options and returns the selected option.
  4726. /// Behaves like 'menu' without the target labels, except when cancel is
  4727. /// pressed.
  4728. /// When cancel is pressed, the script continues and 255 is returned.
  4729. ///
  4730. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4731. ///
  4732. /// @see menu
  4733. BUILDIN_FUNC(prompt)
  4734. {
  4735. int i;
  4736. const char *text;
  4737. TBL_PC* sd;
  4738. if( !script_rid2sd(sd) )
  4739. return SCRIPT_CMD_SUCCESS;
  4740. #ifdef SECURE_NPCTIMEOUT
  4741. sd->npc_idle_type = NPCT_MENU;
  4742. #endif
  4743. if( sd->state.menu_or_input == 0 )
  4744. {
  4745. struct StringBuf buf;
  4746. StringBuf_Init(&buf);
  4747. sd->npc_menu = 0;
  4748. for( i = 2; i <= script_lastdata(st); ++i )
  4749. {
  4750. text = script_getstr(st, i);
  4751. if( sd->npc_menu > 0 )
  4752. StringBuf_AppendStr(&buf, ":");
  4753. StringBuf_AppendStr(&buf, text);
  4754. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4755. }
  4756. st->state = RERUNLINE;
  4757. sd->state.menu_or_input = 1;
  4758. /**
  4759. * menus beyond this length crash the client (see bugreport:6402)
  4760. **/
  4761. if( StringBuf_Length(&buf) >= 2047 ) {
  4762. struct npc_data * nd = map_id2nd(st->oid);
  4763. char* menu;
  4764. CREATE(menu, char, 2048);
  4765. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4766. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4767. clif_scriptmenu(sd, st->oid, menu);
  4768. aFree(menu);
  4769. } else
  4770. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4771. StringBuf_Destroy(&buf);
  4772. if( sd->npc_menu >= 0xff )
  4773. {
  4774. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4775. script_reportsrc(st);
  4776. }
  4777. }
  4778. else if( sd->npc_menu == 0xff )
  4779. {// Cancel was pressed
  4780. sd->state.menu_or_input = 0;
  4781. pc_setreg(sd, add_str("@menu"), 0xff);
  4782. script_pushint(st, 0xff);
  4783. st->state = RUN;
  4784. }
  4785. else
  4786. {// return selected option
  4787. int menu = 0;
  4788. sd->state.menu_or_input = 0;
  4789. for( i = 2; i <= script_lastdata(st); ++i )
  4790. {
  4791. text = script_getstr(st, i);
  4792. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4793. if( sd->npc_menu <= 0 )
  4794. break;// entry found
  4795. }
  4796. pc_setreg(sd, add_str("@menu"), menu);
  4797. script_pushint(st, menu);
  4798. st->state = RUN;
  4799. }
  4800. return SCRIPT_CMD_SUCCESS;
  4801. }
  4802. /////////////////////////////////////////////////////////////////////
  4803. // ...
  4804. //
  4805. /// Jumps to the target script label.
  4806. ///
  4807. /// goto <label>;
  4808. BUILDIN_FUNC(goto)
  4809. {
  4810. if( !data_islabel(script_getdata(st,2)) )
  4811. {
  4812. ShowError("buildin_goto: Not a label\n");
  4813. script_reportdata(script_getdata(st,2));
  4814. st->state = END;
  4815. return SCRIPT_CMD_FAILURE;
  4816. }
  4817. st->pos = script_getnum(st,2);
  4818. st->state = GOTO;
  4819. return SCRIPT_CMD_SUCCESS;
  4820. }
  4821. /*==========================================
  4822. * user-defined function call
  4823. *------------------------------------------*/
  4824. BUILDIN_FUNC(callfunc)
  4825. {
  4826. int i, j;
  4827. struct script_retinfo* ri;
  4828. struct script_code* scr;
  4829. const char* str = script_getstr(st,2);
  4830. struct reg_db *ref = NULL;
  4831. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4832. if(!scr) {
  4833. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4834. st->state = END;
  4835. return SCRIPT_CMD_FAILURE;
  4836. }
  4837. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4838. ref[0].vars = st->stack->scope.vars;
  4839. if (!st->stack->scope.arrays)
  4840. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4841. ref[0].arrays = st->stack->scope.arrays;
  4842. ref[1].vars = st->script->local.vars;
  4843. if (!st->script->local.arrays)
  4844. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4845. ref[1].arrays = st->script->local.arrays;
  4846. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4847. struct script_data* data = push_copy(st->stack,i);
  4848. if (data_isreference(data) && !data->ref) {
  4849. const char* name = reference_getname(data);
  4850. if (name[0] == '.')
  4851. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4852. }
  4853. }
  4854. CREATE(ri, struct script_retinfo, 1);
  4855. ri->script = st->script; // script code
  4856. ri->scope.vars = st->stack->scope.vars; // scope variables
  4857. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4858. ri->pos = st->pos; // script location
  4859. ri->nargs = j; // argument count
  4860. ri->defsp = st->stack->defsp; // default stack pointer
  4861. push_retinfo(st->stack, ri, ref);
  4862. st->pos = 0;
  4863. st->script = scr;
  4864. st->stack->defsp = st->stack->sp;
  4865. st->state = GOTO;
  4866. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4867. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4868. if (!st->script->local.vars)
  4869. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4870. return SCRIPT_CMD_SUCCESS;
  4871. }
  4872. /*==========================================
  4873. * subroutine call
  4874. *------------------------------------------*/
  4875. BUILDIN_FUNC(callsub)
  4876. {
  4877. int i,j;
  4878. struct script_retinfo* ri;
  4879. int pos = script_getnum(st,2);
  4880. struct reg_db *ref = NULL;
  4881. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4882. ShowError("buildin_callsub: Argument is not a label\n");
  4883. script_reportdata(script_getdata(st,2));
  4884. st->state = END;
  4885. return SCRIPT_CMD_FAILURE;
  4886. }
  4887. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4888. ref[0].vars = st->stack->scope.vars;
  4889. if (!st->stack->scope.arrays)
  4890. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4891. ref[0].arrays = st->stack->scope.arrays;
  4892. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4893. struct script_data* data = push_copy(st->stack,i);
  4894. if (data_isreference(data) && !data->ref) {
  4895. const char* name = reference_getname(data);
  4896. if (name[0] == '.' && name[1] == '@')
  4897. data->ref = &ref[0];
  4898. }
  4899. }
  4900. CREATE(ri, struct script_retinfo, 1);
  4901. ri->script = st->script; // script code
  4902. ri->scope.vars = st->stack->scope.vars; // scope variables
  4903. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4904. ri->pos = st->pos; // script location
  4905. ri->nargs = j; // argument count
  4906. ri->defsp = st->stack->defsp; // default stack pointer
  4907. push_retinfo(st->stack, ri, ref);
  4908. st->pos = pos;
  4909. st->stack->defsp = st->stack->sp;
  4910. st->state = GOTO;
  4911. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4912. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4913. return SCRIPT_CMD_SUCCESS;
  4914. }
  4915. /// Retrieves an argument provided to callfunc/callsub.
  4916. /// If the argument doesn't exist
  4917. ///
  4918. /// getarg(<index>{,<default_value>}) -> <value>
  4919. BUILDIN_FUNC(getarg)
  4920. {
  4921. struct script_retinfo* ri;
  4922. int idx;
  4923. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4924. {
  4925. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4926. st->state = END;
  4927. return SCRIPT_CMD_FAILURE;
  4928. }
  4929. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4930. idx = script_getnum(st,2);
  4931. if( idx >= 0 && idx < ri->nargs )
  4932. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4933. else if( script_hasdata(st,3) )
  4934. script_pushcopy(st, 3);
  4935. else
  4936. {
  4937. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4938. st->state = END;
  4939. return SCRIPT_CMD_FAILURE;
  4940. }
  4941. return SCRIPT_CMD_SUCCESS;
  4942. }
  4943. /// Returns from the current function, optionaly returning a value from the functions.
  4944. /// Don't use outside script functions.
  4945. ///
  4946. /// return;
  4947. /// return <value>;
  4948. BUILDIN_FUNC(return)
  4949. {
  4950. if( script_hasdata(st,2) )
  4951. {// return value
  4952. struct script_data* data;
  4953. script_pushcopy(st, 2);
  4954. data = script_getdatatop(st, -1);
  4955. if( data_isreference(data) ) {
  4956. const char* name = reference_getname(data);
  4957. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4958. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4959. get_val(st, data); // current scope, convert to value
  4960. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4961. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4962. } else if( name[0] == '.' && !data->ref ) { // script variable, link to current script
  4963. data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4964. data->ref->vars = st->script->local.vars;
  4965. if (!st->script->local.arrays)
  4966. st->script->local.arrays = idb_alloc(DB_OPT_BASE);
  4967. data->ref->arrays = st->script->local.arrays;
  4968. }
  4969. }
  4970. }
  4971. else
  4972. {// no return value
  4973. script_pushnil(st);
  4974. }
  4975. st->state = RETFUNC;
  4976. return SCRIPT_CMD_SUCCESS;
  4977. }
  4978. /// Returns a random number from 0 to <range>-1.
  4979. /// Or returns a random number from <min> to <max>.
  4980. /// If <min> is greater than <max>, their numbers are switched.
  4981. /// rand(<range>) -> <int>
  4982. /// rand(<min>,<max>) -> <int>
  4983. BUILDIN_FUNC(rand)
  4984. {
  4985. int range;
  4986. int min;
  4987. if( script_hasdata(st,3) )
  4988. {// min,max
  4989. int max = script_getnum(st,3);
  4990. min = script_getnum(st,2);
  4991. if( max < min )
  4992. swap(min, max);
  4993. range = max - min + 1;
  4994. }
  4995. else
  4996. {// range
  4997. min = 0;
  4998. range = script_getnum(st,2);
  4999. }
  5000. if( range <= 1 )
  5001. script_pushint(st, min);
  5002. else
  5003. script_pushint(st, rnd()%range + min);
  5004. return SCRIPT_CMD_SUCCESS;
  5005. }
  5006. /*==========================================
  5007. * Warp sd to str,x,y or Random or SavePoint/Save
  5008. *------------------------------------------*/
  5009. BUILDIN_FUNC(warp)
  5010. {
  5011. int ret;
  5012. int x,y;
  5013. const char* str;
  5014. struct map_session_data* sd;
  5015. if(!script_charid2sd(5, sd))
  5016. return SCRIPT_CMD_SUCCESS;
  5017. str = script_getstr(st,2);
  5018. x = script_getnum(st,3);
  5019. y = script_getnum(st,4);
  5020. if(strcmp(str,"Random")==0)
  5021. ret = pc_randomwarp(sd,CLR_TELEPORT);
  5022. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  5023. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5024. else
  5025. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  5026. if( ret ) {
  5027. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  5028. return SCRIPT_CMD_FAILURE;
  5029. }
  5030. return SCRIPT_CMD_SUCCESS;
  5031. }
  5032. /*==========================================
  5033. * Warp a specified area
  5034. *------------------------------------------*/
  5035. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  5036. {
  5037. int x2,y2,x3,y3;
  5038. unsigned int index;
  5039. index = va_arg(ap,unsigned int);
  5040. x2 = va_arg(ap,int);
  5041. y2 = va_arg(ap,int);
  5042. x3 = va_arg(ap,int);
  5043. y3 = va_arg(ap,int);
  5044. if(index == 0)
  5045. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  5046. else if(x3 && y3) {
  5047. int max, tx, ty, j = 0;
  5048. int16 m;
  5049. m = map_mapindex2mapid(index);
  5050. // choose a suitable max number of attempts
  5051. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  5052. max = 1000;
  5053. // find a suitable map cell
  5054. do {
  5055. tx = rnd()%(x3-x2+1)+x2;
  5056. ty = rnd()%(y3-y2+1)+y2;
  5057. j++;
  5058. } while( map_getcell(m,tx,ty,CELL_CHKNOPASS) && j < max );
  5059. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  5060. }
  5061. else
  5062. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  5063. return 0;
  5064. }
  5065. BUILDIN_FUNC(areawarp)
  5066. {
  5067. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  5068. unsigned int index;
  5069. const char *str;
  5070. const char *mapname;
  5071. mapname = script_getstr(st,2);
  5072. x0 = script_getnum(st,3);
  5073. y0 = script_getnum(st,4);
  5074. x1 = script_getnum(st,5);
  5075. y1 = script_getnum(st,6);
  5076. str = script_getstr(st,7);
  5077. x2 = script_getnum(st,8);
  5078. y2 = script_getnum(st,9);
  5079. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  5080. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  5081. x3 = 0;
  5082. y3 = 0;
  5083. } else if( x3 && y3 ) {
  5084. // normalize x3/y3 coordinates
  5085. if( x3 < x2 ) swap(x3,x2);
  5086. if( y3 < y2 ) swap(y3,y2);
  5087. }
  5088. }
  5089. if( (m = map_mapname2mapid(mapname)) < 0 )
  5090. return SCRIPT_CMD_FAILURE;
  5091. if( strcmp(str,"Random") == 0 )
  5092. index = 0;
  5093. else if( !(index=mapindex_name2id(str)) )
  5094. return SCRIPT_CMD_FAILURE;
  5095. map_foreachinallarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  5096. return SCRIPT_CMD_SUCCESS;
  5097. }
  5098. /*==========================================
  5099. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  5100. *------------------------------------------*/
  5101. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  5102. {
  5103. int hp, sp;
  5104. hp = va_arg(ap, int);
  5105. sp = va_arg(ap, int);
  5106. pc_percentheal((TBL_PC *)bl,hp,sp);
  5107. return 0;
  5108. }
  5109. BUILDIN_FUNC(areapercentheal)
  5110. {
  5111. int hp,sp,m;
  5112. const char *mapname;
  5113. int x0,y0,x1,y1;
  5114. mapname=script_getstr(st,2);
  5115. x0=script_getnum(st,3);
  5116. y0=script_getnum(st,4);
  5117. x1=script_getnum(st,5);
  5118. y1=script_getnum(st,6);
  5119. hp=script_getnum(st,7);
  5120. sp=script_getnum(st,8);
  5121. if( (m=map_mapname2mapid(mapname))< 0)
  5122. return SCRIPT_CMD_FAILURE;
  5123. map_foreachinallarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  5124. return SCRIPT_CMD_SUCCESS;
  5125. }
  5126. /*==========================================
  5127. * Warpparty - [Fredzilla] [Paradox924X]
  5128. * Syntax: warpparty "to_mapname",x,y,Party_ID,{<"from_mapname">,<range x>,<range y>};
  5129. * If 'from_mapname' is specified, only the party members on that map will be warped
  5130. *------------------------------------------*/
  5131. BUILDIN_FUNC(warpparty)
  5132. {
  5133. TBL_PC *sd = NULL;
  5134. TBL_PC *pl_sd;
  5135. struct party_data* p;
  5136. int type, mapindex = 0, m = -1, i, rx = 0, ry = 0;
  5137. const char* str = script_getstr(st,2);
  5138. int x = script_getnum(st,3);
  5139. int y = script_getnum(st,4);
  5140. int p_id = script_getnum(st,5);
  5141. const char* str2 = NULL;
  5142. if ( script_hasdata(st,6) )
  5143. str2 = script_getstr(st,6);
  5144. if (script_hasdata(st, 7))
  5145. rx = script_getnum(st, 7);
  5146. if (script_hasdata(st, 8))
  5147. ry = script_getnum(st, 8);
  5148. p = party_search(p_id);
  5149. if(!p)
  5150. return SCRIPT_CMD_SUCCESS;
  5151. type = ( strcmp(str,"Random")==0 ) ? 0
  5152. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5153. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5154. : ( strcmp(str,"Leader")==0 ) ? 3
  5155. : 4;
  5156. switch (type)
  5157. {
  5158. case 3:
  5159. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5160. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5161. return SCRIPT_CMD_FAILURE;
  5162. pl_sd = p->data[i].sd;
  5163. mapindex = pl_sd->mapindex;
  5164. m = map_mapindex2mapid(mapindex);
  5165. x = pl_sd->bl.x;
  5166. y = pl_sd->bl.y;
  5167. break;
  5168. case 4:
  5169. mapindex = mapindex_name2id(str);
  5170. if (!mapindex) {// Invalid map
  5171. return SCRIPT_CMD_FAILURE;
  5172. }
  5173. m = map_mapindex2mapid(mapindex);
  5174. break;
  5175. case 2:
  5176. //"SavePoint" uses save point of the currently attached player
  5177. if ( !script_rid2sd(sd) )
  5178. return SCRIPT_CMD_SUCCESS;
  5179. break;
  5180. }
  5181. for (i = 0; i < MAX_PARTY; i++)
  5182. {
  5183. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5184. continue;
  5185. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  5186. continue;
  5187. if( pc_isdead(pl_sd) )
  5188. continue;
  5189. switch( type )
  5190. {
  5191. case 0: // Random
  5192. if(!map[pl_sd->bl.m].flag.nowarp)
  5193. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5194. break;
  5195. case 1: // SavePointAll
  5196. if(!map[pl_sd->bl.m].flag.noreturn)
  5197. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5198. break;
  5199. case 2: // SavePoint
  5200. if(!map[pl_sd->bl.m].flag.noreturn)
  5201. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5202. break;
  5203. case 3: // Leader
  5204. if (p->party.member[i].leader)
  5205. continue;
  5206. case 4: // m,x,y
  5207. if (rx || ry) {
  5208. int x1 = x + rx, y1 = y + ry,
  5209. x0 = x - rx, y0 = y - ry;
  5210. uint8 attempts = 10;
  5211. do {
  5212. x = x0 + rnd()%(x1 - x0 + 1);
  5213. y = y0 + rnd()%(y1 - y0 + 1);
  5214. } while ((--attempts) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  5215. }
  5216. 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))
  5217. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5218. break;
  5219. }
  5220. }
  5221. return SCRIPT_CMD_SUCCESS;
  5222. }
  5223. /*==========================================
  5224. * Warpguild - [Fredzilla]
  5225. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5226. *------------------------------------------*/
  5227. BUILDIN_FUNC(warpguild)
  5228. {
  5229. TBL_PC *sd = NULL;
  5230. TBL_PC *pl_sd;
  5231. struct guild* g;
  5232. struct s_mapiterator* iter;
  5233. int type, mapindex = 0, m = -1;
  5234. const char* str = script_getstr(st,2);
  5235. int x = script_getnum(st,3);
  5236. int y = script_getnum(st,4);
  5237. int gid = script_getnum(st,5);
  5238. g = guild_search(gid);
  5239. if( g == NULL )
  5240. return SCRIPT_CMD_SUCCESS;
  5241. type = ( strcmp(str,"Random")==0 ) ? 0
  5242. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5243. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5244. : 3;
  5245. if( type == 2 && !script_rid2sd(sd) )
  5246. {// "SavePoint" uses save point of the currently attached player
  5247. return SCRIPT_CMD_SUCCESS;
  5248. }
  5249. switch (type) {
  5250. case 3:
  5251. mapindex = mapindex_name2id(str);
  5252. if (!mapindex)
  5253. return SCRIPT_CMD_FAILURE;
  5254. m = map_mapindex2mapid(mapindex);
  5255. break;
  5256. }
  5257. iter = mapit_getallusers();
  5258. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5259. {
  5260. if( pl_sd->status.guild_id != gid )
  5261. continue;
  5262. switch( type )
  5263. {
  5264. case 0: // Random
  5265. if(!map[pl_sd->bl.m].flag.nowarp)
  5266. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5267. break;
  5268. case 1: // SavePointAll
  5269. if(!map[pl_sd->bl.m].flag.noreturn)
  5270. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5271. break;
  5272. case 2: // SavePoint
  5273. if(!map[pl_sd->bl.m].flag.noreturn)
  5274. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5275. break;
  5276. case 3: // m,x,y
  5277. 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))
  5278. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5279. break;
  5280. }
  5281. }
  5282. mapit_free(iter);
  5283. return SCRIPT_CMD_SUCCESS;
  5284. }
  5285. /*==========================================
  5286. * Force Heal a player (hp and sp)
  5287. *------------------------------------------*/
  5288. BUILDIN_FUNC(heal)
  5289. {
  5290. TBL_PC *sd;
  5291. int hp,sp;
  5292. if (!script_charid2sd(4,sd))
  5293. return SCRIPT_CMD_SUCCESS;
  5294. hp=script_getnum(st,2);
  5295. sp=script_getnum(st,3);
  5296. status_heal(&sd->bl, hp, sp, 1);
  5297. return SCRIPT_CMD_SUCCESS;
  5298. }
  5299. /*==========================================
  5300. * Heal a player by item (get vit bonus etc)
  5301. *------------------------------------------*/
  5302. BUILDIN_FUNC(itemheal)
  5303. {
  5304. TBL_PC *sd;
  5305. int hp,sp;
  5306. hp=script_getnum(st,2);
  5307. sp=script_getnum(st,3);
  5308. if(potion_flag==1) {
  5309. potion_hp = hp;
  5310. potion_sp = sp;
  5311. return SCRIPT_CMD_SUCCESS;
  5312. }
  5313. if (!script_charid2sd(4,sd))
  5314. return SCRIPT_CMD_SUCCESS;
  5315. pc_itemheal(sd,sd->itemid,hp,sp);
  5316. return SCRIPT_CMD_SUCCESS;
  5317. }
  5318. /*==========================================
  5319. *
  5320. *------------------------------------------*/
  5321. BUILDIN_FUNC(percentheal)
  5322. {
  5323. int hp,sp;
  5324. TBL_PC* sd;
  5325. hp=script_getnum(st,2);
  5326. sp=script_getnum(st,3);
  5327. if(potion_flag==1) {
  5328. potion_per_hp = hp;
  5329. potion_per_sp = sp;
  5330. return SCRIPT_CMD_SUCCESS;
  5331. }
  5332. if (!script_charid2sd(4,sd))
  5333. return SCRIPT_CMD_SUCCESS;
  5334. #ifdef RENEWAL
  5335. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5336. sp = 0;
  5337. #endif
  5338. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5339. hp = 0;
  5340. sp = 0;
  5341. }
  5342. if (sd->sc.data[SC_BITESCAR])
  5343. hp = 0;
  5344. pc_percentheal(sd,hp,sp);
  5345. return SCRIPT_CMD_SUCCESS;
  5346. }
  5347. /*==========================================
  5348. *
  5349. *------------------------------------------*/
  5350. BUILDIN_FUNC(jobchange)
  5351. {
  5352. int job, upper=-1;
  5353. job=script_getnum(st,2);
  5354. if( script_hasdata(st,3) )
  5355. upper=script_getnum(st,3);
  5356. if (pcdb_checkid(job))
  5357. {
  5358. TBL_PC* sd;
  5359. if (!script_charid2sd(4,sd))
  5360. return SCRIPT_CMD_SUCCESS;
  5361. pc_jobchange(sd, job, upper);
  5362. }
  5363. return SCRIPT_CMD_SUCCESS;
  5364. }
  5365. /*==========================================
  5366. *
  5367. *------------------------------------------*/
  5368. BUILDIN_FUNC(jobname)
  5369. {
  5370. int class_=script_getnum(st,2);
  5371. script_pushconststr(st, (char*)job_name(class_));
  5372. return SCRIPT_CMD_SUCCESS;
  5373. }
  5374. /// Get input from the player.
  5375. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5376. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5377. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5378. /// shorter than 'min' and 0 otherwise.
  5379. ///
  5380. /// input(<var>{,<min>{,<max>}}) -> <int>
  5381. BUILDIN_FUNC(input)
  5382. {
  5383. TBL_PC* sd;
  5384. struct script_data* data;
  5385. int64 uid;
  5386. const char* name;
  5387. int min;
  5388. int max;
  5389. if( !script_rid2sd(sd) )
  5390. return SCRIPT_CMD_SUCCESS;
  5391. data = script_getdata(st,2);
  5392. if( !data_isreference(data) ){
  5393. ShowError("script:input: not a variable\n");
  5394. script_reportdata(data);
  5395. st->state = END;
  5396. return SCRIPT_CMD_FAILURE;
  5397. }
  5398. uid = reference_getuid(data);
  5399. name = reference_getname(data);
  5400. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5401. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5402. #ifdef SECURE_NPCTIMEOUT
  5403. sd->npc_idle_type = NPCT_WAIT;
  5404. #endif
  5405. if( !sd->state.menu_or_input )
  5406. { // first invocation, display npc input box
  5407. sd->state.menu_or_input = 1;
  5408. st->state = RERUNLINE;
  5409. if( is_string_variable(name) )
  5410. clif_scriptinputstr(sd,st->oid);
  5411. else
  5412. clif_scriptinput(sd,st->oid);
  5413. }
  5414. else
  5415. { // take received text/value and store it in the designated variable
  5416. sd->state.menu_or_input = 0;
  5417. if( is_string_variable(name) )
  5418. {
  5419. int len = (int)strlen(sd->npc_str);
  5420. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5421. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5422. }
  5423. else
  5424. {
  5425. int amount = sd->npc_amount;
  5426. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5427. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5428. }
  5429. st->state = RUN;
  5430. }
  5431. return SCRIPT_CMD_SUCCESS;
  5432. }
  5433. // declare the copyarray method here for future reference
  5434. BUILDIN_FUNC(copyarray);
  5435. /// Sets the value of a variable.
  5436. /// The value is converted to the type of the variable.
  5437. ///
  5438. /// set(<variable>,<value>{,<charid>})
  5439. BUILDIN_FUNC(setr)
  5440. {
  5441. TBL_PC* sd = NULL;
  5442. struct script_data* data;
  5443. //struct script_data* datavalue;
  5444. int64 num;
  5445. uint8 pos = 4;
  5446. const char* name, *command = script_getfuncname(st);
  5447. char prefix;
  5448. data = script_getdata(st,2);
  5449. //datavalue = script_getdata(st,3);
  5450. if( !data_isreference(data) )
  5451. {
  5452. ShowError("script:set: not a variable\n");
  5453. script_reportdata(script_getdata(st,2));
  5454. st->state = END;
  5455. return SCRIPT_CMD_FAILURE;
  5456. }
  5457. num = reference_getuid(data);
  5458. name = reference_getname(data);
  5459. prefix = *name;
  5460. if (!strcmp(command, "setr"))
  5461. pos = 5;
  5462. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5463. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5464. return SCRIPT_CMD_FAILURE;
  5465. }
  5466. #if 0
  5467. if( data_isreference(datavalue) )
  5468. {// the value being referenced is a variable
  5469. const char* namevalue = reference_getname(datavalue);
  5470. if( !not_array_variable(*namevalue) )
  5471. {// array variable being copied into another array variable
  5472. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5473. {// player must be attached in order to copy a player variable
  5474. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5475. return SCRIPT_CMD_SUCCESS;
  5476. }
  5477. if( is_string_variable(namevalue) != is_string_variable(name) )
  5478. {// non-matching array value types
  5479. ShowWarning("script:set: two array variables do not match in type.\n");
  5480. return SCRIPT_CMD_SUCCESS;
  5481. }
  5482. // push the maximum number of array values to the stack
  5483. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5484. // call the copy array method directly
  5485. return buildin_copyarray(st);
  5486. }
  5487. }
  5488. #endif
  5489. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5490. if( is_string_variable(name) )
  5491. script_pushstrcopy(st,script_getstr(st, 4));
  5492. else
  5493. script_pushint(st,script_getnum(st, 4));
  5494. } else // Return a copy of the variable reference
  5495. script_pushcopy(st, 2);
  5496. if( is_string_variable(name) )
  5497. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5498. else
  5499. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5500. return SCRIPT_CMD_SUCCESS;
  5501. }
  5502. /////////////////////////////////////////////////////////////////////
  5503. /// Array variables
  5504. ///
  5505. /// Sets values of an array, from the starting index.
  5506. /// ex: setarray arr[1],1,2,3;
  5507. ///
  5508. /// setarray <array variable>,<value1>{,<value2>...};
  5509. BUILDIN_FUNC(setarray)
  5510. {
  5511. struct script_data* data;
  5512. const char* name;
  5513. uint32 start;
  5514. uint32 end;
  5515. int32 id;
  5516. int32 i;
  5517. TBL_PC* sd = NULL;
  5518. data = script_getdata(st, 2);
  5519. if( !data_isreference(data) )
  5520. {
  5521. ShowError("script:setarray: not a variable\n");
  5522. script_reportdata(data);
  5523. st->state = END;
  5524. return SCRIPT_CMD_FAILURE;// not a variable
  5525. }
  5526. id = reference_getid(data);
  5527. start = reference_getindex(data);
  5528. name = reference_getname(data);
  5529. if( not_server_variable(*name) )
  5530. {
  5531. if( !script_rid2sd(sd) )
  5532. return SCRIPT_CMD_SUCCESS;// no player attached
  5533. }
  5534. end = start + script_lastdata(st) - 2;
  5535. if( end > SCRIPT_MAX_ARRAYSIZE )
  5536. end = SCRIPT_MAX_ARRAYSIZE;
  5537. if( is_string_variable(name) )
  5538. {// string array
  5539. for( i = 3; start < end; ++start, ++i )
  5540. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5541. }
  5542. else
  5543. {// int array
  5544. for( i = 3; start < end; ++start, ++i )
  5545. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5546. }
  5547. return SCRIPT_CMD_SUCCESS;
  5548. }
  5549. /// Sets count values of an array, from the starting index.
  5550. /// ex: cleararray arr[0],0,1;
  5551. ///
  5552. /// cleararray <array variable>,<value>,<count>;
  5553. BUILDIN_FUNC(cleararray)
  5554. {
  5555. struct script_data* data;
  5556. const char* name;
  5557. uint32 start;
  5558. uint32 end;
  5559. int32 id;
  5560. void* v;
  5561. TBL_PC* sd = NULL;
  5562. data = script_getdata(st, 2);
  5563. if( !data_isreference(data) )
  5564. {
  5565. ShowError("script:cleararray: not a variable\n");
  5566. script_reportdata(data);
  5567. st->state = END;
  5568. return SCRIPT_CMD_FAILURE;// not a variable
  5569. }
  5570. id = reference_getid(data);
  5571. start = reference_getindex(data);
  5572. name = reference_getname(data);
  5573. if( not_server_variable(*name) )
  5574. {
  5575. if( !script_rid2sd(sd) )
  5576. return SCRIPT_CMD_SUCCESS;// no player attached
  5577. }
  5578. if( is_string_variable(name) )
  5579. v = (void*)script_getstr(st, 3);
  5580. else
  5581. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5582. end = start + script_getnum(st, 4);
  5583. if( end > SCRIPT_MAX_ARRAYSIZE )
  5584. end = SCRIPT_MAX_ARRAYSIZE;
  5585. for( ; start < end; ++start )
  5586. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5587. return SCRIPT_CMD_SUCCESS;
  5588. }
  5589. /// Copies data from one array to another.
  5590. /// ex: copyarray arr[0],arr[2],2;
  5591. ///
  5592. /// copyarray <destination array variable>,<source array variable>,<count>;
  5593. BUILDIN_FUNC(copyarray)
  5594. {
  5595. struct script_data* data1;
  5596. struct script_data* data2;
  5597. const char* name1;
  5598. const char* name2;
  5599. int32 idx1;
  5600. int32 idx2;
  5601. int32 id1;
  5602. int32 id2;
  5603. void* v;
  5604. int32 i;
  5605. uint32 count;
  5606. TBL_PC* sd = NULL;
  5607. data1 = script_getdata(st, 2);
  5608. data2 = script_getdata(st, 3);
  5609. if( !data_isreference(data1) || !data_isreference(data2) )
  5610. {
  5611. ShowError("script:copyarray: not a variable\n");
  5612. script_reportdata(data1);
  5613. script_reportdata(data2);
  5614. st->state = END;
  5615. return SCRIPT_CMD_FAILURE;// not a variable
  5616. }
  5617. id1 = reference_getid(data1);
  5618. id2 = reference_getid(data2);
  5619. idx1 = reference_getindex(data1);
  5620. idx2 = reference_getindex(data2);
  5621. name1 = reference_getname(data1);
  5622. name2 = reference_getname(data2);
  5623. if( is_string_variable(name1) != is_string_variable(name2) )
  5624. {
  5625. ShowError("script:copyarray: type mismatch\n");
  5626. script_reportdata(data1);
  5627. script_reportdata(data2);
  5628. st->state = END;
  5629. return SCRIPT_CMD_FAILURE;// data type mismatch
  5630. }
  5631. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5632. {
  5633. if( !script_rid2sd(sd) )
  5634. return SCRIPT_CMD_SUCCESS;// no player attached
  5635. }
  5636. count = script_getnum(st, 4);
  5637. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5638. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5639. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5640. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5641. if( id1 == id2 && idx1 > idx2 )
  5642. {// destination might be overlapping the source - copy in reverse order
  5643. for( i = count - 1; i >= 0; --i )
  5644. {
  5645. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5646. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5647. script_removetop(st, -1, 0);
  5648. }
  5649. }
  5650. else
  5651. {// normal copy
  5652. for( i = 0; i < count; ++i )
  5653. {
  5654. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5655. {
  5656. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5657. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5658. script_removetop(st, -1, 0);
  5659. }
  5660. else// out of range - assume ""/0
  5661. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5662. }
  5663. }
  5664. return SCRIPT_CMD_SUCCESS;
  5665. }
  5666. /// Returns the size of the array.
  5667. /// Assumes that everything before the starting index exists.
  5668. /// ex: getarraysize(arr[3])
  5669. ///
  5670. /// getarraysize(<array variable>) -> <int>
  5671. BUILDIN_FUNC(getarraysize)
  5672. {
  5673. struct script_data* data;
  5674. const char* name;
  5675. struct map_session_data* sd = NULL;
  5676. data = script_getdata(st, 2);
  5677. if( !data_isreference(data) )
  5678. {
  5679. ShowError("script:getarraysize: not a variable\n");
  5680. script_reportdata(data);
  5681. script_pushnil(st);
  5682. st->state = END;
  5683. return SCRIPT_CMD_FAILURE;// not a variable
  5684. }
  5685. name = reference_getname(data);
  5686. if( not_server_variable(*name) ){
  5687. if (!script_rid2sd(sd))
  5688. return SCRIPT_CMD_SUCCESS;// no player attached
  5689. }
  5690. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5691. return SCRIPT_CMD_SUCCESS;
  5692. }
  5693. int script_array_index_cmp(const void *a, const void *b) {
  5694. return ( *(unsigned int*)a - *(unsigned int*)b );
  5695. }
  5696. /// Deletes count or all the elements in an array, from the starting index.
  5697. /// ex: deletearray arr[4],2;
  5698. ///
  5699. /// deletearray <array variable>;
  5700. /// deletearray <array variable>,<count>;
  5701. BUILDIN_FUNC(deletearray)
  5702. {
  5703. struct script_data* data;
  5704. const char* name;
  5705. unsigned int start, end, i;
  5706. int id;
  5707. TBL_PC *sd = NULL;
  5708. struct script_array *sa = NULL;
  5709. struct reg_db *src = NULL;
  5710. void *value;
  5711. data = script_getdata(st, 2);
  5712. if( !data_isreference(data) ) {
  5713. ShowError("script:deletearray: not a variable\n");
  5714. script_reportdata(data);
  5715. st->state = END;
  5716. return SCRIPT_CMD_FAILURE;// not a variable
  5717. }
  5718. id = reference_getid(data);
  5719. start = reference_getindex(data);
  5720. name = reference_getname(data);
  5721. if( not_server_variable(*name) ) {
  5722. if( !script_rid2sd(sd) )
  5723. return SCRIPT_CMD_SUCCESS;// no player attached
  5724. }
  5725. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5726. ShowError("script:deletearray: not a array\n");
  5727. script_reportdata(data);
  5728. st->state = END;
  5729. return SCRIPT_CMD_FAILURE;// not a variable
  5730. }
  5731. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5732. if ( !(sa = idb_get(src->arrays, id)) ) { // non-existent array, nothing to empty
  5733. return SCRIPT_CMD_SUCCESS;// not a variable
  5734. }
  5735. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5736. if( start >= end )
  5737. return SCRIPT_CMD_SUCCESS;// nothing to free
  5738. if( is_string_variable(name) )
  5739. value = (void *)"";
  5740. else
  5741. value = (void *)0;
  5742. if( script_hasdata(st,3) ) {
  5743. unsigned int count = script_getnum(st, 3);
  5744. if( count > end - start )
  5745. count = end - start;
  5746. if( count <= 0 )
  5747. return SCRIPT_CMD_SUCCESS;// nothing to free
  5748. if( end - start < sa->size ) {
  5749. // Better to iterate directly on the array, no speed-up from using sa
  5750. for( ; start + count < end; ++start ) {
  5751. // Compact and overwrite
  5752. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5753. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5754. script_removetop(st, -1, 0);
  5755. }
  5756. for( ; start < end; start++ ) {
  5757. // Clean up any leftovers that weren't overwritten
  5758. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5759. }
  5760. } else {
  5761. // using sa to speed up
  5762. unsigned int *list = NULL, size = 0;
  5763. list = script_array_cpy_list(sa);
  5764. size = sa->size;
  5765. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5766. ARR_FIND(0, size, i, list[i] >= start);
  5767. for( ; i < size && list[i] < start + count; i++ ) {
  5768. // Clear any entries between start and start+count, if they exist
  5769. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5770. }
  5771. for( ; i < size && list[i] < end; i++ ) {
  5772. // Move back count positions any entries between start+count to fill the gaps
  5773. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5774. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5775. script_removetop(st, -1, 0);
  5776. // Clear their originals
  5777. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5778. }
  5779. }
  5780. } else {
  5781. unsigned int *list = NULL, size = 0;
  5782. list = script_array_cpy_list(sa);
  5783. size = sa->size;
  5784. for(i = 0; i < size; i++) {
  5785. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5786. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5787. }
  5788. }
  5789. return SCRIPT_CMD_SUCCESS;
  5790. }
  5791. /// Returns a reference to the target index of the array variable.
  5792. /// Equivalent to var[index].
  5793. ///
  5794. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5795. BUILDIN_FUNC(getelementofarray)
  5796. {
  5797. struct script_data* data;
  5798. int32 id;
  5799. int64 i;
  5800. data = script_getdata(st, 2);
  5801. if( !data_isreference(data) )
  5802. {
  5803. ShowError("script:getelementofarray: not a variable\n");
  5804. script_reportdata(data);
  5805. script_pushnil(st);
  5806. st->state = END;
  5807. return SCRIPT_CMD_SUCCESS;// not a variable
  5808. }
  5809. id = reference_getid(data);
  5810. i = script_getnum(st, 3);
  5811. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5812. ShowWarning("script:getelementofarray: index out of range (%"PRId64")\n", i);
  5813. script_reportdata(data);
  5814. script_pushnil(st);
  5815. st->state = END;
  5816. return SCRIPT_CMD_FAILURE;// out of range
  5817. }
  5818. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5819. return SCRIPT_CMD_SUCCESS;
  5820. }
  5821. /////////////////////////////////////////////////////////////////////
  5822. /// ...
  5823. ///
  5824. /*==========================================
  5825. *
  5826. *------------------------------------------*/
  5827. BUILDIN_FUNC(setlook)
  5828. {
  5829. int type,val;
  5830. TBL_PC* sd;
  5831. type = script_getnum(st,2);
  5832. val = script_getnum(st,3);
  5833. if (!script_charid2sd(4,sd))
  5834. return SCRIPT_CMD_SUCCESS;
  5835. pc_changelook(sd,type,val);
  5836. return SCRIPT_CMD_SUCCESS;
  5837. }
  5838. BUILDIN_FUNC(changelook)
  5839. { // As setlook but only client side
  5840. int type,val;
  5841. TBL_PC* sd;
  5842. type = script_getnum(st,2);
  5843. val = script_getnum(st,3);
  5844. if (!script_charid2sd(4,sd))
  5845. return SCRIPT_CMD_SUCCESS;
  5846. clif_changelook(&sd->bl,type,val);
  5847. return SCRIPT_CMD_SUCCESS;
  5848. }
  5849. /*==========================================
  5850. *
  5851. *------------------------------------------*/
  5852. BUILDIN_FUNC(cutin)
  5853. {
  5854. TBL_PC* sd;
  5855. if( !script_rid2sd(sd) )
  5856. return SCRIPT_CMD_SUCCESS;
  5857. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5858. return SCRIPT_CMD_SUCCESS;
  5859. }
  5860. /*==========================================
  5861. *
  5862. *------------------------------------------*/
  5863. BUILDIN_FUNC(viewpoint)
  5864. {
  5865. int type,x,y,id,color;
  5866. TBL_PC* sd;
  5867. type=script_getnum(st,2);
  5868. x=script_getnum(st,3);
  5869. y=script_getnum(st,4);
  5870. id=script_getnum(st,5);
  5871. color=script_getnum(st,6);
  5872. if( !script_rid2sd(sd) )
  5873. return SCRIPT_CMD_SUCCESS;
  5874. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5875. return SCRIPT_CMD_SUCCESS;
  5876. }
  5877. /**
  5878. * Set random options for new item
  5879. * @param st Script state
  5880. * @param it Temporary item data
  5881. * @param funcname Function name
  5882. * @param x First position of random option id array from the script
  5883. **/
  5884. static int script_getitem_randomoption(struct script_state *st, struct item *it, const char *funcname, int x) {
  5885. int i, opt_id_n, opt_val_n, opt_param_n;
  5886. struct script_data *opt_id = script_getdata(st,x);
  5887. struct script_data *opt_val = script_getdata(st,x+1);
  5888. struct script_data *opt_param = script_getdata(st,x+2);
  5889. const char *opt_id_var = reference_getname(opt_id);
  5890. const char *opt_val_var = reference_getname(opt_val);
  5891. const char *opt_param_var = reference_getname(opt_param);
  5892. int32 opt_id_id, opt_id_idx;
  5893. int32 opt_val_id, opt_val_idx;
  5894. int32 opt_param_id, opt_param_idx;
  5895. struct reg_db *opt_id_ref = NULL, *opt_val_ref = NULL, *opt_param_ref = NULL;
  5896. if (opt_id_var[strlen(opt_id_var)-1] == '$') {
  5897. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_id_var);
  5898. return SCRIPT_CMD_FAILURE;
  5899. }
  5900. if (opt_val_var[strlen(opt_val_var)-1] == '$') {
  5901. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_val_var);
  5902. return SCRIPT_CMD_FAILURE;
  5903. }
  5904. if (opt_param_var[strlen(opt_param_var)-1] == '$') {
  5905. ShowError("buildin_%s: The array %s is not numeric type.\n", funcname, opt_param_var);
  5906. return SCRIPT_CMD_FAILURE;
  5907. }
  5908. opt_id_ref = reference_getref(opt_id);
  5909. opt_id_n = script_array_highest_key(st, NULL, opt_id_var, opt_id_ref);
  5910. if (opt_id_n < 1) {
  5911. ShowError("buildin_%s: No option id listed.\n", funcname);
  5912. return SCRIPT_CMD_FAILURE;
  5913. }
  5914. opt_val_ref = reference_getref(opt_val);
  5915. opt_param_ref = reference_getref(opt_param);
  5916. opt_val_n = script_array_highest_key(st, NULL, opt_val_var, opt_val_ref);
  5917. opt_param_n = script_array_highest_key(st, NULL, opt_param_var, opt_param_ref);
  5918. if (opt_val_n < 1) {
  5919. ShowError("buildin_%s: No option value listed.\n", funcname);
  5920. return SCRIPT_CMD_FAILURE;
  5921. }
  5922. if (opt_param_n < 1) {
  5923. ShowError("buildin_%s: No option parameter listed.\n", funcname);
  5924. return SCRIPT_CMD_FAILURE;
  5925. }
  5926. opt_id_id = reference_getid(opt_id);
  5927. opt_val_id = reference_getid(opt_val);
  5928. opt_param_id = reference_getid(opt_param);
  5929. opt_id_idx = reference_getindex(opt_id);
  5930. opt_val_idx = reference_getindex(opt_val);
  5931. opt_param_idx = reference_getindex(opt_param);
  5932. for (i = 0; i < opt_id_n && i < MAX_ITEM_RDM_OPT; i++) {
  5933. it->option[i].id = (short)__64BPRTSIZE(get_val2(st,reference_uid(opt_id_id,opt_id_idx+i),opt_id_ref));
  5934. script_removetop(st, -1, 0);
  5935. it->option[i].value = (short)__64BPRTSIZE(get_val2(st,reference_uid(opt_val_id,opt_val_idx+i),opt_val_ref));
  5936. script_removetop(st, -1, 0);
  5937. it->option[i].param = (char)__64BPRTSIZE(get_val2(st,reference_uid(opt_param_id,opt_param_idx+i),opt_param_ref));
  5938. script_removetop(st, -1, 0);
  5939. }
  5940. return SCRIPT_CMD_SUCCESS;
  5941. }
  5942. /// Returns number of items in inventory/cart/storage
  5943. /// countitem <nameID>{,<accountID>});
  5944. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5945. /// cartcountitem <nameID>{,<accountID>});
  5946. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5947. /// storagecountitem <nameID>{,<accountID>});
  5948. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5949. /// guildstoragecountitem <nameID>{,<accountID>});
  5950. /// guildstoragecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5951. /// countitem3(<item id>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>)
  5952. /// countitem3("<item name>",<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>)
  5953. BUILDIN_FUNC(countitem)
  5954. {
  5955. int i = 0, aid = 3;
  5956. struct item_data* id = NULL;
  5957. struct script_data* data;
  5958. char *command = (char *)script_getfuncname(st);
  5959. uint8 loc = 0;
  5960. uint16 size, count = 0;
  5961. struct item *items;
  5962. TBL_PC *sd = NULL;
  5963. struct s_storage *gstor = NULL;
  5964. if( command[strlen(command)-1] == '2' ) {
  5965. i = 1;
  5966. aid = 10;
  5967. }
  5968. else if (command[strlen(command)-1] == '3') {
  5969. i = 1;
  5970. aid = 13;
  5971. }
  5972. if( script_hasdata(st,aid) ) {
  5973. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5974. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5975. st->state = END;
  5976. return SCRIPT_CMD_FAILURE;
  5977. }
  5978. }
  5979. else {
  5980. if( !script_rid2sd(sd) )
  5981. return SCRIPT_CMD_SUCCESS;
  5982. }
  5983. if( !strncmp(command, "cart", 4) ) {
  5984. loc = TABLE_CART;
  5985. size = MAX_CART;
  5986. items = sd->cart.u.items_cart;
  5987. }
  5988. else if( !strncmp(command, "storage", 7) ) {
  5989. loc = TABLE_STORAGE;
  5990. size = MAX_STORAGE;
  5991. items = sd->storage.u.items_storage;
  5992. }
  5993. else if( !strncmp(command, "guildstorage", 12) ) {
  5994. gstor = guild2storage2(sd->status.guild_id);
  5995. if (gstor && !sd->state.storage_flag) {
  5996. loc = TABLE_GUILD_STORAGE;
  5997. size = MAX_GUILD_STORAGE;
  5998. items = gstor->u.items_guild;
  5999. } else {
  6000. script_pushint(st, -1);
  6001. return SCRIPT_CMD_SUCCESS;
  6002. }
  6003. }
  6004. else {
  6005. size = MAX_INVENTORY;
  6006. items = sd->inventory.u.items_inventory;
  6007. }
  6008. if( loc == TABLE_CART && !pc_iscarton(sd) ) {
  6009. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  6010. script_pushint(st,-1);
  6011. return SCRIPT_CMD_SUCCESS;
  6012. }
  6013. data = script_getdata(st, 2);
  6014. get_val(st, data); // Convert into value in case of a variable
  6015. if( data_isstring(data) ) // item name
  6016. id = itemdb_searchname(conv_str(st, data));
  6017. else // item id
  6018. id = itemdb_exists(conv_num(st, data));
  6019. if( id == NULL ) {
  6020. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  6021. script_pushint(st,0);
  6022. return SCRIPT_CMD_FAILURE;
  6023. }
  6024. if (loc == TABLE_GUILD_STORAGE)
  6025. gstor->lock = true;
  6026. if( !i ) { // For count/cart/storagecountitem function
  6027. unsigned short nameid = id->nameid;
  6028. for( i = 0; i < size; i++ )
  6029. if( &items[i] && items[i].nameid == nameid )
  6030. count += items[i].amount;
  6031. }
  6032. else { // For count/cart/storagecountitem2 function
  6033. struct item it;
  6034. bool check_randomopt = false;
  6035. memset(&it, 0, sizeof(it));
  6036. it.nameid = id->nameid;
  6037. it.identify = script_getnum(st,3);
  6038. it.refine = script_getnum(st,4);
  6039. it.attribute = script_getnum(st,5);
  6040. it.card[0] = script_getnum(st,6);
  6041. it.card[1] = script_getnum(st,7);
  6042. it.card[2] = script_getnum(st,8);
  6043. it.card[3] = script_getnum(st,9);
  6044. if (command[strlen(command)-1] == '3') {
  6045. int res = script_getitem_randomoption(st, &it, command, 10);
  6046. if (res != SCRIPT_CMD_SUCCESS)
  6047. return SCRIPT_CMD_FAILURE;
  6048. check_randomopt = true;
  6049. }
  6050. for( i = 0; i < size; i++ ) {
  6051. struct item *itm = &items[i];
  6052. if (!itm || !itm->nameid || itm->amount < 1)
  6053. continue;
  6054. if (itm->nameid != it.nameid || itm->identify != it.identify || itm->refine != it.refine || itm->attribute != it.attribute)
  6055. continue;
  6056. if (memcmp(it.card, itm->card, sizeof(it.card)))
  6057. continue;
  6058. if (check_randomopt) {
  6059. uint8 j;
  6060. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6061. if (itm->option[j].id != it.option[j].id || itm->option[j].value != it.option[j].value || itm->option[j].param != it.option[j].param)
  6062. break;
  6063. }
  6064. if (j != MAX_ITEM_RDM_OPT)
  6065. continue;
  6066. }
  6067. count += items[i].amount;
  6068. }
  6069. }
  6070. if (loc == TABLE_GUILD_STORAGE) {
  6071. storage_guild_storageclose(sd);
  6072. gstor->lock = false;
  6073. }
  6074. script_pushint(st, count);
  6075. return SCRIPT_CMD_SUCCESS;
  6076. }
  6077. /*==========================================
  6078. * Check if item with this amount can fit in inventory
  6079. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  6080. * Return
  6081. * 0 : fail
  6082. * 1 : success (npc side only)
  6083. *------------------------------------------*/
  6084. BUILDIN_FUNC(checkweight)
  6085. {
  6086. int slots = 0;
  6087. unsigned short amount2 = 0;
  6088. unsigned int weight = 0, i, nbargs;
  6089. struct item_data* id = NULL;
  6090. struct map_session_data* sd;
  6091. if( !script_rid2sd(sd) )
  6092. return SCRIPT_CMD_SUCCESS;
  6093. nbargs = script_lastdata(st)+1;
  6094. if(nbargs%2) {
  6095. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  6096. script_pushint(st,0);
  6097. return SCRIPT_CMD_FAILURE;
  6098. }
  6099. slots = pc_inventoryblank(sd); //nb of empty slot
  6100. for (i = 2; i < nbargs; i += 2) {
  6101. unsigned short nameid, amount;
  6102. struct script_data* data = script_getdata(st,i);
  6103. get_val(st, data); // Convert into value in case of a variable
  6104. if( data_isstring(data) ) // item name
  6105. id = itemdb_searchname(conv_str(st, data));
  6106. else // item id
  6107. id = itemdb_exists(conv_num(st, data));
  6108. if( id == NULL ) {
  6109. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  6110. script_pushint(st,0);
  6111. return SCRIPT_CMD_FAILURE;
  6112. }
  6113. nameid = id->nameid;
  6114. amount = script_getnum(st,i+1);
  6115. if( amount < 1 ) {
  6116. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  6117. script_pushint(st,0);
  6118. return SCRIPT_CMD_FAILURE;
  6119. }
  6120. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  6121. if( weight + sd->weight > sd->max_weight )
  6122. {// too heavy
  6123. script_pushint(st,0);
  6124. return SCRIPT_CMD_SUCCESS;
  6125. }
  6126. switch( pc_checkadditem(sd, nameid, amount) ) {
  6127. case CHKADDITEM_EXIST:
  6128. // item is already in inventory, but there is still space for the requested amount
  6129. break;
  6130. case CHKADDITEM_NEW:
  6131. if( itemdb_isstackable(nameid) ) {
  6132. // stackable
  6133. amount2++;
  6134. if( slots < amount2 ) {
  6135. script_pushint(st,0);
  6136. return SCRIPT_CMD_SUCCESS;
  6137. }
  6138. } else {
  6139. // non-stackable
  6140. amount2 += amount;
  6141. if( slots < amount2) {
  6142. script_pushint(st,0);
  6143. return SCRIPT_CMD_SUCCESS;
  6144. }
  6145. }
  6146. break;
  6147. case CHKADDITEM_OVERAMOUNT:
  6148. script_pushint(st,0);
  6149. return SCRIPT_CMD_SUCCESS;
  6150. }
  6151. }
  6152. script_pushint(st,1);
  6153. return SCRIPT_CMD_SUCCESS;
  6154. }
  6155. BUILDIN_FUNC(checkweight2)
  6156. {
  6157. //variable sub checkweight
  6158. int i = 0, slots = 0, weight = 0;
  6159. short fail = 0;
  6160. unsigned short amount2 = 0;
  6161. //variable for array parsing
  6162. struct script_data* data_it;
  6163. struct script_data* data_nb;
  6164. const char* name_it;
  6165. const char* name_nb;
  6166. int32 id_it, id_nb;
  6167. int32 idx_it, idx_nb;
  6168. int nb_it, nb_nb; //array size
  6169. TBL_PC *sd;
  6170. if( !script_rid2sd(sd) )
  6171. return SCRIPT_CMD_SUCCESS;
  6172. data_it = script_getdata(st, 2);
  6173. data_nb = script_getdata(st, 3);
  6174. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  6175. ShowError("buildin_checkweight2: parameter not a variable\n");
  6176. script_pushint(st,0);
  6177. return SCRIPT_CMD_FAILURE;// not a variable
  6178. }
  6179. id_it = reference_getid(data_it);
  6180. id_nb = reference_getid(data_nb);
  6181. idx_it = reference_getindex(data_it);
  6182. idx_nb = reference_getindex(data_nb);
  6183. name_it = reference_getname(data_it);
  6184. name_nb = reference_getname(data_nb);
  6185. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  6186. ShowError("buildin_checkweight2: illegal type, need int\n");
  6187. script_pushint(st,0);
  6188. return SCRIPT_CMD_FAILURE;// not supported
  6189. }
  6190. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  6191. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  6192. if(nb_it != nb_nb) {
  6193. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  6194. fail = 1;
  6195. }
  6196. slots = pc_inventoryblank(sd);
  6197. for(i=0; i<nb_it; i++) {
  6198. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  6199. script_removetop(st, -1, 0);
  6200. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  6201. script_removetop(st, -1, 0);
  6202. if(fail)
  6203. continue; //cpntonie to depop rest
  6204. if(itemdb_exists(nameid) == NULL ) {
  6205. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  6206. fail=1;
  6207. continue;
  6208. }
  6209. if(amount < 0 ) {
  6210. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  6211. fail = 1;
  6212. continue;
  6213. }
  6214. weight += itemdb_weight(nameid)*amount;
  6215. if( weight + sd->weight > sd->max_weight ) {
  6216. fail = 1;
  6217. continue;
  6218. }
  6219. switch( pc_checkadditem(sd, nameid, amount) ) {
  6220. case CHKADDITEM_EXIST:
  6221. // item is already in inventory, but there is still space for the requested amount
  6222. break;
  6223. case CHKADDITEM_NEW:
  6224. if( itemdb_isstackable(nameid) ) {// stackable
  6225. amount2++;
  6226. if( slots < amount2 )
  6227. fail = 1;
  6228. } else {// non-stackable
  6229. amount2 += amount;
  6230. if( slots < amount2 ) {
  6231. fail = 1;
  6232. }
  6233. }
  6234. break;
  6235. case CHKADDITEM_OVERAMOUNT:
  6236. fail = 1;
  6237. } //end switch
  6238. } //end loop DO NOT break it prematurly we need to depop all stack
  6239. fail ? script_pushint(st,0) : script_pushint(st,1);
  6240. return SCRIPT_CMD_SUCCESS;
  6241. }
  6242. /*==========================================
  6243. * getitem <item id>,<amount>{,<account ID>};
  6244. * getitem "<item name>",<amount>{,<account ID>};
  6245. *
  6246. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6247. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6248. * Type:
  6249. * 0 - No bound
  6250. * 1 - Account Bound
  6251. * 2 - Guild Bound
  6252. * 3 - Party Bound
  6253. * 4 - Character Bound
  6254. *------------------------------------------*/
  6255. BUILDIN_FUNC(getitem)
  6256. {
  6257. int get_count, i;
  6258. unsigned short nameid, amount;
  6259. struct item it;
  6260. TBL_PC *sd;
  6261. struct script_data *data;
  6262. unsigned char flag = 0;
  6263. const char* command = script_getfuncname(st);
  6264. struct item_data *id = NULL;
  6265. data = script_getdata(st,2);
  6266. get_val(st,data);
  6267. if( data_isstring(data) ) {// "<item name>"
  6268. const char *name = conv_str(st,data);
  6269. id = itemdb_searchname(name);
  6270. if( id == NULL ){
  6271. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6272. return SCRIPT_CMD_FAILURE; //No item created.
  6273. }
  6274. nameid = id->nameid;
  6275. } else if( data_isint(data) ) {// <item id>
  6276. nameid = conv_num(st,data);
  6277. if( !(id = itemdb_exists(nameid)) ){
  6278. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  6279. return SCRIPT_CMD_FAILURE; //No item created.
  6280. }
  6281. } else {
  6282. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  6283. return SCRIPT_CMD_FAILURE;
  6284. }
  6285. // <amount>
  6286. if( (amount = script_getnum(st,3)) <= 0)
  6287. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6288. memset(&it,0,sizeof(it));
  6289. it.nameid = nameid;
  6290. it.identify = 1;
  6291. it.bound = BOUND_NONE;
  6292. if( !strcmp(command,"getitembound") ) {
  6293. char bound = script_getnum(st,4);
  6294. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6295. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6296. return SCRIPT_CMD_FAILURE;
  6297. }
  6298. script_mapid2sd(5,sd);
  6299. it.bound = bound;
  6300. } else {
  6301. script_mapid2sd(4,sd);
  6302. }
  6303. if( sd == NULL ) // no target
  6304. return SCRIPT_CMD_SUCCESS;
  6305. //Check if it's stackable.
  6306. if (!itemdb_isstackable2(id))
  6307. get_count = 1;
  6308. else
  6309. get_count = amount;
  6310. for (i = 0; i < amount; i += get_count)
  6311. {
  6312. // if not pet egg
  6313. if (!pet_create_egg(sd, nameid))
  6314. {
  6315. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6316. {
  6317. clif_additem(sd, 0, 0, flag);
  6318. if( pc_candrop(sd,&it) )
  6319. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6320. }
  6321. }
  6322. }
  6323. return SCRIPT_CMD_SUCCESS;
  6324. }
  6325. /*==========================================
  6326. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6327. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6328. *
  6329. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6330. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6331. *
  6332. * getitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6333. * getitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6334. *
  6335. * getitembound3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6336. * getitembound3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  6337. * Type:
  6338. * 0 - No bound
  6339. * 1 - Account Bound
  6340. * 2 - Guild Bound
  6341. * 3 - Party Bound
  6342. * 4 - Character Bound
  6343. *------------------------------------------*/
  6344. BUILDIN_FUNC(getitem2)
  6345. {
  6346. unsigned short nameid, amount;
  6347. int iden, ref, attr;
  6348. unsigned short c1, c2, c3, c4;
  6349. char bound = BOUND_NONE;
  6350. struct item_data *item_data = NULL;
  6351. struct item item_tmp;
  6352. TBL_PC *sd;
  6353. struct script_data *data;
  6354. const char* command = script_getfuncname(st);
  6355. int offset = 0;
  6356. if( !strncmp(command,"getitembound",12) ) {
  6357. int aid_pos = 12;
  6358. bound = script_getnum(st,11);
  6359. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6360. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6361. return SCRIPT_CMD_FAILURE;
  6362. }
  6363. if (command[strlen(command)-1] == '3') {
  6364. offset = 12;
  6365. aid_pos = 15;
  6366. }
  6367. script_mapid2sd(aid_pos,sd);
  6368. } else {
  6369. int aid_pos = 11;
  6370. if (strcmpi(command,"getitem3") == 0) {
  6371. offset = 11;
  6372. aid_pos = 14;
  6373. }
  6374. script_mapid2sd(aid_pos,sd);
  6375. }
  6376. if( sd == NULL ) // no target
  6377. return SCRIPT_CMD_SUCCESS;
  6378. data = script_getdata(st,2);
  6379. get_val(st,data);
  6380. if( data_isstring(data) ) {
  6381. const char *name = conv_str(st,data);
  6382. if( (item_data = itemdb_searchname(name)) == NULL ){
  6383. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6384. return SCRIPT_CMD_FAILURE; //No item created.
  6385. }
  6386. nameid = item_data->nameid;
  6387. } else {
  6388. nameid = conv_num(st,data);
  6389. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6390. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6391. return SCRIPT_CMD_FAILURE; //No item created.
  6392. }
  6393. }
  6394. amount = script_getnum(st,3);
  6395. iden = script_getnum(st,4);
  6396. ref = script_getnum(st,5);
  6397. attr = script_getnum(st,6);
  6398. c1 = (unsigned short)script_getnum(st,7);
  6399. c2 = (unsigned short)script_getnum(st,8);
  6400. c3 = (unsigned short)script_getnum(st,9);
  6401. c4 = (unsigned short)script_getnum(st,10);
  6402. if( item_data ) {
  6403. int get_count = 0, i;
  6404. memset(&item_tmp,0,sizeof(item_tmp));
  6405. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6406. if(ref > MAX_REFINE)
  6407. ref = MAX_REFINE;
  6408. }
  6409. else if( item_data->type == IT_PETEGG ) {
  6410. iden = 1;
  6411. ref = 0;
  6412. }
  6413. else {
  6414. iden = 1;
  6415. ref = attr = 0;
  6416. }
  6417. item_tmp.nameid = nameid;
  6418. item_tmp.identify = iden;
  6419. item_tmp.refine = ref;
  6420. item_tmp.attribute = attr;
  6421. item_tmp.card[0] = c1;
  6422. item_tmp.card[1] = c2;
  6423. item_tmp.card[2] = c3;
  6424. item_tmp.card[3] = c4;
  6425. item_tmp.bound = bound;
  6426. if (offset != 0) {
  6427. int res = script_getitem_randomoption(st, &item_tmp, command, offset);
  6428. if (res == SCRIPT_CMD_FAILURE)
  6429. return SCRIPT_CMD_FAILURE;
  6430. }
  6431. //Check if it's stackable.
  6432. if (!itemdb_isstackable2(item_data))
  6433. get_count = 1;
  6434. else
  6435. get_count = amount;
  6436. for (i = 0; i < amount; i += get_count)
  6437. {
  6438. // if not pet egg
  6439. if (!pet_create_egg(sd, nameid))
  6440. {
  6441. unsigned char flag = 0;
  6442. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6443. {
  6444. clif_additem(sd, 0, 0, flag);
  6445. if( pc_candrop(sd,&item_tmp) )
  6446. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6447. }
  6448. }
  6449. }
  6450. }
  6451. return SCRIPT_CMD_SUCCESS;
  6452. }
  6453. /** Gives rental item to player
  6454. * rentitem <item id>,<seconds>{,<account_id>}
  6455. * rentitem "<item name>",<seconds>{,<account_id>}
  6456. */
  6457. BUILDIN_FUNC(rentitem) {
  6458. struct map_session_data *sd;
  6459. struct script_data *data;
  6460. struct item it;
  6461. int seconds;
  6462. unsigned short nameid = 0;
  6463. unsigned char flag = 0;
  6464. data = script_getdata(st,2);
  6465. get_val(st,data);
  6466. if (!script_accid2sd(4,sd))
  6467. return SCRIPT_CMD_FAILURE;
  6468. if( data_isstring(data) )
  6469. {
  6470. const char *name = conv_str(st,data);
  6471. struct item_data *itd = itemdb_searchname(name);
  6472. if( itd == NULL )
  6473. {
  6474. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6475. return SCRIPT_CMD_FAILURE;
  6476. }
  6477. nameid = itd->nameid;
  6478. }
  6479. else if( data_isint(data) )
  6480. {
  6481. nameid = conv_num(st,data);
  6482. if( nameid == 0 || !itemdb_exists(nameid) )
  6483. {
  6484. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6485. return SCRIPT_CMD_FAILURE;
  6486. }
  6487. }
  6488. else
  6489. {
  6490. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6491. return SCRIPT_CMD_FAILURE;
  6492. }
  6493. seconds = script_getnum(st,3);
  6494. memset(&it, 0, sizeof(it));
  6495. it.nameid = nameid;
  6496. it.identify = 1;
  6497. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6498. it.bound = BOUND_NONE;
  6499. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6500. {
  6501. clif_additem(sd, 0, 0, flag);
  6502. return SCRIPT_CMD_FAILURE;
  6503. }
  6504. return SCRIPT_CMD_SUCCESS;
  6505. }
  6506. /**
  6507. * Gives rental item to player with advanced option
  6508. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6509. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6510. *
  6511. * rentitem3 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6512. * rentitem3 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account_id>};
  6513. */
  6514. BUILDIN_FUNC(rentitem2) {
  6515. struct map_session_data *sd;
  6516. struct script_data *data;
  6517. struct item it;
  6518. struct item_data *id;
  6519. int seconds;
  6520. unsigned short nameid = 0;
  6521. unsigned char flag = 0;
  6522. int iden,ref,attr,c1,c2,c3,c4;
  6523. const char *funcname = script_getfuncname(st);
  6524. data = script_getdata(st,2);
  6525. get_val(st,data);
  6526. if (funcname[strlen(funcname)-1] == '3') {
  6527. if (!script_accid2sd(14,sd))
  6528. return SCRIPT_CMD_FAILURE;
  6529. } else if (!script_accid2sd(11,sd))
  6530. return SCRIPT_CMD_FAILURE;
  6531. if( data_isstring(data) ) {
  6532. const char *name = conv_str(st,data);
  6533. id = itemdb_searchname(name);
  6534. if( id == NULL ) {
  6535. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6536. return SCRIPT_CMD_FAILURE;
  6537. }
  6538. nameid = id->nameid;
  6539. }
  6540. else if( data_isint(data) ) {
  6541. nameid = conv_num(st,data);
  6542. if( !(id = itemdb_search(nameid))) {
  6543. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6544. return SCRIPT_CMD_FAILURE;
  6545. }
  6546. }
  6547. else {
  6548. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6549. return SCRIPT_CMD_FAILURE;
  6550. }
  6551. seconds = script_getnum(st,3);
  6552. iden = script_getnum(st,4);
  6553. ref = script_getnum(st,5);
  6554. attr = script_getnum(st,6);
  6555. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6556. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6557. }
  6558. else if (id->type==IT_PETEGG) {
  6559. iden = 1;
  6560. ref = 0;
  6561. }
  6562. else {
  6563. iden = 1;
  6564. ref = attr = 0;
  6565. }
  6566. c1 = (short)script_getnum(st,7);
  6567. c2 = (short)script_getnum(st,8);
  6568. c3 = (short)script_getnum(st,9);
  6569. c4 = (short)script_getnum(st,10);
  6570. memset(&it, 0, sizeof(it));
  6571. it.nameid = nameid;
  6572. it.identify = iden;
  6573. it.refine = ref;
  6574. it.attribute = attr;
  6575. it.card[0] = (short)c1;
  6576. it.card[1] = (short)c2;
  6577. it.card[2] = (short)c3;
  6578. it.card[3] = (short)c4;
  6579. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6580. if (funcname[strlen(funcname)-1] == '3') {
  6581. int res = script_getitem_randomoption(st, &it, funcname, 11);
  6582. if (res != SCRIPT_CMD_SUCCESS)
  6583. return res;
  6584. }
  6585. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6586. clif_additem(sd, 0, 0, flag);
  6587. return SCRIPT_CMD_FAILURE;
  6588. }
  6589. return SCRIPT_CMD_SUCCESS;
  6590. }
  6591. /*==========================================
  6592. * gets an item with someone's name inscribed [Skotlex]
  6593. * getinscribeditem item_num, character_name
  6594. * Returned Qty is always 1, only works on equip-able
  6595. * equipment
  6596. *------------------------------------------*/
  6597. BUILDIN_FUNC(getnameditem)
  6598. {
  6599. unsigned short nameid;
  6600. struct item item_tmp;
  6601. TBL_PC *sd, *tsd;
  6602. struct script_data *data;
  6603. if (!script_rid2sd(sd))
  6604. { //Player not attached!
  6605. script_pushint(st,0);
  6606. return SCRIPT_CMD_SUCCESS;
  6607. }
  6608. data=script_getdata(st,2);
  6609. get_val(st,data);
  6610. if( data_isstring(data) ){
  6611. const char *name=conv_str(st,data);
  6612. struct item_data *item_data = itemdb_searchname(name);
  6613. if( item_data == NULL)
  6614. { //Failed
  6615. script_pushint(st,0);
  6616. return SCRIPT_CMD_SUCCESS;
  6617. }
  6618. nameid = item_data->nameid;
  6619. }else
  6620. nameid = conv_num(st,data);
  6621. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6622. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6623. script_pushint(st,0);
  6624. return SCRIPT_CMD_SUCCESS;
  6625. }
  6626. data=script_getdata(st,3);
  6627. get_val(st,data);
  6628. if( data_isstring(data) ) //Char Name
  6629. tsd=map_nick2sd(conv_str(st,data),false);
  6630. else //Char Id was given
  6631. tsd=map_charid2sd(conv_num(st,data));
  6632. if( tsd == NULL )
  6633. { //Failed
  6634. script_pushint(st,0);
  6635. return SCRIPT_CMD_SUCCESS;
  6636. }
  6637. memset(&item_tmp,0,sizeof(item_tmp));
  6638. item_tmp.nameid=nameid;
  6639. item_tmp.amount=1;
  6640. item_tmp.identify=1;
  6641. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6642. item_tmp.card[2]=tsd->status.char_id;
  6643. item_tmp.card[3]=tsd->status.char_id >> 16;
  6644. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6645. script_pushint(st,0);
  6646. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6647. }
  6648. script_pushint(st,1);
  6649. return SCRIPT_CMD_SUCCESS;
  6650. }
  6651. /*==========================================
  6652. * gets a random item ID from an item group [Skotlex]
  6653. * groupranditem <group_num>{,<sub_group>};
  6654. *------------------------------------------*/
  6655. BUILDIN_FUNC(grouprandomitem) {
  6656. struct s_item_group_entry *entry = NULL;
  6657. int sub_group = 1;
  6658. FETCH(3, sub_group);
  6659. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6660. if (!entry) {
  6661. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6662. script_pushint(st,UNKNOWN_ITEM_ID);
  6663. return SCRIPT_CMD_FAILURE;
  6664. }
  6665. script_pushint(st,entry->nameid);
  6666. return SCRIPT_CMD_SUCCESS;
  6667. }
  6668. /**
  6669. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6670. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6671. */
  6672. BUILDIN_FUNC(makeitem) {
  6673. uint16 nameid, amount, flag = 0, x, y;
  6674. const char *mapname;
  6675. int m;
  6676. struct item item_tmp;
  6677. struct script_data *data;
  6678. data = script_getdata(st,2);
  6679. get_val(st,data);
  6680. if( data_isstring(data) ){
  6681. const char *name = conv_str(st,data);
  6682. struct item_data *item_data = itemdb_searchname(name);
  6683. if( item_data )
  6684. nameid = item_data->nameid;
  6685. else
  6686. nameid = UNKNOWN_ITEM_ID;
  6687. }
  6688. else
  6689. nameid = conv_num(st,data);
  6690. amount = script_getnum(st,3);
  6691. mapname = script_getstr(st,4);
  6692. x = script_getnum(st,5);
  6693. y = script_getnum(st,6);
  6694. if(strcmp(mapname,"this")==0) {
  6695. TBL_PC *sd;
  6696. if (!script_rid2sd(sd))
  6697. return SCRIPT_CMD_SUCCESS; //Failed...
  6698. m = sd->bl.m;
  6699. } else
  6700. m = map_mapname2mapid(mapname);
  6701. if(nameid<0) {
  6702. nameid = -nameid;
  6703. flag = 1;
  6704. }
  6705. if(nameid > 0) {
  6706. memset(&item_tmp,0,sizeof(item_tmp));
  6707. item_tmp.nameid = nameid;
  6708. if(!flag)
  6709. item_tmp.identify = 1;
  6710. else
  6711. item_tmp.identify = itemdb_isidentified(nameid);
  6712. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6713. }
  6714. return SCRIPT_CMD_SUCCESS;
  6715. }
  6716. /**
  6717. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6718. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6719. *
  6720. * makeitem3 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>;
  6721. * makeitem3 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>;
  6722. */
  6723. BUILDIN_FUNC(makeitem2) {
  6724. uint16 nameid, amount, x, y;
  6725. const char *mapname;
  6726. int m;
  6727. struct item item_tmp;
  6728. struct script_data *data;
  6729. struct item_data *id;
  6730. const char *funcname = script_getfuncname(st);
  6731. data = script_getdata(st,2);
  6732. get_val(st,data);
  6733. if( data_isstring(data) ){
  6734. const char *name = conv_str(st,data);
  6735. struct item_data *item_data = itemdb_searchname(name);
  6736. if( item_data )
  6737. nameid = item_data->nameid;
  6738. else
  6739. nameid = UNKNOWN_ITEM_ID;
  6740. }
  6741. else
  6742. nameid = conv_num(st,data);
  6743. amount = script_getnum(st,3);
  6744. mapname = script_getstr(st,4);
  6745. x = script_getnum(st,5);
  6746. y = script_getnum(st,6);
  6747. if (strcmp(mapname,"this")==0) {
  6748. TBL_PC *sd;
  6749. if (!script_rid2sd(sd))
  6750. return SCRIPT_CMD_SUCCESS; //Failed...
  6751. m = sd->bl.m;
  6752. }
  6753. else
  6754. m = map_mapname2mapid(mapname);
  6755. if ((id = itemdb_search(nameid))) {
  6756. char iden, ref, attr;
  6757. memset(&item_tmp,0,sizeof(item_tmp));
  6758. item_tmp.nameid = nameid;
  6759. iden = (char)script_getnum(st,7);
  6760. ref = (char)script_getnum(st,8);
  6761. attr = (char)script_getnum(st,9);
  6762. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6763. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6764. }
  6765. else if (id->type==IT_PETEGG) {
  6766. iden = 1;
  6767. ref = 0;
  6768. }
  6769. else {
  6770. iden = 1;
  6771. ref = attr = 0;
  6772. }
  6773. item_tmp.identify = iden;
  6774. item_tmp.refine = ref;
  6775. item_tmp.attribute = attr;
  6776. item_tmp.card[0] = script_getnum(st,10);
  6777. item_tmp.card[1] = script_getnum(st,11);
  6778. item_tmp.card[2] = script_getnum(st,12);
  6779. item_tmp.card[3] = script_getnum(st,13);
  6780. if (funcname[strlen(funcname)-1] == '3') {
  6781. int res = script_getitem_randomoption(st, &item_tmp, funcname, 14);
  6782. if (res != SCRIPT_CMD_SUCCESS)
  6783. return res;
  6784. }
  6785. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6786. }
  6787. else
  6788. return SCRIPT_CMD_FAILURE;
  6789. return SCRIPT_CMD_SUCCESS;
  6790. }
  6791. /// Counts / deletes the current item given by idx.
  6792. /// Used by buildin_delitem_search
  6793. /// Relies on all input data being already fully valid.
  6794. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6795. {
  6796. int delamount;
  6797. struct item *itm = NULL;
  6798. struct s_storage *gstor = NULL;
  6799. switch(loc) {
  6800. case TABLE_CART:
  6801. itm = &sd->cart.u.items_cart[idx];
  6802. break;
  6803. case TABLE_STORAGE:
  6804. itm = &sd->storage.u.items_storage[idx];
  6805. break;
  6806. case TABLE_GUILD_STORAGE:
  6807. {
  6808. gstor = guild2storage2(sd->status.guild_id);
  6809. itm = &gstor->u.items_guild[idx];
  6810. }
  6811. break;
  6812. default: // TABLE_INVENTORY
  6813. itm = &sd->inventory.u.items_inventory[idx];
  6814. break;
  6815. }
  6816. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6817. if( delete_items )
  6818. {
  6819. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6820. {// delete associated pet
  6821. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6822. }
  6823. switch(loc) {
  6824. case TABLE_CART:
  6825. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6826. break;
  6827. case TABLE_STORAGE:
  6828. storage_delitem(sd,&sd->storage,idx,delamount);
  6829. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6830. break;
  6831. case TABLE_GUILD_STORAGE:
  6832. gstor->lock = true;
  6833. storage_guild_delitem(sd, gstor, idx, delamount);
  6834. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  6835. storage_guild_storageclose(sd);
  6836. gstor->lock = false;
  6837. break;
  6838. default: // TABLE_INVENTORY
  6839. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6840. break;
  6841. }
  6842. }
  6843. amount[0]-= delamount;
  6844. }
  6845. /// Searches for item(s) and checks, if there is enough of them.
  6846. /// Used by delitem and delitem2
  6847. /// Relies on all input data being already fully valid.
  6848. /// @param exact_match will also match item by specified attributes
  6849. /// 0x0: Only item id
  6850. /// 0x1: identify, attributes, cards
  6851. /// 0x2: random option
  6852. /// @return true when all items could be deleted, false when there were not enough items to delete
  6853. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, uint8 exact_match, uint8 loc)
  6854. {
  6855. bool delete_items = false;
  6856. int i, amount, size;
  6857. struct item *items;
  6858. // prefer always non-equipped items
  6859. it->equip = 0;
  6860. // when searching for nameid only, prefer additionally
  6861. if( !exact_match )
  6862. {
  6863. // non-refined items
  6864. it->refine = 0;
  6865. // card-less items
  6866. memset(it->card, 0, sizeof(it->card));
  6867. }
  6868. switch(loc) {
  6869. case TABLE_CART:
  6870. size = MAX_CART;
  6871. items = sd->cart.u.items_cart;
  6872. break;
  6873. case TABLE_STORAGE:
  6874. size = MAX_STORAGE;
  6875. items = sd->storage.u.items_storage;
  6876. break;
  6877. case TABLE_GUILD_STORAGE:
  6878. {
  6879. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6880. size = MAX_GUILD_STORAGE;
  6881. items = gstor->u.items_guild;
  6882. }
  6883. break;
  6884. default: // TABLE_INVENTORY
  6885. size = MAX_INVENTORY;
  6886. items = sd->inventory.u.items_inventory;
  6887. break;
  6888. }
  6889. for(;;)
  6890. {
  6891. unsigned short important = 0;
  6892. amount = it->amount;
  6893. // 1st pass -- less important items / exact match
  6894. for( i = 0; amount && i < size; i++ )
  6895. {
  6896. struct item *itm = NULL;
  6897. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6898. {// wrong/invalid item
  6899. continue;
  6900. }
  6901. if( itm->equip != it->equip || itm->refine != it->refine )
  6902. {// not matching attributes
  6903. important++;
  6904. continue;
  6905. }
  6906. if( exact_match )
  6907. {
  6908. if( (exact_match&0x1) && ( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  6909. {// not matching exact attributes
  6910. continue;
  6911. }
  6912. if (exact_match&0x2) {
  6913. uint8 j;
  6914. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6915. if (itm->option[j].id != it->option[j].id || itm->option[j].value != it->option[j].value || itm->option[j].param != it->option[j].param)
  6916. break;
  6917. }
  6918. if (j != MAX_ITEM_RDM_OPT)
  6919. continue;
  6920. }
  6921. }
  6922. else
  6923. {
  6924. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6925. {
  6926. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6927. {// pet which cannot be deleted
  6928. continue;
  6929. }
  6930. }
  6931. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6932. {// named/carded item
  6933. important++;
  6934. continue;
  6935. }
  6936. }
  6937. // count / delete item
  6938. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6939. }
  6940. // 2nd pass -- any matching item
  6941. if( amount == 0 || important == 0 )
  6942. {// either everything was already consumed or no items were skipped
  6943. ;
  6944. }
  6945. else for( i = 0; amount && i < size; i++ )
  6946. {
  6947. struct item *itm = NULL;
  6948. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6949. {// wrong/invalid item
  6950. continue;
  6951. }
  6952. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6953. {// pet which cannot be deleted
  6954. continue;
  6955. }
  6956. if( exact_match )
  6957. {
  6958. if( (exact_match&0x1) && ( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) ) )
  6959. {// not matching attributes
  6960. continue;
  6961. }
  6962. if (exact_match&0x2) {
  6963. uint8 j;
  6964. for (j = 0; j < MAX_ITEM_RDM_OPT; j++) {
  6965. if (itm->option[j].id != it->option[j].id || itm->option[j].value != it->option[j].value || itm->option[j].param != it->option[j].param)
  6966. break;
  6967. }
  6968. if (j != MAX_ITEM_RDM_OPT)
  6969. continue;
  6970. }
  6971. }
  6972. // count / delete item
  6973. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6974. }
  6975. if( amount )
  6976. {// not enough items
  6977. return false;
  6978. }
  6979. else if( delete_items )
  6980. {// we are done with the work
  6981. return true;
  6982. }
  6983. else
  6984. {// get rid of the items now
  6985. delete_items = true;
  6986. }
  6987. }
  6988. }
  6989. /// Deletes items from the target/attached player.
  6990. /// Prioritizes ordinary items.
  6991. ///
  6992. /// delitem <item id>,<amount>{,<account id>}
  6993. /// delitem "<item name>",<amount>{,<account id>}
  6994. /// cartdelitem <item id>,<amount>{,<account id>}
  6995. /// cartdelitem "<item name>",<amount>{,<account id>}
  6996. /// storagedelitem <item id>,<amount>{,<account id>}
  6997. /// storagedelitem "<item name>",<amount>{,<account id>}
  6998. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  6999. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  7000. BUILDIN_FUNC(delitem)
  7001. {
  7002. TBL_PC *sd;
  7003. struct item it;
  7004. struct script_data *data;
  7005. uint8 loc = 0;
  7006. char* command = (char*)script_getfuncname(st);
  7007. if(!strncmp(command, "cart", 4))
  7008. loc = TABLE_CART;
  7009. else if(!strncmp(command, "storage", 7))
  7010. loc = TABLE_STORAGE;
  7011. else if(!strncmp(command, "guildstorage", 12))
  7012. loc = TABLE_GUILD_STORAGE;
  7013. if( !script_accid2sd(4,sd) ){
  7014. // In any case cancel script execution
  7015. st->state = END;
  7016. return SCRIPT_CMD_SUCCESS;
  7017. }
  7018. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7019. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7020. script_pushint(st, -1);
  7021. return SCRIPT_CMD_FAILURE;
  7022. }
  7023. if (loc == TABLE_GUILD_STORAGE) {
  7024. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7025. if (gstor == NULL || sd->state.storage_flag) {
  7026. script_pushint(st, -1);
  7027. return SCRIPT_CMD_FAILURE;
  7028. }
  7029. }
  7030. data = script_getdata(st,2);
  7031. get_val(st,data);
  7032. if( data_isstring(data) )
  7033. {
  7034. const char* item_name = conv_str(st,data);
  7035. struct item_data* id = itemdb_searchname(item_name);
  7036. if( id == NULL )
  7037. {
  7038. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7039. st->state = END;
  7040. return SCRIPT_CMD_FAILURE;
  7041. }
  7042. it.nameid = id->nameid;// "<item name>"
  7043. }
  7044. else
  7045. {
  7046. it.nameid = conv_num(st,data);// <item id>
  7047. if( !itemdb_exists( it.nameid ) )
  7048. {
  7049. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  7050. st->state = END;
  7051. return SCRIPT_CMD_FAILURE;
  7052. }
  7053. }
  7054. it.amount=script_getnum(st,3);
  7055. if( it.amount <= 0 )
  7056. return SCRIPT_CMD_SUCCESS;// nothing to do
  7057. if( buildin_delitem_search(sd, &it, 0, loc) )
  7058. {// success
  7059. return SCRIPT_CMD_SUCCESS;
  7060. }
  7061. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  7062. st->state = END;
  7063. st->mes_active = 0;
  7064. clif_scriptclose(sd, st->oid);
  7065. return SCRIPT_CMD_FAILURE;
  7066. }
  7067. /// Deletes items from the target/attached player.
  7068. ///
  7069. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7070. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7071. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7072. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7073. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7074. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7075. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7076. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  7077. /// delitem3 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7078. /// delitem3 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<RandomIDArray>,<RandomValueArray>,<RandomParamArray>{,<account ID>};
  7079. BUILDIN_FUNC(delitem2)
  7080. {
  7081. TBL_PC *sd;
  7082. struct item it;
  7083. struct script_data *data;
  7084. uint8 loc = 0;
  7085. char* command = (char*)script_getfuncname(st);
  7086. int aid_pos = 11;
  7087. uint8 flag = 0x1;
  7088. if(!strncmp(command, "cart", 4))
  7089. loc = TABLE_CART;
  7090. else if(!strncmp(command, "storage", 7))
  7091. loc = TABLE_STORAGE;
  7092. else if(!strncmp(command, "guildstorage", 12))
  7093. loc = TABLE_GUILD_STORAGE;
  7094. if (command[strlen(command)-1] == '3')
  7095. aid_pos = 14;
  7096. if( !script_accid2sd(aid_pos,sd) ){
  7097. // In any case cancel script execution
  7098. st->state = END;
  7099. return SCRIPT_CMD_SUCCESS;
  7100. }
  7101. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  7102. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  7103. script_pushint(st,-1);
  7104. return SCRIPT_CMD_FAILURE;
  7105. }
  7106. if (loc == TABLE_GUILD_STORAGE) {
  7107. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  7108. if (gstor == NULL || sd->state.storage_flag) {
  7109. script_pushint(st, -1);
  7110. return SCRIPT_CMD_FAILURE;
  7111. }
  7112. }
  7113. memset(&it, 0, sizeof(it));
  7114. data = script_getdata(st,2);
  7115. get_val(st,data);
  7116. if( data_isstring(data) )
  7117. {
  7118. const char* item_name = conv_str(st,data);
  7119. struct item_data* id = itemdb_searchname(item_name);
  7120. if( id == NULL )
  7121. {
  7122. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  7123. st->state = END;
  7124. return SCRIPT_CMD_FAILURE;
  7125. }
  7126. it.nameid = id->nameid;// "<item name>"
  7127. }
  7128. else
  7129. {
  7130. it.nameid = conv_num(st,data);// <item id>
  7131. if( !itemdb_exists( it.nameid ) )
  7132. {
  7133. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  7134. st->state = END;
  7135. return SCRIPT_CMD_FAILURE;
  7136. }
  7137. }
  7138. it.amount=script_getnum(st,3);
  7139. it.identify=script_getnum(st,4);
  7140. it.refine=script_getnum(st,5);
  7141. it.attribute=script_getnum(st,6);
  7142. it.card[0]=(short)script_getnum(st,7);
  7143. it.card[1]=(short)script_getnum(st,8);
  7144. it.card[2]=(short)script_getnum(st,9);
  7145. it.card[3]=(short)script_getnum(st,10);
  7146. if (command[strlen(command)-1] == '3') {
  7147. int res = script_getitem_randomoption(st, &it, command, 11);
  7148. if (res != SCRIPT_CMD_SUCCESS)
  7149. return SCRIPT_CMD_FAILURE;
  7150. flag |= 0x2;
  7151. }
  7152. if( it.amount <= 0 )
  7153. return SCRIPT_CMD_SUCCESS;// nothing to do
  7154. if( buildin_delitem_search(sd, &it, flag, loc) )
  7155. {// success
  7156. return SCRIPT_CMD_SUCCESS;
  7157. }
  7158. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  7159. st->state = END;
  7160. st->mes_active = 0;
  7161. clif_scriptclose(sd, st->oid);
  7162. return SCRIPT_CMD_FAILURE;
  7163. }
  7164. /*==========================================
  7165. * Enables/Disables use of items while in an NPC [Skotlex]
  7166. *------------------------------------------*/
  7167. BUILDIN_FUNC(enableitemuse)
  7168. {
  7169. TBL_PC *sd;
  7170. if (script_rid2sd(sd))
  7171. st->npc_item_flag = sd->npc_item_flag = 1;
  7172. return SCRIPT_CMD_SUCCESS;
  7173. }
  7174. BUILDIN_FUNC(disableitemuse)
  7175. {
  7176. TBL_PC *sd;
  7177. if (script_rid2sd(sd))
  7178. st->npc_item_flag = sd->npc_item_flag = 0;
  7179. return SCRIPT_CMD_SUCCESS;
  7180. }
  7181. /*==========================================
  7182. * Returns a character's specified stat.
  7183. * Check pc_readparam for available options.
  7184. * readparam <param>{,"<nick>"}
  7185. * readparam <param>{,<char_id>}
  7186. *------------------------------------------*/
  7187. BUILDIN_FUNC(readparam)
  7188. {
  7189. int value;
  7190. struct script_data *data = script_getdata(st, 2);
  7191. TBL_PC *sd;
  7192. if( script_hasdata(st, 3) ){
  7193. if( script_isint(st, 3) ){
  7194. script_charid2sd(3, sd);
  7195. }else{
  7196. script_nick2sd(3, sd);
  7197. }
  7198. }else{
  7199. script_rid2sd(sd);
  7200. }
  7201. if( !sd ){
  7202. script_pushint(st, -1);
  7203. return SCRIPT_CMD_FAILURE;
  7204. }
  7205. // If you use a parameter, return the value behind it
  7206. if( reference_toparam(data) ){
  7207. get_val_(st, data, sd);
  7208. value = (int)data->u.num;
  7209. }else{
  7210. value = pc_readparam(sd,script_getnum(st, 2));
  7211. }
  7212. script_pushint(st,value);
  7213. return SCRIPT_CMD_SUCCESS;
  7214. }
  7215. /*==========================================
  7216. * Return charid identification
  7217. * return by @num :
  7218. * 0 : char_id
  7219. * 1 : party_id
  7220. * 2 : guild_id
  7221. * 3 : account_id
  7222. * 4 : bg_id
  7223. * 5 : clan_id
  7224. *------------------------------------------*/
  7225. BUILDIN_FUNC(getcharid)
  7226. {
  7227. int num;
  7228. TBL_PC *sd;
  7229. num = script_getnum(st,2);
  7230. if( !script_nick2sd(3,sd) ){
  7231. script_pushint(st,0); //return 0, according docs
  7232. return SCRIPT_CMD_SUCCESS;
  7233. }
  7234. switch( num ) {
  7235. case 0: script_pushint(st,sd->status.char_id); break;
  7236. case 1: script_pushint(st,sd->status.party_id); break;
  7237. case 2: script_pushint(st,sd->status.guild_id); break;
  7238. case 3: script_pushint(st,sd->status.account_id); break;
  7239. case 4: script_pushint(st,sd->bg_id); break;
  7240. case 5: script_pushint(st,sd->status.clan_id); break;
  7241. default:
  7242. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  7243. script_pushint(st,0);
  7244. break;
  7245. }
  7246. return SCRIPT_CMD_SUCCESS;
  7247. }
  7248. /*==========================================
  7249. * returns the GID of an NPC
  7250. *------------------------------------------*/
  7251. BUILDIN_FUNC(getnpcid)
  7252. {
  7253. int num = script_getnum(st,2);
  7254. struct npc_data* nd = NULL;
  7255. if( script_hasdata(st,3) )
  7256. {// unique npc name
  7257. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  7258. {
  7259. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  7260. script_pushint(st,0);
  7261. return SCRIPT_CMD_FAILURE;
  7262. }
  7263. }
  7264. switch (num) {
  7265. case 0:
  7266. script_pushint(st,nd ? nd->bl.id : st->oid);
  7267. break;
  7268. default:
  7269. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  7270. script_pushint(st,0);
  7271. return SCRIPT_CMD_FAILURE;
  7272. }
  7273. return SCRIPT_CMD_SUCCESS;
  7274. }
  7275. /*==========================================
  7276. * Return the name of the party_id
  7277. * null if not found
  7278. *------------------------------------------*/
  7279. BUILDIN_FUNC(getpartyname)
  7280. {
  7281. int party_id;
  7282. struct party_data* p;
  7283. party_id = script_getnum(st,2);
  7284. if( ( p = party_search(party_id) ) != NULL )
  7285. {
  7286. script_pushstrcopy(st,p->party.name);
  7287. }
  7288. else
  7289. {
  7290. script_pushconststr(st,"null");
  7291. }
  7292. return SCRIPT_CMD_SUCCESS;
  7293. }
  7294. /*==========================================
  7295. * Get the information of the members of a party by type
  7296. * @party_id, @type
  7297. * return by @type :
  7298. * - : nom des membres
  7299. * 1 : char_id des membres
  7300. * 2 : account_id des membres
  7301. *------------------------------------------*/
  7302. BUILDIN_FUNC(getpartymember)
  7303. {
  7304. struct party_data *p;
  7305. uint8 j = 0;
  7306. p = party_search(script_getnum(st,2));
  7307. if (p != NULL) {
  7308. uint8 i, type = 0;
  7309. struct script_data *data = NULL;
  7310. char *varname = NULL;
  7311. if (script_hasdata(st,3))
  7312. type = script_getnum(st,3);
  7313. if (script_hasdata(st,4)) {
  7314. data = script_getdata(st, 4);
  7315. if (!data_isreference(data)) {
  7316. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7317. return SCRIPT_CMD_FAILURE;
  7318. }
  7319. varname = reference_getname(data);
  7320. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7321. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7322. return SCRIPT_CMD_FAILURE;
  7323. }
  7324. }
  7325. for (i = 0; i < MAX_PARTY; i++) {
  7326. if (p->party.member[i].account_id) {
  7327. switch (type) {
  7328. case 2:
  7329. if (data)
  7330. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  7331. else
  7332. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7333. break;
  7334. case 1:
  7335. if (data)
  7336. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  7337. else
  7338. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7339. break;
  7340. default:
  7341. if (data)
  7342. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  7343. else
  7344. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7345. break;
  7346. }
  7347. j++;
  7348. }
  7349. }
  7350. }
  7351. mapreg_setreg(add_str("$@partymembercount"),j);
  7352. script_pushint(st, j);
  7353. return SCRIPT_CMD_SUCCESS;
  7354. }
  7355. /*==========================================
  7356. * Retrieves party leader. if flag is specified,
  7357. * return some of the leader data. Otherwise, return name.
  7358. *------------------------------------------*/
  7359. BUILDIN_FUNC(getpartyleader)
  7360. {
  7361. int party_id, type = 0, i=0;
  7362. struct party_data *p;
  7363. party_id=script_getnum(st,2);
  7364. if( script_hasdata(st,3) )
  7365. type=script_getnum(st,3);
  7366. p=party_search(party_id);
  7367. if (p) //Search leader
  7368. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7369. if (!p || i == MAX_PARTY) { //leader not found
  7370. if (type)
  7371. script_pushint(st,-1);
  7372. else
  7373. script_pushconststr(st,"null");
  7374. return SCRIPT_CMD_SUCCESS;
  7375. }
  7376. switch (type) {
  7377. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7378. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7379. case 3: script_pushint(st,p->party.member[i].class_); break;
  7380. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7381. case 5: script_pushint(st,p->party.member[i].lv); break;
  7382. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7383. }
  7384. return SCRIPT_CMD_SUCCESS;
  7385. }
  7386. /*==========================================
  7387. * Return the name of the @guild_id
  7388. * null if not found
  7389. *------------------------------------------*/
  7390. BUILDIN_FUNC(getguildname)
  7391. {
  7392. int guild_id;
  7393. struct guild* g;
  7394. guild_id = script_getnum(st,2);
  7395. if( ( g = guild_search(guild_id) ) != NULL )
  7396. script_pushstrcopy(st,g->name);
  7397. else
  7398. script_pushconststr(st,"null");
  7399. return SCRIPT_CMD_SUCCESS;
  7400. }
  7401. /*==========================================
  7402. * Return the name of the guild master of @guild_id
  7403. * null if not found
  7404. *------------------------------------------*/
  7405. BUILDIN_FUNC(getguildmaster)
  7406. {
  7407. int guild_id;
  7408. struct guild* g;
  7409. guild_id = script_getnum(st,2);
  7410. if( ( g = guild_search(guild_id) ) != NULL )
  7411. script_pushstrcopy(st,g->member[0].name);
  7412. else
  7413. script_pushconststr(st,"null");
  7414. return SCRIPT_CMD_SUCCESS;
  7415. }
  7416. BUILDIN_FUNC(getguildmasterid)
  7417. {
  7418. int guild_id;
  7419. struct guild* g;
  7420. guild_id = script_getnum(st,2);
  7421. if( ( g = guild_search(guild_id) ) != NULL )
  7422. script_pushint(st,g->member[0].char_id);
  7423. else
  7424. script_pushint(st,0);
  7425. return SCRIPT_CMD_SUCCESS;
  7426. }
  7427. /*==========================================
  7428. * Get char string information by type :
  7429. * Return by @type :
  7430. * 0 : char_name
  7431. * 1 : party_name or ""
  7432. * 2 : guild_name or ""
  7433. * 3 : map_name
  7434. * - : ""
  7435. * strcharinfo(<type>{,<char_id>})
  7436. *------------------------------------------*/
  7437. BUILDIN_FUNC(strcharinfo)
  7438. {
  7439. TBL_PC *sd;
  7440. int num;
  7441. struct guild* g;
  7442. struct party_data* p;
  7443. if (!script_charid2sd(3,sd)) {
  7444. script_pushconststr(st,"");
  7445. return SCRIPT_CMD_FAILURE;
  7446. }
  7447. num=script_getnum(st,2);
  7448. switch(num){
  7449. case 0:
  7450. script_pushstrcopy(st,sd->status.name);
  7451. break;
  7452. case 1:
  7453. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7454. script_pushstrcopy(st,p->party.name);
  7455. } else {
  7456. script_pushconststr(st,"");
  7457. }
  7458. break;
  7459. case 2:
  7460. if( ( g = sd->guild ) != NULL ) {
  7461. script_pushstrcopy(st,g->name);
  7462. } else {
  7463. script_pushconststr(st,"");
  7464. }
  7465. break;
  7466. case 3:
  7467. script_pushconststr(st,map[sd->bl.m].name);
  7468. break;
  7469. default:
  7470. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7471. script_pushconststr(st,"");
  7472. break;
  7473. }
  7474. return SCRIPT_CMD_SUCCESS;
  7475. }
  7476. /*==========================================
  7477. * Get npc string information by type
  7478. * return by @type:
  7479. * 0 : name
  7480. * 1 : str#
  7481. * 2 : #str
  7482. * 3 : ::str
  7483. * 4 : map name
  7484. *------------------------------------------*/
  7485. BUILDIN_FUNC(strnpcinfo)
  7486. {
  7487. TBL_NPC* nd;
  7488. int num;
  7489. char *buf,*name=NULL;
  7490. nd = map_id2nd(st->oid);
  7491. if (!nd) {
  7492. script_pushconststr(st, "");
  7493. return SCRIPT_CMD_SUCCESS;
  7494. }
  7495. num = script_getnum(st,2);
  7496. switch(num){
  7497. case 0: // display name
  7498. name = aStrdup(nd->name);
  7499. break;
  7500. case 1: // visible part of display name
  7501. if((buf = strchr(nd->name,'#')) != NULL)
  7502. {
  7503. name = aStrdup(nd->name);
  7504. name[buf - nd->name] = 0;
  7505. } else // Return the name, there is no '#' present
  7506. name = aStrdup(nd->name);
  7507. break;
  7508. case 2: // # fragment
  7509. if((buf = strchr(nd->name,'#')) != NULL)
  7510. name = aStrdup(buf+1);
  7511. break;
  7512. case 3: // unique name
  7513. name = aStrdup(nd->exname);
  7514. break;
  7515. case 4: // map name
  7516. if (nd->bl.m >= 0)
  7517. name = aStrdup(map[nd->bl.m].name);
  7518. break;
  7519. }
  7520. if(name)
  7521. script_pushstr(st, name);
  7522. else
  7523. script_pushconststr(st, "");
  7524. return SCRIPT_CMD_SUCCESS;
  7525. }
  7526. /**
  7527. * getequipid({<equipment slot>,<char_id>})
  7528. **/
  7529. BUILDIN_FUNC(getequipid)
  7530. {
  7531. int i, num;
  7532. TBL_PC* sd;
  7533. if (!script_charid2sd(3, sd)) {
  7534. script_pushint(st,-1);
  7535. return SCRIPT_CMD_FAILURE;
  7536. }
  7537. if (script_hasdata(st, 2))
  7538. num = script_getnum(st, 2);
  7539. else
  7540. num = EQI_COMPOUND_ON;
  7541. if (num == EQI_COMPOUND_ON)
  7542. i = current_equip_item_index;
  7543. else if (equip_index_check(num)) // get inventory position of item
  7544. i = pc_checkequip(sd, equip_bitmask[num]);
  7545. else {
  7546. ShowError( "buildin_getequipid: Unknown equip index '%d'\n", num );
  7547. script_pushint(st,-1);
  7548. return SCRIPT_CMD_FAILURE;
  7549. }
  7550. if (i >= 0 && i < MAX_INVENTORY && sd->inventory_data[i])
  7551. script_pushint(st, sd->inventory_data[i]->nameid);
  7552. else
  7553. script_pushint(st, -1);
  7554. return SCRIPT_CMD_SUCCESS;
  7555. }
  7556. /*==========================================
  7557. * getequipuniqueid(<equipment slot>{,<char_id>})
  7558. *------------------------------------------*/
  7559. BUILDIN_FUNC(getequipuniqueid)
  7560. {
  7561. int i, num;
  7562. TBL_PC* sd;
  7563. struct item* item;
  7564. if (!script_charid2sd(3, sd)) {
  7565. script_pushconststr(st, "");
  7566. return SCRIPT_CMD_FAILURE;
  7567. }
  7568. num = script_getnum(st,2);
  7569. if ( !equip_index_check(num) ) {
  7570. script_pushconststr(st, "");
  7571. return SCRIPT_CMD_FAILURE;
  7572. }
  7573. // get inventory position of item
  7574. i = pc_checkequip(sd,equip_bitmask[num]);
  7575. if (i < 0) {
  7576. script_pushconststr(st, "");
  7577. return SCRIPT_CMD_FAILURE;
  7578. }
  7579. item = &sd->inventory.u.items_inventory[i];
  7580. if (item != 0) {
  7581. int maxlen = 256;
  7582. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7583. memset(buf, 0, maxlen);
  7584. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7585. script_pushstr(st, buf);
  7586. } else
  7587. script_pushconststr(st, "");
  7588. return SCRIPT_CMD_SUCCESS;
  7589. }
  7590. /*==========================================
  7591. * Get the equipement name at pos
  7592. * return item jname or ""
  7593. * getequipname(<equipment slot>{,<char_id>})
  7594. *------------------------------------------*/
  7595. BUILDIN_FUNC(getequipname)
  7596. {
  7597. int i, num;
  7598. TBL_PC* sd;
  7599. struct item_data* item;
  7600. if (!script_charid2sd(3, sd)) {
  7601. script_pushconststr(st, "");
  7602. return SCRIPT_CMD_FAILURE;
  7603. }
  7604. num = script_getnum(st,2);
  7605. if( !equip_index_check(num) )
  7606. {
  7607. script_pushconststr(st,"");
  7608. return SCRIPT_CMD_SUCCESS;
  7609. }
  7610. // get inventory position of item
  7611. i = pc_checkequip(sd,equip_bitmask[num]);
  7612. if( i < 0 )
  7613. {
  7614. script_pushconststr(st,"");
  7615. return SCRIPT_CMD_SUCCESS;
  7616. }
  7617. item = sd->inventory_data[i];
  7618. if( item != 0 )
  7619. script_pushstrcopy(st,item->jname);
  7620. else
  7621. script_pushconststr(st,"");
  7622. return SCRIPT_CMD_SUCCESS;
  7623. }
  7624. /*==========================================
  7625. * getbrokenid [Valaris]
  7626. * getbrokenid(<number>{,<char_id>})
  7627. *------------------------------------------*/
  7628. BUILDIN_FUNC(getbrokenid)
  7629. {
  7630. int i,num,id = 0,brokencounter = 0;
  7631. TBL_PC *sd;
  7632. if (!script_charid2sd(3, sd)) {
  7633. script_pushint(st,0);
  7634. return SCRIPT_CMD_FAILURE;
  7635. }
  7636. num = script_getnum(st,2);
  7637. for(i = 0; i < MAX_INVENTORY; i++) {
  7638. if(sd->inventory.u.items_inventory[i].attribute) {
  7639. brokencounter++;
  7640. if(num == brokencounter){
  7641. id = sd->inventory.u.items_inventory[i].nameid;
  7642. break;
  7643. }
  7644. }
  7645. }
  7646. script_pushint(st,id);
  7647. return SCRIPT_CMD_SUCCESS;
  7648. }
  7649. /*==========================================
  7650. * repair [Valaris]
  7651. * repair <num>{,<char_id>};
  7652. *------------------------------------------*/
  7653. BUILDIN_FUNC(repair)
  7654. {
  7655. int i,num;
  7656. int repaircounter = 0;
  7657. TBL_PC *sd;
  7658. if (!script_charid2sd(3,sd))
  7659. return SCRIPT_CMD_FAILURE;
  7660. num = script_getnum(st,2);
  7661. for(i = 0; i < MAX_INVENTORY; i++) {
  7662. if(sd->inventory.u.items_inventory[i].attribute) {
  7663. repaircounter++;
  7664. if(num == repaircounter) {
  7665. sd->inventory.u.items_inventory[i].attribute = 0;
  7666. clif_equiplist(sd);
  7667. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  7668. clif_misceffect(&sd->bl, 3);
  7669. break;
  7670. }
  7671. }
  7672. }
  7673. return SCRIPT_CMD_SUCCESS;
  7674. }
  7675. /*==========================================
  7676. * repairall {<char_id>}
  7677. *------------------------------------------*/
  7678. BUILDIN_FUNC(repairall)
  7679. {
  7680. int i, repaircounter = 0;
  7681. TBL_PC *sd;
  7682. if (!script_charid2sd(2,sd))
  7683. return SCRIPT_CMD_FAILURE;
  7684. for(i = 0; i < MAX_INVENTORY; i++)
  7685. {
  7686. if(sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].attribute)
  7687. {
  7688. sd->inventory.u.items_inventory[i].attribute = 0;
  7689. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  7690. repaircounter++;
  7691. }
  7692. }
  7693. if(repaircounter)
  7694. {
  7695. clif_misceffect(&sd->bl, 3);
  7696. clif_equiplist(sd);
  7697. }
  7698. return SCRIPT_CMD_SUCCESS;
  7699. }
  7700. /*==========================================
  7701. * Chk if player have something equiped at pos
  7702. * getequipisequiped <pos>{,<char_id>}
  7703. *------------------------------------------*/
  7704. BUILDIN_FUNC(getequipisequiped)
  7705. {
  7706. int i = -1,num;
  7707. TBL_PC *sd;
  7708. num = script_getnum(st,2);
  7709. if (!script_charid2sd(3,sd))
  7710. return SCRIPT_CMD_FAILURE;
  7711. if ( equip_index_check(num) )
  7712. i=pc_checkequip(sd,equip_bitmask[num]);
  7713. if(i >= 0)
  7714. script_pushint(st,1);
  7715. else
  7716. script_pushint(st,0);
  7717. return SCRIPT_CMD_SUCCESS;
  7718. }
  7719. /*==========================================
  7720. * Chk if the player have something equiped at pos
  7721. * if so chk if this item ain't marked not refinable or rental
  7722. * return (npc)
  7723. * 1 : true
  7724. * 0 : false
  7725. * getequipisenableref(<equipment slot>{,<char_id>})
  7726. *------------------------------------------*/
  7727. BUILDIN_FUNC(getequipisenableref)
  7728. {
  7729. int i = -1,num;
  7730. TBL_PC *sd;
  7731. num = script_getnum(st,2);
  7732. if (!script_charid2sd(3, sd)) {
  7733. script_pushint(st,0);
  7734. return SCRIPT_CMD_FAILURE;
  7735. }
  7736. if( equip_index_check(num) )
  7737. i = pc_checkequip(sd,equip_bitmask[num]);
  7738. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  7739. script_pushint(st,1);
  7740. else
  7741. script_pushint(st,0);
  7742. return SCRIPT_CMD_SUCCESS;
  7743. }
  7744. /*==========================================
  7745. * Get the item refined value at pos
  7746. * return (npc)
  7747. * x : refine amount
  7748. * 0 : false (not refined)
  7749. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7750. *------------------------------------------*/
  7751. BUILDIN_FUNC(getequiprefinerycnt)
  7752. {
  7753. int i = -1,num;
  7754. TBL_PC *sd;
  7755. num = script_getnum(st,2);
  7756. if (!script_charid2sd(3, sd)) {
  7757. script_pushint(st,0);
  7758. return SCRIPT_CMD_FAILURE;
  7759. }
  7760. if (equip_index_check(num))
  7761. i=pc_checkequip(sd,equip_bitmask[num]);
  7762. if(i >= 0)
  7763. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  7764. else
  7765. script_pushint(st,0);
  7766. return SCRIPT_CMD_SUCCESS;
  7767. }
  7768. /*==========================================
  7769. * Get the weapon level value at pos
  7770. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7771. * return (npc)
  7772. * x : weapon level
  7773. * 0 : false
  7774. * getequipweaponlv(<equipment slot>{,<char_id>})
  7775. *------------------------------------------*/
  7776. BUILDIN_FUNC(getequipweaponlv)
  7777. {
  7778. int i = -1, num;
  7779. TBL_PC *sd;
  7780. num = script_getnum(st, 2);
  7781. if (!script_charid2sd(3, sd)) {
  7782. script_pushint(st, 0);
  7783. return SCRIPT_CMD_FAILURE;
  7784. }
  7785. if (num == -1){
  7786. if( current_equip_item_index == -1 ){
  7787. script_pushint(st, 0);
  7788. return SCRIPT_CMD_FAILURE;
  7789. }
  7790. i = current_equip_item_index;
  7791. }else if (equip_index_check(num))
  7792. i = pc_checkequip(sd, equip_bitmask[num]);
  7793. if (i >= 0 && sd->inventory_data[i])
  7794. script_pushint(st, sd->inventory_data[i]->wlv);
  7795. else
  7796. script_pushint(st, 0);
  7797. return SCRIPT_CMD_SUCCESS;
  7798. }
  7799. /*==========================================
  7800. * Get the item refine chance (from refine.txt) for item at pos
  7801. * return (npc)
  7802. * x : refine chance
  7803. * 0 : false (max refine level or unequip..)
  7804. * getequippercentrefinery(<equipment slot>{,<char_id>})
  7805. *------------------------------------------*/
  7806. BUILDIN_FUNC(getequippercentrefinery)
  7807. {
  7808. int i = -1,num;
  7809. TBL_PC *sd;
  7810. num = script_getnum(st,2);
  7811. if (!script_charid2sd(3, sd)) {
  7812. script_pushint(st,0);
  7813. return SCRIPT_CMD_FAILURE;
  7814. }
  7815. if (equip_index_check(num))
  7816. i = pc_checkequip(sd,equip_bitmask[num]);
  7817. if(i >= 0 && sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].refine < MAX_REFINE)
  7818. 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));
  7819. else
  7820. script_pushint(st,0);
  7821. return SCRIPT_CMD_SUCCESS;
  7822. }
  7823. /*==========================================
  7824. * Refine +1 item at pos and log and display refine
  7825. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7826. *------------------------------------------*/
  7827. BUILDIN_FUNC(successrefitem) {
  7828. short i = -1, up = 1;
  7829. int pos;
  7830. TBL_PC *sd;
  7831. pos = script_getnum(st,2);
  7832. if (!script_charid2sd(4, sd)) {
  7833. script_pushint(st, -1);
  7834. return SCRIPT_CMD_FAILURE;
  7835. }
  7836. if (script_hasdata(st, 3))
  7837. up = script_getnum(st, 3);
  7838. if (equip_index_check(pos))
  7839. i = pc_checkequip(sd,equip_bitmask[pos]);
  7840. if (i >= 0) {
  7841. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7842. //Logs items, got from (N)PC scripts [Lupus]
  7843. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7844. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  7845. script_pushint(st, MAX_REFINE);
  7846. return SCRIPT_CMD_SUCCESS;
  7847. }
  7848. sd->inventory.u.items_inventory[i].refine += up;
  7849. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7850. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7851. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  7852. clif_delitem(sd,i,1,3);
  7853. //Logs items, got from (N)PC scripts [Lupus]
  7854. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7855. clif_additem(sd,i,1,0);
  7856. pc_equipitem(sd,i,ep);
  7857. clif_misceffect(&sd->bl,3);
  7858. achievement_update_objective(sd, AG_REFINE_SUCCESS, 2, sd->inventory_data[i]->wlv, sd->inventory.u.items_inventory[i].refine);
  7859. if (sd->inventory.u.items_inventory[i].refine == MAX_REFINE &&
  7860. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  7861. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]))
  7862. { // Fame point system [DracoRPG]
  7863. switch (sd->inventory_data[i]->wlv){
  7864. case 1:
  7865. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7866. break;
  7867. case 2:
  7868. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7869. break;
  7870. case 3:
  7871. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7872. break;
  7873. }
  7874. }
  7875. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7876. return SCRIPT_CMD_SUCCESS;
  7877. }
  7878. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7879. script_pushint(st, -1);
  7880. return SCRIPT_CMD_FAILURE;
  7881. }
  7882. /*==========================================
  7883. * Show a failed Refine +1 attempt
  7884. * failedrefitem <equipment slot>{,<char_id>}
  7885. *------------------------------------------*/
  7886. BUILDIN_FUNC(failedrefitem) {
  7887. short i = -1;
  7888. int pos;
  7889. TBL_PC *sd;
  7890. pos = script_getnum(st,2);
  7891. if (!script_charid2sd(3, sd)) {
  7892. script_pushint(st, 0);
  7893. return SCRIPT_CMD_FAILURE;
  7894. }
  7895. if (equip_index_check(pos))
  7896. i = pc_checkequip(sd,equip_bitmask[pos]);
  7897. if (i >= 0) {
  7898. sd->inventory.u.items_inventory[i].refine = 0;
  7899. pc_unequipitem(sd,i,3); //recalculate bonus
  7900. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  7901. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7902. clif_misceffect(&sd->bl,2); // display failure effect
  7903. achievement_update_objective(sd, AG_REFINE_FAIL, 1, 1);
  7904. script_pushint(st, 1);
  7905. return SCRIPT_CMD_SUCCESS;
  7906. }
  7907. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7908. script_pushint(st, 0);
  7909. return SCRIPT_CMD_FAILURE;
  7910. }
  7911. /*==========================================
  7912. * Downgrades an Equipment Part by -1 . [Masao]
  7913. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7914. *------------------------------------------*/
  7915. BUILDIN_FUNC(downrefitem) {
  7916. short i = -1, down = 1;
  7917. int pos;
  7918. TBL_PC *sd;
  7919. if (!script_charid2sd(4, sd)) {
  7920. script_pushint(st, -1);
  7921. return SCRIPT_CMD_FAILURE;
  7922. }
  7923. pos = script_getnum(st,2);
  7924. if (script_hasdata(st, 3))
  7925. down = script_getnum(st, 3);
  7926. if (equip_index_check(pos))
  7927. i = pc_checkequip(sd,equip_bitmask[pos]);
  7928. if (i >= 0) {
  7929. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7930. //Logs items, got from (N)PC scripts [Lupus]
  7931. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7932. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7933. sd->inventory.u.items_inventory[i].refine -= down;
  7934. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7935. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  7936. clif_delitem(sd,i,1,3);
  7937. //Logs items, got from (N)PC scripts [Lupus]
  7938. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7939. clif_additem(sd,i,1,0);
  7940. pc_equipitem(sd,i,ep);
  7941. clif_misceffect(&sd->bl,2);
  7942. achievement_update_objective(sd, AG_REFINE_FAIL, 1, sd->inventory.u.items_inventory[i].refine);
  7943. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7944. return SCRIPT_CMD_SUCCESS;
  7945. }
  7946. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7947. script_pushint(st, -1);
  7948. return SCRIPT_CMD_FAILURE;
  7949. }
  7950. /**
  7951. * Delete the item equipped at pos.
  7952. * delequip <equipment slot>{,<char_id>};
  7953. **/
  7954. BUILDIN_FUNC(delequip) {
  7955. short i = -1;
  7956. int pos;
  7957. int8 ret;
  7958. TBL_PC *sd;
  7959. pos = script_getnum(st,2);
  7960. if (!script_charid2sd(3,sd)) {
  7961. st->state = END;
  7962. return SCRIPT_CMD_FAILURE;
  7963. }
  7964. if (equip_index_check(pos))
  7965. i = pc_checkequip(sd,equip_bitmask[pos]);
  7966. if (i >= 0) {
  7967. pc_unequipitem(sd,i,3); //recalculate bonus
  7968. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7969. }
  7970. else {
  7971. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7972. st->state = END;
  7973. return SCRIPT_CMD_FAILURE;
  7974. }
  7975. script_pushint(st,ret);
  7976. return SCRIPT_CMD_SUCCESS;
  7977. }
  7978. /**
  7979. * Break the item equipped at pos.
  7980. * breakequip <equipment slot>{,<char_id>};
  7981. **/
  7982. BUILDIN_FUNC(breakequip) {
  7983. short i = -1;
  7984. int pos;
  7985. TBL_PC *sd;
  7986. pos = script_getnum(st,2);
  7987. if (!script_charid2sd(3,sd))
  7988. return SCRIPT_CMD_FAILURE;
  7989. if (equip_index_check(pos))
  7990. i = pc_checkequip(sd,equip_bitmask[pos]);
  7991. if (i >= 0) {
  7992. sd->inventory.u.items_inventory[i].attribute = 1;
  7993. pc_unequipitem(sd,i,3);
  7994. clif_equiplist(sd);
  7995. script_pushint(st,1);
  7996. return SCRIPT_CMD_SUCCESS;
  7997. }
  7998. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7999. script_pushint(st,0);
  8000. return SCRIPT_CMD_FAILURE;
  8001. }
  8002. /**
  8003. * statusup <stat>{,<char_id>};
  8004. **/
  8005. BUILDIN_FUNC(statusup)
  8006. {
  8007. int type;
  8008. TBL_PC *sd;
  8009. type = script_getnum(st,2);
  8010. if (!script_charid2sd(3, sd))
  8011. return SCRIPT_CMD_FAILURE;
  8012. pc_statusup(sd, type, 1);
  8013. return SCRIPT_CMD_SUCCESS;
  8014. }
  8015. /**
  8016. * statusup2 <stat>,<amount>{,<char_id>};
  8017. **/
  8018. BUILDIN_FUNC(statusup2)
  8019. {
  8020. int type,val;
  8021. TBL_PC *sd;
  8022. type=script_getnum(st,2);
  8023. val=script_getnum(st,3);
  8024. if (!script_charid2sd(4, sd))
  8025. return SCRIPT_CMD_FAILURE;
  8026. pc_statusup2(sd,type,val);
  8027. return SCRIPT_CMD_SUCCESS;
  8028. }
  8029. /// See 'doc/item_bonus.txt'
  8030. ///
  8031. /// bonus <bonus type>,<val1>;
  8032. /// bonus2 <bonus type>,<val1>,<val2>;
  8033. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  8034. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  8035. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  8036. BUILDIN_FUNC(bonus)
  8037. {
  8038. int type;
  8039. int val1;
  8040. int val2 = 0;
  8041. int val3 = 0;
  8042. int val4 = 0;
  8043. int val5 = 0;
  8044. TBL_PC* sd;
  8045. struct script_data *data;
  8046. if( !script_rid2sd(sd) )
  8047. return SCRIPT_CMD_SUCCESS; // no player attached
  8048. type = script_getnum(st,2);
  8049. switch( type ) {
  8050. case SP_AUTOSPELL:
  8051. case SP_AUTOSPELL_WHENHIT:
  8052. case SP_AUTOSPELL_ONSKILL:
  8053. case SP_SKILL_ATK:
  8054. case SP_SKILL_HEAL:
  8055. case SP_SKILL_HEAL2:
  8056. case SP_ADD_SKILL_BLOW:
  8057. case SP_CASTRATE:
  8058. case SP_ADDEFF_ONSKILL:
  8059. case SP_SKILL_USE_SP_RATE:
  8060. case SP_SKILL_COOLDOWN:
  8061. case SP_SKILL_FIXEDCAST:
  8062. case SP_SKILL_VARIABLECAST:
  8063. case SP_VARCASTRATE:
  8064. case SP_FIXCASTRATE:
  8065. case SP_SKILL_USE_SP:
  8066. case SP_SUB_SKILL:
  8067. // these bonuses support skill names
  8068. data = script_getdata(st, 3);
  8069. get_val(st, data); // Convert into value in case of a variable
  8070. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8071. break;
  8072. default:
  8073. val1 = script_getnum(st,3);
  8074. break;
  8075. }
  8076. switch( script_lastdata(st)-2 ) {
  8077. case 1:
  8078. pc_bonus(sd, type, val1);
  8079. break;
  8080. case 2:
  8081. val2 = script_getnum(st,4);
  8082. pc_bonus2(sd, type, val1, val2);
  8083. break;
  8084. case 3:
  8085. val2 = script_getnum(st,4);
  8086. val3 = script_getnum(st,5);
  8087. pc_bonus3(sd, type, val1, val2, val3);
  8088. break;
  8089. case 4:
  8090. data = script_getdata(st, 4);
  8091. get_val(st, data); // Convert into value in case of a variable
  8092. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  8093. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8094. else
  8095. val2 = script_getnum(st,4);
  8096. val3 = script_getnum(st,5);
  8097. val4 = script_getnum(st,6);
  8098. pc_bonus4(sd, type, val1, val2, val3, val4);
  8099. break;
  8100. case 5:
  8101. data = script_getdata(st, 4);
  8102. get_val(st, data); // Convert into value in case of a variable
  8103. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  8104. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  8105. else
  8106. val2 = script_getnum(st,4);
  8107. val3 = script_getnum(st,5);
  8108. val4 = script_getnum(st,6);
  8109. val5 = script_getnum(st,7);
  8110. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  8111. break;
  8112. default:
  8113. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  8114. break;
  8115. }
  8116. return SCRIPT_CMD_SUCCESS;
  8117. }
  8118. BUILDIN_FUNC(autobonus)
  8119. {
  8120. unsigned int dur, pos;
  8121. short rate;
  8122. short atk_type = 0;
  8123. TBL_PC* sd;
  8124. const char *bonus_script, *other_script = NULL;
  8125. if( !script_rid2sd(sd) )
  8126. return SCRIPT_CMD_SUCCESS; // no player attached
  8127. if (current_equip_combo_pos)
  8128. pos = current_equip_combo_pos;
  8129. else
  8130. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8131. if((sd->state.autobonus&pos) == pos)
  8132. return SCRIPT_CMD_SUCCESS;
  8133. rate = script_getnum(st,3);
  8134. dur = script_getnum(st,4);
  8135. bonus_script = script_getstr(st,2);
  8136. if( !rate || !dur || !pos || !bonus_script )
  8137. return SCRIPT_CMD_SUCCESS;
  8138. if( script_hasdata(st,5) )
  8139. atk_type = script_getnum(st,5);
  8140. if( script_hasdata(st,6) )
  8141. other_script = script_getstr(st,6);
  8142. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  8143. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  8144. {
  8145. script_add_autobonus(bonus_script);
  8146. if( other_script )
  8147. script_add_autobonus(other_script);
  8148. }
  8149. return SCRIPT_CMD_SUCCESS;
  8150. }
  8151. BUILDIN_FUNC(autobonus2)
  8152. {
  8153. unsigned int dur, pos;
  8154. short rate;
  8155. short atk_type = 0;
  8156. TBL_PC* sd;
  8157. const char *bonus_script, *other_script = NULL;
  8158. if( !script_rid2sd(sd) )
  8159. return SCRIPT_CMD_SUCCESS; // no player attached
  8160. if (current_equip_combo_pos)
  8161. pos = current_equip_combo_pos;
  8162. else
  8163. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8164. if((sd->state.autobonus&pos) == pos)
  8165. return SCRIPT_CMD_SUCCESS;
  8166. rate = script_getnum(st,3);
  8167. dur = script_getnum(st,4);
  8168. bonus_script = script_getstr(st,2);
  8169. if( !rate || !dur || !pos || !bonus_script )
  8170. return SCRIPT_CMD_SUCCESS;
  8171. if( script_hasdata(st,5) )
  8172. atk_type = script_getnum(st,5);
  8173. if( script_hasdata(st,6) )
  8174. other_script = script_getstr(st,6);
  8175. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  8176. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  8177. {
  8178. script_add_autobonus(bonus_script);
  8179. if( other_script )
  8180. script_add_autobonus(other_script);
  8181. }
  8182. return SCRIPT_CMD_SUCCESS;
  8183. }
  8184. BUILDIN_FUNC(autobonus3)
  8185. {
  8186. unsigned int dur, pos;
  8187. short rate,atk_type;
  8188. TBL_PC* sd;
  8189. const char *bonus_script, *other_script = NULL;
  8190. struct script_data *data;
  8191. if( !script_rid2sd(sd) )
  8192. return SCRIPT_CMD_SUCCESS; // no player attached
  8193. if (current_equip_combo_pos)
  8194. pos = current_equip_combo_pos;
  8195. else
  8196. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  8197. if((sd->state.autobonus&pos) == pos)
  8198. return SCRIPT_CMD_SUCCESS;
  8199. rate = script_getnum(st,3);
  8200. dur = script_getnum(st,4);
  8201. data = script_getdata(st, 5);
  8202. get_val(st, data); // Convert into value in case of a variable
  8203. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  8204. bonus_script = script_getstr(st,2);
  8205. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  8206. return SCRIPT_CMD_SUCCESS;
  8207. if( script_hasdata(st,6) )
  8208. other_script = script_getstr(st,6);
  8209. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  8210. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  8211. {
  8212. script_add_autobonus(bonus_script);
  8213. if( other_script )
  8214. script_add_autobonus(other_script);
  8215. }
  8216. return SCRIPT_CMD_SUCCESS;
  8217. }
  8218. /// Changes the level of a player skill.
  8219. /// <flag> defaults to 1
  8220. /// <flag>=0 : set the level of the skill
  8221. /// <flag>=1 : set the temporary level of the skill
  8222. /// <flag>=2 : add to the level of the skill
  8223. ///
  8224. /// skill <skill id>,<level>,<flag>
  8225. /// skill <skill id>,<level>
  8226. /// skill "<skill name>",<level>,<flag>
  8227. /// skill "<skill name>",<level>
  8228. BUILDIN_FUNC(skill)
  8229. {
  8230. int id;
  8231. int level;
  8232. int flag = ADDSKILL_TEMP;
  8233. TBL_PC* sd;
  8234. struct script_data *data;
  8235. const char* command = script_getfuncname(st);
  8236. if( !script_rid2sd(sd) )
  8237. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8238. if (strcmpi(command, "addtoskill") == 0)
  8239. flag = ADDSKILL_TEMP_ADDLEVEL;
  8240. data = script_getdata(st, 2);
  8241. get_val(st, data); // Convert into value in case of a variable
  8242. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8243. level = script_getnum(st,3);
  8244. if( script_hasdata(st,4) )
  8245. flag = script_getnum(st,4);
  8246. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8247. return SCRIPT_CMD_SUCCESS;
  8248. }
  8249. /// Increases the level of a guild skill.
  8250. ///
  8251. /// guildskill <skill id>,<amount>;
  8252. /// guildskill "<skill name>",<amount>;
  8253. BUILDIN_FUNC(guildskill)
  8254. {
  8255. int id;
  8256. int level;
  8257. TBL_PC* sd;
  8258. int i;
  8259. struct script_data *data;
  8260. if( !script_rid2sd(sd) )
  8261. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8262. data = script_getdata(st, 2);
  8263. get_val(st, data); // Convert into value in case of a variable
  8264. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8265. level = script_getnum(st,3);
  8266. for( i=0; i < level; i++ )
  8267. guild_skillup(sd, id);
  8268. return SCRIPT_CMD_SUCCESS;
  8269. }
  8270. /// Returns the level of the player skill.
  8271. ///
  8272. /// getskilllv(<skill id>) -> <level>
  8273. /// getskilllv("<skill name>") -> <level>
  8274. BUILDIN_FUNC(getskilllv)
  8275. {
  8276. int id;
  8277. TBL_PC* sd;
  8278. struct script_data *data;
  8279. if( !script_rid2sd(sd) )
  8280. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8281. data = script_getdata(st, 2);
  8282. get_val(st, data); // Convert into value in case of a variable
  8283. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8284. script_pushint(st, pc_checkskill(sd,id));
  8285. return SCRIPT_CMD_SUCCESS;
  8286. }
  8287. /// Returns the level of the guild skill.
  8288. ///
  8289. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8290. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8291. BUILDIN_FUNC(getgdskilllv)
  8292. {
  8293. int guild_id;
  8294. uint16 skill_id;
  8295. struct guild* g;
  8296. struct script_data *data;
  8297. guild_id = script_getnum(st,2);
  8298. data = script_getdata(st, 3);
  8299. get_val(st, data); // Convert into value in case of a variable
  8300. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8301. g = guild_search(guild_id);
  8302. if( g == NULL )
  8303. script_pushint(st, -1);
  8304. else
  8305. script_pushint(st, guild_checkskill(g,skill_id));
  8306. return SCRIPT_CMD_SUCCESS;
  8307. }
  8308. /// Returns the 'basic_skill_check' setting.
  8309. /// This config determines if the server checks the skill level of NV_BASIC
  8310. /// before allowing the basic actions.
  8311. ///
  8312. /// basicskillcheck() -> <bool>
  8313. BUILDIN_FUNC(basicskillcheck)
  8314. {
  8315. script_pushint(st, battle_config.basic_skill_check);
  8316. return SCRIPT_CMD_SUCCESS;
  8317. }
  8318. /// Returns the GM level of the player.
  8319. ///
  8320. /// getgmlevel({<char_id>}) -> <level>
  8321. BUILDIN_FUNC(getgmlevel)
  8322. {
  8323. TBL_PC* sd;
  8324. if (!script_charid2sd(2,sd))
  8325. return SCRIPT_CMD_FAILURE;
  8326. script_pushint(st, pc_get_group_level(sd));
  8327. return SCRIPT_CMD_SUCCESS;
  8328. }
  8329. /// Returns the group ID of the player.
  8330. ///
  8331. /// getgroupid({<char_id>}) -> <int>
  8332. BUILDIN_FUNC(getgroupid)
  8333. {
  8334. TBL_PC* sd;
  8335. if (!script_charid2sd(2,sd))
  8336. return SCRIPT_CMD_FAILURE;
  8337. script_pushint(st, pc_get_group_id(sd));
  8338. return SCRIPT_CMD_SUCCESS;
  8339. }
  8340. /// Terminates the execution of this script instance.
  8341. ///
  8342. /// end
  8343. BUILDIN_FUNC(end)
  8344. {
  8345. TBL_PC* sd;
  8346. sd = map_id2sd(st->rid);
  8347. st->state = END;
  8348. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  8349. int i;
  8350. for(i = 0; i < st->stack->sp; i++) {
  8351. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  8352. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  8353. st->script = ri->script;
  8354. break;
  8355. }
  8356. }
  8357. }
  8358. if( st->mes_active )
  8359. st->mes_active = 0;
  8360. if (sd){
  8361. if (sd->state.callshop == 0)
  8362. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8363. else
  8364. sd->state.callshop = 0;
  8365. }
  8366. return SCRIPT_CMD_SUCCESS;
  8367. }
  8368. /// Checks if the player has that effect state (option).
  8369. ///
  8370. /// checkoption(<option>{,<char_id>}) -> <bool>
  8371. BUILDIN_FUNC(checkoption)
  8372. {
  8373. int option;
  8374. TBL_PC* sd;
  8375. if (!script_charid2sd(3,sd))
  8376. return SCRIPT_CMD_FAILURE;
  8377. option = script_getnum(st,2);
  8378. if( sd->sc.option&option )
  8379. script_pushint(st, 1);
  8380. else
  8381. script_pushint(st, 0);
  8382. return SCRIPT_CMD_SUCCESS;
  8383. }
  8384. /// Checks if the player is in that body state (opt1).
  8385. ///
  8386. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8387. BUILDIN_FUNC(checkoption1)
  8388. {
  8389. int opt1;
  8390. TBL_PC* sd;
  8391. if (!script_charid2sd(3,sd))
  8392. return SCRIPT_CMD_FAILURE;
  8393. opt1 = script_getnum(st,2);
  8394. if( sd->sc.opt1 == opt1 )
  8395. script_pushint(st, 1);
  8396. else
  8397. script_pushint(st, 0);
  8398. return SCRIPT_CMD_SUCCESS;
  8399. }
  8400. /// Checks if the player has that health state (opt2).
  8401. ///
  8402. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8403. BUILDIN_FUNC(checkoption2)
  8404. {
  8405. int opt2;
  8406. TBL_PC* sd;
  8407. if (!script_charid2sd(3,sd))
  8408. return SCRIPT_CMD_FAILURE;
  8409. opt2 = script_getnum(st,2);
  8410. if( sd->sc.opt2&opt2 )
  8411. script_pushint(st, 1);
  8412. else
  8413. script_pushint(st, 0);
  8414. return SCRIPT_CMD_SUCCESS;
  8415. }
  8416. /// Changes the effect state (option) of the player.
  8417. /// <flag> defaults to 1
  8418. /// <flag>=0 : removes the option
  8419. /// <flag>=other : adds the option
  8420. ///
  8421. /// setoption <option>{,<flag>{,<char_id>}};
  8422. BUILDIN_FUNC(setoption)
  8423. {
  8424. int option;
  8425. int flag = 1;
  8426. TBL_PC* sd;
  8427. if (!script_charid2sd(4,sd))
  8428. return SCRIPT_CMD_FAILURE;
  8429. option = script_getnum(st,2);
  8430. if( script_hasdata(st,3) )
  8431. flag = script_getnum(st,3);
  8432. else if( !option ){// Request to remove everything.
  8433. flag = 0;
  8434. option = OPTION_FALCON|OPTION_RIDING;
  8435. #ifndef NEW_CARTS
  8436. option |= OPTION_CART;
  8437. #endif
  8438. }
  8439. if( flag ){// Add option
  8440. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8441. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8442. pc_setoption(sd, sd->sc.option|option);
  8443. } else// Remove option
  8444. pc_setoption(sd, sd->sc.option&~option);
  8445. return SCRIPT_CMD_SUCCESS;
  8446. }
  8447. /// Returns if the player has a cart.
  8448. ///
  8449. /// checkcart({char_id}) -> <bool>
  8450. ///
  8451. /// @author Valaris
  8452. BUILDIN_FUNC(checkcart)
  8453. {
  8454. TBL_PC* sd;
  8455. if (!script_charid2sd(2,sd))
  8456. return SCRIPT_CMD_FAILURE;
  8457. if( pc_iscarton(sd) )
  8458. script_pushint(st, 1);
  8459. else
  8460. script_pushint(st, 0);
  8461. return SCRIPT_CMD_SUCCESS;
  8462. }
  8463. /// Sets the cart of the player.
  8464. /// <type> defaults to 1
  8465. /// <type>=0 : removes the cart
  8466. /// <type>=1 : Normal cart
  8467. /// <type>=2 : Wooden cart
  8468. /// <type>=3 : Covered cart with flowers and ferns
  8469. /// <type>=4 : Wooden cart with a Panda doll on the back
  8470. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8471. ///
  8472. /// setcart {<type>{,<char_id>}};
  8473. BUILDIN_FUNC(setcart)
  8474. {
  8475. int type = 1;
  8476. TBL_PC* sd;
  8477. if (!script_charid2sd(3,sd))
  8478. return SCRIPT_CMD_FAILURE;
  8479. if( script_hasdata(st,2) )
  8480. type = script_getnum(st,2);
  8481. pc_setcart(sd, type);
  8482. return SCRIPT_CMD_SUCCESS;
  8483. }
  8484. /// Returns if the player has a falcon.
  8485. ///
  8486. /// checkfalcon({char_id}) -> <bool>
  8487. ///
  8488. /// @author Valaris
  8489. BUILDIN_FUNC(checkfalcon)
  8490. {
  8491. TBL_PC* sd;
  8492. if (!script_charid2sd(2,sd))
  8493. return SCRIPT_CMD_FAILURE;
  8494. if( pc_isfalcon(sd) )
  8495. script_pushint(st, 1);
  8496. else
  8497. script_pushint(st, 0);
  8498. return SCRIPT_CMD_SUCCESS;
  8499. }
  8500. /// Sets if the player has a falcon or not.
  8501. /// <flag> defaults to 1
  8502. ///
  8503. /// setfalcon {<flag>{,<char_id>}};
  8504. BUILDIN_FUNC(setfalcon)
  8505. {
  8506. int flag = 1;
  8507. TBL_PC* sd;
  8508. if (!script_charid2sd(3,sd))
  8509. return SCRIPT_CMD_FAILURE;
  8510. if( script_hasdata(st,2) )
  8511. flag = script_getnum(st,2);
  8512. pc_setfalcon(sd, flag);
  8513. return SCRIPT_CMD_SUCCESS;
  8514. }
  8515. /// Returns if the player is riding.
  8516. ///
  8517. /// checkriding({char_id}) -> <bool>
  8518. ///
  8519. /// @author Valaris
  8520. BUILDIN_FUNC(checkriding)
  8521. {
  8522. TBL_PC* sd;
  8523. if (!script_charid2sd(2,sd))
  8524. return SCRIPT_CMD_FAILURE;
  8525. if( pc_isriding(sd) )
  8526. script_pushint(st, 1);
  8527. else
  8528. script_pushint(st, 0);
  8529. return SCRIPT_CMD_SUCCESS;
  8530. }
  8531. /// Sets if the player is riding.
  8532. /// <flag> defaults to 1
  8533. ///
  8534. /// setriding {<flag>{,<char_id>}};
  8535. BUILDIN_FUNC(setriding)
  8536. {
  8537. int flag = 1;
  8538. TBL_PC* sd;
  8539. if (!script_charid2sd(3,sd))
  8540. return SCRIPT_CMD_FAILURE;
  8541. if( script_hasdata(st,2) )
  8542. flag = script_getnum(st,2);
  8543. pc_setriding(sd, flag);
  8544. return SCRIPT_CMD_SUCCESS;
  8545. }
  8546. /// Returns if the player has a warg.
  8547. ///
  8548. /// checkwug({char_id}) -> <bool>
  8549. ///
  8550. BUILDIN_FUNC(checkwug)
  8551. {
  8552. TBL_PC* sd;
  8553. if (!script_charid2sd(2,sd))
  8554. return SCRIPT_CMD_FAILURE;
  8555. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8556. script_pushint(st, 1);
  8557. else
  8558. script_pushint(st, 0);
  8559. return SCRIPT_CMD_SUCCESS;
  8560. }
  8561. /// Returns if the player is wearing MADO Gear.
  8562. ///
  8563. /// checkmadogear({<char_id>}) -> <bool>
  8564. ///
  8565. BUILDIN_FUNC(checkmadogear)
  8566. {
  8567. TBL_PC* sd;
  8568. if (!script_charid2sd(2,sd))
  8569. return SCRIPT_CMD_FAILURE;
  8570. if( pc_ismadogear(sd) )
  8571. script_pushint(st, 1);
  8572. else
  8573. script_pushint(st, 0);
  8574. return SCRIPT_CMD_SUCCESS;
  8575. }
  8576. /// Sets if the player is riding MADO Gear.
  8577. /// <flag> defaults to 1
  8578. ///
  8579. /// setmadogear {<flag>{,<char_id>}};
  8580. BUILDIN_FUNC(setmadogear)
  8581. {
  8582. int flag = 1;
  8583. TBL_PC* sd;
  8584. if (!script_charid2sd(3,sd))
  8585. return SCRIPT_CMD_FAILURE;
  8586. if( script_hasdata(st,2) )
  8587. flag = script_getnum(st,2);
  8588. pc_setmadogear(sd, flag);
  8589. return SCRIPT_CMD_SUCCESS;
  8590. }
  8591. /// Sets the save point of the player.
  8592. ///
  8593. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8594. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8595. BUILDIN_FUNC(savepoint)
  8596. {
  8597. int x, y, m, cid_pos = 5;
  8598. unsigned short map_idx;
  8599. const char* str;
  8600. TBL_PC* sd;
  8601. if (script_lastdata(st) > 5)
  8602. cid_pos = 7;
  8603. if (!script_charid2sd(cid_pos,sd))
  8604. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8605. str = script_getstr(st, 2);
  8606. map_idx = mapindex_name2id(str);
  8607. if( !map_idx )
  8608. return SCRIPT_CMD_FAILURE;
  8609. x = script_getnum(st,3);
  8610. y = script_getnum(st,4);
  8611. m = map_mapindex2mapid(map_idx);
  8612. if (cid_pos == 7) {
  8613. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  8614. x1 = x + dx, y1 = y + dy,
  8615. x0 = x - dx, y0 = y - dy;
  8616. uint8 n = 10;
  8617. do {
  8618. x = x0 + rnd()%(x1-x0+1);
  8619. y = y0 + rnd()%(y1-y0+1);
  8620. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  8621. }
  8622. // Check for valid coordinates if map in local map-server
  8623. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  8624. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  8625. return SCRIPT_CMD_FAILURE;
  8626. }
  8627. pc_setsavepoint(sd, map_idx, x, y);
  8628. return SCRIPT_CMD_SUCCESS;
  8629. }
  8630. /*==========================================
  8631. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  8632. *------------------------------------------*/
  8633. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8634. {
  8635. int type;
  8636. time_t timer;
  8637. struct tm *t;
  8638. type=script_getnum(st,2);
  8639. switch(type){
  8640. case 2:
  8641. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8642. // from the system clock.)
  8643. script_pushint(st,(int)time(NULL));
  8644. break;
  8645. case 1:
  8646. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8647. time(&timer);
  8648. t=localtime(&timer);
  8649. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8650. break;
  8651. case 0:
  8652. default:
  8653. //type 0:(System Ticks)
  8654. script_pushint(st,gettick());
  8655. break;
  8656. }
  8657. return SCRIPT_CMD_SUCCESS;
  8658. }
  8659. /*==========================================
  8660. * GetTime(Type)
  8661. *
  8662. * Returns the current value of a certain date type.
  8663. * Possible types are:
  8664. * - DT_SECOND Current seconds
  8665. * - DT_MINUTE Current minute
  8666. * - DT_HOUR Current hour
  8667. * - DT_DAYOFWEEK Day of current week
  8668. * - DT_DAYOFMONTH Day of current month
  8669. * - DT_MONTH Current month
  8670. * - DT_YEAR Current year
  8671. * - DT_DAYOFYEAR Day of current year
  8672. *
  8673. * If none of the above types is supplied -1 will be returned to the script
  8674. * and the script source will be reported into the mapserver console.
  8675. *------------------------------------------*/
  8676. BUILDIN_FUNC(gettime)
  8677. {
  8678. int type;
  8679. type = script_getnum(st,2);
  8680. if( type <= DT_MIN || type >= DT_MAX ){
  8681. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  8682. script_reportsrc(st);
  8683. script_pushint(st,-1);
  8684. }else{
  8685. script_pushint(st,date_get((enum e_date_type)type));
  8686. }
  8687. return SCRIPT_CMD_SUCCESS;
  8688. }
  8689. /*==========================================
  8690. * GetTimeStr("TimeFMT", Length);
  8691. *------------------------------------------*/
  8692. BUILDIN_FUNC(gettimestr)
  8693. {
  8694. char *tmpstr;
  8695. const char *fmtstr;
  8696. int maxlen;
  8697. time_t now = time(NULL);
  8698. fmtstr=script_getstr(st,2);
  8699. maxlen=script_getnum(st,3);
  8700. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8701. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8702. tmpstr[maxlen]='\0';
  8703. script_pushstr(st,tmpstr);
  8704. return SCRIPT_CMD_SUCCESS;
  8705. }
  8706. /*==========================================
  8707. * Open player storage
  8708. *------------------------------------------*/
  8709. BUILDIN_FUNC(openstorage)
  8710. {
  8711. TBL_PC* sd;
  8712. if( !script_rid2sd(sd) )
  8713. return SCRIPT_CMD_SUCCESS;
  8714. storage_storageopen(sd);
  8715. return SCRIPT_CMD_SUCCESS;
  8716. }
  8717. BUILDIN_FUNC(guildopenstorage)
  8718. {
  8719. TBL_PC* sd;
  8720. int ret;
  8721. if( !script_rid2sd(sd) )
  8722. return SCRIPT_CMD_SUCCESS;
  8723. ret = storage_guild_storageopen(sd);
  8724. script_pushint(st,ret);
  8725. return SCRIPT_CMD_SUCCESS;
  8726. }
  8727. /*==========================================
  8728. * Make player use a skill trought item usage
  8729. *------------------------------------------*/
  8730. /// itemskill <skill id>,<level>
  8731. /// itemskill "<skill name>",<level>
  8732. BUILDIN_FUNC(itemskill)
  8733. {
  8734. int id;
  8735. int lv;
  8736. bool keep_requirement;
  8737. TBL_PC* sd;
  8738. struct script_data *data;
  8739. if( !script_rid2sd(sd) || sd->ud.skilltimer != INVALID_TIMER )
  8740. return SCRIPT_CMD_SUCCESS;
  8741. data = script_getdata(st, 2);
  8742. get_val(st, data); // Convert into value in case of a variable
  8743. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8744. lv = script_getnum(st,3);
  8745. if (script_hasdata(st, 4)) {
  8746. keep_requirement = (script_getnum(st, 4) != 0);
  8747. } else {
  8748. keep_requirement = false;
  8749. }
  8750. sd->skillitem=id;
  8751. sd->skillitemlv=lv;
  8752. sd->skillitem_keep_requirement = keep_requirement;
  8753. clif_item_skill(sd,id,lv);
  8754. return SCRIPT_CMD_SUCCESS;
  8755. }
  8756. /*==========================================
  8757. * Attempt to create an item
  8758. *------------------------------------------*/
  8759. BUILDIN_FUNC(produce)
  8760. {
  8761. int trigger;
  8762. TBL_PC* sd;
  8763. if( !script_rid2sd(sd) )
  8764. return SCRIPT_CMD_SUCCESS;
  8765. trigger=script_getnum(st,2);
  8766. clif_skill_produce_mix_list(sd, -1, trigger);
  8767. return SCRIPT_CMD_SUCCESS;
  8768. }
  8769. /*==========================================
  8770. *
  8771. *------------------------------------------*/
  8772. BUILDIN_FUNC(cooking)
  8773. {
  8774. int trigger;
  8775. TBL_PC* sd;
  8776. if( !script_rid2sd(sd) )
  8777. return SCRIPT_CMD_SUCCESS;
  8778. trigger=script_getnum(st,2);
  8779. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8780. return SCRIPT_CMD_SUCCESS;
  8781. }
  8782. /*==========================================
  8783. * Create a pet
  8784. *------------------------------------------*/
  8785. BUILDIN_FUNC(makepet)
  8786. {
  8787. TBL_PC* sd;
  8788. int id,pet_id;
  8789. if( !script_rid2sd(sd) )
  8790. return SCRIPT_CMD_SUCCESS;
  8791. id=script_getnum(st,2);
  8792. pet_id = search_petDB_index(id, PET_CLASS);
  8793. if (pet_id < 0)
  8794. pet_id = search_petDB_index(id, PET_EGG);
  8795. if (pet_id >= 0 && sd) {
  8796. sd->catch_target_class = pet_db[pet_id].class_;
  8797. intif_create_pet(sd->status.account_id, sd->status.char_id, pet_db[pet_id].class_, mob_db(pet_db[pet_id].class_)->lv, pet_db[pet_id].EggID, 0, pet_db[pet_id].intimate, 100, 0, 1, pet_db[pet_id].jname);
  8798. }
  8799. return SCRIPT_CMD_SUCCESS;
  8800. }
  8801. /**
  8802. * Give player exp base,job * quest_exp_rate/100
  8803. * getexp <base xp>,<job xp>{,<char_id>};
  8804. **/
  8805. BUILDIN_FUNC(getexp)
  8806. {
  8807. TBL_PC* sd;
  8808. int base=0,job=0;
  8809. double bonus;
  8810. if (!script_charid2sd(4,sd))
  8811. return SCRIPT_CMD_FAILURE;
  8812. base=script_getnum(st,2);
  8813. job =script_getnum(st,3);
  8814. if(base<0 || job<0)
  8815. return SCRIPT_CMD_SUCCESS;
  8816. // bonus for npc-given exp
  8817. bonus = battle_config.quest_exp_rate / 100.;
  8818. if (base)
  8819. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8820. if (job)
  8821. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8822. pc_gainexp(sd, NULL, base, job, 1);
  8823. return SCRIPT_CMD_SUCCESS;
  8824. }
  8825. /*==========================================
  8826. * Gain guild exp [Celest]
  8827. *------------------------------------------*/
  8828. BUILDIN_FUNC(guildgetexp)
  8829. {
  8830. TBL_PC* sd;
  8831. int exp;
  8832. if( !script_rid2sd(sd) )
  8833. return SCRIPT_CMD_SUCCESS;
  8834. exp = script_getnum(st,2);
  8835. if(exp < 0)
  8836. return SCRIPT_CMD_SUCCESS;
  8837. if(sd && sd->status.guild_id > 0)
  8838. guild_getexp (sd, exp);
  8839. return SCRIPT_CMD_SUCCESS;
  8840. }
  8841. /*==========================================
  8842. * Changes the guild master of a guild [Skotlex]
  8843. *------------------------------------------*/
  8844. BUILDIN_FUNC(guildchangegm)
  8845. {
  8846. TBL_PC *sd;
  8847. int guild_id;
  8848. const char *name;
  8849. guild_id = script_getnum(st,2);
  8850. name = script_getstr(st,3);
  8851. sd=map_nick2sd(name,false);
  8852. if (!sd)
  8853. script_pushint(st,0);
  8854. else
  8855. script_pushint(st,guild_gm_change(guild_id, sd->status.char_id));
  8856. return SCRIPT_CMD_SUCCESS;
  8857. }
  8858. /*==========================================
  8859. * Spawn a monster:
  8860. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8861. *------------------------------------------*/
  8862. BUILDIN_FUNC(monster)
  8863. {
  8864. const char* mapn = script_getstr(st,2);
  8865. int x = script_getnum(st,3);
  8866. int y = script_getnum(st,4);
  8867. const char* str = script_getstr(st,5);
  8868. int class_ = script_getnum(st,6);
  8869. int amount = script_getnum(st,7);
  8870. const char* event = "";
  8871. unsigned int size = SZ_SMALL;
  8872. unsigned int ai = AI_NONE;
  8873. struct map_session_data* sd;
  8874. int16 m;
  8875. int i;
  8876. if (script_hasdata(st, 8)) {
  8877. event = script_getstr(st, 8);
  8878. check_event(st, event);
  8879. }
  8880. if (script_hasdata(st, 9)) {
  8881. size = script_getnum(st, 9);
  8882. if (size > SZ_BIG) {
  8883. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8884. return SCRIPT_CMD_FAILURE;
  8885. }
  8886. }
  8887. if (script_hasdata(st, 10)) {
  8888. ai = script_getnum(st, 10);
  8889. if (ai >= AI_MAX) {
  8890. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8891. return SCRIPT_CMD_FAILURE;
  8892. }
  8893. }
  8894. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8895. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8896. return SCRIPT_CMD_FAILURE;
  8897. }
  8898. sd = map_id2sd(st->rid);
  8899. if (sd && strcmp(mapn, "this") == 0)
  8900. m = sd->bl.m;
  8901. else
  8902. m = map_mapname2mapid(mapn);
  8903. for(i = 0; i < amount; i++) { //not optimised
  8904. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8905. if (mobid)
  8906. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8907. }
  8908. return SCRIPT_CMD_SUCCESS;
  8909. }
  8910. /*==========================================
  8911. * Request List of Monster Drops
  8912. *------------------------------------------*/
  8913. BUILDIN_FUNC(getmobdrops)
  8914. {
  8915. int class_ = script_getnum(st,2);
  8916. int i, j = 0;
  8917. struct mob_db *mob;
  8918. if( !mobdb_checkid(class_) )
  8919. {
  8920. script_pushint(st, 0);
  8921. return SCRIPT_CMD_SUCCESS;
  8922. }
  8923. mob = mob_db(class_);
  8924. for( i = 0; i < MAX_MOB_DROP_TOTAL; i++ )
  8925. {
  8926. if( mob->dropitem[i].nameid < 1 )
  8927. continue;
  8928. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8929. continue;
  8930. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8931. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8932. j++;
  8933. }
  8934. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8935. script_pushint(st, 1);
  8936. return SCRIPT_CMD_SUCCESS;
  8937. }
  8938. /*==========================================
  8939. * Spawn a monster in a random location
  8940. * in x0,x1,y0,y1 area.
  8941. *------------------------------------------*/
  8942. BUILDIN_FUNC(areamonster)
  8943. {
  8944. const char* mapn = script_getstr(st,2);
  8945. int x0 = script_getnum(st,3);
  8946. int y0 = script_getnum(st,4);
  8947. int x1 = script_getnum(st,5);
  8948. int y1 = script_getnum(st,6);
  8949. const char* str = script_getstr(st,7);
  8950. int class_ = script_getnum(st,8);
  8951. int amount = script_getnum(st,9);
  8952. const char* event = "";
  8953. unsigned int size = SZ_SMALL;
  8954. unsigned int ai = AI_NONE;
  8955. struct map_session_data* sd;
  8956. int16 m;
  8957. int i;
  8958. if (script_hasdata(st,10)) {
  8959. event = script_getstr(st, 10);
  8960. check_event(st, event);
  8961. }
  8962. if (script_hasdata(st, 11)) {
  8963. size = script_getnum(st, 11);
  8964. if (size > 3) {
  8965. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8966. return SCRIPT_CMD_FAILURE;
  8967. }
  8968. }
  8969. if (script_hasdata(st, 12)) {
  8970. ai = script_getnum(st, 12);
  8971. if (ai >= AI_MAX) {
  8972. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8973. return SCRIPT_CMD_FAILURE;
  8974. }
  8975. }
  8976. sd = map_id2sd(st->rid);
  8977. if (sd && strcmp(mapn, "this") == 0)
  8978. m = sd->bl.m;
  8979. else
  8980. m = map_mapname2mapid(mapn);
  8981. for(i = 0; i < amount; i++) { //not optimised
  8982. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8983. if (mobid)
  8984. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8985. }
  8986. return SCRIPT_CMD_SUCCESS;
  8987. }
  8988. /*==========================================
  8989. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8990. *------------------------------------------*/
  8991. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8992. { //same fix but with killmonster instead - stripping events from mobs.
  8993. TBL_MOB* md = (TBL_MOB*)bl;
  8994. char *event=va_arg(ap,char *);
  8995. int allflag=va_arg(ap,int);
  8996. md->state.npc_killmonster = 1;
  8997. if(!allflag){
  8998. if(strcmp(event,md->npc_event)==0)
  8999. status_kill(bl);
  9000. }else{
  9001. if(!md->spawn)
  9002. status_kill(bl);
  9003. }
  9004. md->state.npc_killmonster = 0;
  9005. return SCRIPT_CMD_SUCCESS;
  9006. }
  9007. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  9008. {
  9009. TBL_MOB* md = (TBL_MOB*)bl;
  9010. char *event=va_arg(ap,char *);
  9011. int allflag=va_arg(ap,int);
  9012. if(!allflag){
  9013. if(strcmp(event,md->npc_event)==0)
  9014. status_kill(bl);
  9015. }else{
  9016. if(!md->spawn)
  9017. status_kill(bl);
  9018. }
  9019. return SCRIPT_CMD_SUCCESS;
  9020. }
  9021. BUILDIN_FUNC(killmonster)
  9022. {
  9023. const char *mapname,*event;
  9024. int16 m,allflag=0;
  9025. mapname=script_getstr(st,2);
  9026. event=script_getstr(st,3);
  9027. if(strcmp(event,"All")==0)
  9028. allflag = 1;
  9029. else
  9030. check_event(st, event);
  9031. if( (m=map_mapname2mapid(mapname))<0 )
  9032. return SCRIPT_CMD_SUCCESS;
  9033. if( script_hasdata(st,4) ) {
  9034. if ( script_getnum(st,4) == 1 ) {
  9035. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  9036. return SCRIPT_CMD_SUCCESS;
  9037. }
  9038. }
  9039. map_freeblock_lock();
  9040. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  9041. map_freeblock_unlock();
  9042. return SCRIPT_CMD_SUCCESS;
  9043. }
  9044. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  9045. { //Strips the event from the mob if it's killed the old method.
  9046. struct mob_data *md;
  9047. md = BL_CAST(BL_MOB, bl);
  9048. if (md->npc_event[0])
  9049. md->npc_event[0] = 0;
  9050. status_kill(bl);
  9051. return 0;
  9052. }
  9053. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  9054. {
  9055. status_kill(bl);
  9056. return 0;
  9057. }
  9058. BUILDIN_FUNC(killmonsterall)
  9059. {
  9060. const char *mapname;
  9061. int16 m;
  9062. mapname=script_getstr(st,2);
  9063. if( (m = map_mapname2mapid(mapname))<0 )
  9064. return SCRIPT_CMD_SUCCESS;
  9065. if( script_hasdata(st,3) ) {
  9066. if ( script_getnum(st,3) == 1 ) {
  9067. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  9068. return SCRIPT_CMD_SUCCESS;
  9069. }
  9070. }
  9071. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  9072. return SCRIPT_CMD_SUCCESS;
  9073. }
  9074. /*==========================================
  9075. * Creates a clone of a player.
  9076. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  9077. *------------------------------------------*/
  9078. BUILDIN_FUNC(clone)
  9079. {
  9080. TBL_PC *sd, *msd=NULL;
  9081. uint32 char_id, master_id = 0, x, y, flag = 0;
  9082. int16 m;
  9083. enum e_mode mode = 0;
  9084. unsigned int duration = 0;
  9085. const char *mapname,*event;
  9086. mapname=script_getstr(st,2);
  9087. x=script_getnum(st,3);
  9088. y=script_getnum(st,4);
  9089. event=script_getstr(st,5);
  9090. char_id=script_getnum(st,6);
  9091. if( script_hasdata(st,7) )
  9092. master_id=script_getnum(st,7);
  9093. if( script_hasdata(st,8) )
  9094. mode=script_getnum(st,8);
  9095. if( script_hasdata(st,9) )
  9096. flag=script_getnum(st,9);
  9097. if( script_hasdata(st,10) )
  9098. duration=script_getnum(st,10);
  9099. check_event(st, event);
  9100. m = map_mapname2mapid(mapname);
  9101. if (m < 0)
  9102. return SCRIPT_CMD_SUCCESS;
  9103. sd = map_charid2sd(char_id);
  9104. if (master_id) {
  9105. msd = map_charid2sd(master_id);
  9106. if (msd)
  9107. master_id = msd->bl.id;
  9108. else
  9109. master_id = 0;
  9110. }
  9111. if (sd) //Return ID of newly crafted clone.
  9112. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  9113. else //Failed to create clone.
  9114. script_pushint(st,0);
  9115. return SCRIPT_CMD_SUCCESS;
  9116. }
  9117. /*==========================================
  9118. *------------------------------------------*/
  9119. BUILDIN_FUNC(doevent)
  9120. {
  9121. const char* event;
  9122. struct map_session_data* sd;
  9123. if( !script_rid2sd(sd) )
  9124. return SCRIPT_CMD_SUCCESS;
  9125. event = script_getstr(st,2);
  9126. check_event(st, event);
  9127. npc_event(sd, event, 0);
  9128. return SCRIPT_CMD_SUCCESS;
  9129. }
  9130. /*==========================================
  9131. *------------------------------------------*/
  9132. BUILDIN_FUNC(donpcevent)
  9133. {
  9134. const char* event = script_getstr(st,2);
  9135. check_event(st, event);
  9136. if( !npc_event_do(event) ) {
  9137. struct npc_data * nd = map_id2nd(st->oid);
  9138. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  9139. script_pushint(st, 0);
  9140. } else
  9141. script_pushint(st, 1);
  9142. return SCRIPT_CMD_SUCCESS;
  9143. }
  9144. /// for Aegis compatibility
  9145. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  9146. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  9147. {
  9148. const char* npc = script_getstr(st,2);
  9149. const char* command = script_getstr(st,3);
  9150. char event[EVENT_NAME_LENGTH];
  9151. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  9152. check_event(st, event);
  9153. if( npc_event_do(event) ){
  9154. script_pushint(st, true);
  9155. }else{
  9156. struct npc_data * nd = map_id2nd(st->oid);
  9157. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  9158. script_pushint(st, false);
  9159. }
  9160. return SCRIPT_CMD_SUCCESS;
  9161. }
  9162. /*==========================================
  9163. *------------------------------------------*/
  9164. BUILDIN_FUNC(addtimer)
  9165. {
  9166. int tick;
  9167. const char* event;
  9168. TBL_PC* sd;
  9169. if( !script_rid2sd(sd) )
  9170. return SCRIPT_CMD_SUCCESS;
  9171. tick = script_getnum(st,2);
  9172. event = script_getstr(st, 3);
  9173. check_event(st, event);
  9174. if (!pc_addeventtimer(sd,tick,event)) {
  9175. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  9176. return SCRIPT_CMD_FAILURE;
  9177. }
  9178. return SCRIPT_CMD_SUCCESS;
  9179. }
  9180. /*==========================================
  9181. *------------------------------------------*/
  9182. BUILDIN_FUNC(deltimer)
  9183. {
  9184. const char *event;
  9185. TBL_PC* sd;
  9186. if( !script_rid2sd(sd) )
  9187. return SCRIPT_CMD_SUCCESS;
  9188. event=script_getstr(st, 2);
  9189. check_event(st, event);
  9190. pc_deleventtimer(sd,event);
  9191. return SCRIPT_CMD_SUCCESS;
  9192. }
  9193. /*==========================================
  9194. *------------------------------------------*/
  9195. BUILDIN_FUNC(addtimercount)
  9196. {
  9197. const char *event;
  9198. int tick;
  9199. TBL_PC* sd;
  9200. if( !script_rid2sd(sd) )
  9201. return SCRIPT_CMD_SUCCESS;
  9202. tick=script_getnum(st,2);
  9203. event=script_getstr(st,3);
  9204. check_event(st, event);
  9205. pc_addeventtimercount(sd,event,tick);
  9206. return SCRIPT_CMD_SUCCESS;
  9207. }
  9208. /*==========================================
  9209. *------------------------------------------*/
  9210. BUILDIN_FUNC(initnpctimer)
  9211. {
  9212. struct npc_data *nd;
  9213. int flag = 0;
  9214. if( script_hasdata(st,3) )
  9215. { //Two arguments: NPC name and attach flag.
  9216. nd = npc_name2id(script_getstr(st, 2));
  9217. flag = script_getnum(st,3);
  9218. }
  9219. else if( script_hasdata(st,2) )
  9220. { //Check if argument is numeric (flag) or string (npc name)
  9221. struct script_data *data;
  9222. data = script_getdata(st,2);
  9223. get_val(st,data);
  9224. if( data_isstring(data) ) //NPC name
  9225. nd = npc_name2id(conv_str(st, data));
  9226. else if( data_isint(data) ) //Flag
  9227. {
  9228. nd = (struct npc_data *)map_id2bl(st->oid);
  9229. flag = conv_num(st,data);
  9230. }
  9231. else
  9232. {
  9233. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9234. return SCRIPT_CMD_FAILURE;
  9235. }
  9236. }
  9237. else
  9238. nd = (struct npc_data *)map_id2bl(st->oid);
  9239. if( !nd )
  9240. return SCRIPT_CMD_SUCCESS;
  9241. if( flag ) //Attach
  9242. {
  9243. TBL_PC* sd;
  9244. if( !script_rid2sd(sd) )
  9245. return SCRIPT_CMD_SUCCESS;
  9246. nd->u.scr.rid = sd->bl.id;
  9247. }
  9248. nd->u.scr.timertick = 0;
  9249. npc_settimerevent_tick(nd,0);
  9250. npc_timerevent_start(nd, st->rid);
  9251. return SCRIPT_CMD_SUCCESS;
  9252. }
  9253. /*==========================================
  9254. *------------------------------------------*/
  9255. BUILDIN_FUNC(startnpctimer)
  9256. {
  9257. struct npc_data *nd;
  9258. int flag = 0;
  9259. if( script_hasdata(st,3) )
  9260. { //Two arguments: NPC name and attach flag.
  9261. nd = npc_name2id(script_getstr(st, 2));
  9262. flag = script_getnum(st,3);
  9263. }
  9264. else if( script_hasdata(st,2) )
  9265. { //Check if argument is numeric (flag) or string (npc name)
  9266. struct script_data *data;
  9267. data = script_getdata(st,2);
  9268. get_val(st,data);
  9269. if( data_isstring(data) ) //NPC name
  9270. nd = npc_name2id(conv_str(st, data));
  9271. else if( data_isint(data) ) //Flag
  9272. {
  9273. nd = (struct npc_data *)map_id2bl(st->oid);
  9274. flag = conv_num(st,data);
  9275. }
  9276. else
  9277. {
  9278. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9279. return SCRIPT_CMD_FAILURE;
  9280. }
  9281. }
  9282. else
  9283. nd=(struct npc_data *)map_id2bl(st->oid);
  9284. if( !nd )
  9285. return SCRIPT_CMD_SUCCESS;
  9286. if( flag ) //Attach
  9287. {
  9288. TBL_PC* sd;
  9289. if( !script_rid2sd(sd) )
  9290. return SCRIPT_CMD_SUCCESS;
  9291. nd->u.scr.rid = sd->bl.id;
  9292. }
  9293. npc_timerevent_start(nd, st->rid);
  9294. return SCRIPT_CMD_SUCCESS;
  9295. }
  9296. /*==========================================
  9297. *------------------------------------------*/
  9298. BUILDIN_FUNC(stopnpctimer)
  9299. {
  9300. struct npc_data *nd;
  9301. int flag = 0;
  9302. if( script_hasdata(st,3) )
  9303. { //Two arguments: NPC name and attach flag.
  9304. nd = npc_name2id(script_getstr(st, 2));
  9305. flag = script_getnum(st,3);
  9306. }
  9307. else if( script_hasdata(st,2) )
  9308. { //Check if argument is numeric (flag) or string (npc name)
  9309. struct script_data *data;
  9310. data = script_getdata(st,2);
  9311. get_val(st,data);
  9312. if( data_isstring(data) ) //NPC name
  9313. nd = npc_name2id(conv_str(st, data));
  9314. else if( data_isint(data) ) //Flag
  9315. {
  9316. nd = (struct npc_data *)map_id2bl(st->oid);
  9317. flag = conv_num(st,data);
  9318. }
  9319. else
  9320. {
  9321. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9322. return SCRIPT_CMD_FAILURE;
  9323. }
  9324. }
  9325. else
  9326. nd=(struct npc_data *)map_id2bl(st->oid);
  9327. if( !nd )
  9328. return SCRIPT_CMD_SUCCESS;
  9329. if( flag ) //Detach
  9330. nd->u.scr.rid = 0;
  9331. npc_timerevent_stop(nd);
  9332. return SCRIPT_CMD_SUCCESS;
  9333. }
  9334. /*==========================================
  9335. *------------------------------------------*/
  9336. BUILDIN_FUNC(getnpctimer)
  9337. {
  9338. struct npc_data *nd;
  9339. TBL_PC *sd;
  9340. int type = script_getnum(st,2);
  9341. int val = 0;
  9342. if( script_hasdata(st,3) )
  9343. nd = npc_name2id(script_getstr(st,3));
  9344. else
  9345. nd = (struct npc_data *)map_id2bl(st->oid);
  9346. if( !nd || nd->bl.type != BL_NPC )
  9347. {
  9348. script_pushint(st,0);
  9349. ShowError("getnpctimer: Invalid NPC.\n");
  9350. return SCRIPT_CMD_FAILURE;
  9351. }
  9352. switch( type )
  9353. {
  9354. case 0: val = npc_gettimerevent_tick(nd); break;
  9355. case 1:
  9356. if( nd->u.scr.rid )
  9357. {
  9358. sd = map_id2sd(nd->u.scr.rid);
  9359. if( !sd )
  9360. {
  9361. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9362. break;
  9363. }
  9364. val = (sd->npc_timer_id != INVALID_TIMER);
  9365. }
  9366. else
  9367. val = (nd->u.scr.timerid != INVALID_TIMER);
  9368. break;
  9369. case 2: val = nd->u.scr.timeramount; break;
  9370. }
  9371. script_pushint(st,val);
  9372. return SCRIPT_CMD_SUCCESS;
  9373. }
  9374. /*==========================================
  9375. *------------------------------------------*/
  9376. BUILDIN_FUNC(setnpctimer)
  9377. {
  9378. int tick;
  9379. struct npc_data *nd;
  9380. tick = script_getnum(st,2);
  9381. if( script_hasdata(st,3) )
  9382. nd = npc_name2id(script_getstr(st,3));
  9383. else
  9384. nd = (struct npc_data *)map_id2bl(st->oid);
  9385. if( !nd || nd->bl.type != BL_NPC )
  9386. {
  9387. script_pushint(st,1);
  9388. ShowError("setnpctimer: Invalid NPC.\n");
  9389. return SCRIPT_CMD_FAILURE;
  9390. }
  9391. npc_settimerevent_tick(nd,tick);
  9392. script_pushint(st,0);
  9393. return SCRIPT_CMD_SUCCESS;
  9394. }
  9395. /*==========================================
  9396. * attaches the player rid to the timer [Celest]
  9397. *------------------------------------------*/
  9398. BUILDIN_FUNC(attachnpctimer)
  9399. {
  9400. TBL_PC *sd;
  9401. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9402. if( !nd || nd->bl.type != BL_NPC )
  9403. {
  9404. script_pushint(st,1);
  9405. ShowError("setnpctimer: Invalid NPC.\n");
  9406. return SCRIPT_CMD_FAILURE;
  9407. }
  9408. if( !script_nick2sd(2,sd) ){
  9409. script_pushint(st,1);
  9410. ShowWarning("attachnpctimer: Invalid player.\n");
  9411. return SCRIPT_CMD_FAILURE;
  9412. }
  9413. nd->u.scr.rid = sd->bl.id;
  9414. script_pushint(st,0);
  9415. return SCRIPT_CMD_SUCCESS;
  9416. }
  9417. /*==========================================
  9418. * detaches a player rid from the timer [Celest]
  9419. *------------------------------------------*/
  9420. BUILDIN_FUNC(detachnpctimer)
  9421. {
  9422. struct npc_data *nd;
  9423. if( script_hasdata(st,2) )
  9424. nd = npc_name2id(script_getstr(st,2));
  9425. else
  9426. nd = (struct npc_data *)map_id2bl(st->oid);
  9427. if( !nd || nd->bl.type != BL_NPC )
  9428. {
  9429. script_pushint(st,1);
  9430. ShowError("detachnpctimer: Invalid NPC.\n");
  9431. return SCRIPT_CMD_FAILURE;
  9432. }
  9433. nd->u.scr.rid = 0;
  9434. script_pushint(st,0);
  9435. return SCRIPT_CMD_SUCCESS;
  9436. }
  9437. /*==========================================
  9438. * To avoid "player not attached" script errors, this function is provided,
  9439. * it checks if there is a player attached to the current script. [Skotlex]
  9440. * If no, returns 0, if yes, returns the account_id of the attached player.
  9441. *------------------------------------------*/
  9442. BUILDIN_FUNC(playerattached)
  9443. {
  9444. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9445. script_pushint(st,0);
  9446. else
  9447. script_pushint(st,st->rid);
  9448. return SCRIPT_CMD_SUCCESS;
  9449. }
  9450. /*==========================================
  9451. *------------------------------------------*/
  9452. BUILDIN_FUNC(announce)
  9453. {
  9454. const char *mes = script_getstr(st,2);
  9455. int flag = script_getnum(st,3);
  9456. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9457. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9458. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9459. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9460. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9461. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9462. {
  9463. send_target target;
  9464. struct block_list *bl;
  9465. // If bc_npc flag is set, use NPC as broadcast source
  9466. if(flag&BC_NPC){
  9467. bl = map_id2bl(st->oid);
  9468. }else{
  9469. struct map_session_data* sd;
  9470. if( script_rid2sd(sd) )
  9471. bl = &sd->bl;
  9472. else
  9473. bl = NULL;
  9474. }
  9475. if (bl == NULL)
  9476. return SCRIPT_CMD_SUCCESS;
  9477. switch (flag&BC_TARGET_MASK) {
  9478. case BC_MAP: target = ALL_SAMEMAP; break;
  9479. case BC_AREA: target = AREA; break;
  9480. case BC_SELF: target = SELF; break;
  9481. default: target = ALL_CLIENT; break; // BC_ALL
  9482. }
  9483. if (fontColor)
  9484. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9485. else
  9486. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9487. }
  9488. else
  9489. {
  9490. if (fontColor)
  9491. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9492. else
  9493. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9494. }
  9495. return SCRIPT_CMD_SUCCESS;
  9496. }
  9497. /*==========================================
  9498. *------------------------------------------*/
  9499. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9500. {
  9501. char *mes = va_arg(ap, char *);
  9502. int len = va_arg(ap, int);
  9503. int type = va_arg(ap, int);
  9504. char *fontColor = va_arg(ap, char *);
  9505. short fontType = (short)va_arg(ap, int);
  9506. short fontSize = (short)va_arg(ap, int);
  9507. short fontAlign = (short)va_arg(ap, int);
  9508. short fontY = (short)va_arg(ap, int);
  9509. if (fontColor)
  9510. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9511. else
  9512. clif_broadcast(bl, mes, len, type, SELF);
  9513. return SCRIPT_CMD_SUCCESS;
  9514. }
  9515. BUILDIN_FUNC(mapannounce)
  9516. {
  9517. const char *mapname = script_getstr(st,2);
  9518. const char *mes = script_getstr(st,3);
  9519. int flag = script_getnum(st,4);
  9520. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9521. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9522. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9523. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9524. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9525. int16 m;
  9526. if ((m = map_mapname2mapid(mapname)) < 0)
  9527. return SCRIPT_CMD_SUCCESS;
  9528. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9529. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9530. return SCRIPT_CMD_SUCCESS;
  9531. }
  9532. /*==========================================
  9533. *------------------------------------------*/
  9534. BUILDIN_FUNC(areaannounce)
  9535. {
  9536. const char *mapname = script_getstr(st,2);
  9537. int x0 = script_getnum(st,3);
  9538. int y0 = script_getnum(st,4);
  9539. int x1 = script_getnum(st,5);
  9540. int y1 = script_getnum(st,6);
  9541. const char *mes = script_getstr(st,7);
  9542. int flag = script_getnum(st,8);
  9543. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9544. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9545. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9546. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9547. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9548. int16 m;
  9549. if ((m = map_mapname2mapid(mapname)) < 0)
  9550. return SCRIPT_CMD_SUCCESS;
  9551. map_foreachinallarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9552. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9553. return SCRIPT_CMD_SUCCESS;
  9554. }
  9555. /*==========================================
  9556. *------------------------------------------*/
  9557. BUILDIN_FUNC(getusers)
  9558. {
  9559. int flag, val = 0;
  9560. struct map_session_data* sd;
  9561. struct block_list* bl = NULL;
  9562. flag = script_getnum(st,2);
  9563. switch(flag&0x07)
  9564. {
  9565. case 0:
  9566. if(flag&0x8)
  9567. {// npc
  9568. bl = map_id2bl(st->oid);
  9569. }
  9570. else if(script_rid2sd(sd))
  9571. {// pc
  9572. bl = &sd->bl;
  9573. }
  9574. if(bl)
  9575. {
  9576. val = map[bl->m].users;
  9577. }
  9578. break;
  9579. case 1:
  9580. val = map_getusers();
  9581. break;
  9582. default:
  9583. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9584. script_pushint(st,0);
  9585. return SCRIPT_CMD_FAILURE;
  9586. }
  9587. script_pushint(st,val);
  9588. return SCRIPT_CMD_SUCCESS;
  9589. }
  9590. /*==========================================
  9591. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9592. *------------------------------------------*/
  9593. BUILDIN_FUNC(getmapguildusers)
  9594. {
  9595. const char *str;
  9596. int16 m;
  9597. int gid;
  9598. int c=0;
  9599. struct guild *g = NULL;
  9600. str=script_getstr(st,2);
  9601. gid=script_getnum(st,3);
  9602. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9603. script_pushint(st,-1);
  9604. return SCRIPT_CMD_SUCCESS;
  9605. }
  9606. g = guild_search(gid);
  9607. if (g){
  9608. unsigned short i;
  9609. for(i = 0; i < g->max_member; i++)
  9610. {
  9611. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9612. c++;
  9613. }
  9614. }
  9615. script_pushint(st,c);
  9616. return SCRIPT_CMD_SUCCESS;
  9617. }
  9618. /*==========================================
  9619. *------------------------------------------*/
  9620. BUILDIN_FUNC(getmapusers)
  9621. {
  9622. const char *str;
  9623. int16 m;
  9624. str=script_getstr(st,2);
  9625. if( (m=map_mapname2mapid(str))< 0){
  9626. script_pushint(st,-1);
  9627. return SCRIPT_CMD_SUCCESS;
  9628. }
  9629. script_pushint(st,map[m].users);
  9630. return SCRIPT_CMD_SUCCESS;
  9631. }
  9632. /*==========================================
  9633. *------------------------------------------*/
  9634. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9635. {
  9636. int *users=va_arg(ap,int *);
  9637. (*users)++;
  9638. return SCRIPT_CMD_SUCCESS;
  9639. }
  9640. BUILDIN_FUNC(getareausers)
  9641. {
  9642. const char *str;
  9643. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9644. str=script_getstr(st,2);
  9645. x0=script_getnum(st,3);
  9646. y0=script_getnum(st,4);
  9647. x1=script_getnum(st,5);
  9648. y1=script_getnum(st,6);
  9649. if( (m=map_mapname2mapid(str))< 0){
  9650. script_pushint(st,-1);
  9651. return SCRIPT_CMD_SUCCESS;
  9652. }
  9653. map_foreachinallarea(buildin_getareausers_sub,
  9654. m,x0,y0,x1,y1,BL_PC,&users);
  9655. script_pushint(st,users);
  9656. return SCRIPT_CMD_SUCCESS;
  9657. }
  9658. /*==========================================
  9659. *------------------------------------------*/
  9660. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9661. {
  9662. unsigned short nameid = (unsigned short)va_arg(ap, int);
  9663. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  9664. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9665. if(drop->item.nameid==nameid)
  9666. (*amount)+=drop->item.amount;
  9667. return SCRIPT_CMD_SUCCESS;
  9668. }
  9669. BUILDIN_FUNC(getareadropitem)
  9670. {
  9671. const char *str;
  9672. int16 m,x0,y0,x1,y1;
  9673. unsigned short nameid, amount = 0;
  9674. struct script_data *data;
  9675. str=script_getstr(st,2);
  9676. x0=script_getnum(st,3);
  9677. y0=script_getnum(st,4);
  9678. x1=script_getnum(st,5);
  9679. y1=script_getnum(st,6);
  9680. data=script_getdata(st,7);
  9681. get_val(st,data);
  9682. if( data_isstring(data) ){
  9683. const char *name=conv_str(st,data);
  9684. struct item_data *item_data = itemdb_searchname(name);
  9685. nameid=UNKNOWN_ITEM_ID;
  9686. if( item_data )
  9687. nameid=item_data->nameid;
  9688. }else
  9689. nameid=conv_num(st,data);
  9690. if( (m=map_mapname2mapid(str))< 0){
  9691. script_pushint(st,-1);
  9692. return SCRIPT_CMD_SUCCESS;
  9693. }
  9694. map_foreachinallarea(buildin_getareadropitem_sub,
  9695. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9696. script_pushint(st,amount);
  9697. return SCRIPT_CMD_SUCCESS;
  9698. }
  9699. /*==========================================
  9700. *------------------------------------------*/
  9701. BUILDIN_FUNC(enablenpc)
  9702. {
  9703. const char *str;
  9704. str=script_getstr(st,2);
  9705. npc_enable(str,1);
  9706. return SCRIPT_CMD_SUCCESS;
  9707. }
  9708. /*==========================================
  9709. *------------------------------------------*/
  9710. BUILDIN_FUNC(disablenpc)
  9711. {
  9712. const char *str;
  9713. str=script_getstr(st,2);
  9714. npc_enable(str,0);
  9715. return SCRIPT_CMD_SUCCESS;
  9716. }
  9717. /*==========================================
  9718. *------------------------------------------*/
  9719. BUILDIN_FUNC(hideoffnpc)
  9720. {
  9721. const char *str;
  9722. str=script_getstr(st,2);
  9723. npc_enable(str,2);
  9724. return SCRIPT_CMD_SUCCESS;
  9725. }
  9726. /*==========================================
  9727. *------------------------------------------*/
  9728. BUILDIN_FUNC(hideonnpc)
  9729. {
  9730. const char *str;
  9731. str=script_getstr(st,2);
  9732. npc_enable(str,4);
  9733. return SCRIPT_CMD_SUCCESS;
  9734. }
  9735. /* Starts a status effect on the target unit or on the attached player.
  9736. *
  9737. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9738. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9739. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9740. * <flag>: enum e_status_change_start_flags
  9741. */
  9742. BUILDIN_FUNC(sc_start)
  9743. {
  9744. TBL_NPC * nd = map_id2nd(st->oid);
  9745. struct block_list* bl;
  9746. enum sc_type type;
  9747. int tick, val1, val2, val3, val4=0, rate, flag;
  9748. char start_type;
  9749. const char* command = script_getfuncname(st);
  9750. if(strstr(command, "4"))
  9751. start_type = 4;
  9752. else if(strstr(command, "2"))
  9753. start_type = 2;
  9754. else
  9755. start_type = 1;
  9756. type = (sc_type)script_getnum(st,2);
  9757. tick = script_getnum(st,3);
  9758. val1 = script_getnum(st,4);
  9759. //If from NPC we make default flag 1 to be unavoidable
  9760. if(nd && nd->bl.id == fake_nd->bl.id)
  9761. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9762. else
  9763. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9764. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9765. if(script_hasdata(st,(6+start_type)))
  9766. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9767. else
  9768. bl = map_id2bl(st->rid);
  9769. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9770. {// When there isn't a duration specified, try to get it from the skill_db
  9771. tick = skill_get_time(status_sc2skill(type), val1);
  9772. }
  9773. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9774. bl = map_id2bl(potion_target);
  9775. tick /= 2;// Thrown potions only last half.
  9776. val4 = 1;// Mark that this was a thrown sc_effect
  9777. }
  9778. if(!bl)
  9779. return SCRIPT_CMD_SUCCESS;
  9780. switch(start_type) {
  9781. case 1:
  9782. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9783. break;
  9784. case 2:
  9785. val2 = script_getnum(st,5);
  9786. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9787. break;
  9788. case 4:
  9789. val2 = script_getnum(st,5);
  9790. val3 = script_getnum(st,6);
  9791. val4 = script_getnum(st,7);
  9792. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9793. break;
  9794. }
  9795. return SCRIPT_CMD_SUCCESS;
  9796. }
  9797. /// Ends one or all status effects on the target unit or on the attached player.
  9798. ///
  9799. /// sc_end <effect_id>{,<unit_id>};
  9800. BUILDIN_FUNC(sc_end)
  9801. {
  9802. struct block_list* bl;
  9803. int type;
  9804. type = script_getnum(st, 2);
  9805. if (script_hasdata(st, 3))
  9806. bl = map_id2bl(script_getnum(st, 3));
  9807. else
  9808. bl = map_id2bl(st->rid);
  9809. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9810. bl = map_id2bl(potion_target);
  9811. if (!bl)
  9812. return SCRIPT_CMD_SUCCESS;
  9813. if (type >= 0 && type < SC_MAX) {
  9814. struct status_change *sc = status_get_sc(bl);
  9815. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9816. if (!sce)
  9817. return SCRIPT_CMD_SUCCESS;
  9818. switch (type) {
  9819. case SC_WEIGHT50:
  9820. case SC_WEIGHT90:
  9821. case SC_NOCHAT:
  9822. case SC_PUSH_CART:
  9823. case SC_ALL_RIDING:
  9824. case SC_STYLE_CHANGE:
  9825. case SC_MONSTER_TRANSFORM:
  9826. case SC_ACTIVE_MONSTER_TRANSFORM:
  9827. case SC_MTF_ASPD:
  9828. case SC_MTF_RANGEATK:
  9829. case SC_MTF_MATK:
  9830. case SC_MTF_MLEATKED:
  9831. case SC_MTF_CRIDAMAGE:
  9832. case SC_MTF_ASPD2:
  9833. case SC_MTF_RANGEATK2:
  9834. case SC_MTF_MATK2:
  9835. case SC_MTF_MHP:
  9836. case SC_MTF_MSP:
  9837. case SC_MTF_PUMPKIN:
  9838. case SC_MTF_HITFLEE:
  9839. case SC_ATTHASTE_CASH:
  9840. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  9841. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  9842. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  9843. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  9844. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  9845. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  9846. return SCRIPT_CMD_SUCCESS;
  9847. default:
  9848. break;
  9849. }
  9850. //This should help status_change_end force disabling the SC in case it has no limit.
  9851. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9852. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9853. } else
  9854. status_change_clear(bl, 3); // remove all effects
  9855. return SCRIPT_CMD_SUCCESS;
  9856. }
  9857. /*==========================================
  9858. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9859. *------------------------------------------*/
  9860. BUILDIN_FUNC(getscrate)
  9861. {
  9862. struct block_list *bl;
  9863. int type,rate;
  9864. type=script_getnum(st,2);
  9865. rate=script_getnum(st,3);
  9866. if( script_hasdata(st,4) ) //get for the bl assigned
  9867. bl = map_id2bl(script_getnum(st,4));
  9868. else
  9869. bl = map_id2bl(st->rid);
  9870. if (bl)
  9871. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9872. script_pushint(st,rate);
  9873. return SCRIPT_CMD_SUCCESS;
  9874. }
  9875. /**
  9876. * getstatus(<effect type>{,<type>{,<char_id>}});
  9877. **/
  9878. BUILDIN_FUNC(getstatus)
  9879. {
  9880. int id, type;
  9881. struct map_session_data* sd;
  9882. if (!script_charid2sd(4,sd))
  9883. return SCRIPT_CMD_FAILURE;
  9884. id = script_getnum(st, 2);
  9885. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9886. if( id <= SC_NONE || id >= SC_MAX )
  9887. {// invalid status type given
  9888. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9889. return SCRIPT_CMD_SUCCESS;
  9890. }
  9891. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9892. {// no status is active
  9893. script_pushint(st, 0);
  9894. return SCRIPT_CMD_SUCCESS;
  9895. }
  9896. switch( type )
  9897. {
  9898. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9899. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9900. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9901. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9902. case 5:
  9903. {
  9904. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9905. if( timer )
  9906. {// return the amount of time remaining
  9907. script_pushint(st, timer->tick - gettick());
  9908. } else {
  9909. script_pushint(st, -1);
  9910. }
  9911. }
  9912. break;
  9913. default: script_pushint(st, 1); break;
  9914. }
  9915. return SCRIPT_CMD_SUCCESS;
  9916. }
  9917. /*==========================================
  9918. *
  9919. *------------------------------------------*/
  9920. BUILDIN_FUNC(debugmes)
  9921. {
  9922. const char *str;
  9923. str=script_getstr(st,2);
  9924. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9925. return SCRIPT_CMD_SUCCESS;
  9926. }
  9927. /*==========================================
  9928. *------------------------------------------*/
  9929. BUILDIN_FUNC(catchpet)
  9930. {
  9931. int pet_id;
  9932. TBL_PC *sd;
  9933. if( !script_rid2sd(sd) )
  9934. return SCRIPT_CMD_SUCCESS;
  9935. pet_id= script_getnum(st,2);
  9936. pet_catch_process1(sd,pet_id);
  9937. return SCRIPT_CMD_SUCCESS;
  9938. }
  9939. /*==========================================
  9940. * [orn]
  9941. *------------------------------------------*/
  9942. BUILDIN_FUNC(homunculus_evolution)
  9943. {
  9944. TBL_PC *sd;
  9945. if( !script_rid2sd(sd) )
  9946. return SCRIPT_CMD_SUCCESS;
  9947. if(hom_is_active(sd->hd))
  9948. {
  9949. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9950. hom_evolution(sd->hd);
  9951. else
  9952. clif_emotion(&sd->hd->bl, E_SWT);
  9953. }
  9954. return SCRIPT_CMD_SUCCESS;
  9955. }
  9956. /*==========================================
  9957. * Checks for vaporized morph state
  9958. * and deletes ITEMID_STRANGE_EMBRYO.
  9959. *------------------------------------------*/
  9960. BUILDIN_FUNC(homunculus_mutate)
  9961. {
  9962. int homun_id;
  9963. TBL_PC *sd;
  9964. if( !script_rid2sd(sd) || sd->hd == NULL )
  9965. return SCRIPT_CMD_SUCCESS;
  9966. if(script_hasdata(st,2))
  9967. homun_id = script_getnum(st,2);
  9968. else
  9969. homun_id = 6048 + (rnd() % 4);
  9970. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9971. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9972. int m_id = hom_class2mapid(homun_id);
  9973. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9974. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9975. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9976. hom_call(sd); // Respawn homunculus.
  9977. hom_mutate(sd->hd, homun_id);
  9978. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9979. script_pushint(st, 1);
  9980. return SCRIPT_CMD_SUCCESS;
  9981. } else
  9982. clif_emotion(&sd->bl, E_SWT);
  9983. } else
  9984. clif_emotion(&sd->bl, E_SWT);
  9985. script_pushint(st, 0);
  9986. return SCRIPT_CMD_SUCCESS;
  9987. }
  9988. /*==========================================
  9989. * Puts homunculus into morph state
  9990. * and gives ITEMID_STRANGE_EMBRYO.
  9991. *------------------------------------------*/
  9992. BUILDIN_FUNC(morphembryo)
  9993. {
  9994. struct item item_tmp;
  9995. TBL_PC *sd;
  9996. if( !script_rid2sd(sd) || sd->hd == NULL )
  9997. return SCRIPT_CMD_SUCCESS;
  9998. if( hom_is_active(sd->hd) ) {
  9999. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  10000. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  10001. char i;
  10002. memset(&item_tmp, 0, sizeof(item_tmp));
  10003. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  10004. item_tmp.identify = 1;
  10005. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  10006. clif_additem(sd, 0, 0, i);
  10007. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  10008. } else {
  10009. hom_vaporize(sd, HOM_ST_MORPH);
  10010. script_pushint(st, 1);
  10011. return SCRIPT_CMD_SUCCESS;
  10012. }
  10013. } else
  10014. clif_emotion(&sd->hd->bl, E_SWT);
  10015. } else
  10016. clif_emotion(&sd->bl, E_SWT);
  10017. script_pushint(st, 0);
  10018. return SCRIPT_CMD_SUCCESS;
  10019. }
  10020. // [Zephyrus]
  10021. BUILDIN_FUNC(homunculus_shuffle)
  10022. {
  10023. TBL_PC *sd;
  10024. if( !script_rid2sd(sd) )
  10025. return SCRIPT_CMD_SUCCESS;
  10026. if(hom_is_active(sd->hd))
  10027. hom_shuffle(sd->hd);
  10028. return SCRIPT_CMD_SUCCESS;
  10029. }
  10030. /*==========================================
  10031. * Check for homunculus state.
  10032. * Return: -1 = No homunculus
  10033. * 0 = Homunculus is active
  10034. * 1 = Homunculus is vaporized (rest)
  10035. * 2 = Homunculus is in morph state
  10036. *------------------------------------------*/
  10037. BUILDIN_FUNC(checkhomcall)
  10038. {
  10039. TBL_PC *sd;
  10040. TBL_HOM *hd;
  10041. if( !script_rid2sd(sd) )
  10042. return SCRIPT_CMD_SUCCESS;
  10043. hd = sd->hd;
  10044. if( !hd )
  10045. script_pushint(st, -1);
  10046. else
  10047. script_pushint(st, hd->homunculus.vaporize);
  10048. return SCRIPT_CMD_SUCCESS;
  10049. }
  10050. //These two functions bring the eA MAPID_* class functionality to scripts.
  10051. BUILDIN_FUNC(eaclass)
  10052. {
  10053. int class_;
  10054. if( script_hasdata(st,2) )
  10055. class_ = script_getnum(st,2);
  10056. else {
  10057. TBL_PC *sd;
  10058. if (!script_charid2sd(3,sd)) {
  10059. script_pushint(st,-1);
  10060. return SCRIPT_CMD_SUCCESS;
  10061. }
  10062. class_ = sd->status.class_;
  10063. }
  10064. script_pushint(st,pc_jobid2mapid(class_));
  10065. return SCRIPT_CMD_SUCCESS;
  10066. }
  10067. BUILDIN_FUNC(roclass)
  10068. {
  10069. int class_ =script_getnum(st,2);
  10070. int sex;
  10071. if( script_hasdata(st,3) )
  10072. sex = script_getnum(st,3);
  10073. else {
  10074. TBL_PC *sd;
  10075. if (st->rid && script_rid2sd(sd))
  10076. sex = sd->status.sex;
  10077. else
  10078. sex = SEX_MALE; //Just use male when not found.
  10079. }
  10080. script_pushint(st,pc_mapid2jobid(class_, sex));
  10081. return SCRIPT_CMD_SUCCESS;
  10082. }
  10083. /*==========================================
  10084. * Tells client to open a hatching window, used for pet incubator
  10085. *------------------------------------------*/
  10086. BUILDIN_FUNC(birthpet)
  10087. {
  10088. TBL_PC *sd;
  10089. if( !script_rid2sd(sd) )
  10090. return SCRIPT_CMD_SUCCESS;
  10091. if( sd->status.pet_id )
  10092. {// do not send egg list, when you already have a pet
  10093. return SCRIPT_CMD_SUCCESS;
  10094. }
  10095. clif_sendegg(sd);
  10096. return SCRIPT_CMD_SUCCESS;
  10097. }
  10098. /**
  10099. * resetlvl <action type>{,<char_id>};
  10100. * @param action_type:
  10101. * 1 : make like after rebirth
  10102. * 2 : blvl,jlvl=1, skillpoint=0
  10103. * 3 : don't reset skill, blvl=1
  10104. * 4 : jlvl=0
  10105. * @author AppleGirl
  10106. **/
  10107. BUILDIN_FUNC(resetlvl)
  10108. {
  10109. TBL_PC *sd;
  10110. int type=script_getnum(st,2);
  10111. if (!script_charid2sd(3,sd))
  10112. return SCRIPT_CMD_FAILURE;
  10113. pc_resetlvl(sd,type);
  10114. return SCRIPT_CMD_SUCCESS;
  10115. }
  10116. /**
  10117. * Reset a player status point
  10118. * resetstatus({<char_id>});
  10119. **/
  10120. BUILDIN_FUNC(resetstatus)
  10121. {
  10122. TBL_PC *sd;
  10123. if (!script_charid2sd(2,sd))
  10124. return SCRIPT_CMD_FAILURE;
  10125. pc_resetstate(sd);
  10126. return SCRIPT_CMD_SUCCESS;
  10127. }
  10128. /**
  10129. * Reset player's skill
  10130. * resetskill({<char_id>});
  10131. **/
  10132. BUILDIN_FUNC(resetskill)
  10133. {
  10134. TBL_PC *sd;
  10135. if (!script_charid2sd(2,sd))
  10136. return SCRIPT_CMD_FAILURE;
  10137. pc_resetskill(sd,1);
  10138. return SCRIPT_CMD_SUCCESS;
  10139. }
  10140. /**
  10141. * Counts total amount of skill points.
  10142. * skillpointcount({<char_id>})
  10143. **/
  10144. BUILDIN_FUNC(skillpointcount)
  10145. {
  10146. TBL_PC *sd;
  10147. if (!script_charid2sd(2,sd))
  10148. return SCRIPT_CMD_FAILURE;
  10149. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  10150. return SCRIPT_CMD_SUCCESS;
  10151. }
  10152. /*==========================================
  10153. *
  10154. *------------------------------------------*/
  10155. BUILDIN_FUNC(changebase)
  10156. {
  10157. TBL_PC *sd=NULL;
  10158. int vclass;
  10159. if( !script_mapid2sd(3,sd) )
  10160. return SCRIPT_CMD_SUCCESS;
  10161. vclass = script_getnum(st,2);
  10162. if(vclass == JOB_WEDDING)
  10163. {
  10164. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  10165. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  10166. )
  10167. return SCRIPT_CMD_SUCCESS;
  10168. }
  10169. if(!sd->disguise && vclass != sd->vd.class_) {
  10170. status_set_viewdata(&sd->bl, vclass);
  10171. //Updated client view. Base, Weapon and Cloth Colors.
  10172. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  10173. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10174. if (sd->vd.cloth_color)
  10175. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  10176. if (sd->vd.body_style)
  10177. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  10178. clif_skillinfoblock(sd);
  10179. }
  10180. return SCRIPT_CMD_SUCCESS;
  10181. }
  10182. /**
  10183. * Change account sex and unequip all item and request for a changesex to char-serv
  10184. * changesex({<char_id>});
  10185. */
  10186. BUILDIN_FUNC(changesex)
  10187. {
  10188. int i;
  10189. TBL_PC *sd = NULL;
  10190. if (!script_charid2sd(2,sd))
  10191. return SCRIPT_CMD_FAILURE;
  10192. pc_resetskill(sd,4);
  10193. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10194. for(i = 0; i < EQI_MAX; i++) {
  10195. if (sd->equip_index[i] >= 0)
  10196. pc_unequipitem(sd, sd->equip_index[i], 3);
  10197. }
  10198. chrif_changesex(sd, true);
  10199. return SCRIPT_CMD_SUCCESS;
  10200. }
  10201. /**
  10202. * Change character's sex and unequip all item and request for a changesex to char-serv
  10203. * changecharsex({<char_id>});
  10204. */
  10205. BUILDIN_FUNC(changecharsex)
  10206. {
  10207. #if PACKETVER >= 20141016
  10208. int i;
  10209. TBL_PC *sd = NULL;
  10210. if (!script_charid2sd(2,sd))
  10211. return SCRIPT_CMD_FAILURE;
  10212. pc_resetskill(sd,4);
  10213. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  10214. for (i = 0; i < EQI_MAX; i++) {
  10215. if (sd->equip_index[i] >= 0)
  10216. pc_unequipitem(sd, sd->equip_index[i], 3);
  10217. }
  10218. chrif_changesex(sd, false);
  10219. return SCRIPT_CMD_SUCCESS;
  10220. #else
  10221. return SCRIPT_CMD_FAILURE;
  10222. #endif
  10223. }
  10224. /*==========================================
  10225. * Works like 'announce' but outputs in the common chat window
  10226. *------------------------------------------*/
  10227. BUILDIN_FUNC(globalmes)
  10228. {
  10229. struct block_list *bl = map_id2bl(st->oid);
  10230. struct npc_data *nd = (struct npc_data *)bl;
  10231. const char *name=NULL,*mes;
  10232. mes=script_getstr(st,2);
  10233. if(mes==NULL)
  10234. return SCRIPT_CMD_SUCCESS;
  10235. if(script_hasdata(st,3)){ // npc name to display
  10236. name=script_getstr(st,3);
  10237. } else {
  10238. name=nd->name; //use current npc name
  10239. }
  10240. npc_globalmessage(name,mes); // broadcast to all players connected
  10241. return SCRIPT_CMD_SUCCESS;
  10242. }
  10243. /////////////////////////////////////////////////////////////////////
  10244. // NPC waiting room (chat room)
  10245. //
  10246. /// Creates a waiting room (chat room) for this npc.
  10247. ///
  10248. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  10249. BUILDIN_FUNC(waitingroom)
  10250. {
  10251. struct npc_data* nd;
  10252. const char* title = script_getstr(st, 2);
  10253. int limit = script_getnum(st, 3);
  10254. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  10255. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  10256. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  10257. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  10258. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  10259. nd = (struct npc_data *)map_id2bl(st->oid);
  10260. if( nd != NULL )
  10261. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  10262. return SCRIPT_CMD_SUCCESS;
  10263. }
  10264. /// Removes the waiting room of the current or target npc.
  10265. ///
  10266. /// delwaitingroom "<npc_name>";
  10267. /// delwaitingroom;
  10268. BUILDIN_FUNC(delwaitingroom)
  10269. {
  10270. struct npc_data* nd;
  10271. if( script_hasdata(st,2) )
  10272. nd = npc_name2id(script_getstr(st, 2));
  10273. else
  10274. nd = (struct npc_data *)map_id2bl(st->oid);
  10275. if( nd != NULL )
  10276. chat_deletenpcchat(nd);
  10277. return SCRIPT_CMD_SUCCESS;
  10278. }
  10279. /// Kick the specified player from the waiting room of the target npc.
  10280. ///
  10281. /// waitingroomkick "<npc_name>", <kickusername>;
  10282. BUILDIN_FUNC(waitingroomkick)
  10283. {
  10284. struct npc_data* nd;
  10285. struct chat_data* cd;
  10286. const char* kickusername;
  10287. nd = npc_name2id(script_getstr(st,2));
  10288. kickusername = script_getstr(st,3);
  10289. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10290. chat_npckickchat(cd, kickusername);
  10291. return SCRIPT_CMD_SUCCESS;
  10292. }
  10293. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  10294. /// Num users stored in .@waitingroom_usercount
  10295. ///
  10296. /// getwaitingroomusers "<npc_name>";
  10297. BUILDIN_FUNC(getwaitingroomusers)
  10298. {
  10299. struct npc_data* nd;
  10300. struct chat_data* cd;
  10301. int i, j=0;
  10302. if( script_hasdata(st,2) )
  10303. nd = npc_name2id(script_getstr(st, 2));
  10304. else
  10305. nd = (struct npc_data *)map_id2bl(st->oid);
  10306. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  10307. for(i = 0; i < cd->users; ++i) {
  10308. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  10309. j++;
  10310. }
  10311. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  10312. }
  10313. return SCRIPT_CMD_SUCCESS;
  10314. }
  10315. /// Kicks all the players from the waiting room of the current or target npc.
  10316. ///
  10317. /// kickwaitingroomall "<npc_name>";
  10318. /// kickwaitingroomall;
  10319. BUILDIN_FUNC(waitingroomkickall)
  10320. {
  10321. struct npc_data* nd;
  10322. struct chat_data* cd;
  10323. if( script_hasdata(st,2) )
  10324. nd = npc_name2id(script_getstr(st,2));
  10325. else
  10326. nd = (struct npc_data *)map_id2bl(st->oid);
  10327. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10328. chat_npckickall(cd);
  10329. return SCRIPT_CMD_SUCCESS;
  10330. }
  10331. /// Enables the waiting room event of the current or target npc.
  10332. ///
  10333. /// enablewaitingroomevent "<npc_name>";
  10334. /// enablewaitingroomevent;
  10335. BUILDIN_FUNC(enablewaitingroomevent)
  10336. {
  10337. struct npc_data* nd;
  10338. struct chat_data* cd;
  10339. if( script_hasdata(st,2) )
  10340. nd = npc_name2id(script_getstr(st, 2));
  10341. else
  10342. nd = (struct npc_data *)map_id2bl(st->oid);
  10343. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10344. chat_enableevent(cd);
  10345. return SCRIPT_CMD_SUCCESS;
  10346. }
  10347. /// Disables the waiting room event of the current or target npc.
  10348. ///
  10349. /// disablewaitingroomevent "<npc_name>";
  10350. /// disablewaitingroomevent;
  10351. BUILDIN_FUNC(disablewaitingroomevent)
  10352. {
  10353. struct npc_data *nd;
  10354. struct chat_data *cd;
  10355. if( script_hasdata(st,2) )
  10356. nd = npc_name2id(script_getstr(st, 2));
  10357. else
  10358. nd = (struct npc_data *)map_id2bl(st->oid);
  10359. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10360. chat_disableevent(cd);
  10361. return SCRIPT_CMD_SUCCESS;
  10362. }
  10363. /// Returns info on the waiting room of the current or target npc.
  10364. /// Returns -1 if the type unknown
  10365. /// <type>=0 : current number of users
  10366. /// <type>=1 : maximum number of users allowed
  10367. /// <type>=2 : the number of users that trigger the event
  10368. /// <type>=3 : if the trigger is disabled
  10369. /// <type>=4 : the title of the waiting room
  10370. /// <type>=5 : the password of the waiting room
  10371. /// <type>=16 : the name of the waiting room event
  10372. /// <type>=32 : if the waiting room is full
  10373. /// <type>=33 : if there are enough users to trigger the event
  10374. ///
  10375. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10376. /// getwaitingroomstate(<type>) -> <info>
  10377. BUILDIN_FUNC(getwaitingroomstate)
  10378. {
  10379. struct npc_data *nd;
  10380. struct chat_data *cd;
  10381. int type;
  10382. type = script_getnum(st,2);
  10383. if( script_hasdata(st,3) )
  10384. nd = npc_name2id(script_getstr(st, 3));
  10385. else
  10386. nd = (struct npc_data *)map_id2bl(st->oid);
  10387. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10388. {
  10389. script_pushint(st, -1);
  10390. return SCRIPT_CMD_SUCCESS;
  10391. }
  10392. switch(type)
  10393. {
  10394. case 0: script_pushint(st, cd->users); break;
  10395. case 1: script_pushint(st, cd->limit); break;
  10396. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10397. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10398. case 4: script_pushstrcopy(st, cd->title); break;
  10399. case 5: script_pushstrcopy(st, cd->pass); break;
  10400. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10401. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10402. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10403. default: script_pushint(st, -1); break;
  10404. }
  10405. return SCRIPT_CMD_SUCCESS;
  10406. }
  10407. /// Warps the trigger or target amount of players to the target map and position.
  10408. /// Players are automatically removed from the waiting room.
  10409. /// Those waiting the longest will get warped first.
  10410. /// The target map can be "Random" for a random position in the current map,
  10411. /// and "SavePoint" for the savepoint map+position.
  10412. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10413. ///
  10414. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10415. /// The total number of teleported players is put into $@warpwaitingpcnum
  10416. ///
  10417. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10418. /// warpwaitingpc "<map name>",<x>,<y>;
  10419. BUILDIN_FUNC(warpwaitingpc)
  10420. {
  10421. int x;
  10422. int y;
  10423. int i;
  10424. int n;
  10425. const char* map_name;
  10426. struct npc_data* nd;
  10427. struct chat_data* cd;
  10428. nd = (struct npc_data *)map_id2bl(st->oid);
  10429. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10430. return SCRIPT_CMD_SUCCESS;
  10431. map_name = script_getstr(st,2);
  10432. x = script_getnum(st,3);
  10433. y = script_getnum(st,4);
  10434. n = cd->trigger&0x7f;
  10435. if( script_hasdata(st,5) )
  10436. n = script_getnum(st,5);
  10437. for( i = 0; i < n && cd->users > 0; i++ )
  10438. {
  10439. TBL_PC* sd = cd->usersd[0];
  10440. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  10441. {// can't teleport on this map
  10442. break;
  10443. }
  10444. if( cd->zeny )
  10445. {// fee set
  10446. if( (uint32)sd->status.zeny < cd->zeny )
  10447. {// no zeny to cover set fee
  10448. break;
  10449. }
  10450. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10451. }
  10452. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10453. if( strcmp(map_name,"Random") == 0 )
  10454. pc_randomwarp(sd,CLR_TELEPORT);
  10455. else if( strcmp(map_name,"SavePoint") == 0 )
  10456. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10457. else
  10458. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10459. }
  10460. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10461. return SCRIPT_CMD_SUCCESS;
  10462. }
  10463. /////////////////////////////////////////////////////////////////////
  10464. // ...
  10465. //
  10466. /// Detaches a character from a script.
  10467. ///
  10468. /// @param st Script state to detach the character from.
  10469. static void script_detach_rid(struct script_state* st)
  10470. {
  10471. if(st->rid)
  10472. {
  10473. script_detach_state(st, false);
  10474. st->rid = 0;
  10475. }
  10476. }
  10477. /*=========================================================================
  10478. * Attaches a set of RIDs to the current script. [digitalhamster]
  10479. * addrid(<type>{,<flag>{,<parameters>}});
  10480. * <type>:
  10481. * 0 : All players in the server.
  10482. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10483. * 2 : Party members of a specified party ID.
  10484. * [ Parameters: <party id> ]
  10485. * 3 : Guild members of a specified guild ID.
  10486. * [ Parameters: <guild id> ]
  10487. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10488. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10489. * 5 : All players in the map.
  10490. * [ Parameters: "<map name>" ]
  10491. * Account ID: The specified account ID.
  10492. * <flag>:
  10493. * 0 : Players are always attached. (default)
  10494. * 1 : Players currently running another script will not be attached.
  10495. *-------------------------------------------------------------------------*/
  10496. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10497. {
  10498. int forceflag;
  10499. struct map_session_data *sd = (TBL_PC *)bl;
  10500. struct script_state* st;
  10501. st = va_arg(ap,struct script_state*);
  10502. forceflag = va_arg(ap,int);
  10503. if(!forceflag || !sd->st)
  10504. if(sd->status.account_id != st->rid)
  10505. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10506. return 0;
  10507. }
  10508. BUILDIN_FUNC(addrid)
  10509. {
  10510. struct s_mapiterator* iter;
  10511. struct block_list *bl;
  10512. TBL_PC *sd;
  10513. if(st->rid < 1) {
  10514. st->state = END;
  10515. bl = map_id2bl(st->oid);
  10516. } else
  10517. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10518. iter = mapit_getallusers();
  10519. switch(script_getnum(st,2)) {
  10520. case 0:
  10521. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10522. if(!script_getnum(st,3) || !sd->st)
  10523. if(sd->status.account_id != st->rid) //attached player already runs.
  10524. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10525. }
  10526. break;
  10527. case 1:
  10528. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10529. if(!script_getnum(st,3) || !sd->st)
  10530. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10531. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10532. }
  10533. break;
  10534. case 2:
  10535. if(script_getnum(st,4) == 0) {
  10536. script_pushint(st,0);
  10537. return SCRIPT_CMD_SUCCESS;
  10538. }
  10539. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10540. if(!script_getnum(st,3) || !sd->st)
  10541. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10542. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10543. }
  10544. break;
  10545. case 3:
  10546. if(script_getnum(st,4) == 0) {
  10547. script_pushint(st,0);
  10548. return SCRIPT_CMD_SUCCESS;
  10549. }
  10550. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10551. if(!script_getnum(st,3) || !sd->st)
  10552. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10553. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10554. }
  10555. break;
  10556. case 4:
  10557. map_foreachinallarea(buildin_addrid_sub,
  10558. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10559. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10560. break;
  10561. case 5:
  10562. if (script_getstr(st, 4) == NULL) {
  10563. script_pushint(st, 0);
  10564. return SCRIPT_CMD_FAILURE;
  10565. }
  10566. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  10567. script_pushint(st, 0);
  10568. return SCRIPT_CMD_FAILURE;
  10569. }
  10570. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  10571. break;
  10572. default:
  10573. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10574. script_pushint(st,0);
  10575. return SCRIPT_CMD_SUCCESS;
  10576. }
  10577. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10578. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10579. script_pushint(st,1);
  10580. }
  10581. return SCRIPT_CMD_SUCCESS;
  10582. }
  10583. mapit_free(iter);
  10584. script_pushint(st,1);
  10585. return SCRIPT_CMD_SUCCESS;
  10586. }
  10587. /*==========================================
  10588. * Attach sd char id to script and detach current one if any
  10589. *------------------------------------------*/
  10590. BUILDIN_FUNC(attachrid)
  10591. {
  10592. int rid = script_getnum(st,2);
  10593. if (map_id2sd(rid) != NULL) {
  10594. script_detach_rid(st);
  10595. st->rid = rid;
  10596. script_attach_state(st);
  10597. script_pushint(st,1);
  10598. } else
  10599. script_pushint(st,0);
  10600. return SCRIPT_CMD_SUCCESS;
  10601. }
  10602. /*==========================================
  10603. * Detach script to rid
  10604. *------------------------------------------*/
  10605. BUILDIN_FUNC(detachrid)
  10606. {
  10607. script_detach_rid(st);
  10608. return SCRIPT_CMD_SUCCESS;
  10609. }
  10610. /*==========================================
  10611. * Chk if account connected, (and charid from account if specified)
  10612. *------------------------------------------*/
  10613. BUILDIN_FUNC(isloggedin)
  10614. {
  10615. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10616. if (script_hasdata(st,3) && sd &&
  10617. sd->status.char_id != script_getnum(st,3))
  10618. sd = NULL;
  10619. push_val(st->stack,C_INT,sd!=NULL);
  10620. return SCRIPT_CMD_SUCCESS;
  10621. }
  10622. /*==========================================
  10623. *
  10624. *------------------------------------------*/
  10625. BUILDIN_FUNC(setmapflagnosave)
  10626. {
  10627. int16 m,x,y;
  10628. unsigned short mapindex;
  10629. const char *str,*str2;
  10630. str=script_getstr(st,2);
  10631. str2=script_getstr(st,3);
  10632. x=script_getnum(st,4);
  10633. y=script_getnum(st,5);
  10634. m = map_mapname2mapid(str);
  10635. mapindex = mapindex_name2id(str2);
  10636. if(m >= 0 && mapindex) {
  10637. map[m].flag.nosave=1;
  10638. map[m].save.map=mapindex;
  10639. map[m].save.x=x;
  10640. map[m].save.y=y;
  10641. }
  10642. return SCRIPT_CMD_SUCCESS;
  10643. }
  10644. BUILDIN_FUNC(getmapflag)
  10645. {
  10646. int16 m,i;
  10647. const char *str;
  10648. str=script_getstr(st,2);
  10649. i=script_getnum(st,3);
  10650. m = map_mapname2mapid(str);
  10651. if(m >= 0) {
  10652. switch(i) {
  10653. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10654. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10655. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10656. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10657. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10658. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10659. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10660. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10661. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10662. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10663. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10664. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10665. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10666. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10667. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10668. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10669. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10670. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10671. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10672. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10673. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10674. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10675. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10676. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10677. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10678. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10679. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10680. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10681. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10682. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10683. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10684. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10685. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10686. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10687. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10688. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10689. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10690. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10691. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10692. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10693. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10694. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10695. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10696. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10697. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10698. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10699. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10700. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10701. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10702. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10703. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10704. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10705. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10706. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10707. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10708. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10709. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10710. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10711. case MF_NOCOSTUME: script_pushint(st,map[m].flag.nocostume); break;
  10712. case MF_GVG_TE_CASTLE: script_pushint(st,map[m].flag.gvg_te_castle); break;
  10713. case MF_GVG_TE: script_pushint(st,map[m].flag.gvg_te); break;
  10714. case MF_HIDEMOBHPBAR: script_pushint(st,map[m].flag.hidemobhpbar); break;
  10715. #ifdef ADJUST_SKILL_DAMAGE
  10716. case MF_SKILL_DAMAGE:
  10717. {
  10718. int ret_val=0, type=0;
  10719. FETCH(4,type);
  10720. switch (type) {
  10721. case 1: ret_val = map[m].adjust.damage.pc; break;
  10722. case 2: ret_val = map[m].adjust.damage.mob; break;
  10723. case 3: ret_val = map[m].adjust.damage.boss; break;
  10724. case 4: ret_val = map[m].adjust.damage.other; break;
  10725. case 5: ret_val = map[m].adjust.damage.caster; break;
  10726. default: ret_val = map[m].flag.skill_damage; break;
  10727. }
  10728. script_pushint(st,ret_val); break;
  10729. } break;
  10730. #endif
  10731. }
  10732. }
  10733. return SCRIPT_CMD_SUCCESS;
  10734. }
  10735. /* pvp timer handling */
  10736. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10737. TBL_PC* sd = (TBL_PC*)bl;
  10738. if (sd->pvp_timer == INVALID_TIMER) {
  10739. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10740. sd->pvp_rank = 0;
  10741. sd->pvp_lastusers = 0;
  10742. sd->pvp_point = 5;
  10743. sd->pvp_won = 0;
  10744. sd->pvp_lost = 0;
  10745. }
  10746. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE,SELF);
  10747. return 0;
  10748. }
  10749. BUILDIN_FUNC(setmapflag)
  10750. {
  10751. int16 m,i;
  10752. const char *str;
  10753. int val=0;
  10754. str=script_getstr(st,2);
  10755. i=script_getnum(st,3);
  10756. FETCH(4,val);
  10757. m = map_mapname2mapid(str);
  10758. if(m >= 0) {
  10759. switch(i) {
  10760. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10761. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10762. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10763. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10764. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10765. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10766. case MF_PVP:
  10767. map[m].flag.pvp = 1;
  10768. if( !battle_config.pk_mode ) {
  10769. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10770. }
  10771. break;
  10772. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10773. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10774. case MF_GVG:
  10775. map[m].flag.gvg = 1;
  10776. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10777. break;
  10778. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10779. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10780. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10781. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10782. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10783. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10784. case MF_SNOW: map[m].flag.snow = 1; break;
  10785. case MF_FOG: map[m].flag.fog = 1; break;
  10786. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10787. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10788. case MF_NOGO: map[m].flag.nogo = 1; break;
  10789. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10790. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10791. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10792. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10793. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10794. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10795. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10796. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10797. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10798. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10799. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10800. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10801. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10802. case MF_RESTRICTED:
  10803. map[m].zone |= 1<<(val+1);
  10804. map[m].flag.restricted=1;
  10805. break;
  10806. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10807. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10808. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10809. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10810. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10811. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10812. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10813. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10814. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10815. case MF_TOWN: map[m].flag.town = 1; break;
  10816. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10817. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10818. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10819. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10820. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10821. case MF_RESET: map[m].flag.reset = 1; break;
  10822. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10823. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10824. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10825. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10826. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10827. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10828. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10829. case MF_NOCOSTUME: map[m].flag.nocostume = 1; break;
  10830. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 1; break;
  10831. case MF_GVG_TE:
  10832. map[m].flag.gvg_te = 1;
  10833. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10834. break;
  10835. case MF_HIDEMOBHPBAR: map[m].flag.hidemobhpbar = 1; break;
  10836. #ifdef ADJUST_SKILL_DAMAGE
  10837. case MF_SKILL_DAMAGE:
  10838. {
  10839. int type=0;
  10840. FETCH(5,type);
  10841. switch (type) {
  10842. case 1: map[m].adjust.damage.pc = val; break;
  10843. case 2: map[m].adjust.damage.mob = val; break;
  10844. case 3: map[m].adjust.damage.boss = val; break;
  10845. case 4: map[m].adjust.damage.other = val; break;
  10846. case 5: map[m].adjust.damage.caster = val; break;
  10847. }
  10848. map[m].flag.skill_damage = 1;
  10849. } break;
  10850. #endif
  10851. }
  10852. }
  10853. return SCRIPT_CMD_SUCCESS;
  10854. }
  10855. BUILDIN_FUNC(removemapflag)
  10856. {
  10857. int16 m,i;
  10858. const char *str;
  10859. int val=0;
  10860. str=script_getstr(st,2);
  10861. i=script_getnum(st,3);
  10862. FETCH(4,val);
  10863. m = map_mapname2mapid(str);
  10864. if(m >= 0) {
  10865. switch(i) {
  10866. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10867. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10868. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10869. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10870. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10871. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10872. case MF_PVP:
  10873. map[m].flag.pvp = 0;
  10874. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10875. break;
  10876. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10877. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10878. case MF_GVG:
  10879. map[m].flag.gvg = 0;
  10880. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10881. break;
  10882. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10883. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10884. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10885. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10886. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10887. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10888. case MF_SNOW: map[m].flag.snow = 0; break;
  10889. case MF_FOG: map[m].flag.fog = 0; break;
  10890. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10891. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10892. case MF_NOGO: map[m].flag.nogo = 0; break;
  10893. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10894. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10895. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10896. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10897. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10898. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10899. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10900. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10901. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10902. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10903. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10904. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10905. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10906. case MF_RESTRICTED:
  10907. map[m].zone ^= 1<<(val+1);
  10908. if (map[m].zone == 0){
  10909. map[m].flag.restricted=0;
  10910. }
  10911. break;
  10912. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10913. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10914. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10915. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10916. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10917. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10918. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10919. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10920. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10921. case MF_TOWN: map[m].flag.town = 0; break;
  10922. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10923. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10924. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10925. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10926. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10927. case MF_RESET: map[m].flag.reset = 0; break;
  10928. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10929. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10930. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10931. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10932. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10933. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10934. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10935. case MF_NOCOSTUME: map[m].flag.nocostume = 0; break;
  10936. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 0; break;
  10937. case MF_GVG_TE:
  10938. map[m].flag.gvg_te = 0;
  10939. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10940. break;
  10941. case MF_HIDEMOBHPBAR: map[m].flag.hidemobhpbar = 0; break;
  10942. #ifdef ADJUST_SKILL_DAMAGE
  10943. case MF_SKILL_DAMAGE:
  10944. {
  10945. map[m].flag.skill_damage = 0;
  10946. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10947. if (map[m].skill_damage.count)
  10948. map_skill_damage_free(&map[m]);
  10949. } break;
  10950. #endif
  10951. }
  10952. }
  10953. return SCRIPT_CMD_SUCCESS;
  10954. }
  10955. BUILDIN_FUNC(pvpon)
  10956. {
  10957. int16 m;
  10958. const char *str;
  10959. TBL_PC* sd = NULL;
  10960. struct s_mapiterator* iter;
  10961. str = script_getstr(st,2);
  10962. m = map_mapname2mapid(str);
  10963. if( m < 0 || map[m].flag.pvp )
  10964. return SCRIPT_CMD_SUCCESS; // nothing to do
  10965. map[m].flag.pvp = 1;
  10966. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10967. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10968. return SCRIPT_CMD_SUCCESS;
  10969. iter = mapit_getallusers();
  10970. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10971. {
  10972. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10973. continue; // not applicable
  10974. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10975. sd->pvp_rank = 0;
  10976. sd->pvp_lastusers = 0;
  10977. sd->pvp_point = 5;
  10978. sd->pvp_won = 0;
  10979. sd->pvp_lost = 0;
  10980. }
  10981. mapit_free(iter);
  10982. return SCRIPT_CMD_SUCCESS;
  10983. }
  10984. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10985. {
  10986. TBL_PC* sd = (TBL_PC*)bl;
  10987. clif_pvpset(sd, 0, 0, 2);
  10988. if (sd->pvp_timer != INVALID_TIMER) {
  10989. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10990. sd->pvp_timer = INVALID_TIMER;
  10991. }
  10992. return 0;
  10993. }
  10994. BUILDIN_FUNC(pvpoff)
  10995. {
  10996. int16 m;
  10997. const char *str;
  10998. str=script_getstr(st,2);
  10999. m = map_mapname2mapid(str);
  11000. if(m < 0 || !map[m].flag.pvp)
  11001. return SCRIPT_CMD_SUCCESS; //fixed Lupus
  11002. map[m].flag.pvp = 0;
  11003. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  11004. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  11005. return SCRIPT_CMD_SUCCESS;
  11006. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  11007. return SCRIPT_CMD_SUCCESS;
  11008. }
  11009. BUILDIN_FUNC(gvgon)
  11010. {
  11011. int16 m;
  11012. const char *str;
  11013. str=script_getstr(st,2);
  11014. m = map_mapname2mapid(str);
  11015. if(m >= 0 && !map[m].flag.gvg) {
  11016. map[m].flag.gvg = 1;
  11017. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  11018. }
  11019. return SCRIPT_CMD_SUCCESS;
  11020. }
  11021. BUILDIN_FUNC(gvgoff)
  11022. {
  11023. int16 m;
  11024. const char *str;
  11025. str=script_getstr(st,2);
  11026. m = map_mapname2mapid(str);
  11027. if(m >= 0 && map[m].flag.gvg) {
  11028. map[m].flag.gvg = 0;
  11029. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  11030. }
  11031. return SCRIPT_CMD_SUCCESS;
  11032. }
  11033. BUILDIN_FUNC(gvgon3)
  11034. {
  11035. int16 m;
  11036. const char *str;
  11037. str = script_getstr(st,2);
  11038. m = map_mapname2mapid(str);
  11039. if (m >= 0 && !map[m].flag.gvg_te) {
  11040. map[m].flag.gvg_te = 1;
  11041. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  11042. }
  11043. return SCRIPT_CMD_SUCCESS;
  11044. }
  11045. BUILDIN_FUNC(gvgoff3)
  11046. {
  11047. int16 m;
  11048. const char *str;
  11049. str = script_getstr(st,2);
  11050. m = map_mapname2mapid(str);
  11051. if (m >= 0 && map[m].flag.gvg_te) {
  11052. map[m].flag.gvg_te = 0;
  11053. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  11054. }
  11055. return SCRIPT_CMD_SUCCESS;
  11056. }
  11057. /*==========================================
  11058. * Shows an emoticon on top of the player/npc
  11059. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  11060. *------------------------------------------*/
  11061. //Optional second parameter added by [Skotlex]
  11062. BUILDIN_FUNC(emotion)
  11063. {
  11064. int type;
  11065. int player=0;
  11066. type=script_getnum(st,2);
  11067. if(type < 0 || type > 100)
  11068. return SCRIPT_CMD_SUCCESS;
  11069. if( script_hasdata(st,3) )
  11070. player=script_getnum(st,3);
  11071. if (player) {
  11072. TBL_PC *sd = NULL;
  11073. if( script_nick2sd(4,sd) ){
  11074. clif_emotion(&sd->bl, type);
  11075. }
  11076. } else
  11077. if( script_hasdata(st,4) )
  11078. {
  11079. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  11080. if(nd)
  11081. clif_emotion(&nd->bl,type);
  11082. }
  11083. else
  11084. clif_emotion(map_id2bl(st->oid),type);
  11085. return SCRIPT_CMD_SUCCESS;
  11086. }
  11087. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  11088. {
  11089. int16 m=va_arg(ap,int);
  11090. int g_id=va_arg(ap,int);
  11091. int flag=va_arg(ap,int);
  11092. if(!sd || sd->bl.m != m)
  11093. return 0;
  11094. if(
  11095. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  11096. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  11097. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  11098. )
  11099. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  11100. return 1;
  11101. }
  11102. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  11103. {
  11104. struct mob_data *md=(struct mob_data *)bl;
  11105. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  11106. status_kill(bl);
  11107. return 1;
  11108. }
  11109. /*
  11110. * Function to kick guild members out of a map and to their save points.
  11111. * m : mapid
  11112. * g_id : owner guild id
  11113. * flag & 1 : Warp out owners
  11114. * flag & 2 : Warp out outsiders
  11115. * flag & 4 : reserved for mobs
  11116. * */
  11117. BUILDIN_FUNC(maprespawnguildid)
  11118. {
  11119. const char *mapname=script_getstr(st,2);
  11120. int g_id=script_getnum(st,3);
  11121. int flag=script_getnum(st,4);
  11122. int16 m=map_mapname2mapid(mapname);
  11123. if(m == -1)
  11124. return SCRIPT_CMD_SUCCESS;
  11125. //Catch ALL players (in case some are 'between maps' on execution time)
  11126. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  11127. if (flag&4) //Remove script mobs.
  11128. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  11129. return SCRIPT_CMD_SUCCESS;
  11130. }
  11131. /// Siege commands
  11132. /**
  11133. * Start WoE:FE
  11134. * agitstart();
  11135. */
  11136. BUILDIN_FUNC(agitstart)
  11137. {
  11138. guild_agit_start();
  11139. return SCRIPT_CMD_SUCCESS;
  11140. }
  11141. /**
  11142. * End WoE:FE
  11143. * agitend();
  11144. */
  11145. BUILDIN_FUNC(agitend)
  11146. {
  11147. guild_agit_end();
  11148. return SCRIPT_CMD_SUCCESS;
  11149. }
  11150. /**
  11151. * Start WoE:SE
  11152. * agitstart2();
  11153. */
  11154. BUILDIN_FUNC(agitstart2)
  11155. {
  11156. guild_agit2_start();
  11157. return SCRIPT_CMD_SUCCESS;
  11158. }
  11159. /**
  11160. * End WoE:SE
  11161. * agitend();
  11162. */
  11163. BUILDIN_FUNC(agitend2)
  11164. {
  11165. guild_agit2_end();
  11166. return SCRIPT_CMD_SUCCESS;
  11167. }
  11168. /**
  11169. * Start WoE:TE
  11170. * agitstart3();
  11171. */
  11172. BUILDIN_FUNC(agitstart3)
  11173. {
  11174. guild_agit3_start();
  11175. return SCRIPT_CMD_SUCCESS;
  11176. }
  11177. /**
  11178. * End WoE:TE
  11179. * agitend3();
  11180. */
  11181. BUILDIN_FUNC(agitend3)
  11182. {
  11183. guild_agit3_end();
  11184. return SCRIPT_CMD_SUCCESS;
  11185. }
  11186. /**
  11187. * Returns whether WoE:FE is on or off.
  11188. * agitcheck();
  11189. */
  11190. BUILDIN_FUNC(agitcheck)
  11191. {
  11192. script_pushint(st, agit_flag);
  11193. return SCRIPT_CMD_SUCCESS;
  11194. }
  11195. /**
  11196. * Returns whether WoE:SE is on or off.
  11197. * agitcheck2();
  11198. */
  11199. BUILDIN_FUNC(agitcheck2)
  11200. {
  11201. script_pushint(st, agit2_flag);
  11202. return SCRIPT_CMD_SUCCESS;
  11203. }
  11204. /**
  11205. * Returns whether WoE:TE is on or off.
  11206. * agitcheck3();
  11207. */
  11208. BUILDIN_FUNC(agitcheck3)
  11209. {
  11210. script_pushint(st, agit3_flag);
  11211. return SCRIPT_CMD_SUCCESS;
  11212. }
  11213. /// Sets the guild_id of this npc.
  11214. ///
  11215. /// flagemblem <guild_id>;
  11216. BUILDIN_FUNC(flagemblem)
  11217. {
  11218. TBL_NPC* nd;
  11219. int g_id = script_getnum(st,2);
  11220. if(g_id < 0)
  11221. return SCRIPT_CMD_SUCCESS;
  11222. nd = (TBL_NPC*)map_id2nd(st->oid);
  11223. if( nd == NULL ) {
  11224. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  11225. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  11226. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  11227. } else {
  11228. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11229. nd->u.scr.guild_id = g_id;
  11230. clif_guild_emblem_area(&nd->bl);
  11231. /* guild flag caching */
  11232. if( g_id ) /* adding a id */
  11233. guild_flag_add(nd);
  11234. else if( changed ) /* removing a flag */
  11235. guild_flag_remove(nd);
  11236. }
  11237. return SCRIPT_CMD_SUCCESS;
  11238. }
  11239. BUILDIN_FUNC(getcastlename)
  11240. {
  11241. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11242. struct guild_castle* gc = guild_mapname2gc(mapname);
  11243. const char* name = (gc) ? gc->castle_name : "";
  11244. script_pushstrcopy(st,name);
  11245. return SCRIPT_CMD_SUCCESS;
  11246. }
  11247. BUILDIN_FUNC(getcastledata)
  11248. {
  11249. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11250. int index = script_getnum(st,3);
  11251. struct guild_castle *gc = guild_mapname2gc(mapname);
  11252. if (gc == NULL) {
  11253. script_pushint(st,0);
  11254. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11255. return SCRIPT_CMD_FAILURE;
  11256. }
  11257. switch (index) {
  11258. case 1:
  11259. script_pushint(st,gc->guild_id); break;
  11260. case 2:
  11261. script_pushint(st,gc->economy); break;
  11262. case 3:
  11263. script_pushint(st,gc->defense); break;
  11264. case 4:
  11265. script_pushint(st,gc->triggerE); break;
  11266. case 5:
  11267. script_pushint(st,gc->triggerD); break;
  11268. case 6:
  11269. script_pushint(st,gc->nextTime); break;
  11270. case 7:
  11271. script_pushint(st,gc->payTime); break;
  11272. case 8:
  11273. script_pushint(st,gc->createTime); break;
  11274. case 9:
  11275. script_pushint(st,gc->visibleC); break;
  11276. default:
  11277. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  11278. script_pushint(st,gc->guardian[index-10].visible);
  11279. break;
  11280. }
  11281. script_pushint(st,0);
  11282. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11283. return SCRIPT_CMD_FAILURE;
  11284. }
  11285. return SCRIPT_CMD_SUCCESS;
  11286. }
  11287. BUILDIN_FUNC(setcastledata)
  11288. {
  11289. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11290. int index = script_getnum(st,3);
  11291. int value = script_getnum(st,4);
  11292. struct guild_castle *gc = guild_mapname2gc(mapname);
  11293. if (gc == NULL) {
  11294. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11295. return SCRIPT_CMD_FAILURE;
  11296. }
  11297. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  11298. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11299. return SCRIPT_CMD_FAILURE;
  11300. }
  11301. guild_castledatasave(gc->castle_id, index, value);
  11302. return SCRIPT_CMD_SUCCESS;
  11303. }
  11304. /* =====================================================================
  11305. * ---------------------------------------------------------------------*/
  11306. BUILDIN_FUNC(requestguildinfo)
  11307. {
  11308. int guild_id=script_getnum(st,2);
  11309. const char *event=NULL;
  11310. if( script_hasdata(st,3) ){
  11311. event=script_getstr(st,3);
  11312. check_event(st, event);
  11313. }
  11314. if(guild_id>0)
  11315. guild_npc_request_info(guild_id,event);
  11316. return SCRIPT_CMD_SUCCESS;
  11317. }
  11318. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11319. /// getequipcardcnt(<equipment slot>);
  11320. BUILDIN_FUNC(getequipcardcnt)
  11321. {
  11322. int i=-1,j,num;
  11323. TBL_PC *sd;
  11324. int count;
  11325. if( !script_rid2sd(sd) )
  11326. return SCRIPT_CMD_SUCCESS;
  11327. num=script_getnum(st,2);
  11328. if (equip_index_check(num))
  11329. i=pc_checkequip(sd,equip_bitmask[num]);
  11330. if (i < 0 || !sd->inventory_data[i]) {
  11331. script_pushint(st,0);
  11332. return SCRIPT_CMD_SUCCESS;
  11333. }
  11334. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11335. {
  11336. script_pushint(st,0);
  11337. return SCRIPT_CMD_SUCCESS;
  11338. }
  11339. count = 0;
  11340. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  11341. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11342. count++;
  11343. script_pushint(st,count);
  11344. return SCRIPT_CMD_SUCCESS;
  11345. }
  11346. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11347. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11348. /// successremovecards <slot>;
  11349. BUILDIN_FUNC(successremovecards) {
  11350. int i=-1,c,cardflag=0;
  11351. TBL_PC* sd;
  11352. int num;
  11353. if( !script_rid2sd(sd) )
  11354. return SCRIPT_CMD_SUCCESS;
  11355. num = script_getnum(st,2);
  11356. if (equip_index_check(num))
  11357. i=pc_checkequip(sd,equip_bitmask[num]);
  11358. if (i < 0 || !sd->inventory_data[i]) {
  11359. return SCRIPT_CMD_SUCCESS;
  11360. }
  11361. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11362. return SCRIPT_CMD_SUCCESS;
  11363. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11364. 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
  11365. unsigned char flag = 0;
  11366. struct item item_tmp;
  11367. memset(&item_tmp,0,sizeof(item_tmp));
  11368. cardflag = 1;
  11369. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11370. item_tmp.identify = 1;
  11371. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11372. clif_additem(sd,0,0,flag);
  11373. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11374. }
  11375. }
  11376. }
  11377. if(cardflag == 1) {//if card was remove remplace item with no card
  11378. unsigned char flag = 0, j;
  11379. struct item item_tmp;
  11380. memset(&item_tmp,0,sizeof(item_tmp));
  11381. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11382. item_tmp.identify = 1;
  11383. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11384. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11385. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11386. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11387. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11388. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11389. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11390. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11391. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11392. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11393. }
  11394. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11395. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11396. clif_additem(sd,0,0,flag);
  11397. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11398. }
  11399. clif_misceffect(&sd->bl,3);
  11400. }
  11401. return SCRIPT_CMD_SUCCESS;
  11402. }
  11403. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11404. /// failedremovecards <slot>, <type>;
  11405. /// <type>=0 : will destroy both the item and the cards.
  11406. /// <type>=1 : will keep the item, but destroy the cards.
  11407. /// <type>=2 : will keep the cards, but destroy the item.
  11408. /// <type>=? : will just display the failure effect.
  11409. BUILDIN_FUNC(failedremovecards) {
  11410. int i=-1,c,cardflag=0;
  11411. TBL_PC* sd;
  11412. int num;
  11413. int typefail;
  11414. if( !script_rid2sd(sd) )
  11415. return SCRIPT_CMD_SUCCESS;
  11416. num = script_getnum(st,2);
  11417. typefail = script_getnum(st,3);
  11418. if (equip_index_check(num))
  11419. i=pc_checkequip(sd,equip_bitmask[num]);
  11420. if (i < 0 || !sd->inventory_data[i])
  11421. return SCRIPT_CMD_SUCCESS;
  11422. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11423. return SCRIPT_CMD_SUCCESS;
  11424. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11425. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11426. cardflag = 1;
  11427. if(typefail == 2) {// add cards to inventory, clear
  11428. unsigned char flag = 0;
  11429. struct item item_tmp;
  11430. memset(&item_tmp,0,sizeof(item_tmp));
  11431. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11432. item_tmp.identify = 1;
  11433. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11434. clif_additem(sd,0,0,flag);
  11435. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11436. }
  11437. }
  11438. }
  11439. }
  11440. if(cardflag == 1) {
  11441. if(typefail == 0 || typefail == 2){ // destroy the item
  11442. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11443. }else if(typefail == 1){ // destroy the card
  11444. unsigned char flag = 0, j;
  11445. struct item item_tmp;
  11446. memset(&item_tmp,0,sizeof(item_tmp));
  11447. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11448. item_tmp.identify = 1;
  11449. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11450. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11451. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11452. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11453. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11454. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11455. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11456. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11457. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11458. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11459. }
  11460. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11461. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11462. clif_additem(sd,0,0,flag);
  11463. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11464. }
  11465. }
  11466. clif_misceffect(&sd->bl,2);
  11467. }
  11468. return SCRIPT_CMD_SUCCESS;
  11469. }
  11470. /* ================================================================
  11471. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11472. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11473. * improved by [Lance]
  11474. * ================================================================*/
  11475. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11476. {
  11477. int x,y,m,check_val=0,check_ID=0,i=0;
  11478. struct guild *g = NULL;
  11479. struct party_data *p = NULL;
  11480. const char *str;
  11481. const char *mapname;
  11482. unsigned int index;
  11483. mapname=script_getstr(st,2);
  11484. str=script_getstr(st,3);
  11485. x=script_getnum(st,4);
  11486. y=script_getnum(st,5);
  11487. if(script_hasdata(st,7)){
  11488. check_val=script_getnum(st,6);
  11489. check_ID=script_getnum(st,7);
  11490. }
  11491. if((m=map_mapname2mapid(mapname))< 0)
  11492. return SCRIPT_CMD_SUCCESS;
  11493. if(!(index=mapindex_name2id(str)))
  11494. return SCRIPT_CMD_SUCCESS;
  11495. switch(check_val){
  11496. case 1:
  11497. g = guild_search(check_ID);
  11498. if (g){
  11499. for( i=0; i < g->max_member; i++)
  11500. {
  11501. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11502. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11503. }
  11504. }
  11505. }
  11506. break;
  11507. case 2:
  11508. p = party_search(check_ID);
  11509. if(p){
  11510. for(i=0;i<MAX_PARTY; i++){
  11511. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11512. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11513. }
  11514. }
  11515. }
  11516. break;
  11517. default:
  11518. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11519. break;
  11520. }
  11521. return SCRIPT_CMD_SUCCESS;
  11522. }
  11523. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11524. {
  11525. char *event=va_arg(ap,char *);
  11526. struct mob_data *md = ((struct mob_data *)bl);
  11527. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11528. return 1;
  11529. return SCRIPT_CMD_SUCCESS;
  11530. }
  11531. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11532. {
  11533. const char *mapname,*event;
  11534. int16 m;
  11535. mapname=script_getstr(st,2);
  11536. event=script_getstr(st,3);
  11537. if( strcmp(event, "all") == 0 )
  11538. event = NULL;
  11539. else
  11540. check_event(st, event);
  11541. if( strcmp(mapname, "this") == 0 ) {
  11542. struct map_session_data *sd;
  11543. if( script_rid2sd(sd) )
  11544. m = sd->bl.m;
  11545. else {
  11546. script_pushint(st,-1);
  11547. return SCRIPT_CMD_SUCCESS;
  11548. }
  11549. }
  11550. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11551. script_pushint(st,-1);
  11552. return SCRIPT_CMD_SUCCESS;
  11553. }
  11554. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11555. return SCRIPT_CMD_SUCCESS;
  11556. }
  11557. BUILDIN_FUNC(marriage)
  11558. {
  11559. TBL_PC *sd;
  11560. TBL_PC *p_sd;
  11561. if(!script_rid2sd(sd) || !(p_sd=map_nick2sd(script_getstr(st,2),false)) || !pc_marriage(sd,p_sd)){
  11562. script_pushint(st,0);
  11563. return SCRIPT_CMD_SUCCESS;
  11564. }
  11565. script_pushint(st,1);
  11566. return SCRIPT_CMD_SUCCESS;
  11567. }
  11568. BUILDIN_FUNC(wedding_effect)
  11569. {
  11570. TBL_PC *sd;
  11571. struct block_list *bl;
  11572. if(!script_rid2sd(sd)) {
  11573. bl=map_id2bl(st->oid);
  11574. } else
  11575. bl=&sd->bl;
  11576. clif_wedding_effect(bl);
  11577. return SCRIPT_CMD_SUCCESS;
  11578. }
  11579. /**
  11580. * divorce({<char_id>})
  11581. **/
  11582. BUILDIN_FUNC(divorce)
  11583. {
  11584. TBL_PC *sd;
  11585. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11586. script_pushint(st,0);
  11587. return SCRIPT_CMD_FAILURE;
  11588. }
  11589. script_pushint(st,1);
  11590. return SCRIPT_CMD_SUCCESS;
  11591. }
  11592. /**
  11593. * ispartneron({<char_id>})
  11594. **/
  11595. BUILDIN_FUNC(ispartneron)
  11596. {
  11597. TBL_PC *sd;
  11598. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11599. map_charid2sd(sd->status.partner_id) == NULL)
  11600. {
  11601. script_pushint(st,0);
  11602. return SCRIPT_CMD_FAILURE;
  11603. }
  11604. script_pushint(st,1);
  11605. return SCRIPT_CMD_SUCCESS;
  11606. }
  11607. /**
  11608. * getpartnerid({<char_id>})
  11609. **/
  11610. BUILDIN_FUNC(getpartnerid)
  11611. {
  11612. TBL_PC *sd;
  11613. if (!script_charid2sd(2,sd)) {
  11614. script_pushint(st,0);
  11615. return SCRIPT_CMD_FAILURE;
  11616. }
  11617. script_pushint(st,sd->status.partner_id);
  11618. return SCRIPT_CMD_SUCCESS;
  11619. }
  11620. /**
  11621. * getchildid({<char_id>})
  11622. **/
  11623. BUILDIN_FUNC(getchildid)
  11624. {
  11625. TBL_PC *sd;
  11626. if (!script_charid2sd(2,sd)) {
  11627. script_pushint(st,0);
  11628. return SCRIPT_CMD_FAILURE;
  11629. }
  11630. script_pushint(st,sd->status.child);
  11631. return SCRIPT_CMD_SUCCESS;
  11632. }
  11633. /**
  11634. * getmotherid({<char_id>})
  11635. **/
  11636. BUILDIN_FUNC(getmotherid)
  11637. {
  11638. TBL_PC *sd;
  11639. if (!script_charid2sd(2,sd)) {
  11640. script_pushint(st,0);
  11641. return SCRIPT_CMD_FAILURE;
  11642. }
  11643. script_pushint(st,sd->status.mother);
  11644. return SCRIPT_CMD_SUCCESS;
  11645. }
  11646. /**
  11647. * getfatherid({<char_id>})
  11648. **/
  11649. BUILDIN_FUNC(getfatherid)
  11650. {
  11651. TBL_PC *sd;
  11652. if (!script_charid2sd(2,sd)) {
  11653. script_pushint(st,0);
  11654. return SCRIPT_CMD_FAILURE;
  11655. }
  11656. script_pushint(st,sd->status.father);
  11657. return SCRIPT_CMD_SUCCESS;
  11658. }
  11659. BUILDIN_FUNC(warppartner)
  11660. {
  11661. int x,y;
  11662. unsigned short mapindex;
  11663. const char *str;
  11664. TBL_PC *sd;
  11665. TBL_PC *p_sd;
  11666. if(!script_rid2sd(sd) || !pc_ismarried(sd) ||
  11667. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11668. script_pushint(st,0);
  11669. return SCRIPT_CMD_SUCCESS;
  11670. }
  11671. str=script_getstr(st,2);
  11672. x=script_getnum(st,3);
  11673. y=script_getnum(st,4);
  11674. mapindex = mapindex_name2id(str);
  11675. if (mapindex) {
  11676. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11677. script_pushint(st,1);
  11678. } else
  11679. script_pushint(st,0);
  11680. return SCRIPT_CMD_SUCCESS;
  11681. }
  11682. /*================================================
  11683. * Script for Displaying MOB Information [Valaris]
  11684. *------------------------------------------------*/
  11685. BUILDIN_FUNC(strmobinfo)
  11686. {
  11687. int num=script_getnum(st,2);
  11688. int class_=script_getnum(st,3);
  11689. if(!mobdb_checkid(class_))
  11690. {
  11691. if (num < 3) //requested a string
  11692. script_pushconststr(st,"");
  11693. else
  11694. script_pushint(st,0);
  11695. return SCRIPT_CMD_SUCCESS;
  11696. }
  11697. switch (num) {
  11698. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11699. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11700. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11701. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11702. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11703. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11704. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11705. default:
  11706. script_pushint(st,0);
  11707. break;
  11708. }
  11709. return SCRIPT_CMD_SUCCESS;
  11710. }
  11711. /*==========================================
  11712. * Summon guardians [Valaris]
  11713. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11714. *------------------------------------------*/
  11715. BUILDIN_FUNC(guardian)
  11716. {
  11717. int class_=0,x=0,y=0,guardian=0;
  11718. const char *str,*mapname,*evt="";
  11719. bool has_index = false;
  11720. mapname =script_getstr(st,2);
  11721. x =script_getnum(st,3);
  11722. y =script_getnum(st,4);
  11723. str =script_getstr(st,5);
  11724. class_=script_getnum(st,6);
  11725. if( script_hasdata(st,8) )
  11726. {// "<event label>",<guardian index>
  11727. evt=script_getstr(st,7);
  11728. guardian=script_getnum(st,8);
  11729. has_index = true;
  11730. } else if( script_hasdata(st,7) ){
  11731. struct script_data *data = script_getdata(st,7);
  11732. get_val(st,data);
  11733. if( data_isstring(data) )
  11734. {// "<event label>"
  11735. evt=script_getstr(st,7);
  11736. } else if( data_isint(data) )
  11737. {// <guardian index>
  11738. guardian=script_getnum(st,7);
  11739. has_index = true;
  11740. } else {
  11741. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11742. script_reportdata(data);
  11743. return SCRIPT_CMD_FAILURE;
  11744. }
  11745. }
  11746. check_event(st, evt);
  11747. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11748. return SCRIPT_CMD_SUCCESS;
  11749. }
  11750. /*==========================================
  11751. * Invisible Walls [Zephyrus]
  11752. *------------------------------------------*/
  11753. BUILDIN_FUNC(setwall)
  11754. {
  11755. const char *mapname, *name;
  11756. int x, y, m, size, dir;
  11757. bool shootable;
  11758. mapname = script_getstr(st,2);
  11759. x = script_getnum(st,3);
  11760. y = script_getnum(st,4);
  11761. size = script_getnum(st,5);
  11762. dir = script_getnum(st,6);
  11763. shootable = script_getnum(st,7);
  11764. name = script_getstr(st,8);
  11765. if( (m = map_mapname2mapid(mapname)) < 0 )
  11766. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11767. map_iwall_set(m, x, y, size, dir, shootable, name);
  11768. return SCRIPT_CMD_SUCCESS;
  11769. }
  11770. BUILDIN_FUNC(delwall)
  11771. {
  11772. const char *name = script_getstr(st,2);
  11773. map_iwall_remove(name);
  11774. return SCRIPT_CMD_SUCCESS;
  11775. }
  11776. /// Retrieves various information about the specified guardian.
  11777. ///
  11778. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11779. /// type: 0 - whether it is deployed or not
  11780. /// 1 - maximum hp
  11781. /// 2 - current hp
  11782. ///
  11783. BUILDIN_FUNC(guardianinfo)
  11784. {
  11785. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11786. int id = script_getnum(st,3);
  11787. int type = script_getnum(st,4);
  11788. struct guild_castle* gc = guild_mapname2gc(mapname);
  11789. struct mob_data* gd;
  11790. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11791. {
  11792. script_pushint(st,-1);
  11793. return SCRIPT_CMD_SUCCESS;
  11794. }
  11795. if( type == 0 )
  11796. script_pushint(st, gc->guardian[id].visible);
  11797. else
  11798. if( !gc->guardian[id].visible )
  11799. script_pushint(st,-1);
  11800. else
  11801. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11802. script_pushint(st,-1);
  11803. else
  11804. {
  11805. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11806. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11807. else
  11808. script_pushint(st,-1);
  11809. }
  11810. return SCRIPT_CMD_SUCCESS;
  11811. }
  11812. /*==========================================
  11813. * Get the item name by item_id or null
  11814. *------------------------------------------*/
  11815. BUILDIN_FUNC(getitemname)
  11816. {
  11817. unsigned short item_id = 0;
  11818. struct item_data *i_data;
  11819. char *item_name;
  11820. struct script_data *data;
  11821. data=script_getdata(st,2);
  11822. get_val(st,data);
  11823. if( data_isstring(data) ){
  11824. const char *name=conv_str(st,data);
  11825. struct item_data *item_data = itemdb_searchname(name);
  11826. if( item_data )
  11827. item_id=item_data->nameid;
  11828. }else
  11829. item_id=conv_num(st,data);
  11830. i_data = itemdb_exists(item_id);
  11831. if (i_data == NULL)
  11832. {
  11833. script_pushconststr(st,"null");
  11834. return SCRIPT_CMD_SUCCESS;
  11835. }
  11836. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11837. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11838. script_pushstr(st,item_name);
  11839. return SCRIPT_CMD_SUCCESS;
  11840. }
  11841. /*==========================================
  11842. * Returns number of slots an item has. [Skotlex]
  11843. *------------------------------------------*/
  11844. BUILDIN_FUNC(getitemslots)
  11845. {
  11846. unsigned short item_id;
  11847. struct item_data *i_data;
  11848. item_id=script_getnum(st,2);
  11849. i_data = itemdb_exists(item_id);
  11850. if (i_data)
  11851. script_pushint(st,i_data->slot);
  11852. else
  11853. script_pushint(st,-1);
  11854. return SCRIPT_CMD_SUCCESS;
  11855. }
  11856. // TODO: add matk here if needed/once we get rid of RENEWAL
  11857. /*==========================================
  11858. * Returns some values of an item [Lupus]
  11859. * Price, Weight, etc...
  11860. getiteminfo(itemID,n), where n
  11861. 0 value_buy;
  11862. 1 value_sell;
  11863. 2 type;
  11864. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11865. if = 0, then monsters don't drop it at all (rare or a quest item)
  11866. if = -1, then this item is sold in NPC shops only
  11867. 4 sex;
  11868. 5 equip;
  11869. 6 weight;
  11870. 7 atk;
  11871. 8 def;
  11872. 9 range;
  11873. 10 slot;
  11874. 11 look;
  11875. 12 elv;
  11876. 13 wlv;
  11877. 14 view id
  11878. 15 eLvmax
  11879. 16 matk (renewal)
  11880. *------------------------------------------*/
  11881. BUILDIN_FUNC(getiteminfo)
  11882. {
  11883. unsigned short item_id,n;
  11884. struct item_data *i_data;
  11885. item_id = script_getnum(st,2);
  11886. n = script_getnum(st,3);
  11887. i_data = itemdb_exists(item_id);
  11888. if (i_data && n <= 16) {
  11889. int *item_arr = (int*)&i_data->value_buy;
  11890. #ifndef RENEWAL
  11891. if (n == 16)
  11892. script_pushint(st,0);
  11893. else
  11894. #endif
  11895. script_pushint(st,item_arr[n]);
  11896. } else
  11897. script_pushint(st,-1);
  11898. return SCRIPT_CMD_SUCCESS;
  11899. }
  11900. /*==========================================
  11901. * Set some values of an item [Lupus]
  11902. * Price, Weight, etc...
  11903. setiteminfo(itemID,n,Value), where n
  11904. 0 value_buy;
  11905. 1 value_sell;
  11906. 2 type;
  11907. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11908. if = 0, then monsters don't drop it at all (rare or a quest item)
  11909. if = -1, then this item is sold in NPC shops only
  11910. 4 sex;
  11911. 5 equip;
  11912. 6 weight;
  11913. 7 atk;
  11914. 8 def;
  11915. 9 range;
  11916. 10 slot;
  11917. 11 look;
  11918. 12 elv;
  11919. 13 wlv;
  11920. 14 view id
  11921. * Returns Value or -1 if the wrong field's been set
  11922. *------------------------------------------*/
  11923. BUILDIN_FUNC(setiteminfo)
  11924. {
  11925. unsigned short item_id;
  11926. int n,value;
  11927. struct item_data *i_data;
  11928. item_id = script_getnum(st,2);
  11929. n = script_getnum(st,3);
  11930. value = script_getnum(st,4);
  11931. i_data = itemdb_exists(item_id);
  11932. if (i_data && n>=0 && n<=14) {
  11933. int *item_arr = (int*)&i_data->value_buy;
  11934. item_arr[n] = value;
  11935. script_pushint(st,value);
  11936. } else
  11937. script_pushint(st,-1);
  11938. return SCRIPT_CMD_SUCCESS;
  11939. }
  11940. /*==========================================
  11941. * Returns value from equipped item slot n [Lupus]
  11942. getequipcardid(num,slot)
  11943. where
  11944. num = eqip position slot
  11945. slot = 0,1,2,3 (Card Slot N)
  11946. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11947. it's useful when you want to check item cards or if it's signed
  11948. Useful for such quests as "Sign this refined item with players name" etc
  11949. Hat[0] +4 -> Player's Hat[0] +4
  11950. *------------------------------------------*/
  11951. BUILDIN_FUNC(getequipcardid)
  11952. {
  11953. int i=-1,num,slot;
  11954. TBL_PC *sd;
  11955. if( !script_rid2sd(sd) )
  11956. return SCRIPT_CMD_SUCCESS;
  11957. num=script_getnum(st,2);
  11958. slot=script_getnum(st,3);
  11959. if (equip_index_check(num))
  11960. i=pc_checkequip(sd,equip_bitmask[num]);
  11961. if(i >= 0 && slot>=0 && slot<4)
  11962. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  11963. else
  11964. script_pushint(st,0);
  11965. return SCRIPT_CMD_SUCCESS;
  11966. }
  11967. /*==========================================
  11968. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11969. *------------------------------------------*/
  11970. BUILDIN_FUNC(petskillbonus)
  11971. {
  11972. struct pet_data *pd;
  11973. TBL_PC *sd;
  11974. if(!script_rid2sd(sd) || sd->pd == NULL)
  11975. return SCRIPT_CMD_FAILURE;
  11976. pd = sd->pd;
  11977. if (pd->bonus)
  11978. { //Clear previous bonus
  11979. if (pd->bonus->timer != INVALID_TIMER)
  11980. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11981. } else //init
  11982. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11983. pd->bonus->type = script_getnum(st,2);
  11984. pd->bonus->val = script_getnum(st,3);
  11985. pd->bonus->duration = script_getnum(st,4);
  11986. pd->bonus->delay = script_getnum(st,5);
  11987. if (pd->state.skillbonus == 1)
  11988. pd->state.skillbonus = 0; // waiting state
  11989. // wait for timer to start
  11990. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11991. pd->bonus->timer = INVALID_TIMER;
  11992. else
  11993. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11994. return SCRIPT_CMD_SUCCESS;
  11995. }
  11996. /*==========================================
  11997. * pet looting [Valaris] //Rewritten by [Skotlex]
  11998. *------------------------------------------*/
  11999. BUILDIN_FUNC(petloot)
  12000. {
  12001. int max;
  12002. struct pet_data *pd;
  12003. TBL_PC *sd;
  12004. if(!script_rid2sd(sd) || sd->pd==NULL)
  12005. return SCRIPT_CMD_SUCCESS;
  12006. max=script_getnum(st,2);
  12007. if(max < 1)
  12008. max = 1; //Let'em loot at least 1 item.
  12009. else if (max > MAX_PETLOOT_SIZE)
  12010. max = MAX_PETLOOT_SIZE;
  12011. pd = sd->pd;
  12012. if (pd->loot != NULL)
  12013. { //Release whatever was there already and reallocate memory
  12014. pet_lootitem_drop(pd, pd->master);
  12015. aFree(pd->loot->item);
  12016. }
  12017. else
  12018. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  12019. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  12020. pd->loot->max=max;
  12021. pd->loot->count = 0;
  12022. pd->loot->weight = 0;
  12023. return SCRIPT_CMD_SUCCESS;
  12024. }
  12025. /*==========================================
  12026. * Set arrays with info of all sd inventory :
  12027. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  12028. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  12029. * @inventorylist_card(0..3), @inventorylist_expire
  12030. * @inventorylist_count = scalar
  12031. *------------------------------------------*/
  12032. BUILDIN_FUNC(getinventorylist)
  12033. {
  12034. TBL_PC *sd;
  12035. char card_var[NAME_LENGTH];
  12036. int i,j=0,k;
  12037. if (!script_charid2sd(2,sd))
  12038. return SCRIPT_CMD_FAILURE;
  12039. for(i=0;i<MAX_INVENTORY;i++){
  12040. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  12041. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  12042. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  12043. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  12044. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  12045. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  12046. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  12047. for (k = 0; k < MAX_SLOTS; k++)
  12048. {
  12049. sprintf(card_var, "@inventorylist_card%d",k+1);
  12050. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  12051. }
  12052. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  12053. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  12054. j++;
  12055. }
  12056. }
  12057. pc_setreg(sd,add_str("@inventorylist_count"),j);
  12058. return SCRIPT_CMD_SUCCESS;
  12059. }
  12060. /**
  12061. * getskilllist ({<char_id>});
  12062. **/
  12063. BUILDIN_FUNC(getskilllist)
  12064. {
  12065. TBL_PC *sd;
  12066. int i,j=0;
  12067. if (!script_charid2sd(2,sd))
  12068. return SCRIPT_CMD_FAILURE;
  12069. for(i=0;i<MAX_SKILL;i++){
  12070. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  12071. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  12072. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  12073. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  12074. j++;
  12075. }
  12076. }
  12077. pc_setreg(sd,add_str("@skilllist_count"),j);
  12078. return SCRIPT_CMD_SUCCESS;
  12079. }
  12080. /**
  12081. * clearitem ({<char_id>});
  12082. **/
  12083. BUILDIN_FUNC(clearitem)
  12084. {
  12085. TBL_PC *sd;
  12086. int i;
  12087. if (!script_charid2sd(2,sd))
  12088. return SCRIPT_CMD_FAILURE;
  12089. for (i=0; i<MAX_INVENTORY; i++) {
  12090. if (sd->inventory.u.items_inventory[i].amount) {
  12091. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  12092. }
  12093. }
  12094. return SCRIPT_CMD_SUCCESS;
  12095. }
  12096. /**
  12097. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  12098. * disguise <Monster ID>{,<char_id>};
  12099. **/
  12100. BUILDIN_FUNC(disguise)
  12101. {
  12102. int id;
  12103. TBL_PC* sd;
  12104. if (!script_charid2sd(3,sd))
  12105. return SCRIPT_CMD_FAILURE;
  12106. id = script_getnum(st,2);
  12107. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  12108. pc_disguise(sd, id);
  12109. script_pushint(st,id);
  12110. } else
  12111. script_pushint(st,0);
  12112. return SCRIPT_CMD_SUCCESS;
  12113. }
  12114. /**
  12115. * Undisguise Player (returns 1 if success, 0 on fail)
  12116. * undisguise {<char_id>};
  12117. **/
  12118. BUILDIN_FUNC(undisguise)
  12119. {
  12120. TBL_PC* sd;
  12121. if (!script_charid2sd(2,sd))
  12122. return SCRIPT_CMD_FAILURE;
  12123. if (sd->disguise) {
  12124. pc_disguise(sd, 0);
  12125. script_pushint(st,0);
  12126. } else {
  12127. script_pushint(st,1);
  12128. }
  12129. return SCRIPT_CMD_SUCCESS;
  12130. }
  12131. /**
  12132. * Transform an NPC to another _class
  12133. *
  12134. * classchange(<view id>{,"<NPC name>","<flag>"});
  12135. * @param flag: Specify target
  12136. * BC_AREA - Sprite is sent to players in the vicinity of the source (default).
  12137. * BC_SELF - Sprite is sent only to player attached.
  12138. */
  12139. BUILDIN_FUNC(classchange)
  12140. {
  12141. int _class, type = 1;
  12142. struct npc_data* nd = NULL;
  12143. TBL_PC *sd = map_id2sd(st->rid);
  12144. send_target target = AREA;
  12145. _class = script_getnum(st,2);
  12146. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  12147. nd = npc_name2id(script_getstr(st, 3));
  12148. else
  12149. nd = (struct npc_data *)map_id2bl(st->oid);
  12150. if (nd == NULL)
  12151. return SCRIPT_CMD_FAILURE;
  12152. if (script_hasdata(st, 4)) {
  12153. switch(script_getnum(st, 4)) {
  12154. case BC_SELF: target = SELF; break;
  12155. case BC_AREA:
  12156. default: target = AREA; break;
  12157. }
  12158. }
  12159. if (target != SELF)
  12160. clif_class_change(&nd->bl,_class,type);
  12161. else if (sd == NULL)
  12162. return SCRIPT_CMD_FAILURE;
  12163. else
  12164. clif_class_change_target(&nd->bl,_class,type,target,sd);
  12165. return SCRIPT_CMD_SUCCESS;
  12166. }
  12167. /*==========================================
  12168. * Display an effect
  12169. *------------------------------------------*/
  12170. BUILDIN_FUNC(misceffect)
  12171. {
  12172. int type;
  12173. type=script_getnum(st,2);
  12174. if(st->oid && st->oid != fake_nd->bl.id) {
  12175. struct block_list *bl = map_id2bl(st->oid);
  12176. if (bl)
  12177. clif_specialeffect(bl,type,AREA);
  12178. } else{
  12179. TBL_PC *sd;
  12180. if(script_rid2sd(sd))
  12181. clif_specialeffect(&sd->bl,type,AREA);
  12182. }
  12183. return SCRIPT_CMD_SUCCESS;
  12184. }
  12185. /*==========================================
  12186. * Play a BGM on a single client [Rikter/Yommy]
  12187. *------------------------------------------*/
  12188. BUILDIN_FUNC(playBGM)
  12189. {
  12190. struct map_session_data* sd;
  12191. if( script_rid2sd(sd) ) {
  12192. clif_playBGM(sd, script_getstr(st,2));
  12193. }
  12194. return SCRIPT_CMD_SUCCESS;
  12195. }
  12196. static int playBGM_sub(struct block_list* bl,va_list ap)
  12197. {
  12198. const char* name = va_arg(ap,const char*);
  12199. clif_playBGM(BL_CAST(BL_PC, bl), name);
  12200. return 0;
  12201. }
  12202. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  12203. {
  12204. const char* name = va_arg(args, const char*);
  12205. clif_playBGM(sd, name);
  12206. return 0;
  12207. }
  12208. /*==========================================
  12209. * Play a BGM on multiple client [Rikter/Yommy]
  12210. *------------------------------------------*/
  12211. BUILDIN_FUNC(playBGMall)
  12212. {
  12213. const char* name;
  12214. name = script_getstr(st,2);
  12215. if( script_hasdata(st,7) ) {// specified part of map
  12216. const char* mapname = script_getstr(st,3);
  12217. int x0 = script_getnum(st,4);
  12218. int y0 = script_getnum(st,5);
  12219. int x1 = script_getnum(st,6);
  12220. int y1 = script_getnum(st,7);
  12221. map_foreachinallarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  12222. }
  12223. else if( script_hasdata(st,3) ) {// entire map
  12224. const char* mapname = script_getstr(st,3);
  12225. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  12226. }
  12227. else {// entire server
  12228. map_foreachpc(&playBGM_foreachpc_sub, name);
  12229. }
  12230. return SCRIPT_CMD_SUCCESS;
  12231. }
  12232. /*==========================================
  12233. * Play a .wav sound for sd
  12234. *------------------------------------------*/
  12235. BUILDIN_FUNC(soundeffect)
  12236. {
  12237. TBL_PC* sd;
  12238. if(script_rid2sd(sd)){
  12239. const char* name = script_getstr(st,2);
  12240. int type = script_getnum(st,3);
  12241. clif_soundeffect(sd,&sd->bl,name,type);
  12242. }
  12243. return SCRIPT_CMD_SUCCESS;
  12244. }
  12245. int soundeffect_sub(struct block_list* bl,va_list ap)
  12246. {
  12247. char* name = va_arg(ap,char*);
  12248. int type = va_arg(ap,int);
  12249. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  12250. return 0;
  12251. }
  12252. /*==========================================
  12253. * Play a sound effect (.wav) on multiple clients
  12254. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  12255. *------------------------------------------*/
  12256. BUILDIN_FUNC(soundeffectall)
  12257. {
  12258. struct block_list* bl;
  12259. struct map_session_data* sd;
  12260. const char* name;
  12261. int type;
  12262. if( st->rid && script_rid2sd(sd) )
  12263. bl = &sd->bl;
  12264. else
  12265. bl = map_id2bl(st->oid);
  12266. if (!bl)
  12267. return SCRIPT_CMD_SUCCESS;
  12268. name = script_getstr(st,2);
  12269. type = script_getnum(st,3);
  12270. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12271. if(!script_hasdata(st,4))
  12272. { // area around
  12273. clif_soundeffectall(bl, name, type, AREA);
  12274. }
  12275. else
  12276. if(!script_hasdata(st,5))
  12277. { // entire map
  12278. const char* mapname = script_getstr(st,4);
  12279. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  12280. }
  12281. else
  12282. if(script_hasdata(st,8))
  12283. { // specified part of map
  12284. const char* mapname = script_getstr(st,4);
  12285. int x0 = script_getnum(st,5);
  12286. int y0 = script_getnum(st,6);
  12287. int x1 = script_getnum(st,7);
  12288. int y1 = script_getnum(st,8);
  12289. map_foreachinallarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  12290. }
  12291. else
  12292. {
  12293. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  12294. }
  12295. return SCRIPT_CMD_SUCCESS;
  12296. }
  12297. /*==========================================
  12298. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  12299. *------------------------------------------*/
  12300. BUILDIN_FUNC(petrecovery)
  12301. {
  12302. struct pet_data *pd;
  12303. TBL_PC *sd;
  12304. int sc;
  12305. if(!script_rid2sd(sd) || sd->pd == NULL)
  12306. return SCRIPT_CMD_FAILURE;
  12307. sc = script_getnum(st,2);
  12308. if (sc <= SC_NONE || sc >= SC_MAX) {
  12309. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  12310. return SCRIPT_CMD_FAILURE;
  12311. }
  12312. pd = sd->pd;
  12313. if (pd->recovery)
  12314. { //Halt previous bonus
  12315. if (pd->recovery->timer != INVALID_TIMER)
  12316. delete_timer(pd->recovery->timer, pet_recovery_timer);
  12317. } else //Init
  12318. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  12319. pd->recovery->type = (sc_type)sc;
  12320. pd->recovery->delay = script_getnum(st,3);
  12321. pd->recovery->timer = INVALID_TIMER;
  12322. return SCRIPT_CMD_SUCCESS;
  12323. }
  12324. /*==========================================
  12325. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12326. *------------------------------------------*/
  12327. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12328. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12329. BUILDIN_FUNC(petskillattack)
  12330. {
  12331. struct pet_data *pd;
  12332. struct script_data *data;
  12333. TBL_PC *sd;
  12334. int id = 0;
  12335. if(!script_rid2sd(sd) || sd->pd == NULL)
  12336. return SCRIPT_CMD_FAILURE;
  12337. data = script_getdata(st, 2);
  12338. get_val(st, data);
  12339. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12340. if (!id) {
  12341. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12342. return SCRIPT_CMD_FAILURE;
  12343. }
  12344. pd = sd->pd;
  12345. if (pd->a_skill == NULL)
  12346. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12347. pd->a_skill->id = id;
  12348. pd->a_skill->damage = 0;
  12349. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12350. pd->a_skill->div_ = 0;
  12351. pd->a_skill->rate = script_getnum(st,4);
  12352. pd->a_skill->bonusrate = script_getnum(st,5);
  12353. return SCRIPT_CMD_SUCCESS;
  12354. }
  12355. /*==========================================
  12356. * pet attack skills [Valaris]
  12357. *------------------------------------------*/
  12358. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12359. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12360. BUILDIN_FUNC(petskillattack2)
  12361. {
  12362. struct pet_data *pd;
  12363. struct script_data *data;
  12364. TBL_PC *sd;
  12365. int id = 0;
  12366. if(!script_rid2sd(sd) || sd->pd == NULL)
  12367. return SCRIPT_CMD_FAILURE;
  12368. data = script_getdata(st, 2);
  12369. get_val(st, data);
  12370. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12371. if (!id) {
  12372. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12373. return SCRIPT_CMD_FAILURE;
  12374. }
  12375. pd = sd->pd;
  12376. if (pd->a_skill == NULL)
  12377. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12378. pd->a_skill->id = id;
  12379. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12380. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12381. pd->a_skill->div_ = script_getnum(st,4);
  12382. pd->a_skill->rate = script_getnum(st,5);
  12383. pd->a_skill->bonusrate = script_getnum(st,6);
  12384. return SCRIPT_CMD_SUCCESS;
  12385. }
  12386. /*==========================================
  12387. * pet support skills [Skotlex]
  12388. *------------------------------------------*/
  12389. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12390. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12391. BUILDIN_FUNC(petskillsupport)
  12392. {
  12393. struct pet_data *pd;
  12394. struct script_data *data;
  12395. TBL_PC *sd;
  12396. int id = 0;
  12397. if(!script_rid2sd(sd) || sd->pd == NULL)
  12398. return SCRIPT_CMD_FAILURE;
  12399. data = script_getdata(st, 2);
  12400. get_val(st, data);
  12401. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12402. if (!id) {
  12403. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12404. return SCRIPT_CMD_FAILURE;
  12405. }
  12406. pd = sd->pd;
  12407. if (pd->s_skill)
  12408. { //Clear previous skill
  12409. if (pd->s_skill->timer != INVALID_TIMER)
  12410. {
  12411. if (pd->s_skill->id)
  12412. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12413. else
  12414. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12415. }
  12416. } else //init memory
  12417. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12418. pd->s_skill->id = id;
  12419. pd->s_skill->lv = script_getnum(st,3);
  12420. pd->s_skill->delay = script_getnum(st,4);
  12421. pd->s_skill->hp = script_getnum(st,5);
  12422. pd->s_skill->sp = script_getnum(st,6);
  12423. //Use delay as initial offset to avoid skill/heal exploits
  12424. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12425. pd->s_skill->timer = INVALID_TIMER;
  12426. else
  12427. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12428. return SCRIPT_CMD_SUCCESS;
  12429. }
  12430. /*==========================================
  12431. * Scripted skill effects [Celest]
  12432. *------------------------------------------*/
  12433. /// skilleffect <skill id>,<level>
  12434. /// skilleffect "<skill name>",<level>
  12435. BUILDIN_FUNC(skilleffect)
  12436. {
  12437. TBL_PC *sd;
  12438. uint16 skill_id, skill_lv;
  12439. struct script_data *data;
  12440. if( !script_rid2sd(sd) )
  12441. return SCRIPT_CMD_SUCCESS;
  12442. data = script_getdata(st, 2);
  12443. get_val(st, data); // Convert into value in case of a variable
  12444. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12445. skill_lv = script_getnum(st,3);
  12446. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12447. * which leaves the server thinking it still is sitting. */
  12448. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12449. skill_sit(sd, 0);
  12450. clif_standing(&sd->bl);
  12451. }
  12452. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  12453. return SCRIPT_CMD_SUCCESS;
  12454. }
  12455. /*==========================================
  12456. * NPC skill effects [Valaris]
  12457. *------------------------------------------*/
  12458. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12459. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12460. BUILDIN_FUNC(npcskilleffect)
  12461. {
  12462. struct block_list *bl= map_id2bl(st->oid);
  12463. uint16 skill_id, skill_lv;
  12464. int x, y;
  12465. struct script_data *data = script_getdata(st, 2);
  12466. get_val(st, data); // Convert into value in case of a variable
  12467. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12468. skill_lv=script_getnum(st,3);
  12469. x=script_getnum(st,4);
  12470. y=script_getnum(st,5);
  12471. if (bl)
  12472. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12473. return SCRIPT_CMD_SUCCESS;
  12474. }
  12475. /*==========================================
  12476. * Special effects [Valaris]
  12477. *------------------------------------------*/
  12478. BUILDIN_FUNC(specialeffect)
  12479. {
  12480. struct block_list *bl=map_id2bl(st->oid);
  12481. int type = script_getnum(st,2);
  12482. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12483. if(bl==NULL)
  12484. return SCRIPT_CMD_SUCCESS;
  12485. if( script_hasdata(st,4) )
  12486. {
  12487. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12488. if(nd)
  12489. clif_specialeffect(&nd->bl, type, target);
  12490. }
  12491. else
  12492. {
  12493. if (target == SELF) {
  12494. TBL_PC *sd;
  12495. if (script_rid2sd(sd))
  12496. clif_specialeffect_single(bl,type,sd->fd);
  12497. } else {
  12498. clif_specialeffect(bl, type, target);
  12499. }
  12500. }
  12501. return SCRIPT_CMD_SUCCESS;
  12502. }
  12503. BUILDIN_FUNC(specialeffect2)
  12504. {
  12505. TBL_PC *sd;
  12506. if( script_nick2sd(4,sd) ){
  12507. int type = script_getnum(st,2);
  12508. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12509. clif_specialeffect(&sd->bl, type, target);
  12510. }
  12511. return SCRIPT_CMD_SUCCESS;
  12512. }
  12513. /**
  12514. * nude({<char_id>});
  12515. * @author [Valaris]
  12516. **/
  12517. BUILDIN_FUNC(nude)
  12518. {
  12519. TBL_PC *sd;
  12520. int i, calcflag = 0;
  12521. if (!script_charid2sd(2,sd))
  12522. return SCRIPT_CMD_FAILURE;
  12523. for( i = 0 ; i < EQI_MAX; i++ ) {
  12524. if( sd->equip_index[ i ] >= 0 ) {
  12525. if( !calcflag )
  12526. calcflag = 1;
  12527. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12528. }
  12529. }
  12530. if( calcflag )
  12531. status_calc_pc(sd,SCO_NONE);
  12532. return SCRIPT_CMD_SUCCESS;
  12533. }
  12534. int atcommand_sub(struct script_state* st,int type) {
  12535. TBL_PC *sd, dummy_sd;
  12536. int fd;
  12537. const char *cmd;
  12538. cmd = script_getstr(st,2);
  12539. if (st->rid) {
  12540. if( !script_rid2sd(sd) )
  12541. return SCRIPT_CMD_SUCCESS;
  12542. fd = sd->fd;
  12543. } else { //Use a dummy character.
  12544. sd = &dummy_sd;
  12545. fd = 0;
  12546. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12547. if (st->oid) {
  12548. struct block_list* bl = map_id2bl(st->oid);
  12549. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12550. if (bl->type == BL_NPC)
  12551. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12552. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : mapindex_name2id(map_default.mapname);
  12553. }
  12554. // Init Group ID, Level, & permissions
  12555. sd->group_id = sd->group_level = 99;
  12556. sd->permissions |= PC_PERM_ALLPERMISSION;
  12557. }
  12558. if (!is_atcommand(fd, sd, cmd, type)) {
  12559. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12560. script_reportsrc(st);
  12561. return SCRIPT_CMD_FAILURE;
  12562. }
  12563. return SCRIPT_CMD_SUCCESS;
  12564. }
  12565. /*==========================================
  12566. * gmcommand [MouseJstr]
  12567. *------------------------------------------*/
  12568. BUILDIN_FUNC(atcommand) {
  12569. return atcommand_sub(st,0);
  12570. }
  12571. /** Displays a message for the player only (like system messages like "you got an apple" )
  12572. * dispbottom("<message>"{,<color>{,<char_id>}})
  12573. * @param message
  12574. * @param color Hex color default (Green)
  12575. */
  12576. BUILDIN_FUNC(dispbottom)
  12577. {
  12578. TBL_PC *sd;
  12579. int color = 0;
  12580. const char *message;
  12581. if (!script_charid2sd(4,sd))
  12582. return SCRIPT_CMD_FAILURE;
  12583. message = script_getstr(st,2);
  12584. if (script_hasdata(st,3))
  12585. color = script_getnum(st,3); // <color>
  12586. if(sd) {
  12587. if (script_hasdata(st,3))
  12588. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  12589. else
  12590. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  12591. }
  12592. return SCRIPT_CMD_SUCCESS;
  12593. }
  12594. /*===================================
  12595. * Heal portion of recovery command
  12596. *-----------------------------------*/
  12597. int recovery_sub(struct map_session_data* sd, int revive)
  12598. {
  12599. if(revive&(1|4) && pc_isdead(sd)) {
  12600. status_revive(&sd->bl, 100, 100);
  12601. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12602. clif_specialeffect(&sd->bl, 77, AREA);
  12603. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12604. status_percent_heal(&sd->bl, 100, 100);
  12605. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12606. }
  12607. return SCRIPT_CMD_SUCCESS;
  12608. }
  12609. /*=========================================================================
  12610. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12611. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12612. * <type> determines <option>:
  12613. * 0 : char_id
  12614. * 1 : party_id
  12615. * 2 : guild_id
  12616. * 3 : map_name
  12617. * 4 : all characters
  12618. * <revive_flag>:
  12619. * 1 : Revive and heal all players (default)
  12620. * 2 : Heal living players only
  12621. * 4 : Revive dead players only
  12622. * <map name>:
  12623. * for types 1-2 : map_name (null = all maps)
  12624. *-------------------------------------------------------------------------*/
  12625. BUILDIN_FUNC(recovery)
  12626. {
  12627. TBL_PC *sd;
  12628. int map_idx = 0, type = 0, revive = 1;
  12629. type = script_getnum(st,2);
  12630. if(script_hasdata(st,4))
  12631. revive = script_getnum(st,4);
  12632. switch(type) {
  12633. case 0:
  12634. if(!script_charid2sd(3,sd))
  12635. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12636. recovery_sub(sd, revive);
  12637. break;
  12638. case 1:
  12639. {
  12640. struct party_data* p;
  12641. //When no party given, we use invoker party
  12642. int p_id = 0, i;
  12643. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12644. map_idx = map_mapname2mapid(script_getstr(st,5));
  12645. if(map_idx < 1) { //But we'll check anyways
  12646. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12647. return SCRIPT_CMD_FAILURE;
  12648. }
  12649. }
  12650. if(script_hasdata(st,3))
  12651. p_id = script_getnum(st,3);
  12652. else if(script_rid2sd(sd))
  12653. p_id = sd->status.party_id;
  12654. p = party_search(p_id);
  12655. if(p == NULL)
  12656. return SCRIPT_CMD_SUCCESS;
  12657. for (i = 0; i < MAX_PARTY; i++) {
  12658. struct map_session_data* pl_sd;
  12659. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12660. || (map_idx && pl_sd->bl.m != map_idx))
  12661. continue;
  12662. recovery_sub(pl_sd, revive);
  12663. }
  12664. break;
  12665. }
  12666. case 2:
  12667. {
  12668. struct guild* g;
  12669. //When no guild given, we use invoker guild
  12670. int g_id = 0, i;
  12671. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12672. map_idx = map_mapname2mapid(script_getstr(st,5));
  12673. if(map_idx < 1) { //But we'll check anyways
  12674. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12675. return SCRIPT_CMD_FAILURE;
  12676. }
  12677. }
  12678. if(script_hasdata(st,3))
  12679. g_id = script_getnum(st,3);
  12680. else if(script_rid2sd(sd))
  12681. g_id = sd->status.guild_id;
  12682. g = guild_search(g_id);
  12683. if(g == NULL)
  12684. return SCRIPT_CMD_SUCCESS;
  12685. for (i = 0; i < MAX_GUILD; i++) {
  12686. struct map_session_data* pl_sd;
  12687. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12688. || (map_idx && pl_sd->bl.m != map_idx))
  12689. continue;
  12690. recovery_sub(pl_sd, revive);
  12691. }
  12692. break;
  12693. }
  12694. case 3:
  12695. if(script_hasdata(st,3))
  12696. map_idx = map_mapname2mapid(script_getstr(st,3));
  12697. else if(script_rid2sd(sd))
  12698. map_idx = sd->bl.m;
  12699. if(map_idx < 1)
  12700. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12701. case 4:
  12702. {
  12703. struct s_mapiterator *iter;
  12704. struct script_data *data = script_getdata(st, 3);
  12705. get_val(st, data); // Convert into value in case of a variable
  12706. if(script_hasdata(st,3) && !data_isstring(data))
  12707. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12708. iter = mapit_getallusers();
  12709. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12710. if(type == 3 && sd->bl.m != map_idx)
  12711. continue;
  12712. recovery_sub(sd, revive);
  12713. }
  12714. mapit_free(iter);
  12715. break;
  12716. }
  12717. default:
  12718. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12719. script_pushint(st,-1);
  12720. return SCRIPT_CMD_FAILURE;
  12721. }
  12722. script_pushint(st,1); //Successfully executed without errors
  12723. return SCRIPT_CMD_SUCCESS;
  12724. }
  12725. /*==========================================
  12726. * Get your pet info: getpetinfo <type>{,<char_id>}
  12727. * n -> 0:pet_id 1:pet_class 2:pet_name
  12728. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12729. * 7:game id
  12730. *------------------------------------------*/
  12731. BUILDIN_FUNC(getpetinfo)
  12732. {
  12733. TBL_PC *sd;
  12734. TBL_PET *pd;
  12735. int type = script_getnum(st,2);
  12736. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12737. if (type == 2)
  12738. script_pushconststr(st,"null");
  12739. else
  12740. script_pushint(st,0);
  12741. return SCRIPT_CMD_SUCCESS;
  12742. }
  12743. switch(type){
  12744. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  12745. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  12746. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  12747. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  12748. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  12749. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  12750. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  12751. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  12752. default:
  12753. script_pushint(st,0);
  12754. break;
  12755. }
  12756. return SCRIPT_CMD_SUCCESS;
  12757. }
  12758. /*==========================================
  12759. * Get your homunculus info: gethominfo <type>{,<char_id>};
  12760. * type -> 0:hom_id 1:class 2:name
  12761. * 3:friendly 4:hungry, 5: rename flag.
  12762. * 6: level, 7: game id
  12763. *------------------------------------------*/
  12764. BUILDIN_FUNC(gethominfo)
  12765. {
  12766. TBL_PC *sd;
  12767. TBL_HOM *hd;
  12768. int type=script_getnum(st,2);
  12769. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  12770. if (type == 2)
  12771. script_pushconststr(st,"null");
  12772. else
  12773. script_pushint(st,0);
  12774. return SCRIPT_CMD_SUCCESS;
  12775. }
  12776. switch(type){
  12777. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12778. case 1: script_pushint(st,hd->homunculus.class_); break;
  12779. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12780. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12781. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12782. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12783. case 6: script_pushint(st,hd->homunculus.level); break;
  12784. case 7: script_pushint(st,hd->bl.id); break;
  12785. default:
  12786. script_pushint(st,0);
  12787. break;
  12788. }
  12789. return SCRIPT_CMD_SUCCESS;
  12790. }
  12791. /// Retrieves information about character's mercenary
  12792. /// getmercinfo <type>[,<char id>];
  12793. BUILDIN_FUNC(getmercinfo)
  12794. {
  12795. int type;
  12796. struct map_session_data* sd;
  12797. struct mercenary_data* md;
  12798. if( !script_charid2sd(3,sd) ){
  12799. script_pushnil(st);
  12800. return SCRIPT_CMD_FAILURE;
  12801. }
  12802. type = script_getnum(st,2);
  12803. md = sd->md;
  12804. if( md == NULL ){
  12805. if( type == 2 )
  12806. script_pushconststr(st,"");
  12807. else
  12808. script_pushint(st,0);
  12809. return SCRIPT_CMD_SUCCESS;
  12810. }
  12811. switch( type )
  12812. {
  12813. case 0: script_pushint(st,md->mercenary.mercenary_id); break;
  12814. case 1: script_pushint(st,md->mercenary.class_); break;
  12815. case 2: script_pushstrcopy(st,md->db->name); break;
  12816. case 3: script_pushint(st,mercenary_get_faith(md)); break;
  12817. case 4: script_pushint(st,mercenary_get_calls(md)); break;
  12818. case 5: script_pushint(st,md->mercenary.kill_count); break;
  12819. case 6: script_pushint(st,mercenary_get_lifetime(md)); break;
  12820. case 7: script_pushint(st,md->db->lv); break;
  12821. case 8: script_pushint(st,md->bl.id); break;
  12822. default:
  12823. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12824. script_pushnil(st);
  12825. return SCRIPT_CMD_FAILURE;
  12826. }
  12827. return SCRIPT_CMD_SUCCESS;
  12828. }
  12829. /*==========================================
  12830. * Shows wether your inventory(and equips) contain
  12831. selected card or not.
  12832. checkequipedcard(4001);
  12833. *------------------------------------------*/
  12834. BUILDIN_FUNC(checkequipedcard)
  12835. {
  12836. TBL_PC *sd;
  12837. if(script_rid2sd(sd)){
  12838. int n,i,c=0;
  12839. c=script_getnum(st,2);
  12840. for(i=0;i<MAX_INVENTORY;i++){
  12841. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  12842. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12843. continue;
  12844. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12845. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  12846. script_pushint(st,1);
  12847. return SCRIPT_CMD_SUCCESS;
  12848. }
  12849. }
  12850. }
  12851. }
  12852. }
  12853. script_pushint(st,0);
  12854. return SCRIPT_CMD_SUCCESS;
  12855. }
  12856. BUILDIN_FUNC(jump_zero)
  12857. {
  12858. int sel;
  12859. sel=script_getnum(st,2);
  12860. if(!sel) {
  12861. int pos;
  12862. if( !data_islabel(script_getdata(st,3)) ){
  12863. ShowError("script: jump_zero: not label !\n");
  12864. st->state=END;
  12865. return SCRIPT_CMD_FAILURE;
  12866. }
  12867. pos=script_getnum(st,3);
  12868. st->pos=pos;
  12869. st->state=GOTO;
  12870. }
  12871. return SCRIPT_CMD_SUCCESS;
  12872. }
  12873. /*==========================================
  12874. * movenpc [MouseJstr]
  12875. *------------------------------------------*/
  12876. BUILDIN_FUNC(movenpc)
  12877. {
  12878. TBL_NPC *nd = NULL;
  12879. const char *npc;
  12880. int x,y;
  12881. npc = script_getstr(st,2);
  12882. x = script_getnum(st,3);
  12883. y = script_getnum(st,4);
  12884. if ((nd = npc_name2id(npc)) == NULL){
  12885. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12886. return -1;
  12887. }
  12888. if (script_hasdata(st,5))
  12889. nd->ud.dir = script_getnum(st,5) % 8;
  12890. npc_movenpc(nd, x, y);
  12891. return SCRIPT_CMD_SUCCESS;
  12892. }
  12893. /*==========================================
  12894. * message [MouseJstr]
  12895. *------------------------------------------*/
  12896. BUILDIN_FUNC(message)
  12897. {
  12898. const char *msg,*player;
  12899. TBL_PC *pl_sd = NULL;
  12900. player = script_getstr(st,2);
  12901. msg = script_getstr(st,3);
  12902. if((pl_sd=map_nick2sd((char *) player,false)) == NULL)
  12903. return SCRIPT_CMD_SUCCESS;
  12904. clif_displaymessage(pl_sd->fd, msg);
  12905. return SCRIPT_CMD_SUCCESS;
  12906. }
  12907. /**
  12908. * npctalk("<message>"{,"<NPC name>","<flag>"});
  12909. * @param flag: Specify target
  12910. * BC_ALL - Broadcast message is sent server-wide.
  12911. * BC_MAP - Message is sent to everyone in the same map as the source of the npc.
  12912. * BC_AREA - Message is sent to players in the vicinity of the source (default).
  12913. * BC_SELF - Message is sent only to player attached.
  12914. */
  12915. BUILDIN_FUNC(npctalk)
  12916. {
  12917. struct npc_data* nd = NULL;
  12918. const char* str = script_getstr(st,2);
  12919. if (script_hasdata(st, 3) && strlen(script_getstr(st,3)) > 0)
  12920. nd = npc_name2id(script_getstr(st, 3));
  12921. else
  12922. nd = (struct npc_data *)map_id2bl(st->oid);
  12923. if (nd != NULL) {
  12924. send_target target = AREA;
  12925. char message[CHAT_SIZE_MAX];
  12926. if (script_hasdata(st, 4)) {
  12927. switch(script_getnum(st, 4)) {
  12928. case BC_ALL: target = ALL_CLIENT; break;
  12929. case BC_MAP: target = ALL_SAMEMAP; break;
  12930. case BC_SELF: target = SELF; break;
  12931. case BC_AREA:
  12932. default: target = AREA; break;
  12933. }
  12934. }
  12935. safesnprintf(message, sizeof(message), "%s", str);
  12936. if (target != SELF)
  12937. clif_messagecolor(&nd->bl, color_table[COLOR_WHITE], message, false, target);
  12938. else {
  12939. TBL_PC *sd = map_id2sd(st->rid);
  12940. if (sd == NULL)
  12941. return SCRIPT_CMD_FAILURE;
  12942. clif_messagecolor_target(&nd->bl, color_table[COLOR_WHITE], message, false, target, sd);
  12943. }
  12944. }
  12945. return SCRIPT_CMD_SUCCESS;
  12946. }
  12947. /**
  12948. * Sends a message to the waitingroom of the invoking NPC.
  12949. * chatmes "<message>"{,"<NPC name>"};
  12950. * @author Jey
  12951. */
  12952. BUILDIN_FUNC(chatmes)
  12953. {
  12954. struct npc_data* nd = NULL;
  12955. const char* str = script_getstr(st,2);
  12956. if (script_hasdata(st, 3))
  12957. nd = npc_name2id(script_getstr(st, 3));
  12958. else
  12959. nd = (struct npc_data *)map_id2bl(st->oid);
  12960. if (nd != NULL && nd->chat_id) {
  12961. char message[256];
  12962. safesnprintf(message, sizeof(message), "%s", str);
  12963. clif_GlobalMessage(map_id2bl(nd->chat_id), message, CHAT_WOS);
  12964. }
  12965. return SCRIPT_CMD_SUCCESS;
  12966. }
  12967. // change npc walkspeed [Valaris]
  12968. BUILDIN_FUNC(npcspeed)
  12969. {
  12970. struct npc_data* nd;
  12971. int speed;
  12972. speed = script_getnum(st,2);
  12973. nd =(struct npc_data *)map_id2bl(st->oid);
  12974. if( nd ) {
  12975. nd->speed = speed;
  12976. nd->ud.state.speed_changed = 1;
  12977. }
  12978. return SCRIPT_CMD_SUCCESS;
  12979. }
  12980. // make an npc walk to a position [Valaris]
  12981. BUILDIN_FUNC(npcwalkto)
  12982. {
  12983. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12984. int x=0,y=0;
  12985. x=script_getnum(st,2);
  12986. y=script_getnum(st,3);
  12987. if(nd) {
  12988. if (!nd->status.hp)
  12989. status_calc_npc(nd, SCO_FIRST);
  12990. else
  12991. status_calc_npc(nd, SCO_NONE);
  12992. unit_walktoxy(&nd->bl,x,y,0);
  12993. }
  12994. return SCRIPT_CMD_SUCCESS;
  12995. }
  12996. // stop an npc's movement [Valaris]
  12997. BUILDIN_FUNC(npcstop)
  12998. {
  12999. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  13000. if(nd) {
  13001. unit_stop_walking(&nd->bl,1|4);
  13002. }
  13003. return SCRIPT_CMD_SUCCESS;
  13004. }
  13005. /**
  13006. * getlook(<type>{,<char_id>})
  13007. **/
  13008. BUILDIN_FUNC(getlook)
  13009. {
  13010. int type,val;
  13011. TBL_PC *sd;
  13012. if (!script_charid2sd(3,sd)) {
  13013. script_pushint(st,-1);
  13014. return SCRIPT_CMD_FAILURE;
  13015. }
  13016. type=script_getnum(st,2);
  13017. val=-1;
  13018. switch(type) {
  13019. // TODO: implement LOOK_BASE as stated in script doc
  13020. case LOOK_HAIR: val=sd->status.hair; break; //1
  13021. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  13022. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  13023. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  13024. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  13025. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  13026. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  13027. case LOOK_SHIELD: val=sd->status.shield; break; //8
  13028. case LOOK_SHOES: break; //9
  13029. case LOOK_ROBE: val=sd->status.robe; break; //12
  13030. case LOOK_BODY2: val=sd->status.body; break; //13
  13031. }
  13032. script_pushint(st,val);
  13033. return SCRIPT_CMD_SUCCESS;
  13034. }
  13035. /**
  13036. * getsavepoint(<information type>{,<char_id>})
  13037. * @param type 0- map name, 1- x, 2- y
  13038. **/
  13039. BUILDIN_FUNC(getsavepoint)
  13040. {
  13041. TBL_PC* sd;
  13042. int type;
  13043. if (!script_charid2sd(3,sd)) {
  13044. script_pushint(st,0);
  13045. return SCRIPT_CMD_SUCCESS;
  13046. }
  13047. type = script_getnum(st,2);
  13048. switch(type) {
  13049. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  13050. case 1: script_pushint(st,sd->status.save_point.x); break;
  13051. case 2: script_pushint(st,sd->status.save_point.y); break;
  13052. default:
  13053. script_pushint(st,0);
  13054. break;
  13055. }
  13056. return SCRIPT_CMD_SUCCESS;
  13057. }
  13058. /**
  13059. * Get position for BL objects.
  13060. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  13061. * @param mapname: String variable for output map name
  13062. * @param x: Integer variable for output coord X
  13063. * @param y: Integer variable for output coord Y
  13064. * @param type: Type of object
  13065. * UNITTYPE_PC - Character coord
  13066. * UNITTYPE_NPC - NPC coord
  13067. * UNITTYPE_PET - Pet coord
  13068. * UNITTYPE_HOM - Homun coord
  13069. * UNITTYPE_MER - Mercenary coord
  13070. * UNITTYPE_ELEM - Elemental coord
  13071. * @param charname: Name object. If empty or "this" use the current object
  13072. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  13073. */
  13074. BUILDIN_FUNC(getmapxy)
  13075. {
  13076. struct block_list *bl = NULL;
  13077. TBL_PC *sd=NULL;
  13078. int64 num;
  13079. const char *name;
  13080. char prefix;
  13081. int x,y,type;
  13082. char mapname[MAP_NAME_LENGTH];
  13083. if( !data_isreference(script_getdata(st,2)) ) {
  13084. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  13085. script_pushint(st,-1);
  13086. return SCRIPT_CMD_FAILURE;
  13087. }
  13088. if( !data_isreference(script_getdata(st,3)) ) {
  13089. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  13090. script_pushint(st,-1);
  13091. return SCRIPT_CMD_FAILURE;
  13092. }
  13093. if( !data_isreference(script_getdata(st,4)) ) {
  13094. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  13095. script_pushint(st,-1);
  13096. return SCRIPT_CMD_FAILURE;
  13097. }
  13098. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  13099. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  13100. script_pushint(st,-1);
  13101. return SCRIPT_CMD_FAILURE;
  13102. }
  13103. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  13104. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  13105. script_pushint(st,-1);
  13106. return SCRIPT_CMD_FAILURE;
  13107. }
  13108. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  13109. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  13110. script_pushint(st,-1);
  13111. return SCRIPT_CMD_FAILURE;
  13112. }
  13113. // Possible needly check function parameters on C_STR,C_INT,C_INT
  13114. type=script_getnum(st,5);
  13115. switch (type) {
  13116. case UNITTYPE_PC: //Get Character Position
  13117. if( script_nick2sd(6,sd) )
  13118. bl = &sd->bl;
  13119. break;
  13120. case UNITTYPE_NPC: //Get NPC Position
  13121. if( script_hasdata(st,6) )
  13122. {
  13123. struct npc_data *nd;
  13124. nd=npc_name2id(script_getstr(st,6));
  13125. if (nd)
  13126. bl = &nd->bl;
  13127. } else //In case the origin is not an npc?
  13128. bl=map_id2bl(st->oid);
  13129. break;
  13130. case UNITTYPE_PET: //Get Pet Position
  13131. if( script_nick2sd(6, sd) && sd->pd )
  13132. bl = &sd->pd->bl;
  13133. break;
  13134. case UNITTYPE_HOM: //Get Homun Position
  13135. if( script_nick2sd(6, sd) && sd->hd )
  13136. bl = &sd->hd->bl;
  13137. break;
  13138. case UNITTYPE_MER: //Get Mercenary Position
  13139. if( script_nick2sd(6, sd) && sd->md )
  13140. bl = &sd->md->bl;
  13141. break;
  13142. case UNITTYPE_ELEM: //Get Elemental Position
  13143. if( script_nick2sd(6, sd) && sd->ed )
  13144. bl = &sd->ed->bl;
  13145. break;
  13146. default:
  13147. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  13148. script_pushint(st,-1);
  13149. return SCRIPT_CMD_FAILURE;
  13150. }
  13151. if (!bl) { //No object found.
  13152. script_pushint(st,-1);
  13153. return SCRIPT_CMD_SUCCESS;
  13154. }
  13155. x= bl->x;
  13156. y= bl->y;
  13157. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  13158. //Set MapName$
  13159. num=st->stack->stack_data[st->start+2].u.num;
  13160. name=get_str(script_getvarid(num));
  13161. prefix=*name;
  13162. if(not_server_variable(prefix)){
  13163. if( !script_rid2sd(sd) ){
  13164. ShowError( "buildin_getmapxy: variable '%s' for mapname is not a server variable, but no player is attached!", name );
  13165. return SCRIPT_CMD_FAILURE;
  13166. }
  13167. }else
  13168. sd=NULL;
  13169. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  13170. //Set MapX
  13171. num=st->stack->stack_data[st->start+3].u.num;
  13172. name=get_str(script_getvarid(num));
  13173. prefix=*name;
  13174. if(not_server_variable(prefix)){
  13175. if( !script_rid2sd(sd) ){
  13176. ShowError( "buildin_getmapxy: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  13177. return SCRIPT_CMD_FAILURE;
  13178. }
  13179. }else
  13180. sd=NULL;
  13181. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  13182. //Set MapY
  13183. num=st->stack->stack_data[st->start+4].u.num;
  13184. name=get_str(script_getvarid(num));
  13185. prefix=*name;
  13186. if(not_server_variable(prefix)){
  13187. if( !script_rid2sd(sd) ){
  13188. ShowError( "buildin_getmapxy: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  13189. return SCRIPT_CMD_FAILURE;
  13190. }
  13191. }else
  13192. sd=NULL;
  13193. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  13194. //Return Success value
  13195. script_pushint(st,0);
  13196. return SCRIPT_CMD_SUCCESS;
  13197. }
  13198. /// Returns the map name of given map ID.
  13199. ///
  13200. /// mapid2name <map ID>;
  13201. BUILDIN_FUNC(mapid2name)
  13202. {
  13203. uint16 m = script_getnum(st, 2);
  13204. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  13205. script_pushstr(st, "");
  13206. return SCRIPT_CMD_FAILURE;
  13207. }
  13208. script_pushstrcopy(st, map_mapid2mapname(m));
  13209. return SCRIPT_CMD_SUCCESS;
  13210. }
  13211. /*==========================================
  13212. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  13213. *------------------------------------------*/
  13214. BUILDIN_FUNC(logmes)
  13215. {
  13216. const char *str;
  13217. TBL_PC* sd;
  13218. if( !script_rid2sd(sd) )
  13219. return SCRIPT_CMD_FAILURE;
  13220. str = script_getstr(st,2);
  13221. log_npc(sd,str);
  13222. return SCRIPT_CMD_SUCCESS;
  13223. }
  13224. BUILDIN_FUNC(summon)
  13225. {
  13226. int _class, timeout=0;
  13227. const char *str,*event="";
  13228. TBL_PC *sd;
  13229. struct mob_data *md;
  13230. int tick = gettick();
  13231. if (!script_rid2sd(sd))
  13232. return SCRIPT_CMD_SUCCESS;
  13233. str =script_getstr(st,2);
  13234. _class=script_getnum(st,3);
  13235. if( script_hasdata(st,4) )
  13236. timeout=script_getnum(st,4);
  13237. if( script_hasdata(st,5) ){
  13238. event=script_getstr(st,5);
  13239. check_event(st, event);
  13240. }
  13241. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  13242. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  13243. if (md) {
  13244. md->master_id=sd->bl.id;
  13245. md->special_state.ai = AI_ATTACK;
  13246. if( md->deletetimer != INVALID_TIMER )
  13247. delete_timer(md->deletetimer, mob_timer_delete);
  13248. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  13249. mob_spawn (md); //Now it is ready for spawning.
  13250. clif_specialeffect(&md->bl,344,AREA);
  13251. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  13252. }
  13253. script_pushint(st, md->bl.id);
  13254. return SCRIPT_CMD_SUCCESS;
  13255. }
  13256. /*==========================================
  13257. * Checks whether it is daytime/nighttime
  13258. *------------------------------------------*/
  13259. BUILDIN_FUNC(isnight)
  13260. {
  13261. script_pushint(st,(night_flag == 1));
  13262. return SCRIPT_CMD_SUCCESS;
  13263. }
  13264. BUILDIN_FUNC(isday)
  13265. {
  13266. script_pushint(st,(night_flag == 0));
  13267. return SCRIPT_CMD_SUCCESS;
  13268. }
  13269. /*================================================
  13270. * Check how many items/cards in the list are
  13271. * equipped - used for 2/15's cards patch [celest]
  13272. *------------------------------------------------*/
  13273. BUILDIN_FUNC(isequippedcnt)
  13274. {
  13275. TBL_PC *sd;
  13276. int i, id = 1;
  13277. int ret = 0;
  13278. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13279. script_pushint(st,0);
  13280. return SCRIPT_CMD_SUCCESS;
  13281. }
  13282. for (i=0; id!=0; i++) {
  13283. short j;
  13284. FETCH (i+2, id) else id = 0;
  13285. if (id <= 0)
  13286. continue;
  13287. for (j=0; j<EQI_MAX; j++) {
  13288. short index = sd->equip_index[j];
  13289. if(index < 0)
  13290. continue;
  13291. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  13292. continue;
  13293. if(!sd->inventory_data[index])
  13294. continue;
  13295. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  13296. if (sd->inventory_data[index]->nameid == id)
  13297. ret+= sd->inventory.u.items_inventory[index].amount;
  13298. } else { //Count cards.
  13299. short k;
  13300. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13301. continue; //No cards
  13302. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13303. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13304. ret++; //[Lupus]
  13305. }
  13306. }
  13307. }
  13308. }
  13309. script_pushint(st,ret);
  13310. return SCRIPT_CMD_SUCCESS;
  13311. }
  13312. /*================================================
  13313. * Check whether another card has been
  13314. * equipped - used for 2/15's cards patch [celest]
  13315. * -- Items checked cannot be reused in another
  13316. * card set to prevent exploits
  13317. *------------------------------------------------*/
  13318. BUILDIN_FUNC(isequipped)
  13319. {
  13320. TBL_PC *sd;
  13321. int i, id = 1;
  13322. int ret = -1;
  13323. //Original hash to reverse it when full check fails.
  13324. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  13325. if (!script_rid2sd(sd)) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13326. script_pushint(st,0);
  13327. return SCRIPT_CMD_SUCCESS;
  13328. }
  13329. setitem_hash = sd->bonus.setitem_hash;
  13330. setitem_hash2 = sd->bonus.setitem_hash2;
  13331. for (i=0; id!=0; i++) {
  13332. int flag = 0;
  13333. short j;
  13334. FETCH (i+2, id) else id = 0;
  13335. if (id <= 0)
  13336. continue;
  13337. for (j=0; j<EQI_MAX; j++) {
  13338. short index = sd->equip_index[j];
  13339. if(index < 0)
  13340. continue;
  13341. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  13342. continue;
  13343. if(!sd->inventory_data[index])
  13344. continue;
  13345. if (itemdb_type(id) != IT_CARD) {
  13346. if (sd->inventory_data[index]->nameid != id)
  13347. continue;
  13348. flag = 1;
  13349. break;
  13350. } else { //Cards
  13351. short k;
  13352. if (sd->inventory_data[index]->slot == 0 ||
  13353. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13354. continue;
  13355. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  13356. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13357. unsigned int hash = 0;
  13358. if (sd->inventory.u.items_inventory[index].card[k] != id)
  13359. continue;
  13360. hash = 1<<((j<5?j:j-5)*4 + k);
  13361. // check if card is already used by another set
  13362. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13363. continue;
  13364. // We have found a match
  13365. flag = 1;
  13366. // Set hash so this card cannot be used by another
  13367. if (j<5)
  13368. sd->bonus.setitem_hash |= hash;
  13369. else
  13370. sd->bonus.setitem_hash2 |= hash;
  13371. break;
  13372. }
  13373. }
  13374. if (flag) break; //Card found
  13375. }
  13376. if (ret == -1)
  13377. ret = flag;
  13378. else
  13379. ret &= flag;
  13380. if (!ret) break;
  13381. }
  13382. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13383. sd->bonus.setitem_hash = setitem_hash;
  13384. sd->bonus.setitem_hash2 = setitem_hash2;
  13385. }
  13386. script_pushint(st,ret);
  13387. return SCRIPT_CMD_SUCCESS;
  13388. }
  13389. /*================================================
  13390. * Check how many given inserted cards in the CURRENT
  13391. * weapon - used for 2/15's cards patch [Lupus]
  13392. *------------------------------------------------*/
  13393. BUILDIN_FUNC(cardscnt)
  13394. {
  13395. TBL_PC *sd;
  13396. int i, k, id = 1;
  13397. int ret = 0;
  13398. int index;
  13399. if( !script_rid2sd(sd) ){
  13400. script_pushint(st,0);
  13401. return SCRIPT_CMD_SUCCESS;
  13402. }
  13403. for (i=0; id!=0; i++) {
  13404. FETCH (i+2, id) else id = 0;
  13405. if (id <= 0)
  13406. continue;
  13407. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  13408. if(index < 0) continue;
  13409. if(!sd->inventory_data[index])
  13410. continue;
  13411. if(itemdb_type(id) != IT_CARD) {
  13412. if (sd->inventory_data[index]->nameid == id)
  13413. ret+= sd->inventory.u.items_inventory[index].amount;
  13414. } else {
  13415. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13416. continue;
  13417. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13418. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13419. ret++;
  13420. }
  13421. }
  13422. }
  13423. script_pushint(st,ret);
  13424. // script_pushint(st,current_equip_item_index);
  13425. return SCRIPT_CMD_SUCCESS;
  13426. }
  13427. /*=======================================================
  13428. * Returns the refined number of the current item, or an
  13429. * item with inventory index specified
  13430. *-------------------------------------------------------*/
  13431. BUILDIN_FUNC(getrefine)
  13432. {
  13433. TBL_PC *sd;
  13434. if (script_rid2sd(sd)){
  13435. if( current_equip_item_index == -1 ){
  13436. script_pushint(st, 0);
  13437. return SCRIPT_CMD_FAILURE;
  13438. }
  13439. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13440. }else
  13441. script_pushint(st,0);
  13442. return SCRIPT_CMD_SUCCESS;
  13443. }
  13444. /*=======================================================
  13445. * Day/Night controls
  13446. *-------------------------------------------------------*/
  13447. BUILDIN_FUNC(night)
  13448. {
  13449. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13450. return SCRIPT_CMD_SUCCESS;
  13451. }
  13452. BUILDIN_FUNC(day)
  13453. {
  13454. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13455. return SCRIPT_CMD_SUCCESS;
  13456. }
  13457. /**
  13458. * unequip <equipment slot>{,<char_id>};
  13459. * @author [Spectre]
  13460. **/
  13461. BUILDIN_FUNC(unequip) {
  13462. int pos;
  13463. TBL_PC *sd;
  13464. if (!script_charid2sd(3,sd))
  13465. return SCRIPT_CMD_FAILURE;
  13466. pos = script_getnum(st,2);
  13467. if (equip_index_check(pos)) {
  13468. short i = pc_checkequip(sd,equip_bitmask[pos]);
  13469. if (i >= 0) {
  13470. pc_unequipitem(sd,i,1|2);
  13471. script_pushint(st, 1);
  13472. return SCRIPT_CMD_SUCCESS;
  13473. }
  13474. }
  13475. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13476. script_pushint(st, 0);
  13477. return SCRIPT_CMD_FAILURE;
  13478. }
  13479. /**
  13480. * equip <item id>{,<char_id>};
  13481. **/
  13482. BUILDIN_FUNC(equip) {
  13483. unsigned short nameid = 0;
  13484. TBL_PC *sd;
  13485. struct item_data *item_data;
  13486. if (!script_charid2sd(3,sd))
  13487. return SCRIPT_CMD_FAILURE;
  13488. nameid = script_getnum(st,2);
  13489. if ((item_data = itemdb_exists(nameid))) {
  13490. int i;
  13491. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  13492. if (i < MAX_INVENTORY) {
  13493. pc_equipitem(sd,i,item_data->equip);
  13494. script_pushint(st,1);
  13495. return SCRIPT_CMD_SUCCESS;
  13496. }
  13497. }
  13498. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13499. script_pushint(st,0);
  13500. return SCRIPT_CMD_FAILURE;
  13501. }
  13502. BUILDIN_FUNC(autoequip)
  13503. {
  13504. unsigned short nameid;
  13505. int flag;
  13506. struct item_data *item_data;
  13507. nameid=script_getnum(st,2);
  13508. flag=script_getnum(st,3);
  13509. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13510. {
  13511. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13512. return SCRIPT_CMD_FAILURE;
  13513. }
  13514. if( !itemdb_isequip2(item_data) )
  13515. {
  13516. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13517. return SCRIPT_CMD_FAILURE;
  13518. }
  13519. item_data->flag.autoequip = flag>0?1:0;
  13520. return SCRIPT_CMD_SUCCESS;
  13521. }
  13522. /**
  13523. * Set the value of a given battle config
  13524. * setbattleflag "<battle flag>",<value>{,<reload>};
  13525. */
  13526. BUILDIN_FUNC(setbattleflag)
  13527. {
  13528. const char *flag, *value;
  13529. flag = script_getstr(st,2);
  13530. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13531. if (battle_set_value(flag, value) == 0)
  13532. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13533. else {
  13534. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13535. if (script_hasdata(st, 4) && script_getnum(st, 4)) { // Only attempt to reload monster data if told to
  13536. const char *expdrop_flags[] = { // Only certain flags require a reload, check for those types
  13537. "base_exp_rate", "job_exp_rate", "mvp_exp_rate", "quest_exp_rate", "heal_exp", "resurrection_exp",
  13538. "item_rate_common", "item_rate_common_boss", "item_rate_common_mvp", "item_drop_common_min", "item_drop_common_max",
  13539. "item_rate_heal", "item_rate_heal_boss", "item_rate_heal_mvp", "item_rate_heal_min", "item_rate_heal_max",
  13540. "item_rate_use", "item_rate_use_boss", "item_rate_use_mvp", "item_rate_use_min", "item_rate_use_max",
  13541. "item_rate_equip", "item_rate_equip_boss", "item_rate_equip_mvp", "item_rate_equip_min", "item_rate_equip_max",
  13542. "item_rate_card", "item_rate_card_boss", "item_rate_card_mvp", "item_rate_card_min", "item_rate_card_max",
  13543. "item_rate_mvp", "item_rate_mvp_min", "item_rate_mvp_max", "item_rate_mvp_mode",
  13544. "item_rate_treasure", "item_rate_treasure_min", "item_rate_treasure_max",
  13545. "item_logarithmic_drops", "drop_rate0item", "drop_rateincrease",
  13546. };
  13547. uint8 i;
  13548. for (i = 0; i < ARRAYLENGTH(expdrop_flags); i++) {
  13549. if (!strcmpi(flag, expdrop_flags[i])) {
  13550. mob_reload();
  13551. break;
  13552. }
  13553. }
  13554. }
  13555. }
  13556. return SCRIPT_CMD_SUCCESS;
  13557. }
  13558. /**
  13559. * Get the value of a given battle config
  13560. * getbattleflag("<battle flag>")
  13561. */
  13562. BUILDIN_FUNC(getbattleflag)
  13563. {
  13564. const char *flag = script_getstr(st,2);
  13565. script_pushint(st,battle_get_value(flag));
  13566. return SCRIPT_CMD_SUCCESS;
  13567. }
  13568. //=======================================================
  13569. // strlen [Valaris]
  13570. //-------------------------------------------------------
  13571. BUILDIN_FUNC(getstrlen)
  13572. {
  13573. const char *str = script_getstr(st,2);
  13574. int len = (str) ? (int)strlen(str) : 0;
  13575. script_pushint(st,len);
  13576. return SCRIPT_CMD_SUCCESS;
  13577. }
  13578. //=======================================================
  13579. // isalpha [Valaris]
  13580. //-------------------------------------------------------
  13581. BUILDIN_FUNC(charisalpha)
  13582. {
  13583. const char *str=script_getstr(st,2);
  13584. int pos=script_getnum(st,3);
  13585. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13586. script_pushint(st,val);
  13587. return SCRIPT_CMD_SUCCESS;
  13588. }
  13589. //=======================================================
  13590. // charisupper <str>, <index>
  13591. //-------------------------------------------------------
  13592. BUILDIN_FUNC(charisupper)
  13593. {
  13594. const char *str = script_getstr(st,2);
  13595. int pos = script_getnum(st,3);
  13596. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13597. script_pushint(st,val);
  13598. return SCRIPT_CMD_SUCCESS;
  13599. }
  13600. //=======================================================
  13601. // charislower <str>, <index>
  13602. //-------------------------------------------------------
  13603. BUILDIN_FUNC(charislower)
  13604. {
  13605. const char *str = script_getstr(st,2);
  13606. int pos = script_getnum(st,3);
  13607. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13608. script_pushint(st,val);
  13609. return SCRIPT_CMD_SUCCESS;
  13610. }
  13611. //=======================================================
  13612. // charat <str>, <index>
  13613. //-------------------------------------------------------
  13614. BUILDIN_FUNC(charat) {
  13615. const char *str = script_getstr(st,2);
  13616. int pos = script_getnum(st,3);
  13617. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13618. char output[2];
  13619. output[0] = str[pos];
  13620. output[1] = '\0';
  13621. script_pushstrcopy(st, output);
  13622. } else
  13623. script_pushconststr(st, "");
  13624. return SCRIPT_CMD_SUCCESS;
  13625. }
  13626. //=======================================================
  13627. // setchar <string>, <char>, <index>
  13628. //-------------------------------------------------------
  13629. BUILDIN_FUNC(setchar)
  13630. {
  13631. const char *str = script_getstr(st,2);
  13632. const char *c = script_getstr(st,3);
  13633. int index = script_getnum(st,4);
  13634. char *output = aStrdup(str);
  13635. if(index >= 0 && index < strlen(output))
  13636. output[index] = *c;
  13637. script_pushstr(st, output);
  13638. return SCRIPT_CMD_SUCCESS;
  13639. }
  13640. //=======================================================
  13641. // insertchar <string>, <char>, <index>
  13642. //-------------------------------------------------------
  13643. BUILDIN_FUNC(insertchar)
  13644. {
  13645. const char *str = script_getstr(st,2);
  13646. const char *c = script_getstr(st,3);
  13647. int index = script_getnum(st,4);
  13648. char *output;
  13649. size_t len = strlen(str);
  13650. if(index < 0)
  13651. index = 0;
  13652. else if(index > len)
  13653. index = len;
  13654. output = (char*)aMalloc(len + 2);
  13655. memcpy(output, str, index);
  13656. output[index] = c[0];
  13657. memcpy(&output[index+1], &str[index], len - index);
  13658. output[len+1] = '\0';
  13659. script_pushstr(st, output);
  13660. return SCRIPT_CMD_SUCCESS;
  13661. }
  13662. //=======================================================
  13663. // delchar <string>, <index>
  13664. //-------------------------------------------------------
  13665. BUILDIN_FUNC(delchar)
  13666. {
  13667. const char *str = script_getstr(st,2);
  13668. int index = script_getnum(st,3);
  13669. char *output;
  13670. size_t len = strlen(str);
  13671. if(index < 0 || index > len) {
  13672. //return original
  13673. output = aStrdup(str);
  13674. script_pushstr(st, output);
  13675. return SCRIPT_CMD_SUCCESS;
  13676. }
  13677. output = (char*)aMalloc(len);
  13678. memcpy(output, str, index);
  13679. memcpy(&output[index], &str[index+1], len - index);
  13680. script_pushstr(st, output);
  13681. return SCRIPT_CMD_SUCCESS;
  13682. }
  13683. //=======================================================
  13684. // strtoupper <str>
  13685. //-------------------------------------------------------
  13686. BUILDIN_FUNC(strtoupper)
  13687. {
  13688. const char *str = script_getstr(st,2);
  13689. char *output = aStrdup(str);
  13690. char *cursor = output;
  13691. while (*cursor != '\0') {
  13692. *cursor = TOUPPER(*cursor);
  13693. cursor++;
  13694. }
  13695. script_pushstr(st, output);
  13696. return SCRIPT_CMD_SUCCESS;
  13697. }
  13698. //=======================================================
  13699. // strtolower <str>
  13700. //-------------------------------------------------------
  13701. BUILDIN_FUNC(strtolower)
  13702. {
  13703. const char *str = script_getstr(st,2);
  13704. char *output = aStrdup(str);
  13705. char *cursor = output;
  13706. while (*cursor != '\0') {
  13707. *cursor = TOLOWER(*cursor);
  13708. cursor++;
  13709. }
  13710. script_pushstr(st, output);
  13711. return SCRIPT_CMD_SUCCESS;
  13712. }
  13713. //=======================================================
  13714. // substr <str>, <start>, <end>
  13715. //-------------------------------------------------------
  13716. BUILDIN_FUNC(substr)
  13717. {
  13718. const char *str = script_getstr(st,2);
  13719. char *output;
  13720. int start = script_getnum(st,3);
  13721. int end = script_getnum(st,4);
  13722. int len = 0;
  13723. if(start >= 0 && end < strlen(str) && start <= end) {
  13724. len = end - start + 1;
  13725. output = (char*)aMalloc(len + 1);
  13726. memcpy(output, &str[start], len);
  13727. } else
  13728. output = (char*)aMalloc(1);
  13729. output[len] = '\0';
  13730. script_pushstr(st, output);
  13731. return SCRIPT_CMD_SUCCESS;
  13732. }
  13733. //=======================================================
  13734. // explode <dest_string_array>, <str>, <delimiter>
  13735. // Note: delimiter is limited to 1 char
  13736. //-------------------------------------------------------
  13737. BUILDIN_FUNC(explode)
  13738. {
  13739. struct script_data* data = script_getdata(st, 2);
  13740. const char *str = script_getstr(st,3);
  13741. const char delimiter = script_getstr(st, 4)[0];
  13742. int32 id;
  13743. size_t len = strlen(str);
  13744. int i = 0, j = 0;
  13745. int start;
  13746. char *temp;
  13747. const char* name;
  13748. TBL_PC* sd = NULL;
  13749. temp = (char*)aMalloc(len + 1);
  13750. if( !data_isreference(data) ) {
  13751. ShowError("script:explode: not a variable\n");
  13752. script_reportdata(data);
  13753. st->state = END;
  13754. return SCRIPT_CMD_FAILURE;// not a variable
  13755. }
  13756. id = reference_getid(data);
  13757. start = reference_getindex(data);
  13758. name = reference_getname(data);
  13759. if( !is_string_variable(name) ) {
  13760. ShowError("script:explode: not string array\n");
  13761. script_reportdata(data);
  13762. st->state = END;
  13763. return SCRIPT_CMD_FAILURE;// data type mismatch
  13764. }
  13765. if( not_server_variable(*name) ) {
  13766. if( !script_rid2sd(sd) )
  13767. return SCRIPT_CMD_SUCCESS;// no player attached
  13768. }
  13769. while(str[i] != '\0') {
  13770. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13771. temp[j] = '\0';
  13772. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13773. j = 0;
  13774. ++i;
  13775. } else {
  13776. temp[j++] = str[i++];
  13777. }
  13778. }
  13779. //set last string
  13780. temp[j] = '\0';
  13781. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13782. aFree(temp);
  13783. return SCRIPT_CMD_SUCCESS;
  13784. }
  13785. //=======================================================
  13786. // implode <string_array>
  13787. // implode <string_array>, <glue>
  13788. //-------------------------------------------------------
  13789. BUILDIN_FUNC(implode)
  13790. {
  13791. struct script_data* data = script_getdata(st, 2);
  13792. const char *name;
  13793. uint32 glue_len = 0, array_size, id;
  13794. char *output;
  13795. TBL_PC* sd = NULL;
  13796. if( !data_isreference(data) ) {
  13797. ShowError("script:implode: not a variable\n");
  13798. script_reportdata(data);
  13799. st->state = END;
  13800. return SCRIPT_CMD_FAILURE;// not a variable
  13801. }
  13802. id = reference_getid(data);
  13803. name = reference_getname(data);
  13804. if( !is_string_variable(name) ) {
  13805. ShowError("script:implode: not string array\n");
  13806. script_reportdata(data);
  13807. st->state = END;
  13808. return SCRIPT_CMD_FAILURE;// data type mismatch
  13809. }
  13810. if( not_server_variable(*name) && !script_rid2sd(sd) ) {
  13811. return SCRIPT_CMD_SUCCESS;// no player attached
  13812. }
  13813. //count chars
  13814. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  13815. if(array_size == -1) { //empty array check (AmsTaff)
  13816. ShowWarning("script:implode: array length = 0\n");
  13817. output = (char*)aMalloc(sizeof(char)*5);
  13818. sprintf(output,"%s","NULL");
  13819. } else {
  13820. const char *glue = NULL, *temp;
  13821. size_t len = 0;
  13822. int i, k = 0;
  13823. for(i = 0; i <= array_size; ++i) {
  13824. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13825. len += strlen(temp);
  13826. script_removetop(st, -1, 0);
  13827. }
  13828. //allocate mem
  13829. if( script_hasdata(st,3) ) {
  13830. glue = script_getstr(st,3);
  13831. glue_len = strlen(glue);
  13832. len += glue_len * (array_size);
  13833. }
  13834. output = (char*)aMalloc(len + 1);
  13835. //build output
  13836. for(i = 0; i < array_size; ++i) {
  13837. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13838. len = strlen(temp);
  13839. memcpy(&output[k], temp, len);
  13840. k += len;
  13841. if(glue_len != 0) {
  13842. memcpy(&output[k], glue, glue_len);
  13843. k += glue_len;
  13844. }
  13845. script_removetop(st, -1, 0);
  13846. }
  13847. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13848. len = strlen(temp);
  13849. memcpy(&output[k], temp, len);
  13850. k += len;
  13851. script_removetop(st, -1, 0);
  13852. output[k] = '\0';
  13853. }
  13854. script_pushstr(st, output);
  13855. return SCRIPT_CMD_SUCCESS;
  13856. }
  13857. //=======================================================
  13858. // sprintf(<format>, ...);
  13859. // Implements C sprintf, except format %n. The resulting string is
  13860. // returned, instead of being saved in variable by reference.
  13861. //-------------------------------------------------------
  13862. BUILDIN_FUNC(sprintf)
  13863. {
  13864. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13865. const char* format;
  13866. char* p;
  13867. char* q;
  13868. char* buf = NULL;
  13869. char* buf2 = NULL;
  13870. struct script_data* data;
  13871. StringBuf final_buf;
  13872. // Fetch init data
  13873. format = script_getstr(st, 2);
  13874. argc = script_lastdata(st)-2;
  13875. len = strlen(format);
  13876. // Skip parsing, where no parsing is required.
  13877. if(len == 0) {
  13878. script_pushconststr(st,"");
  13879. return SCRIPT_CMD_SUCCESS;
  13880. }
  13881. // Pessimistic alloc
  13882. CREATE(buf, char, len+1);
  13883. // Need not be parsed, just solve stuff like %%.
  13884. if(argc == 0) {
  13885. memcpy(buf,format,len+1);
  13886. script_pushstrcopy(st, buf);
  13887. aFree(buf);
  13888. return SCRIPT_CMD_SUCCESS;
  13889. }
  13890. safestrncpy(buf, format, len+1);
  13891. // Issue sprintf for each parameter
  13892. StringBuf_Init(&final_buf);
  13893. q = buf;
  13894. while((p = strchr(q, '%')) != NULL) {
  13895. if(p != q) {
  13896. len = p - q + 1;
  13897. if(buf2_len < len) {
  13898. RECREATE(buf2, char, len);
  13899. buf2_len = len;
  13900. }
  13901. safestrncpy(buf2, q, len);
  13902. StringBuf_AppendStr(&final_buf, buf2);
  13903. q = p;
  13904. }
  13905. p = q + 1;
  13906. if(*p == '%') { // %%
  13907. StringBuf_AppendStr(&final_buf, "%");
  13908. q+=2;
  13909. continue;
  13910. }
  13911. if(*p == 'n') { // %n
  13912. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13913. script_reportsrc(st);
  13914. q+=2;
  13915. continue;
  13916. }
  13917. if(arg >= argc) {
  13918. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13919. if(buf) aFree(buf);
  13920. if(buf2) aFree(buf2);
  13921. StringBuf_Destroy(&final_buf);
  13922. script_pushconststr(st,"");
  13923. return SCRIPT_CMD_FAILURE;
  13924. }
  13925. if((p = strchr(q+1, '%')) == NULL)
  13926. p = strchr(q, 0); // EOS
  13927. len = p - q + 1;
  13928. if(buf2_len < len) {
  13929. RECREATE(buf2, char, len);
  13930. buf2_len = len;
  13931. }
  13932. safestrncpy(buf2, q, len);
  13933. q = p;
  13934. // Note: This assumes the passed value being the correct
  13935. // type to the current format specifier. If not, the server
  13936. // probably crashes or returns anything else, than expected,
  13937. // but it would behave in normal code the same way so it's
  13938. // the scripter's responsibility.
  13939. data = script_getdata(st, arg+3);
  13940. if(data_isstring(data)) // String
  13941. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13942. else if(data_isint(data)) // Number
  13943. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13944. else if(data_isreference(data)) { // Variable
  13945. char* name = reference_getname(data);
  13946. if(name[strlen(name)-1]=='$') // var Str
  13947. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13948. else // var Int
  13949. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13950. } else { // Unsupported type
  13951. ShowError("buildin_sprintf: Unknown argument type!\n");
  13952. if(buf) aFree(buf);
  13953. if(buf2) aFree(buf2);
  13954. StringBuf_Destroy(&final_buf);
  13955. script_pushconststr(st,"");
  13956. return SCRIPT_CMD_FAILURE;
  13957. }
  13958. arg++;
  13959. }
  13960. // Append anything left
  13961. if(*q)
  13962. StringBuf_AppendStr(&final_buf, q);
  13963. // Passed more, than needed
  13964. if(arg < argc) {
  13965. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13966. script_reportsrc(st);
  13967. }
  13968. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13969. if(buf) aFree(buf);
  13970. if(buf2) aFree(buf2);
  13971. StringBuf_Destroy(&final_buf);
  13972. return SCRIPT_CMD_SUCCESS;
  13973. }
  13974. //=======================================================
  13975. // sscanf(<str>, <format>, ...);
  13976. // Implements C sscanf.
  13977. //-------------------------------------------------------
  13978. BUILDIN_FUNC(sscanf){
  13979. unsigned int argc, arg = 0, len;
  13980. struct script_data* data;
  13981. struct map_session_data* sd = NULL;
  13982. const char* str;
  13983. const char* format;
  13984. const char* p;
  13985. const char* q;
  13986. char* buf = NULL;
  13987. char* buf_p;
  13988. char* ref_str = NULL;
  13989. int ref_int;
  13990. // Get data
  13991. str = script_getstr(st, 2);
  13992. format = script_getstr(st, 3);
  13993. argc = script_lastdata(st)-3;
  13994. len = strlen(format);
  13995. if (len != 0 && strlen(str) == 0) {
  13996. // If the source string is empty but the format string is not, we return -1
  13997. // according to the C specs. (if the format string is also empty, we shall
  13998. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13999. script_pushint(st, -1);
  14000. return SCRIPT_CMD_SUCCESS;
  14001. }
  14002. CREATE(buf, char, len*2+1);
  14003. // Issue sscanf for each parameter
  14004. *buf = 0;
  14005. q = format;
  14006. while((p = strchr(q, '%'))){
  14007. if(p!=q){
  14008. strncat(buf, q, (size_t)(p-q));
  14009. q = p;
  14010. }
  14011. p = q+1;
  14012. if(*p=='*' || *p=='%'){ // Skip
  14013. strncat(buf, q, 2);
  14014. q+=2;
  14015. continue;
  14016. }
  14017. if(arg>=argc){
  14018. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  14019. script_pushint(st, -1);
  14020. if(buf) aFree(buf);
  14021. if(ref_str) aFree(ref_str);
  14022. return SCRIPT_CMD_FAILURE;
  14023. }
  14024. if((p = strchr(q+1, '%'))==NULL){
  14025. p = strchr(q, 0); // EOS
  14026. }
  14027. len = p-q;
  14028. strncat(buf, q, len);
  14029. q = p;
  14030. // Validate output
  14031. data = script_getdata(st, arg+4);
  14032. if(!data_isreference(data) || !reference_tovariable(data)){
  14033. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  14034. script_pushint(st, -1);
  14035. if(buf) aFree(buf);
  14036. if(ref_str) aFree(ref_str);
  14037. return SCRIPT_CMD_FAILURE;
  14038. }
  14039. buf_p = reference_getname(data);
  14040. if(not_server_variable(*buf_p) && !script_rid2sd(sd)){
  14041. script_pushint(st, -1);
  14042. if(buf) aFree(buf);
  14043. if(ref_str) aFree(ref_str);
  14044. return SCRIPT_CMD_SUCCESS;
  14045. }
  14046. // Save value if any
  14047. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  14048. if(ref_str==NULL){
  14049. CREATE(ref_str, char, strlen(str)+1);
  14050. }
  14051. if(sscanf(str, buf, ref_str)==0){
  14052. break;
  14053. }
  14054. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  14055. } else { // Number
  14056. if(sscanf(str, buf, &ref_int)==0){
  14057. break;
  14058. }
  14059. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  14060. }
  14061. arg++;
  14062. // Disable used format (%... -> %*...)
  14063. buf_p = strchr(buf, 0);
  14064. memmove(buf_p-len+2, buf_p-len+1, len);
  14065. *(buf_p-len+1) = '*';
  14066. }
  14067. script_pushint(st, arg);
  14068. if(buf) aFree(buf);
  14069. if(ref_str) aFree(ref_str);
  14070. return SCRIPT_CMD_SUCCESS;
  14071. }
  14072. //=======================================================
  14073. // strpos(<haystack>, <needle>)
  14074. // strpos(<haystack>, <needle>, <offset>)
  14075. //
  14076. // Implements PHP style strpos. Adapted from code from
  14077. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  14078. //-------------------------------------------------------
  14079. BUILDIN_FUNC(strpos) {
  14080. const char *haystack = script_getstr(st,2);
  14081. const char *needle = script_getstr(st,3);
  14082. int i;
  14083. size_t len;
  14084. if( script_hasdata(st,4) )
  14085. i = script_getnum(st,4);
  14086. else
  14087. i = 0;
  14088. if (needle[0] == '\0') {
  14089. script_pushint(st, -1);
  14090. return SCRIPT_CMD_SUCCESS;
  14091. }
  14092. len = strlen(haystack);
  14093. for ( ; i < len; ++i ) {
  14094. if ( haystack[i] == *needle ) {
  14095. // matched starting char -- loop through remaining chars
  14096. const char *h, *n;
  14097. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  14098. if ( *h != *n ) {
  14099. break;
  14100. }
  14101. }
  14102. if ( !*n ) { // matched all of 'needle' to null termination
  14103. script_pushint(st, i);
  14104. return SCRIPT_CMD_SUCCESS;
  14105. }
  14106. }
  14107. }
  14108. script_pushint(st, -1);
  14109. return SCRIPT_CMD_SUCCESS;
  14110. }
  14111. //===============================================================
  14112. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  14113. //
  14114. // Note: Finds all instances of <search> in <input> and replaces
  14115. // with <replace>. If specified will only replace as many
  14116. // instances as specified in <count>. By default will be case
  14117. // sensitive.
  14118. //---------------------------------------------------------------
  14119. BUILDIN_FUNC(replacestr)
  14120. {
  14121. const char *input = script_getstr(st, 2);
  14122. const char *find = script_getstr(st, 3);
  14123. const char *replace = script_getstr(st, 4);
  14124. size_t inputlen = strlen(input);
  14125. size_t findlen = strlen(find);
  14126. struct StringBuf output;
  14127. bool usecase = true;
  14128. int count = 0;
  14129. int numFinds = 0;
  14130. int i = 0, f = 0;
  14131. if(findlen == 0) {
  14132. ShowError("script:replacestr: Invalid search length.\n");
  14133. st->state = END;
  14134. return SCRIPT_CMD_FAILURE;
  14135. }
  14136. if(script_hasdata(st, 5)) {
  14137. struct script_data *data = script_getdata(st, 5);
  14138. get_val(st, data); // Convert into value in case of a variable
  14139. if( !data_isstring(data) )
  14140. usecase = script_getnum(st, 5) != 0;
  14141. else {
  14142. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  14143. st->state = END;
  14144. return SCRIPT_CMD_FAILURE;
  14145. }
  14146. }
  14147. if(script_hasdata(st, 6)) {
  14148. count = script_getnum(st, 6);
  14149. if(count == 0) {
  14150. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  14151. st->state = END;
  14152. return SCRIPT_CMD_FAILURE;
  14153. }
  14154. }
  14155. StringBuf_Init(&output);
  14156. for(; i < inputlen; i++) {
  14157. if(count && count == numFinds) { //found enough, stop looking
  14158. break;
  14159. }
  14160. for(f = 0; f <= findlen; f++) {
  14161. if(f == findlen) { //complete match
  14162. numFinds++;
  14163. StringBuf_AppendStr(&output, replace);
  14164. i += findlen - 1;
  14165. break;
  14166. } else {
  14167. if(usecase) {
  14168. if((i + f) > inputlen || input[i + f] != find[f]) {
  14169. StringBuf_Printf(&output, "%c", input[i]);
  14170. break;
  14171. }
  14172. } else {
  14173. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14174. StringBuf_Printf(&output, "%c", input[i]);
  14175. break;
  14176. }
  14177. }
  14178. }
  14179. }
  14180. }
  14181. //append excess after enough found
  14182. if(i < inputlen)
  14183. StringBuf_AppendStr(&output, &(input[i]));
  14184. script_pushstrcopy(st, StringBuf_Value(&output));
  14185. StringBuf_Destroy(&output);
  14186. return SCRIPT_CMD_SUCCESS;
  14187. }
  14188. //========================================================
  14189. // countstr <input>, <search>{, <usecase>}
  14190. //
  14191. // Note: Counts the number of times <search> occurs in
  14192. // <input>. By default will be case sensitive.
  14193. //--------------------------------------------------------
  14194. BUILDIN_FUNC(countstr)
  14195. {
  14196. const char *input = script_getstr(st, 2);
  14197. const char *find = script_getstr(st, 3);
  14198. size_t inputlen = strlen(input);
  14199. size_t findlen = strlen(find);
  14200. bool usecase = true;
  14201. int numFinds = 0;
  14202. int i = 0, f = 0;
  14203. if(findlen == 0) {
  14204. ShowError("script:countstr: Invalid search length.\n");
  14205. st->state = END;
  14206. return SCRIPT_CMD_FAILURE;
  14207. }
  14208. if(script_hasdata(st, 4)) {
  14209. struct script_data *data;
  14210. data = script_getdata(st, 4);
  14211. get_val(st, data); // Convert into value in case of a variable
  14212. if( !data_isstring(data) )
  14213. usecase = script_getnum(st, 4) != 0;
  14214. else {
  14215. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  14216. st->state = END;
  14217. return SCRIPT_CMD_FAILURE;
  14218. }
  14219. }
  14220. for(; i < inputlen; i++) {
  14221. for(f = 0; f <= findlen; f++) {
  14222. if(f == findlen) { //complete match
  14223. numFinds++;
  14224. i += findlen - 1;
  14225. break;
  14226. } else {
  14227. if(usecase) {
  14228. if((i + f) > inputlen || input[i + f] != find[f]) {
  14229. break;
  14230. }
  14231. } else {
  14232. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  14233. break;
  14234. }
  14235. }
  14236. }
  14237. }
  14238. }
  14239. script_pushint(st, numFinds);
  14240. return SCRIPT_CMD_SUCCESS;
  14241. }
  14242. /// Changes the display name and/or display class of the npc.
  14243. /// Returns 0 is successful, 1 if the npc does not exist.
  14244. ///
  14245. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  14246. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  14247. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  14248. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  14249. BUILDIN_FUNC(setnpcdisplay)
  14250. {
  14251. const char* name;
  14252. const char* newname = NULL;
  14253. int class_ = -1, size = -1;
  14254. struct script_data* data;
  14255. struct npc_data* nd;
  14256. name = script_getstr(st,2);
  14257. data = script_getdata(st,3);
  14258. if( script_hasdata(st,4) )
  14259. class_ = script_getnum(st,4);
  14260. if( script_hasdata(st,5) )
  14261. size = script_getnum(st,5);
  14262. get_val(st, data);
  14263. if( data_isstring(data) )
  14264. newname = conv_str(st,data);
  14265. else if( data_isint(data) )
  14266. class_ = conv_num(st,data);
  14267. else
  14268. {
  14269. ShowError("script:setnpcdisplay: expected string or number\n");
  14270. script_reportdata(data);
  14271. return SCRIPT_CMD_FAILURE;
  14272. }
  14273. nd = npc_name2id(name);
  14274. if( nd == NULL )
  14275. {// not found
  14276. script_pushint(st,1);
  14277. return SCRIPT_CMD_SUCCESS;
  14278. }
  14279. // update npc
  14280. if( newname )
  14281. npc_setdisplayname(nd, newname);
  14282. if( size != -1 && size != (int)nd->size )
  14283. nd->size = size;
  14284. else
  14285. size = -1;
  14286. if( class_ != -1 && nd->class_ != class_ )
  14287. npc_setclass(nd, class_);
  14288. else if( size != -1 )
  14289. { // Required to update the visual size
  14290. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  14291. clif_spawn(&nd->bl);
  14292. }
  14293. script_pushint(st,0);
  14294. return SCRIPT_CMD_SUCCESS;
  14295. }
  14296. BUILDIN_FUNC(atoi)
  14297. {
  14298. const char *value;
  14299. value = script_getstr(st,2);
  14300. script_pushint(st,atoi(value));
  14301. return SCRIPT_CMD_SUCCESS;
  14302. }
  14303. BUILDIN_FUNC(axtoi)
  14304. {
  14305. const char *hex = script_getstr(st,2);
  14306. long value = strtol(hex, NULL, 16);
  14307. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14308. value = cap_value(value, INT_MIN, INT_MAX);
  14309. #endif
  14310. script_pushint(st, (int)value);
  14311. return SCRIPT_CMD_SUCCESS;
  14312. }
  14313. BUILDIN_FUNC(strtol)
  14314. {
  14315. const char *string = script_getstr(st, 2);
  14316. int base = script_getnum(st, 3);
  14317. long value = strtol(string, NULL, base);
  14318. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14319. value = cap_value(value, INT_MIN, INT_MAX);
  14320. #endif
  14321. script_pushint(st, (int)value);
  14322. return SCRIPT_CMD_SUCCESS;
  14323. }
  14324. // case-insensitive substring search [lordalfa]
  14325. BUILDIN_FUNC(compare)
  14326. {
  14327. const char *message;
  14328. const char *cmpstring;
  14329. message = script_getstr(st,2);
  14330. cmpstring = script_getstr(st,3);
  14331. script_pushint(st,(stristr(message,cmpstring) != NULL));
  14332. return SCRIPT_CMD_SUCCESS;
  14333. }
  14334. BUILDIN_FUNC(strcmp)
  14335. {
  14336. const char *str1;
  14337. const char *str2;
  14338. str1 = script_getstr(st,2);
  14339. str2 = script_getstr(st,3);
  14340. script_pushint(st,strcmp(str1, str2));
  14341. return SCRIPT_CMD_SUCCESS;
  14342. }
  14343. // [zBuffer] List of mathematics commands --->
  14344. BUILDIN_FUNC(sqrt)
  14345. {
  14346. double i, a;
  14347. i = script_getnum(st,2);
  14348. a = sqrt(i);
  14349. script_pushint(st,(int)a);
  14350. return SCRIPT_CMD_SUCCESS;
  14351. }
  14352. BUILDIN_FUNC(pow)
  14353. {
  14354. double i, a, b;
  14355. a = script_getnum(st,2);
  14356. b = script_getnum(st,3);
  14357. i = pow(a,b);
  14358. script_pushint(st,(int)i);
  14359. return SCRIPT_CMD_SUCCESS;
  14360. }
  14361. BUILDIN_FUNC(distance)
  14362. {
  14363. int x0, y0, x1, y1;
  14364. x0 = script_getnum(st,2);
  14365. y0 = script_getnum(st,3);
  14366. x1 = script_getnum(st,4);
  14367. y1 = script_getnum(st,5);
  14368. script_pushint(st,distance_xy(x0,y0,x1,y1));
  14369. return SCRIPT_CMD_SUCCESS;
  14370. }
  14371. // <--- [zBuffer] List of mathematics commands
  14372. BUILDIN_FUNC(md5)
  14373. {
  14374. const char *tmpstr;
  14375. char *md5str;
  14376. tmpstr = script_getstr(st,2);
  14377. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14378. MD5_String(tmpstr, md5str);
  14379. script_pushstr(st, md5str);
  14380. return SCRIPT_CMD_SUCCESS;
  14381. }
  14382. // [zBuffer] List of dynamic var commands --->
  14383. /**
  14384. * setd "<variable name>",<value>{,<char_id>};
  14385. **/
  14386. BUILDIN_FUNC(setd)
  14387. {
  14388. TBL_PC *sd = NULL;
  14389. char varname[100];
  14390. const char *buffer;
  14391. int elem;
  14392. buffer = script_getstr(st, 2);
  14393. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14394. elem = 0;
  14395. if( not_server_variable(*varname) ) {
  14396. if (!script_charid2sd(4,sd))
  14397. return SCRIPT_CMD_FAILURE;
  14398. }
  14399. if( is_string_variable(varname) ) {
  14400. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  14401. } else {
  14402. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  14403. }
  14404. return SCRIPT_CMD_SUCCESS;
  14405. }
  14406. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14407. {
  14408. int i, j;
  14409. TBL_PC* sd = NULL;
  14410. const char* query;
  14411. struct script_data* data;
  14412. const char* name;
  14413. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14414. int num_vars;
  14415. int num_cols;
  14416. // check target variables
  14417. for( i = 3; script_hasdata(st,i); ++i ) {
  14418. data = script_getdata(st, i);
  14419. if( data_isreference(data) ) { // it's a variable
  14420. name = reference_getname(data);
  14421. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14422. if( !script_rid2sd(sd) ) { // no player attached
  14423. script_reportdata(data);
  14424. st->state = END;
  14425. return SCRIPT_CMD_FAILURE;
  14426. }
  14427. }
  14428. } else {
  14429. ShowError("script:query_sql: not a variable\n");
  14430. script_reportdata(data);
  14431. st->state = END;
  14432. return SCRIPT_CMD_FAILURE;
  14433. }
  14434. }
  14435. num_vars = i - 3;
  14436. // Execute the query
  14437. query = script_getstr(st,2);
  14438. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14439. Sql_ShowDebug(handle);
  14440. script_pushint(st, -1);
  14441. return SCRIPT_CMD_FAILURE;
  14442. }
  14443. if( Sql_NumRows(handle) == 0 ) { // No data received
  14444. Sql_FreeResult(handle);
  14445. script_pushint(st, 0);
  14446. return SCRIPT_CMD_SUCCESS;
  14447. }
  14448. // Count the number of columns to store
  14449. num_cols = Sql_NumColumns(handle);
  14450. if( num_vars < num_cols ) {
  14451. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14452. script_reportsrc(st);
  14453. } else if( num_vars > num_cols ) {
  14454. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14455. script_reportsrc(st);
  14456. }
  14457. // Store data
  14458. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14459. for( j = 0; j < num_vars; ++j ) {
  14460. char* str = NULL;
  14461. if( j < num_cols )
  14462. Sql_GetData(handle, j, &str, NULL);
  14463. data = script_getdata(st, j+3);
  14464. name = reference_getname(data);
  14465. if( is_string_variable(name) )
  14466. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  14467. else
  14468. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  14469. }
  14470. }
  14471. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14472. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14473. script_reportsrc(st);
  14474. }
  14475. // Free data
  14476. Sql_FreeResult(handle);
  14477. script_pushint(st, i);
  14478. return SCRIPT_CMD_SUCCESS;
  14479. }
  14480. BUILDIN_FUNC(query_sql) {
  14481. #ifdef BETA_THREAD_TEST
  14482. if( st->state != RERUNLINE ) {
  14483. queryThread_add(st,false);
  14484. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14485. } else
  14486. st->state = RUN;
  14487. return SCRIPT_CMD_SUCCESS;
  14488. #else
  14489. return buildin_query_sql_sub(st, qsmysql_handle);
  14490. #endif
  14491. }
  14492. BUILDIN_FUNC(query_logsql) {
  14493. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14494. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14495. script_pushint(st,-1);
  14496. return SCRIPT_CMD_FAILURE;
  14497. }
  14498. #ifdef BETA_THREAD_TEST
  14499. if( st->state != RERUNLINE ) {
  14500. queryThread_add(st,true);
  14501. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14502. } else
  14503. st->state = RUN;
  14504. return SCRIPT_CMD_SUCCESS;
  14505. #else
  14506. return buildin_query_sql_sub(st, logmysql_handle);
  14507. #endif
  14508. }
  14509. //Allows escaping of a given string.
  14510. BUILDIN_FUNC(escape_sql)
  14511. {
  14512. const char *str;
  14513. char *esc_str;
  14514. size_t len;
  14515. str = script_getstr(st,2);
  14516. len = strlen(str);
  14517. esc_str = (char*)aMalloc(len*2+1);
  14518. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14519. script_pushstr(st, esc_str);
  14520. return SCRIPT_CMD_SUCCESS;
  14521. }
  14522. BUILDIN_FUNC(getd)
  14523. {
  14524. char varname[100];
  14525. const char *buffer;
  14526. int elem;
  14527. buffer = script_getstr(st, 2);
  14528. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14529. elem = 0;
  14530. // Push the 'pointer' so it's more flexible [Lance]
  14531. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14532. return SCRIPT_CMD_SUCCESS;
  14533. }
  14534. BUILDIN_FUNC(callshop)
  14535. {
  14536. TBL_PC *sd = NULL;
  14537. struct npc_data *nd;
  14538. const char *shopname;
  14539. int flag = 0;
  14540. if (!script_rid2sd(sd)) {
  14541. script_pushint(st,0);
  14542. return SCRIPT_CMD_SUCCESS;
  14543. }
  14544. shopname = script_getstr(st, 2);
  14545. if (script_hasdata(st,3))
  14546. flag = script_getnum(st,3);
  14547. nd = npc_name2id(shopname);
  14548. 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) ) {
  14549. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14550. script_pushint(st,0);
  14551. return SCRIPT_CMD_FAILURE;
  14552. }
  14553. if (nd->subtype == NPCTYPE_SHOP) {
  14554. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14555. sd->state.callshop = 1;
  14556. switch (flag) {
  14557. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14558. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14559. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14560. }
  14561. }
  14562. #if PACKETVER >= 20131223
  14563. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14564. unsigned short i;
  14565. for (i = 0; i < nd->u.shop.count; i++) {
  14566. if (nd->u.shop.shop_item[i].qty)
  14567. break;
  14568. }
  14569. if (i == nd->u.shop.count) {
  14570. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  14571. return SCRIPT_CMD_FAILURE;
  14572. }
  14573. sd->npc_shopid = nd->bl.id;
  14574. clif_npc_market_open(sd, nd);
  14575. script_pushint(st,1);
  14576. return SCRIPT_CMD_SUCCESS;
  14577. }
  14578. #endif
  14579. else
  14580. clif_cashshop_show(sd, nd);
  14581. sd->npc_shopid = nd->bl.id;
  14582. script_pushint(st,1);
  14583. return SCRIPT_CMD_SUCCESS;
  14584. }
  14585. BUILDIN_FUNC(npcshopitem)
  14586. {
  14587. const char* npcname = script_getstr(st, 2);
  14588. struct npc_data* nd = npc_name2id(npcname);
  14589. int n, i;
  14590. int amount;
  14591. uint16 offs = 2;
  14592. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14593. script_pushint(st,0);
  14594. return SCRIPT_CMD_SUCCESS;
  14595. }
  14596. #if PACKETVER >= 20131223
  14597. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14598. offs = 3;
  14599. npc_market_delfromsql_(nd->exname, 0, true);
  14600. }
  14601. #endif
  14602. // get the count of new entries
  14603. amount = (script_lastdata(st)-2)/offs;
  14604. // generate new shop item list
  14605. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14606. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14607. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14608. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14609. #if PACKETVER >= 20131223
  14610. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14611. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14612. nd->u.shop.shop_item[n].flag = 1;
  14613. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14614. }
  14615. #endif
  14616. }
  14617. nd->u.shop.count = n;
  14618. script_pushint(st,1);
  14619. return SCRIPT_CMD_SUCCESS;
  14620. }
  14621. BUILDIN_FUNC(npcshopadditem)
  14622. {
  14623. const char* npcname = script_getstr(st,2);
  14624. struct npc_data* nd = npc_name2id(npcname);
  14625. int n, i;
  14626. uint16 offs = 2, amount;
  14627. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14628. script_pushint(st,0);
  14629. return SCRIPT_CMD_SUCCESS;
  14630. }
  14631. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14632. offs = 3;
  14633. // get the count of new entries
  14634. amount = (script_lastdata(st)-2)/offs;
  14635. #if PACKETVER >= 20131223
  14636. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14637. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14638. uint16 nameid = script_getnum(st,i), j;
  14639. // Check existing entries
  14640. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14641. if (j == nd->u.shop.count) {
  14642. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14643. j = nd->u.shop.count;
  14644. nd->u.shop.shop_item[j].flag = 1;
  14645. nd->u.shop.count++;
  14646. }
  14647. nd->u.shop.shop_item[j].nameid = nameid;
  14648. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14649. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14650. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14651. }
  14652. script_pushint(st,1);
  14653. return SCRIPT_CMD_SUCCESS;
  14654. }
  14655. #endif
  14656. // append new items to existing shop item list
  14657. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14658. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14659. {
  14660. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14661. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14662. }
  14663. nd->u.shop.count = n;
  14664. script_pushint(st,1);
  14665. return SCRIPT_CMD_SUCCESS;
  14666. }
  14667. BUILDIN_FUNC(npcshopdelitem)
  14668. {
  14669. const char* npcname = script_getstr(st,2);
  14670. struct npc_data* nd = npc_name2id(npcname);
  14671. int n, i, size;
  14672. unsigned short amount;
  14673. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14674. script_pushint(st,0);
  14675. return SCRIPT_CMD_SUCCESS;
  14676. }
  14677. amount = script_lastdata(st)-2;
  14678. size = nd->u.shop.count;
  14679. // remove specified items from the shop item list
  14680. for( i = 3; i < 3 + amount; i++ ) {
  14681. unsigned short nameid = script_getnum(st,i);
  14682. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14683. if( n < size ) {
  14684. if (n+1 != size)
  14685. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14686. #if PACKETVER >= 20131223
  14687. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14688. npc_market_delfromsql_(nd->exname, nameid, false);
  14689. #endif
  14690. size--;
  14691. }
  14692. }
  14693. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14694. nd->u.shop.count = size;
  14695. script_pushint(st,1);
  14696. return SCRIPT_CMD_SUCCESS;
  14697. }
  14698. /**
  14699. * Sets a script to attach to a shop npc.
  14700. * npcshopattach "<npc_name>";
  14701. **/
  14702. BUILDIN_FUNC(npcshopattach)
  14703. {
  14704. const char* npcname = script_getstr(st,2);
  14705. struct npc_data* nd = npc_name2id(npcname);
  14706. int flag = 1;
  14707. if( script_hasdata(st,3) )
  14708. flag = script_getnum(st,3);
  14709. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14710. script_pushint(st,0);
  14711. return SCRIPT_CMD_SUCCESS;
  14712. }
  14713. if (flag)
  14714. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14715. else
  14716. nd->master_nd = NULL;
  14717. script_pushint(st,1);
  14718. return SCRIPT_CMD_SUCCESS;
  14719. }
  14720. /*==========================================
  14721. * Returns some values of an item [Lupus]
  14722. * Price, Weight, etc...
  14723. setitemscript(itemID,"{new item bonus script}",[n]);
  14724. Where n:
  14725. 0 - script
  14726. 1 - Equip script
  14727. 2 - Unequip script
  14728. *------------------------------------------*/
  14729. BUILDIN_FUNC(setitemscript)
  14730. {
  14731. unsigned short item_id;
  14732. int n = 0;
  14733. const char *script;
  14734. struct item_data *i_data;
  14735. struct script_code **dstscript;
  14736. item_id = script_getnum(st,2);
  14737. script = script_getstr(st,3);
  14738. if( script_hasdata(st,4) )
  14739. n=script_getnum(st,4);
  14740. i_data = itemdb_exists(item_id);
  14741. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14742. script_pushint(st,0);
  14743. return SCRIPT_CMD_SUCCESS;
  14744. }
  14745. switch (n) {
  14746. case 2:
  14747. dstscript = &i_data->unequip_script;
  14748. break;
  14749. case 1:
  14750. dstscript = &i_data->equip_script;
  14751. break;
  14752. default:
  14753. dstscript = &i_data->script;
  14754. break;
  14755. }
  14756. if(*dstscript)
  14757. script_free_code(*dstscript);
  14758. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14759. script_pushint(st,1);
  14760. return SCRIPT_CMD_SUCCESS;
  14761. }
  14762. /*=======================================================
  14763. * Add or Update a mob drop temporarily [Akinari]
  14764. * Original Idea By: [Lupus]
  14765. *
  14766. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14767. *
  14768. * If given an item the mob already drops, the rate
  14769. * is updated to the new rate. Rate cannot exceed 10000
  14770. * Returns 1 if succeeded (added/updated a mob drop)
  14771. *-------------------------------------------------------*/
  14772. BUILDIN_FUNC(addmonsterdrop)
  14773. {
  14774. struct mob_db *mob;
  14775. struct script_data *data;
  14776. unsigned short item_id;
  14777. int rate;
  14778. data = script_getdata(st, 2);
  14779. get_val(st, data); // Convert into value in case of a variable
  14780. if(data_isstring(data))
  14781. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14782. else
  14783. mob = mob_db(script_getnum(st,2));
  14784. item_id=script_getnum(st,3);
  14785. rate=script_getnum(st,4);
  14786. if(!itemdb_exists(item_id)){
  14787. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14788. return SCRIPT_CMD_FAILURE;
  14789. }
  14790. if(mob) { //We got a valid monster, check for available drop slot
  14791. unsigned char i, c = 0;
  14792. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  14793. if(mob->dropitem[i].nameid) {
  14794. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14795. c = i;
  14796. break;
  14797. }
  14798. continue;
  14799. }
  14800. if(!c) //Accept first available slot only
  14801. c = i;
  14802. }
  14803. if(c) { //Fill in the slot with the item and rate
  14804. mob->dropitem[c].nameid = item_id;
  14805. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14806. itemdb_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  14807. script_pushint(st,1);
  14808. } else //No place to put the new drop
  14809. script_pushint(st,0);
  14810. } else {
  14811. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14812. return SCRIPT_CMD_FAILURE;
  14813. }
  14814. return SCRIPT_CMD_SUCCESS;
  14815. }
  14816. /*=======================================================
  14817. * Delete a mob drop temporarily [Akinari]
  14818. * Original Idea By: [Lupus]
  14819. *
  14820. * delmonsterdrop <mob_id or name>,<item_id>;
  14821. *
  14822. * Returns 1 if succeeded (deleted a mob drop)
  14823. *-------------------------------------------------------*/
  14824. BUILDIN_FUNC(delmonsterdrop)
  14825. {
  14826. struct mob_db *mob;
  14827. struct script_data *data;
  14828. unsigned short item_id;
  14829. data = script_getdata(st, 2);
  14830. get_val(st, data); // Convert into value in case of a variable
  14831. if(data_isstring(data))
  14832. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14833. else
  14834. mob = mob_db(script_getnum(st,2));
  14835. item_id=script_getnum(st,3);
  14836. if(!itemdb_exists(item_id)){
  14837. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14838. return SCRIPT_CMD_FAILURE;
  14839. }
  14840. if(mob) { //We got a valid monster, check for item drop on monster
  14841. unsigned char i;
  14842. for(i = 0; i < MAX_MOB_DROP_TOTAL; i++) {
  14843. if(mob->dropitem[i].nameid == item_id) {
  14844. mob->dropitem[i].nameid = 0;
  14845. mob->dropitem[i].p = 0;
  14846. itemdb_reload_itemmob_data(); // Reload the mob search data stored in the item_data
  14847. script_pushint(st,1);
  14848. return SCRIPT_CMD_SUCCESS;
  14849. }
  14850. }
  14851. //No drop on that monster
  14852. script_pushint(st,0);
  14853. } else {
  14854. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14855. return SCRIPT_CMD_FAILURE;
  14856. }
  14857. return SCRIPT_CMD_SUCCESS;
  14858. }
  14859. /*==========================================
  14860. * Returns some values of a monster [Lupus]
  14861. * Name, Level, race, size, etc...
  14862. getmonsterinfo(monsterID,queryIndex);
  14863. *------------------------------------------*/
  14864. BUILDIN_FUNC(getmonsterinfo)
  14865. {
  14866. struct mob_db *mob;
  14867. int mob_id;
  14868. mob_id = script_getnum(st,2);
  14869. if (!mobdb_checkid(mob_id)) {
  14870. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14871. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  14872. script_pushconststr(st,"null");
  14873. else
  14874. script_pushint(st,-1);
  14875. return SCRIPT_CMD_SUCCESS;
  14876. }
  14877. mob = mob_db(mob_id);
  14878. switch ( script_getnum(st,3) ) {
  14879. case MOB_NAME: script_pushstrcopy(st,mob->jname); break;
  14880. case MOB_LV: script_pushint(st,mob->lv); break;
  14881. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  14882. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  14883. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  14884. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  14885. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  14886. case MOB_DEF: script_pushint(st,mob->status.def); break;
  14887. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  14888. case MOB_STR: script_pushint(st,mob->status.str); break;
  14889. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  14890. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  14891. case MOB_INT: script_pushint(st,mob->status.int_); break;
  14892. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  14893. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  14894. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  14895. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  14896. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  14897. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  14898. case MOB_RACE: script_pushint(st,mob->status.race); break;
  14899. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  14900. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  14901. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  14902. default: script_pushint(st,-1); //wrong Index
  14903. }
  14904. return SCRIPT_CMD_SUCCESS;
  14905. }
  14906. /**
  14907. * Check player's vending/buyingstore/autotrade state
  14908. * checkvending({"<Player Name>"})
  14909. * @author [Nab4]
  14910. */
  14911. BUILDIN_FUNC(checkvending) {
  14912. TBL_PC *sd = NULL;
  14913. if (!script_nick2sd(2,sd) ) {
  14914. script_pushint(st,0);
  14915. return SCRIPT_CMD_SUCCESS;
  14916. }
  14917. else {
  14918. int8 ret = 0;
  14919. if (sd->state.vending)
  14920. ret = 1;
  14921. else if (sd->state.buyingstore)
  14922. ret = 4;
  14923. if (sd->state.autotrade)
  14924. ret |= 2;
  14925. script_pushint(st, ret);
  14926. }
  14927. return SCRIPT_CMD_SUCCESS;
  14928. }
  14929. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14930. {
  14931. TBL_PC *sd = NULL;
  14932. if( script_nick2sd(2,sd) )
  14933. script_pushint(st,(sd->chatID != 0));
  14934. else
  14935. script_pushint(st,0);
  14936. return SCRIPT_CMD_SUCCESS;
  14937. }
  14938. BUILDIN_FUNC(checkidle)
  14939. {
  14940. TBL_PC *sd = NULL;
  14941. if( script_nick2sd(2,sd) )
  14942. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14943. else
  14944. script_pushint(st, 0);
  14945. return SCRIPT_CMD_SUCCESS;
  14946. }
  14947. BUILDIN_FUNC(searchitem)
  14948. {
  14949. struct script_data* data = script_getdata(st, 2);
  14950. const char *itemname = script_getstr(st,3);
  14951. struct item_data *items[MAX_SEARCH];
  14952. int count;
  14953. char* name;
  14954. int32 start;
  14955. int32 id;
  14956. int32 i;
  14957. TBL_PC* sd = NULL;
  14958. if ((items[0] = itemdb_exists(atoi(itemname))))
  14959. count = 1;
  14960. else
  14961. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14962. if (!count) {
  14963. script_pushint(st, 0);
  14964. return SCRIPT_CMD_SUCCESS;
  14965. }
  14966. if( !data_isreference(data) )
  14967. {
  14968. ShowError("script:searchitem: not a variable\n");
  14969. script_reportdata(data);
  14970. st->state = END;
  14971. return SCRIPT_CMD_FAILURE;// not a variable
  14972. }
  14973. id = reference_getid(data);
  14974. start = reference_getindex(data);
  14975. name = reference_getname(data);
  14976. if( not_server_variable(*name) && !script_rid2sd(sd) )
  14977. {
  14978. return SCRIPT_CMD_SUCCESS;// no player attached
  14979. }
  14980. if( is_string_variable(name) )
  14981. {// string array
  14982. ShowError("script:searchitem: not an integer array reference\n");
  14983. script_reportdata(data);
  14984. st->state = END;
  14985. return SCRIPT_CMD_FAILURE;// not supported
  14986. }
  14987. for( i = 0; i < count; ++start, ++i )
  14988. {// Set array
  14989. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14990. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14991. }
  14992. script_pushint(st, count);
  14993. return SCRIPT_CMD_SUCCESS;
  14994. }
  14995. // [zBuffer] List of player cont commands --->
  14996. BUILDIN_FUNC(rid2name)
  14997. {
  14998. struct block_list *bl = NULL;
  14999. int rid = script_getnum(st,2);
  15000. if((bl = map_id2bl(rid)))
  15001. {
  15002. switch(bl->type) {
  15003. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  15004. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  15005. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  15006. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  15007. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  15008. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  15009. default:
  15010. ShowError("buildin_rid2name: BL type unknown.\n");
  15011. script_pushconststr(st,"");
  15012. break;
  15013. }
  15014. } else {
  15015. ShowError("buildin_rid2name: invalid RID\n");
  15016. script_pushconststr(st,"(null)");
  15017. }
  15018. return SCRIPT_CMD_SUCCESS;
  15019. }
  15020. /**
  15021. * Toggle a unit from moving.
  15022. * pcblockmove(<unit_id>,<option>);
  15023. */
  15024. BUILDIN_FUNC(pcblockmove)
  15025. {
  15026. struct block_list *bl = NULL;
  15027. if (script_getnum(st, 2))
  15028. bl = map_id2bl(script_getnum(st,2));
  15029. else
  15030. bl = map_id2bl(st->rid);
  15031. if (bl) {
  15032. struct unit_data *ud = unit_bl2ud(bl);
  15033. if (ud)
  15034. ud->state.blockedmove = script_getnum(st,3) > 0;
  15035. }
  15036. return SCRIPT_CMD_SUCCESS;
  15037. }
  15038. /**
  15039. * Toggle a unit from casting skills.
  15040. * pcblockskill(<unit_id>,<option>);
  15041. */
  15042. BUILDIN_FUNC(pcblockskill)
  15043. {
  15044. struct block_list *bl = NULL;
  15045. if (script_getnum(st, 2))
  15046. bl = map_id2bl(script_getnum(st,2));
  15047. else
  15048. bl = map_id2bl(st->rid);
  15049. if (bl) {
  15050. struct unit_data *ud = unit_bl2ud(bl);
  15051. if (ud)
  15052. ud->state.blockedskill = script_getnum(st, 3) > 0;
  15053. }
  15054. return SCRIPT_CMD_SUCCESS;
  15055. }
  15056. BUILDIN_FUNC(pcfollow)
  15057. {
  15058. TBL_PC *sd;
  15059. if( script_mapid2sd(2,sd) )
  15060. pc_follow(sd, script_getnum(st,3));
  15061. return SCRIPT_CMD_SUCCESS;
  15062. }
  15063. BUILDIN_FUNC(pcstopfollow)
  15064. {
  15065. TBL_PC *sd;
  15066. if(script_mapid2sd(2,sd))
  15067. pc_stop_following(sd);
  15068. return SCRIPT_CMD_SUCCESS;
  15069. }
  15070. // <--- [zBuffer] List of player cont commands
  15071. // [zBuffer] List of unit control commands --->
  15072. /// Checks to see if the unit exists.
  15073. ///
  15074. /// unitexists <unit id>;
  15075. BUILDIN_FUNC(unitexists)
  15076. {
  15077. struct block_list* bl;
  15078. bl = map_id2bl(script_getnum(st, 2));
  15079. if (!bl)
  15080. script_pushint(st, false);
  15081. else
  15082. script_pushint(st, true);
  15083. return SCRIPT_CMD_SUCCESS;
  15084. }
  15085. /// Gets the type of the given Game ID.
  15086. ///
  15087. /// getunittype <unit id>;
  15088. BUILDIN_FUNC(getunittype)
  15089. {
  15090. struct block_list* bl;
  15091. uint8 value = 0;
  15092. if(!script_rid2bl(2,bl))
  15093. {
  15094. script_pushint(st, -1);
  15095. return SCRIPT_CMD_FAILURE;
  15096. }
  15097. switch (bl->type) {
  15098. case BL_PC: value = UNITTYPE_PC; break;
  15099. case BL_NPC: value = UNITTYPE_NPC; break;
  15100. case BL_PET: value = UNITTYPE_PET; break;
  15101. case BL_MOB: value = UNITTYPE_MOB; break;
  15102. case BL_HOM: value = UNITTYPE_HOM; break;
  15103. case BL_MER: value = UNITTYPE_MER; break;
  15104. case BL_ELEM: value = UNITTYPE_ELEM; break;
  15105. default: value = -1; break;
  15106. }
  15107. script_pushint(st, value);
  15108. return SCRIPT_CMD_SUCCESS;
  15109. }
  15110. /// Gets specific live information of a bl.
  15111. ///
  15112. /// getunitdata <unit id>,<arrayname>;
  15113. BUILDIN_FUNC(getunitdata)
  15114. {
  15115. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  15116. struct block_list* bl;
  15117. TBL_MOB* md = NULL;
  15118. TBL_HOM* hd = NULL;
  15119. TBL_MER* mc = NULL;
  15120. TBL_PET* pd = NULL;
  15121. TBL_ELEM* ed = NULL;
  15122. TBL_NPC* nd = NULL;
  15123. char* name;
  15124. struct script_data *data = script_getdata(st, 3);
  15125. if (!data_isreference(data)) {
  15126. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  15127. return SCRIPT_CMD_FAILURE;
  15128. }
  15129. if(!script_rid2bl(2,bl))
  15130. {
  15131. script_pushint(st, -1);
  15132. return SCRIPT_CMD_FAILURE;
  15133. }
  15134. switch (bl->type) {
  15135. case BL_MOB: md = map_id2md(bl->id); break;
  15136. case BL_HOM: hd = map_id2hd(bl->id); break;
  15137. case BL_PET: pd = map_id2pd(bl->id); break;
  15138. case BL_MER: mc = map_id2mc(bl->id); break;
  15139. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15140. case BL_NPC: nd = map_id2nd(bl->id); break;
  15141. }
  15142. name = reference_getname(data);
  15143. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  15144. switch(bl->type) {
  15145. case BL_MOB:
  15146. if (!md) {
  15147. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  15148. return SCRIPT_CMD_FAILURE;
  15149. }
  15150. getunitdata_sub(UMOB_SIZE, md->status.size);
  15151. getunitdata_sub(UMOB_LEVEL, md->level);
  15152. getunitdata_sub(UMOB_HP, md->status.hp);
  15153. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  15154. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  15155. getunitdata_sub(UMOB_MAPID, md->bl.m);
  15156. getunitdata_sub(UMOB_X, md->bl.x);
  15157. getunitdata_sub(UMOB_Y, md->bl.y);
  15158. getunitdata_sub(UMOB_SPEED, md->status.speed);
  15159. getunitdata_sub(UMOB_MODE, md->status.mode);
  15160. getunitdata_sub(UMOB_AI, md->special_state.ai);
  15161. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  15162. getunitdata_sub(UMOB_SEX, md->vd->sex);
  15163. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  15164. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  15165. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  15166. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  15167. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  15168. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  15169. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  15170. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  15171. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  15172. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  15173. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  15174. getunitdata_sub(UMOB_STR, md->status.str);
  15175. getunitdata_sub(UMOB_AGI, md->status.agi);
  15176. getunitdata_sub(UMOB_VIT, md->status.vit);
  15177. getunitdata_sub(UMOB_INT, md->status.int_);
  15178. getunitdata_sub(UMOB_DEX, md->status.dex);
  15179. getunitdata_sub(UMOB_LUK, md->status.luk);
  15180. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  15181. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  15182. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  15183. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  15184. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  15185. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  15186. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  15187. getunitdata_sub(UMOB_DEF, md->status.def);
  15188. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  15189. getunitdata_sub(UMOB_HIT, md->status.hit);
  15190. getunitdata_sub(UMOB_FLEE, md->status.flee);
  15191. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  15192. getunitdata_sub(UMOB_CRIT, md->status.cri);
  15193. getunitdata_sub(UMOB_RACE, md->status.race);
  15194. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  15195. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  15196. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  15197. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  15198. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  15199. break;
  15200. case BL_HOM:
  15201. if (!hd) {
  15202. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  15203. return SCRIPT_CMD_FAILURE;
  15204. }
  15205. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  15206. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  15207. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  15208. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  15209. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  15210. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  15211. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  15212. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  15213. getunitdata_sub(UHOM_X, hd->bl.x);
  15214. getunitdata_sub(UHOM_Y, hd->bl.y);
  15215. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  15216. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  15217. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  15218. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  15219. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  15220. getunitdata_sub(UHOM_STR, hd->base_status.str);
  15221. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  15222. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  15223. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  15224. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  15225. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  15226. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  15227. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  15228. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  15229. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  15230. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  15231. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  15232. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  15233. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  15234. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  15235. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  15236. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  15237. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  15238. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  15239. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  15240. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  15241. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  15242. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  15243. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  15244. break;
  15245. case BL_PET:
  15246. if (!pd) {
  15247. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  15248. return SCRIPT_CMD_FAILURE;
  15249. }
  15250. getunitdata_sub(UPET_SIZE, pd->status.size);
  15251. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  15252. getunitdata_sub(UPET_HP, pd->status.hp);
  15253. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  15254. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  15255. getunitdata_sub(UPET_MAPID, pd->bl.m);
  15256. getunitdata_sub(UPET_X, pd->bl.x);
  15257. getunitdata_sub(UPET_Y, pd->bl.y);
  15258. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  15259. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  15260. getunitdata_sub(UPET_SPEED, pd->status.speed);
  15261. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  15262. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  15263. getunitdata_sub(UPET_STR, pd->status.str);
  15264. getunitdata_sub(UPET_AGI, pd->status.agi);
  15265. getunitdata_sub(UPET_VIT, pd->status.vit);
  15266. getunitdata_sub(UPET_INT, pd->status.int_);
  15267. getunitdata_sub(UPET_DEX, pd->status.dex);
  15268. getunitdata_sub(UPET_LUK, pd->status.luk);
  15269. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  15270. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  15271. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  15272. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  15273. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  15274. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  15275. getunitdata_sub(UPET_DEF, pd->status.def);
  15276. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  15277. getunitdata_sub(UPET_HIT, pd->status.hit);
  15278. getunitdata_sub(UPET_FLEE, pd->status.flee);
  15279. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  15280. getunitdata_sub(UPET_CRIT, pd->status.cri);
  15281. getunitdata_sub(UPET_RACE, pd->status.race);
  15282. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  15283. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  15284. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  15285. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  15286. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  15287. break;
  15288. case BL_MER:
  15289. if (!mc) {
  15290. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  15291. return SCRIPT_CMD_FAILURE;
  15292. }
  15293. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  15294. getunitdata_sub(UMER_HP, mc->base_status.hp);
  15295. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  15296. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  15297. getunitdata_sub(UMER_MAPID, mc->bl.m);
  15298. getunitdata_sub(UMER_X, mc->bl.x);
  15299. getunitdata_sub(UMER_Y, mc->bl.y);
  15300. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  15301. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  15302. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  15303. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  15304. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  15305. getunitdata_sub(UMER_STR, mc->base_status.str);
  15306. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  15307. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  15308. getunitdata_sub(UMER_INT, mc->base_status.int_);
  15309. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  15310. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  15311. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  15312. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  15313. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  15314. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  15315. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  15316. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  15317. getunitdata_sub(UMER_DEF, mc->base_status.def);
  15318. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  15319. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  15320. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  15321. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  15322. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  15323. getunitdata_sub(UMER_RACE, mc->base_status.race);
  15324. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  15325. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  15326. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  15327. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  15328. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  15329. break;
  15330. case BL_ELEM:
  15331. if (!ed) {
  15332. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  15333. return SCRIPT_CMD_FAILURE;
  15334. }
  15335. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  15336. getunitdata_sub(UELE_HP, ed->elemental.hp);
  15337. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  15338. getunitdata_sub(UELE_SP, ed->elemental.sp);
  15339. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  15340. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  15341. getunitdata_sub(UELE_MAPID, ed->bl.m);
  15342. getunitdata_sub(UELE_X, ed->bl.x);
  15343. getunitdata_sub(UELE_Y, ed->bl.y);
  15344. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  15345. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  15346. getunitdata_sub(UELE_SP, ed->base_status.speed);
  15347. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  15348. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  15349. getunitdata_sub(UELE_STR, ed->base_status.str);
  15350. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  15351. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  15352. getunitdata_sub(UELE_INT, ed->base_status.int_);
  15353. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  15354. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  15355. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  15356. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  15357. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  15358. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  15359. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  15360. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  15361. getunitdata_sub(UELE_DEF, ed->base_status.def);
  15362. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  15363. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  15364. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  15365. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  15366. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  15367. getunitdata_sub(UELE_RACE, ed->base_status.race);
  15368. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  15369. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  15370. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  15371. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  15372. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  15373. break;
  15374. case BL_NPC:
  15375. if (!nd) {
  15376. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  15377. return SCRIPT_CMD_FAILURE;
  15378. }
  15379. getunitdata_sub(UNPC_DISPLAY, nd->class_);
  15380. getunitdata_sub(UNPC_LEVEL, nd->level);
  15381. getunitdata_sub(UNPC_HP, nd->status.hp);
  15382. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  15383. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  15384. getunitdata_sub(UNPC_X, nd->bl.x);
  15385. getunitdata_sub(UNPC_Y, nd->bl.y);
  15386. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  15387. getunitdata_sub(UNPC_STR, nd->status.str);
  15388. getunitdata_sub(UNPC_AGI, nd->status.agi);
  15389. getunitdata_sub(UNPC_VIT, nd->status.vit);
  15390. getunitdata_sub(UNPC_INT, nd->status.int_);
  15391. getunitdata_sub(UNPC_DEX, nd->status.dex);
  15392. getunitdata_sub(UNPC_LUK, nd->status.luk);
  15393. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  15394. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  15395. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  15396. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  15397. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  15398. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  15399. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  15400. getunitdata_sub(UNPC_DEF, nd->status.def);
  15401. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  15402. getunitdata_sub(UNPC_HIT, nd->status.hit);
  15403. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  15404. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  15405. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  15406. getunitdata_sub(UNPC_RACE, nd->status.race);
  15407. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  15408. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  15409. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  15410. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  15411. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  15412. break;
  15413. default:
  15414. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  15415. return SCRIPT_CMD_FAILURE;
  15416. }
  15417. #undef getunitdata_sub
  15418. return SCRIPT_CMD_SUCCESS;
  15419. }
  15420. /// Changes the live data of a bl.
  15421. ///
  15422. /// setunitdata <unit id>,<type>,<value>;
  15423. BUILDIN_FUNC(setunitdata)
  15424. {
  15425. struct block_list* bl = NULL;
  15426. struct script_data* data;
  15427. const char *mapname = NULL;
  15428. TBL_MOB* md = NULL;
  15429. TBL_HOM* hd = NULL;
  15430. TBL_MER* mc = NULL;
  15431. TBL_PET* pd = NULL;
  15432. TBL_ELEM* ed = NULL;
  15433. TBL_NPC* nd = NULL;
  15434. int type, value = 0;
  15435. bool calc_status = false;
  15436. if(!script_rid2bl(2,bl))
  15437. {
  15438. script_pushint(st, -1);
  15439. return SCRIPT_CMD_FAILURE;
  15440. }
  15441. switch (bl->type) {
  15442. case BL_MOB: md = map_id2md(bl->id); break;
  15443. case BL_HOM: hd = map_id2hd(bl->id); break;
  15444. case BL_PET: pd = map_id2pd(bl->id); break;
  15445. case BL_MER: mc = map_id2mc(bl->id); break;
  15446. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15447. case BL_NPC:
  15448. nd = map_id2nd(bl->id);
  15449. if (!nd->status.hp)
  15450. status_calc_npc(nd, SCO_FIRST);
  15451. else
  15452. status_calc_npc(nd, SCO_NONE);
  15453. break;
  15454. default:
  15455. ShowError("buildin_setunitdata: Invalid object!\n");
  15456. return SCRIPT_CMD_FAILURE;
  15457. }
  15458. type = script_getnum(st, 3);
  15459. data = script_getdata(st, 4);
  15460. get_val(st, data);
  15461. if (type == 5 && data_isstring(data))
  15462. mapname = conv_str(st, data);
  15463. else if (data_isint(data))
  15464. value = conv_num(st, data);
  15465. else {
  15466. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).\n", data->type);
  15467. return SCRIPT_CMD_FAILURE;
  15468. }
  15469. switch (bl->type) {
  15470. case BL_MOB:
  15471. if (!md) {
  15472. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  15473. return SCRIPT_CMD_FAILURE;
  15474. }
  15475. if (!md->base_status) {
  15476. md->base_status = (struct status_data*)aCalloc(1, sizeof(struct status_data));
  15477. memcpy(md->base_status, &md->db->status, sizeof(struct status_data));
  15478. }
  15479. switch (type) {
  15480. case UMOB_SIZE: md->base_status->size = (unsigned char)value; calc_status = true; break;
  15481. case UMOB_LEVEL: md->level = (unsigned short)value; break;
  15482. case UMOB_HP: md->base_status->hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15483. 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;
  15484. case UMOB_MASTERAID: md->master_id = value; break;
  15485. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15486. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15487. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  15488. case UMOB_SPEED: md->base_status->speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15489. case UMOB_MODE: md->base_status->mode = (enum e_mode)value; calc_status = true; break;
  15490. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15491. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15492. case UMOB_SEX: md->vd->sex = (char)value; break;
  15493. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); break;
  15494. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15495. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15496. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15497. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15498. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15499. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15500. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15501. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15502. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15503. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15504. case UMOB_STR: md->base_status->str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15505. case UMOB_AGI: md->base_status->agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15506. case UMOB_VIT: md->base_status->vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15507. case UMOB_INT: md->base_status->int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15508. case UMOB_DEX: md->base_status->dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15509. case UMOB_LUK: md->base_status->luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); calc_status = true; break;
  15510. case UMOB_SLAVECPYMSTRMD:
  15511. if (value > 0) {
  15512. TBL_MOB *md2 = NULL;
  15513. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15514. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15515. break;
  15516. }
  15517. md->base_status->mode = md2->status.mode;
  15518. md->state.copy_master_mode = 1;
  15519. } else
  15520. md->state.copy_master_mode = 0;
  15521. calc_status = true;
  15522. break;
  15523. case UMOB_DMGIMMUNE: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15524. case UMOB_ATKRANGE: md->base_status->rhw.range = (unsigned short)value; calc_status = true; break;
  15525. case UMOB_ATKMIN: md->base_status->rhw.atk = (unsigned short)value; calc_status = true; break;
  15526. case UMOB_ATKMAX: md->base_status->rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15527. case UMOB_MATKMIN: md->base_status->matk_min = (unsigned short)value; calc_status = true; break;
  15528. case UMOB_MATKMAX: md->base_status->matk_max = (unsigned short)value; calc_status = true; break;
  15529. case UMOB_DEF: md->base_status->def = (defType)value; calc_status = true; break;
  15530. case UMOB_MDEF: md->base_status->mdef = (defType)value; calc_status = true; break;
  15531. case UMOB_HIT: md->base_status->hit = (short)value; calc_status = true; break;
  15532. case UMOB_FLEE: md->base_status->flee = (short)value; calc_status = true; break;
  15533. case UMOB_PDODGE: md->base_status->flee2 = (short)value; calc_status = true; break;
  15534. case UMOB_CRIT: md->base_status->cri = (short)value; calc_status = true; break;
  15535. case UMOB_RACE: md->base_status->race = (unsigned char)value; calc_status = true; break;
  15536. case UMOB_ELETYPE: md->base_status->def_ele = (unsigned char)value; calc_status = true; break;
  15537. case UMOB_ELELEVEL: md->base_status->ele_lv = (unsigned char)value; calc_status = true; break;
  15538. case UMOB_AMOTION: md->base_status->amotion = (short)value; calc_status = true; break;
  15539. case UMOB_ADELAY: md->base_status->adelay = (short)value; calc_status = true; break;
  15540. case UMOB_DMOTION: md->base_status->dmotion = (short)value; calc_status = true; break;
  15541. default:
  15542. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15543. return SCRIPT_CMD_FAILURE;
  15544. }
  15545. if (calc_status)
  15546. status_calc_bl(&md->bl, SCB_BATTLE);
  15547. break;
  15548. case BL_HOM:
  15549. if (!hd) {
  15550. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15551. return SCRIPT_CMD_FAILURE;
  15552. }
  15553. switch (type) {
  15554. case UHOM_SIZE: hd->base_status.size = (unsigned char)value; calc_status = true; break;
  15555. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15556. case UHOM_HP: hd->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15557. case UHOM_MAXHP: hd->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15558. case UHOM_SP: hd->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  15559. case UHOM_MAXSP: hd->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  15560. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15561. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15562. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15563. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15564. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15565. 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;
  15566. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15567. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15568. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15569. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15570. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15571. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15572. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15573. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15574. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); calc_status = true; break;
  15575. case UHOM_DMGIMMUNE: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15576. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15577. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15578. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15579. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15580. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15581. case UHOM_DEF: hd->base_status.def = (defType)value; calc_status = true; break;
  15582. case UHOM_MDEF: hd->base_status.mdef = (defType)value; calc_status = true; break;
  15583. case UHOM_HIT: hd->base_status.hit = (short)value; calc_status = true; break;
  15584. case UHOM_FLEE: hd->base_status.flee = (short)value; calc_status = true; break;
  15585. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; calc_status = true; break;
  15586. case UHOM_CRIT: hd->base_status.cri = (short)value; calc_status = true; break;
  15587. case UHOM_RACE: hd->base_status.race = (unsigned char)value; calc_status = true; break;
  15588. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15589. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15590. case UHOM_AMOTION: hd->base_status.amotion = (short)value; calc_status = true; break;
  15591. case UHOM_ADELAY: hd->base_status.adelay = (short)value; calc_status = true; break;
  15592. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; calc_status = true; break;
  15593. default:
  15594. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15595. return SCRIPT_CMD_FAILURE;
  15596. }
  15597. if (calc_status)
  15598. status_calc_bl(&hd->bl, SCB_BATTLE);
  15599. break;
  15600. case BL_PET:
  15601. if (!pd) {
  15602. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15603. return SCRIPT_CMD_FAILURE;
  15604. }
  15605. switch (type) {
  15606. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15607. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15608. case UPET_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15609. case UPET_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15610. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15611. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15612. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15613. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15614. case UPET_HUNGER: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  15615. 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;
  15616. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15617. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15618. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15619. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15620. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15621. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15622. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15623. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15624. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15625. case UPET_DMGIMMUNE: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15626. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15627. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15628. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15629. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15630. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15631. case UPET_DEF: pd->status.def = (defType)value; break;
  15632. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15633. case UPET_HIT: pd->status.hit = (short)value; break;
  15634. case UPET_FLEE: pd->status.flee = (short)value; break;
  15635. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  15636. case UPET_CRIT: pd->status.cri = (short)value; break;
  15637. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  15638. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  15639. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  15640. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  15641. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  15642. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  15643. default:
  15644. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15645. return SCRIPT_CMD_FAILURE;
  15646. }
  15647. break;
  15648. case BL_MER:
  15649. if (!mc) {
  15650. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15651. return SCRIPT_CMD_FAILURE;
  15652. }
  15653. switch (type) {
  15654. case UMER_SIZE: mc->base_status.size = (unsigned char)value; calc_status = true; break;
  15655. case UMER_HP: mc->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15656. case UMER_MAXHP: mc->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15657. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  15658. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15659. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15660. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15661. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  15662. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  15663. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15664. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15665. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15666. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15667. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15668. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15669. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15670. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15671. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); calc_status = true; break;
  15672. case UMER_DMGIMMUNE: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15673. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15674. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15675. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15676. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15677. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15678. case UMER_DEF: mc->base_status.def = (defType)value; calc_status = true; break;
  15679. case UMER_MDEF: mc->base_status.mdef = (defType)value; calc_status = true; break;
  15680. case UMER_HIT: mc->base_status.hit = (short)value; calc_status = true; break;
  15681. case UMER_FLEE: mc->base_status.flee = (short)value; calc_status = true; break;
  15682. case UMER_PDODGE: mc->base_status.flee2 = (short)value; calc_status = true; break;
  15683. case UMER_CRIT: mc->base_status.cri = (short)value; calc_status = true; break;
  15684. case UMER_RACE: mc->base_status.race = (unsigned char)value; calc_status = true; break;
  15685. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15686. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15687. case UMER_AMOTION: mc->base_status.amotion = (short)value; calc_status = true; break;
  15688. case UMER_ADELAY: mc->base_status.adelay = (short)value; calc_status = true; break;
  15689. case UMER_DMOTION: mc->base_status.dmotion = (short)value; calc_status = true; break;
  15690. default:
  15691. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  15692. return SCRIPT_CMD_FAILURE;
  15693. }
  15694. if (calc_status)
  15695. status_calc_bl(&mc->bl, SCB_BATTLE);
  15696. break;
  15697. case BL_ELEM:
  15698. if (!ed) {
  15699. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  15700. return SCRIPT_CMD_FAILURE;
  15701. }
  15702. switch (type) {
  15703. case UELE_SIZE: ed->base_status.size = (unsigned char)value; calc_status = true; break;
  15704. case UELE_HP: ed->base_status.hp = (unsigned int)value; status_set_hp(bl, (unsigned int)value, 0); break;
  15705. case UELE_MAXHP: ed->base_status.max_hp = (unsigned int)value; status_set_maxhp(bl, (unsigned int)value, 0); break;
  15706. case UELE_SP: ed->base_status.sp = (unsigned int)value; status_set_sp(bl, (unsigned int)value, 0); break;
  15707. case UELE_MAXSP: ed->base_status.max_sp = (unsigned int)value; status_set_maxsp(bl, (unsigned int)value, 0); break;
  15708. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  15709. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15710. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  15711. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  15712. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  15713. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; calc_status = true; break;
  15714. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15715. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15716. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15717. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15718. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15719. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15720. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15721. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15722. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); calc_status = true; break;
  15723. case UELE_DMGIMMUNE: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15724. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; calc_status = true; break;
  15725. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; calc_status = true; break;
  15726. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; calc_status = true; break;
  15727. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; calc_status = true; break;
  15728. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; calc_status = true; break;
  15729. case UELE_DEF: ed->base_status.def = (defType)value; calc_status = true; break;
  15730. case UELE_MDEF: ed->base_status.mdef = (defType)value; calc_status = true; break;
  15731. case UELE_HIT: ed->base_status.hit = (short)value; calc_status = true; break;
  15732. case UELE_FLEE: ed->base_status.flee = (short)value; calc_status = true; break;
  15733. case UELE_PDODGE: ed->base_status.flee2 = (short)value; calc_status = true; break;
  15734. case UELE_CRIT: ed->base_status.cri = (short)value; calc_status = true; break;
  15735. case UELE_RACE: ed->base_status.race = (unsigned char)value; calc_status = true; break;
  15736. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; calc_status = true; break;
  15737. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; calc_status = true; break;
  15738. case UELE_AMOTION: ed->base_status.amotion = (short)value; calc_status = true; break;
  15739. case UELE_ADELAY: ed->base_status.adelay = (short)value; calc_status = true; break;
  15740. case UELE_DMOTION: ed->base_status.dmotion = (short)value; calc_status = true; break;
  15741. default:
  15742. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  15743. return SCRIPT_CMD_FAILURE;
  15744. }
  15745. if (calc_status)
  15746. status_calc_bl(&ed->bl, SCB_BATTLE);
  15747. break;
  15748. case BL_NPC:
  15749. if (!nd) {
  15750. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  15751. return SCRIPT_CMD_FAILURE;
  15752. }
  15753. switch (type) {
  15754. case UNPC_DISPLAY: status_set_viewdata(bl, (unsigned short)value); break;
  15755. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  15756. case UNPC_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15757. case UNPC_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15758. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15759. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  15760. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  15761. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15762. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15763. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15764. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15765. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15766. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15767. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15768. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  15769. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  15770. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  15771. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  15772. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  15773. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  15774. case UNPC_DEF: nd->status.def = (defType)value; break;
  15775. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  15776. case UNPC_HIT: nd->status.hit = (short)value; break;
  15777. case UNPC_FLEE: nd->status.flee = (short)value; break;
  15778. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  15779. case UNPC_CRIT: nd->status.cri = (short)value; break;
  15780. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  15781. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  15782. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  15783. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  15784. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  15785. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  15786. default:
  15787. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  15788. return SCRIPT_CMD_FAILURE;
  15789. }
  15790. break;
  15791. default:
  15792. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  15793. return SCRIPT_CMD_FAILURE;
  15794. }
  15795. // Client information updates
  15796. switch (bl->type) {
  15797. case BL_HOM:
  15798. clif_send_homdata(hd->master, SP_ACK, 0);
  15799. break;
  15800. case BL_PET:
  15801. clif_send_petstatus(pd->master);
  15802. break;
  15803. case BL_MER:
  15804. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  15805. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  15806. break;
  15807. case BL_ELEM:
  15808. clif_elemental_info(ed->master);
  15809. break;
  15810. }
  15811. return SCRIPT_CMD_SUCCESS;
  15812. }
  15813. /// Gets the name of a bl.
  15814. /// Supported types are [MOB|HOM|PET|NPC].
  15815. /// MER and ELEM don't support custom names.
  15816. ///
  15817. /// getunitname <unit id>;
  15818. BUILDIN_FUNC(getunitname)
  15819. {
  15820. struct block_list* bl = NULL;
  15821. if(!script_rid2bl(2,bl)){
  15822. script_pushconststr(st, "Unknown");
  15823. return SCRIPT_CMD_FAILURE;
  15824. }
  15825. script_pushstrcopy(st, status_get_name(bl));
  15826. return SCRIPT_CMD_SUCCESS;
  15827. }
  15828. /// Changes the name of a bl.
  15829. /// Supported types are [MOB|HOM|PET].
  15830. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  15831. ///
  15832. /// setunitname <unit id>,<name>;
  15833. BUILDIN_FUNC(setunitname)
  15834. {
  15835. struct block_list* bl = NULL;
  15836. TBL_MOB* md = NULL;
  15837. TBL_HOM* hd = NULL;
  15838. TBL_PET* pd = NULL;
  15839. if(!script_rid2bl(2,bl))
  15840. {
  15841. script_pushconststr(st, "Unknown");
  15842. return SCRIPT_CMD_FAILURE;
  15843. }
  15844. switch (bl->type) {
  15845. case BL_MOB: md = map_id2md(bl->id); break;
  15846. case BL_HOM: hd = map_id2hd(bl->id); break;
  15847. case BL_PET: pd = map_id2pd(bl->id); break;
  15848. }
  15849. switch (bl->type) {
  15850. case BL_MOB:
  15851. if (!md) {
  15852. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  15853. return SCRIPT_CMD_FAILURE;
  15854. }
  15855. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  15856. break;
  15857. case BL_HOM:
  15858. if (!hd) {
  15859. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  15860. return SCRIPT_CMD_FAILURE;
  15861. }
  15862. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  15863. break;
  15864. case BL_PET:
  15865. if (!pd) {
  15866. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  15867. return SCRIPT_CMD_FAILURE;
  15868. }
  15869. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  15870. break;
  15871. default:
  15872. ShowWarning("buildin_setunitname: Unknown object type!\n");
  15873. return SCRIPT_CMD_FAILURE;
  15874. }
  15875. clif_name_area(bl); // Send update to client.
  15876. return SCRIPT_CMD_SUCCESS;
  15877. }
  15878. /// Makes the unit walk to target position or map.
  15879. /// Returns if it was successful.
  15880. ///
  15881. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  15882. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  15883. BUILDIN_FUNC(unitwalk)
  15884. {
  15885. struct block_list* bl;
  15886. struct unit_data *ud = NULL;
  15887. const char *cmd = script_getfuncname(st), *done_label = "";
  15888. uint8 off = 5;
  15889. if(!script_rid2bl(2,bl))
  15890. {
  15891. script_pushint(st, 0);
  15892. return SCRIPT_CMD_FAILURE;
  15893. }
  15894. ud = unit_bl2ud(bl);
  15895. if (!strcmp(cmd,"unitwalk")) {
  15896. int x = script_getnum(st,3);
  15897. int y = script_getnum(st,4);
  15898. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  15899. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  15900. } else {
  15901. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  15902. if (!tbl) {
  15903. ShowError("buildin_unitwalk: Bad target destination.\n");
  15904. script_pushint(st, 0);
  15905. return SCRIPT_CMD_FAILURE;
  15906. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  15907. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  15908. off = 4;
  15909. }
  15910. if (ud && script_hasdata(st, off)) {
  15911. done_label = script_getstr(st, off);
  15912. check_event(st, done_label);
  15913. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  15914. }
  15915. return SCRIPT_CMD_SUCCESS;
  15916. }
  15917. /// Kills the unit.
  15918. ///
  15919. /// unitkill <unit_id>;
  15920. BUILDIN_FUNC(unitkill)
  15921. {
  15922. struct block_list* bl;
  15923. if(script_rid2bl(2,bl))
  15924. status_kill(bl);
  15925. return SCRIPT_CMD_SUCCESS;
  15926. }
  15927. /// Warps the unit to the target position in the target map.
  15928. /// Returns if it was successful.
  15929. ///
  15930. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  15931. BUILDIN_FUNC(unitwarp)
  15932. {
  15933. int map_idx;
  15934. short x;
  15935. short y;
  15936. struct block_list* bl;
  15937. const char *mapname;
  15938. mapname = script_getstr(st, 3);
  15939. x = (short)script_getnum(st,4);
  15940. y = (short)script_getnum(st,5);
  15941. if(!script_rid2bl(2,bl))
  15942. {
  15943. script_pushint(st, 0);
  15944. return SCRIPT_CMD_SUCCESS;
  15945. }
  15946. if (!strcmp(mapname,"this"))
  15947. map_idx = bl?bl->m:-1;
  15948. else
  15949. map_idx = map_mapname2mapid(mapname);
  15950. if (map_idx >= 0 && bl != NULL)
  15951. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15952. else
  15953. script_pushint(st, 0);
  15954. return SCRIPT_CMD_SUCCESS;
  15955. }
  15956. /// Makes the unit attack the target.
  15957. /// If the unit is a player and <action type> is not 0, it does a continuous
  15958. /// attack instead of a single attack.
  15959. /// Returns if the request was successful.
  15960. ///
  15961. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15962. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15963. BUILDIN_FUNC(unitattack)
  15964. {
  15965. struct block_list* unit_bl;
  15966. struct block_list* target_bl = NULL;
  15967. struct script_data* data;
  15968. int actiontype = 0;
  15969. if (!script_rid2bl(2,unit_bl)) {
  15970. script_pushint(st, 0);
  15971. return SCRIPT_CMD_FAILURE;
  15972. }
  15973. data = script_getdata(st, 3);
  15974. get_val(st, data);
  15975. if (data_isstring(data)) {
  15976. TBL_PC* sd = map_nick2sd(conv_str(st, data),false);
  15977. if( sd != NULL )
  15978. target_bl = &sd->bl;
  15979. } else
  15980. target_bl = map_id2bl(conv_num(st, data));
  15981. if (!target_bl) {
  15982. script_pushint(st, 0);
  15983. return SCRIPT_CMD_FAILURE;
  15984. }
  15985. if (script_hasdata(st,4))
  15986. actiontype = script_getnum(st,4);
  15987. switch(unit_bl->type) {
  15988. case BL_PC:
  15989. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15990. script_pushint(st, 1);
  15991. return SCRIPT_CMD_SUCCESS;
  15992. case BL_MOB:
  15993. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15994. break;
  15995. case BL_PET:
  15996. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15997. break;
  15998. default:
  15999. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  16000. script_pushint(st, 0);
  16001. return SCRIPT_CMD_FAILURE;
  16002. }
  16003. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  16004. return SCRIPT_CMD_SUCCESS;
  16005. }
  16006. /// Makes the unit stop attacking.
  16007. ///
  16008. /// unitstopattack <unit_id>;
  16009. BUILDIN_FUNC(unitstopattack)
  16010. {
  16011. struct block_list* bl;
  16012. if(script_rid2bl(2,bl))
  16013. {
  16014. unit_stop_attack(bl);
  16015. if (bl->type == BL_MOB)
  16016. ((TBL_MOB*)bl)->target_id = 0;
  16017. }
  16018. return SCRIPT_CMD_SUCCESS;
  16019. }
  16020. /// Makes the unit stop walking.
  16021. ///
  16022. /// unitstopwalk <unit_id>{,<flag>};
  16023. BUILDIN_FUNC(unitstopwalk)
  16024. {
  16025. struct block_list* bl;
  16026. int flag = USW_NONE;
  16027. if (script_hasdata(st, 3))
  16028. flag = script_getnum(st, 3);
  16029. if(script_rid2bl(2,bl))
  16030. unit_stop_walking(bl, flag);
  16031. return SCRIPT_CMD_SUCCESS;
  16032. }
  16033. /**
  16034. * Makes the unit say the given message.
  16035. *
  16036. * unittalk <unit_id>,"<message>"{,"<flag>"};
  16037. * @param flag: Specify target
  16038. * bc_area - Message is sent to players in the vicinity of the source (default).
  16039. * bc_self - Message is sent only to player attached.
  16040. */
  16041. BUILDIN_FUNC(unittalk)
  16042. {
  16043. const char* message;
  16044. struct block_list* bl;
  16045. message = script_getstr(st, 3);
  16046. if(script_rid2bl(2,bl))
  16047. {
  16048. send_target target = AREA;
  16049. struct StringBuf sbuf;
  16050. if (script_hasdata(st, 4)) {
  16051. if (script_getnum(st, 4) == BC_SELF) {
  16052. if (map_id2sd(bl->id) == NULL) {
  16053. ShowWarning("script: unittalk: bc_self can't be used for non-players objects.\n");
  16054. return SCRIPT_CMD_FAILURE;
  16055. }
  16056. target = SELF;
  16057. }
  16058. }
  16059. StringBuf_Init(&sbuf);
  16060. StringBuf_Printf(&sbuf, "%s", message);
  16061. clif_disp_overhead_(bl, StringBuf_Value(&sbuf), target);
  16062. StringBuf_Destroy(&sbuf);
  16063. }
  16064. return SCRIPT_CMD_SUCCESS;
  16065. }
  16066. /// Makes the unit do an emotion.
  16067. ///
  16068. /// unitemote <unit_id>,<emotion>;
  16069. ///
  16070. /// @see e_* in db/const.txt
  16071. BUILDIN_FUNC(unitemote)
  16072. {
  16073. int emotion;
  16074. struct block_list* bl;
  16075. emotion = script_getnum(st,3);
  16076. if(script_rid2bl(2,bl))
  16077. clif_emotion(bl, emotion);
  16078. return SCRIPT_CMD_SUCCESS;
  16079. }
  16080. /// Makes the unit cast the skill on the target or self if no target is specified.
  16081. ///
  16082. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  16083. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  16084. BUILDIN_FUNC(unitskilluseid)
  16085. {
  16086. int unit_id, target_id, casttime;
  16087. uint16 skill_id, skill_lv;
  16088. struct block_list* bl;
  16089. struct script_data *data;
  16090. unit_id = script_getnum(st,2);
  16091. data = script_getdata(st, 3);
  16092. get_val(st, data); // Convert into value in case of a variable
  16093. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  16094. skill_lv = script_getnum(st,4);
  16095. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  16096. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  16097. if(script_rid2bl(2,bl)){
  16098. if (bl->type == BL_NPC) {
  16099. if (!((TBL_NPC*)bl)->status.hp)
  16100. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16101. else
  16102. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16103. }
  16104. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  16105. }
  16106. return SCRIPT_CMD_SUCCESS;
  16107. }
  16108. /// Makes the unit cast the skill on the target position.
  16109. ///
  16110. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  16111. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  16112. BUILDIN_FUNC(unitskillusepos)
  16113. {
  16114. int skill_x, skill_y, casttime;
  16115. uint16 skill_id, skill_lv;
  16116. struct block_list* bl;
  16117. struct script_data *data;
  16118. data = script_getdata(st, 3);
  16119. get_val(st, data); // Convert into value in case of a variable
  16120. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  16121. skill_lv = script_getnum(st,4);
  16122. skill_x = script_getnum(st,5);
  16123. skill_y = script_getnum(st,6);
  16124. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  16125. if(script_rid2bl(2,bl)){
  16126. if (bl->type == BL_NPC) {
  16127. if (!((TBL_NPC*)bl)->status.hp)
  16128. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  16129. else
  16130. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  16131. }
  16132. 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));
  16133. }
  16134. return SCRIPT_CMD_SUCCESS;
  16135. }
  16136. // <--- [zBuffer] List of unit control commands
  16137. /// Pauses the execution of the script, detaching the player
  16138. ///
  16139. /// sleep <mili seconds>;
  16140. BUILDIN_FUNC(sleep)
  16141. {
  16142. // First call(by function call)
  16143. if (st->sleep.tick == 0) {
  16144. int ticks;
  16145. ticks = script_getnum(st, 2);
  16146. if (ticks <= 0) {
  16147. ShowError("buildin_sleep2: negative amount('%d') of milli seconds is not supported\n", ticks);
  16148. return SCRIPT_CMD_FAILURE;
  16149. }
  16150. // detach the player
  16151. script_detach_rid(st);
  16152. // sleep for the target amount of time
  16153. st->state = RERUNLINE;
  16154. st->sleep.tick = ticks;
  16155. // Second call(by timer after sleeping time is over)
  16156. } else {
  16157. // Continue the script
  16158. st->state = RUN;
  16159. st->sleep.tick = 0;
  16160. }
  16161. return SCRIPT_CMD_SUCCESS;
  16162. }
  16163. /// Pauses the execution of the script, keeping the unit attached
  16164. /// Stops the script if no unit is attached
  16165. ///
  16166. /// sleep2(<milli seconds>)
  16167. BUILDIN_FUNC(sleep2)
  16168. {
  16169. // First call(by function call)
  16170. if (st->sleep.tick == 0) {
  16171. int ticks;
  16172. ticks = script_getnum(st, 2);
  16173. if (ticks <= 0) {
  16174. ShowError( "buildin_sleep2: negative amount('%d') of milli seconds is not supported\n", ticks );
  16175. return SCRIPT_CMD_FAILURE;
  16176. }
  16177. if (map_id2bl(st->rid) == NULL) {
  16178. ShowError( "buildin_sleep2: no unit is attached\n" );
  16179. return SCRIPT_CMD_FAILURE;
  16180. }
  16181. // sleep for the target amount of time
  16182. st->state = RERUNLINE;
  16183. st->sleep.tick = ticks;
  16184. // Second call(by timer after sleeping time is over)
  16185. } else {
  16186. // Check if the unit is still attached
  16187. // NOTE: This should never happen, since run_script_timer already checks this
  16188. if (map_id2bl(st->rid) == NULL) {
  16189. // The unit is not attached anymore - terminate the script
  16190. st->rid = 0;
  16191. st->state = END;
  16192. } else {
  16193. // The unit is still attached - continue the script
  16194. st->state = RUN;
  16195. st->sleep.tick = 0;
  16196. }
  16197. }
  16198. return SCRIPT_CMD_SUCCESS;
  16199. }
  16200. /// Awakes all the sleep timers of the target npc
  16201. ///
  16202. /// awake "<npc name>";
  16203. BUILDIN_FUNC(awake)
  16204. {
  16205. DBIterator *iter;
  16206. struct script_state *tst;
  16207. struct npc_data* nd;
  16208. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  16209. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  16210. return SCRIPT_CMD_FAILURE;
  16211. }
  16212. iter = db_iterator(st_db);
  16213. for (tst = dbi_first(iter); dbi_exists(iter); tst = dbi_next(iter)) {
  16214. if (tst->oid == nd->bl.id) {
  16215. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  16216. continue;
  16217. }
  16218. delete_timer(tst->sleep.timer, run_script_timer);
  16219. // Trigger the timer function
  16220. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  16221. }
  16222. }
  16223. dbi_destroy(iter);
  16224. return SCRIPT_CMD_SUCCESS;
  16225. }
  16226. /// Returns a reference to a variable of the target NPC.
  16227. /// Returns 0 if an error occurs.
  16228. ///
  16229. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  16230. BUILDIN_FUNC(getvariableofnpc)
  16231. {
  16232. struct script_data* data;
  16233. const char* name;
  16234. struct npc_data* nd;
  16235. data = script_getdata(st,2);
  16236. if( !data_isreference(data) )
  16237. {// Not a reference (aka varaible name)
  16238. ShowError("buildin_getvariableofnpc: not a variable\n");
  16239. script_reportdata(data);
  16240. script_pushnil(st);
  16241. st->state = END;
  16242. return SCRIPT_CMD_FAILURE;
  16243. }
  16244. name = reference_getname(data);
  16245. if( *name != '.' || name[1] == '@' )
  16246. {// not a npc variable
  16247. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  16248. script_reportdata(data);
  16249. script_pushnil(st);
  16250. st->state = END;
  16251. return SCRIPT_CMD_FAILURE;
  16252. }
  16253. nd = npc_name2id(script_getstr(st,3));
  16254. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  16255. {// NPC not found or has no script
  16256. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  16257. script_pushnil(st);
  16258. st->state = END;
  16259. return SCRIPT_CMD_FAILURE;
  16260. }
  16261. if (!nd->u.scr.script->local.vars)
  16262. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  16263. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  16264. return SCRIPT_CMD_SUCCESS;
  16265. }
  16266. /// Opens a warp portal.
  16267. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  16268. ///
  16269. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  16270. ///
  16271. /// @author blackhole89
  16272. BUILDIN_FUNC(warpportal)
  16273. {
  16274. int spx;
  16275. int spy;
  16276. unsigned short mapindex;
  16277. int tpx;
  16278. int tpy;
  16279. struct skill_unit_group* group;
  16280. struct block_list* bl;
  16281. bl = map_id2bl(st->oid);
  16282. if( bl == NULL ) {
  16283. ShowError("buildin_warpportal: NPC is needed\n");
  16284. return SCRIPT_CMD_FAILURE;
  16285. }
  16286. spx = script_getnum(st,2);
  16287. spy = script_getnum(st,3);
  16288. mapindex = mapindex_name2id(script_getstr(st, 4));
  16289. tpx = script_getnum(st,5);
  16290. tpy = script_getnum(st,6);
  16291. if( mapindex == 0 ) {
  16292. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  16293. return SCRIPT_CMD_FAILURE;
  16294. }
  16295. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  16296. if( group == NULL )
  16297. return SCRIPT_CMD_FAILURE;// failed
  16298. group->val1 = (group->val1<<16)|(short)0;
  16299. group->val2 = (tpx<<16) | tpy;
  16300. group->val3 = mapindex;
  16301. return SCRIPT_CMD_SUCCESS;
  16302. }
  16303. /**
  16304. * openmail({<char_id>});
  16305. **/
  16306. BUILDIN_FUNC(openmail)
  16307. {
  16308. TBL_PC* sd;
  16309. if (!script_charid2sd(2,sd))
  16310. return SCRIPT_CMD_FAILURE;
  16311. mail_openmail(sd);
  16312. return SCRIPT_CMD_SUCCESS;
  16313. }
  16314. /**
  16315. * openauction({<char_id>});
  16316. **/
  16317. BUILDIN_FUNC(openauction)
  16318. {
  16319. TBL_PC* sd;
  16320. if (!script_charid2sd(2,sd))
  16321. return SCRIPT_CMD_FAILURE;
  16322. if( !battle_config.feature_auction ) {
  16323. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  16324. return SCRIPT_CMD_SUCCESS;
  16325. }
  16326. clif_Auction_openwindow(sd);
  16327. return SCRIPT_CMD_SUCCESS;
  16328. }
  16329. /// Retrieves the value of the specified flag of the specified cell.
  16330. ///
  16331. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  16332. ///
  16333. /// @see cell_chk* constants in const.txt for the types
  16334. BUILDIN_FUNC(checkcell)
  16335. {
  16336. int16 m = map_mapname2mapid(script_getstr(st,2));
  16337. int16 x = script_getnum(st,3);
  16338. int16 y = script_getnum(st,4);
  16339. cell_chk type = (cell_chk)script_getnum(st,5);
  16340. script_pushint(st, map_getcell(m, x, y, type));
  16341. return SCRIPT_CMD_SUCCESS;
  16342. }
  16343. /// Modifies flags of cells in the specified area.
  16344. ///
  16345. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  16346. ///
  16347. /// @see cell_* constants in const.txt for the types
  16348. BUILDIN_FUNC(setcell)
  16349. {
  16350. int16 m = map_mapname2mapid(script_getstr(st,2));
  16351. int16 x1 = script_getnum(st,3);
  16352. int16 y1 = script_getnum(st,4);
  16353. int16 x2 = script_getnum(st,5);
  16354. int16 y2 = script_getnum(st,6);
  16355. cell_t type = (cell_t)script_getnum(st,7);
  16356. bool flag = (bool)script_getnum(st,8);
  16357. int x,y;
  16358. if( x1 > x2 ) swap(x1,x2);
  16359. if( y1 > y2 ) swap(y1,y2);
  16360. for( y = y1; y <= y2; ++y )
  16361. for( x = x1; x <= x2; ++x )
  16362. map_setcell(m, x, y, type, flag);
  16363. return SCRIPT_CMD_SUCCESS;
  16364. }
  16365. /**
  16366. * Gets a free cell in the specified area.
  16367. * getfreecell "<map name>",<rX>,<rY>{,<x>,<y>,<rangeX>,<rangeY>,<flag>};
  16368. */
  16369. BUILDIN_FUNC(getfreecell)
  16370. {
  16371. const char *mapn = script_getstr(st, 2), *name;
  16372. char prefix;
  16373. struct map_session_data *sd;
  16374. int64 num;
  16375. int16 m, x = 0, y = 0;
  16376. int rx = -1, ry = -1, flag = 1;
  16377. sd = map_id2sd(st->rid);
  16378. if (!data_isreference(script_getdata(st, 3))) {
  16379. ShowWarning("script: buildin_getfreecell: rX is not a variable.\n");
  16380. script_pushint(st, -1);
  16381. return SCRIPT_CMD_FAILURE;
  16382. }
  16383. if (!data_isreference(script_getdata(st, 4))) {
  16384. ShowWarning("script: buildin_getfreecell: rY is not a variable.\n");
  16385. script_pushint(st, -1);
  16386. return SCRIPT_CMD_FAILURE;
  16387. }
  16388. if (is_string_variable(reference_getname(script_getdata(st, 3)))) {
  16389. ShowWarning("script: buildin_getfreecell: rX is a string, must be an INT.\n");
  16390. script_pushint(st, -1);
  16391. return SCRIPT_CMD_FAILURE;
  16392. }
  16393. if (is_string_variable(reference_getname(script_getdata(st, 4)))) {
  16394. ShowWarning("script: buildin_getfreecell: rY is a string, must be an INT.\n");
  16395. script_pushint(st, -1);
  16396. return SCRIPT_CMD_FAILURE;
  16397. }
  16398. if (script_hasdata(st, 5))
  16399. x = script_getnum(st, 5);
  16400. if (script_hasdata(st, 6))
  16401. y = script_getnum(st, 6);
  16402. if (script_hasdata(st, 7))
  16403. rx = script_getnum(st, 7);
  16404. if (script_hasdata(st, 8))
  16405. ry = script_getnum(st, 8);
  16406. if (script_hasdata(st, 9))
  16407. flag = script_getnum(st, 9);
  16408. if (sd && strcmp(mapn, "this") == 0)
  16409. m = sd->bl.m;
  16410. else
  16411. m = map_mapname2mapid(mapn);
  16412. map_search_freecell(NULL, m, &x, &y, rx, ry, flag);
  16413. // Set MapX
  16414. num = st->stack->stack_data[st->start + 3].u.num;
  16415. name = get_str(num&0x00ffffff);
  16416. prefix = *name;
  16417. if (not_server_variable(prefix)){
  16418. if( !script_rid2sd(sd) ){
  16419. ShowError( "buildin_getfreecell: variable '%s' for mapX is not a server variable, but no player is attached!", name );
  16420. return SCRIPT_CMD_FAILURE;
  16421. }
  16422. }else
  16423. sd = NULL;
  16424. set_reg(st, sd, num, name, (void*)__64BPRTSIZE((int)x), script_getref(st, 3));
  16425. // Set MapY
  16426. num = st->stack->stack_data[st->start + 4].u.num;
  16427. name = get_str(num&0x00ffffff);
  16428. prefix = *name;
  16429. if (not_server_variable(prefix)){
  16430. if( !script_rid2sd(sd) ){
  16431. ShowError( "buildin_getfreecell: variable '%s' for mapY is not a server variable, but no player is attached!", name );
  16432. return SCRIPT_CMD_FAILURE;
  16433. }
  16434. }else
  16435. sd = NULL;
  16436. set_reg(st, sd, num, name, (void*)__64BPRTSIZE((int)y), script_getref(st, 4));
  16437. return SCRIPT_CMD_SUCCESS;
  16438. }
  16439. /*==========================================
  16440. * Mercenary Commands
  16441. *------------------------------------------*/
  16442. BUILDIN_FUNC(mercenary_create)
  16443. {
  16444. struct map_session_data *sd;
  16445. int class_, contract_time;
  16446. if( !script_rid2sd(sd) || sd->md || sd->status.mer_id != 0 )
  16447. return SCRIPT_CMD_SUCCESS;
  16448. class_ = script_getnum(st,2);
  16449. if( !mercenary_class(class_) )
  16450. return SCRIPT_CMD_SUCCESS;
  16451. contract_time = script_getnum(st,3);
  16452. mercenary_create(sd, class_, contract_time);
  16453. return SCRIPT_CMD_SUCCESS;
  16454. }
  16455. BUILDIN_FUNC(mercenary_heal)
  16456. {
  16457. struct map_session_data *sd;
  16458. int hp, sp;
  16459. if( !script_rid2sd(sd) || sd->md == NULL )
  16460. return SCRIPT_CMD_SUCCESS;
  16461. hp = script_getnum(st,2);
  16462. sp = script_getnum(st,3);
  16463. status_heal(&sd->md->bl, hp, sp, 0);
  16464. return SCRIPT_CMD_SUCCESS;
  16465. }
  16466. BUILDIN_FUNC(mercenary_sc_start)
  16467. {
  16468. struct map_session_data *sd;
  16469. enum sc_type type;
  16470. int tick, val1;
  16471. if( !script_rid2sd(sd) || sd->md == NULL )
  16472. return SCRIPT_CMD_SUCCESS;
  16473. type = (sc_type)script_getnum(st,2);
  16474. tick = script_getnum(st,3);
  16475. val1 = script_getnum(st,4);
  16476. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  16477. return SCRIPT_CMD_SUCCESS;
  16478. }
  16479. BUILDIN_FUNC(mercenary_get_calls)
  16480. {
  16481. struct map_session_data *sd;
  16482. int guild;
  16483. if( !script_rid2sd(sd) )
  16484. return SCRIPT_CMD_SUCCESS;
  16485. guild = script_getnum(st,2);
  16486. switch( guild )
  16487. {
  16488. case ARCH_MERC_GUILD:
  16489. script_pushint(st,sd->status.arch_calls);
  16490. break;
  16491. case SPEAR_MERC_GUILD:
  16492. script_pushint(st,sd->status.spear_calls);
  16493. break;
  16494. case SWORD_MERC_GUILD:
  16495. script_pushint(st,sd->status.sword_calls);
  16496. break;
  16497. default:
  16498. script_pushint(st,0);
  16499. break;
  16500. }
  16501. return SCRIPT_CMD_SUCCESS;
  16502. }
  16503. BUILDIN_FUNC(mercenary_set_calls)
  16504. {
  16505. struct map_session_data *sd;
  16506. int guild, value, *calls;
  16507. if( !script_rid2sd(sd) )
  16508. return SCRIPT_CMD_SUCCESS;
  16509. guild = script_getnum(st,2);
  16510. value = script_getnum(st,3);
  16511. switch( guild )
  16512. {
  16513. case ARCH_MERC_GUILD:
  16514. calls = &sd->status.arch_calls;
  16515. break;
  16516. case SPEAR_MERC_GUILD:
  16517. calls = &sd->status.spear_calls;
  16518. break;
  16519. case SWORD_MERC_GUILD:
  16520. calls = &sd->status.sword_calls;
  16521. break;
  16522. default:
  16523. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  16524. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16525. }
  16526. *calls += value;
  16527. *calls = cap_value(*calls, 0, INT_MAX);
  16528. return SCRIPT_CMD_SUCCESS;
  16529. }
  16530. BUILDIN_FUNC(mercenary_get_faith)
  16531. {
  16532. struct map_session_data *sd;
  16533. int guild;
  16534. if( !script_rid2sd(sd) )
  16535. return SCRIPT_CMD_SUCCESS;
  16536. guild = script_getnum(st,2);
  16537. switch( guild )
  16538. {
  16539. case ARCH_MERC_GUILD:
  16540. script_pushint(st,sd->status.arch_faith);
  16541. break;
  16542. case SPEAR_MERC_GUILD:
  16543. script_pushint(st,sd->status.spear_faith);
  16544. break;
  16545. case SWORD_MERC_GUILD:
  16546. script_pushint(st,sd->status.sword_faith);
  16547. break;
  16548. default:
  16549. script_pushint(st,0);
  16550. break;
  16551. }
  16552. return SCRIPT_CMD_SUCCESS;
  16553. }
  16554. BUILDIN_FUNC(mercenary_set_faith)
  16555. {
  16556. struct map_session_data *sd;
  16557. int guild, value, *calls;
  16558. if( !script_rid2sd(sd) )
  16559. return SCRIPT_CMD_SUCCESS;
  16560. guild = script_getnum(st,2);
  16561. value = script_getnum(st,3);
  16562. switch( guild )
  16563. {
  16564. case ARCH_MERC_GUILD:
  16565. calls = &sd->status.arch_faith;
  16566. break;
  16567. case SPEAR_MERC_GUILD:
  16568. calls = &sd->status.spear_faith;
  16569. break;
  16570. case SWORD_MERC_GUILD:
  16571. calls = &sd->status.sword_faith;
  16572. break;
  16573. default:
  16574. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  16575. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16576. }
  16577. *calls += value;
  16578. *calls = cap_value(*calls, 0, INT_MAX);
  16579. if( mercenary_get_guild(sd->md) == guild )
  16580. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  16581. return SCRIPT_CMD_SUCCESS;
  16582. }
  16583. /*------------------------------------------
  16584. * Book Reading
  16585. *------------------------------------------*/
  16586. BUILDIN_FUNC(readbook)
  16587. {
  16588. struct map_session_data *sd;
  16589. int book_id, page;
  16590. if( !script_rid2sd(sd) )
  16591. return SCRIPT_CMD_SUCCESS;
  16592. book_id = script_getnum(st,2);
  16593. page = script_getnum(st,3);
  16594. clif_readbook(sd->fd, book_id, page);
  16595. return SCRIPT_CMD_SUCCESS;
  16596. }
  16597. /******************
  16598. Questlog script commands
  16599. *******************/
  16600. /**
  16601. * Add job criteria to questinfo
  16602. * @param qi Quest Info
  16603. * @param job
  16604. * @author [Cydh]
  16605. **/
  16606. static void buildin_questinfo_setjob(struct questinfo *qi, int job) {
  16607. RECREATE(qi->jobid, unsigned short, qi->jobid_count+1);
  16608. qi->jobid[qi->jobid_count++] = job;
  16609. }
  16610. /**
  16611. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  16612. **/
  16613. BUILDIN_FUNC(questinfo)
  16614. {
  16615. TBL_NPC* nd = map_id2nd(st->oid);
  16616. int quest_id, icon, color = 0;
  16617. struct questinfo qi, *q2;
  16618. if( nd == NULL || nd->bl.m == -1 ) {
  16619. ShowError("buildin_questinfo: No NPC attached.\n");
  16620. return SCRIPT_CMD_FAILURE;
  16621. }
  16622. quest_id = script_getnum(st, 2);
  16623. icon = script_getnum(st, 3);
  16624. #if PACKETVER >= 20120410
  16625. switch(icon){
  16626. case QTYPE_QUEST:
  16627. case QTYPE_QUEST2:
  16628. case QTYPE_JOB:
  16629. case QTYPE_JOB2:
  16630. case QTYPE_EVENT:
  16631. case QTYPE_EVENT2:
  16632. case QTYPE_WARG:
  16633. case QTYPE_WARG2:
  16634. // Leave everything as it is
  16635. break;
  16636. case QTYPE_NONE:
  16637. default:
  16638. // Default to nothing if icon id is invalid.
  16639. icon = QTYPE_NONE;
  16640. break;
  16641. }
  16642. #else
  16643. if(icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  16644. icon = QTYPE_QUEST;
  16645. else
  16646. icon = icon + 1;
  16647. #endif
  16648. qi.quest_id = quest_id;
  16649. qi.icon = (unsigned char)icon;
  16650. qi.nd = nd;
  16651. if( script_hasdata(st, 4) ) {
  16652. color = script_getnum(st, 4);
  16653. if( color < 0 || color > 3 ) {
  16654. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  16655. script_reportfunc(st);
  16656. color = 0;
  16657. }
  16658. }
  16659. qi.color = (unsigned char)color;
  16660. qi.min_level = 1;
  16661. qi.max_level = MAX_LEVEL;
  16662. q2 = map_add_questinfo(nd->bl.m, &qi);
  16663. q2->req = NULL;
  16664. q2->req_count = 0;
  16665. q2->jobid = NULL;
  16666. q2->jobid_count = 0;
  16667. if(script_hasdata(st, 5)) {
  16668. int job = script_getnum(st, 5);
  16669. if (!pcdb_checkid(job))
  16670. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  16671. else {
  16672. buildin_questinfo_setjob(q2, job);
  16673. }
  16674. }
  16675. return SCRIPT_CMD_SUCCESS;
  16676. }
  16677. /**
  16678. * setquest <ID>{,<char_id>};
  16679. **/
  16680. BUILDIN_FUNC(setquest)
  16681. {
  16682. struct map_session_data *sd;
  16683. int quest_id;
  16684. quest_id = script_getnum(st, 2);
  16685. if (!script_charid2sd(3,sd))
  16686. return SCRIPT_CMD_FAILURE;
  16687. if( quest_add(sd, quest_id) == -1 ){
  16688. script_reportsrc(st);
  16689. script_reportfunc(st);
  16690. }
  16691. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  16692. pc_show_questinfo(sd);
  16693. return SCRIPT_CMD_SUCCESS;
  16694. }
  16695. /**
  16696. * erasequest <ID>{,<char_id>};
  16697. **/
  16698. BUILDIN_FUNC(erasequest)
  16699. {
  16700. struct map_session_data *sd;
  16701. if (!script_charid2sd(3,sd))
  16702. return SCRIPT_CMD_FAILURE;
  16703. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  16704. script_reportsrc(st);
  16705. script_reportfunc(st);
  16706. }
  16707. return SCRIPT_CMD_SUCCESS;
  16708. }
  16709. /**
  16710. * completequest <ID>{,<char_id>};
  16711. **/
  16712. BUILDIN_FUNC(completequest)
  16713. {
  16714. struct map_session_data *sd;
  16715. if (!script_charid2sd(3,sd))
  16716. return SCRIPT_CMD_FAILURE;
  16717. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  16718. //20120410 or 20090218
  16719. pc_show_questinfo(sd);
  16720. return SCRIPT_CMD_SUCCESS;
  16721. }
  16722. /**
  16723. * changequest <ID>,<ID2>{,<char_id>};
  16724. **/
  16725. BUILDIN_FUNC(changequest)
  16726. {
  16727. struct map_session_data *sd;
  16728. if (!script_charid2sd(4,sd))
  16729. return SCRIPT_CMD_FAILURE;
  16730. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  16731. script_reportsrc(st);
  16732. script_reportfunc(st);
  16733. }
  16734. //20120410 or 20090218
  16735. pc_show_questinfo(sd);
  16736. return SCRIPT_CMD_SUCCESS;
  16737. }
  16738. /**
  16739. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  16740. **/
  16741. BUILDIN_FUNC(checkquest)
  16742. {
  16743. struct map_session_data *sd;
  16744. enum quest_check_type type = HAVEQUEST;
  16745. if( script_hasdata(st, 3) )
  16746. type = (enum quest_check_type)script_getnum(st, 3);
  16747. if (!script_charid2sd(4,sd))
  16748. return SCRIPT_CMD_FAILURE;
  16749. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  16750. return SCRIPT_CMD_SUCCESS;
  16751. }
  16752. /**
  16753. * isbegin_quest(<ID>{,<char_id>})
  16754. **/
  16755. BUILDIN_FUNC(isbegin_quest)
  16756. {
  16757. struct map_session_data *sd;
  16758. int i;
  16759. if (!script_charid2sd(3,sd))
  16760. return SCRIPT_CMD_FAILURE;
  16761. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  16762. script_pushint(st, i + (i < 1));
  16763. return SCRIPT_CMD_SUCCESS;
  16764. }
  16765. /**
  16766. * showevent <icon>{,<mark color>{,<char_id>}}
  16767. **/
  16768. BUILDIN_FUNC(showevent)
  16769. {
  16770. TBL_PC *sd;
  16771. struct npc_data *nd = map_id2nd(st->oid);
  16772. int icon, color = 0;
  16773. if (!script_charid2sd(4,sd))
  16774. return SCRIPT_CMD_FAILURE;
  16775. if( sd == NULL || nd == NULL )
  16776. return SCRIPT_CMD_SUCCESS;
  16777. icon = script_getnum(st, 2);
  16778. if( script_hasdata(st, 3) ) {
  16779. color = script_getnum(st, 3);
  16780. if( color < 0 || color > 3 ) {
  16781. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  16782. script_reportfunc(st);
  16783. color = 0;
  16784. }
  16785. }
  16786. #if PACKETVER >= 20120410
  16787. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  16788. icon = 9999; // Default to nothing if icon id is invalid.
  16789. #else
  16790. if(icon < 0 || icon > 7)
  16791. icon = 0;
  16792. else
  16793. icon = icon + 1;
  16794. #endif
  16795. clif_quest_show_event(sd, &nd->bl, icon, color);
  16796. return SCRIPT_CMD_SUCCESS;
  16797. }
  16798. /*==========================================
  16799. * BattleGround System
  16800. *------------------------------------------*/
  16801. BUILDIN_FUNC(waitingroom2bg)
  16802. {
  16803. struct npc_data *nd;
  16804. struct chat_data *cd;
  16805. const char *map_name, *ev = "", *dev = "";
  16806. int x, y, mapindex = 0, bg_id;
  16807. unsigned char i,c=0;
  16808. if( script_hasdata(st,7) )
  16809. nd = npc_name2id(script_getstr(st,7));
  16810. else
  16811. nd = (struct npc_data *)map_id2bl(st->oid);
  16812. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  16813. {
  16814. script_pushint(st,0);
  16815. return SCRIPT_CMD_SUCCESS;
  16816. }
  16817. map_name = script_getstr(st,2);
  16818. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16819. { // Invalid Map
  16820. script_pushint(st, 0);
  16821. return SCRIPT_CMD_SUCCESS;
  16822. }
  16823. x = script_getnum(st,3);
  16824. y = script_getnum(st,4);
  16825. if(script_hasdata(st,5))
  16826. ev = script_getstr(st,5); // Logout Event
  16827. if(script_hasdata(st,6))
  16828. dev = script_getstr(st,6); // Die Event
  16829. check_event(st, ev);
  16830. check_event(st, dev);
  16831. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  16832. { // Creation failed
  16833. script_pushint(st,0);
  16834. return SCRIPT_CMD_SUCCESS;
  16835. }
  16836. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  16837. struct map_session_data *sd;
  16838. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ){
  16839. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  16840. ++c;
  16841. }
  16842. }
  16843. mapreg_setreg(add_str("$@arenamembersnum"), c);
  16844. script_pushint(st,bg_id);
  16845. return SCRIPT_CMD_SUCCESS;
  16846. }
  16847. BUILDIN_FUNC(waitingroom2bg_single)
  16848. {
  16849. const char* map_name;
  16850. struct npc_data *nd;
  16851. struct chat_data *cd;
  16852. struct map_session_data *sd;
  16853. struct battleground_data *bg;
  16854. int x, y, mapindex, bg_id;
  16855. bg_id = script_getnum(st,2);
  16856. if ((bg = bg_team_search(bg_id)) == NULL) {
  16857. script_pushint(st, false);
  16858. return SCRIPT_CMD_SUCCESS;
  16859. }
  16860. if (script_hasdata(st, 3)) {
  16861. map_name = script_getstr(st, 3);
  16862. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16863. script_pushint(st, false);
  16864. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16865. }
  16866. x = script_getnum(st, 4);
  16867. y = script_getnum(st, 5);
  16868. }
  16869. else {
  16870. mapindex = bg->mapindex;
  16871. x = bg->x;
  16872. y = bg->y;
  16873. }
  16874. nd = npc_name2id(script_getstr(st,6));
  16875. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  16876. return SCRIPT_CMD_SUCCESS;
  16877. if( (sd = cd->usersd[0]) == NULL )
  16878. return SCRIPT_CMD_SUCCESS;
  16879. if( bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16880. {
  16881. script_pushint(st, true);
  16882. }
  16883. else
  16884. script_pushint(st, false);
  16885. return SCRIPT_CMD_SUCCESS;
  16886. }
  16887. /// Creates an instance of battleground battle group.
  16888. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  16889. /// @author [secretdataz]
  16890. BUILDIN_FUNC(bg_create) {
  16891. const char *map_name, *ev = "", *dev = "";
  16892. int x, y, mapindex = 0;
  16893. map_name = script_getstr(st, 2);
  16894. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16895. { // Invalid Map
  16896. script_pushint(st, 0);
  16897. return SCRIPT_CMD_SUCCESS;
  16898. }
  16899. x = script_getnum(st, 3);
  16900. y = script_getnum(st, 4);
  16901. if(script_hasdata(st, 5))
  16902. ev = script_getstr(st, 5); // Logout Event
  16903. if(script_hasdata(st, 6))
  16904. dev = script_getstr(st, 6); // Die Event
  16905. check_event(st, ev);
  16906. check_event(st, dev);
  16907. script_pushint(st, bg_create(mapindex, x, y, ev, dev));
  16908. return SCRIPT_CMD_SUCCESS;
  16909. }
  16910. /// Adds attached player or <char id> (if specified) to an existing
  16911. /// battleground group and warps it to the specified coordinates on
  16912. /// the given map.
  16913. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  16914. /// @author [secretdataz]
  16915. BUILDIN_FUNC(bg_join) {
  16916. const char* map_name;
  16917. struct map_session_data *sd;
  16918. struct battleground_data *bg;
  16919. int x, y, bg_id, mapindex;
  16920. bg_id = script_getnum(st, 2);
  16921. if ((bg = bg_team_search(bg_id)) == NULL) {
  16922. script_pushint(st, false);
  16923. return SCRIPT_CMD_SUCCESS;
  16924. }
  16925. if (script_hasdata(st, 3)) {
  16926. map_name = script_getstr(st, 3);
  16927. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16928. script_pushint(st, false);
  16929. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16930. }
  16931. x = script_getnum(st, 4);
  16932. y = script_getnum(st, 5);
  16933. } else {
  16934. mapindex = bg->mapindex;
  16935. x = bg->x;
  16936. y = bg->y;
  16937. }
  16938. if (!script_charid2sd(6, sd)) {
  16939. script_pushint(st, false);
  16940. return SCRIPT_CMD_FAILURE;
  16941. }
  16942. if (bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16943. {
  16944. script_pushint(st, true);
  16945. }
  16946. else
  16947. script_pushint(st, false);
  16948. return SCRIPT_CMD_SUCCESS;
  16949. }
  16950. BUILDIN_FUNC(bg_team_setxy)
  16951. {
  16952. struct battleground_data *bg;
  16953. int bg_id;
  16954. bg_id = script_getnum(st,2);
  16955. if( (bg = bg_team_search(bg_id)) == NULL )
  16956. return SCRIPT_CMD_SUCCESS;
  16957. bg->x = script_getnum(st,3);
  16958. bg->y = script_getnum(st,4);
  16959. return SCRIPT_CMD_SUCCESS;
  16960. }
  16961. BUILDIN_FUNC(bg_warp)
  16962. {
  16963. int x, y, mapindex, bg_id;
  16964. const char* map_name;
  16965. bg_id = script_getnum(st,2);
  16966. map_name = script_getstr(st,3);
  16967. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16968. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16969. x = script_getnum(st,4);
  16970. y = script_getnum(st,5);
  16971. bg_team_warp(bg_id, mapindex, x, y);
  16972. return SCRIPT_CMD_SUCCESS;
  16973. }
  16974. BUILDIN_FUNC(bg_monster)
  16975. {
  16976. int class_ = 0, x = 0, y = 0, bg_id = 0;
  16977. const char *str,*mapname, *evt="";
  16978. bg_id = script_getnum(st,2);
  16979. mapname = script_getstr(st,3);
  16980. x = script_getnum(st,4);
  16981. y = script_getnum(st,5);
  16982. str = script_getstr(st,6);
  16983. class_ = script_getnum(st,7);
  16984. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  16985. check_event(st, evt);
  16986. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  16987. return SCRIPT_CMD_SUCCESS;
  16988. }
  16989. BUILDIN_FUNC(bg_monster_set_team)
  16990. {
  16991. struct mob_data *md;
  16992. struct block_list *mbl;
  16993. int id = script_getnum(st,2),
  16994. bg_id = script_getnum(st,3);
  16995. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  16996. return SCRIPT_CMD_SUCCESS;
  16997. md = (TBL_MOB *)mbl;
  16998. md->bg_id = bg_id;
  16999. mob_stop_attack(md);
  17000. mob_stop_walking(md, 0);
  17001. md->target_id = md->attacked_id = 0;
  17002. clif_name_area(&md->bl);
  17003. return SCRIPT_CMD_SUCCESS;
  17004. }
  17005. BUILDIN_FUNC(bg_leave)
  17006. {
  17007. struct map_session_data *sd = NULL;
  17008. if( !script_charid2sd(2,sd) || !sd->bg_id )
  17009. return SCRIPT_CMD_SUCCESS;
  17010. bg_team_leave(sd,0);
  17011. return SCRIPT_CMD_SUCCESS;
  17012. }
  17013. BUILDIN_FUNC(bg_destroy)
  17014. {
  17015. int bg_id = script_getnum(st,2);
  17016. bg_team_delete(bg_id);
  17017. return SCRIPT_CMD_SUCCESS;
  17018. }
  17019. BUILDIN_FUNC(bg_getareausers)
  17020. {
  17021. const char *str;
  17022. int16 m, x0, y0, x1, y1;
  17023. int bg_id;
  17024. int i = 0, c = 0;
  17025. struct battleground_data *bg = NULL;
  17026. bg_id = script_getnum(st,2);
  17027. str = script_getstr(st,3);
  17028. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  17029. {
  17030. script_pushint(st,0);
  17031. return SCRIPT_CMD_SUCCESS;
  17032. }
  17033. x0 = script_getnum(st,4);
  17034. y0 = script_getnum(st,5);
  17035. x1 = script_getnum(st,6);
  17036. y1 = script_getnum(st,7);
  17037. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  17038. {
  17039. struct map_session_data *sd;
  17040. if( (sd = bg->members[i].sd) == NULL )
  17041. continue;
  17042. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  17043. continue;
  17044. c++;
  17045. }
  17046. script_pushint(st,c);
  17047. return SCRIPT_CMD_SUCCESS;
  17048. }
  17049. BUILDIN_FUNC(bg_updatescore)
  17050. {
  17051. const char *str;
  17052. int16 m;
  17053. str = script_getstr(st,2);
  17054. if( (m = map_mapname2mapid(str)) < 0 )
  17055. return SCRIPT_CMD_SUCCESS;
  17056. map[m].bgscore_lion = script_getnum(st,3);
  17057. map[m].bgscore_eagle = script_getnum(st,4);
  17058. clif_bg_updatescore(m);
  17059. return SCRIPT_CMD_SUCCESS;
  17060. }
  17061. BUILDIN_FUNC(bg_get_data)
  17062. {
  17063. struct battleground_data *bg;
  17064. int bg_id = script_getnum(st,2),
  17065. type = script_getnum(st,3), i;
  17066. if( (bg = bg_team_search(bg_id)) == NULL )
  17067. {
  17068. script_pushint(st,0);
  17069. return SCRIPT_CMD_SUCCESS;
  17070. }
  17071. switch( type )
  17072. {
  17073. case 0: script_pushint(st, bg->count); break;
  17074. case 1:
  17075. for (i = 0; bg->members[i].sd != NULL; i++)
  17076. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), bg->members[i].sd->bl.id);
  17077. mapreg_setreg(add_str("$@arenamemberscount"), i);
  17078. script_pushint(st, i);
  17079. break;
  17080. default:
  17081. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  17082. break;
  17083. }
  17084. return SCRIPT_CMD_SUCCESS;
  17085. }
  17086. /*==========================================
  17087. * Instancing System
  17088. *------------------------------------------*/
  17089. //Returns an Instance ID
  17090. //Checks NPC first, then if player is attached we check
  17091. unsigned short script_instancegetid(struct script_state* st)
  17092. {
  17093. unsigned short instance_id = 0;
  17094. struct npc_data *nd;
  17095. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  17096. instance_id = nd->instance_id;
  17097. else {
  17098. struct map_session_data *sd = NULL;
  17099. struct party_data *pd = NULL;
  17100. struct guild *gd = NULL;
  17101. struct clan *cd = NULL;
  17102. if (script_rid2sd(sd)) {
  17103. if (sd->instance_id)
  17104. instance_id = sd->instance_id;
  17105. if (instance_id == 0 && sd->status.party_id && (pd = party_search(sd->status.party_id)) != NULL && pd->instance_id)
  17106. instance_id = pd->instance_id;
  17107. if (instance_id == 0 && sd->status.guild_id && (gd = guild_search(sd->status.guild_id)) != NULL && gd->instance_id)
  17108. instance_id = gd->instance_id;
  17109. if (instance_id == 0 && sd->status.clan_id && (cd = clan_search(sd->status.clan_id)) != NULL && cd->instance_id)
  17110. instance_id = cd->instance_id;
  17111. }
  17112. }
  17113. return instance_id;
  17114. }
  17115. /*==========================================
  17116. * Creates the instance
  17117. * Returns Instance ID if created successfully
  17118. *------------------------------------------*/
  17119. BUILDIN_FUNC(instance_create)
  17120. {
  17121. enum instance_mode mode = IM_PARTY;
  17122. int owner_id = 0;
  17123. if (script_hasdata(st, 3)) {
  17124. mode = script_getnum(st, 3);
  17125. if (mode < IM_NONE || mode >= IM_MAX) {
  17126. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  17127. return SCRIPT_CMD_FAILURE;
  17128. }
  17129. }
  17130. if (script_hasdata(st, 4))
  17131. owner_id = script_getnum(st, 4);
  17132. else {
  17133. // If sd is NULL, instance_create will return -2.
  17134. struct map_session_data *sd = NULL;
  17135. switch(mode) {
  17136. case IM_NONE:
  17137. owner_id = st->oid;
  17138. break;
  17139. case IM_CHAR:
  17140. if (script_rid2sd(sd))
  17141. owner_id = sd->status.char_id;
  17142. break;
  17143. case IM_PARTY:
  17144. if (script_rid2sd(sd))
  17145. owner_id = sd->status.party_id;
  17146. break;
  17147. case IM_GUILD:
  17148. if (script_rid2sd(sd))
  17149. owner_id = sd->status.guild_id;
  17150. break;
  17151. case IM_CLAN:
  17152. if (script_rid2sd(sd))
  17153. owner_id = sd->status.clan_id;
  17154. break;
  17155. default:
  17156. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  17157. return SCRIPT_CMD_FAILURE;
  17158. }
  17159. }
  17160. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  17161. return SCRIPT_CMD_SUCCESS;
  17162. }
  17163. /*==========================================
  17164. * Destroys an instance (unofficial)
  17165. * Officially instances are only destroyed by timeout
  17166. *
  17167. * instance_destroy {<instance_id>};
  17168. *------------------------------------------*/
  17169. BUILDIN_FUNC(instance_destroy)
  17170. {
  17171. unsigned short instance_id;
  17172. if( script_hasdata(st,2) )
  17173. instance_id = script_getnum(st,2);
  17174. else
  17175. instance_id = script_instancegetid(st);
  17176. if( instance_id == 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  17177. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %hu.\n", instance_id);
  17178. return SCRIPT_CMD_FAILURE;
  17179. }
  17180. instance_destroy(instance_id);
  17181. return SCRIPT_CMD_SUCCESS;
  17182. }
  17183. /*==========================================
  17184. * Warps player to instance
  17185. * Results:
  17186. * IE_OK: Success
  17187. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  17188. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  17189. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  17190. *------------------------------------------*/
  17191. BUILDIN_FUNC(instance_enter)
  17192. {
  17193. struct map_session_data *sd = NULL;
  17194. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  17195. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  17196. unsigned short instance_id;
  17197. if (script_hasdata(st, 6))
  17198. instance_id = script_getnum(st, 6);
  17199. else
  17200. instance_id = script_instancegetid(st);
  17201. if (!script_charid2sd(5,sd))
  17202. return SCRIPT_CMD_FAILURE;
  17203. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  17204. return SCRIPT_CMD_SUCCESS;
  17205. }
  17206. /*==========================================
  17207. * Returns the name of a duplicated NPC
  17208. *
  17209. * instance_npcname <npc_name>{,<instance_id>};
  17210. * <npc_name> is the full name of an NPC.
  17211. *------------------------------------------*/
  17212. BUILDIN_FUNC(instance_npcname)
  17213. {
  17214. const char *str;
  17215. unsigned short instance_id = 0;
  17216. struct npc_data *nd;
  17217. str = script_getstr(st,2);
  17218. if( script_hasdata(st,3) )
  17219. instance_id = script_getnum(st,3);
  17220. else
  17221. instance_id = script_instancegetid(st);
  17222. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  17223. static char npcname[NAME_LENGTH];
  17224. snprintf(npcname, sizeof(npcname), "dup_%hu_%d", instance_id, nd->bl.id);
  17225. script_pushconststr(st,npcname);
  17226. } else {
  17227. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %hu, NPC name: \"%s\".)\n", instance_id, str);
  17228. st->state = END;
  17229. return SCRIPT_CMD_FAILURE;
  17230. }
  17231. return SCRIPT_CMD_SUCCESS;
  17232. }
  17233. /*==========================================
  17234. * Returns the name of a duplicated map
  17235. *
  17236. * instance_mapname <map_name>{,<instance_id>};
  17237. *------------------------------------------*/
  17238. BUILDIN_FUNC(instance_mapname)
  17239. {
  17240. const char *str;
  17241. int16 m;
  17242. unsigned short instance_id = 0;
  17243. str = script_getstr(st,2);
  17244. if( script_hasdata(st,3) )
  17245. instance_id = script_getnum(st,3);
  17246. else
  17247. instance_id = script_instancegetid(st);
  17248. // Check that instance mapname is a valid map
  17249. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  17250. script_pushconststr(st, "");
  17251. else
  17252. script_pushconststr(st, map[m].name);
  17253. return SCRIPT_CMD_SUCCESS;
  17254. }
  17255. /*==========================================
  17256. * Returns an Instance ID
  17257. *------------------------------------------*/
  17258. BUILDIN_FUNC(instance_id)
  17259. {
  17260. script_pushint(st, script_instancegetid(st));
  17261. return SCRIPT_CMD_SUCCESS;
  17262. }
  17263. /*==========================================
  17264. * Warps all players inside an instance
  17265. *
  17266. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  17267. *------------------------------------------*/
  17268. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  17269. {
  17270. unsigned int m = va_arg(ap,unsigned int);
  17271. int x = va_arg(ap,int);
  17272. int y = va_arg(ap,int);
  17273. unsigned short instance_id = va_arg(ap,unsigned int);
  17274. struct map_session_data *sd = NULL;
  17275. int owner_id = 0;
  17276. nullpo_retr(0, bl);
  17277. if (bl->type != BL_PC)
  17278. return 0;
  17279. sd = (TBL_PC *)bl;
  17280. owner_id = instance_data[instance_id].owner_id;
  17281. switch(instance_data[instance_id].mode) {
  17282. case IM_NONE:
  17283. break;
  17284. case IM_CHAR:
  17285. if (sd->status.char_id != owner_id)
  17286. return 0;
  17287. break;
  17288. case IM_PARTY:
  17289. if (sd->status.party_id != owner_id)
  17290. return 0;
  17291. break;
  17292. case IM_GUILD:
  17293. if (sd->status.guild_id != owner_id)
  17294. return 0;
  17295. case IM_CLAN:
  17296. if (sd->status.clan_id != owner_id)
  17297. return 0;
  17298. }
  17299. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  17300. return 1;
  17301. }
  17302. BUILDIN_FUNC(instance_warpall)
  17303. {
  17304. int16 m, i;
  17305. unsigned short instance_id;
  17306. const char *mapn;
  17307. int x, y;
  17308. mapn = script_getstr(st,2);
  17309. x = script_getnum(st,3);
  17310. y = script_getnum(st,4);
  17311. if( script_hasdata(st,5) )
  17312. instance_id = script_getnum(st,5);
  17313. else
  17314. instance_id = script_instancegetid(st);
  17315. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  17316. return SCRIPT_CMD_FAILURE;
  17317. for(i = 0; i < instance_data[instance_id].cnt_map; i++)
  17318. map_foreachinmap(buildin_instance_warpall_sub, instance_data[instance_id].map[i]->m, BL_PC, map_id2index(m), x, y, instance_id);
  17319. return SCRIPT_CMD_SUCCESS;
  17320. }
  17321. /*==========================================
  17322. * Broadcasts to all maps inside an instance
  17323. *
  17324. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  17325. * Using 0 for <instance id> will auto-detect the id.
  17326. *------------------------------------------*/
  17327. BUILDIN_FUNC(instance_announce) {
  17328. unsigned short instance_id = script_getnum(st,2);
  17329. const char *mes = script_getstr(st,3);
  17330. int flag = script_getnum(st,4);
  17331. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  17332. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  17333. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  17334. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  17335. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  17336. int i;
  17337. if( instance_id == 0 ) {
  17338. instance_id = script_instancegetid(st);
  17339. }
  17340. if( !instance_id && &instance_data[instance_id] != NULL) {
  17341. ShowError("buildin_instance_announce: Intance is not found.\n");
  17342. return SCRIPT_CMD_FAILURE;
  17343. }
  17344. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  17345. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i]->m, BL_PC,
  17346. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  17347. return SCRIPT_CMD_SUCCESS;
  17348. }
  17349. /*==========================================
  17350. * instance_check_party [malufett]
  17351. * Values:
  17352. * party_id : Party ID of the invoking character. [Required Parameter]
  17353. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  17354. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17355. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17356. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  17357. * Example 2: instance_check_party (getcharid(1),1,1,99);
  17358. *------------------------------------------*/
  17359. BUILDIN_FUNC(instance_check_party)
  17360. {
  17361. int amount, min, max, i, party_id, c = 0;
  17362. struct party_data *p = NULL;
  17363. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  17364. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17365. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17366. if( min < 1 || min > MAX_LEVEL) {
  17367. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  17368. return SCRIPT_CMD_FAILURE;
  17369. } else if( max < 1 || max > MAX_LEVEL) {
  17370. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  17371. return SCRIPT_CMD_FAILURE;
  17372. }
  17373. if( script_hasdata(st,2) )
  17374. party_id = script_getnum(st,2);
  17375. else return SCRIPT_CMD_FAILURE;
  17376. if( !(p = party_search(party_id)) ) {
  17377. script_pushint(st, 0); // Returns false if party does not exist.
  17378. return SCRIPT_CMD_FAILURE;
  17379. }
  17380. for( i = 0; i < MAX_PARTY; i++ ) {
  17381. struct map_session_data *pl_sd;
  17382. if( (pl_sd = p->data[i].sd) )
  17383. if(map_id2bl(pl_sd->bl.id)) {
  17384. if(pl_sd->status.base_level < min) {
  17385. script_pushint(st, 0);
  17386. return SCRIPT_CMD_SUCCESS;
  17387. } else if(pl_sd->status.base_level > max) {
  17388. script_pushint(st, 0);
  17389. return SCRIPT_CMD_SUCCESS;
  17390. }
  17391. c++;
  17392. }
  17393. }
  17394. if(c < amount)
  17395. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  17396. else
  17397. script_pushint(st, 1);
  17398. return SCRIPT_CMD_SUCCESS;
  17399. }
  17400. /*==========================================
  17401. * instance_check_guild
  17402. * Values:
  17403. * guild_id : Guild ID of the invoking character. [Required Parameter]
  17404. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  17405. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17406. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17407. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  17408. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  17409. *------------------------------------------*/
  17410. BUILDIN_FUNC(instance_check_guild)
  17411. {
  17412. int amount, min, max, i, guild_id = 0, c = 0;
  17413. struct guild *g = NULL;
  17414. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  17415. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17416. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17417. if (min < 1 || min > MAX_LEVEL) {
  17418. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  17419. return SCRIPT_CMD_FAILURE;
  17420. } else if (max < 1 || max > MAX_LEVEL) {
  17421. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  17422. return SCRIPT_CMD_FAILURE;
  17423. }
  17424. if (script_hasdata(st,2))
  17425. guild_id = script_getnum(st,2);
  17426. else
  17427. return SCRIPT_CMD_FAILURE;
  17428. if (!(g = guild_search(guild_id))) {
  17429. script_pushint(st, 0); // Returns false if guild does not exist.
  17430. return SCRIPT_CMD_FAILURE;
  17431. }
  17432. for(i = 0; i < MAX_GUILD; i++) {
  17433. struct map_session_data *pl_sd;
  17434. if ((pl_sd = g->member[i].sd)) {
  17435. if (map_id2bl(pl_sd->bl.id)) {
  17436. if (pl_sd->status.base_level < min) {
  17437. script_pushint(st, 0);
  17438. return SCRIPT_CMD_SUCCESS;
  17439. } else if (pl_sd->status.base_level > max) {
  17440. script_pushint(st, 0);
  17441. return SCRIPT_CMD_SUCCESS;
  17442. }
  17443. c++;
  17444. }
  17445. }
  17446. }
  17447. if (c < amount)
  17448. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  17449. else
  17450. script_pushint(st, 1);
  17451. return SCRIPT_CMD_SUCCESS;
  17452. }
  17453. /*==========================================
  17454. * instance_check_clan
  17455. * Values:
  17456. * clan_id : Clan ID of the invoking character. [Required Parameter]
  17457. * amount : Amount of needed Clan members for the Instance. [Optional Parameter]
  17458. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  17459. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  17460. * Example: instance_check_clan (getcharid(5){,amount}{,min}{,max});
  17461. * Example 2: instance_check_clan (getcharid(5),1,1,99);
  17462. *------------------------------------------*/
  17463. BUILDIN_FUNC(instance_check_clan)
  17464. {
  17465. int amount, min, max, i, clan_id = 0, c = 0;
  17466. struct clan *cd = NULL;
  17467. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Clan members for the Instance.
  17468. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  17469. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  17470. if (min < 1 || min > MAX_LEVEL) {
  17471. ShowError("buildin_instance_check_clan: Invalid min level, %d\n", min);
  17472. return SCRIPT_CMD_FAILURE;
  17473. } else if (max < 1 || max > MAX_LEVEL) {
  17474. ShowError("buildin_instance_check_clan: Invalid max level, %d\n", max);
  17475. return SCRIPT_CMD_FAILURE;
  17476. }
  17477. if (script_hasdata(st,2))
  17478. clan_id = script_getnum(st,2);
  17479. else
  17480. return SCRIPT_CMD_FAILURE;
  17481. if (!(cd = clan_search(clan_id))) {
  17482. script_pushint(st, 0); // Returns false if clan does not exist.
  17483. return SCRIPT_CMD_FAILURE;
  17484. }
  17485. for(i = 0; i < MAX_CLAN; i++) {
  17486. struct map_session_data *pl_sd;
  17487. if ((pl_sd = cd->members[i])) {
  17488. if (map_id2bl(pl_sd->bl.id)) {
  17489. if (pl_sd->status.base_level < min) {
  17490. script_pushint(st, 0);
  17491. return SCRIPT_CMD_SUCCESS;
  17492. } else if (pl_sd->status.base_level > max) {
  17493. script_pushint(st, 0);
  17494. return SCRIPT_CMD_SUCCESS;
  17495. }
  17496. c++;
  17497. }
  17498. }
  17499. }
  17500. if (c < amount)
  17501. script_pushint(st, 0); // Not enough Members in the Clan to join Instance.
  17502. else
  17503. script_pushint(st, 1);
  17504. return SCRIPT_CMD_SUCCESS;
  17505. }
  17506. /*==========================================
  17507. * instance_info
  17508. * Values:
  17509. * name : name of the instance you want to look up. [Required Parameter]
  17510. * type : type of information you want to look up for the specified instance. [Required Parameter]
  17511. * index : index of the map in the instance. [Optional Parameter]
  17512. *------------------------------------------*/
  17513. BUILDIN_FUNC(instance_info)
  17514. {
  17515. const char* name = script_getstr(st, 2);
  17516. int type = script_getnum(st, 3);
  17517. int index = 0;
  17518. struct instance_db *db = instance_searchname_db(name);
  17519. if( !db ){
  17520. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  17521. script_pushint(st, -1);
  17522. return SCRIPT_CMD_FAILURE;
  17523. }
  17524. switch( type ){
  17525. case IIT_ID:
  17526. script_pushint(st, db->id);
  17527. break;
  17528. case IIT_TIME_LIMIT:
  17529. script_pushint(st, db->limit);
  17530. break;
  17531. case IIT_IDLE_TIMEOUT:
  17532. script_pushint(st, db->timeout);
  17533. break;
  17534. case IIT_ENTER_MAP:
  17535. script_pushstrcopy(st, StringBuf_Value(db->enter.mapname));
  17536. break;
  17537. case IIT_ENTER_X:
  17538. script_pushint(st, db->enter.x);
  17539. break;
  17540. case IIT_ENTER_Y:
  17541. script_pushint(st, db->enter.y);
  17542. break;
  17543. case IIT_MAPCOUNT:
  17544. script_pushint(st, db->maplist_count);
  17545. break;
  17546. case IIT_MAP:
  17547. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  17548. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  17549. script_pushstr(st, "");
  17550. return SCRIPT_CMD_FAILURE;
  17551. }
  17552. index = script_getnum(st, 4);
  17553. if( index < 0 ){
  17554. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  17555. script_pushstr(st, "");
  17556. return SCRIPT_CMD_FAILURE;
  17557. }
  17558. if( index > UINT8_MAX ){
  17559. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  17560. script_pushstr(st, "");
  17561. return SCRIPT_CMD_FAILURE;
  17562. }
  17563. script_pushstrcopy(st, StringBuf_Value(db->maplist[index]));
  17564. break;
  17565. default:
  17566. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  17567. script_pushint(st, -1);
  17568. return SCRIPT_CMD_FAILURE;
  17569. }
  17570. return SCRIPT_CMD_SUCCESS;
  17571. }
  17572. /*==========================================
  17573. * Custom Fonts
  17574. *------------------------------------------*/
  17575. BUILDIN_FUNC(setfont)
  17576. {
  17577. struct map_session_data *sd;
  17578. int font;
  17579. if( !script_rid2sd(sd) )
  17580. return SCRIPT_CMD_SUCCESS;
  17581. font = script_getnum(st,2);
  17582. if( sd->status.font != font )
  17583. sd->status.font = font;
  17584. else
  17585. sd->status.font = 0;
  17586. clif_font(sd);
  17587. return SCRIPT_CMD_SUCCESS;
  17588. }
  17589. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  17590. {
  17591. TBL_MOB* md = (TBL_MOB*)bl;
  17592. struct block_list *tbl;
  17593. int mobid = va_arg(ap,int);
  17594. uint16 skill_id = va_arg(ap,int);
  17595. uint16 skill_lv = va_arg(ap,int);
  17596. int casttime = va_arg(ap,int);
  17597. int cancel = va_arg(ap,int);
  17598. int emotion = va_arg(ap,int);
  17599. int target = va_arg(ap,int);
  17600. if( md->mob_id != mobid )
  17601. return 0;
  17602. // 0:self, 1:target, 2:master, default:random
  17603. switch( target ) {
  17604. case 0: tbl = map_id2bl(md->bl.id); break;
  17605. case 1: tbl = map_id2bl(md->target_id); break;
  17606. case 2: tbl = map_id2bl(md->master_id); break;
  17607. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  17608. }
  17609. if( !tbl )
  17610. return 0;
  17611. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  17612. unit_skillcastcancel(bl,0);
  17613. if( skill_get_casttype(skill_id) == CAST_GROUND )
  17614. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  17615. else
  17616. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  17617. clif_emotion(&md->bl, emotion);
  17618. return 1;
  17619. }
  17620. /*==========================================
  17621. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  17622. *------------------------------------------*/
  17623. BUILDIN_FUNC(areamobuseskill)
  17624. {
  17625. struct block_list center;
  17626. struct script_data *data;
  17627. int16 m;
  17628. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  17629. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  17630. ShowError("areamobuseskill: invalid map name.\n");
  17631. return SCRIPT_CMD_SUCCESS;
  17632. }
  17633. center.m = m;
  17634. center.x = script_getnum(st,3);
  17635. center.y = script_getnum(st,4);
  17636. range = script_getnum(st,5);
  17637. mobid = script_getnum(st,6);
  17638. data = script_getdata(st, 7);
  17639. get_val(st, data); // Convert into value in case of a variable
  17640. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  17641. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  17642. skill_lv = battle_config.mob_max_skilllvl;
  17643. casttime = script_getnum(st,9);
  17644. cancel = script_getnum(st,10);
  17645. emotion = script_getnum(st,11);
  17646. target = script_getnum(st,12);
  17647. map_foreachinallrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  17648. return SCRIPT_CMD_SUCCESS;
  17649. }
  17650. /**
  17651. * Display a progress bar above a character
  17652. * progressbar "<color>",<seconds>;
  17653. */
  17654. BUILDIN_FUNC(progressbar)
  17655. {
  17656. struct map_session_data * sd;
  17657. const char * color;
  17658. unsigned int second;
  17659. if( !script_rid2sd(sd) )
  17660. return SCRIPT_CMD_SUCCESS;
  17661. st->state = STOP;
  17662. color = script_getstr(st,2);
  17663. second = script_getnum(st,3);
  17664. sd->progressbar.npc_id = st->oid;
  17665. sd->progressbar.timeout = gettick() + second*1000;
  17666. sd->state.workinprogress = WIP_DISABLE_ALL;
  17667. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  17668. return SCRIPT_CMD_SUCCESS;
  17669. }
  17670. /**
  17671. * Display a progress bar above an NPC
  17672. * progressbar_npc "<color>",<seconds>{,<"NPC Name">};
  17673. */
  17674. BUILDIN_FUNC(progressbar_npc){
  17675. struct npc_data* nd = NULL;
  17676. if( script_hasdata(st, 4) ){
  17677. const char* name = script_getstr(st, 4);
  17678. nd = npc_name2id(name);
  17679. if( !nd ){
  17680. ShowError( "buildin_progressbar_npc: NPC \"%s\" was not found.\n", name );
  17681. return SCRIPT_CMD_FAILURE;
  17682. }
  17683. }else{
  17684. nd = map_id2nd(st->oid);
  17685. }
  17686. // First call(by function call)
  17687. if( !nd->progressbar.timeout ){
  17688. const char *color;
  17689. int second;
  17690. color = script_getstr(st, 2);
  17691. second = script_getnum(st, 3);
  17692. if( second < 0 ){
  17693. ShowError( "buildin_progressbar_npc: negative amount('%d') of seconds is not supported\n", second );
  17694. return SCRIPT_CMD_FAILURE;
  17695. }
  17696. // detach the player
  17697. script_detach_rid(st);
  17698. // sleep for the target amount of time
  17699. st->state = RERUNLINE;
  17700. st->sleep.tick = second * 1000;
  17701. nd->progressbar.timeout = gettick() + second * 1000;
  17702. nd->progressbar.color = strtol(color, (char **)NULL, 0);
  17703. clif_progressbar_npc_area(nd);
  17704. // Second call(by timer after sleeping time is over)
  17705. } else {
  17706. // Continue the script
  17707. st->state = RUN;
  17708. st->sleep.tick = 0;
  17709. nd->progressbar.timeout = nd->progressbar.color = 0;
  17710. }
  17711. return SCRIPT_CMD_SUCCESS;
  17712. }
  17713. BUILDIN_FUNC(pushpc)
  17714. {
  17715. uint8 dir;
  17716. int cells, dx, dy;
  17717. struct map_session_data* sd;
  17718. if(!script_rid2sd(sd))
  17719. {
  17720. return SCRIPT_CMD_SUCCESS;
  17721. }
  17722. dir = script_getnum(st,2);
  17723. cells = script_getnum(st,3);
  17724. if(dir >= DIR_MAX)
  17725. {
  17726. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  17727. script_reportsrc(st);
  17728. dir%= DIR_MAX; // trim spin-over
  17729. }
  17730. if(!cells)
  17731. {// zero distance
  17732. return SCRIPT_CMD_SUCCESS;
  17733. }
  17734. else if(cells<0)
  17735. {// pushing backwards
  17736. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  17737. cells = -cells;
  17738. }
  17739. dx = dirx[dir];
  17740. dy = diry[dir];
  17741. unit_blown(&sd->bl, dx, dy, cells, 0);
  17742. return SCRIPT_CMD_SUCCESS;
  17743. }
  17744. /// Invokes buying store preparation window
  17745. /// buyingstore <slots>;
  17746. BUILDIN_FUNC(buyingstore)
  17747. {
  17748. struct map_session_data* sd;
  17749. if( !script_rid2sd(sd) )
  17750. {
  17751. return SCRIPT_CMD_SUCCESS;
  17752. }
  17753. if( npc_isnear(&sd->bl) ) {
  17754. char output[150];
  17755. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  17756. clif_displaymessage(sd->fd, output);
  17757. return SCRIPT_CMD_SUCCESS;
  17758. }
  17759. buyingstore_setup(sd, script_getnum(st,2));
  17760. return SCRIPT_CMD_SUCCESS;
  17761. }
  17762. /// Invokes search store info window
  17763. /// searchstores <uses>,<effect>;
  17764. BUILDIN_FUNC(searchstores)
  17765. {
  17766. unsigned short effect;
  17767. unsigned int uses;
  17768. struct map_session_data* sd;
  17769. if( !script_rid2sd(sd) )
  17770. {
  17771. return SCRIPT_CMD_SUCCESS;
  17772. }
  17773. uses = script_getnum(st,2);
  17774. effect = script_getnum(st,3);
  17775. if( !uses )
  17776. {
  17777. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  17778. return SCRIPT_CMD_FAILURE;
  17779. }
  17780. if( effect > 1 )
  17781. {
  17782. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  17783. return SCRIPT_CMD_FAILURE;
  17784. }
  17785. searchstore_open(sd, uses, effect);
  17786. return SCRIPT_CMD_SUCCESS;
  17787. }
  17788. /// Displays a number as large digital clock.
  17789. /// showdigit <value>[,<type>];
  17790. BUILDIN_FUNC(showdigit)
  17791. {
  17792. unsigned int type = 0;
  17793. int value;
  17794. struct map_session_data* sd;
  17795. if( !script_rid2sd(sd) )
  17796. {
  17797. return SCRIPT_CMD_SUCCESS;
  17798. }
  17799. value = script_getnum(st,2);
  17800. if( script_hasdata(st,3) ){
  17801. type = script_getnum(st,3);
  17802. }
  17803. switch( type ){
  17804. case 0:
  17805. break;
  17806. case 1:
  17807. case 2:
  17808. // Use absolute value and then the negative value of it as starting value
  17809. // This is what gravity's client does for these counters
  17810. value = -abs(value);
  17811. break;
  17812. case 3:
  17813. value = abs(value);
  17814. if( value > 99 ){
  17815. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  17816. script_reportsrc(st);
  17817. value = 99;
  17818. }
  17819. break;
  17820. default:
  17821. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  17822. return SCRIPT_CMD_FAILURE;
  17823. }
  17824. clif_showdigit(sd, (unsigned char)type, value);
  17825. return SCRIPT_CMD_SUCCESS;
  17826. }
  17827. /**
  17828. * Rune Knight
  17829. * makerune <% success bonus>{,<char_id>};
  17830. **/
  17831. BUILDIN_FUNC(makerune) {
  17832. TBL_PC* sd;
  17833. if (!script_charid2sd(3,sd))
  17834. return SCRIPT_CMD_FAILURE;
  17835. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  17836. sd->itemid = script_getnum(st,2);
  17837. return SCRIPT_CMD_SUCCESS;
  17838. }
  17839. /**
  17840. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  17841. **/
  17842. BUILDIN_FUNC(checkdragon) {
  17843. TBL_PC* sd;
  17844. if (!script_charid2sd(2,sd))
  17845. return SCRIPT_CMD_FAILURE;
  17846. if( pc_isridingdragon(sd) )
  17847. script_pushint(st,1);
  17848. else
  17849. script_pushint(st,0);
  17850. return SCRIPT_CMD_SUCCESS;
  17851. }
  17852. /**
  17853. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  17854. * - Toggles the dragon on a RK if he can mount;
  17855. * @param Color - when not provided uses the green dragon;
  17856. * - 1 : Green Dragon
  17857. * - 2 : Brown Dragon
  17858. * - 3 : Gray Dragon
  17859. * - 4 : Blue Dragon
  17860. * - 5 : Red Dragon
  17861. **/
  17862. BUILDIN_FUNC(setdragon) {
  17863. TBL_PC* sd;
  17864. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  17865. if (!script_charid2sd(3,sd))
  17866. return SCRIPT_CMD_FAILURE;
  17867. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  17868. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  17869. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  17870. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  17871. script_pushint(st,1);
  17872. } else {//Not mounted; Mount now.
  17873. unsigned int option = OPTION_DRAGON1;
  17874. if( color ) {
  17875. option = ( color == 1 ? OPTION_DRAGON1 :
  17876. color == 2 ? OPTION_DRAGON2 :
  17877. color == 3 ? OPTION_DRAGON3 :
  17878. color == 4 ? OPTION_DRAGON4 :
  17879. color == 5 ? OPTION_DRAGON5 : 0);
  17880. if( !option ) {
  17881. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  17882. option = OPTION_DRAGON1;
  17883. }
  17884. }
  17885. pc_setoption(sd, sd->sc.option|option);
  17886. script_pushint(st,1);
  17887. }
  17888. return SCRIPT_CMD_SUCCESS;
  17889. }
  17890. /**
  17891. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  17892. **/
  17893. BUILDIN_FUNC(ismounting) {
  17894. TBL_PC* sd;
  17895. if (!script_charid2sd(2,sd))
  17896. return SCRIPT_CMD_FAILURE;
  17897. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17898. script_pushint(st,1);
  17899. else
  17900. script_pushint(st,0);
  17901. return SCRIPT_CMD_SUCCESS;
  17902. }
  17903. /**
  17904. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  17905. * - Toggles new mounts on a player when he can mount
  17906. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  17907. * - Will unmount the player is he is already mounting
  17908. **/
  17909. BUILDIN_FUNC(setmounting) {
  17910. TBL_PC* sd;
  17911. if (!script_charid2sd(2,sd))
  17912. return SCRIPT_CMD_FAILURE;
  17913. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  17914. clif_msg(sd, NEED_REINS_OF_MOUNT);
  17915. script_pushint(st,0); //can't mount with one of these
  17916. } else {
  17917. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17918. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  17919. else
  17920. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  17921. script_pushint(st,1);//in both cases, return 1.
  17922. }
  17923. return SCRIPT_CMD_SUCCESS;
  17924. }
  17925. /**
  17926. * Retrieves quantity of arguments provided to callfunc/callsub.
  17927. * getargcount() -> amount of arguments received in a function
  17928. **/
  17929. BUILDIN_FUNC(getargcount) {
  17930. struct script_retinfo* ri;
  17931. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  17932. ShowError("script:getargcount: used out of function or callsub label!\n");
  17933. st->state = END;
  17934. return SCRIPT_CMD_FAILURE;
  17935. }
  17936. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  17937. script_pushint(st, ri->nargs);
  17938. return SCRIPT_CMD_SUCCESS;
  17939. }
  17940. /**
  17941. * getcharip(<account ID>/<character ID>/<character name>)
  17942. **/
  17943. BUILDIN_FUNC(getcharip)
  17944. {
  17945. struct map_session_data* sd = NULL;
  17946. /* check if a character name is specified */
  17947. if( script_hasdata(st, 2) )
  17948. {
  17949. struct script_data *data;
  17950. data = script_getdata(st, 2);
  17951. get_val(st, data); // Convert into value in case of a variable
  17952. if (data_isstring(data))
  17953. sd = map_nick2sd(script_getstr(st, 2),false);
  17954. else if (data_isint(data) || script_getnum(st, 2))
  17955. {
  17956. int id = 0;
  17957. id = script_getnum(st, 2);
  17958. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  17959. }
  17960. }
  17961. else
  17962. script_rid2sd(sd);
  17963. /* check for sd and IP */
  17964. if (!sd || !session[sd->fd]->client_addr)
  17965. {
  17966. script_pushconststr(st, "");
  17967. return SCRIPT_CMD_SUCCESS;
  17968. }
  17969. /* return the client ip_addr converted for output */
  17970. if (sd && sd->fd && session[sd->fd])
  17971. {
  17972. /* initiliaze */
  17973. const char *ip_addr = NULL;
  17974. uint32 ip;
  17975. /* set ip, ip_addr and convert to ip and push str */
  17976. ip = session[sd->fd]->client_addr;
  17977. ip_addr = ip2str(ip, NULL);
  17978. script_pushstrcopy(st, ip_addr);
  17979. }
  17980. return SCRIPT_CMD_SUCCESS;
  17981. }
  17982. /**
  17983. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  17984. **/
  17985. BUILDIN_FUNC(is_function) {
  17986. const char* str = script_getstr(st,2);
  17987. if( strdb_exists(userfunc_db, str) )
  17988. script_pushint(st,1);
  17989. else
  17990. script_pushint(st,0);
  17991. return SCRIPT_CMD_SUCCESS;
  17992. }
  17993. /**
  17994. * get_revision() -> retrieves the current svn revision (if available)
  17995. **/
  17996. BUILDIN_FUNC(get_revision) {
  17997. const char *svn = get_svn_revision();
  17998. if ( svn[0] != UNKNOWN_VERSION )
  17999. script_pushint(st,atoi(svn));
  18000. else
  18001. script_pushint(st,-1); //unknown
  18002. return SCRIPT_CMD_SUCCESS;
  18003. }
  18004. /* get_hash() -> retrieves the current git hash (if available)*/
  18005. BUILDIN_FUNC(get_githash) {
  18006. const char* git = get_git_hash();
  18007. char buf[CHAT_SIZE_MAX];
  18008. safestrncpy(buf,git,strlen(git)+1);
  18009. if ( git[0] != UNKNOWN_VERSION )
  18010. script_pushstr(st,buf);
  18011. else
  18012. script_pushstr(st,"Unknown"); //unknown
  18013. return SCRIPT_CMD_SUCCESS;
  18014. }
  18015. /**
  18016. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  18017. **/
  18018. BUILDIN_FUNC(freeloop) {
  18019. if( script_hasdata(st,2) ) {
  18020. if( script_getnum(st,2) )
  18021. st->freeloop = 1;
  18022. else
  18023. st->freeloop = 0;
  18024. }
  18025. script_pushint(st, st->freeloop);
  18026. return SCRIPT_CMD_SUCCESS;
  18027. }
  18028. /**
  18029. * @commands (script based)
  18030. **/
  18031. BUILDIN_FUNC(bindatcmd) {
  18032. const char* atcmd;
  18033. const char* eventName;
  18034. int i, level = 0, level2 = 0;
  18035. bool create = false;
  18036. atcmd = script_getstr(st,2);
  18037. eventName = script_getstr(st,3);
  18038. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  18039. atcmd++;
  18040. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  18041. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  18042. if( atcmd_binding_count == 0 ) {
  18043. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  18044. create = true;
  18045. } else {
  18046. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  18047. if( i < atcmd_binding_count ) {/* update existent entry */
  18048. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  18049. atcmd_binding[i]->level = level;
  18050. atcmd_binding[i]->level2 = level2;
  18051. } else
  18052. create = true;
  18053. }
  18054. if( create ) {
  18055. i = atcmd_binding_count;
  18056. if( atcmd_binding_count++ != 0 )
  18057. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  18058. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  18059. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  18060. safestrncpy(atcmd_binding[i]->npc_event, eventName, EVENT_NAME_LENGTH);
  18061. atcmd_binding[i]->level = level;
  18062. atcmd_binding[i]->level2 = level2;
  18063. }
  18064. return SCRIPT_CMD_SUCCESS;
  18065. }
  18066. BUILDIN_FUNC(unbindatcmd) {
  18067. const char* atcmd;
  18068. int i = 0;
  18069. atcmd = script_getstr(st, 2);
  18070. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  18071. atcmd++;
  18072. if( atcmd_binding_count == 0 ) {
  18073. script_pushint(st, 0);
  18074. return SCRIPT_CMD_SUCCESS;
  18075. }
  18076. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  18077. if( i < atcmd_binding_count ) {
  18078. int cursor = 0;
  18079. aFree(atcmd_binding[i]);
  18080. atcmd_binding[i] = NULL;
  18081. /* compact the list now that we freed a slot somewhere */
  18082. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  18083. if( atcmd_binding[i] == NULL )
  18084. continue;
  18085. if( cursor != i ) {
  18086. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  18087. }
  18088. cursor++;
  18089. }
  18090. if( (atcmd_binding_count = cursor) == 0 )
  18091. aFree(atcmd_binding);
  18092. script_pushint(st, 1);
  18093. } else
  18094. script_pushint(st, 0);/* not found */
  18095. return SCRIPT_CMD_SUCCESS;
  18096. }
  18097. BUILDIN_FUNC(useatcmd) {
  18098. return atcommand_sub(st,3);
  18099. }
  18100. BUILDIN_FUNC(checkre)
  18101. {
  18102. int num;
  18103. num=script_getnum(st,2);
  18104. switch(num){
  18105. case 0:
  18106. #ifdef RENEWAL
  18107. script_pushint(st, 1);
  18108. #else
  18109. script_pushint(st, 0);
  18110. #endif
  18111. break;
  18112. case 1:
  18113. #ifdef RENEWAL_CAST
  18114. script_pushint(st, 1);
  18115. #else
  18116. script_pushint(st, 0);
  18117. #endif
  18118. break;
  18119. case 2:
  18120. #ifdef RENEWAL_DROP
  18121. script_pushint(st, 1);
  18122. #else
  18123. script_pushint(st, 0);
  18124. #endif
  18125. break;
  18126. case 3:
  18127. #ifdef RENEWAL_EXP
  18128. script_pushint(st, 1);
  18129. #else
  18130. script_pushint(st, 0);
  18131. #endif
  18132. break;
  18133. case 4:
  18134. #ifdef RENEWAL_LVDMG
  18135. script_pushint(st, 1);
  18136. #else
  18137. script_pushint(st, 0);
  18138. #endif
  18139. break;
  18140. case 5:
  18141. #ifdef RENEWAL_ASPD
  18142. script_pushint(st, 1);
  18143. #else
  18144. script_pushint(st, 0);
  18145. #endif
  18146. break;
  18147. default:
  18148. ShowWarning("buildin_checkre: unknown parameter.\n");
  18149. break;
  18150. }
  18151. return SCRIPT_CMD_SUCCESS;
  18152. }
  18153. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>}} */
  18154. BUILDIN_FUNC(getrandgroupitem) {
  18155. TBL_PC* sd;
  18156. int i, get_count = 0;
  18157. uint16 group, qty = 0;
  18158. uint8 sub_group = 1;
  18159. struct item item_tmp;
  18160. struct s_item_group_entry *entry = NULL;
  18161. if (!script_rid2sd(sd))
  18162. return SCRIPT_CMD_SUCCESS;
  18163. group = script_getnum(st,2);
  18164. if (!group) {
  18165. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  18166. return SCRIPT_CMD_FAILURE;
  18167. }
  18168. FETCH(3, qty);
  18169. FETCH(4, sub_group);
  18170. entry = itemdb_get_randgroupitem(group,sub_group);
  18171. if (!entry)
  18172. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  18173. memset(&item_tmp,0,sizeof(item_tmp));
  18174. item_tmp.nameid = entry->nameid;
  18175. item_tmp.identify = itemdb_isidentified(entry->nameid);
  18176. if (!qty)
  18177. qty = entry->amount;
  18178. //Check if it's stackable.
  18179. if (!itemdb_isstackable(entry->nameid)) {
  18180. item_tmp.amount = 1;
  18181. get_count = qty;
  18182. }
  18183. else {
  18184. item_tmp.amount = qty;
  18185. get_count = 1;
  18186. }
  18187. for (i = 0; i < get_count; i++) {
  18188. // if not pet egg
  18189. if (!pet_create_egg(sd, entry->nameid)) {
  18190. unsigned char flag = 0;
  18191. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  18192. clif_additem(sd,0,0,flag);
  18193. if (pc_candrop(sd,&item_tmp))
  18194. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  18195. }
  18196. }
  18197. }
  18198. return SCRIPT_CMD_SUCCESS;
  18199. }
  18200. /* getgroupitem <group_id>{,<char_id>};
  18201. * Gives item(s) to the attached player based on item group contents
  18202. */
  18203. BUILDIN_FUNC(getgroupitem) {
  18204. TBL_PC *sd;
  18205. int group_id = script_getnum(st,2);
  18206. if (!script_charid2sd(3,sd))
  18207. return SCRIPT_CMD_SUCCESS;
  18208. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  18209. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  18210. return SCRIPT_CMD_FAILURE;
  18211. }
  18212. return SCRIPT_CMD_SUCCESS;
  18213. }
  18214. /* cleanmap <map_name>;
  18215. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  18216. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  18217. {
  18218. nullpo_ret(bl);
  18219. map_clearflooritem(bl);
  18220. return SCRIPT_CMD_SUCCESS;
  18221. }
  18222. BUILDIN_FUNC(cleanmap)
  18223. {
  18224. const char *mapname;
  18225. int16 m;
  18226. mapname = script_getstr(st, 2);
  18227. m = map_mapname2mapid(mapname);
  18228. if (!m)
  18229. return SCRIPT_CMD_FAILURE;
  18230. if ((script_lastdata(st) - 2) < 4) {
  18231. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  18232. } else {
  18233. int16 x0 = script_getnum(st, 3);
  18234. int16 y0 = script_getnum(st, 4);
  18235. int16 x1 = script_getnum(st, 5);
  18236. int16 y1 = script_getnum(st, 6);
  18237. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  18238. map_foreachinallarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  18239. } else {
  18240. ShowError("cleanarea: invalid coordinate defined!\n");
  18241. return SCRIPT_CMD_FAILURE;
  18242. }
  18243. }
  18244. return SCRIPT_CMD_SUCCESS;
  18245. }
  18246. /* Cast a skill on the attached player.
  18247. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  18248. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  18249. BUILDIN_FUNC(npcskill)
  18250. {
  18251. uint16 skill_id;
  18252. unsigned short skill_level;
  18253. unsigned int stat_point;
  18254. unsigned int npc_level;
  18255. struct npc_data *nd;
  18256. struct map_session_data *sd;
  18257. struct script_data *data;
  18258. if( !script_rid2sd(sd) )
  18259. return SCRIPT_CMD_SUCCESS;
  18260. data = script_getdata(st, 2);
  18261. get_val(st, data); // Convert into value in case of a variable
  18262. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  18263. skill_level = script_getnum(st, 3);
  18264. stat_point = script_getnum(st, 4);
  18265. npc_level = script_getnum(st, 5);
  18266. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  18267. if (stat_point > battle_config.max_third_parameter) {
  18268. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  18269. return SCRIPT_CMD_FAILURE;
  18270. }
  18271. if (npc_level > MAX_LEVEL) {
  18272. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  18273. return SCRIPT_CMD_FAILURE;
  18274. }
  18275. if (nd == NULL) { //ain't possible, but I don't trust people.
  18276. return SCRIPT_CMD_FAILURE;
  18277. }
  18278. nd->level = npc_level;
  18279. nd->stat_point = stat_point;
  18280. if (!nd->status.hp)
  18281. status_calc_npc(nd, SCO_FIRST);
  18282. else
  18283. status_calc_npc(nd, SCO_NONE);
  18284. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  18285. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  18286. else
  18287. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  18288. return SCRIPT_CMD_SUCCESS;
  18289. }
  18290. /* Consumes an item.
  18291. * consumeitem <item id>{,<char_id>};
  18292. * consumeitem "<item name>"{,<char_id>};
  18293. * @param item: Item ID or name
  18294. */
  18295. BUILDIN_FUNC(consumeitem)
  18296. {
  18297. TBL_PC *sd;
  18298. struct script_data *data;
  18299. struct item_data *item_data;
  18300. if (!script_charid2sd(3, sd))
  18301. return SCRIPT_CMD_FAILURE;
  18302. data = script_getdata( st, 2 );
  18303. get_val( st, data );
  18304. if( data_isstring( data ) ){
  18305. const char *name = conv_str( st, data );
  18306. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  18307. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  18308. return SCRIPT_CMD_FAILURE;
  18309. }
  18310. }else if( data_isint( data ) ){
  18311. unsigned short nameid = conv_num( st, data );
  18312. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  18313. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  18314. return SCRIPT_CMD_FAILURE;
  18315. }
  18316. }else{
  18317. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  18318. return SCRIPT_CMD_FAILURE;
  18319. }
  18320. run_script( item_data->script, 0, sd->bl.id, 0 );
  18321. return SCRIPT_CMD_SUCCESS;
  18322. }
  18323. /** Makes a player sit/stand.
  18324. * sit {"<character name>"};
  18325. * stand {"<character name>"};
  18326. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  18327. * @param name: Player name that will be invoked
  18328. */
  18329. BUILDIN_FUNC(sit)
  18330. {
  18331. TBL_PC *sd;
  18332. if( !script_nick2sd(2,sd) )
  18333. return SCRIPT_CMD_FAILURE;
  18334. if( !pc_issit(sd) ) {
  18335. pc_setsit(sd);
  18336. skill_sit(sd, 1);
  18337. clif_sitting(&sd->bl);
  18338. }
  18339. return SCRIPT_CMD_SUCCESS;
  18340. }
  18341. /** Makes player to stand
  18342. * @param name: Player name that will be set
  18343. */
  18344. BUILDIN_FUNC(stand)
  18345. {
  18346. TBL_PC *sd;
  18347. if( !script_nick2sd(2,sd) )
  18348. return SCRIPT_CMD_FAILURE;
  18349. if( pc_issit(sd) && pc_setstand(sd, false)) {
  18350. skill_sit(sd, 0);
  18351. clif_standing(&sd->bl);
  18352. }
  18353. return SCRIPT_CMD_SUCCESS;
  18354. }
  18355. /** Creates an array of bounded item IDs
  18356. * countbound {<type>{,<char_id>}};
  18357. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  18358. * @return amt: Amount of items found
  18359. */
  18360. BUILDIN_FUNC(countbound)
  18361. {
  18362. int i, type, j = 0, k = 0;
  18363. TBL_PC *sd;
  18364. if (!script_charid2sd(3,sd))
  18365. return SCRIPT_CMD_FAILURE;
  18366. type = script_getnum(st,2);
  18367. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  18368. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  18369. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  18370. ))
  18371. {
  18372. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  18373. k++;
  18374. j += sd->inventory.u.items_inventory[i].amount;
  18375. }
  18376. }
  18377. script_pushint(st,j);
  18378. return SCRIPT_CMD_SUCCESS;
  18379. }
  18380. /** Creates new party
  18381. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  18382. * @param party_name: String of party name that will be created
  18383. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  18384. * @param item_share: 0-Each Take. 1-Party Share
  18385. * @param item_share_type: 0-Each Take. 1-Even Share
  18386. * @return val: Result value
  18387. * -3 - party name is exist
  18388. * -2 - player is in party already
  18389. * -1 - player is not found
  18390. * 0 - unknown error
  18391. * 1 - success, will return party id $@party_create_id
  18392. */
  18393. BUILDIN_FUNC(party_create)
  18394. {
  18395. char party_name[NAME_LENGTH];
  18396. int item1 = 0, item2 = 0;
  18397. TBL_PC *sd = NULL;
  18398. if (!script_charid2sd(3, sd)) {
  18399. script_pushint(st,-1);
  18400. return SCRIPT_CMD_FAILURE;
  18401. }
  18402. if( sd->status.party_id ) {
  18403. script_pushint(st,-2);
  18404. return SCRIPT_CMD_FAILURE;
  18405. }
  18406. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  18407. trim(party_name);
  18408. if( party_searchname(party_name) ) {
  18409. script_pushint(st,-3);
  18410. return SCRIPT_CMD_FAILURE;
  18411. }
  18412. if( script_getnum(st,4) )
  18413. item1 = 1;
  18414. if( script_getnum(st,5) )
  18415. item2 = 1;
  18416. party_create_byscript = 1;
  18417. script_pushint(st,party_create(sd,party_name,item1,item2));
  18418. return SCRIPT_CMD_SUCCESS;
  18419. }
  18420. /** Adds player to specified party
  18421. * party_addmember <party id>,<char id>;
  18422. * @param party_id: The party that will be entered by player
  18423. * @param char_id: Char id of player that will be joined to the party
  18424. * @return val: Result value
  18425. * -5 - another character of the same account is in the party
  18426. * -4 - party is full
  18427. * -3 - party is not found
  18428. * -2 - player is in party already
  18429. * -1 - player is not found
  18430. * 0 - unknown error
  18431. * 1 - success
  18432. */
  18433. BUILDIN_FUNC(party_addmember)
  18434. {
  18435. int party_id = script_getnum(st,2);
  18436. TBL_PC *sd;
  18437. struct party_data *party;
  18438. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  18439. script_pushint(st,-1);
  18440. return SCRIPT_CMD_FAILURE;
  18441. }
  18442. if( sd->status.party_id ) {
  18443. script_pushint(st,-2);
  18444. return SCRIPT_CMD_FAILURE;
  18445. }
  18446. if( !(party = party_search(party_id)) ) {
  18447. script_pushint(st,-3);
  18448. return SCRIPT_CMD_FAILURE;
  18449. }
  18450. if (battle_config.block_account_in_same_party) {
  18451. int i;
  18452. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  18453. if (i < MAX_PARTY) {
  18454. script_pushint(st,-5);
  18455. return SCRIPT_CMD_FAILURE;
  18456. }
  18457. }
  18458. if( party->party.count >= MAX_PARTY ) {
  18459. script_pushint(st,-4);
  18460. return SCRIPT_CMD_FAILURE;
  18461. }
  18462. sd->party_invite = party_id;
  18463. script_pushint(st,party_add_member(party_id,sd));
  18464. return SCRIPT_CMD_SUCCESS;
  18465. }
  18466. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  18467. * party_delmember {<char id>,<party_id>};
  18468. * @param: char_id
  18469. * @param: party_id
  18470. * @return val: Result value
  18471. * -3 - player is not in party
  18472. * -2 - party is not found
  18473. * -1 - player is not found
  18474. * 0 - unknown error
  18475. * 1 - success
  18476. */
  18477. BUILDIN_FUNC(party_delmember)
  18478. {
  18479. TBL_PC *sd = NULL;
  18480. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !script_rid2sd(sd) ) {
  18481. script_pushint(st,-1);
  18482. return SCRIPT_CMD_FAILURE;
  18483. }
  18484. if( sd || script_charid2sd(2,sd) )
  18485. script_pushint(st,party_removemember2(sd,0,0));
  18486. else
  18487. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  18488. return SCRIPT_CMD_SUCCESS;
  18489. }
  18490. /** Changes party leader of specified party (even the leader is offline)
  18491. * party_changeleader <party id>,<char id>;
  18492. * @param party_id: ID of party
  18493. * @param char_id: Char ID of new leader
  18494. * @return val: Result value
  18495. * -4 - player is party leader already
  18496. * -3 - player is not in this party
  18497. * -2 - player is not found
  18498. * -1 - party is not found
  18499. * 0 - unknown error
  18500. * 1 - success */
  18501. BUILDIN_FUNC(party_changeleader)
  18502. {
  18503. int i, party_id = script_getnum(st,2);
  18504. TBL_PC *sd = NULL;
  18505. TBL_PC *tsd = NULL;
  18506. struct party_data *party = NULL;
  18507. if( !(party = party_search(party_id)) ) {
  18508. script_pushint(st,-1);
  18509. return SCRIPT_CMD_FAILURE;
  18510. }
  18511. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  18512. script_pushint(st,-2);
  18513. return SCRIPT_CMD_FAILURE;
  18514. }
  18515. if( tsd->status.party_id != party_id ) {
  18516. script_pushint(st,-3);
  18517. return SCRIPT_CMD_FAILURE;
  18518. }
  18519. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  18520. if( i >= MAX_PARTY ) { //this is should impossible!
  18521. script_pushint(st,0);
  18522. return SCRIPT_CMD_FAILURE;
  18523. }
  18524. if( party->data[i].sd == tsd ) {
  18525. script_pushint(st,-4);
  18526. return SCRIPT_CMD_FAILURE;
  18527. }
  18528. script_pushint(st,party_changeleader(sd,tsd,party));
  18529. return SCRIPT_CMD_SUCCESS;
  18530. }
  18531. /** Changes party option
  18532. * party_changeoption <party id>,<option>,<flag>;
  18533. * @param party_id: ID of party that will be changed
  18534. * @param option: Type of option
  18535. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  18536. */
  18537. BUILDIN_FUNC(party_changeoption)
  18538. {
  18539. struct party_data *party;
  18540. if( !(party = party_search(script_getnum(st,2))) ) {
  18541. script_pushint(st,-1);
  18542. return SCRIPT_CMD_FAILURE;
  18543. }
  18544. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  18545. return SCRIPT_CMD_SUCCESS;
  18546. }
  18547. /** Destroys party with party id.
  18548. * party_destroy <party id>;
  18549. * @param party_id: ID of party that will be destroyed
  18550. */
  18551. BUILDIN_FUNC(party_destroy)
  18552. {
  18553. int i;
  18554. struct party_data *party;
  18555. if( !(party = party_search(script_getnum(st,2))) ) {
  18556. script_pushint(st,0);
  18557. return SCRIPT_CMD_FAILURE;
  18558. }
  18559. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  18560. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  18561. int j;
  18562. for( j = 0; j < MAX_PARTY; j++ ) {
  18563. TBL_PC *sd = party->data[j].sd;
  18564. if(sd)
  18565. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  18566. else if( party->party.member[j].char_id )
  18567. 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);
  18568. }
  18569. party_broken(party->party.party_id);
  18570. script_pushint(st,1);
  18571. }
  18572. else //leader leave = party broken
  18573. script_pushint(st,party_leave(party->data[i].sd));
  18574. return SCRIPT_CMD_SUCCESS;
  18575. }
  18576. /** Returns various information about a player's VIP status. Need to enable VIP system
  18577. * vip_status <type>,{"<character name>"};
  18578. * @param type: Info type, see enum vip_status_type
  18579. * @param name: Character name (Optional)
  18580. */
  18581. BUILDIN_FUNC(vip_status) {
  18582. #ifdef VIP_ENABLE
  18583. TBL_PC *sd;
  18584. int type;
  18585. if( !script_nick2sd(3,sd) )
  18586. return SCRIPT_CMD_FAILURE;
  18587. type = script_getnum(st, 2);
  18588. switch(type) {
  18589. case VIP_STATUS_ACTIVE: // Get VIP status.
  18590. script_pushint(st, pc_isvip(sd));
  18591. break;
  18592. case VIP_STATUS_EXPIRE: // Get VIP expire date.
  18593. if (pc_isvip(sd)) {
  18594. script_pushint(st, sd->vip.time);
  18595. } else
  18596. script_pushint(st, 0);
  18597. break;
  18598. case VIP_STATUS_REMAINING: // Get remaining time.
  18599. if (pc_isvip(sd)) {
  18600. script_pushint(st, sd->vip.time - time(NULL));
  18601. } else
  18602. script_pushint(st, 0);
  18603. break;
  18604. default:
  18605. ShowError( "buildin_vip_status: Unsupported type %d.\n", type );
  18606. return SCRIPT_CMD_FAILURE;
  18607. }
  18608. #else
  18609. script_pushint(st, 0);
  18610. #endif
  18611. return SCRIPT_CMD_SUCCESS;
  18612. }
  18613. /** Adds or removes VIP time in minutes. Need to enable VIP system
  18614. * vip_time <time in mn>,{"<character name>"};
  18615. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  18616. * @param name: Character name (optional)
  18617. */
  18618. BUILDIN_FUNC(vip_time) {
  18619. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  18620. TBL_PC *sd;
  18621. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  18622. if( !script_nick2sd(3,sd) )
  18623. return SCRIPT_CMD_FAILURE;
  18624. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  18625. #endif
  18626. return SCRIPT_CMD_SUCCESS;
  18627. }
  18628. /**
  18629. * Turns a player into a monster and optionally can grant a SC attribute effect.
  18630. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18631. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18632. * @param monster: Monster ID or name
  18633. * @param duration: Transform duration in millisecond (ms)
  18634. * @param sc_type: Type of SC that will be affected during the transformation
  18635. * @param val1: Value for SC
  18636. * @param val2: Value for SC
  18637. * @param val3: Value for SC
  18638. * @param val4: Value for SC
  18639. * @author: malufett
  18640. */
  18641. BUILDIN_FUNC(montransform) {
  18642. TBL_PC *sd;
  18643. enum sc_type type;
  18644. int tick, mob_id, val1, val2, val3, val4;
  18645. struct script_data *data;
  18646. val1 = val2 = val3 = val4 = 0;
  18647. if( !script_rid2sd(sd) )
  18648. return SCRIPT_CMD_FAILURE;
  18649. data = script_getdata(st, 2);
  18650. get_val(st, data); // Convert into value in case of a variable
  18651. if( data_isstring(data) )
  18652. mob_id = mobdb_searchname(script_getstr(st, 2));
  18653. else
  18654. mob_id = mobdb_checkid(script_getnum(st, 2));
  18655. tick = script_getnum(st, 3);
  18656. if (script_hasdata(st, 4))
  18657. type = (sc_type)script_getnum(st, 4);
  18658. else
  18659. type = SC_NONE;
  18660. if (mob_id == 0) {
  18661. if( data_isstring(data) )
  18662. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  18663. else
  18664. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  18665. return SCRIPT_CMD_FAILURE;
  18666. }
  18667. if (mob_id == MOBID_EMPERIUM) {
  18668. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  18669. return SCRIPT_CMD_FAILURE;
  18670. }
  18671. if (!(type >= SC_NONE && type < SC_MAX)) {
  18672. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  18673. return SCRIPT_CMD_FAILURE;
  18674. }
  18675. if (script_hasdata(st, 5))
  18676. val1 = script_getnum(st, 5);
  18677. if (script_hasdata(st, 6))
  18678. val2 = script_getnum(st, 6);
  18679. if (script_hasdata(st, 7))
  18680. val3 = script_getnum(st, 7);
  18681. if (script_hasdata(st, 8))
  18682. val4 = script_getnum(st, 8);
  18683. if (tick != 0) {
  18684. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  18685. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  18686. return SCRIPT_CMD_FAILURE;
  18687. }
  18688. if (sd->disguise){
  18689. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  18690. return SCRIPT_CMD_FAILURE;
  18691. }
  18692. if (!strcmp(script_getfuncname(st), "active_transform")) {
  18693. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18694. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18695. } else {
  18696. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18697. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18698. }
  18699. if (type != SC_NONE)
  18700. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  18701. }
  18702. return SCRIPT_CMD_SUCCESS;
  18703. }
  18704. /**
  18705. * Attach script to player for certain duration
  18706. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  18707. * @param "script code"
  18708. * @param duration
  18709. * @param flag
  18710. * @param icon
  18711. * @param char_id
  18712. * @author [Cydh]
  18713. **/
  18714. BUILDIN_FUNC(bonus_script) {
  18715. uint16 flag = 0;
  18716. int16 icon = SI_BLANK;
  18717. uint32 dur;
  18718. uint8 type = 0;
  18719. TBL_PC* sd;
  18720. const char *script_str = NULL;
  18721. struct s_bonus_script_entry *entry = NULL;
  18722. if ( !script_charid2sd(7,sd) )
  18723. return SCRIPT_CMD_FAILURE;
  18724. script_str = script_getstr(st,2);
  18725. dur = 1000 * abs(script_getnum(st,3));
  18726. FETCH(4, flag);
  18727. FETCH(5, type);
  18728. FETCH(6, icon);
  18729. // No Script string, No Duration!
  18730. if (script_str[0] == '\0' || !dur) {
  18731. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  18732. return SCRIPT_CMD_FAILURE;
  18733. }
  18734. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  18735. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  18736. return SCRIPT_CMD_FAILURE;
  18737. }
  18738. if (icon <= SI_BLANK || icon >= SI_MAX)
  18739. icon = SI_BLANK;
  18740. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  18741. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  18742. status_calc_pc(sd,SCO_NONE);
  18743. }
  18744. return SCRIPT_CMD_SUCCESS;
  18745. }
  18746. /**
  18747. * Removes all bonus script from player
  18748. * bonus_script_clear {<flag>,{<char_id>}};
  18749. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  18750. * @param char_id Clear script from this character
  18751. * @author [Cydh]
  18752. */
  18753. BUILDIN_FUNC(bonus_script_clear) {
  18754. TBL_PC* sd;
  18755. bool flag = false;
  18756. if (!script_charid2sd(3,sd))
  18757. return SCRIPT_CMD_FAILURE;
  18758. if (script_hasdata(st,2))
  18759. flag = script_getnum(st,2);
  18760. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  18761. return SCRIPT_CMD_SUCCESS;
  18762. }
  18763. /** Allows player to use atcommand while talking with NPC
  18764. * enable_command;
  18765. * @author [Cydh], [Kichi]
  18766. */
  18767. BUILDIN_FUNC(enable_command) {
  18768. TBL_PC* sd;
  18769. if (!script_rid2sd(sd))
  18770. return SCRIPT_CMD_FAILURE;
  18771. sd->state.disable_atcommand_on_npc = 0;
  18772. return SCRIPT_CMD_SUCCESS;
  18773. }
  18774. /** Prevents player to use atcommand while talking with NPC
  18775. * disable_command;
  18776. * @author [Cydh], [Kichi]
  18777. */
  18778. BUILDIN_FUNC(disable_command) {
  18779. TBL_PC* sd;
  18780. if (!script_rid2sd(sd))
  18781. return SCRIPT_CMD_FAILURE;
  18782. sd->state.disable_atcommand_on_npc = 1;
  18783. return SCRIPT_CMD_SUCCESS;
  18784. }
  18785. /** Get the information of the members of a guild by type.
  18786. * getguildmember <guild_id>{,<type>};
  18787. * @param guild_id: ID of guild
  18788. * @param type: Type of option (optional)
  18789. */
  18790. BUILDIN_FUNC(getguildmember)
  18791. {
  18792. struct guild *g = NULL;
  18793. uint8 j = 0;
  18794. g = guild_search(script_getnum(st,2));
  18795. if (g) {
  18796. uint8 i, type = 0;
  18797. struct script_data *data = NULL;
  18798. char *varname = NULL;
  18799. if (script_hasdata(st,3))
  18800. type = script_getnum(st,3);
  18801. if (script_hasdata(st,4)) {
  18802. data = script_getdata(st, 4);
  18803. if (!data_isreference(data)) {
  18804. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  18805. return SCRIPT_CMD_FAILURE;
  18806. }
  18807. varname = reference_getname(data);
  18808. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  18809. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  18810. return SCRIPT_CMD_FAILURE;
  18811. }
  18812. }
  18813. for (i = 0; i < MAX_GUILD; i++) {
  18814. if (g->member[i].account_id) {
  18815. switch (type) {
  18816. case 2:
  18817. if (data)
  18818. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  18819. else
  18820. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  18821. break;
  18822. case 1:
  18823. if (data)
  18824. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  18825. else
  18826. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  18827. break;
  18828. default:
  18829. if (data)
  18830. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  18831. else
  18832. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  18833. break;
  18834. }
  18835. j++;
  18836. }
  18837. }
  18838. }
  18839. mapreg_setreg(add_str("$@guildmembercount"), j);
  18840. script_pushint(st, j);
  18841. return SCRIPT_CMD_SUCCESS;
  18842. }
  18843. /** Adds spirit ball to player for 'duration' in milisecond
  18844. * addspiritball <count>,<duration>{,<char_id>};
  18845. * @param count How many spirit ball will be added
  18846. * @param duration How long spiritball is active until it disappears
  18847. * @param char_id Target player (Optional)
  18848. * @author [Cydh]
  18849. */
  18850. BUILDIN_FUNC(addspiritball) {
  18851. uint8 i, count;
  18852. uint16 duration;
  18853. struct map_session_data *sd = NULL;
  18854. if (script_hasdata(st,4)) {
  18855. if (!script_isstring(st,4))
  18856. sd = map_charid2sd(script_getnum(st,4));
  18857. else
  18858. sd = map_nick2sd(script_getstr(st,4),false);
  18859. }
  18860. else
  18861. script_rid2sd(sd);
  18862. if (!sd)
  18863. return SCRIPT_CMD_FAILURE;
  18864. count = script_getnum(st,2);
  18865. if (count == 0)
  18866. return SCRIPT_CMD_SUCCESS;
  18867. duration = script_getnum(st,3);
  18868. for (i = 0; i < count; i++)
  18869. pc_addspiritball(sd,duration,10);
  18870. return SCRIPT_CMD_SUCCESS;
  18871. }
  18872. /** Deletes the spirit ball(s) from player
  18873. * delspiritball <count>{,<char_id>};
  18874. * @param count How many spirit ball will be deleted
  18875. * @param char_id Target player (Optional)
  18876. * @author [Cydh]
  18877. */
  18878. BUILDIN_FUNC(delspiritball) {
  18879. uint8 count;
  18880. struct map_session_data *sd = NULL;
  18881. if (script_hasdata(st,3)) {
  18882. if (!script_isstring(st,3))
  18883. sd = map_charid2sd(script_getnum(st,3));
  18884. else
  18885. sd = map_nick2sd(script_getstr(st,3),false);
  18886. }
  18887. else
  18888. script_rid2sd(sd);
  18889. if (!sd)
  18890. return SCRIPT_CMD_FAILURE;
  18891. count = script_getnum(st,2);
  18892. if (count == 0)
  18893. count = 1;
  18894. pc_delspiritball(sd,count,0);
  18895. return SCRIPT_CMD_SUCCESS;
  18896. }
  18897. /** Counts the spirit ball that player has
  18898. * countspiritball {<char_id>};
  18899. * @param char_id Target player (Optional)
  18900. * @author [Cydh]
  18901. */
  18902. BUILDIN_FUNC(countspiritball) {
  18903. struct map_session_data *sd;
  18904. if (script_hasdata(st,2)) {
  18905. if (!script_isstring(st,2))
  18906. sd = map_charid2sd(script_getnum(st,2));
  18907. else
  18908. sd = map_nick2sd(script_getstr(st,2),false);
  18909. }
  18910. else
  18911. script_rid2sd(sd);
  18912. if (!sd)
  18913. return SCRIPT_CMD_FAILURE;
  18914. script_pushint(st,sd->spiritball);
  18915. return SCRIPT_CMD_SUCCESS;
  18916. }
  18917. /** Merges separated stackable items because of guid
  18918. * mergeitem {<char_id>};
  18919. * @param char_id Char ID (Optional)
  18920. * @author [Cydh]
  18921. */
  18922. BUILDIN_FUNC(mergeitem) {
  18923. struct map_session_data *sd;
  18924. if (!script_charid2sd(2, sd))
  18925. return SCRIPT_CMD_FAILURE;
  18926. clif_merge_item_open(sd);
  18927. return SCRIPT_CMD_SUCCESS;
  18928. }
  18929. /** Merges separated stackable items because of guid (Unofficial)
  18930. * mergeitem2 {<item_id>,{<char_id>}};
  18931. * mergeitem2 {"<item name>",{<char_id>}};
  18932. * @param item Item ID/Name for merging specific item (Optional)
  18933. * @author [Cydh]
  18934. */
  18935. BUILDIN_FUNC(mergeitem2) {
  18936. struct map_session_data *sd;
  18937. struct item *items = NULL;
  18938. uint16 i, count = 0, nameid = 0;
  18939. if (!script_charid2sd(3, sd))
  18940. return SCRIPT_CMD_FAILURE;
  18941. if (script_hasdata(st, 2)) {
  18942. struct script_data *data = script_getdata(st, 2);
  18943. get_val(st, data);
  18944. if (data_isstring(data)) {// "<item name>"
  18945. const char *name = conv_str(st,data);
  18946. struct item_data *id;
  18947. if (!(id = itemdb_searchname(name))) {
  18948. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  18949. script_pushint(st, count);
  18950. return SCRIPT_CMD_FAILURE;
  18951. }
  18952. nameid = id->nameid;
  18953. }
  18954. else if (data_isint(data)) {// <item id>
  18955. nameid = conv_num(st,data);
  18956. if (!itemdb_exists(nameid)) {
  18957. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  18958. script_pushint(st, count);
  18959. return SCRIPT_CMD_FAILURE;
  18960. }
  18961. }
  18962. }
  18963. for (i = 0; i < MAX_INVENTORY; i++) {
  18964. struct item *it = &sd->inventory.u.items_inventory[i];
  18965. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  18966. continue;
  18967. if ((!nameid || (nameid == it->nameid))) {
  18968. uint8 k;
  18969. if (!count) {
  18970. CREATE(items, struct item, 1);
  18971. memcpy(&items[count++], it, sizeof(struct item));
  18972. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18973. continue;
  18974. }
  18975. for (k = 0; k < count; k++) {
  18976. // Find Match
  18977. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  18978. items[k].amount += it->amount;
  18979. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18980. break;
  18981. }
  18982. }
  18983. if (k >= count) {
  18984. // New entry
  18985. RECREATE(items, struct item, count+1);
  18986. memcpy(&items[count++], it, sizeof(struct item));
  18987. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18988. }
  18989. }
  18990. }
  18991. if (!items) // Nothing todo here
  18992. return SCRIPT_CMD_SUCCESS;
  18993. // Retrieve the items
  18994. for (i = 0; i < count; i++) {
  18995. uint8 flag = 0;
  18996. if (!&items[i])
  18997. continue;
  18998. items[i].id = 0;
  18999. items[i].unique_id = 0;
  19000. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  19001. clif_additem(sd, i, items[i].amount, flag);
  19002. }
  19003. aFree(items);
  19004. script_pushint(st, count);
  19005. return SCRIPT_CMD_SUCCESS;
  19006. }
  19007. /**
  19008. * Update an entry from NPC shop.
  19009. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  19010. **/
  19011. BUILDIN_FUNC(npcshopupdate) {
  19012. const char* npcname = script_getstr(st, 2);
  19013. struct npc_data* nd = npc_name2id(npcname);
  19014. uint16 nameid = script_getnum(st, 3);
  19015. int price = script_getnum(st, 4);
  19016. #if PACKETVER >= 20131223
  19017. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  19018. #endif
  19019. int i;
  19020. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  19021. script_pushint(st,0);
  19022. return SCRIPT_CMD_FAILURE;
  19023. }
  19024. if (!nd->u.shop.count) {
  19025. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  19026. script_pushint(st,0);
  19027. return SCRIPT_CMD_FAILURE;
  19028. }
  19029. for (i = 0; i < nd->u.shop.count; i++) {
  19030. if (nd->u.shop.shop_item[i].nameid == nameid) {
  19031. if (price != 0)
  19032. nd->u.shop.shop_item[i].value = price;
  19033. #if PACKETVER >= 20131223
  19034. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  19035. nd->u.shop.shop_item[i].qty = stock;
  19036. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  19037. }
  19038. #endif
  19039. }
  19040. }
  19041. script_pushint(st,1);
  19042. return SCRIPT_CMD_SUCCESS;
  19043. }
  19044. // Clan System
  19045. BUILDIN_FUNC(clan_join){
  19046. struct map_session_data *sd;
  19047. int clan_id = script_getnum(st,2);
  19048. if( !script_charid2sd( 3, sd ) ){
  19049. script_pushint(st, false);
  19050. return SCRIPT_CMD_FAILURE;
  19051. }
  19052. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  19053. script_pushint(st, true);
  19054. else
  19055. script_pushint(st, false);
  19056. return SCRIPT_CMD_SUCCESS;
  19057. }
  19058. BUILDIN_FUNC(clan_leave){
  19059. struct map_session_data *sd;
  19060. if( !script_charid2sd( 2, sd ) ){
  19061. script_pushint(st, false);
  19062. return SCRIPT_CMD_FAILURE;
  19063. }
  19064. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  19065. script_pushint(st, true);
  19066. else
  19067. script_pushint(st, false);
  19068. return SCRIPT_CMD_SUCCESS;
  19069. }
  19070. /**
  19071. * Get rid from running script.
  19072. * getattachedrid();
  19073. **/
  19074. BUILDIN_FUNC(getattachedrid) {
  19075. script_pushint(st, st->rid);
  19076. return SCRIPT_CMD_SUCCESS;
  19077. }
  19078. /**
  19079. * Get variable from a Player
  19080. * getvar <variable>,<char_id>;
  19081. */
  19082. BUILDIN_FUNC(getvar) {
  19083. int char_id = script_getnum(st, 3);
  19084. struct map_session_data *sd = map_charid2sd(char_id);
  19085. struct script_data *data = NULL;
  19086. const char *name = NULL;
  19087. if (!sd) {
  19088. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  19089. return SCRIPT_CMD_FAILURE;
  19090. }
  19091. data = script_getdata(st, 2);
  19092. if (!data_isreference(data)) {
  19093. ShowError("buildin_getvar: Not a variable\n");
  19094. script_reportdata(data);
  19095. script_pushnil(st);
  19096. st->state = END;
  19097. return SCRIPT_CMD_FAILURE;
  19098. }
  19099. name = reference_getname(data);
  19100. if (reference_toparam(data)) {
  19101. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  19102. script_reportdata(data);
  19103. script_pushnil(st);
  19104. st->state = END;
  19105. return SCRIPT_CMD_FAILURE;
  19106. }
  19107. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  19108. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  19109. script_reportdata(data);
  19110. script_pushnil(st);
  19111. st->state = END;
  19112. return SCRIPT_CMD_FAILURE;
  19113. }
  19114. get_val_(st, data, sd);
  19115. if (data_isint(data))
  19116. script_pushint(st, conv_num_(st, data, sd));
  19117. else
  19118. script_pushstrcopy(st, conv_str_(st, data, sd));
  19119. push_val2(st->stack, C_NAME, reference_getuid(data), reference_getref(data));
  19120. return SCRIPT_CMD_SUCCESS;
  19121. }
  19122. /**
  19123. * Display script message
  19124. * showscript "<message>"{,<GID>};
  19125. **/
  19126. BUILDIN_FUNC(showscript) {
  19127. struct block_list *bl = NULL;
  19128. const char *msg = script_getstr(st,2);
  19129. int id = 0;
  19130. if (script_hasdata(st,3)) {
  19131. id = script_getnum(st,3);
  19132. bl = map_id2bl(id);
  19133. }
  19134. else {
  19135. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  19136. }
  19137. if (!bl) {
  19138. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  19139. script_pushint(st,0);
  19140. return SCRIPT_CMD_FAILURE;
  19141. }
  19142. clif_showscript(bl, msg);
  19143. script_pushint(st,1);
  19144. return SCRIPT_CMD_SUCCESS;
  19145. }
  19146. /**
  19147. * Ignore the SECURE_NPCTIMEOUT function.
  19148. * ignoretimeout <flag>{,<char_id>};
  19149. */
  19150. BUILDIN_FUNC(ignoretimeout)
  19151. {
  19152. #ifdef SECURE_NPCTIMEOUT
  19153. struct map_session_data *sd = NULL;
  19154. if (script_hasdata(st,3)) {
  19155. if (!script_isstring(st,3))
  19156. sd = map_charid2sd(script_getnum(st,3));
  19157. else
  19158. sd = map_nick2sd(script_getstr(st,3),false);
  19159. } else
  19160. script_rid2sd(sd);
  19161. if (!sd)
  19162. return SCRIPT_CMD_FAILURE;
  19163. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  19164. #endif
  19165. return SCRIPT_CMD_SUCCESS;
  19166. }
  19167. /**
  19168. * geteleminfo <type>{,<char_id>};
  19169. **/
  19170. BUILDIN_FUNC(geteleminfo) {
  19171. TBL_ELEM *ed = NULL;
  19172. TBL_PC *sd = NULL;
  19173. int type = script_getnum(st,2);
  19174. if (!script_charid2sd(3, sd)) {
  19175. script_pushint(st, 0);
  19176. return SCRIPT_CMD_SUCCESS;
  19177. }
  19178. if (!(ed = sd->ed)) {
  19179. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  19180. script_pushint(st, 0);
  19181. return SCRIPT_CMD_SUCCESS;
  19182. }
  19183. switch (type) {
  19184. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  19185. case 1: script_pushint(st, ed->bl.id); break;
  19186. default:
  19187. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  19188. script_pushint(st, 0);
  19189. return SCRIPT_CMD_FAILURE;
  19190. }
  19191. return SCRIPT_CMD_SUCCESS;
  19192. }
  19193. /**
  19194. * Set the quest info of quest_id only showed on player in level range.
  19195. * setquestinfo_level <quest_id>,<min_level>,<max_level>
  19196. * @author [Cydh]
  19197. **/
  19198. BUILDIN_FUNC(setquestinfo_level) {
  19199. TBL_NPC* nd = map_id2nd(st->oid);
  19200. int quest_id = script_getnum(st, 2);
  19201. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  19202. if (!qi) {
  19203. ShowError("buildin_setquestinfo_level: Quest with ID '%d' is not defined yet.\n", quest_id);
  19204. return SCRIPT_CMD_FAILURE;
  19205. }
  19206. qi->min_level = script_getnum(st, 3);
  19207. qi->max_level = script_getnum(st, 4);
  19208. if (!qi->max_level)
  19209. qi->max_level = MAX_LEVEL;
  19210. return SCRIPT_CMD_SUCCESS;
  19211. }
  19212. /**
  19213. * Set the quest info of quest_id only showed for player that has quest criteria
  19214. * setquestinfo_req <quest_id>,<quest_req_id>,<state>{,<quest_req_id>,<state>,...};
  19215. * @author [Cydh]
  19216. **/
  19217. BUILDIN_FUNC(setquestinfo_req) {
  19218. TBL_NPC* nd = map_id2nd(st->oid);
  19219. int quest_id = script_getnum(st, 2);
  19220. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  19221. uint8 i = 0;
  19222. uint8 num = script_lastdata(st);
  19223. if (!qi) {
  19224. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not defined yet.\n", quest_id);
  19225. return SCRIPT_CMD_FAILURE;
  19226. }
  19227. if (quest_search(quest_id) == &quest_dummy) {
  19228. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not found in Quest DB.\n", quest_id);
  19229. return SCRIPT_CMD_FAILURE;
  19230. }
  19231. if (num%2) {
  19232. ShowError("buildin_setquestinfo_req: Odd number of parameters(%d) - pairs of requirements are expected.\n", num-2);
  19233. return SCRIPT_CMD_FAILURE;
  19234. }
  19235. for (i = 3; i <= num; i += 2) {
  19236. RECREATE(qi->req, struct questinfo_req, qi->req_count+1);
  19237. qi->req[qi->req_count].quest_id = script_getnum(st, i);
  19238. qi->req[qi->req_count].state = (script_getnum(st, i+1) >= 2) ? 2 : (script_getnum(st, i+1) <= 0) ? 0 : 1;
  19239. qi->req_count++;
  19240. }
  19241. return SCRIPT_CMD_SUCCESS;
  19242. }
  19243. /**
  19244. * Set the quest info of quest_id only showed for player that has specified Job
  19245. * setquestinfo_job <quest_id>,<job>{,<job>...};
  19246. * @author [Cydh]
  19247. **/
  19248. BUILDIN_FUNC(setquestinfo_job) {
  19249. TBL_NPC* nd = map_id2nd(st->oid);
  19250. int quest_id = script_getnum(st, 2);
  19251. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  19252. int job_id = 0;
  19253. uint8 i = 0;
  19254. uint8 num = script_lastdata(st)+1;
  19255. if (!qi) {
  19256. ShowError("buildin_setquestinfo_job: Quest with ID '%d' is not defined yet.\n", quest_id);
  19257. return SCRIPT_CMD_FAILURE;
  19258. }
  19259. for (i = 3; i < num; i++) {
  19260. job_id = script_getnum(st, i);
  19261. if (!pcdb_checkid(job_id)) {
  19262. ShowError("buildin_setquestinfo_job: Invalid job id '%d' in Quest with ID %d.\n", job_id, quest_id);
  19263. continue;
  19264. }
  19265. buildin_questinfo_setjob(qi, job_id);
  19266. }
  19267. return SCRIPT_CMD_SUCCESS;
  19268. }
  19269. /**
  19270. * opendressroom(<flag>{,<char_id>});
  19271. */
  19272. BUILDIN_FUNC(opendressroom)
  19273. {
  19274. #if PACKETVER >= 20150513
  19275. int flag = 1;
  19276. TBL_PC* sd;
  19277. if( script_hasdata(st,2) )
  19278. flag = script_getnum(st,2);
  19279. if (!script_charid2sd(3, sd))
  19280. return SCRIPT_CMD_FAILURE;
  19281. clif_dressing_room(sd, flag);
  19282. return SCRIPT_CMD_SUCCESS;
  19283. #else
  19284. return SCRIPT_CMD_FAILURE;
  19285. #endif
  19286. }
  19287. /**
  19288. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  19289. */
  19290. BUILDIN_FUNC(navigateto){
  19291. #if PACKETVER >= 20111010
  19292. TBL_PC* sd;
  19293. const char *mapname;
  19294. uint16 x = 0, y = 0, monster_id = 0;
  19295. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  19296. bool hideWindow = true;
  19297. mapname = script_getstr(st,2);
  19298. if( script_hasdata(st,3) )
  19299. x = script_getnum(st,3);
  19300. if( script_hasdata(st,4) )
  19301. y = script_getnum(st,4);
  19302. if( script_hasdata(st,5) )
  19303. flag = (uint8)script_getnum(st,5);
  19304. if( script_hasdata(st,6) )
  19305. hideWindow = script_getnum(st,6) ? true : false;
  19306. if( script_hasdata(st,7) )
  19307. monster_id = script_getnum(st,7);
  19308. if (!script_charid2sd(8, sd))
  19309. return SCRIPT_CMD_FAILURE;
  19310. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  19311. return SCRIPT_CMD_SUCCESS;
  19312. #else
  19313. return SCRIPT_CMD_FAILURE;
  19314. #endif
  19315. }
  19316. /**
  19317. * adopt("<parent_name>","<baby_name>");
  19318. * adopt(<parent_id>,<baby_id>);
  19319. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  19320. */
  19321. BUILDIN_FUNC(adopt)
  19322. {
  19323. TBL_PC *sd, *b_sd;
  19324. struct script_data *data;
  19325. enum adopt_responses response;
  19326. data = script_getdata(st, 2);
  19327. get_val(st, data);
  19328. if (data_isstring(data)) {
  19329. const char *name = conv_str(st, data);
  19330. sd = map_nick2sd(name,false);
  19331. if (sd == NULL) {
  19332. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  19333. return SCRIPT_CMD_FAILURE;
  19334. }
  19335. } else if (data_isint(data)) {
  19336. uint32 char_id = conv_num(st, data);
  19337. sd = map_charid2sd(char_id);
  19338. if (sd == NULL) {
  19339. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  19340. return SCRIPT_CMD_FAILURE;
  19341. }
  19342. } else {
  19343. ShowError("buildin_adopt: Invalid data type for argument #1 (%d).", data->type);
  19344. return SCRIPT_CMD_FAILURE;
  19345. }
  19346. data = script_getdata(st, 3);
  19347. get_val(st, data);
  19348. if (data_isstring(data)) {
  19349. const char *name = conv_str(st, data);
  19350. b_sd = map_nick2sd(name,false);
  19351. if (b_sd == NULL) {
  19352. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  19353. return SCRIPT_CMD_FAILURE;
  19354. }
  19355. } else if (data_isint(data)) {
  19356. uint32 char_id = conv_num(st, data);
  19357. b_sd = map_charid2sd(char_id);
  19358. if (b_sd == NULL) {
  19359. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  19360. return SCRIPT_CMD_FAILURE;
  19361. }
  19362. } else {
  19363. ShowError("buildin_adopt: Invalid data type for argument #2 (%d).", data->type);
  19364. return SCRIPT_CMD_FAILURE;
  19365. }
  19366. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  19367. if (response == ADOPT_ALLOWED) {
  19368. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  19369. b_sd->adopt_invite = sd->status.account_id;
  19370. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  19371. script_pushint(st, ADOPT_ALLOWED);
  19372. return SCRIPT_CMD_SUCCESS;
  19373. }
  19374. script_pushint(st, response);
  19375. return SCRIPT_CMD_FAILURE;
  19376. }
  19377. /**
  19378. * Returns the minimum or maximum of all the given parameters for integer variables.
  19379. *
  19380. * min( <value or array>{,value or array 2,...} );
  19381. * minimum( <value or array>{,value or array 2,...} );
  19382. * max( <value or array>{,value or array 2,...} );
  19383. * maximum( <value or array>{,value or array 2,...} );
  19384. */
  19385. BUILDIN_FUNC(minmax){
  19386. char *functionname;
  19387. unsigned int i;
  19388. int value;
  19389. // Function pointer for our comparison function (either min or max at the moment)
  19390. int32 (*func)(int32, int32);
  19391. // Get the real function name
  19392. functionname = script_getfuncname(st);
  19393. // Our data should start at offset 2
  19394. i = 2;
  19395. if( !script_hasdata( st, i ) ){
  19396. ShowError( "buildin_%s: no arguments given!\n", functionname );
  19397. st->state = END;
  19398. return SCRIPT_CMD_FAILURE;
  19399. }
  19400. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  19401. value = INT_MAX;
  19402. func = i32min;
  19403. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  19404. value = INT_MIN;
  19405. func = i32max;
  19406. }else{
  19407. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  19408. st->state = END;
  19409. return SCRIPT_CMD_FAILURE;
  19410. }
  19411. // As long as we have data on our script stack
  19412. while( script_hasdata(st,i) ){
  19413. struct script_data *data;
  19414. // Get the next piece of data from the script stack
  19415. data = script_getdata( st, i );
  19416. // Is the current parameter a single integer?
  19417. if( data_isint( data ) ){
  19418. value = func( value, script_getnum( st, i ) );
  19419. // Is the current parameter an array variable?
  19420. }else if( data_isreference( data ) ){
  19421. const char *name;
  19422. struct map_session_data* sd;
  19423. unsigned int start, end;
  19424. // Get the name of the variable
  19425. name = reference_getname(data);
  19426. // Check if it's a string variable
  19427. if( is_string_variable( name ) ){
  19428. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  19429. script_reportdata( data );
  19430. st->state = END;
  19431. return SCRIPT_CMD_FAILURE;
  19432. }
  19433. // Get the session data, if a player is attached
  19434. sd = st->rid ? map_id2sd(st->rid) : NULL;
  19435. // Try to find the array's source pointer
  19436. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  19437. ShowError( "buildin_%s: not a array!\n", functionname );
  19438. script_reportdata( data );
  19439. st->state = END;
  19440. return SCRIPT_CMD_FAILURE;
  19441. }
  19442. // Get the start and end indices of the array
  19443. start = reference_getindex( data );
  19444. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  19445. // Skip empty arrays
  19446. if( start < end ){
  19447. int id;
  19448. // For getting the values we need the id of the array
  19449. id = reference_getid( data );
  19450. // Loop through each value stored in the array
  19451. for( ; start < end; start++ ){
  19452. value = func( value, (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) ) );
  19453. script_removetop( st, -1, 0 );
  19454. }
  19455. }
  19456. }else{
  19457. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  19458. script_reportdata( data );
  19459. st->state = END;
  19460. return SCRIPT_CMD_FAILURE;
  19461. }
  19462. // Continue with the next stack entry
  19463. i++;
  19464. }
  19465. script_pushint( st, value );
  19466. return SCRIPT_CMD_SUCCESS;
  19467. }
  19468. /**
  19469. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  19470. * Unlike `getexp` that affected by some adjustments.
  19471. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  19472. * @author [Cydh]
  19473. **/
  19474. BUILDIN_FUNC(getexp2) {
  19475. TBL_PC *sd = NULL;
  19476. int base_exp = script_getnum(st, 2);
  19477. int job_exp = script_getnum(st, 3);
  19478. if (!script_charid2sd(4, sd))
  19479. return SCRIPT_CMD_FAILURE;
  19480. if (base_exp == 0 && job_exp == 0)
  19481. return SCRIPT_CMD_SUCCESS;
  19482. if (base_exp > 0)
  19483. pc_gainexp(sd, NULL, base_exp, 0, 2);
  19484. else if (base_exp < 0)
  19485. pc_lostexp(sd, base_exp * -1, 0);
  19486. if (job_exp > 0)
  19487. pc_gainexp(sd, NULL, 0, job_exp, 2);
  19488. else if (job_exp < 0)
  19489. pc_lostexp(sd, 0, job_exp * -1);
  19490. return SCRIPT_CMD_SUCCESS;
  19491. }
  19492. /**
  19493. * Force stat recalculation of sd
  19494. * recalculatestat;
  19495. * @author [secretdataz]
  19496. **/
  19497. BUILDIN_FUNC(recalculatestat) {
  19498. TBL_PC* sd;
  19499. if (!script_rid2sd(sd))
  19500. return SCRIPT_CMD_FAILURE;
  19501. status_calc_pc(sd, SCO_FORCE);
  19502. return SCRIPT_CMD_SUCCESS;
  19503. }
  19504. BUILDIN_FUNC(hateffect){
  19505. #if PACKETVER >= 20150513
  19506. struct map_session_data* sd;
  19507. bool enable;
  19508. int i, effectID;
  19509. if( !script_rid2sd(sd) )
  19510. return SCRIPT_CMD_FAILURE;
  19511. effectID = script_getnum(st,2);
  19512. enable = script_getnum(st,3) ? true : false;
  19513. ARR_FIND( 0, sd->hatEffectCount, i, sd->hatEffectIDs[i] == effectID );
  19514. if( enable ){
  19515. if( i < sd->hatEffectCount ){
  19516. return SCRIPT_CMD_SUCCESS;
  19517. }
  19518. RECREATE(sd->hatEffectIDs,uint32,sd->hatEffectCount+1);
  19519. sd->hatEffectIDs[sd->hatEffectCount] = effectID;
  19520. sd->hatEffectCount++;
  19521. }else{
  19522. if( i == sd->hatEffectCount ){
  19523. return SCRIPT_CMD_SUCCESS;
  19524. }
  19525. for( ; i < sd->hatEffectCount - 1; i++ ){
  19526. sd->hatEffectIDs[i] = sd->hatEffectIDs[i+1];
  19527. }
  19528. sd->hatEffectCount--;
  19529. if( !sd->hatEffectCount ){
  19530. aFree(sd->hatEffectIDs);
  19531. sd->hatEffectIDs = NULL;
  19532. }
  19533. }
  19534. if( !sd->state.connect_new ){
  19535. clif_hat_effect_single( sd, effectID, enable );
  19536. }
  19537. #endif
  19538. return SCRIPT_CMD_SUCCESS;
  19539. }
  19540. /**
  19541. * Retrieves param of current random option. Intended for random option script only.
  19542. * getrandomoptinfo(<type>);
  19543. * @author [secretdataz]
  19544. **/
  19545. BUILDIN_FUNC(getrandomoptinfo) {
  19546. struct map_session_data *sd;
  19547. int val;
  19548. if (script_rid2sd(sd) && 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) {
  19549. int param = script_getnum(st, 2);
  19550. switch (param) {
  19551. case ROA_ID:
  19552. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  19553. break;
  19554. case ROA_VALUE:
  19555. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  19556. break;
  19557. case ROA_PARAM:
  19558. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  19559. break;
  19560. default:
  19561. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  19562. val = 0;
  19563. break;
  19564. }
  19565. script_pushint(st, val);
  19566. }
  19567. else {
  19568. script_pushint(st, 0);
  19569. }
  19570. return SCRIPT_CMD_SUCCESS;
  19571. }
  19572. /**
  19573. * Retrieves a random option on an equipped item.
  19574. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  19575. * @author [secretdataz]
  19576. */
  19577. BUILDIN_FUNC(getequiprandomoption) {
  19578. struct map_session_data *sd;
  19579. int val;
  19580. short i = -1;
  19581. int pos = script_getnum(st, 2);
  19582. int index = script_getnum(st, 3);
  19583. int type = script_getnum(st, 4);
  19584. if (!script_charid2sd(5, sd)) {
  19585. script_pushint(st, -1);
  19586. return SCRIPT_CMD_FAILURE;
  19587. }
  19588. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19589. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  19590. script_pushint(st, -1);
  19591. return SCRIPT_CMD_FAILURE;
  19592. }
  19593. if (equip_index_check(pos))
  19594. i = pc_checkequip(sd, equip_bitmask[pos]);
  19595. if (i < 0) {
  19596. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19597. script_pushint(st, -1);
  19598. return SCRIPT_CMD_FAILURE;
  19599. }
  19600. switch (type) {
  19601. case ROA_ID:
  19602. val = sd->inventory.u.items_inventory[i].option[index].id;
  19603. break;
  19604. case ROA_VALUE:
  19605. val = sd->inventory.u.items_inventory[i].option[index].value;
  19606. break;
  19607. case ROA_PARAM:
  19608. val = sd->inventory.u.items_inventory[i].option[index].param;
  19609. break;
  19610. default:
  19611. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  19612. val = 0;
  19613. break;
  19614. }
  19615. script_pushint(st, val);
  19616. return SCRIPT_CMD_SUCCESS;
  19617. }
  19618. /**
  19619. * Adds a random option on a random option slot on an equipped item and overwrites
  19620. * existing random option in the process.
  19621. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  19622. * @author [secretdataz]
  19623. */
  19624. BUILDIN_FUNC(setrandomoption) {
  19625. struct map_session_data *sd;
  19626. struct s_random_opt_data *opt;
  19627. int pos, index, id, value, param, ep;
  19628. int i = -1;
  19629. if (!script_charid2sd(7, sd))
  19630. return SCRIPT_CMD_FAILURE;
  19631. pos = script_getnum(st, 2);
  19632. index = script_getnum(st, 3);
  19633. id = script_getnum(st, 4);
  19634. value = script_getnum(st, 5);
  19635. param = script_getnum(st, 6);
  19636. if ((opt = itemdb_randomopt_exists((short)id)) == NULL) {
  19637. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  19638. script_pushint(st, 0);
  19639. return SCRIPT_CMD_FAILURE;
  19640. }
  19641. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19642. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  19643. script_pushint(st, 0);
  19644. return SCRIPT_CMD_FAILURE;
  19645. }
  19646. if (equip_index_check(pos))
  19647. i = pc_checkequip(sd, equip_bitmask[pos]);
  19648. if (i >= 0) {
  19649. ep = sd->inventory.u.items_inventory[i].equip;
  19650. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19651. pc_unequipitem(sd, i, 2);
  19652. sd->inventory.u.items_inventory[i].option[index].id = id;
  19653. sd->inventory.u.items_inventory[i].option[index].value = value;
  19654. sd->inventory.u.items_inventory[i].option[index].param = param;
  19655. clif_delitem(sd, i, 1, 3);
  19656. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19657. clif_additem(sd, i, 1, 0);
  19658. pc_equipitem(sd, i, ep);
  19659. script_pushint(st, 1);
  19660. return SCRIPT_CMD_SUCCESS;
  19661. }
  19662. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19663. script_pushint(st, 0);
  19664. return SCRIPT_CMD_FAILURE;
  19665. }
  19666. /// Returns the number of stat points needed to change the specified stat by val.
  19667. /// If val is negative, returns the number of stat points that would be needed to
  19668. /// raise the specified stat from (current value - val) to current value.
  19669. /// *needed_status_point(<type>,<val>{,<char id>});
  19670. /// @author [secretdataz]
  19671. BUILDIN_FUNC(needed_status_point) {
  19672. struct map_session_data *sd;
  19673. int type, val;
  19674. if (!script_charid2sd(4, sd))
  19675. return SCRIPT_CMD_FAILURE;
  19676. type = script_getnum(st, 2);
  19677. val = script_getnum(st, 3);
  19678. script_pushint(st, pc_need_status_point(sd, type, val));
  19679. return SCRIPT_CMD_SUCCESS;
  19680. }
  19681. /**
  19682. * jobcanentermap("<mapname>"{,<JobID>});
  19683. * Check if (player with) JobID can enter the map.
  19684. * @param mapname Map name
  19685. * @param JobID Player's JobID (optional)
  19686. **/
  19687. BUILDIN_FUNC(jobcanentermap) {
  19688. const char *mapname = script_getstr(st, 2);
  19689. int mapidx = mapindex_name2id(mapname), m = -1;
  19690. int jobid = 0;
  19691. TBL_PC *sd = NULL;
  19692. if (!mapidx) {// Invalid map
  19693. script_pushint(st, false);
  19694. return SCRIPT_CMD_FAILURE;
  19695. }
  19696. m = map_mapindex2mapid(mapidx);
  19697. if (m == -1) { // Map is on different map server
  19698. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  19699. script_pushint(st, false);
  19700. return SCRIPT_CMD_FAILURE;
  19701. }
  19702. if (script_hasdata(st, 3)) {
  19703. jobid = script_getnum(st, 3);
  19704. } else {
  19705. if (!script_rid2sd(sd)) {
  19706. script_pushint(st, false);
  19707. return SCRIPT_CMD_FAILURE;
  19708. }
  19709. jobid = sd->status.class_;
  19710. }
  19711. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? sd->group_level : 0));
  19712. return SCRIPT_CMD_SUCCESS;
  19713. }
  19714. /**
  19715. * Return alliance information between the two guilds.
  19716. * getguildalliance(<guild id1>,<guild id2>);
  19717. * Return values:
  19718. * -2 - Guild ID1 does not exist
  19719. * -1 - Guild ID2 does not exist
  19720. * 0 - Both guilds have no relation OR guild ID aren't given
  19721. * 1 - Both guilds are allies
  19722. * 2 - Both guilds are antagonists
  19723. */
  19724. BUILDIN_FUNC(getguildalliance)
  19725. {
  19726. struct guild *guild_data1, *guild_data2;
  19727. int guild_id1, guild_id2, i = 0;
  19728. guild_id1 = script_getnum(st,2);
  19729. guild_id2 = script_getnum(st,3);
  19730. if (guild_id1 < 1 || guild_id2 < 1) {
  19731. script_pushint(st, 0);
  19732. return SCRIPT_CMD_SUCCESS;
  19733. }
  19734. if (guild_id1 == guild_id2) {
  19735. script_pushint(st, 1);
  19736. return SCRIPT_CMD_SUCCESS;
  19737. }
  19738. guild_data1 = guild_search(guild_id1);
  19739. guild_data2 = guild_search(guild_id2);
  19740. if (guild_data1 == NULL) {
  19741. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  19742. script_pushint(st, -2);
  19743. return SCRIPT_CMD_FAILURE;
  19744. }
  19745. if (guild_data2 == NULL) {
  19746. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  19747. script_pushint(st, -1);
  19748. return SCRIPT_CMD_FAILURE;
  19749. }
  19750. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  19751. if (i == MAX_GUILDALLIANCE) {
  19752. script_pushint(st, 0);
  19753. return SCRIPT_CMD_SUCCESS;
  19754. }
  19755. if (guild_data1->alliance[i].opposition)
  19756. script_pushint(st, 2);
  19757. else
  19758. script_pushint(st, 1);
  19759. return SCRIPT_CMD_SUCCESS;
  19760. }
  19761. /*
  19762. * openstorage2 <storage_id>,<mode>{,<account_id>}
  19763. * mode @see enum e_storage_mode
  19764. **/
  19765. BUILDIN_FUNC(openstorage2) {
  19766. int stor_id = script_getnum(st, 2);
  19767. TBL_PC *sd = NULL;
  19768. if (!script_accid2sd(4, sd)) {
  19769. script_pushint(st, 0);
  19770. return SCRIPT_CMD_FAILURE;
  19771. }
  19772. if (!storage_exists(stor_id)) {
  19773. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  19774. return SCRIPT_CMD_FAILURE;
  19775. }
  19776. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  19777. return SCRIPT_CMD_SUCCESS;
  19778. }
  19779. /**
  19780. * Create a new channel
  19781. * channel_create "<chname>","<alias>"{,"<password>"{<option>{,<delay>{,<color>{,<char_id>}}}}};
  19782. * @author [Cydh]
  19783. **/
  19784. BUILDIN_FUNC(channel_create) {
  19785. struct Channel tmp_chan, *ch = NULL;
  19786. const char *chname = script_getstr(st,2), *pass = NULL;
  19787. int i = channel_chk((char*)chname, NULL, 3);
  19788. TBL_PC *sd = NULL;
  19789. if (i != 0) {
  19790. ShowError("buildin_channel_create: Channel name '%s' is invalid. Errno %d\n", chname, i);
  19791. script_pushint(st,i);
  19792. return SCRIPT_CMD_FAILURE;
  19793. }
  19794. memset(&tmp_chan, 0, sizeof(struct Channel));
  19795. if (script_hasdata(st,8)) {
  19796. tmp_chan.char_id = script_getnum(st,8);
  19797. if (!(sd = map_charid2sd(tmp_chan.char_id))) {
  19798. ShowError("buildin_channel_create: Player with char id '%d' is not found.\n", tmp_chan.char_id);
  19799. script_pushint(st,-5);
  19800. return SCRIPT_CMD_FAILURE;
  19801. }
  19802. tmp_chan.type = CHAN_TYPE_PRIVATE;
  19803. i = 1;
  19804. }
  19805. else {
  19806. tmp_chan.type = CHAN_TYPE_PUBLIC;
  19807. i = 0;
  19808. }
  19809. safestrncpy(tmp_chan.name, chname+1, sizeof(tmp_chan.name));
  19810. safestrncpy(tmp_chan.alias, script_getstr(st,3), sizeof(tmp_chan.alias));
  19811. if (script_hasdata(st,4) && (pass = script_getstr(st,4)) && strcmpi(pass,"null") != 0)
  19812. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  19813. if (script_hasdata(st,5))
  19814. tmp_chan.opt = script_getnum(st,5);
  19815. else
  19816. tmp_chan.opt = i ? channel_config.private_channel.opt : CHAN_OPT_BASE;
  19817. if (script_hasdata(st,6))
  19818. tmp_chan.msg_delay = script_getnum(st,6);
  19819. else
  19820. tmp_chan.msg_delay = i ? channel_config.private_channel.delay : 1000;
  19821. if (script_hasdata(st,7))
  19822. tmp_chan.color = script_getnum(st,7);
  19823. else
  19824. tmp_chan.color = i ? channel_config.private_channel.color : channel_getColor("Default");
  19825. if (!(ch = channel_create(&tmp_chan))) {
  19826. ShowError("buildin_channel_create: Cannot create channel '%s'.\n", chname);
  19827. script_pushint(st,0);
  19828. return SCRIPT_CMD_FAILURE;
  19829. }
  19830. if (tmp_chan.char_id)
  19831. channel_join(ch, sd);
  19832. script_pushint(st,1);
  19833. return SCRIPT_CMD_SUCCESS;
  19834. }
  19835. /**
  19836. * Set channel option
  19837. * channel_setopt "<chname>",<option>,<value>;
  19838. * @author [Cydh]
  19839. **/
  19840. BUILDIN_FUNC(channel_setopt) {
  19841. struct Channel *ch = NULL;
  19842. const char *chname = script_getstr(st,2);
  19843. int opt = script_getnum(st,3), value = script_getnum(st,4);
  19844. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  19845. ShowError("buildin_channel_setopt: Channel name '%s' is invalid.\n", chname);
  19846. script_pushint(st,0);
  19847. return SCRIPT_CMD_FAILURE;
  19848. }
  19849. switch (opt) {
  19850. case CHAN_OPT_ANNOUNCE_SELF:
  19851. case CHAN_OPT_ANNOUNCE_JOIN:
  19852. case CHAN_OPT_ANNOUNCE_LEAVE:
  19853. case CHAN_OPT_COLOR_OVERRIDE:
  19854. case CHAN_OPT_CAN_CHAT:
  19855. case CHAN_OPT_CAN_LEAVE:
  19856. case CHAN_OPT_AUTOJOIN:
  19857. if (value)
  19858. ch->opt |= opt;
  19859. else
  19860. ch->opt &= ~opt;
  19861. break;
  19862. case CHAN_OPT_MSG_DELAY:
  19863. ch->msg_delay = value;
  19864. break;
  19865. default:
  19866. ShowError("buildin_channel_setopt: Invalid option %d!\n", opt);
  19867. script_pushint(st,0);
  19868. return SCRIPT_CMD_FAILURE;
  19869. }
  19870. script_pushint(st,1);
  19871. return SCRIPT_CMD_SUCCESS;
  19872. }
  19873. /**
  19874. * Set channel color
  19875. * channel_setcolor "<chname>",<color>;
  19876. * @author [Cydh]
  19877. **/
  19878. BUILDIN_FUNC(channel_setcolor) {
  19879. struct Channel *ch = NULL;
  19880. const char *chname = script_getstr(st,2);
  19881. int color = script_getnum(st,3);
  19882. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  19883. ShowError("buildin_channel_setcolor: Channel name '%s' is invalid.\n", chname);
  19884. script_pushint(st,0);
  19885. return SCRIPT_CMD_FAILURE;
  19886. }
  19887. ch->color = (color & 0x0000FF) << 16 | (color & 0x00FF00) | (color & 0xFF0000) >> 16;//RGB to BGR
  19888. script_pushint(st,1);
  19889. return SCRIPT_CMD_SUCCESS;
  19890. }
  19891. /**
  19892. * Set channel password
  19893. * channel_setpass "<chname>","<password>";
  19894. * @author [Cydh]
  19895. **/
  19896. BUILDIN_FUNC(channel_setpass) {
  19897. struct Channel *ch = NULL;
  19898. const char *chname = script_getstr(st,2), *passwd = script_getstr(st,3);
  19899. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  19900. ShowError("buildin_channel_setpass: Channel name '%s' is invalid.\n", chname);
  19901. script_pushint(st,0);
  19902. return SCRIPT_CMD_FAILURE;
  19903. }
  19904. if (!passwd || !strcmpi(passwd,"null"))
  19905. memset(ch->pass, '\0', sizeof(ch->pass));
  19906. else
  19907. safestrncpy(ch->pass, passwd, sizeof(ch->pass));
  19908. script_pushint(st,1);
  19909. return SCRIPT_CMD_SUCCESS;
  19910. }
  19911. /**
  19912. * Set authorized groups
  19913. * channel_setgroup "<chname>",<group_id>{,...,<group_id>};
  19914. * @author [Cydh]
  19915. **/
  19916. BUILDIN_FUNC(channel_setgroup) {
  19917. struct Channel *ch = NULL;
  19918. const char *funcname = script_getfuncname(st), *chname = script_getstr(st,2);
  19919. int i, n = 0, group = 0;
  19920. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  19921. ShowError("buildin_channel_setgroup: Channel name '%s' is invalid.\n", chname);
  19922. script_pushint(st,0);
  19923. return SCRIPT_CMD_FAILURE;
  19924. }
  19925. if (funcname[strlen(funcname)-1] == '2') {
  19926. struct script_data *data = script_getdata(st,3);
  19927. const char *varname = reference_getname(data);
  19928. int32 id, idx;
  19929. if (varname[strlen(varname)-1] == '$') {
  19930. ShowError("buildin_channel_setgroup: The array %s is not numeric type.\n", varname);
  19931. script_pushint(st,0);
  19932. return SCRIPT_CMD_FAILURE;
  19933. }
  19934. n = script_array_highest_key(st, NULL, reference_getname(data), reference_getref(data));
  19935. if (n < 1) {
  19936. ShowError("buildin_channel_setgroup: No group id listed.\n");
  19937. script_pushint(st,0);
  19938. return SCRIPT_CMD_FAILURE;
  19939. }
  19940. if (ch->groups)
  19941. aFree(ch->groups);
  19942. ch->groups = NULL;
  19943. ch->group_count = 0;
  19944. id = reference_getid(data);
  19945. idx = reference_getindex(data);
  19946. for (i = 0; i < n; i++) {
  19947. group = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id,idx+i),reference_getref(data)));
  19948. if (group == 0) {
  19949. script_pushint(st,1);
  19950. return SCRIPT_CMD_SUCCESS;
  19951. }
  19952. RECREATE(ch->groups, unsigned short, ++ch->group_count);
  19953. ch->groups[ch->group_count-1] = group;
  19954. ShowInfo("buildin_channel_setgroup: (2) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  19955. }
  19956. }
  19957. else {
  19958. group = script_getnum(st,3);
  19959. n = script_lastdata(st)-1;
  19960. if (n < 1) {
  19961. ShowError("buildin_channel_setgroup: Please input at least 1 group_id.\n");
  19962. script_pushint(st,0);
  19963. return SCRIPT_CMD_FAILURE;
  19964. }
  19965. if (ch->groups)
  19966. aFree(ch->groups);
  19967. ch->groups = NULL;
  19968. ch->group_count = 0;
  19969. if (group == 0) { // Removed group list
  19970. script_pushint(st,1);
  19971. return SCRIPT_CMD_SUCCESS;
  19972. }
  19973. CREATE(ch->groups, unsigned short, n);
  19974. for (i = 3; i < n+2; i++) {
  19975. ch->groups[ch->group_count++] = script_getnum(st,i);
  19976. ShowInfo("buildin_channel_setgroup: (1) Added group %d. Num: %d\n", ch->groups[ch->group_count-1], ch->group_count);
  19977. }
  19978. }
  19979. script_pushint(st,n);
  19980. return SCRIPT_CMD_SUCCESS;
  19981. }
  19982. /**
  19983. * Send message on channel
  19984. * channel_chat "<chname>","<message>"{,<color>};
  19985. * @author [Cydh]
  19986. **/
  19987. BUILDIN_FUNC(channel_chat) {
  19988. struct Channel *ch = NULL;
  19989. const char *chname = script_getstr(st,2), *msg = script_getstr(st,3);
  19990. char output[CHAT_SIZE_MAX+1];
  19991. unsigned long color = 0;
  19992. // Check also local channels
  19993. if (chname[0] != '#') { // By Map
  19994. int m = mapindex_name2id(chname);
  19995. if (!m || (m = map_mapindex2mapid(m)) < 0) {
  19996. ShowError("buildin_channel_chat: Invalid map '%s'.\n", chname);
  19997. script_pushint(st,0);
  19998. return SCRIPT_CMD_FAILURE;
  19999. }
  20000. if (!(ch = map[m].channel)) {
  20001. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  20002. script_pushint(st,0);
  20003. return SCRIPT_CMD_SUCCESS;
  20004. }
  20005. }
  20006. else if (strcmpi(chname+1,channel_config.map_tmpl.name) == 0) {
  20007. TBL_NPC *nd = map_id2nd(st->oid);
  20008. if (!nd || nd->bl.m == -1) {
  20009. ShowError("buildin_channel_chat: Floating NPC needs map name instead '%s'.\n", chname);
  20010. script_pushint(st,0);
  20011. return SCRIPT_CMD_FAILURE;
  20012. }
  20013. if (!(ch = map[nd->bl.m].channel)) {
  20014. ShowDebug("buildin_channel_chat: Map '%s' doesn't have local channel yet.\n", chname);
  20015. script_pushint(st,0);
  20016. return SCRIPT_CMD_SUCCESS;
  20017. }
  20018. }
  20019. else if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20020. ShowError("buildin_channel_chat: Channel name '%s' is invalid.\n", chname);
  20021. script_pushint(st,0);
  20022. return SCRIPT_CMD_FAILURE;
  20023. }
  20024. if (!ch) {
  20025. script_pushint(st,0);
  20026. return SCRIPT_CMD_FAILURE;
  20027. }
  20028. color = ch->color;
  20029. FETCH(4, color);
  20030. safesnprintf(output, CHAT_SIZE_MAX, "%s %s", ch->alias, msg);
  20031. clif_channel_msg(ch,output,color);
  20032. script_pushint(st,1);
  20033. return SCRIPT_CMD_SUCCESS;
  20034. }
  20035. /**
  20036. * Ban player from a channel
  20037. * channel_ban "<chname>",<char_id>;
  20038. * @author [Cydh]
  20039. **/
  20040. BUILDIN_FUNC(channel_ban) {
  20041. struct Channel *ch = NULL;
  20042. const char *chname = script_getstr(st,2);
  20043. unsigned int char_id = script_getnum(st,3);
  20044. TBL_PC *tsd;
  20045. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20046. ShowError("buildin_channel_ban: Channel name '%s' is invalid.\n", chname);
  20047. script_pushint(st,0);
  20048. return SCRIPT_CMD_FAILURE;
  20049. }
  20050. if (ch->char_id == char_id) {
  20051. script_pushint(st,0);
  20052. return SCRIPT_CMD_SUCCESS;
  20053. }
  20054. tsd = map_charid2sd(char_id);
  20055. if (tsd && pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  20056. script_pushint(st,0);
  20057. return SCRIPT_CMD_SUCCESS;
  20058. }
  20059. if (!idb_exists(ch->banned, char_id)) {
  20060. struct chan_banentry *cbe;
  20061. char output[CHAT_SIZE_MAX+1];
  20062. CREATE(cbe, struct chan_banentry, 1);
  20063. cbe->char_id = char_id;
  20064. if (tsd) {
  20065. strcpy(cbe->char_name,tsd->status.name);
  20066. channel_clean(ch,tsd,0);
  20067. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, tsd->status.name); // %s %s has been banned.
  20068. }
  20069. else
  20070. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,769), ch->alias, "****"); // %s %s has been banned.
  20071. idb_put(ch->banned, char_id, cbe);
  20072. clif_channel_msg(ch,output,ch->color);
  20073. }
  20074. script_pushint(st,1);
  20075. return SCRIPT_CMD_SUCCESS;
  20076. }
  20077. /**
  20078. * Ban player from a channel
  20079. * channel_unban "<chname>",<char_id>;
  20080. * @author [Cydh]
  20081. **/
  20082. BUILDIN_FUNC(channel_unban) {
  20083. struct Channel *ch = NULL;
  20084. const char *chname = script_getstr(st,2);
  20085. unsigned int char_id = script_getnum(st,3);
  20086. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20087. ShowError("buildin_channel_unban: Channel name '%s' is invalid.\n", chname);
  20088. script_pushint(st,0);
  20089. return SCRIPT_CMD_FAILURE;
  20090. }
  20091. if (ch->char_id == char_id) {
  20092. script_pushint(st,0);
  20093. return SCRIPT_CMD_SUCCESS;
  20094. }
  20095. if (idb_exists(ch->banned, char_id)) {
  20096. char output[CHAT_SIZE_MAX+1];
  20097. TBL_PC *tsd = map_charid2sd(char_id);
  20098. if (tsd)
  20099. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, tsd->status.name); // %s %s has been unbanned
  20100. else
  20101. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,770), ch->alias, "****"); // %s %s has been unbanned.
  20102. idb_remove(ch->banned, char_id);
  20103. clif_channel_msg(ch,output,ch->color);
  20104. }
  20105. script_pushint(st,1);
  20106. return SCRIPT_CMD_SUCCESS;
  20107. }
  20108. /**
  20109. * Kick player from a channel
  20110. * channel_kick "<chname>",<char_id>;
  20111. * channel_kick "<chname>","<char_name>";
  20112. * @author [Cydh]
  20113. **/
  20114. BUILDIN_FUNC(channel_kick) {
  20115. struct Channel *ch = NULL;
  20116. const char *chname = script_getstr(st,2);
  20117. struct script_data *data = script_getdata(st,3);
  20118. TBL_PC *tsd = NULL;
  20119. int res = 1;
  20120. get_val(st, data);
  20121. if (data_isstring(data)) {
  20122. if (!(tsd = map_nick2sd(conv_str(st,data),false))) {
  20123. ShowError("buildin_channel_kick: Player with nick '%s' is not online\n", conv_str(st,data));
  20124. script_pushint(st,0);
  20125. return SCRIPT_CMD_FAILURE;
  20126. }
  20127. }
  20128. else {
  20129. if (!(tsd = map_charid2sd(conv_num(st,data)))) {
  20130. ShowError("buildin_channel_kick: Player with char_id '%d' is not online\n", conv_num(st,data));
  20131. script_pushint(st,0);
  20132. return SCRIPT_CMD_FAILURE;
  20133. }
  20134. }
  20135. if (!(ch = channel_name2channel((char *)chname, tsd, 0))) {
  20136. ShowError("buildin_channel_kick: Channel name '%s' is invalid.\n", chname);
  20137. script_pushint(st,0);
  20138. return SCRIPT_CMD_FAILURE;
  20139. }
  20140. if (channel_pc_haschan(tsd, ch) < 0 || ch->char_id == tsd->status.char_id || pc_has_permission(tsd,PC_PERM_CHANNEL_ADMIN)) {
  20141. script_pushint(st,0);
  20142. return SCRIPT_CMD_SUCCESS;
  20143. }
  20144. switch(ch->type){
  20145. case CHAN_TYPE_ALLY: res = channel_pcquit(tsd,3); break;
  20146. case CHAN_TYPE_MAP: res = channel_pcquit(tsd,4); break;
  20147. default: res = channel_clean(ch,tsd,0); break;
  20148. }
  20149. if (res == 0) {
  20150. char output[CHAT_SIZE_MAX+1];
  20151. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(tsd,889), ch->alias, tsd->status.name); // "%s %s is kicked"
  20152. clif_channel_msg(ch,output,ch->color);
  20153. }
  20154. script_pushint(st,1);
  20155. return SCRIPT_CMD_SUCCESS;
  20156. }
  20157. /**
  20158. * Delete a channel
  20159. * channel_delete "<chname>";
  20160. * @author [Cydh]
  20161. **/
  20162. BUILDIN_FUNC(channel_delete) {
  20163. struct Channel *ch = NULL;
  20164. const char *chname = script_getstr(st,2);
  20165. if (!(ch = channel_name2channel((char *)chname, NULL, 0))) {
  20166. ShowError("channel_delete: Channel name '%s' is invalid.\n", chname);
  20167. script_pushint(st,-1);
  20168. return SCRIPT_CMD_FAILURE;
  20169. }
  20170. script_pushint(st,channel_delete(ch,true));
  20171. return SCRIPT_CMD_SUCCESS;
  20172. }
  20173. BUILDIN_FUNC(unloadnpc) {
  20174. const char *name;
  20175. struct npc_data* nd;
  20176. name = script_getstr(st, 2);
  20177. nd = npc_name2id(name);
  20178. if( nd == NULL ){
  20179. ShowError( "buildin_unloadnpc: npc '%s' was not found.\n", name );
  20180. return SCRIPT_CMD_FAILURE;
  20181. }
  20182. npc_unload_duplicates(nd);
  20183. npc_unload(nd, true);
  20184. npc_read_event_script();
  20185. return SCRIPT_CMD_SUCCESS;
  20186. }
  20187. /**
  20188. * Add an achievement to the player's log
  20189. * achievementadd(<achievement ID>{,<char ID>});
  20190. */
  20191. BUILDIN_FUNC(achievementadd) {
  20192. struct map_session_data *sd;
  20193. int achievement_id = script_getnum(st, 2);
  20194. if (!script_charid2sd(3, sd)) {
  20195. script_pushint(st, false);
  20196. return SCRIPT_CMD_FAILURE;
  20197. }
  20198. if (achievement_search(achievement_id) == &achievement_dummy) {
  20199. ShowWarning("buildin_achievementadd: Achievement '%d' doesn't exist.\n", achievement_id);
  20200. script_pushint(st, false);
  20201. return SCRIPT_CMD_FAILURE;
  20202. }
  20203. if (achievement_add(sd, achievement_id))
  20204. script_pushint(st, true);
  20205. else
  20206. script_pushint(st, false);
  20207. return SCRIPT_CMD_SUCCESS;
  20208. }
  20209. /**
  20210. * Removes an achievement on a player.
  20211. * achievementremove(<achievement ID>{,<char ID>});
  20212. * Just for Atemo. ;)
  20213. */
  20214. BUILDIN_FUNC(achievementremove) {
  20215. struct map_session_data *sd;
  20216. int achievement_id = script_getnum(st, 2);
  20217. if (!script_charid2sd(3, sd)) {
  20218. script_pushint(st, false);
  20219. return SCRIPT_CMD_FAILURE;
  20220. }
  20221. if (achievement_search(achievement_id) == &achievement_dummy) {
  20222. ShowWarning("buildin_achievementremove: Achievement '%d' doesn't exist.\n", achievement_id);
  20223. script_pushint(st, false);
  20224. return SCRIPT_CMD_SUCCESS;
  20225. }
  20226. if (achievement_remove(sd, achievement_id))
  20227. script_pushint(st, true);
  20228. else
  20229. script_pushint(st, false);
  20230. return SCRIPT_CMD_SUCCESS;
  20231. }
  20232. /**
  20233. * Returns achievement progress
  20234. * achievementinfo(<achievement ID>,<type>{,<char ID>});
  20235. */
  20236. BUILDIN_FUNC(achievementinfo) {
  20237. struct map_session_data *sd;
  20238. int achievement_id = script_getnum(st, 2);
  20239. if (!script_charid2sd(4, sd)) {
  20240. script_pushint(st, false);
  20241. return SCRIPT_CMD_FAILURE;
  20242. }
  20243. script_pushint(st, achievement_check_progress(sd, achievement_id, script_getnum(st, 3)));
  20244. return SCRIPT_CMD_SUCCESS;
  20245. }
  20246. /**
  20247. * Award an achievement; Ignores requirements
  20248. * achievementcomplete(<achievement ID>{,<char ID>});
  20249. */
  20250. BUILDIN_FUNC(achievementcomplete) {
  20251. struct map_session_data *sd;
  20252. int i, achievement_id = script_getnum(st, 2);
  20253. if (!script_charid2sd(3, sd)) {
  20254. script_pushint(st, false);
  20255. return SCRIPT_CMD_FAILURE;
  20256. }
  20257. if (achievement_search(achievement_id) == &achievement_dummy) {
  20258. ShowWarning("buildin_achievementcomplete: Achievement '%d' doesn't exist.\n", achievement_id);
  20259. script_pushint(st, false);
  20260. return SCRIPT_CMD_FAILURE;
  20261. }
  20262. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20263. if (i == sd->achievement_data.count)
  20264. achievement_add(sd, achievement_id);
  20265. achievement_update_achievement(sd, achievement_id, true);
  20266. script_pushint(st, true);
  20267. return SCRIPT_CMD_SUCCESS;
  20268. }
  20269. /**
  20270. * Checks if the achievement exists on player.
  20271. * achievementexists(<achievement ID>{,<char ID>});
  20272. */
  20273. BUILDIN_FUNC(achievementexists) {
  20274. struct map_session_data *sd;
  20275. int i, achievement_id = script_getnum(st, 2);
  20276. if (!script_charid2sd(3, sd)) {
  20277. script_pushint(st, false);
  20278. return SCRIPT_CMD_FAILURE;
  20279. }
  20280. if (achievement_search(achievement_id) == &achievement_dummy) {
  20281. ShowWarning("buildin_achievementexists: Achievement '%d' doesn't exist.\n", achievement_id);
  20282. script_pushint(st, false);
  20283. return SCRIPT_CMD_SUCCESS;
  20284. }
  20285. ARR_FIND(0, sd->achievement_data.count, i, sd->achievement_data.achievements[i].achievement_id == achievement_id);
  20286. script_pushint(st, i < sd->achievement_data.count ? true : false);
  20287. return SCRIPT_CMD_SUCCESS;
  20288. }
  20289. #include "../custom/script.inc"
  20290. // declarations that were supposed to be exported from npc_chat.c
  20291. #ifdef PCRE_SUPPORT
  20292. BUILDIN_FUNC(defpattern);
  20293. BUILDIN_FUNC(activatepset);
  20294. BUILDIN_FUNC(deactivatepset);
  20295. BUILDIN_FUNC(deletepset);
  20296. #endif
  20297. /** Regular expression matching
  20298. * preg_match(<pattern>,<string>{,<offset>})
  20299. */
  20300. BUILDIN_FUNC(preg_match) {
  20301. #ifdef PCRE_SUPPORT
  20302. pcre *re;
  20303. pcre_extra *pcreExtra;
  20304. const char *error;
  20305. int erroffset, r, offset = 0;
  20306. int subStrVec[30];
  20307. const char* pattern = script_getstr(st,2);
  20308. const char* subject = script_getstr(st,3);
  20309. if (script_hasdata(st,4))
  20310. offset = script_getnum(st,4);
  20311. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  20312. pcreExtra = pcre_study(re, 0, &error);
  20313. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  20314. pcre_free(re);
  20315. if (pcreExtra != NULL)
  20316. pcre_free(pcreExtra);
  20317. if (r < 0)
  20318. script_pushint(st,0);
  20319. else
  20320. script_pushint(st,(r > 0) ? r : 30 / 3);
  20321. return SCRIPT_CMD_SUCCESS;
  20322. #else
  20323. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  20324. script_pushint(st,0);
  20325. return SCRIPT_CMD_SUCCESS;
  20326. #endif
  20327. }
  20328. /// script command definitions
  20329. /// for an explanation on args, see add_buildin_func
  20330. struct script_function buildin_func[] = {
  20331. // NPC interaction
  20332. BUILDIN_DEF(mes,"s*"),
  20333. BUILDIN_DEF(next,""),
  20334. BUILDIN_DEF(close,""),
  20335. BUILDIN_DEF(close2,""),
  20336. BUILDIN_DEF(menu,"sl*"),
  20337. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  20338. BUILDIN_DEF(prompt,"s*"),
  20339. //
  20340. BUILDIN_DEF(goto,"l"),
  20341. BUILDIN_DEF(callsub,"l*"),
  20342. BUILDIN_DEF(callfunc,"s*"),
  20343. BUILDIN_DEF(return,"?"),
  20344. BUILDIN_DEF(getarg,"i?"),
  20345. BUILDIN_DEF(jobchange,"i??"),
  20346. BUILDIN_DEF(jobname,"i"),
  20347. BUILDIN_DEF(input,"r??"),
  20348. BUILDIN_DEF(warp,"sii?"),
  20349. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  20350. BUILDIN_DEF(areawarp,"siiiisii??"),
  20351. BUILDIN_DEF(warpparty,"siii???"), // [Fredzilla] [Paradox924X]
  20352. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  20353. BUILDIN_DEF(setlook,"ii?"),
  20354. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  20355. BUILDIN_DEF2(setr,"set","rv?"),
  20356. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  20357. BUILDIN_DEF(setarray,"rv*"),
  20358. BUILDIN_DEF(cleararray,"rvi"),
  20359. BUILDIN_DEF(copyarray,"rri"),
  20360. BUILDIN_DEF(getarraysize,"r"),
  20361. BUILDIN_DEF(deletearray,"r?"),
  20362. BUILDIN_DEF(getelementofarray,"ri"),
  20363. BUILDIN_DEF(getitem,"vi?"),
  20364. BUILDIN_DEF(rentitem,"vi?"),
  20365. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  20366. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  20367. BUILDIN_DEF(getnameditem,"vv"),
  20368. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  20369. BUILDIN_DEF(makeitem,"visii"),
  20370. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  20371. BUILDIN_DEF(delitem,"vi?"),
  20372. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  20373. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  20374. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  20375. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  20376. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  20377. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  20378. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  20379. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  20380. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  20381. BUILDIN_DEF(cutin,"si"),
  20382. BUILDIN_DEF(viewpoint,"iiiii"),
  20383. BUILDIN_DEF(heal,"ii?"),
  20384. BUILDIN_DEF(itemheal,"ii?"),
  20385. BUILDIN_DEF(percentheal,"ii?"),
  20386. BUILDIN_DEF(rand,"i?"),
  20387. BUILDIN_DEF(countitem,"v?"),
  20388. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  20389. BUILDIN_DEF2(countitem,"guildstoragecountitem","v?"),
  20390. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  20391. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  20392. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  20393. BUILDIN_DEF2(countitem,"guildstoragecountitem2","viiiiiii?"),
  20394. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  20395. BUILDIN_DEF(checkweight,"vi*"),
  20396. BUILDIN_DEF(checkweight2,"rr"),
  20397. BUILDIN_DEF(readparam,"i?"),
  20398. BUILDIN_DEF(getcharid,"i?"),
  20399. BUILDIN_DEF(getnpcid,"i?"),
  20400. BUILDIN_DEF(getpartyname,"i"),
  20401. BUILDIN_DEF(getpartymember,"i??"),
  20402. BUILDIN_DEF(getpartyleader,"i?"),
  20403. BUILDIN_DEF(getguildname,"i"),
  20404. BUILDIN_DEF(getguildmaster,"i"),
  20405. BUILDIN_DEF(getguildmasterid,"i"),
  20406. BUILDIN_DEF(strcharinfo,"i?"),
  20407. BUILDIN_DEF(strnpcinfo,"i"),
  20408. BUILDIN_DEF(getequipid,"??"),
  20409. BUILDIN_DEF(getequipuniqueid,"i?"),
  20410. BUILDIN_DEF(getequipname,"i?"),
  20411. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  20412. BUILDIN_DEF(repair,"i?"), // [Valaris]
  20413. BUILDIN_DEF(repairall,"?"),
  20414. BUILDIN_DEF(getequipisequiped,"i?"),
  20415. BUILDIN_DEF(getequipisenableref,"i?"),
  20416. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  20417. BUILDIN_DEF(getequipweaponlv,"i?"),
  20418. BUILDIN_DEF(getequippercentrefinery,"i?"),
  20419. BUILDIN_DEF(successrefitem,"i??"),
  20420. BUILDIN_DEF(failedrefitem,"i?"),
  20421. BUILDIN_DEF(downrefitem,"i??"),
  20422. BUILDIN_DEF(statusup,"i?"),
  20423. BUILDIN_DEF(statusup2,"ii?"),
  20424. BUILDIN_DEF(bonus,"iv"),
  20425. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  20426. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  20427. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  20428. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  20429. BUILDIN_DEF(autobonus,"sii??"),
  20430. BUILDIN_DEF(autobonus2,"sii??"),
  20431. BUILDIN_DEF(autobonus3,"siiv?"),
  20432. BUILDIN_DEF(skill,"vi?"),
  20433. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  20434. BUILDIN_DEF(guildskill,"vi"),
  20435. BUILDIN_DEF(getskilllv,"v"),
  20436. BUILDIN_DEF(getgdskilllv,"iv"),
  20437. BUILDIN_DEF(basicskillcheck,""),
  20438. BUILDIN_DEF(getgmlevel,"?"),
  20439. BUILDIN_DEF(getgroupid,"?"),
  20440. BUILDIN_DEF(end,""),
  20441. BUILDIN_DEF(checkoption,"i?"),
  20442. BUILDIN_DEF(setoption,"i??"),
  20443. BUILDIN_DEF(setcart,"??"),
  20444. BUILDIN_DEF(checkcart,"?"),
  20445. BUILDIN_DEF(setfalcon,"??"),
  20446. BUILDIN_DEF(checkfalcon,"?"),
  20447. BUILDIN_DEF(setriding,"??"),
  20448. BUILDIN_DEF(checkriding,"?"),
  20449. BUILDIN_DEF(checkwug,"?"),
  20450. BUILDIN_DEF(checkmadogear,"?"),
  20451. BUILDIN_DEF(setmadogear,"??"),
  20452. BUILDIN_DEF2(savepoint,"save","sii???"),
  20453. BUILDIN_DEF(savepoint,"sii???"),
  20454. BUILDIN_DEF(gettimetick,"i"),
  20455. BUILDIN_DEF(gettime,"i"),
  20456. BUILDIN_DEF(gettimestr,"si"),
  20457. BUILDIN_DEF(openstorage,""),
  20458. BUILDIN_DEF(guildopenstorage,""),
  20459. BUILDIN_DEF(itemskill,"vi?"),
  20460. BUILDIN_DEF(produce,"i"),
  20461. BUILDIN_DEF(cooking,"i"),
  20462. BUILDIN_DEF(monster,"siisii???"),
  20463. BUILDIN_DEF(getmobdrops,"i"),
  20464. BUILDIN_DEF(areamonster,"siiiisii???"),
  20465. BUILDIN_DEF(killmonster,"ss?"),
  20466. BUILDIN_DEF(killmonsterall,"s?"),
  20467. BUILDIN_DEF(clone,"siisi????"),
  20468. BUILDIN_DEF(doevent,"s"),
  20469. BUILDIN_DEF(donpcevent,"s"),
  20470. BUILDIN_DEF(cmdothernpc,"ss"),
  20471. BUILDIN_DEF(addtimer,"is"),
  20472. BUILDIN_DEF(deltimer,"s"),
  20473. BUILDIN_DEF(addtimercount,"is"),
  20474. BUILDIN_DEF(initnpctimer,"??"),
  20475. BUILDIN_DEF(stopnpctimer,"??"),
  20476. BUILDIN_DEF(startnpctimer,"??"),
  20477. BUILDIN_DEF(setnpctimer,"i?"),
  20478. BUILDIN_DEF(getnpctimer,"i?"),
  20479. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  20480. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  20481. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  20482. BUILDIN_DEF(announce,"si?????"),
  20483. BUILDIN_DEF(mapannounce,"ssi?????"),
  20484. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  20485. BUILDIN_DEF(getusers,"i"),
  20486. BUILDIN_DEF(getmapguildusers,"si"),
  20487. BUILDIN_DEF(getmapusers,"s"),
  20488. BUILDIN_DEF(getareausers,"siiii"),
  20489. BUILDIN_DEF(getareadropitem,"siiiiv"),
  20490. BUILDIN_DEF(enablenpc,"s"),
  20491. BUILDIN_DEF(disablenpc,"s"),
  20492. BUILDIN_DEF(hideoffnpc,"s"),
  20493. BUILDIN_DEF(hideonnpc,"s"),
  20494. BUILDIN_DEF(sc_start,"iii???"),
  20495. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  20496. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  20497. BUILDIN_DEF(sc_end,"i?"),
  20498. BUILDIN_DEF(getstatus, "i??"),
  20499. BUILDIN_DEF(getscrate,"ii?"),
  20500. BUILDIN_DEF(debugmes,"s"),
  20501. BUILDIN_DEF2(catchpet,"pet","i"),
  20502. BUILDIN_DEF2(birthpet,"bpet",""),
  20503. BUILDIN_DEF(resetlvl,"i?"),
  20504. BUILDIN_DEF(resetstatus,"?"),
  20505. BUILDIN_DEF(resetskill,"?"),
  20506. BUILDIN_DEF(skillpointcount,"?"),
  20507. BUILDIN_DEF(changebase,"i?"),
  20508. BUILDIN_DEF(changesex,"?"),
  20509. BUILDIN_DEF(changecharsex,"?"),
  20510. BUILDIN_DEF(waitingroom,"si?????"),
  20511. BUILDIN_DEF(delwaitingroom,"?"),
  20512. BUILDIN_DEF(waitingroomkick,"ss"),
  20513. BUILDIN_DEF(getwaitingroomusers, "?"),
  20514. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  20515. BUILDIN_DEF(enablewaitingroomevent,"?"),
  20516. BUILDIN_DEF(disablewaitingroomevent,"?"),
  20517. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  20518. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  20519. BUILDIN_DEF(getwaitingroomstate,"i?"),
  20520. BUILDIN_DEF(warpwaitingpc,"sii?"),
  20521. BUILDIN_DEF(attachrid,"i"),
  20522. BUILDIN_DEF(addrid,"i?????"),
  20523. BUILDIN_DEF(detachrid,""),
  20524. BUILDIN_DEF(isloggedin,"i?"),
  20525. BUILDIN_DEF(setmapflagnosave,"ssii"),
  20526. BUILDIN_DEF(getmapflag,"si?"),
  20527. BUILDIN_DEF(setmapflag,"si??"),
  20528. BUILDIN_DEF(removemapflag,"si?"),
  20529. BUILDIN_DEF(pvpon,"s"),
  20530. BUILDIN_DEF(pvpoff,"s"),
  20531. BUILDIN_DEF(gvgon,"s"),
  20532. BUILDIN_DEF(gvgoff,"s"),
  20533. BUILDIN_DEF(emotion,"i??"),
  20534. BUILDIN_DEF(maprespawnguildid,"sii"),
  20535. BUILDIN_DEF(agitstart,""), // <Agit>
  20536. BUILDIN_DEF(agitend,""),
  20537. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  20538. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  20539. BUILDIN_DEF(getcastlename,"s"),
  20540. BUILDIN_DEF(getcastledata,"si"),
  20541. BUILDIN_DEF(setcastledata,"sii"),
  20542. BUILDIN_DEF(requestguildinfo,"i?"),
  20543. BUILDIN_DEF(getequipcardcnt,"i"),
  20544. BUILDIN_DEF(successremovecards,"i"),
  20545. BUILDIN_DEF(failedremovecards,"ii"),
  20546. BUILDIN_DEF(marriage,"s"),
  20547. BUILDIN_DEF2(wedding_effect,"wedding",""),
  20548. BUILDIN_DEF(divorce,"?"),
  20549. BUILDIN_DEF(ispartneron,"?"),
  20550. BUILDIN_DEF(getpartnerid,"?"),
  20551. BUILDIN_DEF(getchildid,"?"),
  20552. BUILDIN_DEF(getmotherid,"?"),
  20553. BUILDIN_DEF(getfatherid,"?"),
  20554. BUILDIN_DEF(warppartner,"sii"),
  20555. BUILDIN_DEF(getitemname,"v"),
  20556. BUILDIN_DEF(getitemslots,"i"),
  20557. BUILDIN_DEF(makepet,"i"),
  20558. BUILDIN_DEF(getexp,"ii?"),
  20559. BUILDIN_DEF(getinventorylist,"?"),
  20560. BUILDIN_DEF(getskilllist,"?"),
  20561. BUILDIN_DEF(clearitem,"?"),
  20562. BUILDIN_DEF(classchange,"i??"),
  20563. BUILDIN_DEF(misceffect,"i"),
  20564. BUILDIN_DEF(playBGM,"s"),
  20565. BUILDIN_DEF(playBGMall,"s?????"),
  20566. BUILDIN_DEF(soundeffect,"si"),
  20567. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  20568. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  20569. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  20570. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  20571. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  20572. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  20573. BUILDIN_DEF(petloot,"i"), // [Valaris]
  20574. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  20575. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  20576. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  20577. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  20578. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  20579. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  20580. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  20581. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  20582. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  20583. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  20584. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  20585. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  20586. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  20587. BUILDIN_DEF(npctalk,"s??"), // [Valaris]
  20588. BUILDIN_DEF(chatmes,"s?"), // [Jey]
  20589. BUILDIN_DEF(mobcount,"ss"),
  20590. BUILDIN_DEF(getlook,"i?"),
  20591. BUILDIN_DEF(getsavepoint,"i?"),
  20592. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  20593. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  20594. BUILDIN_DEF(npcstop,""), // [Valaris]
  20595. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  20596. BUILDIN_DEF(mapid2name,"i"),
  20597. BUILDIN_DEF(checkoption1,"i?"),
  20598. BUILDIN_DEF(checkoption2,"i?"),
  20599. BUILDIN_DEF(guildgetexp,"i"),
  20600. BUILDIN_DEF(guildchangegm,"is"),
  20601. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  20602. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  20603. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  20604. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  20605. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  20606. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  20607. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  20608. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  20609. BUILDIN_DEF(night,""), // sets the server to night time
  20610. BUILDIN_DEF(day,""), // sets the server to day time
  20611. #ifdef PCRE_SUPPORT
  20612. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  20613. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  20614. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  20615. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  20616. #endif
  20617. BUILDIN_DEF(preg_match,"ss?"),
  20618. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  20619. BUILDIN_DEF(recovery,"i???"),
  20620. BUILDIN_DEF(getpetinfo,"i?"),
  20621. BUILDIN_DEF(gethominfo,"i?"),
  20622. BUILDIN_DEF(getmercinfo,"i?"),
  20623. BUILDIN_DEF(checkequipedcard,"i"),
  20624. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  20625. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  20626. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  20627. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  20628. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  20629. BUILDIN_DEF(charat,"si"),
  20630. BUILDIN_DEF(setchar,"ssi"),
  20631. BUILDIN_DEF(insertchar,"ssi"),
  20632. BUILDIN_DEF(delchar,"si"),
  20633. BUILDIN_DEF(strtoupper,"s"),
  20634. BUILDIN_DEF(strtolower,"s"),
  20635. BUILDIN_DEF(charisupper, "si"),
  20636. BUILDIN_DEF(charislower, "si"),
  20637. BUILDIN_DEF(substr,"sii"),
  20638. BUILDIN_DEF(explode, "rss"),
  20639. BUILDIN_DEF(implode, "r?"),
  20640. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  20641. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  20642. BUILDIN_DEF(strpos,"ss?"),
  20643. BUILDIN_DEF(replacestr,"sss??"),
  20644. BUILDIN_DEF(countstr,"ss?"),
  20645. BUILDIN_DEF(setnpcdisplay,"sv??"),
  20646. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  20647. BUILDIN_DEF(strcmp,"ss"),
  20648. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  20649. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  20650. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  20651. // [zBuffer] List of mathematics commands --->
  20652. BUILDIN_DEF(sqrt,"i"),
  20653. BUILDIN_DEF(pow,"ii"),
  20654. BUILDIN_DEF(distance,"iiii"),
  20655. BUILDIN_DEF2(minmax,"min", "*"),
  20656. BUILDIN_DEF2(minmax,"minimum", "*"),
  20657. BUILDIN_DEF2(minmax,"max", "*"),
  20658. BUILDIN_DEF2(minmax,"maximum", "*"),
  20659. // <--- [zBuffer] List of mathematics commands
  20660. BUILDIN_DEF(md5,"s"),
  20661. // [zBuffer] List of dynamic var commands --->
  20662. BUILDIN_DEF(getd,"s"),
  20663. BUILDIN_DEF(setd,"sv?"),
  20664. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  20665. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  20666. BUILDIN_DEF(npcshopadditem,"sii*"),
  20667. BUILDIN_DEF(npcshopdelitem,"si*"),
  20668. BUILDIN_DEF(npcshopattach,"s?"),
  20669. BUILDIN_DEF(equip,"i?"),
  20670. BUILDIN_DEF(autoequip,"ii"),
  20671. BUILDIN_DEF(setbattleflag,"si?"),
  20672. BUILDIN_DEF(getbattleflag,"s"),
  20673. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  20674. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  20675. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  20676. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  20677. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  20678. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  20679. BUILDIN_DEF(axtoi,"s"),
  20680. BUILDIN_DEF(query_sql,"s*"),
  20681. BUILDIN_DEF(query_logsql,"s*"),
  20682. BUILDIN_DEF(escape_sql,"v"),
  20683. BUILDIN_DEF(atoi,"s"),
  20684. BUILDIN_DEF(strtol,"si"),
  20685. // [zBuffer] List of player cont commands --->
  20686. BUILDIN_DEF(rid2name,"i"),
  20687. BUILDIN_DEF(pcfollow,"ii"),
  20688. BUILDIN_DEF(pcstopfollow,"i"),
  20689. BUILDIN_DEF(pcblockmove,"ii"),
  20690. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  20691. BUILDIN_DEF(pcblockskill,"ii"),
  20692. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  20693. // <--- [zBuffer] List of player cont commands
  20694. // [zBuffer] List of unit control commands --->
  20695. BUILDIN_DEF(unitexists,"i"),
  20696. BUILDIN_DEF(getunittype,"i"),
  20697. BUILDIN_DEF(getunitname,"i"),
  20698. BUILDIN_DEF(setunitname,"is"),
  20699. BUILDIN_DEF(getunitdata,"i*"),
  20700. BUILDIN_DEF(setunitdata,"iii"),
  20701. BUILDIN_DEF(unitwalk,"iii?"),
  20702. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  20703. BUILDIN_DEF(unitkill,"i"),
  20704. BUILDIN_DEF(unitwarp,"isii"),
  20705. BUILDIN_DEF(unitattack,"iv?"),
  20706. BUILDIN_DEF(unitstopattack,"i"),
  20707. BUILDIN_DEF(unitstopwalk,"i?"),
  20708. BUILDIN_DEF(unittalk,"is?"),
  20709. BUILDIN_DEF(unitemote,"ii"),
  20710. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  20711. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  20712. // <--- [zBuffer] List of unit control commands
  20713. BUILDIN_DEF(sleep,"i"),
  20714. BUILDIN_DEF(sleep2,"i"),
  20715. BUILDIN_DEF(awake,"s"),
  20716. BUILDIN_DEF(getvariableofnpc,"rs"),
  20717. BUILDIN_DEF(warpportal,"iisii"),
  20718. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  20719. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  20720. BUILDIN_DEF(morphembryo,""),
  20721. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  20722. BUILDIN_DEF(checkhomcall,""),
  20723. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  20724. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  20725. BUILDIN_DEF(checkvending,"?"),
  20726. BUILDIN_DEF(checkchatting,"?"),
  20727. BUILDIN_DEF(checkidle,"?"),
  20728. BUILDIN_DEF(openmail,"?"),
  20729. BUILDIN_DEF(openauction,"?"),
  20730. BUILDIN_DEF(checkcell,"siii"),
  20731. BUILDIN_DEF(setcell,"siiiiii"),
  20732. BUILDIN_DEF(getfreecell,"srr?????"),
  20733. BUILDIN_DEF(setwall,"siiiiis"),
  20734. BUILDIN_DEF(delwall,"s"),
  20735. BUILDIN_DEF(searchitem,"rs"),
  20736. BUILDIN_DEF(mercenary_create,"ii"),
  20737. BUILDIN_DEF(mercenary_heal,"ii"),
  20738. BUILDIN_DEF(mercenary_sc_start,"iii"),
  20739. BUILDIN_DEF(mercenary_get_calls,"i"),
  20740. BUILDIN_DEF(mercenary_get_faith,"i"),
  20741. BUILDIN_DEF(mercenary_set_calls,"ii"),
  20742. BUILDIN_DEF(mercenary_set_faith,"ii"),
  20743. BUILDIN_DEF(readbook,"ii"),
  20744. BUILDIN_DEF(setfont,"i"),
  20745. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  20746. BUILDIN_DEF(progressbar,"si"),
  20747. BUILDIN_DEF(progressbar_npc, "si?"),
  20748. BUILDIN_DEF(pushpc,"ii"),
  20749. BUILDIN_DEF(buyingstore,"i"),
  20750. BUILDIN_DEF(searchstores,"ii"),
  20751. BUILDIN_DEF(showdigit,"i?"),
  20752. // WoE SE
  20753. BUILDIN_DEF(agitstart2,""),
  20754. BUILDIN_DEF(agitend2,""),
  20755. BUILDIN_DEF(agitcheck2,""),
  20756. // BattleGround
  20757. BUILDIN_DEF(waitingroom2bg,"sii???"),
  20758. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  20759. BUILDIN_DEF(bg_team_setxy,"iii"),
  20760. BUILDIN_DEF(bg_warp,"isii"),
  20761. BUILDIN_DEF(bg_monster,"isiisi?"),
  20762. BUILDIN_DEF(bg_monster_set_team,"ii"),
  20763. BUILDIN_DEF(bg_leave,"?"),
  20764. BUILDIN_DEF(bg_destroy,"i"),
  20765. BUILDIN_DEF(areapercentheal,"siiiiii"),
  20766. BUILDIN_DEF(bg_get_data,"ii"),
  20767. BUILDIN_DEF(bg_getareausers,"isiiii"),
  20768. BUILDIN_DEF(bg_updatescore,"sii"),
  20769. BUILDIN_DEF(bg_join,"i????"),
  20770. BUILDIN_DEF(bg_create,"sii??"),
  20771. // Instancing
  20772. BUILDIN_DEF(instance_create,"s??"),
  20773. BUILDIN_DEF(instance_destroy,"?"),
  20774. BUILDIN_DEF(instance_id,""),
  20775. BUILDIN_DEF(instance_enter,"s????"),
  20776. BUILDIN_DEF(instance_npcname,"s?"),
  20777. BUILDIN_DEF(instance_mapname,"s?"),
  20778. BUILDIN_DEF(instance_warpall,"sii?"),
  20779. BUILDIN_DEF(instance_announce,"isi?????"),
  20780. BUILDIN_DEF(instance_check_party,"i???"),
  20781. BUILDIN_DEF(instance_check_guild,"i???"),
  20782. BUILDIN_DEF(instance_check_clan,"i???"),
  20783. BUILDIN_DEF(instance_info,"si?"),
  20784. /**
  20785. * 3rd-related
  20786. **/
  20787. BUILDIN_DEF(makerune,"i?"),
  20788. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  20789. BUILDIN_DEF(setdragon,"??"),//[Ind]
  20790. BUILDIN_DEF(ismounting,"?"),//[Ind]
  20791. BUILDIN_DEF(setmounting,"?"),//[Ind]
  20792. BUILDIN_DEF(checkre,"i"),
  20793. /**
  20794. * rAthena and beyond!
  20795. **/
  20796. BUILDIN_DEF(getargcount,""),
  20797. BUILDIN_DEF(getcharip,"?"),
  20798. BUILDIN_DEF(is_function,"s"),
  20799. BUILDIN_DEF(get_revision,""),
  20800. BUILDIN_DEF(get_githash,""),
  20801. BUILDIN_DEF(freeloop,"?"),
  20802. BUILDIN_DEF(getrandgroupitem,"i??"),
  20803. BUILDIN_DEF(cleanmap,"s"),
  20804. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  20805. BUILDIN_DEF(npcskill,"viii"),
  20806. BUILDIN_DEF(consumeitem,"v?"),
  20807. BUILDIN_DEF(delequip,"i?"),
  20808. BUILDIN_DEF(breakequip,"i?"),
  20809. BUILDIN_DEF(sit,"?"),
  20810. BUILDIN_DEF(stand,"?"),
  20811. //@commands (script based)
  20812. BUILDIN_DEF(bindatcmd, "ss??"),
  20813. BUILDIN_DEF(unbindatcmd, "s"),
  20814. BUILDIN_DEF(useatcmd, "s"),
  20815. //Quest Log System [Inkfish]
  20816. BUILDIN_DEF(questinfo, "ii??"),
  20817. BUILDIN_DEF(setquest, "i?"),
  20818. BUILDIN_DEF(erasequest, "i?"),
  20819. BUILDIN_DEF(completequest, "i?"),
  20820. BUILDIN_DEF(checkquest, "i??"),
  20821. BUILDIN_DEF(isbegin_quest,"i?"),
  20822. BUILDIN_DEF(changequest, "ii?"),
  20823. BUILDIN_DEF(showevent, "i??"),
  20824. //Bound items [Xantara] & [Akinari]
  20825. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  20826. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  20827. BUILDIN_DEF(countbound, "??"),
  20828. // Party related
  20829. BUILDIN_DEF(party_create,"s???"),
  20830. BUILDIN_DEF(party_addmember,"ii"),
  20831. BUILDIN_DEF(party_delmember,"??"),
  20832. BUILDIN_DEF(party_changeleader,"ii"),
  20833. BUILDIN_DEF(party_changeoption,"iii"),
  20834. BUILDIN_DEF(party_destroy,"i"),
  20835. // Clan system
  20836. BUILDIN_DEF(clan_join,"i?"),
  20837. BUILDIN_DEF(clan_leave,"?"),
  20838. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  20839. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  20840. BUILDIN_DEF(vip_status,"i?"),
  20841. BUILDIN_DEF(vip_time,"i?"),
  20842. BUILDIN_DEF(bonus_script,"si????"),
  20843. BUILDIN_DEF(bonus_script_clear,"??"),
  20844. BUILDIN_DEF(getgroupitem,"i?"),
  20845. BUILDIN_DEF(enable_command,""),
  20846. BUILDIN_DEF(disable_command,""),
  20847. BUILDIN_DEF(getguildmember,"i??"),
  20848. BUILDIN_DEF(addspiritball,"ii?"),
  20849. BUILDIN_DEF(delspiritball,"i?"),
  20850. BUILDIN_DEF(countspiritball,"?"),
  20851. BUILDIN_DEF(mergeitem,"?"),
  20852. BUILDIN_DEF(mergeitem2,"??"),
  20853. BUILDIN_DEF(npcshopupdate,"sii?"),
  20854. BUILDIN_DEF(getattachedrid,""),
  20855. BUILDIN_DEF(getvar,"vi"),
  20856. BUILDIN_DEF(showscript,"s?"),
  20857. BUILDIN_DEF(ignoretimeout,"i?"),
  20858. BUILDIN_DEF(geteleminfo,"i?"),
  20859. BUILDIN_DEF(setquestinfo_level,"iii"),
  20860. BUILDIN_DEF(setquestinfo_req,"iii*"),
  20861. BUILDIN_DEF(setquestinfo_job,"ii*"),
  20862. BUILDIN_DEF(opendressroom,"i?"),
  20863. BUILDIN_DEF(navigateto,"s???????"),
  20864. BUILDIN_DEF(getguildalliance,"ii"),
  20865. BUILDIN_DEF(adopt,"vv"),
  20866. BUILDIN_DEF(getexp2,"ii?"),
  20867. BUILDIN_DEF(recalculatestat,""),
  20868. BUILDIN_DEF(hateffect,"ii"),
  20869. BUILDIN_DEF(getrandomoptinfo, "i"),
  20870. BUILDIN_DEF(getequiprandomoption, "iii?"),
  20871. BUILDIN_DEF(setrandomoption,"iiiii?"),
  20872. BUILDIN_DEF(needed_status_point,"ii?"),
  20873. BUILDIN_DEF(jobcanentermap,"s?"),
  20874. BUILDIN_DEF(openstorage2,"ii?"),
  20875. BUILDIN_DEF(unloadnpc, "s"),
  20876. // WoE TE
  20877. BUILDIN_DEF(agitstart3,""),
  20878. BUILDIN_DEF(agitend3,""),
  20879. BUILDIN_DEF(agitcheck3,""),
  20880. BUILDIN_DEF(gvgon3,"s"),
  20881. BUILDIN_DEF(gvgoff3,"s"),
  20882. // Channel System
  20883. BUILDIN_DEF(channel_create,"ss?????"),
  20884. BUILDIN_DEF(channel_setopt,"sii"),
  20885. BUILDIN_DEF(channel_setcolor,"si"),
  20886. BUILDIN_DEF(channel_setpass,"ss"),
  20887. BUILDIN_DEF(channel_setgroup,"si*"),
  20888. BUILDIN_DEF2(channel_setgroup,"channel_setgroup2","sr"),
  20889. BUILDIN_DEF(channel_chat,"ss?"),
  20890. BUILDIN_DEF(channel_ban,"si"),
  20891. BUILDIN_DEF(channel_unban,"si"),
  20892. BUILDIN_DEF(channel_kick,"sv"),
  20893. BUILDIN_DEF(channel_delete,"s"),
  20894. // Item Random Option Extension [Cydh]
  20895. BUILDIN_DEF2(getitem2,"getitem3","viiiiiiiirrr?"),
  20896. BUILDIN_DEF2(getitem2,"getitembound3","viiiiiiiiirrr?"),
  20897. BUILDIN_DEF2(rentitem2,"rentitem3","viiiiiiiirrr?"),
  20898. BUILDIN_DEF2(makeitem2,"makeitem3","visiiiiiiiiirrr"),
  20899. BUILDIN_DEF2(delitem2,"delitem3","viiiiiiiirrr?"),
  20900. BUILDIN_DEF2(countitem,"countitem3","viiiiiiirrr?"),
  20901. // Achievement System
  20902. BUILDIN_DEF(achievementinfo,"ii?"),
  20903. BUILDIN_DEF(achievementadd,"i?"),
  20904. BUILDIN_DEF(achievementremove,"i?"),
  20905. BUILDIN_DEF(achievementcomplete,"i?"),
  20906. BUILDIN_DEF(achievementexists,"i?"),
  20907. #include "../custom/script_def.inc"
  20908. {NULL,NULL,NULL},
  20909. };