script.c 604 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643
  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 "clif.h"
  30. #include "chrif.h"
  31. #include "itemdb.h"
  32. #include "pc.h"
  33. #include "storage.h"
  34. #include "pet.h"
  35. #include "mapreg.h"
  36. #include "homunculus.h"
  37. #include "instance.h"
  38. #include "mercenary.h"
  39. #include "intif.h"
  40. #include "chat.h"
  41. #include "battleground.h"
  42. #include "party.h"
  43. #include "mail.h"
  44. #include "quest.h"
  45. #include "elemental.h"
  46. #include <math.h>
  47. #ifndef WIN32
  48. #endif
  49. #include <setjmp.h>
  50. #include <errno.h>
  51. TBL_PC *script_rid2sd(struct script_state *st);
  52. /**
  53. * Get `sd` from a account id in `loc` param instead of attached rid
  54. * @param st Script
  55. * @param loc Location to look account id in script parameter
  56. * @param sd Variable that will be assigned
  57. * @return True if `sd` is assigned, false otherwise
  58. **/
  59. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  60. if (script_hasdata(st, loc)) {
  61. int id_ = script_getnum(st, loc);
  62. if (!(*sd = map_id2sd(id_)))
  63. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  64. }
  65. else
  66. *sd = script_rid2sd(st);
  67. return (*sd) ? true : false;
  68. }
  69. /**
  70. * Get `sd` from a char id in `loc` param instead of attached rid
  71. * @param st Script
  72. * @param loc Location to look char id in script parameter
  73. * @param sd Variable that will be assigned
  74. * @return True if `sd` is assigned, false otherwise
  75. **/
  76. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  77. if (script_hasdata(st, loc)) {
  78. int id_ = script_getnum(st, loc);
  79. if (!(*sd = map_charid2sd(id_)))
  80. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  81. }
  82. else
  83. *sd = script_rid2sd(st);
  84. return (*sd) ? true : false;
  85. }
  86. /**
  87. * Get `sd` from a nick in `loc` param instead of attached rid
  88. * @param st Script
  89. * @param loc Location to look nick in script parameter
  90. * @param sd Variable that will be assigned
  91. * @return True if `sd` is assigned, false otherwise
  92. **/
  93. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  94. if (script_hasdata(st, loc)) {
  95. const char *name_ = script_getstr(st, loc);
  96. if (!(*sd = map_nick2sd(name_)))
  97. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  98. }
  99. else
  100. *sd = script_rid2sd(st);
  101. return (*sd) ? true : false;
  102. }
  103. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  104. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  105. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  106. /// temporary buffer for passing around compiled bytecode
  107. /// @see add_scriptb, set_label, parse_script
  108. static unsigned char* script_buf = NULL;
  109. static int script_pos = 0, script_size = 0;
  110. static inline int GETVALUE(const unsigned char* buf, int i)
  111. {
  112. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  113. }
  114. static inline void SETVALUE(unsigned char* buf, int i, int n)
  115. {
  116. buf[i] = GetByte(n, 0);
  117. buf[i+1] = GetByte(n, 1);
  118. buf[i+2] = GetByte(n, 2);
  119. }
  120. // String buffer structures.
  121. // str_data stores string information
  122. static struct str_data_struct {
  123. enum c_op type;
  124. int str;
  125. int backpatch;
  126. int label;
  127. int (*func)(struct script_state *st);
  128. int val;
  129. int next;
  130. } *str_data = NULL;
  131. static int str_data_size = 0; // size of the data
  132. static int str_num = LABEL_START; // next id to be assigned
  133. // str_buf holds the strings themselves
  134. static char *str_buf;
  135. static int str_size = 0; // size of the buffer
  136. static int str_pos = 0; // next position to be assigned
  137. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  138. #define SCRIPT_HASH_SIZE 1021
  139. int str_hash[SCRIPT_HASH_SIZE];
  140. // Specifies which string hashing method to use
  141. //#define SCRIPT_HASH_DJB2
  142. //#define SCRIPT_HASH_SDBM
  143. #define SCRIPT_HASH_ELF
  144. static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
  145. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  146. static int parse_options = 0;
  147. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  148. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  149. // important buildin function references for usage in scripts
  150. static int buildin_set_ref = 0;
  151. static int buildin_callsub_ref = 0;
  152. static int buildin_callfunc_ref = 0;
  153. static int buildin_getelementofarray_ref = 0;
  154. // Caches compiled autoscript item code.
  155. // Note: This is not cleared when reloading itemdb.
  156. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
  157. struct Script_Config script_config = {
  158. 1, // warn_func_mismatch_argtypes
  159. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  160. 0, INT_MAX, // input_min_value/input_max_value
  161. "OnPCDieEvent", //die_event_name
  162. "OnPCKillEvent", //kill_pc_event_name
  163. "OnNPCKillEvent", //kill_mob_event_name
  164. "OnPCLoginEvent", //login_event_name
  165. "OnPCLogoutEvent", //logout_event_name
  166. "OnPCLoadMapEvent", //loadmap_event_name
  167. "OnPCBaseLvUpEvent", //baselvup_event_name
  168. "OnPCJobLvUpEvent", //joblvup_event_name
  169. "OnPCStatCalcEvent", //stat_calc_event_name
  170. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  171. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  172. };
  173. static jmp_buf error_jump;
  174. static char* error_msg;
  175. static const char* error_pos;
  176. static int error_report; // if the error should produce output
  177. // Used by disp_warning_message
  178. static const char* parser_current_src;
  179. static const char* parser_current_file;
  180. static int parser_current_line;
  181. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  182. // [Eoe / jA 1080, 1081, 1094, 1164]
  183. enum curly_type {
  184. TYPE_NULL = 0,
  185. TYPE_IF,
  186. TYPE_SWITCH,
  187. TYPE_WHILE,
  188. TYPE_FOR,
  189. TYPE_DO,
  190. TYPE_USERFUNC,
  191. TYPE_ARGLIST // function argument list
  192. };
  193. enum e_arglist
  194. {
  195. ARGLIST_UNDEFINED = 0,
  196. ARGLIST_NO_PAREN = 1,
  197. ARGLIST_PAREN = 2,
  198. };
  199. static struct {
  200. struct {
  201. enum curly_type type;
  202. int index;
  203. int count;
  204. int flag;
  205. struct linkdb_node *case_label;
  206. } curly[256]; // Information right parenthesis
  207. int curly_count; // The number of right brackets
  208. int index; // Number of the syntax used in the script
  209. } syntax;
  210. const char* parse_curly_close(const char* p);
  211. const char* parse_syntax_close(const char* p);
  212. const char* parse_syntax_close_sub(const char* p,int* flag);
  213. const char* parse_syntax(const char* p);
  214. static int parse_syntax_for_flag = 0;
  215. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  216. extern unsigned int current_equip_combo_pos;
  217. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  218. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  219. int potion_target = 0;
  220. unsigned int *generic_ui_array = NULL;
  221. unsigned int generic_ui_array_size = 0;
  222. c_op get_com(unsigned char *script,int *pos);
  223. int get_num(unsigned char *script,int *pos);
  224. typedef struct script_function {
  225. int (*func)(struct script_state *st);
  226. const char *name;
  227. const char *arg;
  228. } script_function;
  229. extern script_function buildin_func[];
  230. #ifdef BETA_THREAD_TEST
  231. /**
  232. * MySQL Query Slave
  233. **/
  234. static SPIN_LOCK queryThreadLock;
  235. static rAthread queryThread = NULL;
  236. static ramutex queryThreadMutex = NULL;
  237. static racond queryThreadCond = NULL;
  238. static volatile int32 queryThreadTerminate = 0;
  239. struct queryThreadEntry {
  240. bool ok;
  241. bool type; /* main db or log db? */
  242. struct script_state *st;
  243. };
  244. /* Ladies and Gentleman the Manager! */
  245. struct {
  246. struct queryThreadEntry **entry;/* array of structs */
  247. int count;
  248. int timer;/* used to receive processed entries */
  249. } queryThreadData;
  250. #endif
  251. /*==========================================
  252. * (Only those needed) local declaration prototype
  253. *------------------------------------------*/
  254. const char* parse_subexpr(const char* p,int limit);
  255. int run_func(struct script_state *st);
  256. unsigned short script_instancegetid(struct script_state *st);
  257. enum {
  258. MF_NOMEMO, //0
  259. MF_NOTELEPORT,
  260. MF_NOSAVE,
  261. MF_NOBRANCH,
  262. MF_NOPENALTY,
  263. MF_NOZENYPENALTY,
  264. MF_PVP,
  265. MF_PVP_NOPARTY,
  266. MF_PVP_NOGUILD,
  267. MF_GVG,
  268. MF_GVG_NOPARTY, //10
  269. MF_NOTRADE,
  270. MF_NOSKILL,
  271. MF_NOWARP,
  272. MF_PARTYLOCK,
  273. MF_NOICEWALL,
  274. MF_SNOW,
  275. MF_FOG,
  276. MF_SAKURA,
  277. MF_LEAVES,
  278. /**
  279. * No longer available, keeping here just in case it's back someday. [Ind]
  280. **/
  281. //MF_RAIN, //20
  282. // 21 free
  283. MF_NOGO = 22,
  284. MF_CLOUDS,
  285. MF_CLOUDS2,
  286. MF_FIREWORKS,
  287. MF_GVG_CASTLE,
  288. MF_GVG_DUNGEON,
  289. MF_NIGHTENABLED,
  290. MF_NOBASEEXP,
  291. MF_NOJOBEXP, //30
  292. MF_NOMOBLOOT,
  293. MF_NOMVPLOOT,
  294. MF_NORETURN,
  295. MF_NOWARPTO,
  296. MF_NIGHTMAREDROP,
  297. MF_RESTRICTED,
  298. MF_NOCOMMAND,
  299. MF_NODROP,
  300. MF_JEXP,
  301. MF_BEXP, //40
  302. MF_NOVENDING,
  303. MF_LOADEVENT,
  304. MF_NOCHAT,
  305. MF_NOEXPPENALTY,
  306. MF_GUILDLOCK,
  307. MF_TOWN,
  308. MF_AUTOTRADE,
  309. MF_ALLOWKS,
  310. MF_MONSTER_NOTELEPORT,
  311. MF_PVP_NOCALCRANK, //50
  312. MF_BATTLEGROUND,
  313. MF_RESET,
  314. MF_CHANNELAUTOJOIN,
  315. MF_NOUSECART,
  316. MF_NOITEMCONSUMPTION,
  317. MF_SUMSTARTMIRACLE,
  318. MF_NOMINEEFFECT,
  319. MF_NOLOCKON,
  320. MF_NOTOMB,
  321. MF_SKILL_DAMAGE, //60
  322. MF_NOCOSTUME,
  323. MF_GVG_TE_CASTLE,
  324. MF_GVG_TE,
  325. };
  326. const char* script_op2name(int op)
  327. {
  328. #define RETURN_OP_NAME(type) case type: return #type
  329. switch( op )
  330. {
  331. RETURN_OP_NAME(C_NOP);
  332. RETURN_OP_NAME(C_POS);
  333. RETURN_OP_NAME(C_INT);
  334. RETURN_OP_NAME(C_PARAM);
  335. RETURN_OP_NAME(C_FUNC);
  336. RETURN_OP_NAME(C_STR);
  337. RETURN_OP_NAME(C_CONSTSTR);
  338. RETURN_OP_NAME(C_ARG);
  339. RETURN_OP_NAME(C_NAME);
  340. RETURN_OP_NAME(C_EOL);
  341. RETURN_OP_NAME(C_RETINFO);
  342. RETURN_OP_NAME(C_USERFUNC);
  343. RETURN_OP_NAME(C_USERFUNC_POS);
  344. RETURN_OP_NAME(C_REF);
  345. // operators
  346. RETURN_OP_NAME(C_OP3);
  347. RETURN_OP_NAME(C_LOR);
  348. RETURN_OP_NAME(C_LAND);
  349. RETURN_OP_NAME(C_LE);
  350. RETURN_OP_NAME(C_LT);
  351. RETURN_OP_NAME(C_GE);
  352. RETURN_OP_NAME(C_GT);
  353. RETURN_OP_NAME(C_EQ);
  354. RETURN_OP_NAME(C_NE);
  355. RETURN_OP_NAME(C_XOR);
  356. RETURN_OP_NAME(C_OR);
  357. RETURN_OP_NAME(C_AND);
  358. RETURN_OP_NAME(C_ADD);
  359. RETURN_OP_NAME(C_SUB);
  360. RETURN_OP_NAME(C_MUL);
  361. RETURN_OP_NAME(C_DIV);
  362. RETURN_OP_NAME(C_MOD);
  363. RETURN_OP_NAME(C_NEG);
  364. RETURN_OP_NAME(C_LNOT);
  365. RETURN_OP_NAME(C_NOT);
  366. RETURN_OP_NAME(C_R_SHIFT);
  367. RETURN_OP_NAME(C_L_SHIFT);
  368. RETURN_OP_NAME(C_ADD_POST);
  369. RETURN_OP_NAME(C_SUB_POST);
  370. RETURN_OP_NAME(C_ADD_PRE);
  371. RETURN_OP_NAME(C_SUB_PRE);
  372. default:
  373. ShowDebug("script_op2name: unexpected op=%d\n", op);
  374. return "???";
  375. }
  376. #undef RETURN_OP_NAME
  377. }
  378. #ifdef DEBUG_DUMP_STACK
  379. static void script_dump_stack(struct script_state* st)
  380. {
  381. int i;
  382. ShowMessage("\tstart = %d\n", st->start);
  383. ShowMessage("\tend = %d\n", st->end);
  384. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  385. ShowMessage("\tsp = %d\n", st->stack->sp);
  386. for( i = 0; i < st->stack->sp; ++i )
  387. {
  388. struct script_data* data = &st->stack->stack_data[i];
  389. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  390. switch( data->type )
  391. {
  392. case C_INT:
  393. case C_POS:
  394. ShowMessage(" %d\n", data->u.num);
  395. break;
  396. case C_STR:
  397. case C_CONSTSTR:
  398. ShowMessage(" \"%s\"\n", data->u.str);
  399. break;
  400. case C_NAME:
  401. 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));
  402. break;
  403. case C_RETINFO:
  404. {
  405. struct script_retinfo* ri = data->u.ri;
  406. 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);
  407. }
  408. break;
  409. default:
  410. ShowMessage("\n");
  411. break;
  412. }
  413. }
  414. }
  415. #endif
  416. /// Reports on the console the src of a script error.
  417. static void script_reportsrc(struct script_state *st)
  418. {
  419. struct block_list* bl;
  420. if( st->oid == 0 )
  421. return; //Can't report source.
  422. bl = map_id2bl(st->oid);
  423. if( bl == NULL )
  424. return;
  425. switch( bl->type ) {
  426. case BL_NPC:
  427. if( bl->m >= 0 )
  428. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  429. else
  430. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  431. break;
  432. default:
  433. if( bl->m >= 0 )
  434. 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);
  435. else
  436. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  437. break;
  438. }
  439. }
  440. /// Reports on the console information about the script data.
  441. static void script_reportdata(struct script_data* data)
  442. {
  443. if( data == NULL )
  444. return;
  445. switch( data->type ) {
  446. case C_NOP:// no value
  447. ShowDebug("Data: nothing (nil)\n");
  448. break;
  449. case C_INT:// number
  450. ShowDebug("Data: number value=%"PRId64"\n", data->u.num);
  451. break;
  452. case C_STR:
  453. case C_CONSTSTR:// string
  454. if( data->u.str ) {
  455. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  456. } else {
  457. ShowDebug("Data: string value=NULL\n");
  458. }
  459. break;
  460. case C_NAME:// reference
  461. if( reference_tovariable(data) ) {// variable
  462. const char* name = reference_getname(data);
  463. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  464. } else if( reference_toconstant(data) ) {// constant
  465. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  466. } else if( reference_toparam(data) ) {// param
  467. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  468. } else {// ???
  469. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  470. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  471. }
  472. break;
  473. case C_POS:// label
  474. ShowDebug("Data: label pos=%"PRId64"\n", data->u.num);
  475. break;
  476. default:
  477. ShowDebug("Data: %s\n", script_op2name(data->type));
  478. break;
  479. }
  480. }
  481. /// Reports on the console information about the current built-in function.
  482. static void script_reportfunc(struct script_state* st)
  483. {
  484. int params, id;
  485. struct script_data* data;
  486. if( !script_hasdata(st,0) )
  487. {// no stack
  488. return;
  489. }
  490. data = script_getdata(st,0);
  491. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  492. {// script currently not executing a built-in function or corrupt stack
  493. return;
  494. }
  495. id = reference_getid(data);
  496. params = script_lastdata(st)-1;
  497. if( params > 0 )
  498. {
  499. int i;
  500. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  501. for( i = 2; i <= script_lastdata(st); i++ )
  502. {
  503. script_reportdata(script_getdata(st,i));
  504. }
  505. }
  506. else
  507. {
  508. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  509. }
  510. }
  511. /*==========================================
  512. * Output error message
  513. *------------------------------------------*/
  514. static void disp_error_message2(const char *mes,const char *pos,int report)
  515. {
  516. error_msg = aStrdup(mes);
  517. error_pos = pos;
  518. error_report = report;
  519. longjmp( error_jump, 1 );
  520. }
  521. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  522. static void disp_warning_message(const char *mes, const char *pos) {
  523. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  524. }
  525. /// Checks event parameter validity
  526. static void check_event(struct script_state *st, const char *evt)
  527. {
  528. if( evt && evt[0] && !stristr(evt, "::On") )
  529. {
  530. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  531. script_reportsrc(st);
  532. }
  533. }
  534. /*==========================================
  535. * Hashes the input string
  536. *------------------------------------------*/
  537. static unsigned int calc_hash(const char* p)
  538. {
  539. unsigned int h;
  540. #if defined(SCRIPT_HASH_DJB2)
  541. h = 5381;
  542. while( *p ) // hash*33 + c
  543. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  544. #elif defined(SCRIPT_HASH_SDBM)
  545. h = 0;
  546. while( *p ) // hash*65599 + c
  547. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  548. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  549. h = 0;
  550. while( *p ){
  551. unsigned int g;
  552. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  553. g = h & 0xF0000000;
  554. if( g )
  555. {
  556. h ^= g >> 24;
  557. h &= ~g;
  558. }
  559. }
  560. #else // athena hash
  561. h = 0;
  562. while( *p )
  563. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  564. #endif
  565. return h % SCRIPT_HASH_SIZE;
  566. }
  567. /*==========================================
  568. * str_data manipulation functions
  569. *------------------------------------------*/
  570. /// Looks up string using the provided id.
  571. const char* get_str(int id)
  572. {
  573. Assert( id >= LABEL_START && id < str_size );
  574. return str_buf+str_data[id].str;
  575. }
  576. /// Returns the uid of the string, or -1.
  577. static int search_str(const char* p)
  578. {
  579. int i;
  580. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  581. if( strcasecmp(get_str(i),p) == 0 )
  582. return i;
  583. return -1;
  584. }
  585. /// Stores a copy of the string and returns its id.
  586. /// If an identical string is already present, returns its id instead.
  587. int add_str(const char* p)
  588. {
  589. int h;
  590. int len;
  591. h = calc_hash(p);
  592. if( str_hash[h] == 0 )
  593. {// empty bucket, add new node here
  594. str_hash[h] = str_num;
  595. }
  596. else
  597. {// scan for end of list, or occurence of identical string
  598. int i;
  599. for( i = str_hash[h]; ; i = str_data[i].next )
  600. {
  601. if( strcasecmp(get_str(i),p) == 0 )
  602. return i; // string already in list
  603. if( str_data[i].next == 0 )
  604. break; // reached the end
  605. }
  606. // append node to end of list
  607. str_data[i].next = str_num;
  608. }
  609. // grow list if neccessary
  610. if( str_num >= str_data_size )
  611. {
  612. str_data_size += 128;
  613. RECREATE(str_data,struct str_data_struct,str_data_size);
  614. memset(str_data + (str_data_size - 128), '\0', 128);
  615. }
  616. len=(int)strlen(p);
  617. // grow string buffer if neccessary
  618. while( str_pos+len+1 >= str_size )
  619. {
  620. str_size += 256;
  621. RECREATE(str_buf,char,str_size);
  622. memset(str_buf + (str_size - 256), '\0', 256);
  623. }
  624. safestrncpy(str_buf+str_pos, p, len+1);
  625. str_data[str_num].type = C_NOP;
  626. str_data[str_num].str = str_pos;
  627. str_data[str_num].next = 0;
  628. str_data[str_num].func = NULL;
  629. str_data[str_num].backpatch = -1;
  630. str_data[str_num].label = -1;
  631. str_pos += len+1;
  632. return str_num++;
  633. }
  634. /// Appends 1 byte to the script buffer.
  635. static void add_scriptb(int a)
  636. {
  637. if( script_pos+1 >= script_size )
  638. {
  639. script_size += SCRIPT_BLOCK_SIZE;
  640. RECREATE(script_buf,unsigned char,script_size);
  641. }
  642. script_buf[script_pos++] = (uint8)(a);
  643. }
  644. /// Appends a c_op value to the script buffer.
  645. /// The value is variable-length encoded into 8-bit blocks.
  646. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  647. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  648. static void add_scriptc(int a)
  649. {
  650. while( a >= 0x40 )
  651. {
  652. add_scriptb((a&0x3f)|0x40);
  653. a = (a - 0x40) >> 6;
  654. }
  655. add_scriptb(a);
  656. }
  657. /// Appends an integer value to the script buffer.
  658. /// The value is variable-length encoded into 8-bit blocks.
  659. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  660. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  661. static void add_scripti(int a)
  662. {
  663. while( a >= 0x40 )
  664. {
  665. add_scriptb((a&0x3f)|0xc0);
  666. a = (a - 0x40) >> 6;
  667. }
  668. add_scriptb(a|0x80);
  669. }
  670. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  671. ///
  672. /// @param l The id of the str_data entry
  673. // Maximum up to 16M
  674. static void add_scriptl(int l)
  675. {
  676. int backpatch = str_data[l].backpatch;
  677. switch(str_data[l].type){
  678. case C_POS:
  679. case C_USERFUNC_POS:
  680. add_scriptc(C_POS);
  681. add_scriptb(str_data[l].label);
  682. add_scriptb(str_data[l].label>>8);
  683. add_scriptb(str_data[l].label>>16);
  684. break;
  685. case C_NOP:
  686. case C_USERFUNC:
  687. // Embedded data backpatch there is a possibility of label
  688. add_scriptc(C_NAME);
  689. str_data[l].backpatch = script_pos;
  690. add_scriptb(backpatch);
  691. add_scriptb(backpatch>>8);
  692. add_scriptb(backpatch>>16);
  693. break;
  694. case C_INT:
  695. add_scripti(abs(str_data[l].val));
  696. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  697. add_scriptc(C_NEG);
  698. break;
  699. default: // assume C_NAME
  700. add_scriptc(C_NAME);
  701. add_scriptb(l);
  702. add_scriptb(l>>8);
  703. add_scriptb(l>>16);
  704. break;
  705. }
  706. }
  707. /*==========================================
  708. * Resolve the label
  709. *------------------------------------------*/
  710. void set_label(int l,int pos, const char* script_pos_cur)
  711. {
  712. int i;
  713. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  714. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  715. disp_error_message("set_label: invalid label name",script_pos_cur);
  716. return;
  717. }
  718. if(str_data[l].label!=-1){
  719. disp_error_message("set_label: dup label ",script_pos_cur);
  720. return;
  721. }
  722. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  723. str_data[l].label=pos;
  724. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  725. int next=GETVALUE(script_buf,i);
  726. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  727. SETVALUE(script_buf,i,pos);
  728. i=next;
  729. }
  730. }
  731. /// Skips spaces and/or comments.
  732. const char* skip_space(const char* p)
  733. {
  734. if( p == NULL )
  735. return NULL;
  736. for(;;)
  737. {
  738. while( ISSPACE(*p) )
  739. ++p;
  740. if( *p == '/' && p[1] == '/' )
  741. {// line comment
  742. while(*p && *p!='\n')
  743. ++p;
  744. }
  745. else if( *p == '/' && p[1] == '*' )
  746. {// block comment
  747. p += 2;
  748. for(;;)
  749. {
  750. if( *p == '\0' ) {
  751. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  752. return p;
  753. }
  754. if( *p == '*' && p[1] == '/' )
  755. {// end of block comment
  756. p += 2;
  757. break;
  758. }
  759. ++p;
  760. }
  761. }
  762. else
  763. break;
  764. }
  765. return p;
  766. }
  767. /// Skips a word.
  768. /// A word consists of undercores and/or alphanumeric characters,
  769. /// and valid variable prefixes/postfixes.
  770. static const char* skip_word(const char* p)
  771. {
  772. // prefix
  773. switch( *p ) {
  774. case '@':// temporary char variable
  775. ++p; break;
  776. case '#':// account variable
  777. p += ( p[1] == '#' ? 2 : 1 ); break;
  778. case '\'':// instance variable
  779. ++p; break;
  780. case '.':// npc variable
  781. p += ( p[1] == '@' ? 2 : 1 ); break;
  782. case '$':// global variable
  783. p += ( p[1] == '@' ? 2 : 1 ); break;
  784. }
  785. while( ISALNUM(*p) || *p == '_' )
  786. ++p;
  787. // postfix
  788. if( *p == '$' )// string
  789. p++;
  790. return p;
  791. }
  792. /// Adds a word to str_data.
  793. /// @see skip_word
  794. /// @see add_str
  795. static int add_word(const char* p)
  796. {
  797. char* word;
  798. int len;
  799. int i;
  800. // Check for a word
  801. len = skip_word(p) - p;
  802. if( len == 0 )
  803. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  804. // Duplicate the word
  805. word = (char*)aMalloc(len+1);
  806. memcpy(word, p, len);
  807. word[len] = 0;
  808. // add the word
  809. i = add_str(word);
  810. aFree(word);
  811. return i;
  812. }
  813. /// Parses a function call.
  814. /// The argument list can have parenthesis or not.
  815. /// The number of arguments is checked.
  816. static
  817. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  818. {
  819. const char* p2;
  820. const char* arg=NULL;
  821. int func;
  822. func = add_word(p);
  823. if( str_data[func].type == C_FUNC ){
  824. // buildin function
  825. add_scriptl(func);
  826. add_scriptc(C_ARG);
  827. arg = buildin_func[str_data[func].val].arg;
  828. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  829. // script defined function
  830. add_scriptl(buildin_callsub_ref);
  831. add_scriptc(C_ARG);
  832. add_scriptl(func);
  833. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  834. if( *arg == 0 )
  835. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  836. if( *arg != '*' )
  837. ++arg; // count func as argument
  838. } else {
  839. #ifdef SCRIPT_CALLFUNC_CHECK
  840. const char* name = get_str(func);
  841. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  842. #endif
  843. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  844. #ifdef SCRIPT_CALLFUNC_CHECK
  845. } else {;
  846. add_scriptl(buildin_callfunc_ref);
  847. add_scriptc(C_ARG);
  848. add_scriptc(C_STR);
  849. while( *name ) add_scriptb(*name ++);
  850. add_scriptb(0);
  851. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  852. if( *arg != '*' ) ++ arg;
  853. }
  854. #endif
  855. }
  856. p = skip_word(p);
  857. p = skip_space(p);
  858. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  859. syntax.curly[syntax.curly_count].count = 0;
  860. if( *p == ';' )
  861. {// <func name> ';'
  862. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  863. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  864. {// <func name> '(' ')'
  865. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  866. p = p2;
  867. /*
  868. } else if( 0 && require_paren && *p != '(' )
  869. {// <func name>
  870. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  871. */
  872. } else
  873. {// <func name> <arg list>
  874. if( require_paren ){
  875. if( *p != '(' )
  876. disp_error_message("need '('",p);
  877. ++p; // skip '('
  878. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  879. } else if( *p == '(' ){
  880. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  881. } else {
  882. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  883. }
  884. ++syntax.curly_count;
  885. while( *arg ) {
  886. p2=parse_subexpr(p,-1);
  887. if( p == p2 )
  888. break; // not an argument
  889. if( *arg != '*' )
  890. ++arg; // next argument
  891. p=skip_space(p2);
  892. if( *arg == 0 || *p != ',' )
  893. break; // no more arguments
  894. ++p; // skip comma
  895. }
  896. --syntax.curly_count;
  897. }
  898. if( *arg && *arg != '?' && *arg != '*' )
  899. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  900. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  901. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  902. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  903. if( *p != ')' )
  904. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  905. ++p;
  906. }
  907. add_scriptc(C_FUNC);
  908. return p;
  909. }
  910. /// Processes end of logical script line.
  911. /// @param first When true, only fix up scheduling data is initialized
  912. /// @param p Script position for error reporting in set_label
  913. static void parse_nextline(bool first, const char* p)
  914. {
  915. if( !first )
  916. {
  917. add_scriptc(C_EOL); // mark end of line for stack cleanup
  918. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  919. }
  920. // initialize data for new '-' label fix up scheduling
  921. str_data[LABEL_NEXTLINE].type = C_NOP;
  922. str_data[LABEL_NEXTLINE].backpatch = -1;
  923. str_data[LABEL_NEXTLINE].label = -1;
  924. }
  925. /**
  926. * Pushes a variable into stack, processing its array index if needed.
  927. * @see parse_variable
  928. */
  929. void parse_variable_sub_push(int word, const char *p2)
  930. {
  931. if( p2 ) { // Process the variable index
  932. const char *p3 = NULL;
  933. // Push the getelementofarray method into the stack
  934. add_scriptl(buildin_getelementofarray_ref);
  935. add_scriptc(C_ARG);
  936. add_scriptl(word);
  937. // Process the sub-expression for this assignment
  938. p3 = parse_subexpr(p2 + 1, 1);
  939. p3 = skip_space(p3);
  940. if( *p3 != ']' ) // Closing parenthesis is required for this script
  941. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  942. // Push the closing function stack operator onto the stack
  943. add_scriptc(C_FUNC);
  944. p3++;
  945. } else // No array index, simply push the variable or value onto the stack
  946. add_scriptl(word);
  947. }
  948. /// Parse a variable assignment using the direct equals operator
  949. /// @param p script position where the function should run from
  950. /// @return NULL if not a variable assignment, the new position otherwise
  951. const char* parse_variable(const char* p) {
  952. int word;
  953. c_op type = C_NOP;
  954. const char *p2 = NULL;
  955. const char *var = p;
  956. if ((p[0] == '+' && p[1] == '+' && (type = C_ADD_PRE)) // pre ++
  957. || (p[0] == '-' && p[1] == '-' && (type = C_SUB_PRE))) // pre --
  958. var = p = skip_space(&p[2]);
  959. // skip the variable where applicable
  960. p = skip_word(p);
  961. p = skip_space(p);
  962. if( p == NULL ) {// end of the line or invalid buffer
  963. return NULL;
  964. }
  965. if( *p == '[' ) {// array variable so process the array as appropriate
  966. int i,j;
  967. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  968. if( *p ++ == ']' && --(j) == 0 ) break;
  969. if( *p == '[' ) ++ j;
  970. }
  971. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  972. disp_error_message("Missing right expression or closing bracket for variable.", p);
  973. }
  974. }
  975. if( type == C_NOP &&
  976. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  977. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  978. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  979. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  980. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  981. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  982. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  983. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  984. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  985. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  986. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_POST) ) // post ++
  987. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_POST) ) // post --
  988. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  989. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  990. ) )
  991. {// failed to find a matching operator combination so invalid
  992. return NULL;
  993. }
  994. switch( type ) {
  995. case C_ADD_PRE: // pre ++
  996. case C_SUB_PRE: // pre --
  997. // Nothing more to skip
  998. break;
  999. case C_EQ: {// incremental modifier
  1000. p = skip_space( &p[1] );
  1001. }
  1002. break;
  1003. case C_L_SHIFT:
  1004. case C_R_SHIFT: {// left or right shift modifier
  1005. p = skip_space( &p[3] );
  1006. }
  1007. break;
  1008. default: {// normal incremental command
  1009. p = skip_space( &p[2] );
  1010. }
  1011. }
  1012. if( p == NULL ) {// end of line or invalid buffer
  1013. return NULL;
  1014. }
  1015. // push the set function onto the stack
  1016. add_scriptl(buildin_set_ref);
  1017. add_scriptc(C_ARG);
  1018. // always append parenthesis to avoid errors
  1019. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1020. syntax.curly[syntax.curly_count].count = 0;
  1021. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1022. // increment the total curly count for the position in the script
  1023. ++ syntax.curly_count;
  1024. // parse the variable currently being modified
  1025. word = add_word(var);
  1026. 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
  1027. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1028. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1029. if( type != C_EQ )
  1030. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1031. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1032. add_scripti(1);
  1033. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1034. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1035. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1036. add_scripti(1);
  1037. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1038. } else { // Process the value as an expression
  1039. p = parse_subexpr(p, -1);
  1040. if( type != C_EQ )
  1041. {// push the type of modifier onto the stack
  1042. add_scriptc(type);
  1043. }
  1044. }
  1045. // decrement the curly count for the position within the script
  1046. -- syntax.curly_count;
  1047. // close the script by appending the function operator
  1048. add_scriptc(C_FUNC);
  1049. // push the buffer from the method
  1050. return p;
  1051. }
  1052. /*
  1053. * Checks whether the gives string is a number literal
  1054. *
  1055. * Mainly necessary to differentiate between number literals and NPC name
  1056. * constants, since several of those start with a digit.
  1057. *
  1058. * All this does is to check if the string begins with an optional + or - sign,
  1059. * followed by a hexadecimal or decimal number literal literal and is NOT
  1060. * followed by a underscore or letter.
  1061. *
  1062. * @author : Hercules.ws
  1063. * @param p Pointer to the string to check
  1064. * @return Whether the string is a number literal
  1065. */
  1066. bool is_number(const char *p) {
  1067. const char *np;
  1068. if (!p)
  1069. return false;
  1070. if (*p == '-' || *p == '+')
  1071. p++;
  1072. np = p;
  1073. if (*p == '0' && p[1] == 'x') {
  1074. p+=2;
  1075. np = p;
  1076. // Hexadecimal
  1077. while (ISXDIGIT(*np))
  1078. np++;
  1079. } else {
  1080. // Decimal
  1081. while (ISDIGIT(*np))
  1082. np++;
  1083. }
  1084. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1085. return true;
  1086. return false;
  1087. }
  1088. /*==========================================
  1089. * Analysis section
  1090. *------------------------------------------*/
  1091. const char* parse_simpleexpr(const char *p)
  1092. {
  1093. long long i;
  1094. p=skip_space(p);
  1095. if(*p==';' || *p==',')
  1096. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1097. if(*p=='('){
  1098. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1099. ++syntax.curly[i].count;
  1100. p=parse_subexpr(p+1,-1);
  1101. p=skip_space(p);
  1102. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1103. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1104. ){
  1105. if( *p == ',' ){
  1106. syntax.curly[i].flag = ARGLIST_PAREN;
  1107. return p;
  1108. } else
  1109. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1110. }
  1111. if( *p != ')' )
  1112. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1113. ++p;
  1114. } else if(is_number(p)) {
  1115. char *np;
  1116. while(*p == '0' && ISDIGIT(p[1])) p++;
  1117. i=strtoll(p,&np,0);
  1118. if( i < INT_MIN ) {
  1119. i = INT_MIN;
  1120. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1121. } else if( i > INT_MAX ) {
  1122. i = INT_MAX;
  1123. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1124. }
  1125. add_scripti((int)i);
  1126. p=np;
  1127. } else if(*p=='"'){
  1128. add_scriptc(C_STR);
  1129. p++;
  1130. while( *p && *p != '"' ){
  1131. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1132. {
  1133. char buf[8];
  1134. size_t len = skip_escaped_c(p) - p;
  1135. size_t n = sv_unescape_c(buf, p, len);
  1136. if( n != 1 )
  1137. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1138. p += len;
  1139. add_scriptb(*buf);
  1140. continue;
  1141. }
  1142. else if( *p == '\n' )
  1143. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1144. add_scriptb(*p++);
  1145. }
  1146. if(!*p)
  1147. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1148. add_scriptb(0);
  1149. p++; //'"'
  1150. } else {
  1151. int l;
  1152. const char* pv;
  1153. // label , register , function etc
  1154. if(skip_word(p)==p)
  1155. disp_error_message("parse_simpleexpr: unexpected character",p);
  1156. l=add_word(p);
  1157. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1158. return parse_callfunc(p,1,0);
  1159. #ifdef SCRIPT_CALLFUNC_CHECK
  1160. else {
  1161. const char* name = get_str(l);
  1162. if( strdb_get(userfunc_db,name) != NULL ) {
  1163. return parse_callfunc(p,1,1);
  1164. }
  1165. }
  1166. #endif
  1167. if( (pv = parse_variable(p)) )
  1168. {// successfully processed a variable assignment
  1169. return pv;
  1170. }
  1171. p=skip_word(p);
  1172. if( *p == '[' ){
  1173. // array(name[i] => getelementofarray(name,i) )
  1174. add_scriptl(buildin_getelementofarray_ref);
  1175. add_scriptc(C_ARG);
  1176. add_scriptl(l);
  1177. p=parse_subexpr(p+1,-1);
  1178. p=skip_space(p);
  1179. if( *p != ']' )
  1180. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1181. ++p;
  1182. add_scriptc(C_FUNC);
  1183. }else
  1184. add_scriptl(l);
  1185. }
  1186. return p;
  1187. }
  1188. /*==========================================
  1189. * Analysis of the expression
  1190. *------------------------------------------*/
  1191. const char* parse_subexpr(const char* p,int limit)
  1192. {
  1193. int op,opl,len;
  1194. p=skip_space(p);
  1195. if( *p == '-' ){
  1196. const char* tmpp = skip_space(p+1);
  1197. if( *tmpp == ';' || *tmpp == ',' ){
  1198. add_scriptl(LABEL_NEXTLINE);
  1199. p++;
  1200. return p;
  1201. }
  1202. }
  1203. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1204. p = parse_variable(p);
  1205. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1206. p = parse_subexpr(p + 1, 11);
  1207. add_scriptc(op);
  1208. } else
  1209. p = parse_simpleexpr(p);
  1210. p = skip_space(p);
  1211. while((
  1212. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1213. (op=C_ADD,opl=9,len=1,*p=='+') ||
  1214. (op=C_SUB,opl=9,len=1,*p=='-') ||
  1215. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1216. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1217. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1218. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1219. (op=C_AND,opl=5,len=1,*p=='&') ||
  1220. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1221. (op=C_OR,opl=3,len=1,*p=='|') ||
  1222. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1223. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1224. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1225. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1226. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1227. (op=C_GT,opl=7,len=1,*p=='>') ||
  1228. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1229. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1230. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1231. p+=len;
  1232. if(op == C_OP3) {
  1233. p=parse_subexpr(p,-1);
  1234. p=skip_space(p);
  1235. if( *(p++) != ':')
  1236. disp_error_message("parse_subexpr: expected ':'", p-1);
  1237. p=parse_subexpr(p,-1);
  1238. } else {
  1239. p=parse_subexpr(p,opl);
  1240. }
  1241. add_scriptc(op);
  1242. p=skip_space(p);
  1243. }
  1244. return p; /* return first untreated operator */
  1245. }
  1246. /*==========================================
  1247. * Evaluation of the expression
  1248. *------------------------------------------*/
  1249. const char* parse_expr(const char *p)
  1250. {
  1251. switch(*p){
  1252. case ')': case ';': case ':': case '[': case ']':
  1253. case '}':
  1254. disp_error_message("parse_expr: unexpected character",p);
  1255. }
  1256. p=parse_subexpr(p,-1);
  1257. return p;
  1258. }
  1259. /*==========================================
  1260. * Analysis of the line
  1261. *------------------------------------------*/
  1262. const char* parse_line(const char* p)
  1263. {
  1264. const char* p2;
  1265. p=skip_space(p);
  1266. if(*p==';') {
  1267. //Close decision for if(); for(); while();
  1268. p = parse_syntax_close(p + 1);
  1269. return p;
  1270. }
  1271. if(*p==')' && parse_syntax_for_flag)
  1272. return p+1;
  1273. p = skip_space(p);
  1274. if(p[0] == '{') {
  1275. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1276. syntax.curly[syntax.curly_count].count = -1;
  1277. syntax.curly[syntax.curly_count].index = -1;
  1278. syntax.curly_count++;
  1279. return p + 1;
  1280. } else if(p[0] == '}') {
  1281. return parse_curly_close(p);
  1282. }
  1283. // Syntax-related processing
  1284. p2 = parse_syntax(p);
  1285. if(p2 != NULL)
  1286. return p2;
  1287. // attempt to process a variable assignment
  1288. p2 = parse_variable(p);
  1289. if( p2 != NULL )
  1290. {// variable assignment processed so leave the method
  1291. return parse_syntax_close(p2 + 1);
  1292. }
  1293. p = parse_callfunc(p,0,0);
  1294. p = skip_space(p);
  1295. if(parse_syntax_for_flag) {
  1296. if( *p != ')' )
  1297. disp_error_message("parse_line: expected ')'",p);
  1298. } else {
  1299. if( *p != ';' )
  1300. disp_error_message("parse_line: expected ';'",p);
  1301. }
  1302. //Binding decision for if(), for(), while()
  1303. p = parse_syntax_close(p+1);
  1304. return p;
  1305. }
  1306. // { ... } Closing process
  1307. const char* parse_curly_close(const char* p)
  1308. {
  1309. if(syntax.curly_count <= 0) {
  1310. disp_error_message("parse_curly_close: unexpected string",p);
  1311. return p + 1;
  1312. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1313. syntax.curly_count--;
  1314. //Close decision if, for , while
  1315. p = parse_syntax_close(p + 1);
  1316. return p;
  1317. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1318. //Closing switch()
  1319. int pos = syntax.curly_count-1;
  1320. char label[256];
  1321. int l;
  1322. // Remove temporary variables
  1323. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1324. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1325. parse_line(label);
  1326. syntax.curly_count--;
  1327. // Go to the end pointer unconditionally
  1328. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1329. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1330. parse_line(label);
  1331. syntax.curly_count--;
  1332. // You are here labeled
  1333. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1334. l=add_str(label);
  1335. set_label(l,script_pos, p);
  1336. if(syntax.curly[pos].flag) {
  1337. //Exists default
  1338. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1339. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1340. parse_line(label);
  1341. syntax.curly_count--;
  1342. }
  1343. // Label end
  1344. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1345. l=add_str(label);
  1346. set_label(l,script_pos, p);
  1347. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1348. syntax.curly_count--;
  1349. //Closing decision if, for , while
  1350. p = parse_syntax_close(p + 1);
  1351. return p;
  1352. } else {
  1353. disp_error_message("parse_curly_close: unexpected string",p);
  1354. return p + 1;
  1355. }
  1356. }
  1357. // Syntax-related processing
  1358. // break, case, continue, default, do, for, function,
  1359. // if, switch, while ? will handle this internally.
  1360. const char* parse_syntax(const char* p)
  1361. {
  1362. const char *p2 = skip_word(p);
  1363. switch(*p) {
  1364. case 'B':
  1365. case 'b':
  1366. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1367. // break Processing
  1368. char label[256];
  1369. int pos = syntax.curly_count - 1;
  1370. while(pos >= 0) {
  1371. if(syntax.curly[pos].type == TYPE_DO) {
  1372. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1373. break;
  1374. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1375. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1376. break;
  1377. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1378. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1379. break;
  1380. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1381. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1382. break;
  1383. }
  1384. pos--;
  1385. }
  1386. if(pos < 0) {
  1387. disp_error_message("parse_syntax: unexpected 'break'",p);
  1388. } else {
  1389. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1390. parse_line(label);
  1391. syntax.curly_count--;
  1392. }
  1393. p = skip_space(p2);
  1394. if(*p != ';')
  1395. disp_error_message("parse_syntax: expected ';'",p);
  1396. // Closing decision if, for , while
  1397. p = parse_syntax_close(p + 1);
  1398. return p;
  1399. }
  1400. break;
  1401. case 'c':
  1402. case 'C':
  1403. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1404. //Processing case
  1405. int pos = syntax.curly_count-1;
  1406. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1407. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1408. return p+1;
  1409. } else {
  1410. char label[256];
  1411. int l,v;
  1412. char *np;
  1413. if(syntax.curly[pos].count != 1) {
  1414. //Jump for FALLTHRU
  1415. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1416. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1417. parse_line(label);
  1418. syntax.curly_count--;
  1419. // You are here labeled
  1420. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1421. l=add_str(label);
  1422. set_label(l,script_pos, p);
  1423. }
  1424. //Decision statement switch
  1425. p = skip_space(p2);
  1426. if(p == p2) {
  1427. disp_error_message("parse_syntax: expected a space ' '",p);
  1428. }
  1429. // check whether case label is integer or not
  1430. if(is_number(p)) {
  1431. //Numeric value
  1432. v = (int)strtol(p,&np,0);
  1433. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1434. p++;
  1435. p = skip_word(p);
  1436. if(np != p)
  1437. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1438. } else {
  1439. //Check for constants
  1440. p2 = skip_word(p);
  1441. v = (int)(size_t) (p2-p); // length of word at p2
  1442. memcpy(label,p,v);
  1443. label[v]='\0';
  1444. if( !script_get_constant(label, &v) )
  1445. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1446. p = skip_word(p);
  1447. }
  1448. p = skip_space(p);
  1449. if(*p != ':')
  1450. disp_error_message("parse_syntax: expect ':'",p);
  1451. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1452. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1453. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1454. // Bad I do not parse twice
  1455. p2 = parse_line(label);
  1456. parse_line(p2);
  1457. syntax.curly_count--;
  1458. if(syntax.curly[pos].count != 1) {
  1459. // Label after the completion of FALLTHRU
  1460. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1461. l=add_str(label);
  1462. set_label(l,script_pos,p);
  1463. }
  1464. // check duplication of case label [Rayce]
  1465. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1466. disp_error_message("parse_syntax: dup 'case'",p);
  1467. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1468. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1469. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1470. parse_line(label);
  1471. syntax.curly_count--;
  1472. syntax.curly[pos].count++;
  1473. }
  1474. return p + 1;
  1475. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1476. // Processing continue
  1477. char label[256];
  1478. int pos = syntax.curly_count - 1;
  1479. while(pos >= 0) {
  1480. if(syntax.curly[pos].type == TYPE_DO) {
  1481. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1482. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1483. break;
  1484. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1485. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1486. break;
  1487. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1488. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1489. break;
  1490. }
  1491. pos--;
  1492. }
  1493. if(pos < 0) {
  1494. disp_error_message("parse_syntax: unexpected 'continue'",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 'd':
  1509. case 'D':
  1510. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1511. // Switch - default processing
  1512. int pos = syntax.curly_count-1;
  1513. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1514. disp_error_message("parse_syntax: unexpected 'default'",p);
  1515. } else if(syntax.curly[pos].flag) {
  1516. disp_error_message("parse_syntax: dup 'default'",p);
  1517. } else {
  1518. char label[256];
  1519. int l;
  1520. // Put the label location
  1521. p = skip_space(p2);
  1522. if(*p != ':') {
  1523. disp_error_message("parse_syntax: expected ':'",p);
  1524. }
  1525. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1526. l=add_str(label);
  1527. set_label(l,script_pos,p);
  1528. // Skip to the next link w/o condition
  1529. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1530. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1531. parse_line(label);
  1532. syntax.curly_count--;
  1533. // The default label
  1534. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1535. l=add_str(label);
  1536. set_label(l,script_pos,p);
  1537. syntax.curly[syntax.curly_count - 1].flag = 1;
  1538. syntax.curly[pos].count++;
  1539. }
  1540. return p + 1;
  1541. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1542. int l;
  1543. char label[256];
  1544. p=skip_space(p2);
  1545. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1546. syntax.curly[syntax.curly_count].count = 1;
  1547. syntax.curly[syntax.curly_count].index = syntax.index++;
  1548. syntax.curly[syntax.curly_count].flag = 0;
  1549. // Label of the (do) form here
  1550. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1551. l=add_str(label);
  1552. set_label(l,script_pos,p);
  1553. syntax.curly_count++;
  1554. return p;
  1555. }
  1556. break;
  1557. case 'f':
  1558. case 'F':
  1559. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1560. int l;
  1561. char label[256];
  1562. int pos = syntax.curly_count;
  1563. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1564. syntax.curly[syntax.curly_count].count = 1;
  1565. syntax.curly[syntax.curly_count].index = syntax.index++;
  1566. syntax.curly[syntax.curly_count].flag = 0;
  1567. syntax.curly_count++;
  1568. p=skip_space(p2);
  1569. if(*p != '(')
  1570. disp_error_message("parse_syntax: expected '('",p);
  1571. p++;
  1572. // Execute the initialization statement
  1573. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1574. p=parse_line(p);
  1575. syntax.curly_count--;
  1576. // Form the start of label decision
  1577. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1578. l=add_str(label);
  1579. set_label(l,script_pos,p);
  1580. p=skip_space(p);
  1581. if(*p == ';') {
  1582. // For (; Because the pattern of always true ;)
  1583. ;
  1584. } else {
  1585. // Skip to the end point if the condition is false
  1586. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1587. add_scriptl(add_str("jump_zero"));
  1588. add_scriptc(C_ARG);
  1589. p=parse_expr(p);
  1590. p=skip_space(p);
  1591. add_scriptl(add_str(label));
  1592. add_scriptc(C_FUNC);
  1593. }
  1594. if(*p != ';')
  1595. disp_error_message("parse_syntax: expected ';'",p);
  1596. p++;
  1597. // Skip to the beginning of the loop
  1598. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1599. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1600. parse_line(label);
  1601. syntax.curly_count--;
  1602. // Labels to form the next loop
  1603. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1604. l=add_str(label);
  1605. set_label(l,script_pos,p);
  1606. // Process the next time you enter the loop
  1607. // A ')' last for; flag to be treated as'
  1608. parse_syntax_for_flag = 1;
  1609. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1610. p=parse_line(p);
  1611. syntax.curly_count--;
  1612. parse_syntax_for_flag = 0;
  1613. // Skip to the determination process conditions
  1614. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1615. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1616. parse_line(label);
  1617. syntax.curly_count--;
  1618. // Loop start labeling
  1619. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1620. l=add_str(label);
  1621. set_label(l,script_pos,p);
  1622. return p;
  1623. }
  1624. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1625. {// internal script function
  1626. const char *func_name;
  1627. func_name = skip_space(p2);
  1628. p = skip_word(func_name);
  1629. if( p == func_name )
  1630. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1631. p2 = skip_space(p);
  1632. if( *p2 == ';' )
  1633. {// function <name> ;
  1634. // function declaration - just register the name
  1635. int l;
  1636. l = add_word(func_name);
  1637. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1638. str_data[l].type = C_USERFUNC;
  1639. else if( str_data[l].type == C_USERFUNC )
  1640. ; // already registered
  1641. else
  1642. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1643. // Close condition of if, for, while
  1644. p = parse_syntax_close(p2 + 1);
  1645. return p;
  1646. }
  1647. else if(*p2 == '{')
  1648. {// function <name> <line/block of code>
  1649. char label[256];
  1650. int l;
  1651. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1652. syntax.curly[syntax.curly_count].count = 1;
  1653. syntax.curly[syntax.curly_count].index = syntax.index++;
  1654. syntax.curly[syntax.curly_count].flag = 0;
  1655. ++syntax.curly_count;
  1656. // Jump over the function code
  1657. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1658. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1659. ++syntax.curly_count;
  1660. parse_line(label);
  1661. --syntax.curly_count;
  1662. // Set the position of the function (label)
  1663. l=add_word(func_name);
  1664. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1665. {
  1666. str_data[l].type = C_USERFUNC;
  1667. set_label(l, script_pos, p);
  1668. if( parse_options&SCRIPT_USE_LABEL_DB )
  1669. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1670. }
  1671. else
  1672. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1673. return skip_space(p);
  1674. }
  1675. else
  1676. {
  1677. disp_error_message("expect ';' or '{' at function syntax",p);
  1678. }
  1679. }
  1680. break;
  1681. case 'i':
  1682. case 'I':
  1683. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1684. // If process
  1685. char label[256];
  1686. p=skip_space(p2);
  1687. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1688. disp_error_message("need '('",p);
  1689. }
  1690. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1691. syntax.curly[syntax.curly_count].count = 1;
  1692. syntax.curly[syntax.curly_count].index = syntax.index++;
  1693. syntax.curly[syntax.curly_count].flag = 0;
  1694. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1695. syntax.curly_count++;
  1696. add_scriptl(add_str("jump_zero"));
  1697. add_scriptc(C_ARG);
  1698. p=parse_expr(p);
  1699. p=skip_space(p);
  1700. add_scriptl(add_str(label));
  1701. add_scriptc(C_FUNC);
  1702. return p;
  1703. }
  1704. break;
  1705. case 's':
  1706. case 'S':
  1707. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1708. // Processing of switch ()
  1709. char label[256];
  1710. p=skip_space(p2);
  1711. if(*p != '(') {
  1712. disp_error_message("need '('",p);
  1713. }
  1714. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1715. syntax.curly[syntax.curly_count].count = 1;
  1716. syntax.curly[syntax.curly_count].index = syntax.index++;
  1717. syntax.curly[syntax.curly_count].flag = 0;
  1718. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1719. syntax.curly_count++;
  1720. add_scriptl(add_str("set"));
  1721. add_scriptc(C_ARG);
  1722. add_scriptl(add_str(label));
  1723. p=parse_expr(p);
  1724. p=skip_space(p);
  1725. if(*p != '{') {
  1726. disp_error_message("parse_syntax: expected '{'",p);
  1727. }
  1728. add_scriptc(C_FUNC);
  1729. return p + 1;
  1730. }
  1731. break;
  1732. case 'w':
  1733. case 'W':
  1734. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1735. int l;
  1736. char label[256];
  1737. p=skip_space(p2);
  1738. if(*p != '(') {
  1739. disp_error_message("need '('",p);
  1740. }
  1741. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1742. syntax.curly[syntax.curly_count].count = 1;
  1743. syntax.curly[syntax.curly_count].index = syntax.index++;
  1744. syntax.curly[syntax.curly_count].flag = 0;
  1745. // Form the start of label decision
  1746. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1747. l=add_str(label);
  1748. set_label(l,script_pos,p);
  1749. // Skip to the end point if the condition is false
  1750. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1751. syntax.curly_count++;
  1752. add_scriptl(add_str("jump_zero"));
  1753. add_scriptc(C_ARG);
  1754. p=parse_expr(p);
  1755. p=skip_space(p);
  1756. add_scriptl(add_str(label));
  1757. add_scriptc(C_FUNC);
  1758. return p;
  1759. }
  1760. break;
  1761. }
  1762. return NULL;
  1763. }
  1764. const char* parse_syntax_close(const char *p) {
  1765. // If (...) for (...) hoge (); as to make sure closed closed once again
  1766. int flag;
  1767. do {
  1768. p = parse_syntax_close_sub(p,&flag);
  1769. } while(flag);
  1770. return p;
  1771. }
  1772. // Close judgment if, for, while, of do
  1773. // flag == 1 : closed
  1774. // flag == 0 : not closed
  1775. const char* parse_syntax_close_sub(const char* p,int* flag)
  1776. {
  1777. char label[256];
  1778. int pos = syntax.curly_count - 1;
  1779. int l;
  1780. *flag = 1;
  1781. if(syntax.curly_count <= 0) {
  1782. *flag = 0;
  1783. return p;
  1784. } else if(syntax.curly[pos].type == TYPE_IF) {
  1785. const char *bp = p;
  1786. const char *p2;
  1787. // if-block and else-block end is a new line
  1788. parse_nextline(false, p);
  1789. // Skip to the last location if
  1790. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1791. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1792. parse_line(label);
  1793. syntax.curly_count--;
  1794. // Put the label of the location
  1795. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1796. l=add_str(label);
  1797. set_label(l,script_pos,p);
  1798. syntax.curly[pos].count++;
  1799. p = skip_space(p);
  1800. p2 = skip_word(p);
  1801. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1802. // else or else - if
  1803. p = skip_space(p2);
  1804. p2 = skip_word(p);
  1805. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1806. // else - if
  1807. p=skip_space(p2);
  1808. if(*p != '(') {
  1809. disp_error_message("need '('",p);
  1810. }
  1811. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1812. add_scriptl(add_str("jump_zero"));
  1813. add_scriptc(C_ARG);
  1814. p=parse_expr(p);
  1815. p=skip_space(p);
  1816. add_scriptl(add_str(label));
  1817. add_scriptc(C_FUNC);
  1818. *flag = 0;
  1819. return p;
  1820. } else {
  1821. // else
  1822. if(!syntax.curly[pos].flag) {
  1823. syntax.curly[pos].flag = 1;
  1824. *flag = 0;
  1825. return p;
  1826. }
  1827. }
  1828. }
  1829. // Close if
  1830. syntax.curly_count--;
  1831. // Put the label of the final location
  1832. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1833. l=add_str(label);
  1834. set_label(l,script_pos,p);
  1835. if(syntax.curly[pos].flag == 1) {
  1836. // Because the position of the pointer is the same if not else for this
  1837. return bp;
  1838. }
  1839. return p;
  1840. } else if(syntax.curly[pos].type == TYPE_DO) {
  1841. int l2;
  1842. char label2[256];
  1843. const char *p2;
  1844. if(syntax.curly[pos].flag) {
  1845. // (Come here continue) to form the label here
  1846. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1847. l2=add_str(label2);
  1848. set_label(l2,script_pos,p);
  1849. }
  1850. // Skip to the end point if the condition is false
  1851. p = skip_space(p);
  1852. p2 = skip_word(p);
  1853. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1854. disp_error_message("parse_syntax: expected 'while'",p);
  1855. p = skip_space(p2);
  1856. if(*p != '(') {
  1857. disp_error_message("need '('",p);
  1858. }
  1859. // do-block end is a new line
  1860. parse_nextline(false, p);
  1861. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1862. add_scriptl(add_str("jump_zero"));
  1863. add_scriptc(C_ARG);
  1864. p=parse_expr(p);
  1865. p=skip_space(p);
  1866. add_scriptl(add_str(label2));
  1867. add_scriptc(C_FUNC);
  1868. // Skip to the starting point
  1869. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1870. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1871. parse_line(label2);
  1872. syntax.curly_count--;
  1873. // Form label of the end point conditions
  1874. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1875. l2=add_str(label2);
  1876. set_label(l2,script_pos,p);
  1877. p = skip_space(p);
  1878. if(*p != ';') {
  1879. disp_error_message("parse_syntax: expected ';'",p);
  1880. return p+1;
  1881. }
  1882. p++;
  1883. syntax.curly_count--;
  1884. return p;
  1885. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1886. // for-block end is a new line
  1887. parse_nextline(false, p);
  1888. // Skip to the next loop
  1889. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1890. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1891. parse_line(label);
  1892. syntax.curly_count--;
  1893. // End for labeling
  1894. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1895. l=add_str(label);
  1896. set_label(l,script_pos,p);
  1897. syntax.curly_count--;
  1898. return p;
  1899. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1900. // while-block end is a new line
  1901. parse_nextline(false, p);
  1902. // Skip to the decision while
  1903. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1904. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1905. parse_line(label);
  1906. syntax.curly_count--;
  1907. // End while labeling
  1908. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1909. l=add_str(label);
  1910. set_label(l,script_pos,p);
  1911. syntax.curly_count--;
  1912. return p;
  1913. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1914. int pos2 = syntax.curly_count-1;
  1915. char label2[256];
  1916. int l2;
  1917. // Back
  1918. sprintf(label2,"return;");
  1919. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1920. parse_line(label2);
  1921. syntax.curly_count--;
  1922. // Put the label of the location
  1923. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1924. l2=add_str(label2);
  1925. set_label(l2,script_pos,p);
  1926. syntax.curly_count--;
  1927. return p;
  1928. } else {
  1929. *flag = 0;
  1930. return p;
  1931. }
  1932. }
  1933. /*==========================================
  1934. * Added built-in functions
  1935. *------------------------------------------*/
  1936. static void add_buildin_func(void)
  1937. {
  1938. int i;
  1939. for( i = 0; buildin_func[i].func; i++ )
  1940. {
  1941. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1942. // 'v' - value (either string or int or reference)
  1943. // 's' - string
  1944. // 'i' - int
  1945. // 'r' - reference (of a variable)
  1946. // 'l' - label
  1947. // '?' - one optional parameter
  1948. // '*' - unknown number of optional parameters
  1949. const char* p = buildin_func[i].arg;
  1950. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1951. while( *p == '?' ) ++p;
  1952. if( *p == '*' ) ++p;
  1953. if( *p != 0){
  1954. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1955. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1956. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1957. } else {
  1958. int n = add_str(buildin_func[i].name);
  1959. str_data[n].type = C_FUNC;
  1960. str_data[n].val = i;
  1961. str_data[n].func = buildin_func[i].func;
  1962. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  1963. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1964. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1965. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1966. }
  1967. }
  1968. }
  1969. /// Retrieves the value of a constant.
  1970. bool script_get_constant(const char* name, int* value)
  1971. {
  1972. int n = search_str(name);
  1973. if( n == -1 || str_data[n].type != C_INT )
  1974. {// not found or not a constant
  1975. return false;
  1976. }
  1977. value[0] = str_data[n].val;
  1978. return true;
  1979. }
  1980. /// Creates new constant or parameter with given value.
  1981. void script_set_constant(const char* name, int value, bool isparameter)
  1982. {
  1983. int n = add_str(name);
  1984. if( str_data[n].type == C_NOP )
  1985. {// new
  1986. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1987. str_data[n].val = value;
  1988. }
  1989. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1990. {// existing parameter or constant
  1991. 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);
  1992. }
  1993. else
  1994. {// existing name
  1995. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1996. }
  1997. }
  1998. /*==========================================
  1999. * Reading constant databases
  2000. * const.txt
  2001. *------------------------------------------*/
  2002. static void read_constdb(void)
  2003. {
  2004. FILE *fp;
  2005. char line[1024],name[1024],val[1024];
  2006. int type;
  2007. int entries=0, skipped=0, linenum=0;
  2008. sprintf(line, "%s/const.txt", db_path);
  2009. fp=fopen(line, "r");
  2010. if(fp==NULL){
  2011. ShowError("can't read %s\n", line);
  2012. return ;
  2013. }
  2014. while(fgets(line, sizeof(line), fp))
  2015. {
  2016. linenum++;
  2017. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  2018. continue;
  2019. if(line[0]=='/' && line[1]=='/') //ignore commented line
  2020. continue;
  2021. type=0;
  2022. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%11d",name,val,&type)>=2 ||
  2023. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d",name,val,&type)>=2){
  2024. entries++;
  2025. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2026. }
  2027. else {
  2028. skipped++;
  2029. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2030. }
  2031. }
  2032. fclose(fp);
  2033. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2034. if(skipped){
  2035. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2036. }
  2037. }
  2038. /**
  2039. * Sets source-end constants for NPC scripts to access.
  2040. **/
  2041. void script_hardcoded_constants(void) {
  2042. #include "script_constants.h"
  2043. }
  2044. /*==========================================
  2045. * Display emplacement line of script
  2046. *------------------------------------------*/
  2047. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2048. {
  2049. int i;
  2050. if( p == NULL || !p[0] ) return NULL;
  2051. if( line < 0 )
  2052. StringBuf_Printf(buf, "*% 5d : ", -line);
  2053. else
  2054. StringBuf_Printf(buf, " % 5d : ", line);
  2055. for(i=0;p[i] && p[i] != '\n';i++){
  2056. if(p + i != mark)
  2057. StringBuf_Printf(buf, "%c", p[i]);
  2058. else
  2059. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2060. }
  2061. StringBuf_AppendStr(buf, "\n");
  2062. return p+i+(p[i] == '\n' ? 1 : 0);
  2063. }
  2064. 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) {
  2065. // Find the line where the error occurred
  2066. int j;
  2067. int line = start_line;
  2068. const char *p;
  2069. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2070. for(p=src;p && *p;line++){
  2071. const char *lineend=strchr(p,'\n');
  2072. if(lineend==NULL || error_pos_cur<lineend){
  2073. break;
  2074. }
  2075. for( j = 0; j < 4; j++ ) {
  2076. linestart[j] = linestart[j+1];
  2077. }
  2078. linestart[4] = p;
  2079. p=lineend+1;
  2080. }
  2081. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2082. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2083. for(j = 0; j < 5; j++ ) {
  2084. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2085. }
  2086. p = script_print_line(buf, p, error_pos_cur, -line);
  2087. for(j = 0; j < 5; j++) {
  2088. p = script_print_line(buf, p, NULL, line + j + 1 );
  2089. }
  2090. }
  2091. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2092. StringBuf buf;
  2093. StringBuf_Init(&buf);
  2094. StringBuf_AppendStr(&buf, "\a\n");
  2095. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2096. ShowError("%s", StringBuf_Value(&buf));
  2097. StringBuf_Destroy(&buf);
  2098. }
  2099. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2100. StringBuf buf;
  2101. StringBuf_Init(&buf);
  2102. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2103. ShowWarning("%s", StringBuf_Value(&buf));
  2104. StringBuf_Destroy(&buf);
  2105. }
  2106. /*==========================================
  2107. * Analysis of the script
  2108. *------------------------------------------*/
  2109. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2110. {
  2111. const char *p,*tmpp;
  2112. int i;
  2113. struct script_code* code = NULL;
  2114. static int first=1;
  2115. char end;
  2116. bool unresolved_names = false;
  2117. parser_current_src = src;
  2118. parser_current_file = file;
  2119. parser_current_line = line;
  2120. if( src == NULL )
  2121. return NULL;// empty script
  2122. memset(&syntax,0,sizeof(syntax));
  2123. if(first){
  2124. add_buildin_func();
  2125. read_constdb();
  2126. script_hardcoded_constants();
  2127. first=0;
  2128. }
  2129. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2130. script_pos=0;
  2131. script_size=SCRIPT_BLOCK_SIZE;
  2132. parse_nextline(true, NULL);
  2133. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2134. if( options&SCRIPT_USE_LABEL_DB )
  2135. db_clear(scriptlabel_db);
  2136. parse_options = options;
  2137. if( setjmp( error_jump ) != 0 ) {
  2138. //Restore program state when script has problems. [from jA]
  2139. int j;
  2140. const int size = ARRAYLENGTH(syntax.curly);
  2141. if( error_report )
  2142. script_error(src,file,line,error_msg,error_pos);
  2143. aFree( error_msg );
  2144. aFree( script_buf );
  2145. script_pos = 0;
  2146. script_size = 0;
  2147. script_buf = NULL;
  2148. for(j=LABEL_START;j<str_num;j++)
  2149. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2150. for(j=0; j<size; j++)
  2151. linkdb_final(&syntax.curly[j].case_label);
  2152. return NULL;
  2153. }
  2154. parse_syntax_for_flag=0;
  2155. p=src;
  2156. p=skip_space(p);
  2157. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2158. {// does not require brackets around the script
  2159. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2160. {// empty script and can return NULL
  2161. aFree( script_buf );
  2162. script_pos = 0;
  2163. script_size = 0;
  2164. script_buf = NULL;
  2165. return NULL;
  2166. }
  2167. end = '\0';
  2168. }
  2169. else
  2170. {// requires brackets around the script
  2171. if( *p != '{' )
  2172. disp_error_message("not found '{'",p);
  2173. p = skip_space(p+1);
  2174. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2175. {// empty script and can return NULL
  2176. aFree( script_buf );
  2177. script_pos = 0;
  2178. script_size = 0;
  2179. script_buf = NULL;
  2180. return NULL;
  2181. }
  2182. end = '}';
  2183. }
  2184. // clear references of labels, variables and internal functions
  2185. for(i=LABEL_START;i<str_num;i++){
  2186. if(
  2187. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2188. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2189. ){
  2190. str_data[i].type=C_NOP;
  2191. str_data[i].backpatch=-1;
  2192. str_data[i].label=-1;
  2193. }
  2194. }
  2195. while( syntax.curly_count != 0 || *p != end )
  2196. {
  2197. if( *p == '\0' )
  2198. disp_error_message("unexpected end of script",p);
  2199. // Special handling only label
  2200. tmpp=skip_space(skip_word(p));
  2201. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2202. i=add_word(p);
  2203. set_label(i,script_pos,p);
  2204. if( parse_options&SCRIPT_USE_LABEL_DB )
  2205. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2206. p=tmpp+1;
  2207. p=skip_space(p);
  2208. continue;
  2209. }
  2210. // All other lumped
  2211. p=parse_line(p);
  2212. p=skip_space(p);
  2213. parse_nextline(false, p);
  2214. }
  2215. add_scriptc(C_NOP);
  2216. // trim code to size
  2217. script_size = script_pos;
  2218. RECREATE(script_buf,unsigned char,script_pos);
  2219. // default unknown references to variables
  2220. for(i=LABEL_START;i<str_num;i++){
  2221. if(str_data[i].type==C_NOP){
  2222. int j;
  2223. str_data[i].type=C_NAME;
  2224. str_data[i].label=i;
  2225. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2226. int next=GETVALUE(script_buf,j);
  2227. SETVALUE(script_buf,j,i);
  2228. j=next;
  2229. }
  2230. }
  2231. else if( str_data[i].type == C_USERFUNC )
  2232. {// 'function name;' without follow-up code
  2233. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2234. unresolved_names = true;
  2235. }
  2236. }
  2237. if( unresolved_names )
  2238. {
  2239. disp_error_message("parse_script: unresolved function references", p);
  2240. }
  2241. #ifdef DEBUG_DISP
  2242. for(i=0;i<script_pos;i++){
  2243. if((i&15)==0) ShowMessage("%04x : ",i);
  2244. ShowMessage("%02x ",script_buf[i]);
  2245. if((i&15)==15) ShowMessage("\n");
  2246. }
  2247. ShowMessage("\n");
  2248. #endif
  2249. #ifdef DEBUG_DISASM
  2250. {
  2251. int i = 0,j;
  2252. while(i < script_pos) {
  2253. c_op op = get_com(script_buf,&i);
  2254. ShowMessage("%06x %s", i, script_op2name(op));
  2255. j = i;
  2256. switch(op) {
  2257. case C_INT:
  2258. ShowMessage(" %d", get_num(script_buf,&i));
  2259. break;
  2260. case C_POS:
  2261. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2262. i += 3;
  2263. break;
  2264. case C_NAME:
  2265. j = (*(int*)(script_buf+i)&0xffffff);
  2266. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2267. i += 3;
  2268. break;
  2269. case C_STR:
  2270. j = strlen(script_buf + i);
  2271. ShowMessage(" %s", script_buf + i);
  2272. i += j+1;
  2273. break;
  2274. }
  2275. ShowMessage(CL_CLL"\n");
  2276. }
  2277. }
  2278. #endif
  2279. CREATE(code,struct script_code,1);
  2280. code->script_buf = script_buf;
  2281. code->script_size = script_size;
  2282. code->local.vars = NULL;
  2283. code->local.arrays = NULL;
  2284. return code;
  2285. }
  2286. /// Returns the player attached to this script, identified by the rid.
  2287. /// If there is no player attached, the script is terminated.
  2288. TBL_PC *script_rid2sd(struct script_state *st)
  2289. {
  2290. TBL_PC *sd=map_id2sd(st->rid);
  2291. if(!sd){
  2292. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2293. script_reportfunc(st);
  2294. script_reportsrc(st);
  2295. st->state = END;
  2296. }
  2297. return sd;
  2298. }
  2299. /**
  2300. * Dereferences a variable/constant, replacing it with a copy of the value.
  2301. * @param st Script state
  2302. * @param data Variable/constant
  2303. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2304. */
  2305. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2306. {
  2307. const char* name;
  2308. char prefix;
  2309. char postfix;
  2310. if( !data_isreference(data) )
  2311. return;// not a variable/constant
  2312. name = reference_getname(data);
  2313. prefix = name[0];
  2314. postfix = name[strlen(name) - 1];
  2315. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2316. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2317. if (sd == NULL)
  2318. sd = script_rid2sd(st);
  2319. if( sd == NULL ) {// needs player attached
  2320. if( postfix == '$' ) {// string variable
  2321. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2322. data->type = C_CONSTSTR;
  2323. data->u.str = "";
  2324. } else {// integer variable
  2325. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2326. data->type = C_INT;
  2327. data->u.num = 0;
  2328. }
  2329. return;
  2330. }
  2331. }
  2332. if( postfix == '$' ) {// string variable
  2333. switch( prefix ) {
  2334. case '@':
  2335. data->u.str = pc_readregstr(sd, data->u.num);
  2336. break;
  2337. case '$':
  2338. data->u.str = mapreg_readregstr(data->u.num);
  2339. break;
  2340. case '#':
  2341. if( name[1] == '#' )
  2342. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2343. else
  2344. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2345. break;
  2346. case '.':
  2347. {
  2348. struct DBMap* n = data->ref ?
  2349. data->ref->vars : name[1] == '@' ?
  2350. st->stack->scope.vars : // instance/scope variable
  2351. st->script->local.vars; // npc variable
  2352. if( n )
  2353. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2354. else
  2355. data->u.str = NULL;
  2356. }
  2357. break;
  2358. case '\'':
  2359. {
  2360. unsigned short instance_id = script_instancegetid(st);
  2361. if( instance_id )
  2362. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2363. else {
  2364. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2365. data->u.str = NULL;
  2366. }
  2367. }
  2368. break;
  2369. default:
  2370. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2371. break;
  2372. }
  2373. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2374. data->type = C_CONSTSTR;
  2375. data->u.str = "";
  2376. } else {// duplicate string
  2377. data->type = C_STR;
  2378. data->u.str = aStrdup(data->u.str);
  2379. }
  2380. } else {// integer variable
  2381. data->type = C_INT;
  2382. if( reference_toconstant(data) ) {
  2383. data->u.num = reference_getconstant(data);
  2384. } else if( reference_toparam(data) ) {
  2385. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2386. } else
  2387. switch( prefix ) {
  2388. case '@':
  2389. data->u.num = pc_readreg(sd, data->u.num);
  2390. break;
  2391. case '$':
  2392. data->u.num = mapreg_readreg(data->u.num);
  2393. break;
  2394. case '#':
  2395. if( name[1] == '#' )
  2396. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2397. else
  2398. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2399. break;
  2400. case '.':
  2401. {
  2402. struct DBMap* n = data->ref ?
  2403. data->ref->vars : name[1] == '@' ?
  2404. st->stack->scope.vars : // instance/scope variable
  2405. st->script->local.vars; // npc variable
  2406. if( n )
  2407. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2408. else
  2409. data->u.num = 0;
  2410. }
  2411. break;
  2412. case '\'':
  2413. {
  2414. unsigned short instance_id = script_instancegetid(st);
  2415. if( instance_id )
  2416. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2417. else {
  2418. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2419. data->u.num = 0;
  2420. }
  2421. }
  2422. break;
  2423. default:
  2424. data->u.num = pc_readglobalreg(sd, data->u.num);
  2425. break;
  2426. }
  2427. }
  2428. data->ref = NULL;
  2429. return;
  2430. }
  2431. void get_val(struct script_state* st, struct script_data* data)
  2432. {
  2433. get_val_(st,data,NULL);
  2434. }
  2435. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2436. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2437. /// The value is left in the top of the stack and needs to be removed manually.
  2438. /// @param st[in]: script state.
  2439. /// @param uid[in]: reference identifier.
  2440. /// @param ref[in]: the container to look up the reference into.
  2441. /// @return: the retrieved value of the reference.
  2442. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2443. {
  2444. struct script_data* data;
  2445. push_val2(st->stack, C_NAME, uid, ref);
  2446. data = script_getdatatop(st, -1);
  2447. get_val(st, data);
  2448. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2449. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2450. }
  2451. /**
  2452. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2453. * Will be gone as soon as undefined var feature is implemented
  2454. **/
  2455. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2456. {
  2457. const char *name = get_str(script_getvarid(uid));
  2458. // is here st can be null pointer and st->rid is wrong?
  2459. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2460. bool insert = false;
  2461. if (sd && !st) {
  2462. // when sd comes, st isn't available
  2463. insert = true;
  2464. } else {
  2465. if( is_string_variable(name) ) {
  2466. char* str = (char*)get_val2(st, uid, ref);
  2467. if( str && *str )
  2468. insert = true;
  2469. script_removetop(st, -1, 0);
  2470. } else {
  2471. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2472. if( num )
  2473. insert = true;
  2474. script_removetop(st, -1, 0);
  2475. }
  2476. }
  2477. if (src && src->arrays) {
  2478. struct script_array *sa = idb_get(src->arrays, script_getvarid(uid));
  2479. if (sa) {
  2480. unsigned int i;
  2481. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2482. if( i != sa->size ) {
  2483. if( !insert )
  2484. script_array_remove_member(src,sa,i);
  2485. return;
  2486. }
  2487. script_array_add_member(sa,0);
  2488. } else if (insert) {
  2489. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2490. }
  2491. }
  2492. }
  2493. /**
  2494. * Returns array size by ID
  2495. **/
  2496. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2497. {
  2498. struct script_array *sa = NULL;
  2499. struct reg_db *src = script_array_src(st, sd, name, ref);
  2500. if (src && src->arrays)
  2501. sa = idb_get(src->arrays, search_str(name));
  2502. return sa ? sa->size : 0;
  2503. }
  2504. /**
  2505. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2506. **/
  2507. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2508. {
  2509. struct script_array *sa = NULL;
  2510. struct reg_db *src = script_array_src(st, sd, name, ref);
  2511. if (src && src->arrays) {
  2512. int key = add_word(name);
  2513. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2514. if( ( sa = idb_get(src->arrays, key) ) ) {
  2515. unsigned int i, highest_key = 0;
  2516. for(i = 0; i < sa->size; i++) {
  2517. if( sa->members[i] > highest_key )
  2518. highest_key = sa->members[i];
  2519. }
  2520. return sa->size ? highest_key + 1 : 0;
  2521. }
  2522. }
  2523. return SCRIPT_CMD_SUCCESS;
  2524. }
  2525. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2526. {
  2527. struct script_array *sa = db_data2ptr(data);
  2528. aFree(sa->members);
  2529. ers_free(array_ers, sa);
  2530. return SCRIPT_CMD_SUCCESS;
  2531. }
  2532. /**
  2533. * Clears script_array and removes it from script->array_db
  2534. **/
  2535. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2536. {
  2537. aFree(sa->members);
  2538. idb_remove(src->arrays, sa->id);
  2539. ers_free(array_ers, sa);
  2540. }
  2541. /**
  2542. * Removes a member from a script_array list
  2543. *
  2544. * @param idx the index of the member in script_array struct list, not of the actual array member
  2545. **/
  2546. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2547. {
  2548. unsigned int i, cursor;
  2549. // it's the only member left, no need to do anything other than delete the array data
  2550. if( sa->size == 1 ) {
  2551. script_array_delete(src,sa);
  2552. return;
  2553. }
  2554. sa->members[idx] = UINT_MAX;
  2555. for(i = 0, cursor = 0; i < sa->size; i++) {
  2556. if( sa->members[i] == UINT_MAX )
  2557. continue;
  2558. if( i != cursor )
  2559. sa->members[cursor] = sa->members[i];
  2560. cursor++;
  2561. }
  2562. sa->size = cursor;
  2563. }
  2564. /**
  2565. * Appends a new array index to the list in script_array
  2566. *
  2567. * @param idx the index of the array member being inserted
  2568. **/
  2569. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2570. {
  2571. RECREATE(sa->members, unsigned int, ++sa->size);
  2572. sa->members[sa->size - 1] = idx;
  2573. }
  2574. /**
  2575. * Obtains the source of the array database for this type and scenario
  2576. * Initializes such database when not yet initialized.
  2577. **/
  2578. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2579. {
  2580. struct reg_db *src = NULL;
  2581. switch( name[0] ) {
  2582. // from player
  2583. default: // char reg
  2584. case '@': // temp char reg
  2585. case '#': // account reg
  2586. src = &sd->regs;
  2587. break;
  2588. case '$': // map reg
  2589. src = &regs;
  2590. break;
  2591. case '.': // npc/script
  2592. if (ref)
  2593. src = ref;
  2594. else
  2595. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2596. break;
  2597. case '\'': // instance
  2598. {
  2599. unsigned short instance_id = script_instancegetid(st);
  2600. if( instance_id ) {
  2601. src = &instance_data[instance_id].regs;
  2602. }
  2603. break;
  2604. }
  2605. }
  2606. if( src ) {
  2607. if( !src->arrays )
  2608. src->arrays = idb_alloc(DB_OPT_BASE);
  2609. return src;
  2610. }
  2611. return NULL;
  2612. }
  2613. /**
  2614. * Processes a array member modification, and update data accordingly
  2615. *
  2616. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2617. * @param num[in] Variable ID
  2618. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2619. **/
  2620. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2621. {
  2622. struct script_array *sa = NULL;
  2623. int id = script_getvarid(num);
  2624. unsigned int index = script_getvaridx(num);
  2625. if (!src->arrays) {
  2626. src->arrays = idb_alloc(DB_OPT_BASE);
  2627. } else {
  2628. sa = idb_get(src->arrays, id);
  2629. }
  2630. if( sa ) {
  2631. unsigned int i;
  2632. // search
  2633. for(i = 0; i < sa->size; i++) {
  2634. if( sa->members[i] == index )
  2635. break;
  2636. }
  2637. // if existent
  2638. if( i != sa->size ) {
  2639. // if empty, we gotta remove it
  2640. if( empty ) {
  2641. script_array_remove_member(src, sa, i);
  2642. }
  2643. } else if( !empty ) { /* new entry */
  2644. script_array_add_member(sa,index);
  2645. // we do nothing if its empty, no point in modifying array data for a new empty member
  2646. }
  2647. } else if ( !empty ) { // we only move to create if not empty
  2648. sa = ers_alloc(array_ers, struct script_array);
  2649. sa->id = id;
  2650. sa->members = NULL;
  2651. sa->size = 0;
  2652. script_array_add_member(sa,index);
  2653. idb_put(src->arrays, id, sa);
  2654. }
  2655. }
  2656. /**
  2657. * Stores the value of a script variable
  2658. *
  2659. * @param st: current script state.
  2660. * @param sd: current character data.
  2661. * @param num: variable identifier.
  2662. * @param name: variable name.
  2663. * @param value: new value.
  2664. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2665. * @return: 0 failure, 1 success.
  2666. *
  2667. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2668. *------------------------------------------*/
  2669. int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2670. {
  2671. char prefix = name[0];
  2672. if( is_string_variable(name) ) {// string variable
  2673. const char *str = (const char*)value;
  2674. switch (prefix) {
  2675. case '@':
  2676. pc_setregstr(sd, num, str);
  2677. return 1;
  2678. case '$':
  2679. return mapreg_setregstr(num, str);
  2680. case '#':
  2681. return (name[1] == '#') ?
  2682. pc_setaccountreg2str(sd, num, str) :
  2683. pc_setaccountregstr(sd, num, str);
  2684. case '.':
  2685. {
  2686. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2687. if( n ) {
  2688. if (str[0]) {
  2689. i64db_put(n->vars, num, aStrdup(str));
  2690. if( script_getvaridx(num) )
  2691. script_array_update(n, num, false);
  2692. } else {
  2693. i64db_remove(n->vars, num);
  2694. if( script_getvaridx(num) )
  2695. script_array_update(n, num, true);
  2696. }
  2697. }
  2698. }
  2699. return 1;
  2700. case '\'':
  2701. {
  2702. unsigned short instance_id = script_instancegetid(st);
  2703. if( instance_id ) {
  2704. if( str[0] ) {
  2705. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2706. if( script_getvaridx(num) )
  2707. script_array_update(&instance_data[instance_id].regs, num, false);
  2708. } else {
  2709. i64db_remove(instance_data[instance_id].regs.vars, num);
  2710. if (script_getvaridx(num))
  2711. script_array_update(&instance_data[instance_id].regs, num, true);
  2712. }
  2713. } else {
  2714. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2715. script_reportsrc(st);
  2716. }
  2717. return 1;
  2718. }
  2719. default:
  2720. return pc_setglobalreg_str(sd, num, str);
  2721. }
  2722. } else {// integer variable
  2723. int val = (int)__64BPRTSIZE(value);
  2724. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2725. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2726. if( st != NULL ) {
  2727. ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val);
  2728. script_reportsrc(st);
  2729. st->state = END;
  2730. }
  2731. return 0;
  2732. }
  2733. return 1;
  2734. }
  2735. switch (prefix) {
  2736. case '@':
  2737. pc_setreg(sd, num, val);
  2738. return 1;
  2739. case '$':
  2740. return mapreg_setreg(num, val);
  2741. case '#':
  2742. return (name[1] == '#') ?
  2743. pc_setaccountreg2(sd, num, val) :
  2744. pc_setaccountreg(sd, num, val);
  2745. case '.':
  2746. {
  2747. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2748. if( n ) {
  2749. if( val != 0 ) {
  2750. i64db_iput(n->vars, num, val);
  2751. if( script_getvaridx(num) )
  2752. script_array_update(n, num, false);
  2753. } else {
  2754. i64db_remove(n->vars, num);
  2755. if( script_getvaridx(num) )
  2756. script_array_update(n, num, true);
  2757. }
  2758. }
  2759. }
  2760. return 1;
  2761. case '\'':
  2762. {
  2763. unsigned short instance_id = script_instancegetid(st);
  2764. if( instance_id ) {
  2765. if( val != 0 ) {
  2766. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2767. if( script_getvaridx(num) )
  2768. script_array_update(&instance_data[instance_id].regs, num, false);
  2769. } else {
  2770. i64db_remove(instance_data[instance_id].regs.vars, num);
  2771. if (script_getvaridx(num))
  2772. script_array_update(&instance_data[instance_id].regs, num, true);
  2773. }
  2774. } else {
  2775. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2776. script_reportsrc(st);
  2777. }
  2778. return 1;
  2779. }
  2780. default:
  2781. return pc_setglobalreg(sd, num, val);
  2782. }
  2783. }
  2784. }
  2785. int set_var(TBL_PC* sd, char* name, void* val)
  2786. {
  2787. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2788. }
  2789. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2790. {
  2791. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2792. }
  2793. /**
  2794. * Converts the data to a string
  2795. * @param st
  2796. * @param data
  2797. * @param sd
  2798. */
  2799. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2800. {
  2801. char* p;
  2802. get_val_(st, data, sd);
  2803. if( data_isstring(data) )
  2804. {// nothing to convert
  2805. }
  2806. else if( data_isint(data) )
  2807. {// int -> string
  2808. CREATE(p, char, ITEM_NAME_LENGTH);
  2809. snprintf(p, ITEM_NAME_LENGTH, "%"PRId64"", data->u.num);
  2810. p[ITEM_NAME_LENGTH-1] = '\0';
  2811. data->type = C_STR;
  2812. data->u.str = p;
  2813. }
  2814. else if( data_isreference(data) )
  2815. {// reference -> string
  2816. //##TODO when does this happen (check get_val) [FlavioJS]
  2817. data->type = C_CONSTSTR;
  2818. data->u.str = reference_getname(data);
  2819. }
  2820. else
  2821. {// unsupported data type
  2822. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2823. script_reportdata(data);
  2824. script_reportsrc(st);
  2825. data->type = C_CONSTSTR;
  2826. data->u.str = "";
  2827. }
  2828. return data->u.str;
  2829. }
  2830. const char* conv_str(struct script_state* st, struct script_data* data)
  2831. {
  2832. return conv_str_(st, data, NULL);
  2833. }
  2834. /**
  2835. * Converts the data to an int
  2836. * @param st
  2837. * @param data
  2838. * @param sd
  2839. */
  2840. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2841. {
  2842. get_val_(st, data, sd);
  2843. if( data_isint(data) )
  2844. {// nothing to convert
  2845. }
  2846. else if( data_isstring(data) )
  2847. {// string -> int
  2848. // the result does not overflow or underflow, it is capped instead
  2849. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2850. char* p = data->u.str;
  2851. long num;
  2852. errno = 0;
  2853. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2854. if( errno == ERANGE
  2855. #if LONG_MAX > INT_MAX
  2856. || num < INT_MIN || num > INT_MAX
  2857. #endif
  2858. )
  2859. {
  2860. if( num <= INT_MIN )
  2861. {
  2862. num = INT_MIN;
  2863. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2864. }
  2865. else//if( num >= INT_MAX )
  2866. {
  2867. num = INT_MAX;
  2868. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2869. }
  2870. script_reportdata(data);
  2871. script_reportsrc(st);
  2872. }
  2873. if( data->type == C_STR )
  2874. aFree(p);
  2875. data->type = C_INT;
  2876. data->u.num = (int)num;
  2877. }
  2878. #if 0
  2879. // FIXME this function is being used to retrieve the position of labels and
  2880. // probably other stuff [FlavioJS]
  2881. else
  2882. {// unsupported data type
  2883. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2884. script_reportdata(data);
  2885. script_reportsrc(st);
  2886. data->type = C_INT;
  2887. data->u.num = 0;
  2888. }
  2889. #endif
  2890. return (int)data->u.num;
  2891. }
  2892. int conv_num(struct script_state* st, struct script_data* data)
  2893. {
  2894. return conv_num_(st, data, NULL);
  2895. }
  2896. //
  2897. // Stack operations
  2898. //
  2899. /// Increases the size of the stack
  2900. void stack_expand(struct script_stack* stack)
  2901. {
  2902. stack->sp_max += 64;
  2903. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2904. stack->sp_max * sizeof(stack->stack_data[0]) );
  2905. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2906. 64 * sizeof(stack->stack_data[0]) );
  2907. }
  2908. /// Pushes a value into the stack
  2909. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2910. /// Pushes a value into the stack (with reference)
  2911. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  2912. {
  2913. if( stack->sp >= stack->sp_max )
  2914. stack_expand(stack);
  2915. stack->stack_data[stack->sp].type = type;
  2916. stack->stack_data[stack->sp].u.num = val;
  2917. stack->stack_data[stack->sp].ref = ref;
  2918. stack->sp++;
  2919. return &stack->stack_data[stack->sp-1];
  2920. }
  2921. /// Pushes a string into the stack
  2922. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2923. {
  2924. if( stack->sp >= stack->sp_max )
  2925. stack_expand(stack);
  2926. stack->stack_data[stack->sp].type = type;
  2927. stack->stack_data[stack->sp].u.str = str;
  2928. stack->stack_data[stack->sp].ref = NULL;
  2929. stack->sp++;
  2930. return &stack->stack_data[stack->sp-1];
  2931. }
  2932. /// Pushes a retinfo into the stack
  2933. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  2934. {
  2935. if( stack->sp >= stack->sp_max )
  2936. stack_expand(stack);
  2937. stack->stack_data[stack->sp].type = C_RETINFO;
  2938. stack->stack_data[stack->sp].u.ri = ri;
  2939. stack->stack_data[stack->sp].ref = ref;
  2940. stack->sp++;
  2941. return &stack->stack_data[stack->sp-1];
  2942. }
  2943. /// Pushes a copy of the target position into the stack
  2944. struct script_data* push_copy(struct script_stack* stack, int pos)
  2945. {
  2946. switch( stack->stack_data[pos].type ) {
  2947. case C_CONSTSTR:
  2948. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2949. break;
  2950. case C_STR:
  2951. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2952. break;
  2953. case C_RETINFO:
  2954. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2955. exit(1);
  2956. break;
  2957. default:
  2958. return push_val2(
  2959. stack,stack->stack_data[pos].type,
  2960. stack->stack_data[pos].u.num,
  2961. stack->stack_data[pos].ref
  2962. );
  2963. break;
  2964. }
  2965. }
  2966. /// Removes the values in indexes [start,end] from the stack.
  2967. /// Adjusts all stack pointers.
  2968. void pop_stack(struct script_state* st, int start, int end)
  2969. {
  2970. struct script_stack* stack = st->stack;
  2971. struct script_data* data;
  2972. int i;
  2973. if( start < 0 )
  2974. start = 0;
  2975. if( end > stack->sp )
  2976. end = stack->sp;
  2977. if( start >= end )
  2978. return;// nothing to pop
  2979. // free stack elements
  2980. for( i = start; i < end; i++ )
  2981. {
  2982. data = &stack->stack_data[i];
  2983. if( data->type == C_STR )
  2984. aFree(data->u.str);
  2985. if( data->type == C_RETINFO ) {
  2986. struct script_retinfo* ri = data->u.ri;
  2987. if (ri->scope.vars) {
  2988. script_free_vars(ri->scope.vars);
  2989. ri->scope.vars = NULL;
  2990. }
  2991. if (ri->scope.arrays) {
  2992. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  2993. ri->scope.arrays = NULL;
  2994. }
  2995. if( data->ref )
  2996. aFree(data->ref);
  2997. aFree(ri);
  2998. }
  2999. data->type = C_NOP;
  3000. }
  3001. // move the rest of the elements
  3002. if( stack->sp > end )
  3003. {
  3004. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3005. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3006. stack->stack_data[i].type = C_NOP;
  3007. }
  3008. // adjust stack pointers
  3009. if( st->start > end ) st->start -= end - start;
  3010. else if( st->start > start ) st->start = start;
  3011. if( st->end > end ) st->end -= end - start;
  3012. else if( st->end > start ) st->end = start;
  3013. if( stack->defsp > end ) stack->defsp -= end - start;
  3014. else if( stack->defsp > start ) stack->defsp = start;
  3015. stack->sp -= end - start;
  3016. }
  3017. ///
  3018. ///
  3019. ///
  3020. /*==========================================
  3021. * Release script dependent variable, dependent variable of function
  3022. *------------------------------------------*/
  3023. void script_free_vars(struct DBMap* storage)
  3024. {
  3025. if( storage ) {
  3026. // destroy the storage construct containing the variables
  3027. db_destroy(storage);
  3028. }
  3029. }
  3030. void script_free_code(struct script_code* code)
  3031. {
  3032. nullpo_retv(code);
  3033. if (code->instances)
  3034. script_stop_instances(code);
  3035. script_free_vars(code->local.vars);
  3036. if (code->local.arrays)
  3037. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3038. aFree(code->script_buf);
  3039. aFree(code);
  3040. }
  3041. /// Creates a new script state.
  3042. ///
  3043. /// @param script Script code
  3044. /// @param pos Position in the code
  3045. /// @param rid Who is running the script (attached player)
  3046. /// @param oid Where the code is being run (npc 'object')
  3047. /// @return Script state
  3048. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3049. {
  3050. struct script_state* st;
  3051. st = ers_alloc(st_ers, struct script_state);
  3052. st->stack = ers_alloc(stack_ers, struct script_stack);
  3053. st->stack->sp = 0;
  3054. st->stack->sp_max = 64;
  3055. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3056. st->stack->defsp = st->stack->sp;
  3057. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3058. st->stack->scope.arrays = NULL;
  3059. st->state = RUN;
  3060. st->script = rootscript;
  3061. st->pos = pos;
  3062. st->rid = rid;
  3063. st->oid = oid;
  3064. st->sleep.timer = INVALID_TIMER;
  3065. st->npc_item_flag = battle_config.item_enabled_npc;
  3066. if( st->script->instances != USHRT_MAX )
  3067. st->script->instances++;
  3068. else {
  3069. struct npc_data *nd = map_id2nd(oid);
  3070. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3071. }
  3072. if (!st->script->local.vars)
  3073. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3074. st->id = next_id++;
  3075. active_scripts++;
  3076. idb_put(st_db, st->id, st);
  3077. return st;
  3078. }
  3079. /// Frees a script state.
  3080. ///
  3081. /// @param st Script state
  3082. void script_free_state(struct script_state* st)
  3083. {
  3084. if (idb_exists(st_db, st->id)) {
  3085. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3086. if (st->bk_st) // backup was not restored
  3087. 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);
  3088. if (sd && sd->st == st) { // Current script is aborted.
  3089. if(sd->state.using_fake_npc) {
  3090. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3091. sd->state.using_fake_npc = 0;
  3092. }
  3093. sd->st = NULL;
  3094. sd->npc_id = 0;
  3095. }
  3096. if (st->sleep.timer != INVALID_TIMER)
  3097. delete_timer(st->sleep.timer, run_script_timer);
  3098. if (st->stack) {
  3099. script_free_vars(st->stack->scope.vars);
  3100. if (st->stack->scope.arrays)
  3101. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3102. pop_stack(st, 0, st->stack->sp);
  3103. aFree(st->stack->stack_data);
  3104. ers_free(stack_ers, st->stack);
  3105. st->stack = NULL;
  3106. }
  3107. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3108. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3109. script_free_vars(st->script->local.vars);
  3110. st->script->local.vars = NULL;
  3111. }
  3112. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3113. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3114. st->script->local.arrays = NULL;
  3115. }
  3116. }
  3117. st->pos = -1;
  3118. idb_remove(st_db, st->id);
  3119. ers_free(st_ers, st);
  3120. if (--active_scripts == 0)
  3121. next_id = 0;
  3122. }
  3123. }
  3124. //
  3125. // Main execution unit
  3126. //
  3127. /*==========================================
  3128. * Read command
  3129. *------------------------------------------*/
  3130. c_op get_com(unsigned char *script,int *pos)
  3131. {
  3132. int i = 0, j = 0;
  3133. if(script[*pos]>=0x80){
  3134. return C_INT;
  3135. }
  3136. while(script[*pos]>=0x40){
  3137. i=script[(*pos)++]<<j;
  3138. j+=6;
  3139. }
  3140. return (c_op)(i+(script[(*pos)++]<<j));
  3141. }
  3142. /*==========================================
  3143. * Income figures
  3144. *------------------------------------------*/
  3145. int get_num(unsigned char *script,int *pos)
  3146. {
  3147. int i,j;
  3148. i=0; j=0;
  3149. while(script[*pos]>=0xc0){
  3150. i+=(script[(*pos)++]&0x7f)<<j;
  3151. j+=6;
  3152. }
  3153. return i+((script[(*pos)++]&0x7f)<<j);
  3154. }
  3155. /// Ternary operators
  3156. /// test ? if_true : if_false
  3157. void op_3(struct script_state* st, int op)
  3158. {
  3159. struct script_data* data;
  3160. int flag = 0;
  3161. data = script_getdatatop(st, -3);
  3162. get_val(st, data);
  3163. if( data_isstring(data) )
  3164. flag = data->u.str[0];// "" -> false
  3165. else if( data_isint(data) )
  3166. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3167. else
  3168. {
  3169. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3170. script_reportdata(data);
  3171. script_reportsrc(st);
  3172. script_removetop(st, -3, 0);
  3173. script_pushnil(st);
  3174. return;
  3175. }
  3176. if( flag )
  3177. script_pushcopytop(st, -2);
  3178. else
  3179. script_pushcopytop(st, -1);
  3180. script_removetop(st, -4, -1);
  3181. }
  3182. /// Binary string operators
  3183. /// s1 EQ s2 -> i
  3184. /// s1 NE s2 -> i
  3185. /// s1 GT s2 -> i
  3186. /// s1 GE s2 -> i
  3187. /// s1 LT s2 -> i
  3188. /// s1 LE s2 -> i
  3189. /// s1 ADD s2 -> s
  3190. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3191. {
  3192. int a = 0;
  3193. switch(op){
  3194. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3195. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3196. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3197. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3198. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3199. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3200. case C_ADD:
  3201. {
  3202. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3203. strcpy(buf, s1);
  3204. strcat(buf, s2);
  3205. script_pushstr(st, buf);
  3206. return;
  3207. }
  3208. default:
  3209. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3210. script_reportsrc(st);
  3211. script_pushnil(st);
  3212. st->state = END;
  3213. return;
  3214. }
  3215. script_pushint(st,a);
  3216. }
  3217. /// Binary number operators
  3218. /// i OP i -> i
  3219. void op_2num(struct script_state* st, int op, int i1, int i2)
  3220. {
  3221. int ret;
  3222. double ret_double;
  3223. switch( op ) {
  3224. case C_AND: ret = i1 & i2; break;
  3225. case C_OR: ret = i1 | i2; break;
  3226. case C_XOR: ret = i1 ^ i2; break;
  3227. case C_LAND: ret = (i1 && i2); break;
  3228. case C_LOR: ret = (i1 || i2); break;
  3229. case C_EQ: ret = (i1 == i2); break;
  3230. case C_NE: ret = (i1 != i2); break;
  3231. case C_GT: ret = (i1 > i2); break;
  3232. case C_GE: ret = (i1 >= i2); break;
  3233. case C_LT: ret = (i1 < i2); break;
  3234. case C_LE: ret = (i1 <= i2); break;
  3235. case C_R_SHIFT: ret = i1>>i2; break;
  3236. case C_L_SHIFT: ret = i1<<i2; break;
  3237. case C_DIV:
  3238. case C_MOD:
  3239. if( i2 == 0 )
  3240. {
  3241. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3242. script_reportsrc(st);
  3243. script_pushnil(st);
  3244. st->state = END;
  3245. return;
  3246. }
  3247. else if( op == C_DIV )
  3248. ret = i1 / i2;
  3249. else//if( op == C_MOD )
  3250. ret = i1 % i2;
  3251. break;
  3252. default:
  3253. switch( op ) {// operators that can overflow/underflow
  3254. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3255. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3256. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3257. default:
  3258. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3259. script_reportsrc(st);
  3260. script_pushnil(st);
  3261. return;
  3262. }
  3263. if( ret_double < (double)INT_MIN ) {
  3264. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3265. script_reportsrc(st);
  3266. ret = INT_MIN;
  3267. }
  3268. else if( ret_double > (double)INT_MAX ) {
  3269. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3270. script_reportsrc(st);
  3271. ret = INT_MAX;
  3272. }
  3273. }
  3274. script_pushint(st, ret);
  3275. }
  3276. /// Binary operators
  3277. void op_2(struct script_state *st, int op)
  3278. {
  3279. struct script_data* left, leftref;
  3280. struct script_data* right;
  3281. leftref.type = C_NOP;
  3282. left = script_getdatatop(st, -2);
  3283. right = script_getdatatop(st, -1);
  3284. if (st->op2ref) {
  3285. if (data_isreference(left)) {
  3286. leftref = *left;
  3287. }
  3288. st->op2ref = 0;
  3289. }
  3290. get_val(st, left);
  3291. get_val(st, right);
  3292. // automatic conversions
  3293. switch( op )
  3294. {
  3295. case C_ADD:
  3296. if( data_isint(left) && data_isstring(right) )
  3297. {// convert int-string to string-string
  3298. conv_str(st, left);
  3299. }
  3300. else if( data_isstring(left) && data_isint(right) )
  3301. {// convert string-int to string-string
  3302. conv_str(st, right);
  3303. }
  3304. break;
  3305. }
  3306. if( data_isstring(left) && data_isstring(right) )
  3307. {// ss => op_2str
  3308. op_2str(st, op, left->u.str, right->u.str);
  3309. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3310. if (leftref.type != C_NOP)
  3311. {
  3312. if (left->type == C_STR) // don't free C_CONSTSTR
  3313. aFree(left->u.str);
  3314. *left = leftref;
  3315. }
  3316. }
  3317. else if( data_isint(left) && data_isint(right) )
  3318. {// ii => op_2num
  3319. int i1 = (int)left->u.num;
  3320. int i2 = (int)right->u.num;
  3321. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3322. op_2num(st, op, i1, i2);
  3323. if (leftref.type != C_NOP)
  3324. *left = leftref;
  3325. }
  3326. else
  3327. {// invalid argument
  3328. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3329. script_reportdata(left);
  3330. script_reportdata(right);
  3331. script_reportsrc(st);
  3332. script_removetop(st, -2, 0);
  3333. script_pushnil(st);
  3334. st->state = END;
  3335. }
  3336. }
  3337. /// Unary operators
  3338. /// NEG i -> i
  3339. /// NOT i -> i
  3340. /// LNOT i -> i
  3341. void op_1(struct script_state* st, int op)
  3342. {
  3343. struct script_data* data;
  3344. int i1;
  3345. data = script_getdatatop(st, -1);
  3346. get_val(st, data);
  3347. if( !data_isint(data) )
  3348. {// not a number
  3349. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3350. script_reportdata(data);
  3351. script_reportsrc(st);
  3352. script_pushnil(st);
  3353. st->state = END;
  3354. return;
  3355. }
  3356. i1 = (int)data->u.num;
  3357. script_removetop(st, -1, 0);
  3358. switch( op )
  3359. {
  3360. case C_NEG: i1 = -i1; break;
  3361. case C_NOT: i1 = ~i1; break;
  3362. case C_LNOT: i1 = !i1; break;
  3363. default:
  3364. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3365. script_reportsrc(st);
  3366. script_pushnil(st);
  3367. st->state = END;
  3368. return;
  3369. }
  3370. script_pushint(st, i1);
  3371. }
  3372. /// Checks the type of all arguments passed to a built-in function.
  3373. ///
  3374. /// @param st Script state whose stack arguments should be inspected.
  3375. /// @param func Built-in function for which the arguments are intended.
  3376. static void script_check_buildin_argtype(struct script_state* st, int func)
  3377. {
  3378. int idx, invalid = 0;
  3379. for( idx = 2; script_hasdata(st, idx); idx++ )
  3380. {
  3381. struct script_data* data = script_getdata(st, idx);
  3382. script_function* sf = &buildin_func[str_data[func].val];
  3383. char type = sf->arg[idx-2];
  3384. if( type == '?' || type == '*' )
  3385. {// optional argument or unknown number of optional parameters ( no types are after this )
  3386. break;
  3387. }
  3388. else if( type == 0 )
  3389. {// more arguments than necessary ( should not happen, as it is checked before )
  3390. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3391. invalid++;
  3392. break;
  3393. }
  3394. else
  3395. {
  3396. const char* name = NULL;
  3397. if( data_isreference(data) )
  3398. {// get name for variables to determine the type they refer to
  3399. name = reference_getname(data);
  3400. }
  3401. switch( type )
  3402. {
  3403. case 'v':
  3404. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3405. {// variant
  3406. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3407. script_reportdata(data);
  3408. invalid++;
  3409. }
  3410. break;
  3411. case 's':
  3412. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3413. {// string
  3414. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3415. script_reportdata(data);
  3416. invalid++;
  3417. }
  3418. break;
  3419. case 'i':
  3420. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3421. {// int ( params and constants are always int )
  3422. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3423. script_reportdata(data);
  3424. invalid++;
  3425. }
  3426. break;
  3427. case 'r':
  3428. if( !data_isreference(data) )
  3429. {// variables
  3430. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3431. script_reportdata(data);
  3432. invalid++;
  3433. }
  3434. break;
  3435. case 'l':
  3436. if( !data_islabel(data) && !data_isfunclabel(data) )
  3437. {// label
  3438. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3439. script_reportdata(data);
  3440. invalid++;
  3441. }
  3442. break;
  3443. }
  3444. }
  3445. }
  3446. if(invalid)
  3447. {
  3448. ShowDebug("Function: %s\n", get_str(func));
  3449. script_reportsrc(st);
  3450. }
  3451. }
  3452. /// Executes a buildin command.
  3453. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3454. int run_func(struct script_state *st)
  3455. {
  3456. struct script_data* data;
  3457. int i,start_sp,end_sp,func;
  3458. end_sp = st->stack->sp;// position after the last argument
  3459. for( i = end_sp-1; i > 0 ; --i )
  3460. if( st->stack->stack_data[i].type == C_ARG )
  3461. break;
  3462. if( i == 0 ) {
  3463. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3464. st->state = END;
  3465. script_reportsrc(st);
  3466. return 1;
  3467. }
  3468. start_sp = i-1;// C_NAME of the command
  3469. st->start = start_sp;
  3470. st->end = end_sp;
  3471. data = &st->stack->stack_data[st->start];
  3472. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3473. func = (int)data->u.num;
  3474. st->funcname = reference_getname(data);
  3475. } else {
  3476. ShowError("script:run_func: not a buildin command.\n");
  3477. script_reportdata(data);
  3478. script_reportsrc(st);
  3479. st->state = END;
  3480. return 1;
  3481. }
  3482. if( script_config.warn_func_mismatch_argtypes ) {
  3483. script_check_buildin_argtype(st, func);
  3484. }
  3485. if(str_data[func].func) {
  3486. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3487. script_reportsrc(st);
  3488. } else {
  3489. 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));
  3490. script_reportsrc(st);
  3491. st->state = END;
  3492. }
  3493. // Stack's datum are used when re-running functions [Eoe]
  3494. if( st->state == RERUNLINE )
  3495. return 0;
  3496. pop_stack(st, st->start, st->end);
  3497. if( st->state == RETFUNC ) {// return from a user-defined function
  3498. struct script_retinfo* ri;
  3499. int olddefsp = st->stack->defsp;
  3500. int nargs;
  3501. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3502. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3503. {
  3504. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3505. script_reportsrc(st);
  3506. st->state = END;
  3507. return 1;
  3508. }
  3509. script_free_vars(st->stack->scope.vars);
  3510. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3511. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3512. nargs = ri->nargs;
  3513. st->pos = ri->pos;
  3514. st->script = ri->script;
  3515. st->stack->scope.vars = ri->scope.vars;
  3516. st->stack->scope.arrays = ri->scope.arrays;
  3517. st->stack->defsp = ri->defsp;
  3518. memset(ri, 0, sizeof(struct script_retinfo));
  3519. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3520. st->state = GOTO;
  3521. }
  3522. return 0;
  3523. }
  3524. /*==========================================
  3525. * script execution
  3526. *------------------------------------------*/
  3527. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3528. {
  3529. struct script_state *st;
  3530. if( rootscript == NULL || pos < 0 )
  3531. return;
  3532. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3533. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3534. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3535. st = script_alloc_state(rootscript, pos, rid, oid);
  3536. run_script_main(st);
  3537. }
  3538. void script_stop_instances(struct script_code *code) {
  3539. DBIterator *iter;
  3540. struct script_state* st;
  3541. if( !active_scripts )
  3542. return; // Don't even bother.
  3543. iter = db_iterator(st_db);
  3544. for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) {
  3545. if( st->script == code )
  3546. script_free_state(st);
  3547. }
  3548. dbi_destroy(iter);
  3549. }
  3550. /*==========================================
  3551. * Timer function for sleep
  3552. *------------------------------------------*/
  3553. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3554. {
  3555. struct script_state *st = (struct script_state *)data;
  3556. TBL_PC *sd = map_id2sd(st->rid);
  3557. if ((sd && sd->status.char_id != id) || (st->rid && !sd)) { // Character mismatch. Cancel execution.
  3558. st->rid = 0;
  3559. st->state = END;
  3560. }
  3561. st->sleep.timer = INVALID_TIMER;
  3562. if(st->state != RERUNLINE)
  3563. st->sleep.tick = 0;
  3564. run_script_main(st);
  3565. return 0;
  3566. }
  3567. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3568. ///
  3569. /// @param st Script state to detach.
  3570. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3571. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3572. {
  3573. struct map_session_data* sd;
  3574. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3575. sd->st = st->bk_st;
  3576. sd->npc_id = st->bk_npcid;
  3577. sd->state.disable_atcommand_on_npc = 0;
  3578. if(st->bk_st) {
  3579. //Remove tag for removal.
  3580. st->bk_st = NULL;
  3581. st->bk_npcid = 0;
  3582. } else if(dequeue_event) {
  3583. #ifdef SECURE_NPCTIMEOUT
  3584. /**
  3585. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3586. **/
  3587. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3588. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3589. sd->npc_idle_timer = INVALID_TIMER;
  3590. }
  3591. #endif
  3592. npc_event_dequeue(sd);
  3593. }
  3594. }
  3595. else if(st->bk_st)
  3596. {// rid was set to 0, before detaching the script state
  3597. 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);
  3598. script_reportsrc(st->bk_st);
  3599. script_free_state(st->bk_st);
  3600. st->bk_st = NULL;
  3601. }
  3602. }
  3603. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3604. ///
  3605. /// @param st Script state to attach.
  3606. static void script_attach_state(struct script_state* st)
  3607. {
  3608. struct map_session_data* sd;
  3609. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3610. {
  3611. if(st!=sd->st)
  3612. {
  3613. if(st->bk_st)
  3614. {// there is already a backup
  3615. 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);
  3616. }
  3617. st->bk_st = sd->st;
  3618. st->bk_npcid = sd->npc_id;
  3619. }
  3620. sd->st = st;
  3621. sd->npc_id = st->oid;
  3622. sd->npc_item_flag = st->npc_item_flag; // load default.
  3623. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3624. #ifdef SECURE_NPCTIMEOUT
  3625. if( sd->npc_idle_timer == INVALID_TIMER )
  3626. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3627. sd->npc_idle_tick = gettick();
  3628. #endif
  3629. }
  3630. }
  3631. /*==========================================
  3632. * The main part of the script execution
  3633. *------------------------------------------*/
  3634. void run_script_main(struct script_state *st)
  3635. {
  3636. int cmdcount = script_config.check_cmdcount;
  3637. int gotocount = script_config.check_gotocount;
  3638. TBL_PC *sd;
  3639. struct script_stack *stack = st->stack;
  3640. script_attach_state(st);
  3641. if(st->state == RERUNLINE) {
  3642. run_func(st);
  3643. if(st->state == GOTO)
  3644. st->state = RUN;
  3645. } else if(st->state != END)
  3646. st->state = RUN;
  3647. while(st->state == RUN) {
  3648. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3649. switch(c){
  3650. case C_EOL:
  3651. if( stack->defsp > stack->sp )
  3652. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3653. else
  3654. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3655. break;
  3656. case C_INT:
  3657. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3658. break;
  3659. case C_POS:
  3660. case C_NAME:
  3661. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3662. st->pos+=3;
  3663. break;
  3664. case C_ARG:
  3665. push_val(stack,c,0);
  3666. break;
  3667. case C_STR:
  3668. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3669. while(st->script->script_buf[st->pos++]);
  3670. break;
  3671. case C_FUNC:
  3672. run_func(st);
  3673. if(st->state==GOTO){
  3674. st->state = RUN;
  3675. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3676. ShowError("script:run_script_main: infinity loop !\n");
  3677. script_reportsrc(st);
  3678. st->state=END;
  3679. }
  3680. }
  3681. break;
  3682. case C_REF:
  3683. st->op2ref = 1;
  3684. break;
  3685. case C_NEG:
  3686. case C_NOT:
  3687. case C_LNOT:
  3688. op_1(st ,c);
  3689. break;
  3690. case C_ADD:
  3691. case C_SUB:
  3692. case C_MUL:
  3693. case C_DIV:
  3694. case C_MOD:
  3695. case C_EQ:
  3696. case C_NE:
  3697. case C_GT:
  3698. case C_GE:
  3699. case C_LT:
  3700. case C_LE:
  3701. case C_AND:
  3702. case C_OR:
  3703. case C_XOR:
  3704. case C_LAND:
  3705. case C_LOR:
  3706. case C_R_SHIFT:
  3707. case C_L_SHIFT:
  3708. op_2(st, c);
  3709. break;
  3710. case C_OP3:
  3711. op_3(st, c);
  3712. break;
  3713. case C_NOP:
  3714. st->state=END;
  3715. break;
  3716. default:
  3717. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3718. st->state=END;
  3719. break;
  3720. }
  3721. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3722. ShowError("script:run_script_main: infinity loop !\n");
  3723. script_reportsrc(st);
  3724. st->state=END;
  3725. }
  3726. }
  3727. if(st->sleep.tick > 0) {
  3728. //Restore previous script
  3729. script_detach_state(st, false);
  3730. //Delay execution
  3731. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3732. st->sleep.charid = sd?sd->status.char_id:0;
  3733. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3734. run_script_timer, st->sleep.charid, (intptr_t)st);
  3735. } else if(st->state != END && st->rid) {
  3736. //Resume later (st is already attached to player).
  3737. if(st->bk_st) {
  3738. ShowWarning("Unable to restore stack! Double continuation!\n");
  3739. //Report BOTH scripts to see if that can help somehow.
  3740. ShowDebug("Previous script (lost):\n");
  3741. script_reportsrc(st->bk_st);
  3742. ShowDebug("Current script:\n");
  3743. script_reportsrc(st);
  3744. script_free_state(st->bk_st);
  3745. st->bk_st = NULL;
  3746. }
  3747. } else {
  3748. //Dispose of script.
  3749. if ((sd = map_id2sd(st->rid))!=NULL)
  3750. { //Restore previous stack and save char.
  3751. if(sd->state.using_fake_npc){
  3752. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3753. sd->state.using_fake_npc = 0;
  3754. }
  3755. //Restore previous script if any.
  3756. script_detach_state(st, true);
  3757. if (sd->vars_dirty)
  3758. intif_saveregistry(sd);
  3759. }
  3760. script_free_state(st);
  3761. }
  3762. }
  3763. int script_config_read(char *cfgName)
  3764. {
  3765. int i;
  3766. char line[1024],w1[1024],w2[1024];
  3767. FILE *fp;
  3768. fp=fopen(cfgName,"r");
  3769. if(fp==NULL){
  3770. ShowError("File not found: %s\n", cfgName);
  3771. return 1;
  3772. }
  3773. while(fgets(line, sizeof(line), fp))
  3774. {
  3775. if(line[0] == '/' && line[1] == '/')
  3776. continue;
  3777. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3778. if(i!=2)
  3779. continue;
  3780. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3781. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3782. }
  3783. else if(strcmpi(w1,"check_cmdcount")==0) {
  3784. script_config.check_cmdcount = config_switch(w2);
  3785. }
  3786. else if(strcmpi(w1,"check_gotocount")==0) {
  3787. script_config.check_gotocount = config_switch(w2);
  3788. }
  3789. else if(strcmpi(w1,"input_min_value")==0) {
  3790. script_config.input_min_value = config_switch(w2);
  3791. }
  3792. else if(strcmpi(w1,"input_max_value")==0) {
  3793. script_config.input_max_value = config_switch(w2);
  3794. }
  3795. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3796. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3797. }
  3798. else if(strcmpi(w1,"import")==0){
  3799. script_config_read(w2);
  3800. }
  3801. else {
  3802. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3803. }
  3804. }
  3805. fclose(fp);
  3806. return 0;
  3807. }
  3808. /**
  3809. * @see DBApply
  3810. */
  3811. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3812. {
  3813. struct script_code *code = db_data2ptr(data);
  3814. if (code)
  3815. script_free_code(code);
  3816. return 0;
  3817. }
  3818. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3819. {
  3820. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3821. if( script )
  3822. {
  3823. int j;
  3824. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  3825. if( j < EQI_MAX ) {
  3826. //Single item autobonus
  3827. current_equip_item_index = sd->equip_index[j];
  3828. current_equip_combo_pos = 0;
  3829. } else {
  3830. //Combo autobonus
  3831. current_equip_item_index = -1;
  3832. current_equip_combo_pos = pos;
  3833. }
  3834. run_script(script,0,sd->bl.id,0);
  3835. }
  3836. }
  3837. void script_add_autobonus(const char *autobonus)
  3838. {
  3839. if( strdb_get(autobonus_db, autobonus) == NULL )
  3840. {
  3841. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3842. if( script )
  3843. strdb_put(autobonus_db, autobonus, script);
  3844. }
  3845. }
  3846. /// resets a temporary character array variable to given value
  3847. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3848. {
  3849. struct script_array *sa = NULL;
  3850. struct reg_db *src = NULL;
  3851. unsigned int i, *list = NULL, size = 0;
  3852. int key;
  3853. key = add_str(varname);
  3854. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  3855. return;
  3856. if( value )
  3857. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  3858. if( !(sa = idb_get(src->arrays, key)) ) /* non-existent array, nothing to empty */
  3859. return;
  3860. size = sa->size;
  3861. list = script_array_cpy_list(sa);
  3862. for(i = 0; i < size; i++) {
  3863. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  3864. }
  3865. }
  3866. /// sets a temporary character array variable element idx to given value
  3867. /// @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.
  3868. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  3869. {
  3870. int key;
  3871. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  3872. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  3873. return;
  3874. }
  3875. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3876. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  3877. if( refcache ) {
  3878. // save to avoid repeated script->add_str calls
  3879. refcache[0] = key;
  3880. }
  3881. }
  3882. /**
  3883. * Clears persistent variables from memory
  3884. **/
  3885. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  3886. {
  3887. struct script_reg_state *src;
  3888. if( data->type != DB_DATA_PTR ) // got no need for those!
  3889. return 0;
  3890. src = db_data2ptr(data);
  3891. if( src->type ) {
  3892. struct script_reg_str *p = (struct script_reg_str *)src;
  3893. if( p->value )
  3894. aFree(p->value);
  3895. ers_free(str_reg_ers,p);
  3896. } else {
  3897. ers_free(num_reg_ers,(struct script_reg_num*)src);
  3898. }
  3899. return 0;
  3900. }
  3901. /**
  3902. * Clears a single persistent variable
  3903. **/
  3904. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  3905. {
  3906. i64db_remove(sd->regs.vars, reg);
  3907. if( data->type ) {
  3908. struct script_reg_str *p = (struct script_reg_str*)data;
  3909. if( p->value )
  3910. aFree(p->value);
  3911. ers_free(str_reg_ers,p);
  3912. } else {
  3913. ers_free(num_reg_ers,(struct script_reg_num*)data);
  3914. }
  3915. }
  3916. unsigned int *script_array_cpy_list(struct script_array *sa)
  3917. {
  3918. if( sa->size > generic_ui_array_size )
  3919. script_generic_ui_array_expand(sa->size);
  3920. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  3921. return generic_ui_array;
  3922. }
  3923. void script_generic_ui_array_expand (unsigned int plus)
  3924. {
  3925. generic_ui_array_size += plus + 100;
  3926. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  3927. }
  3928. int buildin_query_sql_sub(struct script_state *st, Sql *handle);
  3929. #ifdef BETA_THREAD_TEST
  3930. /* used to receive items the queryThread has already processed */
  3931. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3932. int i, cursor = 0;
  3933. bool allOk = true;
  3934. EnterSpinLock(&queryThreadLock);
  3935. for( i = 0; i < queryThreadData.count; i++ ) {
  3936. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3937. if( !entry->ok ) {
  3938. allOk = false;
  3939. continue;
  3940. }
  3941. run_script_main(entry->st);
  3942. entry->st = NULL;/* empty entries */
  3943. aFree(entry);
  3944. queryThreadData.entry[i] = NULL;
  3945. }
  3946. if( allOk ) {
  3947. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3948. delete_timer(queryThreadData.timer, queryThread_timer);
  3949. queryThreadData.timer = INVALID_TIMER;
  3950. }
  3951. /* now lets clear the mess. */
  3952. for( i = 0; i < queryThreadData.count; i++ ) {
  3953. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3954. if( entry == NULL )
  3955. continue;/* entry on hold */
  3956. /* move */
  3957. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3958. cursor++;
  3959. }
  3960. queryThreadData.count = cursor;
  3961. LeaveSpinLock(&queryThreadLock);
  3962. return 0;
  3963. }
  3964. void queryThread_add(struct script_state *st, bool type) {
  3965. int idx = 0;
  3966. struct queryThreadEntry* entry = NULL;
  3967. EnterSpinLock(&queryThreadLock);
  3968. if( queryThreadData.count++ != 0 )
  3969. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3970. idx = queryThreadData.count-1;
  3971. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3972. entry = queryThreadData.entry[idx];
  3973. entry->st = st;
  3974. entry->ok = false;
  3975. entry->type = type;
  3976. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3977. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3978. }
  3979. LeaveSpinLock(&queryThreadLock);
  3980. /* unlock the queryThread */
  3981. racond_signal(queryThreadCond);
  3982. }
  3983. /* adds a new log to the queue */
  3984. void queryThread_log(char * entry, int length) {
  3985. int idx = logThreadData.count;
  3986. EnterSpinLock(&queryThreadLock);
  3987. if( logThreadData.count++ != 0 )
  3988. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3989. CREATE(logThreadData.entry[idx], char, length + 1 );
  3990. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3991. LeaveSpinLock(&queryThreadLock);
  3992. /* unlock the queryThread */
  3993. racond_signal(queryThreadCond);
  3994. }
  3995. /* queryThread_main */
  3996. static void *queryThread_main(void *x) {
  3997. Sql *queryThread_handle = Sql_Malloc();
  3998. int i;
  3999. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  4000. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4001. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  4002. Sql_ShowDebug(queryThread_handle);
  4003. Sql_Free(queryThread_handle);
  4004. exit(EXIT_FAILURE);
  4005. }
  4006. if( strlen(default_codepage) > 0 )
  4007. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  4008. Sql_ShowDebug(queryThread_handle);
  4009. if( log_config.sql_logs ) {
  4010. logmysql_handle = Sql_Malloc();
  4011. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  4012. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4013. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  4014. Sql_ShowDebug(logmysql_handle);
  4015. Sql_Free(logmysql_handle);
  4016. exit(EXIT_FAILURE);
  4017. }
  4018. if( strlen(default_codepage) > 0 )
  4019. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  4020. Sql_ShowDebug(logmysql_handle);
  4021. }
  4022. while( 1 ) {
  4023. if(queryThreadTerminate > 0)
  4024. break;
  4025. EnterSpinLock(&queryThreadLock);
  4026. /* mess with queryThreadData within the lock */
  4027. for( i = 0; i < queryThreadData.count; i++ ) {
  4028. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4029. if( entry->ok )
  4030. continue;
  4031. else if ( !entry->st || !entry->st->stack ) {
  4032. entry->ok = true;/* dispose */
  4033. continue;
  4034. }
  4035. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  4036. entry->ok = true;/* we're done with this */
  4037. }
  4038. /* also check for any logs in need to be sent */
  4039. if( log_config.sql_logs ) {
  4040. for( i = 0; i < logThreadData.count; i++ ) {
  4041. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  4042. Sql_ShowDebug(logmysql_handle);
  4043. aFree(logThreadData.entry[i]);
  4044. }
  4045. logThreadData.count = 0;
  4046. }
  4047. LeaveSpinLock(&queryThreadLock);
  4048. ramutex_lock( queryThreadMutex );
  4049. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  4050. ramutex_unlock( queryThreadMutex );
  4051. }
  4052. Sql_Free(queryThread_handle);
  4053. if( log_config.sql_logs ) {
  4054. Sql_Free(logmysql_handle);
  4055. }
  4056. return NULL;
  4057. }
  4058. #endif
  4059. /*==========================================
  4060. * Destructor
  4061. *------------------------------------------*/
  4062. void do_final_script() {
  4063. int i;
  4064. DBIterator *iter;
  4065. struct script_state *st;
  4066. #ifdef DEBUG_HASH
  4067. if (battle_config.etc_log)
  4068. {
  4069. FILE *fp = fopen("hash_dump.txt","wt");
  4070. if(fp) {
  4071. int count[SCRIPT_HASH_SIZE];
  4072. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4073. int n=0;
  4074. int min=INT_MAX,max=0,zero=0;
  4075. double mean=0.0f;
  4076. double median=0.0f;
  4077. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4078. memset(count, 0, sizeof(count));
  4079. fprintf(fp,"num : hash : data_name\n");
  4080. fprintf(fp,"---------------------------------------------------------------\n");
  4081. for(i=LABEL_START; i<str_num; i++) {
  4082. unsigned int h = calc_hash(get_str(i));
  4083. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4084. ++count[h];
  4085. }
  4086. fprintf(fp,"--------------------\n\n");
  4087. memset(count2, 0, sizeof(count2));
  4088. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4089. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4090. if(min > count[i])
  4091. min = count[i]; // minimun count of collision
  4092. if(max < count[i])
  4093. max = count[i]; // maximun count of collision
  4094. if(count[i] == 0)
  4095. zero++;
  4096. ++count2[count[i]];
  4097. }
  4098. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4099. for( i=min; i <= max; ++i ){
  4100. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4101. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4102. }
  4103. for( i=min; i <= max; ++i ){
  4104. n += count2[i];
  4105. if( n*2 >= SCRIPT_HASH_SIZE )
  4106. {
  4107. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4108. median = (i+i+1)/2.0f;
  4109. else
  4110. median = i;
  4111. break;
  4112. }
  4113. }
  4114. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4115. fclose(fp);
  4116. }
  4117. }
  4118. #endif
  4119. mapreg_final();
  4120. db_destroy(scriptlabel_db);
  4121. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4122. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4123. ers_destroy(array_ers);
  4124. if (generic_ui_array)
  4125. aFree(generic_ui_array);
  4126. iter = db_iterator(st_db);
  4127. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4128. script_free_state(st);
  4129. dbi_destroy(iter);
  4130. if (str_data)
  4131. aFree(str_data);
  4132. if (str_buf)
  4133. aFree(str_buf);
  4134. for( i = 0; i < atcmd_binding_count; i++ ) {
  4135. aFree(atcmd_binding[i]);
  4136. }
  4137. if( atcmd_binding_count != 0 )
  4138. aFree(atcmd_binding);
  4139. ers_destroy(st_ers);
  4140. ers_destroy(stack_ers);
  4141. db_destroy(st_db);
  4142. #ifdef BETA_THREAD_TEST
  4143. /* QueryThread */
  4144. InterlockedIncrement(&queryThreadTerminate);
  4145. racond_signal(queryThreadCond);
  4146. rathread_wait(queryThread, NULL);
  4147. // Destroy cond var and mutex.
  4148. racond_destroy( queryThreadCond );
  4149. ramutex_destroy( queryThreadMutex );
  4150. /* Clear missing vars */
  4151. for( i = 0; i < queryThreadData.count; i++ ) {
  4152. aFree(queryThreadData.entry[i]);
  4153. }
  4154. aFree(queryThreadData.entry);
  4155. for( i = 0; i < logThreadData.count; i++ ) {
  4156. aFree(logThreadData.entry[i]);
  4157. }
  4158. aFree(logThreadData.entry);
  4159. #endif
  4160. }
  4161. /*==========================================
  4162. * Initialization
  4163. *------------------------------------------*/
  4164. void do_init_script(void) {
  4165. st_db = idb_alloc(DB_OPT_BASE);
  4166. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4167. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4168. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4169. st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_CACHE_OPTIONS);
  4170. stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_FLEX_CHUNK);
  4171. 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
  4172. ers_chunk_size(st_ers, 10);
  4173. ers_chunk_size(stack_ers, 10);
  4174. active_scripts = 0;
  4175. next_id = 0;
  4176. mapreg_init();
  4177. #ifdef BETA_THREAD_TEST
  4178. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4179. queryThreadData.count = 0;
  4180. CREATE(logThreadData.entry, char *, 1);
  4181. logThreadData.count = 0;
  4182. /* QueryThread Start */
  4183. InitializeSpinLock(&queryThreadLock);
  4184. queryThreadData.timer = INVALID_TIMER;
  4185. queryThreadTerminate = 0;
  4186. queryThreadMutex = ramutex_create();
  4187. queryThreadCond = racond_create();
  4188. queryThread = rathread_create(queryThread_main, NULL);
  4189. if(queryThread == NULL){
  4190. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4191. exit(EXIT_FAILURE);
  4192. }
  4193. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4194. #endif
  4195. }
  4196. void script_reload(void) {
  4197. int i;
  4198. DBIterator *iter;
  4199. struct script_state *st;
  4200. #ifdef BETA_THREAD_TEST
  4201. /* we're reloading so any queries undergoing should be...exterminated. */
  4202. EnterSpinLock(&queryThreadLock);
  4203. for( i = 0; i < queryThreadData.count; i++ ) {
  4204. aFree(queryThreadData.entry[i]);
  4205. }
  4206. queryThreadData.count = 0;
  4207. if( queryThreadData.timer != INVALID_TIMER ) {
  4208. delete_timer(queryThreadData.timer, queryThread_timer);
  4209. queryThreadData.timer = INVALID_TIMER;
  4210. }
  4211. LeaveSpinLock(&queryThreadLock);
  4212. #endif
  4213. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4214. db_clear(scriptlabel_db);
  4215. // @commands (script based)
  4216. // Clear bindings
  4217. for( i = 0; i < atcmd_binding_count; i++ ) {
  4218. aFree(atcmd_binding[i]);
  4219. }
  4220. if( atcmd_binding_count != 0 )
  4221. aFree(atcmd_binding);
  4222. atcmd_binding_count = 0;
  4223. iter = db_iterator(st_db);
  4224. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4225. script_free_state(st);
  4226. dbi_destroy(iter);
  4227. db_clear(st_db);
  4228. mapreg_reload();
  4229. }
  4230. //-----------------------------------------------------------------------------
  4231. // buildin functions
  4232. //
  4233. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4234. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4235. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4236. /////////////////////////////////////////////////////////////////////
  4237. // NPC interaction
  4238. //
  4239. /// Appends a message to the npc dialog.
  4240. /// If a dialog doesn't exist yet, one is created.
  4241. ///
  4242. /// mes "<message>";
  4243. BUILDIN_FUNC(mes)
  4244. {
  4245. TBL_PC* sd = script_rid2sd(st);
  4246. if( sd == NULL )
  4247. return SCRIPT_CMD_SUCCESS;
  4248. if( !script_hasdata(st, 3) )
  4249. {// only a single line detected in the script
  4250. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4251. }
  4252. else
  4253. {// parse multiple lines as they exist
  4254. int i;
  4255. for( i = 2; script_hasdata(st, i); i++ )
  4256. {
  4257. // send the message to the client
  4258. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4259. }
  4260. }
  4261. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4262. return SCRIPT_CMD_SUCCESS;
  4263. }
  4264. /// Displays the button 'next' in the npc dialog.
  4265. /// The dialog text is cleared and the script continues when the button is pressed.
  4266. ///
  4267. /// next;
  4268. BUILDIN_FUNC(next)
  4269. {
  4270. TBL_PC* sd;
  4271. sd = script_rid2sd(st);
  4272. if( sd == NULL )
  4273. return SCRIPT_CMD_SUCCESS;
  4274. #ifdef SECURE_NPCTIMEOUT
  4275. sd->npc_idle_type = NPCT_WAIT;
  4276. #endif
  4277. st->state = STOP;
  4278. clif_scriptnext(sd, st->oid);
  4279. return SCRIPT_CMD_SUCCESS;
  4280. }
  4281. /// Ends the script and displays the button 'close' on the npc dialog.
  4282. /// The dialog is closed when the button is pressed.
  4283. ///
  4284. /// close;
  4285. BUILDIN_FUNC(close)
  4286. {
  4287. TBL_PC* sd;
  4288. sd = script_rid2sd(st);
  4289. if( sd == NULL )
  4290. return SCRIPT_CMD_SUCCESS;
  4291. if( !st->mes_active ) {
  4292. TBL_NPC* nd = map_id2nd(st->oid);
  4293. st->state = END; // Keep backwards compatibility.
  4294. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4295. } else {
  4296. st->state = CLOSE;
  4297. st->mes_active = 0;
  4298. }
  4299. clif_scriptclose(sd, st->oid);
  4300. return SCRIPT_CMD_SUCCESS;
  4301. }
  4302. /// Displays the button 'close' on the npc dialog.
  4303. /// The dialog is closed and the script continues when the button is pressed.
  4304. ///
  4305. /// close2;
  4306. BUILDIN_FUNC(close2)
  4307. {
  4308. TBL_PC* sd;
  4309. sd = script_rid2sd(st);
  4310. if( sd == NULL )
  4311. return SCRIPT_CMD_SUCCESS;
  4312. st->state = STOP;
  4313. if( st->mes_active )
  4314. st->mes_active = 0;
  4315. clif_scriptclose(sd, st->oid);
  4316. return SCRIPT_CMD_SUCCESS;
  4317. }
  4318. /// Counts the number of valid and total number of options in 'str'
  4319. /// If max_count > 0 the counting stops when that valid option is reached
  4320. /// total is incremented for each option (NULL is supported)
  4321. static int menu_countoptions(const char* str, int max_count, int* total)
  4322. {
  4323. int count = 0;
  4324. int bogus_total;
  4325. if( total == NULL )
  4326. total = &bogus_total;
  4327. ++(*total);
  4328. // initial empty options
  4329. while( *str == ':' )
  4330. {
  4331. ++str;
  4332. ++(*total);
  4333. }
  4334. // count menu options
  4335. while( *str != '\0' )
  4336. {
  4337. ++count;
  4338. --max_count;
  4339. if( max_count == 0 )
  4340. break;
  4341. while( *str != ':' && *str != '\0' )
  4342. ++str;
  4343. while( *str == ':' )
  4344. {
  4345. ++str;
  4346. ++(*total);
  4347. }
  4348. }
  4349. return count;
  4350. }
  4351. /// Displays a menu with options and goes to the target label.
  4352. /// The script is stopped if cancel is pressed.
  4353. /// Options with no text are not displayed in the client.
  4354. ///
  4355. /// Options can be grouped together, separated by the character ':' in the text:
  4356. /// ex: menu "A:B:C",L_target;
  4357. /// All these options go to the specified target label.
  4358. ///
  4359. /// The index of the selected option is put in the variable @menu.
  4360. /// Indexes start with 1 and are consistent with grouped and empty options.
  4361. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4362. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4363. ///
  4364. /// NOTE: the client closes the npc dialog when cancel is pressed
  4365. ///
  4366. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4367. BUILDIN_FUNC(menu)
  4368. {
  4369. int i;
  4370. const char* text;
  4371. TBL_PC* sd;
  4372. sd = script_rid2sd(st);
  4373. if( sd == NULL )
  4374. return SCRIPT_CMD_SUCCESS;
  4375. #ifdef SECURE_NPCTIMEOUT
  4376. sd->npc_idle_type = NPCT_MENU;
  4377. #endif
  4378. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4379. if( sd->state.menu_or_input == 0 )
  4380. {
  4381. struct StringBuf buf;
  4382. if( script_lastdata(st) % 2 == 0 )
  4383. {// argument count is not even (1st argument is at index 2)
  4384. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4385. st->state = END;
  4386. return SCRIPT_CMD_FAILURE;
  4387. }
  4388. StringBuf_Init(&buf);
  4389. sd->npc_menu = 0;
  4390. for( i = 2; i < script_lastdata(st); i += 2 )
  4391. {
  4392. struct script_data* data;
  4393. // menu options
  4394. text = script_getstr(st, i);
  4395. // target label
  4396. data = script_getdata(st, i+1);
  4397. if( !data_islabel(data) )
  4398. {// not a label
  4399. StringBuf_Destroy(&buf);
  4400. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4401. script_reportdata(data);
  4402. st->state = END;
  4403. return SCRIPT_CMD_FAILURE;
  4404. }
  4405. // append option(s)
  4406. if( text[0] == '\0' )
  4407. continue;// empty string, ignore
  4408. if( sd->npc_menu > 0 )
  4409. StringBuf_AppendStr(&buf, ":");
  4410. StringBuf_AppendStr(&buf, text);
  4411. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4412. }
  4413. st->state = RERUNLINE;
  4414. sd->state.menu_or_input = 1;
  4415. /**
  4416. * menus beyond this length crash the client (see bugreport:6402)
  4417. **/
  4418. if( StringBuf_Length(&buf) >= 2047 ) {
  4419. struct npc_data * nd = map_id2nd(st->oid);
  4420. char* menu;
  4421. CREATE(menu, char, 2048);
  4422. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4423. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4424. clif_scriptmenu(sd, st->oid, menu);
  4425. aFree(menu);
  4426. } else
  4427. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4428. StringBuf_Destroy(&buf);
  4429. if( sd->npc_menu >= 0xff )
  4430. {// 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
  4431. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4432. script_reportsrc(st);
  4433. }
  4434. }
  4435. else if( sd->npc_menu == 0xff )
  4436. {// Cancel was pressed
  4437. sd->state.menu_or_input = 0;
  4438. st->state = END;
  4439. }
  4440. else
  4441. {// goto target label
  4442. int menu = 0;
  4443. sd->state.menu_or_input = 0;
  4444. if( sd->npc_menu <= 0 )
  4445. {
  4446. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4447. st->state = END;
  4448. return SCRIPT_CMD_FAILURE;
  4449. }
  4450. // get target label
  4451. for( i = 2; i < script_lastdata(st); i += 2 )
  4452. {
  4453. text = script_getstr(st, i);
  4454. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4455. if( sd->npc_menu <= 0 )
  4456. break;// entry found
  4457. }
  4458. if( sd->npc_menu > 0 )
  4459. {// Invalid selection
  4460. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4461. st->state = END;
  4462. return SCRIPT_CMD_FAILURE;
  4463. }
  4464. if( !data_islabel(script_getdata(st, i + 1)) )
  4465. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4466. ShowError("buildin_menu: Unexpected data in label argument\n");
  4467. script_reportdata(script_getdata(st, i + 1));
  4468. st->state = END;
  4469. return SCRIPT_CMD_FAILURE;
  4470. }
  4471. pc_setreg(sd, add_str("@menu"), menu);
  4472. st->pos = script_getnum(st, i + 1);
  4473. st->state = GOTO;
  4474. }
  4475. return SCRIPT_CMD_SUCCESS;
  4476. }
  4477. /// Displays a menu with options and returns the selected option.
  4478. /// Behaves like 'menu' without the target labels.
  4479. ///
  4480. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4481. ///
  4482. /// @see menu
  4483. BUILDIN_FUNC(select)
  4484. {
  4485. int i;
  4486. const char* text;
  4487. TBL_PC* sd;
  4488. sd = script_rid2sd(st);
  4489. if( sd == NULL )
  4490. return SCRIPT_CMD_SUCCESS;
  4491. #ifdef SECURE_NPCTIMEOUT
  4492. sd->npc_idle_type = NPCT_MENU;
  4493. #endif
  4494. if( sd->state.menu_or_input == 0 ) {
  4495. struct StringBuf buf;
  4496. StringBuf_Init(&buf);
  4497. sd->npc_menu = 0;
  4498. for( i = 2; i <= script_lastdata(st); ++i ) {
  4499. text = script_getstr(st, i);
  4500. if( sd->npc_menu > 0 )
  4501. StringBuf_AppendStr(&buf, ":");
  4502. StringBuf_AppendStr(&buf, text);
  4503. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4504. }
  4505. st->state = RERUNLINE;
  4506. sd->state.menu_or_input = 1;
  4507. /**
  4508. * menus beyond this length crash the client (see bugreport:6402)
  4509. **/
  4510. if( StringBuf_Length(&buf) >= 2047 ) {
  4511. struct npc_data * nd = map_id2nd(st->oid);
  4512. char* menu;
  4513. CREATE(menu, char, 2048);
  4514. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4515. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4516. clif_scriptmenu(sd, st->oid, menu);
  4517. aFree(menu);
  4518. } else
  4519. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4520. StringBuf_Destroy(&buf);
  4521. if( sd->npc_menu >= 0xff ) {
  4522. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4523. script_reportsrc(st);
  4524. }
  4525. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4526. sd->state.menu_or_input = 0;
  4527. st->state = END;
  4528. } else {// return selected option
  4529. int menu = 0;
  4530. sd->state.menu_or_input = 0;
  4531. for( i = 2; i <= script_lastdata(st); ++i ) {
  4532. text = script_getstr(st, i);
  4533. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4534. if( sd->npc_menu <= 0 )
  4535. break;// entry found
  4536. }
  4537. pc_setreg(sd, add_str("@menu"), menu);
  4538. script_pushint(st, menu);
  4539. st->state = RUN;
  4540. }
  4541. return SCRIPT_CMD_SUCCESS;
  4542. }
  4543. /// Displays a menu with options and returns the selected option.
  4544. /// Behaves like 'menu' without the target labels, except when cancel is
  4545. /// pressed.
  4546. /// When cancel is pressed, the script continues and 255 is returned.
  4547. ///
  4548. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4549. ///
  4550. /// @see menu
  4551. BUILDIN_FUNC(prompt)
  4552. {
  4553. int i;
  4554. const char *text;
  4555. TBL_PC* sd;
  4556. sd = script_rid2sd(st);
  4557. if( sd == NULL )
  4558. return SCRIPT_CMD_SUCCESS;
  4559. #ifdef SECURE_NPCTIMEOUT
  4560. sd->npc_idle_type = NPCT_MENU;
  4561. #endif
  4562. if( sd->state.menu_or_input == 0 )
  4563. {
  4564. struct StringBuf buf;
  4565. StringBuf_Init(&buf);
  4566. sd->npc_menu = 0;
  4567. for( i = 2; i <= script_lastdata(st); ++i )
  4568. {
  4569. text = script_getstr(st, i);
  4570. if( sd->npc_menu > 0 )
  4571. StringBuf_AppendStr(&buf, ":");
  4572. StringBuf_AppendStr(&buf, text);
  4573. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4574. }
  4575. st->state = RERUNLINE;
  4576. sd->state.menu_or_input = 1;
  4577. /**
  4578. * menus beyond this length crash the client (see bugreport:6402)
  4579. **/
  4580. if( StringBuf_Length(&buf) >= 2047 ) {
  4581. struct npc_data * nd = map_id2nd(st->oid);
  4582. char* menu;
  4583. CREATE(menu, char, 2048);
  4584. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4585. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4586. clif_scriptmenu(sd, st->oid, menu);
  4587. aFree(menu);
  4588. } else
  4589. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4590. StringBuf_Destroy(&buf);
  4591. if( sd->npc_menu >= 0xff )
  4592. {
  4593. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4594. script_reportsrc(st);
  4595. }
  4596. }
  4597. else if( sd->npc_menu == 0xff )
  4598. {// Cancel was pressed
  4599. sd->state.menu_or_input = 0;
  4600. pc_setreg(sd, add_str("@menu"), 0xff);
  4601. script_pushint(st, 0xff);
  4602. st->state = RUN;
  4603. }
  4604. else
  4605. {// return selected option
  4606. int menu = 0;
  4607. sd->state.menu_or_input = 0;
  4608. for( i = 2; i <= script_lastdata(st); ++i )
  4609. {
  4610. text = script_getstr(st, i);
  4611. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4612. if( sd->npc_menu <= 0 )
  4613. break;// entry found
  4614. }
  4615. pc_setreg(sd, add_str("@menu"), menu);
  4616. script_pushint(st, menu);
  4617. st->state = RUN;
  4618. }
  4619. return SCRIPT_CMD_SUCCESS;
  4620. }
  4621. /////////////////////////////////////////////////////////////////////
  4622. // ...
  4623. //
  4624. /// Jumps to the target script label.
  4625. ///
  4626. /// goto <label>;
  4627. BUILDIN_FUNC(goto)
  4628. {
  4629. if( !data_islabel(script_getdata(st,2)) )
  4630. {
  4631. ShowError("buildin_goto: Not a label\n");
  4632. script_reportdata(script_getdata(st,2));
  4633. st->state = END;
  4634. return SCRIPT_CMD_FAILURE;
  4635. }
  4636. st->pos = script_getnum(st,2);
  4637. st->state = GOTO;
  4638. return SCRIPT_CMD_SUCCESS;
  4639. }
  4640. /*==========================================
  4641. * user-defined function call
  4642. *------------------------------------------*/
  4643. BUILDIN_FUNC(callfunc)
  4644. {
  4645. int i, j;
  4646. struct script_retinfo* ri;
  4647. struct script_code* scr;
  4648. const char* str = script_getstr(st,2);
  4649. struct reg_db *ref = NULL;
  4650. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4651. if(!scr) {
  4652. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4653. st->state = END;
  4654. return SCRIPT_CMD_FAILURE;
  4655. }
  4656. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4657. ref[0].vars = st->stack->scope.vars;
  4658. if (!st->stack->scope.arrays)
  4659. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4660. ref[0].arrays = st->stack->scope.arrays;
  4661. ref[1].vars = st->script->local.vars;
  4662. if (!st->script->local.arrays)
  4663. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4664. ref[1].arrays = st->script->local.arrays;
  4665. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4666. struct script_data* data = push_copy(st->stack,i);
  4667. if (data_isreference(data) && !data->ref) {
  4668. const char* name = reference_getname(data);
  4669. if (name[0] == '.')
  4670. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4671. }
  4672. }
  4673. CREATE(ri, struct script_retinfo, 1);
  4674. ri->script = st->script; // script code
  4675. ri->scope.vars = st->stack->scope.vars; // scope variables
  4676. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4677. ri->pos = st->pos; // script location
  4678. ri->nargs = j; // argument count
  4679. ri->defsp = st->stack->defsp; // default stack pointer
  4680. push_retinfo(st->stack, ri, ref);
  4681. st->pos = 0;
  4682. st->script = scr;
  4683. st->stack->defsp = st->stack->sp;
  4684. st->state = GOTO;
  4685. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4686. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4687. if (!st->script->local.vars)
  4688. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4689. return SCRIPT_CMD_SUCCESS;
  4690. }
  4691. /*==========================================
  4692. * subroutine call
  4693. *------------------------------------------*/
  4694. BUILDIN_FUNC(callsub)
  4695. {
  4696. int i,j;
  4697. struct script_retinfo* ri;
  4698. int pos = script_getnum(st,2);
  4699. struct reg_db *ref = NULL;
  4700. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4701. ShowError("buildin_callsub: Argument is not a label\n");
  4702. script_reportdata(script_getdata(st,2));
  4703. st->state = END;
  4704. return SCRIPT_CMD_FAILURE;
  4705. }
  4706. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4707. ref[0].vars = st->stack->scope.vars;
  4708. if (!st->stack->scope.arrays)
  4709. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4710. ref[0].arrays = st->stack->scope.arrays;
  4711. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4712. struct script_data* data = push_copy(st->stack,i);
  4713. if (data_isreference(data) && !data->ref) {
  4714. const char* name = reference_getname(data);
  4715. if (name[0] == '.' && name[1] == '@')
  4716. data->ref = &ref[0];
  4717. }
  4718. }
  4719. CREATE(ri, struct script_retinfo, 1);
  4720. ri->script = st->script; // script code
  4721. ri->scope.vars = st->stack->scope.vars; // scope variables
  4722. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4723. ri->pos = st->pos; // script location
  4724. ri->nargs = j; // argument count
  4725. ri->defsp = st->stack->defsp; // default stack pointer
  4726. push_retinfo(st->stack, ri, ref);
  4727. st->pos = pos;
  4728. st->stack->defsp = st->stack->sp;
  4729. st->state = GOTO;
  4730. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4731. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4732. return SCRIPT_CMD_SUCCESS;
  4733. }
  4734. /// Retrieves an argument provided to callfunc/callsub.
  4735. /// If the argument doesn't exist
  4736. ///
  4737. /// getarg(<index>{,<default_value>}) -> <value>
  4738. BUILDIN_FUNC(getarg)
  4739. {
  4740. struct script_retinfo* ri;
  4741. int idx;
  4742. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4743. {
  4744. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4745. st->state = END;
  4746. return SCRIPT_CMD_FAILURE;
  4747. }
  4748. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4749. idx = script_getnum(st,2);
  4750. if( idx >= 0 && idx < ri->nargs )
  4751. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4752. else if( script_hasdata(st,3) )
  4753. script_pushcopy(st, 3);
  4754. else
  4755. {
  4756. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4757. st->state = END;
  4758. return SCRIPT_CMD_FAILURE;
  4759. }
  4760. return SCRIPT_CMD_SUCCESS;
  4761. }
  4762. /// Returns from the current function, optionaly returning a value from the functions.
  4763. /// Don't use outside script functions.
  4764. ///
  4765. /// return;
  4766. /// return <value>;
  4767. BUILDIN_FUNC(return)
  4768. {
  4769. if( script_hasdata(st,2) )
  4770. {// return value
  4771. struct script_data* data;
  4772. script_pushcopy(st, 2);
  4773. data = script_getdatatop(st, -1);
  4774. if( data_isreference(data) ) {
  4775. const char* name = reference_getname(data);
  4776. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4777. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4778. get_val(st, data); // current scope, convert to value
  4779. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4780. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4781. } else if( name[0] == '.' && !data->ref ) { // script variable, link to current script
  4782. data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4783. data->ref->vars = st->script->local.vars;
  4784. if (!st->script->local.arrays)
  4785. st->script->local.arrays = idb_alloc(DB_OPT_BASE);
  4786. data->ref->arrays = st->script->local.arrays;
  4787. }
  4788. }
  4789. }
  4790. else
  4791. {// no return value
  4792. script_pushnil(st);
  4793. }
  4794. st->state = RETFUNC;
  4795. return SCRIPT_CMD_SUCCESS;
  4796. }
  4797. /// Returns a random number from 0 to <range>-1.
  4798. /// Or returns a random number from <min> to <max>.
  4799. /// If <min> is greater than <max>, their numbers are switched.
  4800. /// rand(<range>) -> <int>
  4801. /// rand(<min>,<max>) -> <int>
  4802. BUILDIN_FUNC(rand)
  4803. {
  4804. int range;
  4805. int min;
  4806. if( script_hasdata(st,3) )
  4807. {// min,max
  4808. int max = script_getnum(st,3);
  4809. min = script_getnum(st,2);
  4810. if( max < min )
  4811. swap(min, max);
  4812. range = max - min + 1;
  4813. }
  4814. else
  4815. {// range
  4816. min = 0;
  4817. range = script_getnum(st,2);
  4818. }
  4819. if( range <= 1 )
  4820. script_pushint(st, min);
  4821. else
  4822. script_pushint(st, rnd()%range + min);
  4823. return SCRIPT_CMD_SUCCESS;
  4824. }
  4825. /*==========================================
  4826. * Warp sd to str,x,y or Random or SavePoint/Save
  4827. *------------------------------------------*/
  4828. BUILDIN_FUNC(warp)
  4829. {
  4830. int ret;
  4831. int x,y;
  4832. const char* str;
  4833. struct map_session_data* sd;
  4834. if(!script_charid2sd(5, sd))
  4835. return SCRIPT_CMD_SUCCESS;
  4836. str = script_getstr(st,2);
  4837. x = script_getnum(st,3);
  4838. y = script_getnum(st,4);
  4839. if(strcmp(str,"Random")==0)
  4840. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4841. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4842. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4843. else
  4844. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4845. if( ret ) {
  4846. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4847. return SCRIPT_CMD_FAILURE;
  4848. }
  4849. return SCRIPT_CMD_SUCCESS;
  4850. }
  4851. /*==========================================
  4852. * Warp a specified area
  4853. *------------------------------------------*/
  4854. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4855. {
  4856. int x2,y2,x3,y3;
  4857. unsigned int index;
  4858. index = va_arg(ap,unsigned int);
  4859. x2 = va_arg(ap,int);
  4860. y2 = va_arg(ap,int);
  4861. x3 = va_arg(ap,int);
  4862. y3 = va_arg(ap,int);
  4863. if(index == 0)
  4864. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4865. else if(x3 && y3) {
  4866. int max, tx, ty, j = 0;
  4867. // choose a suitable max number of attempts
  4868. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4869. max = 1000;
  4870. // find a suitable map cell
  4871. do {
  4872. tx = rnd()%(x3-x2+1)+x2;
  4873. ty = rnd()%(y3-y2+1)+y2;
  4874. j++;
  4875. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4876. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4877. }
  4878. else
  4879. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4880. return 0;
  4881. }
  4882. BUILDIN_FUNC(areawarp)
  4883. {
  4884. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4885. unsigned int index;
  4886. const char *str;
  4887. const char *mapname;
  4888. mapname = script_getstr(st,2);
  4889. x0 = script_getnum(st,3);
  4890. y0 = script_getnum(st,4);
  4891. x1 = script_getnum(st,5);
  4892. y1 = script_getnum(st,6);
  4893. str = script_getstr(st,7);
  4894. x2 = script_getnum(st,8);
  4895. y2 = script_getnum(st,9);
  4896. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4897. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4898. x3 = 0;
  4899. y3 = 0;
  4900. } else if( x3 && y3 ) {
  4901. // normalize x3/y3 coordinates
  4902. if( x3 < x2 ) swap(x3,x2);
  4903. if( y3 < y2 ) swap(y3,y2);
  4904. }
  4905. }
  4906. if( (m = map_mapname2mapid(mapname)) < 0 )
  4907. return SCRIPT_CMD_FAILURE;
  4908. if( strcmp(str,"Random") == 0 )
  4909. index = 0;
  4910. else if( !(index=mapindex_name2id(str)) )
  4911. return SCRIPT_CMD_FAILURE;
  4912. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4913. return SCRIPT_CMD_SUCCESS;
  4914. }
  4915. /*==========================================
  4916. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4917. *------------------------------------------*/
  4918. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4919. {
  4920. int hp, sp;
  4921. hp = va_arg(ap, int);
  4922. sp = va_arg(ap, int);
  4923. pc_percentheal((TBL_PC *)bl,hp,sp);
  4924. return 0;
  4925. }
  4926. BUILDIN_FUNC(areapercentheal)
  4927. {
  4928. int hp,sp,m;
  4929. const char *mapname;
  4930. int x0,y0,x1,y1;
  4931. mapname=script_getstr(st,2);
  4932. x0=script_getnum(st,3);
  4933. y0=script_getnum(st,4);
  4934. x1=script_getnum(st,5);
  4935. y1=script_getnum(st,6);
  4936. hp=script_getnum(st,7);
  4937. sp=script_getnum(st,8);
  4938. if( (m=map_mapname2mapid(mapname))< 0)
  4939. return SCRIPT_CMD_FAILURE;
  4940. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4941. return SCRIPT_CMD_SUCCESS;
  4942. }
  4943. /*==========================================
  4944. * Warpparty - [Fredzilla] [Paradox924X]
  4945. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4946. * If 'from_mapname' is specified, only the party members on that map will be warped
  4947. *------------------------------------------*/
  4948. BUILDIN_FUNC(warpparty)
  4949. {
  4950. TBL_PC *sd = NULL;
  4951. TBL_PC *pl_sd;
  4952. struct party_data* p;
  4953. int type;
  4954. int mapindex = 0, m = -1;
  4955. int i;
  4956. const char* str = script_getstr(st,2);
  4957. int x = script_getnum(st,3);
  4958. int y = script_getnum(st,4);
  4959. int p_id = script_getnum(st,5);
  4960. const char* str2 = NULL;
  4961. if ( script_hasdata(st,6) )
  4962. str2 = script_getstr(st,6);
  4963. p = party_search(p_id);
  4964. if(!p)
  4965. return SCRIPT_CMD_SUCCESS;
  4966. type = ( strcmp(str,"Random")==0 ) ? 0
  4967. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4968. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4969. : ( strcmp(str,"Leader")==0 ) ? 3
  4970. : 4;
  4971. switch (type)
  4972. {
  4973. case 3:
  4974. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4975. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4976. return SCRIPT_CMD_FAILURE;
  4977. pl_sd = p->data[i].sd;
  4978. mapindex = pl_sd->mapindex;
  4979. m = map_mapindex2mapid(mapindex);
  4980. x = pl_sd->bl.x;
  4981. y = pl_sd->bl.y;
  4982. break;
  4983. case 4:
  4984. mapindex = mapindex_name2id(str);
  4985. if (!mapindex) {// Invalid map
  4986. return SCRIPT_CMD_FAILURE;
  4987. }
  4988. m = map_mapindex2mapid(mapindex);
  4989. break;
  4990. case 2:
  4991. //"SavePoint" uses save point of the currently attached player
  4992. if (( sd = script_rid2sd(st) ) == NULL )
  4993. return SCRIPT_CMD_SUCCESS;
  4994. break;
  4995. }
  4996. for (i = 0; i < MAX_PARTY; i++)
  4997. {
  4998. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4999. continue;
  5000. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  5001. continue;
  5002. if( pc_isdead(pl_sd) )
  5003. continue;
  5004. switch( type )
  5005. {
  5006. case 0: // Random
  5007. if(!map[pl_sd->bl.m].flag.nowarp)
  5008. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5009. break;
  5010. case 1: // SavePointAll
  5011. if(!map[pl_sd->bl.m].flag.noreturn)
  5012. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5013. break;
  5014. case 2: // SavePoint
  5015. if(!map[pl_sd->bl.m].flag.noreturn)
  5016. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5017. break;
  5018. case 3: // Leader
  5019. case 4: // m,x,y
  5020. 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))
  5021. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5022. break;
  5023. }
  5024. }
  5025. return SCRIPT_CMD_SUCCESS;
  5026. }
  5027. /*==========================================
  5028. * Warpguild - [Fredzilla]
  5029. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5030. *------------------------------------------*/
  5031. BUILDIN_FUNC(warpguild)
  5032. {
  5033. TBL_PC *sd = NULL;
  5034. TBL_PC *pl_sd;
  5035. struct guild* g;
  5036. struct s_mapiterator* iter;
  5037. int type, mapindex = 0, m = -1;
  5038. const char* str = script_getstr(st,2);
  5039. int x = script_getnum(st,3);
  5040. int y = script_getnum(st,4);
  5041. int gid = script_getnum(st,5);
  5042. g = guild_search(gid);
  5043. if( g == NULL )
  5044. return SCRIPT_CMD_SUCCESS;
  5045. type = ( strcmp(str,"Random")==0 ) ? 0
  5046. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5047. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5048. : 3;
  5049. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  5050. {// "SavePoint" uses save point of the currently attached player
  5051. return SCRIPT_CMD_SUCCESS;
  5052. }
  5053. switch (type) {
  5054. case 3:
  5055. mapindex = mapindex_name2id(str);
  5056. if (!mapindex)
  5057. return SCRIPT_CMD_FAILURE;
  5058. m = map_mapindex2mapid(mapindex);
  5059. break;
  5060. }
  5061. iter = mapit_getallusers();
  5062. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5063. {
  5064. if( pl_sd->status.guild_id != gid )
  5065. continue;
  5066. switch( type )
  5067. {
  5068. case 0: // Random
  5069. if(!map[pl_sd->bl.m].flag.nowarp)
  5070. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5071. break;
  5072. case 1: // SavePointAll
  5073. if(!map[pl_sd->bl.m].flag.noreturn)
  5074. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5075. break;
  5076. case 2: // SavePoint
  5077. if(!map[pl_sd->bl.m].flag.noreturn)
  5078. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5079. break;
  5080. case 3: // m,x,y
  5081. 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))
  5082. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5083. break;
  5084. }
  5085. }
  5086. mapit_free(iter);
  5087. return SCRIPT_CMD_SUCCESS;
  5088. }
  5089. /*==========================================
  5090. * Force Heal a player (hp and sp)
  5091. *------------------------------------------*/
  5092. BUILDIN_FUNC(heal)
  5093. {
  5094. TBL_PC *sd;
  5095. int hp,sp;
  5096. if (!script_charid2sd(4,sd))
  5097. return SCRIPT_CMD_SUCCESS;
  5098. hp=script_getnum(st,2);
  5099. sp=script_getnum(st,3);
  5100. status_heal(&sd->bl, hp, sp, 1);
  5101. return SCRIPT_CMD_SUCCESS;
  5102. }
  5103. /*==========================================
  5104. * Heal a player by item (get vit bonus etc)
  5105. *------------------------------------------*/
  5106. BUILDIN_FUNC(itemheal)
  5107. {
  5108. TBL_PC *sd;
  5109. int hp,sp;
  5110. hp=script_getnum(st,2);
  5111. sp=script_getnum(st,3);
  5112. if(potion_flag==1) {
  5113. potion_hp = hp;
  5114. potion_sp = sp;
  5115. return SCRIPT_CMD_SUCCESS;
  5116. }
  5117. if (!script_charid2sd(4,sd))
  5118. return SCRIPT_CMD_SUCCESS;
  5119. pc_itemheal(sd,sd->itemid,hp,sp);
  5120. return SCRIPT_CMD_SUCCESS;
  5121. }
  5122. /*==========================================
  5123. *
  5124. *------------------------------------------*/
  5125. BUILDIN_FUNC(percentheal)
  5126. {
  5127. int hp,sp;
  5128. TBL_PC* sd;
  5129. hp=script_getnum(st,2);
  5130. sp=script_getnum(st,3);
  5131. if(potion_flag==1) {
  5132. potion_per_hp = hp;
  5133. potion_per_sp = sp;
  5134. return SCRIPT_CMD_SUCCESS;
  5135. }
  5136. if (!script_charid2sd(4,sd))
  5137. return SCRIPT_CMD_SUCCESS;
  5138. #ifdef RENEWAL
  5139. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5140. sp = 0;
  5141. #endif
  5142. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5143. hp = 0;
  5144. sp = 0;
  5145. }
  5146. if (sd->sc.data[SC_BITESCAR])
  5147. hp = 0;
  5148. pc_percentheal(sd,hp,sp);
  5149. return SCRIPT_CMD_SUCCESS;
  5150. }
  5151. /*==========================================
  5152. *
  5153. *------------------------------------------*/
  5154. BUILDIN_FUNC(jobchange)
  5155. {
  5156. int job, upper=-1;
  5157. job=script_getnum(st,2);
  5158. if( script_hasdata(st,3) )
  5159. upper=script_getnum(st,3);
  5160. if (pcdb_checkid(job))
  5161. {
  5162. TBL_PC* sd;
  5163. if (!script_charid2sd(4,sd))
  5164. return SCRIPT_CMD_SUCCESS;
  5165. pc_jobchange(sd, job, upper);
  5166. }
  5167. return SCRIPT_CMD_SUCCESS;
  5168. }
  5169. /*==========================================
  5170. *
  5171. *------------------------------------------*/
  5172. BUILDIN_FUNC(jobname)
  5173. {
  5174. int class_=script_getnum(st,2);
  5175. script_pushconststr(st, (char*)job_name(class_));
  5176. return SCRIPT_CMD_SUCCESS;
  5177. }
  5178. /// Get input from the player.
  5179. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5180. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5181. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5182. /// shorter than 'min' and 0 otherwise.
  5183. ///
  5184. /// input(<var>{,<min>{,<max>}}) -> <int>
  5185. BUILDIN_FUNC(input)
  5186. {
  5187. TBL_PC* sd;
  5188. struct script_data* data;
  5189. int64 uid;
  5190. const char* name;
  5191. int min;
  5192. int max;
  5193. sd = script_rid2sd(st);
  5194. if( sd == NULL )
  5195. return SCRIPT_CMD_SUCCESS;
  5196. data = script_getdata(st,2);
  5197. if( !data_isreference(data) ){
  5198. ShowError("script:input: not a variable\n");
  5199. script_reportdata(data);
  5200. st->state = END;
  5201. return SCRIPT_CMD_FAILURE;
  5202. }
  5203. uid = reference_getuid(data);
  5204. name = reference_getname(data);
  5205. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5206. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5207. #ifdef SECURE_NPCTIMEOUT
  5208. sd->npc_idle_type = NPCT_WAIT;
  5209. #endif
  5210. if( !sd->state.menu_or_input )
  5211. { // first invocation, display npc input box
  5212. sd->state.menu_or_input = 1;
  5213. st->state = RERUNLINE;
  5214. if( is_string_variable(name) )
  5215. clif_scriptinputstr(sd,st->oid);
  5216. else
  5217. clif_scriptinput(sd,st->oid);
  5218. }
  5219. else
  5220. { // take received text/value and store it in the designated variable
  5221. sd->state.menu_or_input = 0;
  5222. if( is_string_variable(name) )
  5223. {
  5224. int len = (int)strlen(sd->npc_str);
  5225. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5226. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5227. }
  5228. else
  5229. {
  5230. int amount = sd->npc_amount;
  5231. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5232. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5233. }
  5234. st->state = RUN;
  5235. }
  5236. return SCRIPT_CMD_SUCCESS;
  5237. }
  5238. // declare the copyarray method here for future reference
  5239. BUILDIN_FUNC(copyarray);
  5240. /// Sets the value of a variable.
  5241. /// The value is converted to the type of the variable.
  5242. ///
  5243. /// set(<variable>,<value>{,<charid>})
  5244. BUILDIN_FUNC(setr)
  5245. {
  5246. TBL_PC* sd = NULL;
  5247. struct script_data* data;
  5248. //struct script_data* datavalue;
  5249. int64 num;
  5250. uint8 pos = 4;
  5251. const char* name, *command = script_getfuncname(st);
  5252. char prefix;
  5253. data = script_getdata(st,2);
  5254. //datavalue = script_getdata(st,3);
  5255. if( !data_isreference(data) )
  5256. {
  5257. ShowError("script:set: not a variable\n");
  5258. script_reportdata(script_getdata(st,2));
  5259. st->state = END;
  5260. return SCRIPT_CMD_FAILURE;
  5261. }
  5262. num = reference_getuid(data);
  5263. name = reference_getname(data);
  5264. prefix = *name;
  5265. if (!strcmp(command, "setr"))
  5266. pos = 5;
  5267. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5268. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5269. return SCRIPT_CMD_FAILURE;
  5270. }
  5271. #if 0
  5272. if( data_isreference(datavalue) )
  5273. {// the value being referenced is a variable
  5274. const char* namevalue = reference_getname(datavalue);
  5275. if( !not_array_variable(*namevalue) )
  5276. {// array variable being copied into another array variable
  5277. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5278. {// player must be attached in order to copy a player variable
  5279. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5280. return SCRIPT_CMD_SUCCESS;
  5281. }
  5282. if( is_string_variable(namevalue) != is_string_variable(name) )
  5283. {// non-matching array value types
  5284. ShowWarning("script:set: two array variables do not match in type.\n");
  5285. return SCRIPT_CMD_SUCCESS;
  5286. }
  5287. // push the maximum number of array values to the stack
  5288. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5289. // call the copy array method directly
  5290. return buildin_copyarray(st);
  5291. }
  5292. }
  5293. #endif
  5294. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5295. if( is_string_variable(name) )
  5296. script_pushstrcopy(st,script_getstr(st, 4));
  5297. else
  5298. script_pushint(st,script_getnum(st, 4));
  5299. } else // Return a copy of the variable reference
  5300. script_pushcopy(st, 2);
  5301. if( is_string_variable(name) )
  5302. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5303. else
  5304. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5305. return SCRIPT_CMD_SUCCESS;
  5306. }
  5307. /////////////////////////////////////////////////////////////////////
  5308. /// Array variables
  5309. ///
  5310. /// Sets values of an array, from the starting index.
  5311. /// ex: setarray arr[1],1,2,3;
  5312. ///
  5313. /// setarray <array variable>,<value1>{,<value2>...};
  5314. BUILDIN_FUNC(setarray)
  5315. {
  5316. struct script_data* data;
  5317. const char* name;
  5318. uint32 start;
  5319. uint32 end;
  5320. int32 id;
  5321. int32 i;
  5322. TBL_PC* sd = NULL;
  5323. data = script_getdata(st, 2);
  5324. if( !data_isreference(data) )
  5325. {
  5326. ShowError("script:setarray: not a variable\n");
  5327. script_reportdata(data);
  5328. st->state = END;
  5329. return SCRIPT_CMD_FAILURE;// not a variable
  5330. }
  5331. id = reference_getid(data);
  5332. start = reference_getindex(data);
  5333. name = reference_getname(data);
  5334. if( not_server_variable(*name) )
  5335. {
  5336. sd = script_rid2sd(st);
  5337. if( sd == NULL )
  5338. return SCRIPT_CMD_SUCCESS;// no player attached
  5339. }
  5340. end = start + script_lastdata(st) - 2;
  5341. if( end > SCRIPT_MAX_ARRAYSIZE )
  5342. end = SCRIPT_MAX_ARRAYSIZE;
  5343. if( is_string_variable(name) )
  5344. {// string array
  5345. for( i = 3; start < end; ++start, ++i )
  5346. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5347. }
  5348. else
  5349. {// int array
  5350. for( i = 3; start < end; ++start, ++i )
  5351. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5352. }
  5353. return SCRIPT_CMD_SUCCESS;
  5354. }
  5355. /// Sets count values of an array, from the starting index.
  5356. /// ex: cleararray arr[0],0,1;
  5357. ///
  5358. /// cleararray <array variable>,<value>,<count>;
  5359. BUILDIN_FUNC(cleararray)
  5360. {
  5361. struct script_data* data;
  5362. const char* name;
  5363. uint32 start;
  5364. uint32 end;
  5365. int32 id;
  5366. void* v;
  5367. TBL_PC* sd = NULL;
  5368. data = script_getdata(st, 2);
  5369. if( !data_isreference(data) )
  5370. {
  5371. ShowError("script:cleararray: not a variable\n");
  5372. script_reportdata(data);
  5373. st->state = END;
  5374. return SCRIPT_CMD_FAILURE;// not a variable
  5375. }
  5376. id = reference_getid(data);
  5377. start = reference_getindex(data);
  5378. name = reference_getname(data);
  5379. if( not_server_variable(*name) )
  5380. {
  5381. sd = script_rid2sd(st);
  5382. if( sd == NULL )
  5383. return SCRIPT_CMD_SUCCESS;// no player attached
  5384. }
  5385. if( is_string_variable(name) )
  5386. v = (void*)script_getstr(st, 3);
  5387. else
  5388. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5389. end = start + script_getnum(st, 4);
  5390. if( end > SCRIPT_MAX_ARRAYSIZE )
  5391. end = SCRIPT_MAX_ARRAYSIZE;
  5392. for( ; start < end; ++start )
  5393. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5394. return SCRIPT_CMD_SUCCESS;
  5395. }
  5396. /// Copies data from one array to another.
  5397. /// ex: copyarray arr[0],arr[2],2;
  5398. ///
  5399. /// copyarray <destination array variable>,<source array variable>,<count>;
  5400. BUILDIN_FUNC(copyarray)
  5401. {
  5402. struct script_data* data1;
  5403. struct script_data* data2;
  5404. const char* name1;
  5405. const char* name2;
  5406. int32 idx1;
  5407. int32 idx2;
  5408. int32 id1;
  5409. int32 id2;
  5410. void* v;
  5411. int32 i;
  5412. uint32 count;
  5413. TBL_PC* sd = NULL;
  5414. data1 = script_getdata(st, 2);
  5415. data2 = script_getdata(st, 3);
  5416. if( !data_isreference(data1) || !data_isreference(data2) )
  5417. {
  5418. ShowError("script:copyarray: not a variable\n");
  5419. script_reportdata(data1);
  5420. script_reportdata(data2);
  5421. st->state = END;
  5422. return SCRIPT_CMD_FAILURE;// not a variable
  5423. }
  5424. id1 = reference_getid(data1);
  5425. id2 = reference_getid(data2);
  5426. idx1 = reference_getindex(data1);
  5427. idx2 = reference_getindex(data2);
  5428. name1 = reference_getname(data1);
  5429. name2 = reference_getname(data2);
  5430. if( is_string_variable(name1) != is_string_variable(name2) )
  5431. {
  5432. ShowError("script:copyarray: type mismatch\n");
  5433. script_reportdata(data1);
  5434. script_reportdata(data2);
  5435. st->state = END;
  5436. return SCRIPT_CMD_FAILURE;// data type mismatch
  5437. }
  5438. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5439. {
  5440. sd = script_rid2sd(st);
  5441. if( sd == NULL )
  5442. return SCRIPT_CMD_SUCCESS;// no player attached
  5443. }
  5444. count = script_getnum(st, 4);
  5445. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5446. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5447. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5448. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5449. if( id1 == id2 && idx1 > idx2 )
  5450. {// destination might be overlapping the source - copy in reverse order
  5451. for( i = count - 1; i >= 0; --i )
  5452. {
  5453. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5454. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5455. script_removetop(st, -1, 0);
  5456. }
  5457. }
  5458. else
  5459. {// normal copy
  5460. for( i = 0; i < count; ++i )
  5461. {
  5462. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5463. {
  5464. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5465. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5466. script_removetop(st, -1, 0);
  5467. }
  5468. else// out of range - assume ""/0
  5469. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5470. }
  5471. }
  5472. return SCRIPT_CMD_SUCCESS;
  5473. }
  5474. /// Returns the size of the array.
  5475. /// Assumes that everything before the starting index exists.
  5476. /// ex: getarraysize(arr[3])
  5477. ///
  5478. /// getarraysize(<array variable>) -> <int>
  5479. BUILDIN_FUNC(getarraysize)
  5480. {
  5481. struct script_data* data;
  5482. data = script_getdata(st, 2);
  5483. if( !data_isreference(data) )
  5484. {
  5485. ShowError("script:getarraysize: not a variable\n");
  5486. script_reportdata(data);
  5487. script_pushnil(st);
  5488. st->state = END;
  5489. return SCRIPT_CMD_FAILURE;// not a variable
  5490. }
  5491. script_pushint(st, script_array_highest_key(st, st->rid ? script_rid2sd(st) : NULL, reference_getname(data), reference_getref(data)));
  5492. return SCRIPT_CMD_SUCCESS;
  5493. }
  5494. int script_array_index_cmp(const void *a, const void *b) {
  5495. return ( *(unsigned int*)a - *(unsigned int*)b );
  5496. }
  5497. /// Deletes count or all the elements in an array, from the starting index.
  5498. /// ex: deletearray arr[4],2;
  5499. ///
  5500. /// deletearray <array variable>;
  5501. /// deletearray <array variable>,<count>;
  5502. BUILDIN_FUNC(deletearray)
  5503. {
  5504. struct script_data* data;
  5505. const char* name;
  5506. unsigned int start, end, i;
  5507. int id;
  5508. TBL_PC *sd = NULL;
  5509. struct script_array *sa = NULL;
  5510. struct reg_db *src = NULL;
  5511. void *value;
  5512. data = script_getdata(st, 2);
  5513. if( !data_isreference(data) ) {
  5514. ShowError("script:deletearray: not a variable\n");
  5515. script_reportdata(data);
  5516. st->state = END;
  5517. return SCRIPT_CMD_FAILURE;// not a variable
  5518. }
  5519. id = reference_getid(data);
  5520. start = reference_getindex(data);
  5521. name = reference_getname(data);
  5522. if( not_server_variable(*name) ) {
  5523. sd = script_rid2sd(st);
  5524. if( sd == NULL )
  5525. return SCRIPT_CMD_SUCCESS;// no player attached
  5526. }
  5527. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5528. ShowError("script:deletearray: not a array\n");
  5529. script_reportdata(data);
  5530. st->state = END;
  5531. return SCRIPT_CMD_FAILURE;// not a variable
  5532. }
  5533. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5534. if ( !(sa = idb_get(src->arrays, id)) ) { // non-existent array, nothing to empty
  5535. return SCRIPT_CMD_SUCCESS;// not a variable
  5536. }
  5537. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5538. if( start >= end )
  5539. return SCRIPT_CMD_SUCCESS;// nothing to free
  5540. if( is_string_variable(name) )
  5541. value = (void *)"";
  5542. else
  5543. value = (void *)0;
  5544. if( script_hasdata(st,3) ) {
  5545. unsigned int count = script_getnum(st, 3);
  5546. if( count > end - start )
  5547. count = end - start;
  5548. if( count <= 0 )
  5549. return SCRIPT_CMD_SUCCESS;// nothing to free
  5550. if( end - start < sa->size ) {
  5551. // Better to iterate directly on the array, no speed-up from using sa
  5552. for( ; start + count < end; ++start ) {
  5553. // Compact and overwrite
  5554. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5555. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5556. script_removetop(st, -1, 0);
  5557. }
  5558. for( ; start < end; start++ ) {
  5559. // Clean up any leftovers that weren't overwritten
  5560. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5561. }
  5562. } else {
  5563. // using sa to speed up
  5564. unsigned int *list = NULL, size = 0;
  5565. list = script_array_cpy_list(sa);
  5566. size = sa->size;
  5567. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5568. ARR_FIND(0, size, i, list[i] >= start);
  5569. for( ; i < size && list[i] < start + count; i++ ) {
  5570. // Clear any entries between start and start+count, if they exist
  5571. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5572. }
  5573. for( ; i < size && list[i] < end; i++ ) {
  5574. // Move back count positions any entries between start+count to fill the gaps
  5575. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5576. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5577. script_removetop(st, -1, 0);
  5578. // Clear their originals
  5579. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5580. }
  5581. }
  5582. } else {
  5583. unsigned int *list = NULL, size = 0;
  5584. list = script_array_cpy_list(sa);
  5585. size = sa->size;
  5586. for(i = 0; i < size; i++) {
  5587. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5588. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5589. }
  5590. }
  5591. return SCRIPT_CMD_SUCCESS;
  5592. }
  5593. /// Returns a reference to the target index of the array variable.
  5594. /// Equivalent to var[index].
  5595. ///
  5596. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5597. BUILDIN_FUNC(getelementofarray)
  5598. {
  5599. struct script_data* data;
  5600. int32 id;
  5601. int64 i;
  5602. data = script_getdata(st, 2);
  5603. if( !data_isreference(data) )
  5604. {
  5605. ShowError("script:getelementofarray: not a variable\n");
  5606. script_reportdata(data);
  5607. script_pushnil(st);
  5608. st->state = END;
  5609. return SCRIPT_CMD_SUCCESS;// not a variable
  5610. }
  5611. id = reference_getid(data);
  5612. i = script_getnum(st, 3);
  5613. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5614. ShowWarning("script:getelementofarray: index out of range (%"PRId64")\n", i);
  5615. script_reportdata(data);
  5616. script_pushnil(st);
  5617. st->state = END;
  5618. return SCRIPT_CMD_FAILURE;// out of range
  5619. }
  5620. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5621. return SCRIPT_CMD_SUCCESS;
  5622. }
  5623. /////////////////////////////////////////////////////////////////////
  5624. /// ...
  5625. ///
  5626. /*==========================================
  5627. *
  5628. *------------------------------------------*/
  5629. BUILDIN_FUNC(setlook)
  5630. {
  5631. int type,val;
  5632. TBL_PC* sd;
  5633. type = script_getnum(st,2);
  5634. val = script_getnum(st,3);
  5635. if (!script_charid2sd(4,sd))
  5636. return SCRIPT_CMD_SUCCESS;
  5637. pc_changelook(sd,type,val);
  5638. return SCRIPT_CMD_SUCCESS;
  5639. }
  5640. BUILDIN_FUNC(changelook)
  5641. { // As setlook but only client side
  5642. int type,val;
  5643. TBL_PC* sd;
  5644. type = script_getnum(st,2);
  5645. val = script_getnum(st,3);
  5646. if (!script_charid2sd(4,sd))
  5647. return SCRIPT_CMD_SUCCESS;
  5648. clif_changelook(&sd->bl,type,val);
  5649. return SCRIPT_CMD_SUCCESS;
  5650. }
  5651. /*==========================================
  5652. *
  5653. *------------------------------------------*/
  5654. BUILDIN_FUNC(cutin)
  5655. {
  5656. TBL_PC* sd;
  5657. sd = script_rid2sd(st);
  5658. if( sd == NULL )
  5659. return SCRIPT_CMD_SUCCESS;
  5660. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5661. return SCRIPT_CMD_SUCCESS;
  5662. }
  5663. /*==========================================
  5664. *
  5665. *------------------------------------------*/
  5666. BUILDIN_FUNC(viewpoint)
  5667. {
  5668. int type,x,y,id,color;
  5669. TBL_PC* sd;
  5670. type=script_getnum(st,2);
  5671. x=script_getnum(st,3);
  5672. y=script_getnum(st,4);
  5673. id=script_getnum(st,5);
  5674. color=script_getnum(st,6);
  5675. sd = script_rid2sd(st);
  5676. if( sd == NULL )
  5677. return SCRIPT_CMD_SUCCESS;
  5678. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5679. return SCRIPT_CMD_SUCCESS;
  5680. }
  5681. /// Returns number of items in inventory/cart/storage
  5682. /// countitem <nameID>{,<accountID>});
  5683. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5684. /// cartcountitem <nameID>{,<accountID>});
  5685. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5686. /// storagecountitem <nameID>{,<accountID>});
  5687. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5688. /// guildstoragecountitem <nameID>{,<accountID>});
  5689. /// guildstoragecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5690. BUILDIN_FUNC(countitem)
  5691. {
  5692. int i = 0, aid = 3;
  5693. struct item_data* id = NULL;
  5694. struct script_data* data;
  5695. char *command = (char *)script_getfuncname(st);
  5696. uint8 loc = 0;
  5697. uint16 size, count = 0;
  5698. struct item *items;
  5699. TBL_PC *sd = NULL;
  5700. struct s_storage *gstor;
  5701. if( command[strlen(command)-1] == '2' ) {
  5702. i = 1;
  5703. aid = 10;
  5704. }
  5705. if( script_hasdata(st,aid) ) {
  5706. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5707. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5708. st->state = END;
  5709. return SCRIPT_CMD_FAILURE;
  5710. }
  5711. }
  5712. else {
  5713. if( !(sd = script_rid2sd(st)) )
  5714. return SCRIPT_CMD_SUCCESS;
  5715. }
  5716. if( !strncmp(command, "cart", 4) ) {
  5717. loc = TABLE_CART;
  5718. size = MAX_CART;
  5719. items = sd->cart.u.items_cart;
  5720. }
  5721. else if( !strncmp(command, "storage", 7) ) {
  5722. loc = TABLE_STORAGE;
  5723. size = MAX_STORAGE;
  5724. items = sd->storage.u.items_storage;
  5725. }
  5726. else if( !strncmp(command, "guildstorage", 12) ) {
  5727. gstor = guild2storage2(sd->status.guild_id);
  5728. if (gstor && !sd->state.storage_flag) {
  5729. loc = TABLE_GUILD_STORAGE;
  5730. size = MAX_GUILD_STORAGE;
  5731. items = gstor->u.items_guild;
  5732. } else {
  5733. script_pushint(st, -1);
  5734. return SCRIPT_CMD_SUCCESS;
  5735. }
  5736. }
  5737. else {
  5738. size = MAX_INVENTORY;
  5739. items = sd->inventory.u.items_inventory;
  5740. }
  5741. if( loc == TABLE_CART && !pc_iscarton(sd) ) {
  5742. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5743. script_pushint(st,-1);
  5744. return SCRIPT_CMD_SUCCESS;
  5745. }
  5746. data = script_getdata(st, 2);
  5747. get_val(st, data); // Convert into value in case of a variable
  5748. if( data_isstring(data) ) // item name
  5749. id = itemdb_searchname(conv_str(st, data));
  5750. else // item id
  5751. id = itemdb_exists(conv_num(st, data));
  5752. if( id == NULL ) {
  5753. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5754. script_pushint(st,0);
  5755. return SCRIPT_CMD_FAILURE;
  5756. }
  5757. if (loc == TABLE_GUILD_STORAGE)
  5758. gstor->lock = true;
  5759. if( !i ) { // For count/cart/storagecountitem function
  5760. unsigned short nameid = id->nameid;
  5761. for( i = 0; i < size; i++ )
  5762. if( &items[i] && items[i].nameid == nameid )
  5763. count += items[i].amount;
  5764. }
  5765. else { // For count/cart/storagecountitem2 function
  5766. unsigned short nameid;
  5767. int iden, ref, attr, c1, c2, c3, c4;
  5768. nameid = id->nameid;
  5769. iden = script_getnum(st,3);
  5770. ref = script_getnum(st,4);
  5771. attr = script_getnum(st,5);
  5772. c1 = script_getnum(st,6);
  5773. c2 = script_getnum(st,7);
  5774. c3 = script_getnum(st,8);
  5775. c4 = script_getnum(st,9);
  5776. for( i = 0; i < size; i++ )
  5777. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5778. items[i].amount > 0 && items[i].identify == iden &&
  5779. items[i].refine == ref && items[i].attribute == attr &&
  5780. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5781. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5782. {
  5783. count += items[i].amount;
  5784. }
  5785. }
  5786. if (loc == TABLE_GUILD_STORAGE) {
  5787. storage_guild_storageclose(sd);
  5788. gstor->lock = false;
  5789. }
  5790. script_pushint(st, count);
  5791. return SCRIPT_CMD_SUCCESS;
  5792. }
  5793. /*==========================================
  5794. * Check if item with this amount can fit in inventory
  5795. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  5796. * Return
  5797. * 0 : fail
  5798. * 1 : success (npc side only)
  5799. *------------------------------------------*/
  5800. BUILDIN_FUNC(checkweight)
  5801. {
  5802. int slots = 0;
  5803. unsigned short amount2 = 0;
  5804. unsigned int weight = 0, i, nbargs;
  5805. struct item_data* id = NULL;
  5806. struct map_session_data* sd;
  5807. if( ( sd = script_rid2sd(st) ) == NULL )
  5808. return SCRIPT_CMD_SUCCESS;
  5809. nbargs = script_lastdata(st)+1;
  5810. if(nbargs%2) {
  5811. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5812. script_pushint(st,0);
  5813. return SCRIPT_CMD_FAILURE;
  5814. }
  5815. slots = pc_inventoryblank(sd); //nb of empty slot
  5816. for (i = 2; i < nbargs; i += 2) {
  5817. unsigned short nameid, amount;
  5818. struct script_data* data = script_getdata(st,i);
  5819. get_val(st, data); // Convert into value in case of a variable
  5820. if( data_isstring(data) ) // item name
  5821. id = itemdb_searchname(conv_str(st, data));
  5822. else // item id
  5823. id = itemdb_exists(conv_num(st, data));
  5824. if( id == NULL ) {
  5825. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5826. script_pushint(st,0);
  5827. return SCRIPT_CMD_FAILURE;
  5828. }
  5829. nameid = id->nameid;
  5830. amount = script_getnum(st,i+1);
  5831. if( amount < 1 ) {
  5832. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5833. script_pushint(st,0);
  5834. return SCRIPT_CMD_FAILURE;
  5835. }
  5836. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  5837. if( weight + sd->weight > sd->max_weight )
  5838. {// too heavy
  5839. script_pushint(st,0);
  5840. return SCRIPT_CMD_SUCCESS;
  5841. }
  5842. switch( pc_checkadditem(sd, nameid, amount) ) {
  5843. case CHKADDITEM_EXIST:
  5844. // item is already in inventory, but there is still space for the requested amount
  5845. break;
  5846. case CHKADDITEM_NEW:
  5847. if( itemdb_isstackable(nameid) ) {
  5848. // stackable
  5849. amount2++;
  5850. if( slots < amount2 ) {
  5851. script_pushint(st,0);
  5852. return SCRIPT_CMD_SUCCESS;
  5853. }
  5854. } else {
  5855. // non-stackable
  5856. amount2 += amount;
  5857. if( slots < amount2) {
  5858. script_pushint(st,0);
  5859. return SCRIPT_CMD_SUCCESS;
  5860. }
  5861. }
  5862. break;
  5863. case CHKADDITEM_OVERAMOUNT:
  5864. script_pushint(st,0);
  5865. return SCRIPT_CMD_SUCCESS;
  5866. }
  5867. }
  5868. script_pushint(st,1);
  5869. return SCRIPT_CMD_SUCCESS;
  5870. }
  5871. BUILDIN_FUNC(checkweight2)
  5872. {
  5873. //variable sub checkweight
  5874. int i = 0, slots = 0, weight = 0;
  5875. short fail = 0;
  5876. unsigned short amount2 = 0;
  5877. //variable for array parsing
  5878. struct script_data* data_it;
  5879. struct script_data* data_nb;
  5880. const char* name_it;
  5881. const char* name_nb;
  5882. int32 id_it, id_nb;
  5883. int32 idx_it, idx_nb;
  5884. int nb_it, nb_nb; //array size
  5885. TBL_PC *sd = script_rid2sd(st);
  5886. nullpo_retr(1,sd);
  5887. data_it = script_getdata(st, 2);
  5888. data_nb = script_getdata(st, 3);
  5889. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5890. ShowError("buildin_checkweight2: parameter not a variable\n");
  5891. script_pushint(st,0);
  5892. return SCRIPT_CMD_FAILURE;// not a variable
  5893. }
  5894. id_it = reference_getid(data_it);
  5895. id_nb = reference_getid(data_nb);
  5896. idx_it = reference_getindex(data_it);
  5897. idx_nb = reference_getindex(data_nb);
  5898. name_it = reference_getname(data_it);
  5899. name_nb = reference_getname(data_nb);
  5900. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5901. ShowError("buildin_checkweight2: illegal type, need int\n");
  5902. script_pushint(st,0);
  5903. return SCRIPT_CMD_FAILURE;// not supported
  5904. }
  5905. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  5906. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  5907. if(nb_it != nb_nb) {
  5908. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5909. fail = 1;
  5910. }
  5911. slots = pc_inventoryblank(sd);
  5912. for(i=0; i<nb_it; i++) {
  5913. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5914. script_removetop(st, -1, 0);
  5915. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5916. script_removetop(st, -1, 0);
  5917. if(fail)
  5918. continue; //cpntonie to depop rest
  5919. if(itemdb_exists(nameid) == NULL ) {
  5920. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  5921. fail=1;
  5922. continue;
  5923. }
  5924. if(amount < 0 ) {
  5925. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  5926. fail = 1;
  5927. continue;
  5928. }
  5929. weight += itemdb_weight(nameid)*amount;
  5930. if( weight + sd->weight > sd->max_weight ) {
  5931. fail = 1;
  5932. continue;
  5933. }
  5934. switch( pc_checkadditem(sd, nameid, amount) ) {
  5935. case CHKADDITEM_EXIST:
  5936. // item is already in inventory, but there is still space for the requested amount
  5937. break;
  5938. case CHKADDITEM_NEW:
  5939. if( itemdb_isstackable(nameid) ) {// stackable
  5940. amount2++;
  5941. if( slots < amount2 )
  5942. fail = 1;
  5943. } else {// non-stackable
  5944. amount2 += amount;
  5945. if( slots < amount2 ) {
  5946. fail = 1;
  5947. }
  5948. }
  5949. break;
  5950. case CHKADDITEM_OVERAMOUNT:
  5951. fail = 1;
  5952. } //end switch
  5953. } //end loop DO NOT break it prematurly we need to depop all stack
  5954. fail ? script_pushint(st,0) : script_pushint(st,1);
  5955. return SCRIPT_CMD_SUCCESS;
  5956. }
  5957. /*==========================================
  5958. * getitem <item id>,<amount>{,<account ID>};
  5959. * getitem "<item name>",<amount>{,<account ID>};
  5960. *
  5961. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5962. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5963. * Type:
  5964. * 0 - No bound
  5965. * 1 - Account Bound
  5966. * 2 - Guild Bound
  5967. * 3 - Party Bound
  5968. * 4 - Character Bound
  5969. *------------------------------------------*/
  5970. BUILDIN_FUNC(getitem)
  5971. {
  5972. int get_count, i;
  5973. unsigned short nameid, amount;
  5974. struct item it;
  5975. TBL_PC *sd;
  5976. struct script_data *data;
  5977. unsigned char flag = 0;
  5978. const char* command = script_getfuncname(st);
  5979. struct item_data *id = NULL;
  5980. data = script_getdata(st,2);
  5981. get_val(st,data);
  5982. if( data_isstring(data) ) {// "<item name>"
  5983. const char *name = conv_str(st,data);
  5984. id = itemdb_searchname(name);
  5985. if( id == NULL ){
  5986. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5987. return SCRIPT_CMD_FAILURE; //No item created.
  5988. }
  5989. nameid = id->nameid;
  5990. } else if( data_isint(data) ) {// <item id>
  5991. nameid = conv_num(st,data);
  5992. if( !(id = itemdb_exists(nameid)) ){
  5993. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5994. return SCRIPT_CMD_FAILURE; //No item created.
  5995. }
  5996. } else {
  5997. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5998. return SCRIPT_CMD_FAILURE;
  5999. }
  6000. // <amount>
  6001. if( (amount = script_getnum(st,3)) <= 0)
  6002. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6003. memset(&it,0,sizeof(it));
  6004. it.nameid = nameid;
  6005. it.identify = 1;
  6006. it.bound = BOUND_NONE;
  6007. if( !strcmp(command,"getitembound") ) {
  6008. char bound = script_getnum(st,4);
  6009. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6010. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6011. return SCRIPT_CMD_FAILURE;
  6012. }
  6013. if( script_hasdata(st,5) )
  6014. sd = map_id2sd(script_getnum(st,5));
  6015. else
  6016. sd = script_rid2sd(st); // Attached player
  6017. it.bound = bound;
  6018. } else if( script_hasdata(st,4) )
  6019. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  6020. else
  6021. sd = script_rid2sd(st); // Attached player
  6022. if( sd == NULL ) // no target
  6023. return SCRIPT_CMD_SUCCESS;
  6024. //Check if it's stackable.
  6025. if (!itemdb_isstackable2(id))
  6026. get_count = 1;
  6027. else
  6028. get_count = amount;
  6029. for (i = 0; i < amount; i += get_count)
  6030. {
  6031. // if not pet egg
  6032. if (!pet_create_egg(sd, nameid))
  6033. {
  6034. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6035. {
  6036. clif_additem(sd, 0, 0, flag);
  6037. if( pc_candrop(sd,&it) )
  6038. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6039. }
  6040. }
  6041. }
  6042. return SCRIPT_CMD_SUCCESS;
  6043. }
  6044. /*==========================================
  6045. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6046. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6047. *
  6048. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6049. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6050. * Type:
  6051. * 0 - No bound
  6052. * 1 - Account Bound
  6053. * 2 - Guild Bound
  6054. * 3 - Party Bound
  6055. * 4 - Character Bound
  6056. *------------------------------------------*/
  6057. BUILDIN_FUNC(getitem2)
  6058. {
  6059. unsigned short nameid, amount;
  6060. int iden, ref, attr;
  6061. unsigned short c1, c2, c3, c4;
  6062. char bound = BOUND_NONE;
  6063. struct item_data *item_data = NULL;
  6064. struct item item_tmp;
  6065. TBL_PC *sd;
  6066. struct script_data *data;
  6067. const char* command = script_getfuncname(st);
  6068. if( !strcmp(command,"getitembound2") ) {
  6069. bound = script_getnum(st,11);
  6070. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6071. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6072. return SCRIPT_CMD_FAILURE;
  6073. }
  6074. if( script_hasdata(st,12) )
  6075. sd = map_id2sd(script_getnum(st,12));
  6076. else
  6077. sd = script_rid2sd(st); // Attached player
  6078. } else if( script_hasdata(st,11) )
  6079. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  6080. else
  6081. sd = script_rid2sd(st); // Attached player
  6082. if( sd == NULL ) // no target
  6083. return SCRIPT_CMD_SUCCESS;
  6084. data = script_getdata(st,2);
  6085. get_val(st,data);
  6086. if( data_isstring(data) ) {
  6087. const char *name = conv_str(st,data);
  6088. if( (item_data = itemdb_searchname(name)) == NULL ){
  6089. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6090. return SCRIPT_CMD_FAILURE; //No item created.
  6091. }
  6092. nameid = item_data->nameid;
  6093. } else {
  6094. nameid = conv_num(st,data);
  6095. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6096. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6097. return SCRIPT_CMD_FAILURE; //No item created.
  6098. }
  6099. }
  6100. amount = script_getnum(st,3);
  6101. iden = script_getnum(st,4);
  6102. ref = script_getnum(st,5);
  6103. attr = script_getnum(st,6);
  6104. c1 = (unsigned short)script_getnum(st,7);
  6105. c2 = (unsigned short)script_getnum(st,8);
  6106. c3 = (unsigned short)script_getnum(st,9);
  6107. c4 = (unsigned short)script_getnum(st,10);
  6108. if( item_data ) {
  6109. int get_count = 0, i;
  6110. memset(&item_tmp,0,sizeof(item_tmp));
  6111. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6112. if(ref > MAX_REFINE)
  6113. ref = MAX_REFINE;
  6114. }
  6115. else if( item_data->type == IT_PETEGG ) {
  6116. iden = 1;
  6117. ref = 0;
  6118. }
  6119. else {
  6120. iden = 1;
  6121. ref = attr = 0;
  6122. }
  6123. item_tmp.nameid = nameid;
  6124. item_tmp.identify = iden;
  6125. item_tmp.refine = ref;
  6126. item_tmp.attribute = attr;
  6127. item_tmp.card[0] = c1;
  6128. item_tmp.card[1] = c2;
  6129. item_tmp.card[2] = c3;
  6130. item_tmp.card[3] = c4;
  6131. item_tmp.bound = bound;
  6132. //Check if it's stackable.
  6133. if (!itemdb_isstackable2(item_data))
  6134. get_count = 1;
  6135. else
  6136. get_count = amount;
  6137. for (i = 0; i < amount; i += get_count)
  6138. {
  6139. // if not pet egg
  6140. if (!pet_create_egg(sd, nameid))
  6141. {
  6142. unsigned char flag = 0;
  6143. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6144. {
  6145. clif_additem(sd, 0, 0, flag);
  6146. if( pc_candrop(sd,&item_tmp) )
  6147. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6148. }
  6149. }
  6150. }
  6151. }
  6152. return SCRIPT_CMD_SUCCESS;
  6153. }
  6154. /** Gives rental item to player
  6155. * rentitem <item id>,<seconds>{,<account_id>}
  6156. * rentitem "<item name>",<seconds>{,<account_id>}
  6157. */
  6158. BUILDIN_FUNC(rentitem) {
  6159. struct map_session_data *sd;
  6160. struct script_data *data;
  6161. struct item it;
  6162. int seconds;
  6163. unsigned short nameid = 0;
  6164. unsigned char flag = 0;
  6165. data = script_getdata(st,2);
  6166. get_val(st,data);
  6167. if (!script_accid2sd(4,sd))
  6168. return SCRIPT_CMD_FAILURE;
  6169. if( data_isstring(data) )
  6170. {
  6171. const char *name = conv_str(st,data);
  6172. struct item_data *itd = itemdb_searchname(name);
  6173. if( itd == NULL )
  6174. {
  6175. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6176. return SCRIPT_CMD_FAILURE;
  6177. }
  6178. nameid = itd->nameid;
  6179. }
  6180. else if( data_isint(data) )
  6181. {
  6182. nameid = conv_num(st,data);
  6183. if( nameid == 0 || !itemdb_exists(nameid) )
  6184. {
  6185. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6186. return SCRIPT_CMD_FAILURE;
  6187. }
  6188. }
  6189. else
  6190. {
  6191. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6192. return SCRIPT_CMD_FAILURE;
  6193. }
  6194. seconds = script_getnum(st,3);
  6195. memset(&it, 0, sizeof(it));
  6196. it.nameid = nameid;
  6197. it.identify = 1;
  6198. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6199. it.bound = BOUND_NONE;
  6200. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6201. {
  6202. clif_additem(sd, 0, 0, flag);
  6203. return SCRIPT_CMD_FAILURE;
  6204. }
  6205. return SCRIPT_CMD_SUCCESS;
  6206. }
  6207. /** Gives rental item to player with advanced option
  6208. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6209. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6210. */
  6211. BUILDIN_FUNC(rentitem2) {
  6212. struct map_session_data *sd;
  6213. struct script_data *data;
  6214. struct item it;
  6215. struct item_data *id;
  6216. int seconds;
  6217. unsigned short nameid = 0;
  6218. unsigned char flag = 0;
  6219. int iden,ref,attr,c1,c2,c3,c4;
  6220. data = script_getdata(st,2);
  6221. get_val(st,data);
  6222. if (!script_accid2sd(11,sd))
  6223. return SCRIPT_CMD_FAILURE;
  6224. if( data_isstring(data) ) {
  6225. const char *name = conv_str(st,data);
  6226. id = itemdb_searchname(name);
  6227. if( id == NULL ) {
  6228. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6229. return SCRIPT_CMD_FAILURE;
  6230. }
  6231. nameid = id->nameid;
  6232. }
  6233. else if( data_isint(data) ) {
  6234. nameid = conv_num(st,data);
  6235. if( !(id = itemdb_search(nameid))) {
  6236. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6237. return SCRIPT_CMD_FAILURE;
  6238. }
  6239. }
  6240. else {
  6241. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6242. return SCRIPT_CMD_FAILURE;
  6243. }
  6244. seconds = script_getnum(st,3);
  6245. iden = script_getnum(st,4);
  6246. ref = script_getnum(st,5);
  6247. attr = script_getnum(st,6);
  6248. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6249. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6250. }
  6251. else if (id->type==IT_PETEGG) {
  6252. iden = 1;
  6253. ref = 0;
  6254. }
  6255. else {
  6256. iden = 1;
  6257. ref = attr = 0;
  6258. }
  6259. c1 = (short)script_getnum(st,7);
  6260. c2 = (short)script_getnum(st,8);
  6261. c3 = (short)script_getnum(st,9);
  6262. c4 = (short)script_getnum(st,10);
  6263. memset(&it, 0, sizeof(it));
  6264. it.nameid = nameid;
  6265. it.identify = iden;
  6266. it.refine = ref;
  6267. it.attribute = attr;
  6268. it.card[0] = (short)c1;
  6269. it.card[1] = (short)c2;
  6270. it.card[2] = (short)c3;
  6271. it.card[3] = (short)c4;
  6272. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6273. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6274. clif_additem(sd, 0, 0, flag);
  6275. return SCRIPT_CMD_FAILURE;
  6276. }
  6277. return SCRIPT_CMD_SUCCESS;
  6278. }
  6279. /*==========================================
  6280. * gets an item with someone's name inscribed [Skotlex]
  6281. * getinscribeditem item_num, character_name
  6282. * Returned Qty is always 1, only works on equip-able
  6283. * equipment
  6284. *------------------------------------------*/
  6285. BUILDIN_FUNC(getnameditem)
  6286. {
  6287. unsigned short nameid;
  6288. struct item item_tmp;
  6289. TBL_PC *sd, *tsd;
  6290. struct script_data *data;
  6291. sd = script_rid2sd(st);
  6292. if (sd == NULL)
  6293. { //Player not attached!
  6294. script_pushint(st,0);
  6295. return SCRIPT_CMD_SUCCESS;
  6296. }
  6297. data=script_getdata(st,2);
  6298. get_val(st,data);
  6299. if( data_isstring(data) ){
  6300. const char *name=conv_str(st,data);
  6301. struct item_data *item_data = itemdb_searchname(name);
  6302. if( item_data == NULL)
  6303. { //Failed
  6304. script_pushint(st,0);
  6305. return SCRIPT_CMD_SUCCESS;
  6306. }
  6307. nameid = item_data->nameid;
  6308. }else
  6309. nameid = conv_num(st,data);
  6310. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6311. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6312. script_pushint(st,0);
  6313. return SCRIPT_CMD_SUCCESS;
  6314. }
  6315. data=script_getdata(st,3);
  6316. get_val(st,data);
  6317. if( data_isstring(data) ) //Char Name
  6318. tsd=map_nick2sd(conv_str(st,data));
  6319. else //Char Id was given
  6320. tsd=map_charid2sd(conv_num(st,data));
  6321. if( tsd == NULL )
  6322. { //Failed
  6323. script_pushint(st,0);
  6324. return SCRIPT_CMD_SUCCESS;
  6325. }
  6326. memset(&item_tmp,0,sizeof(item_tmp));
  6327. item_tmp.nameid=nameid;
  6328. item_tmp.amount=1;
  6329. item_tmp.identify=1;
  6330. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6331. item_tmp.card[2]=tsd->status.char_id;
  6332. item_tmp.card[3]=tsd->status.char_id >> 16;
  6333. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6334. script_pushint(st,0);
  6335. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6336. }
  6337. script_pushint(st,1);
  6338. return SCRIPT_CMD_SUCCESS;
  6339. }
  6340. /*==========================================
  6341. * gets a random item ID from an item group [Skotlex]
  6342. * groupranditem <group_num>{,<sub_group>};
  6343. *------------------------------------------*/
  6344. BUILDIN_FUNC(grouprandomitem) {
  6345. struct s_item_group_entry *entry = NULL;
  6346. int sub_group = 1;
  6347. FETCH(3, sub_group);
  6348. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6349. if (!entry) {
  6350. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6351. script_pushint(st,UNKNOWN_ITEM_ID);
  6352. return SCRIPT_CMD_FAILURE;
  6353. }
  6354. script_pushint(st,entry->nameid);
  6355. return SCRIPT_CMD_SUCCESS;
  6356. }
  6357. /**
  6358. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6359. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6360. */
  6361. BUILDIN_FUNC(makeitem) {
  6362. uint16 nameid, amount, flag = 0, x, y;
  6363. const char *mapname;
  6364. int m;
  6365. struct item item_tmp;
  6366. struct script_data *data;
  6367. data = script_getdata(st,2);
  6368. get_val(st,data);
  6369. if( data_isstring(data) ){
  6370. const char *name = conv_str(st,data);
  6371. struct item_data *item_data = itemdb_searchname(name);
  6372. if( item_data )
  6373. nameid = item_data->nameid;
  6374. else
  6375. nameid = UNKNOWN_ITEM_ID;
  6376. }
  6377. else
  6378. nameid = conv_num(st,data);
  6379. amount = script_getnum(st,3);
  6380. mapname = script_getstr(st,4);
  6381. x = script_getnum(st,5);
  6382. y = script_getnum(st,6);
  6383. if(strcmp(mapname,"this")==0) {
  6384. TBL_PC *sd;
  6385. sd = script_rid2sd(st);
  6386. if (!sd)
  6387. return SCRIPT_CMD_SUCCESS; //Failed...
  6388. m = sd->bl.m;
  6389. } else
  6390. m = map_mapname2mapid(mapname);
  6391. if(nameid<0) {
  6392. nameid = -nameid;
  6393. flag = 1;
  6394. }
  6395. if(nameid > 0) {
  6396. memset(&item_tmp,0,sizeof(item_tmp));
  6397. item_tmp.nameid = nameid;
  6398. if(!flag)
  6399. item_tmp.identify = 1;
  6400. else
  6401. item_tmp.identify = itemdb_isidentified(nameid);
  6402. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6403. }
  6404. return SCRIPT_CMD_SUCCESS;
  6405. }
  6406. /**
  6407. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6408. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6409. */
  6410. BUILDIN_FUNC(makeitem2) {
  6411. uint16 nameid, amount, x, y;
  6412. const char *mapname;
  6413. int m;
  6414. struct item item_tmp;
  6415. struct script_data *data;
  6416. struct item_data *id;
  6417. data = script_getdata(st,2);
  6418. get_val(st,data);
  6419. if( data_isstring(data) ){
  6420. const char *name = conv_str(st,data);
  6421. struct item_data *item_data = itemdb_searchname(name);
  6422. if( item_data )
  6423. nameid = item_data->nameid;
  6424. else
  6425. nameid = UNKNOWN_ITEM_ID;
  6426. }
  6427. else
  6428. nameid = conv_num(st,data);
  6429. amount = script_getnum(st,3);
  6430. mapname = script_getstr(st,4);
  6431. x = script_getnum(st,5);
  6432. y = script_getnum(st,6);
  6433. if (strcmp(mapname,"this")==0) {
  6434. TBL_PC *sd;
  6435. sd = script_rid2sd(st);
  6436. if (!sd)
  6437. return SCRIPT_CMD_SUCCESS; //Failed...
  6438. m = sd->bl.m;
  6439. }
  6440. else
  6441. m = map_mapname2mapid(mapname);
  6442. if ((id = itemdb_search(nameid))) {
  6443. char iden, ref, attr;
  6444. memset(&item_tmp,0,sizeof(item_tmp));
  6445. item_tmp.nameid = nameid;
  6446. iden = (char)script_getnum(st,7);
  6447. ref = (char)script_getnum(st,8);
  6448. attr = (char)script_getnum(st,9);
  6449. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6450. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6451. }
  6452. else if (id->type==IT_PETEGG) {
  6453. iden = 1;
  6454. ref = 0;
  6455. }
  6456. else {
  6457. iden = 1;
  6458. ref = attr = 0;
  6459. }
  6460. item_tmp.identify = iden;
  6461. item_tmp.refine = ref;
  6462. item_tmp.attribute = attr;
  6463. item_tmp.card[0] = script_getnum(st,10);
  6464. item_tmp.card[1] = script_getnum(st,11);
  6465. item_tmp.card[2] = script_getnum(st,12);
  6466. item_tmp.card[3] = script_getnum(st,13);
  6467. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6468. }
  6469. else
  6470. return SCRIPT_CMD_FAILURE;
  6471. return SCRIPT_CMD_SUCCESS;
  6472. }
  6473. /// Counts / deletes the current item given by idx.
  6474. /// Used by buildin_delitem_search
  6475. /// Relies on all input data being already fully valid.
  6476. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6477. {
  6478. int delamount;
  6479. struct item *itm = NULL;
  6480. struct s_storage *gstor = NULL;
  6481. switch(loc) {
  6482. case TABLE_CART:
  6483. itm = &sd->cart.u.items_cart[idx];
  6484. break;
  6485. case TABLE_STORAGE:
  6486. itm = &sd->storage.u.items_storage[idx];
  6487. break;
  6488. case TABLE_GUILD_STORAGE:
  6489. {
  6490. gstor = guild2storage2(sd->status.guild_id);
  6491. itm = &gstor->u.items_guild[idx];
  6492. }
  6493. break;
  6494. default: // TABLE_INVENTORY
  6495. itm = &sd->inventory.u.items_inventory[idx];
  6496. break;
  6497. }
  6498. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6499. if( delete_items )
  6500. {
  6501. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6502. {// delete associated pet
  6503. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6504. }
  6505. switch(loc) {
  6506. case TABLE_CART:
  6507. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6508. break;
  6509. case TABLE_STORAGE:
  6510. storage_delitem(sd,&sd->storage,idx,delamount);
  6511. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6512. break;
  6513. case TABLE_GUILD_STORAGE:
  6514. gstor->lock = true;
  6515. storage_guild_delitem(sd, gstor, idx, delamount);
  6516. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  6517. storage_guild_storageclose(sd);
  6518. gstor->lock = false;
  6519. break;
  6520. default: // TABLE_INVENTORY
  6521. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6522. break;
  6523. }
  6524. }
  6525. amount[0]-= delamount;
  6526. }
  6527. /// Searches for item(s) and checks, if there is enough of them.
  6528. /// Used by delitem and delitem2
  6529. /// Relies on all input data being already fully valid.
  6530. /// @param exact_match will also match item attributes and cards, not just name id
  6531. /// @return true when all items could be deleted, false when there were not enough items to delete
  6532. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6533. {
  6534. bool delete_items = false;
  6535. int i, amount, size;
  6536. struct item *items;
  6537. // prefer always non-equipped items
  6538. it->equip = 0;
  6539. // when searching for nameid only, prefer additionally
  6540. if( !exact_match )
  6541. {
  6542. // non-refined items
  6543. it->refine = 0;
  6544. // card-less items
  6545. memset(it->card, 0, sizeof(it->card));
  6546. }
  6547. switch(loc) {
  6548. case TABLE_CART:
  6549. size = MAX_CART;
  6550. items = sd->cart.u.items_cart;
  6551. break;
  6552. case TABLE_STORAGE:
  6553. size = MAX_STORAGE;
  6554. items = sd->storage.u.items_storage;
  6555. break;
  6556. case TABLE_GUILD_STORAGE:
  6557. {
  6558. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6559. size = MAX_GUILD_STORAGE;
  6560. items = gstor->u.items_guild;
  6561. }
  6562. break;
  6563. default: // TABLE_INVENTORY
  6564. size = MAX_INVENTORY;
  6565. items = sd->inventory.u.items_inventory;
  6566. break;
  6567. }
  6568. for(;;)
  6569. {
  6570. unsigned short important = 0;
  6571. amount = it->amount;
  6572. // 1st pass -- less important items / exact match
  6573. for( i = 0; amount && i < size; i++ )
  6574. {
  6575. struct item *itm = NULL;
  6576. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6577. {// wrong/invalid item
  6578. continue;
  6579. }
  6580. if( itm->equip != it->equip || itm->refine != it->refine )
  6581. {// not matching attributes
  6582. important++;
  6583. continue;
  6584. }
  6585. if( exact_match )
  6586. {
  6587. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6588. {// not matching exact attributes
  6589. continue;
  6590. }
  6591. }
  6592. else
  6593. {
  6594. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6595. {
  6596. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6597. {// pet which cannot be deleted
  6598. continue;
  6599. }
  6600. }
  6601. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6602. {// named/carded item
  6603. important++;
  6604. continue;
  6605. }
  6606. }
  6607. // count / delete item
  6608. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6609. }
  6610. // 2nd pass -- any matching item
  6611. if( amount == 0 || important == 0 )
  6612. {// either everything was already consumed or no items were skipped
  6613. ;
  6614. }
  6615. else for( i = 0; amount && i < size; i++ )
  6616. {
  6617. struct item *itm = NULL;
  6618. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6619. {// wrong/invalid item
  6620. continue;
  6621. }
  6622. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6623. {// pet which cannot be deleted
  6624. continue;
  6625. }
  6626. if( exact_match )
  6627. {
  6628. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6629. {// not matching attributes
  6630. continue;
  6631. }
  6632. }
  6633. // count / delete item
  6634. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6635. }
  6636. if( amount )
  6637. {// not enough items
  6638. return false;
  6639. }
  6640. else if( delete_items )
  6641. {// we are done with the work
  6642. return true;
  6643. }
  6644. else
  6645. {// get rid of the items now
  6646. delete_items = true;
  6647. }
  6648. }
  6649. }
  6650. /// Deletes items from the target/attached player.
  6651. /// Prioritizes ordinary items.
  6652. ///
  6653. /// delitem <item id>,<amount>{,<account id>}
  6654. /// delitem "<item name>",<amount>{,<account id>}
  6655. /// cartdelitem <item id>,<amount>{,<account id>}
  6656. /// cartdelitem "<item name>",<amount>{,<account id>}
  6657. /// storagedelitem <item id>,<amount>{,<account id>}
  6658. /// storagedelitem "<item name>",<amount>{,<account id>}
  6659. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  6660. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  6661. BUILDIN_FUNC(delitem)
  6662. {
  6663. TBL_PC *sd;
  6664. struct item it;
  6665. struct script_data *data;
  6666. uint8 loc = 0;
  6667. char* command = (char*)script_getfuncname(st);
  6668. if(!strncmp(command, "cart", 4))
  6669. loc = TABLE_CART;
  6670. else if(!strncmp(command, "storage", 7))
  6671. loc = TABLE_STORAGE;
  6672. else if(!strncmp(command, "guildstorage", 12))
  6673. loc = TABLE_GUILD_STORAGE;
  6674. if( script_hasdata(st,4) )
  6675. {
  6676. uint32 account_id = script_getnum(st,4);
  6677. sd = map_id2sd(account_id); // <account id>
  6678. if( sd == NULL )
  6679. {
  6680. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6681. st->state = END;
  6682. return SCRIPT_CMD_FAILURE;
  6683. }
  6684. }
  6685. else
  6686. {
  6687. sd = script_rid2sd(st);// attached player
  6688. if( sd == NULL )
  6689. return SCRIPT_CMD_SUCCESS;
  6690. }
  6691. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  6692. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6693. script_pushint(st, -1);
  6694. return SCRIPT_CMD_FAILURE;
  6695. }
  6696. if (loc == TABLE_GUILD_STORAGE) {
  6697. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6698. if (gstor == NULL || sd->state.storage_flag) {
  6699. script_pushint(st, -1);
  6700. return SCRIPT_CMD_FAILURE;
  6701. }
  6702. }
  6703. data = script_getdata(st,2);
  6704. get_val(st,data);
  6705. if( data_isstring(data) )
  6706. {
  6707. const char* item_name = conv_str(st,data);
  6708. struct item_data* id = itemdb_searchname(item_name);
  6709. if( id == NULL )
  6710. {
  6711. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6712. st->state = END;
  6713. return SCRIPT_CMD_FAILURE;
  6714. }
  6715. it.nameid = id->nameid;// "<item name>"
  6716. }
  6717. else
  6718. {
  6719. it.nameid = conv_num(st,data);// <item id>
  6720. if( !itemdb_exists( it.nameid ) )
  6721. {
  6722. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6723. st->state = END;
  6724. return SCRIPT_CMD_FAILURE;
  6725. }
  6726. }
  6727. it.amount=script_getnum(st,3);
  6728. if( it.amount <= 0 )
  6729. return SCRIPT_CMD_SUCCESS;// nothing to do
  6730. if( buildin_delitem_search(sd, &it, false, loc) )
  6731. {// success
  6732. return SCRIPT_CMD_SUCCESS;
  6733. }
  6734. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6735. st->state = END;
  6736. st->mes_active = 0;
  6737. clif_scriptclose(sd, st->oid);
  6738. return SCRIPT_CMD_FAILURE;
  6739. }
  6740. /// Deletes items from the target/attached player.
  6741. ///
  6742. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6743. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6744. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6745. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6746. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6747. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6748. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6749. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6750. BUILDIN_FUNC(delitem2)
  6751. {
  6752. TBL_PC *sd;
  6753. struct item it;
  6754. struct script_data *data;
  6755. uint8 loc = 0;
  6756. char* command = (char*)script_getfuncname(st);
  6757. if(!strncmp(command, "cart", 4))
  6758. loc = TABLE_CART;
  6759. else if(!strncmp(command, "storage", 7))
  6760. loc = TABLE_STORAGE;
  6761. else if(!strncmp(command, "guildstorage", 12))
  6762. loc = TABLE_GUILD_STORAGE;
  6763. if( script_hasdata(st,11) )
  6764. {
  6765. uint32 account_id = script_getnum(st,11);
  6766. sd = map_id2sd(account_id); // <account id>
  6767. if( sd == NULL )
  6768. {
  6769. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6770. st->state = END;
  6771. return SCRIPT_CMD_FAILURE;
  6772. }
  6773. }
  6774. else
  6775. {
  6776. sd = script_rid2sd(st);// attached player
  6777. if( sd == NULL )
  6778. return SCRIPT_CMD_SUCCESS;
  6779. }
  6780. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  6781. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6782. script_pushint(st,-1);
  6783. return SCRIPT_CMD_FAILURE;
  6784. }
  6785. if (loc == TABLE_GUILD_STORAGE) {
  6786. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6787. if (gstor == NULL || sd->state.storage_flag) {
  6788. script_pushint(st, -1);
  6789. return SCRIPT_CMD_FAILURE;
  6790. }
  6791. }
  6792. data = script_getdata(st,2);
  6793. get_val(st,data);
  6794. if( data_isstring(data) )
  6795. {
  6796. const char* item_name = conv_str(st,data);
  6797. struct item_data* id = itemdb_searchname(item_name);
  6798. if( id == NULL )
  6799. {
  6800. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6801. st->state = END;
  6802. return SCRIPT_CMD_FAILURE;
  6803. }
  6804. it.nameid = id->nameid;// "<item name>"
  6805. }
  6806. else
  6807. {
  6808. it.nameid = conv_num(st,data);// <item id>
  6809. if( !itemdb_exists( it.nameid ) )
  6810. {
  6811. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6812. st->state = END;
  6813. return SCRIPT_CMD_FAILURE;
  6814. }
  6815. }
  6816. it.amount=script_getnum(st,3);
  6817. it.identify=script_getnum(st,4);
  6818. it.refine=script_getnum(st,5);
  6819. it.attribute=script_getnum(st,6);
  6820. it.card[0]=(short)script_getnum(st,7);
  6821. it.card[1]=(short)script_getnum(st,8);
  6822. it.card[2]=(short)script_getnum(st,9);
  6823. it.card[3]=(short)script_getnum(st,10);
  6824. if( it.amount <= 0 )
  6825. return SCRIPT_CMD_SUCCESS;// nothing to do
  6826. if( buildin_delitem_search(sd, &it, true, loc) )
  6827. {// success
  6828. return SCRIPT_CMD_SUCCESS;
  6829. }
  6830. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6831. st->state = END;
  6832. st->mes_active = 0;
  6833. clif_scriptclose(sd, st->oid);
  6834. return SCRIPT_CMD_FAILURE;
  6835. }
  6836. /*==========================================
  6837. * Enables/Disables use of items while in an NPC [Skotlex]
  6838. *------------------------------------------*/
  6839. BUILDIN_FUNC(enableitemuse)
  6840. {
  6841. TBL_PC *sd;
  6842. sd=script_rid2sd(st);
  6843. if (sd)
  6844. st->npc_item_flag = sd->npc_item_flag = 1;
  6845. return SCRIPT_CMD_SUCCESS;
  6846. }
  6847. BUILDIN_FUNC(disableitemuse)
  6848. {
  6849. TBL_PC *sd;
  6850. sd=script_rid2sd(st);
  6851. if (sd)
  6852. st->npc_item_flag = sd->npc_item_flag = 0;
  6853. return SCRIPT_CMD_SUCCESS;
  6854. }
  6855. /*==========================================
  6856. * Returns a character's specified stat.
  6857. * Check pc_readparam for available options.
  6858. * readparam <param>{,"<nick>"}
  6859. *------------------------------------------*/
  6860. BUILDIN_FUNC(readparam)
  6861. {
  6862. int value;
  6863. struct script_data *data = script_getdata(st, 2);
  6864. TBL_PC *sd;
  6865. if (!script_nick2sd(3,sd)) {
  6866. script_pushint(st,-1);
  6867. return SCRIPT_CMD_FAILURE;
  6868. }
  6869. // If you use a parameter, return the value behind it
  6870. if( reference_toparam(data) ){
  6871. get_val_(st, data, sd);
  6872. value = (int)data->u.num;
  6873. }else{
  6874. value = pc_readparam(sd,script_getnum(st, 2));
  6875. }
  6876. script_pushint(st,value);
  6877. return SCRIPT_CMD_SUCCESS;
  6878. }
  6879. /*==========================================
  6880. * Return charid identification
  6881. * return by @num :
  6882. * 0 : char_id
  6883. * 1 : party_id
  6884. * 2 : guild_id
  6885. * 3 : account_id
  6886. * 4 : bg_id
  6887. *------------------------------------------*/
  6888. BUILDIN_FUNC(getcharid)
  6889. {
  6890. int num;
  6891. TBL_PC *sd;
  6892. num = script_getnum(st,2);
  6893. if( script_hasdata(st,3) )
  6894. sd=map_nick2sd(script_getstr(st,3));
  6895. else
  6896. sd=script_rid2sd(st);
  6897. if(sd==NULL){
  6898. script_pushint(st,0); //return 0, according docs
  6899. return SCRIPT_CMD_SUCCESS;
  6900. }
  6901. switch( num ) {
  6902. case 0: script_pushint(st,sd->status.char_id); break;
  6903. case 1: script_pushint(st,sd->status.party_id); break;
  6904. case 2: script_pushint(st,sd->status.guild_id); break;
  6905. case 3: script_pushint(st,sd->status.account_id); break;
  6906. case 4: script_pushint(st,sd->bg_id); break;
  6907. default:
  6908. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6909. script_pushint(st,0);
  6910. break;
  6911. }
  6912. return SCRIPT_CMD_SUCCESS;
  6913. }
  6914. /*==========================================
  6915. * returns the GID of an NPC
  6916. *------------------------------------------*/
  6917. BUILDIN_FUNC(getnpcid)
  6918. {
  6919. int num = script_getnum(st,2);
  6920. struct npc_data* nd = NULL;
  6921. if( script_hasdata(st,3) )
  6922. {// unique npc name
  6923. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6924. {
  6925. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6926. script_pushint(st,0);
  6927. return SCRIPT_CMD_FAILURE;
  6928. }
  6929. }
  6930. switch (num) {
  6931. case 0:
  6932. script_pushint(st,nd ? nd->bl.id : st->oid);
  6933. break;
  6934. default:
  6935. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6936. script_pushint(st,0);
  6937. return SCRIPT_CMD_FAILURE;
  6938. }
  6939. return SCRIPT_CMD_SUCCESS;
  6940. }
  6941. /*==========================================
  6942. * Return the name of the party_id
  6943. * null if not found
  6944. *------------------------------------------*/
  6945. BUILDIN_FUNC(getpartyname)
  6946. {
  6947. int party_id;
  6948. struct party_data* p;
  6949. party_id = script_getnum(st,2);
  6950. if( ( p = party_search(party_id) ) != NULL )
  6951. {
  6952. script_pushstrcopy(st,p->party.name);
  6953. }
  6954. else
  6955. {
  6956. script_pushconststr(st,"null");
  6957. }
  6958. return SCRIPT_CMD_SUCCESS;
  6959. }
  6960. /*==========================================
  6961. * Get the information of the members of a party by type
  6962. * @party_id, @type
  6963. * return by @type :
  6964. * - : nom des membres
  6965. * 1 : char_id des membres
  6966. * 2 : account_id des membres
  6967. *------------------------------------------*/
  6968. BUILDIN_FUNC(getpartymember)
  6969. {
  6970. struct party_data *p;
  6971. uint8 j = 0;
  6972. p = party_search(script_getnum(st,2));
  6973. if (p != NULL) {
  6974. uint8 i, type = 0;
  6975. struct script_data *data = NULL;
  6976. char *varname = NULL;
  6977. if (script_hasdata(st,3))
  6978. type = script_getnum(st,3);
  6979. if (script_hasdata(st,4)) {
  6980. data = script_getdata(st, 4);
  6981. if (!data_isreference(data)) {
  6982. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  6983. return SCRIPT_CMD_FAILURE;
  6984. }
  6985. varname = reference_getname(data);
  6986. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  6987. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  6988. return SCRIPT_CMD_FAILURE;
  6989. }
  6990. }
  6991. for (i = 0; i < MAX_PARTY; i++) {
  6992. if (p->party.member[i].account_id) {
  6993. switch (type) {
  6994. case 2:
  6995. if (data)
  6996. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  6997. else
  6998. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6999. break;
  7000. case 1:
  7001. if (data)
  7002. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  7003. else
  7004. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7005. break;
  7006. default:
  7007. if (data)
  7008. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  7009. else
  7010. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7011. break;
  7012. }
  7013. j++;
  7014. }
  7015. }
  7016. }
  7017. mapreg_setreg(add_str("$@partymembercount"),j);
  7018. script_pushint(st, j);
  7019. return SCRIPT_CMD_SUCCESS;
  7020. }
  7021. /*==========================================
  7022. * Retrieves party leader. if flag is specified,
  7023. * return some of the leader data. Otherwise, return name.
  7024. *------------------------------------------*/
  7025. BUILDIN_FUNC(getpartyleader)
  7026. {
  7027. int party_id, type = 0, i=0;
  7028. struct party_data *p;
  7029. party_id=script_getnum(st,2);
  7030. if( script_hasdata(st,3) )
  7031. type=script_getnum(st,3);
  7032. p=party_search(party_id);
  7033. if (p) //Search leader
  7034. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7035. if (!p || i == MAX_PARTY) { //leader not found
  7036. if (type)
  7037. script_pushint(st,-1);
  7038. else
  7039. script_pushconststr(st,"null");
  7040. return SCRIPT_CMD_SUCCESS;
  7041. }
  7042. switch (type) {
  7043. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7044. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7045. case 3: script_pushint(st,p->party.member[i].class_); break;
  7046. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7047. case 5: script_pushint(st,p->party.member[i].lv); break;
  7048. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7049. }
  7050. return SCRIPT_CMD_SUCCESS;
  7051. }
  7052. /*==========================================
  7053. * Return the name of the @guild_id
  7054. * null if not found
  7055. *------------------------------------------*/
  7056. BUILDIN_FUNC(getguildname)
  7057. {
  7058. int guild_id;
  7059. struct guild* g;
  7060. guild_id = script_getnum(st,2);
  7061. if( ( g = guild_search(guild_id) ) != NULL )
  7062. script_pushstrcopy(st,g->name);
  7063. else
  7064. script_pushconststr(st,"null");
  7065. return SCRIPT_CMD_SUCCESS;
  7066. }
  7067. /*==========================================
  7068. * Return the name of the guild master of @guild_id
  7069. * null if not found
  7070. *------------------------------------------*/
  7071. BUILDIN_FUNC(getguildmaster)
  7072. {
  7073. int guild_id;
  7074. struct guild* g;
  7075. guild_id = script_getnum(st,2);
  7076. if( ( g = guild_search(guild_id) ) != NULL )
  7077. script_pushstrcopy(st,g->member[0].name);
  7078. else
  7079. script_pushconststr(st,"null");
  7080. return SCRIPT_CMD_SUCCESS;
  7081. }
  7082. BUILDIN_FUNC(getguildmasterid)
  7083. {
  7084. int guild_id;
  7085. struct guild* g;
  7086. guild_id = script_getnum(st,2);
  7087. if( ( g = guild_search(guild_id) ) != NULL )
  7088. script_pushint(st,g->member[0].char_id);
  7089. else
  7090. script_pushint(st,0);
  7091. return SCRIPT_CMD_SUCCESS;
  7092. }
  7093. /*==========================================
  7094. * Get char string information by type :
  7095. * Return by @type :
  7096. * 0 : char_name
  7097. * 1 : party_name or ""
  7098. * 2 : guild_name or ""
  7099. * 3 : map_name
  7100. * - : ""
  7101. * strcharinfo(<type>{,<char_id>})
  7102. *------------------------------------------*/
  7103. BUILDIN_FUNC(strcharinfo)
  7104. {
  7105. TBL_PC *sd;
  7106. int num;
  7107. struct guild* g;
  7108. struct party_data* p;
  7109. if (!script_charid2sd(3,sd)) {
  7110. script_pushconststr(st,"");
  7111. return SCRIPT_CMD_FAILURE;
  7112. }
  7113. num=script_getnum(st,2);
  7114. switch(num){
  7115. case 0:
  7116. script_pushstrcopy(st,sd->status.name);
  7117. break;
  7118. case 1:
  7119. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7120. script_pushstrcopy(st,p->party.name);
  7121. } else {
  7122. script_pushconststr(st,"");
  7123. }
  7124. break;
  7125. case 2:
  7126. if( ( g = sd->guild ) != NULL ) {
  7127. script_pushstrcopy(st,g->name);
  7128. } else {
  7129. script_pushconststr(st,"");
  7130. }
  7131. break;
  7132. case 3:
  7133. script_pushconststr(st,map[sd->bl.m].name);
  7134. break;
  7135. default:
  7136. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7137. script_pushconststr(st,"");
  7138. break;
  7139. }
  7140. return SCRIPT_CMD_SUCCESS;
  7141. }
  7142. /*==========================================
  7143. * Get npc string information by type
  7144. * return by @type:
  7145. * 0 : name
  7146. * 1 : str#
  7147. * 2 : #str
  7148. * 3 : ::str
  7149. * 4 : map name
  7150. *------------------------------------------*/
  7151. BUILDIN_FUNC(strnpcinfo)
  7152. {
  7153. TBL_NPC* nd;
  7154. int num;
  7155. char *buf,*name=NULL;
  7156. nd = map_id2nd(st->oid);
  7157. if (!nd) {
  7158. script_pushconststr(st, "");
  7159. return SCRIPT_CMD_SUCCESS;
  7160. }
  7161. num = script_getnum(st,2);
  7162. switch(num){
  7163. case 0: // display name
  7164. name = aStrdup(nd->name);
  7165. break;
  7166. case 1: // visible part of display name
  7167. if((buf = strchr(nd->name,'#')) != NULL)
  7168. {
  7169. name = aStrdup(nd->name);
  7170. name[buf - nd->name] = 0;
  7171. } else // Return the name, there is no '#' present
  7172. name = aStrdup(nd->name);
  7173. break;
  7174. case 2: // # fragment
  7175. if((buf = strchr(nd->name,'#')) != NULL)
  7176. name = aStrdup(buf+1);
  7177. break;
  7178. case 3: // unique name
  7179. name = aStrdup(nd->exname);
  7180. break;
  7181. case 4: // map name
  7182. name = aStrdup(map[nd->bl.m].name);
  7183. break;
  7184. }
  7185. if(name)
  7186. script_pushstr(st, name);
  7187. else
  7188. script_pushconststr(st, "");
  7189. return SCRIPT_CMD_SUCCESS;
  7190. }
  7191. /**
  7192. * getequipid(<equipment slot>{,<char_id>})
  7193. **/
  7194. BUILDIN_FUNC(getequipid)
  7195. {
  7196. int i, num;
  7197. TBL_PC* sd;
  7198. struct item_data* item;
  7199. if (!script_charid2sd(3, sd)) {
  7200. script_pushint(st,-1);
  7201. return SCRIPT_CMD_FAILURE;
  7202. }
  7203. num = script_getnum(st,2);
  7204. if( !equip_index_check(num) )
  7205. {
  7206. script_pushint(st,-1);
  7207. return SCRIPT_CMD_FAILURE;
  7208. }
  7209. // get inventory position of item
  7210. i = pc_checkequip(sd,equip_bitmask[num]);
  7211. if( i < 0 )
  7212. {
  7213. script_pushint(st,-1);
  7214. return SCRIPT_CMD_SUCCESS;
  7215. }
  7216. item = sd->inventory_data[i];
  7217. if( item != 0 )
  7218. script_pushint(st,item->nameid);
  7219. else
  7220. script_pushint(st,0);
  7221. return SCRIPT_CMD_SUCCESS;
  7222. }
  7223. /*==========================================
  7224. * getequipuniqueid(<equipment slot>{,<char_id>})
  7225. *------------------------------------------*/
  7226. BUILDIN_FUNC(getequipuniqueid)
  7227. {
  7228. int i, num;
  7229. TBL_PC* sd;
  7230. struct item* item;
  7231. if (!script_charid2sd(3, sd)) {
  7232. script_pushconststr(st, "");
  7233. return SCRIPT_CMD_FAILURE;
  7234. }
  7235. num = script_getnum(st,2);
  7236. if ( !equip_index_check(num) ) {
  7237. script_pushconststr(st, "");
  7238. return SCRIPT_CMD_FAILURE;
  7239. }
  7240. // get inventory position of item
  7241. i = pc_checkequip(sd,equip_bitmask[num]);
  7242. if (i < 0) {
  7243. script_pushconststr(st, "");
  7244. return SCRIPT_CMD_FAILURE;
  7245. }
  7246. item = &sd->inventory.u.items_inventory[i];
  7247. if (item != 0) {
  7248. int maxlen = 256;
  7249. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7250. memset(buf, 0, maxlen);
  7251. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7252. script_pushstr(st, buf);
  7253. } else
  7254. script_pushconststr(st, "");
  7255. return SCRIPT_CMD_SUCCESS;
  7256. }
  7257. /*==========================================
  7258. * Get the equipement name at pos
  7259. * return item jname or ""
  7260. * getequipname(<equipment slot>{,<char_id>})
  7261. *------------------------------------------*/
  7262. BUILDIN_FUNC(getequipname)
  7263. {
  7264. int i, num;
  7265. TBL_PC* sd;
  7266. struct item_data* item;
  7267. if (!script_charid2sd(3, sd)) {
  7268. script_pushconststr(st, "");
  7269. return SCRIPT_CMD_FAILURE;
  7270. }
  7271. num = script_getnum(st,2);
  7272. if( !equip_index_check(num) )
  7273. {
  7274. script_pushconststr(st,"");
  7275. return SCRIPT_CMD_SUCCESS;
  7276. }
  7277. // get inventory position of item
  7278. i = pc_checkequip(sd,equip_bitmask[num]);
  7279. if( i < 0 )
  7280. {
  7281. script_pushconststr(st,"");
  7282. return SCRIPT_CMD_SUCCESS;
  7283. }
  7284. item = sd->inventory_data[i];
  7285. if( item != 0 )
  7286. script_pushstrcopy(st,item->jname);
  7287. else
  7288. script_pushconststr(st,"");
  7289. return SCRIPT_CMD_SUCCESS;
  7290. }
  7291. /*==========================================
  7292. * getbrokenid [Valaris]
  7293. * getbrokenid(<number>{,<char_id>})
  7294. *------------------------------------------*/
  7295. BUILDIN_FUNC(getbrokenid)
  7296. {
  7297. int i,num,id = 0,brokencounter = 0;
  7298. TBL_PC *sd;
  7299. if (!script_charid2sd(3, sd)) {
  7300. script_pushint(st,0);
  7301. return SCRIPT_CMD_FAILURE;
  7302. }
  7303. num = script_getnum(st,2);
  7304. for(i = 0; i < MAX_INVENTORY; i++) {
  7305. if(sd->inventory.u.items_inventory[i].attribute) {
  7306. brokencounter++;
  7307. if(num == brokencounter){
  7308. id = sd->inventory.u.items_inventory[i].nameid;
  7309. break;
  7310. }
  7311. }
  7312. }
  7313. script_pushint(st,id);
  7314. return SCRIPT_CMD_SUCCESS;
  7315. }
  7316. /*==========================================
  7317. * repair [Valaris]
  7318. * repair <num>{,<char_id>};
  7319. *------------------------------------------*/
  7320. BUILDIN_FUNC(repair)
  7321. {
  7322. int i,num;
  7323. int repaircounter = 0;
  7324. TBL_PC *sd;
  7325. if (!script_charid2sd(3,sd))
  7326. return SCRIPT_CMD_FAILURE;
  7327. num = script_getnum(st,2);
  7328. for(i = 0; i < MAX_INVENTORY; i++) {
  7329. if(sd->inventory.u.items_inventory[i].attribute) {
  7330. repaircounter++;
  7331. if(num == repaircounter) {
  7332. sd->inventory.u.items_inventory[i].attribute = 0;
  7333. clif_equiplist(sd);
  7334. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  7335. clif_misceffect(&sd->bl, 3);
  7336. break;
  7337. }
  7338. }
  7339. }
  7340. return SCRIPT_CMD_SUCCESS;
  7341. }
  7342. /*==========================================
  7343. * repairall {<char_id>}
  7344. *------------------------------------------*/
  7345. BUILDIN_FUNC(repairall)
  7346. {
  7347. int i, repaircounter = 0;
  7348. TBL_PC *sd;
  7349. if (!script_charid2sd(2,sd))
  7350. return SCRIPT_CMD_FAILURE;
  7351. for(i = 0; i < MAX_INVENTORY; i++)
  7352. {
  7353. if(sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].attribute)
  7354. {
  7355. sd->inventory.u.items_inventory[i].attribute = 0;
  7356. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  7357. repaircounter++;
  7358. }
  7359. }
  7360. if(repaircounter)
  7361. {
  7362. clif_misceffect(&sd->bl, 3);
  7363. clif_equiplist(sd);
  7364. }
  7365. return SCRIPT_CMD_SUCCESS;
  7366. }
  7367. /*==========================================
  7368. * Chk if player have something equiped at pos
  7369. * getequipisequiped <pos>{,<char_id>}
  7370. *------------------------------------------*/
  7371. BUILDIN_FUNC(getequipisequiped)
  7372. {
  7373. int i = -1,num;
  7374. TBL_PC *sd;
  7375. num = script_getnum(st,2);
  7376. if (!script_charid2sd(3,sd))
  7377. return SCRIPT_CMD_FAILURE;
  7378. if ( equip_index_check(num) )
  7379. i=pc_checkequip(sd,equip_bitmask[num]);
  7380. if(i >= 0)
  7381. script_pushint(st,1);
  7382. else
  7383. script_pushint(st,0);
  7384. return SCRIPT_CMD_SUCCESS;
  7385. }
  7386. /*==========================================
  7387. * Chk if the player have something equiped at pos
  7388. * if so chk if this item ain't marked not refinable or rental
  7389. * return (npc)
  7390. * 1 : true
  7391. * 0 : false
  7392. * getequipisenableref(<equipment slot>{,<char_id>})
  7393. *------------------------------------------*/
  7394. BUILDIN_FUNC(getequipisenableref)
  7395. {
  7396. int i = -1,num;
  7397. TBL_PC *sd;
  7398. num = script_getnum(st,2);
  7399. if (!script_charid2sd(3, sd)) {
  7400. script_pushint(st,0);
  7401. return SCRIPT_CMD_FAILURE;
  7402. }
  7403. if( equip_index_check(num) )
  7404. i = pc_checkequip(sd,equip_bitmask[num]);
  7405. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  7406. script_pushint(st,1);
  7407. else
  7408. script_pushint(st,0);
  7409. return SCRIPT_CMD_SUCCESS;
  7410. }
  7411. /*==========================================
  7412. * Get the item refined value at pos
  7413. * return (npc)
  7414. * x : refine amount
  7415. * 0 : false (not refined)
  7416. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7417. *------------------------------------------*/
  7418. BUILDIN_FUNC(getequiprefinerycnt)
  7419. {
  7420. int i = -1,num;
  7421. TBL_PC *sd;
  7422. num = script_getnum(st,2);
  7423. if (!script_charid2sd(3, sd)) {
  7424. script_pushint(st,0);
  7425. return SCRIPT_CMD_FAILURE;
  7426. }
  7427. if (equip_index_check(num))
  7428. i=pc_checkequip(sd,equip_bitmask[num]);
  7429. if(i >= 0)
  7430. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  7431. else
  7432. script_pushint(st,0);
  7433. return SCRIPT_CMD_SUCCESS;
  7434. }
  7435. /*==========================================
  7436. * Get the weapon level value at pos
  7437. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7438. * return (npc)
  7439. * x : weapon level
  7440. * 0 : false
  7441. * getequipweaponlv(<equipment slot>{,<char_id>})
  7442. *------------------------------------------*/
  7443. BUILDIN_FUNC(getequipweaponlv)
  7444. {
  7445. int i = -1, num;
  7446. TBL_PC *sd;
  7447. num = script_getnum(st, 2);
  7448. if (!script_charid2sd(3, sd)) {
  7449. script_pushint(st, 0);
  7450. return SCRIPT_CMD_FAILURE;
  7451. }
  7452. if (num == -1)
  7453. i = current_equip_item_index;
  7454. else if (equip_index_check(num))
  7455. i = pc_checkequip(sd, equip_bitmask[num]);
  7456. if (i >= 0 && sd->inventory_data[i])
  7457. script_pushint(st, sd->inventory_data[i]->wlv);
  7458. else
  7459. script_pushint(st, 0);
  7460. return SCRIPT_CMD_SUCCESS;
  7461. }
  7462. /*==========================================
  7463. * Get the item refine chance (from refine.txt) for item at pos
  7464. * return (npc)
  7465. * x : refine chance
  7466. * 0 : false (max refine level or unequip..)
  7467. * getequippercentrefinery(<equipment slot>{,<char_id>})
  7468. *------------------------------------------*/
  7469. BUILDIN_FUNC(getequippercentrefinery)
  7470. {
  7471. int i = -1,num;
  7472. TBL_PC *sd;
  7473. num = script_getnum(st,2);
  7474. if (!script_charid2sd(3, sd)) {
  7475. script_pushint(st,0);
  7476. return SCRIPT_CMD_FAILURE;
  7477. }
  7478. if (equip_index_check(num))
  7479. i = pc_checkequip(sd,equip_bitmask[num]);
  7480. if(i >= 0 && sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].refine < MAX_REFINE)
  7481. 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));
  7482. else
  7483. script_pushint(st,0);
  7484. return SCRIPT_CMD_SUCCESS;
  7485. }
  7486. /*==========================================
  7487. * Refine +1 item at pos and log and display refine
  7488. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7489. *------------------------------------------*/
  7490. BUILDIN_FUNC(successrefitem) {
  7491. short i = -1, up = 1;
  7492. int pos;
  7493. TBL_PC *sd;
  7494. pos = script_getnum(st,2);
  7495. if (!script_charid2sd(4, sd)) {
  7496. script_pushint(st, -1);
  7497. return SCRIPT_CMD_FAILURE;
  7498. }
  7499. if (script_hasdata(st, 3))
  7500. up = script_getnum(st, 3);
  7501. if (equip_index_check(pos))
  7502. i = pc_checkequip(sd,equip_bitmask[pos]);
  7503. if (i >= 0) {
  7504. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7505. //Logs items, got from (N)PC scripts [Lupus]
  7506. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7507. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  7508. script_pushint(st, MAX_REFINE);
  7509. return SCRIPT_CMD_SUCCESS;
  7510. }
  7511. sd->inventory.u.items_inventory[i].refine += up;
  7512. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7513. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7514. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  7515. clif_delitem(sd,i,1,3);
  7516. //Logs items, got from (N)PC scripts [Lupus]
  7517. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7518. clif_additem(sd,i,1,0);
  7519. pc_equipitem(sd,i,ep);
  7520. clif_misceffect(&sd->bl,3);
  7521. if (sd->inventory.u.items_inventory[i].refine == MAX_REFINE &&
  7522. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  7523. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]))
  7524. { // Fame point system [DracoRPG]
  7525. switch (sd->inventory_data[i]->wlv){
  7526. case 1:
  7527. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7528. break;
  7529. case 2:
  7530. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7531. break;
  7532. case 3:
  7533. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7534. break;
  7535. }
  7536. }
  7537. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7538. return SCRIPT_CMD_SUCCESS;
  7539. }
  7540. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7541. script_pushint(st, -1);
  7542. return SCRIPT_CMD_FAILURE;
  7543. }
  7544. /*==========================================
  7545. * Show a failed Refine +1 attempt
  7546. * failedrefitem <equipment slot>{,<char_id>}
  7547. *------------------------------------------*/
  7548. BUILDIN_FUNC(failedrefitem) {
  7549. short i = -1;
  7550. int pos;
  7551. TBL_PC *sd;
  7552. pos = script_getnum(st,2);
  7553. if (!script_charid2sd(3, sd)) {
  7554. script_pushint(st, 0);
  7555. return SCRIPT_CMD_FAILURE;
  7556. }
  7557. if (equip_index_check(pos))
  7558. i = pc_checkequip(sd,equip_bitmask[pos]);
  7559. if (i >= 0) {
  7560. sd->inventory.u.items_inventory[i].refine = 0;
  7561. pc_unequipitem(sd,i,3); //recalculate bonus
  7562. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  7563. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7564. clif_misceffect(&sd->bl,2); // display failure effect
  7565. script_pushint(st, 1);
  7566. return SCRIPT_CMD_SUCCESS;
  7567. }
  7568. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7569. script_pushint(st, 0);
  7570. return SCRIPT_CMD_FAILURE;
  7571. }
  7572. /*==========================================
  7573. * Downgrades an Equipment Part by -1 . [Masao]
  7574. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7575. *------------------------------------------*/
  7576. BUILDIN_FUNC(downrefitem) {
  7577. short i = -1, down = 1;
  7578. int pos;
  7579. TBL_PC *sd;
  7580. if (!script_charid2sd(4, sd)) {
  7581. script_pushint(st, -1);
  7582. return SCRIPT_CMD_FAILURE;
  7583. }
  7584. pos = script_getnum(st,2);
  7585. if (script_hasdata(st, 3))
  7586. down = script_getnum(st, 3);
  7587. if (equip_index_check(pos))
  7588. i = pc_checkequip(sd,equip_bitmask[pos]);
  7589. if (i >= 0) {
  7590. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7591. //Logs items, got from (N)PC scripts [Lupus]
  7592. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7593. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7594. sd->inventory.u.items_inventory[i].refine -= down;
  7595. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7596. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  7597. clif_delitem(sd,i,1,3);
  7598. //Logs items, got from (N)PC scripts [Lupus]
  7599. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7600. clif_additem(sd,i,1,0);
  7601. pc_equipitem(sd,i,ep);
  7602. clif_misceffect(&sd->bl,2);
  7603. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7604. return SCRIPT_CMD_SUCCESS;
  7605. }
  7606. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7607. script_pushint(st, -1);
  7608. return SCRIPT_CMD_FAILURE;
  7609. }
  7610. /**
  7611. * Delete the item equipped at pos.
  7612. * delequip <equipment slot>{,<char_id>};
  7613. **/
  7614. BUILDIN_FUNC(delequip) {
  7615. short i = -1;
  7616. int pos;
  7617. int8 ret;
  7618. TBL_PC *sd;
  7619. pos = script_getnum(st,2);
  7620. if (!script_charid2sd(3,sd)) {
  7621. st->state = END;
  7622. return SCRIPT_CMD_FAILURE;
  7623. }
  7624. if (equip_index_check(pos))
  7625. i = pc_checkequip(sd,equip_bitmask[pos]);
  7626. if (i >= 0) {
  7627. pc_unequipitem(sd,i,3); //recalculate bonus
  7628. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7629. }
  7630. else {
  7631. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7632. st->state = END;
  7633. return SCRIPT_CMD_FAILURE;
  7634. }
  7635. script_pushint(st,ret);
  7636. return SCRIPT_CMD_SUCCESS;
  7637. }
  7638. /**
  7639. * Break the item equipped at pos.
  7640. * breakequip <equipment slot>{,<char_id>};
  7641. **/
  7642. BUILDIN_FUNC(breakequip) {
  7643. short i = -1;
  7644. int pos;
  7645. TBL_PC *sd;
  7646. pos = script_getnum(st,2);
  7647. if (!script_charid2sd(3,sd))
  7648. return SCRIPT_CMD_FAILURE;
  7649. if (equip_index_check(pos))
  7650. i = pc_checkequip(sd,equip_bitmask[pos]);
  7651. if (i >= 0) {
  7652. sd->inventory.u.items_inventory[i].attribute = 1;
  7653. pc_unequipitem(sd,i,3);
  7654. clif_equiplist(sd);
  7655. script_pushint(st,1);
  7656. return SCRIPT_CMD_SUCCESS;
  7657. }
  7658. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7659. script_pushint(st,0);
  7660. return SCRIPT_CMD_FAILURE;
  7661. }
  7662. /**
  7663. * statusup <stat>{,<char_id>};
  7664. **/
  7665. BUILDIN_FUNC(statusup)
  7666. {
  7667. int type;
  7668. TBL_PC *sd;
  7669. type = script_getnum(st,2);
  7670. if (!script_charid2sd(3, sd))
  7671. return SCRIPT_CMD_FAILURE;
  7672. pc_statusup(sd, type, 1);
  7673. return SCRIPT_CMD_SUCCESS;
  7674. }
  7675. /**
  7676. * statusup2 <stat>,<amount>{,<char_id>};
  7677. **/
  7678. BUILDIN_FUNC(statusup2)
  7679. {
  7680. int type,val;
  7681. TBL_PC *sd;
  7682. type=script_getnum(st,2);
  7683. val=script_getnum(st,3);
  7684. if (!script_charid2sd(4, sd))
  7685. return SCRIPT_CMD_FAILURE;
  7686. pc_statusup2(sd,type,val);
  7687. return SCRIPT_CMD_SUCCESS;
  7688. }
  7689. /// See 'doc/item_bonus.txt'
  7690. ///
  7691. /// bonus <bonus type>,<val1>;
  7692. /// bonus2 <bonus type>,<val1>,<val2>;
  7693. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7694. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7695. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7696. BUILDIN_FUNC(bonus)
  7697. {
  7698. int type;
  7699. int val1;
  7700. int val2 = 0;
  7701. int val3 = 0;
  7702. int val4 = 0;
  7703. int val5 = 0;
  7704. TBL_PC* sd;
  7705. struct script_data *data;
  7706. sd = script_rid2sd(st);
  7707. if( sd == NULL )
  7708. return SCRIPT_CMD_SUCCESS; // no player attached
  7709. type = script_getnum(st,2);
  7710. switch( type ) {
  7711. case SP_AUTOSPELL:
  7712. case SP_AUTOSPELL_WHENHIT:
  7713. case SP_AUTOSPELL_ONSKILL:
  7714. case SP_SKILL_ATK:
  7715. case SP_SKILL_HEAL:
  7716. case SP_SKILL_HEAL2:
  7717. case SP_ADD_SKILL_BLOW:
  7718. case SP_CASTRATE:
  7719. case SP_ADDEFF_ONSKILL:
  7720. case SP_SKILL_USE_SP_RATE:
  7721. case SP_SKILL_COOLDOWN:
  7722. case SP_SKILL_FIXEDCAST:
  7723. case SP_SKILL_VARIABLECAST:
  7724. case SP_VARCASTRATE:
  7725. case SP_FIXCASTRATE:
  7726. case SP_SKILL_USE_SP:
  7727. case SP_SUB_SKILL:
  7728. // these bonuses support skill names
  7729. data = script_getdata(st, 3);
  7730. get_val(st, data); // Convert into value in case of a variable
  7731. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7732. break;
  7733. default:
  7734. val1 = script_getnum(st,3);
  7735. break;
  7736. }
  7737. switch( script_lastdata(st)-2 ) {
  7738. case 1:
  7739. pc_bonus(sd, type, val1);
  7740. break;
  7741. case 2:
  7742. val2 = script_getnum(st,4);
  7743. pc_bonus2(sd, type, val1, val2);
  7744. break;
  7745. case 3:
  7746. val2 = script_getnum(st,4);
  7747. val3 = script_getnum(st,5);
  7748. pc_bonus3(sd, type, val1, val2, val3);
  7749. break;
  7750. case 4:
  7751. data = script_getdata(st, 4);
  7752. get_val(st, data); // Convert into value in case of a variable
  7753. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7754. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7755. else
  7756. val2 = script_getnum(st,4);
  7757. val3 = script_getnum(st,5);
  7758. val4 = script_getnum(st,6);
  7759. pc_bonus4(sd, type, val1, val2, val3, val4);
  7760. break;
  7761. case 5:
  7762. data = script_getdata(st, 4);
  7763. get_val(st, data); // Convert into value in case of a variable
  7764. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7765. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7766. else
  7767. val2 = script_getnum(st,4);
  7768. val3 = script_getnum(st,5);
  7769. val4 = script_getnum(st,6);
  7770. val5 = script_getnum(st,7);
  7771. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7772. break;
  7773. default:
  7774. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7775. break;
  7776. }
  7777. return SCRIPT_CMD_SUCCESS;
  7778. }
  7779. BUILDIN_FUNC(autobonus)
  7780. {
  7781. unsigned int dur, pos;
  7782. short rate;
  7783. short atk_type = 0;
  7784. TBL_PC* sd;
  7785. const char *bonus_script, *other_script = NULL;
  7786. sd = script_rid2sd(st);
  7787. if( sd == NULL )
  7788. return SCRIPT_CMD_SUCCESS; // no player attached
  7789. if (current_equip_combo_pos)
  7790. pos = current_equip_combo_pos;
  7791. else
  7792. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7793. if((sd->state.autobonus&pos) == pos)
  7794. return SCRIPT_CMD_SUCCESS;
  7795. rate = script_getnum(st,3);
  7796. dur = script_getnum(st,4);
  7797. bonus_script = script_getstr(st,2);
  7798. if( !rate || !dur || !pos || !bonus_script )
  7799. return SCRIPT_CMD_SUCCESS;
  7800. if( script_hasdata(st,5) )
  7801. atk_type = script_getnum(st,5);
  7802. if( script_hasdata(st,6) )
  7803. other_script = script_getstr(st,6);
  7804. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7805. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7806. {
  7807. script_add_autobonus(bonus_script);
  7808. if( other_script )
  7809. script_add_autobonus(other_script);
  7810. }
  7811. return SCRIPT_CMD_SUCCESS;
  7812. }
  7813. BUILDIN_FUNC(autobonus2)
  7814. {
  7815. unsigned int dur, pos;
  7816. short rate;
  7817. short atk_type = 0;
  7818. TBL_PC* sd;
  7819. const char *bonus_script, *other_script = NULL;
  7820. sd = script_rid2sd(st);
  7821. if( sd == NULL )
  7822. return SCRIPT_CMD_SUCCESS; // no player attached
  7823. if (current_equip_combo_pos)
  7824. pos = current_equip_combo_pos;
  7825. else
  7826. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7827. if((sd->state.autobonus&pos) == pos)
  7828. return SCRIPT_CMD_SUCCESS;
  7829. rate = script_getnum(st,3);
  7830. dur = script_getnum(st,4);
  7831. bonus_script = script_getstr(st,2);
  7832. if( !rate || !dur || !pos || !bonus_script )
  7833. return SCRIPT_CMD_SUCCESS;
  7834. if( script_hasdata(st,5) )
  7835. atk_type = script_getnum(st,5);
  7836. if( script_hasdata(st,6) )
  7837. other_script = script_getstr(st,6);
  7838. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7839. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7840. {
  7841. script_add_autobonus(bonus_script);
  7842. if( other_script )
  7843. script_add_autobonus(other_script);
  7844. }
  7845. return SCRIPT_CMD_SUCCESS;
  7846. }
  7847. BUILDIN_FUNC(autobonus3)
  7848. {
  7849. unsigned int dur, pos;
  7850. short rate,atk_type;
  7851. TBL_PC* sd;
  7852. const char *bonus_script, *other_script = NULL;
  7853. struct script_data *data;
  7854. sd = script_rid2sd(st);
  7855. if( sd == NULL )
  7856. return SCRIPT_CMD_SUCCESS; // no player attached
  7857. if (current_equip_combo_pos)
  7858. pos = current_equip_combo_pos;
  7859. else
  7860. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7861. if((sd->state.autobonus&pos) == pos)
  7862. return SCRIPT_CMD_SUCCESS;
  7863. rate = script_getnum(st,3);
  7864. dur = script_getnum(st,4);
  7865. data = script_getdata(st, 5);
  7866. get_val(st, data); // Convert into value in case of a variable
  7867. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7868. bonus_script = script_getstr(st,2);
  7869. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  7870. return SCRIPT_CMD_SUCCESS;
  7871. if( script_hasdata(st,6) )
  7872. other_script = script_getstr(st,6);
  7873. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7874. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  7875. {
  7876. script_add_autobonus(bonus_script);
  7877. if( other_script )
  7878. script_add_autobonus(other_script);
  7879. }
  7880. return SCRIPT_CMD_SUCCESS;
  7881. }
  7882. /// Changes the level of a player skill.
  7883. /// <flag> defaults to 1
  7884. /// <flag>=0 : set the level of the skill
  7885. /// <flag>=1 : set the temporary level of the skill
  7886. /// <flag>=2 : add to the level of the skill
  7887. ///
  7888. /// skill <skill id>,<level>,<flag>
  7889. /// skill <skill id>,<level>
  7890. /// skill "<skill name>",<level>,<flag>
  7891. /// skill "<skill name>",<level>
  7892. BUILDIN_FUNC(skill)
  7893. {
  7894. int id;
  7895. int level;
  7896. int flag = ADDSKILL_TEMP;
  7897. TBL_PC* sd;
  7898. struct script_data *data;
  7899. const char* command = script_getfuncname(st);
  7900. sd = script_rid2sd(st);
  7901. if( sd == NULL )
  7902. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7903. if (strcmpi(command, "addtoskill") == 0)
  7904. flag = ADDSKILL_TEMP_ADDLEVEL;
  7905. data = script_getdata(st, 2);
  7906. get_val(st, data); // Convert into value in case of a variable
  7907. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7908. level = script_getnum(st,3);
  7909. if( script_hasdata(st,4) )
  7910. flag = script_getnum(st,4);
  7911. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  7912. return SCRIPT_CMD_SUCCESS;
  7913. }
  7914. /// Increases the level of a guild skill.
  7915. ///
  7916. /// guildskill <skill id>,<amount>;
  7917. /// guildskill "<skill name>",<amount>;
  7918. BUILDIN_FUNC(guildskill)
  7919. {
  7920. int id;
  7921. int level;
  7922. TBL_PC* sd;
  7923. int i;
  7924. struct script_data *data;
  7925. sd = script_rid2sd(st);
  7926. if( sd == NULL )
  7927. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7928. data = script_getdata(st, 2);
  7929. get_val(st, data); // Convert into value in case of a variable
  7930. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7931. level = script_getnum(st,3);
  7932. for( i=0; i < level; i++ )
  7933. guild_skillup(sd, id);
  7934. return SCRIPT_CMD_SUCCESS;
  7935. }
  7936. /// Returns the level of the player skill.
  7937. ///
  7938. /// getskilllv(<skill id>) -> <level>
  7939. /// getskilllv("<skill name>") -> <level>
  7940. BUILDIN_FUNC(getskilllv)
  7941. {
  7942. int id;
  7943. TBL_PC* sd;
  7944. struct script_data *data;
  7945. sd = script_rid2sd(st);
  7946. if( sd == NULL )
  7947. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7948. data = script_getdata(st, 2);
  7949. get_val(st, data); // Convert into value in case of a variable
  7950. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7951. script_pushint(st, pc_checkskill(sd,id));
  7952. return SCRIPT_CMD_SUCCESS;
  7953. }
  7954. /// Returns the level of the guild skill.
  7955. ///
  7956. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7957. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7958. BUILDIN_FUNC(getgdskilllv)
  7959. {
  7960. int guild_id;
  7961. uint16 skill_id;
  7962. struct guild* g;
  7963. struct script_data *data;
  7964. guild_id = script_getnum(st,2);
  7965. data = script_getdata(st, 3);
  7966. get_val(st, data); // Convert into value in case of a variable
  7967. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7968. g = guild_search(guild_id);
  7969. if( g == NULL )
  7970. script_pushint(st, -1);
  7971. else
  7972. script_pushint(st, guild_checkskill(g,skill_id));
  7973. return SCRIPT_CMD_SUCCESS;
  7974. }
  7975. /// Returns the 'basic_skill_check' setting.
  7976. /// This config determines if the server checks the skill level of NV_BASIC
  7977. /// before allowing the basic actions.
  7978. ///
  7979. /// basicskillcheck() -> <bool>
  7980. BUILDIN_FUNC(basicskillcheck)
  7981. {
  7982. script_pushint(st, battle_config.basic_skill_check);
  7983. return SCRIPT_CMD_SUCCESS;
  7984. }
  7985. /// Returns the GM level of the player.
  7986. ///
  7987. /// getgmlevel({<char_id>}) -> <level>
  7988. BUILDIN_FUNC(getgmlevel)
  7989. {
  7990. TBL_PC* sd;
  7991. if (!script_charid2sd(2,sd))
  7992. return SCRIPT_CMD_FAILURE;
  7993. script_pushint(st, pc_get_group_level(sd));
  7994. return SCRIPT_CMD_SUCCESS;
  7995. }
  7996. /// Returns the group ID of the player.
  7997. ///
  7998. /// getgroupid({<char_id>}) -> <int>
  7999. BUILDIN_FUNC(getgroupid)
  8000. {
  8001. TBL_PC* sd;
  8002. if (!script_charid2sd(2,sd))
  8003. return SCRIPT_CMD_FAILURE;
  8004. script_pushint(st, pc_get_group_id(sd));
  8005. return SCRIPT_CMD_SUCCESS;
  8006. }
  8007. /// Terminates the execution of this script instance.
  8008. ///
  8009. /// end
  8010. BUILDIN_FUNC(end)
  8011. {
  8012. TBL_PC* sd;
  8013. sd = map_id2sd(st->rid);
  8014. st->state = END;
  8015. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  8016. int i;
  8017. for(i = 0; i < st->stack->sp; i++) {
  8018. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  8019. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  8020. st->script = ri->script;
  8021. break;
  8022. }
  8023. }
  8024. }
  8025. if( st->mes_active )
  8026. st->mes_active = 0;
  8027. if (sd){
  8028. if (sd->state.callshop == 0)
  8029. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8030. else
  8031. sd->state.callshop = 0;
  8032. }
  8033. return SCRIPT_CMD_SUCCESS;
  8034. }
  8035. /// Checks if the player has that effect state (option).
  8036. ///
  8037. /// checkoption(<option>{,<char_id>}) -> <bool>
  8038. BUILDIN_FUNC(checkoption)
  8039. {
  8040. int option;
  8041. TBL_PC* sd;
  8042. if (!script_charid2sd(3,sd))
  8043. return SCRIPT_CMD_FAILURE;
  8044. option = script_getnum(st,2);
  8045. if( sd->sc.option&option )
  8046. script_pushint(st, 1);
  8047. else
  8048. script_pushint(st, 0);
  8049. return SCRIPT_CMD_SUCCESS;
  8050. }
  8051. /// Checks if the player is in that body state (opt1).
  8052. ///
  8053. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8054. BUILDIN_FUNC(checkoption1)
  8055. {
  8056. int opt1;
  8057. TBL_PC* sd;
  8058. if (!script_charid2sd(3,sd))
  8059. return SCRIPT_CMD_FAILURE;
  8060. opt1 = script_getnum(st,2);
  8061. if( sd->sc.opt1 == opt1 )
  8062. script_pushint(st, 1);
  8063. else
  8064. script_pushint(st, 0);
  8065. return SCRIPT_CMD_SUCCESS;
  8066. }
  8067. /// Checks if the player has that health state (opt2).
  8068. ///
  8069. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8070. BUILDIN_FUNC(checkoption2)
  8071. {
  8072. int opt2;
  8073. TBL_PC* sd;
  8074. if (!script_charid2sd(3,sd))
  8075. return SCRIPT_CMD_FAILURE;
  8076. opt2 = script_getnum(st,2);
  8077. if( sd->sc.opt2&opt2 )
  8078. script_pushint(st, 1);
  8079. else
  8080. script_pushint(st, 0);
  8081. return SCRIPT_CMD_SUCCESS;
  8082. }
  8083. /// Changes the effect state (option) of the player.
  8084. /// <flag> defaults to 1
  8085. /// <flag>=0 : removes the option
  8086. /// <flag>=other : adds the option
  8087. ///
  8088. /// setoption <option>{,<flag>{,<char_id>}};
  8089. BUILDIN_FUNC(setoption)
  8090. {
  8091. int option;
  8092. int flag = 1;
  8093. TBL_PC* sd;
  8094. if (!script_charid2sd(4,sd))
  8095. return SCRIPT_CMD_FAILURE;
  8096. option = script_getnum(st,2);
  8097. if( script_hasdata(st,3) )
  8098. flag = script_getnum(st,3);
  8099. else if( !option ){// Request to remove everything.
  8100. flag = 0;
  8101. option = OPTION_FALCON|OPTION_RIDING;
  8102. #ifndef NEW_CARTS
  8103. option |= OPTION_CART;
  8104. #endif
  8105. }
  8106. if( flag ){// Add option
  8107. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8108. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8109. pc_setoption(sd, sd->sc.option|option);
  8110. } else// Remove option
  8111. pc_setoption(sd, sd->sc.option&~option);
  8112. return SCRIPT_CMD_SUCCESS;
  8113. }
  8114. /// Returns if the player has a cart.
  8115. ///
  8116. /// checkcart({char_id}) -> <bool>
  8117. ///
  8118. /// @author Valaris
  8119. BUILDIN_FUNC(checkcart)
  8120. {
  8121. TBL_PC* sd;
  8122. if (!script_charid2sd(2,sd))
  8123. return SCRIPT_CMD_FAILURE;
  8124. if( pc_iscarton(sd) )
  8125. script_pushint(st, 1);
  8126. else
  8127. script_pushint(st, 0);
  8128. return SCRIPT_CMD_SUCCESS;
  8129. }
  8130. /// Sets the cart of the player.
  8131. /// <type> defaults to 1
  8132. /// <type>=0 : removes the cart
  8133. /// <type>=1 : Normal cart
  8134. /// <type>=2 : Wooden cart
  8135. /// <type>=3 : Covered cart with flowers and ferns
  8136. /// <type>=4 : Wooden cart with a Panda doll on the back
  8137. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8138. ///
  8139. /// setcart {<type>{,<char_id>}};
  8140. BUILDIN_FUNC(setcart)
  8141. {
  8142. int type = 1;
  8143. TBL_PC* sd;
  8144. if (!script_charid2sd(3,sd))
  8145. return SCRIPT_CMD_FAILURE;
  8146. if( script_hasdata(st,2) )
  8147. type = script_getnum(st,2);
  8148. pc_setcart(sd, type);
  8149. return SCRIPT_CMD_SUCCESS;
  8150. }
  8151. /// Returns if the player has a falcon.
  8152. ///
  8153. /// checkfalcon({char_id}) -> <bool>
  8154. ///
  8155. /// @author Valaris
  8156. BUILDIN_FUNC(checkfalcon)
  8157. {
  8158. TBL_PC* sd;
  8159. if (!script_charid2sd(2,sd))
  8160. return SCRIPT_CMD_FAILURE;
  8161. if( pc_isfalcon(sd) )
  8162. script_pushint(st, 1);
  8163. else
  8164. script_pushint(st, 0);
  8165. return SCRIPT_CMD_SUCCESS;
  8166. }
  8167. /// Sets if the player has a falcon or not.
  8168. /// <flag> defaults to 1
  8169. ///
  8170. /// setfalcon {<flag>{,<char_id>}};
  8171. BUILDIN_FUNC(setfalcon)
  8172. {
  8173. int flag = 1;
  8174. TBL_PC* sd;
  8175. if (!script_charid2sd(3,sd))
  8176. return SCRIPT_CMD_FAILURE;
  8177. if( script_hasdata(st,2) )
  8178. flag = script_getnum(st,2);
  8179. pc_setfalcon(sd, flag);
  8180. return SCRIPT_CMD_SUCCESS;
  8181. }
  8182. /// Returns if the player is riding.
  8183. ///
  8184. /// checkriding({char_id}) -> <bool>
  8185. ///
  8186. /// @author Valaris
  8187. BUILDIN_FUNC(checkriding)
  8188. {
  8189. TBL_PC* sd;
  8190. if (!script_charid2sd(2,sd))
  8191. return SCRIPT_CMD_FAILURE;
  8192. if( pc_isriding(sd) )
  8193. script_pushint(st, 1);
  8194. else
  8195. script_pushint(st, 0);
  8196. return SCRIPT_CMD_SUCCESS;
  8197. }
  8198. /// Sets if the player is riding.
  8199. /// <flag> defaults to 1
  8200. ///
  8201. /// setriding {<flag>{,<char_id>}};
  8202. BUILDIN_FUNC(setriding)
  8203. {
  8204. int flag = 1;
  8205. TBL_PC* sd;
  8206. if (!script_charid2sd(3,sd))
  8207. return SCRIPT_CMD_FAILURE;
  8208. if( script_hasdata(st,2) )
  8209. flag = script_getnum(st,2);
  8210. pc_setriding(sd, flag);
  8211. return SCRIPT_CMD_SUCCESS;
  8212. }
  8213. /// Returns if the player has a warg.
  8214. ///
  8215. /// checkwug({char_id}) -> <bool>
  8216. ///
  8217. BUILDIN_FUNC(checkwug)
  8218. {
  8219. TBL_PC* sd;
  8220. if (!script_charid2sd(2,sd))
  8221. return SCRIPT_CMD_FAILURE;
  8222. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8223. script_pushint(st, 1);
  8224. else
  8225. script_pushint(st, 0);
  8226. return SCRIPT_CMD_SUCCESS;
  8227. }
  8228. /// Returns if the player is wearing MADO Gear.
  8229. ///
  8230. /// checkmadogear({<char_id>}) -> <bool>
  8231. ///
  8232. BUILDIN_FUNC(checkmadogear)
  8233. {
  8234. TBL_PC* sd;
  8235. if (!script_charid2sd(2,sd))
  8236. return SCRIPT_CMD_FAILURE;
  8237. if( pc_ismadogear(sd) )
  8238. script_pushint(st, 1);
  8239. else
  8240. script_pushint(st, 0);
  8241. return SCRIPT_CMD_SUCCESS;
  8242. }
  8243. /// Sets if the player is riding MADO Gear.
  8244. /// <flag> defaults to 1
  8245. ///
  8246. /// setmadogear {<flag>{,<char_id>}};
  8247. BUILDIN_FUNC(setmadogear)
  8248. {
  8249. int flag = 1;
  8250. TBL_PC* sd;
  8251. if (!script_charid2sd(3,sd))
  8252. return SCRIPT_CMD_FAILURE;
  8253. if( script_hasdata(st,2) )
  8254. flag = script_getnum(st,2);
  8255. pc_setmadogear(sd, flag);
  8256. return SCRIPT_CMD_SUCCESS;
  8257. }
  8258. /// Sets the save point of the player.
  8259. ///
  8260. /// save "<map name>",<x>,<y>{,<char_id>}
  8261. /// savepoint "<map name>",<x>,<y>{,<char_id>}
  8262. BUILDIN_FUNC(savepoint)
  8263. {
  8264. int x;
  8265. int y;
  8266. short map_idx;
  8267. const char* str;
  8268. TBL_PC* sd;
  8269. if (!script_charid2sd(5,sd))
  8270. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8271. str = script_getstr(st, 2);
  8272. x = script_getnum(st,3);
  8273. y = script_getnum(st,4);
  8274. map_idx = mapindex_name2id(str);
  8275. if( map_idx )
  8276. pc_setsavepoint(sd, map_idx, x, y);
  8277. return SCRIPT_CMD_SUCCESS;
  8278. }
  8279. /*==========================================
  8280. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  8281. *------------------------------------------*/
  8282. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8283. {
  8284. int type;
  8285. time_t timer;
  8286. struct tm *t;
  8287. type=script_getnum(st,2);
  8288. switch(type){
  8289. case 2:
  8290. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8291. // from the system clock.)
  8292. script_pushint(st,(int)time(NULL));
  8293. break;
  8294. case 1:
  8295. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8296. time(&timer);
  8297. t=localtime(&timer);
  8298. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8299. break;
  8300. case 0:
  8301. default:
  8302. //type 0:(System Ticks)
  8303. script_pushint(st,gettick());
  8304. break;
  8305. }
  8306. return SCRIPT_CMD_SUCCESS;
  8307. }
  8308. /*==========================================
  8309. * GetTime(Type);
  8310. * 1: Sec 2: Min 3: Hour
  8311. * 4: WeekDay 5: MonthDay 6: Month
  8312. * 7: Year
  8313. *------------------------------------------*/
  8314. BUILDIN_FUNC(gettime) /* Asgard Version */
  8315. {
  8316. int type;
  8317. time_t timer;
  8318. struct tm *t;
  8319. type=script_getnum(st,2);
  8320. time(&timer);
  8321. t=localtime(&timer);
  8322. switch(type){
  8323. case 1://Sec(0~59)
  8324. script_pushint(st,t->tm_sec);
  8325. break;
  8326. case 2://Min(0~59)
  8327. script_pushint(st,t->tm_min);
  8328. break;
  8329. case 3://Hour(0~23)
  8330. script_pushint(st,t->tm_hour);
  8331. break;
  8332. case 4://WeekDay(0~6)
  8333. script_pushint(st,t->tm_wday);
  8334. break;
  8335. case 5://MonthDay(01~31)
  8336. script_pushint(st,t->tm_mday);
  8337. break;
  8338. case 6://Month(01~12)
  8339. script_pushint(st,t->tm_mon+1);
  8340. break;
  8341. case 7://Year(20xx)
  8342. script_pushint(st,t->tm_year+1900);
  8343. break;
  8344. case 8://Year Day(01~366)
  8345. script_pushint(st,t->tm_yday+1);
  8346. break;
  8347. default://(format error)
  8348. script_pushint(st,-1);
  8349. break;
  8350. }
  8351. return SCRIPT_CMD_SUCCESS;
  8352. }
  8353. /*==========================================
  8354. * GetTimeStr("TimeFMT", Length);
  8355. *------------------------------------------*/
  8356. BUILDIN_FUNC(gettimestr)
  8357. {
  8358. char *tmpstr;
  8359. const char *fmtstr;
  8360. int maxlen;
  8361. time_t now = time(NULL);
  8362. fmtstr=script_getstr(st,2);
  8363. maxlen=script_getnum(st,3);
  8364. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8365. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8366. tmpstr[maxlen]='\0';
  8367. script_pushstr(st,tmpstr);
  8368. return SCRIPT_CMD_SUCCESS;
  8369. }
  8370. /*==========================================
  8371. * Open player storage
  8372. *------------------------------------------*/
  8373. BUILDIN_FUNC(openstorage)
  8374. {
  8375. TBL_PC* sd;
  8376. sd = script_rid2sd(st);
  8377. if( sd == NULL )
  8378. return SCRIPT_CMD_SUCCESS;
  8379. storage_storageopen(sd);
  8380. return SCRIPT_CMD_SUCCESS;
  8381. }
  8382. BUILDIN_FUNC(guildopenstorage)
  8383. {
  8384. TBL_PC* sd;
  8385. int ret;
  8386. sd = script_rid2sd(st);
  8387. if( sd == NULL )
  8388. return SCRIPT_CMD_SUCCESS;
  8389. ret = storage_guild_storageopen(sd);
  8390. script_pushint(st,ret);
  8391. return SCRIPT_CMD_SUCCESS;
  8392. }
  8393. /*==========================================
  8394. * Make player use a skill trought item usage
  8395. *------------------------------------------*/
  8396. /// itemskill <skill id>,<level>
  8397. /// itemskill "<skill name>",<level>
  8398. BUILDIN_FUNC(itemskill)
  8399. {
  8400. int id;
  8401. int lv;
  8402. bool keep_requirement;
  8403. TBL_PC* sd;
  8404. struct script_data *data;
  8405. sd = script_rid2sd(st);
  8406. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8407. return SCRIPT_CMD_SUCCESS;
  8408. data = script_getdata(st, 2);
  8409. get_val(st, data); // Convert into value in case of a variable
  8410. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8411. lv = script_getnum(st,3);
  8412. if (script_hasdata(st, 4)) {
  8413. keep_requirement = (script_getnum(st, 4) != 0);
  8414. } else {
  8415. keep_requirement = false;
  8416. }
  8417. sd->skillitem=id;
  8418. sd->skillitemlv=lv;
  8419. sd->skillitem_keep_requirement = keep_requirement;
  8420. clif_item_skill(sd,id,lv);
  8421. return SCRIPT_CMD_SUCCESS;
  8422. }
  8423. /*==========================================
  8424. * Attempt to create an item
  8425. *------------------------------------------*/
  8426. BUILDIN_FUNC(produce)
  8427. {
  8428. int trigger;
  8429. TBL_PC* sd;
  8430. sd = script_rid2sd(st);
  8431. if( sd == NULL )
  8432. return SCRIPT_CMD_SUCCESS;
  8433. trigger=script_getnum(st,2);
  8434. clif_skill_produce_mix_list(sd, -1, trigger);
  8435. return SCRIPT_CMD_SUCCESS;
  8436. }
  8437. /*==========================================
  8438. *
  8439. *------------------------------------------*/
  8440. BUILDIN_FUNC(cooking)
  8441. {
  8442. int trigger;
  8443. TBL_PC* sd;
  8444. sd = script_rid2sd(st);
  8445. if( sd == NULL )
  8446. return SCRIPT_CMD_SUCCESS;
  8447. trigger=script_getnum(st,2);
  8448. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8449. return SCRIPT_CMD_SUCCESS;
  8450. }
  8451. /*==========================================
  8452. * Create a pet
  8453. *------------------------------------------*/
  8454. BUILDIN_FUNC(makepet)
  8455. {
  8456. TBL_PC* sd;
  8457. int id,pet_id;
  8458. id=script_getnum(st,2);
  8459. sd = script_rid2sd(st);
  8460. if( sd == NULL )
  8461. return SCRIPT_CMD_SUCCESS;
  8462. pet_id = search_petDB_index(id, PET_CLASS);
  8463. if (pet_id < 0)
  8464. pet_id = search_petDB_index(id, PET_EGG);
  8465. if (pet_id >= 0 && sd) {
  8466. sd->catch_target_class = pet_db[pet_id].class_;
  8467. intif_create_pet(
  8468. sd->status.account_id, sd->status.char_id,
  8469. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8470. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8471. 100, 0, 1, pet_db[pet_id].jname);
  8472. }
  8473. return SCRIPT_CMD_SUCCESS;
  8474. }
  8475. /**
  8476. * Give player exp base,job * quest_exp_rate/100
  8477. * getexp <base xp>,<job xp>{,<char_id>};
  8478. **/
  8479. BUILDIN_FUNC(getexp)
  8480. {
  8481. TBL_PC* sd;
  8482. int base=0,job=0;
  8483. double bonus;
  8484. if (!script_charid2sd(4,sd))
  8485. return SCRIPT_CMD_FAILURE;
  8486. base=script_getnum(st,2);
  8487. job =script_getnum(st,3);
  8488. if(base<0 || job<0)
  8489. return SCRIPT_CMD_SUCCESS;
  8490. // bonus for npc-given exp
  8491. bonus = battle_config.quest_exp_rate / 100.;
  8492. if (base)
  8493. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8494. if (job)
  8495. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8496. pc_gainexp(sd, NULL, base, job, 1);
  8497. return SCRIPT_CMD_SUCCESS;
  8498. }
  8499. /*==========================================
  8500. * Gain guild exp [Celest]
  8501. *------------------------------------------*/
  8502. BUILDIN_FUNC(guildgetexp)
  8503. {
  8504. TBL_PC* sd;
  8505. int exp;
  8506. sd = script_rid2sd(st);
  8507. if( sd == NULL )
  8508. return SCRIPT_CMD_SUCCESS;
  8509. exp = script_getnum(st,2);
  8510. if(exp < 0)
  8511. return SCRIPT_CMD_SUCCESS;
  8512. if(sd && sd->status.guild_id > 0)
  8513. guild_getexp (sd, exp);
  8514. return SCRIPT_CMD_SUCCESS;
  8515. }
  8516. /*==========================================
  8517. * Changes the guild master of a guild [Skotlex]
  8518. *------------------------------------------*/
  8519. BUILDIN_FUNC(guildchangegm)
  8520. {
  8521. TBL_PC *sd;
  8522. int guild_id;
  8523. const char *name;
  8524. guild_id = script_getnum(st,2);
  8525. name = script_getstr(st,3);
  8526. sd=map_nick2sd(name);
  8527. if (!sd)
  8528. script_pushint(st,0);
  8529. else
  8530. script_pushint(st,guild_gm_change(guild_id, sd));
  8531. return SCRIPT_CMD_SUCCESS;
  8532. }
  8533. /*==========================================
  8534. * Spawn a monster:
  8535. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8536. *------------------------------------------*/
  8537. BUILDIN_FUNC(monster)
  8538. {
  8539. const char* mapn = script_getstr(st,2);
  8540. int x = script_getnum(st,3);
  8541. int y = script_getnum(st,4);
  8542. const char* str = script_getstr(st,5);
  8543. int class_ = script_getnum(st,6);
  8544. int amount = script_getnum(st,7);
  8545. const char* event = "";
  8546. unsigned int size = SZ_SMALL;
  8547. unsigned int ai = AI_NONE;
  8548. struct map_session_data* sd;
  8549. int16 m;
  8550. int i;
  8551. if (script_hasdata(st, 8)) {
  8552. event = script_getstr(st, 8);
  8553. check_event(st, event);
  8554. }
  8555. if (script_hasdata(st, 9)) {
  8556. size = script_getnum(st, 9);
  8557. if (size > 3) {
  8558. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8559. return SCRIPT_CMD_FAILURE;
  8560. }
  8561. }
  8562. if (script_hasdata(st, 10)) {
  8563. ai = script_getnum(st, 10);
  8564. if (ai >= AI_MAX) {
  8565. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8566. return SCRIPT_CMD_FAILURE;
  8567. }
  8568. }
  8569. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8570. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8571. return SCRIPT_CMD_FAILURE;
  8572. }
  8573. sd = map_id2sd(st->rid);
  8574. if (sd && strcmp(mapn, "this") == 0)
  8575. m = sd->bl.m;
  8576. else
  8577. m = map_mapname2mapid(mapn);
  8578. for(i = 0; i < amount; i++) { //not optimised
  8579. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8580. if (mobid)
  8581. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8582. }
  8583. return SCRIPT_CMD_SUCCESS;
  8584. }
  8585. /*==========================================
  8586. * Request List of Monster Drops
  8587. *------------------------------------------*/
  8588. BUILDIN_FUNC(getmobdrops)
  8589. {
  8590. int class_ = script_getnum(st,2);
  8591. int i, j = 0;
  8592. struct mob_db *mob;
  8593. if( !mobdb_checkid(class_) )
  8594. {
  8595. script_pushint(st, 0);
  8596. return SCRIPT_CMD_SUCCESS;
  8597. }
  8598. mob = mob_db(class_);
  8599. for( i = 0; i < MAX_MOB_DROP; i++ )
  8600. {
  8601. if( mob->dropitem[i].nameid < 1 )
  8602. continue;
  8603. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8604. continue;
  8605. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8606. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8607. j++;
  8608. }
  8609. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8610. script_pushint(st, 1);
  8611. return SCRIPT_CMD_SUCCESS;
  8612. }
  8613. /*==========================================
  8614. * Spawn a monster in a random location
  8615. * in x0,x1,y0,y1 area.
  8616. *------------------------------------------*/
  8617. BUILDIN_FUNC(areamonster)
  8618. {
  8619. const char* mapn = script_getstr(st,2);
  8620. int x0 = script_getnum(st,3);
  8621. int y0 = script_getnum(st,4);
  8622. int x1 = script_getnum(st,5);
  8623. int y1 = script_getnum(st,6);
  8624. const char* str = script_getstr(st,7);
  8625. int class_ = script_getnum(st,8);
  8626. int amount = script_getnum(st,9);
  8627. const char* event = "";
  8628. unsigned int size = SZ_SMALL;
  8629. unsigned int ai = AI_NONE;
  8630. struct map_session_data* sd;
  8631. int16 m;
  8632. int i;
  8633. if (script_hasdata(st,10)) {
  8634. event = script_getstr(st, 10);
  8635. check_event(st, event);
  8636. }
  8637. if (script_hasdata(st, 11)) {
  8638. size = script_getnum(st, 11);
  8639. if (size > 3) {
  8640. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8641. return SCRIPT_CMD_FAILURE;
  8642. }
  8643. }
  8644. if (script_hasdata(st, 12)) {
  8645. ai = script_getnum(st, 12);
  8646. if (ai >= AI_MAX) {
  8647. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8648. return SCRIPT_CMD_FAILURE;
  8649. }
  8650. }
  8651. sd = map_id2sd(st->rid);
  8652. if (sd && strcmp(mapn, "this") == 0)
  8653. m = sd->bl.m;
  8654. else
  8655. m = map_mapname2mapid(mapn);
  8656. for(i = 0; i < amount; i++) { //not optimised
  8657. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8658. if (mobid)
  8659. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8660. }
  8661. return SCRIPT_CMD_SUCCESS;
  8662. }
  8663. /*==========================================
  8664. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8665. *------------------------------------------*/
  8666. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8667. { //same fix but with killmonster instead - stripping events from mobs.
  8668. TBL_MOB* md = (TBL_MOB*)bl;
  8669. char *event=va_arg(ap,char *);
  8670. int allflag=va_arg(ap,int);
  8671. md->state.npc_killmonster = 1;
  8672. if(!allflag){
  8673. if(strcmp(event,md->npc_event)==0)
  8674. status_kill(bl);
  8675. }else{
  8676. if(!md->spawn)
  8677. status_kill(bl);
  8678. }
  8679. md->state.npc_killmonster = 0;
  8680. return SCRIPT_CMD_SUCCESS;
  8681. }
  8682. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8683. {
  8684. TBL_MOB* md = (TBL_MOB*)bl;
  8685. char *event=va_arg(ap,char *);
  8686. int allflag=va_arg(ap,int);
  8687. if(!allflag){
  8688. if(strcmp(event,md->npc_event)==0)
  8689. status_kill(bl);
  8690. }else{
  8691. if(!md->spawn)
  8692. status_kill(bl);
  8693. }
  8694. return SCRIPT_CMD_SUCCESS;
  8695. }
  8696. BUILDIN_FUNC(killmonster)
  8697. {
  8698. const char *mapname,*event;
  8699. int16 m,allflag=0;
  8700. mapname=script_getstr(st,2);
  8701. event=script_getstr(st,3);
  8702. if(strcmp(event,"All")==0)
  8703. allflag = 1;
  8704. else
  8705. check_event(st, event);
  8706. if( (m=map_mapname2mapid(mapname))<0 )
  8707. return SCRIPT_CMD_SUCCESS;
  8708. if( script_hasdata(st,4) ) {
  8709. if ( script_getnum(st,4) == 1 ) {
  8710. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8711. return SCRIPT_CMD_SUCCESS;
  8712. }
  8713. }
  8714. map_freeblock_lock();
  8715. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8716. map_freeblock_unlock();
  8717. return SCRIPT_CMD_SUCCESS;
  8718. }
  8719. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8720. { //Strips the event from the mob if it's killed the old method.
  8721. struct mob_data *md;
  8722. md = BL_CAST(BL_MOB, bl);
  8723. if (md->npc_event[0])
  8724. md->npc_event[0] = 0;
  8725. status_kill(bl);
  8726. return 0;
  8727. }
  8728. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8729. {
  8730. status_kill(bl);
  8731. return 0;
  8732. }
  8733. BUILDIN_FUNC(killmonsterall)
  8734. {
  8735. const char *mapname;
  8736. int16 m;
  8737. mapname=script_getstr(st,2);
  8738. if( (m = map_mapname2mapid(mapname))<0 )
  8739. return SCRIPT_CMD_SUCCESS;
  8740. if( script_hasdata(st,3) ) {
  8741. if ( script_getnum(st,3) == 1 ) {
  8742. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8743. return SCRIPT_CMD_SUCCESS;
  8744. }
  8745. }
  8746. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8747. return SCRIPT_CMD_SUCCESS;
  8748. }
  8749. /*==========================================
  8750. * Creates a clone of a player.
  8751. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8752. *------------------------------------------*/
  8753. BUILDIN_FUNC(clone)
  8754. {
  8755. TBL_PC *sd, *msd=NULL;
  8756. uint32 char_id, master_id = 0, x, y, flag = 0, m;
  8757. enum e_mode mode = 0;
  8758. unsigned int duration = 0;
  8759. const char *mapname,*event;
  8760. mapname=script_getstr(st,2);
  8761. x=script_getnum(st,3);
  8762. y=script_getnum(st,4);
  8763. event=script_getstr(st,5);
  8764. char_id=script_getnum(st,6);
  8765. if( script_hasdata(st,7) )
  8766. master_id=script_getnum(st,7);
  8767. if( script_hasdata(st,8) )
  8768. mode=script_getnum(st,8);
  8769. if( script_hasdata(st,9) )
  8770. flag=script_getnum(st,9);
  8771. if( script_hasdata(st,10) )
  8772. duration=script_getnum(st,10);
  8773. check_event(st, event);
  8774. m = map_mapname2mapid(mapname);
  8775. if (m < 0)
  8776. return SCRIPT_CMD_SUCCESS;
  8777. sd = map_charid2sd(char_id);
  8778. if (master_id) {
  8779. msd = map_charid2sd(master_id);
  8780. if (msd)
  8781. master_id = msd->bl.id;
  8782. else
  8783. master_id = 0;
  8784. }
  8785. if (sd) //Return ID of newly crafted clone.
  8786. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8787. else //Failed to create clone.
  8788. script_pushint(st,0);
  8789. return SCRIPT_CMD_SUCCESS;
  8790. }
  8791. /*==========================================
  8792. *------------------------------------------*/
  8793. BUILDIN_FUNC(doevent)
  8794. {
  8795. const char* event = script_getstr(st,2);
  8796. struct map_session_data* sd;
  8797. if( ( sd = script_rid2sd(st) ) == NULL )
  8798. {
  8799. return SCRIPT_CMD_SUCCESS;
  8800. }
  8801. check_event(st, event);
  8802. npc_event(sd, event, 0);
  8803. return SCRIPT_CMD_SUCCESS;
  8804. }
  8805. /*==========================================
  8806. *------------------------------------------*/
  8807. BUILDIN_FUNC(donpcevent)
  8808. {
  8809. const char* event = script_getstr(st,2);
  8810. check_event(st, event);
  8811. if( !npc_event_do(event) ) {
  8812. struct npc_data * nd = map_id2nd(st->oid);
  8813. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8814. script_pushint(st, 0);
  8815. } else
  8816. script_pushint(st, 1);
  8817. return SCRIPT_CMD_SUCCESS;
  8818. }
  8819. /// for Aegis compatibility
  8820. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8821. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8822. {
  8823. const char* npc = script_getstr(st,2);
  8824. const char* command = script_getstr(st,3);
  8825. char event[EVENT_NAME_LENGTH];
  8826. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8827. check_event(st, event);
  8828. npc_event_do(event);
  8829. return SCRIPT_CMD_SUCCESS;
  8830. }
  8831. /*==========================================
  8832. *------------------------------------------*/
  8833. BUILDIN_FUNC(addtimer)
  8834. {
  8835. int tick = script_getnum(st,2);
  8836. const char* event = script_getstr(st, 3);
  8837. TBL_PC* sd;
  8838. check_event(st, event);
  8839. sd = script_rid2sd(st);
  8840. if( sd == NULL )
  8841. return SCRIPT_CMD_SUCCESS;
  8842. if (!pc_addeventtimer(sd,tick,event)) {
  8843. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8844. return SCRIPT_CMD_FAILURE;
  8845. }
  8846. return SCRIPT_CMD_SUCCESS;
  8847. }
  8848. /*==========================================
  8849. *------------------------------------------*/
  8850. BUILDIN_FUNC(deltimer)
  8851. {
  8852. const char *event;
  8853. TBL_PC* sd;
  8854. event=script_getstr(st, 2);
  8855. sd = script_rid2sd(st);
  8856. if( sd == NULL )
  8857. return SCRIPT_CMD_SUCCESS;
  8858. check_event(st, event);
  8859. pc_deleventtimer(sd,event);
  8860. return SCRIPT_CMD_SUCCESS;
  8861. }
  8862. /*==========================================
  8863. *------------------------------------------*/
  8864. BUILDIN_FUNC(addtimercount)
  8865. {
  8866. const char *event;
  8867. int tick;
  8868. TBL_PC* sd;
  8869. tick=script_getnum(st,2);
  8870. event=script_getstr(st,3);
  8871. sd = script_rid2sd(st);
  8872. if( sd == NULL )
  8873. return SCRIPT_CMD_SUCCESS;
  8874. check_event(st, event);
  8875. pc_addeventtimercount(sd,event,tick);
  8876. return SCRIPT_CMD_SUCCESS;
  8877. }
  8878. /*==========================================
  8879. *------------------------------------------*/
  8880. BUILDIN_FUNC(initnpctimer)
  8881. {
  8882. struct npc_data *nd;
  8883. int flag = 0;
  8884. if( script_hasdata(st,3) )
  8885. { //Two arguments: NPC name and attach flag.
  8886. nd = npc_name2id(script_getstr(st, 2));
  8887. flag = script_getnum(st,3);
  8888. }
  8889. else if( script_hasdata(st,2) )
  8890. { //Check if argument is numeric (flag) or string (npc name)
  8891. struct script_data *data;
  8892. data = script_getdata(st,2);
  8893. get_val(st,data);
  8894. if( data_isstring(data) ) //NPC name
  8895. nd = npc_name2id(conv_str(st, data));
  8896. else if( data_isint(data) ) //Flag
  8897. {
  8898. nd = (struct npc_data *)map_id2bl(st->oid);
  8899. flag = conv_num(st,data);
  8900. }
  8901. else
  8902. {
  8903. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8904. return SCRIPT_CMD_FAILURE;
  8905. }
  8906. }
  8907. else
  8908. nd = (struct npc_data *)map_id2bl(st->oid);
  8909. if( !nd )
  8910. return SCRIPT_CMD_SUCCESS;
  8911. if( flag ) //Attach
  8912. {
  8913. TBL_PC* sd = script_rid2sd(st);
  8914. if( sd == NULL )
  8915. return SCRIPT_CMD_SUCCESS;
  8916. nd->u.scr.rid = sd->bl.id;
  8917. }
  8918. nd->u.scr.timertick = 0;
  8919. npc_settimerevent_tick(nd,0);
  8920. npc_timerevent_start(nd, st->rid);
  8921. return SCRIPT_CMD_SUCCESS;
  8922. }
  8923. /*==========================================
  8924. *------------------------------------------*/
  8925. BUILDIN_FUNC(startnpctimer)
  8926. {
  8927. struct npc_data *nd;
  8928. int flag = 0;
  8929. if( script_hasdata(st,3) )
  8930. { //Two arguments: NPC name and attach flag.
  8931. nd = npc_name2id(script_getstr(st, 2));
  8932. flag = script_getnum(st,3);
  8933. }
  8934. else if( script_hasdata(st,2) )
  8935. { //Check if argument is numeric (flag) or string (npc name)
  8936. struct script_data *data;
  8937. data = script_getdata(st,2);
  8938. get_val(st,data);
  8939. if( data_isstring(data) ) //NPC name
  8940. nd = npc_name2id(conv_str(st, data));
  8941. else if( data_isint(data) ) //Flag
  8942. {
  8943. nd = (struct npc_data *)map_id2bl(st->oid);
  8944. flag = conv_num(st,data);
  8945. }
  8946. else
  8947. {
  8948. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8949. return SCRIPT_CMD_FAILURE;
  8950. }
  8951. }
  8952. else
  8953. nd=(struct npc_data *)map_id2bl(st->oid);
  8954. if( !nd )
  8955. return SCRIPT_CMD_SUCCESS;
  8956. if( flag ) //Attach
  8957. {
  8958. TBL_PC* sd = script_rid2sd(st);
  8959. if( sd == NULL )
  8960. return SCRIPT_CMD_SUCCESS;
  8961. nd->u.scr.rid = sd->bl.id;
  8962. }
  8963. npc_timerevent_start(nd, st->rid);
  8964. return SCRIPT_CMD_SUCCESS;
  8965. }
  8966. /*==========================================
  8967. *------------------------------------------*/
  8968. BUILDIN_FUNC(stopnpctimer)
  8969. {
  8970. struct npc_data *nd;
  8971. int flag = 0;
  8972. if( script_hasdata(st,3) )
  8973. { //Two arguments: NPC name and attach flag.
  8974. nd = npc_name2id(script_getstr(st, 2));
  8975. flag = script_getnum(st,3);
  8976. }
  8977. else if( script_hasdata(st,2) )
  8978. { //Check if argument is numeric (flag) or string (npc name)
  8979. struct script_data *data;
  8980. data = script_getdata(st,2);
  8981. get_val(st,data);
  8982. if( data_isstring(data) ) //NPC name
  8983. nd = npc_name2id(conv_str(st, data));
  8984. else if( data_isint(data) ) //Flag
  8985. {
  8986. nd = (struct npc_data *)map_id2bl(st->oid);
  8987. flag = conv_num(st,data);
  8988. }
  8989. else
  8990. {
  8991. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8992. return SCRIPT_CMD_FAILURE;
  8993. }
  8994. }
  8995. else
  8996. nd=(struct npc_data *)map_id2bl(st->oid);
  8997. if( !nd )
  8998. return SCRIPT_CMD_SUCCESS;
  8999. if( flag ) //Detach
  9000. nd->u.scr.rid = 0;
  9001. npc_timerevent_stop(nd);
  9002. return SCRIPT_CMD_SUCCESS;
  9003. }
  9004. /*==========================================
  9005. *------------------------------------------*/
  9006. BUILDIN_FUNC(getnpctimer)
  9007. {
  9008. struct npc_data *nd;
  9009. TBL_PC *sd;
  9010. int type = script_getnum(st,2);
  9011. int val = 0;
  9012. if( script_hasdata(st,3) )
  9013. nd = npc_name2id(script_getstr(st,3));
  9014. else
  9015. nd = (struct npc_data *)map_id2bl(st->oid);
  9016. if( !nd || nd->bl.type != BL_NPC )
  9017. {
  9018. script_pushint(st,0);
  9019. ShowError("getnpctimer: Invalid NPC.\n");
  9020. return SCRIPT_CMD_FAILURE;
  9021. }
  9022. switch( type )
  9023. {
  9024. case 0: val = npc_gettimerevent_tick(nd); break;
  9025. case 1:
  9026. if( nd->u.scr.rid )
  9027. {
  9028. sd = map_id2sd(nd->u.scr.rid);
  9029. if( !sd )
  9030. {
  9031. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9032. break;
  9033. }
  9034. val = (sd->npc_timer_id != INVALID_TIMER);
  9035. }
  9036. else
  9037. val = (nd->u.scr.timerid != INVALID_TIMER);
  9038. break;
  9039. case 2: val = nd->u.scr.timeramount; break;
  9040. }
  9041. script_pushint(st,val);
  9042. return SCRIPT_CMD_SUCCESS;
  9043. }
  9044. /*==========================================
  9045. *------------------------------------------*/
  9046. BUILDIN_FUNC(setnpctimer)
  9047. {
  9048. int tick;
  9049. struct npc_data *nd;
  9050. tick = script_getnum(st,2);
  9051. if( script_hasdata(st,3) )
  9052. nd = npc_name2id(script_getstr(st,3));
  9053. else
  9054. nd = (struct npc_data *)map_id2bl(st->oid);
  9055. if( !nd || nd->bl.type != BL_NPC )
  9056. {
  9057. script_pushint(st,1);
  9058. ShowError("setnpctimer: Invalid NPC.\n");
  9059. return SCRIPT_CMD_FAILURE;
  9060. }
  9061. npc_settimerevent_tick(nd,tick);
  9062. script_pushint(st,0);
  9063. return SCRIPT_CMD_SUCCESS;
  9064. }
  9065. /*==========================================
  9066. * attaches the player rid to the timer [Celest]
  9067. *------------------------------------------*/
  9068. BUILDIN_FUNC(attachnpctimer)
  9069. {
  9070. TBL_PC *sd;
  9071. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9072. if( !nd || nd->bl.type != BL_NPC )
  9073. {
  9074. script_pushint(st,1);
  9075. ShowError("setnpctimer: Invalid NPC.\n");
  9076. return SCRIPT_CMD_FAILURE;
  9077. }
  9078. if( script_hasdata(st,2) )
  9079. sd = map_nick2sd(script_getstr(st,2));
  9080. else
  9081. sd = script_rid2sd(st);
  9082. if( !sd )
  9083. {
  9084. script_pushint(st,1);
  9085. ShowWarning("attachnpctimer: Invalid player.\n");
  9086. return SCRIPT_CMD_FAILURE;
  9087. }
  9088. nd->u.scr.rid = sd->bl.id;
  9089. script_pushint(st,0);
  9090. return SCRIPT_CMD_SUCCESS;
  9091. }
  9092. /*==========================================
  9093. * detaches a player rid from the timer [Celest]
  9094. *------------------------------------------*/
  9095. BUILDIN_FUNC(detachnpctimer)
  9096. {
  9097. struct npc_data *nd;
  9098. if( script_hasdata(st,2) )
  9099. nd = npc_name2id(script_getstr(st,2));
  9100. else
  9101. nd = (struct npc_data *)map_id2bl(st->oid);
  9102. if( !nd || nd->bl.type != BL_NPC )
  9103. {
  9104. script_pushint(st,1);
  9105. ShowError("detachnpctimer: Invalid NPC.\n");
  9106. return SCRIPT_CMD_FAILURE;
  9107. }
  9108. nd->u.scr.rid = 0;
  9109. script_pushint(st,0);
  9110. return SCRIPT_CMD_SUCCESS;
  9111. }
  9112. /*==========================================
  9113. * To avoid "player not attached" script errors, this function is provided,
  9114. * it checks if there is a player attached to the current script. [Skotlex]
  9115. * If no, returns 0, if yes, returns the account_id of the attached player.
  9116. *------------------------------------------*/
  9117. BUILDIN_FUNC(playerattached)
  9118. {
  9119. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9120. script_pushint(st,0);
  9121. else
  9122. script_pushint(st,st->rid);
  9123. return SCRIPT_CMD_SUCCESS;
  9124. }
  9125. /*==========================================
  9126. *------------------------------------------*/
  9127. BUILDIN_FUNC(announce)
  9128. {
  9129. const char *mes = script_getstr(st,2);
  9130. int flag = script_getnum(st,3);
  9131. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9132. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9133. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9134. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9135. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9136. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9137. {
  9138. send_target target;
  9139. struct block_list *bl = (flag&BC_NPC) ? map_id2bl(st->oid) : (struct block_list *)script_rid2sd(st); // If bc_npc flag is set, use NPC as broadcast source
  9140. if (bl == NULL)
  9141. return SCRIPT_CMD_SUCCESS;
  9142. switch (flag&BC_TARGET_MASK) {
  9143. case BC_MAP: target = ALL_SAMEMAP; break;
  9144. case BC_AREA: target = AREA; break;
  9145. case BC_SELF: target = SELF; break;
  9146. default: target = ALL_CLIENT; break; // BC_ALL
  9147. }
  9148. if (fontColor)
  9149. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9150. else
  9151. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9152. }
  9153. else
  9154. {
  9155. if (fontColor)
  9156. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9157. else
  9158. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9159. }
  9160. return SCRIPT_CMD_SUCCESS;
  9161. }
  9162. /*==========================================
  9163. *------------------------------------------*/
  9164. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9165. {
  9166. char *mes = va_arg(ap, char *);
  9167. int len = va_arg(ap, int);
  9168. int type = va_arg(ap, int);
  9169. char *fontColor = va_arg(ap, char *);
  9170. short fontType = (short)va_arg(ap, int);
  9171. short fontSize = (short)va_arg(ap, int);
  9172. short fontAlign = (short)va_arg(ap, int);
  9173. short fontY = (short)va_arg(ap, int);
  9174. if (fontColor)
  9175. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9176. else
  9177. clif_broadcast(bl, mes, len, type, SELF);
  9178. return SCRIPT_CMD_SUCCESS;
  9179. }
  9180. BUILDIN_FUNC(mapannounce)
  9181. {
  9182. const char *mapname = script_getstr(st,2);
  9183. const char *mes = script_getstr(st,3);
  9184. int flag = script_getnum(st,4);
  9185. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9186. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9187. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9188. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9189. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9190. int16 m;
  9191. if ((m = map_mapname2mapid(mapname)) < 0)
  9192. return SCRIPT_CMD_SUCCESS;
  9193. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9194. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9195. return SCRIPT_CMD_SUCCESS;
  9196. }
  9197. /*==========================================
  9198. *------------------------------------------*/
  9199. BUILDIN_FUNC(areaannounce)
  9200. {
  9201. const char *mapname = script_getstr(st,2);
  9202. int x0 = script_getnum(st,3);
  9203. int y0 = script_getnum(st,4);
  9204. int x1 = script_getnum(st,5);
  9205. int y1 = script_getnum(st,6);
  9206. const char *mes = script_getstr(st,7);
  9207. int flag = script_getnum(st,8);
  9208. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9209. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9210. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9211. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9212. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9213. int16 m;
  9214. if ((m = map_mapname2mapid(mapname)) < 0)
  9215. return SCRIPT_CMD_SUCCESS;
  9216. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9217. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9218. return SCRIPT_CMD_SUCCESS;
  9219. }
  9220. /*==========================================
  9221. *------------------------------------------*/
  9222. BUILDIN_FUNC(getusers)
  9223. {
  9224. int flag, val = 0;
  9225. struct map_session_data* sd;
  9226. struct block_list* bl = NULL;
  9227. flag = script_getnum(st,2);
  9228. switch(flag&0x07)
  9229. {
  9230. case 0:
  9231. if(flag&0x8)
  9232. {// npc
  9233. bl = map_id2bl(st->oid);
  9234. }
  9235. else if((sd = script_rid2sd(st))!=NULL)
  9236. {// pc
  9237. bl = &sd->bl;
  9238. }
  9239. if(bl)
  9240. {
  9241. val = map[bl->m].users;
  9242. }
  9243. break;
  9244. case 1:
  9245. val = map_getusers();
  9246. break;
  9247. default:
  9248. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9249. script_pushint(st,0);
  9250. return SCRIPT_CMD_FAILURE;
  9251. }
  9252. script_pushint(st,val);
  9253. return SCRIPT_CMD_SUCCESS;
  9254. }
  9255. /*==========================================
  9256. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9257. *------------------------------------------*/
  9258. BUILDIN_FUNC(getmapguildusers)
  9259. {
  9260. const char *str;
  9261. int16 m;
  9262. int gid;
  9263. int c=0;
  9264. struct guild *g = NULL;
  9265. str=script_getstr(st,2);
  9266. gid=script_getnum(st,3);
  9267. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9268. script_pushint(st,-1);
  9269. return SCRIPT_CMD_SUCCESS;
  9270. }
  9271. g = guild_search(gid);
  9272. if (g){
  9273. unsigned short i;
  9274. for(i = 0; i < g->max_member; i++)
  9275. {
  9276. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9277. c++;
  9278. }
  9279. }
  9280. script_pushint(st,c);
  9281. return SCRIPT_CMD_SUCCESS;
  9282. }
  9283. /*==========================================
  9284. *------------------------------------------*/
  9285. BUILDIN_FUNC(getmapusers)
  9286. {
  9287. const char *str;
  9288. int16 m;
  9289. str=script_getstr(st,2);
  9290. if( (m=map_mapname2mapid(str))< 0){
  9291. script_pushint(st,-1);
  9292. return SCRIPT_CMD_SUCCESS;
  9293. }
  9294. script_pushint(st,map[m].users);
  9295. return SCRIPT_CMD_SUCCESS;
  9296. }
  9297. /*==========================================
  9298. *------------------------------------------*/
  9299. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9300. {
  9301. int *users=va_arg(ap,int *);
  9302. (*users)++;
  9303. return SCRIPT_CMD_SUCCESS;
  9304. }
  9305. BUILDIN_FUNC(getareausers)
  9306. {
  9307. const char *str;
  9308. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9309. str=script_getstr(st,2);
  9310. x0=script_getnum(st,3);
  9311. y0=script_getnum(st,4);
  9312. x1=script_getnum(st,5);
  9313. y1=script_getnum(st,6);
  9314. if( (m=map_mapname2mapid(str))< 0){
  9315. script_pushint(st,-1);
  9316. return SCRIPT_CMD_SUCCESS;
  9317. }
  9318. map_foreachinarea(buildin_getareausers_sub,
  9319. m,x0,y0,x1,y1,BL_PC,&users);
  9320. script_pushint(st,users);
  9321. return SCRIPT_CMD_SUCCESS;
  9322. }
  9323. /*==========================================
  9324. *------------------------------------------*/
  9325. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9326. {
  9327. unsigned short nameid = (unsigned short)va_arg(ap, int);
  9328. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  9329. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9330. if(drop->item.nameid==nameid)
  9331. (*amount)+=drop->item.amount;
  9332. return SCRIPT_CMD_SUCCESS;
  9333. }
  9334. BUILDIN_FUNC(getareadropitem)
  9335. {
  9336. const char *str;
  9337. int16 m,x0,y0,x1,y1;
  9338. unsigned short nameid, amount = 0;
  9339. struct script_data *data;
  9340. str=script_getstr(st,2);
  9341. x0=script_getnum(st,3);
  9342. y0=script_getnum(st,4);
  9343. x1=script_getnum(st,5);
  9344. y1=script_getnum(st,6);
  9345. data=script_getdata(st,7);
  9346. get_val(st,data);
  9347. if( data_isstring(data) ){
  9348. const char *name=conv_str(st,data);
  9349. struct item_data *item_data = itemdb_searchname(name);
  9350. nameid=UNKNOWN_ITEM_ID;
  9351. if( item_data )
  9352. nameid=item_data->nameid;
  9353. }else
  9354. nameid=conv_num(st,data);
  9355. if( (m=map_mapname2mapid(str))< 0){
  9356. script_pushint(st,-1);
  9357. return SCRIPT_CMD_SUCCESS;
  9358. }
  9359. map_foreachinarea(buildin_getareadropitem_sub,
  9360. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9361. script_pushint(st,amount);
  9362. return SCRIPT_CMD_SUCCESS;
  9363. }
  9364. /*==========================================
  9365. *------------------------------------------*/
  9366. BUILDIN_FUNC(enablenpc)
  9367. {
  9368. const char *str;
  9369. str=script_getstr(st,2);
  9370. npc_enable(str,1);
  9371. return SCRIPT_CMD_SUCCESS;
  9372. }
  9373. /*==========================================
  9374. *------------------------------------------*/
  9375. BUILDIN_FUNC(disablenpc)
  9376. {
  9377. const char *str;
  9378. str=script_getstr(st,2);
  9379. npc_enable(str,0);
  9380. return SCRIPT_CMD_SUCCESS;
  9381. }
  9382. /*==========================================
  9383. *------------------------------------------*/
  9384. BUILDIN_FUNC(hideoffnpc)
  9385. {
  9386. const char *str;
  9387. str=script_getstr(st,2);
  9388. npc_enable(str,2);
  9389. return SCRIPT_CMD_SUCCESS;
  9390. }
  9391. /*==========================================
  9392. *------------------------------------------*/
  9393. BUILDIN_FUNC(hideonnpc)
  9394. {
  9395. const char *str;
  9396. str=script_getstr(st,2);
  9397. npc_enable(str,4);
  9398. return SCRIPT_CMD_SUCCESS;
  9399. }
  9400. /* Starts a status effect on the target unit or on the attached player.
  9401. *
  9402. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9403. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9404. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9405. * <flag>: enum e_status_change_start_flags
  9406. */
  9407. BUILDIN_FUNC(sc_start)
  9408. {
  9409. TBL_NPC * nd = map_id2nd(st->oid);
  9410. struct block_list* bl;
  9411. enum sc_type type;
  9412. int tick, val1, val2, val3, val4=0, rate, flag;
  9413. char start_type;
  9414. const char* command = script_getfuncname(st);
  9415. if(strstr(command, "4"))
  9416. start_type = 4;
  9417. else if(strstr(command, "2"))
  9418. start_type = 2;
  9419. else
  9420. start_type = 1;
  9421. type = (sc_type)script_getnum(st,2);
  9422. tick = script_getnum(st,3);
  9423. val1 = script_getnum(st,4);
  9424. //If from NPC we make default flag 1 to be unavoidable
  9425. if(nd && nd->bl.id == fake_nd->bl.id)
  9426. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9427. else
  9428. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9429. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9430. if(script_hasdata(st,(6+start_type)))
  9431. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9432. else
  9433. bl = map_id2bl(st->rid);
  9434. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9435. {// When there isn't a duration specified, try to get it from the skill_db
  9436. tick = skill_get_time(status_sc2skill(type), val1);
  9437. }
  9438. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9439. bl = map_id2bl(potion_target);
  9440. tick /= 2;// Thrown potions only last half.
  9441. val4 = 1;// Mark that this was a thrown sc_effect
  9442. }
  9443. if(!bl)
  9444. return SCRIPT_CMD_SUCCESS;
  9445. switch(start_type) {
  9446. case 1:
  9447. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9448. break;
  9449. case 2:
  9450. val2 = script_getnum(st,5);
  9451. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9452. break;
  9453. case 4:
  9454. val2 = script_getnum(st,5);
  9455. val3 = script_getnum(st,6);
  9456. val4 = script_getnum(st,7);
  9457. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9458. break;
  9459. }
  9460. return SCRIPT_CMD_SUCCESS;
  9461. }
  9462. /// Ends one or all status effects on the target unit or on the attached player.
  9463. ///
  9464. /// sc_end <effect_id>{,<unit_id>};
  9465. BUILDIN_FUNC(sc_end)
  9466. {
  9467. struct block_list* bl;
  9468. int type;
  9469. type = script_getnum(st, 2);
  9470. if (script_hasdata(st, 3))
  9471. bl = map_id2bl(script_getnum(st, 3));
  9472. else
  9473. bl = map_id2bl(st->rid);
  9474. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9475. bl = map_id2bl(potion_target);
  9476. if (!bl)
  9477. return SCRIPT_CMD_SUCCESS;
  9478. if (type >= 0 && type < SC_MAX) {
  9479. struct status_change *sc = status_get_sc(bl);
  9480. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9481. if (!sce)
  9482. return SCRIPT_CMD_SUCCESS;
  9483. switch (type) {
  9484. case SC_WEIGHT50:
  9485. case SC_WEIGHT90:
  9486. case SC_NOCHAT:
  9487. case SC_PUSH_CART:
  9488. case SC_ALL_RIDING:
  9489. case SC_STYLE_CHANGE:
  9490. case SC_MONSTER_TRANSFORM:
  9491. case SC_ACTIVE_MONSTER_TRANSFORM:
  9492. case SC_MTF_ASPD:
  9493. case SC_MTF_RANGEATK:
  9494. case SC_MTF_MATK:
  9495. case SC_MTF_MLEATKED:
  9496. case SC_MTF_CRIDAMAGE:
  9497. case SC_MTF_ASPD2:
  9498. case SC_MTF_RANGEATK2:
  9499. case SC_MTF_MATK2:
  9500. case SC_MTF_MHP:
  9501. case SC_MTF_MSP:
  9502. case SC_MTF_PUMPKIN:
  9503. case SC_MTF_HITFLEE:
  9504. case SC_ATTHASTE_CASH:
  9505. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  9506. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  9507. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  9508. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  9509. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  9510. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  9511. return SCRIPT_CMD_SUCCESS;
  9512. default:
  9513. break;
  9514. }
  9515. //This should help status_change_end force disabling the SC in case it has no limit.
  9516. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9517. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9518. } else
  9519. status_change_clear(bl, 3); // remove all effects
  9520. return SCRIPT_CMD_SUCCESS;
  9521. }
  9522. /*==========================================
  9523. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9524. *------------------------------------------*/
  9525. BUILDIN_FUNC(getscrate)
  9526. {
  9527. struct block_list *bl;
  9528. int type,rate;
  9529. type=script_getnum(st,2);
  9530. rate=script_getnum(st,3);
  9531. if( script_hasdata(st,4) ) //get for the bl assigned
  9532. bl = map_id2bl(script_getnum(st,4));
  9533. else
  9534. bl = map_id2bl(st->rid);
  9535. if (bl)
  9536. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9537. script_pushint(st,rate);
  9538. return SCRIPT_CMD_SUCCESS;
  9539. }
  9540. /**
  9541. * getstatus(<effect type>{,<type>{,<char_id>}});
  9542. **/
  9543. BUILDIN_FUNC(getstatus)
  9544. {
  9545. int id, type;
  9546. struct map_session_data* sd;
  9547. if (!script_charid2sd(4,sd))
  9548. return SCRIPT_CMD_FAILURE;
  9549. id = script_getnum(st, 2);
  9550. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9551. if( id <= SC_NONE || id >= SC_MAX )
  9552. {// invalid status type given
  9553. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9554. return SCRIPT_CMD_SUCCESS;
  9555. }
  9556. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9557. {// no status is active
  9558. script_pushint(st, 0);
  9559. return SCRIPT_CMD_SUCCESS;
  9560. }
  9561. switch( type )
  9562. {
  9563. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9564. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9565. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9566. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9567. case 5:
  9568. {
  9569. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9570. if( timer )
  9571. {// return the amount of time remaining
  9572. script_pushint(st, timer->tick - gettick());
  9573. } else {
  9574. script_pushint(st, -1);
  9575. }
  9576. }
  9577. break;
  9578. default: script_pushint(st, 1); break;
  9579. }
  9580. return SCRIPT_CMD_SUCCESS;
  9581. }
  9582. /*==========================================
  9583. *
  9584. *------------------------------------------*/
  9585. BUILDIN_FUNC(debugmes)
  9586. {
  9587. const char *str;
  9588. str=script_getstr(st,2);
  9589. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9590. return SCRIPT_CMD_SUCCESS;
  9591. }
  9592. /*==========================================
  9593. *------------------------------------------*/
  9594. BUILDIN_FUNC(catchpet)
  9595. {
  9596. int pet_id;
  9597. TBL_PC *sd;
  9598. pet_id= script_getnum(st,2);
  9599. sd=script_rid2sd(st);
  9600. if( sd == NULL )
  9601. return SCRIPT_CMD_SUCCESS;
  9602. pet_catch_process1(sd,pet_id);
  9603. return SCRIPT_CMD_SUCCESS;
  9604. }
  9605. /*==========================================
  9606. * [orn]
  9607. *------------------------------------------*/
  9608. BUILDIN_FUNC(homunculus_evolution)
  9609. {
  9610. TBL_PC *sd;
  9611. sd=script_rid2sd(st);
  9612. if( sd == NULL )
  9613. return SCRIPT_CMD_SUCCESS;
  9614. if(hom_is_active(sd->hd))
  9615. {
  9616. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9617. hom_evolution(sd->hd);
  9618. else
  9619. clif_emotion(&sd->hd->bl, E_SWT);
  9620. }
  9621. return SCRIPT_CMD_SUCCESS;
  9622. }
  9623. /*==========================================
  9624. * Checks for vaporized morph state
  9625. * and deletes ITEMID_STRANGE_EMBRYO.
  9626. *------------------------------------------*/
  9627. BUILDIN_FUNC(homunculus_mutate)
  9628. {
  9629. int homun_id;
  9630. TBL_PC *sd;
  9631. sd = script_rid2sd(st);
  9632. if( sd == NULL || sd->hd == NULL )
  9633. return SCRIPT_CMD_SUCCESS;
  9634. if(script_hasdata(st,2))
  9635. homun_id = script_getnum(st,2);
  9636. else
  9637. homun_id = 6048 + (rnd() % 4);
  9638. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9639. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9640. int m_id = hom_class2mapid(homun_id);
  9641. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9642. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9643. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9644. hom_call(sd); // Respawn homunculus.
  9645. hom_mutate(sd->hd, homun_id);
  9646. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9647. script_pushint(st, 1);
  9648. return SCRIPT_CMD_SUCCESS;
  9649. } else
  9650. clif_emotion(&sd->bl, E_SWT);
  9651. } else
  9652. clif_emotion(&sd->bl, E_SWT);
  9653. script_pushint(st, 0);
  9654. return SCRIPT_CMD_SUCCESS;
  9655. }
  9656. /*==========================================
  9657. * Puts homunculus into morph state
  9658. * and gives ITEMID_STRANGE_EMBRYO.
  9659. *------------------------------------------*/
  9660. BUILDIN_FUNC(morphembryo)
  9661. {
  9662. struct item item_tmp;
  9663. TBL_PC *sd;
  9664. sd = script_rid2sd(st);
  9665. if( sd == NULL || sd->hd == NULL )
  9666. return SCRIPT_CMD_SUCCESS;
  9667. if( hom_is_active(sd->hd) ) {
  9668. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9669. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9670. char i;
  9671. memset(&item_tmp, 0, sizeof(item_tmp));
  9672. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9673. item_tmp.identify = 1;
  9674. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9675. clif_additem(sd, 0, 0, i);
  9676. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9677. } else {
  9678. hom_vaporize(sd, HOM_ST_MORPH);
  9679. script_pushint(st, 1);
  9680. return SCRIPT_CMD_SUCCESS;
  9681. }
  9682. } else
  9683. clif_emotion(&sd->hd->bl, E_SWT);
  9684. } else
  9685. clif_emotion(&sd->bl, E_SWT);
  9686. script_pushint(st, 0);
  9687. return SCRIPT_CMD_SUCCESS;
  9688. }
  9689. // [Zephyrus]
  9690. BUILDIN_FUNC(homunculus_shuffle)
  9691. {
  9692. TBL_PC *sd;
  9693. sd=script_rid2sd(st);
  9694. if( sd == NULL )
  9695. return SCRIPT_CMD_SUCCESS;
  9696. if(hom_is_active(sd->hd))
  9697. hom_shuffle(sd->hd);
  9698. return SCRIPT_CMD_SUCCESS;
  9699. }
  9700. /*==========================================
  9701. * Check for homunculus state.
  9702. * Return: -1 = No homunculus
  9703. * 0 = Homunculus is active
  9704. * 1 = Homunculus is vaporized (rest)
  9705. * 2 = Homunculus is in morph state
  9706. *------------------------------------------*/
  9707. BUILDIN_FUNC(checkhomcall)
  9708. {
  9709. TBL_PC *sd = script_rid2sd(st);
  9710. TBL_HOM *hd;
  9711. if( sd == NULL )
  9712. return SCRIPT_CMD_SUCCESS;
  9713. hd = sd->hd;
  9714. if( !hd )
  9715. script_pushint(st, -1);
  9716. else
  9717. script_pushint(st, hd->homunculus.vaporize);
  9718. return SCRIPT_CMD_SUCCESS;
  9719. }
  9720. //These two functions bring the eA MAPID_* class functionality to scripts.
  9721. BUILDIN_FUNC(eaclass)
  9722. {
  9723. int class_;
  9724. if( script_hasdata(st,2) )
  9725. class_ = script_getnum(st,2);
  9726. else {
  9727. TBL_PC *sd;
  9728. if (!script_charid2sd(3,sd)) {
  9729. script_pushint(st,-1);
  9730. return SCRIPT_CMD_SUCCESS;
  9731. }
  9732. class_ = sd->status.class_;
  9733. }
  9734. script_pushint(st,pc_jobid2mapid(class_));
  9735. return SCRIPT_CMD_SUCCESS;
  9736. }
  9737. BUILDIN_FUNC(roclass)
  9738. {
  9739. int class_ =script_getnum(st,2);
  9740. int sex;
  9741. if( script_hasdata(st,3) )
  9742. sex = script_getnum(st,3);
  9743. else {
  9744. TBL_PC *sd;
  9745. if (st->rid && (sd=script_rid2sd(st)))
  9746. sex = sd->status.sex;
  9747. else
  9748. sex = 1; //Just use male when not found.
  9749. }
  9750. script_pushint(st,pc_mapid2jobid(class_, sex));
  9751. return SCRIPT_CMD_SUCCESS;
  9752. }
  9753. /*==========================================
  9754. * Tells client to open a hatching window, used for pet incubator
  9755. *------------------------------------------*/
  9756. BUILDIN_FUNC(birthpet)
  9757. {
  9758. TBL_PC *sd;
  9759. sd=script_rid2sd(st);
  9760. if( sd == NULL )
  9761. return SCRIPT_CMD_SUCCESS;
  9762. if( sd->status.pet_id )
  9763. {// do not send egg list, when you already have a pet
  9764. return SCRIPT_CMD_SUCCESS;
  9765. }
  9766. clif_sendegg(sd);
  9767. return SCRIPT_CMD_SUCCESS;
  9768. }
  9769. /**
  9770. * resetlvl <action type>{,<char_id>};
  9771. * @param action_type:
  9772. * 1 : make like after rebirth
  9773. * 2 : blvl,jlvl=1, skillpoint=0
  9774. * 3 : don't reset skill, blvl=1
  9775. * 4 : jlvl=0
  9776. * @author AppleGirl
  9777. **/
  9778. BUILDIN_FUNC(resetlvl)
  9779. {
  9780. TBL_PC *sd;
  9781. int type=script_getnum(st,2);
  9782. if (!script_charid2sd(3,sd))
  9783. return SCRIPT_CMD_FAILURE;
  9784. pc_resetlvl(sd,type);
  9785. return SCRIPT_CMD_SUCCESS;
  9786. }
  9787. /**
  9788. * Reset a player status point
  9789. * resetstatus({<char_id>});
  9790. **/
  9791. BUILDIN_FUNC(resetstatus)
  9792. {
  9793. TBL_PC *sd;
  9794. if (!script_charid2sd(2,sd))
  9795. return SCRIPT_CMD_FAILURE;
  9796. pc_resetstate(sd);
  9797. return SCRIPT_CMD_SUCCESS;
  9798. }
  9799. /**
  9800. * Reset player's skill
  9801. * resetskill({<char_id>});
  9802. **/
  9803. BUILDIN_FUNC(resetskill)
  9804. {
  9805. TBL_PC *sd;
  9806. if (!script_charid2sd(2,sd))
  9807. return SCRIPT_CMD_FAILURE;
  9808. pc_resetskill(sd,1);
  9809. return SCRIPT_CMD_SUCCESS;
  9810. }
  9811. /**
  9812. * Counts total amount of skill points.
  9813. * skillpointcount({<char_id>})
  9814. **/
  9815. BUILDIN_FUNC(skillpointcount)
  9816. {
  9817. TBL_PC *sd;
  9818. if (!script_charid2sd(2,sd))
  9819. return SCRIPT_CMD_FAILURE;
  9820. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9821. return SCRIPT_CMD_SUCCESS;
  9822. }
  9823. /*==========================================
  9824. *
  9825. *------------------------------------------*/
  9826. BUILDIN_FUNC(changebase)
  9827. {
  9828. TBL_PC *sd=NULL;
  9829. int vclass;
  9830. if( script_hasdata(st,3) )
  9831. sd=map_id2sd(script_getnum(st,3));
  9832. else
  9833. sd=script_rid2sd(st);
  9834. if(sd == NULL)
  9835. return SCRIPT_CMD_SUCCESS;
  9836. vclass = script_getnum(st,2);
  9837. if(vclass == JOB_WEDDING)
  9838. {
  9839. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9840. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9841. )
  9842. return SCRIPT_CMD_SUCCESS;
  9843. }
  9844. if(!sd->disguise && vclass != sd->vd.class_) {
  9845. status_set_viewdata(&sd->bl, vclass);
  9846. //Updated client view. Base, Weapon and Cloth Colors.
  9847. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9848. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9849. if (sd->vd.cloth_color)
  9850. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9851. if (sd->vd.body_style)
  9852. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9853. clif_skillinfoblock(sd);
  9854. }
  9855. return SCRIPT_CMD_SUCCESS;
  9856. }
  9857. /**
  9858. * Change account sex and unequip all item and request for a changesex to char-serv
  9859. * changesex({<char_id>});
  9860. */
  9861. BUILDIN_FUNC(changesex)
  9862. {
  9863. int i;
  9864. TBL_PC *sd = NULL;
  9865. if (!script_charid2sd(2,sd))
  9866. return SCRIPT_CMD_FAILURE;
  9867. pc_resetskill(sd,4);
  9868. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9869. for(i = 0; i < EQI_MAX; i++) {
  9870. if (sd->equip_index[i] >= 0)
  9871. pc_unequipitem(sd, sd->equip_index[i], 3);
  9872. }
  9873. chrif_changesex(sd, true);
  9874. return SCRIPT_CMD_SUCCESS;
  9875. }
  9876. /**
  9877. * Change character's sex and unequip all item and request for a changesex to char-serv
  9878. * changecharsex({<char_id>});
  9879. */
  9880. BUILDIN_FUNC(changecharsex)
  9881. {
  9882. #if PACKETVER >= 20141016
  9883. int i;
  9884. TBL_PC *sd = NULL;
  9885. if (!script_charid2sd(2,sd))
  9886. return SCRIPT_CMD_FAILURE;
  9887. pc_resetskill(sd,4);
  9888. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9889. for (i = 0; i < EQI_MAX; i++) {
  9890. if (sd->equip_index[i] >= 0)
  9891. pc_unequipitem(sd, sd->equip_index[i], 3);
  9892. }
  9893. chrif_changesex(sd, false);
  9894. return SCRIPT_CMD_SUCCESS;
  9895. #else
  9896. return SCRIPT_CMD_FAILURE;
  9897. #endif
  9898. }
  9899. /*==========================================
  9900. * Works like 'announce' but outputs in the common chat window
  9901. *------------------------------------------*/
  9902. BUILDIN_FUNC(globalmes)
  9903. {
  9904. struct block_list *bl = map_id2bl(st->oid);
  9905. struct npc_data *nd = (struct npc_data *)bl;
  9906. const char *name=NULL,*mes;
  9907. mes=script_getstr(st,2);
  9908. if(mes==NULL)
  9909. return SCRIPT_CMD_SUCCESS;
  9910. if(script_hasdata(st,3)){ // npc name to display
  9911. name=script_getstr(st,3);
  9912. } else {
  9913. name=nd->name; //use current npc name
  9914. }
  9915. npc_globalmessage(name,mes); // broadcast to all players connected
  9916. return SCRIPT_CMD_SUCCESS;
  9917. }
  9918. /////////////////////////////////////////////////////////////////////
  9919. // NPC waiting room (chat room)
  9920. //
  9921. /// Creates a waiting room (chat room) for this npc.
  9922. ///
  9923. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9924. BUILDIN_FUNC(waitingroom)
  9925. {
  9926. struct npc_data* nd;
  9927. const char* title = script_getstr(st, 2);
  9928. int limit = script_getnum(st, 3);
  9929. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9930. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9931. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9932. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9933. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9934. nd = (struct npc_data *)map_id2bl(st->oid);
  9935. if( nd != NULL )
  9936. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  9937. return SCRIPT_CMD_SUCCESS;
  9938. }
  9939. /// Removes the waiting room of the current or target npc.
  9940. ///
  9941. /// delwaitingroom "<npc_name>";
  9942. /// delwaitingroom;
  9943. BUILDIN_FUNC(delwaitingroom)
  9944. {
  9945. struct npc_data* nd;
  9946. if( script_hasdata(st,2) )
  9947. nd = npc_name2id(script_getstr(st, 2));
  9948. else
  9949. nd = (struct npc_data *)map_id2bl(st->oid);
  9950. if( nd != NULL )
  9951. chat_deletenpcchat(nd);
  9952. return SCRIPT_CMD_SUCCESS;
  9953. }
  9954. /// Kick the specified player from the waiting room of the target npc.
  9955. ///
  9956. /// waitingroomkick "<npc_name>", <kickusername>;
  9957. BUILDIN_FUNC(waitingroomkick)
  9958. {
  9959. struct npc_data* nd;
  9960. struct chat_data* cd;
  9961. const char* kickusername;
  9962. nd = npc_name2id(script_getstr(st,2));
  9963. kickusername = script_getstr(st,3);
  9964. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9965. chat_npckickchat(cd, kickusername);
  9966. return SCRIPT_CMD_SUCCESS;
  9967. }
  9968. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  9969. /// Num users stored in .@waitingroom_usercount
  9970. ///
  9971. /// getwaitingroomusers "<npc_name>";
  9972. BUILDIN_FUNC(getwaitingroomusers)
  9973. {
  9974. struct npc_data* nd;
  9975. struct chat_data* cd;
  9976. int i, j=0;
  9977. if( script_hasdata(st,2) )
  9978. nd = npc_name2id(script_getstr(st, 2));
  9979. else
  9980. nd = (struct npc_data *)map_id2bl(st->oid);
  9981. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  9982. for(i = 0; i < cd->users; ++i) {
  9983. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  9984. j++;
  9985. }
  9986. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  9987. }
  9988. return SCRIPT_CMD_SUCCESS;
  9989. }
  9990. /// Kicks all the players from the waiting room of the current or target npc.
  9991. ///
  9992. /// kickwaitingroomall "<npc_name>";
  9993. /// kickwaitingroomall;
  9994. BUILDIN_FUNC(waitingroomkickall)
  9995. {
  9996. struct npc_data* nd;
  9997. struct chat_data* cd;
  9998. if( script_hasdata(st,2) )
  9999. nd = npc_name2id(script_getstr(st,2));
  10000. else
  10001. nd = (struct npc_data *)map_id2bl(st->oid);
  10002. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10003. chat_npckickall(cd);
  10004. return SCRIPT_CMD_SUCCESS;
  10005. }
  10006. /// Enables the waiting room event of the current or target npc.
  10007. ///
  10008. /// enablewaitingroomevent "<npc_name>";
  10009. /// enablewaitingroomevent;
  10010. BUILDIN_FUNC(enablewaitingroomevent)
  10011. {
  10012. struct npc_data* nd;
  10013. struct chat_data* cd;
  10014. if( script_hasdata(st,2) )
  10015. nd = npc_name2id(script_getstr(st, 2));
  10016. else
  10017. nd = (struct npc_data *)map_id2bl(st->oid);
  10018. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10019. chat_enableevent(cd);
  10020. return SCRIPT_CMD_SUCCESS;
  10021. }
  10022. /// Disables the waiting room event of the current or target npc.
  10023. ///
  10024. /// disablewaitingroomevent "<npc_name>";
  10025. /// disablewaitingroomevent;
  10026. BUILDIN_FUNC(disablewaitingroomevent)
  10027. {
  10028. struct npc_data *nd;
  10029. struct chat_data *cd;
  10030. if( script_hasdata(st,2) )
  10031. nd = npc_name2id(script_getstr(st, 2));
  10032. else
  10033. nd = (struct npc_data *)map_id2bl(st->oid);
  10034. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10035. chat_disableevent(cd);
  10036. return SCRIPT_CMD_SUCCESS;
  10037. }
  10038. /// Returns info on the waiting room of the current or target npc.
  10039. /// Returns -1 if the type unknown
  10040. /// <type>=0 : current number of users
  10041. /// <type>=1 : maximum number of users allowed
  10042. /// <type>=2 : the number of users that trigger the event
  10043. /// <type>=3 : if the trigger is disabled
  10044. /// <type>=4 : the title of the waiting room
  10045. /// <type>=5 : the password of the waiting room
  10046. /// <type>=16 : the name of the waiting room event
  10047. /// <type>=32 : if the waiting room is full
  10048. /// <type>=33 : if there are enough users to trigger the event
  10049. ///
  10050. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10051. /// getwaitingroomstate(<type>) -> <info>
  10052. BUILDIN_FUNC(getwaitingroomstate)
  10053. {
  10054. struct npc_data *nd;
  10055. struct chat_data *cd;
  10056. int type;
  10057. type = script_getnum(st,2);
  10058. if( script_hasdata(st,3) )
  10059. nd = npc_name2id(script_getstr(st, 3));
  10060. else
  10061. nd = (struct npc_data *)map_id2bl(st->oid);
  10062. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10063. {
  10064. script_pushint(st, -1);
  10065. return SCRIPT_CMD_SUCCESS;
  10066. }
  10067. switch(type)
  10068. {
  10069. case 0: script_pushint(st, cd->users); break;
  10070. case 1: script_pushint(st, cd->limit); break;
  10071. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10072. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10073. case 4: script_pushstrcopy(st, cd->title); break;
  10074. case 5: script_pushstrcopy(st, cd->pass); break;
  10075. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10076. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10077. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10078. default: script_pushint(st, -1); break;
  10079. }
  10080. return SCRIPT_CMD_SUCCESS;
  10081. }
  10082. /// Warps the trigger or target amount of players to the target map and position.
  10083. /// Players are automatically removed from the waiting room.
  10084. /// Those waiting the longest will get warped first.
  10085. /// The target map can be "Random" for a random position in the current map,
  10086. /// and "SavePoint" for the savepoint map+position.
  10087. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10088. ///
  10089. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10090. /// The total number of teleported players is put into $@warpwaitingpcnum
  10091. ///
  10092. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10093. /// warpwaitingpc "<map name>",<x>,<y>;
  10094. BUILDIN_FUNC(warpwaitingpc)
  10095. {
  10096. int x;
  10097. int y;
  10098. int i;
  10099. int n;
  10100. const char* map_name;
  10101. struct npc_data* nd;
  10102. struct chat_data* cd;
  10103. nd = (struct npc_data *)map_id2bl(st->oid);
  10104. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10105. return SCRIPT_CMD_SUCCESS;
  10106. map_name = script_getstr(st,2);
  10107. x = script_getnum(st,3);
  10108. y = script_getnum(st,4);
  10109. n = cd->trigger&0x7f;
  10110. if( script_hasdata(st,5) )
  10111. n = script_getnum(st,5);
  10112. for( i = 0; i < n && cd->users > 0; i++ )
  10113. {
  10114. TBL_PC* sd = cd->usersd[0];
  10115. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  10116. {// can't teleport on this map
  10117. break;
  10118. }
  10119. if( cd->zeny )
  10120. {// fee set
  10121. if( (uint32)sd->status.zeny < cd->zeny )
  10122. {// no zeny to cover set fee
  10123. break;
  10124. }
  10125. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10126. }
  10127. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10128. if( strcmp(map_name,"Random") == 0 )
  10129. pc_randomwarp(sd,CLR_TELEPORT);
  10130. else if( strcmp(map_name,"SavePoint") == 0 )
  10131. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10132. else
  10133. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10134. }
  10135. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10136. return SCRIPT_CMD_SUCCESS;
  10137. }
  10138. /////////////////////////////////////////////////////////////////////
  10139. // ...
  10140. //
  10141. /// Detaches a character from a script.
  10142. ///
  10143. /// @param st Script state to detach the character from.
  10144. static void script_detach_rid(struct script_state* st)
  10145. {
  10146. if(st->rid)
  10147. {
  10148. script_detach_state(st, false);
  10149. st->rid = 0;
  10150. }
  10151. }
  10152. /*=========================================================================
  10153. * Attaches a set of RIDs to the current script. [digitalhamster]
  10154. * addrid(<type>{,<flag>{,<parameters>}});
  10155. * <type>:
  10156. * 0 : All players in the server.
  10157. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10158. * 2 : Party members of a specified party ID.
  10159. * [ Parameters: <party id> ]
  10160. * 3 : Guild members of a specified guild ID.
  10161. * [ Parameters: <guild id> ]
  10162. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10163. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10164. * 5 : All players in the map.
  10165. * [ Parameters: "<map name>" ]
  10166. * Account ID: The specified account ID.
  10167. * <flag>:
  10168. * 0 : Players are always attached. (default)
  10169. * 1 : Players currently running another script will not be attached.
  10170. *-------------------------------------------------------------------------*/
  10171. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10172. {
  10173. int forceflag;
  10174. struct map_session_data *sd = (TBL_PC *)bl;
  10175. struct script_state* st;
  10176. st = va_arg(ap,struct script_state*);
  10177. forceflag = va_arg(ap,int);
  10178. if(!forceflag || !sd->st)
  10179. if(sd->status.account_id != st->rid)
  10180. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10181. return 0;
  10182. }
  10183. BUILDIN_FUNC(addrid)
  10184. {
  10185. struct s_mapiterator* iter;
  10186. struct block_list *bl;
  10187. TBL_PC *sd;
  10188. if(st->rid < 1) {
  10189. st->state = END;
  10190. bl = map_id2bl(st->oid);
  10191. } else
  10192. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10193. iter = mapit_getallusers();
  10194. switch(script_getnum(st,2)) {
  10195. case 0:
  10196. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10197. if(!script_getnum(st,3) || !sd->st)
  10198. if(sd->status.account_id != st->rid) //attached player already runs.
  10199. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10200. }
  10201. break;
  10202. case 1:
  10203. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10204. if(!script_getnum(st,3) || !sd->st)
  10205. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10206. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10207. }
  10208. break;
  10209. case 2:
  10210. if(script_getnum(st,4) == 0) {
  10211. script_pushint(st,0);
  10212. return SCRIPT_CMD_SUCCESS;
  10213. }
  10214. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10215. if(!script_getnum(st,3) || !sd->st)
  10216. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10217. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10218. }
  10219. break;
  10220. case 3:
  10221. if(script_getnum(st,4) == 0) {
  10222. script_pushint(st,0);
  10223. return SCRIPT_CMD_SUCCESS;
  10224. }
  10225. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10226. if(!script_getnum(st,3) || !sd->st)
  10227. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10228. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10229. }
  10230. break;
  10231. case 4:
  10232. map_foreachinarea(buildin_addrid_sub,
  10233. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10234. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10235. break;
  10236. case 5:
  10237. if (script_getstr(st, 4) == NULL) {
  10238. script_pushint(st, 0);
  10239. return SCRIPT_CMD_FAILURE;
  10240. }
  10241. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  10242. script_pushint(st, 0);
  10243. return SCRIPT_CMD_FAILURE;
  10244. }
  10245. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  10246. break;
  10247. default:
  10248. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10249. script_pushint(st,0);
  10250. return SCRIPT_CMD_SUCCESS;
  10251. }
  10252. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10253. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10254. script_pushint(st,1);
  10255. }
  10256. return SCRIPT_CMD_SUCCESS;
  10257. }
  10258. mapit_free(iter);
  10259. script_pushint(st,1);
  10260. return SCRIPT_CMD_SUCCESS;
  10261. }
  10262. /*==========================================
  10263. * Attach sd char id to script and detach current one if any
  10264. *------------------------------------------*/
  10265. BUILDIN_FUNC(attachrid)
  10266. {
  10267. int rid = script_getnum(st,2);
  10268. if (map_id2sd(rid) != NULL) {
  10269. script_detach_rid(st);
  10270. st->rid = rid;
  10271. script_attach_state(st);
  10272. script_pushint(st,1);
  10273. } else
  10274. script_pushint(st,0);
  10275. return SCRIPT_CMD_SUCCESS;
  10276. }
  10277. /*==========================================
  10278. * Detach script to rid
  10279. *------------------------------------------*/
  10280. BUILDIN_FUNC(detachrid)
  10281. {
  10282. script_detach_rid(st);
  10283. return SCRIPT_CMD_SUCCESS;
  10284. }
  10285. /*==========================================
  10286. * Chk if account connected, (and charid from account if specified)
  10287. *------------------------------------------*/
  10288. BUILDIN_FUNC(isloggedin)
  10289. {
  10290. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10291. if (script_hasdata(st,3) && sd &&
  10292. sd->status.char_id != script_getnum(st,3))
  10293. sd = NULL;
  10294. push_val(st->stack,C_INT,sd!=NULL);
  10295. return SCRIPT_CMD_SUCCESS;
  10296. }
  10297. /*==========================================
  10298. *
  10299. *------------------------------------------*/
  10300. BUILDIN_FUNC(setmapflagnosave)
  10301. {
  10302. int16 m,x,y;
  10303. unsigned short mapindex;
  10304. const char *str,*str2;
  10305. str=script_getstr(st,2);
  10306. str2=script_getstr(st,3);
  10307. x=script_getnum(st,4);
  10308. y=script_getnum(st,5);
  10309. m = map_mapname2mapid(str);
  10310. mapindex = mapindex_name2id(str2);
  10311. if(m >= 0 && mapindex) {
  10312. map[m].flag.nosave=1;
  10313. map[m].save.map=mapindex;
  10314. map[m].save.x=x;
  10315. map[m].save.y=y;
  10316. }
  10317. return SCRIPT_CMD_SUCCESS;
  10318. }
  10319. BUILDIN_FUNC(getmapflag)
  10320. {
  10321. int16 m,i;
  10322. const char *str;
  10323. str=script_getstr(st,2);
  10324. i=script_getnum(st,3);
  10325. m = map_mapname2mapid(str);
  10326. if(m >= 0) {
  10327. switch(i) {
  10328. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10329. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10330. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10331. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10332. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10333. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10334. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10335. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10336. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10337. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10338. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10339. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10340. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10341. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10342. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10343. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10344. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10345. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10346. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10347. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10348. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10349. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10350. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10351. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10352. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10353. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10354. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10355. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10356. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10357. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10358. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10359. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10360. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10361. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10362. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10363. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10364. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10365. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10366. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10367. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10368. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10369. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10370. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10371. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10372. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10373. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10374. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10375. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10376. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10377. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10378. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10379. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10380. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10381. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10382. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10383. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10384. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10385. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10386. case MF_NOCOSTUME: script_pushint(st,map[m].flag.nocostume); break;
  10387. case MF_GVG_TE_CASTLE: script_pushint(st,map[m].flag.gvg_te_castle); break;
  10388. case MF_GVG_TE: script_pushint(st,map[m].flag.gvg_te); break;
  10389. #ifdef ADJUST_SKILL_DAMAGE
  10390. case MF_SKILL_DAMAGE:
  10391. {
  10392. int ret_val=0, type=0;
  10393. FETCH(4,type);
  10394. switch (type) {
  10395. case 1: ret_val = map[m].adjust.damage.pc; break;
  10396. case 2: ret_val = map[m].adjust.damage.mob; break;
  10397. case 3: ret_val = map[m].adjust.damage.boss; break;
  10398. case 4: ret_val = map[m].adjust.damage.other; break;
  10399. case 5: ret_val = map[m].adjust.damage.caster; break;
  10400. default: ret_val = map[m].flag.skill_damage; break;
  10401. }
  10402. script_pushint(st,ret_val); break;
  10403. } break;
  10404. #endif
  10405. }
  10406. }
  10407. return SCRIPT_CMD_SUCCESS;
  10408. }
  10409. /* pvp timer handling */
  10410. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10411. TBL_PC* sd = (TBL_PC*)bl;
  10412. if (sd->pvp_timer == INVALID_TIMER) {
  10413. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10414. sd->pvp_rank = 0;
  10415. sd->pvp_lastusers = 0;
  10416. sd->pvp_point = 5;
  10417. sd->pvp_won = 0;
  10418. sd->pvp_lost = 0;
  10419. }
  10420. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE,SELF);
  10421. return 0;
  10422. }
  10423. BUILDIN_FUNC(setmapflag)
  10424. {
  10425. int16 m,i;
  10426. const char *str;
  10427. int val=0;
  10428. str=script_getstr(st,2);
  10429. i=script_getnum(st,3);
  10430. FETCH(4,val);
  10431. m = map_mapname2mapid(str);
  10432. if(m >= 0) {
  10433. switch(i) {
  10434. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10435. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10436. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10437. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10438. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10439. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10440. case MF_PVP:
  10441. map[m].flag.pvp = 1;
  10442. if( !battle_config.pk_mode ) {
  10443. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10444. }
  10445. break;
  10446. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10447. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10448. case MF_GVG:
  10449. map[m].flag.gvg = 1;
  10450. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10451. break;
  10452. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10453. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10454. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10455. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10456. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10457. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10458. case MF_SNOW: map[m].flag.snow = 1; break;
  10459. case MF_FOG: map[m].flag.fog = 1; break;
  10460. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10461. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10462. case MF_NOGO: map[m].flag.nogo = 1; break;
  10463. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10464. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10465. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10466. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10467. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10468. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10469. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10470. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10471. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10472. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10473. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10474. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10475. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10476. case MF_RESTRICTED:
  10477. map[m].zone |= 1<<(val+1);
  10478. map[m].flag.restricted=1;
  10479. break;
  10480. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10481. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10482. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10483. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10484. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10485. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10486. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10487. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10488. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10489. case MF_TOWN: map[m].flag.town = 1; break;
  10490. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10491. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10492. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10493. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10494. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10495. case MF_RESET: map[m].flag.reset = 1; break;
  10496. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10497. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10498. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10499. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10500. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10501. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10502. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10503. case MF_NOCOSTUME: map[m].flag.nocostume = 1; break;
  10504. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 1; break;
  10505. case MF_GVG_TE:
  10506. map[m].flag.gvg_te = 1;
  10507. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10508. break;
  10509. #ifdef ADJUST_SKILL_DAMAGE
  10510. case MF_SKILL_DAMAGE:
  10511. {
  10512. int type=0;
  10513. FETCH(5,type);
  10514. switch (type) {
  10515. case 1: map[m].adjust.damage.pc = val; break;
  10516. case 2: map[m].adjust.damage.mob = val; break;
  10517. case 3: map[m].adjust.damage.boss = val; break;
  10518. case 4: map[m].adjust.damage.other = val; break;
  10519. case 5: map[m].adjust.damage.caster = val; break;
  10520. }
  10521. map[m].flag.skill_damage = 1;
  10522. } break;
  10523. #endif
  10524. }
  10525. }
  10526. return SCRIPT_CMD_SUCCESS;
  10527. }
  10528. BUILDIN_FUNC(removemapflag)
  10529. {
  10530. int16 m,i;
  10531. const char *str;
  10532. int val=0;
  10533. str=script_getstr(st,2);
  10534. i=script_getnum(st,3);
  10535. FETCH(4,val);
  10536. m = map_mapname2mapid(str);
  10537. if(m >= 0) {
  10538. switch(i) {
  10539. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10540. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10541. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10542. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10543. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10544. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10545. case MF_PVP:
  10546. map[m].flag.pvp = 0;
  10547. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10548. break;
  10549. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10550. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10551. case MF_GVG:
  10552. map[m].flag.gvg = 0;
  10553. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10554. break;
  10555. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10556. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10557. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10558. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10559. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10560. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10561. case MF_SNOW: map[m].flag.snow = 0; break;
  10562. case MF_FOG: map[m].flag.fog = 0; break;
  10563. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10564. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10565. case MF_NOGO: map[m].flag.nogo = 0; break;
  10566. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10567. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10568. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10569. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10570. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10571. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10572. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10573. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10574. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10575. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10576. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10577. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10578. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10579. case MF_RESTRICTED:
  10580. map[m].zone ^= 1<<(val+1);
  10581. if (map[m].zone == 0){
  10582. map[m].flag.restricted=0;
  10583. }
  10584. break;
  10585. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10586. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10587. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10588. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10589. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10590. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10591. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10592. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10593. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10594. case MF_TOWN: map[m].flag.town = 0; break;
  10595. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10596. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10597. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10598. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10599. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10600. case MF_RESET: map[m].flag.reset = 0; break;
  10601. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10602. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10603. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10604. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10605. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10606. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10607. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10608. case MF_NOCOSTUME: map[m].flag.nocostume = 0; break;
  10609. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 0; break;
  10610. case MF_GVG_TE:
  10611. map[m].flag.gvg_te = 0;
  10612. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10613. break;
  10614. #ifdef ADJUST_SKILL_DAMAGE
  10615. case MF_SKILL_DAMAGE:
  10616. {
  10617. map[m].flag.skill_damage = 0;
  10618. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10619. if (map[m].skill_damage.count)
  10620. map_skill_damage_free(&map[m]);
  10621. } break;
  10622. #endif
  10623. }
  10624. }
  10625. return SCRIPT_CMD_SUCCESS;
  10626. }
  10627. BUILDIN_FUNC(pvpon)
  10628. {
  10629. int16 m;
  10630. const char *str;
  10631. TBL_PC* sd = NULL;
  10632. struct s_mapiterator* iter;
  10633. str = script_getstr(st,2);
  10634. m = map_mapname2mapid(str);
  10635. if( m < 0 || map[m].flag.pvp )
  10636. return SCRIPT_CMD_SUCCESS; // nothing to do
  10637. map[m].flag.pvp = 1;
  10638. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10639. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10640. return SCRIPT_CMD_SUCCESS;
  10641. iter = mapit_getallusers();
  10642. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10643. {
  10644. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10645. continue; // not applicable
  10646. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10647. sd->pvp_rank = 0;
  10648. sd->pvp_lastusers = 0;
  10649. sd->pvp_point = 5;
  10650. sd->pvp_won = 0;
  10651. sd->pvp_lost = 0;
  10652. }
  10653. mapit_free(iter);
  10654. return SCRIPT_CMD_SUCCESS;
  10655. }
  10656. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10657. {
  10658. TBL_PC* sd = (TBL_PC*)bl;
  10659. clif_pvpset(sd, 0, 0, 2);
  10660. if (sd->pvp_timer != INVALID_TIMER) {
  10661. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10662. sd->pvp_timer = INVALID_TIMER;
  10663. }
  10664. return 0;
  10665. }
  10666. BUILDIN_FUNC(pvpoff)
  10667. {
  10668. int16 m;
  10669. const char *str;
  10670. str=script_getstr(st,2);
  10671. m = map_mapname2mapid(str);
  10672. if(m < 0 || !map[m].flag.pvp)
  10673. return SCRIPT_CMD_SUCCESS; //fixed Lupus
  10674. map[m].flag.pvp = 0;
  10675. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10676. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10677. return SCRIPT_CMD_SUCCESS;
  10678. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10679. return SCRIPT_CMD_SUCCESS;
  10680. }
  10681. BUILDIN_FUNC(gvgon)
  10682. {
  10683. int16 m;
  10684. const char *str;
  10685. str=script_getstr(st,2);
  10686. m = map_mapname2mapid(str);
  10687. if(m >= 0 && !map[m].flag.gvg) {
  10688. map[m].flag.gvg = 1;
  10689. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10690. }
  10691. return SCRIPT_CMD_SUCCESS;
  10692. }
  10693. BUILDIN_FUNC(gvgoff)
  10694. {
  10695. int16 m;
  10696. const char *str;
  10697. str=script_getstr(st,2);
  10698. m = map_mapname2mapid(str);
  10699. if(m >= 0 && map[m].flag.gvg) {
  10700. map[m].flag.gvg = 0;
  10701. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10702. }
  10703. return SCRIPT_CMD_SUCCESS;
  10704. }
  10705. BUILDIN_FUNC(gvgon3)
  10706. {
  10707. int16 m;
  10708. const char *str;
  10709. str = script_getstr(st,2);
  10710. m = map_mapname2mapid(str);
  10711. if (m >= 0 && !map[m].flag.gvg_te) {
  10712. map[m].flag.gvg_te = 1;
  10713. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10714. }
  10715. return SCRIPT_CMD_SUCCESS;
  10716. }
  10717. BUILDIN_FUNC(gvgoff3)
  10718. {
  10719. int16 m;
  10720. const char *str;
  10721. str = script_getstr(st,2);
  10722. m = map_mapname2mapid(str);
  10723. if (m >= 0 && map[m].flag.gvg_te) {
  10724. map[m].flag.gvg_te = 0;
  10725. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10726. }
  10727. return SCRIPT_CMD_SUCCESS;
  10728. }
  10729. /*==========================================
  10730. * Shows an emoticon on top of the player/npc
  10731. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10732. *------------------------------------------*/
  10733. //Optional second parameter added by [Skotlex]
  10734. BUILDIN_FUNC(emotion)
  10735. {
  10736. int type;
  10737. int player=0;
  10738. type=script_getnum(st,2);
  10739. if(type < 0 || type > 100)
  10740. return SCRIPT_CMD_SUCCESS;
  10741. if( script_hasdata(st,3) )
  10742. player=script_getnum(st,3);
  10743. if (player) {
  10744. TBL_PC *sd = NULL;
  10745. if( script_hasdata(st,4) )
  10746. sd = map_nick2sd(script_getstr(st,4));
  10747. else
  10748. sd = script_rid2sd(st);
  10749. if (sd)
  10750. clif_emotion(&sd->bl,type);
  10751. } else
  10752. if( script_hasdata(st,4) )
  10753. {
  10754. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10755. if(nd)
  10756. clif_emotion(&nd->bl,type);
  10757. }
  10758. else
  10759. clif_emotion(map_id2bl(st->oid),type);
  10760. return SCRIPT_CMD_SUCCESS;
  10761. }
  10762. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10763. {
  10764. int16 m=va_arg(ap,int);
  10765. int g_id=va_arg(ap,int);
  10766. int flag=va_arg(ap,int);
  10767. if(!sd || sd->bl.m != m)
  10768. return 0;
  10769. if(
  10770. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10771. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10772. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10773. )
  10774. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10775. return 1;
  10776. }
  10777. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10778. {
  10779. struct mob_data *md=(struct mob_data *)bl;
  10780. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM)
  10781. status_kill(bl);
  10782. return 0;
  10783. }
  10784. /*
  10785. * Function to kick guild members out of a map and to their save points.
  10786. * m : mapid
  10787. * g_id : owner guild id
  10788. * flag & 1 : Warp out owners
  10789. * flag & 2 : Warp out outsiders
  10790. * flag & 4 : reserved for mobs
  10791. * */
  10792. BUILDIN_FUNC(maprespawnguildid)
  10793. {
  10794. const char *mapname=script_getstr(st,2);
  10795. int g_id=script_getnum(st,3);
  10796. int flag=script_getnum(st,4);
  10797. int16 m=map_mapname2mapid(mapname);
  10798. if(m == -1)
  10799. return SCRIPT_CMD_SUCCESS;
  10800. //Catch ALL players (in case some are 'between maps' on execution time)
  10801. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10802. if (flag&4) //Remove script mobs.
  10803. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10804. return SCRIPT_CMD_SUCCESS;
  10805. }
  10806. /// Siege commands
  10807. /**
  10808. * Start WoE:FE
  10809. * agitstart();
  10810. */
  10811. BUILDIN_FUNC(agitstart)
  10812. {
  10813. if (agit_flag)
  10814. return SCRIPT_CMD_SUCCESS;// Agit already Started.
  10815. agit_flag = true;
  10816. guild_agit_start();
  10817. return SCRIPT_CMD_SUCCESS;
  10818. }
  10819. /**
  10820. * End WoE:FE
  10821. * agitend();
  10822. */
  10823. BUILDIN_FUNC(agitend)
  10824. {
  10825. if (!agit_flag)
  10826. return SCRIPT_CMD_SUCCESS;// Agit already Ended.
  10827. agit_flag = false;
  10828. guild_agit_end();
  10829. return SCRIPT_CMD_SUCCESS;
  10830. }
  10831. /**
  10832. * Start WoE:SE
  10833. * agitstart2();
  10834. */
  10835. BUILDIN_FUNC(agitstart2)
  10836. {
  10837. if (agit2_flag)
  10838. return SCRIPT_CMD_SUCCESS;// Agit2 already Started.
  10839. agit2_flag = true;
  10840. guild_agit2_start();
  10841. return SCRIPT_CMD_SUCCESS;
  10842. }
  10843. /**
  10844. * End WoE:SE
  10845. * agitend();
  10846. */
  10847. BUILDIN_FUNC(agitend2)
  10848. {
  10849. if (!agit2_flag)
  10850. return SCRIPT_CMD_SUCCESS;// Agit2 already Ended.
  10851. agit2_flag = false;
  10852. guild_agit2_end();
  10853. return SCRIPT_CMD_SUCCESS;
  10854. }
  10855. /**
  10856. * Start WoE:TE
  10857. * agitstart3();
  10858. */
  10859. BUILDIN_FUNC(agitstart3)
  10860. {
  10861. if (agit3_flag)
  10862. return SCRIPT_CMD_SUCCESS;// AgitTE already Started.
  10863. agit3_flag = true;
  10864. guild_agit3_start();
  10865. return SCRIPT_CMD_SUCCESS;
  10866. }
  10867. /**
  10868. * End WoE:TE
  10869. * agitend3();
  10870. */
  10871. BUILDIN_FUNC(agitend3)
  10872. {
  10873. if (!agit3_flag)
  10874. return SCRIPT_CMD_SUCCESS;// AgitTE already Ended.
  10875. agit3_flag = false;
  10876. guild_agit3_end();
  10877. return SCRIPT_CMD_SUCCESS;
  10878. }
  10879. /**
  10880. * Returns whether WoE:FE is on or off.
  10881. * agitcheck();
  10882. */
  10883. BUILDIN_FUNC(agitcheck)
  10884. {
  10885. script_pushint(st, agit_flag);
  10886. return SCRIPT_CMD_SUCCESS;
  10887. }
  10888. /**
  10889. * Returns whether WoE:SE is on or off.
  10890. * agitcheck2();
  10891. */
  10892. BUILDIN_FUNC(agitcheck2)
  10893. {
  10894. script_pushint(st, agit2_flag);
  10895. return SCRIPT_CMD_SUCCESS;
  10896. }
  10897. /**
  10898. * Returns whether WoE:TE is on or off.
  10899. * agitcheck3();
  10900. */
  10901. BUILDIN_FUNC(agitcheck3)
  10902. {
  10903. script_pushint(st, agit3_flag);
  10904. return SCRIPT_CMD_SUCCESS;
  10905. }
  10906. /// Sets the guild_id of this npc.
  10907. ///
  10908. /// flagemblem <guild_id>;
  10909. BUILDIN_FUNC(flagemblem)
  10910. {
  10911. TBL_NPC* nd;
  10912. int g_id = script_getnum(st,2);
  10913. if(g_id < 0)
  10914. return SCRIPT_CMD_SUCCESS;
  10915. nd = (TBL_NPC*)map_id2nd(st->oid);
  10916. if( nd == NULL ) {
  10917. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10918. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  10919. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10920. } else {
  10921. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10922. nd->u.scr.guild_id = g_id;
  10923. clif_guild_emblem_area(&nd->bl);
  10924. /* guild flag caching */
  10925. if( g_id ) /* adding a id */
  10926. guild_flag_add(nd);
  10927. else if( changed ) /* removing a flag */
  10928. guild_flag_remove(nd);
  10929. }
  10930. return SCRIPT_CMD_SUCCESS;
  10931. }
  10932. BUILDIN_FUNC(getcastlename)
  10933. {
  10934. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10935. struct guild_castle* gc = guild_mapname2gc(mapname);
  10936. const char* name = (gc) ? gc->castle_name : "";
  10937. script_pushstrcopy(st,name);
  10938. return SCRIPT_CMD_SUCCESS;
  10939. }
  10940. BUILDIN_FUNC(getcastledata)
  10941. {
  10942. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10943. int index = script_getnum(st,3);
  10944. struct guild_castle *gc = guild_mapname2gc(mapname);
  10945. if (gc == NULL) {
  10946. script_pushint(st,0);
  10947. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  10948. return SCRIPT_CMD_FAILURE;
  10949. }
  10950. switch (index) {
  10951. case 1:
  10952. script_pushint(st,gc->guild_id); break;
  10953. case 2:
  10954. script_pushint(st,gc->economy); break;
  10955. case 3:
  10956. script_pushint(st,gc->defense); break;
  10957. case 4:
  10958. script_pushint(st,gc->triggerE); break;
  10959. case 5:
  10960. script_pushint(st,gc->triggerD); break;
  10961. case 6:
  10962. script_pushint(st,gc->nextTime); break;
  10963. case 7:
  10964. script_pushint(st,gc->payTime); break;
  10965. case 8:
  10966. script_pushint(st,gc->createTime); break;
  10967. case 9:
  10968. script_pushint(st,gc->visibleC); break;
  10969. default:
  10970. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10971. script_pushint(st,gc->guardian[index-10].visible);
  10972. break;
  10973. }
  10974. script_pushint(st,0);
  10975. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  10976. return SCRIPT_CMD_FAILURE;
  10977. }
  10978. return SCRIPT_CMD_SUCCESS;
  10979. }
  10980. BUILDIN_FUNC(setcastledata)
  10981. {
  10982. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10983. int index = script_getnum(st,3);
  10984. int value = script_getnum(st,4);
  10985. struct guild_castle *gc = guild_mapname2gc(mapname);
  10986. if (gc == NULL) {
  10987. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10988. return SCRIPT_CMD_FAILURE;
  10989. }
  10990. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10991. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10992. return SCRIPT_CMD_FAILURE;
  10993. }
  10994. guild_castledatasave(gc->castle_id, index, value);
  10995. return SCRIPT_CMD_SUCCESS;
  10996. }
  10997. /* =====================================================================
  10998. * ---------------------------------------------------------------------*/
  10999. BUILDIN_FUNC(requestguildinfo)
  11000. {
  11001. int guild_id=script_getnum(st,2);
  11002. const char *event=NULL;
  11003. if( script_hasdata(st,3) ){
  11004. event=script_getstr(st,3);
  11005. check_event(st, event);
  11006. }
  11007. if(guild_id>0)
  11008. guild_npc_request_info(guild_id,event);
  11009. return SCRIPT_CMD_SUCCESS;
  11010. }
  11011. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11012. /// getequipcardcnt(<equipment slot>);
  11013. BUILDIN_FUNC(getequipcardcnt)
  11014. {
  11015. int i=-1,j,num;
  11016. TBL_PC *sd;
  11017. int count;
  11018. num=script_getnum(st,2);
  11019. sd=script_rid2sd(st);
  11020. if (equip_index_check(num))
  11021. i=pc_checkequip(sd,equip_bitmask[num]);
  11022. if (i < 0 || !sd->inventory_data[i]) {
  11023. script_pushint(st,0);
  11024. return SCRIPT_CMD_SUCCESS;
  11025. }
  11026. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11027. {
  11028. script_pushint(st,0);
  11029. return SCRIPT_CMD_SUCCESS;
  11030. }
  11031. count = 0;
  11032. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  11033. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11034. count++;
  11035. script_pushint(st,count);
  11036. return SCRIPT_CMD_SUCCESS;
  11037. }
  11038. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11039. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11040. /// successremovecards <slot>;
  11041. BUILDIN_FUNC(successremovecards) {
  11042. int i=-1,c,cardflag=0;
  11043. TBL_PC* sd = script_rid2sd(st);
  11044. int num = script_getnum(st,2);
  11045. if (equip_index_check(num))
  11046. i=pc_checkequip(sd,equip_bitmask[num]);
  11047. if (i < 0 || !sd->inventory_data[i]) {
  11048. return SCRIPT_CMD_SUCCESS;
  11049. }
  11050. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11051. return SCRIPT_CMD_SUCCESS;
  11052. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11053. 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
  11054. unsigned char flag = 0;
  11055. struct item item_tmp;
  11056. memset(&item_tmp,0,sizeof(item_tmp));
  11057. cardflag = 1;
  11058. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11059. item_tmp.identify = 1;
  11060. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11061. clif_additem(sd,0,0,flag);
  11062. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11063. }
  11064. }
  11065. }
  11066. if(cardflag == 1) {//if card was remove remplace item with no card
  11067. unsigned char flag = 0, j;
  11068. struct item item_tmp;
  11069. memset(&item_tmp,0,sizeof(item_tmp));
  11070. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11071. item_tmp.identify = 1;
  11072. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11073. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11074. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11075. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11076. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11077. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11078. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11079. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11080. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11081. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11082. }
  11083. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11084. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11085. clif_additem(sd,0,0,flag);
  11086. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11087. }
  11088. clif_misceffect(&sd->bl,3);
  11089. }
  11090. return SCRIPT_CMD_SUCCESS;
  11091. }
  11092. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11093. /// failedremovecards <slot>, <type>;
  11094. /// <type>=0 : will destroy both the item and the cards.
  11095. /// <type>=1 : will keep the item, but destroy the cards.
  11096. /// <type>=2 : will keep the cards, but destroy the item.
  11097. /// <type>=? : will just display the failure effect.
  11098. BUILDIN_FUNC(failedremovecards) {
  11099. int i=-1,c,cardflag=0;
  11100. TBL_PC* sd = script_rid2sd(st);
  11101. int num = script_getnum(st,2);
  11102. int typefail = script_getnum(st,3);
  11103. if (equip_index_check(num))
  11104. i=pc_checkequip(sd,equip_bitmask[num]);
  11105. if (i < 0 || !sd->inventory_data[i])
  11106. return SCRIPT_CMD_SUCCESS;
  11107. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11108. return SCRIPT_CMD_SUCCESS;
  11109. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11110. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11111. cardflag = 1;
  11112. if(typefail == 2) {// add cards to inventory, clear
  11113. unsigned char flag = 0;
  11114. struct item item_tmp;
  11115. memset(&item_tmp,0,sizeof(item_tmp));
  11116. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11117. item_tmp.identify = 1;
  11118. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11119. clif_additem(sd,0,0,flag);
  11120. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11121. }
  11122. }
  11123. }
  11124. }
  11125. if(cardflag == 1) {
  11126. if(typefail == 0 || typefail == 2){ // destroy the item
  11127. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11128. }else if(typefail == 1){ // destroy the card
  11129. unsigned char flag = 0, j;
  11130. struct item item_tmp;
  11131. memset(&item_tmp,0,sizeof(item_tmp));
  11132. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11133. item_tmp.identify = 1;
  11134. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11135. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11136. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11137. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11138. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11139. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11140. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11141. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11142. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11143. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11144. }
  11145. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11146. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11147. clif_additem(sd,0,0,flag);
  11148. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11149. }
  11150. }
  11151. clif_misceffect(&sd->bl,2);
  11152. }
  11153. return SCRIPT_CMD_SUCCESS;
  11154. }
  11155. /* ================================================================
  11156. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11157. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11158. * improved by [Lance]
  11159. * ================================================================*/
  11160. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11161. {
  11162. int x,y,m,check_val=0,check_ID=0,i=0;
  11163. struct guild *g = NULL;
  11164. struct party_data *p = NULL;
  11165. const char *str;
  11166. const char *mapname;
  11167. unsigned int index;
  11168. mapname=script_getstr(st,2);
  11169. str=script_getstr(st,3);
  11170. x=script_getnum(st,4);
  11171. y=script_getnum(st,5);
  11172. if(script_hasdata(st,7)){
  11173. check_val=script_getnum(st,6);
  11174. check_ID=script_getnum(st,7);
  11175. }
  11176. if((m=map_mapname2mapid(mapname))< 0)
  11177. return SCRIPT_CMD_SUCCESS;
  11178. if(!(index=mapindex_name2id(str)))
  11179. return SCRIPT_CMD_SUCCESS;
  11180. switch(check_val){
  11181. case 1:
  11182. g = guild_search(check_ID);
  11183. if (g){
  11184. for( i=0; i < g->max_member; i++)
  11185. {
  11186. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11187. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11188. }
  11189. }
  11190. }
  11191. break;
  11192. case 2:
  11193. p = party_search(check_ID);
  11194. if(p){
  11195. for(i=0;i<MAX_PARTY; i++){
  11196. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11197. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11198. }
  11199. }
  11200. }
  11201. break;
  11202. default:
  11203. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11204. break;
  11205. }
  11206. return SCRIPT_CMD_SUCCESS;
  11207. }
  11208. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11209. {
  11210. char *event=va_arg(ap,char *);
  11211. struct mob_data *md = ((struct mob_data *)bl);
  11212. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11213. return 1;
  11214. return SCRIPT_CMD_SUCCESS;
  11215. }
  11216. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11217. {
  11218. const char *mapname,*event;
  11219. int16 m;
  11220. mapname=script_getstr(st,2);
  11221. event=script_getstr(st,3);
  11222. if( strcmp(event, "all") == 0 )
  11223. event = NULL;
  11224. else
  11225. check_event(st, event);
  11226. if( strcmp(mapname, "this") == 0 ) {
  11227. struct map_session_data *sd = script_rid2sd(st);
  11228. if( sd )
  11229. m = sd->bl.m;
  11230. else {
  11231. script_pushint(st,-1);
  11232. return SCRIPT_CMD_SUCCESS;
  11233. }
  11234. }
  11235. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11236. script_pushint(st,-1);
  11237. return SCRIPT_CMD_SUCCESS;
  11238. }
  11239. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11240. return SCRIPT_CMD_SUCCESS;
  11241. }
  11242. BUILDIN_FUNC(marriage)
  11243. {
  11244. const char *partner=script_getstr(st,2);
  11245. TBL_PC *sd=script_rid2sd(st);
  11246. TBL_PC *p_sd=map_nick2sd(partner);
  11247. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  11248. script_pushint(st,0);
  11249. return SCRIPT_CMD_SUCCESS;
  11250. }
  11251. script_pushint(st,1);
  11252. return SCRIPT_CMD_SUCCESS;
  11253. }
  11254. BUILDIN_FUNC(wedding_effect)
  11255. {
  11256. TBL_PC *sd=script_rid2sd(st);
  11257. struct block_list *bl;
  11258. if(sd==NULL) {
  11259. bl=map_id2bl(st->oid);
  11260. } else
  11261. bl=&sd->bl;
  11262. clif_wedding_effect(bl);
  11263. return SCRIPT_CMD_SUCCESS;
  11264. }
  11265. /**
  11266. * divorce({<char_id>})
  11267. **/
  11268. BUILDIN_FUNC(divorce)
  11269. {
  11270. TBL_PC *sd;
  11271. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11272. script_pushint(st,0);
  11273. return SCRIPT_CMD_FAILURE;
  11274. }
  11275. script_pushint(st,1);
  11276. return SCRIPT_CMD_SUCCESS;
  11277. }
  11278. /**
  11279. * ispartneron({<char_id>})
  11280. **/
  11281. BUILDIN_FUNC(ispartneron)
  11282. {
  11283. TBL_PC *sd;
  11284. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11285. map_charid2sd(sd->status.partner_id) == NULL)
  11286. {
  11287. script_pushint(st,0);
  11288. return SCRIPT_CMD_FAILURE;
  11289. }
  11290. script_pushint(st,1);
  11291. return SCRIPT_CMD_SUCCESS;
  11292. }
  11293. /**
  11294. * getpartnerid({<char_id>})
  11295. **/
  11296. BUILDIN_FUNC(getpartnerid)
  11297. {
  11298. TBL_PC *sd;
  11299. if (!script_charid2sd(2,sd)) {
  11300. script_pushint(st,0);
  11301. return SCRIPT_CMD_FAILURE;
  11302. }
  11303. script_pushint(st,sd->status.partner_id);
  11304. return SCRIPT_CMD_SUCCESS;
  11305. }
  11306. /**
  11307. * getchildid({<char_id>})
  11308. **/
  11309. BUILDIN_FUNC(getchildid)
  11310. {
  11311. TBL_PC *sd;
  11312. if (!script_charid2sd(2,sd)) {
  11313. script_pushint(st,0);
  11314. return SCRIPT_CMD_FAILURE;
  11315. }
  11316. script_pushint(st,sd->status.child);
  11317. return SCRIPT_CMD_SUCCESS;
  11318. }
  11319. /**
  11320. * getmotherid({<char_id>})
  11321. **/
  11322. BUILDIN_FUNC(getmotherid)
  11323. {
  11324. TBL_PC *sd;
  11325. if (!script_charid2sd(2,sd)) {
  11326. script_pushint(st,0);
  11327. return SCRIPT_CMD_FAILURE;
  11328. }
  11329. script_pushint(st,sd->status.mother);
  11330. return SCRIPT_CMD_SUCCESS;
  11331. }
  11332. /**
  11333. * getfatherid({<char_id>})
  11334. **/
  11335. BUILDIN_FUNC(getfatherid)
  11336. {
  11337. TBL_PC *sd;
  11338. if (!script_charid2sd(2,sd)) {
  11339. script_pushint(st,0);
  11340. return SCRIPT_CMD_FAILURE;
  11341. }
  11342. script_pushint(st,sd->status.father);
  11343. return SCRIPT_CMD_SUCCESS;
  11344. }
  11345. BUILDIN_FUNC(warppartner)
  11346. {
  11347. int x,y;
  11348. unsigned short mapindex;
  11349. const char *str;
  11350. TBL_PC *sd=script_rid2sd(st);
  11351. TBL_PC *p_sd=NULL;
  11352. if(sd==NULL || !pc_ismarried(sd) ||
  11353. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11354. script_pushint(st,0);
  11355. return SCRIPT_CMD_SUCCESS;
  11356. }
  11357. str=script_getstr(st,2);
  11358. x=script_getnum(st,3);
  11359. y=script_getnum(st,4);
  11360. mapindex = mapindex_name2id(str);
  11361. if (mapindex) {
  11362. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11363. script_pushint(st,1);
  11364. } else
  11365. script_pushint(st,0);
  11366. return SCRIPT_CMD_SUCCESS;
  11367. }
  11368. /*================================================
  11369. * Script for Displaying MOB Information [Valaris]
  11370. *------------------------------------------------*/
  11371. BUILDIN_FUNC(strmobinfo)
  11372. {
  11373. int num=script_getnum(st,2);
  11374. int class_=script_getnum(st,3);
  11375. if(!mobdb_checkid(class_))
  11376. {
  11377. if (num < 3) //requested a string
  11378. script_pushconststr(st,"");
  11379. else
  11380. script_pushint(st,0);
  11381. return SCRIPT_CMD_SUCCESS;
  11382. }
  11383. switch (num) {
  11384. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11385. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11386. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11387. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11388. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11389. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11390. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11391. default:
  11392. script_pushint(st,0);
  11393. break;
  11394. }
  11395. return SCRIPT_CMD_SUCCESS;
  11396. }
  11397. /*==========================================
  11398. * Summon guardians [Valaris]
  11399. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11400. *------------------------------------------*/
  11401. BUILDIN_FUNC(guardian)
  11402. {
  11403. int class_=0,x=0,y=0,guardian=0;
  11404. const char *str,*mapname,*evt="";
  11405. bool has_index = false;
  11406. mapname =script_getstr(st,2);
  11407. x =script_getnum(st,3);
  11408. y =script_getnum(st,4);
  11409. str =script_getstr(st,5);
  11410. class_=script_getnum(st,6);
  11411. if( script_hasdata(st,8) )
  11412. {// "<event label>",<guardian index>
  11413. evt=script_getstr(st,7);
  11414. guardian=script_getnum(st,8);
  11415. has_index = true;
  11416. } else if( script_hasdata(st,7) ){
  11417. struct script_data *data = script_getdata(st,7);
  11418. get_val(st,data);
  11419. if( data_isstring(data) )
  11420. {// "<event label>"
  11421. evt=script_getstr(st,7);
  11422. } else if( data_isint(data) )
  11423. {// <guardian index>
  11424. guardian=script_getnum(st,7);
  11425. has_index = true;
  11426. } else {
  11427. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11428. script_reportdata(data);
  11429. return SCRIPT_CMD_FAILURE;
  11430. }
  11431. }
  11432. check_event(st, evt);
  11433. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11434. return SCRIPT_CMD_SUCCESS;
  11435. }
  11436. /*==========================================
  11437. * Invisible Walls [Zephyrus]
  11438. *------------------------------------------*/
  11439. BUILDIN_FUNC(setwall)
  11440. {
  11441. const char *mapname, *name;
  11442. int x, y, m, size, dir;
  11443. bool shootable;
  11444. mapname = script_getstr(st,2);
  11445. x = script_getnum(st,3);
  11446. y = script_getnum(st,4);
  11447. size = script_getnum(st,5);
  11448. dir = script_getnum(st,6);
  11449. shootable = script_getnum(st,7);
  11450. name = script_getstr(st,8);
  11451. if( (m = map_mapname2mapid(mapname)) < 0 )
  11452. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11453. map_iwall_set(m, x, y, size, dir, shootable, name);
  11454. return SCRIPT_CMD_SUCCESS;
  11455. }
  11456. BUILDIN_FUNC(delwall)
  11457. {
  11458. const char *name = script_getstr(st,2);
  11459. map_iwall_remove(name);
  11460. return SCRIPT_CMD_SUCCESS;
  11461. }
  11462. /// Retrieves various information about the specified guardian.
  11463. ///
  11464. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11465. /// type: 0 - whether it is deployed or not
  11466. /// 1 - maximum hp
  11467. /// 2 - current hp
  11468. ///
  11469. BUILDIN_FUNC(guardianinfo)
  11470. {
  11471. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11472. int id = script_getnum(st,3);
  11473. int type = script_getnum(st,4);
  11474. struct guild_castle* gc = guild_mapname2gc(mapname);
  11475. struct mob_data* gd;
  11476. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11477. {
  11478. script_pushint(st,-1);
  11479. return SCRIPT_CMD_SUCCESS;
  11480. }
  11481. if( type == 0 )
  11482. script_pushint(st, gc->guardian[id].visible);
  11483. else
  11484. if( !gc->guardian[id].visible )
  11485. script_pushint(st,-1);
  11486. else
  11487. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11488. script_pushint(st,-1);
  11489. else
  11490. {
  11491. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11492. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11493. else
  11494. script_pushint(st,-1);
  11495. }
  11496. return SCRIPT_CMD_SUCCESS;
  11497. }
  11498. /*==========================================
  11499. * Get the item name by item_id or null
  11500. *------------------------------------------*/
  11501. BUILDIN_FUNC(getitemname)
  11502. {
  11503. unsigned short item_id = 0;
  11504. struct item_data *i_data;
  11505. char *item_name;
  11506. struct script_data *data;
  11507. data=script_getdata(st,2);
  11508. get_val(st,data);
  11509. if( data_isstring(data) ){
  11510. const char *name=conv_str(st,data);
  11511. struct item_data *item_data = itemdb_searchname(name);
  11512. if( item_data )
  11513. item_id=item_data->nameid;
  11514. }else
  11515. item_id=conv_num(st,data);
  11516. i_data = itemdb_exists(item_id);
  11517. if (i_data == NULL)
  11518. {
  11519. script_pushconststr(st,"null");
  11520. return SCRIPT_CMD_SUCCESS;
  11521. }
  11522. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11523. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11524. script_pushstr(st,item_name);
  11525. return SCRIPT_CMD_SUCCESS;
  11526. }
  11527. /*==========================================
  11528. * Returns number of slots an item has. [Skotlex]
  11529. *------------------------------------------*/
  11530. BUILDIN_FUNC(getitemslots)
  11531. {
  11532. unsigned short item_id;
  11533. struct item_data *i_data;
  11534. item_id=script_getnum(st,2);
  11535. i_data = itemdb_exists(item_id);
  11536. if (i_data)
  11537. script_pushint(st,i_data->slot);
  11538. else
  11539. script_pushint(st,-1);
  11540. return SCRIPT_CMD_SUCCESS;
  11541. }
  11542. // TODO: add matk here if needed/once we get rid of RENEWAL
  11543. /*==========================================
  11544. * Returns some values of an item [Lupus]
  11545. * Price, Weight, etc...
  11546. getiteminfo(itemID,n), where n
  11547. 0 value_buy;
  11548. 1 value_sell;
  11549. 2 type;
  11550. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11551. if = 0, then monsters don't drop it at all (rare or a quest item)
  11552. if = -1, then this item is sold in NPC shops only
  11553. 4 sex;
  11554. 5 equip;
  11555. 6 weight;
  11556. 7 atk;
  11557. 8 def;
  11558. 9 range;
  11559. 10 slot;
  11560. 11 look;
  11561. 12 elv;
  11562. 13 wlv;
  11563. 14 view id
  11564. 15 eLvmax
  11565. 16 matk (renewal)
  11566. *------------------------------------------*/
  11567. BUILDIN_FUNC(getiteminfo)
  11568. {
  11569. unsigned short item_id,n;
  11570. struct item_data *i_data;
  11571. item_id = script_getnum(st,2);
  11572. n = script_getnum(st,3);
  11573. i_data = itemdb_exists(item_id);
  11574. if (i_data && n <= 16) {
  11575. int *item_arr = (int*)&i_data->value_buy;
  11576. #ifndef RENEWAL
  11577. if (n == 16)
  11578. script_pushint(st,0);
  11579. else
  11580. #endif
  11581. script_pushint(st,item_arr[n]);
  11582. } else
  11583. script_pushint(st,-1);
  11584. return SCRIPT_CMD_SUCCESS;
  11585. }
  11586. /*==========================================
  11587. * Set some values of an item [Lupus]
  11588. * Price, Weight, etc...
  11589. setiteminfo(itemID,n,Value), where n
  11590. 0 value_buy;
  11591. 1 value_sell;
  11592. 2 type;
  11593. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11594. if = 0, then monsters don't drop it at all (rare or a quest item)
  11595. if = -1, then this item is sold in NPC shops only
  11596. 4 sex;
  11597. 5 equip;
  11598. 6 weight;
  11599. 7 atk;
  11600. 8 def;
  11601. 9 range;
  11602. 10 slot;
  11603. 11 look;
  11604. 12 elv;
  11605. 13 wlv;
  11606. 14 view id
  11607. * Returns Value or -1 if the wrong field's been set
  11608. *------------------------------------------*/
  11609. BUILDIN_FUNC(setiteminfo)
  11610. {
  11611. unsigned short item_id;
  11612. int n,value;
  11613. struct item_data *i_data;
  11614. item_id = script_getnum(st,2);
  11615. n = script_getnum(st,3);
  11616. value = script_getnum(st,4);
  11617. i_data = itemdb_exists(item_id);
  11618. if (i_data && n>=0 && n<=14) {
  11619. int *item_arr = (int*)&i_data->value_buy;
  11620. item_arr[n] = value;
  11621. script_pushint(st,value);
  11622. } else
  11623. script_pushint(st,-1);
  11624. return SCRIPT_CMD_SUCCESS;
  11625. }
  11626. /*==========================================
  11627. * Returns value from equipped item slot n [Lupus]
  11628. getequipcardid(num,slot)
  11629. where
  11630. num = eqip position slot
  11631. slot = 0,1,2,3 (Card Slot N)
  11632. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11633. it's useful when you want to check item cards or if it's signed
  11634. Useful for such quests as "Sign this refined item with players name" etc
  11635. Hat[0] +4 -> Player's Hat[0] +4
  11636. *------------------------------------------*/
  11637. BUILDIN_FUNC(getequipcardid)
  11638. {
  11639. int i=-1,num,slot;
  11640. TBL_PC *sd;
  11641. num=script_getnum(st,2);
  11642. slot=script_getnum(st,3);
  11643. sd=script_rid2sd(st);
  11644. if (equip_index_check(num))
  11645. i=pc_checkequip(sd,equip_bitmask[num]);
  11646. if(i >= 0 && slot>=0 && slot<4)
  11647. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  11648. else
  11649. script_pushint(st,0);
  11650. return SCRIPT_CMD_SUCCESS;
  11651. }
  11652. /*==========================================
  11653. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11654. *------------------------------------------*/
  11655. BUILDIN_FUNC(petskillbonus)
  11656. {
  11657. struct pet_data *pd;
  11658. TBL_PC *sd = script_rid2sd(st);
  11659. if(sd == NULL || sd->pd == NULL)
  11660. return SCRIPT_CMD_FAILURE;
  11661. pd = sd->pd;
  11662. if (pd->bonus)
  11663. { //Clear previous bonus
  11664. if (pd->bonus->timer != INVALID_TIMER)
  11665. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11666. } else //init
  11667. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11668. pd->bonus->type = script_getnum(st,2);
  11669. pd->bonus->val = script_getnum(st,3);
  11670. pd->bonus->duration = script_getnum(st,4);
  11671. pd->bonus->delay = script_getnum(st,5);
  11672. if (pd->state.skillbonus == 1)
  11673. pd->state.skillbonus = 0; // waiting state
  11674. // wait for timer to start
  11675. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11676. pd->bonus->timer = INVALID_TIMER;
  11677. else
  11678. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11679. return SCRIPT_CMD_SUCCESS;
  11680. }
  11681. /*==========================================
  11682. * pet looting [Valaris] //Rewritten by [Skotlex]
  11683. *------------------------------------------*/
  11684. BUILDIN_FUNC(petloot)
  11685. {
  11686. int max;
  11687. struct pet_data *pd;
  11688. TBL_PC *sd=script_rid2sd(st);
  11689. if(sd==NULL || sd->pd==NULL)
  11690. return SCRIPT_CMD_SUCCESS;
  11691. max=script_getnum(st,2);
  11692. if(max < 1)
  11693. max = 1; //Let'em loot at least 1 item.
  11694. else if (max > MAX_PETLOOT_SIZE)
  11695. max = MAX_PETLOOT_SIZE;
  11696. pd = sd->pd;
  11697. if (pd->loot != NULL)
  11698. { //Release whatever was there already and reallocate memory
  11699. pet_lootitem_drop(pd, pd->master);
  11700. aFree(pd->loot->item);
  11701. }
  11702. else
  11703. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11704. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11705. pd->loot->max=max;
  11706. pd->loot->count = 0;
  11707. pd->loot->weight = 0;
  11708. return SCRIPT_CMD_SUCCESS;
  11709. }
  11710. /*==========================================
  11711. * Set arrays with info of all sd inventory :
  11712. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11713. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11714. * @inventorylist_card(0..3), @inventorylist_expire
  11715. * @inventorylist_count = scalar
  11716. *------------------------------------------*/
  11717. BUILDIN_FUNC(getinventorylist)
  11718. {
  11719. TBL_PC *sd;
  11720. char card_var[NAME_LENGTH];
  11721. int i,j=0,k;
  11722. if (!script_charid2sd(2,sd))
  11723. return SCRIPT_CMD_FAILURE;
  11724. for(i=0;i<MAX_INVENTORY;i++){
  11725. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  11726. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  11727. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  11728. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  11729. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  11730. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  11731. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  11732. for (k = 0; k < MAX_SLOTS; k++)
  11733. {
  11734. sprintf(card_var, "@inventorylist_card%d",k+1);
  11735. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  11736. }
  11737. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  11738. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  11739. j++;
  11740. }
  11741. }
  11742. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11743. return SCRIPT_CMD_SUCCESS;
  11744. }
  11745. /**
  11746. * getskilllist ({<char_id>});
  11747. **/
  11748. BUILDIN_FUNC(getskilllist)
  11749. {
  11750. TBL_PC *sd;
  11751. int i,j=0;
  11752. if (!script_charid2sd(2,sd))
  11753. return SCRIPT_CMD_FAILURE;
  11754. for(i=0;i<MAX_SKILL;i++){
  11755. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11756. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11757. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11758. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11759. j++;
  11760. }
  11761. }
  11762. pc_setreg(sd,add_str("@skilllist_count"),j);
  11763. return SCRIPT_CMD_SUCCESS;
  11764. }
  11765. /**
  11766. * clearitem ({<char_id>});
  11767. **/
  11768. BUILDIN_FUNC(clearitem)
  11769. {
  11770. TBL_PC *sd;
  11771. int i;
  11772. if (!script_charid2sd(2,sd))
  11773. return SCRIPT_CMD_FAILURE;
  11774. for (i=0; i<MAX_INVENTORY; i++) {
  11775. if (sd->inventory.u.items_inventory[i].amount) {
  11776. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11777. }
  11778. }
  11779. return SCRIPT_CMD_SUCCESS;
  11780. }
  11781. /**
  11782. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11783. * disguise <Monster ID>{,<char_id>};
  11784. **/
  11785. BUILDIN_FUNC(disguise)
  11786. {
  11787. int id;
  11788. TBL_PC* sd;
  11789. if (!script_charid2sd(3,sd))
  11790. return SCRIPT_CMD_FAILURE;
  11791. id = script_getnum(st,2);
  11792. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11793. pc_disguise(sd, id);
  11794. script_pushint(st,id);
  11795. } else
  11796. script_pushint(st,0);
  11797. return SCRIPT_CMD_SUCCESS;
  11798. }
  11799. /**
  11800. * Undisguise Player (returns 1 if success, 0 on fail)
  11801. * undisguise {<char_id>};
  11802. **/
  11803. BUILDIN_FUNC(undisguise)
  11804. {
  11805. TBL_PC* sd;
  11806. if (!script_charid2sd(2,sd))
  11807. return SCRIPT_CMD_FAILURE;
  11808. if (sd->disguise) {
  11809. pc_disguise(sd, 0);
  11810. script_pushint(st,0);
  11811. } else {
  11812. script_pushint(st,1);
  11813. }
  11814. return SCRIPT_CMD_SUCCESS;
  11815. }
  11816. /*==========================================
  11817. * Transform a bl to another _class,
  11818. * @type unused
  11819. *------------------------------------------*/
  11820. BUILDIN_FUNC(classchange)
  11821. {
  11822. int _class,type;
  11823. struct block_list *bl=map_id2bl(st->oid);
  11824. if (bl==NULL)
  11825. return SCRIPT_CMD_FAILURE;
  11826. _class=script_getnum(st,2);
  11827. type=script_getnum(st,3);
  11828. clif_class_change(bl,_class,type);
  11829. return SCRIPT_CMD_SUCCESS;
  11830. }
  11831. /*==========================================
  11832. * Display an effect
  11833. *------------------------------------------*/
  11834. BUILDIN_FUNC(misceffect)
  11835. {
  11836. int type;
  11837. type=script_getnum(st,2);
  11838. if(st->oid && st->oid != fake_nd->bl.id) {
  11839. struct block_list *bl = map_id2bl(st->oid);
  11840. if (bl)
  11841. clif_specialeffect(bl,type,AREA);
  11842. } else{
  11843. TBL_PC *sd=script_rid2sd(st);
  11844. if(sd)
  11845. clif_specialeffect(&sd->bl,type,AREA);
  11846. }
  11847. return SCRIPT_CMD_SUCCESS;
  11848. }
  11849. /*==========================================
  11850. * Play a BGM on a single client [Rikter/Yommy]
  11851. *------------------------------------------*/
  11852. BUILDIN_FUNC(playBGM)
  11853. {
  11854. struct map_session_data* sd;
  11855. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11856. clif_playBGM(sd, script_getstr(st,2));
  11857. }
  11858. return SCRIPT_CMD_SUCCESS;
  11859. }
  11860. static int playBGM_sub(struct block_list* bl,va_list ap)
  11861. {
  11862. const char* name = va_arg(ap,const char*);
  11863. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11864. return 0;
  11865. }
  11866. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11867. {
  11868. const char* name = va_arg(args, const char*);
  11869. clif_playBGM(sd, name);
  11870. return 0;
  11871. }
  11872. /*==========================================
  11873. * Play a BGM on multiple client [Rikter/Yommy]
  11874. *------------------------------------------*/
  11875. BUILDIN_FUNC(playBGMall)
  11876. {
  11877. const char* name;
  11878. name = script_getstr(st,2);
  11879. if( script_hasdata(st,7) ) {// specified part of map
  11880. const char* mapname = script_getstr(st,3);
  11881. int x0 = script_getnum(st,4);
  11882. int y0 = script_getnum(st,5);
  11883. int x1 = script_getnum(st,6);
  11884. int y1 = script_getnum(st,7);
  11885. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  11886. }
  11887. else if( script_hasdata(st,3) ) {// entire map
  11888. const char* mapname = script_getstr(st,3);
  11889. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  11890. }
  11891. else {// entire server
  11892. map_foreachpc(&playBGM_foreachpc_sub, name);
  11893. }
  11894. return SCRIPT_CMD_SUCCESS;
  11895. }
  11896. /*==========================================
  11897. * Play a .wav sound for sd
  11898. *------------------------------------------*/
  11899. BUILDIN_FUNC(soundeffect)
  11900. {
  11901. TBL_PC* sd = script_rid2sd(st);
  11902. const char* name = script_getstr(st,2);
  11903. int type = script_getnum(st,3);
  11904. if(sd) {
  11905. clif_soundeffect(sd,&sd->bl,name,type);
  11906. }
  11907. return SCRIPT_CMD_SUCCESS;
  11908. }
  11909. int soundeffect_sub(struct block_list* bl,va_list ap)
  11910. {
  11911. char* name = va_arg(ap,char*);
  11912. int type = va_arg(ap,int);
  11913. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11914. return 0;
  11915. }
  11916. /*==========================================
  11917. * Play a sound effect (.wav) on multiple clients
  11918. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11919. *------------------------------------------*/
  11920. BUILDIN_FUNC(soundeffectall)
  11921. {
  11922. struct block_list* bl;
  11923. const char* name;
  11924. int type;
  11925. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11926. if (!bl)
  11927. return SCRIPT_CMD_SUCCESS;
  11928. name = script_getstr(st,2);
  11929. type = script_getnum(st,3);
  11930. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11931. if(!script_hasdata(st,4))
  11932. { // area around
  11933. clif_soundeffectall(bl, name, type, AREA);
  11934. }
  11935. else
  11936. if(!script_hasdata(st,5))
  11937. { // entire map
  11938. const char* mapname = script_getstr(st,4);
  11939. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  11940. }
  11941. else
  11942. if(script_hasdata(st,8))
  11943. { // specified part of map
  11944. const char* mapname = script_getstr(st,4);
  11945. int x0 = script_getnum(st,5);
  11946. int y0 = script_getnum(st,6);
  11947. int x1 = script_getnum(st,7);
  11948. int y1 = script_getnum(st,8);
  11949. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  11950. }
  11951. else
  11952. {
  11953. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11954. }
  11955. return SCRIPT_CMD_SUCCESS;
  11956. }
  11957. /*==========================================
  11958. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11959. *------------------------------------------*/
  11960. BUILDIN_FUNC(petrecovery)
  11961. {
  11962. struct pet_data *pd;
  11963. TBL_PC *sd=script_rid2sd(st);
  11964. int sc;
  11965. if(sd == NULL || sd->pd == NULL)
  11966. return SCRIPT_CMD_FAILURE;
  11967. sc = script_getnum(st,2);
  11968. if (sc <= SC_NONE || sc >= SC_MAX) {
  11969. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  11970. return SCRIPT_CMD_FAILURE;
  11971. }
  11972. pd = sd->pd;
  11973. if (pd->recovery)
  11974. { //Halt previous bonus
  11975. if (pd->recovery->timer != INVALID_TIMER)
  11976. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11977. } else //Init
  11978. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11979. pd->recovery->type = (sc_type)sc;
  11980. pd->recovery->delay = script_getnum(st,3);
  11981. pd->recovery->timer = INVALID_TIMER;
  11982. return SCRIPT_CMD_SUCCESS;
  11983. }
  11984. /*==========================================
  11985. * pet healing [Valaris] //Rewritten by [Skotlex]
  11986. *------------------------------------------*/
  11987. BUILDIN_FUNC(petheal)
  11988. {
  11989. struct pet_data *pd;
  11990. TBL_PC *sd=script_rid2sd(st);
  11991. if(sd==NULL || sd->pd==NULL)
  11992. return SCRIPT_CMD_SUCCESS;
  11993. pd=sd->pd;
  11994. if (pd->s_skill)
  11995. { //Clear previous skill
  11996. if (pd->s_skill->timer != INVALID_TIMER)
  11997. {
  11998. if (pd->s_skill->id)
  11999. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12000. else
  12001. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12002. }
  12003. } else //init memory
  12004. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12005. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  12006. //Use the lv as the amount to heal
  12007. pd->s_skill->lv=script_getnum(st,2);
  12008. pd->s_skill->delay=script_getnum(st,3);
  12009. pd->s_skill->hp=script_getnum(st,4);
  12010. pd->s_skill->sp=script_getnum(st,5);
  12011. //Use delay as initial offset to avoid skill/heal exploits
  12012. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12013. pd->s_skill->timer = INVALID_TIMER;
  12014. else
  12015. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  12016. return SCRIPT_CMD_SUCCESS;
  12017. }
  12018. /*==========================================
  12019. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12020. *------------------------------------------*/
  12021. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12022. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12023. BUILDIN_FUNC(petskillattack)
  12024. {
  12025. struct pet_data *pd;
  12026. struct script_data *data;
  12027. TBL_PC *sd = script_rid2sd(st);
  12028. int id = 0;
  12029. if(sd == NULL || sd->pd == NULL)
  12030. return SCRIPT_CMD_FAILURE;
  12031. data = script_getdata(st, 2);
  12032. get_val(st, data);
  12033. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12034. if (!id) {
  12035. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12036. return SCRIPT_CMD_FAILURE;
  12037. }
  12038. pd = sd->pd;
  12039. if (pd->a_skill == NULL)
  12040. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12041. pd->a_skill->id = id;
  12042. pd->a_skill->damage = 0;
  12043. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12044. pd->a_skill->div_ = 0;
  12045. pd->a_skill->rate = script_getnum(st,4);
  12046. pd->a_skill->bonusrate = script_getnum(st,5);
  12047. return SCRIPT_CMD_SUCCESS;
  12048. }
  12049. /*==========================================
  12050. * pet attack skills [Valaris]
  12051. *------------------------------------------*/
  12052. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12053. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12054. BUILDIN_FUNC(petskillattack2)
  12055. {
  12056. struct pet_data *pd;
  12057. struct script_data *data;
  12058. TBL_PC *sd = script_rid2sd(st);
  12059. int id = 0;
  12060. if(sd == NULL || sd->pd == NULL)
  12061. return SCRIPT_CMD_FAILURE;
  12062. data = script_getdata(st, 2);
  12063. get_val(st, data);
  12064. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12065. if (!id) {
  12066. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12067. return SCRIPT_CMD_FAILURE;
  12068. }
  12069. pd = sd->pd;
  12070. if (pd->a_skill == NULL)
  12071. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12072. pd->a_skill->id = id;
  12073. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12074. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12075. pd->a_skill->div_ = script_getnum(st,4);
  12076. pd->a_skill->rate = script_getnum(st,5);
  12077. pd->a_skill->bonusrate = script_getnum(st,6);
  12078. return SCRIPT_CMD_SUCCESS;
  12079. }
  12080. /*==========================================
  12081. * pet support skills [Skotlex]
  12082. *------------------------------------------*/
  12083. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12084. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12085. BUILDIN_FUNC(petskillsupport)
  12086. {
  12087. struct pet_data *pd;
  12088. struct script_data *data;
  12089. TBL_PC *sd = script_rid2sd(st);
  12090. int id = 0;
  12091. if(sd == NULL || sd->pd == NULL)
  12092. return SCRIPT_CMD_FAILURE;
  12093. data = script_getdata(st, 2);
  12094. get_val(st, data);
  12095. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12096. if (!id) {
  12097. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12098. return SCRIPT_CMD_FAILURE;
  12099. }
  12100. pd = sd->pd;
  12101. if (pd->s_skill)
  12102. { //Clear previous skill
  12103. if (pd->s_skill->timer != INVALID_TIMER)
  12104. {
  12105. if (pd->s_skill->id)
  12106. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12107. else
  12108. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12109. }
  12110. } else //init memory
  12111. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12112. pd->s_skill->id = id;
  12113. pd->s_skill->lv = script_getnum(st,3);
  12114. pd->s_skill->delay = script_getnum(st,4);
  12115. pd->s_skill->hp = script_getnum(st,5);
  12116. pd->s_skill->sp = script_getnum(st,6);
  12117. //Use delay as initial offset to avoid skill/heal exploits
  12118. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12119. pd->s_skill->timer = INVALID_TIMER;
  12120. else
  12121. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12122. return SCRIPT_CMD_SUCCESS;
  12123. }
  12124. /*==========================================
  12125. * Scripted skill effects [Celest]
  12126. *------------------------------------------*/
  12127. /// skilleffect <skill id>,<level>
  12128. /// skilleffect "<skill name>",<level>
  12129. BUILDIN_FUNC(skilleffect)
  12130. {
  12131. TBL_PC *sd;
  12132. uint16 skill_id, skill_lv;
  12133. struct script_data *data = script_getdata(st, 2);
  12134. get_val(st, data); // Convert into value in case of a variable
  12135. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12136. skill_lv = script_getnum(st,3);
  12137. sd = script_rid2sd(st);
  12138. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12139. * which leaves the server thinking it still is sitting. */
  12140. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12141. skill_sit(sd, 0);
  12142. clif_standing(&sd->bl);
  12143. }
  12144. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  12145. return SCRIPT_CMD_SUCCESS;
  12146. }
  12147. /*==========================================
  12148. * NPC skill effects [Valaris]
  12149. *------------------------------------------*/
  12150. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12151. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12152. BUILDIN_FUNC(npcskilleffect)
  12153. {
  12154. struct block_list *bl= map_id2bl(st->oid);
  12155. uint16 skill_id, skill_lv;
  12156. int x, y;
  12157. struct script_data *data = script_getdata(st, 2);
  12158. get_val(st, data); // Convert into value in case of a variable
  12159. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12160. skill_lv=script_getnum(st,3);
  12161. x=script_getnum(st,4);
  12162. y=script_getnum(st,5);
  12163. if (bl)
  12164. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12165. return SCRIPT_CMD_SUCCESS;
  12166. }
  12167. /*==========================================
  12168. * Special effects [Valaris]
  12169. *------------------------------------------*/
  12170. BUILDIN_FUNC(specialeffect)
  12171. {
  12172. struct block_list *bl=map_id2bl(st->oid);
  12173. int type = script_getnum(st,2);
  12174. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12175. if(bl==NULL)
  12176. return SCRIPT_CMD_SUCCESS;
  12177. if( script_hasdata(st,4) )
  12178. {
  12179. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12180. if(nd)
  12181. clif_specialeffect(&nd->bl, type, target);
  12182. }
  12183. else
  12184. {
  12185. if (target == SELF) {
  12186. TBL_PC *sd=script_rid2sd(st);
  12187. if (sd)
  12188. clif_specialeffect_single(bl,type,sd->fd);
  12189. } else {
  12190. clif_specialeffect(bl, type, target);
  12191. }
  12192. }
  12193. return SCRIPT_CMD_SUCCESS;
  12194. }
  12195. BUILDIN_FUNC(specialeffect2)
  12196. {
  12197. TBL_PC *sd=script_rid2sd(st);
  12198. int type = script_getnum(st,2);
  12199. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12200. if( script_hasdata(st,4) )
  12201. sd = map_nick2sd(script_getstr(st,4));
  12202. if (sd)
  12203. clif_specialeffect(&sd->bl, type, target);
  12204. return SCRIPT_CMD_SUCCESS;
  12205. }
  12206. /**
  12207. * nude({<char_id>});
  12208. * @author [Valaris]
  12209. **/
  12210. BUILDIN_FUNC(nude)
  12211. {
  12212. TBL_PC *sd;
  12213. int i, calcflag = 0;
  12214. if (!script_charid2sd(2,sd))
  12215. return SCRIPT_CMD_FAILURE;
  12216. for( i = 0 ; i < EQI_MAX; i++ ) {
  12217. if( sd->equip_index[ i ] >= 0 ) {
  12218. if( !calcflag )
  12219. calcflag = 1;
  12220. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12221. }
  12222. }
  12223. if( calcflag )
  12224. status_calc_pc(sd,SCO_NONE);
  12225. return SCRIPT_CMD_SUCCESS;
  12226. }
  12227. int atcommand_sub(struct script_state* st,int type) {
  12228. TBL_PC *sd, dummy_sd;
  12229. int fd;
  12230. const char *cmd;
  12231. cmd = script_getstr(st,2);
  12232. if (st->rid) {
  12233. sd = script_rid2sd(st);
  12234. fd = sd->fd;
  12235. } else { //Use a dummy character.
  12236. sd = &dummy_sd;
  12237. fd = 0;
  12238. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12239. if (st->oid) {
  12240. struct block_list* bl = map_id2bl(st->oid);
  12241. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12242. if (bl->type == BL_NPC)
  12243. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12244. sd->mapindex = (bl->m > 0) ? bl->m : mapindex_name2id(map_default.mapname);
  12245. }
  12246. // Init Group ID, Level, & permissions
  12247. sd->group_id = sd->group_level = 99;
  12248. sd->permissions |= PC_PERM_ALLPERMISSION;
  12249. }
  12250. if (!is_atcommand(fd, sd, cmd, type)) {
  12251. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12252. script_reportsrc(st);
  12253. return SCRIPT_CMD_FAILURE;
  12254. }
  12255. return SCRIPT_CMD_SUCCESS;
  12256. }
  12257. /*==========================================
  12258. * gmcommand [MouseJstr]
  12259. *------------------------------------------*/
  12260. BUILDIN_FUNC(atcommand) {
  12261. return atcommand_sub(st,0);
  12262. }
  12263. /** Displays a message for the player only (like system messages like "you got an apple" )
  12264. * dispbottom("<message>"{,<color>{,<char_id>}})
  12265. * @param message
  12266. * @param color Hex color default (Green)
  12267. */
  12268. BUILDIN_FUNC(dispbottom)
  12269. {
  12270. TBL_PC *sd;
  12271. int color = 0;
  12272. const char *message;
  12273. if (!script_charid2sd(4,sd))
  12274. return SCRIPT_CMD_FAILURE;
  12275. message = script_getstr(st,2);
  12276. if (script_hasdata(st,3))
  12277. color = script_getnum(st,3); // <color>
  12278. if(sd) {
  12279. if (script_hasdata(st,3))
  12280. clif_messagecolor2(sd, color, message); // [Napster]
  12281. else
  12282. clif_disp_onlyself(sd, message, (int)strlen(message));
  12283. }
  12284. return SCRIPT_CMD_SUCCESS;
  12285. }
  12286. /*===================================
  12287. * Heal portion of recovery command
  12288. *-----------------------------------*/
  12289. int recovery_sub(struct map_session_data* sd, int revive)
  12290. {
  12291. if(revive&(1|4) && pc_isdead(sd)) {
  12292. status_revive(&sd->bl, 100, 100);
  12293. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12294. clif_specialeffect(&sd->bl, 77, AREA);
  12295. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12296. status_percent_heal(&sd->bl, 100, 100);
  12297. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12298. }
  12299. return SCRIPT_CMD_SUCCESS;
  12300. }
  12301. /*=========================================================================
  12302. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12303. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12304. * <type> determines <option>:
  12305. * 0 : char_id
  12306. * 1 : party_id
  12307. * 2 : guild_id
  12308. * 3 : map_name
  12309. * 4 : all characters
  12310. * <revive_flag>:
  12311. * 1 : Revive and heal all players (default)
  12312. * 2 : Heal living players only
  12313. * 4 : Revive dead players only
  12314. * <map name>:
  12315. * for types 1-2 : map_name (null = all maps)
  12316. *-------------------------------------------------------------------------*/
  12317. BUILDIN_FUNC(recovery)
  12318. {
  12319. TBL_PC *sd;
  12320. int map_idx = 0, type = 0, revive = 1;
  12321. type = script_getnum(st,2);
  12322. if(script_hasdata(st,4))
  12323. revive = script_getnum(st,4);
  12324. switch(type) {
  12325. case 0:
  12326. if(script_hasdata(st,3))
  12327. sd=map_charid2sd(script_getnum(st,3));
  12328. else if((sd = script_rid2sd(st)) == NULL)
  12329. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12330. recovery_sub(sd, revive);
  12331. break;
  12332. case 1:
  12333. {
  12334. struct party_data* p;
  12335. //When no party given, we use invoker party
  12336. int p_id = 0, i;
  12337. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12338. map_idx = map_mapname2mapid(script_getstr(st,5));
  12339. if(map_idx < 1) { //But we'll check anyways
  12340. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12341. return SCRIPT_CMD_FAILURE;
  12342. }
  12343. }
  12344. if(script_hasdata(st,3))
  12345. p_id = script_getnum(st,3);
  12346. else if((sd = script_rid2sd(st)))
  12347. p_id = sd->status.party_id;
  12348. p = party_search(p_id);
  12349. if(p == NULL)
  12350. return SCRIPT_CMD_SUCCESS;
  12351. for (i = 0; i < MAX_PARTY; i++) {
  12352. struct map_session_data* pl_sd;
  12353. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12354. || (map_idx && pl_sd->bl.m != map_idx))
  12355. continue;
  12356. recovery_sub(pl_sd, revive);
  12357. }
  12358. break;
  12359. }
  12360. case 2:
  12361. {
  12362. struct guild* g;
  12363. //When no guild given, we use invoker guild
  12364. int g_id = 0, i;
  12365. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12366. map_idx = map_mapname2mapid(script_getstr(st,5));
  12367. if(map_idx < 1) { //But we'll check anyways
  12368. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12369. return SCRIPT_CMD_FAILURE;
  12370. }
  12371. }
  12372. if(script_hasdata(st,3))
  12373. g_id = script_getnum(st,3);
  12374. else if((sd = script_rid2sd(st)))
  12375. g_id = sd->status.guild_id;
  12376. g = guild_search(g_id);
  12377. if(g == NULL)
  12378. return SCRIPT_CMD_SUCCESS;
  12379. for (i = 0; i < MAX_GUILD; i++) {
  12380. struct map_session_data* pl_sd;
  12381. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12382. || (map_idx && pl_sd->bl.m != map_idx))
  12383. continue;
  12384. recovery_sub(pl_sd, revive);
  12385. }
  12386. break;
  12387. }
  12388. case 3:
  12389. if(script_hasdata(st,3))
  12390. map_idx = map_mapname2mapid(script_getstr(st,3));
  12391. else if((sd = script_rid2sd(st)))
  12392. map_idx = sd->bl.m;
  12393. if(map_idx < 1)
  12394. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12395. case 4:
  12396. {
  12397. struct s_mapiterator *iter;
  12398. struct script_data *data = script_getdata(st, 3);
  12399. get_val(st, data); // Convert into value in case of a variable
  12400. if(script_hasdata(st,3) && !data_isstring(data))
  12401. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12402. iter = mapit_getallusers();
  12403. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12404. if(type == 3 && sd->bl.m != map_idx)
  12405. continue;
  12406. recovery_sub(sd, revive);
  12407. }
  12408. mapit_free(iter);
  12409. break;
  12410. }
  12411. default:
  12412. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12413. script_pushint(st,-1);
  12414. return SCRIPT_CMD_FAILURE;
  12415. }
  12416. script_pushint(st,1); //Successfully executed without errors
  12417. return SCRIPT_CMD_SUCCESS;
  12418. }
  12419. /*==========================================
  12420. * Get your pet info: getpetinfo <type>{,<char_id>}
  12421. * n -> 0:pet_id 1:pet_class 2:pet_name
  12422. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12423. * 7:game id
  12424. *------------------------------------------*/
  12425. BUILDIN_FUNC(getpetinfo)
  12426. {
  12427. TBL_PC *sd=script_rid2sd(st);
  12428. TBL_PET *pd;
  12429. int type = script_getnum(st,2);
  12430. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12431. if (type == 2)
  12432. script_pushconststr(st,"null");
  12433. else
  12434. script_pushint(st,0);
  12435. return SCRIPT_CMD_SUCCESS;
  12436. }
  12437. switch(type){
  12438. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  12439. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  12440. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  12441. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  12442. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  12443. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  12444. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  12445. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  12446. default:
  12447. script_pushint(st,0);
  12448. break;
  12449. }
  12450. return SCRIPT_CMD_SUCCESS;
  12451. }
  12452. /*==========================================
  12453. * Get your homunculus info: gethominfo <type>{,<char_id>};
  12454. * type -> 0:hom_id 1:class 2:name
  12455. * 3:friendly 4:hungry, 5: rename flag.
  12456. * 6: level, 7: game id
  12457. *------------------------------------------*/
  12458. BUILDIN_FUNC(gethominfo)
  12459. {
  12460. TBL_PC *sd=script_rid2sd(st);
  12461. TBL_HOM *hd;
  12462. int type=script_getnum(st,2);
  12463. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  12464. if (type == 2)
  12465. script_pushconststr(st,"null");
  12466. else
  12467. script_pushint(st,0);
  12468. return SCRIPT_CMD_SUCCESS;
  12469. }
  12470. switch(type){
  12471. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12472. case 1: script_pushint(st,hd->homunculus.class_); break;
  12473. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12474. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12475. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12476. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12477. case 6: script_pushint(st,hd->homunculus.level); break;
  12478. case 7: script_pushint(st,hd->bl.id); break;
  12479. default:
  12480. script_pushint(st,0);
  12481. break;
  12482. }
  12483. return SCRIPT_CMD_SUCCESS;
  12484. }
  12485. /// Retrieves information about character's mercenary
  12486. /// getmercinfo <type>[,<char id>];
  12487. BUILDIN_FUNC(getmercinfo)
  12488. {
  12489. int type;
  12490. struct map_session_data* sd;
  12491. struct mercenary_data* md;
  12492. type = script_getnum(st,2);
  12493. if( script_hasdata(st,3) )
  12494. {
  12495. uint32 char_id = script_getnum(st,3);
  12496. if( ( sd = map_charid2sd(char_id) ) == NULL )
  12497. {
  12498. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  12499. script_pushnil(st);
  12500. return SCRIPT_CMD_FAILURE;
  12501. }
  12502. }
  12503. else
  12504. {
  12505. if( ( sd = script_rid2sd(st) ) == NULL )
  12506. {
  12507. script_pushnil(st);
  12508. return SCRIPT_CMD_SUCCESS;
  12509. }
  12510. }
  12511. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12512. switch( type )
  12513. {
  12514. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12515. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12516. case 2:
  12517. if( md )
  12518. script_pushstrcopy(st,md->db->name);
  12519. else
  12520. script_pushconststr(st,"");
  12521. break;
  12522. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12523. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12524. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12525. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12526. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12527. case 8: script_pushint(st,md ? md->bl.id : 0); break;
  12528. default:
  12529. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12530. script_pushnil(st);
  12531. return SCRIPT_CMD_FAILURE;
  12532. }
  12533. return SCRIPT_CMD_SUCCESS;
  12534. }
  12535. /*==========================================
  12536. * Shows wether your inventory(and equips) contain
  12537. selected card or not.
  12538. checkequipedcard(4001);
  12539. *------------------------------------------*/
  12540. BUILDIN_FUNC(checkequipedcard)
  12541. {
  12542. TBL_PC *sd=script_rid2sd(st);
  12543. if(sd){
  12544. int n,i,c=0;
  12545. c=script_getnum(st,2);
  12546. for(i=0;i<MAX_INVENTORY;i++){
  12547. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  12548. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12549. continue;
  12550. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12551. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  12552. script_pushint(st,1);
  12553. return SCRIPT_CMD_SUCCESS;
  12554. }
  12555. }
  12556. }
  12557. }
  12558. }
  12559. script_pushint(st,0);
  12560. return SCRIPT_CMD_SUCCESS;
  12561. }
  12562. BUILDIN_FUNC(jump_zero)
  12563. {
  12564. int sel;
  12565. sel=script_getnum(st,2);
  12566. if(!sel) {
  12567. int pos;
  12568. if( !data_islabel(script_getdata(st,3)) ){
  12569. ShowError("script: jump_zero: not label !\n");
  12570. st->state=END;
  12571. return SCRIPT_CMD_FAILURE;
  12572. }
  12573. pos=script_getnum(st,3);
  12574. st->pos=pos;
  12575. st->state=GOTO;
  12576. }
  12577. return SCRIPT_CMD_SUCCESS;
  12578. }
  12579. /*==========================================
  12580. * movenpc [MouseJstr]
  12581. *------------------------------------------*/
  12582. BUILDIN_FUNC(movenpc)
  12583. {
  12584. TBL_NPC *nd = NULL;
  12585. const char *npc;
  12586. int x,y;
  12587. npc = script_getstr(st,2);
  12588. x = script_getnum(st,3);
  12589. y = script_getnum(st,4);
  12590. if ((nd = npc_name2id(npc)) == NULL){
  12591. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12592. return -1;
  12593. }
  12594. if (script_hasdata(st,5))
  12595. nd->ud.dir = script_getnum(st,5) % 8;
  12596. npc_movenpc(nd, x, y);
  12597. return SCRIPT_CMD_SUCCESS;
  12598. }
  12599. /*==========================================
  12600. * message [MouseJstr]
  12601. *------------------------------------------*/
  12602. BUILDIN_FUNC(message)
  12603. {
  12604. const char *msg,*player;
  12605. TBL_PC *pl_sd = NULL;
  12606. player = script_getstr(st,2);
  12607. msg = script_getstr(st,3);
  12608. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  12609. return SCRIPT_CMD_SUCCESS;
  12610. clif_displaymessage(pl_sd->fd, msg);
  12611. return SCRIPT_CMD_SUCCESS;
  12612. }
  12613. /*==========================================
  12614. * npctalk (sends message to surrounding area)
  12615. *------------------------------------------*/
  12616. BUILDIN_FUNC(npctalk)
  12617. {
  12618. struct npc_data* nd = NULL;
  12619. const char* str = script_getstr(st,2);
  12620. if (script_hasdata(st, 3))
  12621. nd = npc_name2id(script_getstr(st, 3));
  12622. else
  12623. nd = (struct npc_data *)map_id2bl(st->oid);
  12624. if (nd != NULL) {
  12625. char message[256];
  12626. safesnprintf(message, sizeof(message), "%s", str);
  12627. clif_disp_overhead(&nd->bl, message);
  12628. }
  12629. return SCRIPT_CMD_SUCCESS;
  12630. }
  12631. // change npc walkspeed [Valaris]
  12632. BUILDIN_FUNC(npcspeed)
  12633. {
  12634. struct npc_data* nd;
  12635. int speed;
  12636. speed = script_getnum(st,2);
  12637. nd =(struct npc_data *)map_id2bl(st->oid);
  12638. if( nd ) {
  12639. nd->speed = speed;
  12640. nd->ud.state.speed_changed = 1;
  12641. }
  12642. return SCRIPT_CMD_SUCCESS;
  12643. }
  12644. // make an npc walk to a position [Valaris]
  12645. BUILDIN_FUNC(npcwalkto)
  12646. {
  12647. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12648. int x=0,y=0;
  12649. x=script_getnum(st,2);
  12650. y=script_getnum(st,3);
  12651. if(nd) {
  12652. if (!nd->status.hp)
  12653. status_calc_npc(nd, SCO_FIRST);
  12654. else
  12655. status_calc_npc(nd, SCO_NONE);
  12656. unit_walktoxy(&nd->bl,x,y,0);
  12657. }
  12658. return SCRIPT_CMD_SUCCESS;
  12659. }
  12660. // stop an npc's movement [Valaris]
  12661. BUILDIN_FUNC(npcstop)
  12662. {
  12663. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12664. if(nd) {
  12665. unit_stop_walking(&nd->bl,1|4);
  12666. }
  12667. return SCRIPT_CMD_SUCCESS;
  12668. }
  12669. /**
  12670. * getlook(<type>{,<char_id>})
  12671. **/
  12672. BUILDIN_FUNC(getlook)
  12673. {
  12674. int type,val;
  12675. TBL_PC *sd;
  12676. if (!script_charid2sd(3,sd)) {
  12677. script_pushint(st,-1);
  12678. return SCRIPT_CMD_FAILURE;
  12679. }
  12680. type=script_getnum(st,2);
  12681. val=-1;
  12682. switch(type) {
  12683. // TODO: implement LOOK_BASE as stated in script doc
  12684. case LOOK_HAIR: val=sd->status.hair; break; //1
  12685. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12686. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12687. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12688. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12689. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12690. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12691. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12692. case LOOK_SHOES: break; //9
  12693. case LOOK_ROBE: val=sd->status.robe; break; //12
  12694. case LOOK_BODY2: val=sd->status.body; break; //13
  12695. }
  12696. script_pushint(st,val);
  12697. return SCRIPT_CMD_SUCCESS;
  12698. }
  12699. /**
  12700. * getsavepoint(<information type>{,<char_id>})
  12701. * @param type 0- map name, 1- x, 2- y
  12702. **/
  12703. BUILDIN_FUNC(getsavepoint)
  12704. {
  12705. TBL_PC* sd;
  12706. int type;
  12707. if (!script_charid2sd(3,sd)) {
  12708. script_pushint(st,0);
  12709. return SCRIPT_CMD_SUCCESS;
  12710. }
  12711. type = script_getnum(st,2);
  12712. switch(type) {
  12713. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12714. case 1: script_pushint(st,sd->status.save_point.x); break;
  12715. case 2: script_pushint(st,sd->status.save_point.y); break;
  12716. default:
  12717. script_pushint(st,0);
  12718. break;
  12719. }
  12720. return SCRIPT_CMD_SUCCESS;
  12721. }
  12722. /**
  12723. * Get position for BL objects.
  12724. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  12725. * @param mapname: String variable for output map name
  12726. * @param x: Integer variable for output coord X
  12727. * @param y: Integer variable for output coord Y
  12728. * @param type: Type of object
  12729. * UNITTYPE_PC - Character coord
  12730. * UNITTYPE_NPC - NPC coord
  12731. * UNITTYPE_PET - Pet coord
  12732. * UNITTYPE_HOM - Homun coord
  12733. * UNITTYPE_MER - Mercenary coord
  12734. * UNITTYPE_ELEM - Elemental coord
  12735. * @param charname: Name object. If empty or "this" use the current object
  12736. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  12737. */
  12738. BUILDIN_FUNC(getmapxy)
  12739. {
  12740. struct block_list *bl = NULL;
  12741. TBL_PC *sd=NULL;
  12742. int64 num;
  12743. const char *name;
  12744. char prefix;
  12745. int x,y,type;
  12746. char mapname[MAP_NAME_LENGTH];
  12747. if( !data_isreference(script_getdata(st,2)) ) {
  12748. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  12749. script_pushint(st,-1);
  12750. return SCRIPT_CMD_FAILURE;
  12751. }
  12752. if( !data_isreference(script_getdata(st,3)) ) {
  12753. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  12754. script_pushint(st,-1);
  12755. return SCRIPT_CMD_FAILURE;
  12756. }
  12757. if( !data_isreference(script_getdata(st,4)) ) {
  12758. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  12759. script_pushint(st,-1);
  12760. return SCRIPT_CMD_FAILURE;
  12761. }
  12762. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  12763. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  12764. script_pushint(st,-1);
  12765. return SCRIPT_CMD_FAILURE;
  12766. }
  12767. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  12768. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  12769. script_pushint(st,-1);
  12770. return SCRIPT_CMD_FAILURE;
  12771. }
  12772. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  12773. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  12774. script_pushint(st,-1);
  12775. return SCRIPT_CMD_FAILURE;
  12776. }
  12777. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12778. type=script_getnum(st,5);
  12779. switch (type) {
  12780. case UNITTYPE_PC: //Get Character Position
  12781. if( script_hasdata(st,6) )
  12782. sd=map_nick2sd(script_getstr(st,6));
  12783. else
  12784. sd=script_rid2sd(st);
  12785. if (sd)
  12786. bl = &sd->bl;
  12787. break;
  12788. case UNITTYPE_NPC: //Get NPC Position
  12789. if( script_hasdata(st,6) )
  12790. {
  12791. struct npc_data *nd;
  12792. nd=npc_name2id(script_getstr(st,6));
  12793. if (nd)
  12794. bl = &nd->bl;
  12795. } else //In case the origin is not an npc?
  12796. bl=map_id2bl(st->oid);
  12797. break;
  12798. case UNITTYPE_PET: //Get Pet Position
  12799. if(script_hasdata(st,6))
  12800. sd=map_nick2sd(script_getstr(st,6));
  12801. else
  12802. sd=script_rid2sd(st);
  12803. if (sd && sd->pd)
  12804. bl = &sd->pd->bl;
  12805. break;
  12806. case UNITTYPE_HOM: //Get Homun Position
  12807. if(script_hasdata(st,6))
  12808. sd=map_nick2sd(script_getstr(st,6));
  12809. else
  12810. sd=script_rid2sd(st);
  12811. if (sd && sd->hd)
  12812. bl = &sd->hd->bl;
  12813. break;
  12814. case UNITTYPE_MER: //Get Mercenary Position
  12815. if(script_hasdata(st,6))
  12816. sd=map_nick2sd(script_getstr(st,6));
  12817. else
  12818. sd=script_rid2sd(st);
  12819. if (sd && sd->md)
  12820. bl = &sd->md->bl;
  12821. break;
  12822. case UNITTYPE_ELEM: //Get Elemental Position
  12823. if(script_hasdata(st,6))
  12824. sd=map_nick2sd(script_getstr(st,6));
  12825. else
  12826. sd=script_rid2sd(st);
  12827. if (sd && sd->ed)
  12828. bl = &sd->ed->bl;
  12829. break;
  12830. default:
  12831. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  12832. script_pushint(st,-1);
  12833. return SCRIPT_CMD_FAILURE;
  12834. }
  12835. if (!bl) { //No object found.
  12836. script_pushint(st,-1);
  12837. return SCRIPT_CMD_SUCCESS;
  12838. }
  12839. x= bl->x;
  12840. y= bl->y;
  12841. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12842. //Set MapName$
  12843. num=st->stack->stack_data[st->start+2].u.num;
  12844. name=get_str(script_getvarid(num));
  12845. prefix=*name;
  12846. if(not_server_variable(prefix))
  12847. sd=script_rid2sd(st);
  12848. else
  12849. sd=NULL;
  12850. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12851. //Set MapX
  12852. num=st->stack->stack_data[st->start+3].u.num;
  12853. name=get_str(script_getvarid(num));
  12854. prefix=*name;
  12855. if(not_server_variable(prefix))
  12856. sd=script_rid2sd(st);
  12857. else
  12858. sd=NULL;
  12859. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12860. //Set MapY
  12861. num=st->stack->stack_data[st->start+4].u.num;
  12862. name=get_str(script_getvarid(num));
  12863. prefix=*name;
  12864. if(not_server_variable(prefix))
  12865. sd=script_rid2sd(st);
  12866. else
  12867. sd=NULL;
  12868. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12869. //Return Success value
  12870. script_pushint(st,0);
  12871. return SCRIPT_CMD_SUCCESS;
  12872. }
  12873. /// Returns the map name of given map ID.
  12874. ///
  12875. /// mapid2name <map ID>;
  12876. BUILDIN_FUNC(mapid2name)
  12877. {
  12878. uint16 m = script_getnum(st, 2);
  12879. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  12880. script_pushstr(st, "");
  12881. return SCRIPT_CMD_FAILURE;
  12882. }
  12883. script_pushstrcopy(st, map_mapid2mapname(m));
  12884. return SCRIPT_CMD_SUCCESS;
  12885. }
  12886. /*==========================================
  12887. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12888. *------------------------------------------*/
  12889. BUILDIN_FUNC(logmes)
  12890. {
  12891. const char *str;
  12892. TBL_PC* sd;
  12893. sd = script_rid2sd(st);
  12894. if( sd == NULL )
  12895. return SCRIPT_CMD_FAILURE;
  12896. str = script_getstr(st,2);
  12897. log_npc(sd,str);
  12898. return SCRIPT_CMD_SUCCESS;
  12899. }
  12900. BUILDIN_FUNC(summon)
  12901. {
  12902. int _class, timeout=0;
  12903. const char *str,*event="";
  12904. TBL_PC *sd;
  12905. struct mob_data *md;
  12906. int tick = gettick();
  12907. sd=script_rid2sd(st);
  12908. if (!sd)
  12909. return SCRIPT_CMD_SUCCESS;
  12910. str =script_getstr(st,2);
  12911. _class=script_getnum(st,3);
  12912. if( script_hasdata(st,4) )
  12913. timeout=script_getnum(st,4);
  12914. if( script_hasdata(st,5) ){
  12915. event=script_getstr(st,5);
  12916. check_event(st, event);
  12917. }
  12918. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12919. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12920. if (md) {
  12921. md->master_id=sd->bl.id;
  12922. md->special_state.ai = AI_ATTACK;
  12923. if( md->deletetimer != INVALID_TIMER )
  12924. delete_timer(md->deletetimer, mob_timer_delete);
  12925. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12926. mob_spawn (md); //Now it is ready for spawning.
  12927. clif_specialeffect(&md->bl,344,AREA);
  12928. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12929. }
  12930. script_pushint(st, md->bl.id);
  12931. return SCRIPT_CMD_SUCCESS;
  12932. }
  12933. /*==========================================
  12934. * Checks whether it is daytime/nighttime
  12935. *------------------------------------------*/
  12936. BUILDIN_FUNC(isnight)
  12937. {
  12938. script_pushint(st,(night_flag == 1));
  12939. return SCRIPT_CMD_SUCCESS;
  12940. }
  12941. BUILDIN_FUNC(isday)
  12942. {
  12943. script_pushint(st,(night_flag == 0));
  12944. return SCRIPT_CMD_SUCCESS;
  12945. }
  12946. /*================================================
  12947. * Check how many items/cards in the list are
  12948. * equipped - used for 2/15's cards patch [celest]
  12949. *------------------------------------------------*/
  12950. BUILDIN_FUNC(isequippedcnt)
  12951. {
  12952. TBL_PC *sd;
  12953. int i, id = 1;
  12954. int ret = 0;
  12955. sd = script_rid2sd(st);
  12956. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12957. script_pushint(st,0);
  12958. return SCRIPT_CMD_SUCCESS;
  12959. }
  12960. for (i=0; id!=0; i++) {
  12961. short j;
  12962. FETCH (i+2, id) else id = 0;
  12963. if (id <= 0)
  12964. continue;
  12965. for (j=0; j<EQI_MAX; j++) {
  12966. short index = sd->equip_index[j];
  12967. if(index < 0)
  12968. continue;
  12969. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  12970. continue;
  12971. if(!sd->inventory_data[index])
  12972. continue;
  12973. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  12974. if (sd->inventory_data[index]->nameid == id)
  12975. ret+= sd->inventory.u.items_inventory[index].amount;
  12976. } else { //Count cards.
  12977. short k;
  12978. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  12979. continue; //No cards
  12980. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12981. if (sd->inventory.u.items_inventory[index].card[k] == id)
  12982. ret++; //[Lupus]
  12983. }
  12984. }
  12985. }
  12986. }
  12987. script_pushint(st,ret);
  12988. return SCRIPT_CMD_SUCCESS;
  12989. }
  12990. /*================================================
  12991. * Check whether another card has been
  12992. * equipped - used for 2/15's cards patch [celest]
  12993. * -- Items checked cannot be reused in another
  12994. * card set to prevent exploits
  12995. *------------------------------------------------*/
  12996. BUILDIN_FUNC(isequipped)
  12997. {
  12998. TBL_PC *sd;
  12999. int i, id = 1;
  13000. int ret = -1;
  13001. //Original hash to reverse it when full check fails.
  13002. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  13003. sd = script_rid2sd(st);
  13004. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13005. script_pushint(st,0);
  13006. return SCRIPT_CMD_SUCCESS;
  13007. }
  13008. setitem_hash = sd->bonus.setitem_hash;
  13009. setitem_hash2 = sd->bonus.setitem_hash2;
  13010. for (i=0; id!=0; i++) {
  13011. int flag = 0;
  13012. short j;
  13013. FETCH (i+2, id) else id = 0;
  13014. if (id <= 0)
  13015. continue;
  13016. for (j=0; j<EQI_MAX; j++) {
  13017. short index = sd->equip_index[j];
  13018. if(index < 0)
  13019. continue;
  13020. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  13021. continue;
  13022. if(!sd->inventory_data[index])
  13023. continue;
  13024. if (itemdb_type(id) != IT_CARD) {
  13025. if (sd->inventory_data[index]->nameid != id)
  13026. continue;
  13027. flag = 1;
  13028. break;
  13029. } else { //Cards
  13030. short k;
  13031. if (sd->inventory_data[index]->slot == 0 ||
  13032. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13033. continue;
  13034. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  13035. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13036. unsigned int hash = 0;
  13037. if (sd->inventory.u.items_inventory[index].card[k] != id)
  13038. continue;
  13039. hash = 1<<((j<5?j:j-5)*4 + k);
  13040. // check if card is already used by another set
  13041. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13042. continue;
  13043. // We have found a match
  13044. flag = 1;
  13045. // Set hash so this card cannot be used by another
  13046. if (j<5)
  13047. sd->bonus.setitem_hash |= hash;
  13048. else
  13049. sd->bonus.setitem_hash2 |= hash;
  13050. break;
  13051. }
  13052. }
  13053. if (flag) break; //Card found
  13054. }
  13055. if (ret == -1)
  13056. ret = flag;
  13057. else
  13058. ret &= flag;
  13059. if (!ret) break;
  13060. }
  13061. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13062. sd->bonus.setitem_hash = setitem_hash;
  13063. sd->bonus.setitem_hash2 = setitem_hash2;
  13064. }
  13065. script_pushint(st,ret);
  13066. return SCRIPT_CMD_SUCCESS;
  13067. }
  13068. /*================================================
  13069. * Check how many given inserted cards in the CURRENT
  13070. * weapon - used for 2/15's cards patch [Lupus]
  13071. *------------------------------------------------*/
  13072. BUILDIN_FUNC(cardscnt)
  13073. {
  13074. TBL_PC *sd;
  13075. int i, k, id = 1;
  13076. int ret = 0;
  13077. int index;
  13078. sd = script_rid2sd(st);
  13079. for (i=0; id!=0; i++) {
  13080. FETCH (i+2, id) else id = 0;
  13081. if (id <= 0)
  13082. continue;
  13083. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  13084. if(index < 0) continue;
  13085. if(!sd->inventory_data[index])
  13086. continue;
  13087. if(itemdb_type(id) != IT_CARD) {
  13088. if (sd->inventory_data[index]->nameid == id)
  13089. ret+= sd->inventory.u.items_inventory[index].amount;
  13090. } else {
  13091. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13092. continue;
  13093. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13094. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13095. ret++;
  13096. }
  13097. }
  13098. }
  13099. script_pushint(st,ret);
  13100. // script_pushint(st,current_equip_item_index);
  13101. return SCRIPT_CMD_SUCCESS;
  13102. }
  13103. /*=======================================================
  13104. * Returns the refined number of the current item, or an
  13105. * item with inventory index specified
  13106. *-------------------------------------------------------*/
  13107. BUILDIN_FUNC(getrefine)
  13108. {
  13109. TBL_PC *sd;
  13110. if ((sd = script_rid2sd(st))!= NULL)
  13111. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13112. else
  13113. script_pushint(st,0);
  13114. return SCRIPT_CMD_SUCCESS;
  13115. }
  13116. /*=======================================================
  13117. * Day/Night controls
  13118. *-------------------------------------------------------*/
  13119. BUILDIN_FUNC(night)
  13120. {
  13121. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13122. return SCRIPT_CMD_SUCCESS;
  13123. }
  13124. BUILDIN_FUNC(day)
  13125. {
  13126. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13127. return SCRIPT_CMD_SUCCESS;
  13128. }
  13129. /**
  13130. * unequip <equipment slot>{,<char_id>};
  13131. * @author [Spectre]
  13132. **/
  13133. BUILDIN_FUNC(unequip) {
  13134. int pos;
  13135. TBL_PC *sd;
  13136. if (!script_charid2sd(3,sd))
  13137. return SCRIPT_CMD_FAILURE;
  13138. pos = script_getnum(st,2);
  13139. if (equip_index_check(pos)) {
  13140. short i = pc_checkequip(sd,equip_bitmask[pos]);
  13141. if (i >= 0) {
  13142. pc_unequipitem(sd,i,1|2);
  13143. script_pushint(st, 1);
  13144. return SCRIPT_CMD_SUCCESS;
  13145. }
  13146. }
  13147. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13148. script_pushint(st, 0);
  13149. return SCRIPT_CMD_FAILURE;
  13150. }
  13151. /**
  13152. * equip <item id>{,<char_id>};
  13153. **/
  13154. BUILDIN_FUNC(equip) {
  13155. unsigned short nameid = 0;
  13156. TBL_PC *sd;
  13157. struct item_data *item_data;
  13158. if (!script_charid2sd(3,sd))
  13159. return SCRIPT_CMD_FAILURE;
  13160. nameid = script_getnum(st,2);
  13161. if ((item_data = itemdb_exists(nameid))) {
  13162. int i;
  13163. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  13164. if (i < MAX_INVENTORY) {
  13165. pc_equipitem(sd,i,item_data->equip);
  13166. script_pushint(st,1);
  13167. return SCRIPT_CMD_SUCCESS;
  13168. }
  13169. }
  13170. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13171. script_pushint(st,0);
  13172. return SCRIPT_CMD_FAILURE;
  13173. }
  13174. BUILDIN_FUNC(autoequip)
  13175. {
  13176. unsigned short nameid;
  13177. int flag;
  13178. struct item_data *item_data;
  13179. nameid=script_getnum(st,2);
  13180. flag=script_getnum(st,3);
  13181. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13182. {
  13183. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13184. return SCRIPT_CMD_FAILURE;
  13185. }
  13186. if( !itemdb_isequip2(item_data) )
  13187. {
  13188. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13189. return SCRIPT_CMD_FAILURE;
  13190. }
  13191. item_data->flag.autoequip = flag>0?1:0;
  13192. return SCRIPT_CMD_SUCCESS;
  13193. }
  13194. BUILDIN_FUNC(setbattleflag)
  13195. {
  13196. const char *flag, *value;
  13197. flag = script_getstr(st,2);
  13198. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13199. if (battle_set_value(flag, value) == 0)
  13200. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13201. else
  13202. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13203. return SCRIPT_CMD_SUCCESS;
  13204. }
  13205. BUILDIN_FUNC(getbattleflag)
  13206. {
  13207. const char *flag;
  13208. flag = script_getstr(st,2);
  13209. script_pushint(st,battle_get_value(flag));
  13210. return SCRIPT_CMD_SUCCESS;
  13211. }
  13212. //=======================================================
  13213. // strlen [Valaris]
  13214. //-------------------------------------------------------
  13215. BUILDIN_FUNC(getstrlen)
  13216. {
  13217. const char *str = script_getstr(st,2);
  13218. int len = (str) ? (int)strlen(str) : 0;
  13219. script_pushint(st,len);
  13220. return SCRIPT_CMD_SUCCESS;
  13221. }
  13222. //=======================================================
  13223. // isalpha [Valaris]
  13224. //-------------------------------------------------------
  13225. BUILDIN_FUNC(charisalpha)
  13226. {
  13227. const char *str=script_getstr(st,2);
  13228. int pos=script_getnum(st,3);
  13229. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13230. script_pushint(st,val);
  13231. return SCRIPT_CMD_SUCCESS;
  13232. }
  13233. //=======================================================
  13234. // charisupper <str>, <index>
  13235. //-------------------------------------------------------
  13236. BUILDIN_FUNC(charisupper)
  13237. {
  13238. const char *str = script_getstr(st,2);
  13239. int pos = script_getnum(st,3);
  13240. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13241. script_pushint(st,val);
  13242. return SCRIPT_CMD_SUCCESS;
  13243. }
  13244. //=======================================================
  13245. // charislower <str>, <index>
  13246. //-------------------------------------------------------
  13247. BUILDIN_FUNC(charislower)
  13248. {
  13249. const char *str = script_getstr(st,2);
  13250. int pos = script_getnum(st,3);
  13251. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13252. script_pushint(st,val);
  13253. return SCRIPT_CMD_SUCCESS;
  13254. }
  13255. //=======================================================
  13256. // charat <str>, <index>
  13257. //-------------------------------------------------------
  13258. BUILDIN_FUNC(charat) {
  13259. const char *str = script_getstr(st,2);
  13260. int pos = script_getnum(st,3);
  13261. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13262. char output[2];
  13263. output[0] = str[pos];
  13264. output[1] = '\0';
  13265. script_pushstrcopy(st, output);
  13266. } else
  13267. script_pushconststr(st, "");
  13268. return SCRIPT_CMD_SUCCESS;
  13269. }
  13270. //=======================================================
  13271. // setchar <string>, <char>, <index>
  13272. //-------------------------------------------------------
  13273. BUILDIN_FUNC(setchar)
  13274. {
  13275. const char *str = script_getstr(st,2);
  13276. const char *c = script_getstr(st,3);
  13277. int index = script_getnum(st,4);
  13278. char *output = aStrdup(str);
  13279. if(index >= 0 && index < strlen(output))
  13280. output[index] = *c;
  13281. script_pushstr(st, output);
  13282. return SCRIPT_CMD_SUCCESS;
  13283. }
  13284. //=======================================================
  13285. // insertchar <string>, <char>, <index>
  13286. //-------------------------------------------------------
  13287. BUILDIN_FUNC(insertchar)
  13288. {
  13289. const char *str = script_getstr(st,2);
  13290. const char *c = script_getstr(st,3);
  13291. int index = script_getnum(st,4);
  13292. char *output;
  13293. size_t len = strlen(str);
  13294. if(index < 0)
  13295. index = 0;
  13296. else if(index > len)
  13297. index = len;
  13298. output = (char*)aMalloc(len + 2);
  13299. memcpy(output, str, index);
  13300. output[index] = c[0];
  13301. memcpy(&output[index+1], &str[index], len - index);
  13302. output[len+1] = '\0';
  13303. script_pushstr(st, output);
  13304. return SCRIPT_CMD_SUCCESS;
  13305. }
  13306. //=======================================================
  13307. // delchar <string>, <index>
  13308. //-------------------------------------------------------
  13309. BUILDIN_FUNC(delchar)
  13310. {
  13311. const char *str = script_getstr(st,2);
  13312. int index = script_getnum(st,3);
  13313. char *output;
  13314. size_t len = strlen(str);
  13315. if(index < 0 || index > len) {
  13316. //return original
  13317. output = aStrdup(str);
  13318. script_pushstr(st, output);
  13319. return SCRIPT_CMD_SUCCESS;
  13320. }
  13321. output = (char*)aMalloc(len);
  13322. memcpy(output, str, index);
  13323. memcpy(&output[index], &str[index+1], len - index);
  13324. script_pushstr(st, output);
  13325. return SCRIPT_CMD_SUCCESS;
  13326. }
  13327. //=======================================================
  13328. // strtoupper <str>
  13329. //-------------------------------------------------------
  13330. BUILDIN_FUNC(strtoupper)
  13331. {
  13332. const char *str = script_getstr(st,2);
  13333. char *output = aStrdup(str);
  13334. char *cursor = output;
  13335. while (*cursor != '\0') {
  13336. *cursor = TOUPPER(*cursor);
  13337. cursor++;
  13338. }
  13339. script_pushstr(st, output);
  13340. return SCRIPT_CMD_SUCCESS;
  13341. }
  13342. //=======================================================
  13343. // strtolower <str>
  13344. //-------------------------------------------------------
  13345. BUILDIN_FUNC(strtolower)
  13346. {
  13347. const char *str = script_getstr(st,2);
  13348. char *output = aStrdup(str);
  13349. char *cursor = output;
  13350. while (*cursor != '\0') {
  13351. *cursor = TOLOWER(*cursor);
  13352. cursor++;
  13353. }
  13354. script_pushstr(st, output);
  13355. return SCRIPT_CMD_SUCCESS;
  13356. }
  13357. //=======================================================
  13358. // substr <str>, <start>, <end>
  13359. //-------------------------------------------------------
  13360. BUILDIN_FUNC(substr)
  13361. {
  13362. const char *str = script_getstr(st,2);
  13363. char *output;
  13364. int start = script_getnum(st,3);
  13365. int end = script_getnum(st,4);
  13366. int len = 0;
  13367. if(start >= 0 && end < strlen(str) && start <= end) {
  13368. len = end - start + 1;
  13369. output = (char*)aMalloc(len + 1);
  13370. memcpy(output, &str[start], len);
  13371. } else
  13372. output = (char*)aMalloc(1);
  13373. output[len] = '\0';
  13374. script_pushstr(st, output);
  13375. return SCRIPT_CMD_SUCCESS;
  13376. }
  13377. //=======================================================
  13378. // explode <dest_string_array>, <str>, <delimiter>
  13379. // Note: delimiter is limited to 1 char
  13380. //-------------------------------------------------------
  13381. BUILDIN_FUNC(explode)
  13382. {
  13383. struct script_data* data = script_getdata(st, 2);
  13384. const char *str = script_getstr(st,3);
  13385. const char delimiter = script_getstr(st, 4)[0];
  13386. int32 id;
  13387. size_t len = strlen(str);
  13388. int i = 0, j = 0;
  13389. int start;
  13390. char *temp;
  13391. const char* name;
  13392. TBL_PC* sd = NULL;
  13393. temp = (char*)aMalloc(len + 1);
  13394. if( !data_isreference(data) ) {
  13395. ShowError("script:explode: not a variable\n");
  13396. script_reportdata(data);
  13397. st->state = END;
  13398. return SCRIPT_CMD_FAILURE;// not a variable
  13399. }
  13400. id = reference_getid(data);
  13401. start = reference_getindex(data);
  13402. name = reference_getname(data);
  13403. if( !is_string_variable(name) ) {
  13404. ShowError("script:explode: not string array\n");
  13405. script_reportdata(data);
  13406. st->state = END;
  13407. return SCRIPT_CMD_FAILURE;// data type mismatch
  13408. }
  13409. if( not_server_variable(*name) ) {
  13410. sd = script_rid2sd(st);
  13411. if( sd == NULL )
  13412. return SCRIPT_CMD_SUCCESS;// no player attached
  13413. }
  13414. while(str[i] != '\0') {
  13415. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13416. temp[j] = '\0';
  13417. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13418. j = 0;
  13419. ++i;
  13420. } else {
  13421. temp[j++] = str[i++];
  13422. }
  13423. }
  13424. //set last string
  13425. temp[j] = '\0';
  13426. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13427. aFree(temp);
  13428. return SCRIPT_CMD_SUCCESS;
  13429. }
  13430. //=======================================================
  13431. // implode <string_array>
  13432. // implode <string_array>, <glue>
  13433. //-------------------------------------------------------
  13434. BUILDIN_FUNC(implode)
  13435. {
  13436. struct script_data* data = script_getdata(st, 2);
  13437. const char *name;
  13438. uint32 glue_len = 0, array_size, id;
  13439. char *output;
  13440. TBL_PC* sd = NULL;
  13441. if( !data_isreference(data) ) {
  13442. ShowError("script:implode: not a variable\n");
  13443. script_reportdata(data);
  13444. st->state = END;
  13445. return SCRIPT_CMD_FAILURE;// not a variable
  13446. }
  13447. id = reference_getid(data);
  13448. name = reference_getname(data);
  13449. if( !is_string_variable(name) ) {
  13450. ShowError("script:implode: not string array\n");
  13451. script_reportdata(data);
  13452. st->state = END;
  13453. return SCRIPT_CMD_FAILURE;// data type mismatch
  13454. }
  13455. if( not_server_variable(*name) ) {
  13456. sd = script_rid2sd(st);
  13457. if( sd == NULL )
  13458. return SCRIPT_CMD_SUCCESS;// no player attached
  13459. }
  13460. //count chars
  13461. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  13462. if(array_size == -1) { //empty array check (AmsTaff)
  13463. ShowWarning("script:implode: array length = 0\n");
  13464. output = (char*)aMalloc(sizeof(char)*5);
  13465. sprintf(output,"%s","NULL");
  13466. } else {
  13467. const char *glue = NULL, *temp;
  13468. size_t len = 0;
  13469. int i, k = 0;
  13470. for(i = 0; i <= array_size; ++i) {
  13471. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13472. len += strlen(temp);
  13473. script_removetop(st, -1, 0);
  13474. }
  13475. //allocate mem
  13476. if( script_hasdata(st,3) ) {
  13477. glue = script_getstr(st,3);
  13478. glue_len = strlen(glue);
  13479. len += glue_len * (array_size);
  13480. }
  13481. output = (char*)aMalloc(len + 1);
  13482. //build output
  13483. for(i = 0; i < array_size; ++i) {
  13484. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13485. len = strlen(temp);
  13486. memcpy(&output[k], temp, len);
  13487. k += len;
  13488. if(glue_len != 0) {
  13489. memcpy(&output[k], glue, glue_len);
  13490. k += glue_len;
  13491. }
  13492. script_removetop(st, -1, 0);
  13493. }
  13494. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13495. len = strlen(temp);
  13496. memcpy(&output[k], temp, len);
  13497. k += len;
  13498. script_removetop(st, -1, 0);
  13499. output[k] = '\0';
  13500. }
  13501. script_pushstr(st, output);
  13502. return SCRIPT_CMD_SUCCESS;
  13503. }
  13504. //=======================================================
  13505. // sprintf(<format>, ...);
  13506. // Implements C sprintf, except format %n. The resulting string is
  13507. // returned, instead of being saved in variable by reference.
  13508. //-------------------------------------------------------
  13509. BUILDIN_FUNC(sprintf)
  13510. {
  13511. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13512. const char* format;
  13513. char* p;
  13514. char* q;
  13515. char* buf = NULL;
  13516. char* buf2 = NULL;
  13517. struct script_data* data;
  13518. StringBuf final_buf;
  13519. // Fetch init data
  13520. format = script_getstr(st, 2);
  13521. argc = script_lastdata(st)-2;
  13522. len = strlen(format);
  13523. // Skip parsing, where no parsing is required.
  13524. if(len == 0) {
  13525. script_pushconststr(st,"");
  13526. return SCRIPT_CMD_SUCCESS;
  13527. }
  13528. // Pessimistic alloc
  13529. CREATE(buf, char, len+1);
  13530. // Need not be parsed, just solve stuff like %%.
  13531. if(argc == 0) {
  13532. memcpy(buf,format,len+1);
  13533. script_pushstrcopy(st, buf);
  13534. aFree(buf);
  13535. return SCRIPT_CMD_SUCCESS;
  13536. }
  13537. safestrncpy(buf, format, len+1);
  13538. // Issue sprintf for each parameter
  13539. StringBuf_Init(&final_buf);
  13540. q = buf;
  13541. while((p = strchr(q, '%')) != NULL) {
  13542. if(p != q) {
  13543. len = p - q + 1;
  13544. if(buf2_len < len) {
  13545. RECREATE(buf2, char, len);
  13546. buf2_len = len;
  13547. }
  13548. safestrncpy(buf2, q, len);
  13549. StringBuf_AppendStr(&final_buf, buf2);
  13550. q = p;
  13551. }
  13552. p = q + 1;
  13553. if(*p == '%') { // %%
  13554. StringBuf_AppendStr(&final_buf, "%");
  13555. q+=2;
  13556. continue;
  13557. }
  13558. if(*p == 'n') { // %n
  13559. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13560. script_reportsrc(st);
  13561. q+=2;
  13562. continue;
  13563. }
  13564. if(arg >= argc) {
  13565. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13566. if(buf) aFree(buf);
  13567. if(buf2) aFree(buf2);
  13568. StringBuf_Destroy(&final_buf);
  13569. script_pushconststr(st,"");
  13570. return SCRIPT_CMD_FAILURE;
  13571. }
  13572. if((p = strchr(q+1, '%')) == NULL)
  13573. p = strchr(q, 0); // EOS
  13574. len = p - q + 1;
  13575. if(buf2_len < len) {
  13576. RECREATE(buf2, char, len);
  13577. buf2_len = len;
  13578. }
  13579. safestrncpy(buf2, q, len);
  13580. q = p;
  13581. // Note: This assumes the passed value being the correct
  13582. // type to the current format specifier. If not, the server
  13583. // probably crashes or returns anything else, than expected,
  13584. // but it would behave in normal code the same way so it's
  13585. // the scripter's responsibility.
  13586. data = script_getdata(st, arg+3);
  13587. if(data_isstring(data)) // String
  13588. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13589. else if(data_isint(data)) // Number
  13590. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13591. else if(data_isreference(data)) { // Variable
  13592. char* name = reference_getname(data);
  13593. if(name[strlen(name)-1]=='$') // var Str
  13594. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13595. else // var Int
  13596. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13597. } else { // Unsupported type
  13598. ShowError("buildin_sprintf: Unknown argument type!\n");
  13599. if(buf) aFree(buf);
  13600. if(buf2) aFree(buf2);
  13601. StringBuf_Destroy(&final_buf);
  13602. script_pushconststr(st,"");
  13603. return SCRIPT_CMD_FAILURE;
  13604. }
  13605. arg++;
  13606. }
  13607. // Append anything left
  13608. if(*q)
  13609. StringBuf_AppendStr(&final_buf, q);
  13610. // Passed more, than needed
  13611. if(arg < argc) {
  13612. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13613. script_reportsrc(st);
  13614. }
  13615. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13616. if(buf) aFree(buf);
  13617. if(buf2) aFree(buf2);
  13618. StringBuf_Destroy(&final_buf);
  13619. return SCRIPT_CMD_SUCCESS;
  13620. }
  13621. //=======================================================
  13622. // sscanf(<str>, <format>, ...);
  13623. // Implements C sscanf.
  13624. //-------------------------------------------------------
  13625. BUILDIN_FUNC(sscanf){
  13626. unsigned int argc, arg = 0, len;
  13627. struct script_data* data;
  13628. struct map_session_data* sd = NULL;
  13629. const char* str;
  13630. const char* format;
  13631. const char* p;
  13632. const char* q;
  13633. char* buf = NULL;
  13634. char* buf_p;
  13635. char* ref_str = NULL;
  13636. int ref_int;
  13637. // Get data
  13638. str = script_getstr(st, 2);
  13639. format = script_getstr(st, 3);
  13640. argc = script_lastdata(st)-3;
  13641. len = strlen(format);
  13642. if (len != 0 && strlen(str) == 0) {
  13643. // If the source string is empty but the format string is not, we return -1
  13644. // according to the C specs. (if the format string is also empty, we shall
  13645. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13646. script_pushint(st, -1);
  13647. return SCRIPT_CMD_SUCCESS;
  13648. }
  13649. CREATE(buf, char, len*2+1);
  13650. // Issue sscanf for each parameter
  13651. *buf = 0;
  13652. q = format;
  13653. while((p = strchr(q, '%'))){
  13654. if(p!=q){
  13655. strncat(buf, q, (size_t)(p-q));
  13656. q = p;
  13657. }
  13658. p = q+1;
  13659. if(*p=='*' || *p=='%'){ // Skip
  13660. strncat(buf, q, 2);
  13661. q+=2;
  13662. continue;
  13663. }
  13664. if(arg>=argc){
  13665. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13666. script_pushint(st, -1);
  13667. if(buf) aFree(buf);
  13668. if(ref_str) aFree(ref_str);
  13669. return SCRIPT_CMD_FAILURE;
  13670. }
  13671. if((p = strchr(q+1, '%'))==NULL){
  13672. p = strchr(q, 0); // EOS
  13673. }
  13674. len = p-q;
  13675. strncat(buf, q, len);
  13676. q = p;
  13677. // Validate output
  13678. data = script_getdata(st, arg+4);
  13679. if(!data_isreference(data) || !reference_tovariable(data)){
  13680. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13681. script_pushint(st, -1);
  13682. if(buf) aFree(buf);
  13683. if(ref_str) aFree(ref_str);
  13684. return SCRIPT_CMD_FAILURE;
  13685. }
  13686. buf_p = reference_getname(data);
  13687. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13688. script_pushint(st, -1);
  13689. if(buf) aFree(buf);
  13690. if(ref_str) aFree(ref_str);
  13691. return SCRIPT_CMD_SUCCESS;
  13692. }
  13693. // Save value if any
  13694. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13695. if(ref_str==NULL){
  13696. CREATE(ref_str, char, strlen(str)+1);
  13697. }
  13698. if(sscanf(str, buf, ref_str)==0){
  13699. break;
  13700. }
  13701. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13702. } else { // Number
  13703. if(sscanf(str, buf, &ref_int)==0){
  13704. break;
  13705. }
  13706. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13707. }
  13708. arg++;
  13709. // Disable used format (%... -> %*...)
  13710. buf_p = strchr(buf, 0);
  13711. memmove(buf_p-len+2, buf_p-len+1, len);
  13712. *(buf_p-len+1) = '*';
  13713. }
  13714. script_pushint(st, arg);
  13715. if(buf) aFree(buf);
  13716. if(ref_str) aFree(ref_str);
  13717. return SCRIPT_CMD_SUCCESS;
  13718. }
  13719. //=======================================================
  13720. // strpos(<haystack>, <needle>)
  13721. // strpos(<haystack>, <needle>, <offset>)
  13722. //
  13723. // Implements PHP style strpos. Adapted from code from
  13724. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13725. //-------------------------------------------------------
  13726. BUILDIN_FUNC(strpos) {
  13727. const char *haystack = script_getstr(st,2);
  13728. const char *needle = script_getstr(st,3);
  13729. int i;
  13730. size_t len;
  13731. if( script_hasdata(st,4) )
  13732. i = script_getnum(st,4);
  13733. else
  13734. i = 0;
  13735. if (needle[0] == '\0') {
  13736. script_pushint(st, -1);
  13737. return SCRIPT_CMD_SUCCESS;
  13738. }
  13739. len = strlen(haystack);
  13740. for ( ; i < len; ++i ) {
  13741. if ( haystack[i] == *needle ) {
  13742. // matched starting char -- loop through remaining chars
  13743. const char *h, *n;
  13744. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13745. if ( *h != *n ) {
  13746. break;
  13747. }
  13748. }
  13749. if ( !*n ) { // matched all of 'needle' to null termination
  13750. script_pushint(st, i);
  13751. return SCRIPT_CMD_SUCCESS;
  13752. }
  13753. }
  13754. }
  13755. script_pushint(st, -1);
  13756. return SCRIPT_CMD_SUCCESS;
  13757. }
  13758. //===============================================================
  13759. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13760. //
  13761. // Note: Finds all instances of <search> in <input> and replaces
  13762. // with <replace>. If specified will only replace as many
  13763. // instances as specified in <count>. By default will be case
  13764. // sensitive.
  13765. //---------------------------------------------------------------
  13766. BUILDIN_FUNC(replacestr)
  13767. {
  13768. const char *input = script_getstr(st, 2);
  13769. const char *find = script_getstr(st, 3);
  13770. const char *replace = script_getstr(st, 4);
  13771. size_t inputlen = strlen(input);
  13772. size_t findlen = strlen(find);
  13773. struct StringBuf output;
  13774. bool usecase = true;
  13775. int count = 0;
  13776. int numFinds = 0;
  13777. int i = 0, f = 0;
  13778. if(findlen == 0) {
  13779. ShowError("script:replacestr: Invalid search length.\n");
  13780. st->state = END;
  13781. return SCRIPT_CMD_FAILURE;
  13782. }
  13783. if(script_hasdata(st, 5)) {
  13784. struct script_data *data = script_getdata(st, 5);
  13785. get_val(st, data); // Convert into value in case of a variable
  13786. if( !data_isstring(data) )
  13787. usecase = script_getnum(st, 5) != 0;
  13788. else {
  13789. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13790. st->state = END;
  13791. return SCRIPT_CMD_FAILURE;
  13792. }
  13793. }
  13794. if(script_hasdata(st, 6)) {
  13795. count = script_getnum(st, 6);
  13796. if(count == 0) {
  13797. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13798. st->state = END;
  13799. return SCRIPT_CMD_FAILURE;
  13800. }
  13801. }
  13802. StringBuf_Init(&output);
  13803. for(; i < inputlen; i++) {
  13804. if(count && count == numFinds) { //found enough, stop looking
  13805. break;
  13806. }
  13807. for(f = 0; f <= findlen; f++) {
  13808. if(f == findlen) { //complete match
  13809. numFinds++;
  13810. StringBuf_AppendStr(&output, replace);
  13811. i += findlen - 1;
  13812. break;
  13813. } else {
  13814. if(usecase) {
  13815. if((i + f) > inputlen || input[i + f] != find[f]) {
  13816. StringBuf_Printf(&output, "%c", input[i]);
  13817. break;
  13818. }
  13819. } else {
  13820. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13821. StringBuf_Printf(&output, "%c", input[i]);
  13822. break;
  13823. }
  13824. }
  13825. }
  13826. }
  13827. }
  13828. //append excess after enough found
  13829. if(i < inputlen)
  13830. StringBuf_AppendStr(&output, &(input[i]));
  13831. script_pushstrcopy(st, StringBuf_Value(&output));
  13832. StringBuf_Destroy(&output);
  13833. return SCRIPT_CMD_SUCCESS;
  13834. }
  13835. //========================================================
  13836. // countstr <input>, <search>{, <usecase>}
  13837. //
  13838. // Note: Counts the number of times <search> occurs in
  13839. // <input>. By default will be case sensitive.
  13840. //--------------------------------------------------------
  13841. BUILDIN_FUNC(countstr)
  13842. {
  13843. const char *input = script_getstr(st, 2);
  13844. const char *find = script_getstr(st, 3);
  13845. size_t inputlen = strlen(input);
  13846. size_t findlen = strlen(find);
  13847. bool usecase = true;
  13848. int numFinds = 0;
  13849. int i = 0, f = 0;
  13850. if(findlen == 0) {
  13851. ShowError("script:countstr: Invalid search length.\n");
  13852. st->state = END;
  13853. return SCRIPT_CMD_FAILURE;
  13854. }
  13855. if(script_hasdata(st, 4)) {
  13856. struct script_data *data;
  13857. data = script_getdata(st, 4);
  13858. get_val(st, data); // Convert into value in case of a variable
  13859. if( !data_isstring(data) )
  13860. usecase = script_getnum(st, 4) != 0;
  13861. else {
  13862. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13863. st->state = END;
  13864. return SCRIPT_CMD_FAILURE;
  13865. }
  13866. }
  13867. for(; i < inputlen; i++) {
  13868. for(f = 0; f <= findlen; f++) {
  13869. if(f == findlen) { //complete match
  13870. numFinds++;
  13871. i += findlen - 1;
  13872. break;
  13873. } else {
  13874. if(usecase) {
  13875. if((i + f) > inputlen || input[i + f] != find[f]) {
  13876. break;
  13877. }
  13878. } else {
  13879. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13880. break;
  13881. }
  13882. }
  13883. }
  13884. }
  13885. }
  13886. script_pushint(st, numFinds);
  13887. return SCRIPT_CMD_SUCCESS;
  13888. }
  13889. /// Changes the display name and/or display class of the npc.
  13890. /// Returns 0 is successful, 1 if the npc does not exist.
  13891. ///
  13892. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13893. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13894. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13895. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13896. BUILDIN_FUNC(setnpcdisplay)
  13897. {
  13898. const char* name;
  13899. const char* newname = NULL;
  13900. int class_ = -1, size = -1;
  13901. struct script_data* data;
  13902. struct npc_data* nd;
  13903. name = script_getstr(st,2);
  13904. data = script_getdata(st,3);
  13905. if( script_hasdata(st,4) )
  13906. class_ = script_getnum(st,4);
  13907. if( script_hasdata(st,5) )
  13908. size = script_getnum(st,5);
  13909. get_val(st, data);
  13910. if( data_isstring(data) )
  13911. newname = conv_str(st,data);
  13912. else if( data_isint(data) )
  13913. class_ = conv_num(st,data);
  13914. else
  13915. {
  13916. ShowError("script:setnpcdisplay: expected string or number\n");
  13917. script_reportdata(data);
  13918. return SCRIPT_CMD_FAILURE;
  13919. }
  13920. nd = npc_name2id(name);
  13921. if( nd == NULL )
  13922. {// not found
  13923. script_pushint(st,1);
  13924. return SCRIPT_CMD_SUCCESS;
  13925. }
  13926. // update npc
  13927. if( newname )
  13928. npc_setdisplayname(nd, newname);
  13929. if( size != -1 && size != (int)nd->size )
  13930. nd->size = size;
  13931. else
  13932. size = -1;
  13933. if( class_ != -1 && nd->class_ != class_ )
  13934. npc_setclass(nd, class_);
  13935. else if( size != -1 )
  13936. { // Required to update the visual size
  13937. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13938. clif_spawn(&nd->bl);
  13939. }
  13940. script_pushint(st,0);
  13941. return SCRIPT_CMD_SUCCESS;
  13942. }
  13943. BUILDIN_FUNC(atoi)
  13944. {
  13945. const char *value;
  13946. value = script_getstr(st,2);
  13947. script_pushint(st,atoi(value));
  13948. return SCRIPT_CMD_SUCCESS;
  13949. }
  13950. BUILDIN_FUNC(axtoi)
  13951. {
  13952. const char *hex = script_getstr(st,2);
  13953. long value = strtol(hex, NULL, 16);
  13954. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13955. value = cap_value(value, INT_MIN, INT_MAX);
  13956. #endif
  13957. script_pushint(st, (int)value);
  13958. return SCRIPT_CMD_SUCCESS;
  13959. }
  13960. BUILDIN_FUNC(strtol)
  13961. {
  13962. const char *string = script_getstr(st, 2);
  13963. int base = script_getnum(st, 3);
  13964. long value = strtol(string, NULL, base);
  13965. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13966. value = cap_value(value, INT_MIN, INT_MAX);
  13967. #endif
  13968. script_pushint(st, (int)value);
  13969. return SCRIPT_CMD_SUCCESS;
  13970. }
  13971. // case-insensitive substring search [lordalfa]
  13972. BUILDIN_FUNC(compare)
  13973. {
  13974. const char *message;
  13975. const char *cmpstring;
  13976. message = script_getstr(st,2);
  13977. cmpstring = script_getstr(st,3);
  13978. script_pushint(st,(stristr(message,cmpstring) != NULL));
  13979. return SCRIPT_CMD_SUCCESS;
  13980. }
  13981. BUILDIN_FUNC(strcmp)
  13982. {
  13983. const char *str1;
  13984. const char *str2;
  13985. str1 = script_getstr(st,2);
  13986. str2 = script_getstr(st,3);
  13987. script_pushint(st,strcmp(str1, str2));
  13988. return SCRIPT_CMD_SUCCESS;
  13989. }
  13990. // [zBuffer] List of mathematics commands --->
  13991. BUILDIN_FUNC(sqrt)
  13992. {
  13993. double i, a;
  13994. i = script_getnum(st,2);
  13995. a = sqrt(i);
  13996. script_pushint(st,(int)a);
  13997. return SCRIPT_CMD_SUCCESS;
  13998. }
  13999. BUILDIN_FUNC(pow)
  14000. {
  14001. double i, a, b;
  14002. a = script_getnum(st,2);
  14003. b = script_getnum(st,3);
  14004. i = pow(a,b);
  14005. script_pushint(st,(int)i);
  14006. return SCRIPT_CMD_SUCCESS;
  14007. }
  14008. BUILDIN_FUNC(distance)
  14009. {
  14010. int x0, y0, x1, y1;
  14011. x0 = script_getnum(st,2);
  14012. y0 = script_getnum(st,3);
  14013. x1 = script_getnum(st,4);
  14014. y1 = script_getnum(st,5);
  14015. script_pushint(st,distance_xy(x0,y0,x1,y1));
  14016. return SCRIPT_CMD_SUCCESS;
  14017. }
  14018. // <--- [zBuffer] List of mathematics commands
  14019. BUILDIN_FUNC(md5)
  14020. {
  14021. const char *tmpstr;
  14022. char *md5str;
  14023. tmpstr = script_getstr(st,2);
  14024. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14025. MD5_String(tmpstr, md5str);
  14026. script_pushstr(st, md5str);
  14027. return SCRIPT_CMD_SUCCESS;
  14028. }
  14029. // [zBuffer] List of dynamic var commands --->
  14030. /**
  14031. * setd "<variable name>",<value>{,<char_id>};
  14032. **/
  14033. BUILDIN_FUNC(setd)
  14034. {
  14035. TBL_PC *sd = NULL;
  14036. char varname[100];
  14037. const char *buffer;
  14038. int elem;
  14039. buffer = script_getstr(st, 2);
  14040. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14041. elem = 0;
  14042. if( not_server_variable(*varname) ) {
  14043. if (!script_charid2sd(4,sd))
  14044. return SCRIPT_CMD_FAILURE;
  14045. }
  14046. if( is_string_variable(varname) ) {
  14047. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  14048. } else {
  14049. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  14050. }
  14051. return SCRIPT_CMD_SUCCESS;
  14052. }
  14053. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14054. {
  14055. int i, j;
  14056. TBL_PC* sd = NULL;
  14057. const char* query;
  14058. struct script_data* data;
  14059. const char* name;
  14060. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14061. int num_vars;
  14062. int num_cols;
  14063. // check target variables
  14064. for( i = 3; script_hasdata(st,i); ++i ) {
  14065. data = script_getdata(st, i);
  14066. if( data_isreference(data) ) { // it's a variable
  14067. name = reference_getname(data);
  14068. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14069. sd = script_rid2sd(st);
  14070. if( sd == NULL ) { // no player attached
  14071. script_reportdata(data);
  14072. st->state = END;
  14073. return SCRIPT_CMD_FAILURE;
  14074. }
  14075. }
  14076. } else {
  14077. ShowError("script:query_sql: not a variable\n");
  14078. script_reportdata(data);
  14079. st->state = END;
  14080. return SCRIPT_CMD_FAILURE;
  14081. }
  14082. }
  14083. num_vars = i - 3;
  14084. // Execute the query
  14085. query = script_getstr(st,2);
  14086. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14087. Sql_ShowDebug(handle);
  14088. script_pushint(st, -1);
  14089. return SCRIPT_CMD_FAILURE;
  14090. }
  14091. if( Sql_NumRows(handle) == 0 ) { // No data received
  14092. Sql_FreeResult(handle);
  14093. script_pushint(st, 0);
  14094. return SCRIPT_CMD_SUCCESS;
  14095. }
  14096. // Count the number of columns to store
  14097. num_cols = Sql_NumColumns(handle);
  14098. if( num_vars < num_cols ) {
  14099. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14100. script_reportsrc(st);
  14101. } else if( num_vars > num_cols ) {
  14102. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14103. script_reportsrc(st);
  14104. }
  14105. // Store data
  14106. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14107. for( j = 0; j < num_vars; ++j ) {
  14108. char* str = NULL;
  14109. if( j < num_cols )
  14110. Sql_GetData(handle, j, &str, NULL);
  14111. data = script_getdata(st, j+3);
  14112. name = reference_getname(data);
  14113. if( is_string_variable(name) )
  14114. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  14115. else
  14116. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  14117. }
  14118. }
  14119. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14120. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14121. script_reportsrc(st);
  14122. }
  14123. // Free data
  14124. Sql_FreeResult(handle);
  14125. script_pushint(st, i);
  14126. return SCRIPT_CMD_SUCCESS;
  14127. }
  14128. BUILDIN_FUNC(query_sql) {
  14129. #ifdef BETA_THREAD_TEST
  14130. if( st->state != RERUNLINE ) {
  14131. queryThread_add(st,false);
  14132. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14133. } else
  14134. st->state = RUN;
  14135. return SCRIPT_CMD_SUCCESS;
  14136. #else
  14137. return buildin_query_sql_sub(st, qsmysql_handle);
  14138. #endif
  14139. }
  14140. BUILDIN_FUNC(query_logsql) {
  14141. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14142. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14143. script_pushint(st,-1);
  14144. return SCRIPT_CMD_FAILURE;
  14145. }
  14146. #ifdef BETA_THREAD_TEST
  14147. if( st->state != RERUNLINE ) {
  14148. queryThread_add(st,true);
  14149. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14150. } else
  14151. st->state = RUN;
  14152. return SCRIPT_CMD_SUCCESS;
  14153. #else
  14154. return buildin_query_sql_sub(st, logmysql_handle);
  14155. #endif
  14156. }
  14157. //Allows escaping of a given string.
  14158. BUILDIN_FUNC(escape_sql)
  14159. {
  14160. const char *str;
  14161. char *esc_str;
  14162. size_t len;
  14163. str = script_getstr(st,2);
  14164. len = strlen(str);
  14165. esc_str = (char*)aMalloc(len*2+1);
  14166. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14167. script_pushstr(st, esc_str);
  14168. return SCRIPT_CMD_SUCCESS;
  14169. }
  14170. BUILDIN_FUNC(getd)
  14171. {
  14172. char varname[100];
  14173. const char *buffer;
  14174. int elem;
  14175. buffer = script_getstr(st, 2);
  14176. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14177. elem = 0;
  14178. // Push the 'pointer' so it's more flexible [Lance]
  14179. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14180. return SCRIPT_CMD_SUCCESS;
  14181. }
  14182. BUILDIN_FUNC(callshop)
  14183. {
  14184. TBL_PC *sd = NULL;
  14185. struct npc_data *nd;
  14186. const char *shopname;
  14187. int flag = 0;
  14188. sd = script_rid2sd(st);
  14189. if (!sd) {
  14190. script_pushint(st,0);
  14191. return SCRIPT_CMD_SUCCESS;
  14192. }
  14193. shopname = script_getstr(st, 2);
  14194. if (script_hasdata(st,3))
  14195. flag = script_getnum(st,3);
  14196. nd = npc_name2id(shopname);
  14197. 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) ) {
  14198. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14199. script_pushint(st,0);
  14200. return SCRIPT_CMD_FAILURE;
  14201. }
  14202. if (nd->subtype == NPCTYPE_SHOP) {
  14203. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14204. sd->state.callshop = 1;
  14205. switch (flag) {
  14206. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14207. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14208. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14209. }
  14210. }
  14211. #if PACKETVER >= 20131223
  14212. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14213. unsigned short i;
  14214. for (i = 0; i < nd->u.shop.count; i++) {
  14215. if (nd->u.shop.shop_item[i].qty)
  14216. break;
  14217. }
  14218. if (i == nd->u.shop.count) {
  14219. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 534));
  14220. return SCRIPT_CMD_FAILURE;
  14221. }
  14222. sd->npc_shopid = nd->bl.id;
  14223. clif_npc_market_open(sd, nd);
  14224. script_pushint(st,1);
  14225. return SCRIPT_CMD_SUCCESS;
  14226. }
  14227. #endif
  14228. else
  14229. clif_cashshop_show(sd, nd);
  14230. sd->npc_shopid = nd->bl.id;
  14231. script_pushint(st,1);
  14232. return SCRIPT_CMD_SUCCESS;
  14233. }
  14234. BUILDIN_FUNC(npcshopitem)
  14235. {
  14236. const char* npcname = script_getstr(st, 2);
  14237. struct npc_data* nd = npc_name2id(npcname);
  14238. int n, i;
  14239. int amount;
  14240. uint16 offs = 2;
  14241. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14242. script_pushint(st,0);
  14243. return SCRIPT_CMD_SUCCESS;
  14244. }
  14245. #if PACKETVER >= 20131223
  14246. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14247. offs = 3;
  14248. npc_market_delfromsql_(nd->exname, 0, true);
  14249. }
  14250. #endif
  14251. // get the count of new entries
  14252. amount = (script_lastdata(st)-2)/offs;
  14253. // generate new shop item list
  14254. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14255. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14256. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14257. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14258. #if PACKETVER >= 20131223
  14259. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14260. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14261. nd->u.shop.shop_item[n].flag = 1;
  14262. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14263. }
  14264. #endif
  14265. }
  14266. nd->u.shop.count = n;
  14267. script_pushint(st,1);
  14268. return SCRIPT_CMD_SUCCESS;
  14269. }
  14270. BUILDIN_FUNC(npcshopadditem)
  14271. {
  14272. const char* npcname = script_getstr(st,2);
  14273. struct npc_data* nd = npc_name2id(npcname);
  14274. int n, i;
  14275. uint16 offs = 2, amount;
  14276. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14277. script_pushint(st,0);
  14278. return SCRIPT_CMD_SUCCESS;
  14279. }
  14280. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14281. offs = 3;
  14282. // get the count of new entries
  14283. amount = (script_lastdata(st)-2)/offs;
  14284. #if PACKETVER >= 20131223
  14285. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14286. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14287. uint16 nameid = script_getnum(st,i), j;
  14288. // Check existing entries
  14289. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14290. if (j == nd->u.shop.count) {
  14291. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14292. j = nd->u.shop.count;
  14293. nd->u.shop.shop_item[j].flag = 1;
  14294. nd->u.shop.count++;
  14295. }
  14296. nd->u.shop.shop_item[j].nameid = nameid;
  14297. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14298. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14299. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14300. }
  14301. script_pushint(st,1);
  14302. return SCRIPT_CMD_SUCCESS;
  14303. }
  14304. #endif
  14305. // append new items to existing shop item list
  14306. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14307. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14308. {
  14309. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14310. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14311. }
  14312. nd->u.shop.count = n;
  14313. script_pushint(st,1);
  14314. return SCRIPT_CMD_SUCCESS;
  14315. }
  14316. BUILDIN_FUNC(npcshopdelitem)
  14317. {
  14318. const char* npcname = script_getstr(st,2);
  14319. struct npc_data* nd = npc_name2id(npcname);
  14320. int n, i, size;
  14321. unsigned short amount;
  14322. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14323. script_pushint(st,0);
  14324. return SCRIPT_CMD_SUCCESS;
  14325. }
  14326. amount = script_lastdata(st)-2;
  14327. size = nd->u.shop.count;
  14328. // remove specified items from the shop item list
  14329. for( i = 3; i < 3 + amount; i++ ) {
  14330. unsigned short nameid = script_getnum(st,i);
  14331. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14332. if( n < size ) {
  14333. if (n+1 != size)
  14334. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14335. #if PACKETVER >= 20131223
  14336. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14337. npc_market_delfromsql_(nd->exname, nameid, false);
  14338. #endif
  14339. size--;
  14340. }
  14341. }
  14342. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14343. nd->u.shop.count = size;
  14344. script_pushint(st,1);
  14345. return SCRIPT_CMD_SUCCESS;
  14346. }
  14347. /**
  14348. * Sets a script to attach to a shop npc.
  14349. * npcshopattach "<npc_name>";
  14350. **/
  14351. BUILDIN_FUNC(npcshopattach)
  14352. {
  14353. const char* npcname = script_getstr(st,2);
  14354. struct npc_data* nd = npc_name2id(npcname);
  14355. int flag = 1;
  14356. if( script_hasdata(st,3) )
  14357. flag = script_getnum(st,3);
  14358. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14359. script_pushint(st,0);
  14360. return SCRIPT_CMD_SUCCESS;
  14361. }
  14362. if (flag)
  14363. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14364. else
  14365. nd->master_nd = NULL;
  14366. script_pushint(st,1);
  14367. return SCRIPT_CMD_SUCCESS;
  14368. }
  14369. /*==========================================
  14370. * Returns some values of an item [Lupus]
  14371. * Price, Weight, etc...
  14372. setitemscript(itemID,"{new item bonus script}",[n]);
  14373. Where n:
  14374. 0 - script
  14375. 1 - Equip script
  14376. 2 - Unequip script
  14377. *------------------------------------------*/
  14378. BUILDIN_FUNC(setitemscript)
  14379. {
  14380. unsigned short item_id;
  14381. int n = 0;
  14382. const char *script;
  14383. struct item_data *i_data;
  14384. struct script_code **dstscript;
  14385. item_id = script_getnum(st,2);
  14386. script = script_getstr(st,3);
  14387. if( script_hasdata(st,4) )
  14388. n=script_getnum(st,4);
  14389. i_data = itemdb_exists(item_id);
  14390. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14391. script_pushint(st,0);
  14392. return SCRIPT_CMD_SUCCESS;
  14393. }
  14394. switch (n) {
  14395. case 2:
  14396. dstscript = &i_data->unequip_script;
  14397. break;
  14398. case 1:
  14399. dstscript = &i_data->equip_script;
  14400. break;
  14401. default:
  14402. dstscript = &i_data->script;
  14403. break;
  14404. }
  14405. if(*dstscript)
  14406. script_free_code(*dstscript);
  14407. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14408. script_pushint(st,1);
  14409. return SCRIPT_CMD_SUCCESS;
  14410. }
  14411. /*=======================================================
  14412. * Add or Update a mob drop temporarily [Akinari]
  14413. * Original Idea By: [Lupus]
  14414. *
  14415. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14416. *
  14417. * If given an item the mob already drops, the rate
  14418. * is updated to the new rate. Rate cannot exceed 10000
  14419. * Returns 1 if succeeded (added/updated a mob drop)
  14420. *-------------------------------------------------------*/
  14421. BUILDIN_FUNC(addmonsterdrop)
  14422. {
  14423. struct mob_db *mob;
  14424. struct script_data *data;
  14425. unsigned short item_id;
  14426. int rate;
  14427. data = script_getdata(st, 2);
  14428. get_val(st, data); // Convert into value in case of a variable
  14429. if(data_isstring(data))
  14430. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14431. else
  14432. mob = mob_db(script_getnum(st,2));
  14433. item_id=script_getnum(st,3);
  14434. rate=script_getnum(st,4);
  14435. if(!itemdb_exists(item_id)){
  14436. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14437. return SCRIPT_CMD_FAILURE;
  14438. }
  14439. if(mob) { //We got a valid monster, check for available drop slot
  14440. unsigned char i, c = 0;
  14441. for(i = 0; i < MAX_MOB_DROP; i++) {
  14442. if(mob->dropitem[i].nameid) {
  14443. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14444. c = i;
  14445. break;
  14446. }
  14447. continue;
  14448. }
  14449. if(!c) //Accept first available slot only
  14450. c = i;
  14451. }
  14452. if(c) { //Fill in the slot with the item and rate
  14453. mob->dropitem[c].nameid = item_id;
  14454. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14455. script_pushint(st,1);
  14456. } else //No place to put the new drop
  14457. script_pushint(st,0);
  14458. } else {
  14459. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14460. return SCRIPT_CMD_FAILURE;
  14461. }
  14462. return SCRIPT_CMD_SUCCESS;
  14463. }
  14464. /*=======================================================
  14465. * Delete a mob drop temporarily [Akinari]
  14466. * Original Idea By: [Lupus]
  14467. *
  14468. * delmonsterdrop <mob_id or name>,<item_id>;
  14469. *
  14470. * Returns 1 if succeeded (deleted a mob drop)
  14471. *-------------------------------------------------------*/
  14472. BUILDIN_FUNC(delmonsterdrop)
  14473. {
  14474. struct mob_db *mob;
  14475. struct script_data *data;
  14476. unsigned short item_id;
  14477. data = script_getdata(st, 2);
  14478. get_val(st, data); // Convert into value in case of a variable
  14479. if(data_isstring(data))
  14480. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14481. else
  14482. mob = mob_db(script_getnum(st,2));
  14483. item_id=script_getnum(st,3);
  14484. if(!itemdb_exists(item_id)){
  14485. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14486. return SCRIPT_CMD_FAILURE;
  14487. }
  14488. if(mob) { //We got a valid monster, check for item drop on monster
  14489. unsigned char i;
  14490. for(i = 0; i < MAX_MOB_DROP; i++) {
  14491. if(mob->dropitem[i].nameid == item_id) {
  14492. mob->dropitem[i].nameid = 0;
  14493. mob->dropitem[i].p = 0;
  14494. script_pushint(st,1);
  14495. return SCRIPT_CMD_SUCCESS;
  14496. }
  14497. }
  14498. //No drop on that monster
  14499. script_pushint(st,0);
  14500. } else {
  14501. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14502. return SCRIPT_CMD_FAILURE;
  14503. }
  14504. return SCRIPT_CMD_SUCCESS;
  14505. }
  14506. /*==========================================
  14507. * Returns some values of a monster [Lupus]
  14508. * Name, Level, race, size, etc...
  14509. getmonsterinfo(monsterID,queryIndex);
  14510. *------------------------------------------*/
  14511. BUILDIN_FUNC(getmonsterinfo)
  14512. {
  14513. struct mob_db *mob;
  14514. int mob_id;
  14515. mob_id = script_getnum(st,2);
  14516. if (!mobdb_checkid(mob_id)) {
  14517. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14518. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  14519. script_pushconststr(st,"null");
  14520. else
  14521. script_pushint(st,-1);
  14522. return SCRIPT_CMD_SUCCESS;
  14523. }
  14524. mob = mob_db(mob_id);
  14525. switch ( script_getnum(st,3) ) {
  14526. case MOB_NAME: script_pushstrcopy(st,mob->jname); break;
  14527. case MOB_LV: script_pushint(st,mob->lv); break;
  14528. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  14529. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  14530. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  14531. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  14532. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  14533. case MOB_DEF: script_pushint(st,mob->status.def); break;
  14534. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  14535. case MOB_STR: script_pushint(st,mob->status.str); break;
  14536. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  14537. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  14538. case MOB_INT: script_pushint(st,mob->status.int_); break;
  14539. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  14540. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  14541. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  14542. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  14543. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  14544. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  14545. case MOB_RACE: script_pushint(st,mob->status.race); break;
  14546. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  14547. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  14548. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  14549. default: script_pushint(st,-1); //wrong Index
  14550. }
  14551. return SCRIPT_CMD_SUCCESS;
  14552. }
  14553. /**
  14554. * Check player's vending/buyingstore/autotrade state
  14555. * checkvending({"<Player Name>"})
  14556. * @author [Nab4]
  14557. */
  14558. BUILDIN_FUNC(checkvending) {
  14559. TBL_PC *sd = NULL;
  14560. if (script_hasdata(st,2)) {
  14561. if (!(sd = map_nick2sd(script_getstr(st,2)))) {
  14562. ShowError("buildin_checkvending: Player '%s' is not online!\n", script_getstr(st,2));
  14563. return SCRIPT_CMD_FAILURE;
  14564. }
  14565. }
  14566. else
  14567. sd = script_rid2sd(st);
  14568. if (!sd) {
  14569. script_pushint(st,0);
  14570. return SCRIPT_CMD_SUCCESS;
  14571. }
  14572. else {
  14573. int8 ret = 0;
  14574. if (sd->state.vending)
  14575. ret = 1;
  14576. else if (sd->state.buyingstore)
  14577. ret = 4;
  14578. if (sd->state.autotrade)
  14579. ret |= 2;
  14580. script_pushint(st, ret);
  14581. }
  14582. return SCRIPT_CMD_SUCCESS;
  14583. }
  14584. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14585. {
  14586. TBL_PC *sd = NULL;
  14587. if(script_hasdata(st,2))
  14588. sd = map_nick2sd(script_getstr(st,2));
  14589. else
  14590. sd = script_rid2sd(st);
  14591. if(sd)
  14592. script_pushint(st,(sd->chatID != 0));
  14593. else
  14594. script_pushint(st,0);
  14595. return SCRIPT_CMD_SUCCESS;
  14596. }
  14597. BUILDIN_FUNC(checkidle)
  14598. {
  14599. TBL_PC *sd = NULL;
  14600. if (script_hasdata(st, 2))
  14601. sd = map_nick2sd(script_getstr(st, 2));
  14602. else
  14603. sd = script_rid2sd(st);
  14604. if (sd)
  14605. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14606. else
  14607. script_pushint(st, 0);
  14608. return SCRIPT_CMD_SUCCESS;
  14609. }
  14610. BUILDIN_FUNC(searchitem)
  14611. {
  14612. struct script_data* data = script_getdata(st, 2);
  14613. const char *itemname = script_getstr(st,3);
  14614. struct item_data *items[MAX_SEARCH];
  14615. int count;
  14616. char* name;
  14617. int32 start;
  14618. int32 id;
  14619. int32 i;
  14620. TBL_PC* sd = NULL;
  14621. if ((items[0] = itemdb_exists(atoi(itemname))))
  14622. count = 1;
  14623. else
  14624. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14625. if (!count) {
  14626. script_pushint(st, 0);
  14627. return SCRIPT_CMD_SUCCESS;
  14628. }
  14629. if( !data_isreference(data) )
  14630. {
  14631. ShowError("script:searchitem: not a variable\n");
  14632. script_reportdata(data);
  14633. st->state = END;
  14634. return SCRIPT_CMD_FAILURE;// not a variable
  14635. }
  14636. id = reference_getid(data);
  14637. start = reference_getindex(data);
  14638. name = reference_getname(data);
  14639. if( not_server_variable(*name) )
  14640. {
  14641. sd = script_rid2sd(st);
  14642. if( sd == NULL )
  14643. return SCRIPT_CMD_SUCCESS;// no player attached
  14644. }
  14645. if( is_string_variable(name) )
  14646. {// string array
  14647. ShowError("script:searchitem: not an integer array reference\n");
  14648. script_reportdata(data);
  14649. st->state = END;
  14650. return SCRIPT_CMD_FAILURE;// not supported
  14651. }
  14652. for( i = 0; i < count; ++start, ++i )
  14653. {// Set array
  14654. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14655. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14656. }
  14657. script_pushint(st, count);
  14658. return SCRIPT_CMD_SUCCESS;
  14659. }
  14660. // [zBuffer] List of player cont commands --->
  14661. BUILDIN_FUNC(rid2name)
  14662. {
  14663. struct block_list *bl = NULL;
  14664. int rid = script_getnum(st,2);
  14665. if((bl = map_id2bl(rid)))
  14666. {
  14667. switch(bl->type) {
  14668. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14669. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14670. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14671. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14672. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14673. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14674. default:
  14675. ShowError("buildin_rid2name: BL type unknown.\n");
  14676. script_pushconststr(st,"");
  14677. break;
  14678. }
  14679. } else {
  14680. ShowError("buildin_rid2name: invalid RID\n");
  14681. script_pushconststr(st,"(null)");
  14682. }
  14683. return SCRIPT_CMD_SUCCESS;
  14684. }
  14685. BUILDIN_FUNC(pcblockmove)
  14686. {
  14687. struct block_list *bl = NULL;
  14688. if (script_getnum(st, 2))
  14689. bl = map_id2bl(script_getnum(st,2));
  14690. else
  14691. bl = map_id2bl(st->rid);
  14692. if (bl) {
  14693. struct unit_data *ud = unit_bl2ud(bl);
  14694. if (ud)
  14695. ud->state.blockedmove = script_getnum(st,3) > 0;
  14696. }
  14697. return SCRIPT_CMD_SUCCESS;
  14698. }
  14699. BUILDIN_FUNC(pcfollow)
  14700. {
  14701. int id, targetid;
  14702. TBL_PC *sd = NULL;
  14703. id = script_getnum(st,2);
  14704. targetid = script_getnum(st,3);
  14705. if(id)
  14706. sd = map_id2sd(id);
  14707. else
  14708. sd = script_rid2sd(st);
  14709. if(sd)
  14710. pc_follow(sd, targetid);
  14711. return SCRIPT_CMD_SUCCESS;
  14712. }
  14713. BUILDIN_FUNC(pcstopfollow)
  14714. {
  14715. int id;
  14716. TBL_PC *sd = NULL;
  14717. id = script_getnum(st,2);
  14718. if(id)
  14719. sd = map_id2sd(id);
  14720. else
  14721. sd = script_rid2sd(st);
  14722. if(sd)
  14723. pc_stop_following(sd);
  14724. return SCRIPT_CMD_SUCCESS;
  14725. }
  14726. // <--- [zBuffer] List of player cont commands
  14727. // [zBuffer] List of unit control commands --->
  14728. /// Checks to see if the unit exists.
  14729. ///
  14730. /// unitexists <unit id>;
  14731. BUILDIN_FUNC(unitexists)
  14732. {
  14733. struct block_list* bl;
  14734. bl = map_id2bl(script_getnum(st, 2));
  14735. if (!bl)
  14736. script_pushint(st, false);
  14737. else
  14738. script_pushint(st, true);
  14739. return SCRIPT_CMD_SUCCESS;
  14740. }
  14741. /// Gets the type of the given Game ID.
  14742. ///
  14743. /// getunittype <unit id>;
  14744. BUILDIN_FUNC(getunittype)
  14745. {
  14746. struct block_list* bl;
  14747. uint8 value = 0;
  14748. bl = map_id2bl(script_getnum(st, 2));
  14749. if (!bl) {
  14750. ShowWarning("buildin_getunittype: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14751. script_pushint(st, -1);
  14752. return SCRIPT_CMD_FAILURE;
  14753. }
  14754. switch (bl->type) {
  14755. case BL_PC: value = UNITTYPE_PC; break;
  14756. case BL_NPC: value = UNITTYPE_NPC; break;
  14757. case BL_PET: value = UNITTYPE_PET; break;
  14758. case BL_MOB: value = UNITTYPE_MOB; break;
  14759. case BL_HOM: value = UNITTYPE_HOM; break;
  14760. case BL_MER: value = UNITTYPE_MER; break;
  14761. case BL_ELEM: value = UNITTYPE_ELEM; break;
  14762. default: value = -1; break;
  14763. }
  14764. script_pushint(st, value);
  14765. return SCRIPT_CMD_SUCCESS;
  14766. }
  14767. /// Gets specific live information of a bl.
  14768. ///
  14769. /// getunitdata <unit id>,<arrayname>;
  14770. BUILDIN_FUNC(getunitdata)
  14771. {
  14772. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14773. struct block_list* bl;
  14774. TBL_MOB* md = NULL;
  14775. TBL_HOM* hd = NULL;
  14776. TBL_MER* mc = NULL;
  14777. TBL_PET* pd = NULL;
  14778. TBL_ELEM* ed = NULL;
  14779. TBL_NPC* nd = NULL;
  14780. char* name;
  14781. struct script_data *data = script_getdata(st, 3);
  14782. if (!data_isreference(data)) {
  14783. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14784. return SCRIPT_CMD_FAILURE;
  14785. }
  14786. bl = map_id2bl(script_getnum(st, 2));
  14787. if (!bl) {
  14788. ShowWarning("buildin_getunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14789. script_pushint(st, -1);
  14790. return SCRIPT_CMD_FAILURE;
  14791. }
  14792. switch (bl->type) {
  14793. case BL_MOB: md = map_id2md(bl->id); break;
  14794. case BL_HOM: hd = map_id2hd(bl->id); break;
  14795. case BL_PET: pd = map_id2pd(bl->id); break;
  14796. case BL_MER: mc = map_id2mc(bl->id); break;
  14797. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14798. case BL_NPC: nd = map_id2nd(bl->id); break;
  14799. }
  14800. name = reference_getname(data);
  14801. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  14802. switch(bl->type) {
  14803. case BL_MOB:
  14804. if (!md) {
  14805. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14806. return SCRIPT_CMD_FAILURE;
  14807. }
  14808. getunitdata_sub(UMOB_SIZE, md->status.size);
  14809. getunitdata_sub(UMOB_LEVEL, md->level);
  14810. getunitdata_sub(UMOB_HP, md->status.hp);
  14811. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  14812. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  14813. getunitdata_sub(UMOB_MAPID, md->bl.m);
  14814. getunitdata_sub(UMOB_X, md->bl.x);
  14815. getunitdata_sub(UMOB_Y, md->bl.y);
  14816. getunitdata_sub(UMOB_SPEED, md->status.speed);
  14817. getunitdata_sub(UMOB_MODE, md->status.mode);
  14818. getunitdata_sub(UMOB_AI, md->special_state.ai);
  14819. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  14820. getunitdata_sub(UMOB_SEX, md->vd->sex);
  14821. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  14822. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  14823. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  14824. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  14825. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  14826. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  14827. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  14828. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  14829. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  14830. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  14831. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  14832. getunitdata_sub(UMOB_STR, md->status.str);
  14833. getunitdata_sub(UMOB_AGI, md->status.agi);
  14834. getunitdata_sub(UMOB_VIT, md->status.vit);
  14835. getunitdata_sub(UMOB_INT, md->status.int_);
  14836. getunitdata_sub(UMOB_DEX, md->status.dex);
  14837. getunitdata_sub(UMOB_LUK, md->status.luk);
  14838. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  14839. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  14840. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  14841. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  14842. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  14843. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  14844. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  14845. getunitdata_sub(UMOB_DEF, md->status.def);
  14846. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  14847. getunitdata_sub(UMOB_HIT, md->status.hit);
  14848. getunitdata_sub(UMOB_FLEE, md->status.flee);
  14849. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  14850. getunitdata_sub(UMOB_CRIT, md->status.cri);
  14851. getunitdata_sub(UMOB_RACE, md->status.race);
  14852. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  14853. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  14854. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  14855. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  14856. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  14857. break;
  14858. case BL_HOM:
  14859. if (!hd) {
  14860. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  14861. return SCRIPT_CMD_FAILURE;
  14862. }
  14863. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  14864. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  14865. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  14866. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  14867. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  14868. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  14869. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  14870. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  14871. getunitdata_sub(UHOM_X, hd->bl.x);
  14872. getunitdata_sub(UHOM_Y, hd->bl.y);
  14873. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  14874. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  14875. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  14876. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  14877. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  14878. getunitdata_sub(UHOM_STR, hd->base_status.str);
  14879. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  14880. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  14881. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  14882. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  14883. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  14884. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  14885. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  14886. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  14887. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  14888. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  14889. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  14890. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  14891. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  14892. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  14893. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  14894. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  14895. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  14896. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  14897. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  14898. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  14899. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  14900. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  14901. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  14902. break;
  14903. case BL_PET:
  14904. if (!pd) {
  14905. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  14906. return SCRIPT_CMD_FAILURE;
  14907. }
  14908. getunitdata_sub(UPET_SIZE, pd->status.size);
  14909. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  14910. getunitdata_sub(UPET_HP, pd->status.hp);
  14911. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  14912. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  14913. getunitdata_sub(UPET_MAPID, pd->bl.m);
  14914. getunitdata_sub(UPET_X, pd->bl.x);
  14915. getunitdata_sub(UPET_Y, pd->bl.y);
  14916. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  14917. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  14918. getunitdata_sub(UPET_SPEED, pd->status.speed);
  14919. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  14920. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  14921. getunitdata_sub(UPET_STR, pd->status.str);
  14922. getunitdata_sub(UPET_AGI, pd->status.agi);
  14923. getunitdata_sub(UPET_VIT, pd->status.vit);
  14924. getunitdata_sub(UPET_INT, pd->status.int_);
  14925. getunitdata_sub(UPET_DEX, pd->status.dex);
  14926. getunitdata_sub(UPET_LUK, pd->status.luk);
  14927. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  14928. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  14929. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  14930. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  14931. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  14932. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  14933. getunitdata_sub(UPET_DEF, pd->status.def);
  14934. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  14935. getunitdata_sub(UPET_HIT, pd->status.hit);
  14936. getunitdata_sub(UPET_FLEE, pd->status.flee);
  14937. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  14938. getunitdata_sub(UPET_CRIT, pd->status.cri);
  14939. getunitdata_sub(UPET_RACE, pd->status.race);
  14940. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  14941. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  14942. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  14943. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  14944. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  14945. break;
  14946. case BL_MER:
  14947. if (!mc) {
  14948. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  14949. return SCRIPT_CMD_FAILURE;
  14950. }
  14951. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  14952. getunitdata_sub(UMER_HP, mc->base_status.hp);
  14953. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  14954. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  14955. getunitdata_sub(UMER_MAPID, mc->bl.m);
  14956. getunitdata_sub(UMER_X, mc->bl.x);
  14957. getunitdata_sub(UMER_Y, mc->bl.y);
  14958. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  14959. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  14960. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  14961. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  14962. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  14963. getunitdata_sub(UMER_STR, mc->base_status.str);
  14964. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  14965. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  14966. getunitdata_sub(UMER_INT, mc->base_status.int_);
  14967. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  14968. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  14969. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  14970. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  14971. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  14972. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  14973. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  14974. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  14975. getunitdata_sub(UMER_DEF, mc->base_status.def);
  14976. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  14977. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  14978. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  14979. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  14980. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  14981. getunitdata_sub(UMER_RACE, mc->base_status.race);
  14982. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  14983. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  14984. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  14985. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  14986. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  14987. break;
  14988. case BL_ELEM:
  14989. if (!ed) {
  14990. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  14991. return SCRIPT_CMD_FAILURE;
  14992. }
  14993. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  14994. getunitdata_sub(UELE_HP, ed->elemental.hp);
  14995. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  14996. getunitdata_sub(UELE_SP, ed->elemental.sp);
  14997. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  14998. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  14999. getunitdata_sub(UELE_MAPID, ed->bl.m);
  15000. getunitdata_sub(UELE_X, ed->bl.x);
  15001. getunitdata_sub(UELE_Y, ed->bl.y);
  15002. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  15003. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  15004. getunitdata_sub(UELE_SP, ed->base_status.speed);
  15005. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  15006. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  15007. getunitdata_sub(UELE_STR, ed->base_status.str);
  15008. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  15009. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  15010. getunitdata_sub(UELE_INT, ed->base_status.int_);
  15011. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  15012. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  15013. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  15014. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  15015. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  15016. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  15017. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  15018. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  15019. getunitdata_sub(UELE_DEF, ed->base_status.def);
  15020. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  15021. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  15022. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  15023. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  15024. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  15025. getunitdata_sub(UELE_RACE, ed->base_status.race);
  15026. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  15027. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  15028. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  15029. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  15030. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  15031. break;
  15032. case BL_NPC:
  15033. if (!nd) {
  15034. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  15035. return SCRIPT_CMD_FAILURE;
  15036. }
  15037. getunitdata_sub(UNPC_DISPLAY, nd->class_);
  15038. getunitdata_sub(UNPC_LEVEL, nd->level);
  15039. getunitdata_sub(UNPC_HP, nd->status.hp);
  15040. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  15041. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  15042. getunitdata_sub(UNPC_X, nd->bl.x);
  15043. getunitdata_sub(UNPC_Y, nd->bl.y);
  15044. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  15045. getunitdata_sub(UNPC_STR, nd->status.str);
  15046. getunitdata_sub(UNPC_AGI, nd->status.agi);
  15047. getunitdata_sub(UNPC_VIT, nd->status.vit);
  15048. getunitdata_sub(UNPC_INT, nd->status.int_);
  15049. getunitdata_sub(UNPC_DEX, nd->status.dex);
  15050. getunitdata_sub(UNPC_LUK, nd->status.luk);
  15051. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  15052. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  15053. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  15054. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  15055. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  15056. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  15057. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  15058. getunitdata_sub(UNPC_DEF, nd->status.def);
  15059. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  15060. getunitdata_sub(UNPC_HIT, nd->status.hit);
  15061. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  15062. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  15063. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  15064. getunitdata_sub(UNPC_RACE, nd->status.race);
  15065. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  15066. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  15067. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  15068. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  15069. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  15070. break;
  15071. default:
  15072. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  15073. return SCRIPT_CMD_FAILURE;
  15074. }
  15075. #undef getunitdata_sub
  15076. return SCRIPT_CMD_SUCCESS;
  15077. }
  15078. /// Changes the live data of a bl.
  15079. ///
  15080. /// setunitdata <unit id>,<type>,<value>;
  15081. BUILDIN_FUNC(setunitdata)
  15082. {
  15083. struct block_list* bl = NULL;
  15084. struct script_data* data;
  15085. const char *mapname = NULL;
  15086. TBL_MOB* md = NULL;
  15087. TBL_HOM* hd = NULL;
  15088. TBL_MER* mc = NULL;
  15089. TBL_PET* pd = NULL;
  15090. TBL_ELEM* ed = NULL;
  15091. TBL_NPC* nd = NULL;
  15092. int type, value = 0;
  15093. bl = map_id2bl(script_getnum(st, 2));
  15094. if (!bl) {
  15095. ShowWarning("buildin_setunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15096. script_pushint(st, -1);
  15097. return SCRIPT_CMD_FAILURE;
  15098. }
  15099. switch (bl->type) {
  15100. case BL_MOB: md = map_id2md(bl->id); break;
  15101. case BL_HOM: hd = map_id2hd(bl->id); break;
  15102. case BL_PET: pd = map_id2pd(bl->id); break;
  15103. case BL_MER: mc = map_id2mc(bl->id); break;
  15104. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15105. case BL_NPC:
  15106. nd = map_id2nd(bl->id);
  15107. if (!nd->status.hp)
  15108. status_calc_npc(nd, SCO_FIRST);
  15109. else
  15110. status_calc_npc(nd, SCO_NONE);
  15111. break;
  15112. default:
  15113. ShowError("buildin_setunitdata: Invalid object!\n");
  15114. return SCRIPT_CMD_FAILURE;
  15115. }
  15116. type = script_getnum(st, 3);
  15117. data = script_getdata(st, 4);
  15118. get_val(st, data);
  15119. if (type == 5 && data_isstring(data))
  15120. mapname = conv_str(st, data);
  15121. else if (data_isint(data))
  15122. value = conv_num(st, data);
  15123. else {
  15124. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).\n", data->type);
  15125. return SCRIPT_CMD_FAILURE;
  15126. }
  15127. switch (bl->type) {
  15128. case BL_MOB:
  15129. if (!md) {
  15130. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  15131. return SCRIPT_CMD_FAILURE;
  15132. }
  15133. switch (type) {
  15134. case UMOB_SIZE: md->status.size = (unsigned char)value; break;
  15135. case UMOB_LEVEL: md->level = (unsigned short)value; break;
  15136. case UMOB_HP: status_set_hp(bl, (unsigned int)value, 0); clif_charnameack(0, &md->bl); break;
  15137. case UMOB_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); clif_charnameack(0, &md->bl); break;
  15138. case UMOB_MASTERAID: md->master_id = value; break;
  15139. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15140. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15141. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  15142. case UMOB_SPEED: md->status.speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15143. case UMOB_MODE: md->status.mode = (enum e_mode)value; break;
  15144. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15145. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15146. case UMOB_SEX: md->vd->sex = (char)value; break;
  15147. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); break;
  15148. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15149. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15150. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15151. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15152. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15153. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15154. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15155. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15156. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15157. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15158. case UMOB_STR: md->status.str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15159. case UMOB_AGI: md->status.agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15160. case UMOB_VIT: md->status.vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15161. case UMOB_INT: md->status.int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15162. case UMOB_DEX: md->status.dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15163. case UMOB_LUK: md->status.luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15164. case UMOB_SLAVECPYMSTRMD:
  15165. if (value > 0) {
  15166. TBL_MOB *md2 = NULL;
  15167. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15168. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15169. break;
  15170. }
  15171. md->status.mode = md2->status.mode;
  15172. md->state.copy_master_mode = 1;
  15173. } else
  15174. md->state.copy_master_mode = 0;
  15175. break;
  15176. case UMOB_DMGIMMUNE: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15177. case UMOB_ATKRANGE: md->status.rhw.range = (unsigned short)value; break;
  15178. case UMOB_ATKMIN: md->status.rhw.atk = (unsigned short)value; break;
  15179. case UMOB_ATKMAX: md->status.rhw.atk2 = (unsigned short)value; break;
  15180. case UMOB_MATKMIN: md->status.matk_min = (unsigned short)value; break;
  15181. case UMOB_MATKMAX: md->status.matk_max = (unsigned short)value; break;
  15182. case UMOB_DEF: md->status.def = (defType)value; break;
  15183. case UMOB_MDEF: md->status.mdef = (defType)value; break;
  15184. case UMOB_HIT: md->status.hit = (short)value; break;
  15185. case UMOB_FLEE: md->status.flee = (short)value; break;
  15186. case UMOB_PDODGE: md->status.flee2 = (short)value; break;
  15187. case UMOB_CRIT: md->status.cri = (short)value; break;
  15188. case UMOB_RACE: md->status.race = (unsigned char)value; break;
  15189. case UMOB_ELETYPE: md->status.def_ele = (unsigned char)value; break;
  15190. case UMOB_ELELEVEL: md->status.ele_lv = (unsigned char)value; break;
  15191. case UMOB_AMOTION: md->status.amotion = (short)value; break;
  15192. case UMOB_ADELAY: md->status.adelay = (short)value; break;
  15193. case UMOB_DMOTION: md->status.dmotion = (short)value; break;
  15194. default:
  15195. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15196. return SCRIPT_CMD_FAILURE;
  15197. }
  15198. break;
  15199. case BL_HOM:
  15200. if (!hd) {
  15201. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15202. return SCRIPT_CMD_FAILURE;
  15203. }
  15204. switch (type) {
  15205. case UHOM_SIZE: hd->base_status.size = (unsigned char)value; break;
  15206. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15207. case UHOM_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15208. case UHOM_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15209. case UHOM_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15210. case UHOM_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15211. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15212. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15213. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15214. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15215. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15216. 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;
  15217. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15218. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15219. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15220. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15221. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15222. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15223. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15224. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15225. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15226. case UHOM_DMGIMMUNE: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15227. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; break;
  15228. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; break;
  15229. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; break;
  15230. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; break;
  15231. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; break;
  15232. case UHOM_DEF: hd->base_status.def = (defType)value; break;
  15233. case UHOM_MDEF: hd->base_status.mdef = (defType)value; break;
  15234. case UHOM_HIT: hd->base_status.hit = (short)value; break;
  15235. case UHOM_FLEE: hd->base_status.flee = (short)value; break;
  15236. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; break;
  15237. case UHOM_CRIT: hd->base_status.cri = (short)value; break;
  15238. case UHOM_RACE: hd->base_status.race = (unsigned char)value; break;
  15239. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; break;
  15240. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; break;
  15241. case UHOM_AMOTION: hd->base_status.amotion = (short)value; break;
  15242. case UHOM_ADELAY: hd->base_status.adelay = (short)value; break;
  15243. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; break;
  15244. default:
  15245. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15246. return SCRIPT_CMD_FAILURE;
  15247. }
  15248. break;
  15249. case BL_PET:
  15250. if (!pd) {
  15251. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15252. return SCRIPT_CMD_FAILURE;
  15253. }
  15254. switch (type) {
  15255. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15256. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15257. case UPET_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15258. case UPET_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15259. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15260. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15261. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15262. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15263. case UPET_HUNGER: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  15264. 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;
  15265. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15266. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15267. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15268. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15269. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15270. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15271. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15272. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15273. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15274. case UPET_DMGIMMUNE: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15275. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15276. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15277. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15278. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15279. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15280. case UPET_DEF: pd->status.def = (defType)value; break;
  15281. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15282. case UPET_HIT: pd->status.hit = (short)value; break;
  15283. case UPET_FLEE: pd->status.flee = (short)value; break;
  15284. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  15285. case UPET_CRIT: pd->status.cri = (short)value; break;
  15286. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  15287. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  15288. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  15289. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  15290. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  15291. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  15292. default:
  15293. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15294. return SCRIPT_CMD_FAILURE;
  15295. }
  15296. break;
  15297. case BL_MER:
  15298. if (!mc) {
  15299. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15300. return SCRIPT_CMD_FAILURE;
  15301. }
  15302. switch (type) {
  15303. case UMER_SIZE: mc->base_status.size = (unsigned char)value; break;
  15304. case UMER_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15305. case UMER_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15306. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  15307. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15308. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15309. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15310. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  15311. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  15312. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15313. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15314. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15315. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15316. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15317. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15318. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15319. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15320. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15321. case UMER_DMGIMMUNE: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15322. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; break;
  15323. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; break;
  15324. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; break;
  15325. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; break;
  15326. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; break;
  15327. case UMER_DEF: mc->base_status.def = (defType)value; break;
  15328. case UMER_MDEF: mc->base_status.mdef = (defType)value; break;
  15329. case UMER_HIT: mc->base_status.hit = (short)value; break;
  15330. case UMER_FLEE: mc->base_status.flee = (short)value; break;
  15331. case UMER_PDODGE: mc->base_status.flee2 = (short)value; break;
  15332. case UMER_CRIT: mc->base_status.cri = (short)value; break;
  15333. case UMER_RACE: mc->base_status.race = (unsigned char)value; break;
  15334. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; break;
  15335. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; break;
  15336. case UMER_AMOTION: mc->base_status.amotion = (short)value; break;
  15337. case UMER_ADELAY: mc->base_status.adelay = (short)value; break;
  15338. case UMER_DMOTION: mc->base_status.dmotion = (short)value; break;
  15339. default:
  15340. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  15341. return SCRIPT_CMD_FAILURE;
  15342. }
  15343. break;
  15344. case BL_ELEM:
  15345. if (!ed) {
  15346. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  15347. return SCRIPT_CMD_FAILURE;
  15348. }
  15349. switch (type) {
  15350. case UELE_SIZE: ed->base_status.size = (unsigned char)value; break;
  15351. case UELE_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15352. case UELE_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15353. case UELE_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15354. case UELE_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15355. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  15356. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15357. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  15358. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  15359. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  15360. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; break;
  15361. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15362. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15363. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15364. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15365. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15366. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15367. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15368. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15369. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15370. case UELE_DMGIMMUNE: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15371. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; break;
  15372. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; break;
  15373. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; break;
  15374. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; break;
  15375. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; break;
  15376. case UELE_DEF: ed->base_status.def = (defType)value; break;
  15377. case UELE_MDEF: ed->base_status.mdef = (defType)value; break;
  15378. case UELE_HIT: ed->base_status.hit = (short)value; break;
  15379. case UELE_FLEE: ed->base_status.flee = (short)value; break;
  15380. case UELE_PDODGE: ed->base_status.flee2 = (short)value; break;
  15381. case UELE_CRIT: ed->base_status.cri = (short)value; break;
  15382. case UELE_RACE: ed->base_status.race = (unsigned char)value; break;
  15383. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; break;
  15384. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; break;
  15385. case UELE_AMOTION: ed->base_status.amotion = (short)value; break;
  15386. case UELE_ADELAY: ed->base_status.adelay = (short)value; break;
  15387. case UELE_DMOTION: ed->base_status.dmotion = (short)value; break;
  15388. default:
  15389. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  15390. return SCRIPT_CMD_FAILURE;
  15391. }
  15392. break;
  15393. case BL_NPC:
  15394. if (!nd) {
  15395. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  15396. return SCRIPT_CMD_FAILURE;
  15397. }
  15398. switch (type) {
  15399. case UNPC_DISPLAY: status_set_viewdata(bl, (unsigned short)value); break;
  15400. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  15401. case UNPC_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15402. case UNPC_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15403. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15404. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  15405. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  15406. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15407. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15408. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15409. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15410. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15411. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15412. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15413. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  15414. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  15415. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  15416. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  15417. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  15418. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  15419. case UNPC_DEF: nd->status.def = (defType)value; break;
  15420. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  15421. case UNPC_HIT: nd->status.hit = (short)value; break;
  15422. case UNPC_FLEE: nd->status.flee = (short)value; break;
  15423. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  15424. case UNPC_CRIT: nd->status.cri = (short)value; break;
  15425. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  15426. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  15427. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  15428. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  15429. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  15430. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  15431. default:
  15432. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  15433. return SCRIPT_CMD_FAILURE;
  15434. }
  15435. break;
  15436. default:
  15437. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  15438. return SCRIPT_CMD_FAILURE;
  15439. }
  15440. // Client information updates
  15441. switch (bl->type) {
  15442. case BL_HOM:
  15443. clif_send_homdata(hd->master, SP_ACK, 0);
  15444. break;
  15445. case BL_PET:
  15446. clif_send_petstatus(pd->master);
  15447. break;
  15448. case BL_MER:
  15449. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  15450. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  15451. break;
  15452. case BL_ELEM:
  15453. clif_elemental_info(ed->master);
  15454. break;
  15455. }
  15456. return SCRIPT_CMD_SUCCESS;
  15457. }
  15458. /// Gets the name of a bl.
  15459. /// Supported types are [MOB|HOM|PET|NPC].
  15460. /// MER and ELEM don't support custom names.
  15461. ///
  15462. /// getunitname <unit id>;
  15463. BUILDIN_FUNC(getunitname)
  15464. {
  15465. struct block_list* bl = NULL;
  15466. bl = map_id2bl(script_getnum(st, 2));
  15467. if (!bl) {
  15468. ShowWarning("buildin_getunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15469. script_pushconststr(st, "Unknown");
  15470. return SCRIPT_CMD_FAILURE;
  15471. }
  15472. script_pushstrcopy(st, status_get_name(bl));
  15473. return SCRIPT_CMD_SUCCESS;
  15474. }
  15475. /// Changes the name of a bl.
  15476. /// Supported types are [MOB|HOM|PET].
  15477. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  15478. ///
  15479. /// setunitname <unit id>,<name>;
  15480. BUILDIN_FUNC(setunitname)
  15481. {
  15482. struct block_list* bl = NULL;
  15483. TBL_MOB* md = NULL;
  15484. TBL_HOM* hd = NULL;
  15485. TBL_PET* pd = NULL;
  15486. bl = map_id2bl(script_getnum(st, 2));
  15487. if (!bl) {
  15488. ShowWarning("buildin_setunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15489. script_pushconststr(st, "Unknown");
  15490. return SCRIPT_CMD_FAILURE;
  15491. }
  15492. switch (bl->type) {
  15493. case BL_MOB: md = map_id2md(bl->id); break;
  15494. case BL_HOM: hd = map_id2hd(bl->id); break;
  15495. case BL_PET: pd = map_id2pd(bl->id); break;
  15496. }
  15497. switch (bl->type) {
  15498. case BL_MOB:
  15499. if (!md) {
  15500. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  15501. return SCRIPT_CMD_FAILURE;
  15502. }
  15503. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  15504. break;
  15505. case BL_HOM:
  15506. if (!hd) {
  15507. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  15508. return SCRIPT_CMD_FAILURE;
  15509. }
  15510. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  15511. break;
  15512. case BL_PET:
  15513. if (!pd) {
  15514. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  15515. return SCRIPT_CMD_FAILURE;
  15516. }
  15517. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  15518. break;
  15519. default:
  15520. ShowWarning("buildin_setunitname: Unknown object type!\n");
  15521. return SCRIPT_CMD_FAILURE;
  15522. }
  15523. clif_charnameack(0, bl); // Send update to client.
  15524. return SCRIPT_CMD_SUCCESS;
  15525. }
  15526. /// Makes the unit walk to target position or map.
  15527. /// Returns if it was successful.
  15528. ///
  15529. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  15530. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  15531. BUILDIN_FUNC(unitwalk)
  15532. {
  15533. struct block_list* bl;
  15534. struct unit_data *ud = NULL;
  15535. const char *cmd = script_getfuncname(st), *done_label = "";
  15536. uint8 off = 5;
  15537. bl = map_id2bl(script_getnum(st,2));
  15538. if (!bl) {
  15539. ShowError("buildin_unitwalk: Invalid unit with ID '%d'.\n", script_getnum(st,2));
  15540. script_pushint(st, 0);
  15541. return SCRIPT_CMD_FAILURE;
  15542. }
  15543. ud = unit_bl2ud(bl);
  15544. if (!strcmp(cmd,"unitwalk")) {
  15545. int x = script_getnum(st,3);
  15546. int y = script_getnum(st,4);
  15547. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  15548. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  15549. } else {
  15550. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  15551. if (!tbl) {
  15552. ShowError("buildin_unitwalk: Bad target destination.\n");
  15553. script_pushint(st, 0);
  15554. return SCRIPT_CMD_FAILURE;
  15555. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  15556. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  15557. off = 4;
  15558. }
  15559. if (ud && script_hasdata(st, off)) {
  15560. done_label = script_getstr(st, off);
  15561. check_event(st, done_label);
  15562. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  15563. }
  15564. return SCRIPT_CMD_SUCCESS;
  15565. }
  15566. /// Kills the unit.
  15567. ///
  15568. /// unitkill <unit_id>;
  15569. BUILDIN_FUNC(unitkill)
  15570. {
  15571. struct block_list* bl = map_id2bl(script_getnum(st,2));
  15572. if (bl != NULL)
  15573. status_kill(bl);
  15574. return SCRIPT_CMD_SUCCESS;
  15575. }
  15576. /// Warps the unit to the target position in the target map.
  15577. /// Returns if it was successful.
  15578. ///
  15579. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  15580. BUILDIN_FUNC(unitwarp)
  15581. {
  15582. int unit_id;
  15583. int map_idx;
  15584. short x;
  15585. short y;
  15586. struct block_list* bl;
  15587. const char *mapname;
  15588. unit_id = script_getnum(st,2);
  15589. mapname = script_getstr(st, 3);
  15590. x = (short)script_getnum(st,4);
  15591. y = (short)script_getnum(st,5);
  15592. if (!unit_id) //Warp the script's runner
  15593. bl = map_id2bl(st->rid);
  15594. else
  15595. bl = map_id2bl(unit_id);
  15596. if (!strcmp(mapname,"this"))
  15597. map_idx = bl?bl->m:-1;
  15598. else
  15599. map_idx = map_mapname2mapid(mapname);
  15600. if (map_idx >= 0 && bl != NULL)
  15601. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15602. else
  15603. script_pushint(st, 0);
  15604. return SCRIPT_CMD_SUCCESS;
  15605. }
  15606. /// Makes the unit attack the target.
  15607. /// If the unit is a player and <action type> is not 0, it does a continuous
  15608. /// attack instead of a single attack.
  15609. /// Returns if the request was successful.
  15610. ///
  15611. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15612. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15613. BUILDIN_FUNC(unitattack)
  15614. {
  15615. struct block_list* unit_bl;
  15616. struct block_list* target_bl = NULL;
  15617. struct script_data* data;
  15618. int actiontype = 0;
  15619. unit_bl = map_id2bl(script_getnum(st,2));
  15620. if (!unit_bl) {
  15621. script_pushint(st, 0);
  15622. return SCRIPT_CMD_FAILURE;
  15623. }
  15624. data = script_getdata(st, 3);
  15625. get_val(st, data);
  15626. if (data_isstring(data)) {
  15627. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  15628. if( sd != NULL )
  15629. target_bl = &sd->bl;
  15630. } else
  15631. target_bl = map_id2bl(conv_num(st, data));
  15632. if (!target_bl) {
  15633. script_pushint(st, 0);
  15634. return SCRIPT_CMD_FAILURE;
  15635. }
  15636. if (script_hasdata(st,4))
  15637. actiontype = script_getnum(st,4);
  15638. switch(unit_bl->type) {
  15639. case BL_PC:
  15640. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15641. script_pushint(st, 1);
  15642. return SCRIPT_CMD_SUCCESS;
  15643. case BL_MOB:
  15644. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15645. break;
  15646. case BL_PET:
  15647. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15648. break;
  15649. default:
  15650. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  15651. script_pushint(st, 0);
  15652. return SCRIPT_CMD_FAILURE;
  15653. }
  15654. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  15655. return SCRIPT_CMD_SUCCESS;
  15656. }
  15657. /// Makes the unit stop attacking.
  15658. ///
  15659. /// unitstopattack <unit_id>;
  15660. BUILDIN_FUNC(unitstopattack)
  15661. {
  15662. int unit_id;
  15663. struct block_list* bl;
  15664. unit_id = script_getnum(st,2);
  15665. bl = map_id2bl(unit_id);
  15666. if (bl != NULL) {
  15667. unit_stop_attack(bl);
  15668. if (bl->type == BL_MOB)
  15669. ((TBL_MOB*)bl)->target_id = 0;
  15670. }
  15671. return SCRIPT_CMD_SUCCESS;
  15672. }
  15673. /// Makes the unit stop walking.
  15674. ///
  15675. /// unitstopwalk <unit_id>;
  15676. BUILDIN_FUNC(unitstopwalk)
  15677. {
  15678. int unit_id;
  15679. struct block_list* bl;
  15680. unit_id = script_getnum(st,2);
  15681. bl = map_id2bl(unit_id);
  15682. if (bl != NULL)
  15683. unit_stop_walking(bl, 0);
  15684. return SCRIPT_CMD_SUCCESS;
  15685. }
  15686. /// Makes the unit say the given message.
  15687. ///
  15688. /// unittalk <unit_id>,"<message>";
  15689. BUILDIN_FUNC(unittalk)
  15690. {
  15691. int unit_id;
  15692. const char* message;
  15693. struct block_list* bl;
  15694. unit_id = script_getnum(st,2);
  15695. message = script_getstr(st, 3);
  15696. bl = map_id2bl(unit_id);
  15697. if (bl != NULL) {
  15698. struct StringBuf sbuf;
  15699. StringBuf_Init(&sbuf);
  15700. StringBuf_Printf(&sbuf, "%s", message);
  15701. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  15702. StringBuf_Destroy(&sbuf);
  15703. }
  15704. return SCRIPT_CMD_SUCCESS;
  15705. }
  15706. /// Makes the unit do an emotion.
  15707. ///
  15708. /// unitemote <unit_id>,<emotion>;
  15709. ///
  15710. /// @see e_* in db/const.txt
  15711. BUILDIN_FUNC(unitemote)
  15712. {
  15713. int unit_id;
  15714. int emotion;
  15715. struct block_list* bl;
  15716. unit_id = script_getnum(st,2);
  15717. emotion = script_getnum(st,3);
  15718. bl = map_id2bl(unit_id);
  15719. if (bl != NULL)
  15720. clif_emotion(bl, emotion);
  15721. return SCRIPT_CMD_SUCCESS;
  15722. }
  15723. /// Makes the unit cast the skill on the target or self if no target is specified.
  15724. ///
  15725. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  15726. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  15727. BUILDIN_FUNC(unitskilluseid)
  15728. {
  15729. int unit_id, target_id, casttime;
  15730. uint16 skill_id, skill_lv;
  15731. struct block_list* bl;
  15732. struct script_data *data;
  15733. unit_id = script_getnum(st,2);
  15734. data = script_getdata(st, 3);
  15735. get_val(st, data); // Convert into value in case of a variable
  15736. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15737. skill_lv = script_getnum(st,4);
  15738. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  15739. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  15740. bl = map_id2bl(unit_id);
  15741. if (bl != NULL) {
  15742. if (bl->type == BL_NPC) {
  15743. if (!((TBL_NPC*)bl)->status.hp)
  15744. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15745. else
  15746. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15747. }
  15748. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  15749. }
  15750. return SCRIPT_CMD_SUCCESS;
  15751. }
  15752. /// Makes the unit cast the skill on the target position.
  15753. ///
  15754. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15755. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15756. BUILDIN_FUNC(unitskillusepos)
  15757. {
  15758. int unit_id, skill_x, skill_y, casttime;
  15759. uint16 skill_id, skill_lv;
  15760. struct block_list* bl;
  15761. struct script_data *data;
  15762. unit_id = script_getnum(st,2);
  15763. data = script_getdata(st, 3);
  15764. get_val(st, data); // Convert into value in case of a variable
  15765. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15766. skill_lv = script_getnum(st,4);
  15767. skill_x = script_getnum(st,5);
  15768. skill_y = script_getnum(st,6);
  15769. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  15770. bl = map_id2bl(unit_id);
  15771. if (bl != NULL) {
  15772. if (bl->type == BL_NPC) {
  15773. if (!((TBL_NPC*)bl)->status.hp)
  15774. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15775. else
  15776. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15777. }
  15778. 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));
  15779. }
  15780. return SCRIPT_CMD_SUCCESS;
  15781. }
  15782. // <--- [zBuffer] List of unit control commands
  15783. /// Pauses the execution of the script, detaching the player
  15784. ///
  15785. /// sleep <mili seconds>;
  15786. BUILDIN_FUNC(sleep)
  15787. {
  15788. int ticks;
  15789. ticks = script_getnum(st,2);
  15790. // detach the player
  15791. script_detach_rid(st);
  15792. if( ticks <= 0 )
  15793. {// do nothing
  15794. }
  15795. else if( st->sleep.tick == 0 )
  15796. {// sleep for the target amount of time
  15797. st->state = RERUNLINE;
  15798. st->sleep.tick = ticks;
  15799. }
  15800. else
  15801. {// sleep time is over
  15802. st->state = RUN;
  15803. st->sleep.tick = 0;
  15804. }
  15805. return SCRIPT_CMD_SUCCESS;
  15806. }
  15807. /// Pauses the execution of the script, keeping the player attached
  15808. /// Returns if a player is still attached
  15809. ///
  15810. /// sleep2(<mili secconds>) -> <bool>
  15811. BUILDIN_FUNC(sleep2)
  15812. {
  15813. int ticks;
  15814. ticks = script_getnum(st,2);
  15815. if( ticks <= 0 )
  15816. {// do nothing
  15817. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15818. }
  15819. else if( !st->sleep.tick )
  15820. {// sleep for the target amount of time
  15821. st->state = RERUNLINE;
  15822. st->sleep.tick = ticks;
  15823. }
  15824. else
  15825. {// sleep time is over
  15826. st->state = RUN;
  15827. st->sleep.tick = 0;
  15828. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15829. }
  15830. return SCRIPT_CMD_SUCCESS;
  15831. }
  15832. /// Awakes all the sleep timers of the target npc
  15833. ///
  15834. /// awake "<npc name>";
  15835. BUILDIN_FUNC(awake)
  15836. {
  15837. DBIterator *iter;
  15838. struct script_state *tst;
  15839. struct npc_data* nd;
  15840. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  15841. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  15842. return SCRIPT_CMD_FAILURE;
  15843. }
  15844. iter = db_iterator(st_db);
  15845. for (tst = dbi_first(iter); dbi_exists(iter); tst = dbi_next(iter)) {
  15846. if (tst->oid == nd->bl.id) {
  15847. TBL_PC* sd = map_id2sd(tst->rid);
  15848. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  15849. continue;
  15850. }
  15851. if ((sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd)) {
  15852. // char not online anymore / another char of the same account is online - Cancel execution
  15853. tst->state = END;
  15854. tst->rid = 0;
  15855. }
  15856. delete_timer(tst->sleep.timer, run_script_timer);
  15857. tst->sleep.timer = INVALID_TIMER;
  15858. if (tst->state != RERUNLINE)
  15859. tst->sleep.tick = 0;
  15860. run_script_main(tst);
  15861. }
  15862. }
  15863. dbi_destroy(iter);
  15864. return SCRIPT_CMD_SUCCESS;
  15865. }
  15866. /// Returns a reference to a variable of the target NPC.
  15867. /// Returns 0 if an error occurs.
  15868. ///
  15869. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  15870. BUILDIN_FUNC(getvariableofnpc)
  15871. {
  15872. struct script_data* data;
  15873. const char* name;
  15874. struct npc_data* nd;
  15875. data = script_getdata(st,2);
  15876. if( !data_isreference(data) )
  15877. {// Not a reference (aka varaible name)
  15878. ShowError("buildin_getvariableofnpc: not a variable\n");
  15879. script_reportdata(data);
  15880. script_pushnil(st);
  15881. st->state = END;
  15882. return SCRIPT_CMD_FAILURE;
  15883. }
  15884. name = reference_getname(data);
  15885. if( *name != '.' || name[1] == '@' )
  15886. {// not a npc variable
  15887. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  15888. script_reportdata(data);
  15889. script_pushnil(st);
  15890. st->state = END;
  15891. return SCRIPT_CMD_FAILURE;
  15892. }
  15893. nd = npc_name2id(script_getstr(st,3));
  15894. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  15895. {// NPC not found or has no script
  15896. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  15897. script_pushnil(st);
  15898. st->state = END;
  15899. return SCRIPT_CMD_FAILURE;
  15900. }
  15901. if (!nd->u.scr.script->local.vars)
  15902. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  15903. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  15904. return SCRIPT_CMD_SUCCESS;
  15905. }
  15906. /// Opens a warp portal.
  15907. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  15908. ///
  15909. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  15910. ///
  15911. /// @author blackhole89
  15912. BUILDIN_FUNC(warpportal)
  15913. {
  15914. int spx;
  15915. int spy;
  15916. unsigned short mapindex;
  15917. int tpx;
  15918. int tpy;
  15919. struct skill_unit_group* group;
  15920. struct block_list* bl;
  15921. bl = map_id2bl(st->oid);
  15922. if( bl == NULL ) {
  15923. ShowError("buildin_warpportal: NPC is needed\n");
  15924. return SCRIPT_CMD_FAILURE;
  15925. }
  15926. spx = script_getnum(st,2);
  15927. spy = script_getnum(st,3);
  15928. mapindex = mapindex_name2id(script_getstr(st, 4));
  15929. tpx = script_getnum(st,5);
  15930. tpy = script_getnum(st,6);
  15931. if( mapindex == 0 ) {
  15932. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  15933. return SCRIPT_CMD_FAILURE;
  15934. }
  15935. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  15936. if( group == NULL )
  15937. return SCRIPT_CMD_FAILURE;// failed
  15938. group->val1 = (group->val1<<16)|(short)0;
  15939. group->val2 = (tpx<<16) | tpy;
  15940. group->val3 = mapindex;
  15941. return SCRIPT_CMD_SUCCESS;
  15942. }
  15943. /**
  15944. * openmail({<char_id>});
  15945. **/
  15946. BUILDIN_FUNC(openmail)
  15947. {
  15948. TBL_PC* sd;
  15949. if (!script_charid2sd(2,sd))
  15950. return SCRIPT_CMD_FAILURE;
  15951. mail_openmail(sd);
  15952. return SCRIPT_CMD_SUCCESS;
  15953. }
  15954. /**
  15955. * openauction({<char_id>});
  15956. **/
  15957. BUILDIN_FUNC(openauction)
  15958. {
  15959. TBL_PC* sd;
  15960. if (!script_charid2sd(2,sd))
  15961. return SCRIPT_CMD_FAILURE;
  15962. if( !battle_config.feature_auction ) {
  15963. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 517));
  15964. return SCRIPT_CMD_SUCCESS;
  15965. }
  15966. clif_Auction_openwindow(sd);
  15967. return SCRIPT_CMD_SUCCESS;
  15968. }
  15969. /// Retrieves the value of the specified flag of the specified cell.
  15970. ///
  15971. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  15972. ///
  15973. /// @see cell_chk* constants in const.txt for the types
  15974. BUILDIN_FUNC(checkcell)
  15975. {
  15976. int16 m = map_mapname2mapid(script_getstr(st,2));
  15977. int16 x = script_getnum(st,3);
  15978. int16 y = script_getnum(st,4);
  15979. cell_chk type = (cell_chk)script_getnum(st,5);
  15980. script_pushint(st, map_getcell(m, x, y, type));
  15981. return SCRIPT_CMD_SUCCESS;
  15982. }
  15983. /// Modifies flags of cells in the specified area.
  15984. ///
  15985. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  15986. ///
  15987. /// @see cell_* constants in const.txt for the types
  15988. BUILDIN_FUNC(setcell)
  15989. {
  15990. int16 m = map_mapname2mapid(script_getstr(st,2));
  15991. int16 x1 = script_getnum(st,3);
  15992. int16 y1 = script_getnum(st,4);
  15993. int16 x2 = script_getnum(st,5);
  15994. int16 y2 = script_getnum(st,6);
  15995. cell_t type = (cell_t)script_getnum(st,7);
  15996. bool flag = (bool)script_getnum(st,8);
  15997. int x,y;
  15998. if( x1 > x2 ) swap(x1,x2);
  15999. if( y1 > y2 ) swap(y1,y2);
  16000. for( y = y1; y <= y2; ++y )
  16001. for( x = x1; x <= x2; ++x )
  16002. map_setcell(m, x, y, type, flag);
  16003. return SCRIPT_CMD_SUCCESS;
  16004. }
  16005. /*==========================================
  16006. * Mercenary Commands
  16007. *------------------------------------------*/
  16008. BUILDIN_FUNC(mercenary_create)
  16009. {
  16010. struct map_session_data *sd;
  16011. int class_, contract_time;
  16012. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  16013. return SCRIPT_CMD_SUCCESS;
  16014. class_ = script_getnum(st,2);
  16015. if( !mercenary_class(class_) )
  16016. return SCRIPT_CMD_SUCCESS;
  16017. contract_time = script_getnum(st,3);
  16018. mercenary_create(sd, class_, contract_time);
  16019. return SCRIPT_CMD_SUCCESS;
  16020. }
  16021. BUILDIN_FUNC(mercenary_heal)
  16022. {
  16023. struct map_session_data *sd = script_rid2sd(st);
  16024. int hp, sp;
  16025. if( sd == NULL || sd->md == NULL )
  16026. return SCRIPT_CMD_SUCCESS;
  16027. hp = script_getnum(st,2);
  16028. sp = script_getnum(st,3);
  16029. status_heal(&sd->md->bl, hp, sp, 0);
  16030. return SCRIPT_CMD_SUCCESS;
  16031. }
  16032. BUILDIN_FUNC(mercenary_sc_start)
  16033. {
  16034. struct map_session_data *sd = script_rid2sd(st);
  16035. enum sc_type type;
  16036. int tick, val1;
  16037. if( sd == NULL || sd->md == NULL )
  16038. return SCRIPT_CMD_SUCCESS;
  16039. type = (sc_type)script_getnum(st,2);
  16040. tick = script_getnum(st,3);
  16041. val1 = script_getnum(st,4);
  16042. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  16043. return SCRIPT_CMD_SUCCESS;
  16044. }
  16045. BUILDIN_FUNC(mercenary_get_calls)
  16046. {
  16047. struct map_session_data *sd = script_rid2sd(st);
  16048. int guild;
  16049. if( sd == NULL )
  16050. return SCRIPT_CMD_SUCCESS;
  16051. guild = script_getnum(st,2);
  16052. switch( guild )
  16053. {
  16054. case ARCH_MERC_GUILD:
  16055. script_pushint(st,sd->status.arch_calls);
  16056. break;
  16057. case SPEAR_MERC_GUILD:
  16058. script_pushint(st,sd->status.spear_calls);
  16059. break;
  16060. case SWORD_MERC_GUILD:
  16061. script_pushint(st,sd->status.sword_calls);
  16062. break;
  16063. default:
  16064. script_pushint(st,0);
  16065. break;
  16066. }
  16067. return SCRIPT_CMD_SUCCESS;
  16068. }
  16069. BUILDIN_FUNC(mercenary_set_calls)
  16070. {
  16071. struct map_session_data *sd = script_rid2sd(st);
  16072. int guild, value, *calls;
  16073. if( sd == NULL )
  16074. return SCRIPT_CMD_SUCCESS;
  16075. guild = script_getnum(st,2);
  16076. value = script_getnum(st,3);
  16077. switch( guild )
  16078. {
  16079. case ARCH_MERC_GUILD:
  16080. calls = &sd->status.arch_calls;
  16081. break;
  16082. case SPEAR_MERC_GUILD:
  16083. calls = &sd->status.spear_calls;
  16084. break;
  16085. case SWORD_MERC_GUILD:
  16086. calls = &sd->status.sword_calls;
  16087. break;
  16088. default:
  16089. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  16090. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16091. }
  16092. *calls += value;
  16093. *calls = cap_value(*calls, 0, INT_MAX);
  16094. return SCRIPT_CMD_SUCCESS;
  16095. }
  16096. BUILDIN_FUNC(mercenary_get_faith)
  16097. {
  16098. struct map_session_data *sd = script_rid2sd(st);
  16099. int guild;
  16100. if( sd == NULL )
  16101. return SCRIPT_CMD_SUCCESS;
  16102. guild = script_getnum(st,2);
  16103. switch( guild )
  16104. {
  16105. case ARCH_MERC_GUILD:
  16106. script_pushint(st,sd->status.arch_faith);
  16107. break;
  16108. case SPEAR_MERC_GUILD:
  16109. script_pushint(st,sd->status.spear_faith);
  16110. break;
  16111. case SWORD_MERC_GUILD:
  16112. script_pushint(st,sd->status.sword_faith);
  16113. break;
  16114. default:
  16115. script_pushint(st,0);
  16116. break;
  16117. }
  16118. return SCRIPT_CMD_SUCCESS;
  16119. }
  16120. BUILDIN_FUNC(mercenary_set_faith)
  16121. {
  16122. struct map_session_data *sd = script_rid2sd(st);
  16123. int guild, value, *calls;
  16124. if( sd == NULL )
  16125. return SCRIPT_CMD_SUCCESS;
  16126. guild = script_getnum(st,2);
  16127. value = script_getnum(st,3);
  16128. switch( guild )
  16129. {
  16130. case ARCH_MERC_GUILD:
  16131. calls = &sd->status.arch_faith;
  16132. break;
  16133. case SPEAR_MERC_GUILD:
  16134. calls = &sd->status.spear_faith;
  16135. break;
  16136. case SWORD_MERC_GUILD:
  16137. calls = &sd->status.sword_faith;
  16138. break;
  16139. default:
  16140. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  16141. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16142. }
  16143. *calls += value;
  16144. *calls = cap_value(*calls, 0, INT_MAX);
  16145. if( mercenary_get_guild(sd->md) == guild )
  16146. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  16147. return SCRIPT_CMD_SUCCESS;
  16148. }
  16149. /*------------------------------------------
  16150. * Book Reading
  16151. *------------------------------------------*/
  16152. BUILDIN_FUNC(readbook)
  16153. {
  16154. struct map_session_data *sd;
  16155. int book_id, page;
  16156. if( (sd = script_rid2sd(st)) == NULL )
  16157. return SCRIPT_CMD_SUCCESS;
  16158. book_id = script_getnum(st,2);
  16159. page = script_getnum(st,3);
  16160. clif_readbook(sd->fd, book_id, page);
  16161. return SCRIPT_CMD_SUCCESS;
  16162. }
  16163. /******************
  16164. Questlog script commands
  16165. *******************/
  16166. /**
  16167. * Add job criteria to questinfo
  16168. * @param qi Quest Info
  16169. * @param job
  16170. * @author [Cydh]
  16171. **/
  16172. static void buildin_questinfo_setjob(struct questinfo *qi, int job) {
  16173. RECREATE(qi->jobid, unsigned short, qi->jobid_count+1);
  16174. qi->jobid[qi->jobid_count++] = job;
  16175. }
  16176. /**
  16177. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  16178. **/
  16179. BUILDIN_FUNC(questinfo)
  16180. {
  16181. TBL_NPC* nd = map_id2nd(st->oid);
  16182. int quest_id, icon;
  16183. struct questinfo qi, *q2;
  16184. if( nd == NULL || nd->bl.m == -1 ) {
  16185. ShowError("buildin_questinfo: No NPC attached.\n");
  16186. return SCRIPT_CMD_FAILURE;
  16187. }
  16188. quest_id = script_getnum(st, 2);
  16189. icon = script_getnum(st, 3);
  16190. #if PACKETVER >= 20120410
  16191. switch(icon){
  16192. case QTYPE_QUEST:
  16193. case QTYPE_QUEST2:
  16194. case QTYPE_JOB:
  16195. case QTYPE_JOB2:
  16196. case QTYPE_EVENT:
  16197. case QTYPE_EVENT2:
  16198. case QTYPE_WARG:
  16199. case QTYPE_WARG2:
  16200. // Leave everything as it is
  16201. break;
  16202. case QTYPE_NONE:
  16203. default:
  16204. // Default to nothing if icon id is invalid.
  16205. icon = QTYPE_NONE;
  16206. break;
  16207. }
  16208. #else
  16209. if(icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  16210. icon = QTYPE_QUEST;
  16211. else
  16212. icon = icon + 1;
  16213. #endif
  16214. qi.quest_id = quest_id;
  16215. qi.icon = (unsigned char)icon;
  16216. qi.nd = nd;
  16217. if( script_hasdata(st, 4) ) {
  16218. int color = script_getnum(st, 4);
  16219. if( color < 0 || color > 3 ) {
  16220. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  16221. script_reportfunc(st);
  16222. color = 0;
  16223. }
  16224. qi.color = (unsigned char)color;
  16225. }
  16226. qi.min_level = 1;
  16227. qi.max_level = MAX_LEVEL;
  16228. q2 = map_add_questinfo(nd->bl.m, &qi);
  16229. q2->req = NULL;
  16230. q2->req_count = 0;
  16231. q2->jobid = NULL;
  16232. q2->jobid_count = 0;
  16233. if(script_hasdata(st, 5)) {
  16234. int job = script_getnum(st, 5);
  16235. if (!pcdb_checkid(job))
  16236. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  16237. else {
  16238. buildin_questinfo_setjob(q2, job);
  16239. }
  16240. }
  16241. return SCRIPT_CMD_SUCCESS;
  16242. }
  16243. /**
  16244. * setquest <ID>{,<char_id>};
  16245. **/
  16246. BUILDIN_FUNC(setquest)
  16247. {
  16248. struct map_session_data *sd;
  16249. int quest_id;
  16250. quest_id = script_getnum(st, 2);
  16251. if (!script_charid2sd(3,sd))
  16252. return SCRIPT_CMD_FAILURE;
  16253. if( quest_add(sd, quest_id) == -1 ){
  16254. script_reportsrc(st);
  16255. script_reportfunc(st);
  16256. }
  16257. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  16258. pc_show_questinfo(sd);
  16259. return SCRIPT_CMD_SUCCESS;
  16260. }
  16261. /**
  16262. * erasequest <ID>{,<char_id>};
  16263. **/
  16264. BUILDIN_FUNC(erasequest)
  16265. {
  16266. struct map_session_data *sd;
  16267. if (!script_charid2sd(3,sd))
  16268. return SCRIPT_CMD_FAILURE;
  16269. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  16270. script_reportsrc(st);
  16271. script_reportfunc(st);
  16272. }
  16273. return SCRIPT_CMD_SUCCESS;
  16274. }
  16275. /**
  16276. * completequest <ID>{,<char_id>};
  16277. **/
  16278. BUILDIN_FUNC(completequest)
  16279. {
  16280. struct map_session_data *sd;
  16281. if (!script_charid2sd(3,sd))
  16282. return SCRIPT_CMD_FAILURE;
  16283. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  16284. //20120410 or 20090218
  16285. pc_show_questinfo(sd);
  16286. return SCRIPT_CMD_SUCCESS;
  16287. }
  16288. /**
  16289. * changequest <ID>,<ID2>{,<char_id>};
  16290. **/
  16291. BUILDIN_FUNC(changequest)
  16292. {
  16293. struct map_session_data *sd;
  16294. if (!script_charid2sd(4,sd))
  16295. return SCRIPT_CMD_FAILURE;
  16296. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  16297. script_reportsrc(st);
  16298. script_reportfunc(st);
  16299. }
  16300. //20120410 or 20090218
  16301. pc_show_questinfo(sd);
  16302. return SCRIPT_CMD_SUCCESS;
  16303. }
  16304. /**
  16305. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  16306. **/
  16307. BUILDIN_FUNC(checkquest)
  16308. {
  16309. struct map_session_data *sd;
  16310. enum quest_check_type type = HAVEQUEST;
  16311. if( script_hasdata(st, 3) )
  16312. type = (enum quest_check_type)script_getnum(st, 3);
  16313. if (!script_charid2sd(4,sd))
  16314. return SCRIPT_CMD_FAILURE;
  16315. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  16316. return SCRIPT_CMD_SUCCESS;
  16317. }
  16318. /**
  16319. * isbegin_quest(<ID>{,<char_id>})
  16320. **/
  16321. BUILDIN_FUNC(isbegin_quest)
  16322. {
  16323. struct map_session_data *sd;
  16324. int i;
  16325. if (!script_charid2sd(3,sd))
  16326. return SCRIPT_CMD_FAILURE;
  16327. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  16328. script_pushint(st, i + (i < 1));
  16329. return SCRIPT_CMD_SUCCESS;
  16330. }
  16331. /**
  16332. * showevent <icon>{,<mark color>{,<char_id>}}
  16333. **/
  16334. BUILDIN_FUNC(showevent)
  16335. {
  16336. TBL_PC *sd;
  16337. struct npc_data *nd = map_id2nd(st->oid);
  16338. int icon, color = 0;
  16339. if (!script_charid2sd(4,sd))
  16340. return SCRIPT_CMD_FAILURE;
  16341. if( sd == NULL || nd == NULL )
  16342. return SCRIPT_CMD_SUCCESS;
  16343. icon = script_getnum(st, 2);
  16344. if( script_hasdata(st, 3) ) {
  16345. color = script_getnum(st, 3);
  16346. if( color < 0 || color > 3 ) {
  16347. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  16348. script_reportfunc(st);
  16349. color = 0;
  16350. }
  16351. }
  16352. #if PACKETVER >= 20120410
  16353. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  16354. icon = 9999; // Default to nothing if icon id is invalid.
  16355. #else
  16356. if(icon < 0 || icon > 7)
  16357. icon = 0;
  16358. else
  16359. icon = icon + 1;
  16360. #endif
  16361. clif_quest_show_event(sd, &nd->bl, icon, color);
  16362. return SCRIPT_CMD_SUCCESS;
  16363. }
  16364. /*==========================================
  16365. * BattleGround System
  16366. *------------------------------------------*/
  16367. BUILDIN_FUNC(waitingroom2bg)
  16368. {
  16369. struct npc_data *nd;
  16370. struct chat_data *cd;
  16371. const char *map_name, *ev = "", *dev = "";
  16372. int x, y, mapindex = 0, bg_id;
  16373. unsigned char i,c=0;
  16374. if( script_hasdata(st,7) )
  16375. nd = npc_name2id(script_getstr(st,7));
  16376. else
  16377. nd = (struct npc_data *)map_id2bl(st->oid);
  16378. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  16379. {
  16380. script_pushint(st,0);
  16381. return SCRIPT_CMD_SUCCESS;
  16382. }
  16383. map_name = script_getstr(st,2);
  16384. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16385. { // Invalid Map
  16386. script_pushint(st, 0);
  16387. return SCRIPT_CMD_SUCCESS;
  16388. }
  16389. x = script_getnum(st,3);
  16390. y = script_getnum(st,4);
  16391. if(script_hasdata(st,5))
  16392. ev = script_getstr(st,5); // Logout Event
  16393. if(script_hasdata(st,6))
  16394. dev = script_getstr(st,6); // Die Event
  16395. check_event(st, ev);
  16396. check_event(st, dev);
  16397. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  16398. { // Creation failed
  16399. script_pushint(st,0);
  16400. return SCRIPT_CMD_SUCCESS;
  16401. }
  16402. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  16403. struct map_session_data *sd;
  16404. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ){
  16405. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  16406. ++c;
  16407. }
  16408. }
  16409. mapreg_setreg(add_str("$@arenamembersnum"), c);
  16410. script_pushint(st,bg_id);
  16411. return SCRIPT_CMD_SUCCESS;
  16412. }
  16413. BUILDIN_FUNC(waitingroom2bg_single)
  16414. {
  16415. const char* map_name;
  16416. struct npc_data *nd;
  16417. struct chat_data *cd;
  16418. struct map_session_data *sd;
  16419. struct battleground_data *bg;
  16420. int x, y, mapindex, bg_id;
  16421. bg_id = script_getnum(st,2);
  16422. if ((bg = bg_team_search(bg_id)) == NULL) {
  16423. script_pushint(st, false);
  16424. return SCRIPT_CMD_SUCCESS;
  16425. }
  16426. if (script_hasdata(st, 3)) {
  16427. map_name = script_getstr(st, 3);
  16428. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16429. script_pushint(st, false);
  16430. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16431. }
  16432. x = script_getnum(st, 4);
  16433. y = script_getnum(st, 5);
  16434. }
  16435. else {
  16436. mapindex = bg->mapindex;
  16437. x = bg->x;
  16438. y = bg->y;
  16439. }
  16440. nd = npc_name2id(script_getstr(st,6));
  16441. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  16442. return SCRIPT_CMD_SUCCESS;
  16443. if( (sd = cd->usersd[0]) == NULL )
  16444. return SCRIPT_CMD_SUCCESS;
  16445. if( bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16446. {
  16447. script_pushint(st, true);
  16448. }
  16449. else
  16450. script_pushint(st, false);
  16451. return SCRIPT_CMD_SUCCESS;
  16452. }
  16453. /// Creates an instance of battleground battle group.
  16454. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  16455. /// @author [secretdataz]
  16456. BUILDIN_FUNC(bg_create) {
  16457. const char *map_name, *ev = "", *dev = "";
  16458. int x, y, mapindex = 0;
  16459. map_name = script_getstr(st, 2);
  16460. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16461. { // Invalid Map
  16462. script_pushint(st, 0);
  16463. return SCRIPT_CMD_SUCCESS;
  16464. }
  16465. x = script_getnum(st, 3);
  16466. y = script_getnum(st, 4);
  16467. if(script_hasdata(st, 5))
  16468. ev = script_getstr(st, 5); // Logout Event
  16469. if(script_hasdata(st, 6))
  16470. dev = script_getstr(st, 6); // Die Event
  16471. check_event(st, ev);
  16472. check_event(st, dev);
  16473. script_pushint(st, bg_create(mapindex, x, y, ev, dev));
  16474. return SCRIPT_CMD_SUCCESS;
  16475. }
  16476. /// Adds attached player or <char id> (if specified) to an existing
  16477. /// battleground group and warps it to the specified coordinates on
  16478. /// the given map.
  16479. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  16480. /// @author [secretdataz]
  16481. BUILDIN_FUNC(bg_join) {
  16482. const char* map_name;
  16483. struct map_session_data *sd;
  16484. struct battleground_data *bg;
  16485. int x, y, bg_id, mapindex;
  16486. bg_id = script_getnum(st, 2);
  16487. if ((bg = bg_team_search(bg_id)) == NULL) {
  16488. script_pushint(st, false);
  16489. return SCRIPT_CMD_SUCCESS;
  16490. }
  16491. if (script_hasdata(st, 3)) {
  16492. map_name = script_getstr(st, 3);
  16493. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16494. script_pushint(st, false);
  16495. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16496. }
  16497. x = script_getnum(st, 4);
  16498. y = script_getnum(st, 5);
  16499. } else {
  16500. mapindex = bg->mapindex;
  16501. x = bg->x;
  16502. y = bg->y;
  16503. }
  16504. if (!script_charid2sd(6, sd)) {
  16505. script_pushint(st, false);
  16506. return SCRIPT_CMD_FAILURE;
  16507. }
  16508. if (bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16509. {
  16510. script_pushint(st, true);
  16511. }
  16512. else
  16513. script_pushint(st, false);
  16514. return SCRIPT_CMD_SUCCESS;
  16515. }
  16516. BUILDIN_FUNC(bg_team_setxy)
  16517. {
  16518. struct battleground_data *bg;
  16519. int bg_id;
  16520. bg_id = script_getnum(st,2);
  16521. if( (bg = bg_team_search(bg_id)) == NULL )
  16522. return SCRIPT_CMD_SUCCESS;
  16523. bg->x = script_getnum(st,3);
  16524. bg->y = script_getnum(st,4);
  16525. return SCRIPT_CMD_SUCCESS;
  16526. }
  16527. BUILDIN_FUNC(bg_warp)
  16528. {
  16529. int x, y, mapindex, bg_id;
  16530. const char* map_name;
  16531. bg_id = script_getnum(st,2);
  16532. map_name = script_getstr(st,3);
  16533. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16534. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16535. x = script_getnum(st,4);
  16536. y = script_getnum(st,5);
  16537. bg_team_warp(bg_id, mapindex, x, y);
  16538. return SCRIPT_CMD_SUCCESS;
  16539. }
  16540. BUILDIN_FUNC(bg_monster)
  16541. {
  16542. int class_ = 0, x = 0, y = 0, bg_id = 0;
  16543. const char *str,*mapname, *evt="";
  16544. bg_id = script_getnum(st,2);
  16545. mapname = script_getstr(st,3);
  16546. x = script_getnum(st,4);
  16547. y = script_getnum(st,5);
  16548. str = script_getstr(st,6);
  16549. class_ = script_getnum(st,7);
  16550. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  16551. check_event(st, evt);
  16552. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  16553. return SCRIPT_CMD_SUCCESS;
  16554. }
  16555. BUILDIN_FUNC(bg_monster_set_team)
  16556. {
  16557. struct mob_data *md;
  16558. struct block_list *mbl;
  16559. int id = script_getnum(st,2),
  16560. bg_id = script_getnum(st,3);
  16561. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  16562. return SCRIPT_CMD_SUCCESS;
  16563. md = (TBL_MOB *)mbl;
  16564. md->bg_id = bg_id;
  16565. mob_stop_attack(md);
  16566. mob_stop_walking(md, 0);
  16567. md->target_id = md->attacked_id = 0;
  16568. clif_charnameack(0, &md->bl);
  16569. return SCRIPT_CMD_SUCCESS;
  16570. }
  16571. BUILDIN_FUNC(bg_leave)
  16572. {
  16573. struct map_session_data *sd = NULL;
  16574. if( !script_charid2sd(2,sd) || !sd->bg_id )
  16575. return SCRIPT_CMD_SUCCESS;
  16576. bg_team_leave(sd,0);
  16577. return SCRIPT_CMD_SUCCESS;
  16578. }
  16579. BUILDIN_FUNC(bg_destroy)
  16580. {
  16581. int bg_id = script_getnum(st,2);
  16582. bg_team_delete(bg_id);
  16583. return SCRIPT_CMD_SUCCESS;
  16584. }
  16585. BUILDIN_FUNC(bg_getareausers)
  16586. {
  16587. const char *str;
  16588. int16 m, x0, y0, x1, y1;
  16589. int bg_id;
  16590. int i = 0, c = 0;
  16591. struct battleground_data *bg = NULL;
  16592. bg_id = script_getnum(st,2);
  16593. str = script_getstr(st,3);
  16594. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  16595. {
  16596. script_pushint(st,0);
  16597. return SCRIPT_CMD_SUCCESS;
  16598. }
  16599. x0 = script_getnum(st,4);
  16600. y0 = script_getnum(st,5);
  16601. x1 = script_getnum(st,6);
  16602. y1 = script_getnum(st,7);
  16603. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  16604. {
  16605. struct map_session_data *sd;
  16606. if( (sd = bg->members[i].sd) == NULL )
  16607. continue;
  16608. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  16609. continue;
  16610. c++;
  16611. }
  16612. script_pushint(st,c);
  16613. return SCRIPT_CMD_SUCCESS;
  16614. }
  16615. BUILDIN_FUNC(bg_updatescore)
  16616. {
  16617. const char *str;
  16618. int16 m;
  16619. str = script_getstr(st,2);
  16620. if( (m = map_mapname2mapid(str)) < 0 )
  16621. return SCRIPT_CMD_SUCCESS;
  16622. map[m].bgscore_lion = script_getnum(st,3);
  16623. map[m].bgscore_eagle = script_getnum(st,4);
  16624. clif_bg_updatescore(m);
  16625. return SCRIPT_CMD_SUCCESS;
  16626. }
  16627. BUILDIN_FUNC(bg_get_data)
  16628. {
  16629. struct battleground_data *bg;
  16630. int bg_id = script_getnum(st,2),
  16631. type = script_getnum(st,3);
  16632. if( (bg = bg_team_search(bg_id)) == NULL )
  16633. {
  16634. script_pushint(st,0);
  16635. return SCRIPT_CMD_SUCCESS;
  16636. }
  16637. switch( type )
  16638. {
  16639. case 0: script_pushint(st, bg->count); break;
  16640. default:
  16641. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  16642. break;
  16643. }
  16644. return SCRIPT_CMD_SUCCESS;
  16645. }
  16646. /*==========================================
  16647. * Instancing System
  16648. *------------------------------------------*/
  16649. //Returns an Instance ID
  16650. //Checks NPC first, then if player is attached we check
  16651. unsigned short script_instancegetid(struct script_state* st)
  16652. {
  16653. unsigned short instance_id = 0;
  16654. struct npc_data *nd;
  16655. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  16656. instance_id = nd->instance_id;
  16657. else {
  16658. struct map_session_data *sd = NULL;
  16659. struct party_data *p = NULL;
  16660. struct guild *g = NULL;
  16661. if ((sd = script_rid2sd(st)) != NULL) {
  16662. if (sd->instance_id)
  16663. instance_id = sd->instance_id;
  16664. if (instance_id == 0 && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id)
  16665. instance_id = p->instance_id;
  16666. if (instance_id == 0 && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
  16667. instance_id = g->instance_id;
  16668. }
  16669. }
  16670. return instance_id;
  16671. }
  16672. /*==========================================
  16673. * Creates the instance
  16674. * Returns Instance ID if created successfully
  16675. *------------------------------------------*/
  16676. BUILDIN_FUNC(instance_create)
  16677. {
  16678. enum instance_mode mode = IM_PARTY;
  16679. int owner_id = 0;
  16680. if (script_hasdata(st, 3)) {
  16681. mode = script_getnum(st, 3);
  16682. if (mode < IM_NONE || mode >= IM_MAX) {
  16683. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  16684. return SCRIPT_CMD_FAILURE;
  16685. }
  16686. }
  16687. if (script_hasdata(st, 4))
  16688. owner_id = script_getnum(st, 4);
  16689. else {
  16690. struct map_session_data *sd = NULL;
  16691. sd = script_rid2sd(st); // If sd is NULL, instance_create will return -2.
  16692. switch(mode) {
  16693. case IM_NONE:
  16694. owner_id = st->oid;
  16695. break;
  16696. case IM_CHAR:
  16697. if (sd)
  16698. owner_id = sd->status.char_id;
  16699. break;
  16700. case IM_PARTY:
  16701. if (sd)
  16702. owner_id = sd->status.party_id;
  16703. break;
  16704. case IM_GUILD:
  16705. if (sd)
  16706. owner_id = sd->status.guild_id;
  16707. break;
  16708. default:
  16709. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  16710. return SCRIPT_CMD_FAILURE;
  16711. }
  16712. }
  16713. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  16714. return SCRIPT_CMD_SUCCESS;
  16715. }
  16716. /*==========================================
  16717. * Destroys an instance (unofficial)
  16718. * Officially instances are only destroyed by timeout
  16719. *
  16720. * instance_destroy {<instance_id>};
  16721. *------------------------------------------*/
  16722. BUILDIN_FUNC(instance_destroy)
  16723. {
  16724. unsigned short instance_id;
  16725. if( script_hasdata(st,2) )
  16726. instance_id = script_getnum(st,2);
  16727. else
  16728. instance_id = script_instancegetid(st);
  16729. if( instance_id == 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  16730. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %hu.\n", instance_id);
  16731. return SCRIPT_CMD_FAILURE;
  16732. }
  16733. instance_destroy(instance_id);
  16734. return SCRIPT_CMD_SUCCESS;
  16735. }
  16736. /*==========================================
  16737. * Warps player to instance
  16738. * Results:
  16739. * 0: Success
  16740. * 1: Character not in party/guild (for party/guild type instances)
  16741. * 2: Character/Party/Guild doesn't have instance
  16742. * 3: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  16743. *------------------------------------------*/
  16744. BUILDIN_FUNC(instance_enter)
  16745. {
  16746. struct map_session_data *sd = NULL;
  16747. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  16748. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  16749. if (!script_charid2sd(5,sd))
  16750. return SCRIPT_CMD_FAILURE;
  16751. if (x != -1 && y != -1)
  16752. script_pushint(st, instance_enter_position(sd, script_instancegetid(st), script_getstr(st, 2), x, y));
  16753. else
  16754. script_pushint(st, instance_enter(sd, script_instancegetid(st), script_getstr(st, 2)));
  16755. return SCRIPT_CMD_SUCCESS;
  16756. }
  16757. /*==========================================
  16758. * Returns the name of a duplicated NPC
  16759. *
  16760. * instance_npcname <npc_name>{,<instance_id>};
  16761. * <npc_name> is the full name of an NPC.
  16762. *------------------------------------------*/
  16763. BUILDIN_FUNC(instance_npcname)
  16764. {
  16765. const char *str;
  16766. unsigned short instance_id = 0;
  16767. struct npc_data *nd;
  16768. str = script_getstr(st,2);
  16769. if( script_hasdata(st,3) )
  16770. instance_id = script_getnum(st,3);
  16771. else
  16772. instance_id = script_instancegetid(st);
  16773. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  16774. static char npcname[NAME_LENGTH];
  16775. snprintf(npcname, sizeof(npcname), "dup_%hu_%d", instance_id, nd->bl.id);
  16776. script_pushconststr(st,npcname);
  16777. } else {
  16778. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %hu, NPC name: \"%s\".)\n", instance_id, str);
  16779. st->state = END;
  16780. return SCRIPT_CMD_FAILURE;
  16781. }
  16782. return SCRIPT_CMD_SUCCESS;
  16783. }
  16784. /*==========================================
  16785. * Returns the name of a duplicated map
  16786. *
  16787. * instance_mapname <map_name>{,<instance_id>};
  16788. *------------------------------------------*/
  16789. BUILDIN_FUNC(instance_mapname)
  16790. {
  16791. const char *str;
  16792. int16 m;
  16793. unsigned short instance_id = 0;
  16794. str = script_getstr(st,2);
  16795. if( script_hasdata(st,3) )
  16796. instance_id = script_getnum(st,3);
  16797. else
  16798. instance_id = script_instancegetid(st);
  16799. // Check that instance mapname is a valid map
  16800. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  16801. script_pushconststr(st, "");
  16802. else
  16803. script_pushconststr(st, map[m].name);
  16804. return SCRIPT_CMD_SUCCESS;
  16805. }
  16806. /*==========================================
  16807. * Returns an Instance ID
  16808. *------------------------------------------*/
  16809. BUILDIN_FUNC(instance_id)
  16810. {
  16811. script_pushint(st, script_instancegetid(st));
  16812. return SCRIPT_CMD_SUCCESS;
  16813. }
  16814. /*==========================================
  16815. * Warps all players inside an instance
  16816. *
  16817. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  16818. *------------------------------------------*/
  16819. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  16820. {
  16821. unsigned int m = va_arg(ap,unsigned int);
  16822. int x = va_arg(ap,int);
  16823. int y = va_arg(ap,int);
  16824. unsigned short instance_id = va_arg(ap,unsigned int);
  16825. struct map_session_data *sd = NULL;
  16826. int owner_id = 0;
  16827. nullpo_retr(0, bl);
  16828. if (bl->type != BL_PC)
  16829. return 0;
  16830. sd = (TBL_PC *)bl;
  16831. owner_id = instance_data[instance_id].owner_id;
  16832. switch(instance_data[instance_id].mode) {
  16833. case IM_NONE:
  16834. break;
  16835. case IM_CHAR:
  16836. if (sd->status.char_id != owner_id)
  16837. return 0;
  16838. break;
  16839. case IM_PARTY:
  16840. if (sd->status.party_id != owner_id)
  16841. return 0;
  16842. break;
  16843. case IM_GUILD:
  16844. if (sd->status.guild_id != owner_id)
  16845. return 0;
  16846. }
  16847. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  16848. return 1;
  16849. }
  16850. BUILDIN_FUNC(instance_warpall)
  16851. {
  16852. int16 m, i;
  16853. unsigned short instance_id;
  16854. const char *mapn;
  16855. int x, y;
  16856. mapn = script_getstr(st,2);
  16857. x = script_getnum(st,3);
  16858. y = script_getnum(st,4);
  16859. if( script_hasdata(st,5) )
  16860. instance_id = script_getnum(st,5);
  16861. else
  16862. instance_id = script_instancegetid(st);
  16863. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  16864. return SCRIPT_CMD_FAILURE;
  16865. for(i = 0; i < instance_data[instance_id].cnt_map; i++)
  16866. map_foreachinmap(buildin_instance_warpall_sub, instance_data[instance_id].map[i]->m, BL_PC, map_id2index(m), x, y, instance_id);
  16867. return SCRIPT_CMD_SUCCESS;
  16868. }
  16869. /*==========================================
  16870. * Broadcasts to all maps inside an instance
  16871. *
  16872. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  16873. * Using 0 for <instance id> will auto-detect the id.
  16874. *------------------------------------------*/
  16875. BUILDIN_FUNC(instance_announce) {
  16876. unsigned short instance_id = script_getnum(st,2);
  16877. const char *mes = script_getstr(st,3);
  16878. int flag = script_getnum(st,4);
  16879. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  16880. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  16881. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  16882. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  16883. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  16884. int i;
  16885. if( instance_id == 0 ) {
  16886. instance_id = script_instancegetid(st);
  16887. }
  16888. if( !instance_id && &instance_data[instance_id] != NULL) {
  16889. ShowError("buildin_instance_announce: Intance is not found.\n");
  16890. return SCRIPT_CMD_FAILURE;
  16891. }
  16892. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  16893. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i]->m, BL_PC,
  16894. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  16895. return SCRIPT_CMD_SUCCESS;
  16896. }
  16897. /*==========================================
  16898. * instance_check_party [malufett]
  16899. * Values:
  16900. * party_id : Party ID of the invoking character. [Required Parameter]
  16901. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  16902. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16903. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16904. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  16905. * Example 2: instance_check_party (getcharid(1),1,1,99);
  16906. *------------------------------------------*/
  16907. BUILDIN_FUNC(instance_check_party)
  16908. {
  16909. int amount, min, max, i, party_id, c = 0;
  16910. struct party_data *p = NULL;
  16911. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  16912. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16913. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16914. if( min < 1 || min > MAX_LEVEL) {
  16915. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  16916. return SCRIPT_CMD_FAILURE;
  16917. } else if( max < 1 || max > MAX_LEVEL) {
  16918. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  16919. return SCRIPT_CMD_FAILURE;
  16920. }
  16921. if( script_hasdata(st,2) )
  16922. party_id = script_getnum(st,2);
  16923. else return SCRIPT_CMD_FAILURE;
  16924. if( !(p = party_search(party_id)) ) {
  16925. script_pushint(st, 0); // Returns false if party does not exist.
  16926. return SCRIPT_CMD_FAILURE;
  16927. }
  16928. for( i = 0; i < MAX_PARTY; i++ ) {
  16929. struct map_session_data *pl_sd;
  16930. if( (pl_sd = p->data[i].sd) )
  16931. if(map_id2bl(pl_sd->bl.id)) {
  16932. if(pl_sd->status.base_level < min) {
  16933. script_pushint(st, 0);
  16934. return SCRIPT_CMD_SUCCESS;
  16935. } else if(pl_sd->status.base_level > max) {
  16936. script_pushint(st, 0);
  16937. return SCRIPT_CMD_SUCCESS;
  16938. }
  16939. c++;
  16940. }
  16941. }
  16942. if(c < amount)
  16943. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  16944. else
  16945. script_pushint(st, 1);
  16946. return SCRIPT_CMD_SUCCESS;
  16947. }
  16948. /*==========================================
  16949. * instance_check_guild
  16950. * Values:
  16951. * guild_id : Guild ID of the invoking character. [Required Parameter]
  16952. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  16953. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16954. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16955. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  16956. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  16957. *------------------------------------------*/
  16958. BUILDIN_FUNC(instance_check_guild)
  16959. {
  16960. int amount, min, max, i, guild_id = 0, c = 0;
  16961. struct guild *g = NULL;
  16962. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  16963. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16964. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16965. if (min < 1 || min > MAX_LEVEL) {
  16966. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  16967. return SCRIPT_CMD_FAILURE;
  16968. } else if (max < 1 || max > MAX_LEVEL) {
  16969. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  16970. return SCRIPT_CMD_FAILURE;
  16971. }
  16972. if (script_hasdata(st,2))
  16973. guild_id = script_getnum(st,2);
  16974. else
  16975. return SCRIPT_CMD_FAILURE;
  16976. if (!(g = guild_search(guild_id))) {
  16977. script_pushint(st, 0); // Returns false if guild does not exist.
  16978. return SCRIPT_CMD_FAILURE;
  16979. }
  16980. for(i = 0; i < MAX_GUILD; i++) {
  16981. struct map_session_data *pl_sd;
  16982. if ((pl_sd = g->member[i].sd)) {
  16983. if (map_id2bl(pl_sd->bl.id)) {
  16984. if (pl_sd->status.base_level < min) {
  16985. script_pushint(st, 0);
  16986. return SCRIPT_CMD_SUCCESS;
  16987. } else if (pl_sd->status.base_level > max) {
  16988. script_pushint(st, 0);
  16989. return SCRIPT_CMD_SUCCESS;
  16990. }
  16991. c++;
  16992. }
  16993. }
  16994. }
  16995. if (c < amount)
  16996. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  16997. else
  16998. script_pushint(st, 1);
  16999. return SCRIPT_CMD_SUCCESS;
  17000. }
  17001. /*==========================================
  17002. * Custom Fonts
  17003. *------------------------------------------*/
  17004. BUILDIN_FUNC(setfont)
  17005. {
  17006. struct map_session_data *sd = script_rid2sd(st);
  17007. int font = script_getnum(st,2);
  17008. if( sd == NULL )
  17009. return SCRIPT_CMD_SUCCESS;
  17010. if( sd->status.font != font )
  17011. sd->status.font = font;
  17012. else
  17013. sd->status.font = 0;
  17014. clif_font(sd);
  17015. return SCRIPT_CMD_SUCCESS;
  17016. }
  17017. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  17018. {
  17019. TBL_MOB* md = (TBL_MOB*)bl;
  17020. struct block_list *tbl;
  17021. int mobid = va_arg(ap,int);
  17022. uint16 skill_id = va_arg(ap,int);
  17023. uint16 skill_lv = va_arg(ap,int);
  17024. int casttime = va_arg(ap,int);
  17025. int cancel = va_arg(ap,int);
  17026. int emotion = va_arg(ap,int);
  17027. int target = va_arg(ap,int);
  17028. if( md->mob_id != mobid )
  17029. return 0;
  17030. // 0:self, 1:target, 2:master, default:random
  17031. switch( target ) {
  17032. case 0: tbl = map_id2bl(md->bl.id); break;
  17033. case 1: tbl = map_id2bl(md->target_id); break;
  17034. case 2: tbl = map_id2bl(md->master_id); break;
  17035. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  17036. }
  17037. if( !tbl )
  17038. return 0;
  17039. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  17040. unit_skillcastcancel(bl,0);
  17041. if( skill_get_casttype(skill_id) == CAST_GROUND )
  17042. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  17043. else
  17044. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  17045. clif_emotion(&md->bl, emotion);
  17046. return 1;
  17047. }
  17048. /*==========================================
  17049. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  17050. *------------------------------------------*/
  17051. BUILDIN_FUNC(areamobuseskill)
  17052. {
  17053. struct block_list center;
  17054. struct script_data *data;
  17055. int16 m;
  17056. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  17057. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  17058. ShowError("areamobuseskill: invalid map name.\n");
  17059. return SCRIPT_CMD_SUCCESS;
  17060. }
  17061. center.m = m;
  17062. center.x = script_getnum(st,3);
  17063. center.y = script_getnum(st,4);
  17064. range = script_getnum(st,5);
  17065. mobid = script_getnum(st,6);
  17066. data = script_getdata(st, 7);
  17067. get_val(st, data); // Convert into value in case of a variable
  17068. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  17069. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  17070. skill_lv = battle_config.mob_max_skilllvl;
  17071. casttime = script_getnum(st,9);
  17072. cancel = script_getnum(st,10);
  17073. emotion = script_getnum(st,11);
  17074. target = script_getnum(st,12);
  17075. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  17076. return SCRIPT_CMD_SUCCESS;
  17077. }
  17078. BUILDIN_FUNC(progressbar)
  17079. {
  17080. struct map_session_data * sd = script_rid2sd(st);
  17081. const char * color;
  17082. unsigned int second;
  17083. if( !st || !sd )
  17084. return SCRIPT_CMD_SUCCESS;
  17085. st->state = STOP;
  17086. color = script_getstr(st,2);
  17087. second = script_getnum(st,3);
  17088. sd->progressbar.npc_id = st->oid;
  17089. sd->progressbar.timeout = gettick() + second*1000;
  17090. sd->state.workinprogress = WIP_DISABLE_ALL;
  17091. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  17092. return SCRIPT_CMD_SUCCESS;
  17093. }
  17094. BUILDIN_FUNC(pushpc)
  17095. {
  17096. uint8 dir;
  17097. int cells, dx, dy;
  17098. struct map_session_data* sd;
  17099. if((sd = script_rid2sd(st))==NULL)
  17100. {
  17101. return SCRIPT_CMD_SUCCESS;
  17102. }
  17103. dir = script_getnum(st,2);
  17104. cells = script_getnum(st,3);
  17105. if(dir >= DIR_MAX)
  17106. {
  17107. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  17108. script_reportsrc(st);
  17109. dir%= DIR_MAX; // trim spin-over
  17110. }
  17111. if(!cells)
  17112. {// zero distance
  17113. return SCRIPT_CMD_SUCCESS;
  17114. }
  17115. else if(cells<0)
  17116. {// pushing backwards
  17117. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  17118. cells = -cells;
  17119. }
  17120. dx = dirx[dir];
  17121. dy = diry[dir];
  17122. unit_blown(&sd->bl, dx, dy, cells, 0);
  17123. return SCRIPT_CMD_SUCCESS;
  17124. }
  17125. /// Invokes buying store preparation window
  17126. /// buyingstore <slots>;
  17127. BUILDIN_FUNC(buyingstore)
  17128. {
  17129. struct map_session_data* sd;
  17130. if( ( sd = script_rid2sd(st) ) == NULL )
  17131. {
  17132. return SCRIPT_CMD_SUCCESS;
  17133. }
  17134. if( npc_isnear(&sd->bl) ) {
  17135. char output[150];
  17136. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  17137. clif_displaymessage(sd->fd, output);
  17138. return SCRIPT_CMD_SUCCESS;
  17139. }
  17140. buyingstore_setup(sd, script_getnum(st,2));
  17141. return SCRIPT_CMD_SUCCESS;
  17142. }
  17143. /// Invokes search store info window
  17144. /// searchstores <uses>,<effect>;
  17145. BUILDIN_FUNC(searchstores)
  17146. {
  17147. unsigned short effect;
  17148. unsigned int uses;
  17149. struct map_session_data* sd;
  17150. if( ( sd = script_rid2sd(st) ) == NULL )
  17151. {
  17152. return SCRIPT_CMD_SUCCESS;
  17153. }
  17154. uses = script_getnum(st,2);
  17155. effect = script_getnum(st,3);
  17156. if( !uses )
  17157. {
  17158. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  17159. return SCRIPT_CMD_FAILURE;
  17160. }
  17161. if( effect > 1 )
  17162. {
  17163. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  17164. return SCRIPT_CMD_FAILURE;
  17165. }
  17166. searchstore_open(sd, uses, effect);
  17167. return SCRIPT_CMD_SUCCESS;
  17168. }
  17169. /// Displays a number as large digital clock.
  17170. /// showdigit <value>[,<type>];
  17171. BUILDIN_FUNC(showdigit)
  17172. {
  17173. unsigned int type = 0;
  17174. int value;
  17175. struct map_session_data* sd;
  17176. if( ( sd = script_rid2sd(st) ) == NULL )
  17177. {
  17178. return SCRIPT_CMD_SUCCESS;
  17179. }
  17180. value = script_getnum(st,2);
  17181. if( script_hasdata(st,3) )
  17182. {
  17183. type = script_getnum(st,3);
  17184. if( type > 3 )
  17185. {
  17186. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  17187. return SCRIPT_CMD_FAILURE;
  17188. }
  17189. }
  17190. clif_showdigit(sd, (unsigned char)type, value);
  17191. return SCRIPT_CMD_SUCCESS;
  17192. }
  17193. /**
  17194. * Rune Knight
  17195. * makerune <% success bonus>{,<char_id>};
  17196. **/
  17197. BUILDIN_FUNC(makerune) {
  17198. TBL_PC* sd;
  17199. if (!script_charid2sd(3,sd))
  17200. return SCRIPT_CMD_FAILURE;
  17201. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  17202. sd->itemid = script_getnum(st,2);
  17203. return SCRIPT_CMD_SUCCESS;
  17204. }
  17205. /**
  17206. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  17207. **/
  17208. BUILDIN_FUNC(checkdragon) {
  17209. TBL_PC* sd;
  17210. if (!script_charid2sd(2,sd))
  17211. return SCRIPT_CMD_FAILURE;
  17212. if( pc_isridingdragon(sd) )
  17213. script_pushint(st,1);
  17214. else
  17215. script_pushint(st,0);
  17216. return SCRIPT_CMD_SUCCESS;
  17217. }
  17218. /**
  17219. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  17220. * - Toggles the dragon on a RK if he can mount;
  17221. * @param Color - when not provided uses the green dragon;
  17222. * - 1 : Green Dragon
  17223. * - 2 : Brown Dragon
  17224. * - 3 : Gray Dragon
  17225. * - 4 : Blue Dragon
  17226. * - 5 : Red Dragon
  17227. **/
  17228. BUILDIN_FUNC(setdragon) {
  17229. TBL_PC* sd;
  17230. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  17231. if (!script_charid2sd(3,sd))
  17232. return SCRIPT_CMD_FAILURE;
  17233. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  17234. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  17235. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  17236. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  17237. script_pushint(st,1);
  17238. } else {//Not mounted; Mount now.
  17239. unsigned int option = OPTION_DRAGON1;
  17240. if( color ) {
  17241. option = ( color == 1 ? OPTION_DRAGON1 :
  17242. color == 2 ? OPTION_DRAGON2 :
  17243. color == 3 ? OPTION_DRAGON3 :
  17244. color == 4 ? OPTION_DRAGON4 :
  17245. color == 5 ? OPTION_DRAGON5 : 0);
  17246. if( !option ) {
  17247. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  17248. option = OPTION_DRAGON1;
  17249. }
  17250. }
  17251. pc_setoption(sd, sd->sc.option|option);
  17252. script_pushint(st,1);
  17253. }
  17254. return SCRIPT_CMD_SUCCESS;
  17255. }
  17256. /**
  17257. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  17258. **/
  17259. BUILDIN_FUNC(ismounting) {
  17260. TBL_PC* sd;
  17261. if (!script_charid2sd(2,sd))
  17262. return SCRIPT_CMD_FAILURE;
  17263. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17264. script_pushint(st,1);
  17265. else
  17266. script_pushint(st,0);
  17267. return SCRIPT_CMD_SUCCESS;
  17268. }
  17269. /**
  17270. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  17271. * - Toggles new mounts on a player when he can mount
  17272. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  17273. * - Will unmount the player is he is already mounting
  17274. **/
  17275. BUILDIN_FUNC(setmounting) {
  17276. TBL_PC* sd;
  17277. if (!script_charid2sd(2,sd))
  17278. return SCRIPT_CMD_FAILURE;
  17279. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  17280. clif_msg(sd, NEED_REINS_OF_MOUNT);
  17281. script_pushint(st,0); //can't mount with one of these
  17282. } else {
  17283. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17284. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  17285. else
  17286. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  17287. script_pushint(st,1);//in both cases, return 1.
  17288. }
  17289. return SCRIPT_CMD_SUCCESS;
  17290. }
  17291. /**
  17292. * Retrieves quantity of arguments provided to callfunc/callsub.
  17293. * getargcount() -> amount of arguments received in a function
  17294. **/
  17295. BUILDIN_FUNC(getargcount) {
  17296. struct script_retinfo* ri;
  17297. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  17298. ShowError("script:getargcount: used out of function or callsub label!\n");
  17299. st->state = END;
  17300. return SCRIPT_CMD_FAILURE;
  17301. }
  17302. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  17303. script_pushint(st, ri->nargs);
  17304. return SCRIPT_CMD_SUCCESS;
  17305. }
  17306. /**
  17307. * getcharip(<account ID>/<character ID>/<character name>)
  17308. **/
  17309. BUILDIN_FUNC(getcharip)
  17310. {
  17311. struct map_session_data* sd = NULL;
  17312. /* check if a character name is specified */
  17313. if( script_hasdata(st, 2) )
  17314. {
  17315. struct script_data *data;
  17316. data = script_getdata(st, 2);
  17317. get_val(st, data); // Convert into value in case of a variable
  17318. if (data_isstring(data))
  17319. sd = map_nick2sd(script_getstr(st, 2));
  17320. else if (data_isint(data) || script_getnum(st, 2))
  17321. {
  17322. int id = 0;
  17323. id = script_getnum(st, 2);
  17324. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  17325. }
  17326. }
  17327. else
  17328. sd = script_rid2sd(st);
  17329. /* check for sd and IP */
  17330. if (!sd || !session[sd->fd]->client_addr)
  17331. {
  17332. script_pushconststr(st, "");
  17333. return SCRIPT_CMD_SUCCESS;
  17334. }
  17335. /* return the client ip_addr converted for output */
  17336. if (sd && sd->fd && session[sd->fd])
  17337. {
  17338. /* initiliaze */
  17339. const char *ip_addr = NULL;
  17340. uint32 ip;
  17341. /* set ip, ip_addr and convert to ip and push str */
  17342. ip = session[sd->fd]->client_addr;
  17343. ip_addr = ip2str(ip, NULL);
  17344. script_pushstrcopy(st, ip_addr);
  17345. }
  17346. return SCRIPT_CMD_SUCCESS;
  17347. }
  17348. /**
  17349. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  17350. **/
  17351. BUILDIN_FUNC(is_function) {
  17352. const char* str = script_getstr(st,2);
  17353. if( strdb_exists(userfunc_db, str) )
  17354. script_pushint(st,1);
  17355. else
  17356. script_pushint(st,0);
  17357. return SCRIPT_CMD_SUCCESS;
  17358. }
  17359. /**
  17360. * get_revision() -> retrieves the current svn revision (if available)
  17361. **/
  17362. BUILDIN_FUNC(get_revision) {
  17363. const char *svn = get_svn_revision();
  17364. if ( svn[0] != UNKNOWN_VERSION )
  17365. script_pushint(st,atoi(svn));
  17366. else
  17367. script_pushint(st,-1); //unknown
  17368. return SCRIPT_CMD_SUCCESS;
  17369. }
  17370. /* get_hash() -> retrieves the current git hash (if available)*/
  17371. BUILDIN_FUNC(get_githash) {
  17372. const char* git = get_git_hash();
  17373. char buf[CHAT_SIZE_MAX];
  17374. safestrncpy(buf,git,strlen(git)+1);
  17375. if ( git[0] != UNKNOWN_VERSION )
  17376. script_pushstr(st,buf);
  17377. else
  17378. script_pushstr(st,"Unknown"); //unknown
  17379. return SCRIPT_CMD_SUCCESS;
  17380. }
  17381. /**
  17382. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  17383. **/
  17384. BUILDIN_FUNC(freeloop) {
  17385. if( script_hasdata(st,2) ) {
  17386. if( script_getnum(st,2) )
  17387. st->freeloop = 1;
  17388. else
  17389. st->freeloop = 0;
  17390. }
  17391. script_pushint(st, st->freeloop);
  17392. return SCRIPT_CMD_SUCCESS;
  17393. }
  17394. /**
  17395. * @commands (script based)
  17396. **/
  17397. BUILDIN_FUNC(bindatcmd) {
  17398. const char* atcmd;
  17399. const char* eventName;
  17400. int i, level = 0, level2 = 0;
  17401. bool create = false;
  17402. atcmd = script_getstr(st,2);
  17403. eventName = script_getstr(st,3);
  17404. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17405. atcmd++;
  17406. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  17407. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  17408. if( atcmd_binding_count == 0 ) {
  17409. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  17410. create = true;
  17411. } else {
  17412. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  17413. if( i < atcmd_binding_count ) {/* update existent entry */
  17414. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17415. atcmd_binding[i]->level = level;
  17416. atcmd_binding[i]->level2 = level2;
  17417. } else
  17418. create = true;
  17419. }
  17420. if( create ) {
  17421. i = atcmd_binding_count;
  17422. if( atcmd_binding_count++ != 0 )
  17423. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  17424. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  17425. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  17426. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17427. atcmd_binding[i]->level = level;
  17428. atcmd_binding[i]->level2 = level2;
  17429. }
  17430. return SCRIPT_CMD_SUCCESS;
  17431. }
  17432. BUILDIN_FUNC(unbindatcmd) {
  17433. const char* atcmd;
  17434. int i = 0;
  17435. atcmd = script_getstr(st, 2);
  17436. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17437. atcmd++;
  17438. if( atcmd_binding_count == 0 ) {
  17439. script_pushint(st, 0);
  17440. return SCRIPT_CMD_SUCCESS;
  17441. }
  17442. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  17443. if( i < atcmd_binding_count ) {
  17444. int cursor = 0;
  17445. aFree(atcmd_binding[i]);
  17446. atcmd_binding[i] = NULL;
  17447. /* compact the list now that we freed a slot somewhere */
  17448. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  17449. if( atcmd_binding[i] == NULL )
  17450. continue;
  17451. if( cursor != i ) {
  17452. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  17453. }
  17454. cursor++;
  17455. }
  17456. if( (atcmd_binding_count = cursor) == 0 )
  17457. aFree(atcmd_binding);
  17458. script_pushint(st, 1);
  17459. } else
  17460. script_pushint(st, 0);/* not found */
  17461. return SCRIPT_CMD_SUCCESS;
  17462. }
  17463. BUILDIN_FUNC(useatcmd) {
  17464. return atcommand_sub(st,3);
  17465. }
  17466. BUILDIN_FUNC(checkre)
  17467. {
  17468. int num;
  17469. num=script_getnum(st,2);
  17470. switch(num){
  17471. case 0:
  17472. #ifdef RENEWAL
  17473. script_pushint(st, 1);
  17474. #else
  17475. script_pushint(st, 0);
  17476. #endif
  17477. break;
  17478. case 1:
  17479. #ifdef RENEWAL_CAST
  17480. script_pushint(st, 1);
  17481. #else
  17482. script_pushint(st, 0);
  17483. #endif
  17484. break;
  17485. case 2:
  17486. #ifdef RENEWAL_DROP
  17487. script_pushint(st, 1);
  17488. #else
  17489. script_pushint(st, 0);
  17490. #endif
  17491. break;
  17492. case 3:
  17493. #ifdef RENEWAL_EXP
  17494. script_pushint(st, 1);
  17495. #else
  17496. script_pushint(st, 0);
  17497. #endif
  17498. break;
  17499. case 4:
  17500. #ifdef RENEWAL_LVDMG
  17501. script_pushint(st, 1);
  17502. #else
  17503. script_pushint(st, 0);
  17504. #endif
  17505. break;
  17506. case 5:
  17507. #ifdef RENEWAL_ASPD
  17508. script_pushint(st, 1);
  17509. #else
  17510. script_pushint(st, 0);
  17511. #endif
  17512. break;
  17513. default:
  17514. ShowWarning("buildin_checkre: unknown parameter.\n");
  17515. break;
  17516. }
  17517. return SCRIPT_CMD_SUCCESS;
  17518. }
  17519. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>}} */
  17520. BUILDIN_FUNC(getrandgroupitem) {
  17521. TBL_PC* sd;
  17522. int i, get_count = 0;
  17523. uint16 group = script_getnum(st,2), qty = 0;
  17524. uint8 sub_group = 1;
  17525. struct item item_tmp;
  17526. struct s_item_group_entry *entry = NULL;
  17527. if (!(sd = script_rid2sd(st)))
  17528. return SCRIPT_CMD_SUCCESS;
  17529. if (!group) {
  17530. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  17531. return SCRIPT_CMD_FAILURE;
  17532. }
  17533. FETCH(3, qty);
  17534. FETCH(4, sub_group);
  17535. entry = itemdb_get_randgroupitem(group,sub_group);
  17536. if (!entry)
  17537. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  17538. memset(&item_tmp,0,sizeof(item_tmp));
  17539. item_tmp.nameid = entry->nameid;
  17540. item_tmp.identify = itemdb_isidentified(entry->nameid);
  17541. if (!qty)
  17542. qty = entry->amount;
  17543. //Check if it's stackable.
  17544. if (!itemdb_isstackable(entry->nameid)) {
  17545. item_tmp.amount = 1;
  17546. get_count = qty;
  17547. }
  17548. else {
  17549. item_tmp.amount = qty;
  17550. get_count = 1;
  17551. }
  17552. for (i = 0; i < get_count; i++) {
  17553. // if not pet egg
  17554. if (!pet_create_egg(sd, entry->nameid)) {
  17555. unsigned char flag = 0;
  17556. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  17557. clif_additem(sd,0,0,flag);
  17558. if (pc_candrop(sd,&item_tmp))
  17559. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  17560. }
  17561. }
  17562. }
  17563. return SCRIPT_CMD_SUCCESS;
  17564. }
  17565. /* getgroupitem <group_id>{,<char_id>};
  17566. * Gives item(s) to the attached player based on item group contents
  17567. */
  17568. BUILDIN_FUNC(getgroupitem) {
  17569. TBL_PC *sd;
  17570. int group_id = script_getnum(st,2);
  17571. if (!script_charid2sd(3,sd))
  17572. return SCRIPT_CMD_SUCCESS;
  17573. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  17574. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  17575. return SCRIPT_CMD_FAILURE;
  17576. }
  17577. return SCRIPT_CMD_SUCCESS;
  17578. }
  17579. /* cleanmap <map_name>;
  17580. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  17581. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  17582. {
  17583. nullpo_ret(bl);
  17584. map_clearflooritem(bl);
  17585. return SCRIPT_CMD_SUCCESS;
  17586. }
  17587. BUILDIN_FUNC(cleanmap)
  17588. {
  17589. const char *mapname;
  17590. int16 m;
  17591. mapname = script_getstr(st, 2);
  17592. m = map_mapname2mapid(mapname);
  17593. if (!m)
  17594. return SCRIPT_CMD_FAILURE;
  17595. if ((script_lastdata(st) - 2) < 4) {
  17596. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  17597. } else {
  17598. int16 x0 = script_getnum(st, 3);
  17599. int16 y0 = script_getnum(st, 4);
  17600. int16 x1 = script_getnum(st, 5);
  17601. int16 y1 = script_getnum(st, 6);
  17602. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  17603. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  17604. } else {
  17605. ShowError("cleanarea: invalid coordinate defined!\n");
  17606. return SCRIPT_CMD_FAILURE;
  17607. }
  17608. }
  17609. return SCRIPT_CMD_SUCCESS;
  17610. }
  17611. /* Cast a skill on the attached player.
  17612. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  17613. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  17614. BUILDIN_FUNC(npcskill)
  17615. {
  17616. uint16 skill_id;
  17617. unsigned short skill_level;
  17618. unsigned int stat_point;
  17619. unsigned int npc_level;
  17620. struct npc_data *nd;
  17621. struct map_session_data *sd;
  17622. struct script_data *data;
  17623. data = script_getdata(st, 2);
  17624. get_val(st, data); // Convert into value in case of a variable
  17625. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  17626. skill_level = script_getnum(st, 3);
  17627. stat_point = script_getnum(st, 4);
  17628. npc_level = script_getnum(st, 5);
  17629. sd = script_rid2sd(st);
  17630. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  17631. if (stat_point > battle_config.max_third_parameter) {
  17632. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  17633. return SCRIPT_CMD_FAILURE;
  17634. }
  17635. if (npc_level > MAX_LEVEL) {
  17636. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  17637. return SCRIPT_CMD_FAILURE;
  17638. }
  17639. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  17640. return SCRIPT_CMD_FAILURE;
  17641. }
  17642. nd->level = npc_level;
  17643. nd->stat_point = stat_point;
  17644. if (!nd->status.hp)
  17645. status_calc_npc(nd, SCO_FIRST);
  17646. else
  17647. status_calc_npc(nd, SCO_NONE);
  17648. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  17649. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  17650. else
  17651. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  17652. return SCRIPT_CMD_SUCCESS;
  17653. }
  17654. /* Consumes an item.
  17655. * consumeitem <item id>{,<char_id>};
  17656. * consumeitem "<item name>"{,<char_id>};
  17657. * @param item: Item ID or name
  17658. */
  17659. BUILDIN_FUNC(consumeitem)
  17660. {
  17661. TBL_PC *sd;
  17662. struct script_data *data;
  17663. struct item_data *item_data;
  17664. if (!script_charid2sd(3, sd))
  17665. return SCRIPT_CMD_FAILURE;
  17666. data = script_getdata( st, 2 );
  17667. get_val( st, data );
  17668. if( data_isstring( data ) ){
  17669. const char *name = conv_str( st, data );
  17670. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  17671. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  17672. return SCRIPT_CMD_FAILURE;
  17673. }
  17674. }else if( data_isint( data ) ){
  17675. unsigned short nameid = conv_num( st, data );
  17676. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  17677. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  17678. return SCRIPT_CMD_FAILURE;
  17679. }
  17680. }else{
  17681. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  17682. return SCRIPT_CMD_FAILURE;
  17683. }
  17684. run_script( item_data->script, 0, sd->bl.id, 0 );
  17685. return SCRIPT_CMD_SUCCESS;
  17686. }
  17687. /** Makes a player sit/stand.
  17688. * sit {"<character name>"};
  17689. * stand {"<character name>"};
  17690. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  17691. * @param name: Player name that will be invoked
  17692. */
  17693. BUILDIN_FUNC(sit)
  17694. {
  17695. TBL_PC *sd;
  17696. if( script_hasdata(st, 2) )
  17697. sd = map_nick2sd(script_getstr(st, 2));
  17698. else
  17699. sd = script_rid2sd(st);
  17700. if( sd == NULL)
  17701. return SCRIPT_CMD_FAILURE;
  17702. if( !pc_issit(sd) ) {
  17703. pc_setsit(sd);
  17704. skill_sit(sd, 1);
  17705. clif_sitting(&sd->bl);
  17706. }
  17707. return SCRIPT_CMD_SUCCESS;
  17708. }
  17709. /** Makes player to stand
  17710. * @param name: Player name that will be set
  17711. */
  17712. BUILDIN_FUNC(stand)
  17713. {
  17714. TBL_PC *sd;
  17715. if( script_hasdata(st, 2) )
  17716. sd = map_nick2sd(script_getstr(st, 2));
  17717. else
  17718. sd = script_rid2sd(st);
  17719. if( sd == NULL)
  17720. return SCRIPT_CMD_FAILURE;
  17721. if( pc_issit(sd) && pc_setstand(sd, false)) {
  17722. skill_sit(sd, 0);
  17723. clif_standing(&sd->bl);
  17724. }
  17725. return SCRIPT_CMD_SUCCESS;
  17726. }
  17727. /** Creates an array of bounded item IDs
  17728. * countbound {<type>{,<char_id>}};
  17729. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  17730. * @return amt: Amount of items found
  17731. */
  17732. BUILDIN_FUNC(countbound)
  17733. {
  17734. int i, type, j = 0, k = 0;
  17735. TBL_PC *sd;
  17736. if (!script_charid2sd(3,sd))
  17737. return SCRIPT_CMD_FAILURE;
  17738. type = script_getnum(st,2);
  17739. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  17740. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  17741. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  17742. ))
  17743. {
  17744. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  17745. k++;
  17746. j += sd->inventory.u.items_inventory[i].amount;
  17747. }
  17748. }
  17749. script_pushint(st,j);
  17750. return SCRIPT_CMD_SUCCESS;
  17751. }
  17752. /** Creates new party
  17753. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  17754. * @param party_name: String of party name that will be created
  17755. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  17756. * @param item_share: 0-Each Take. 1-Party Share
  17757. * @param item_share_type: 0-Each Take. 1-Even Share
  17758. * @return val: Result value
  17759. * -3 - party name is exist
  17760. * -2 - player is in party already
  17761. * -1 - player is not found
  17762. * 0 - unknown error
  17763. * 1 - success, will return party id $@party_create_id
  17764. */
  17765. BUILDIN_FUNC(party_create)
  17766. {
  17767. char party_name[NAME_LENGTH];
  17768. int item1 = 0, item2 = 0;
  17769. TBL_PC *sd;
  17770. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  17771. script_pushint(st,-1);
  17772. return SCRIPT_CMD_FAILURE;
  17773. }
  17774. if( sd->status.party_id ) {
  17775. script_pushint(st,-2);
  17776. return SCRIPT_CMD_FAILURE;
  17777. }
  17778. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  17779. trim(party_name);
  17780. if( party_searchname(party_name) ) {
  17781. script_pushint(st,-3);
  17782. return SCRIPT_CMD_FAILURE;
  17783. }
  17784. if( script_getnum(st,4) )
  17785. item1 = 1;
  17786. if( script_getnum(st,5) )
  17787. item2 = 1;
  17788. party_create_byscript = 1;
  17789. script_pushint(st,party_create(sd,party_name,item1,item2));
  17790. return SCRIPT_CMD_SUCCESS;
  17791. }
  17792. /** Adds player to specified party
  17793. * party_addmember <party id>,<char id>;
  17794. * @param party_id: The party that will be entered by player
  17795. * @param char_id: Char id of player that will be joined to the party
  17796. * @return val: Result value
  17797. * -5 - another character of the same account is in the party
  17798. * -4 - party is full
  17799. * -3 - party is not found
  17800. * -2 - player is in party already
  17801. * -1 - player is not found
  17802. * 0 - unknown error
  17803. * 1 - success
  17804. */
  17805. BUILDIN_FUNC(party_addmember)
  17806. {
  17807. int party_id = script_getnum(st,2);
  17808. TBL_PC *sd;
  17809. struct party_data *party;
  17810. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  17811. script_pushint(st,-1);
  17812. return SCRIPT_CMD_FAILURE;
  17813. }
  17814. if( sd->status.party_id ) {
  17815. script_pushint(st,-2);
  17816. return SCRIPT_CMD_FAILURE;
  17817. }
  17818. if( !(party = party_search(party_id)) ) {
  17819. script_pushint(st,-3);
  17820. return SCRIPT_CMD_FAILURE;
  17821. }
  17822. if (battle_config.block_account_in_same_party) {
  17823. int i;
  17824. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  17825. if (i < MAX_PARTY) {
  17826. script_pushint(st,-5);
  17827. return SCRIPT_CMD_FAILURE;
  17828. }
  17829. }
  17830. if( party->party.count >= MAX_PARTY ) {
  17831. script_pushint(st,-4);
  17832. return SCRIPT_CMD_FAILURE;
  17833. }
  17834. sd->party_invite = party_id;
  17835. script_pushint(st,party_add_member(party_id,sd));
  17836. return SCRIPT_CMD_SUCCESS;
  17837. }
  17838. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  17839. * party_delmember {<char id>,<party_id>};
  17840. * @param: char_id
  17841. * @param: party_id
  17842. * @return val: Result value
  17843. * -3 - player is not in party
  17844. * -2 - party is not found
  17845. * -1 - player is not found
  17846. * 0 - unknown error
  17847. * 1 - success
  17848. */
  17849. BUILDIN_FUNC(party_delmember)
  17850. {
  17851. TBL_PC *sd = NULL;
  17852. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  17853. script_pushint(st,-1);
  17854. return SCRIPT_CMD_FAILURE;
  17855. }
  17856. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  17857. script_pushint(st,party_removemember2(sd,0,0));
  17858. else
  17859. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  17860. return SCRIPT_CMD_SUCCESS;
  17861. }
  17862. /** Changes party leader of specified party (even the leader is offline)
  17863. * party_changeleader <party id>,<char id>;
  17864. * @param party_id: ID of party
  17865. * @param char_id: Char ID of new leader
  17866. * @return val: Result value
  17867. * -4 - player is party leader already
  17868. * -3 - player is not in this party
  17869. * -2 - player is not found
  17870. * -1 - party is not found
  17871. * 0 - unknown error
  17872. * 1 - success */
  17873. BUILDIN_FUNC(party_changeleader)
  17874. {
  17875. int i, party_id = script_getnum(st,2);
  17876. TBL_PC *sd = NULL;
  17877. TBL_PC *tsd = NULL;
  17878. struct party_data *party = NULL;
  17879. if( !(party = party_search(party_id)) ) {
  17880. script_pushint(st,-1);
  17881. return SCRIPT_CMD_FAILURE;
  17882. }
  17883. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  17884. script_pushint(st,-2);
  17885. return SCRIPT_CMD_FAILURE;
  17886. }
  17887. if( tsd->status.party_id != party_id ) {
  17888. script_pushint(st,-3);
  17889. return SCRIPT_CMD_FAILURE;
  17890. }
  17891. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17892. if( i >= MAX_PARTY ) { //this is should impossible!
  17893. script_pushint(st,0);
  17894. return SCRIPT_CMD_FAILURE;
  17895. }
  17896. if( party->data[i].sd == tsd ) {
  17897. script_pushint(st,-4);
  17898. return SCRIPT_CMD_FAILURE;
  17899. }
  17900. script_pushint(st,party_changeleader(sd,tsd,party));
  17901. return SCRIPT_CMD_SUCCESS;
  17902. }
  17903. /** Changes party option
  17904. * party_changeoption <party id>,<option>,<flag>;
  17905. * @param party_id: ID of party that will be changed
  17906. * @param option: Type of option
  17907. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  17908. */
  17909. BUILDIN_FUNC(party_changeoption)
  17910. {
  17911. struct party_data *party;
  17912. if( !(party = party_search(script_getnum(st,2))) ) {
  17913. script_pushint(st,-1);
  17914. return SCRIPT_CMD_FAILURE;
  17915. }
  17916. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  17917. return SCRIPT_CMD_SUCCESS;
  17918. }
  17919. /** Destroys party with party id.
  17920. * party_destroy <party id>;
  17921. * @param party_id: ID of party that will be destroyed
  17922. */
  17923. BUILDIN_FUNC(party_destroy)
  17924. {
  17925. int i;
  17926. struct party_data *party;
  17927. if( !(party = party_search(script_getnum(st,2))) ) {
  17928. script_pushint(st,0);
  17929. return SCRIPT_CMD_FAILURE;
  17930. }
  17931. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17932. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  17933. int j;
  17934. for( j = 0; j < MAX_PARTY; j++ ) {
  17935. TBL_PC *sd = party->data[j].sd;
  17936. if(sd)
  17937. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  17938. else if( party->party.member[j].char_id )
  17939. 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);
  17940. }
  17941. party_broken(party->party.party_id);
  17942. script_pushint(st,1);
  17943. }
  17944. else //leader leave = party broken
  17945. script_pushint(st,party_leave(party->data[i].sd));
  17946. return SCRIPT_CMD_SUCCESS;
  17947. }
  17948. /** Checks if a player's client version meets a required version or date.
  17949. * @param type: 0 - check by version number; 1 - check by date
  17950. * @param data: Input
  17951. */
  17952. BUILDIN_FUNC(is_clientver) {
  17953. TBL_PC *sd = NULL;
  17954. int type = script_getnum(st,2);
  17955. int data = script_getnum(st,3);
  17956. int ret = 0;
  17957. if (script_hasdata(st,4))
  17958. sd = map_charid2sd(script_getnum(st,4));
  17959. else
  17960. sd = script_rid2sd(st);
  17961. if (sd == NULL) {
  17962. script_pushint(st,0);
  17963. return SCRIPT_CMD_FAILURE;
  17964. }
  17965. switch(type){
  17966. case 0:
  17967. ret = (sd->packet_ver >= data)?1:0;
  17968. break;
  17969. case 1:
  17970. ret = (sd->packet_ver >= date2version(data))?1:0;
  17971. break;
  17972. }
  17973. script_pushint(st,ret);
  17974. return SCRIPT_CMD_SUCCESS;
  17975. }
  17976. /** Returns various information about a player's VIP status. Need to enable VIP system
  17977. * vip_status <type>,{"<character name>"};
  17978. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  17979. * @param name: Character name (Optional)
  17980. */
  17981. BUILDIN_FUNC(vip_status) {
  17982. #ifdef VIP_ENABLE
  17983. TBL_PC *sd;
  17984. char vip_str[26];
  17985. time_t now = time(NULL);
  17986. int type = script_getnum(st, 2);
  17987. if (script_hasdata(st, 3))
  17988. sd = map_nick2sd(script_getstr(st, 3));
  17989. else
  17990. sd = script_rid2sd(st);
  17991. if (sd == NULL)
  17992. return SCRIPT_CMD_FAILURE;
  17993. switch(type) {
  17994. case 1: // Get VIP status.
  17995. script_pushint(st, pc_isvip(sd));
  17996. break;
  17997. case 2: // Get VIP expire date.
  17998. if (pc_isvip(sd)) {
  17999. time_t viptime = sd->vip.time;
  18000. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  18001. vip_str[25] = '\0';
  18002. script_pushstrcopy(st, vip_str);
  18003. } else
  18004. script_pushint(st, 0);
  18005. break;
  18006. case 3: // Get remaining time.
  18007. if (pc_isvip(sd)) {
  18008. time_t viptime_remain = sd->vip.time - now;
  18009. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  18010. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  18011. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  18012. script_pushstrcopy(st, vip_str);
  18013. } else
  18014. script_pushint(st, 0);
  18015. break;
  18016. }
  18017. #else
  18018. script_pushint(st, 0);
  18019. #endif
  18020. return SCRIPT_CMD_SUCCESS;
  18021. }
  18022. /** Adds or removes VIP time in minutes. Need to enable VIP system
  18023. * vip_time <time in mn>,{"<character name>"};
  18024. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  18025. * @param name: Character name (optional)
  18026. */
  18027. BUILDIN_FUNC(vip_time) {
  18028. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  18029. TBL_PC *sd;
  18030. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  18031. if (script_hasdata(st, 3))
  18032. sd = map_nick2sd(script_getstr(st, 3));
  18033. else
  18034. sd = script_rid2sd(st);
  18035. if (sd == NULL)
  18036. return SCRIPT_CMD_FAILURE;
  18037. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  18038. #endif
  18039. return SCRIPT_CMD_SUCCESS;
  18040. }
  18041. /**
  18042. * Turns a player into a monster and optionally can grant a SC attribute effect.
  18043. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18044. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18045. * @param monster: Monster ID or name
  18046. * @param duration: Transform duration in millisecond (ms)
  18047. * @param sc_type: Type of SC that will be affected during the transformation
  18048. * @param val1: Value for SC
  18049. * @param val2: Value for SC
  18050. * @param val3: Value for SC
  18051. * @param val4: Value for SC
  18052. * @author: malufett
  18053. */
  18054. BUILDIN_FUNC(montransform) {
  18055. TBL_PC *sd;
  18056. enum sc_type type;
  18057. int tick, mob_id, val1, val2, val3, val4;
  18058. struct script_data *data;
  18059. val1 = val2 = val3 = val4 = 0;
  18060. if( (sd = script_rid2sd(st)) == NULL )
  18061. return SCRIPT_CMD_FAILURE;
  18062. data = script_getdata(st, 2);
  18063. get_val(st, data); // Convert into value in case of a variable
  18064. if( data_isstring(data) )
  18065. mob_id = mobdb_searchname(script_getstr(st, 2));
  18066. else
  18067. mob_id = mobdb_checkid(script_getnum(st, 2));
  18068. tick = script_getnum(st, 3);
  18069. if (script_hasdata(st, 4))
  18070. type = (sc_type)script_getnum(st, 4);
  18071. else
  18072. type = SC_NONE;
  18073. if (mob_id == 0) {
  18074. if( data_isstring(data) )
  18075. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  18076. else
  18077. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  18078. return SCRIPT_CMD_FAILURE;
  18079. }
  18080. if (mob_id == MOBID_EMPERIUM) {
  18081. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  18082. return SCRIPT_CMD_FAILURE;
  18083. }
  18084. if (!(type >= SC_NONE && type < SC_MAX)) {
  18085. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  18086. return SCRIPT_CMD_FAILURE;
  18087. }
  18088. if (script_hasdata(st, 5))
  18089. val1 = script_getnum(st, 5);
  18090. if (script_hasdata(st, 6))
  18091. val2 = script_getnum(st, 6);
  18092. if (script_hasdata(st, 7))
  18093. val3 = script_getnum(st, 7);
  18094. if (script_hasdata(st, 8))
  18095. val4 = script_getnum(st, 8);
  18096. if (tick != 0) {
  18097. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  18098. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  18099. return SCRIPT_CMD_FAILURE;
  18100. }
  18101. if (sd->disguise){
  18102. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  18103. return SCRIPT_CMD_FAILURE;
  18104. }
  18105. if (!strcmp(script_getfuncname(st), "active_transform")) {
  18106. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18107. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18108. } else {
  18109. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18110. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18111. }
  18112. if (type != SC_NONE)
  18113. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  18114. }
  18115. return SCRIPT_CMD_SUCCESS;
  18116. }
  18117. /**
  18118. * Attach script to player for certain duration
  18119. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  18120. * @param "script code"
  18121. * @param duration
  18122. * @param flag
  18123. * @param icon
  18124. * @param char_id
  18125. * @author [Cydh]
  18126. **/
  18127. BUILDIN_FUNC(bonus_script) {
  18128. uint16 flag = 0;
  18129. int16 icon = SI_BLANK;
  18130. uint32 dur;
  18131. uint8 type = 0;
  18132. TBL_PC* sd;
  18133. const char *script_str = NULL;
  18134. struct s_bonus_script_entry *entry = NULL;
  18135. if (script_hasdata(st,7)) {
  18136. if (!(sd = map_charid2sd(script_getnum(st,7)))) {
  18137. ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7));
  18138. return SCRIPT_CMD_FAILURE;
  18139. }
  18140. }
  18141. else
  18142. sd = script_rid2sd(st);
  18143. if (sd == NULL)
  18144. return SCRIPT_CMD_FAILURE;
  18145. script_str = script_getstr(st,2);
  18146. dur = 1000 * abs(script_getnum(st,3));
  18147. FETCH(4, flag);
  18148. FETCH(5, type);
  18149. FETCH(6, icon);
  18150. // No Script string, No Duration!
  18151. if (script_str[0] == '\0' || !dur) {
  18152. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  18153. return SCRIPT_CMD_FAILURE;
  18154. }
  18155. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  18156. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  18157. return SCRIPT_CMD_FAILURE;
  18158. }
  18159. if (icon <= SI_BLANK || icon >= SI_MAX)
  18160. icon = SI_BLANK;
  18161. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  18162. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  18163. status_calc_pc(sd,SCO_NONE);
  18164. }
  18165. return SCRIPT_CMD_SUCCESS;
  18166. }
  18167. /**
  18168. * Removes all bonus script from player
  18169. * bonus_script_clear {<flag>,{<char_id>}};
  18170. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  18171. * @param char_id Clear script from this character
  18172. * @author [Cydh]
  18173. */
  18174. BUILDIN_FUNC(bonus_script_clear) {
  18175. TBL_PC* sd;
  18176. bool flag = false;
  18177. if (script_hasdata(st,2))
  18178. flag = script_getnum(st,2);
  18179. if (script_hasdata(st,3))
  18180. sd = map_charid2sd(script_getnum(st,3));
  18181. else
  18182. sd = script_rid2sd(st);
  18183. if (sd == NULL)
  18184. return SCRIPT_CMD_FAILURE;
  18185. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  18186. return SCRIPT_CMD_SUCCESS;
  18187. }
  18188. /** Allows player to use atcommand while talking with NPC
  18189. * enable_command;
  18190. * @author [Cydh], [Kichi]
  18191. */
  18192. BUILDIN_FUNC(enable_command) {
  18193. TBL_PC* sd = script_rid2sd(st);
  18194. if (!sd)
  18195. return SCRIPT_CMD_FAILURE;
  18196. sd->state.disable_atcommand_on_npc = 0;
  18197. return SCRIPT_CMD_SUCCESS;
  18198. }
  18199. /** Prevents player to use atcommand while talking with NPC
  18200. * disable_command;
  18201. * @author [Cydh], [Kichi]
  18202. */
  18203. BUILDIN_FUNC(disable_command) {
  18204. TBL_PC* sd = script_rid2sd(st);
  18205. if (!sd)
  18206. return SCRIPT_CMD_FAILURE;
  18207. sd->state.disable_atcommand_on_npc = 1;
  18208. return SCRIPT_CMD_SUCCESS;
  18209. }
  18210. /** Get the information of the members of a guild by type.
  18211. * getguildmember <guild_id>{,<type>};
  18212. * @param guild_id: ID of guild
  18213. * @param type: Type of option (optional)
  18214. */
  18215. BUILDIN_FUNC(getguildmember)
  18216. {
  18217. struct guild *g = NULL;
  18218. uint8 j = 0;
  18219. g = guild_search(script_getnum(st,2));
  18220. if (g) {
  18221. uint8 i, type = 0;
  18222. struct script_data *data = NULL;
  18223. char *varname = NULL;
  18224. if (script_hasdata(st,3))
  18225. type = script_getnum(st,3);
  18226. if (script_hasdata(st,4)) {
  18227. data = script_getdata(st, 4);
  18228. if (!data_isreference(data)) {
  18229. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  18230. return SCRIPT_CMD_FAILURE;
  18231. }
  18232. varname = reference_getname(data);
  18233. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  18234. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  18235. return SCRIPT_CMD_FAILURE;
  18236. }
  18237. }
  18238. for (i = 0; i < MAX_GUILD; i++) {
  18239. if (g->member[i].account_id) {
  18240. switch (type) {
  18241. case 2:
  18242. if (data)
  18243. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  18244. else
  18245. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  18246. break;
  18247. case 1:
  18248. if (data)
  18249. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  18250. else
  18251. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  18252. break;
  18253. default:
  18254. if (data)
  18255. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  18256. else
  18257. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  18258. break;
  18259. }
  18260. j++;
  18261. }
  18262. }
  18263. }
  18264. mapreg_setreg(add_str("$@guildmembercount"), j);
  18265. script_pushint(st, j);
  18266. return SCRIPT_CMD_SUCCESS;
  18267. }
  18268. /** Adds spirit ball to player for 'duration' in milisecond
  18269. * addspiritball <count>,<duration>{,<char_id>};
  18270. * @param count How many spirit ball will be added
  18271. * @param duration How long spiritball is active until it disappears
  18272. * @param char_id Target player (Optional)
  18273. * @author [Cydh]
  18274. */
  18275. BUILDIN_FUNC(addspiritball) {
  18276. uint8 i, count = script_getnum(st,2);
  18277. uint16 duration = script_getnum(st,3);
  18278. struct map_session_data *sd = NULL;
  18279. if (count == 0)
  18280. return SCRIPT_CMD_SUCCESS;
  18281. if (script_hasdata(st,4)) {
  18282. if (!script_isstring(st,4))
  18283. sd = map_charid2sd(script_getnum(st,4));
  18284. else
  18285. sd = map_nick2sd(script_getstr(st,4));
  18286. }
  18287. else
  18288. sd = script_rid2sd(st);
  18289. if (!sd)
  18290. return SCRIPT_CMD_FAILURE;
  18291. for (i = 0; i < count; i++)
  18292. pc_addspiritball(sd,duration,10);
  18293. return SCRIPT_CMD_SUCCESS;
  18294. }
  18295. /** Deletes the spirit ball(s) from player
  18296. * delspiritball <count>{,<char_id>};
  18297. * @param count How many spirit ball will be deleted
  18298. * @param char_id Target player (Optional)
  18299. * @author [Cydh]
  18300. */
  18301. BUILDIN_FUNC(delspiritball) {
  18302. uint8 count = script_getnum(st,2);
  18303. struct map_session_data *sd = NULL;
  18304. if (count == 0)
  18305. count = 1;
  18306. if (script_hasdata(st,3)) {
  18307. if (!script_isstring(st,3))
  18308. sd = map_charid2sd(script_getnum(st,3));
  18309. else
  18310. sd = map_nick2sd(script_getstr(st,3));
  18311. }
  18312. else
  18313. sd = script_rid2sd(st);
  18314. if (!sd)
  18315. return SCRIPT_CMD_FAILURE;
  18316. pc_delspiritball(sd,count,0);
  18317. return SCRIPT_CMD_SUCCESS;
  18318. }
  18319. /** Counts the spirit ball that player has
  18320. * countspiritball {<char_id>};
  18321. * @param char_id Target player (Optional)
  18322. * @author [Cydh]
  18323. */
  18324. BUILDIN_FUNC(countspiritball) {
  18325. struct map_session_data *sd;
  18326. if (script_hasdata(st,2)) {
  18327. if (!script_isstring(st,2))
  18328. sd = map_charid2sd(script_getnum(st,2));
  18329. else
  18330. sd = map_nick2sd(script_getstr(st,2));
  18331. }
  18332. else
  18333. sd = script_rid2sd(st);
  18334. if (!sd)
  18335. return SCRIPT_CMD_FAILURE;
  18336. script_pushint(st,sd->spiritball);
  18337. return SCRIPT_CMD_SUCCESS;
  18338. }
  18339. /** Merges separated stackable items because of guid
  18340. * mergeitem {<char_id>};
  18341. * @param char_id Char ID (Optional)
  18342. * @author [Cydh]
  18343. */
  18344. BUILDIN_FUNC(mergeitem) {
  18345. struct map_session_data *sd;
  18346. if (!script_charid2sd(2, sd))
  18347. return SCRIPT_CMD_FAILURE;
  18348. clif_merge_item_open(sd);
  18349. return SCRIPT_CMD_SUCCESS;
  18350. }
  18351. /** Merges separated stackable items because of guid (Unofficial)
  18352. * mergeitem2 {<item_id>,{<char_id>}};
  18353. * mergeitem2 {"<item name>",{<char_id>}};
  18354. * @param item Item ID/Name for merging specific item (Optional)
  18355. * @author [Cydh]
  18356. */
  18357. BUILDIN_FUNC(mergeitem2) {
  18358. struct map_session_data *sd;
  18359. struct item *items = NULL;
  18360. uint16 i, count = 0, nameid = 0;
  18361. if (!script_charid2sd(3, sd))
  18362. return SCRIPT_CMD_FAILURE;
  18363. if (script_hasdata(st, 2)) {
  18364. struct script_data *data = script_getdata(st, 2);
  18365. get_val(st, data);
  18366. if (data_isstring(data)) {// "<item name>"
  18367. const char *name = conv_str(st,data);
  18368. struct item_data *id;
  18369. if (!(id = itemdb_searchname(name))) {
  18370. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  18371. script_pushint(st, count);
  18372. return SCRIPT_CMD_FAILURE;
  18373. }
  18374. nameid = id->nameid;
  18375. }
  18376. else if (data_isint(data)) {// <item id>
  18377. nameid = conv_num(st,data);
  18378. if (!itemdb_exists(nameid)) {
  18379. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  18380. script_pushint(st, count);
  18381. return SCRIPT_CMD_FAILURE;
  18382. }
  18383. }
  18384. }
  18385. for (i = 0; i < MAX_INVENTORY; i++) {
  18386. struct item *it = &sd->inventory.u.items_inventory[i];
  18387. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  18388. continue;
  18389. if ((!nameid || (nameid == it->nameid))) {
  18390. uint8 k;
  18391. if (!count) {
  18392. CREATE(items, struct item, 1);
  18393. memcpy(&items[count++], it, sizeof(struct item));
  18394. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18395. continue;
  18396. }
  18397. for (k = 0; k < count; k++) {
  18398. // Find Match
  18399. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  18400. items[k].amount += it->amount;
  18401. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18402. break;
  18403. }
  18404. }
  18405. if (k >= count) {
  18406. // New entry
  18407. RECREATE(items, struct item, count+1);
  18408. memcpy(&items[count++], it, sizeof(struct item));
  18409. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18410. }
  18411. }
  18412. }
  18413. if (!items) // Nothing todo here
  18414. return SCRIPT_CMD_SUCCESS;
  18415. // Retrieve the items
  18416. for (i = 0; i < count; i++) {
  18417. uint8 flag = 0;
  18418. if (!&items[i])
  18419. continue;
  18420. items[i].id = 0;
  18421. items[i].unique_id = 0;
  18422. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  18423. clif_additem(sd, i, items[i].amount, flag);
  18424. }
  18425. aFree(items);
  18426. script_pushint(st, count);
  18427. return SCRIPT_CMD_SUCCESS;
  18428. }
  18429. /**
  18430. * Update an entry from NPC shop.
  18431. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  18432. **/
  18433. BUILDIN_FUNC(npcshopupdate) {
  18434. const char* npcname = script_getstr(st, 2);
  18435. struct npc_data* nd = npc_name2id(npcname);
  18436. uint16 nameid = script_getnum(st, 3);
  18437. int price = script_getnum(st, 4);
  18438. #if PACKETVER >= 20131223
  18439. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  18440. #endif
  18441. int i;
  18442. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  18443. script_pushint(st,0);
  18444. return SCRIPT_CMD_FAILURE;
  18445. }
  18446. if (!nd->u.shop.count) {
  18447. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  18448. script_pushint(st,0);
  18449. return SCRIPT_CMD_FAILURE;
  18450. }
  18451. for (i = 0; i < nd->u.shop.count; i++) {
  18452. if (nd->u.shop.shop_item[i].nameid == nameid) {
  18453. if (price != 0)
  18454. nd->u.shop.shop_item[i].value = price;
  18455. #if PACKETVER >= 20131223
  18456. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  18457. nd->u.shop.shop_item[i].qty = stock;
  18458. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  18459. }
  18460. #endif
  18461. }
  18462. }
  18463. script_pushint(st,1);
  18464. return SCRIPT_CMD_SUCCESS;
  18465. }
  18466. /**
  18467. * Get rid from running script.
  18468. * getattachedrid();
  18469. **/
  18470. BUILDIN_FUNC(getattachedrid) {
  18471. script_pushint(st, st->rid);
  18472. return SCRIPT_CMD_SUCCESS;
  18473. }
  18474. /**
  18475. * Get variable from a Player
  18476. * getvar <variable>,<char_id>;
  18477. */
  18478. BUILDIN_FUNC(getvar) {
  18479. int char_id = script_getnum(st, 3);
  18480. struct map_session_data *sd = map_charid2sd(char_id);
  18481. struct script_data *data = NULL;
  18482. const char *name = NULL;
  18483. if (!sd) {
  18484. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  18485. return SCRIPT_CMD_FAILURE;
  18486. }
  18487. data = script_getdata(st, 2);
  18488. if (!data_isreference(data)) {
  18489. ShowError("buildin_getvar: Not a variable\n");
  18490. script_reportdata(data);
  18491. script_pushnil(st);
  18492. st->state = END;
  18493. return SCRIPT_CMD_FAILURE;
  18494. }
  18495. name = reference_getname(data);
  18496. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  18497. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  18498. script_reportdata(data);
  18499. script_pushnil(st);
  18500. st->state = END;
  18501. return SCRIPT_CMD_FAILURE;
  18502. }
  18503. get_val_(st, data, sd);
  18504. if (data_isint(data))
  18505. script_pushint(st, conv_num_(st, data, sd));
  18506. else
  18507. script_pushstrcopy(st, conv_str_(st, data, sd));
  18508. push_val2(st->stack, C_NAME, reference_getuid(data), reference_getref(data));
  18509. return SCRIPT_CMD_SUCCESS;
  18510. }
  18511. /**
  18512. * Display script message
  18513. * showscript "<message>"{,<GID>};
  18514. **/
  18515. BUILDIN_FUNC(showscript) {
  18516. struct block_list *bl = NULL;
  18517. const char *msg = script_getstr(st,2);
  18518. int id = 0;
  18519. if (script_hasdata(st,3)) {
  18520. id = script_getnum(st,3);
  18521. bl = map_id2bl(id);
  18522. }
  18523. else {
  18524. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  18525. }
  18526. if (!bl) {
  18527. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  18528. script_pushint(st,0);
  18529. return SCRIPT_CMD_FAILURE;
  18530. }
  18531. clif_showscript(bl, msg);
  18532. script_pushint(st,1);
  18533. return SCRIPT_CMD_SUCCESS;
  18534. }
  18535. /**
  18536. * Ignore the SECURE_NPCTIMEOUT function.
  18537. * ignoretimeout <flag>{,<char_id>};
  18538. */
  18539. BUILDIN_FUNC(ignoretimeout)
  18540. {
  18541. #ifdef SECURE_NPCTIMEOUT
  18542. struct map_session_data *sd = NULL;
  18543. if (script_hasdata(st,3)) {
  18544. if (!script_isstring(st,3))
  18545. sd = map_charid2sd(script_getnum(st,3));
  18546. else
  18547. sd = map_nick2sd(script_getstr(st,3));
  18548. } else
  18549. sd = script_rid2sd(st);
  18550. if (!sd)
  18551. return SCRIPT_CMD_FAILURE;
  18552. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  18553. #endif
  18554. return SCRIPT_CMD_SUCCESS;
  18555. }
  18556. /**
  18557. * geteleminfo <type>{,<char_id>};
  18558. **/
  18559. BUILDIN_FUNC(geteleminfo) {
  18560. TBL_ELEM *ed = NULL;
  18561. TBL_PC *sd = NULL;
  18562. int type = script_getnum(st,2);
  18563. if (!script_charid2sd(3, sd)) {
  18564. script_pushint(st, 0);
  18565. return SCRIPT_CMD_SUCCESS;
  18566. }
  18567. if (!(ed = sd->ed)) {
  18568. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  18569. script_pushint(st, 0);
  18570. return SCRIPT_CMD_SUCCESS;
  18571. }
  18572. switch (type) {
  18573. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  18574. case 1: script_pushint(st, ed->bl.id); break;
  18575. default:
  18576. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  18577. script_pushint(st, 0);
  18578. return SCRIPT_CMD_FAILURE;
  18579. }
  18580. return SCRIPT_CMD_SUCCESS;
  18581. }
  18582. /**
  18583. * Set the quest info of quest_id only showed on player in level range.
  18584. * setquestinfo_level <quest_id>,<min_level>,<max_level>
  18585. * @author [Cydh]
  18586. **/
  18587. BUILDIN_FUNC(setquestinfo_level) {
  18588. TBL_NPC* nd = map_id2nd(st->oid);
  18589. int quest_id = script_getnum(st, 2);
  18590. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18591. if (!qi) {
  18592. ShowError("buildin_setquestinfo_level: Quest with ID '%d' is not defined yet.\n", quest_id);
  18593. return SCRIPT_CMD_FAILURE;
  18594. }
  18595. qi->min_level = script_getnum(st, 3);
  18596. qi->max_level = script_getnum(st, 4);
  18597. if (!qi->max_level)
  18598. qi->max_level = MAX_LEVEL;
  18599. return SCRIPT_CMD_SUCCESS;
  18600. }
  18601. /**
  18602. * Set the quest info of quest_id only showed for player that has quest criteria
  18603. * setquestinfo_req <quest_id>,<quest_req_id>,<state>{,<quest_req_id>,<state>,...};
  18604. * @author [Cydh]
  18605. **/
  18606. BUILDIN_FUNC(setquestinfo_req) {
  18607. TBL_NPC* nd = map_id2nd(st->oid);
  18608. int quest_id = script_getnum(st, 2);
  18609. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18610. uint8 i = 0;
  18611. uint8 num = script_lastdata(st);
  18612. if (!qi) {
  18613. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not defined yet.\n", quest_id);
  18614. return SCRIPT_CMD_FAILURE;
  18615. }
  18616. if (quest_search(quest_id) == &quest_dummy) {
  18617. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not found in Quest DB.\n", quest_id);
  18618. return SCRIPT_CMD_FAILURE;
  18619. }
  18620. if (num%2) {
  18621. ShowError("buildin_setquestinfo_req: Odd number of parameters(%d) - pairs of requirements are expected.\n", num-2);
  18622. return SCRIPT_CMD_FAILURE;
  18623. }
  18624. for (i = 3; i <= num; i += 2) {
  18625. RECREATE(qi->req, struct questinfo_req, qi->req_count+1);
  18626. qi->req[qi->req_count].quest_id = script_getnum(st, i);
  18627. qi->req[qi->req_count].state = (script_getnum(st, i+1) >= 2) ? 2 : (script_getnum(st, i+1) <= 0) ? 0 : 1;
  18628. qi->req_count++;
  18629. }
  18630. return SCRIPT_CMD_SUCCESS;
  18631. }
  18632. /**
  18633. * Set the quest info of quest_id only showed for player that has specified Job
  18634. * setquestinfo_job <quest_id>,<job>{,<job>...};
  18635. * @author [Cydh]
  18636. **/
  18637. BUILDIN_FUNC(setquestinfo_job) {
  18638. TBL_NPC* nd = map_id2nd(st->oid);
  18639. int quest_id = script_getnum(st, 2);
  18640. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18641. int job_id = 0;
  18642. uint8 i = 0;
  18643. uint8 num = script_lastdata(st)+1;
  18644. if (!qi) {
  18645. ShowError("buildin_setquestinfo_job: Quest with ID '%d' is not defined yet.\n", quest_id);
  18646. return SCRIPT_CMD_FAILURE;
  18647. }
  18648. for (i = 3; i < num; i++) {
  18649. job_id = script_getnum(st, i);
  18650. if (!pcdb_checkid(job_id)) {
  18651. ShowError("buildin_setquestinfo_job: Invalid job id '%d' in Quest with ID %d.\n", job_id, quest_id);
  18652. continue;
  18653. }
  18654. buildin_questinfo_setjob(qi, job_id);
  18655. }
  18656. return SCRIPT_CMD_SUCCESS;
  18657. }
  18658. /**
  18659. * opendressroom(<flag>{,<char_id>});
  18660. */
  18661. BUILDIN_FUNC(opendressroom)
  18662. {
  18663. #if PACKETVER >= 20150513
  18664. int flag = 1;
  18665. TBL_PC* sd;
  18666. if( script_hasdata(st,2) )
  18667. flag = script_getnum(st,2);
  18668. if (!script_charid2sd(3, sd))
  18669. return SCRIPT_CMD_FAILURE;
  18670. clif_dressing_room(sd, flag);
  18671. return SCRIPT_CMD_SUCCESS;
  18672. #else
  18673. return SCRIPT_CMD_FAILURE;
  18674. #endif
  18675. }
  18676. /**
  18677. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  18678. */
  18679. BUILDIN_FUNC(navigateto){
  18680. #if PACKETVER >= 20111010
  18681. TBL_PC* sd;
  18682. const char *mapname;
  18683. uint16 x = 0, y = 0, monster_id = 0;
  18684. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  18685. bool hideWindow = true;
  18686. mapname = script_getstr(st,2);
  18687. if( script_hasdata(st,3) )
  18688. x = script_getnum(st,3);
  18689. if( script_hasdata(st,4) )
  18690. y = script_getnum(st,4);
  18691. if( script_hasdata(st,5) )
  18692. flag = (uint8)script_getnum(st,5);
  18693. if( script_hasdata(st,6) )
  18694. hideWindow = script_getnum(st,6) ? true : false;
  18695. if( script_hasdata(st,7) )
  18696. monster_id = script_getnum(st,7);
  18697. if (!script_charid2sd(8, sd))
  18698. return SCRIPT_CMD_FAILURE;
  18699. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  18700. return SCRIPT_CMD_SUCCESS;
  18701. #else
  18702. return SCRIPT_CMD_FAILURE;
  18703. #endif
  18704. }
  18705. /**
  18706. * adopt("<parent_name>","<baby_name>");
  18707. * adopt(<parent_id>,<baby_id>);
  18708. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  18709. */
  18710. BUILDIN_FUNC(adopt)
  18711. {
  18712. TBL_PC *sd, *b_sd;
  18713. struct script_data *data;
  18714. enum adopt_responses response;
  18715. data = script_getdata(st, 2);
  18716. get_val(st, data);
  18717. if (data_isstring(data)) {
  18718. const char *name = conv_str(st, data);
  18719. sd = map_nick2sd(name);
  18720. if (sd == NULL) {
  18721. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  18722. return SCRIPT_CMD_FAILURE;
  18723. }
  18724. } else if (data_isint(data)) {
  18725. uint32 char_id = conv_num(st, data);
  18726. sd = map_charid2sd(char_id);
  18727. if (sd == NULL) {
  18728. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  18729. return SCRIPT_CMD_FAILURE;
  18730. }
  18731. } else {
  18732. ShowError("buildin_adopt: Invalid data type for argument #1 (%d).", data->type);
  18733. return SCRIPT_CMD_FAILURE;
  18734. }
  18735. data = script_getdata(st, 3);
  18736. get_val(st, data);
  18737. if (data_isstring(data)) {
  18738. const char *name = conv_str(st, data);
  18739. b_sd = map_nick2sd(name);
  18740. if (b_sd == NULL) {
  18741. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  18742. return SCRIPT_CMD_FAILURE;
  18743. }
  18744. } else if (data_isint(data)) {
  18745. uint32 char_id = conv_num(st, data);
  18746. b_sd = map_charid2sd(char_id);
  18747. if (b_sd == NULL) {
  18748. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  18749. return SCRIPT_CMD_FAILURE;
  18750. }
  18751. } else {
  18752. ShowError("buildin_adopt: Invalid data type for argument #2 (%d).", data->type);
  18753. return SCRIPT_CMD_FAILURE;
  18754. }
  18755. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  18756. if (response == ADOPT_ALLOWED) {
  18757. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  18758. b_sd->adopt_invite = sd->status.account_id;
  18759. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  18760. script_pushint(st, ADOPT_ALLOWED);
  18761. return SCRIPT_CMD_SUCCESS;
  18762. }
  18763. script_pushint(st, response);
  18764. return SCRIPT_CMD_FAILURE;
  18765. }
  18766. /**
  18767. * Returns the minimum or maximum of all the given parameters for integer variables.
  18768. *
  18769. * min( <value or array>{,value or array 2,...} );
  18770. * minimum( <value or array>{,value or array 2,...} );
  18771. * max( <value or array>{,value or array 2,...} );
  18772. * maximum( <value or array>{,value or array 2,...} );
  18773. */
  18774. BUILDIN_FUNC(minmax){
  18775. char *functionname;
  18776. unsigned int i;
  18777. int value;
  18778. // Function pointer for our comparison function (either min or max at the moment)
  18779. int32 (*func)(int32, int32);
  18780. // Get the real function name
  18781. functionname = script_getfuncname(st);
  18782. // Our data should start at offset 2
  18783. i = 2;
  18784. if( !script_hasdata( st, i ) ){
  18785. ShowError( "buildin_%s: no arguments given!\n", functionname );
  18786. st->state = END;
  18787. return SCRIPT_CMD_FAILURE;
  18788. }
  18789. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  18790. value = INT_MAX;
  18791. func = i32min;
  18792. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  18793. value = INT_MIN;
  18794. func = i32max;
  18795. }else{
  18796. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  18797. st->state = END;
  18798. return SCRIPT_CMD_FAILURE;
  18799. }
  18800. // As long as we have data on our script stack
  18801. while( script_hasdata(st,i) ){
  18802. struct script_data *data;
  18803. // Get the next piece of data from the script stack
  18804. data = script_getdata( st, i );
  18805. // Is the current parameter a single integer?
  18806. if( data_isint( data ) ){
  18807. value = func( value, script_getnum( st, i ) );
  18808. // Is the current parameter an array variable?
  18809. }else if( data_isreference( data ) ){
  18810. const char *name;
  18811. struct map_session_data* sd;
  18812. unsigned int start, end;
  18813. // Get the name of the variable
  18814. name = reference_getname(data);
  18815. // Check if it's a string variable
  18816. if( is_string_variable( name ) ){
  18817. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  18818. script_reportdata( data );
  18819. st->state = END;
  18820. return SCRIPT_CMD_FAILURE;
  18821. }
  18822. // Get the session data, if a player is attached
  18823. sd = st->rid ? map_id2sd(st->rid) : NULL;
  18824. // Try to find the array's source pointer
  18825. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  18826. ShowError( "buildin_%s: not a array!\n", functionname );
  18827. script_reportdata( data );
  18828. st->state = END;
  18829. return SCRIPT_CMD_FAILURE;
  18830. }
  18831. // Get the start and end indices of the array
  18832. start = reference_getindex( data );
  18833. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  18834. // Skip empty arrays
  18835. if( start < end ){
  18836. int id;
  18837. // For getting the values we need the id of the array
  18838. id = reference_getid( data );
  18839. // Loop through each value stored in the array
  18840. for( ; start < end; start++ ){
  18841. value = func( value, (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) ) );
  18842. script_removetop( st, -1, 0 );
  18843. }
  18844. }
  18845. }else{
  18846. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  18847. script_reportdata( data );
  18848. st->state = END;
  18849. return SCRIPT_CMD_FAILURE;
  18850. }
  18851. // Continue with the next stack entry
  18852. i++;
  18853. }
  18854. script_pushint( st, value );
  18855. return SCRIPT_CMD_SUCCESS;
  18856. }
  18857. /**
  18858. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  18859. * Unlike `getexp` that affected by some adjustments.
  18860. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  18861. * @author [Cydh]
  18862. **/
  18863. BUILDIN_FUNC(getexp2) {
  18864. TBL_PC *sd = NULL;
  18865. int base_exp = script_getnum(st, 2);
  18866. int job_exp = script_getnum(st, 3);
  18867. if (!script_charid2sd(4, sd))
  18868. return SCRIPT_CMD_FAILURE;
  18869. if (base_exp == 0 && job_exp == 0)
  18870. return SCRIPT_CMD_SUCCESS;
  18871. if (base_exp > 0)
  18872. pc_gainexp(sd, NULL, base_exp, 0, 2);
  18873. else if (base_exp < 0)
  18874. pc_lostexp(sd, base_exp * -1, 0);
  18875. if (job_exp > 0)
  18876. pc_gainexp(sd, NULL, 0, job_exp, 2);
  18877. else if (job_exp < 0)
  18878. pc_lostexp(sd, 0, job_exp * -1);
  18879. return SCRIPT_CMD_SUCCESS;
  18880. }
  18881. /**
  18882. * Force stat recalculation of sd
  18883. * recalculatestat;
  18884. * @author [secretdataz]
  18885. **/
  18886. BUILDIN_FUNC(recalculatestat) {
  18887. TBL_PC* sd = script_rid2sd(st);
  18888. if (sd == NULL)
  18889. return SCRIPT_CMD_FAILURE;
  18890. status_calc_pc(sd, SCO_FORCE);
  18891. return SCRIPT_CMD_SUCCESS;
  18892. }
  18893. BUILDIN_FUNC(hateffect){
  18894. #if PACKETVER >= 20150513
  18895. struct map_session_data* sd = script_rid2sd(st);
  18896. bool enable;
  18897. int i, effectID;
  18898. if( sd == NULL )
  18899. return SCRIPT_CMD_FAILURE;
  18900. effectID = script_getnum(st,2);
  18901. enable = script_getnum(st,3) ? true : false;
  18902. ARR_FIND( 0, sd->hatEffectCount, i, sd->hatEffectIDs[i] == effectID );
  18903. if( enable ){
  18904. if( i < sd->hatEffectCount ){
  18905. return SCRIPT_CMD_SUCCESS;
  18906. }
  18907. RECREATE(sd->hatEffectIDs,uint32,sd->hatEffectCount+1);
  18908. sd->hatEffectIDs[sd->hatEffectCount] = effectID;
  18909. sd->hatEffectCount++;
  18910. }else{
  18911. if( i == sd->hatEffectCount ){
  18912. return SCRIPT_CMD_SUCCESS;
  18913. }
  18914. for( ; i < sd->hatEffectCount - 1; i++ ){
  18915. sd->hatEffectIDs[i] = sd->hatEffectIDs[i+1];
  18916. }
  18917. sd->hatEffectCount--;
  18918. if( !sd->hatEffectCount ){
  18919. aFree(sd->hatEffectIDs);
  18920. sd->hatEffectIDs = NULL;
  18921. }
  18922. }
  18923. if( !sd->state.connect_new ){
  18924. clif_hat_effect_single( sd, effectID, enable );
  18925. }
  18926. #endif
  18927. return SCRIPT_CMD_SUCCESS;
  18928. }
  18929. /**
  18930. * Retrieves param of current random option. Intended for random option script only.
  18931. * getrandomoptinfo(<type>);
  18932. * @author [secretdataz]
  18933. **/
  18934. BUILDIN_FUNC(getrandomoptinfo) {
  18935. struct map_session_data *sd;
  18936. int val;
  18937. int param = script_getnum(st, 2);
  18938. if ((sd = script_rid2sd(st)) != NULL && current_equip_item_index != -1 && current_equip_opt_index != -1 && sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id) {
  18939. switch (param) {
  18940. case ROA_ID:
  18941. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  18942. break;
  18943. case ROA_VALUE:
  18944. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  18945. break;
  18946. case ROA_PARAM:
  18947. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  18948. break;
  18949. default:
  18950. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  18951. val = 0;
  18952. break;
  18953. }
  18954. script_pushint(st, val);
  18955. }
  18956. else {
  18957. script_pushint(st, 0);
  18958. }
  18959. return SCRIPT_CMD_SUCCESS;
  18960. }
  18961. /**
  18962. * Retrieves a random option on an equipped item.
  18963. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  18964. * @author [secretdataz]
  18965. */
  18966. BUILDIN_FUNC(getequiprandomoption) {
  18967. struct map_session_data *sd;
  18968. int val;
  18969. short i = -1;
  18970. int pos = script_getnum(st, 2);
  18971. int index = script_getnum(st, 3);
  18972. int type = script_getnum(st, 4);
  18973. if (!script_charid2sd(5, sd)) {
  18974. script_pushint(st, -1);
  18975. return SCRIPT_CMD_FAILURE;
  18976. }
  18977. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  18978. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  18979. script_pushint(st, -1);
  18980. return SCRIPT_CMD_FAILURE;
  18981. }
  18982. if (equip_index_check(pos))
  18983. i = pc_checkequip(sd, equip_bitmask[pos]);
  18984. if (i < 0) {
  18985. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  18986. script_pushint(st, -1);
  18987. return SCRIPT_CMD_FAILURE;
  18988. }
  18989. switch (type) {
  18990. case ROA_ID:
  18991. val = sd->inventory.u.items_inventory[i].option[index].id;
  18992. break;
  18993. case ROA_VALUE:
  18994. val = sd->inventory.u.items_inventory[i].option[index].value;
  18995. break;
  18996. case ROA_PARAM:
  18997. val = sd->inventory.u.items_inventory[i].option[index].param;
  18998. break;
  18999. default:
  19000. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  19001. val = 0;
  19002. break;
  19003. }
  19004. script_pushint(st, val);
  19005. return SCRIPT_CMD_SUCCESS;
  19006. }
  19007. /**
  19008. * Adds a random option on a random option slot on an equipped item and overwrites
  19009. * existing random option in the process.
  19010. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  19011. * @author [secretdataz]
  19012. */
  19013. BUILDIN_FUNC(setrandomoption) {
  19014. struct map_session_data *sd;
  19015. struct s_random_opt_data *opt;
  19016. int pos, index, id, value, param, ep;
  19017. int i = -1;
  19018. if (!script_charid2sd(7, sd))
  19019. return SCRIPT_CMD_FAILURE;
  19020. pos = script_getnum(st, 2);
  19021. index = script_getnum(st, 3);
  19022. id = script_getnum(st, 4);
  19023. value = script_getnum(st, 5);
  19024. param = script_getnum(st, 6);
  19025. if ((opt = itemdb_randomopt_exists((short)id)) == NULL) {
  19026. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  19027. script_pushint(st, 0);
  19028. return SCRIPT_CMD_FAILURE;
  19029. }
  19030. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19031. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  19032. script_pushint(st, 0);
  19033. return SCRIPT_CMD_FAILURE;
  19034. }
  19035. if (equip_index_check(pos))
  19036. i = pc_checkequip(sd, equip_bitmask[pos]);
  19037. if (i >= 0) {
  19038. ep = sd->inventory.u.items_inventory[i].equip;
  19039. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19040. pc_unequipitem(sd, i, 2);
  19041. sd->inventory.u.items_inventory[i].option[index].id = id;
  19042. sd->inventory.u.items_inventory[i].option[index].value = value;
  19043. sd->inventory.u.items_inventory[i].option[index].param = param;
  19044. clif_delitem(sd, i, 1, 3);
  19045. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19046. clif_additem(sd, i, 1, 0);
  19047. pc_equipitem(sd, i, ep);
  19048. script_pushint(st, 1);
  19049. return SCRIPT_CMD_SUCCESS;
  19050. }
  19051. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19052. script_pushint(st, 0);
  19053. return SCRIPT_CMD_FAILURE;
  19054. }
  19055. /// Returns the number of stat points needed to change the specified stat by val.
  19056. /// If val is negative, returns the number of stat points that would be needed to
  19057. /// raise the specified stat from (current value - val) to current value.
  19058. /// *needed_status_point(<type>,<val>{,<char id>});
  19059. /// @author [secretdataz]
  19060. BUILDIN_FUNC(needed_status_point) {
  19061. struct map_session_data *sd;
  19062. int type, val;
  19063. if (!script_charid2sd(4, sd))
  19064. return SCRIPT_CMD_FAILURE;
  19065. type = script_getnum(st, 2);
  19066. val = script_getnum(st, 3);
  19067. script_pushint(st, pc_need_status_point(sd, type, val));
  19068. return SCRIPT_CMD_SUCCESS;
  19069. }
  19070. /**
  19071. * jobcanentermap("<mapname>"{,<JobID>});
  19072. * Check if (player with) JobID can enter the map.
  19073. * @param mapname Map name
  19074. * @param JobID Player's JobID (optional)
  19075. **/
  19076. BUILDIN_FUNC(jobcanentermap) {
  19077. const char *mapname = script_getstr(st, 2);
  19078. int mapidx = mapindex_name2id(mapname), m = -1;
  19079. int jobid = 0;
  19080. TBL_PC *sd = NULL;
  19081. if (!mapidx) {// Invalid map
  19082. script_pushint(st, false);
  19083. return SCRIPT_CMD_FAILURE;
  19084. }
  19085. m = map_mapindex2mapid(mapidx);
  19086. if (m == -1) { // Map is on different map server
  19087. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  19088. script_pushint(st, false);
  19089. return SCRIPT_CMD_FAILURE;
  19090. }
  19091. if (script_hasdata(st, 3)) {
  19092. jobid = script_getnum(st, 3);
  19093. } else {
  19094. if (!(sd = script_rid2sd(st))) {
  19095. script_pushint(st, false);
  19096. return SCRIPT_CMD_FAILURE;
  19097. }
  19098. jobid = sd->status.class_;
  19099. }
  19100. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? sd->group_level : 0));
  19101. return SCRIPT_CMD_SUCCESS;
  19102. }
  19103. /**
  19104. * Return alliance information between the two guilds.
  19105. * getguildalliance(<guild id1>,<guild id2>);
  19106. * Return values:
  19107. * -2 - Guild ID1 does not exist
  19108. * -1 - Guild ID2 does not exist
  19109. * 0 - Both guilds have no relation OR guild ID aren't given
  19110. * 1 - Both guilds are allies
  19111. * 2 - Both guilds are antagonists
  19112. */
  19113. BUILDIN_FUNC(getguildalliance)
  19114. {
  19115. struct guild *guild_data1, *guild_data2;
  19116. int guild_id1, guild_id2, i = 0;
  19117. guild_id1 = script_getnum(st,2);
  19118. guild_id2 = script_getnum(st,3);
  19119. if (guild_id1 < 1 || guild_id2 < 1) {
  19120. script_pushint(st, 0);
  19121. return SCRIPT_CMD_SUCCESS;
  19122. }
  19123. if (guild_id1 == guild_id2) {
  19124. script_pushint(st, 1);
  19125. return SCRIPT_CMD_SUCCESS;
  19126. }
  19127. guild_data1 = guild_search(guild_id1);
  19128. guild_data2 = guild_search(guild_id2);
  19129. if (guild_data1 == NULL) {
  19130. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  19131. script_pushint(st, -2);
  19132. return SCRIPT_CMD_FAILURE;
  19133. }
  19134. if (guild_data2 == NULL) {
  19135. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  19136. script_pushint(st, -1);
  19137. return SCRIPT_CMD_FAILURE;
  19138. }
  19139. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  19140. if (i == MAX_GUILDALLIANCE) {
  19141. script_pushint(st, 0);
  19142. return SCRIPT_CMD_SUCCESS;
  19143. }
  19144. if (guild_data1->alliance[i].opposition)
  19145. script_pushint(st, 2);
  19146. else
  19147. script_pushint(st, 1);
  19148. return SCRIPT_CMD_SUCCESS;
  19149. }
  19150. /*
  19151. * openstorage2 <storage_id>,<mode>{,<account_id>}
  19152. * mode @see enum e_storage_mode
  19153. **/
  19154. BUILDIN_FUNC(openstorage2) {
  19155. int stor_id = script_getnum(st, 2);
  19156. TBL_PC *sd = NULL;
  19157. if (!script_accid2sd(4, sd)) {
  19158. script_pushint(st, 0);
  19159. return SCRIPT_CMD_FAILURE;
  19160. }
  19161. if (!storage_exists(stor_id)) {
  19162. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  19163. return SCRIPT_CMD_FAILURE;
  19164. }
  19165. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  19166. return SCRIPT_CMD_SUCCESS;
  19167. }
  19168. #include "../custom/script.inc"
  19169. // declarations that were supposed to be exported from npc_chat.c
  19170. #ifdef PCRE_SUPPORT
  19171. BUILDIN_FUNC(defpattern);
  19172. BUILDIN_FUNC(activatepset);
  19173. BUILDIN_FUNC(deactivatepset);
  19174. BUILDIN_FUNC(deletepset);
  19175. #endif
  19176. /** Regular expression matching
  19177. * preg_match(<pattern>,<string>{,<offset>})
  19178. */
  19179. BUILDIN_FUNC(preg_match) {
  19180. #ifdef PCRE_SUPPORT
  19181. pcre *re;
  19182. pcre_extra *pcreExtra;
  19183. const char *error;
  19184. int erroffset, r, offset = 0;
  19185. int subStrVec[30];
  19186. const char* pattern = script_getstr(st,2);
  19187. const char* subject = script_getstr(st,3);
  19188. if (script_hasdata(st,4))
  19189. offset = script_getnum(st,4);
  19190. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  19191. pcreExtra = pcre_study(re, 0, &error);
  19192. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  19193. pcre_free(re);
  19194. if (pcreExtra != NULL)
  19195. pcre_free(pcreExtra);
  19196. if (r < 0)
  19197. script_pushint(st,0);
  19198. else
  19199. script_pushint(st,(r > 0) ? r : 30 / 3);
  19200. return SCRIPT_CMD_SUCCESS;
  19201. #else
  19202. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  19203. script_pushint(st,0);
  19204. return SCRIPT_CMD_SUCCESS;
  19205. #endif
  19206. }
  19207. /// script command definitions
  19208. /// for an explanation on args, see add_buildin_func
  19209. struct script_function buildin_func[] = {
  19210. // NPC interaction
  19211. BUILDIN_DEF(mes,"s*"),
  19212. BUILDIN_DEF(next,""),
  19213. BUILDIN_DEF(close,""),
  19214. BUILDIN_DEF(close2,""),
  19215. BUILDIN_DEF(menu,"sl*"),
  19216. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  19217. BUILDIN_DEF(prompt,"s*"),
  19218. //
  19219. BUILDIN_DEF(goto,"l"),
  19220. BUILDIN_DEF(callsub,"l*"),
  19221. BUILDIN_DEF(callfunc,"s*"),
  19222. BUILDIN_DEF(return,"?"),
  19223. BUILDIN_DEF(getarg,"i?"),
  19224. BUILDIN_DEF(jobchange,"i??"),
  19225. BUILDIN_DEF(jobname,"i"),
  19226. BUILDIN_DEF(input,"r??"),
  19227. BUILDIN_DEF(warp,"sii?"),
  19228. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  19229. BUILDIN_DEF(areawarp,"siiiisii??"),
  19230. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  19231. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  19232. BUILDIN_DEF(setlook,"ii?"),
  19233. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  19234. BUILDIN_DEF2(setr,"set","rv?"),
  19235. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  19236. BUILDIN_DEF(setarray,"rv*"),
  19237. BUILDIN_DEF(cleararray,"rvi"),
  19238. BUILDIN_DEF(copyarray,"rri"),
  19239. BUILDIN_DEF(getarraysize,"r"),
  19240. BUILDIN_DEF(deletearray,"r?"),
  19241. BUILDIN_DEF(getelementofarray,"ri"),
  19242. BUILDIN_DEF(getitem,"vi?"),
  19243. BUILDIN_DEF(rentitem,"vi?"),
  19244. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  19245. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  19246. BUILDIN_DEF(getnameditem,"vv"),
  19247. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  19248. BUILDIN_DEF(makeitem,"visii"),
  19249. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  19250. BUILDIN_DEF(delitem,"vi?"),
  19251. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  19252. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  19253. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  19254. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  19255. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  19256. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  19257. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  19258. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  19259. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  19260. BUILDIN_DEF(cutin,"si"),
  19261. BUILDIN_DEF(viewpoint,"iiiii"),
  19262. BUILDIN_DEF(heal,"ii?"),
  19263. BUILDIN_DEF(itemheal,"ii?"),
  19264. BUILDIN_DEF(percentheal,"ii?"),
  19265. BUILDIN_DEF(rand,"i?"),
  19266. BUILDIN_DEF(countitem,"v?"),
  19267. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  19268. BUILDIN_DEF2(countitem,"guildstoragecountitem","v?"),
  19269. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  19270. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  19271. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  19272. BUILDIN_DEF2(countitem,"guildstoragecountitem2","viiiiiii?"),
  19273. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  19274. BUILDIN_DEF(checkweight,"vi*"),
  19275. BUILDIN_DEF(checkweight2,"rr"),
  19276. BUILDIN_DEF(readparam,"i?"),
  19277. BUILDIN_DEF(getcharid,"i?"),
  19278. BUILDIN_DEF(getnpcid,"i?"),
  19279. BUILDIN_DEF(getpartyname,"i"),
  19280. BUILDIN_DEF(getpartymember,"i??"),
  19281. BUILDIN_DEF(getpartyleader,"i?"),
  19282. BUILDIN_DEF(getguildname,"i"),
  19283. BUILDIN_DEF(getguildmaster,"i"),
  19284. BUILDIN_DEF(getguildmasterid,"i"),
  19285. BUILDIN_DEF(strcharinfo,"i?"),
  19286. BUILDIN_DEF(strnpcinfo,"i"),
  19287. BUILDIN_DEF(getequipid,"i?"),
  19288. BUILDIN_DEF(getequipuniqueid,"i?"),
  19289. BUILDIN_DEF(getequipname,"i?"),
  19290. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  19291. BUILDIN_DEF(repair,"i?"), // [Valaris]
  19292. BUILDIN_DEF(repairall,"?"),
  19293. BUILDIN_DEF(getequipisequiped,"i?"),
  19294. BUILDIN_DEF(getequipisenableref,"i?"),
  19295. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  19296. BUILDIN_DEF(getequipweaponlv,"i?"),
  19297. BUILDIN_DEF(getequippercentrefinery,"i?"),
  19298. BUILDIN_DEF(successrefitem,"i??"),
  19299. BUILDIN_DEF(failedrefitem,"i?"),
  19300. BUILDIN_DEF(downrefitem,"i??"),
  19301. BUILDIN_DEF(statusup,"i?"),
  19302. BUILDIN_DEF(statusup2,"ii?"),
  19303. BUILDIN_DEF(bonus,"iv"),
  19304. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  19305. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  19306. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  19307. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  19308. BUILDIN_DEF(autobonus,"sii??"),
  19309. BUILDIN_DEF(autobonus2,"sii??"),
  19310. BUILDIN_DEF(autobonus3,"siiv?"),
  19311. BUILDIN_DEF(skill,"vi?"),
  19312. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  19313. BUILDIN_DEF(guildskill,"vi"),
  19314. BUILDIN_DEF(getskilllv,"v"),
  19315. BUILDIN_DEF(getgdskilllv,"iv"),
  19316. BUILDIN_DEF(basicskillcheck,""),
  19317. BUILDIN_DEF(getgmlevel,"?"),
  19318. BUILDIN_DEF(getgroupid,"?"),
  19319. BUILDIN_DEF(end,""),
  19320. BUILDIN_DEF(checkoption,"i?"),
  19321. BUILDIN_DEF(setoption,"i??"),
  19322. BUILDIN_DEF(setcart,"??"),
  19323. BUILDIN_DEF(checkcart,"?"),
  19324. BUILDIN_DEF(setfalcon,"??"),
  19325. BUILDIN_DEF(checkfalcon,"?"),
  19326. BUILDIN_DEF(setriding,"??"),
  19327. BUILDIN_DEF(checkriding,"?"),
  19328. BUILDIN_DEF(checkwug,"?"),
  19329. BUILDIN_DEF(checkmadogear,"?"),
  19330. BUILDIN_DEF(setmadogear,"??"),
  19331. BUILDIN_DEF2(savepoint,"save","sii?"),
  19332. BUILDIN_DEF(savepoint,"sii?"),
  19333. BUILDIN_DEF(gettimetick,"i"),
  19334. BUILDIN_DEF(gettime,"i"),
  19335. BUILDIN_DEF(gettimestr,"si"),
  19336. BUILDIN_DEF(openstorage,""),
  19337. BUILDIN_DEF(guildopenstorage,""),
  19338. BUILDIN_DEF(itemskill,"vi?"),
  19339. BUILDIN_DEF(produce,"i"),
  19340. BUILDIN_DEF(cooking,"i"),
  19341. BUILDIN_DEF(monster,"siisii???"),
  19342. BUILDIN_DEF(getmobdrops,"i"),
  19343. BUILDIN_DEF(areamonster,"siiiisii???"),
  19344. BUILDIN_DEF(killmonster,"ss?"),
  19345. BUILDIN_DEF(killmonsterall,"s?"),
  19346. BUILDIN_DEF(clone,"siisi????"),
  19347. BUILDIN_DEF(doevent,"s"),
  19348. BUILDIN_DEF(donpcevent,"s"),
  19349. BUILDIN_DEF(cmdothernpc,"ss"),
  19350. BUILDIN_DEF(addtimer,"is"),
  19351. BUILDIN_DEF(deltimer,"s"),
  19352. BUILDIN_DEF(addtimercount,"is"),
  19353. BUILDIN_DEF(initnpctimer,"??"),
  19354. BUILDIN_DEF(stopnpctimer,"??"),
  19355. BUILDIN_DEF(startnpctimer,"??"),
  19356. BUILDIN_DEF(setnpctimer,"i?"),
  19357. BUILDIN_DEF(getnpctimer,"i?"),
  19358. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  19359. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  19360. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  19361. BUILDIN_DEF(announce,"si?????"),
  19362. BUILDIN_DEF(mapannounce,"ssi?????"),
  19363. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  19364. BUILDIN_DEF(getusers,"i"),
  19365. BUILDIN_DEF(getmapguildusers,"si"),
  19366. BUILDIN_DEF(getmapusers,"s"),
  19367. BUILDIN_DEF(getareausers,"siiii"),
  19368. BUILDIN_DEF(getareadropitem,"siiiiv"),
  19369. BUILDIN_DEF(enablenpc,"s"),
  19370. BUILDIN_DEF(disablenpc,"s"),
  19371. BUILDIN_DEF(hideoffnpc,"s"),
  19372. BUILDIN_DEF(hideonnpc,"s"),
  19373. BUILDIN_DEF(sc_start,"iii???"),
  19374. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  19375. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  19376. BUILDIN_DEF(sc_end,"i?"),
  19377. BUILDIN_DEF(getstatus, "i??"),
  19378. BUILDIN_DEF(getscrate,"ii?"),
  19379. BUILDIN_DEF(debugmes,"s"),
  19380. BUILDIN_DEF2(catchpet,"pet","i"),
  19381. BUILDIN_DEF2(birthpet,"bpet",""),
  19382. BUILDIN_DEF(resetlvl,"i?"),
  19383. BUILDIN_DEF(resetstatus,"?"),
  19384. BUILDIN_DEF(resetskill,"?"),
  19385. BUILDIN_DEF(skillpointcount,"?"),
  19386. BUILDIN_DEF(changebase,"i?"),
  19387. BUILDIN_DEF(changesex,"?"),
  19388. BUILDIN_DEF(changecharsex,"?"),
  19389. BUILDIN_DEF(waitingroom,"si?????"),
  19390. BUILDIN_DEF(delwaitingroom,"?"),
  19391. BUILDIN_DEF(waitingroomkick,"ss"),
  19392. BUILDIN_DEF(getwaitingroomusers, "?"),
  19393. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  19394. BUILDIN_DEF(enablewaitingroomevent,"?"),
  19395. BUILDIN_DEF(disablewaitingroomevent,"?"),
  19396. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  19397. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  19398. BUILDIN_DEF(getwaitingroomstate,"i?"),
  19399. BUILDIN_DEF(warpwaitingpc,"sii?"),
  19400. BUILDIN_DEF(attachrid,"i"),
  19401. BUILDIN_DEF(addrid,"i?????"),
  19402. BUILDIN_DEF(detachrid,""),
  19403. BUILDIN_DEF(isloggedin,"i?"),
  19404. BUILDIN_DEF(setmapflagnosave,"ssii"),
  19405. BUILDIN_DEF(getmapflag,"si?"),
  19406. BUILDIN_DEF(setmapflag,"si??"),
  19407. BUILDIN_DEF(removemapflag,"si?"),
  19408. BUILDIN_DEF(pvpon,"s"),
  19409. BUILDIN_DEF(pvpoff,"s"),
  19410. BUILDIN_DEF(gvgon,"s"),
  19411. BUILDIN_DEF(gvgoff,"s"),
  19412. BUILDIN_DEF(emotion,"i??"),
  19413. BUILDIN_DEF(maprespawnguildid,"sii"),
  19414. BUILDIN_DEF(agitstart,""), // <Agit>
  19415. BUILDIN_DEF(agitend,""),
  19416. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  19417. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  19418. BUILDIN_DEF(getcastlename,"s"),
  19419. BUILDIN_DEF(getcastledata,"si"),
  19420. BUILDIN_DEF(setcastledata,"sii"),
  19421. BUILDIN_DEF(requestguildinfo,"i?"),
  19422. BUILDIN_DEF(getequipcardcnt,"i"),
  19423. BUILDIN_DEF(successremovecards,"i"),
  19424. BUILDIN_DEF(failedremovecards,"ii"),
  19425. BUILDIN_DEF(marriage,"s"),
  19426. BUILDIN_DEF2(wedding_effect,"wedding",""),
  19427. BUILDIN_DEF(divorce,"?"),
  19428. BUILDIN_DEF(ispartneron,"?"),
  19429. BUILDIN_DEF(getpartnerid,"?"),
  19430. BUILDIN_DEF(getchildid,"?"),
  19431. BUILDIN_DEF(getmotherid,"?"),
  19432. BUILDIN_DEF(getfatherid,"?"),
  19433. BUILDIN_DEF(warppartner,"sii"),
  19434. BUILDIN_DEF(getitemname,"v"),
  19435. BUILDIN_DEF(getitemslots,"i"),
  19436. BUILDIN_DEF(makepet,"i"),
  19437. BUILDIN_DEF(getexp,"ii?"),
  19438. BUILDIN_DEF(getinventorylist,"?"),
  19439. BUILDIN_DEF(getskilllist,"?"),
  19440. BUILDIN_DEF(clearitem,"?"),
  19441. BUILDIN_DEF(classchange,"ii"),
  19442. BUILDIN_DEF(misceffect,"i"),
  19443. BUILDIN_DEF(playBGM,"s"),
  19444. BUILDIN_DEF(playBGMall,"s?????"),
  19445. BUILDIN_DEF(soundeffect,"si"),
  19446. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  19447. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  19448. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  19449. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  19450. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  19451. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  19452. BUILDIN_DEF(petloot,"i"), // [Valaris]
  19453. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  19454. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  19455. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  19456. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  19457. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  19458. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  19459. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  19460. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  19461. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  19462. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  19463. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  19464. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  19465. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  19466. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  19467. BUILDIN_DEF(npctalk,"s?"), // [Valaris]
  19468. BUILDIN_DEF(mobcount,"ss"),
  19469. BUILDIN_DEF(getlook,"i?"),
  19470. BUILDIN_DEF(getsavepoint,"i?"),
  19471. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  19472. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  19473. BUILDIN_DEF(npcstop,""), // [Valaris]
  19474. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  19475. BUILDIN_DEF(mapid2name,"i"),
  19476. BUILDIN_DEF(checkoption1,"i?"),
  19477. BUILDIN_DEF(checkoption2,"i?"),
  19478. BUILDIN_DEF(guildgetexp,"i"),
  19479. BUILDIN_DEF(guildchangegm,"is"),
  19480. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  19481. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  19482. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  19483. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  19484. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  19485. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  19486. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  19487. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  19488. BUILDIN_DEF(night,""), // sets the server to night time
  19489. BUILDIN_DEF(day,""), // sets the server to day time
  19490. #ifdef PCRE_SUPPORT
  19491. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  19492. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  19493. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  19494. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  19495. #endif
  19496. BUILDIN_DEF(preg_match,"ss?"),
  19497. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  19498. BUILDIN_DEF(recovery,"i???"),
  19499. BUILDIN_DEF(getpetinfo,"i?"),
  19500. BUILDIN_DEF(gethominfo,"i?"),
  19501. BUILDIN_DEF(getmercinfo,"i?"),
  19502. BUILDIN_DEF(checkequipedcard,"i"),
  19503. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  19504. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  19505. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  19506. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  19507. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  19508. BUILDIN_DEF(charat,"si"),
  19509. BUILDIN_DEF(setchar,"ssi"),
  19510. BUILDIN_DEF(insertchar,"ssi"),
  19511. BUILDIN_DEF(delchar,"si"),
  19512. BUILDIN_DEF(strtoupper,"s"),
  19513. BUILDIN_DEF(strtolower,"s"),
  19514. BUILDIN_DEF(charisupper, "si"),
  19515. BUILDIN_DEF(charislower, "si"),
  19516. BUILDIN_DEF(substr,"sii"),
  19517. BUILDIN_DEF(explode, "rss"),
  19518. BUILDIN_DEF(implode, "r?"),
  19519. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  19520. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  19521. BUILDIN_DEF(strpos,"ss?"),
  19522. BUILDIN_DEF(replacestr,"sss??"),
  19523. BUILDIN_DEF(countstr,"ss?"),
  19524. BUILDIN_DEF(setnpcdisplay,"sv??"),
  19525. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  19526. BUILDIN_DEF(strcmp,"ss"),
  19527. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  19528. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  19529. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  19530. // [zBuffer] List of mathematics commands --->
  19531. BUILDIN_DEF(sqrt,"i"),
  19532. BUILDIN_DEF(pow,"ii"),
  19533. BUILDIN_DEF(distance,"iiii"),
  19534. BUILDIN_DEF2(minmax,"min", "*"),
  19535. BUILDIN_DEF2(minmax,"minimum", "*"),
  19536. BUILDIN_DEF2(minmax,"max", "*"),
  19537. BUILDIN_DEF2(minmax,"maximum", "*"),
  19538. // <--- [zBuffer] List of mathematics commands
  19539. BUILDIN_DEF(md5,"s"),
  19540. // [zBuffer] List of dynamic var commands --->
  19541. BUILDIN_DEF(getd,"s"),
  19542. BUILDIN_DEF(setd,"sv?"),
  19543. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  19544. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  19545. BUILDIN_DEF(npcshopadditem,"sii*"),
  19546. BUILDIN_DEF(npcshopdelitem,"si*"),
  19547. BUILDIN_DEF(npcshopattach,"s?"),
  19548. BUILDIN_DEF(equip,"i?"),
  19549. BUILDIN_DEF(autoequip,"ii"),
  19550. BUILDIN_DEF(setbattleflag,"si"),
  19551. BUILDIN_DEF(getbattleflag,"s"),
  19552. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  19553. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  19554. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  19555. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  19556. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  19557. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  19558. BUILDIN_DEF(axtoi,"s"),
  19559. BUILDIN_DEF(query_sql,"s*"),
  19560. BUILDIN_DEF(query_logsql,"s*"),
  19561. BUILDIN_DEF(escape_sql,"v"),
  19562. BUILDIN_DEF(atoi,"s"),
  19563. BUILDIN_DEF(strtol,"si"),
  19564. // [zBuffer] List of player cont commands --->
  19565. BUILDIN_DEF(rid2name,"i"),
  19566. BUILDIN_DEF(pcfollow,"ii"),
  19567. BUILDIN_DEF(pcstopfollow,"i"),
  19568. BUILDIN_DEF(pcblockmove,"ii"),
  19569. // <--- [zBuffer] List of player cont commands
  19570. // [zBuffer] List of unit control commands --->
  19571. BUILDIN_DEF(unitexists,"i"),
  19572. BUILDIN_DEF(getunittype,"i"),
  19573. BUILDIN_DEF(getunitname,"i"),
  19574. BUILDIN_DEF(setunitname,"is"),
  19575. BUILDIN_DEF(getunitdata,"i*"),
  19576. BUILDIN_DEF(setunitdata,"iii"),
  19577. BUILDIN_DEF(unitwalk,"iii?"),
  19578. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  19579. BUILDIN_DEF(unitkill,"i"),
  19580. BUILDIN_DEF(unitwarp,"isii"),
  19581. BUILDIN_DEF(unitattack,"iv?"),
  19582. BUILDIN_DEF(unitstopattack,"i"),
  19583. BUILDIN_DEF(unitstopwalk,"i"),
  19584. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  19585. BUILDIN_DEF(unittalk,"is"),
  19586. BUILDIN_DEF(unitemote,"ii"),
  19587. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  19588. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  19589. // <--- [zBuffer] List of unit control commands
  19590. BUILDIN_DEF(sleep,"i"),
  19591. BUILDIN_DEF(sleep2,"i"),
  19592. BUILDIN_DEF(awake,"s"),
  19593. BUILDIN_DEF(getvariableofnpc,"rs"),
  19594. BUILDIN_DEF(warpportal,"iisii"),
  19595. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  19596. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  19597. BUILDIN_DEF(morphembryo,""),
  19598. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  19599. BUILDIN_DEF(checkhomcall,""),
  19600. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  19601. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  19602. BUILDIN_DEF(checkvending,"?"),
  19603. BUILDIN_DEF(checkchatting,"?"),
  19604. BUILDIN_DEF(checkidle,"?"),
  19605. BUILDIN_DEF(openmail,"?"),
  19606. BUILDIN_DEF(openauction,"?"),
  19607. BUILDIN_DEF(checkcell,"siii"),
  19608. BUILDIN_DEF(setcell,"siiiiii"),
  19609. BUILDIN_DEF(setwall,"siiiiis"),
  19610. BUILDIN_DEF(delwall,"s"),
  19611. BUILDIN_DEF(searchitem,"rs"),
  19612. BUILDIN_DEF(mercenary_create,"ii"),
  19613. BUILDIN_DEF(mercenary_heal,"ii"),
  19614. BUILDIN_DEF(mercenary_sc_start,"iii"),
  19615. BUILDIN_DEF(mercenary_get_calls,"i"),
  19616. BUILDIN_DEF(mercenary_get_faith,"i"),
  19617. BUILDIN_DEF(mercenary_set_calls,"ii"),
  19618. BUILDIN_DEF(mercenary_set_faith,"ii"),
  19619. BUILDIN_DEF(readbook,"ii"),
  19620. BUILDIN_DEF(setfont,"i"),
  19621. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  19622. BUILDIN_DEF(progressbar,"si"),
  19623. BUILDIN_DEF(pushpc,"ii"),
  19624. BUILDIN_DEF(buyingstore,"i"),
  19625. BUILDIN_DEF(searchstores,"ii"),
  19626. BUILDIN_DEF(showdigit,"i?"),
  19627. // WoE SE
  19628. BUILDIN_DEF(agitstart2,""),
  19629. BUILDIN_DEF(agitend2,""),
  19630. BUILDIN_DEF(agitcheck2,""),
  19631. // BattleGround
  19632. BUILDIN_DEF(waitingroom2bg,"sii???"),
  19633. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  19634. BUILDIN_DEF(bg_team_setxy,"iii"),
  19635. BUILDIN_DEF(bg_warp,"isii"),
  19636. BUILDIN_DEF(bg_monster,"isiisi?"),
  19637. BUILDIN_DEF(bg_monster_set_team,"ii"),
  19638. BUILDIN_DEF(bg_leave,"?"),
  19639. BUILDIN_DEF(bg_destroy,"i"),
  19640. BUILDIN_DEF(areapercentheal,"siiiiii"),
  19641. BUILDIN_DEF(bg_get_data,"ii"),
  19642. BUILDIN_DEF(bg_getareausers,"isiiii"),
  19643. BUILDIN_DEF(bg_updatescore,"sii"),
  19644. BUILDIN_DEF(bg_join,"i????"),
  19645. BUILDIN_DEF(bg_create,"sii??"),
  19646. // Instancing
  19647. BUILDIN_DEF(instance_create,"s??"),
  19648. BUILDIN_DEF(instance_destroy,"?"),
  19649. BUILDIN_DEF(instance_id,""),
  19650. BUILDIN_DEF(instance_enter,"s???"),
  19651. BUILDIN_DEF(instance_npcname,"s?"),
  19652. BUILDIN_DEF(instance_mapname,"s?"),
  19653. BUILDIN_DEF(instance_warpall,"sii?"),
  19654. BUILDIN_DEF(instance_announce,"isi?????"),
  19655. BUILDIN_DEF(instance_check_party,"i???"),
  19656. BUILDIN_DEF(instance_check_guild,"i???"),
  19657. /**
  19658. * 3rd-related
  19659. **/
  19660. BUILDIN_DEF(makerune,"i?"),
  19661. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  19662. BUILDIN_DEF(setdragon,"??"),//[Ind]
  19663. BUILDIN_DEF(ismounting,"?"),//[Ind]
  19664. BUILDIN_DEF(setmounting,"?"),//[Ind]
  19665. BUILDIN_DEF(checkre,"i"),
  19666. /**
  19667. * rAthena and beyond!
  19668. **/
  19669. BUILDIN_DEF(getargcount,""),
  19670. BUILDIN_DEF(getcharip,"?"),
  19671. BUILDIN_DEF(is_function,"s"),
  19672. BUILDIN_DEF(get_revision,""),
  19673. BUILDIN_DEF(get_githash,""),
  19674. BUILDIN_DEF(freeloop,"?"),
  19675. BUILDIN_DEF(getrandgroupitem,"i??"),
  19676. BUILDIN_DEF(cleanmap,"s"),
  19677. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  19678. BUILDIN_DEF(npcskill,"viii"),
  19679. BUILDIN_DEF(consumeitem,"v?"),
  19680. BUILDIN_DEF(delequip,"i?"),
  19681. BUILDIN_DEF(breakequip,"i?"),
  19682. BUILDIN_DEF(sit,"?"),
  19683. BUILDIN_DEF(stand,"?"),
  19684. //@commands (script based)
  19685. BUILDIN_DEF(bindatcmd, "ss??"),
  19686. BUILDIN_DEF(unbindatcmd, "s"),
  19687. BUILDIN_DEF(useatcmd, "s"),
  19688. //Quest Log System [Inkfish]
  19689. BUILDIN_DEF(questinfo, "ii??"),
  19690. BUILDIN_DEF(setquest, "i?"),
  19691. BUILDIN_DEF(erasequest, "i?"),
  19692. BUILDIN_DEF(completequest, "i?"),
  19693. BUILDIN_DEF(checkquest, "i??"),
  19694. BUILDIN_DEF(isbegin_quest,"i?"),
  19695. BUILDIN_DEF(changequest, "ii?"),
  19696. BUILDIN_DEF(showevent, "i??"),
  19697. //Bound items [Xantara] & [Akinari]
  19698. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  19699. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  19700. BUILDIN_DEF(countbound, "??"),
  19701. // Party related
  19702. BUILDIN_DEF(party_create,"s???"),
  19703. BUILDIN_DEF(party_addmember,"ii"),
  19704. BUILDIN_DEF(party_delmember,"??"),
  19705. BUILDIN_DEF(party_changeleader,"ii"),
  19706. BUILDIN_DEF(party_changeoption,"iii"),
  19707. BUILDIN_DEF(party_destroy,"i"),
  19708. BUILDIN_DEF(is_clientver,"ii?"),
  19709. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  19710. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  19711. BUILDIN_DEF(vip_status,"i?"),
  19712. BUILDIN_DEF(vip_time,"i?"),
  19713. BUILDIN_DEF(bonus_script,"si????"),
  19714. BUILDIN_DEF(bonus_script_clear,"??"),
  19715. BUILDIN_DEF(getgroupitem,"i?"),
  19716. BUILDIN_DEF(enable_command,""),
  19717. BUILDIN_DEF(disable_command,""),
  19718. BUILDIN_DEF(getguildmember,"i??"),
  19719. BUILDIN_DEF(addspiritball,"ii?"),
  19720. BUILDIN_DEF(delspiritball,"i?"),
  19721. BUILDIN_DEF(countspiritball,"?"),
  19722. BUILDIN_DEF(mergeitem,"?"),
  19723. BUILDIN_DEF(mergeitem2,"??"),
  19724. BUILDIN_DEF(npcshopupdate,"sii?"),
  19725. BUILDIN_DEF(getattachedrid,""),
  19726. BUILDIN_DEF(getvar,"vi"),
  19727. BUILDIN_DEF(showscript,"s?"),
  19728. BUILDIN_DEF(ignoretimeout,"i?"),
  19729. BUILDIN_DEF(geteleminfo,"i?"),
  19730. BUILDIN_DEF(setquestinfo_level,"iii"),
  19731. BUILDIN_DEF(setquestinfo_req,"iii*"),
  19732. BUILDIN_DEF(setquestinfo_job,"ii*"),
  19733. BUILDIN_DEF(opendressroom,"i?"),
  19734. BUILDIN_DEF(navigateto,"s???????"),
  19735. BUILDIN_DEF(getguildalliance,"ii"),
  19736. BUILDIN_DEF(adopt,"vv"),
  19737. BUILDIN_DEF(getexp2,"ii?"),
  19738. BUILDIN_DEF(recalculatestat,""),
  19739. BUILDIN_DEF(hateffect,"ii"),
  19740. BUILDIN_DEF(getrandomoptinfo, "i"),
  19741. BUILDIN_DEF(getequiprandomoption, "iii?"),
  19742. BUILDIN_DEF(setrandomoption,"iiiii?"),
  19743. BUILDIN_DEF(needed_status_point,"ii?"),
  19744. BUILDIN_DEF(jobcanentermap,"s?"),
  19745. BUILDIN_DEF(openstorage2,"ii?"),
  19746. // WoE TE
  19747. BUILDIN_DEF(agitstart3,""),
  19748. BUILDIN_DEF(agitend3,""),
  19749. BUILDIN_DEF(agitcheck3,""),
  19750. BUILDIN_DEF(gvgon3,"s"),
  19751. BUILDIN_DEF(gvgoff3,"s"),
  19752. #include "../custom/script_def.inc"
  19753. {NULL,NULL,NULL},
  19754. };