script.c 549 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235
  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. #include "../common/cbasetypes.h"
  9. #include "../common/malloc.h"
  10. #include "../common/md5calc.h"
  11. #include "../common/nullpo.h"
  12. #include "../common/random.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/socket.h"
  15. #include "../common/strlib.h"
  16. #include "../common/timer.h"
  17. #include "../common/utils.h"
  18. #include "map.h"
  19. #include "path.h"
  20. #include "clif.h"
  21. #include "chrif.h"
  22. #include "itemdb.h"
  23. #include "pc.h"
  24. #include "storage.h"
  25. #include "pet.h"
  26. #include "mapreg.h"
  27. #include "homunculus.h"
  28. #include "instance.h"
  29. #include "mercenary.h"
  30. #include "intif.h"
  31. #include "chat.h"
  32. #include "battleground.h"
  33. #include "party.h"
  34. #include "mail.h"
  35. #include "script.h"
  36. #include "quest.h"
  37. #include "elemental.h"
  38. #ifdef PCRE_SUPPORT
  39. #include "../../3rdparty/pcre/include/pcre.h" // preg_match
  40. #endif
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <math.h>
  45. #ifndef WIN32
  46. #endif
  47. #include <setjmp.h>
  48. #include <errno.h>
  49. #ifdef BETA_THREAD_TEST
  50. #include "../common/atomic.h"
  51. #include "../common/spinlock.h"
  52. #include "../common/thread.h"
  53. #include "../common/mutex.h"
  54. #endif
  55. TBL_PC *script_rid2sd(struct script_state *st);
  56. /**
  57. * Get `sd` from a account id in `loc` param instead of attached rid
  58. * @param st Script
  59. * @param loc Location to look account id in script parameter
  60. * @param sd Variable that will be assigned
  61. * @return True if `sd` is assigned, false otherwise
  62. **/
  63. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  64. if (script_hasdata(st, loc)) {
  65. int id_ = script_getnum(st, loc);
  66. if (!(*sd = map_id2sd(id_)))
  67. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  68. }
  69. else
  70. *sd = script_rid2sd(st);
  71. return (*sd) ? true : false;
  72. }
  73. /**
  74. * Get `sd` from a char id in `loc` param instead of attached rid
  75. * @param st Script
  76. * @param loc Location to look char id in script parameter
  77. * @param sd Variable that will be assigned
  78. * @return True if `sd` is assigned, false otherwise
  79. **/
  80. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  81. if (script_hasdata(st, loc)) {
  82. int id_ = script_getnum(st, loc);
  83. if (!(*sd = map_charid2sd(id_)))
  84. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  85. }
  86. else
  87. *sd = script_rid2sd(st);
  88. return (*sd) ? true : false;
  89. }
  90. /**
  91. * Get `sd` from a nick in `loc` param instead of attached rid
  92. * @param st Script
  93. * @param loc Location to look nick in script parameter
  94. * @param sd Variable that will be assigned
  95. * @return True if `sd` is assigned, false otherwise
  96. **/
  97. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  98. if (script_hasdata(st, loc)) {
  99. const char *name_ = script_getstr(st, loc);
  100. if (!(*sd = map_nick2sd(name_)))
  101. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  102. }
  103. else
  104. *sd = script_rid2sd(st);
  105. return (*sd) ? true : false;
  106. }
  107. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  108. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  109. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  110. /// temporary buffer for passing around compiled bytecode
  111. /// @see add_scriptb, set_label, parse_script
  112. static unsigned char* script_buf = NULL;
  113. static int script_pos = 0, script_size = 0;
  114. static inline int GETVALUE(const unsigned char* buf, int i)
  115. {
  116. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  117. }
  118. static inline void SETVALUE(unsigned char* buf, int i, int n)
  119. {
  120. buf[i] = GetByte(n, 0);
  121. buf[i+1] = GetByte(n, 1);
  122. buf[i+2] = GetByte(n, 2);
  123. }
  124. // String buffer structures.
  125. // str_data stores string information
  126. static struct str_data_struct {
  127. enum c_op type;
  128. int str;
  129. int backpatch;
  130. int label;
  131. int (*func)(struct script_state *st);
  132. int val;
  133. int next;
  134. } *str_data = NULL;
  135. static int str_data_size = 0; // size of the data
  136. static int str_num = LABEL_START; // next id to be assigned
  137. // str_buf holds the strings themselves
  138. static char *str_buf;
  139. static int str_size = 0; // size of the buffer
  140. static int str_pos = 0; // next position to be assigned
  141. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  142. #define SCRIPT_HASH_SIZE 1021
  143. int str_hash[SCRIPT_HASH_SIZE];
  144. // Specifies which string hashing method to use
  145. //#define SCRIPT_HASH_DJB2
  146. //#define SCRIPT_HASH_SDBM
  147. #define SCRIPT_HASH_ELF
  148. static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
  149. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  150. static int parse_options = 0;
  151. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  152. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  153. // important buildin function references for usage in scripts
  154. static int buildin_set_ref = 0;
  155. static int buildin_callsub_ref = 0;
  156. static int buildin_callfunc_ref = 0;
  157. static int buildin_getelementofarray_ref = 0;
  158. // Caches compiled autoscript item code.
  159. // Note: This is not cleared when reloading itemdb.
  160. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
  161. struct Script_Config script_config = {
  162. 1, // warn_func_mismatch_argtypes
  163. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  164. 0, INT_MAX, // input_min_value/input_max_value
  165. "OnPCDieEvent", //die_event_name
  166. "OnPCKillEvent", //kill_pc_event_name
  167. "OnNPCKillEvent", //kill_mob_event_name
  168. "OnPCLoginEvent", //login_event_name
  169. "OnPCLogoutEvent", //logout_event_name
  170. "OnPCLoadMapEvent", //loadmap_event_name
  171. "OnPCBaseLvUpEvent", //baselvup_event_name
  172. "OnPCJobLvUpEvent", //joblvup_event_name
  173. "OnPCStatCalcEvent", //stat_calc_event_name
  174. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  175. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  176. };
  177. static jmp_buf error_jump;
  178. static char* error_msg;
  179. static const char* error_pos;
  180. static int error_report; // if the error should produce output
  181. // Used by disp_warning_message
  182. static const char* parser_current_src;
  183. static const char* parser_current_file;
  184. static int parser_current_line;
  185. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  186. // [Eoe / jA 1080, 1081, 1094, 1164]
  187. enum curly_type {
  188. TYPE_NULL = 0,
  189. TYPE_IF,
  190. TYPE_SWITCH,
  191. TYPE_WHILE,
  192. TYPE_FOR,
  193. TYPE_DO,
  194. TYPE_USERFUNC,
  195. TYPE_ARGLIST // function argument list
  196. };
  197. enum e_arglist
  198. {
  199. ARGLIST_UNDEFINED = 0,
  200. ARGLIST_NO_PAREN = 1,
  201. ARGLIST_PAREN = 2,
  202. };
  203. static struct {
  204. struct {
  205. enum curly_type type;
  206. int index;
  207. int count;
  208. int flag;
  209. struct linkdb_node *case_label;
  210. } curly[256]; // Information right parenthesis
  211. int curly_count; // The number of right brackets
  212. int index; // Number of the syntax used in the script
  213. } syntax;
  214. const char* parse_curly_close(const char* p);
  215. const char* parse_syntax_close(const char* p);
  216. const char* parse_syntax_close_sub(const char* p,int* flag);
  217. const char* parse_syntax(const char* p);
  218. static int parse_syntax_for_flag = 0;
  219. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  220. extern unsigned int current_equip_combo_pos;
  221. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  222. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  223. int potion_target = 0;
  224. unsigned int *generic_ui_array = NULL;
  225. unsigned int generic_ui_array_size = 0;
  226. c_op get_com(unsigned char *script,int *pos);
  227. int get_num(unsigned char *script,int *pos);
  228. typedef struct script_function {
  229. int (*func)(struct script_state *st);
  230. const char *name;
  231. const char *arg;
  232. } script_function;
  233. extern script_function buildin_func[];
  234. #ifdef BETA_THREAD_TEST
  235. /**
  236. * MySQL Query Slave
  237. **/
  238. static SPIN_LOCK queryThreadLock;
  239. static rAthread queryThread = NULL;
  240. static ramutex queryThreadMutex = NULL;
  241. static racond queryThreadCond = NULL;
  242. static volatile int32 queryThreadTerminate = 0;
  243. struct queryThreadEntry {
  244. bool ok;
  245. bool type; /* main db or log db? */
  246. struct script_state *st;
  247. };
  248. /* Ladies and Gentleman the Manager! */
  249. struct {
  250. struct queryThreadEntry **entry;/* array of structs */
  251. int count;
  252. int timer;/* used to receive processed entries */
  253. } queryThreadData;
  254. #endif
  255. /*==========================================
  256. * (Only those needed) local declaration prototype
  257. *------------------------------------------*/
  258. const char* parse_subexpr(const char* p,int limit);
  259. int run_func(struct script_state *st);
  260. int script_instancegetid(struct script_state *st);
  261. enum {
  262. MF_NOMEMO, //0
  263. MF_NOTELEPORT,
  264. MF_NOSAVE,
  265. MF_NOBRANCH,
  266. MF_NOPENALTY,
  267. MF_NOZENYPENALTY,
  268. MF_PVP,
  269. MF_PVP_NOPARTY,
  270. MF_PVP_NOGUILD,
  271. MF_GVG,
  272. MF_GVG_NOPARTY, //10
  273. MF_NOTRADE,
  274. MF_NOSKILL,
  275. MF_NOWARP,
  276. MF_PARTYLOCK,
  277. MF_NOICEWALL,
  278. MF_SNOW,
  279. MF_FOG,
  280. MF_SAKURA,
  281. MF_LEAVES,
  282. /**
  283. * No longer available, keeping here just in case it's back someday. [Ind]
  284. **/
  285. //MF_RAIN, //20
  286. // 21 free
  287. MF_NOGO = 22,
  288. MF_CLOUDS,
  289. MF_CLOUDS2,
  290. MF_FIREWORKS,
  291. MF_GVG_CASTLE,
  292. MF_GVG_DUNGEON,
  293. MF_NIGHTENABLED,
  294. MF_NOBASEEXP,
  295. MF_NOJOBEXP, //30
  296. MF_NOMOBLOOT,
  297. MF_NOMVPLOOT,
  298. MF_NORETURN,
  299. MF_NOWARPTO,
  300. MF_NIGHTMAREDROP,
  301. MF_RESTRICTED,
  302. MF_NOCOMMAND,
  303. MF_NODROP,
  304. MF_JEXP,
  305. MF_BEXP, //40
  306. MF_NOVENDING,
  307. MF_LOADEVENT,
  308. MF_NOCHAT,
  309. MF_NOEXPPENALTY,
  310. MF_GUILDLOCK,
  311. MF_TOWN,
  312. MF_AUTOTRADE,
  313. MF_ALLOWKS,
  314. MF_MONSTER_NOTELEPORT,
  315. MF_PVP_NOCALCRANK, //50
  316. MF_BATTLEGROUND,
  317. MF_RESET,
  318. MF_CHANNELAUTOJOIN,
  319. MF_NOUSECART,
  320. MF_NOITEMCONSUMPTION,
  321. MF_SUMSTARTMIRACLE,
  322. MF_NOMINEEFFECT,
  323. MF_NOLOCKON,
  324. MF_NOTOMB,
  325. MF_SKILL_DAMAGE //60
  326. };
  327. const char* script_op2name(int op)
  328. {
  329. #define RETURN_OP_NAME(type) case type: return #type
  330. switch( op )
  331. {
  332. RETURN_OP_NAME(C_NOP);
  333. RETURN_OP_NAME(C_POS);
  334. RETURN_OP_NAME(C_INT);
  335. RETURN_OP_NAME(C_PARAM);
  336. RETURN_OP_NAME(C_FUNC);
  337. RETURN_OP_NAME(C_STR);
  338. RETURN_OP_NAME(C_CONSTSTR);
  339. RETURN_OP_NAME(C_ARG);
  340. RETURN_OP_NAME(C_NAME);
  341. RETURN_OP_NAME(C_EOL);
  342. RETURN_OP_NAME(C_RETINFO);
  343. RETURN_OP_NAME(C_USERFUNC);
  344. RETURN_OP_NAME(C_USERFUNC_POS);
  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. default:
  369. ShowDebug("script_op2name: unexpected op=%d\n", op);
  370. return "???";
  371. }
  372. #undef RETURN_OP_NAME
  373. }
  374. #ifdef DEBUG_DUMP_STACK
  375. static void script_dump_stack(struct script_state* st)
  376. {
  377. int i;
  378. ShowMessage("\tstart = %d\n", st->start);
  379. ShowMessage("\tend = %d\n", st->end);
  380. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  381. ShowMessage("\tsp = %d\n", st->stack->sp);
  382. for( i = 0; i < st->stack->sp; ++i )
  383. {
  384. struct script_data* data = &st->stack->stack_data[i];
  385. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  386. switch( data->type )
  387. {
  388. case C_INT:
  389. case C_POS:
  390. ShowMessage(" %d\n", data->u.num);
  391. break;
  392. case C_STR:
  393. case C_CONSTSTR:
  394. ShowMessage(" \"%s\"\n", data->u.str);
  395. break;
  396. case C_NAME:
  397. 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));
  398. break;
  399. case C_RETINFO:
  400. {
  401. struct script_retinfo* ri = data->u.ri;
  402. 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);
  403. }
  404. break;
  405. default:
  406. ShowMessage("\n");
  407. break;
  408. }
  409. }
  410. }
  411. #endif
  412. /// Reports on the console the src of a script error.
  413. static void script_reportsrc(struct script_state *st)
  414. {
  415. struct block_list* bl;
  416. if( st->oid == 0 )
  417. return; //Can't report source.
  418. bl = map_id2bl(st->oid);
  419. if( bl == NULL )
  420. return;
  421. switch( bl->type )
  422. {
  423. case BL_NPC:
  424. if( bl->m >= 0 )
  425. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  426. else
  427. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  428. break;
  429. default:
  430. if( bl->m >= 0 )
  431. 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);
  432. else
  433. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  434. break;
  435. }
  436. }
  437. /// Reports on the console information about the script data.
  438. static void script_reportdata(struct script_data* data)
  439. {
  440. if( data == NULL )
  441. return;
  442. switch( data->type ) {
  443. case C_NOP:// no value
  444. ShowDebug("Data: nothing (nil)\n");
  445. break;
  446. case C_INT:// number
  447. ShowDebug("Data: number value=%"PRId64"\n", data->u.num);
  448. break;
  449. case C_STR:
  450. case C_CONSTSTR:// string
  451. if( data->u.str ) {
  452. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  453. } else {
  454. ShowDebug("Data: string value=NULL\n");
  455. }
  456. break;
  457. case C_NAME:// reference
  458. if( reference_tovariable(data) ) {// variable
  459. const char* name = reference_getname(data);
  460. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  461. } else if( reference_toconstant(data) ) {// constant
  462. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  463. } else if( reference_toparam(data) ) {// param
  464. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  465. } else {// ???
  466. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  467. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  468. }
  469. break;
  470. case C_POS:// label
  471. ShowDebug("Data: label pos=%"PRId64"\n", data->u.num);
  472. break;
  473. default:
  474. ShowDebug("Data: %s\n", script_op2name(data->type));
  475. break;
  476. }
  477. }
  478. /// Reports on the console information about the current built-in function.
  479. static void script_reportfunc(struct script_state* st)
  480. {
  481. int params, id;
  482. struct script_data* data;
  483. if( !script_hasdata(st,0) )
  484. {// no stack
  485. return;
  486. }
  487. data = script_getdata(st,0);
  488. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  489. {// script currently not executing a built-in function or corrupt stack
  490. return;
  491. }
  492. id = reference_getid(data);
  493. params = script_lastdata(st)-1;
  494. if( params > 0 )
  495. {
  496. int i;
  497. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  498. for( i = 2; i <= script_lastdata(st); i++ )
  499. {
  500. script_reportdata(script_getdata(st,i));
  501. }
  502. }
  503. else
  504. {
  505. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  506. }
  507. }
  508. /*==========================================
  509. * Output error message
  510. *------------------------------------------*/
  511. static void disp_error_message2(const char *mes,const char *pos,int report)
  512. {
  513. error_msg = aStrdup(mes);
  514. error_pos = pos;
  515. error_report = report;
  516. longjmp( error_jump, 1 );
  517. }
  518. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  519. static void disp_warning_message(const char *mes, const char *pos) {
  520. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  521. }
  522. /// Checks event parameter validity
  523. static void check_event(struct script_state *st, const char *evt)
  524. {
  525. if( evt && evt[0] && !stristr(evt, "::On") )
  526. {
  527. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  528. script_reportsrc(st);
  529. }
  530. }
  531. /*==========================================
  532. * Hashes the input string
  533. *------------------------------------------*/
  534. static unsigned int calc_hash(const char* p)
  535. {
  536. unsigned int h;
  537. #if defined(SCRIPT_HASH_DJB2)
  538. h = 5381;
  539. while( *p ) // hash*33 + c
  540. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  541. #elif defined(SCRIPT_HASH_SDBM)
  542. h = 0;
  543. while( *p ) // hash*65599 + c
  544. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  545. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  546. h = 0;
  547. while( *p ){
  548. unsigned int g;
  549. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  550. g = h & 0xF0000000;
  551. if( g )
  552. {
  553. h ^= g >> 24;
  554. h &= ~g;
  555. }
  556. }
  557. #else // athena hash
  558. h = 0;
  559. while( *p )
  560. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  561. #endif
  562. return h % SCRIPT_HASH_SIZE;
  563. }
  564. /*==========================================
  565. * str_data manipulation functions
  566. *------------------------------------------*/
  567. /// Looks up string using the provided id.
  568. const char* get_str(int id)
  569. {
  570. Assert( id >= LABEL_START && id < str_size );
  571. return str_buf+str_data[id].str;
  572. }
  573. /// Returns the uid of the string, or -1.
  574. static int search_str(const char* p)
  575. {
  576. int i;
  577. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  578. if( strcasecmp(get_str(i),p) == 0 )
  579. return i;
  580. return -1;
  581. }
  582. /// Stores a copy of the string and returns its id.
  583. /// If an identical string is already present, returns its id instead.
  584. int add_str(const char* p)
  585. {
  586. int h;
  587. int len;
  588. h = calc_hash(p);
  589. if( str_hash[h] == 0 )
  590. {// empty bucket, add new node here
  591. str_hash[h] = str_num;
  592. }
  593. else
  594. {// scan for end of list, or occurence of identical string
  595. int i;
  596. for( i = str_hash[h]; ; i = str_data[i].next )
  597. {
  598. if( strcasecmp(get_str(i),p) == 0 )
  599. return i; // string already in list
  600. if( str_data[i].next == 0 )
  601. break; // reached the end
  602. }
  603. // append node to end of list
  604. str_data[i].next = str_num;
  605. }
  606. // grow list if neccessary
  607. if( str_num >= str_data_size )
  608. {
  609. str_data_size += 128;
  610. RECREATE(str_data,struct str_data_struct,str_data_size);
  611. memset(str_data + (str_data_size - 128), '\0', 128);
  612. }
  613. len=(int)strlen(p);
  614. // grow string buffer if neccessary
  615. while( str_pos+len+1 >= str_size )
  616. {
  617. str_size += 256;
  618. RECREATE(str_buf,char,str_size);
  619. memset(str_buf + (str_size - 256), '\0', 256);
  620. }
  621. safestrncpy(str_buf+str_pos, p, len+1);
  622. str_data[str_num].type = C_NOP;
  623. str_data[str_num].str = str_pos;
  624. str_data[str_num].next = 0;
  625. str_data[str_num].func = NULL;
  626. str_data[str_num].backpatch = -1;
  627. str_data[str_num].label = -1;
  628. str_pos += len+1;
  629. return str_num++;
  630. }
  631. /// Appends 1 byte to the script buffer.
  632. static void add_scriptb(int a)
  633. {
  634. if( script_pos+1 >= script_size )
  635. {
  636. script_size += SCRIPT_BLOCK_SIZE;
  637. RECREATE(script_buf,unsigned char,script_size);
  638. }
  639. script_buf[script_pos++] = (uint8)(a);
  640. }
  641. /// Appends a c_op value to the script buffer.
  642. /// The value is variable-length encoded into 8-bit blocks.
  643. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  644. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  645. static void add_scriptc(int a)
  646. {
  647. while( a >= 0x40 )
  648. {
  649. add_scriptb((a&0x3f)|0x40);
  650. a = (a - 0x40) >> 6;
  651. }
  652. add_scriptb(a);
  653. }
  654. /// Appends an integer value to the script buffer.
  655. /// The value is variable-length encoded into 8-bit blocks.
  656. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  657. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  658. static void add_scripti(int a)
  659. {
  660. while( a >= 0x40 )
  661. {
  662. add_scriptb((a&0x3f)|0xc0);
  663. a = (a - 0x40) >> 6;
  664. }
  665. add_scriptb(a|0x80);
  666. }
  667. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  668. ///
  669. /// @param l The id of the str_data entry
  670. // Maximum up to 16M
  671. static void add_scriptl(int l)
  672. {
  673. int backpatch = str_data[l].backpatch;
  674. switch(str_data[l].type){
  675. case C_POS:
  676. case C_USERFUNC_POS:
  677. add_scriptc(C_POS);
  678. add_scriptb(str_data[l].label);
  679. add_scriptb(str_data[l].label>>8);
  680. add_scriptb(str_data[l].label>>16);
  681. break;
  682. case C_NOP:
  683. case C_USERFUNC:
  684. // Embedded data backpatch there is a possibility of label
  685. add_scriptc(C_NAME);
  686. str_data[l].backpatch = script_pos;
  687. add_scriptb(backpatch);
  688. add_scriptb(backpatch>>8);
  689. add_scriptb(backpatch>>16);
  690. break;
  691. case C_INT:
  692. add_scripti(abs(str_data[l].val));
  693. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  694. add_scriptc(C_NEG);
  695. break;
  696. default: // assume C_NAME
  697. add_scriptc(C_NAME);
  698. add_scriptb(l);
  699. add_scriptb(l>>8);
  700. add_scriptb(l>>16);
  701. break;
  702. }
  703. }
  704. /*==========================================
  705. * Resolve the label
  706. *------------------------------------------*/
  707. void set_label(int l,int pos, const char* script_pos_cur)
  708. {
  709. int i;
  710. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  711. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  712. disp_error_message("set_label: invalid label name",script_pos_cur);
  713. return;
  714. }
  715. if(str_data[l].label!=-1){
  716. disp_error_message("set_label: dup label ",script_pos_cur);
  717. return;
  718. }
  719. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  720. str_data[l].label=pos;
  721. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  722. int next=GETVALUE(script_buf,i);
  723. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  724. SETVALUE(script_buf,i,pos);
  725. i=next;
  726. }
  727. }
  728. /// Skips spaces and/or comments.
  729. const char* skip_space(const char* p)
  730. {
  731. if( p == NULL )
  732. return NULL;
  733. for(;;)
  734. {
  735. while( ISSPACE(*p) )
  736. ++p;
  737. if( *p == '/' && p[1] == '/' )
  738. {// line comment
  739. while(*p && *p!='\n')
  740. ++p;
  741. }
  742. else if( *p == '/' && p[1] == '*' )
  743. {// block comment
  744. p += 2;
  745. for(;;)
  746. {
  747. if( *p == '\0' ) {
  748. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  749. return p;
  750. }
  751. if( *p == '*' && p[1] == '/' )
  752. {// end of block comment
  753. p += 2;
  754. break;
  755. }
  756. ++p;
  757. }
  758. }
  759. else
  760. break;
  761. }
  762. return p;
  763. }
  764. /// Skips a word.
  765. /// A word consists of undercores and/or alphanumeric characters,
  766. /// and valid variable prefixes/postfixes.
  767. static const char* skip_word(const char* p)
  768. {
  769. // prefix
  770. switch( *p ) {
  771. case '@':// temporary char variable
  772. ++p; break;
  773. case '#':// account variable
  774. p += ( p[1] == '#' ? 2 : 1 ); break;
  775. case '\'':// instance variable
  776. ++p; break;
  777. case '.':// npc variable
  778. p += ( p[1] == '@' ? 2 : 1 ); break;
  779. case '$':// global variable
  780. p += ( p[1] == '@' ? 2 : 1 ); break;
  781. }
  782. while( ISALNUM(*p) || *p == '_' )
  783. ++p;
  784. // postfix
  785. if( *p == '$' )// string
  786. p++;
  787. return p;
  788. }
  789. /// Adds a word to str_data.
  790. /// @see skip_word
  791. /// @see add_str
  792. static int add_word(const char* p)
  793. {
  794. char* word;
  795. int len;
  796. int i;
  797. // Check for a word
  798. len = skip_word(p) - p;
  799. if( len == 0 )
  800. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  801. // Duplicate the word
  802. word = (char*)aMalloc(len+1);
  803. memcpy(word, p, len);
  804. word[len] = 0;
  805. // add the word
  806. i = add_str(word);
  807. aFree(word);
  808. return i;
  809. }
  810. /// Parses a function call.
  811. /// The argument list can have parenthesis or not.
  812. /// The number of arguments is checked.
  813. static
  814. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  815. {
  816. const char* p2;
  817. const char* arg=NULL;
  818. int func;
  819. func = add_word(p);
  820. if( str_data[func].type == C_FUNC ){
  821. // buildin function
  822. add_scriptl(func);
  823. add_scriptc(C_ARG);
  824. arg = buildin_func[str_data[func].val].arg;
  825. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  826. // script defined function
  827. add_scriptl(buildin_callsub_ref);
  828. add_scriptc(C_ARG);
  829. add_scriptl(func);
  830. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  831. if( *arg == 0 )
  832. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  833. if( *arg != '*' )
  834. ++arg; // count func as argument
  835. } else {
  836. #ifdef SCRIPT_CALLFUNC_CHECK
  837. const char* name = get_str(func);
  838. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  839. #endif
  840. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  841. #ifdef SCRIPT_CALLFUNC_CHECK
  842. } else {;
  843. add_scriptl(buildin_callfunc_ref);
  844. add_scriptc(C_ARG);
  845. add_scriptc(C_STR);
  846. while( *name ) add_scriptb(*name ++);
  847. add_scriptb(0);
  848. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  849. if( *arg != '*' ) ++ arg;
  850. }
  851. #endif
  852. }
  853. p = skip_word(p);
  854. p = skip_space(p);
  855. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  856. syntax.curly[syntax.curly_count].count = 0;
  857. if( *p == ';' )
  858. {// <func name> ';'
  859. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  860. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  861. {// <func name> '(' ')'
  862. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  863. p = p2;
  864. /*
  865. } else if( 0 && require_paren && *p != '(' )
  866. {// <func name>
  867. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  868. */
  869. } else
  870. {// <func name> <arg list>
  871. if( require_paren ){
  872. if( *p != '(' )
  873. disp_error_message("need '('",p);
  874. ++p; // skip '('
  875. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  876. } else if( *p == '(' ){
  877. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  878. } else {
  879. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  880. }
  881. ++syntax.curly_count;
  882. while( *arg ) {
  883. p2=parse_subexpr(p,-1);
  884. if( p == p2 )
  885. break; // not an argument
  886. if( *arg != '*' )
  887. ++arg; // next argument
  888. p=skip_space(p2);
  889. if( *arg == 0 || *p != ',' )
  890. break; // no more arguments
  891. ++p; // skip comma
  892. }
  893. --syntax.curly_count;
  894. }
  895. if( *arg && *arg != '?' && *arg != '*' )
  896. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  897. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  898. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  899. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  900. if( *p != ')' )
  901. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  902. ++p;
  903. }
  904. add_scriptc(C_FUNC);
  905. return p;
  906. }
  907. /// Processes end of logical script line.
  908. /// @param first When true, only fix up scheduling data is initialized
  909. /// @param p Script position for error reporting in set_label
  910. static void parse_nextline(bool first, const char* p)
  911. {
  912. if( !first )
  913. {
  914. add_scriptc(C_EOL); // mark end of line for stack cleanup
  915. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  916. }
  917. // initialize data for new '-' label fix up scheduling
  918. str_data[LABEL_NEXTLINE].type = C_NOP;
  919. str_data[LABEL_NEXTLINE].backpatch = -1;
  920. str_data[LABEL_NEXTLINE].label = -1;
  921. }
  922. /// Parse a variable assignment using the direct equals operator
  923. /// @param p script position where the function should run from
  924. /// @return NULL if not a variable assignment, the new position otherwise
  925. const char* parse_variable(const char* p) {
  926. int word;
  927. c_op type = C_NOP;
  928. const char *p2 = NULL;
  929. const char *var = p;
  930. // skip the variable where applicable
  931. p = skip_word(p);
  932. p = skip_space(p);
  933. if( p == NULL ) {// end of the line or invalid buffer
  934. return NULL;
  935. }
  936. if( *p == '[' ) {// array variable so process the array as appropriate
  937. int i,j;
  938. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  939. if( *p ++ == ']' && --(j) == 0 ) break;
  940. if( *p == '[' ) ++ j;
  941. }
  942. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  943. disp_error_message("Missing right expression or closing bracket for variable.", p);
  944. }
  945. }
  946. if( type == C_NOP &&
  947. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  948. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  949. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  950. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  951. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  952. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  953. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  954. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  955. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  956. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  957. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  958. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  959. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  960. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  961. ) )
  962. {// failed to find a matching operator combination so invalid
  963. return NULL;
  964. }
  965. switch( type ) {
  966. case C_EQ: {// incremental modifier
  967. p = skip_space( &p[1] );
  968. }
  969. break;
  970. case C_L_SHIFT:
  971. case C_R_SHIFT: {// left or right shift modifier
  972. p = skip_space( &p[3] );
  973. }
  974. break;
  975. default: {// normal incremental command
  976. p = skip_space( &p[2] );
  977. }
  978. }
  979. if( p == NULL ) {// end of line or invalid buffer
  980. return NULL;
  981. }
  982. // push the set function onto the stack
  983. add_scriptl(buildin_set_ref);
  984. add_scriptc(C_ARG);
  985. // always append parenthesis to avoid errors
  986. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  987. syntax.curly[syntax.curly_count].count = 0;
  988. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  989. // increment the total curly count for the position in the script
  990. ++ syntax.curly_count;
  991. // parse the variable currently being modified
  992. word = add_word(var);
  993. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  994. {// cannot assign a variable which exists as a function or label
  995. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  996. }
  997. if( p2 ) {// process the variable index
  998. const char* p3 = NULL;
  999. // push the getelementofarray method into the stack
  1000. add_scriptl(buildin_getelementofarray_ref);
  1001. add_scriptc(C_ARG);
  1002. add_scriptl(word);
  1003. // process the sub-expression for this assignment
  1004. p3 = parse_subexpr(p2 + 1, 1);
  1005. p3 = skip_space(p3);
  1006. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1007. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1008. }
  1009. // push the closing function stack operator onto the stack
  1010. add_scriptc(C_FUNC);
  1011. p3 ++;
  1012. } else {// simply push the variable or value onto the stack
  1013. add_scriptl(word);
  1014. }
  1015. if( type != C_EQ )
  1016. add_scriptc(C_REF);
  1017. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1018. add_scripti(1);
  1019. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1020. } else {// process the value as an expression
  1021. p = parse_subexpr(p, -1);
  1022. if( type != C_EQ )
  1023. {// push the type of modifier onto the stack
  1024. add_scriptc(type);
  1025. }
  1026. }
  1027. // decrement the curly count for the position within the script
  1028. -- syntax.curly_count;
  1029. // close the script by appending the function operator
  1030. add_scriptc(C_FUNC);
  1031. // push the buffer from the method
  1032. return p;
  1033. }
  1034. /*
  1035. * Checks whether the gives string is a number literal
  1036. *
  1037. * Mainly necessary to differentiate between number literals and NPC name
  1038. * constants, since several of those start with a digit.
  1039. *
  1040. * All this does is to check if the string begins with an optional + or - sign,
  1041. * followed by a hexadecimal or decimal number literal literal and is NOT
  1042. * followed by a underscore or letter.
  1043. *
  1044. * @author : Hercules.ws
  1045. * @param p Pointer to the string to check
  1046. * @return Whether the string is a number literal
  1047. */
  1048. bool is_number(const char *p) {
  1049. const char *np;
  1050. if (!p)
  1051. return false;
  1052. if (*p == '-' || *p == '+')
  1053. p++;
  1054. np = p;
  1055. if (*p == '0' && p[1] == 'x') {
  1056. p+=2;
  1057. np = p;
  1058. // Hexadecimal
  1059. while (ISXDIGIT(*np))
  1060. np++;
  1061. } else {
  1062. // Decimal
  1063. while (ISDIGIT(*np))
  1064. np++;
  1065. }
  1066. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1067. return true;
  1068. return false;
  1069. }
  1070. /*==========================================
  1071. * Analysis section
  1072. *------------------------------------------*/
  1073. const char* parse_simpleexpr(const char *p)
  1074. {
  1075. long long i;
  1076. p=skip_space(p);
  1077. if(*p==';' || *p==',')
  1078. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1079. if(*p=='('){
  1080. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1081. ++syntax.curly[i].count;
  1082. p=parse_subexpr(p+1,-1);
  1083. p=skip_space(p);
  1084. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1085. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1086. ){
  1087. if( *p == ',' ){
  1088. syntax.curly[i].flag = ARGLIST_PAREN;
  1089. return p;
  1090. } else
  1091. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1092. }
  1093. if( *p != ')' )
  1094. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1095. ++p;
  1096. } else if(is_number(p)) {
  1097. char *np;
  1098. while(*p == '0' && ISDIGIT(p[1])) p++;
  1099. i=strtoll(p,&np,0);
  1100. if( i < INT_MIN ) {
  1101. i = INT_MIN;
  1102. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1103. } else if( i > INT_MAX ) {
  1104. i = INT_MAX;
  1105. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1106. }
  1107. add_scripti((int)i);
  1108. p=np;
  1109. } else if(*p=='"'){
  1110. add_scriptc(C_STR);
  1111. p++;
  1112. while( *p && *p != '"' ){
  1113. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1114. {
  1115. char buf[8];
  1116. size_t len = skip_escaped_c(p) - p;
  1117. size_t n = sv_unescape_c(buf, p, len);
  1118. if( n != 1 )
  1119. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1120. p += len;
  1121. add_scriptb(*buf);
  1122. continue;
  1123. }
  1124. else if( *p == '\n' )
  1125. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1126. add_scriptb(*p++);
  1127. }
  1128. if(!*p)
  1129. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1130. add_scriptb(0);
  1131. p++; //'"'
  1132. } else {
  1133. int l;
  1134. const char* pv;
  1135. // label , register , function etc
  1136. if(skip_word(p)==p)
  1137. disp_error_message("parse_simpleexpr: unexpected character",p);
  1138. l=add_word(p);
  1139. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1140. return parse_callfunc(p,1,0);
  1141. #ifdef SCRIPT_CALLFUNC_CHECK
  1142. else {
  1143. const char* name = get_str(l);
  1144. if( strdb_get(userfunc_db,name) != NULL ) {
  1145. return parse_callfunc(p,1,1);
  1146. }
  1147. }
  1148. #endif
  1149. if( (pv = parse_variable(p)) )
  1150. {// successfully processed a variable assignment
  1151. return pv;
  1152. }
  1153. p=skip_word(p);
  1154. if( *p == '[' ){
  1155. // array(name[i] => getelementofarray(name,i) )
  1156. add_scriptl(buildin_getelementofarray_ref);
  1157. add_scriptc(C_ARG);
  1158. add_scriptl(l);
  1159. p=parse_subexpr(p+1,-1);
  1160. p=skip_space(p);
  1161. if( *p != ']' )
  1162. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1163. ++p;
  1164. add_scriptc(C_FUNC);
  1165. }else
  1166. add_scriptl(l);
  1167. }
  1168. return p;
  1169. }
  1170. /*==========================================
  1171. * Analysis of the expression
  1172. *------------------------------------------*/
  1173. const char* parse_subexpr(const char* p,int limit)
  1174. {
  1175. int op,opl,len;
  1176. p=skip_space(p);
  1177. if( *p == '-' ){
  1178. const char* tmpp = skip_space(p+1);
  1179. if( *tmpp == ';' || *tmpp == ',' ){
  1180. add_scriptl(LABEL_NEXTLINE);
  1181. p++;
  1182. return p;
  1183. }
  1184. }
  1185. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1186. p=parse_subexpr(p+1,10);
  1187. add_scriptc(op);
  1188. } else
  1189. p=parse_simpleexpr(p);
  1190. p=skip_space(p);
  1191. while((
  1192. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1193. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1194. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1195. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1196. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1197. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1198. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1199. (op=C_AND,opl=6,len=1,*p=='&') ||
  1200. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1201. (op=C_OR,opl=5,len=1,*p=='|') ||
  1202. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1203. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1204. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1205. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1206. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1207. (op=C_GT,opl=3,len=1,*p=='>') ||
  1208. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1209. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1210. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1211. p+=len;
  1212. if(op == C_OP3) {
  1213. p=parse_subexpr(p,-1);
  1214. p=skip_space(p);
  1215. if( *(p++) != ':')
  1216. disp_error_message("parse_subexpr: expected ':'", p-1);
  1217. p=parse_subexpr(p,-1);
  1218. } else {
  1219. p=parse_subexpr(p,opl);
  1220. }
  1221. add_scriptc(op);
  1222. p=skip_space(p);
  1223. }
  1224. return p; /* return first untreated operator */
  1225. }
  1226. /*==========================================
  1227. * Evaluation of the expression
  1228. *------------------------------------------*/
  1229. const char* parse_expr(const char *p)
  1230. {
  1231. switch(*p){
  1232. case ')': case ';': case ':': case '[': case ']':
  1233. case '}':
  1234. disp_error_message("parse_expr: unexpected character",p);
  1235. }
  1236. p=parse_subexpr(p,-1);
  1237. return p;
  1238. }
  1239. /*==========================================
  1240. * Analysis of the line
  1241. *------------------------------------------*/
  1242. const char* parse_line(const char* p)
  1243. {
  1244. const char* p2;
  1245. p=skip_space(p);
  1246. if(*p==';') {
  1247. //Close decision for if(); for(); while();
  1248. p = parse_syntax_close(p + 1);
  1249. return p;
  1250. }
  1251. if(*p==')' && parse_syntax_for_flag)
  1252. return p+1;
  1253. p = skip_space(p);
  1254. if(p[0] == '{') {
  1255. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1256. syntax.curly[syntax.curly_count].count = -1;
  1257. syntax.curly[syntax.curly_count].index = -1;
  1258. syntax.curly_count++;
  1259. return p + 1;
  1260. } else if(p[0] == '}') {
  1261. return parse_curly_close(p);
  1262. }
  1263. // Syntax-related processing
  1264. p2 = parse_syntax(p);
  1265. if(p2 != NULL)
  1266. return p2;
  1267. // attempt to process a variable assignment
  1268. p2 = parse_variable(p);
  1269. if( p2 != NULL )
  1270. {// variable assignment processed so leave the method
  1271. return parse_syntax_close(p2 + 1);
  1272. }
  1273. p = parse_callfunc(p,0,0);
  1274. p = skip_space(p);
  1275. if(parse_syntax_for_flag) {
  1276. if( *p != ')' )
  1277. disp_error_message("parse_line: expected ')'",p);
  1278. } else {
  1279. if( *p != ';' )
  1280. disp_error_message("parse_line: expected ';'",p);
  1281. }
  1282. //Binding decision for if(), for(), while()
  1283. p = parse_syntax_close(p+1);
  1284. return p;
  1285. }
  1286. // { ... } Closing process
  1287. const char* parse_curly_close(const char* p)
  1288. {
  1289. if(syntax.curly_count <= 0) {
  1290. disp_error_message("parse_curly_close: unexpected string",p);
  1291. return p + 1;
  1292. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1293. syntax.curly_count--;
  1294. //Close decision if, for , while
  1295. p = parse_syntax_close(p + 1);
  1296. return p;
  1297. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1298. //Closing switch()
  1299. int pos = syntax.curly_count-1;
  1300. char label[256];
  1301. int l;
  1302. // Remove temporary variables
  1303. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1304. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1305. parse_line(label);
  1306. syntax.curly_count--;
  1307. // Go to the end pointer unconditionally
  1308. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1309. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1310. parse_line(label);
  1311. syntax.curly_count--;
  1312. // You are here labeled
  1313. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1314. l=add_str(label);
  1315. set_label(l,script_pos, p);
  1316. if(syntax.curly[pos].flag) {
  1317. //Exists default
  1318. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1319. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1320. parse_line(label);
  1321. syntax.curly_count--;
  1322. }
  1323. // Label end
  1324. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1325. l=add_str(label);
  1326. set_label(l,script_pos, p);
  1327. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1328. syntax.curly_count--;
  1329. //Closing decision if, for , while
  1330. p = parse_syntax_close(p + 1);
  1331. return p;
  1332. } else {
  1333. disp_error_message("parse_curly_close: unexpected string",p);
  1334. return p + 1;
  1335. }
  1336. }
  1337. // Syntax-related processing
  1338. // break, case, continue, default, do, for, function,
  1339. // if, switch, while ? will handle this internally.
  1340. const char* parse_syntax(const char* p)
  1341. {
  1342. const char *p2 = skip_word(p);
  1343. switch(*p) {
  1344. case 'B':
  1345. case 'b':
  1346. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1347. // break Processing
  1348. char label[256];
  1349. int pos = syntax.curly_count - 1;
  1350. while(pos >= 0) {
  1351. if(syntax.curly[pos].type == TYPE_DO) {
  1352. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1353. break;
  1354. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1355. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1356. break;
  1357. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1358. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1359. break;
  1360. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1361. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1362. break;
  1363. }
  1364. pos--;
  1365. }
  1366. if(pos < 0) {
  1367. disp_error_message("parse_syntax: unexpected 'break'",p);
  1368. } else {
  1369. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1370. parse_line(label);
  1371. syntax.curly_count--;
  1372. }
  1373. p = skip_space(p2);
  1374. if(*p != ';')
  1375. disp_error_message("parse_syntax: expected ';'",p);
  1376. // Closing decision if, for , while
  1377. p = parse_syntax_close(p + 1);
  1378. return p;
  1379. }
  1380. break;
  1381. case 'c':
  1382. case 'C':
  1383. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1384. //Processing case
  1385. int pos = syntax.curly_count-1;
  1386. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1387. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1388. return p+1;
  1389. } else {
  1390. char label[256];
  1391. int l,v;
  1392. char *np;
  1393. if(syntax.curly[pos].count != 1) {
  1394. //Jump for FALLTHRU
  1395. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1396. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1397. parse_line(label);
  1398. syntax.curly_count--;
  1399. // You are here labeled
  1400. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1401. l=add_str(label);
  1402. set_label(l,script_pos, p);
  1403. }
  1404. //Decision statement switch
  1405. p = skip_space(p2);
  1406. if(p == p2) {
  1407. disp_error_message("parse_syntax: expected a space ' '",p);
  1408. }
  1409. // check whether case label is integer or not
  1410. if(is_number(p)) {
  1411. //Numeric value
  1412. v = (int)strtol(p,&np,0);
  1413. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1414. p++;
  1415. p = skip_word(p);
  1416. if(np != p)
  1417. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1418. } else {
  1419. //Check for constants
  1420. p2 = skip_word(p);
  1421. v = (int)(size_t) (p2-p); // length of word at p2
  1422. memcpy(label,p,v);
  1423. label[v]='\0';
  1424. if( !script_get_constant(label, &v) )
  1425. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1426. p = skip_word(p);
  1427. }
  1428. p = skip_space(p);
  1429. if(*p != ':')
  1430. disp_error_message("parse_syntax: expect ':'",p);
  1431. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1432. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1433. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1434. // Bad I do not parse twice
  1435. p2 = parse_line(label);
  1436. parse_line(p2);
  1437. syntax.curly_count--;
  1438. if(syntax.curly[pos].count != 1) {
  1439. // Label after the completion of FALLTHRU
  1440. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1441. l=add_str(label);
  1442. set_label(l,script_pos,p);
  1443. }
  1444. // check duplication of case label [Rayce]
  1445. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1446. disp_error_message("parse_syntax: dup 'case'",p);
  1447. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1448. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1449. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1450. parse_line(label);
  1451. syntax.curly_count--;
  1452. syntax.curly[pos].count++;
  1453. }
  1454. return p + 1;
  1455. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1456. // Processing continue
  1457. char label[256];
  1458. int pos = syntax.curly_count - 1;
  1459. while(pos >= 0) {
  1460. if(syntax.curly[pos].type == TYPE_DO) {
  1461. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1462. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1463. break;
  1464. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1465. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1466. break;
  1467. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1468. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1469. break;
  1470. }
  1471. pos--;
  1472. }
  1473. if(pos < 0) {
  1474. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1475. } else {
  1476. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1477. parse_line(label);
  1478. syntax.curly_count--;
  1479. }
  1480. p = skip_space(p2);
  1481. if(*p != ';')
  1482. disp_error_message("parse_syntax: expected ';'",p);
  1483. //Closing decision if, for , while
  1484. p = parse_syntax_close(p + 1);
  1485. return p;
  1486. }
  1487. break;
  1488. case 'd':
  1489. case 'D':
  1490. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1491. // Switch - default processing
  1492. int pos = syntax.curly_count-1;
  1493. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1494. disp_error_message("parse_syntax: unexpected 'default'",p);
  1495. } else if(syntax.curly[pos].flag) {
  1496. disp_error_message("parse_syntax: dup 'default'",p);
  1497. } else {
  1498. char label[256];
  1499. int l;
  1500. // Put the label location
  1501. p = skip_space(p2);
  1502. if(*p != ':') {
  1503. disp_error_message("parse_syntax: expected ':'",p);
  1504. }
  1505. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1506. l=add_str(label);
  1507. set_label(l,script_pos,p);
  1508. // Skip to the next link w/o condition
  1509. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1510. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1511. parse_line(label);
  1512. syntax.curly_count--;
  1513. // The default label
  1514. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1515. l=add_str(label);
  1516. set_label(l,script_pos,p);
  1517. syntax.curly[syntax.curly_count - 1].flag = 1;
  1518. syntax.curly[pos].count++;
  1519. }
  1520. return p + 1;
  1521. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1522. int l;
  1523. char label[256];
  1524. p=skip_space(p2);
  1525. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1526. syntax.curly[syntax.curly_count].count = 1;
  1527. syntax.curly[syntax.curly_count].index = syntax.index++;
  1528. syntax.curly[syntax.curly_count].flag = 0;
  1529. // Label of the (do) form here
  1530. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1531. l=add_str(label);
  1532. set_label(l,script_pos,p);
  1533. syntax.curly_count++;
  1534. return p;
  1535. }
  1536. break;
  1537. case 'f':
  1538. case 'F':
  1539. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1540. int l;
  1541. char label[256];
  1542. int pos = syntax.curly_count;
  1543. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1544. syntax.curly[syntax.curly_count].count = 1;
  1545. syntax.curly[syntax.curly_count].index = syntax.index++;
  1546. syntax.curly[syntax.curly_count].flag = 0;
  1547. syntax.curly_count++;
  1548. p=skip_space(p2);
  1549. if(*p != '(')
  1550. disp_error_message("parse_syntax: expected '('",p);
  1551. p++;
  1552. // Execute the initialization statement
  1553. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1554. p=parse_line(p);
  1555. syntax.curly_count--;
  1556. // Form the start of label decision
  1557. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1558. l=add_str(label);
  1559. set_label(l,script_pos,p);
  1560. p=skip_space(p);
  1561. if(*p == ';') {
  1562. // For (; Because the pattern of always true ;)
  1563. ;
  1564. } else {
  1565. // Skip to the end point if the condition is false
  1566. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1567. add_scriptl(add_str("jump_zero"));
  1568. add_scriptc(C_ARG);
  1569. p=parse_expr(p);
  1570. p=skip_space(p);
  1571. add_scriptl(add_str(label));
  1572. add_scriptc(C_FUNC);
  1573. }
  1574. if(*p != ';')
  1575. disp_error_message("parse_syntax: expected ';'",p);
  1576. p++;
  1577. // Skip to the beginning of the loop
  1578. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1579. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1580. parse_line(label);
  1581. syntax.curly_count--;
  1582. // Labels to form the next loop
  1583. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1584. l=add_str(label);
  1585. set_label(l,script_pos,p);
  1586. // Process the next time you enter the loop
  1587. // A ')' last for; flag to be treated as'
  1588. parse_syntax_for_flag = 1;
  1589. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1590. p=parse_line(p);
  1591. syntax.curly_count--;
  1592. parse_syntax_for_flag = 0;
  1593. // Skip to the determination process conditions
  1594. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1595. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1596. parse_line(label);
  1597. syntax.curly_count--;
  1598. // Loop start labeling
  1599. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1600. l=add_str(label);
  1601. set_label(l,script_pos,p);
  1602. return p;
  1603. }
  1604. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1605. {// internal script function
  1606. const char *func_name;
  1607. func_name = skip_space(p2);
  1608. p = skip_word(func_name);
  1609. if( p == func_name )
  1610. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1611. p2 = skip_space(p);
  1612. if( *p2 == ';' )
  1613. {// function <name> ;
  1614. // function declaration - just register the name
  1615. int l;
  1616. l = add_word(func_name);
  1617. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1618. str_data[l].type = C_USERFUNC;
  1619. else if( str_data[l].type == C_USERFUNC )
  1620. ; // already registered
  1621. else
  1622. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1623. // Close condition of if, for, while
  1624. p = parse_syntax_close(p2 + 1);
  1625. return p;
  1626. }
  1627. else if(*p2 == '{')
  1628. {// function <name> <line/block of code>
  1629. char label[256];
  1630. int l;
  1631. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1632. syntax.curly[syntax.curly_count].count = 1;
  1633. syntax.curly[syntax.curly_count].index = syntax.index++;
  1634. syntax.curly[syntax.curly_count].flag = 0;
  1635. ++syntax.curly_count;
  1636. // Jump over the function code
  1637. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1638. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1639. ++syntax.curly_count;
  1640. parse_line(label);
  1641. --syntax.curly_count;
  1642. // Set the position of the function (label)
  1643. l=add_word(func_name);
  1644. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1645. {
  1646. str_data[l].type = C_USERFUNC;
  1647. set_label(l, script_pos, p);
  1648. if( parse_options&SCRIPT_USE_LABEL_DB )
  1649. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1650. }
  1651. else
  1652. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1653. return skip_space(p);
  1654. }
  1655. else
  1656. {
  1657. disp_error_message("expect ';' or '{' at function syntax",p);
  1658. }
  1659. }
  1660. break;
  1661. case 'i':
  1662. case 'I':
  1663. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1664. // If process
  1665. char label[256];
  1666. p=skip_space(p2);
  1667. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1668. disp_error_message("need '('",p);
  1669. }
  1670. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1671. syntax.curly[syntax.curly_count].count = 1;
  1672. syntax.curly[syntax.curly_count].index = syntax.index++;
  1673. syntax.curly[syntax.curly_count].flag = 0;
  1674. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1675. syntax.curly_count++;
  1676. add_scriptl(add_str("jump_zero"));
  1677. add_scriptc(C_ARG);
  1678. p=parse_expr(p);
  1679. p=skip_space(p);
  1680. add_scriptl(add_str(label));
  1681. add_scriptc(C_FUNC);
  1682. return p;
  1683. }
  1684. break;
  1685. case 's':
  1686. case 'S':
  1687. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1688. // Processing of switch ()
  1689. char label[256];
  1690. p=skip_space(p2);
  1691. if(*p != '(') {
  1692. disp_error_message("need '('",p);
  1693. }
  1694. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1695. syntax.curly[syntax.curly_count].count = 1;
  1696. syntax.curly[syntax.curly_count].index = syntax.index++;
  1697. syntax.curly[syntax.curly_count].flag = 0;
  1698. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1699. syntax.curly_count++;
  1700. add_scriptl(add_str("set"));
  1701. add_scriptc(C_ARG);
  1702. add_scriptl(add_str(label));
  1703. p=parse_expr(p);
  1704. p=skip_space(p);
  1705. if(*p != '{') {
  1706. disp_error_message("parse_syntax: expected '{'",p);
  1707. }
  1708. add_scriptc(C_FUNC);
  1709. return p + 1;
  1710. }
  1711. break;
  1712. case 'w':
  1713. case 'W':
  1714. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1715. int l;
  1716. char label[256];
  1717. p=skip_space(p2);
  1718. if(*p != '(') {
  1719. disp_error_message("need '('",p);
  1720. }
  1721. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1722. syntax.curly[syntax.curly_count].count = 1;
  1723. syntax.curly[syntax.curly_count].index = syntax.index++;
  1724. syntax.curly[syntax.curly_count].flag = 0;
  1725. // Form the start of label decision
  1726. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1727. l=add_str(label);
  1728. set_label(l,script_pos,p);
  1729. // Skip to the end point if the condition is false
  1730. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1731. syntax.curly_count++;
  1732. add_scriptl(add_str("jump_zero"));
  1733. add_scriptc(C_ARG);
  1734. p=parse_expr(p);
  1735. p=skip_space(p);
  1736. add_scriptl(add_str(label));
  1737. add_scriptc(C_FUNC);
  1738. return p;
  1739. }
  1740. break;
  1741. }
  1742. return NULL;
  1743. }
  1744. const char* parse_syntax_close(const char *p) {
  1745. // If (...) for (...) hoge (); as to make sure closed closed once again
  1746. int flag;
  1747. do {
  1748. p = parse_syntax_close_sub(p,&flag);
  1749. } while(flag);
  1750. return p;
  1751. }
  1752. // Close judgment if, for, while, of do
  1753. // flag == 1 : closed
  1754. // flag == 0 : not closed
  1755. const char* parse_syntax_close_sub(const char* p,int* flag)
  1756. {
  1757. char label[256];
  1758. int pos = syntax.curly_count - 1;
  1759. int l;
  1760. *flag = 1;
  1761. if(syntax.curly_count <= 0) {
  1762. *flag = 0;
  1763. return p;
  1764. } else if(syntax.curly[pos].type == TYPE_IF) {
  1765. const char *bp = p;
  1766. const char *p2;
  1767. // if-block and else-block end is a new line
  1768. parse_nextline(false, p);
  1769. // Skip to the last location if
  1770. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1771. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1772. parse_line(label);
  1773. syntax.curly_count--;
  1774. // Put the label of the location
  1775. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1776. l=add_str(label);
  1777. set_label(l,script_pos,p);
  1778. syntax.curly[pos].count++;
  1779. p = skip_space(p);
  1780. p2 = skip_word(p);
  1781. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1782. // else or else - if
  1783. p = skip_space(p2);
  1784. p2 = skip_word(p);
  1785. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1786. // else - if
  1787. p=skip_space(p2);
  1788. if(*p != '(') {
  1789. disp_error_message("need '('",p);
  1790. }
  1791. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1792. add_scriptl(add_str("jump_zero"));
  1793. add_scriptc(C_ARG);
  1794. p=parse_expr(p);
  1795. p=skip_space(p);
  1796. add_scriptl(add_str(label));
  1797. add_scriptc(C_FUNC);
  1798. *flag = 0;
  1799. return p;
  1800. } else {
  1801. // else
  1802. if(!syntax.curly[pos].flag) {
  1803. syntax.curly[pos].flag = 1;
  1804. *flag = 0;
  1805. return p;
  1806. }
  1807. }
  1808. }
  1809. // Close if
  1810. syntax.curly_count--;
  1811. // Put the label of the final location
  1812. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1813. l=add_str(label);
  1814. set_label(l,script_pos,p);
  1815. if(syntax.curly[pos].flag == 1) {
  1816. // Because the position of the pointer is the same if not else for this
  1817. return bp;
  1818. }
  1819. return p;
  1820. } else if(syntax.curly[pos].type == TYPE_DO) {
  1821. int l2;
  1822. char label2[256];
  1823. const char *p2;
  1824. if(syntax.curly[pos].flag) {
  1825. // (Come here continue) to form the label here
  1826. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1827. l2=add_str(label2);
  1828. set_label(l2,script_pos,p);
  1829. }
  1830. // Skip to the end point if the condition is false
  1831. p = skip_space(p);
  1832. p2 = skip_word(p);
  1833. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1834. disp_error_message("parse_syntax: expected 'while'",p);
  1835. p = skip_space(p2);
  1836. if(*p != '(') {
  1837. disp_error_message("need '('",p);
  1838. }
  1839. // do-block end is a new line
  1840. parse_nextline(false, p);
  1841. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1842. add_scriptl(add_str("jump_zero"));
  1843. add_scriptc(C_ARG);
  1844. p=parse_expr(p);
  1845. p=skip_space(p);
  1846. add_scriptl(add_str(label2));
  1847. add_scriptc(C_FUNC);
  1848. // Skip to the starting point
  1849. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1850. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1851. parse_line(label2);
  1852. syntax.curly_count--;
  1853. // Form label of the end point conditions
  1854. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1855. l2=add_str(label2);
  1856. set_label(l2,script_pos,p);
  1857. p = skip_space(p);
  1858. if(*p != ';') {
  1859. disp_error_message("parse_syntax: expected ';'",p);
  1860. return p+1;
  1861. }
  1862. p++;
  1863. syntax.curly_count--;
  1864. return p;
  1865. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1866. // for-block end is a new line
  1867. parse_nextline(false, p);
  1868. // Skip to the next loop
  1869. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1870. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1871. parse_line(label);
  1872. syntax.curly_count--;
  1873. // End for labeling
  1874. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1875. l=add_str(label);
  1876. set_label(l,script_pos,p);
  1877. syntax.curly_count--;
  1878. return p;
  1879. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1880. // while-block end is a new line
  1881. parse_nextline(false, p);
  1882. // Skip to the decision while
  1883. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1884. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1885. parse_line(label);
  1886. syntax.curly_count--;
  1887. // End while labeling
  1888. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1889. l=add_str(label);
  1890. set_label(l,script_pos,p);
  1891. syntax.curly_count--;
  1892. return p;
  1893. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1894. int pos2 = syntax.curly_count-1;
  1895. char label2[256];
  1896. int l2;
  1897. // Back
  1898. sprintf(label2,"return;");
  1899. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1900. parse_line(label2);
  1901. syntax.curly_count--;
  1902. // Put the label of the location
  1903. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1904. l2=add_str(label2);
  1905. set_label(l2,script_pos,p);
  1906. syntax.curly_count--;
  1907. return p;
  1908. } else {
  1909. *flag = 0;
  1910. return p;
  1911. }
  1912. }
  1913. /*==========================================
  1914. * Added built-in functions
  1915. *------------------------------------------*/
  1916. static void add_buildin_func(void)
  1917. {
  1918. int i;
  1919. for( i = 0; buildin_func[i].func; i++ )
  1920. {
  1921. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1922. // 'v' - value (either string or int or reference)
  1923. // 's' - string
  1924. // 'i' - int
  1925. // 'r' - reference (of a variable)
  1926. // 'l' - label
  1927. // '?' - one optional parameter
  1928. // '*' - unknown number of optional parameters
  1929. const char* p = buildin_func[i].arg;
  1930. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1931. while( *p == '?' ) ++p;
  1932. if( *p == '*' ) ++p;
  1933. if( *p != 0){
  1934. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1935. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1936. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1937. } else {
  1938. int n = add_str(buildin_func[i].name);
  1939. str_data[n].type = C_FUNC;
  1940. str_data[n].val = i;
  1941. str_data[n].func = buildin_func[i].func;
  1942. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  1943. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1944. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1945. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1946. }
  1947. }
  1948. }
  1949. /// Retrieves the value of a constant.
  1950. bool script_get_constant(const char* name, int* value)
  1951. {
  1952. int n = search_str(name);
  1953. if( n == -1 || str_data[n].type != C_INT )
  1954. {// not found or not a constant
  1955. return false;
  1956. }
  1957. value[0] = str_data[n].val;
  1958. return true;
  1959. }
  1960. /// Creates new constant or parameter with given value.
  1961. void script_set_constant(const char* name, int value, bool isparameter)
  1962. {
  1963. int n = add_str(name);
  1964. if( str_data[n].type == C_NOP )
  1965. {// new
  1966. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1967. str_data[n].val = value;
  1968. }
  1969. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1970. {// existing parameter or constant
  1971. 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);
  1972. }
  1973. else
  1974. {// existing name
  1975. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1976. }
  1977. }
  1978. /*==========================================
  1979. * Reading constant databases
  1980. * const.txt
  1981. *------------------------------------------*/
  1982. static void read_constdb(void)
  1983. {
  1984. FILE *fp;
  1985. char line[1024],name[1024],val[1024];
  1986. int type;
  1987. int entries=0, skipped=0, linenum=0;
  1988. sprintf(line, "%s/const.txt", db_path);
  1989. fp=fopen(line, "r");
  1990. if(fp==NULL){
  1991. ShowError("can't read %s\n", line);
  1992. return ;
  1993. }
  1994. while(fgets(line, sizeof(line), fp))
  1995. {
  1996. linenum++;
  1997. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  1998. continue;
  1999. if(line[0]=='/' && line[1]=='/') //ignore commented line
  2000. continue;
  2001. type=0;
  2002. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%d",name,val,&type)>=2 ||
  2003. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %d",name,val,&type)>=2){
  2004. entries++;
  2005. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2006. }
  2007. else {
  2008. skipped++;
  2009. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2010. }
  2011. }
  2012. fclose(fp);
  2013. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2014. if(skipped){
  2015. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2016. }
  2017. }
  2018. /**
  2019. * Sets source-end constants for NPC scripts to access.
  2020. **/
  2021. void script_hardcoded_constants(void) {
  2022. /* server defines */
  2023. script_set_constant("PACKETVER",PACKETVER,false);
  2024. script_set_constant("MAX_LEVEL",MAX_LEVEL,false);
  2025. script_set_constant("MAX_STORAGE",MAX_STORAGE,false);
  2026. script_set_constant("MAX_INVENTORY",MAX_INVENTORY,false);
  2027. script_set_constant("MAX_CART",MAX_INVENTORY,false);
  2028. script_set_constant("MAX_ZENY",MAX_ZENY,false);
  2029. script_set_constant("MAX_PARTY",MAX_PARTY,false);
  2030. script_set_constant("MAX_GUILD",MAX_GUILD,false);
  2031. script_set_constant("MAX_GUILDLEVEL",MAX_GUILDLEVEL,false);
  2032. script_set_constant("MAX_GUILD_STORAGE",MAX_GUILD_STORAGE,false);
  2033. script_set_constant("MAX_BG_MEMBERS",MAX_BG_MEMBERS,false);
  2034. script_set_constant("MAX_CHAT_USERS",MAX_CHAT_USERS,false);
  2035. script_set_constant("VIP_SCRIPT",VIP_SCRIPT,false);
  2036. script_set_constant("MIN_STORAGE",MIN_STORAGE,false);
  2037. /* status options */
  2038. script_set_constant("Option_Nothing",OPTION_NOTHING,false);
  2039. script_set_constant("Option_Sight",OPTION_SIGHT,false);
  2040. script_set_constant("Option_Hide",OPTION_HIDE,false);
  2041. script_set_constant("Option_Cloak",OPTION_CLOAK,false);
  2042. script_set_constant("Option_Falcon",OPTION_FALCON,false);
  2043. script_set_constant("Option_Riding",OPTION_RIDING,false);
  2044. script_set_constant("Option_Invisible",OPTION_INVISIBLE,false);
  2045. script_set_constant("Option_Orcish",OPTION_ORCISH,false);
  2046. script_set_constant("Option_Wedding",OPTION_WEDDING,false);
  2047. script_set_constant("Option_Ruwach",OPTION_RUWACH,false);
  2048. script_set_constant("Option_Chasewalk",OPTION_CHASEWALK,false);
  2049. script_set_constant("Option_Flying",OPTION_FLYING,false);
  2050. script_set_constant("Option_Xmas",OPTION_XMAS,false);
  2051. script_set_constant("Option_Transform",OPTION_TRANSFORM,false);
  2052. script_set_constant("Option_Summer",OPTION_SUMMER,false);
  2053. script_set_constant("Option_Dragon1",OPTION_DRAGON1,false);
  2054. script_set_constant("Option_Wug",OPTION_WUG,false);
  2055. script_set_constant("Option_Wugrider",OPTION_WUGRIDER,false);
  2056. script_set_constant("Option_Madogear",OPTION_MADOGEAR,false);
  2057. script_set_constant("Option_Dragon2",OPTION_DRAGON2,false);
  2058. script_set_constant("Option_Dragon3",OPTION_DRAGON3,false);
  2059. script_set_constant("Option_Dragon4",OPTION_DRAGON4,false);
  2060. script_set_constant("Option_Dragon5",OPTION_DRAGON5,false);
  2061. script_set_constant("Option_Hanbok",OPTION_HANBOK,false);
  2062. script_set_constant("Option_Oktoberfest",OPTION_OKTOBERFEST,false);
  2063. /* status option compounds */
  2064. script_set_constant("Option_Dragon",OPTION_DRAGON,false);
  2065. script_set_constant("Option_Costume",OPTION_COSTUME,false);
  2066. /* sc_start flags */
  2067. script_set_constant("SCSTART_NONE",SCSTART_NONE,false);
  2068. script_set_constant("SCSTART_NOAVOID",SCSTART_NOAVOID,false);
  2069. script_set_constant("SCSTART_NOTICKDEF",SCSTART_NOTICKDEF,false);
  2070. script_set_constant("SCSTART_LOADED",SCSTART_LOADED,false);
  2071. script_set_constant("SCSTART_NORATEDEF",SCSTART_NORATEDEF,false);
  2072. script_set_constant("SCSTART_NOICON",SCSTART_NOICON,false);
  2073. }
  2074. /*==========================================
  2075. * Display emplacement line of script
  2076. *------------------------------------------*/
  2077. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2078. {
  2079. int i;
  2080. if( p == NULL || !p[0] ) return NULL;
  2081. if( line < 0 )
  2082. StringBuf_Printf(buf, "*% 5d : ", -line);
  2083. else
  2084. StringBuf_Printf(buf, " % 5d : ", line);
  2085. for(i=0;p[i] && p[i] != '\n';i++){
  2086. if(p + i != mark)
  2087. StringBuf_Printf(buf, "%c", p[i]);
  2088. else
  2089. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2090. }
  2091. StringBuf_AppendStr(buf, "\n");
  2092. return p+i+(p[i] == '\n' ? 1 : 0);
  2093. }
  2094. 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) {
  2095. // Find the line where the error occurred
  2096. int j;
  2097. int line = start_line;
  2098. const char *p;
  2099. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2100. for(p=src;p && *p;line++){
  2101. const char *lineend=strchr(p,'\n');
  2102. if(lineend==NULL || error_pos_cur<lineend){
  2103. break;
  2104. }
  2105. for( j = 0; j < 4; j++ ) {
  2106. linestart[j] = linestart[j+1];
  2107. }
  2108. linestart[4] = p;
  2109. p=lineend+1;
  2110. }
  2111. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2112. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2113. for(j = 0; j < 5; j++ ) {
  2114. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2115. }
  2116. p = script_print_line(buf, p, error_pos_cur, -line);
  2117. for(j = 0; j < 5; j++) {
  2118. p = script_print_line(buf, p, NULL, line + j + 1 );
  2119. }
  2120. }
  2121. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2122. StringBuf buf;
  2123. StringBuf_Init(&buf);
  2124. StringBuf_AppendStr(&buf, "\a\n");
  2125. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2126. ShowError("%s", StringBuf_Value(&buf));
  2127. StringBuf_Destroy(&buf);
  2128. }
  2129. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2130. StringBuf buf;
  2131. StringBuf_Init(&buf);
  2132. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2133. ShowWarning("%s", StringBuf_Value(&buf));
  2134. StringBuf_Destroy(&buf);
  2135. }
  2136. /*==========================================
  2137. * Analysis of the script
  2138. *------------------------------------------*/
  2139. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2140. {
  2141. const char *p,*tmpp;
  2142. int i;
  2143. struct script_code* code = NULL;
  2144. static int first=1;
  2145. char end;
  2146. bool unresolved_names = false;
  2147. parser_current_src = src;
  2148. parser_current_file = file;
  2149. parser_current_line = line;
  2150. if( src == NULL )
  2151. return NULL;// empty script
  2152. memset(&syntax,0,sizeof(syntax));
  2153. if(first){
  2154. add_buildin_func();
  2155. read_constdb();
  2156. script_hardcoded_constants();
  2157. first=0;
  2158. }
  2159. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2160. script_pos=0;
  2161. script_size=SCRIPT_BLOCK_SIZE;
  2162. parse_nextline(true, NULL);
  2163. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2164. if( options&SCRIPT_USE_LABEL_DB )
  2165. db_clear(scriptlabel_db);
  2166. parse_options = options;
  2167. if( setjmp( error_jump ) != 0 ) {
  2168. //Restore program state when script has problems. [from jA]
  2169. int j;
  2170. const int size = ARRAYLENGTH(syntax.curly);
  2171. if( error_report )
  2172. script_error(src,file,line,error_msg,error_pos);
  2173. aFree( error_msg );
  2174. aFree( script_buf );
  2175. script_pos = 0;
  2176. script_size = 0;
  2177. script_buf = NULL;
  2178. for(j=LABEL_START;j<str_num;j++)
  2179. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2180. for(j=0; j<size; j++)
  2181. linkdb_final(&syntax.curly[j].case_label);
  2182. return NULL;
  2183. }
  2184. parse_syntax_for_flag=0;
  2185. p=src;
  2186. p=skip_space(p);
  2187. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2188. {// does not require brackets around the script
  2189. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2190. {// empty script and can return NULL
  2191. aFree( script_buf );
  2192. script_pos = 0;
  2193. script_size = 0;
  2194. script_buf = NULL;
  2195. return NULL;
  2196. }
  2197. end = '\0';
  2198. }
  2199. else
  2200. {// requires brackets around the script
  2201. if( *p != '{' )
  2202. disp_error_message("not found '{'",p);
  2203. p = skip_space(p+1);
  2204. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2205. {// empty script and can return NULL
  2206. aFree( script_buf );
  2207. script_pos = 0;
  2208. script_size = 0;
  2209. script_buf = NULL;
  2210. return NULL;
  2211. }
  2212. end = '}';
  2213. }
  2214. // clear references of labels, variables and internal functions
  2215. for(i=LABEL_START;i<str_num;i++){
  2216. if(
  2217. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2218. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2219. ){
  2220. str_data[i].type=C_NOP;
  2221. str_data[i].backpatch=-1;
  2222. str_data[i].label=-1;
  2223. }
  2224. }
  2225. while( syntax.curly_count != 0 || *p != end )
  2226. {
  2227. if( *p == '\0' )
  2228. disp_error_message("unexpected end of script",p);
  2229. // Special handling only label
  2230. tmpp=skip_space(skip_word(p));
  2231. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2232. i=add_word(p);
  2233. set_label(i,script_pos,p);
  2234. if( parse_options&SCRIPT_USE_LABEL_DB )
  2235. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2236. p=tmpp+1;
  2237. p=skip_space(p);
  2238. continue;
  2239. }
  2240. // All other lumped
  2241. p=parse_line(p);
  2242. p=skip_space(p);
  2243. parse_nextline(false, p);
  2244. }
  2245. add_scriptc(C_NOP);
  2246. // trim code to size
  2247. script_size = script_pos;
  2248. RECREATE(script_buf,unsigned char,script_pos);
  2249. // default unknown references to variables
  2250. for(i=LABEL_START;i<str_num;i++){
  2251. if(str_data[i].type==C_NOP){
  2252. int j;
  2253. str_data[i].type=C_NAME;
  2254. str_data[i].label=i;
  2255. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2256. int next=GETVALUE(script_buf,j);
  2257. SETVALUE(script_buf,j,i);
  2258. j=next;
  2259. }
  2260. }
  2261. else if( str_data[i].type == C_USERFUNC )
  2262. {// 'function name;' without follow-up code
  2263. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2264. unresolved_names = true;
  2265. }
  2266. }
  2267. if( unresolved_names )
  2268. {
  2269. disp_error_message("parse_script: unresolved function references", p);
  2270. }
  2271. #ifdef DEBUG_DISP
  2272. for(i=0;i<script_pos;i++){
  2273. if((i&15)==0) ShowMessage("%04x : ",i);
  2274. ShowMessage("%02x ",script_buf[i]);
  2275. if((i&15)==15) ShowMessage("\n");
  2276. }
  2277. ShowMessage("\n");
  2278. #endif
  2279. #ifdef DEBUG_DISASM
  2280. {
  2281. int i = 0,j;
  2282. while(i < script_pos) {
  2283. c_op op = get_com(script_buf,&i);
  2284. ShowMessage("%06x %s", i, script_op2name(op));
  2285. j = i;
  2286. switch(op) {
  2287. case C_INT:
  2288. ShowMessage(" %d", get_num(script_buf,&i));
  2289. break;
  2290. case C_POS:
  2291. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2292. i += 3;
  2293. break;
  2294. case C_NAME:
  2295. j = (*(int*)(script_buf+i)&0xffffff);
  2296. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2297. i += 3;
  2298. break;
  2299. case C_STR:
  2300. j = strlen(script_buf + i);
  2301. ShowMessage(" %s", script_buf + i);
  2302. i += j+1;
  2303. break;
  2304. }
  2305. ShowMessage(CL_CLL"\n");
  2306. }
  2307. }
  2308. #endif
  2309. CREATE(code,struct script_code,1);
  2310. code->script_buf = script_buf;
  2311. code->script_size = script_size;
  2312. code->local.vars = NULL;
  2313. code->local.arrays = NULL;
  2314. return code;
  2315. }
  2316. /// Returns the player attached to this script, identified by the rid.
  2317. /// If there is no player attached, the script is terminated.
  2318. TBL_PC *script_rid2sd(struct script_state *st)
  2319. {
  2320. TBL_PC *sd=map_id2sd(st->rid);
  2321. if(!sd){
  2322. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2323. script_reportfunc(st);
  2324. script_reportsrc(st);
  2325. st->state = END;
  2326. }
  2327. return sd;
  2328. }
  2329. /**
  2330. * Dereferences a variable/constant, replacing it with a copy of the value.
  2331. * @param st Script state
  2332. * @param data Variable/constant
  2333. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2334. */
  2335. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2336. {
  2337. const char* name;
  2338. char prefix;
  2339. char postfix;
  2340. if( !data_isreference(data) )
  2341. return;// not a variable/constant
  2342. name = reference_getname(data);
  2343. prefix = name[0];
  2344. postfix = name[strlen(name) - 1];
  2345. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2346. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2347. if (sd == NULL)
  2348. sd = script_rid2sd(st);
  2349. if( sd == NULL ) {// needs player attached
  2350. if( postfix == '$' ) {// string variable
  2351. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2352. data->type = C_CONSTSTR;
  2353. data->u.str = "";
  2354. } else {// integer variable
  2355. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2356. data->type = C_INT;
  2357. data->u.num = 0;
  2358. }
  2359. return;
  2360. }
  2361. }
  2362. if( postfix == '$' ) {// string variable
  2363. switch( prefix ) {
  2364. case '@':
  2365. data->u.str = pc_readregstr(sd, data->u.num);
  2366. break;
  2367. case '$':
  2368. data->u.str = mapreg_readregstr(data->u.num);
  2369. break;
  2370. case '#':
  2371. if( name[1] == '#' )
  2372. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2373. else
  2374. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2375. break;
  2376. case '.':
  2377. {
  2378. struct DBMap* n = data->ref ?
  2379. data->ref->vars : name[1] == '@' ?
  2380. st->stack->scope.vars : // instance/scope variable
  2381. st->script->local.vars; // npc variable
  2382. if( n )
  2383. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2384. else
  2385. data->u.str = NULL;
  2386. }
  2387. break;
  2388. case '\'':
  2389. {
  2390. int instance_id = script_instancegetid(st);
  2391. if( instance_id )
  2392. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2393. else {
  2394. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2395. data->u.str = NULL;
  2396. }
  2397. }
  2398. break;
  2399. default:
  2400. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2401. break;
  2402. }
  2403. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2404. data->type = C_CONSTSTR;
  2405. data->u.str = "";
  2406. } else {// duplicate string
  2407. data->type = C_STR;
  2408. data->u.str = aStrdup(data->u.str);
  2409. }
  2410. } else {// integer variable
  2411. data->type = C_INT;
  2412. if( reference_toconstant(data) ) {
  2413. data->u.num = reference_getconstant(data);
  2414. } else if( reference_toparam(data) ) {
  2415. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2416. } else
  2417. switch( prefix ) {
  2418. case '@':
  2419. data->u.num = pc_readreg(sd, data->u.num);
  2420. break;
  2421. case '$':
  2422. data->u.num = mapreg_readreg(data->u.num);
  2423. break;
  2424. case '#':
  2425. if( name[1] == '#' )
  2426. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2427. else
  2428. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2429. break;
  2430. case '.':
  2431. {
  2432. struct DBMap* n = data->ref ?
  2433. data->ref->vars : name[1] == '@' ?
  2434. st->stack->scope.vars : // instance/scope variable
  2435. st->script->local.vars; // npc variable
  2436. if( n )
  2437. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2438. else
  2439. data->u.num = 0;
  2440. }
  2441. break;
  2442. case '\'':
  2443. {
  2444. int instance_id = script_instancegetid(st);
  2445. if( instance_id )
  2446. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2447. else {
  2448. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2449. data->u.num = 0;
  2450. }
  2451. }
  2452. break;
  2453. default:
  2454. data->u.num = pc_readglobalreg(sd, data->u.num);
  2455. break;
  2456. }
  2457. }
  2458. data->ref = NULL;
  2459. return;
  2460. }
  2461. void get_val(struct script_state* st, struct script_data* data)
  2462. {
  2463. get_val_(st,data,NULL);
  2464. }
  2465. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2466. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2467. /// The value is left in the top of the stack and needs to be removed manually.
  2468. /// @param st[in]: script state.
  2469. /// @param uid[in]: reference identifier.
  2470. /// @param ref[in]: the container to look up the reference into.
  2471. /// @return: the retrieved value of the reference.
  2472. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2473. {
  2474. struct script_data* data;
  2475. push_val2(st->stack, C_NAME, uid, ref);
  2476. data = script_getdatatop(st, -1);
  2477. get_val(st, data);
  2478. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2479. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2480. }
  2481. /**
  2482. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2483. * Will be gone as soon as undefined var feature is implemented
  2484. **/
  2485. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2486. {
  2487. const char *name = get_str(script_getvarid(uid));
  2488. // is here st can be null pointer and st->rid is wrong?
  2489. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2490. bool insert = false;
  2491. if (sd && !st) {
  2492. // when sd comes, st isn't available
  2493. insert = true;
  2494. } else {
  2495. if( is_string_variable(name) ) {
  2496. char* str = (char*)get_val2(st, uid, ref);
  2497. if( str && *str )
  2498. insert = true;
  2499. script_removetop(st, -1, 0);
  2500. } else {
  2501. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2502. if( num )
  2503. insert = true;
  2504. script_removetop(st, -1, 0);
  2505. }
  2506. }
  2507. if (src && src->arrays) {
  2508. struct script_array *sa = idb_get(src->arrays, script_getvarid(uid));
  2509. if (sa) {
  2510. unsigned int i;
  2511. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2512. if( i != sa->size ) {
  2513. if( !insert )
  2514. script_array_remove_member(src,sa,i);
  2515. return;
  2516. }
  2517. script_array_add_member(sa,0);
  2518. } else if (insert) {
  2519. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2520. }
  2521. }
  2522. }
  2523. /**
  2524. * Returns array size by ID
  2525. **/
  2526. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2527. {
  2528. struct script_array *sa = NULL;
  2529. struct reg_db *src = script_array_src(st, sd, name, ref);
  2530. if (src && src->arrays)
  2531. sa = idb_get(src->arrays, search_str(name));
  2532. return sa ? sa->size : 0;
  2533. }
  2534. /**
  2535. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2536. **/
  2537. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2538. {
  2539. struct script_array *sa = NULL;
  2540. struct reg_db *src = script_array_src(st, sd, name, ref);
  2541. if (src && src->arrays) {
  2542. int key = add_word(name);
  2543. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2544. if( ( sa = idb_get(src->arrays, key) ) ) {
  2545. unsigned int i, highest_key = 0;
  2546. for(i = 0; i < sa->size; i++) {
  2547. if( sa->members[i] > highest_key )
  2548. highest_key = sa->members[i];
  2549. }
  2550. return sa->size ? highest_key + 1 : 0;
  2551. }
  2552. }
  2553. return 0;
  2554. }
  2555. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2556. {
  2557. struct script_array *sa = db_data2ptr(data);
  2558. aFree(sa->members);
  2559. ers_free(array_ers, sa);
  2560. return 0;
  2561. }
  2562. /**
  2563. * Clears script_array and removes it from script->array_db
  2564. **/
  2565. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2566. {
  2567. aFree(sa->members);
  2568. idb_remove(src->arrays, sa->id);
  2569. ers_free(array_ers, sa);
  2570. }
  2571. /**
  2572. * Removes a member from a script_array list
  2573. *
  2574. * @param idx the index of the member in script_array struct list, not of the actual array member
  2575. **/
  2576. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2577. {
  2578. unsigned int i, cursor;
  2579. // it's the only member left, no need to do anything other than delete the array data
  2580. if( sa->size == 1 ) {
  2581. script_array_delete(src,sa);
  2582. return;
  2583. }
  2584. sa->members[idx] = UINT_MAX;
  2585. for(i = 0, cursor = 0; i < sa->size; i++) {
  2586. if( sa->members[i] == UINT_MAX )
  2587. continue;
  2588. if( i != cursor )
  2589. sa->members[cursor] = sa->members[i];
  2590. cursor++;
  2591. }
  2592. sa->size = cursor;
  2593. }
  2594. /**
  2595. * Appends a new array index to the list in script_array
  2596. *
  2597. * @param idx the index of the array member being inserted
  2598. **/
  2599. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2600. {
  2601. RECREATE(sa->members, unsigned int, ++sa->size);
  2602. sa->members[sa->size - 1] = idx;
  2603. }
  2604. /**
  2605. * Obtains the source of the array database for this type and scenario
  2606. * Initializes such database when not yet initialized.
  2607. **/
  2608. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2609. {
  2610. struct reg_db *src = NULL;
  2611. switch( name[0] ) {
  2612. // from player
  2613. default: // char reg
  2614. case '@': // temp char reg
  2615. case '#': // account reg
  2616. src = &sd->regs;
  2617. break;
  2618. case '$': // map reg
  2619. src = &regs;
  2620. break;
  2621. case '.': // npc/script
  2622. if (ref)
  2623. src = ref;
  2624. else
  2625. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2626. break;
  2627. case '\'': // instance
  2628. {
  2629. int instance_id = script_instancegetid(st);
  2630. if( instance_id ) {
  2631. src = &instance_data[instance_id].regs;
  2632. }
  2633. break;
  2634. }
  2635. }
  2636. if( src ) {
  2637. if( !src->arrays )
  2638. src->arrays = idb_alloc(DB_OPT_BASE);
  2639. return src;
  2640. }
  2641. return NULL;
  2642. }
  2643. /**
  2644. * Processes a array member modification, and update data accordingly
  2645. *
  2646. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2647. * @param num[in] Variable ID
  2648. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2649. **/
  2650. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2651. {
  2652. struct script_array *sa = NULL;
  2653. int id = script_getvarid(num);
  2654. unsigned int index = script_getvaridx(num);
  2655. if (!src->arrays) {
  2656. src->arrays = idb_alloc(DB_OPT_BASE);
  2657. } else {
  2658. sa = idb_get(src->arrays, id);
  2659. }
  2660. if( sa ) {
  2661. unsigned int i;
  2662. // search
  2663. for(i = 0; i < sa->size; i++) {
  2664. if( sa->members[i] == index )
  2665. break;
  2666. }
  2667. // if existent
  2668. if( i != sa->size ) {
  2669. // if empty, we gotta remove it
  2670. if( empty ) {
  2671. script_array_remove_member(src, sa, i);
  2672. }
  2673. } else if( !empty ) { /* new entry */
  2674. script_array_add_member(sa,index);
  2675. // we do nothing if its empty, no point in modifying array data for a new empty member
  2676. }
  2677. } else if ( !empty ) { // we only move to create if not empty
  2678. sa = ers_alloc(array_ers, struct script_array);
  2679. sa->id = id;
  2680. sa->members = NULL;
  2681. sa->size = 0;
  2682. script_array_add_member(sa,index);
  2683. idb_put(src->arrays, id, sa);
  2684. }
  2685. }
  2686. /**
  2687. * Stores the value of a script variable
  2688. *
  2689. * @param st: current script state.
  2690. * @param sd: current character data.
  2691. * @param num: variable identifier.
  2692. * @param name: variable name.
  2693. * @param value: new value.
  2694. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2695. * @return: 0 failure, 1 success.
  2696. *
  2697. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2698. *------------------------------------------*/
  2699. int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2700. {
  2701. char prefix = name[0];
  2702. if( is_string_variable(name) ) {// string variable
  2703. const char *str = (const char*)value;
  2704. switch (prefix) {
  2705. case '@':
  2706. pc_setregstr(sd, num, str);
  2707. return 1;
  2708. case '$':
  2709. return mapreg_setregstr(num, str);
  2710. case '#':
  2711. return (name[1] == '#') ?
  2712. pc_setaccountreg2str(sd, num, str) :
  2713. pc_setaccountregstr(sd, num, str);
  2714. case '.':
  2715. {
  2716. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2717. if( n ) {
  2718. if (str[0]) {
  2719. i64db_put(n->vars, num, aStrdup(str));
  2720. if( script_getvaridx(num) )
  2721. script_array_update(n, num, false);
  2722. } else {
  2723. i64db_remove(n->vars, num);
  2724. if( script_getvaridx(num) )
  2725. script_array_update(n, num, true);
  2726. }
  2727. }
  2728. }
  2729. return 1;
  2730. case '\'':
  2731. {
  2732. int instance_id = script_instancegetid(st);
  2733. if( instance_id ) {
  2734. if( str[0] ) {
  2735. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2736. if( script_getvaridx(num) )
  2737. script_array_update(&instance_data[instance_id].regs, num, false);
  2738. } else {
  2739. i64db_remove(instance_data[instance_id].regs.vars, num);
  2740. if (script_getvaridx(num))
  2741. script_array_update(&instance_data[instance_id].regs, num, true);
  2742. }
  2743. } else {
  2744. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2745. script_reportsrc(st);
  2746. }
  2747. return 1;
  2748. }
  2749. default:
  2750. return pc_setglobalreg_str(sd, num, str);
  2751. }
  2752. } else {// integer variable
  2753. int val = (int)__64BPRTSIZE(value);
  2754. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2755. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2756. if( st != NULL ) {
  2757. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2758. script_reportsrc(st);
  2759. st->state = END;
  2760. }
  2761. return 0;
  2762. }
  2763. return 1;
  2764. }
  2765. switch (prefix) {
  2766. case '@':
  2767. pc_setreg(sd, num, val);
  2768. return 1;
  2769. case '$':
  2770. return mapreg_setreg(num, val);
  2771. case '#':
  2772. return (name[1] == '#') ?
  2773. pc_setaccountreg2(sd, num, val) :
  2774. pc_setaccountreg(sd, num, val);
  2775. case '.':
  2776. {
  2777. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2778. if( n ) {
  2779. if( val != 0 ) {
  2780. i64db_iput(n->vars, num, val);
  2781. if( script_getvaridx(num) )
  2782. script_array_update(n, num, false);
  2783. } else {
  2784. i64db_remove(n->vars, num);
  2785. if( script_getvaridx(num) )
  2786. script_array_update(n, num, true);
  2787. }
  2788. }
  2789. }
  2790. return 1;
  2791. case '\'':
  2792. {
  2793. int instance_id = script_instancegetid(st);
  2794. if( instance_id ) {
  2795. if( val != 0 ) {
  2796. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2797. if( script_getvaridx(num) )
  2798. script_array_update(&instance_data[instance_id].regs, num, false);
  2799. } else {
  2800. i64db_remove(instance_data[instance_id].regs.vars, num);
  2801. if (script_getvaridx(num))
  2802. script_array_update(&instance_data[instance_id].regs, num, true);
  2803. }
  2804. } else {
  2805. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2806. script_reportsrc(st);
  2807. }
  2808. return 1;
  2809. }
  2810. default:
  2811. return pc_setglobalreg(sd, num, val);
  2812. }
  2813. }
  2814. }
  2815. int set_var(TBL_PC* sd, char* name, void* val)
  2816. {
  2817. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2818. }
  2819. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2820. {
  2821. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2822. }
  2823. /**
  2824. * Converts the data to a string
  2825. * @param st
  2826. * @param data
  2827. * @param sd
  2828. */
  2829. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2830. {
  2831. char* p;
  2832. get_val_(st, data, sd);
  2833. if( data_isstring(data) )
  2834. {// nothing to convert
  2835. }
  2836. else if( data_isint(data) )
  2837. {// int -> string
  2838. CREATE(p, char, ITEM_NAME_LENGTH);
  2839. snprintf(p, ITEM_NAME_LENGTH, "%"PRId64"", data->u.num);
  2840. p[ITEM_NAME_LENGTH-1] = '\0';
  2841. data->type = C_STR;
  2842. data->u.str = p;
  2843. }
  2844. else if( data_isreference(data) )
  2845. {// reference -> string
  2846. //##TODO when does this happen (check get_val) [FlavioJS]
  2847. data->type = C_CONSTSTR;
  2848. data->u.str = reference_getname(data);
  2849. }
  2850. else
  2851. {// unsupported data type
  2852. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2853. script_reportdata(data);
  2854. script_reportsrc(st);
  2855. data->type = C_CONSTSTR;
  2856. data->u.str = "";
  2857. }
  2858. return data->u.str;
  2859. }
  2860. const char* conv_str(struct script_state* st, struct script_data* data)
  2861. {
  2862. return conv_str_(st, data, NULL);
  2863. }
  2864. /**
  2865. * Converts the data to an int
  2866. * @param st
  2867. * @param data
  2868. * @param sd
  2869. */
  2870. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2871. {
  2872. get_val_(st, data, sd);
  2873. if( data_isint(data) )
  2874. {// nothing to convert
  2875. }
  2876. else if( data_isstring(data) )
  2877. {// string -> int
  2878. // the result does not overflow or underflow, it is capped instead
  2879. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2880. char* p = data->u.str;
  2881. long num;
  2882. errno = 0;
  2883. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2884. if( errno == ERANGE
  2885. #if LONG_MAX > INT_MAX
  2886. || num < INT_MIN || num > INT_MAX
  2887. #endif
  2888. )
  2889. {
  2890. if( num <= INT_MIN )
  2891. {
  2892. num = INT_MIN;
  2893. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2894. }
  2895. else//if( num >= INT_MAX )
  2896. {
  2897. num = INT_MAX;
  2898. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2899. }
  2900. script_reportdata(data);
  2901. script_reportsrc(st);
  2902. }
  2903. if( data->type == C_STR )
  2904. aFree(p);
  2905. data->type = C_INT;
  2906. data->u.num = (int)num;
  2907. }
  2908. #if 0
  2909. // FIXME this function is being used to retrieve the position of labels and
  2910. // probably other stuff [FlavioJS]
  2911. else
  2912. {// unsupported data type
  2913. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2914. script_reportdata(data);
  2915. script_reportsrc(st);
  2916. data->type = C_INT;
  2917. data->u.num = 0;
  2918. }
  2919. #endif
  2920. return (int)data->u.num;
  2921. }
  2922. int conv_num(struct script_state* st, struct script_data* data)
  2923. {
  2924. return conv_num_(st, data, NULL);
  2925. }
  2926. //
  2927. // Stack operations
  2928. //
  2929. /// Increases the size of the stack
  2930. void stack_expand(struct script_stack* stack)
  2931. {
  2932. stack->sp_max += 64;
  2933. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2934. stack->sp_max * sizeof(stack->stack_data[0]) );
  2935. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2936. 64 * sizeof(stack->stack_data[0]) );
  2937. }
  2938. /// Pushes a value into the stack
  2939. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2940. /// Pushes a value into the stack (with reference)
  2941. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  2942. {
  2943. if( stack->sp >= stack->sp_max )
  2944. stack_expand(stack);
  2945. stack->stack_data[stack->sp].type = type;
  2946. stack->stack_data[stack->sp].u.num = val;
  2947. stack->stack_data[stack->sp].ref = ref;
  2948. stack->sp++;
  2949. return &stack->stack_data[stack->sp-1];
  2950. }
  2951. /// Pushes a string into the stack
  2952. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2953. {
  2954. if( stack->sp >= stack->sp_max )
  2955. stack_expand(stack);
  2956. stack->stack_data[stack->sp].type = type;
  2957. stack->stack_data[stack->sp].u.str = str;
  2958. stack->stack_data[stack->sp].ref = NULL;
  2959. stack->sp++;
  2960. return &stack->stack_data[stack->sp-1];
  2961. }
  2962. /// Pushes a retinfo into the stack
  2963. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  2964. {
  2965. if( stack->sp >= stack->sp_max )
  2966. stack_expand(stack);
  2967. stack->stack_data[stack->sp].type = C_RETINFO;
  2968. stack->stack_data[stack->sp].u.ri = ri;
  2969. stack->stack_data[stack->sp].ref = ref;
  2970. stack->sp++;
  2971. return &stack->stack_data[stack->sp-1];
  2972. }
  2973. /// Pushes a copy of the target position into the stack
  2974. struct script_data* push_copy(struct script_stack* stack, int pos)
  2975. {
  2976. switch( stack->stack_data[pos].type )
  2977. {
  2978. case C_CONSTSTR:
  2979. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2980. break;
  2981. case C_STR:
  2982. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2983. break;
  2984. case C_RETINFO:
  2985. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2986. exit(1);
  2987. break;
  2988. default:
  2989. return push_val2(
  2990. stack,stack->stack_data[pos].type,
  2991. stack->stack_data[pos].u.num,
  2992. stack->stack_data[pos].ref
  2993. );
  2994. break;
  2995. }
  2996. }
  2997. /// Removes the values in indexes [start,end] from the stack.
  2998. /// Adjusts all stack pointers.
  2999. void pop_stack(struct script_state* st, int start, int end)
  3000. {
  3001. struct script_stack* stack = st->stack;
  3002. struct script_data* data;
  3003. int i;
  3004. if( start < 0 )
  3005. start = 0;
  3006. if( end > stack->sp )
  3007. end = stack->sp;
  3008. if( start >= end )
  3009. return;// nothing to pop
  3010. // free stack elements
  3011. for( i = start; i < end; i++ )
  3012. {
  3013. data = &stack->stack_data[i];
  3014. if( data->type == C_STR )
  3015. aFree(data->u.str);
  3016. if( data->type == C_RETINFO ) {
  3017. struct script_retinfo* ri = data->u.ri;
  3018. if (ri->scope.vars) {
  3019. script_free_vars(ri->scope.vars);
  3020. ri->scope.vars = NULL;
  3021. }
  3022. if (ri->scope.arrays) {
  3023. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3024. ri->scope.arrays = NULL;
  3025. }
  3026. if( data->ref )
  3027. aFree(data->ref);
  3028. aFree(ri);
  3029. }
  3030. data->type = C_NOP;
  3031. }
  3032. // move the rest of the elements
  3033. if( stack->sp > end )
  3034. {
  3035. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3036. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3037. stack->stack_data[i].type = C_NOP;
  3038. }
  3039. // adjust stack pointers
  3040. if( st->start > end ) st->start -= end - start;
  3041. else if( st->start > start ) st->start = start;
  3042. if( st->end > end ) st->end -= end - start;
  3043. else if( st->end > start ) st->end = start;
  3044. if( stack->defsp > end ) stack->defsp -= end - start;
  3045. else if( stack->defsp > start ) stack->defsp = start;
  3046. stack->sp -= end - start;
  3047. }
  3048. ///
  3049. ///
  3050. ///
  3051. /*==========================================
  3052. * Release script dependent variable, dependent variable of function
  3053. *------------------------------------------*/
  3054. void script_free_vars(struct DBMap* storage)
  3055. {
  3056. if( storage ) {
  3057. // destroy the storage construct containing the variables
  3058. db_destroy(storage);
  3059. }
  3060. }
  3061. void script_free_code(struct script_code* code)
  3062. {
  3063. nullpo_retv(code);
  3064. if (code->instances)
  3065. script_stop_instances(code);
  3066. script_free_vars(code->local.vars);
  3067. if (code->local.arrays)
  3068. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3069. aFree(code->script_buf);
  3070. aFree(code);
  3071. }
  3072. /// Creates a new script state.
  3073. ///
  3074. /// @param script Script code
  3075. /// @param pos Position in the code
  3076. /// @param rid Who is running the script (attached player)
  3077. /// @param oid Where the code is being run (npc 'object')
  3078. /// @return Script state
  3079. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3080. {
  3081. struct script_state* st;
  3082. st = ers_alloc(st_ers, struct script_state);
  3083. st->stack = ers_alloc(stack_ers, struct script_stack);
  3084. st->stack->sp = 0;
  3085. st->stack->sp_max = 64;
  3086. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3087. st->stack->defsp = st->stack->sp;
  3088. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3089. st->stack->scope.arrays = NULL;
  3090. st->state = RUN;
  3091. st->script = rootscript;
  3092. st->pos = pos;
  3093. st->rid = rid;
  3094. st->oid = oid;
  3095. st->sleep.timer = INVALID_TIMER;
  3096. st->npc_item_flag = battle_config.item_enabled_npc;
  3097. if( st->script->instances != USHRT_MAX )
  3098. st->script->instances++;
  3099. else {
  3100. struct npc_data *nd = map_id2nd(oid);
  3101. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3102. }
  3103. if (!st->script->local.vars)
  3104. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3105. st->id = next_id++;
  3106. active_scripts++;
  3107. idb_put(st_db, st->id, st);
  3108. return st;
  3109. }
  3110. /// Frees a script state.
  3111. ///
  3112. /// @param st Script state
  3113. void script_free_state(struct script_state* st)
  3114. {
  3115. if (idb_exists(st_db, st->id)) {
  3116. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3117. if (st->bk_st) // backup was not restored
  3118. 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);
  3119. if (sd && sd->st == st) { // Current script is aborted.
  3120. if(sd->state.using_fake_npc) {
  3121. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3122. sd->state.using_fake_npc = 0;
  3123. }
  3124. sd->st = NULL;
  3125. sd->npc_id = 0;
  3126. }
  3127. if (st->sleep.timer != INVALID_TIMER)
  3128. delete_timer(st->sleep.timer, run_script_timer);
  3129. if (st->stack) {
  3130. script_free_vars(st->stack->scope.vars);
  3131. if (st->stack->scope.arrays)
  3132. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3133. pop_stack(st, 0, st->stack->sp);
  3134. aFree(st->stack->stack_data);
  3135. ers_free(stack_ers, st->stack);
  3136. st->stack = NULL;
  3137. }
  3138. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3139. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3140. script_free_vars(st->script->local.vars);
  3141. st->script->local.vars = NULL;
  3142. }
  3143. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3144. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3145. st->script->local.arrays = NULL;
  3146. }
  3147. }
  3148. st->pos = -1;
  3149. idb_remove(st_db, st->id);
  3150. ers_free(st_ers, st);
  3151. if (--active_scripts == 0)
  3152. next_id = 0;
  3153. }
  3154. }
  3155. //
  3156. // Main execution unit
  3157. //
  3158. /*==========================================
  3159. * Read command
  3160. *------------------------------------------*/
  3161. c_op get_com(unsigned char *script,int *pos)
  3162. {
  3163. int i = 0, j = 0;
  3164. if(script[*pos]>=0x80){
  3165. return C_INT;
  3166. }
  3167. while(script[*pos]>=0x40){
  3168. i=script[(*pos)++]<<j;
  3169. j+=6;
  3170. }
  3171. return (c_op)(i+(script[(*pos)++]<<j));
  3172. }
  3173. /*==========================================
  3174. * Income figures
  3175. *------------------------------------------*/
  3176. int get_num(unsigned char *script,int *pos)
  3177. {
  3178. int i,j;
  3179. i=0; j=0;
  3180. while(script[*pos]>=0xc0){
  3181. i+=(script[(*pos)++]&0x7f)<<j;
  3182. j+=6;
  3183. }
  3184. return i+((script[(*pos)++]&0x7f)<<j);
  3185. }
  3186. /// Ternary operators
  3187. /// test ? if_true : if_false
  3188. void op_3(struct script_state* st, int op)
  3189. {
  3190. struct script_data* data;
  3191. int flag = 0;
  3192. data = script_getdatatop(st, -3);
  3193. get_val(st, data);
  3194. if( data_isstring(data) )
  3195. flag = data->u.str[0];// "" -> false
  3196. else if( data_isint(data) )
  3197. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3198. else
  3199. {
  3200. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3201. script_reportdata(data);
  3202. script_reportsrc(st);
  3203. script_removetop(st, -3, 0);
  3204. script_pushnil(st);
  3205. return;
  3206. }
  3207. if( flag )
  3208. script_pushcopytop(st, -2);
  3209. else
  3210. script_pushcopytop(st, -1);
  3211. script_removetop(st, -4, -1);
  3212. }
  3213. /// Binary string operators
  3214. /// s1 EQ s2 -> i
  3215. /// s1 NE s2 -> i
  3216. /// s1 GT s2 -> i
  3217. /// s1 GE s2 -> i
  3218. /// s1 LT s2 -> i
  3219. /// s1 LE s2 -> i
  3220. /// s1 ADD s2 -> s
  3221. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3222. {
  3223. int a = 0;
  3224. switch(op){
  3225. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3226. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3227. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3228. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3229. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3230. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3231. case C_ADD:
  3232. {
  3233. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3234. strcpy(buf, s1);
  3235. strcat(buf, s2);
  3236. script_pushstr(st, buf);
  3237. return;
  3238. }
  3239. default:
  3240. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3241. script_reportsrc(st);
  3242. script_pushnil(st);
  3243. st->state = END;
  3244. return;
  3245. }
  3246. script_pushint(st,a);
  3247. }
  3248. /// Binary number operators
  3249. /// i OP i -> i
  3250. void op_2num(struct script_state* st, int op, int i1, int i2)
  3251. {
  3252. int ret;
  3253. double ret_double;
  3254. switch( op )
  3255. {
  3256. case C_AND: ret = i1 & i2; break;
  3257. case C_OR: ret = i1 | i2; break;
  3258. case C_XOR: ret = i1 ^ i2; break;
  3259. case C_LAND: ret = (i1 && i2); break;
  3260. case C_LOR: ret = (i1 || i2); break;
  3261. case C_EQ: ret = (i1 == i2); break;
  3262. case C_NE: ret = (i1 != i2); break;
  3263. case C_GT: ret = (i1 > i2); break;
  3264. case C_GE: ret = (i1 >= i2); break;
  3265. case C_LT: ret = (i1 < i2); break;
  3266. case C_LE: ret = (i1 <= i2); break;
  3267. case C_R_SHIFT: ret = i1>>i2; break;
  3268. case C_L_SHIFT: ret = i1<<i2; break;
  3269. case C_DIV:
  3270. case C_MOD:
  3271. if( i2 == 0 )
  3272. {
  3273. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3274. script_reportsrc(st);
  3275. script_pushnil(st);
  3276. st->state = END;
  3277. return;
  3278. }
  3279. else if( op == C_DIV )
  3280. ret = i1 / i2;
  3281. else//if( op == C_MOD )
  3282. ret = i1 % i2;
  3283. break;
  3284. default:
  3285. switch( op )
  3286. {// operators that can overflow/underflow
  3287. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3288. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3289. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3290. default:
  3291. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3292. script_reportsrc(st);
  3293. script_pushnil(st);
  3294. return;
  3295. }
  3296. if( ret_double < (double)INT_MIN )
  3297. {
  3298. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3299. script_reportsrc(st);
  3300. ret = INT_MIN;
  3301. }
  3302. else if( ret_double > (double)INT_MAX )
  3303. {
  3304. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3305. script_reportsrc(st);
  3306. ret = INT_MAX;
  3307. }
  3308. }
  3309. script_pushint(st, ret);
  3310. }
  3311. /// Binary operators
  3312. void op_2(struct script_state *st, int op)
  3313. {
  3314. struct script_data* left, leftref;
  3315. struct script_data* right;
  3316. leftref.type = C_NOP;
  3317. left = script_getdatatop(st, -2);
  3318. right = script_getdatatop(st, -1);
  3319. if (st->op2ref) {
  3320. if (data_isreference(left)) {
  3321. leftref = *left;
  3322. }
  3323. st->op2ref = 0;
  3324. }
  3325. get_val(st, left);
  3326. get_val(st, right);
  3327. // automatic conversions
  3328. switch( op )
  3329. {
  3330. case C_ADD:
  3331. if( data_isint(left) && data_isstring(right) )
  3332. {// convert int-string to string-string
  3333. conv_str(st, left);
  3334. }
  3335. else if( data_isstring(left) && data_isint(right) )
  3336. {// convert string-int to string-string
  3337. conv_str(st, right);
  3338. }
  3339. break;
  3340. }
  3341. if( data_isstring(left) && data_isstring(right) )
  3342. {// ss => op_2str
  3343. op_2str(st, op, left->u.str, right->u.str);
  3344. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3345. if (leftref.type != C_NOP)
  3346. {
  3347. if (left->type == C_STR) // don't free C_CONSTSTR
  3348. aFree(left->u.str);
  3349. *left = leftref;
  3350. }
  3351. }
  3352. else if( data_isint(left) && data_isint(right) )
  3353. {// ii => op_2num
  3354. int i1 = (int)left->u.num;
  3355. int i2 = (int)right->u.num;
  3356. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3357. op_2num(st, op, i1, i2);
  3358. if (leftref.type != C_NOP)
  3359. *left = leftref;
  3360. }
  3361. else
  3362. {// invalid argument
  3363. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3364. script_reportdata(left);
  3365. script_reportdata(right);
  3366. script_reportsrc(st);
  3367. script_removetop(st, -2, 0);
  3368. script_pushnil(st);
  3369. st->state = END;
  3370. }
  3371. }
  3372. /// Unary operators
  3373. /// NEG i -> i
  3374. /// NOT i -> i
  3375. /// LNOT i -> i
  3376. void op_1(struct script_state* st, int op)
  3377. {
  3378. struct script_data* data;
  3379. int i1;
  3380. data = script_getdatatop(st, -1);
  3381. get_val(st, data);
  3382. if( !data_isint(data) )
  3383. {// not a number
  3384. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3385. script_reportdata(data);
  3386. script_reportsrc(st);
  3387. script_pushnil(st);
  3388. st->state = END;
  3389. return;
  3390. }
  3391. i1 = (int)data->u.num;
  3392. script_removetop(st, -1, 0);
  3393. switch( op )
  3394. {
  3395. case C_NEG: i1 = -i1; break;
  3396. case C_NOT: i1 = ~i1; break;
  3397. case C_LNOT: i1 = !i1; break;
  3398. default:
  3399. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3400. script_reportsrc(st);
  3401. script_pushnil(st);
  3402. st->state = END;
  3403. return;
  3404. }
  3405. script_pushint(st, i1);
  3406. }
  3407. /// Checks the type of all arguments passed to a built-in function.
  3408. ///
  3409. /// @param st Script state whose stack arguments should be inspected.
  3410. /// @param func Built-in function for which the arguments are intended.
  3411. static void script_check_buildin_argtype(struct script_state* st, int func)
  3412. {
  3413. int idx, invalid = 0;
  3414. for( idx = 2; script_hasdata(st, idx); idx++ )
  3415. {
  3416. struct script_data* data = script_getdata(st, idx);
  3417. script_function* sf = &buildin_func[str_data[func].val];
  3418. char type = sf->arg[idx-2];
  3419. if( type == '?' || type == '*' )
  3420. {// optional argument or unknown number of optional parameters ( no types are after this )
  3421. break;
  3422. }
  3423. else if( type == 0 )
  3424. {// more arguments than necessary ( should not happen, as it is checked before )
  3425. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3426. invalid++;
  3427. break;
  3428. }
  3429. else
  3430. {
  3431. const char* name = NULL;
  3432. if( data_isreference(data) )
  3433. {// get name for variables to determine the type they refer to
  3434. name = reference_getname(data);
  3435. }
  3436. switch( type )
  3437. {
  3438. case 'v':
  3439. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3440. {// variant
  3441. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3442. script_reportdata(data);
  3443. invalid++;
  3444. }
  3445. break;
  3446. case 's':
  3447. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3448. {// string
  3449. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3450. script_reportdata(data);
  3451. invalid++;
  3452. }
  3453. break;
  3454. case 'i':
  3455. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3456. {// int ( params and constants are always int )
  3457. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3458. script_reportdata(data);
  3459. invalid++;
  3460. }
  3461. break;
  3462. case 'r':
  3463. if( !data_isreference(data) )
  3464. {// variables
  3465. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3466. script_reportdata(data);
  3467. invalid++;
  3468. }
  3469. break;
  3470. case 'l':
  3471. if( !data_islabel(data) && !data_isfunclabel(data) )
  3472. {// label
  3473. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3474. script_reportdata(data);
  3475. invalid++;
  3476. }
  3477. break;
  3478. }
  3479. }
  3480. }
  3481. if(invalid)
  3482. {
  3483. ShowDebug("Function: %s\n", get_str(func));
  3484. script_reportsrc(st);
  3485. }
  3486. }
  3487. /// Executes a buildin command.
  3488. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3489. int run_func(struct script_state *st)
  3490. {
  3491. struct script_data* data;
  3492. int i,start_sp,end_sp,func;
  3493. end_sp = st->stack->sp;// position after the last argument
  3494. for( i = end_sp-1; i > 0 ; --i )
  3495. if( st->stack->stack_data[i].type == C_ARG )
  3496. break;
  3497. if( i == 0 ) {
  3498. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3499. st->state = END;
  3500. script_reportsrc(st);
  3501. return 1;
  3502. }
  3503. start_sp = i-1;// C_NAME of the command
  3504. st->start = start_sp;
  3505. st->end = end_sp;
  3506. data = &st->stack->stack_data[st->start];
  3507. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3508. func = (int)data->u.num;
  3509. st->funcname = reference_getname(data);
  3510. } else {
  3511. ShowError("script:run_func: not a buildin command.\n");
  3512. script_reportdata(data);
  3513. script_reportsrc(st);
  3514. st->state = END;
  3515. return 1;
  3516. }
  3517. if( script_config.warn_func_mismatch_argtypes ) {
  3518. script_check_buildin_argtype(st, func);
  3519. }
  3520. if(str_data[func].func) {
  3521. if (str_data[func].func(st)) //Report error
  3522. script_reportsrc(st);
  3523. } else {
  3524. 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));
  3525. script_reportsrc(st);
  3526. st->state = END;
  3527. }
  3528. // Stack's datum are used when re-running functions [Eoe]
  3529. if( st->state == RERUNLINE )
  3530. return 0;
  3531. pop_stack(st, st->start, st->end);
  3532. if( st->state == RETFUNC ) {// return from a user-defined function
  3533. struct script_retinfo* ri;
  3534. int olddefsp = st->stack->defsp;
  3535. int nargs;
  3536. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3537. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3538. {
  3539. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3540. script_reportsrc(st);
  3541. st->state = END;
  3542. return 1;
  3543. }
  3544. script_free_vars(st->stack->scope.vars);
  3545. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3546. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3547. nargs = ri->nargs;
  3548. st->pos = ri->pos;
  3549. st->script = ri->script;
  3550. st->stack->scope.vars = ri->scope.vars;
  3551. st->stack->scope.arrays = ri->scope.arrays;
  3552. st->stack->defsp = ri->defsp;
  3553. memset(ri, 0, sizeof(struct script_retinfo));
  3554. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3555. st->state = GOTO;
  3556. }
  3557. return 0;
  3558. }
  3559. /*==========================================
  3560. * script execution
  3561. *------------------------------------------*/
  3562. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3563. {
  3564. struct script_state *st;
  3565. if( rootscript == NULL || pos < 0 )
  3566. return;
  3567. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3568. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3569. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3570. st = script_alloc_state(rootscript, pos, rid, oid);
  3571. run_script_main(st);
  3572. }
  3573. void script_stop_instances(struct script_code *code) {
  3574. DBIterator *iter;
  3575. struct script_state* st;
  3576. if( !active_scripts )
  3577. return; // Don't even bother.
  3578. iter = db_iterator(st_db);
  3579. for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) {
  3580. if( st->script == code )
  3581. script_free_state(st);
  3582. }
  3583. dbi_destroy(iter);
  3584. }
  3585. /*==========================================
  3586. * Timer function for sleep
  3587. *------------------------------------------*/
  3588. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3589. {
  3590. struct script_state *st = (struct script_state *)data;
  3591. TBL_PC *sd = map_id2sd(st->rid);
  3592. if ((sd && sd->status.char_id != id) || (st->rid && !sd)) { // Character mismatch. Cancel execution.
  3593. st->rid = 0;
  3594. st->state = END;
  3595. }
  3596. st->sleep.timer = INVALID_TIMER;
  3597. if(st->state != RERUNLINE)
  3598. st->sleep.tick = 0;
  3599. run_script_main(st);
  3600. return 0;
  3601. }
  3602. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3603. ///
  3604. /// @param st Script state to detach.
  3605. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3606. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3607. {
  3608. struct map_session_data* sd;
  3609. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3610. sd->st = st->bk_st;
  3611. sd->npc_id = st->bk_npcid;
  3612. sd->state.disable_atcommand_on_npc = 0;
  3613. if(st->bk_st) {
  3614. //Remove tag for removal.
  3615. st->bk_st = NULL;
  3616. st->bk_npcid = 0;
  3617. } else if(dequeue_event) {
  3618. #ifdef SECURE_NPCTIMEOUT
  3619. /**
  3620. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3621. **/
  3622. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3623. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3624. sd->npc_idle_timer = INVALID_TIMER;
  3625. }
  3626. #endif
  3627. npc_event_dequeue(sd);
  3628. }
  3629. }
  3630. else if(st->bk_st)
  3631. {// rid was set to 0, before detaching the script state
  3632. 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);
  3633. script_reportsrc(st->bk_st);
  3634. script_free_state(st->bk_st);
  3635. st->bk_st = NULL;
  3636. }
  3637. }
  3638. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3639. ///
  3640. /// @param st Script state to attach.
  3641. static void script_attach_state(struct script_state* st)
  3642. {
  3643. struct map_session_data* sd;
  3644. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3645. {
  3646. if(st!=sd->st)
  3647. {
  3648. if(st->bk_st)
  3649. {// there is already a backup
  3650. 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);
  3651. }
  3652. st->bk_st = sd->st;
  3653. st->bk_npcid = sd->npc_id;
  3654. }
  3655. sd->st = st;
  3656. sd->npc_id = st->oid;
  3657. sd->npc_item_flag = st->npc_item_flag; // load default.
  3658. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3659. #ifdef SECURE_NPCTIMEOUT
  3660. if( sd->npc_idle_timer == INVALID_TIMER )
  3661. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3662. sd->npc_idle_tick = gettick();
  3663. #endif
  3664. }
  3665. }
  3666. /*==========================================
  3667. * The main part of the script execution
  3668. *------------------------------------------*/
  3669. void run_script_main(struct script_state *st)
  3670. {
  3671. int cmdcount = script_config.check_cmdcount;
  3672. int gotocount = script_config.check_gotocount;
  3673. TBL_PC *sd;
  3674. struct script_stack *stack = st->stack;
  3675. script_attach_state(st);
  3676. if(st->state == RERUNLINE) {
  3677. run_func(st);
  3678. if(st->state == GOTO)
  3679. st->state = RUN;
  3680. } else if(st->state != END)
  3681. st->state = RUN;
  3682. while(st->state == RUN) {
  3683. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3684. switch(c){
  3685. case C_EOL:
  3686. if( stack->defsp > stack->sp )
  3687. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3688. else
  3689. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3690. break;
  3691. case C_INT:
  3692. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3693. break;
  3694. case C_POS:
  3695. case C_NAME:
  3696. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3697. st->pos+=3;
  3698. break;
  3699. case C_ARG:
  3700. push_val(stack,c,0);
  3701. break;
  3702. case C_STR:
  3703. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3704. while(st->script->script_buf[st->pos++]);
  3705. break;
  3706. case C_FUNC:
  3707. run_func(st);
  3708. if(st->state==GOTO){
  3709. st->state = RUN;
  3710. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3711. ShowError("run_script: infinity loop !\n");
  3712. script_reportsrc(st);
  3713. st->state=END;
  3714. }
  3715. }
  3716. break;
  3717. case C_REF:
  3718. st->op2ref = 1;
  3719. break;
  3720. case C_NEG:
  3721. case C_NOT:
  3722. case C_LNOT:
  3723. op_1(st ,c);
  3724. break;
  3725. case C_ADD:
  3726. case C_SUB:
  3727. case C_MUL:
  3728. case C_DIV:
  3729. case C_MOD:
  3730. case C_EQ:
  3731. case C_NE:
  3732. case C_GT:
  3733. case C_GE:
  3734. case C_LT:
  3735. case C_LE:
  3736. case C_AND:
  3737. case C_OR:
  3738. case C_XOR:
  3739. case C_LAND:
  3740. case C_LOR:
  3741. case C_R_SHIFT:
  3742. case C_L_SHIFT:
  3743. op_2(st, c);
  3744. break;
  3745. case C_OP3:
  3746. op_3(st, c);
  3747. break;
  3748. case C_NOP:
  3749. st->state=END;
  3750. break;
  3751. default:
  3752. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3753. st->state=END;
  3754. break;
  3755. }
  3756. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3757. ShowError("run_script: infinity loop !\n");
  3758. script_reportsrc(st);
  3759. st->state=END;
  3760. }
  3761. }
  3762. if(st->sleep.tick > 0) {
  3763. //Restore previous script
  3764. script_detach_state(st, false);
  3765. //Delay execution
  3766. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3767. st->sleep.charid = sd?sd->status.char_id:0;
  3768. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3769. run_script_timer, st->sleep.charid, (intptr_t)st);
  3770. } else if(st->state != END && st->rid) {
  3771. //Resume later (st is already attached to player).
  3772. if(st->bk_st) {
  3773. ShowWarning("Unable to restore stack! Double continuation!\n");
  3774. //Report BOTH scripts to see if that can help somehow.
  3775. ShowDebug("Previous script (lost):\n");
  3776. script_reportsrc(st->bk_st);
  3777. ShowDebug("Current script:\n");
  3778. script_reportsrc(st);
  3779. script_free_state(st->bk_st);
  3780. st->bk_st = NULL;
  3781. }
  3782. } else {
  3783. //Dispose of script.
  3784. if ((sd = map_id2sd(st->rid))!=NULL)
  3785. { //Restore previous stack and save char.
  3786. if(sd->state.using_fake_npc){
  3787. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3788. sd->state.using_fake_npc = 0;
  3789. }
  3790. //Restore previous script if any.
  3791. script_detach_state(st, true);
  3792. if (sd->vars_dirty)
  3793. intif_saveregistry(sd);
  3794. }
  3795. script_free_state(st);
  3796. }
  3797. }
  3798. int script_config_read(char *cfgName)
  3799. {
  3800. int i;
  3801. char line[1024],w1[1024],w2[1024];
  3802. FILE *fp;
  3803. fp=fopen(cfgName,"r");
  3804. if(fp==NULL){
  3805. ShowError("File not found: %s\n", cfgName);
  3806. return 1;
  3807. }
  3808. while(fgets(line, sizeof(line), fp))
  3809. {
  3810. if(line[0] == '/' && line[1] == '/')
  3811. continue;
  3812. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3813. if(i!=2)
  3814. continue;
  3815. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3816. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3817. }
  3818. else if(strcmpi(w1,"check_cmdcount")==0) {
  3819. script_config.check_cmdcount = config_switch(w2);
  3820. }
  3821. else if(strcmpi(w1,"check_gotocount")==0) {
  3822. script_config.check_gotocount = config_switch(w2);
  3823. }
  3824. else if(strcmpi(w1,"input_min_value")==0) {
  3825. script_config.input_min_value = config_switch(w2);
  3826. }
  3827. else if(strcmpi(w1,"input_max_value")==0) {
  3828. script_config.input_max_value = config_switch(w2);
  3829. }
  3830. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3831. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3832. }
  3833. else if(strcmpi(w1,"import")==0){
  3834. script_config_read(w2);
  3835. }
  3836. else {
  3837. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3838. }
  3839. }
  3840. fclose(fp);
  3841. return 0;
  3842. }
  3843. /**
  3844. * @see DBApply
  3845. */
  3846. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3847. {
  3848. struct script_code *code = db_data2ptr(data);
  3849. if (code)
  3850. script_free_code(code);
  3851. return 0;
  3852. }
  3853. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3854. {
  3855. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3856. if( script )
  3857. {
  3858. int j;
  3859. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == pos );
  3860. if( j < EQI_MAX ) {
  3861. //Single item autobonus
  3862. current_equip_item_index = sd->equip_index[j];
  3863. current_equip_combo_pos = 0;
  3864. } else {
  3865. //Combo autobonus
  3866. current_equip_item_index = -1;
  3867. current_equip_combo_pos = pos;
  3868. }
  3869. run_script(script,0,sd->bl.id,0);
  3870. }
  3871. }
  3872. void script_add_autobonus(const char *autobonus)
  3873. {
  3874. if( strdb_get(autobonus_db, autobonus) == NULL )
  3875. {
  3876. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3877. if( script )
  3878. strdb_put(autobonus_db, autobonus, script);
  3879. }
  3880. }
  3881. /// resets a temporary character array variable to given value
  3882. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3883. {
  3884. struct script_array *sa = NULL;
  3885. struct reg_db *src = NULL;
  3886. unsigned int i, *list = NULL, size = 0;
  3887. int key;
  3888. key = add_str(varname);
  3889. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  3890. return;
  3891. if( value )
  3892. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  3893. if( !(sa = idb_get(src->arrays, key)) ) /* non-existent array, nothing to empty */
  3894. return;
  3895. size = sa->size;
  3896. list = script_array_cpy_list(sa);
  3897. for(i = 0; i < size; i++) {
  3898. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  3899. }
  3900. }
  3901. /// sets a temporary character array variable element idx to given value
  3902. /// @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.
  3903. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  3904. {
  3905. int key;
  3906. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  3907. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  3908. return;
  3909. }
  3910. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3911. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  3912. if( refcache ) {
  3913. // save to avoid repeated script->add_str calls
  3914. refcache[0] = key;
  3915. }
  3916. }
  3917. /**
  3918. * Clears persistent variables from memory
  3919. **/
  3920. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  3921. {
  3922. struct script_reg_state *src;
  3923. if( data->type != DB_DATA_PTR ) // got no need for those!
  3924. return 0;
  3925. src = db_data2ptr(data);
  3926. if( src->type ) {
  3927. struct script_reg_str *p = (struct script_reg_str *)src;
  3928. if( p->value )
  3929. aFree(p->value);
  3930. ers_free(str_reg_ers,p);
  3931. } else {
  3932. ers_free(num_reg_ers,(struct script_reg_num*)src);
  3933. }
  3934. return 0;
  3935. }
  3936. /**
  3937. * Clears a single persistent variable
  3938. **/
  3939. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  3940. {
  3941. i64db_remove(sd->regs.vars, reg);
  3942. if( data->type ) {
  3943. struct script_reg_str *p = (struct script_reg_str*)data;
  3944. if( p->value )
  3945. aFree(p->value);
  3946. ers_free(str_reg_ers,p);
  3947. } else {
  3948. ers_free(num_reg_ers,(struct script_reg_num*)data);
  3949. }
  3950. }
  3951. unsigned int *script_array_cpy_list(struct script_array *sa)
  3952. {
  3953. if( sa->size > generic_ui_array_size )
  3954. script_generic_ui_array_expand(sa->size);
  3955. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  3956. return generic_ui_array;
  3957. }
  3958. void script_generic_ui_array_expand (unsigned int plus)
  3959. {
  3960. generic_ui_array_size += plus + 100;
  3961. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  3962. }
  3963. int buildin_query_sql_sub(struct script_state *st, Sql *handle);
  3964. #ifdef BETA_THREAD_TEST
  3965. /* used to receive items the queryThread has already processed */
  3966. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3967. int i, cursor = 0;
  3968. bool allOk = true;
  3969. EnterSpinLock(&queryThreadLock);
  3970. for( i = 0; i < queryThreadData.count; i++ ) {
  3971. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3972. if( !entry->ok ) {
  3973. allOk = false;
  3974. continue;
  3975. }
  3976. run_script_main(entry->st);
  3977. entry->st = NULL;/* empty entries */
  3978. aFree(entry);
  3979. queryThreadData.entry[i] = NULL;
  3980. }
  3981. if( allOk ) {
  3982. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3983. delete_timer(queryThreadData.timer, queryThread_timer);
  3984. queryThreadData.timer = INVALID_TIMER;
  3985. }
  3986. /* now lets clear the mess. */
  3987. for( i = 0; i < queryThreadData.count; i++ ) {
  3988. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3989. if( entry == NULL )
  3990. continue;/* entry on hold */
  3991. /* move */
  3992. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3993. cursor++;
  3994. }
  3995. queryThreadData.count = cursor;
  3996. LeaveSpinLock(&queryThreadLock);
  3997. return 0;
  3998. }
  3999. void queryThread_add(struct script_state *st, bool type) {
  4000. int idx = 0;
  4001. struct queryThreadEntry* entry = NULL;
  4002. EnterSpinLock(&queryThreadLock);
  4003. if( queryThreadData.count++ != 0 )
  4004. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  4005. idx = queryThreadData.count-1;
  4006. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  4007. entry = queryThreadData.entry[idx];
  4008. entry->st = st;
  4009. entry->ok = false;
  4010. entry->type = type;
  4011. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  4012. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  4013. }
  4014. LeaveSpinLock(&queryThreadLock);
  4015. /* unlock the queryThread */
  4016. racond_signal(queryThreadCond);
  4017. }
  4018. /* adds a new log to the queue */
  4019. void queryThread_log(char * entry, int length) {
  4020. int idx = logThreadData.count;
  4021. EnterSpinLock(&queryThreadLock);
  4022. if( logThreadData.count++ != 0 )
  4023. RECREATE(logThreadData.entry, char* , logThreadData.count);
  4024. CREATE(logThreadData.entry[idx], char, length + 1 );
  4025. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  4026. LeaveSpinLock(&queryThreadLock);
  4027. /* unlock the queryThread */
  4028. racond_signal(queryThreadCond);
  4029. }
  4030. /* queryThread_main */
  4031. static void *queryThread_main(void *x) {
  4032. Sql *queryThread_handle = Sql_Malloc();
  4033. int i;
  4034. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  4035. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4036. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  4037. Sql_ShowDebug(queryThread_handle);
  4038. Sql_Free(queryThread_handle);
  4039. exit(EXIT_FAILURE);
  4040. }
  4041. if( strlen(default_codepage) > 0 )
  4042. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  4043. Sql_ShowDebug(queryThread_handle);
  4044. if( log_config.sql_logs ) {
  4045. logmysql_handle = Sql_Malloc();
  4046. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  4047. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4048. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  4049. Sql_ShowDebug(logmysql_handle);
  4050. Sql_Free(logmysql_handle);
  4051. exit(EXIT_FAILURE);
  4052. }
  4053. if( strlen(default_codepage) > 0 )
  4054. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  4055. Sql_ShowDebug(logmysql_handle);
  4056. }
  4057. while( 1 ) {
  4058. if(queryThreadTerminate > 0)
  4059. break;
  4060. EnterSpinLock(&queryThreadLock);
  4061. /* mess with queryThreadData within the lock */
  4062. for( i = 0; i < queryThreadData.count; i++ ) {
  4063. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4064. if( entry->ok )
  4065. continue;
  4066. else if ( !entry->st || !entry->st->stack ) {
  4067. entry->ok = true;/* dispose */
  4068. continue;
  4069. }
  4070. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  4071. entry->ok = true;/* we're done with this */
  4072. }
  4073. /* also check for any logs in need to be sent */
  4074. if( log_config.sql_logs ) {
  4075. for( i = 0; i < logThreadData.count; i++ ) {
  4076. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  4077. Sql_ShowDebug(logmysql_handle);
  4078. aFree(logThreadData.entry[i]);
  4079. }
  4080. logThreadData.count = 0;
  4081. }
  4082. LeaveSpinLock(&queryThreadLock);
  4083. ramutex_lock( queryThreadMutex );
  4084. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  4085. ramutex_unlock( queryThreadMutex );
  4086. }
  4087. Sql_Free(queryThread_handle);
  4088. if( log_config.sql_logs ) {
  4089. Sql_Free(logmysql_handle);
  4090. }
  4091. return NULL;
  4092. }
  4093. #endif
  4094. /*==========================================
  4095. * Destructor
  4096. *------------------------------------------*/
  4097. void do_final_script() {
  4098. int i;
  4099. DBIterator *iter;
  4100. struct script_state *st;
  4101. #ifdef DEBUG_HASH
  4102. if (battle_config.etc_log)
  4103. {
  4104. FILE *fp = fopen("hash_dump.txt","wt");
  4105. if(fp) {
  4106. int count[SCRIPT_HASH_SIZE];
  4107. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4108. int n=0;
  4109. int min=INT_MAX,max=0,zero=0;
  4110. double mean=0.0f;
  4111. double median=0.0f;
  4112. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4113. memset(count, 0, sizeof(count));
  4114. fprintf(fp,"num : hash : data_name\n");
  4115. fprintf(fp,"---------------------------------------------------------------\n");
  4116. for(i=LABEL_START; i<str_num; i++) {
  4117. unsigned int h = calc_hash(get_str(i));
  4118. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4119. ++count[h];
  4120. }
  4121. fprintf(fp,"--------------------\n\n");
  4122. memset(count2, 0, sizeof(count2));
  4123. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4124. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4125. if(min > count[i])
  4126. min = count[i]; // minimun count of collision
  4127. if(max < count[i])
  4128. max = count[i]; // maximun count of collision
  4129. if(count[i] == 0)
  4130. zero++;
  4131. ++count2[count[i]];
  4132. }
  4133. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4134. for( i=min; i <= max; ++i ){
  4135. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4136. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4137. }
  4138. for( i=min; i <= max; ++i ){
  4139. n += count2[i];
  4140. if( n*2 >= SCRIPT_HASH_SIZE )
  4141. {
  4142. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4143. median = (i+i+1)/2.0f;
  4144. else
  4145. median = i;
  4146. break;
  4147. }
  4148. }
  4149. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4150. fclose(fp);
  4151. }
  4152. }
  4153. #endif
  4154. mapreg_final();
  4155. db_destroy(scriptlabel_db);
  4156. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4157. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4158. ers_destroy(array_ers);
  4159. if (generic_ui_array)
  4160. aFree(generic_ui_array);
  4161. iter = db_iterator(st_db);
  4162. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4163. script_free_state(st);
  4164. dbi_destroy(iter);
  4165. if (str_data)
  4166. aFree(str_data);
  4167. if (str_buf)
  4168. aFree(str_buf);
  4169. for( i = 0; i < atcmd_binding_count; i++ ) {
  4170. aFree(atcmd_binding[i]);
  4171. }
  4172. if( atcmd_binding_count != 0 )
  4173. aFree(atcmd_binding);
  4174. ers_destroy(st_ers);
  4175. ers_destroy(stack_ers);
  4176. db_destroy(st_db);
  4177. #ifdef BETA_THREAD_TEST
  4178. /* QueryThread */
  4179. InterlockedIncrement(&queryThreadTerminate);
  4180. racond_signal(queryThreadCond);
  4181. rathread_wait(queryThread, NULL);
  4182. // Destroy cond var and mutex.
  4183. racond_destroy( queryThreadCond );
  4184. ramutex_destroy( queryThreadMutex );
  4185. /* Clear missing vars */
  4186. for( i = 0; i < queryThreadData.count; i++ ) {
  4187. aFree(queryThreadData.entry[i]);
  4188. }
  4189. aFree(queryThreadData.entry);
  4190. for( i = 0; i < logThreadData.count; i++ ) {
  4191. aFree(logThreadData.entry[i]);
  4192. }
  4193. aFree(logThreadData.entry);
  4194. #endif
  4195. }
  4196. /*==========================================
  4197. * Initialization
  4198. *------------------------------------------*/
  4199. void do_init_script(void) {
  4200. st_db = idb_alloc(DB_OPT_BASE);
  4201. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4202. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4203. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4204. st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_CACHE_OPTIONS);
  4205. stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_FLEX_CHUNK);
  4206. 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
  4207. ers_chunk_size(st_ers, 10);
  4208. ers_chunk_size(stack_ers, 10);
  4209. active_scripts = 0;
  4210. next_id = 0;
  4211. mapreg_init();
  4212. #ifdef BETA_THREAD_TEST
  4213. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4214. queryThreadData.count = 0;
  4215. CREATE(logThreadData.entry, char *, 1);
  4216. logThreadData.count = 0;
  4217. /* QueryThread Start */
  4218. InitializeSpinLock(&queryThreadLock);
  4219. queryThreadData.timer = INVALID_TIMER;
  4220. queryThreadTerminate = 0;
  4221. queryThreadMutex = ramutex_create();
  4222. queryThreadCond = racond_create();
  4223. queryThread = rathread_create(queryThread_main, NULL);
  4224. if(queryThread == NULL){
  4225. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4226. exit(EXIT_FAILURE);
  4227. }
  4228. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4229. #endif
  4230. }
  4231. void script_reload(void) {
  4232. int i;
  4233. DBIterator *iter;
  4234. struct script_state *st;
  4235. #ifdef BETA_THREAD_TEST
  4236. /* we're reloading so any queries undergoing should be...exterminated. */
  4237. EnterSpinLock(&queryThreadLock);
  4238. for( i = 0; i < queryThreadData.count; i++ ) {
  4239. aFree(queryThreadData.entry[i]);
  4240. }
  4241. queryThreadData.count = 0;
  4242. if( queryThreadData.timer != INVALID_TIMER ) {
  4243. delete_timer(queryThreadData.timer, queryThread_timer);
  4244. queryThreadData.timer = INVALID_TIMER;
  4245. }
  4246. LeaveSpinLock(&queryThreadLock);
  4247. #endif
  4248. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4249. db_clear(scriptlabel_db);
  4250. // @commands (script based)
  4251. // Clear bindings
  4252. for( i = 0; i < atcmd_binding_count; i++ ) {
  4253. aFree(atcmd_binding[i]);
  4254. }
  4255. if( atcmd_binding_count != 0 )
  4256. aFree(atcmd_binding);
  4257. atcmd_binding_count = 0;
  4258. iter = db_iterator(st_db);
  4259. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4260. script_free_state(st);
  4261. dbi_destroy(iter);
  4262. db_clear(st_db);
  4263. mapreg_reload();
  4264. }
  4265. //-----------------------------------------------------------------------------
  4266. // buildin functions
  4267. //
  4268. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4269. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4270. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4271. /////////////////////////////////////////////////////////////////////
  4272. // NPC interaction
  4273. //
  4274. /// Appends a message to the npc dialog.
  4275. /// If a dialog doesn't exist yet, one is created.
  4276. ///
  4277. /// mes "<message>";
  4278. BUILDIN_FUNC(mes)
  4279. {
  4280. TBL_PC* sd = script_rid2sd(st);
  4281. if( sd == NULL )
  4282. return 0;
  4283. if( !script_hasdata(st, 3) )
  4284. {// only a single line detected in the script
  4285. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4286. }
  4287. else
  4288. {// parse multiple lines as they exist
  4289. int i;
  4290. for( i = 2; script_hasdata(st, i); i++ )
  4291. {
  4292. // send the message to the client
  4293. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4294. }
  4295. }
  4296. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4297. return SCRIPT_CMD_SUCCESS;
  4298. }
  4299. /// Displays the button 'next' in the npc dialog.
  4300. /// The dialog text is cleared and the script continues when the button is pressed.
  4301. ///
  4302. /// next;
  4303. BUILDIN_FUNC(next)
  4304. {
  4305. TBL_PC* sd;
  4306. sd = script_rid2sd(st);
  4307. if( sd == NULL )
  4308. return 0;
  4309. #ifdef SECURE_NPCTIMEOUT
  4310. sd->npc_idle_type = NPCT_WAIT;
  4311. #endif
  4312. st->state = STOP;
  4313. clif_scriptnext(sd, st->oid);
  4314. return SCRIPT_CMD_SUCCESS;
  4315. }
  4316. /// Ends the script and displays the button 'close' on the npc dialog.
  4317. /// The dialog is closed when the button is pressed.
  4318. ///
  4319. /// close;
  4320. BUILDIN_FUNC(close)
  4321. {
  4322. TBL_PC* sd;
  4323. sd = script_rid2sd(st);
  4324. if( sd == NULL )
  4325. return 0;
  4326. if( !st->mes_active ) {
  4327. TBL_NPC* nd = map_id2nd(st->oid);
  4328. st->state = END; // Keep backwards compatibility.
  4329. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4330. } else {
  4331. st->state = CLOSE;
  4332. st->mes_active = 0;
  4333. }
  4334. clif_scriptclose(sd, st->oid);
  4335. return SCRIPT_CMD_SUCCESS;
  4336. }
  4337. /// Displays the button 'close' on the npc dialog.
  4338. /// The dialog is closed and the script continues when the button is pressed.
  4339. ///
  4340. /// close2;
  4341. BUILDIN_FUNC(close2)
  4342. {
  4343. TBL_PC* sd;
  4344. sd = script_rid2sd(st);
  4345. if( sd == NULL )
  4346. return 0;
  4347. st->state = STOP;
  4348. if( st->mes_active )
  4349. st->mes_active = 0;
  4350. clif_scriptclose(sd, st->oid);
  4351. return SCRIPT_CMD_SUCCESS;
  4352. }
  4353. /// Counts the number of valid and total number of options in 'str'
  4354. /// If max_count > 0 the counting stops when that valid option is reached
  4355. /// total is incremented for each option (NULL is supported)
  4356. static int menu_countoptions(const char* str, int max_count, int* total)
  4357. {
  4358. int count = 0;
  4359. int bogus_total;
  4360. if( total == NULL )
  4361. total = &bogus_total;
  4362. ++(*total);
  4363. // initial empty options
  4364. while( *str == ':' )
  4365. {
  4366. ++str;
  4367. ++(*total);
  4368. }
  4369. // count menu options
  4370. while( *str != '\0' )
  4371. {
  4372. ++count;
  4373. --max_count;
  4374. if( max_count == 0 )
  4375. break;
  4376. while( *str != ':' && *str != '\0' )
  4377. ++str;
  4378. while( *str == ':' )
  4379. {
  4380. ++str;
  4381. ++(*total);
  4382. }
  4383. }
  4384. return count;
  4385. }
  4386. /// Displays a menu with options and goes to the target label.
  4387. /// The script is stopped if cancel is pressed.
  4388. /// Options with no text are not displayed in the client.
  4389. ///
  4390. /// Options can be grouped together, separated by the character ':' in the text:
  4391. /// ex: menu "A:B:C",L_target;
  4392. /// All these options go to the specified target label.
  4393. ///
  4394. /// The index of the selected option is put in the variable @menu.
  4395. /// Indexes start with 1 and are consistent with grouped and empty options.
  4396. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4397. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4398. ///
  4399. /// NOTE: the client closes the npc dialog when cancel is pressed
  4400. ///
  4401. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4402. BUILDIN_FUNC(menu)
  4403. {
  4404. int i;
  4405. const char* text;
  4406. TBL_PC* sd;
  4407. sd = script_rid2sd(st);
  4408. if( sd == NULL )
  4409. return 0;
  4410. #ifdef SECURE_NPCTIMEOUT
  4411. sd->npc_idle_type = NPCT_MENU;
  4412. #endif
  4413. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4414. if( sd->state.menu_or_input == 0 )
  4415. {
  4416. struct StringBuf buf;
  4417. if( script_lastdata(st) % 2 == 0 )
  4418. {// argument count is not even (1st argument is at index 2)
  4419. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4420. st->state = END;
  4421. return SCRIPT_CMD_FAILURE;
  4422. }
  4423. StringBuf_Init(&buf);
  4424. sd->npc_menu = 0;
  4425. for( i = 2; i < script_lastdata(st); i += 2 )
  4426. {
  4427. struct script_data* data;
  4428. // menu options
  4429. text = script_getstr(st, i);
  4430. // target label
  4431. data = script_getdata(st, i+1);
  4432. if( !data_islabel(data) )
  4433. {// not a label
  4434. StringBuf_Destroy(&buf);
  4435. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4436. script_reportdata(data);
  4437. st->state = END;
  4438. return SCRIPT_CMD_FAILURE;
  4439. }
  4440. // append option(s)
  4441. if( text[0] == '\0' )
  4442. continue;// empty string, ignore
  4443. if( sd->npc_menu > 0 )
  4444. StringBuf_AppendStr(&buf, ":");
  4445. StringBuf_AppendStr(&buf, text);
  4446. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4447. }
  4448. st->state = RERUNLINE;
  4449. sd->state.menu_or_input = 1;
  4450. /**
  4451. * menus beyond this length crash the client (see bugreport:6402)
  4452. **/
  4453. if( StringBuf_Length(&buf) >= 2047 ) {
  4454. struct npc_data * nd = map_id2nd(st->oid);
  4455. char* menu;
  4456. CREATE(menu, char, 2048);
  4457. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4458. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4459. clif_scriptmenu(sd, st->oid, menu);
  4460. aFree(menu);
  4461. } else
  4462. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4463. StringBuf_Destroy(&buf);
  4464. if( sd->npc_menu >= 0xff )
  4465. {// 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
  4466. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4467. script_reportsrc(st);
  4468. }
  4469. }
  4470. else if( sd->npc_menu == 0xff )
  4471. {// Cancel was pressed
  4472. sd->state.menu_or_input = 0;
  4473. st->state = END;
  4474. }
  4475. else
  4476. {// goto target label
  4477. int menu = 0;
  4478. sd->state.menu_or_input = 0;
  4479. if( sd->npc_menu <= 0 )
  4480. {
  4481. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4482. st->state = END;
  4483. return SCRIPT_CMD_FAILURE;
  4484. }
  4485. // get target label
  4486. for( i = 2; i < script_lastdata(st); i += 2 )
  4487. {
  4488. text = script_getstr(st, i);
  4489. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4490. if( sd->npc_menu <= 0 )
  4491. break;// entry found
  4492. }
  4493. if( sd->npc_menu > 0 )
  4494. {// Invalid selection
  4495. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4496. st->state = END;
  4497. return SCRIPT_CMD_FAILURE;
  4498. }
  4499. if( !data_islabel(script_getdata(st, i + 1)) )
  4500. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4501. ShowError("script:menu: unexpected data in label argument\n");
  4502. script_reportdata(script_getdata(st, i + 1));
  4503. st->state = END;
  4504. return SCRIPT_CMD_FAILURE;
  4505. }
  4506. pc_setreg(sd, add_str("@menu"), menu);
  4507. st->pos = script_getnum(st, i + 1);
  4508. st->state = GOTO;
  4509. }
  4510. return SCRIPT_CMD_SUCCESS;
  4511. }
  4512. /// Displays a menu with options and returns the selected option.
  4513. /// Behaves like 'menu' without the target labels.
  4514. ///
  4515. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4516. ///
  4517. /// @see menu
  4518. BUILDIN_FUNC(select)
  4519. {
  4520. int i;
  4521. const char* text;
  4522. TBL_PC* sd;
  4523. sd = script_rid2sd(st);
  4524. if( sd == NULL )
  4525. return 0;
  4526. #ifdef SECURE_NPCTIMEOUT
  4527. sd->npc_idle_type = NPCT_MENU;
  4528. #endif
  4529. if( sd->state.menu_or_input == 0 ) {
  4530. struct StringBuf buf;
  4531. StringBuf_Init(&buf);
  4532. sd->npc_menu = 0;
  4533. for( i = 2; i <= script_lastdata(st); ++i ) {
  4534. text = script_getstr(st, i);
  4535. if( sd->npc_menu > 0 )
  4536. StringBuf_AppendStr(&buf, ":");
  4537. StringBuf_AppendStr(&buf, text);
  4538. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4539. }
  4540. st->state = RERUNLINE;
  4541. sd->state.menu_or_input = 1;
  4542. /**
  4543. * menus beyond this length crash the client (see bugreport:6402)
  4544. **/
  4545. if( StringBuf_Length(&buf) >= 2047 ) {
  4546. struct npc_data * nd = map_id2nd(st->oid);
  4547. char* menu;
  4548. CREATE(menu, char, 2048);
  4549. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4550. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4551. clif_scriptmenu(sd, st->oid, menu);
  4552. aFree(menu);
  4553. } else
  4554. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4555. StringBuf_Destroy(&buf);
  4556. if( sd->npc_menu >= 0xff ) {
  4557. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4558. script_reportsrc(st);
  4559. }
  4560. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4561. sd->state.menu_or_input = 0;
  4562. st->state = END;
  4563. } else {// return selected option
  4564. int menu = 0;
  4565. sd->state.menu_or_input = 0;
  4566. for( i = 2; i <= script_lastdata(st); ++i ) {
  4567. text = script_getstr(st, i);
  4568. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4569. if( sd->npc_menu <= 0 )
  4570. break;// entry found
  4571. }
  4572. pc_setreg(sd, add_str("@menu"), menu);
  4573. script_pushint(st, menu);
  4574. st->state = RUN;
  4575. }
  4576. return SCRIPT_CMD_SUCCESS;
  4577. }
  4578. /// Displays a menu with options and returns the selected option.
  4579. /// Behaves like 'menu' without the target labels, except when cancel is
  4580. /// pressed.
  4581. /// When cancel is pressed, the script continues and 255 is returned.
  4582. ///
  4583. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4584. ///
  4585. /// @see menu
  4586. BUILDIN_FUNC(prompt)
  4587. {
  4588. int i;
  4589. const char *text;
  4590. TBL_PC* sd;
  4591. sd = script_rid2sd(st);
  4592. if( sd == NULL )
  4593. return 0;
  4594. #ifdef SECURE_NPCTIMEOUT
  4595. sd->npc_idle_type = NPCT_MENU;
  4596. #endif
  4597. if( sd->state.menu_or_input == 0 )
  4598. {
  4599. struct StringBuf buf;
  4600. StringBuf_Init(&buf);
  4601. sd->npc_menu = 0;
  4602. for( i = 2; i <= script_lastdata(st); ++i )
  4603. {
  4604. text = script_getstr(st, i);
  4605. if( sd->npc_menu > 0 )
  4606. StringBuf_AppendStr(&buf, ":");
  4607. StringBuf_AppendStr(&buf, text);
  4608. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4609. }
  4610. st->state = RERUNLINE;
  4611. sd->state.menu_or_input = 1;
  4612. /**
  4613. * menus beyond this length crash the client (see bugreport:6402)
  4614. **/
  4615. if( StringBuf_Length(&buf) >= 2047 ) {
  4616. struct npc_data * nd = map_id2nd(st->oid);
  4617. char* menu;
  4618. CREATE(menu, char, 2048);
  4619. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4620. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4621. clif_scriptmenu(sd, st->oid, menu);
  4622. aFree(menu);
  4623. } else
  4624. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4625. StringBuf_Destroy(&buf);
  4626. if( sd->npc_menu >= 0xff )
  4627. {
  4628. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4629. script_reportsrc(st);
  4630. }
  4631. }
  4632. else if( sd->npc_menu == 0xff )
  4633. {// Cancel was pressed
  4634. sd->state.menu_or_input = 0;
  4635. pc_setreg(sd, add_str("@menu"), 0xff);
  4636. script_pushint(st, 0xff);
  4637. st->state = RUN;
  4638. }
  4639. else
  4640. {// return selected option
  4641. int menu = 0;
  4642. sd->state.menu_or_input = 0;
  4643. for( i = 2; i <= script_lastdata(st); ++i )
  4644. {
  4645. text = script_getstr(st, i);
  4646. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4647. if( sd->npc_menu <= 0 )
  4648. break;// entry found
  4649. }
  4650. pc_setreg(sd, add_str("@menu"), menu);
  4651. script_pushint(st, menu);
  4652. st->state = RUN;
  4653. }
  4654. return SCRIPT_CMD_SUCCESS;
  4655. }
  4656. /////////////////////////////////////////////////////////////////////
  4657. // ...
  4658. //
  4659. /// Jumps to the target script label.
  4660. ///
  4661. /// goto <label>;
  4662. BUILDIN_FUNC(goto)
  4663. {
  4664. if( !data_islabel(script_getdata(st,2)) )
  4665. {
  4666. ShowError("script:goto: not a label\n");
  4667. script_reportdata(script_getdata(st,2));
  4668. st->state = END;
  4669. return 1;
  4670. }
  4671. st->pos = script_getnum(st,2);
  4672. st->state = GOTO;
  4673. return SCRIPT_CMD_SUCCESS;
  4674. }
  4675. /*==========================================
  4676. * user-defined function call
  4677. *------------------------------------------*/
  4678. BUILDIN_FUNC(callfunc)
  4679. {
  4680. int i, j;
  4681. struct script_retinfo* ri;
  4682. struct script_code* scr;
  4683. const char* str = script_getstr(st,2);
  4684. struct reg_db *ref = NULL;
  4685. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4686. if(!scr) {
  4687. ShowError("script:callfunc: function not found! [%s]\n", str);
  4688. st->state = END;
  4689. return 1;
  4690. }
  4691. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4692. ref[0].vars = st->stack->scope.vars;
  4693. if (!st->stack->scope.arrays)
  4694. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4695. ref[0].arrays = st->stack->scope.arrays;
  4696. ref[1].vars = st->script->local.vars;
  4697. if (!st->script->local.arrays)
  4698. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4699. ref[1].arrays = st->script->local.arrays;
  4700. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4701. struct script_data* data = push_copy(st->stack,i);
  4702. if (data_isreference(data) && !data->ref) {
  4703. const char* name = reference_getname(data);
  4704. if (name[0] == '.')
  4705. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4706. }
  4707. }
  4708. CREATE(ri, struct script_retinfo, 1);
  4709. ri->script = st->script; // script code
  4710. ri->scope.vars = st->stack->scope.vars; // scope variables
  4711. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4712. ri->pos = st->pos; // script location
  4713. ri->nargs = j; // argument count
  4714. ri->defsp = st->stack->defsp; // default stack pointer
  4715. push_retinfo(st->stack, ri, ref);
  4716. st->pos = 0;
  4717. st->script = scr;
  4718. st->stack->defsp = st->stack->sp;
  4719. st->state = GOTO;
  4720. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4721. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4722. if (!st->script->local.vars)
  4723. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4724. return SCRIPT_CMD_SUCCESS;
  4725. }
  4726. /*==========================================
  4727. * subroutine call
  4728. *------------------------------------------*/
  4729. BUILDIN_FUNC(callsub)
  4730. {
  4731. int i,j;
  4732. struct script_retinfo* ri;
  4733. int pos = script_getnum(st,2);
  4734. struct reg_db *ref = NULL;
  4735. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4736. ShowError("script:callsub: argument is not a label\n");
  4737. script_reportdata(script_getdata(st,2));
  4738. st->state = END;
  4739. return 1;
  4740. }
  4741. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4742. ref[0].vars = st->stack->scope.vars;
  4743. if (!st->stack->scope.arrays)
  4744. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4745. ref[0].arrays = st->stack->scope.arrays;
  4746. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4747. struct script_data* data = push_copy(st->stack,i);
  4748. if (data_isreference(data) && !data->ref) {
  4749. const char* name = reference_getname(data);
  4750. if (name[0] == '.' && name[1] == '@')
  4751. data->ref = &ref[0];
  4752. }
  4753. }
  4754. CREATE(ri, struct script_retinfo, 1);
  4755. ri->script = st->script; // script code
  4756. ri->scope.vars = st->stack->scope.vars; // scope variables
  4757. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4758. ri->pos = st->pos; // script location
  4759. ri->nargs = j; // argument count
  4760. ri->defsp = st->stack->defsp; // default stack pointer
  4761. push_retinfo(st->stack, ri, ref);
  4762. st->pos = pos;
  4763. st->stack->defsp = st->stack->sp;
  4764. st->state = GOTO;
  4765. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4766. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4767. return SCRIPT_CMD_SUCCESS;
  4768. }
  4769. /// Retrieves an argument provided to callfunc/callsub.
  4770. /// If the argument doesn't exist
  4771. ///
  4772. /// getarg(<index>{,<default_value>}) -> <value>
  4773. BUILDIN_FUNC(getarg)
  4774. {
  4775. struct script_retinfo* ri;
  4776. int idx;
  4777. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4778. {
  4779. ShowError("script:getarg: no callfunc or callsub!\n");
  4780. st->state = END;
  4781. return 1;
  4782. }
  4783. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4784. idx = script_getnum(st,2);
  4785. if( idx >= 0 && idx < ri->nargs )
  4786. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4787. else if( script_hasdata(st,3) )
  4788. script_pushcopy(st, 3);
  4789. else
  4790. {
  4791. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4792. st->state = END;
  4793. return 1;
  4794. }
  4795. return SCRIPT_CMD_SUCCESS;
  4796. }
  4797. /// Returns from the current function, optionaly returning a value from the functions.
  4798. /// Don't use outside script functions.
  4799. ///
  4800. /// return;
  4801. /// return <value>;
  4802. BUILDIN_FUNC(return)
  4803. {
  4804. if( script_hasdata(st,2) )
  4805. {// return value
  4806. struct script_data* data;
  4807. script_pushcopy(st, 2);
  4808. data = script_getdatatop(st, -1);
  4809. if( data_isreference(data) ) {
  4810. const char* name = reference_getname(data);
  4811. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4812. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4813. get_val(st, data); // current scope, convert to value
  4814. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4815. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4816. } else if( name[0] == '.' && !data->ref ) { // script variable, link to current script
  4817. data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4818. data->ref->vars = st->script->local.vars;
  4819. if (!st->script->local.arrays)
  4820. st->script->local.arrays = idb_alloc(DB_OPT_BASE);
  4821. data->ref->arrays = st->script->local.arrays;
  4822. }
  4823. }
  4824. }
  4825. else
  4826. {// no return value
  4827. script_pushnil(st);
  4828. }
  4829. st->state = RETFUNC;
  4830. return SCRIPT_CMD_SUCCESS;
  4831. }
  4832. /// Returns a random number from 0 to <range>-1.
  4833. /// Or returns a random number from <min> to <max>.
  4834. /// If <min> is greater than <max>, their numbers are switched.
  4835. /// rand(<range>) -> <int>
  4836. /// rand(<min>,<max>) -> <int>
  4837. BUILDIN_FUNC(rand)
  4838. {
  4839. int range;
  4840. int min;
  4841. if( script_hasdata(st,3) )
  4842. {// min,max
  4843. int max = script_getnum(st,3);
  4844. min = script_getnum(st,2);
  4845. if( max < min )
  4846. swap(min, max);
  4847. range = max - min + 1;
  4848. }
  4849. else
  4850. {// range
  4851. min = 0;
  4852. range = script_getnum(st,2);
  4853. }
  4854. if( range <= 1 )
  4855. script_pushint(st, min);
  4856. else
  4857. script_pushint(st, rnd()%range + min);
  4858. return SCRIPT_CMD_SUCCESS;
  4859. }
  4860. /*==========================================
  4861. * Warp sd to str,x,y or Random or SavePoint/Save
  4862. *------------------------------------------*/
  4863. BUILDIN_FUNC(warp)
  4864. {
  4865. int ret;
  4866. int x,y;
  4867. const char* str;
  4868. TBL_PC* sd;
  4869. sd = script_rid2sd(st);
  4870. if( sd == NULL )
  4871. return 0;
  4872. str = script_getstr(st,2);
  4873. x = script_getnum(st,3);
  4874. y = script_getnum(st,4);
  4875. if(strcmp(str,"Random")==0)
  4876. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4877. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4878. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4879. else
  4880. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4881. if( ret ) {
  4882. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4883. return 1;
  4884. }
  4885. return SCRIPT_CMD_SUCCESS;
  4886. }
  4887. /*==========================================
  4888. * Warp a specified area
  4889. *------------------------------------------*/
  4890. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4891. {
  4892. int x2,y2,x3,y3;
  4893. unsigned int index;
  4894. index = va_arg(ap,unsigned int);
  4895. x2 = va_arg(ap,int);
  4896. y2 = va_arg(ap,int);
  4897. x3 = va_arg(ap,int);
  4898. y3 = va_arg(ap,int);
  4899. if(index == 0)
  4900. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4901. else if(x3 && y3) {
  4902. int max, tx, ty, j = 0;
  4903. // choose a suitable max number of attempts
  4904. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4905. max = 1000;
  4906. // find a suitable map cell
  4907. do {
  4908. tx = rnd()%(x3-x2+1)+x2;
  4909. ty = rnd()%(y3-y2+1)+y2;
  4910. j++;
  4911. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4912. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4913. }
  4914. else
  4915. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4916. return 0;
  4917. }
  4918. BUILDIN_FUNC(areawarp)
  4919. {
  4920. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4921. unsigned int index;
  4922. const char *str;
  4923. const char *mapname;
  4924. mapname = script_getstr(st,2);
  4925. x0 = script_getnum(st,3);
  4926. y0 = script_getnum(st,4);
  4927. x1 = script_getnum(st,5);
  4928. y1 = script_getnum(st,6);
  4929. str = script_getstr(st,7);
  4930. x2 = script_getnum(st,8);
  4931. y2 = script_getnum(st,9);
  4932. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4933. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4934. x3 = 0;
  4935. y3 = 0;
  4936. } else if( x3 && y3 ) {
  4937. // normalize x3/y3 coordinates
  4938. if( x3 < x2 ) swap(x3,x2);
  4939. if( y3 < y2 ) swap(y3,y2);
  4940. }
  4941. }
  4942. if( (m = map_mapname2mapid(mapname)) < 0 )
  4943. return 0;
  4944. if( strcmp(str,"Random") == 0 )
  4945. index = 0;
  4946. else if( !(index=mapindex_name2id(str)) )
  4947. return 0;
  4948. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4949. return SCRIPT_CMD_SUCCESS;
  4950. }
  4951. /*==========================================
  4952. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4953. *------------------------------------------*/
  4954. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4955. {
  4956. int hp, sp;
  4957. hp = va_arg(ap, int);
  4958. sp = va_arg(ap, int);
  4959. pc_percentheal((TBL_PC *)bl,hp,sp);
  4960. return 0;
  4961. }
  4962. BUILDIN_FUNC(areapercentheal)
  4963. {
  4964. int hp,sp,m;
  4965. const char *mapname;
  4966. int x0,y0,x1,y1;
  4967. mapname=script_getstr(st,2);
  4968. x0=script_getnum(st,3);
  4969. y0=script_getnum(st,4);
  4970. x1=script_getnum(st,5);
  4971. y1=script_getnum(st,6);
  4972. hp=script_getnum(st,7);
  4973. sp=script_getnum(st,8);
  4974. if( (m=map_mapname2mapid(mapname))< 0)
  4975. return 0;
  4976. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4977. return SCRIPT_CMD_SUCCESS;
  4978. }
  4979. /*==========================================
  4980. * warpchar [LuzZza]
  4981. * Useful for warp one player from
  4982. * another player npc-session.
  4983. * Using: warpchar "mapname",x,y,Char_ID;
  4984. *------------------------------------------*/
  4985. BUILDIN_FUNC(warpchar)
  4986. {
  4987. int x,y,a;
  4988. const char *str;
  4989. TBL_PC *sd;
  4990. str=script_getstr(st,2);
  4991. x=script_getnum(st,3);
  4992. y=script_getnum(st,4);
  4993. a=script_getnum(st,5);
  4994. sd = map_charid2sd(a);
  4995. if( sd == NULL )
  4996. return 0;
  4997. if(strcmp(str, "Random") == 0)
  4998. pc_randomwarp(sd, CLR_TELEPORT);
  4999. else
  5000. if(strcmp(str, "SavePoint") == 0)
  5001. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  5002. else
  5003. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  5004. return SCRIPT_CMD_SUCCESS;
  5005. }
  5006. /*==========================================
  5007. * Warpparty - [Fredzilla] [Paradox924X]
  5008. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  5009. * If 'from_mapname' is specified, only the party members on that map will be warped
  5010. *------------------------------------------*/
  5011. BUILDIN_FUNC(warpparty)
  5012. {
  5013. TBL_PC *sd = NULL;
  5014. TBL_PC *pl_sd;
  5015. struct party_data* p;
  5016. int type;
  5017. int mapindex;
  5018. int i;
  5019. const char* str = script_getstr(st,2);
  5020. int x = script_getnum(st,3);
  5021. int y = script_getnum(st,4);
  5022. int p_id = script_getnum(st,5);
  5023. const char* str2 = NULL;
  5024. if ( script_hasdata(st,6) )
  5025. str2 = script_getstr(st,6);
  5026. p = party_search(p_id);
  5027. if(!p)
  5028. return 0;
  5029. type = ( strcmp(str,"Random")==0 ) ? 0
  5030. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5031. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5032. : ( strcmp(str,"Leader")==0 ) ? 3
  5033. : 4;
  5034. switch (type)
  5035. {
  5036. case 3:
  5037. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5038. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5039. return 0;
  5040. pl_sd = p->data[i].sd;
  5041. mapindex = pl_sd->mapindex;
  5042. x = pl_sd->bl.x;
  5043. y = pl_sd->bl.y;
  5044. break;
  5045. case 4:
  5046. mapindex = mapindex_name2id(str);
  5047. break;
  5048. case 2:
  5049. //"SavePoint" uses save point of the currently attached player
  5050. if (( sd = script_rid2sd(st) ) == NULL )
  5051. return 0;
  5052. default:
  5053. mapindex = 0;
  5054. break;
  5055. }
  5056. for (i = 0; i < MAX_PARTY; i++)
  5057. {
  5058. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5059. continue;
  5060. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  5061. continue;
  5062. if( pc_isdead(pl_sd) )
  5063. continue;
  5064. switch( type )
  5065. {
  5066. case 0: // Random
  5067. if(!map[pl_sd->bl.m].flag.nowarp)
  5068. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5069. break;
  5070. case 1: // SavePointAll
  5071. if(!map[pl_sd->bl.m].flag.noreturn)
  5072. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5073. break;
  5074. case 2: // SavePoint
  5075. if(!map[pl_sd->bl.m].flag.noreturn)
  5076. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5077. break;
  5078. case 3: // Leader
  5079. case 4: // m,x,y
  5080. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  5081. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5082. break;
  5083. }
  5084. }
  5085. return SCRIPT_CMD_SUCCESS;
  5086. }
  5087. /*==========================================
  5088. * Warpguild - [Fredzilla]
  5089. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5090. *------------------------------------------*/
  5091. BUILDIN_FUNC(warpguild)
  5092. {
  5093. TBL_PC *sd = NULL;
  5094. TBL_PC *pl_sd;
  5095. struct guild* g;
  5096. struct s_mapiterator* iter;
  5097. int type;
  5098. const char* str = script_getstr(st,2);
  5099. int x = script_getnum(st,3);
  5100. int y = script_getnum(st,4);
  5101. int gid = script_getnum(st,5);
  5102. g = guild_search(gid);
  5103. if( g == NULL )
  5104. return 0;
  5105. type = ( strcmp(str,"Random")==0 ) ? 0
  5106. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5107. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5108. : 3;
  5109. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  5110. {// "SavePoint" uses save point of the currently attached player
  5111. return 0;
  5112. }
  5113. iter = mapit_getallusers();
  5114. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5115. {
  5116. if( pl_sd->status.guild_id != gid )
  5117. continue;
  5118. switch( type )
  5119. {
  5120. case 0: // Random
  5121. if(!map[pl_sd->bl.m].flag.nowarp)
  5122. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5123. break;
  5124. case 1: // SavePointAll
  5125. if(!map[pl_sd->bl.m].flag.noreturn)
  5126. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5127. break;
  5128. case 2: // SavePoint
  5129. if(!map[pl_sd->bl.m].flag.noreturn)
  5130. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5131. break;
  5132. case 3: // m,x,y
  5133. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  5134. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  5135. break;
  5136. }
  5137. }
  5138. mapit_free(iter);
  5139. return SCRIPT_CMD_SUCCESS;
  5140. }
  5141. /*==========================================
  5142. * Force Heal a player (hp and sp)
  5143. *------------------------------------------*/
  5144. BUILDIN_FUNC(heal)
  5145. {
  5146. TBL_PC *sd;
  5147. int hp,sp;
  5148. sd = script_rid2sd(st);
  5149. if (!sd) return 0;
  5150. hp=script_getnum(st,2);
  5151. sp=script_getnum(st,3);
  5152. status_heal(&sd->bl, hp, sp, 1);
  5153. return SCRIPT_CMD_SUCCESS;
  5154. }
  5155. /*==========================================
  5156. * Heal a player by item (get vit bonus etc)
  5157. *------------------------------------------*/
  5158. BUILDIN_FUNC(itemheal)
  5159. {
  5160. TBL_PC *sd;
  5161. int hp,sp;
  5162. hp=script_getnum(st,2);
  5163. sp=script_getnum(st,3);
  5164. if(potion_flag==1) {
  5165. potion_hp = hp;
  5166. potion_sp = sp;
  5167. return 0;
  5168. }
  5169. sd = script_rid2sd(st);
  5170. if (!sd) return 0;
  5171. pc_itemheal(sd,sd->itemid,hp,sp);
  5172. return SCRIPT_CMD_SUCCESS;
  5173. }
  5174. /*==========================================
  5175. *
  5176. *------------------------------------------*/
  5177. BUILDIN_FUNC(percentheal)
  5178. {
  5179. int hp,sp;
  5180. TBL_PC* sd;
  5181. hp=script_getnum(st,2);
  5182. sp=script_getnum(st,3);
  5183. if(potion_flag==1) {
  5184. potion_per_hp = hp;
  5185. potion_per_sp = sp;
  5186. return 0;
  5187. }
  5188. sd = script_rid2sd(st);
  5189. if( sd == NULL )
  5190. return 0;
  5191. #ifdef RENEWAL
  5192. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5193. sp = 0;
  5194. #endif
  5195. pc_percentheal(sd,hp,sp);
  5196. return SCRIPT_CMD_SUCCESS;
  5197. }
  5198. /*==========================================
  5199. *
  5200. *------------------------------------------*/
  5201. BUILDIN_FUNC(jobchange)
  5202. {
  5203. int job, upper=-1;
  5204. job=script_getnum(st,2);
  5205. if( script_hasdata(st,3) )
  5206. upper=script_getnum(st,3);
  5207. if (pcdb_checkid(job))
  5208. {
  5209. TBL_PC* sd;
  5210. sd = script_rid2sd(st);
  5211. if( sd == NULL )
  5212. return 0;
  5213. pc_jobchange(sd, job, upper);
  5214. }
  5215. return SCRIPT_CMD_SUCCESS;
  5216. }
  5217. /*==========================================
  5218. *
  5219. *------------------------------------------*/
  5220. BUILDIN_FUNC(jobname)
  5221. {
  5222. int class_=script_getnum(st,2);
  5223. script_pushconststr(st, (char*)job_name(class_));
  5224. return SCRIPT_CMD_SUCCESS;
  5225. }
  5226. /// Get input from the player.
  5227. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5228. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5229. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5230. /// shorter than 'min' and 0 otherwise.
  5231. ///
  5232. /// input(<var>{,<min>{,<max>}}) -> <int>
  5233. BUILDIN_FUNC(input)
  5234. {
  5235. TBL_PC* sd;
  5236. struct script_data* data;
  5237. int64 uid;
  5238. const char* name;
  5239. int min;
  5240. int max;
  5241. sd = script_rid2sd(st);
  5242. if( sd == NULL )
  5243. return 0;
  5244. data = script_getdata(st,2);
  5245. if( !data_isreference(data) ){
  5246. ShowError("script:input: not a variable\n");
  5247. script_reportdata(data);
  5248. st->state = END;
  5249. return 1;
  5250. }
  5251. uid = reference_getuid(data);
  5252. name = reference_getname(data);
  5253. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5254. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5255. #ifdef SECURE_NPCTIMEOUT
  5256. sd->npc_idle_type = NPCT_WAIT;
  5257. #endif
  5258. if( !sd->state.menu_or_input )
  5259. { // first invocation, display npc input box
  5260. sd->state.menu_or_input = 1;
  5261. st->state = RERUNLINE;
  5262. if( is_string_variable(name) )
  5263. clif_scriptinputstr(sd,st->oid);
  5264. else
  5265. clif_scriptinput(sd,st->oid);
  5266. }
  5267. else
  5268. { // take received text/value and store it in the designated variable
  5269. sd->state.menu_or_input = 0;
  5270. if( is_string_variable(name) )
  5271. {
  5272. int len = (int)strlen(sd->npc_str);
  5273. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5274. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5275. }
  5276. else
  5277. {
  5278. int amount = sd->npc_amount;
  5279. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5280. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5281. }
  5282. st->state = RUN;
  5283. }
  5284. return SCRIPT_CMD_SUCCESS;
  5285. }
  5286. // declare the copyarray method here for future reference
  5287. BUILDIN_FUNC(copyarray);
  5288. /// Sets the value of a variable.
  5289. /// The value is converted to the type of the variable.
  5290. ///
  5291. /// set(<variable>,<value>{,<charid>})
  5292. BUILDIN_FUNC(set)
  5293. {
  5294. TBL_PC* sd = NULL;
  5295. struct script_data* data;
  5296. //struct script_data* datavalue;
  5297. int64 num;
  5298. const char* name;
  5299. char prefix;
  5300. data = script_getdata(st,2);
  5301. //datavalue = script_getdata(st,3);
  5302. if( !data_isreference(data) )
  5303. {
  5304. ShowError("script:set: not a variable\n");
  5305. script_reportdata(script_getdata(st,2));
  5306. st->state = END;
  5307. return 1;
  5308. }
  5309. num = reference_getuid(data);
  5310. name = reference_getname(data);
  5311. prefix = *name;
  5312. if (not_server_variable(prefix) && !script_charid2sd(4,sd)) {
  5313. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5314. return SCRIPT_CMD_FAILURE;
  5315. }
  5316. #if 0
  5317. if( data_isreference(datavalue) )
  5318. {// the value being referenced is a variable
  5319. const char* namevalue = reference_getname(datavalue);
  5320. if( !not_array_variable(*namevalue) )
  5321. {// array variable being copied into another array variable
  5322. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5323. {// player must be attached in order to copy a player variable
  5324. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5325. return 0;
  5326. }
  5327. if( is_string_variable(namevalue) != is_string_variable(name) )
  5328. {// non-matching array value types
  5329. ShowWarning("script:set: two array variables do not match in type.\n");
  5330. return 0;
  5331. }
  5332. // push the maximum number of array values to the stack
  5333. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5334. // call the copy array method directly
  5335. return buildin_copyarray(st);
  5336. }
  5337. }
  5338. #endif
  5339. if( is_string_variable(name) )
  5340. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5341. else
  5342. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5343. // return a copy of the variable reference
  5344. script_pushcopy(st,2);
  5345. return SCRIPT_CMD_SUCCESS;
  5346. }
  5347. /////////////////////////////////////////////////////////////////////
  5348. /// Array variables
  5349. ///
  5350. /// Sets values of an array, from the starting index.
  5351. /// ex: setarray arr[1],1,2,3;
  5352. ///
  5353. /// setarray <array variable>,<value1>{,<value2>...};
  5354. BUILDIN_FUNC(setarray)
  5355. {
  5356. struct script_data* data;
  5357. const char* name;
  5358. uint32 start;
  5359. uint32 end;
  5360. int32 id;
  5361. int32 i;
  5362. TBL_PC* sd = NULL;
  5363. data = script_getdata(st, 2);
  5364. if( !data_isreference(data) )
  5365. {
  5366. ShowError("script:setarray: not a variable\n");
  5367. script_reportdata(data);
  5368. st->state = END;
  5369. return 1;// not a variable
  5370. }
  5371. id = reference_getid(data);
  5372. start = reference_getindex(data);
  5373. name = reference_getname(data);
  5374. if( not_server_variable(*name) )
  5375. {
  5376. sd = script_rid2sd(st);
  5377. if( sd == NULL )
  5378. return 0;// no player attached
  5379. }
  5380. end = start + script_lastdata(st) - 2;
  5381. if( end > SCRIPT_MAX_ARRAYSIZE )
  5382. end = SCRIPT_MAX_ARRAYSIZE;
  5383. if( is_string_variable(name) )
  5384. {// string array
  5385. for( i = 3; start < end; ++start, ++i )
  5386. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5387. }
  5388. else
  5389. {// int array
  5390. for( i = 3; start < end; ++start, ++i )
  5391. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5392. }
  5393. return SCRIPT_CMD_SUCCESS;
  5394. }
  5395. /// Sets count values of an array, from the starting index.
  5396. /// ex: cleararray arr[0],0,1;
  5397. ///
  5398. /// cleararray <array variable>,<value>,<count>;
  5399. BUILDIN_FUNC(cleararray)
  5400. {
  5401. struct script_data* data;
  5402. const char* name;
  5403. uint32 start;
  5404. uint32 end;
  5405. int32 id;
  5406. void* v;
  5407. TBL_PC* sd = NULL;
  5408. data = script_getdata(st, 2);
  5409. if( !data_isreference(data) )
  5410. {
  5411. ShowError("script:cleararray: not a variable\n");
  5412. script_reportdata(data);
  5413. st->state = END;
  5414. return 1;// not a variable
  5415. }
  5416. id = reference_getid(data);
  5417. start = reference_getindex(data);
  5418. name = reference_getname(data);
  5419. if( not_server_variable(*name) )
  5420. {
  5421. sd = script_rid2sd(st);
  5422. if( sd == NULL )
  5423. return 0;// no player attached
  5424. }
  5425. if( is_string_variable(name) )
  5426. v = (void*)script_getstr(st, 3);
  5427. else
  5428. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5429. end = start + script_getnum(st, 4);
  5430. if( end > SCRIPT_MAX_ARRAYSIZE )
  5431. end = SCRIPT_MAX_ARRAYSIZE;
  5432. for( ; start < end; ++start )
  5433. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5434. return SCRIPT_CMD_SUCCESS;
  5435. }
  5436. /// Copies data from one array to another.
  5437. /// ex: copyarray arr[0],arr[2],2;
  5438. ///
  5439. /// copyarray <destination array variable>,<source array variable>,<count>;
  5440. BUILDIN_FUNC(copyarray)
  5441. {
  5442. struct script_data* data1;
  5443. struct script_data* data2;
  5444. const char* name1;
  5445. const char* name2;
  5446. int32 idx1;
  5447. int32 idx2;
  5448. int32 id1;
  5449. int32 id2;
  5450. void* v;
  5451. int32 i;
  5452. uint32 count;
  5453. TBL_PC* sd = NULL;
  5454. data1 = script_getdata(st, 2);
  5455. data2 = script_getdata(st, 3);
  5456. if( !data_isreference(data1) || !data_isreference(data2) )
  5457. {
  5458. ShowError("script:copyarray: not a variable\n");
  5459. script_reportdata(data1);
  5460. script_reportdata(data2);
  5461. st->state = END;
  5462. return 1;// not a variable
  5463. }
  5464. id1 = reference_getid(data1);
  5465. id2 = reference_getid(data2);
  5466. idx1 = reference_getindex(data1);
  5467. idx2 = reference_getindex(data2);
  5468. name1 = reference_getname(data1);
  5469. name2 = reference_getname(data2);
  5470. if( is_string_variable(name1) != is_string_variable(name2) )
  5471. {
  5472. ShowError("script:copyarray: type mismatch\n");
  5473. script_reportdata(data1);
  5474. script_reportdata(data2);
  5475. st->state = END;
  5476. return 1;// data type mismatch
  5477. }
  5478. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5479. {
  5480. sd = script_rid2sd(st);
  5481. if( sd == NULL )
  5482. return 0;// no player attached
  5483. }
  5484. count = script_getnum(st, 4);
  5485. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5486. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5487. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5488. return 0;// nothing to copy
  5489. if( id1 == id2 && idx1 > idx2 )
  5490. {// destination might be overlapping the source - copy in reverse order
  5491. for( i = count - 1; i >= 0; --i )
  5492. {
  5493. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5494. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5495. script_removetop(st, -1, 0);
  5496. }
  5497. }
  5498. else
  5499. {// normal copy
  5500. for( i = 0; i < count; ++i )
  5501. {
  5502. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5503. {
  5504. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5505. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5506. script_removetop(st, -1, 0);
  5507. }
  5508. else// out of range - assume ""/0
  5509. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5510. }
  5511. }
  5512. return SCRIPT_CMD_SUCCESS;
  5513. }
  5514. /// Returns the size of the array.
  5515. /// Assumes that everything before the starting index exists.
  5516. /// ex: getarraysize(arr[3])
  5517. ///
  5518. /// getarraysize(<array variable>) -> <int>
  5519. BUILDIN_FUNC(getarraysize)
  5520. {
  5521. struct script_data* data;
  5522. data = script_getdata(st, 2);
  5523. if( !data_isreference(data) )
  5524. {
  5525. ShowError("script:getarraysize: not a variable\n");
  5526. script_reportdata(data);
  5527. script_pushnil(st);
  5528. st->state = END;
  5529. return 1;// not a variable
  5530. }
  5531. script_pushint(st, script_array_highest_key(st, st->rid ? script_rid2sd(st) : NULL, reference_getname(data), reference_getref(data)));
  5532. return SCRIPT_CMD_SUCCESS;
  5533. }
  5534. int script_array_index_cmp(const void *a, const void *b) {
  5535. return ( *(unsigned int*)a - *(unsigned int*)b );
  5536. }
  5537. /// Deletes count or all the elements in an array, from the starting index.
  5538. /// ex: deletearray arr[4],2;
  5539. ///
  5540. /// deletearray <array variable>;
  5541. /// deletearray <array variable>,<count>;
  5542. BUILDIN_FUNC(deletearray)
  5543. {
  5544. struct script_data* data;
  5545. const char* name;
  5546. unsigned int start, end, i;
  5547. int id;
  5548. TBL_PC *sd = NULL;
  5549. struct script_array *sa = NULL;
  5550. struct reg_db *src = NULL;
  5551. void *value;
  5552. data = script_getdata(st, 2);
  5553. if( !data_isreference(data) ) {
  5554. ShowError("script:deletearray: not a variable\n");
  5555. script_reportdata(data);
  5556. st->state = END;
  5557. return 1;// not a variable
  5558. }
  5559. id = reference_getid(data);
  5560. start = reference_getindex(data);
  5561. name = reference_getname(data);
  5562. if( not_server_variable(*name) ) {
  5563. sd = script_rid2sd(st);
  5564. if( sd == NULL )
  5565. return 0;// no player attached
  5566. }
  5567. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5568. ShowError("script:deletearray: not a array\n");
  5569. script_reportdata(data);
  5570. st->state = END;
  5571. return 1;// not a variable
  5572. }
  5573. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5574. if ( !(sa = idb_get(src->arrays, id)) ) { // non-existent array, nothing to empty
  5575. return 0;// not a variable
  5576. }
  5577. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5578. if( start >= end )
  5579. return 0;// nothing to free
  5580. if( is_string_variable(name) )
  5581. value = (void *)"";
  5582. else
  5583. value = (void *)0;
  5584. if( script_hasdata(st,3) ) {
  5585. unsigned int count = script_getnum(st, 3);
  5586. if( count > end - start )
  5587. count = end - start;
  5588. if( count <= 0 )
  5589. return SCRIPT_CMD_SUCCESS;// nothing to free
  5590. if( end - start < sa->size ) {
  5591. // Better to iterate directly on the array, no speed-up from using sa
  5592. for( ; start + count < end; ++start ) {
  5593. // Compact and overwrite
  5594. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5595. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5596. script_removetop(st, -1, 0);
  5597. }
  5598. for( ; start < end; start++ ) {
  5599. // Clean up any leftovers that weren't overwritten
  5600. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5601. }
  5602. } else {
  5603. // using sa to speed up
  5604. unsigned int *list = NULL, size = 0;
  5605. list = script_array_cpy_list(sa);
  5606. size = sa->size;
  5607. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5608. ARR_FIND(0, size, i, list[i] >= start);
  5609. for( ; i < size && list[i] < start + count; i++ ) {
  5610. // Clear any entries between start and start+count, if they exist
  5611. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5612. }
  5613. for( ; i < size && list[i] < end; i++ ) {
  5614. // Move back count positions any entries between start+count to fill the gaps
  5615. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5616. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5617. script_removetop(st, -1, 0);
  5618. // Clear their originals
  5619. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5620. }
  5621. }
  5622. } else {
  5623. unsigned int *list = NULL, size = 0;
  5624. list = script_array_cpy_list(sa);
  5625. size = sa->size;
  5626. for(i = 0; i < size; i++) {
  5627. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5628. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5629. }
  5630. }
  5631. return SCRIPT_CMD_SUCCESS;
  5632. }
  5633. /// Returns a reference to the target index of the array variable.
  5634. /// Equivalent to var[index].
  5635. ///
  5636. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5637. BUILDIN_FUNC(getelementofarray)
  5638. {
  5639. struct script_data* data;
  5640. int32 id;
  5641. int64 i;
  5642. data = script_getdata(st, 2);
  5643. if( !data_isreference(data) )
  5644. {
  5645. ShowError("script:getelementofarray: not a variable\n");
  5646. script_reportdata(data);
  5647. script_pushnil(st);
  5648. st->state = END;
  5649. return 0;// not a variable
  5650. }
  5651. id = reference_getid(data);
  5652. i = script_getnum(st, 3);
  5653. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5654. ShowWarning("script:getelementofarray: index out of range (%"PRId64")\n", i);
  5655. script_reportdata(data);
  5656. script_pushnil(st);
  5657. st->state = END;
  5658. return 1;// out of range
  5659. }
  5660. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5661. return SCRIPT_CMD_SUCCESS;
  5662. }
  5663. /////////////////////////////////////////////////////////////////////
  5664. /// ...
  5665. ///
  5666. /*==========================================
  5667. *
  5668. *------------------------------------------*/
  5669. BUILDIN_FUNC(setlook)
  5670. {
  5671. int type,val;
  5672. TBL_PC* sd;
  5673. type=script_getnum(st,2);
  5674. val=script_getnum(st,3);
  5675. sd = script_rid2sd(st);
  5676. if( sd == NULL )
  5677. return 0;
  5678. pc_changelook(sd,type,val);
  5679. return SCRIPT_CMD_SUCCESS;
  5680. }
  5681. BUILDIN_FUNC(changelook)
  5682. { // As setlook but only client side
  5683. int type,val;
  5684. TBL_PC* sd;
  5685. type=script_getnum(st,2);
  5686. val=script_getnum(st,3);
  5687. sd = script_rid2sd(st);
  5688. if( sd == NULL )
  5689. return 0;
  5690. clif_changelook(&sd->bl,type,val);
  5691. return SCRIPT_CMD_SUCCESS;
  5692. }
  5693. /*==========================================
  5694. *
  5695. *------------------------------------------*/
  5696. BUILDIN_FUNC(cutin)
  5697. {
  5698. TBL_PC* sd;
  5699. sd = script_rid2sd(st);
  5700. if( sd == NULL )
  5701. return 0;
  5702. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5703. return SCRIPT_CMD_SUCCESS;
  5704. }
  5705. /*==========================================
  5706. *
  5707. *------------------------------------------*/
  5708. BUILDIN_FUNC(viewpoint)
  5709. {
  5710. int type,x,y,id,color;
  5711. TBL_PC* sd;
  5712. type=script_getnum(st,2);
  5713. x=script_getnum(st,3);
  5714. y=script_getnum(st,4);
  5715. id=script_getnum(st,5);
  5716. color=script_getnum(st,6);
  5717. sd = script_rid2sd(st);
  5718. if( sd == NULL )
  5719. return 0;
  5720. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5721. return SCRIPT_CMD_SUCCESS;
  5722. }
  5723. /// Returns number of items in inventory/cart/storage
  5724. /// countitem <nameID>{,<accountID>});
  5725. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5726. /// cartcountitem <nameID>{,<accountID>});
  5727. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5728. /// storagecountitem <nameID>{,<accountID>});
  5729. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5730. BUILDIN_FUNC(countitem)
  5731. {
  5732. int i = 0, count = 0, aid = 3;
  5733. struct item_data* id = NULL;
  5734. struct script_data* data;
  5735. char *command = (char *)script_getfuncname(st);
  5736. uint8 loc = 0;
  5737. uint16 size;
  5738. struct item *items;
  5739. TBL_PC *sd = NULL;
  5740. if( command[strlen(command)-1] == '2' ) {
  5741. i = 1;
  5742. aid = 10;
  5743. }
  5744. if( script_hasdata(st,aid) ) {
  5745. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5746. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5747. st->state = END;
  5748. return SCRIPT_CMD_FAILURE;
  5749. }
  5750. }
  5751. else {
  5752. if( !(sd = script_rid2sd(st)) )
  5753. return SCRIPT_CMD_SUCCESS;
  5754. }
  5755. if( !strncmp(command, "cart", 4) ) {
  5756. loc = 1;
  5757. size = MAX_CART;
  5758. items = sd->status.cart;
  5759. }
  5760. else if( !strncmp(command, "storage", 7) ) {
  5761. loc = 2;
  5762. size = MAX_STORAGE;
  5763. items = sd->status.storage.items;
  5764. }
  5765. //TODO: 3 - Guild Storage
  5766. else {
  5767. size = MAX_INVENTORY;
  5768. items = sd->status.inventory;
  5769. }
  5770. if( loc == 1 && !pc_iscarton(sd) ) {
  5771. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5772. script_pushint(st,-1);
  5773. return SCRIPT_CMD_SUCCESS;
  5774. }
  5775. data = script_getdata(st, 2);
  5776. get_val(st, data); // Convert into value in case of a variable
  5777. if( data_isstring(data) ) // item name
  5778. id = itemdb_searchname(conv_str(st, data));
  5779. else // item id
  5780. id = itemdb_exists(conv_num(st, data));
  5781. if( id == NULL ) {
  5782. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5783. script_pushint(st,0);
  5784. return 1;
  5785. }
  5786. if( !i ) { // For count/cart/storagecountitem function
  5787. unsigned short nameid = id->nameid;
  5788. for( i = 0; i < size; i++ )
  5789. if( &items[i] && items[i].nameid == nameid )
  5790. count += items[i].amount;
  5791. }
  5792. else { // For count/cart/storagecountitem2 function
  5793. unsigned short nameid;
  5794. int iden, ref, attr, c1, c2, c3, c4;
  5795. nameid = id->nameid;
  5796. iden = script_getnum(st,3);
  5797. ref = script_getnum(st,4);
  5798. attr = script_getnum(st,5);
  5799. c1 = script_getnum(st,6);
  5800. c2 = script_getnum(st,7);
  5801. c3 = script_getnum(st,8);
  5802. c4 = script_getnum(st,9);
  5803. for( i = 0; i < size; i++ )
  5804. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5805. items[i].amount > 0 && items[i].identify == iden &&
  5806. items[i].refine == ref && items[i].attribute == attr &&
  5807. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5808. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5809. {
  5810. count += items[i].amount;
  5811. }
  5812. }
  5813. script_pushint(st, count);
  5814. return SCRIPT_CMD_SUCCESS;
  5815. }
  5816. /*==========================================
  5817. * Check if item with this amount can fit in inventory
  5818. * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY)
  5819. * Return
  5820. * 0 : fail
  5821. * 1 : success (npc side only)
  5822. *------------------------------------------*/
  5823. BUILDIN_FUNC(checkweight)
  5824. {
  5825. int slots, amount2 = 0;
  5826. unsigned int weight = 0, i, nbargs;
  5827. struct item_data* id = NULL;
  5828. struct map_session_data* sd;
  5829. if( ( sd = script_rid2sd(st) ) == NULL )
  5830. return 0;
  5831. nbargs = script_lastdata(st)+1;
  5832. if(nbargs%2) {
  5833. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5834. script_pushint(st,0);
  5835. return 1;
  5836. }
  5837. slots = pc_inventoryblank(sd); //nb of empty slot
  5838. for (i = 2; i < nbargs; i += 2) {
  5839. unsigned short nameid, amount;
  5840. struct script_data* data = script_getdata(st,i);
  5841. get_val(st, data); // Convert into value in case of a variable
  5842. if( data_isstring(data) ) // item name
  5843. id = itemdb_searchname(conv_str(st, data));
  5844. else // item id
  5845. id = itemdb_exists(conv_num(st, data));
  5846. if( id == NULL ) {
  5847. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5848. script_pushint(st,0);
  5849. return 1;
  5850. }
  5851. nameid = id->nameid;
  5852. amount = script_getnum(st,i+1);
  5853. if( amount < 1 ) {
  5854. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5855. script_pushint(st,0);
  5856. return 1;
  5857. }
  5858. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  5859. if( weight + sd->weight > sd->max_weight )
  5860. {// too heavy
  5861. script_pushint(st,0);
  5862. return 0;
  5863. }
  5864. switch( pc_checkadditem(sd, nameid, amount) ) {
  5865. case CHKADDITEM_EXIST:
  5866. // item is already in inventory, but there is still space for the requested amount
  5867. break;
  5868. case CHKADDITEM_NEW:
  5869. if( itemdb_isstackable(nameid) ) {
  5870. // stackable
  5871. amount2++;
  5872. if( slots < amount2 ) {
  5873. script_pushint(st,0);
  5874. return 0;
  5875. }
  5876. } else {
  5877. // non-stackable
  5878. amount2 += amount;
  5879. if( slots < amount2) {
  5880. script_pushint(st,0);
  5881. return 0;
  5882. }
  5883. }
  5884. break;
  5885. case CHKADDITEM_OVERAMOUNT:
  5886. script_pushint(st,0);
  5887. return 0;
  5888. }
  5889. }
  5890. script_pushint(st,1);
  5891. return SCRIPT_CMD_SUCCESS;
  5892. }
  5893. BUILDIN_FUNC(checkweight2)
  5894. {
  5895. //variable sub checkweight
  5896. int i = 0, amount2 = 0, slots = 0, weight = 0;
  5897. short fail = 0;
  5898. //variable for array parsing
  5899. struct script_data* data_it;
  5900. struct script_data* data_nb;
  5901. const char* name_it;
  5902. const char* name_nb;
  5903. int32 id_it, id_nb;
  5904. int32 idx_it, idx_nb;
  5905. int nb_it, nb_nb; //array size
  5906. TBL_PC *sd = script_rid2sd(st);
  5907. nullpo_retr(1,sd);
  5908. data_it = script_getdata(st, 2);
  5909. data_nb = script_getdata(st, 3);
  5910. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5911. ShowError("buildin_checkweight2: parameter not a variable\n");
  5912. script_pushint(st,0);
  5913. return 1;// not a variable
  5914. }
  5915. id_it = reference_getid(data_it);
  5916. id_nb = reference_getid(data_nb);
  5917. idx_it = reference_getindex(data_it);
  5918. idx_nb = reference_getindex(data_nb);
  5919. name_it = reference_getname(data_it);
  5920. name_nb = reference_getname(data_nb);
  5921. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5922. ShowError("buildin_checkweight2: illegal type, need int\n");
  5923. script_pushint(st,0);
  5924. return 1;// not supported
  5925. }
  5926. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  5927. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  5928. if(nb_it != nb_nb) {
  5929. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5930. fail = 1;
  5931. }
  5932. slots = pc_inventoryblank(sd);
  5933. for(i=0; i<nb_it; i++) {
  5934. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5935. script_removetop(st, -1, 0);
  5936. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5937. script_removetop(st, -1, 0);
  5938. if(fail)
  5939. continue; //cpntonie to depop rest
  5940. if(itemdb_exists(nameid) == NULL ) {
  5941. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  5942. fail=1;
  5943. continue;
  5944. }
  5945. if(amount < 0 ) {
  5946. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  5947. fail = 1;
  5948. continue;
  5949. }
  5950. weight += itemdb_weight(nameid)*amount;
  5951. if( weight + sd->weight > sd->max_weight ) {
  5952. fail = 1;
  5953. continue;
  5954. }
  5955. switch( pc_checkadditem(sd, nameid, amount) ) {
  5956. case CHKADDITEM_EXIST:
  5957. // item is already in inventory, but there is still space for the requested amount
  5958. break;
  5959. case CHKADDITEM_NEW:
  5960. if( itemdb_isstackable(nameid) ) {// stackable
  5961. amount2++;
  5962. if( slots < amount2 )
  5963. fail = 1;
  5964. } else {// non-stackable
  5965. amount2 += amount;
  5966. if( slots < amount2 ) {
  5967. fail = 1;
  5968. }
  5969. }
  5970. break;
  5971. case CHKADDITEM_OVERAMOUNT:
  5972. fail = 1;
  5973. } //end switch
  5974. } //end loop DO NOT break it prematurly we need to depop all stack
  5975. fail ? script_pushint(st,0) : script_pushint(st,1);
  5976. return SCRIPT_CMD_SUCCESS;
  5977. }
  5978. /*==========================================
  5979. * getitem <item id>,<amount>{,<account ID>};
  5980. * getitem "<item name>",<amount>{,<account ID>};
  5981. *
  5982. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5983. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5984. * Type:
  5985. * 0 - No bound
  5986. * 1 - Account Bound
  5987. * 2 - Guild Bound
  5988. * 3 - Party Bound
  5989. * 4 - Character Bound
  5990. *------------------------------------------*/
  5991. BUILDIN_FUNC(getitem)
  5992. {
  5993. int amount, get_count, i;
  5994. unsigned short nameid;
  5995. struct item it;
  5996. TBL_PC *sd;
  5997. struct script_data *data;
  5998. unsigned char flag = 0;
  5999. const char* command = script_getfuncname(st);
  6000. struct item_data *id = NULL;
  6001. data = script_getdata(st,2);
  6002. get_val(st,data);
  6003. if( data_isstring(data) ) {// "<item name>"
  6004. const char *name = conv_str(st,data);
  6005. id = itemdb_searchname(name);
  6006. if( id == NULL ){
  6007. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6008. return SCRIPT_CMD_FAILURE; //No item created.
  6009. }
  6010. nameid = id->nameid;
  6011. } else if( data_isint(data) ) {// <item id>
  6012. nameid = conv_num(st,data);
  6013. if( !(id = itemdb_exists(nameid)) ){
  6014. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  6015. return SCRIPT_CMD_FAILURE; //No item created.
  6016. }
  6017. } else {
  6018. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  6019. return SCRIPT_CMD_FAILURE;
  6020. }
  6021. // <amount>
  6022. if( (amount = script_getnum(st,3)) <= 0)
  6023. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6024. memset(&it,0,sizeof(it));
  6025. it.nameid = nameid;
  6026. it.identify = 1;
  6027. it.bound = BOUND_NONE;
  6028. if( !strcmp(command,"getitembound") ) {
  6029. char bound = script_getnum(st,4);
  6030. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6031. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6032. return SCRIPT_CMD_FAILURE;
  6033. }
  6034. if( script_hasdata(st,5) )
  6035. sd = map_id2sd(script_getnum(st,5));
  6036. else
  6037. sd = script_rid2sd(st); // Attached player
  6038. it.bound = bound;
  6039. } else if( script_hasdata(st,4) )
  6040. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  6041. else
  6042. sd = script_rid2sd(st); // Attached player
  6043. if( sd == NULL ) // no target
  6044. return SCRIPT_CMD_SUCCESS;
  6045. //Check if it's stackable.
  6046. if (!itemdb_isstackable2(id))
  6047. get_count = 1;
  6048. else
  6049. get_count = amount;
  6050. for (i = 0; i < amount; i += get_count)
  6051. {
  6052. // if not pet egg
  6053. if (!pet_create_egg(sd, nameid))
  6054. {
  6055. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6056. {
  6057. clif_additem(sd, 0, 0, flag);
  6058. if( pc_candrop(sd,&it) )
  6059. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6060. }
  6061. }
  6062. }
  6063. return SCRIPT_CMD_SUCCESS;
  6064. }
  6065. /*==========================================
  6066. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6067. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6068. *
  6069. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6070. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6071. * Type:
  6072. * 0 - No bound
  6073. * 1 - Account Bound
  6074. * 2 - Guild Bound
  6075. * 3 - Party Bound
  6076. * 4 - Character Bound
  6077. *------------------------------------------*/
  6078. BUILDIN_FUNC(getitem2)
  6079. {
  6080. int amount;
  6081. unsigned short nameid;
  6082. int iden, ref, attr;
  6083. unsigned short c1, c2, c3, c4;
  6084. char bound = BOUND_NONE;
  6085. struct item_data *item_data = NULL;
  6086. struct item item_tmp;
  6087. TBL_PC *sd;
  6088. struct script_data *data;
  6089. const char* command = script_getfuncname(st);
  6090. if( !strcmp(command,"getitembound2") ) {
  6091. bound = script_getnum(st,11);
  6092. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6093. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6094. return SCRIPT_CMD_FAILURE;
  6095. }
  6096. if( script_hasdata(st,12) )
  6097. sd = map_id2sd(script_getnum(st,12));
  6098. else
  6099. sd = script_rid2sd(st); // Attached player
  6100. } else if( script_hasdata(st,11) )
  6101. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  6102. else
  6103. sd = script_rid2sd(st); // Attached player
  6104. if( sd == NULL ) // no target
  6105. return SCRIPT_CMD_SUCCESS;
  6106. data = script_getdata(st,2);
  6107. get_val(st,data);
  6108. if( data_isstring(data) ) {
  6109. const char *name = conv_str(st,data);
  6110. if( (item_data = itemdb_searchname(name)) == NULL ){
  6111. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6112. return SCRIPT_CMD_FAILURE; //No item created.
  6113. }
  6114. nameid = item_data->nameid;
  6115. } else {
  6116. nameid = conv_num(st,data);
  6117. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6118. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6119. return SCRIPT_CMD_FAILURE; //No item created.
  6120. }
  6121. }
  6122. amount = script_getnum(st,3);
  6123. iden = script_getnum(st,4);
  6124. ref = script_getnum(st,5);
  6125. attr = script_getnum(st,6);
  6126. c1 = (unsigned short)script_getnum(st,7);
  6127. c2 = (unsigned short)script_getnum(st,8);
  6128. c3 = (unsigned short)script_getnum(st,9);
  6129. c4 = (unsigned short)script_getnum(st,10);
  6130. if( item_data ) {
  6131. int get_count = 0, i;
  6132. memset(&item_tmp,0,sizeof(item_tmp));
  6133. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6134. if(ref > MAX_REFINE)
  6135. ref = MAX_REFINE;
  6136. }
  6137. else if( item_data->type == IT_PETEGG ) {
  6138. iden = 1;
  6139. ref = 0;
  6140. }
  6141. else {
  6142. iden = 1;
  6143. ref = attr = 0;
  6144. }
  6145. item_tmp.nameid = nameid;
  6146. item_tmp.identify = iden;
  6147. item_tmp.refine = ref;
  6148. item_tmp.attribute = attr;
  6149. item_tmp.card[0] = c1;
  6150. item_tmp.card[1] = c2;
  6151. item_tmp.card[2] = c3;
  6152. item_tmp.card[3] = c4;
  6153. item_tmp.bound = bound;
  6154. //Check if it's stackable.
  6155. if (!itemdb_isstackable2(item_data))
  6156. get_count = 1;
  6157. else
  6158. get_count = amount;
  6159. for (i = 0; i < amount; i += get_count)
  6160. {
  6161. // if not pet egg
  6162. if (!pet_create_egg(sd, nameid))
  6163. {
  6164. unsigned char flag = 0;
  6165. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6166. {
  6167. clif_additem(sd, 0, 0, flag);
  6168. if( pc_candrop(sd,&item_tmp) )
  6169. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6170. }
  6171. }
  6172. }
  6173. }
  6174. return SCRIPT_CMD_SUCCESS;
  6175. }
  6176. /** Gives rental item to player
  6177. * rentitem <item id>,<seconds>{,<account_id>}
  6178. * rentitem "<item name>",<seconds>{,<account_id>}
  6179. */
  6180. BUILDIN_FUNC(rentitem) {
  6181. struct map_session_data *sd;
  6182. struct script_data *data;
  6183. struct item it;
  6184. int seconds;
  6185. unsigned short nameid = 0;
  6186. unsigned char flag = 0;
  6187. data = script_getdata(st,2);
  6188. get_val(st,data);
  6189. if (!script_accid2sd(4,sd))
  6190. return SCRIPT_CMD_FAILURE;
  6191. if( data_isstring(data) )
  6192. {
  6193. const char *name = conv_str(st,data);
  6194. struct item_data *itd = itemdb_searchname(name);
  6195. if( itd == NULL )
  6196. {
  6197. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6198. return SCRIPT_CMD_FAILURE;
  6199. }
  6200. nameid = itd->nameid;
  6201. }
  6202. else if( data_isint(data) )
  6203. {
  6204. nameid = conv_num(st,data);
  6205. if( nameid == 0 || !itemdb_exists(nameid) )
  6206. {
  6207. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6208. return SCRIPT_CMD_FAILURE;
  6209. }
  6210. }
  6211. else
  6212. {
  6213. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6214. return SCRIPT_CMD_FAILURE;
  6215. }
  6216. seconds = script_getnum(st,3);
  6217. memset(&it, 0, sizeof(it));
  6218. it.nameid = nameid;
  6219. it.identify = 1;
  6220. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6221. it.bound = BOUND_NONE;
  6222. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6223. {
  6224. clif_additem(sd, 0, 0, flag);
  6225. return SCRIPT_CMD_FAILURE;
  6226. }
  6227. return SCRIPT_CMD_SUCCESS;
  6228. }
  6229. /** Gives rental item to player with advanced option
  6230. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6231. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6232. */
  6233. BUILDIN_FUNC(rentitem2) {
  6234. struct map_session_data *sd;
  6235. struct script_data *data;
  6236. struct item it;
  6237. struct item_data *id;
  6238. int seconds;
  6239. unsigned short nameid = 0;
  6240. unsigned char flag = 0;
  6241. int iden,ref,attr,c1,c2,c3,c4;
  6242. data = script_getdata(st,2);
  6243. get_val(st,data);
  6244. if (!script_accid2sd(11,sd))
  6245. return SCRIPT_CMD_FAILURE;
  6246. if( data_isstring(data) ) {
  6247. const char *name = conv_str(st,data);
  6248. id = itemdb_searchname(name);
  6249. if( id == NULL ) {
  6250. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6251. return SCRIPT_CMD_FAILURE;
  6252. }
  6253. nameid = id->nameid;
  6254. }
  6255. else if( data_isint(data) ) {
  6256. nameid = conv_num(st,data);
  6257. if( !(id = itemdb_search(nameid))) {
  6258. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6259. return SCRIPT_CMD_FAILURE;
  6260. }
  6261. }
  6262. else {
  6263. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6264. return SCRIPT_CMD_FAILURE;
  6265. }
  6266. seconds = script_getnum(st,3);
  6267. iden = script_getnum(st,4);
  6268. ref = script_getnum(st,5);
  6269. attr = script_getnum(st,6);
  6270. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6271. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6272. }
  6273. else if (id->type==IT_PETEGG) {
  6274. iden = 1;
  6275. ref = 0;
  6276. }
  6277. else {
  6278. iden = 1;
  6279. ref = attr = 0;
  6280. }
  6281. c1 = (short)script_getnum(st,7);
  6282. c2 = (short)script_getnum(st,8);
  6283. c3 = (short)script_getnum(st,9);
  6284. c4 = (short)script_getnum(st,10);
  6285. memset(&it, 0, sizeof(it));
  6286. it.nameid = nameid;
  6287. it.identify = iden;
  6288. it.refine = ref;
  6289. it.attribute = attr;
  6290. it.card[0] = (short)c1;
  6291. it.card[1] = (short)c2;
  6292. it.card[2] = (short)c3;
  6293. it.card[3] = (short)c4;
  6294. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6295. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6296. clif_additem(sd, 0, 0, flag);
  6297. return SCRIPT_CMD_FAILURE;
  6298. }
  6299. return SCRIPT_CMD_SUCCESS;
  6300. }
  6301. /*==========================================
  6302. * gets an item with someone's name inscribed [Skotlex]
  6303. * getinscribeditem item_num, character_name
  6304. * Returned Qty is always 1, only works on equip-able
  6305. * equipment
  6306. *------------------------------------------*/
  6307. BUILDIN_FUNC(getnameditem)
  6308. {
  6309. unsigned short nameid;
  6310. struct item item_tmp;
  6311. TBL_PC *sd, *tsd;
  6312. struct script_data *data;
  6313. sd = script_rid2sd(st);
  6314. if (sd == NULL)
  6315. { //Player not attached!
  6316. script_pushint(st,0);
  6317. return 0;
  6318. }
  6319. data=script_getdata(st,2);
  6320. get_val(st,data);
  6321. if( data_isstring(data) ){
  6322. const char *name=conv_str(st,data);
  6323. struct item_data *item_data = itemdb_searchname(name);
  6324. if( item_data == NULL)
  6325. { //Failed
  6326. script_pushint(st,0);
  6327. return 0;
  6328. }
  6329. nameid = item_data->nameid;
  6330. }else
  6331. nameid = conv_num(st,data);
  6332. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6333. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6334. script_pushint(st,0);
  6335. return 0;
  6336. }
  6337. data=script_getdata(st,3);
  6338. get_val(st,data);
  6339. if( data_isstring(data) ) //Char Name
  6340. tsd=map_nick2sd(conv_str(st,data));
  6341. else //Char Id was given
  6342. tsd=map_charid2sd(conv_num(st,data));
  6343. if( tsd == NULL )
  6344. { //Failed
  6345. script_pushint(st,0);
  6346. return 0;
  6347. }
  6348. memset(&item_tmp,0,sizeof(item_tmp));
  6349. item_tmp.nameid=nameid;
  6350. item_tmp.amount=1;
  6351. item_tmp.identify=1;
  6352. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6353. item_tmp.card[2]=tsd->status.char_id;
  6354. item_tmp.card[3]=tsd->status.char_id >> 16;
  6355. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6356. script_pushint(st,0);
  6357. return 0; //Failed to add item, we will not drop if they don't fit
  6358. }
  6359. script_pushint(st,1);
  6360. return SCRIPT_CMD_SUCCESS;
  6361. }
  6362. /*==========================================
  6363. * gets a random item ID from an item group [Skotlex]
  6364. * groupranditem <group_num>{,<sub_group>};
  6365. *------------------------------------------*/
  6366. BUILDIN_FUNC(grouprandomitem) {
  6367. struct s_item_group_entry *entry = NULL;
  6368. int sub_group = 1;
  6369. FETCH(3, sub_group);
  6370. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6371. if (!entry) {
  6372. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6373. script_pushint(st,UNKNOWN_ITEM_ID);
  6374. return SCRIPT_CMD_FAILURE;
  6375. }
  6376. script_pushint(st,entry->nameid);
  6377. return SCRIPT_CMD_SUCCESS;
  6378. }
  6379. /**
  6380. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6381. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6382. */
  6383. BUILDIN_FUNC(makeitem) {
  6384. int16 nameid;
  6385. uint16 amount, flag = 0, x, y;
  6386. const char *mapname;
  6387. int m;
  6388. struct item item_tmp;
  6389. struct script_data *data;
  6390. data = script_getdata(st,2);
  6391. get_val(st,data);
  6392. if( data_isstring(data) ){
  6393. const char *name = conv_str(st,data);
  6394. struct item_data *item_data = itemdb_searchname(name);
  6395. if( item_data )
  6396. nameid = item_data->nameid;
  6397. else
  6398. nameid = UNKNOWN_ITEM_ID;
  6399. }
  6400. else
  6401. nameid = conv_num(st,data);
  6402. amount = script_getnum(st,3);
  6403. mapname = script_getstr(st,4);
  6404. x = script_getnum(st,5);
  6405. y = script_getnum(st,6);
  6406. if(strcmp(mapname,"this")==0) {
  6407. TBL_PC *sd;
  6408. sd = script_rid2sd(st);
  6409. if (!sd)
  6410. return SCRIPT_CMD_SUCCESS; //Failed...
  6411. m = sd->bl.m;
  6412. } else
  6413. m = map_mapname2mapid(mapname);
  6414. if(nameid<0) {
  6415. nameid = -nameid;
  6416. flag = 1;
  6417. }
  6418. if(nameid > 0) {
  6419. memset(&item_tmp,0,sizeof(item_tmp));
  6420. item_tmp.nameid = nameid;
  6421. if(!flag)
  6422. item_tmp.identify = 1;
  6423. else
  6424. item_tmp.identify = itemdb_isidentified(nameid);
  6425. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6426. }
  6427. return SCRIPT_CMD_SUCCESS;
  6428. }
  6429. /**
  6430. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6431. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6432. */
  6433. BUILDIN_FUNC(makeitem2) {
  6434. uint16 nameid, amount, x, y;
  6435. const char *mapname;
  6436. int m;
  6437. struct item item_tmp;
  6438. struct script_data *data;
  6439. struct item_data *id;
  6440. data = script_getdata(st,2);
  6441. get_val(st,data);
  6442. if( data_isstring(data) ){
  6443. const char *name = conv_str(st,data);
  6444. struct item_data *item_data = itemdb_searchname(name);
  6445. if( item_data )
  6446. nameid = item_data->nameid;
  6447. else
  6448. nameid = UNKNOWN_ITEM_ID;
  6449. }
  6450. else
  6451. nameid = conv_num(st,data);
  6452. amount = script_getnum(st,3);
  6453. mapname = script_getstr(st,4);
  6454. x = script_getnum(st,5);
  6455. y = script_getnum(st,6);
  6456. if (strcmp(mapname,"this")==0) {
  6457. TBL_PC *sd;
  6458. sd = script_rid2sd(st);
  6459. if (!sd)
  6460. return SCRIPT_CMD_SUCCESS; //Failed...
  6461. m = sd->bl.m;
  6462. }
  6463. else
  6464. m = map_mapname2mapid(mapname);
  6465. if ((id = itemdb_search(nameid))) {
  6466. char iden, ref, attr;
  6467. memset(&item_tmp,0,sizeof(item_tmp));
  6468. item_tmp.nameid = nameid;
  6469. iden = (char)script_getnum(st,7);
  6470. ref = (char)script_getnum(st,8);
  6471. attr = (char)script_getnum(st,9);
  6472. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6473. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6474. }
  6475. else if (id->type==IT_PETEGG) {
  6476. iden = 1;
  6477. ref = 0;
  6478. }
  6479. else {
  6480. iden = 1;
  6481. ref = attr = 0;
  6482. }
  6483. item_tmp.identify = iden;
  6484. item_tmp.refine = ref;
  6485. item_tmp.attribute = attr;
  6486. item_tmp.card[0] = script_getnum(st,10);
  6487. item_tmp.card[1] = script_getnum(st,11);
  6488. item_tmp.card[2] = script_getnum(st,12);
  6489. item_tmp.card[3] = script_getnum(st,13);
  6490. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6491. }
  6492. else
  6493. return SCRIPT_CMD_FAILURE;
  6494. return SCRIPT_CMD_SUCCESS;
  6495. }
  6496. /// Counts / deletes the current item given by idx.
  6497. /// Used by buildin_delitem_search
  6498. /// Relies on all input data being already fully valid.
  6499. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6500. {
  6501. int delamount;
  6502. struct item *itm = NULL;
  6503. switch(loc) {
  6504. case 1: // cart
  6505. itm = &sd->status.cart[idx];
  6506. break;
  6507. case 2: // storage
  6508. itm = &sd->status.storage.items[idx];
  6509. break;
  6510. default: //inventory
  6511. itm = &sd->status.inventory[idx];
  6512. break;
  6513. }
  6514. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6515. if( delete_items )
  6516. {
  6517. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6518. {// delete associated pet
  6519. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6520. }
  6521. switch(loc) {
  6522. case 1:
  6523. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6524. break;
  6525. case 2:
  6526. storage_delitem(sd,idx,delamount);
  6527. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6528. break;
  6529. default:
  6530. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6531. break;
  6532. }
  6533. }
  6534. amount[0]-= delamount;
  6535. }
  6536. /// Searches for item(s) and checks, if there is enough of them.
  6537. /// Used by delitem and delitem2
  6538. /// Relies on all input data being already fully valid.
  6539. /// @param exact_match will also match item attributes and cards, not just name id
  6540. /// @return true when all items could be deleted, false when there were not enough items to delete
  6541. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6542. {
  6543. bool delete_items = false;
  6544. int i, amount, size;
  6545. struct item *items;
  6546. // prefer always non-equipped items
  6547. it->equip = 0;
  6548. // when searching for nameid only, prefer additionally
  6549. if( !exact_match )
  6550. {
  6551. // non-refined items
  6552. it->refine = 0;
  6553. // card-less items
  6554. memset(it->card, 0, sizeof(it->card));
  6555. }
  6556. switch(loc) {
  6557. case 1: // cart
  6558. size = MAX_CART;
  6559. items = sd->status.cart;
  6560. break;
  6561. case 2: // storage
  6562. size = MAX_STORAGE;
  6563. items = sd->status.storage.items;
  6564. break;
  6565. default: //inventory
  6566. size = MAX_INVENTORY;
  6567. items = sd->status.inventory;
  6568. break;
  6569. }
  6570. for(;;)
  6571. {
  6572. unsigned short important = 0;
  6573. amount = it->amount;
  6574. // 1st pass -- less important items / exact match
  6575. for( i = 0; amount && i < size; i++ )
  6576. {
  6577. struct item *itm = NULL;
  6578. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6579. {// wrong/invalid item
  6580. continue;
  6581. }
  6582. if( itm->equip != it->equip || itm->refine != it->refine )
  6583. {// not matching attributes
  6584. important++;
  6585. continue;
  6586. }
  6587. if( exact_match )
  6588. {
  6589. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6590. {// not matching exact attributes
  6591. continue;
  6592. }
  6593. }
  6594. else
  6595. {
  6596. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6597. {
  6598. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6599. {// pet which cannot be deleted
  6600. continue;
  6601. }
  6602. }
  6603. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6604. {// named/carded item
  6605. important++;
  6606. continue;
  6607. }
  6608. }
  6609. // count / delete item
  6610. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6611. }
  6612. // 2nd pass -- any matching item
  6613. if( amount == 0 || important == 0 )
  6614. {// either everything was already consumed or no items were skipped
  6615. ;
  6616. }
  6617. else for( i = 0; amount && i < size; i++ )
  6618. {
  6619. struct item *itm = NULL;
  6620. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6621. {// wrong/invalid item
  6622. continue;
  6623. }
  6624. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6625. {// pet which cannot be deleted
  6626. continue;
  6627. }
  6628. if( exact_match )
  6629. {
  6630. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6631. {// not matching attributes
  6632. continue;
  6633. }
  6634. }
  6635. // count / delete item
  6636. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6637. }
  6638. if( amount )
  6639. {// not enough items
  6640. return false;
  6641. }
  6642. else if( delete_items )
  6643. {// we are done with the work
  6644. return true;
  6645. }
  6646. else
  6647. {// get rid of the items now
  6648. delete_items = true;
  6649. }
  6650. }
  6651. }
  6652. /// Deletes items from the target/attached player.
  6653. /// Prioritizes ordinary items.
  6654. ///
  6655. /// delitem <item id>,<amount>{,<account id>}
  6656. /// delitem "<item name>",<amount>{,<account id>}
  6657. /// cartdelitem <item id>,<amount>{,<account id>}
  6658. /// cartdelitem "<item name>",<amount>{,<account id>}
  6659. /// storagedelitem <item id>,<amount>{,<account id>}
  6660. /// storagedelitem "<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 = 1;
  6670. else if(!strncmp(command, "storage", 7))
  6671. loc = 2;
  6672. //TODO: 3 - Guild Storage
  6673. if( script_hasdata(st,4) )
  6674. {
  6675. uint32 account_id = script_getnum(st,4);
  6676. sd = map_id2sd(account_id); // <account id>
  6677. if( sd == NULL )
  6678. {
  6679. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6680. st->state = END;
  6681. return 1;
  6682. }
  6683. }
  6684. else
  6685. {
  6686. sd = script_rid2sd(st);// attached player
  6687. if( sd == NULL )
  6688. return 0;
  6689. }
  6690. if (loc == 1 && !pc_iscarton(sd)) {
  6691. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6692. return SCRIPT_CMD_FAILURE;
  6693. }
  6694. data = script_getdata(st,2);
  6695. get_val(st,data);
  6696. if( data_isstring(data) )
  6697. {
  6698. const char* item_name = conv_str(st,data);
  6699. struct item_data* id = itemdb_searchname(item_name);
  6700. if( id == NULL )
  6701. {
  6702. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6703. st->state = END;
  6704. return 1;
  6705. }
  6706. it.nameid = id->nameid;// "<item name>"
  6707. }
  6708. else
  6709. {
  6710. it.nameid = conv_num(st,data);// <item id>
  6711. if( !itemdb_exists( it.nameid ) )
  6712. {
  6713. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6714. st->state = END;
  6715. return 1;
  6716. }
  6717. }
  6718. it.amount=script_getnum(st,3);
  6719. if( it.amount <= 0 )
  6720. return 0;// nothing to do
  6721. if( buildin_delitem_search(sd, &it, false, loc) )
  6722. {// success
  6723. return SCRIPT_CMD_SUCCESS;
  6724. }
  6725. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6726. st->state = END;
  6727. st->mes_active = 0;
  6728. clif_scriptclose(sd, st->oid);
  6729. return SCRIPT_CMD_FAILURE;
  6730. }
  6731. /// Deletes items from the target/attached player.
  6732. ///
  6733. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6734. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6735. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6736. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6737. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6738. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6739. BUILDIN_FUNC(delitem2)
  6740. {
  6741. TBL_PC *sd;
  6742. struct item it;
  6743. struct script_data *data;
  6744. uint8 loc = 0;
  6745. char* command = (char*)script_getfuncname(st);
  6746. if(!strncmp(command, "cart", 4))
  6747. loc = 1;
  6748. else if(!strncmp(command, "storage", 7))
  6749. loc = 2;
  6750. //TODO: 3 - Guild Storage
  6751. if( script_hasdata(st,11) )
  6752. {
  6753. uint32 account_id = script_getnum(st,11);
  6754. sd = map_id2sd(account_id); // <account id>
  6755. if( sd == NULL )
  6756. {
  6757. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6758. st->state = END;
  6759. return 1;
  6760. }
  6761. }
  6762. else
  6763. {
  6764. sd = script_rid2sd(st);// attached player
  6765. if( sd == NULL )
  6766. return 0;
  6767. }
  6768. if (loc == 1 && !pc_iscarton(sd)) {
  6769. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6770. script_pushint(st,-1);
  6771. return SCRIPT_CMD_FAILURE;
  6772. }
  6773. data = script_getdata(st,2);
  6774. get_val(st,data);
  6775. if( data_isstring(data) )
  6776. {
  6777. const char* item_name = conv_str(st,data);
  6778. struct item_data* id = itemdb_searchname(item_name);
  6779. if( id == NULL )
  6780. {
  6781. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6782. st->state = END;
  6783. return 1;
  6784. }
  6785. it.nameid = id->nameid;// "<item name>"
  6786. }
  6787. else
  6788. {
  6789. it.nameid = conv_num(st,data);// <item id>
  6790. if( !itemdb_exists( it.nameid ) )
  6791. {
  6792. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6793. st->state = END;
  6794. return 1;
  6795. }
  6796. }
  6797. it.amount=script_getnum(st,3);
  6798. it.identify=script_getnum(st,4);
  6799. it.refine=script_getnum(st,5);
  6800. it.attribute=script_getnum(st,6);
  6801. it.card[0]=(short)script_getnum(st,7);
  6802. it.card[1]=(short)script_getnum(st,8);
  6803. it.card[2]=(short)script_getnum(st,9);
  6804. it.card[3]=(short)script_getnum(st,10);
  6805. if( it.amount <= 0 )
  6806. return 0;// nothing to do
  6807. if( buildin_delitem_search(sd, &it, true, loc) )
  6808. {// success
  6809. return SCRIPT_CMD_SUCCESS;
  6810. }
  6811. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6812. st->state = END;
  6813. st->mes_active = 0;
  6814. clif_scriptclose(sd, st->oid);
  6815. return SCRIPT_CMD_FAILURE;
  6816. }
  6817. /*==========================================
  6818. * Enables/Disables use of items while in an NPC [Skotlex]
  6819. *------------------------------------------*/
  6820. BUILDIN_FUNC(enableitemuse)
  6821. {
  6822. TBL_PC *sd;
  6823. sd=script_rid2sd(st);
  6824. if (sd)
  6825. st->npc_item_flag = sd->npc_item_flag = 1;
  6826. return SCRIPT_CMD_SUCCESS;
  6827. }
  6828. BUILDIN_FUNC(disableitemuse)
  6829. {
  6830. TBL_PC *sd;
  6831. sd=script_rid2sd(st);
  6832. if (sd)
  6833. st->npc_item_flag = sd->npc_item_flag = 0;
  6834. return SCRIPT_CMD_SUCCESS;
  6835. }
  6836. /*==========================================
  6837. * Returns a character's specified stat.
  6838. * Check pc_readparam for available options.
  6839. * readparam <param>{,"<nick>"}
  6840. *------------------------------------------*/
  6841. BUILDIN_FUNC(readparam)
  6842. {
  6843. int type;
  6844. struct script_data *data = script_getdata(st, 2);
  6845. TBL_PC *sd;
  6846. if (!script_nick2sd(3,sd)) {
  6847. script_pushint(st,-1);
  6848. return SCRIPT_CMD_FAILURE;
  6849. }
  6850. if (data->type == C_NAME) { // If using constant name, just get the val
  6851. get_val_(st, data, sd);
  6852. script_pushint(st, (int)data->u.num);
  6853. return SCRIPT_CMD_SUCCESS;
  6854. }
  6855. type = script_getnum(st, 2);
  6856. script_pushint(st,pc_readparam(sd,type));
  6857. return SCRIPT_CMD_SUCCESS;
  6858. }
  6859. /*==========================================
  6860. * Return charid identification
  6861. * return by @num :
  6862. * 0 : char_id
  6863. * 1 : party_id
  6864. * 2 : guild_id
  6865. * 3 : account_id
  6866. * 4 : bg_id
  6867. *------------------------------------------*/
  6868. BUILDIN_FUNC(getcharid)
  6869. {
  6870. int num;
  6871. TBL_PC *sd;
  6872. num = script_getnum(st,2);
  6873. if( script_hasdata(st,3) )
  6874. sd=map_nick2sd(script_getstr(st,3));
  6875. else
  6876. sd=script_rid2sd(st);
  6877. if(sd==NULL){
  6878. script_pushint(st,0); //return 0, according docs
  6879. return 0;
  6880. }
  6881. switch( num ) {
  6882. case 0: script_pushint(st,sd->status.char_id); break;
  6883. case 1: script_pushint(st,sd->status.party_id); break;
  6884. case 2: script_pushint(st,sd->status.guild_id); break;
  6885. case 3: script_pushint(st,sd->status.account_id); break;
  6886. case 4: script_pushint(st,sd->bg_id); break;
  6887. default:
  6888. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6889. script_pushint(st,0);
  6890. break;
  6891. }
  6892. return SCRIPT_CMD_SUCCESS;
  6893. }
  6894. /*==========================================
  6895. * returns the GID of an NPC
  6896. *------------------------------------------*/
  6897. BUILDIN_FUNC(getnpcid)
  6898. {
  6899. int num = script_getnum(st,2);
  6900. struct npc_data* nd = NULL;
  6901. if( script_hasdata(st,3) )
  6902. {// unique npc name
  6903. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6904. {
  6905. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6906. script_pushint(st,0);
  6907. return 1;
  6908. }
  6909. }
  6910. switch (num) {
  6911. case 0:
  6912. script_pushint(st,nd ? nd->bl.id : st->oid);
  6913. break;
  6914. default:
  6915. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6916. script_pushint(st,0);
  6917. return 1;
  6918. }
  6919. return SCRIPT_CMD_SUCCESS;
  6920. }
  6921. /*==========================================
  6922. * Return the name of the party_id
  6923. * null if not found
  6924. *------------------------------------------*/
  6925. BUILDIN_FUNC(getpartyname)
  6926. {
  6927. int party_id;
  6928. struct party_data* p;
  6929. party_id = script_getnum(st,2);
  6930. if( ( p = party_search(party_id) ) != NULL )
  6931. {
  6932. script_pushstrcopy(st,p->party.name);
  6933. }
  6934. else
  6935. {
  6936. script_pushconststr(st,"null");
  6937. }
  6938. return SCRIPT_CMD_SUCCESS;
  6939. }
  6940. /*==========================================
  6941. * Get the information of the members of a party by type
  6942. * @party_id, @type
  6943. * return by @type :
  6944. * - : nom des membres
  6945. * 1 : char_id des membres
  6946. * 2 : account_id des membres
  6947. *------------------------------------------*/
  6948. BUILDIN_FUNC(getpartymember)
  6949. {
  6950. struct party_data *p;
  6951. uint8 j = 0;
  6952. p = party_search(script_getnum(st,2));
  6953. if (p != NULL) {
  6954. uint8 i, type = 0;
  6955. struct script_data *data = NULL;
  6956. char *varname = NULL;
  6957. if (script_hasdata(st,3))
  6958. type = script_getnum(st,3);
  6959. if (script_hasdata(st,4)) {
  6960. data = script_getdata(st, 4);
  6961. if (!data_isreference(data)) {
  6962. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  6963. return SCRIPT_CMD_FAILURE;
  6964. }
  6965. varname = reference_getname(data);
  6966. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  6967. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  6968. return SCRIPT_CMD_FAILURE;
  6969. }
  6970. }
  6971. for (i = 0; i < MAX_PARTY; i++) {
  6972. if (p->party.member[i].account_id) {
  6973. switch (type) {
  6974. case 2:
  6975. if (data)
  6976. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  6977. else
  6978. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6979. break;
  6980. case 1:
  6981. if (data)
  6982. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  6983. else
  6984. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6985. break;
  6986. default:
  6987. if (data)
  6988. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  6989. else
  6990. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6991. break;
  6992. }
  6993. j++;
  6994. }
  6995. }
  6996. }
  6997. mapreg_setreg(add_str("$@partymembercount"),j);
  6998. script_pushint(st, j);
  6999. return SCRIPT_CMD_SUCCESS;
  7000. }
  7001. /*==========================================
  7002. * Retrieves party leader. if flag is specified,
  7003. * return some of the leader data. Otherwise, return name.
  7004. *------------------------------------------*/
  7005. BUILDIN_FUNC(getpartyleader)
  7006. {
  7007. int party_id, type = 0, i=0;
  7008. struct party_data *p;
  7009. party_id=script_getnum(st,2);
  7010. if( script_hasdata(st,3) )
  7011. type=script_getnum(st,3);
  7012. p=party_search(party_id);
  7013. if (p) //Search leader
  7014. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7015. if (!p || i == MAX_PARTY) { //leader not found
  7016. if (type)
  7017. script_pushint(st,-1);
  7018. else
  7019. script_pushconststr(st,"null");
  7020. return 0;
  7021. }
  7022. switch (type) {
  7023. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7024. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7025. case 3: script_pushint(st,p->party.member[i].class_); break;
  7026. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7027. case 5: script_pushint(st,p->party.member[i].lv); break;
  7028. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7029. }
  7030. return SCRIPT_CMD_SUCCESS;
  7031. }
  7032. /*==========================================
  7033. * Return the name of the @guild_id
  7034. * null if not found
  7035. *------------------------------------------*/
  7036. BUILDIN_FUNC(getguildname)
  7037. {
  7038. int guild_id;
  7039. struct guild* g;
  7040. guild_id = script_getnum(st,2);
  7041. if( ( g = guild_search(guild_id) ) != NULL )
  7042. script_pushstrcopy(st,g->name);
  7043. else
  7044. script_pushconststr(st,"null");
  7045. return SCRIPT_CMD_SUCCESS;
  7046. }
  7047. /*==========================================
  7048. * Return the name of the guild master of @guild_id
  7049. * null if not found
  7050. *------------------------------------------*/
  7051. BUILDIN_FUNC(getguildmaster)
  7052. {
  7053. int guild_id;
  7054. struct guild* g;
  7055. guild_id = script_getnum(st,2);
  7056. if( ( g = guild_search(guild_id) ) != NULL )
  7057. script_pushstrcopy(st,g->member[0].name);
  7058. else
  7059. script_pushconststr(st,"null");
  7060. return SCRIPT_CMD_SUCCESS;
  7061. }
  7062. BUILDIN_FUNC(getguildmasterid)
  7063. {
  7064. int guild_id;
  7065. struct guild* g;
  7066. guild_id = script_getnum(st,2);
  7067. if( ( g = guild_search(guild_id) ) != NULL )
  7068. script_pushint(st,g->member[0].char_id);
  7069. else
  7070. script_pushint(st,0);
  7071. return SCRIPT_CMD_SUCCESS;
  7072. }
  7073. /*==========================================
  7074. * Get char string information by type :
  7075. * Return by @type :
  7076. * 0 : char_name
  7077. * 1 : party_name or ""
  7078. * 2 : guild_name or ""
  7079. * 3 : map_name
  7080. * - : ""
  7081. * strcharinfo(<type>{,<char_id>})
  7082. *------------------------------------------*/
  7083. BUILDIN_FUNC(strcharinfo)
  7084. {
  7085. TBL_PC *sd;
  7086. int num;
  7087. struct guild* g;
  7088. struct party_data* p;
  7089. if (!script_charid2sd(3,sd)) {
  7090. script_pushconststr(st,"");
  7091. return SCRIPT_CMD_FAILURE;
  7092. }
  7093. num=script_getnum(st,2);
  7094. switch(num){
  7095. case 0:
  7096. script_pushstrcopy(st,sd->status.name);
  7097. break;
  7098. case 1:
  7099. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7100. script_pushstrcopy(st,p->party.name);
  7101. } else {
  7102. script_pushconststr(st,"");
  7103. }
  7104. break;
  7105. case 2:
  7106. if( ( g = sd->guild ) != NULL ) {
  7107. script_pushstrcopy(st,g->name);
  7108. } else {
  7109. script_pushconststr(st,"");
  7110. }
  7111. break;
  7112. case 3:
  7113. script_pushconststr(st,map[sd->bl.m].name);
  7114. break;
  7115. default:
  7116. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7117. script_pushconststr(st,"");
  7118. break;
  7119. }
  7120. return SCRIPT_CMD_SUCCESS;
  7121. }
  7122. /*==========================================
  7123. * Get npc string information by type
  7124. * return by @type:
  7125. * 0 : name
  7126. * 1 : str#
  7127. * 2 : #str
  7128. * 3 : ::str
  7129. * 4 : map name
  7130. *------------------------------------------*/
  7131. BUILDIN_FUNC(strnpcinfo)
  7132. {
  7133. TBL_NPC* nd;
  7134. int num;
  7135. char *buf,*name=NULL;
  7136. nd = map_id2nd(st->oid);
  7137. if (!nd) {
  7138. script_pushconststr(st, "");
  7139. return 0;
  7140. }
  7141. num = script_getnum(st,2);
  7142. switch(num){
  7143. case 0: // display name
  7144. name = aStrdup(nd->name);
  7145. break;
  7146. case 1: // visible part of display name
  7147. if((buf = strchr(nd->name,'#')) != NULL)
  7148. {
  7149. name = aStrdup(nd->name);
  7150. name[buf - nd->name] = 0;
  7151. } else // Return the name, there is no '#' present
  7152. name = aStrdup(nd->name);
  7153. break;
  7154. case 2: // # fragment
  7155. if((buf = strchr(nd->name,'#')) != NULL)
  7156. name = aStrdup(buf+1);
  7157. break;
  7158. case 3: // unique name
  7159. name = aStrdup(nd->exname);
  7160. break;
  7161. case 4: // map name
  7162. name = aStrdup(map[nd->bl.m].name);
  7163. break;
  7164. }
  7165. if(name)
  7166. script_pushstr(st, name);
  7167. else
  7168. script_pushconststr(st, "");
  7169. return SCRIPT_CMD_SUCCESS;
  7170. }
  7171. /// aegis->athena slot position conversion table
  7172. /// Index is used by EQI_ in db/const.txt
  7173. static unsigned int equip[] = {
  7174. EQP_HEAD_TOP,
  7175. EQP_ARMOR,
  7176. EQP_HAND_L,
  7177. EQP_HAND_R,
  7178. EQP_GARMENT,
  7179. EQP_SHOES,
  7180. EQP_ACC_L,
  7181. EQP_ACC_R,
  7182. EQP_HEAD_MID,
  7183. EQP_HEAD_LOW,
  7184. EQP_COSTUME_HEAD_LOW,
  7185. EQP_COSTUME_HEAD_MID,
  7186. EQP_COSTUME_HEAD_TOP,
  7187. EQP_COSTUME_GARMENT,
  7188. EQP_AMMO,
  7189. EQP_SHADOW_ARMOR,
  7190. EQP_SHADOW_WEAPON,
  7191. EQP_SHADOW_SHIELD,
  7192. EQP_SHADOW_SHOES,
  7193. EQP_SHADOW_ACC_R,
  7194. EQP_SHADOW_ACC_L
  7195. };
  7196. /**
  7197. * getequipid(<equipment slot>{,<char_id>})
  7198. **/
  7199. BUILDIN_FUNC(getequipid)
  7200. {
  7201. int i, num;
  7202. TBL_PC* sd;
  7203. struct item_data* item;
  7204. if (!script_charid2sd(3, sd)) {
  7205. script_pushint(st,-1);
  7206. return SCRIPT_CMD_FAILURE;
  7207. }
  7208. num = script_getnum(st,2) - 1;
  7209. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7210. {
  7211. script_pushint(st,-1);
  7212. return SCRIPT_CMD_FAILURE;
  7213. }
  7214. // get inventory position of item
  7215. i = pc_checkequip(sd,equip[num]);
  7216. if( i < 0 )
  7217. {
  7218. script_pushint(st,-1);
  7219. return SCRIPT_CMD_FAILURE;
  7220. }
  7221. item = sd->inventory_data[i];
  7222. if( item != 0 )
  7223. script_pushint(st,item->nameid);
  7224. else
  7225. script_pushint(st,0);
  7226. return SCRIPT_CMD_SUCCESS;
  7227. }
  7228. /*==========================================
  7229. * getequipuniqueid(<equipment slot>{,<char_id>})
  7230. *------------------------------------------*/
  7231. BUILDIN_FUNC(getequipuniqueid)
  7232. {
  7233. int i, num;
  7234. TBL_PC* sd;
  7235. struct item* item;
  7236. if (!script_charid2sd(3, sd)) {
  7237. script_pushconststr(st, "");
  7238. return SCRIPT_CMD_FAILURE;
  7239. }
  7240. num = script_getnum(st,2) - 1;
  7241. if (num < 0 || num >= ARRAYLENGTH(equip)) {
  7242. script_pushconststr(st, "");
  7243. return SCRIPT_CMD_FAILURE;
  7244. }
  7245. // get inventory position of item
  7246. i = pc_checkequip(sd,equip[num]);
  7247. if (i < 0) {
  7248. script_pushconststr(st, "");
  7249. return SCRIPT_CMD_FAILURE;
  7250. }
  7251. item = &sd->status.inventory[i];
  7252. if (item != 0) {
  7253. char buf[256];
  7254. memset(buf, 0, sizeof(buf));
  7255. snprintf(buf, sizeof(buf)-1, "%llu", (unsigned long long)item->unique_id);
  7256. script_pushstr(st, buf);
  7257. } else
  7258. script_pushconststr(st, "");
  7259. return SCRIPT_CMD_SUCCESS;
  7260. }
  7261. /*==========================================
  7262. * Get the equipement name at pos
  7263. * return item jname or ""
  7264. * getequipname(<equipment slot>{,<char_id>})
  7265. *------------------------------------------*/
  7266. BUILDIN_FUNC(getequipname)
  7267. {
  7268. int i, num;
  7269. TBL_PC* sd;
  7270. struct item_data* item;
  7271. if (!script_charid2sd(3, sd)) {
  7272. script_pushconststr(st, "");
  7273. return SCRIPT_CMD_FAILURE;
  7274. }
  7275. num = script_getnum(st,2) - 1;
  7276. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7277. {
  7278. script_pushconststr(st,"");
  7279. return 0;
  7280. }
  7281. // get inventory position of item
  7282. i = pc_checkequip(sd,equip[num]);
  7283. if( i < 0 )
  7284. {
  7285. script_pushconststr(st,"");
  7286. return 0;
  7287. }
  7288. item = sd->inventory_data[i];
  7289. if( item != 0 )
  7290. script_pushstrcopy(st,item->jname);
  7291. else
  7292. script_pushconststr(st,"");
  7293. return SCRIPT_CMD_SUCCESS;
  7294. }
  7295. /*==========================================
  7296. * getbrokenid [Valaris]
  7297. * getbrokenid(<number>{,<char_id>})
  7298. *------------------------------------------*/
  7299. BUILDIN_FUNC(getbrokenid)
  7300. {
  7301. int i,num,id=0,brokencounter=0;
  7302. TBL_PC *sd;
  7303. if (!script_charid2sd(3, sd)) {
  7304. script_pushint(st,0);
  7305. return SCRIPT_CMD_FAILURE;
  7306. }
  7307. num=script_getnum(st,2);
  7308. for(i=0; i<MAX_INVENTORY; i++) {
  7309. if(sd->status.inventory[i].attribute){
  7310. brokencounter++;
  7311. if(num==brokencounter){
  7312. id=sd->status.inventory[i].nameid;
  7313. break;
  7314. }
  7315. }
  7316. }
  7317. script_pushint(st,id);
  7318. return SCRIPT_CMD_SUCCESS;
  7319. }
  7320. /*==========================================
  7321. * repair [Valaris]
  7322. * repair <num>{,<char_id>};
  7323. *------------------------------------------*/
  7324. BUILDIN_FUNC(repair)
  7325. {
  7326. int i,num;
  7327. int repaircounter=0;
  7328. TBL_PC *sd;
  7329. if (!script_charid2sd(3,sd))
  7330. return SCRIPT_CMD_FAILURE;
  7331. num=script_getnum(st,2);
  7332. for(i=0; i<MAX_INVENTORY; i++) {
  7333. if(sd->status.inventory[i].attribute){
  7334. repaircounter++;
  7335. if(num==repaircounter){
  7336. sd->status.inventory[i].attribute=0;
  7337. clif_equiplist(sd);
  7338. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  7339. clif_misceffect(&sd->bl, 3);
  7340. break;
  7341. }
  7342. }
  7343. }
  7344. return SCRIPT_CMD_SUCCESS;
  7345. }
  7346. /*==========================================
  7347. * repairall {<char_id>}
  7348. *------------------------------------------*/
  7349. BUILDIN_FUNC(repairall)
  7350. {
  7351. int i, repaircounter = 0;
  7352. TBL_PC *sd;
  7353. if (!script_charid2sd(2,sd))
  7354. return SCRIPT_CMD_FAILURE;
  7355. for(i = 0; i < MAX_INVENTORY; i++)
  7356. {
  7357. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  7358. {
  7359. sd->status.inventory[i].attribute = 0;
  7360. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  7361. repaircounter++;
  7362. }
  7363. }
  7364. if(repaircounter)
  7365. {
  7366. clif_misceffect(&sd->bl, 3);
  7367. clif_equiplist(sd);
  7368. }
  7369. return SCRIPT_CMD_SUCCESS;
  7370. }
  7371. /*==========================================
  7372. * Chk if player have something equiped at pos
  7373. * getequipisequiped <pos>{,<char_id>}
  7374. *------------------------------------------*/
  7375. BUILDIN_FUNC(getequipisequiped)
  7376. {
  7377. int i = -1,num;
  7378. TBL_PC *sd;
  7379. num = script_getnum(st,2);
  7380. if (!script_charid2sd(3,sd))
  7381. return SCRIPT_CMD_FAILURE;
  7382. if (num > 0 && num <= ARRAYLENGTH(equip))
  7383. i=pc_checkequip(sd,equip[num-1]);
  7384. if(i >= 0)
  7385. script_pushint(st,1);
  7386. else
  7387. script_pushint(st,0);
  7388. return SCRIPT_CMD_SUCCESS;
  7389. }
  7390. /*==========================================
  7391. * Chk if the player have something equiped at pos
  7392. * if so chk if this item ain't marked not refinable or rental
  7393. * return (npc)
  7394. * 1 : true
  7395. * 0 : false
  7396. * getequipisenableref(<equipment slot>{,<char_id>})
  7397. *------------------------------------------*/
  7398. BUILDIN_FUNC(getequipisenableref)
  7399. {
  7400. int i = -1,num;
  7401. TBL_PC *sd;
  7402. num = script_getnum(st,2);
  7403. if (!script_charid2sd(3, sd)) {
  7404. script_pushint(st,0);
  7405. return SCRIPT_CMD_FAILURE;
  7406. }
  7407. if( num > 0 && num <= ARRAYLENGTH(equip) )
  7408. i = pc_checkequip(sd,equip[num-1]);
  7409. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  7410. script_pushint(st,1);
  7411. else
  7412. script_pushint(st,0);
  7413. return SCRIPT_CMD_SUCCESS;
  7414. }
  7415. /*==========================================
  7416. * Get the item refined value at pos
  7417. * return (npc)
  7418. * x : refine amount
  7419. * 0 : false (not refined)
  7420. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7421. *------------------------------------------*/
  7422. BUILDIN_FUNC(getequiprefinerycnt)
  7423. {
  7424. int i = -1,num;
  7425. TBL_PC *sd;
  7426. num = script_getnum(st,2);
  7427. if (!script_charid2sd(3, sd)) {
  7428. script_pushint(st,0);
  7429. return SCRIPT_CMD_FAILURE;
  7430. }
  7431. if (num > 0 && num <= ARRAYLENGTH(equip))
  7432. i=pc_checkequip(sd,equip[num-1]);
  7433. if(i >= 0)
  7434. script_pushint(st,sd->status.inventory[i].refine);
  7435. else
  7436. script_pushint(st,0);
  7437. return SCRIPT_CMD_SUCCESS;
  7438. }
  7439. /*==========================================
  7440. * Get the weapon level value at pos
  7441. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7442. * return (npc)
  7443. * x : weapon level
  7444. * 0 : false
  7445. * getequipweaponlv(<equipment slot>{,<char_id>})
  7446. *------------------------------------------*/
  7447. BUILDIN_FUNC(getequipweaponlv)
  7448. {
  7449. int i = -1,num;
  7450. TBL_PC *sd;
  7451. num = script_getnum(st,2);
  7452. if (!script_charid2sd(3, sd)) {
  7453. script_pushint(st,0);
  7454. return SCRIPT_CMD_FAILURE;
  7455. }
  7456. if (num > 0 && num <= ARRAYLENGTH(equip))
  7457. i=pc_checkequip(sd,equip[num-1]);
  7458. if(i >= 0 && sd->inventory_data[i])
  7459. script_pushint(st,sd->inventory_data[i]->wlv);
  7460. else
  7461. script_pushint(st,0);
  7462. return SCRIPT_CMD_SUCCESS;
  7463. }
  7464. /*==========================================
  7465. * Get the item refine chance (from refine.txt) for item at pos
  7466. * return (npc)
  7467. * x : refine chance
  7468. * 0 : false (max refine level or unequip..)
  7469. * getequippercentrefinery(<equipment slot>{,<char_id>})
  7470. *------------------------------------------*/
  7471. BUILDIN_FUNC(getequippercentrefinery)
  7472. {
  7473. int i = -1,num;
  7474. TBL_PC *sd;
  7475. num = script_getnum(st,2);
  7476. if (!script_charid2sd(3, sd)) {
  7477. script_pushint(st,0);
  7478. return SCRIPT_CMD_FAILURE;
  7479. }
  7480. if (num > 0 && num <= ARRAYLENGTH(equip))
  7481. i = pc_checkequip(sd,equip[num-1]);
  7482. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  7483. script_pushint(st,status_get_refine_chance((enum refine_type)itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  7484. else
  7485. script_pushint(st,0);
  7486. return SCRIPT_CMD_SUCCESS;
  7487. }
  7488. /*==========================================
  7489. * Refine +1 item at pos and log and display refine
  7490. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7491. *------------------------------------------*/
  7492. BUILDIN_FUNC(successrefitem) {
  7493. short i = -1, up = 1;
  7494. int pos;
  7495. TBL_PC *sd;
  7496. pos = script_getnum(st,2);
  7497. if (!script_charid2sd(4, sd)) {
  7498. script_pushint(st, -1);
  7499. return SCRIPT_CMD_FAILURE;
  7500. }
  7501. if (script_hasdata(st, 3))
  7502. up = script_getnum(st, 3);
  7503. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7504. i = pc_checkequip(sd,equip[pos-1]);
  7505. if (i >= 0) {
  7506. unsigned int ep = sd->status.inventory[i].equip;
  7507. //Logs items, got from (N)PC scripts [Lupus]
  7508. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7509. if (sd->status.inventory[i].refine >= MAX_REFINE) {
  7510. script_pushint(st, MAX_REFINE);
  7511. return SCRIPT_CMD_SUCCESS;
  7512. }
  7513. sd->status.inventory[i].refine += up;
  7514. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7515. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7516. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  7517. clif_delitem(sd,i,1,3);
  7518. //Logs items, got from (N)PC scripts [Lupus]
  7519. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7520. clif_additem(sd,i,1,0);
  7521. pc_equipitem(sd,i,ep);
  7522. clif_misceffect(&sd->bl,3);
  7523. if (sd->status.inventory[i].refine == MAX_REFINE &&
  7524. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  7525. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3]))
  7526. { // Fame point system [DracoRPG]
  7527. switch (sd->inventory_data[i]->wlv){
  7528. case 1:
  7529. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7530. break;
  7531. case 2:
  7532. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7533. break;
  7534. case 3:
  7535. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7536. break;
  7537. }
  7538. }
  7539. script_pushint(st, sd->status.inventory[i].refine);
  7540. return SCRIPT_CMD_SUCCESS;
  7541. }
  7542. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7543. script_pushint(st, -1);
  7544. return SCRIPT_CMD_FAILURE;
  7545. }
  7546. /*==========================================
  7547. * Show a failed Refine +1 attempt
  7548. * failedrefitem <equipment slot>{,<char_id>}
  7549. *------------------------------------------*/
  7550. BUILDIN_FUNC(failedrefitem) {
  7551. short i = -1;
  7552. int pos;
  7553. TBL_PC *sd;
  7554. pos = script_getnum(st,2);
  7555. if (!script_charid2sd(3, sd)) {
  7556. script_pushint(st, 0);
  7557. return SCRIPT_CMD_FAILURE;
  7558. }
  7559. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7560. i = pc_checkequip(sd,equip[pos-1]);
  7561. if (i >= 0) {
  7562. sd->status.inventory[i].refine = 0;
  7563. pc_unequipitem(sd,i,3); //recalculate bonus
  7564. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  7565. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7566. clif_misceffect(&sd->bl,2); // display failure effect
  7567. script_pushint(st, 1);
  7568. return SCRIPT_CMD_SUCCESS;
  7569. }
  7570. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7571. script_pushint(st, 0);
  7572. return SCRIPT_CMD_FAILURE;
  7573. }
  7574. /*==========================================
  7575. * Downgrades an Equipment Part by -1 . [Masao]
  7576. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7577. *------------------------------------------*/
  7578. BUILDIN_FUNC(downrefitem) {
  7579. short i = -1, down = 1;
  7580. int pos;
  7581. TBL_PC *sd;
  7582. if (!script_charid2sd(4, sd)) {
  7583. script_pushint(st, -1);
  7584. return SCRIPT_CMD_FAILURE;
  7585. }
  7586. pos = script_getnum(st,2);
  7587. if (script_hasdata(st, 3))
  7588. down = script_getnum(st, 3);
  7589. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7590. i = pc_checkequip(sd,equip[pos-1]);
  7591. if (i >= 0) {
  7592. unsigned int ep = sd->status.inventory[i].equip;
  7593. //Logs items, got from (N)PC scripts [Lupus]
  7594. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7595. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7596. sd->status.inventory[i].refine -= down;
  7597. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7598. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine);
  7599. clif_delitem(sd,i,1,3);
  7600. //Logs items, got from (N)PC scripts [Lupus]
  7601. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7602. clif_additem(sd,i,1,0);
  7603. pc_equipitem(sd,i,ep);
  7604. clif_misceffect(&sd->bl,2);
  7605. script_pushint(st, sd->status.inventory[i].refine);
  7606. return SCRIPT_CMD_SUCCESS;
  7607. }
  7608. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7609. script_pushint(st, -1);
  7610. return SCRIPT_CMD_FAILURE;
  7611. }
  7612. /**
  7613. * Delete the item equipped at pos.
  7614. * delequip <equipment slot>{,<char_id>};
  7615. **/
  7616. BUILDIN_FUNC(delequip) {
  7617. short i = -1;
  7618. int pos;
  7619. int8 ret;
  7620. TBL_PC *sd;
  7621. pos = script_getnum(st,2);
  7622. if (!script_charid2sd(3,sd)) {
  7623. st->state = END;
  7624. return SCRIPT_CMD_FAILURE;
  7625. }
  7626. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7627. i = pc_checkequip(sd,equip[pos-1]);
  7628. if (i >= 0) {
  7629. pc_unequipitem(sd,i,3); //recalculate bonus
  7630. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7631. }
  7632. else {
  7633. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7634. st->state = END;
  7635. return SCRIPT_CMD_FAILURE;
  7636. }
  7637. script_pushint(st,ret);
  7638. return SCRIPT_CMD_SUCCESS;
  7639. }
  7640. /**
  7641. * Break the item equipped at pos.
  7642. * breakequip <equipment slot>{,<char_id>};
  7643. **/
  7644. BUILDIN_FUNC(breakequip) {
  7645. short i = -1;
  7646. int pos;
  7647. TBL_PC *sd;
  7648. pos = script_getnum(st,2);
  7649. if (!script_charid2sd(3,sd))
  7650. return SCRIPT_CMD_FAILURE;
  7651. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7652. i = pc_checkequip(sd,equip[pos-1]);
  7653. if (i >= 0) {
  7654. sd->status.inventory[i].attribute = 1;
  7655. pc_unequipitem(sd,i,3);
  7656. clif_equiplist(sd);
  7657. script_pushint(st,1);
  7658. return SCRIPT_CMD_SUCCESS;
  7659. }
  7660. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7661. script_pushint(st,0);
  7662. return SCRIPT_CMD_FAILURE;
  7663. }
  7664. /**
  7665. * statusup <stat>{,<char_id>};
  7666. **/
  7667. BUILDIN_FUNC(statusup)
  7668. {
  7669. int type;
  7670. TBL_PC *sd;
  7671. type = script_getnum(st,2);
  7672. if (!script_charid2sd(3, sd))
  7673. return SCRIPT_CMD_FAILURE;
  7674. pc_statusup(sd, type, 1);
  7675. return SCRIPT_CMD_SUCCESS;
  7676. }
  7677. /**
  7678. * statusup2 <stat>,<amount>{,<char_id>};
  7679. **/
  7680. BUILDIN_FUNC(statusup2)
  7681. {
  7682. int type,val;
  7683. TBL_PC *sd;
  7684. type=script_getnum(st,2);
  7685. val=script_getnum(st,3);
  7686. if (!script_charid2sd(4, sd))
  7687. return SCRIPT_CMD_FAILURE;
  7688. pc_statusup2(sd,type,val);
  7689. return SCRIPT_CMD_SUCCESS;
  7690. }
  7691. /// See 'doc/item_bonus.txt'
  7692. ///
  7693. /// bonus <bonus type>,<val1>;
  7694. /// bonus2 <bonus type>,<val1>,<val2>;
  7695. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7696. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7697. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7698. BUILDIN_FUNC(bonus)
  7699. {
  7700. int type;
  7701. int val1;
  7702. int val2 = 0;
  7703. int val3 = 0;
  7704. int val4 = 0;
  7705. int val5 = 0;
  7706. TBL_PC* sd;
  7707. struct script_data *data;
  7708. sd = script_rid2sd(st);
  7709. if( sd == NULL )
  7710. return 0; // no player attached
  7711. type = script_getnum(st,2);
  7712. switch( type ) {
  7713. case SP_AUTOSPELL:
  7714. case SP_AUTOSPELL_WHENHIT:
  7715. case SP_AUTOSPELL_ONSKILL:
  7716. case SP_SKILL_ATK:
  7717. case SP_SKILL_HEAL:
  7718. case SP_SKILL_HEAL2:
  7719. case SP_ADD_SKILL_BLOW:
  7720. case SP_CASTRATE:
  7721. case SP_ADDEFF_ONSKILL:
  7722. case SP_SKILL_USE_SP_RATE:
  7723. case SP_SKILL_COOLDOWN:
  7724. case SP_SKILL_FIXEDCAST:
  7725. case SP_SKILL_VARIABLECAST:
  7726. case SP_VARCASTRATE:
  7727. case SP_FIXCASTRATE:
  7728. case SP_SKILL_USE_SP:
  7729. case SP_SUB_SKILL:
  7730. // these bonuses support skill names
  7731. data = script_getdata(st, 3);
  7732. get_val(st, data); // Convert into value in case of a variable
  7733. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7734. break;
  7735. default:
  7736. val1 = script_getnum(st,3);
  7737. break;
  7738. }
  7739. switch( script_lastdata(st)-2 ) {
  7740. case 1:
  7741. pc_bonus(sd, type, val1);
  7742. break;
  7743. case 2:
  7744. val2 = script_getnum(st,4);
  7745. pc_bonus2(sd, type, val1, val2);
  7746. break;
  7747. case 3:
  7748. val2 = script_getnum(st,4);
  7749. val3 = script_getnum(st,5);
  7750. pc_bonus3(sd, type, val1, val2, val3);
  7751. break;
  7752. case 4:
  7753. data = script_getdata(st, 4);
  7754. get_val(st, data); // Convert into value in case of a variable
  7755. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7756. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7757. else
  7758. val2 = script_getnum(st,4);
  7759. val3 = script_getnum(st,5);
  7760. val4 = script_getnum(st,6);
  7761. pc_bonus4(sd, type, val1, val2, val3, val4);
  7762. break;
  7763. case 5:
  7764. data = script_getdata(st, 4);
  7765. get_val(st, data); // Convert into value in case of a variable
  7766. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7767. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7768. else
  7769. val2 = script_getnum(st,4);
  7770. val3 = script_getnum(st,5);
  7771. val4 = script_getnum(st,6);
  7772. val5 = script_getnum(st,7);
  7773. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7774. break;
  7775. default:
  7776. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7777. break;
  7778. }
  7779. return SCRIPT_CMD_SUCCESS;
  7780. }
  7781. BUILDIN_FUNC(autobonus)
  7782. {
  7783. unsigned int dur, pos;
  7784. short rate;
  7785. short atk_type = 0;
  7786. TBL_PC* sd;
  7787. const char *bonus_script, *other_script = NULL;
  7788. sd = script_rid2sd(st);
  7789. if( sd == NULL )
  7790. return 0; // no player attached
  7791. if (current_equip_combo_pos)
  7792. pos = current_equip_combo_pos;
  7793. else
  7794. pos = sd->status.inventory[current_equip_item_index].equip;
  7795. if((sd->state.autobonus&pos) == pos)
  7796. return 0;
  7797. rate = script_getnum(st,3);
  7798. dur = script_getnum(st,4);
  7799. bonus_script = script_getstr(st,2);
  7800. if( !rate || !dur || !pos || !bonus_script )
  7801. return 0;
  7802. if( script_hasdata(st,5) )
  7803. atk_type = script_getnum(st,5);
  7804. if( script_hasdata(st,6) )
  7805. other_script = script_getstr(st,6);
  7806. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7807. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7808. {
  7809. script_add_autobonus(bonus_script);
  7810. if( other_script )
  7811. script_add_autobonus(other_script);
  7812. }
  7813. return SCRIPT_CMD_SUCCESS;
  7814. }
  7815. BUILDIN_FUNC(autobonus2)
  7816. {
  7817. unsigned int dur, pos;
  7818. short rate;
  7819. short atk_type = 0;
  7820. TBL_PC* sd;
  7821. const char *bonus_script, *other_script = NULL;
  7822. sd = script_rid2sd(st);
  7823. if( sd == NULL )
  7824. return 0; // no player attached
  7825. if (current_equip_combo_pos)
  7826. pos = current_equip_combo_pos;
  7827. else
  7828. pos = sd->status.inventory[current_equip_item_index].equip;
  7829. if((sd->state.autobonus&pos) == pos)
  7830. return 0;
  7831. rate = script_getnum(st,3);
  7832. dur = script_getnum(st,4);
  7833. bonus_script = script_getstr(st,2);
  7834. if( !rate || !dur || !pos || !bonus_script )
  7835. return 0;
  7836. if( script_hasdata(st,5) )
  7837. atk_type = script_getnum(st,5);
  7838. if( script_hasdata(st,6) )
  7839. other_script = script_getstr(st,6);
  7840. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7841. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7842. {
  7843. script_add_autobonus(bonus_script);
  7844. if( other_script )
  7845. script_add_autobonus(other_script);
  7846. }
  7847. return SCRIPT_CMD_SUCCESS;
  7848. }
  7849. BUILDIN_FUNC(autobonus3)
  7850. {
  7851. unsigned int dur, pos;
  7852. short rate,atk_type;
  7853. TBL_PC* sd;
  7854. const char *bonus_script, *other_script = NULL;
  7855. struct script_data *data;
  7856. sd = script_rid2sd(st);
  7857. if( sd == NULL )
  7858. return 0; // no player attached
  7859. if (current_equip_combo_pos)
  7860. pos = current_equip_combo_pos;
  7861. else
  7862. pos = sd->status.inventory[current_equip_item_index].equip;
  7863. if((sd->state.autobonus&pos) == pos)
  7864. return 0;
  7865. rate = script_getnum(st,3);
  7866. dur = script_getnum(st,4);
  7867. data = script_getdata(st, 5);
  7868. get_val(st, data); // Convert into value in case of a variable
  7869. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7870. bonus_script = script_getstr(st,2);
  7871. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  7872. return 0;
  7873. if( script_hasdata(st,6) )
  7874. other_script = script_getstr(st,6);
  7875. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7876. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  7877. {
  7878. script_add_autobonus(bonus_script);
  7879. if( other_script )
  7880. script_add_autobonus(other_script);
  7881. }
  7882. return SCRIPT_CMD_SUCCESS;
  7883. }
  7884. /// Changes the level of a player skill.
  7885. /// <flag> defaults to 1
  7886. /// <flag>=0 : set the level of the skill
  7887. /// <flag>=1 : set the temporary level of the skill
  7888. /// <flag>=2 : add to the level of the skill
  7889. ///
  7890. /// skill <skill id>,<level>,<flag>
  7891. /// skill <skill id>,<level>
  7892. /// skill "<skill name>",<level>,<flag>
  7893. /// skill "<skill name>",<level>
  7894. BUILDIN_FUNC(skill)
  7895. {
  7896. int id;
  7897. int level;
  7898. int flag = ADDSKILL_TEMP;
  7899. TBL_PC* sd;
  7900. struct script_data *data;
  7901. const char* command = script_getfuncname(st);
  7902. sd = script_rid2sd(st);
  7903. if( sd == NULL )
  7904. return 0;// no player attached, report source
  7905. if (strcmpi(command, "addtoskill") == 0)
  7906. flag = ADDSKILL_TEMP_ADDLEVEL;
  7907. data = script_getdata(st, 2);
  7908. get_val(st, data); // Convert into value in case of a variable
  7909. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7910. level = script_getnum(st,3);
  7911. if( script_hasdata(st,4) )
  7912. flag = script_getnum(st,4);
  7913. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  7914. return SCRIPT_CMD_SUCCESS;
  7915. }
  7916. /// Increases the level of a guild skill.
  7917. ///
  7918. /// guildskill <skill id>,<amount>;
  7919. /// guildskill "<skill name>",<amount>;
  7920. BUILDIN_FUNC(guildskill)
  7921. {
  7922. int id;
  7923. int level;
  7924. TBL_PC* sd;
  7925. int i;
  7926. struct script_data *data;
  7927. sd = script_rid2sd(st);
  7928. if( sd == NULL )
  7929. return 0;// no player attached, report source
  7930. data = script_getdata(st, 2);
  7931. get_val(st, data); // Convert into value in case of a variable
  7932. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7933. level = script_getnum(st,3);
  7934. for( i=0; i < level; i++ )
  7935. guild_skillup(sd, id);
  7936. return SCRIPT_CMD_SUCCESS;
  7937. }
  7938. /// Returns the level of the player skill.
  7939. ///
  7940. /// getskilllv(<skill id>) -> <level>
  7941. /// getskilllv("<skill name>") -> <level>
  7942. BUILDIN_FUNC(getskilllv)
  7943. {
  7944. int id;
  7945. TBL_PC* sd;
  7946. struct script_data *data;
  7947. sd = script_rid2sd(st);
  7948. if( sd == NULL )
  7949. return 0;// no player attached, report source
  7950. data = script_getdata(st, 2);
  7951. get_val(st, data); // Convert into value in case of a variable
  7952. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7953. script_pushint(st, pc_checkskill(sd,id));
  7954. return SCRIPT_CMD_SUCCESS;
  7955. }
  7956. /// Returns the level of the guild skill.
  7957. ///
  7958. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7959. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7960. BUILDIN_FUNC(getgdskilllv)
  7961. {
  7962. int guild_id;
  7963. uint16 skill_id;
  7964. struct guild* g;
  7965. struct script_data *data;
  7966. guild_id = script_getnum(st,2);
  7967. data = script_getdata(st, 3);
  7968. get_val(st, data); // Convert into value in case of a variable
  7969. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7970. g = guild_search(guild_id);
  7971. if( g == NULL )
  7972. script_pushint(st, -1);
  7973. else
  7974. script_pushint(st, guild_checkskill(g,skill_id));
  7975. return SCRIPT_CMD_SUCCESS;
  7976. }
  7977. /// Returns the 'basic_skill_check' setting.
  7978. /// This config determines if the server checks the skill level of NV_BASIC
  7979. /// before allowing the basic actions.
  7980. ///
  7981. /// basicskillcheck() -> <bool>
  7982. BUILDIN_FUNC(basicskillcheck)
  7983. {
  7984. script_pushint(st, battle_config.basic_skill_check);
  7985. return SCRIPT_CMD_SUCCESS;
  7986. }
  7987. /// Returns the GM level of the player.
  7988. ///
  7989. /// getgmlevel({<char_id>}) -> <level>
  7990. BUILDIN_FUNC(getgmlevel)
  7991. {
  7992. TBL_PC* sd;
  7993. if (!script_charid2sd(2,sd))
  7994. return SCRIPT_CMD_FAILURE;
  7995. script_pushint(st, pc_get_group_level(sd));
  7996. return SCRIPT_CMD_SUCCESS;
  7997. }
  7998. /// Returns the group ID of the player.
  7999. ///
  8000. /// getgroupid({<char_id>}) -> <int>
  8001. BUILDIN_FUNC(getgroupid)
  8002. {
  8003. TBL_PC* sd;
  8004. if (!script_charid2sd(2,sd))
  8005. return SCRIPT_CMD_FAILURE;
  8006. script_pushint(st, pc_get_group_id(sd));
  8007. return SCRIPT_CMD_SUCCESS;
  8008. }
  8009. /// Terminates the execution of this script instance.
  8010. ///
  8011. /// end
  8012. BUILDIN_FUNC(end)
  8013. {
  8014. TBL_PC* sd;
  8015. sd = map_id2sd(st->rid);
  8016. st->state = END;
  8017. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  8018. int i;
  8019. for(i = 0; i < st->stack->sp; i++) {
  8020. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  8021. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  8022. st->script = ri->script;
  8023. break;
  8024. }
  8025. }
  8026. }
  8027. if( st->mes_active )
  8028. st->mes_active = 0;
  8029. if( sd )
  8030. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8031. return SCRIPT_CMD_SUCCESS;
  8032. }
  8033. /// Checks if the player has that effect state (option).
  8034. ///
  8035. /// checkoption(<option>{,<char_id>}) -> <bool>
  8036. BUILDIN_FUNC(checkoption)
  8037. {
  8038. int option;
  8039. TBL_PC* sd;
  8040. if (!script_charid2sd(3,sd))
  8041. return SCRIPT_CMD_FAILURE;
  8042. option = script_getnum(st,2);
  8043. if( sd->sc.option&option )
  8044. script_pushint(st, 1);
  8045. else
  8046. script_pushint(st, 0);
  8047. return SCRIPT_CMD_SUCCESS;
  8048. }
  8049. /// Checks if the player is in that body state (opt1).
  8050. ///
  8051. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8052. BUILDIN_FUNC(checkoption1)
  8053. {
  8054. int opt1;
  8055. TBL_PC* sd;
  8056. if (!script_charid2sd(3,sd))
  8057. return SCRIPT_CMD_FAILURE;
  8058. opt1 = script_getnum(st,2);
  8059. if( sd->sc.opt1 == opt1 )
  8060. script_pushint(st, 1);
  8061. else
  8062. script_pushint(st, 0);
  8063. return SCRIPT_CMD_SUCCESS;
  8064. }
  8065. /// Checks if the player has that health state (opt2).
  8066. ///
  8067. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8068. BUILDIN_FUNC(checkoption2)
  8069. {
  8070. int opt2;
  8071. TBL_PC* sd;
  8072. if (!script_charid2sd(3,sd))
  8073. return SCRIPT_CMD_FAILURE;
  8074. opt2 = script_getnum(st,2);
  8075. if( sd->sc.opt2&opt2 )
  8076. script_pushint(st, 1);
  8077. else
  8078. script_pushint(st, 0);
  8079. return SCRIPT_CMD_SUCCESS;
  8080. }
  8081. /// Changes the effect state (option) of the player.
  8082. /// <flag> defaults to 1
  8083. /// <flag>=0 : removes the option
  8084. /// <flag>=other : adds the option
  8085. ///
  8086. /// setoption <option>{,<flag>{,<char_id>}};
  8087. BUILDIN_FUNC(setoption)
  8088. {
  8089. int option;
  8090. int flag = 1;
  8091. TBL_PC* sd;
  8092. if (!script_charid2sd(4,sd))
  8093. return SCRIPT_CMD_FAILURE;
  8094. option = script_getnum(st,2);
  8095. if( script_hasdata(st,3) )
  8096. flag = script_getnum(st,3);
  8097. else if( !option ){// Request to remove everything.
  8098. flag = 0;
  8099. option = OPTION_FALCON|OPTION_RIDING;
  8100. #ifndef NEW_CARTS
  8101. option |= OPTION_CART;
  8102. #endif
  8103. }
  8104. if( flag ){// Add option
  8105. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8106. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8107. pc_setoption(sd, sd->sc.option|option);
  8108. } else// Remove option
  8109. pc_setoption(sd, sd->sc.option&~option);
  8110. return SCRIPT_CMD_SUCCESS;
  8111. }
  8112. /// Returns if the player has a cart.
  8113. ///
  8114. /// checkcart({char_id}) -> <bool>
  8115. ///
  8116. /// @author Valaris
  8117. BUILDIN_FUNC(checkcart)
  8118. {
  8119. TBL_PC* sd;
  8120. if (!script_charid2sd(2,sd))
  8121. return SCRIPT_CMD_FAILURE;
  8122. if( pc_iscarton(sd) )
  8123. script_pushint(st, 1);
  8124. else
  8125. script_pushint(st, 0);
  8126. return SCRIPT_CMD_SUCCESS;
  8127. }
  8128. /// Sets the cart of the player.
  8129. /// <type> defaults to 1
  8130. /// <type>=0 : removes the cart
  8131. /// <type>=1 : Normal cart
  8132. /// <type>=2 : Wooden cart
  8133. /// <type>=3 : Covered cart with flowers and ferns
  8134. /// <type>=4 : Wooden cart with a Panda doll on the back
  8135. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8136. ///
  8137. /// setcart {<type>{,<char_id>}};
  8138. BUILDIN_FUNC(setcart)
  8139. {
  8140. int type = 1;
  8141. TBL_PC* sd;
  8142. if (!script_charid2sd(3,sd))
  8143. return SCRIPT_CMD_FAILURE;
  8144. if( script_hasdata(st,2) )
  8145. type = script_getnum(st,2);
  8146. pc_setcart(sd, type);
  8147. return SCRIPT_CMD_SUCCESS;
  8148. }
  8149. /// Returns if the player has a falcon.
  8150. ///
  8151. /// checkfalcon({char_id}) -> <bool>
  8152. ///
  8153. /// @author Valaris
  8154. BUILDIN_FUNC(checkfalcon)
  8155. {
  8156. TBL_PC* sd;
  8157. if (!script_charid2sd(2,sd))
  8158. return SCRIPT_CMD_FAILURE;
  8159. if( pc_isfalcon(sd) )
  8160. script_pushint(st, 1);
  8161. else
  8162. script_pushint(st, 0);
  8163. return SCRIPT_CMD_SUCCESS;
  8164. }
  8165. /// Sets if the player has a falcon or not.
  8166. /// <flag> defaults to 1
  8167. ///
  8168. /// setfalcon {<flag>{,<char_id>}};
  8169. BUILDIN_FUNC(setfalcon)
  8170. {
  8171. int flag = 1;
  8172. TBL_PC* sd;
  8173. if (!script_charid2sd(3,sd))
  8174. return SCRIPT_CMD_FAILURE;
  8175. if( script_hasdata(st,2) )
  8176. flag = script_getnum(st,2);
  8177. pc_setfalcon(sd, flag);
  8178. return SCRIPT_CMD_SUCCESS;
  8179. }
  8180. /// Returns if the player is riding.
  8181. ///
  8182. /// checkriding({char_id}) -> <bool>
  8183. ///
  8184. /// @author Valaris
  8185. BUILDIN_FUNC(checkriding)
  8186. {
  8187. TBL_PC* sd;
  8188. if (!script_charid2sd(2,sd))
  8189. return SCRIPT_CMD_FAILURE;
  8190. if( pc_isriding(sd) )
  8191. script_pushint(st, 1);
  8192. else
  8193. script_pushint(st, 0);
  8194. return SCRIPT_CMD_SUCCESS;
  8195. }
  8196. /// Sets if the player is riding.
  8197. /// <flag> defaults to 1
  8198. ///
  8199. /// setriding {<flag>{,<char_id>}};
  8200. BUILDIN_FUNC(setriding)
  8201. {
  8202. int flag = 1;
  8203. TBL_PC* sd;
  8204. if (!script_charid2sd(3,sd))
  8205. return SCRIPT_CMD_FAILURE;
  8206. if( script_hasdata(st,2) )
  8207. flag = script_getnum(st,2);
  8208. pc_setriding(sd, flag);
  8209. return SCRIPT_CMD_SUCCESS;
  8210. }
  8211. /// Returns if the player has a warg.
  8212. ///
  8213. /// checkwug({char_id}) -> <bool>
  8214. ///
  8215. BUILDIN_FUNC(checkwug)
  8216. {
  8217. TBL_PC* sd;
  8218. if (!script_charid2sd(2,sd))
  8219. return SCRIPT_CMD_FAILURE;
  8220. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8221. script_pushint(st, 1);
  8222. else
  8223. script_pushint(st, 0);
  8224. return SCRIPT_CMD_SUCCESS;
  8225. }
  8226. /// Returns if the player is wearing MADO Gear.
  8227. ///
  8228. /// checkmadogear({<char_id>}) -> <bool>
  8229. ///
  8230. BUILDIN_FUNC(checkmadogear)
  8231. {
  8232. TBL_PC* sd;
  8233. if (!script_charid2sd(2,sd))
  8234. return SCRIPT_CMD_FAILURE;
  8235. if( pc_ismadogear(sd) )
  8236. script_pushint(st, 1);
  8237. else
  8238. script_pushint(st, 0);
  8239. return SCRIPT_CMD_SUCCESS;
  8240. }
  8241. /// Sets if the player is riding MADO Gear.
  8242. /// <flag> defaults to 1
  8243. ///
  8244. /// setmadogear {<flag>{,<char_id>}};
  8245. BUILDIN_FUNC(setmadogear)
  8246. {
  8247. int flag = 1;
  8248. TBL_PC* sd;
  8249. if (!script_charid2sd(3,sd))
  8250. return SCRIPT_CMD_FAILURE;
  8251. if( script_hasdata(st,2) )
  8252. flag = script_getnum(st,2);
  8253. pc_setmadogear(sd, flag);
  8254. return SCRIPT_CMD_SUCCESS;
  8255. }
  8256. /// Sets the save point of the player.
  8257. ///
  8258. /// save "<map name>",<x>,<y>{,<char_id>}
  8259. /// savepoint "<map name>",<x>,<y>{,<char_id>}
  8260. BUILDIN_FUNC(savepoint)
  8261. {
  8262. int x;
  8263. int y;
  8264. short map_idx;
  8265. const char* str;
  8266. TBL_PC* sd;
  8267. if (!script_charid2sd(5,sd))
  8268. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8269. str = script_getstr(st, 2);
  8270. x = script_getnum(st,3);
  8271. y = script_getnum(st,4);
  8272. map_idx = mapindex_name2id(str);
  8273. if( map_idx )
  8274. pc_setsavepoint(sd, map_idx, x, y);
  8275. return SCRIPT_CMD_SUCCESS;
  8276. }
  8277. /*==========================================
  8278. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  8279. *------------------------------------------*/
  8280. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8281. {
  8282. int type;
  8283. time_t timer;
  8284. struct tm *t;
  8285. type=script_getnum(st,2);
  8286. switch(type){
  8287. case 2:
  8288. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8289. // from the system clock.)
  8290. script_pushint(st,(int)time(NULL));
  8291. break;
  8292. case 1:
  8293. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8294. time(&timer);
  8295. t=localtime(&timer);
  8296. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8297. break;
  8298. case 0:
  8299. default:
  8300. //type 0:(System Ticks)
  8301. script_pushint(st,gettick());
  8302. break;
  8303. }
  8304. return SCRIPT_CMD_SUCCESS;
  8305. }
  8306. /*==========================================
  8307. * GetTime(Type);
  8308. * 1: Sec 2: Min 3: Hour
  8309. * 4: WeekDay 5: MonthDay 6: Month
  8310. * 7: Year
  8311. *------------------------------------------*/
  8312. BUILDIN_FUNC(gettime) /* Asgard Version */
  8313. {
  8314. int type;
  8315. time_t timer;
  8316. struct tm *t;
  8317. type=script_getnum(st,2);
  8318. time(&timer);
  8319. t=localtime(&timer);
  8320. switch(type){
  8321. case 1://Sec(0~59)
  8322. script_pushint(st,t->tm_sec);
  8323. break;
  8324. case 2://Min(0~59)
  8325. script_pushint(st,t->tm_min);
  8326. break;
  8327. case 3://Hour(0~23)
  8328. script_pushint(st,t->tm_hour);
  8329. break;
  8330. case 4://WeekDay(0~6)
  8331. script_pushint(st,t->tm_wday);
  8332. break;
  8333. case 5://MonthDay(01~31)
  8334. script_pushint(st,t->tm_mday);
  8335. break;
  8336. case 6://Month(01~12)
  8337. script_pushint(st,t->tm_mon+1);
  8338. break;
  8339. case 7://Year(20xx)
  8340. script_pushint(st,t->tm_year+1900);
  8341. break;
  8342. case 8://Year Day(01~366)
  8343. script_pushint(st,t->tm_yday+1);
  8344. break;
  8345. default://(format error)
  8346. script_pushint(st,-1);
  8347. break;
  8348. }
  8349. return SCRIPT_CMD_SUCCESS;
  8350. }
  8351. /*==========================================
  8352. * GetTimeStr("TimeFMT", Length);
  8353. *------------------------------------------*/
  8354. BUILDIN_FUNC(gettimestr)
  8355. {
  8356. char *tmpstr;
  8357. const char *fmtstr;
  8358. int maxlen;
  8359. time_t now = time(NULL);
  8360. fmtstr=script_getstr(st,2);
  8361. maxlen=script_getnum(st,3);
  8362. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8363. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8364. tmpstr[maxlen]='\0';
  8365. script_pushstr(st,tmpstr);
  8366. return SCRIPT_CMD_SUCCESS;
  8367. }
  8368. /*==========================================
  8369. * Open player storage
  8370. *------------------------------------------*/
  8371. BUILDIN_FUNC(openstorage)
  8372. {
  8373. TBL_PC* sd;
  8374. sd = script_rid2sd(st);
  8375. if( sd == NULL )
  8376. return 0;
  8377. storage_storageopen(sd);
  8378. return SCRIPT_CMD_SUCCESS;
  8379. }
  8380. BUILDIN_FUNC(guildopenstorage)
  8381. {
  8382. TBL_PC* sd;
  8383. int ret;
  8384. sd = script_rid2sd(st);
  8385. if( sd == NULL )
  8386. return 0;
  8387. ret = gstorage_storageopen(sd);
  8388. script_pushint(st,ret);
  8389. return SCRIPT_CMD_SUCCESS;
  8390. }
  8391. /*==========================================
  8392. * Make player use a skill trought item usage
  8393. *------------------------------------------*/
  8394. /// itemskill <skill id>,<level>
  8395. /// itemskill "<skill name>",<level>
  8396. BUILDIN_FUNC(itemskill)
  8397. {
  8398. int id;
  8399. int lv;
  8400. TBL_PC* sd;
  8401. struct script_data *data;
  8402. sd = script_rid2sd(st);
  8403. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8404. return 0;
  8405. data = script_getdata(st, 2);
  8406. get_val(st, data); // Convert into value in case of a variable
  8407. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8408. lv = script_getnum(st,3);
  8409. sd->skillitem=id;
  8410. sd->skillitemlv=lv;
  8411. clif_item_skill(sd,id,lv);
  8412. return SCRIPT_CMD_SUCCESS;
  8413. }
  8414. /*==========================================
  8415. * Attempt to create an item
  8416. *------------------------------------------*/
  8417. BUILDIN_FUNC(produce)
  8418. {
  8419. int trigger;
  8420. TBL_PC* sd;
  8421. sd = script_rid2sd(st);
  8422. if( sd == NULL )
  8423. return 0;
  8424. trigger=script_getnum(st,2);
  8425. clif_skill_produce_mix_list(sd, -1, trigger);
  8426. return SCRIPT_CMD_SUCCESS;
  8427. }
  8428. /*==========================================
  8429. *
  8430. *------------------------------------------*/
  8431. BUILDIN_FUNC(cooking)
  8432. {
  8433. int trigger;
  8434. TBL_PC* sd;
  8435. sd = script_rid2sd(st);
  8436. if( sd == NULL )
  8437. return 0;
  8438. trigger=script_getnum(st,2);
  8439. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8440. return SCRIPT_CMD_SUCCESS;
  8441. }
  8442. /*==========================================
  8443. * Create a pet
  8444. *------------------------------------------*/
  8445. BUILDIN_FUNC(makepet)
  8446. {
  8447. TBL_PC* sd;
  8448. int id,pet_id;
  8449. id=script_getnum(st,2);
  8450. sd = script_rid2sd(st);
  8451. if( sd == NULL )
  8452. return 0;
  8453. pet_id = search_petDB_index(id, PET_CLASS);
  8454. if (pet_id < 0)
  8455. pet_id = search_petDB_index(id, PET_EGG);
  8456. if (pet_id >= 0 && sd) {
  8457. sd->catch_target_class = pet_db[pet_id].class_;
  8458. intif_create_pet(
  8459. sd->status.account_id, sd->status.char_id,
  8460. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8461. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8462. 100, 0, 1, pet_db[pet_id].jname);
  8463. }
  8464. return SCRIPT_CMD_SUCCESS;
  8465. }
  8466. /**
  8467. * Give player exp base,job * quest_exp_rate/100
  8468. * getexp <base xp>,<job xp>{,<char_id>};
  8469. **/
  8470. BUILDIN_FUNC(getexp)
  8471. {
  8472. TBL_PC* sd;
  8473. int base=0,job=0;
  8474. double bonus;
  8475. if (!script_charid2sd(4,sd))
  8476. return SCRIPT_CMD_FAILURE;
  8477. base=script_getnum(st,2);
  8478. job =script_getnum(st,3);
  8479. if(base<0 || job<0)
  8480. return 0;
  8481. // bonus for npc-given exp
  8482. bonus = battle_config.quest_exp_rate / 100.;
  8483. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8484. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8485. pc_gainexp(sd, NULL, base, job, true);
  8486. return SCRIPT_CMD_SUCCESS;
  8487. }
  8488. /*==========================================
  8489. * Gain guild exp [Celest]
  8490. *------------------------------------------*/
  8491. BUILDIN_FUNC(guildgetexp)
  8492. {
  8493. TBL_PC* sd;
  8494. int exp;
  8495. sd = script_rid2sd(st);
  8496. if( sd == NULL )
  8497. return 0;
  8498. exp = script_getnum(st,2);
  8499. if(exp < 0)
  8500. return 0;
  8501. if(sd && sd->status.guild_id > 0)
  8502. guild_getexp (sd, exp);
  8503. return SCRIPT_CMD_SUCCESS;
  8504. }
  8505. /*==========================================
  8506. * Changes the guild master of a guild [Skotlex]
  8507. *------------------------------------------*/
  8508. BUILDIN_FUNC(guildchangegm)
  8509. {
  8510. TBL_PC *sd;
  8511. int guild_id;
  8512. const char *name;
  8513. guild_id = script_getnum(st,2);
  8514. name = script_getstr(st,3);
  8515. sd=map_nick2sd(name);
  8516. if (!sd)
  8517. script_pushint(st,0);
  8518. else
  8519. script_pushint(st,guild_gm_change(guild_id, sd));
  8520. return SCRIPT_CMD_SUCCESS;
  8521. }
  8522. /*==========================================
  8523. * Spawn a monster:
  8524. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8525. *------------------------------------------*/
  8526. BUILDIN_FUNC(monster)
  8527. {
  8528. const char* mapn = script_getstr(st,2);
  8529. int x = script_getnum(st,3);
  8530. int y = script_getnum(st,4);
  8531. const char* str = script_getstr(st,5);
  8532. int class_ = script_getnum(st,6);
  8533. int amount = script_getnum(st,7);
  8534. const char* event = "";
  8535. unsigned int size = SZ_SMALL;
  8536. unsigned int ai = AI_NONE;
  8537. struct map_session_data* sd;
  8538. int16 m;
  8539. int i;
  8540. if (script_hasdata(st, 8)) {
  8541. event = script_getstr(st, 8);
  8542. check_event(st, event);
  8543. }
  8544. if (script_hasdata(st, 9)) {
  8545. size = script_getnum(st, 9);
  8546. if (size > 3) {
  8547. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8548. return 1;
  8549. }
  8550. }
  8551. if (script_hasdata(st, 10)) {
  8552. ai = script_getnum(st, 10);
  8553. if (ai >= AI_MAX) {
  8554. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8555. return 1;
  8556. }
  8557. }
  8558. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8559. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8560. return 1;
  8561. }
  8562. sd = map_id2sd(st->rid);
  8563. if (sd && strcmp(mapn, "this") == 0)
  8564. m = sd->bl.m;
  8565. else
  8566. m = map_mapname2mapid(mapn);
  8567. for(i = 0; i < amount; i++) { //not optimised
  8568. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8569. if (mobid)
  8570. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8571. }
  8572. return SCRIPT_CMD_SUCCESS;
  8573. }
  8574. /*==========================================
  8575. * Request List of Monster Drops
  8576. *------------------------------------------*/
  8577. BUILDIN_FUNC(getmobdrops)
  8578. {
  8579. int class_ = script_getnum(st,2);
  8580. int i, j = 0;
  8581. struct mob_db *mob;
  8582. if( !mobdb_checkid(class_) )
  8583. {
  8584. script_pushint(st, 0);
  8585. return 0;
  8586. }
  8587. mob = mob_db(class_);
  8588. for( i = 0; i < MAX_MOB_DROP; i++ )
  8589. {
  8590. if( mob->dropitem[i].nameid < 1 )
  8591. continue;
  8592. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8593. continue;
  8594. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8595. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8596. j++;
  8597. }
  8598. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8599. script_pushint(st, 1);
  8600. return SCRIPT_CMD_SUCCESS;
  8601. }
  8602. /*==========================================
  8603. * Spawn a monster in a random location
  8604. * in x0,x1,y0,y1 area.
  8605. *------------------------------------------*/
  8606. BUILDIN_FUNC(areamonster)
  8607. {
  8608. const char* mapn = script_getstr(st,2);
  8609. int x0 = script_getnum(st,3);
  8610. int y0 = script_getnum(st,4);
  8611. int x1 = script_getnum(st,5);
  8612. int y1 = script_getnum(st,6);
  8613. const char* str = script_getstr(st,7);
  8614. int class_ = script_getnum(st,8);
  8615. int amount = script_getnum(st,9);
  8616. const char* event = "";
  8617. unsigned int size = SZ_SMALL;
  8618. unsigned int ai = AI_NONE;
  8619. struct map_session_data* sd;
  8620. int16 m;
  8621. int i;
  8622. if (script_hasdata(st,10)) {
  8623. event = script_getstr(st, 10);
  8624. check_event(st, event);
  8625. }
  8626. if (script_hasdata(st, 11)) {
  8627. size = script_getnum(st, 11);
  8628. if (size > 3) {
  8629. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8630. return 1;
  8631. }
  8632. }
  8633. if (script_hasdata(st, 12)) {
  8634. ai = script_getnum(st, 12);
  8635. if (ai >= AI_MAX) {
  8636. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8637. return 1;
  8638. }
  8639. }
  8640. sd = map_id2sd(st->rid);
  8641. if (sd && strcmp(mapn, "this") == 0)
  8642. m = sd->bl.m;
  8643. else
  8644. m = map_mapname2mapid(mapn);
  8645. for(i = 0; i < amount; i++) { //not optimised
  8646. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8647. if (mobid)
  8648. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8649. }
  8650. return SCRIPT_CMD_SUCCESS;
  8651. }
  8652. /*==========================================
  8653. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8654. *------------------------------------------*/
  8655. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8656. { //same fix but with killmonster instead - stripping events from mobs.
  8657. TBL_MOB* md = (TBL_MOB*)bl;
  8658. char *event=va_arg(ap,char *);
  8659. int allflag=va_arg(ap,int);
  8660. md->state.npc_killmonster = 1;
  8661. if(!allflag){
  8662. if(strcmp(event,md->npc_event)==0)
  8663. status_kill(bl);
  8664. }else{
  8665. if(!md->spawn)
  8666. status_kill(bl);
  8667. }
  8668. md->state.npc_killmonster = 0;
  8669. return 0;
  8670. }
  8671. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8672. {
  8673. TBL_MOB* md = (TBL_MOB*)bl;
  8674. char *event=va_arg(ap,char *);
  8675. int allflag=va_arg(ap,int);
  8676. if(!allflag){
  8677. if(strcmp(event,md->npc_event)==0)
  8678. status_kill(bl);
  8679. }else{
  8680. if(!md->spawn)
  8681. status_kill(bl);
  8682. }
  8683. return 0;
  8684. }
  8685. BUILDIN_FUNC(killmonster)
  8686. {
  8687. const char *mapname,*event;
  8688. int16 m,allflag=0;
  8689. mapname=script_getstr(st,2);
  8690. event=script_getstr(st,3);
  8691. if(strcmp(event,"All")==0)
  8692. allflag = 1;
  8693. else
  8694. check_event(st, event);
  8695. if( (m=map_mapname2mapid(mapname))<0 )
  8696. return 0;
  8697. if( script_hasdata(st,4) ) {
  8698. if ( script_getnum(st,4) == 1 ) {
  8699. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8700. return 0;
  8701. }
  8702. }
  8703. map_freeblock_lock();
  8704. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8705. map_freeblock_unlock();
  8706. return SCRIPT_CMD_SUCCESS;
  8707. }
  8708. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8709. { //Strips the event from the mob if it's killed the old method.
  8710. struct mob_data *md;
  8711. md = BL_CAST(BL_MOB, bl);
  8712. if (md->npc_event[0])
  8713. md->npc_event[0] = 0;
  8714. status_kill(bl);
  8715. return 0;
  8716. }
  8717. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8718. {
  8719. status_kill(bl);
  8720. return 0;
  8721. }
  8722. BUILDIN_FUNC(killmonsterall)
  8723. {
  8724. const char *mapname;
  8725. int16 m;
  8726. mapname=script_getstr(st,2);
  8727. if( (m = map_mapname2mapid(mapname))<0 )
  8728. return 0;
  8729. if( script_hasdata(st,3) ) {
  8730. if ( script_getnum(st,3) == 1 ) {
  8731. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8732. return 0;
  8733. }
  8734. }
  8735. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8736. return SCRIPT_CMD_SUCCESS;
  8737. }
  8738. /*==========================================
  8739. * Creates a clone of a player.
  8740. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8741. *------------------------------------------*/
  8742. BUILDIN_FUNC(clone)
  8743. {
  8744. TBL_PC *sd, *msd=NULL;
  8745. uint32 char_id;
  8746. int master_id=0,x,y, mode = 0, flag = 0, m;
  8747. unsigned int duration = 0;
  8748. const char *mapname,*event;
  8749. mapname=script_getstr(st,2);
  8750. x=script_getnum(st,3);
  8751. y=script_getnum(st,4);
  8752. event=script_getstr(st,5);
  8753. char_id=script_getnum(st,6);
  8754. if( script_hasdata(st,7) )
  8755. master_id=script_getnum(st,7);
  8756. if( script_hasdata(st,8) )
  8757. mode=script_getnum(st,8);
  8758. if( script_hasdata(st,9) )
  8759. flag=script_getnum(st,9);
  8760. if( script_hasdata(st,10) )
  8761. duration=script_getnum(st,10);
  8762. check_event(st, event);
  8763. m = map_mapname2mapid(mapname);
  8764. if (m < 0) return 0;
  8765. sd = map_charid2sd(char_id);
  8766. if (master_id) {
  8767. msd = map_charid2sd(master_id);
  8768. if (msd)
  8769. master_id = msd->bl.id;
  8770. else
  8771. master_id = 0;
  8772. }
  8773. if (sd) //Return ID of newly crafted clone.
  8774. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8775. else //Failed to create clone.
  8776. script_pushint(st,0);
  8777. return SCRIPT_CMD_SUCCESS;
  8778. }
  8779. /*==========================================
  8780. *------------------------------------------*/
  8781. BUILDIN_FUNC(doevent)
  8782. {
  8783. const char* event = script_getstr(st,2);
  8784. struct map_session_data* sd;
  8785. if( ( sd = script_rid2sd(st) ) == NULL )
  8786. {
  8787. return 0;
  8788. }
  8789. check_event(st, event);
  8790. npc_event(sd, event, 0);
  8791. return SCRIPT_CMD_SUCCESS;
  8792. }
  8793. /*==========================================
  8794. *------------------------------------------*/
  8795. BUILDIN_FUNC(donpcevent)
  8796. {
  8797. const char* event = script_getstr(st,2);
  8798. check_event(st, event);
  8799. if( !npc_event_do(event) ) {
  8800. struct npc_data * nd = map_id2nd(st->oid);
  8801. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8802. script_pushint(st, 0);
  8803. } else
  8804. script_pushint(st, 1);
  8805. return SCRIPT_CMD_SUCCESS;
  8806. }
  8807. /// for Aegis compatibility
  8808. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8809. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8810. {
  8811. const char* npc = script_getstr(st,2);
  8812. const char* command = script_getstr(st,3);
  8813. char event[EVENT_NAME_LENGTH];
  8814. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8815. check_event(st, event);
  8816. npc_event_do(event);
  8817. return SCRIPT_CMD_SUCCESS;
  8818. }
  8819. /*==========================================
  8820. *------------------------------------------*/
  8821. BUILDIN_FUNC(addtimer)
  8822. {
  8823. int tick = script_getnum(st,2);
  8824. const char* event = script_getstr(st, 3);
  8825. TBL_PC* sd;
  8826. check_event(st, event);
  8827. sd = script_rid2sd(st);
  8828. if( sd == NULL )
  8829. return 0;
  8830. if (!pc_addeventtimer(sd,tick,event)) {
  8831. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8832. return SCRIPT_CMD_FAILURE;
  8833. }
  8834. return SCRIPT_CMD_SUCCESS;
  8835. }
  8836. /*==========================================
  8837. *------------------------------------------*/
  8838. BUILDIN_FUNC(deltimer)
  8839. {
  8840. const char *event;
  8841. TBL_PC* sd;
  8842. event=script_getstr(st, 2);
  8843. sd = script_rid2sd(st);
  8844. if( sd == NULL )
  8845. return 0;
  8846. check_event(st, event);
  8847. pc_deleventtimer(sd,event);
  8848. return SCRIPT_CMD_SUCCESS;
  8849. }
  8850. /*==========================================
  8851. *------------------------------------------*/
  8852. BUILDIN_FUNC(addtimercount)
  8853. {
  8854. const char *event;
  8855. int tick;
  8856. TBL_PC* sd;
  8857. tick=script_getnum(st,2);
  8858. event=script_getstr(st,3);
  8859. sd = script_rid2sd(st);
  8860. if( sd == NULL )
  8861. return 0;
  8862. check_event(st, event);
  8863. pc_addeventtimercount(sd,event,tick);
  8864. return SCRIPT_CMD_SUCCESS;
  8865. }
  8866. /*==========================================
  8867. *------------------------------------------*/
  8868. BUILDIN_FUNC(initnpctimer)
  8869. {
  8870. struct npc_data *nd;
  8871. int flag = 0;
  8872. if( script_hasdata(st,3) )
  8873. { //Two arguments: NPC name and attach flag.
  8874. nd = npc_name2id(script_getstr(st, 2));
  8875. flag = script_getnum(st,3);
  8876. }
  8877. else if( script_hasdata(st,2) )
  8878. { //Check if argument is numeric (flag) or string (npc name)
  8879. struct script_data *data;
  8880. data = script_getdata(st,2);
  8881. get_val(st,data);
  8882. if( data_isstring(data) ) //NPC name
  8883. nd = npc_name2id(conv_str(st, data));
  8884. else if( data_isint(data) ) //Flag
  8885. {
  8886. nd = (struct npc_data *)map_id2bl(st->oid);
  8887. flag = conv_num(st,data);
  8888. }
  8889. else
  8890. {
  8891. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8892. return 1;
  8893. }
  8894. }
  8895. else
  8896. nd = (struct npc_data *)map_id2bl(st->oid);
  8897. if( !nd )
  8898. return 0;
  8899. if( flag ) //Attach
  8900. {
  8901. TBL_PC* sd = script_rid2sd(st);
  8902. if( sd == NULL )
  8903. return 0;
  8904. nd->u.scr.rid = sd->bl.id;
  8905. }
  8906. nd->u.scr.timertick = 0;
  8907. npc_settimerevent_tick(nd,0);
  8908. npc_timerevent_start(nd, st->rid);
  8909. return SCRIPT_CMD_SUCCESS;
  8910. }
  8911. /*==========================================
  8912. *------------------------------------------*/
  8913. BUILDIN_FUNC(startnpctimer)
  8914. {
  8915. struct npc_data *nd;
  8916. int flag = 0;
  8917. if( script_hasdata(st,3) )
  8918. { //Two arguments: NPC name and attach flag.
  8919. nd = npc_name2id(script_getstr(st, 2));
  8920. flag = script_getnum(st,3);
  8921. }
  8922. else if( script_hasdata(st,2) )
  8923. { //Check if argument is numeric (flag) or string (npc name)
  8924. struct script_data *data;
  8925. data = script_getdata(st,2);
  8926. get_val(st,data);
  8927. if( data_isstring(data) ) //NPC name
  8928. nd = npc_name2id(conv_str(st, data));
  8929. else if( data_isint(data) ) //Flag
  8930. {
  8931. nd = (struct npc_data *)map_id2bl(st->oid);
  8932. flag = conv_num(st,data);
  8933. }
  8934. else
  8935. {
  8936. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8937. return 1;
  8938. }
  8939. }
  8940. else
  8941. nd=(struct npc_data *)map_id2bl(st->oid);
  8942. if( !nd )
  8943. return 0;
  8944. if( flag ) //Attach
  8945. {
  8946. TBL_PC* sd = script_rid2sd(st);
  8947. if( sd == NULL )
  8948. return 0;
  8949. nd->u.scr.rid = sd->bl.id;
  8950. }
  8951. npc_timerevent_start(nd, st->rid);
  8952. return SCRIPT_CMD_SUCCESS;
  8953. }
  8954. /*==========================================
  8955. *------------------------------------------*/
  8956. BUILDIN_FUNC(stopnpctimer)
  8957. {
  8958. struct npc_data *nd;
  8959. int flag = 0;
  8960. if( script_hasdata(st,3) )
  8961. { //Two arguments: NPC name and attach flag.
  8962. nd = npc_name2id(script_getstr(st, 2));
  8963. flag = script_getnum(st,3);
  8964. }
  8965. else if( script_hasdata(st,2) )
  8966. { //Check if argument is numeric (flag) or string (npc name)
  8967. struct script_data *data;
  8968. data = script_getdata(st,2);
  8969. get_val(st,data);
  8970. if( data_isstring(data) ) //NPC name
  8971. nd = npc_name2id(conv_str(st, data));
  8972. else if( data_isint(data) ) //Flag
  8973. {
  8974. nd = (struct npc_data *)map_id2bl(st->oid);
  8975. flag = conv_num(st,data);
  8976. }
  8977. else
  8978. {
  8979. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8980. return 1;
  8981. }
  8982. }
  8983. else
  8984. nd=(struct npc_data *)map_id2bl(st->oid);
  8985. if( !nd )
  8986. return 0;
  8987. if( flag ) //Detach
  8988. nd->u.scr.rid = 0;
  8989. npc_timerevent_stop(nd);
  8990. return SCRIPT_CMD_SUCCESS;
  8991. }
  8992. /*==========================================
  8993. *------------------------------------------*/
  8994. BUILDIN_FUNC(getnpctimer)
  8995. {
  8996. struct npc_data *nd;
  8997. TBL_PC *sd;
  8998. int type = script_getnum(st,2);
  8999. int val = 0;
  9000. if( script_hasdata(st,3) )
  9001. nd = npc_name2id(script_getstr(st,3));
  9002. else
  9003. nd = (struct npc_data *)map_id2bl(st->oid);
  9004. if( !nd || nd->bl.type != BL_NPC )
  9005. {
  9006. script_pushint(st,0);
  9007. ShowError("getnpctimer: Invalid NPC.\n");
  9008. return 1;
  9009. }
  9010. switch( type )
  9011. {
  9012. case 0: val = npc_gettimerevent_tick(nd); break;
  9013. case 1:
  9014. if( nd->u.scr.rid )
  9015. {
  9016. sd = map_id2sd(nd->u.scr.rid);
  9017. if( !sd )
  9018. {
  9019. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9020. break;
  9021. }
  9022. val = (sd->npc_timer_id != INVALID_TIMER);
  9023. }
  9024. else
  9025. val = (nd->u.scr.timerid != INVALID_TIMER);
  9026. break;
  9027. case 2: val = nd->u.scr.timeramount; break;
  9028. }
  9029. script_pushint(st,val);
  9030. return SCRIPT_CMD_SUCCESS;
  9031. }
  9032. /*==========================================
  9033. *------------------------------------------*/
  9034. BUILDIN_FUNC(setnpctimer)
  9035. {
  9036. int tick;
  9037. struct npc_data *nd;
  9038. tick = script_getnum(st,2);
  9039. if( script_hasdata(st,3) )
  9040. nd = npc_name2id(script_getstr(st,3));
  9041. else
  9042. nd = (struct npc_data *)map_id2bl(st->oid);
  9043. if( !nd || nd->bl.type != BL_NPC )
  9044. {
  9045. script_pushint(st,1);
  9046. ShowError("setnpctimer: Invalid NPC.\n");
  9047. return 1;
  9048. }
  9049. npc_settimerevent_tick(nd,tick);
  9050. script_pushint(st,0);
  9051. return SCRIPT_CMD_SUCCESS;
  9052. }
  9053. /*==========================================
  9054. * attaches the player rid to the timer [Celest]
  9055. *------------------------------------------*/
  9056. BUILDIN_FUNC(attachnpctimer)
  9057. {
  9058. TBL_PC *sd;
  9059. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9060. if( !nd || nd->bl.type != BL_NPC )
  9061. {
  9062. script_pushint(st,1);
  9063. ShowError("setnpctimer: Invalid NPC.\n");
  9064. return 1;
  9065. }
  9066. if( script_hasdata(st,2) )
  9067. sd = map_nick2sd(script_getstr(st,2));
  9068. else
  9069. sd = script_rid2sd(st);
  9070. if( !sd )
  9071. {
  9072. script_pushint(st,1);
  9073. ShowWarning("attachnpctimer: Invalid player.\n");
  9074. return 1;
  9075. }
  9076. nd->u.scr.rid = sd->bl.id;
  9077. script_pushint(st,0);
  9078. return SCRIPT_CMD_SUCCESS;
  9079. }
  9080. /*==========================================
  9081. * detaches a player rid from the timer [Celest]
  9082. *------------------------------------------*/
  9083. BUILDIN_FUNC(detachnpctimer)
  9084. {
  9085. struct npc_data *nd;
  9086. if( script_hasdata(st,2) )
  9087. nd = npc_name2id(script_getstr(st,2));
  9088. else
  9089. nd = (struct npc_data *)map_id2bl(st->oid);
  9090. if( !nd || nd->bl.type != BL_NPC )
  9091. {
  9092. script_pushint(st,1);
  9093. ShowError("detachnpctimer: Invalid NPC.\n");
  9094. return 1;
  9095. }
  9096. nd->u.scr.rid = 0;
  9097. script_pushint(st,0);
  9098. return SCRIPT_CMD_SUCCESS;
  9099. }
  9100. /*==========================================
  9101. * To avoid "player not attached" script errors, this function is provided,
  9102. * it checks if there is a player attached to the current script. [Skotlex]
  9103. * If no, returns 0, if yes, returns the account_id of the attached player.
  9104. *------------------------------------------*/
  9105. BUILDIN_FUNC(playerattached)
  9106. {
  9107. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9108. script_pushint(st,0);
  9109. else
  9110. script_pushint(st,st->rid);
  9111. return SCRIPT_CMD_SUCCESS;
  9112. }
  9113. /*==========================================
  9114. *------------------------------------------*/
  9115. BUILDIN_FUNC(announce)
  9116. {
  9117. const char *mes = script_getstr(st,2);
  9118. int flag = script_getnum(st,3);
  9119. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9120. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  9121. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9122. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9123. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9124. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9125. {
  9126. send_target target;
  9127. 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
  9128. if (bl == NULL)
  9129. return 0;
  9130. switch (flag&BC_TARGET_MASK) {
  9131. case BC_MAP: target = ALL_SAMEMAP; break;
  9132. case BC_AREA: target = AREA; break;
  9133. case BC_SELF: target = SELF; break;
  9134. default: target = ALL_CLIENT; break; // BC_ALL
  9135. }
  9136. if (fontColor)
  9137. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9138. else
  9139. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9140. }
  9141. else
  9142. {
  9143. if (fontColor)
  9144. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9145. else
  9146. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9147. }
  9148. return SCRIPT_CMD_SUCCESS;
  9149. }
  9150. /*==========================================
  9151. *------------------------------------------*/
  9152. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9153. {
  9154. char *mes = va_arg(ap, char *);
  9155. int len = va_arg(ap, int);
  9156. int type = va_arg(ap, int);
  9157. char *fontColor = va_arg(ap, char *);
  9158. short fontType = (short)va_arg(ap, int);
  9159. short fontSize = (short)va_arg(ap, int);
  9160. short fontAlign = (short)va_arg(ap, int);
  9161. short fontY = (short)va_arg(ap, int);
  9162. if (fontColor)
  9163. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9164. else
  9165. clif_broadcast(bl, mes, len, type, SELF);
  9166. return 0;
  9167. }
  9168. BUILDIN_FUNC(mapannounce)
  9169. {
  9170. const char *mapname = script_getstr(st,2);
  9171. const char *mes = script_getstr(st,3);
  9172. int flag = script_getnum(st,4);
  9173. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9174. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  9175. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9176. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9177. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9178. int16 m;
  9179. if ((m = map_mapname2mapid(mapname)) < 0)
  9180. return 0;
  9181. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9182. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9183. return SCRIPT_CMD_SUCCESS;
  9184. }
  9185. /*==========================================
  9186. *------------------------------------------*/
  9187. BUILDIN_FUNC(areaannounce)
  9188. {
  9189. const char *mapname = script_getstr(st,2);
  9190. int x0 = script_getnum(st,3);
  9191. int y0 = script_getnum(st,4);
  9192. int x1 = script_getnum(st,5);
  9193. int y1 = script_getnum(st,6);
  9194. const char *mes = script_getstr(st,7);
  9195. int flag = script_getnum(st,8);
  9196. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9197. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  9198. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9199. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9200. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9201. int16 m;
  9202. if ((m = map_mapname2mapid(mapname)) < 0)
  9203. return 0;
  9204. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9205. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9206. return SCRIPT_CMD_SUCCESS;
  9207. }
  9208. /*==========================================
  9209. *------------------------------------------*/
  9210. BUILDIN_FUNC(getusers)
  9211. {
  9212. int flag, val = 0;
  9213. struct map_session_data* sd;
  9214. struct block_list* bl = NULL;
  9215. flag = script_getnum(st,2);
  9216. switch(flag&0x07)
  9217. {
  9218. case 0:
  9219. if(flag&0x8)
  9220. {// npc
  9221. bl = map_id2bl(st->oid);
  9222. }
  9223. else if((sd = script_rid2sd(st))!=NULL)
  9224. {// pc
  9225. bl = &sd->bl;
  9226. }
  9227. if(bl)
  9228. {
  9229. val = map[bl->m].users;
  9230. }
  9231. break;
  9232. case 1:
  9233. val = map_getusers();
  9234. break;
  9235. default:
  9236. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9237. script_pushint(st,0);
  9238. return 1;
  9239. }
  9240. script_pushint(st,val);
  9241. return SCRIPT_CMD_SUCCESS;
  9242. }
  9243. /*==========================================
  9244. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9245. *------------------------------------------*/
  9246. BUILDIN_FUNC(getmapguildusers)
  9247. {
  9248. const char *str;
  9249. int16 m;
  9250. int gid;
  9251. int c=0;
  9252. struct guild *g = NULL;
  9253. str=script_getstr(st,2);
  9254. gid=script_getnum(st,3);
  9255. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9256. script_pushint(st,-1);
  9257. return 0;
  9258. }
  9259. g = guild_search(gid);
  9260. if (g){
  9261. unsigned short i;
  9262. for(i = 0; i < g->max_member; i++)
  9263. {
  9264. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9265. c++;
  9266. }
  9267. }
  9268. script_pushint(st,c);
  9269. return SCRIPT_CMD_SUCCESS;
  9270. }
  9271. /*==========================================
  9272. *------------------------------------------*/
  9273. BUILDIN_FUNC(getmapusers)
  9274. {
  9275. const char *str;
  9276. int16 m;
  9277. str=script_getstr(st,2);
  9278. if( (m=map_mapname2mapid(str))< 0){
  9279. script_pushint(st,-1);
  9280. return 0;
  9281. }
  9282. script_pushint(st,map[m].users);
  9283. return SCRIPT_CMD_SUCCESS;
  9284. }
  9285. /*==========================================
  9286. *------------------------------------------*/
  9287. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9288. {
  9289. int *users=va_arg(ap,int *);
  9290. (*users)++;
  9291. return 0;
  9292. }
  9293. BUILDIN_FUNC(getareausers)
  9294. {
  9295. const char *str;
  9296. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9297. str=script_getstr(st,2);
  9298. x0=script_getnum(st,3);
  9299. y0=script_getnum(st,4);
  9300. x1=script_getnum(st,5);
  9301. y1=script_getnum(st,6);
  9302. if( (m=map_mapname2mapid(str))< 0){
  9303. script_pushint(st,-1);
  9304. return 0;
  9305. }
  9306. map_foreachinarea(buildin_getareausers_sub,
  9307. m,x0,y0,x1,y1,BL_PC,&users);
  9308. script_pushint(st,users);
  9309. return SCRIPT_CMD_SUCCESS;
  9310. }
  9311. /*==========================================
  9312. *------------------------------------------*/
  9313. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9314. {
  9315. int nameid=va_arg(ap,int);
  9316. int *amount=va_arg(ap,int *);
  9317. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9318. if(drop->item.nameid==nameid)
  9319. (*amount)+=drop->item.amount;
  9320. return 0;
  9321. }
  9322. BUILDIN_FUNC(getareadropitem)
  9323. {
  9324. const char *str;
  9325. int16 m,x0,y0,x1,y1;
  9326. int nameid,amount=0;
  9327. struct script_data *data;
  9328. str=script_getstr(st,2);
  9329. x0=script_getnum(st,3);
  9330. y0=script_getnum(st,4);
  9331. x1=script_getnum(st,5);
  9332. y1=script_getnum(st,6);
  9333. data=script_getdata(st,7);
  9334. get_val(st,data);
  9335. if( data_isstring(data) ){
  9336. const char *name=conv_str(st,data);
  9337. struct item_data *item_data = itemdb_searchname(name);
  9338. nameid=UNKNOWN_ITEM_ID;
  9339. if( item_data )
  9340. nameid=item_data->nameid;
  9341. }else
  9342. nameid=conv_num(st,data);
  9343. if( (m=map_mapname2mapid(str))< 0){
  9344. script_pushint(st,-1);
  9345. return 0;
  9346. }
  9347. map_foreachinarea(buildin_getareadropitem_sub,
  9348. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9349. script_pushint(st,amount);
  9350. return SCRIPT_CMD_SUCCESS;
  9351. }
  9352. /*==========================================
  9353. *------------------------------------------*/
  9354. BUILDIN_FUNC(enablenpc)
  9355. {
  9356. const char *str;
  9357. str=script_getstr(st,2);
  9358. npc_enable(str,1);
  9359. return SCRIPT_CMD_SUCCESS;
  9360. }
  9361. /*==========================================
  9362. *------------------------------------------*/
  9363. BUILDIN_FUNC(disablenpc)
  9364. {
  9365. const char *str;
  9366. str=script_getstr(st,2);
  9367. npc_enable(str,0);
  9368. return SCRIPT_CMD_SUCCESS;
  9369. }
  9370. /*==========================================
  9371. *------------------------------------------*/
  9372. BUILDIN_FUNC(hideoffnpc)
  9373. {
  9374. const char *str;
  9375. str=script_getstr(st,2);
  9376. npc_enable(str,2);
  9377. return SCRIPT_CMD_SUCCESS;
  9378. }
  9379. /*==========================================
  9380. *------------------------------------------*/
  9381. BUILDIN_FUNC(hideonnpc)
  9382. {
  9383. const char *str;
  9384. str=script_getstr(st,2);
  9385. npc_enable(str,4);
  9386. return SCRIPT_CMD_SUCCESS;
  9387. }
  9388. /* Starts a status effect on the target unit or on the attached player.
  9389. *
  9390. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9391. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9392. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9393. * <flag>: enum e_status_change_start_flags
  9394. */
  9395. BUILDIN_FUNC(sc_start)
  9396. {
  9397. TBL_NPC * nd = map_id2nd(st->oid);
  9398. struct block_list* bl;
  9399. enum sc_type type;
  9400. int tick, val1, val2, val3, val4=0, rate, flag;
  9401. char start_type;
  9402. const char* command = script_getfuncname(st);
  9403. if(strstr(command, "4"))
  9404. start_type = 4;
  9405. else if(strstr(command, "2"))
  9406. start_type = 2;
  9407. else
  9408. start_type = 1;
  9409. type = (sc_type)script_getnum(st,2);
  9410. tick = script_getnum(st,3);
  9411. val1 = script_getnum(st,4);
  9412. //If from NPC we make default flag 1 to be unavoidable
  9413. if(nd && nd->bl.id == fake_nd->bl.id)
  9414. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9415. else
  9416. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9417. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9418. if(script_hasdata(st,(6+start_type)))
  9419. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9420. else
  9421. bl = map_id2bl(st->rid);
  9422. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9423. {// When there isn't a duration specified, try to get it from the skill_db
  9424. tick = skill_get_time(status_sc2skill(type), val1);
  9425. }
  9426. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9427. bl = map_id2bl(potion_target);
  9428. tick /= 2;// Thrown potions only last half.
  9429. val4 = 1;// Mark that this was a thrown sc_effect
  9430. }
  9431. if(!bl)
  9432. return 0;
  9433. switch(start_type) {
  9434. case 1:
  9435. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9436. break;
  9437. case 2:
  9438. val2 = script_getnum(st,5);
  9439. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9440. break;
  9441. case 4:
  9442. val2 = script_getnum(st,5);
  9443. val3 = script_getnum(st,6);
  9444. val4 = script_getnum(st,7);
  9445. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9446. break;
  9447. }
  9448. return SCRIPT_CMD_SUCCESS;
  9449. }
  9450. /// Ends one or all status effects on the target unit or on the attached player.
  9451. ///
  9452. /// sc_end <effect_id>{,<unit_id>};
  9453. BUILDIN_FUNC(sc_end)
  9454. {
  9455. struct block_list* bl;
  9456. int type;
  9457. type = script_getnum(st, 2);
  9458. if (script_hasdata(st, 3))
  9459. bl = map_id2bl(script_getnum(st, 3));
  9460. else
  9461. bl = map_id2bl(st->rid);
  9462. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9463. bl = map_id2bl(potion_target);
  9464. if (!bl)
  9465. return 0;
  9466. if (type >= 0 && type < SC_MAX) {
  9467. struct status_change *sc = status_get_sc(bl);
  9468. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9469. if (!sce)
  9470. return 0;
  9471. switch (type) {
  9472. case SC_WEIGHT50:
  9473. case SC_WEIGHT90:
  9474. case SC_NOCHAT:
  9475. case SC_PUSH_CART:
  9476. case SC_ALL_RIDING:
  9477. case SC_STYLE_CHANGE:
  9478. case SC_MONSTER_TRANSFORM:
  9479. case SC_MTF_ASPD:
  9480. case SC_MTF_RANGEATK:
  9481. case SC_MTF_MATK:
  9482. case SC_MTF_MLEATKED:
  9483. case SC_MTF_CRIDAMAGE:
  9484. case SC_MTF_ASPD2:
  9485. case SC_MTF_RANGEATK2:
  9486. case SC_MTF_MATK2:
  9487. case SC_MTF_MHP:
  9488. case SC_MTF_MSP:
  9489. case SC_MTF_PUMPKIN:
  9490. case SC_MTF_HITFLEE:
  9491. case SC_ATTHASTE_CASH:
  9492. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  9493. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  9494. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  9495. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  9496. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  9497. return 0;
  9498. default:
  9499. break;
  9500. }
  9501. //This should help status_change_end force disabling the SC in case it has no limit.
  9502. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9503. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9504. } else
  9505. status_change_clear(bl, 3); // remove all effects
  9506. return SCRIPT_CMD_SUCCESS;
  9507. }
  9508. /*==========================================
  9509. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9510. *------------------------------------------*/
  9511. BUILDIN_FUNC(getscrate)
  9512. {
  9513. struct block_list *bl;
  9514. int type,rate;
  9515. type=script_getnum(st,2);
  9516. rate=script_getnum(st,3);
  9517. if( script_hasdata(st,4) ) //get for the bl assigned
  9518. bl = map_id2bl(script_getnum(st,4));
  9519. else
  9520. bl = map_id2bl(st->rid);
  9521. if (bl)
  9522. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9523. script_pushint(st,rate);
  9524. return SCRIPT_CMD_SUCCESS;
  9525. }
  9526. /**
  9527. * getstatus(<effect type>{,<type>{,<char_id>}});
  9528. **/
  9529. BUILDIN_FUNC(getstatus)
  9530. {
  9531. int id, type;
  9532. struct map_session_data* sd;
  9533. if (!script_charid2sd(4,sd))
  9534. return SCRIPT_CMD_FAILURE;
  9535. id = script_getnum(st, 2);
  9536. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9537. if( id <= SC_NONE || id >= SC_MAX )
  9538. {// invalid status type given
  9539. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9540. return 0;
  9541. }
  9542. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9543. {// no status is active
  9544. script_pushint(st, 0);
  9545. return 0;
  9546. }
  9547. switch( type )
  9548. {
  9549. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9550. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9551. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9552. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9553. case 5:
  9554. {
  9555. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9556. if( timer )
  9557. {// return the amount of time remaining
  9558. script_pushint(st, timer->tick - gettick());
  9559. }
  9560. }
  9561. break;
  9562. default: script_pushint(st, 1); break;
  9563. }
  9564. return SCRIPT_CMD_SUCCESS;
  9565. }
  9566. /*==========================================
  9567. *
  9568. *------------------------------------------*/
  9569. BUILDIN_FUNC(debugmes)
  9570. {
  9571. const char *str;
  9572. str=script_getstr(st,2);
  9573. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9574. return SCRIPT_CMD_SUCCESS;
  9575. }
  9576. /*==========================================
  9577. *------------------------------------------*/
  9578. BUILDIN_FUNC(catchpet)
  9579. {
  9580. int pet_id;
  9581. TBL_PC *sd;
  9582. pet_id= script_getnum(st,2);
  9583. sd=script_rid2sd(st);
  9584. if( sd == NULL )
  9585. return 0;
  9586. pet_catch_process1(sd,pet_id);
  9587. return SCRIPT_CMD_SUCCESS;
  9588. }
  9589. /*==========================================
  9590. * [orn]
  9591. *------------------------------------------*/
  9592. BUILDIN_FUNC(homunculus_evolution)
  9593. {
  9594. TBL_PC *sd;
  9595. sd=script_rid2sd(st);
  9596. if( sd == NULL )
  9597. return 0;
  9598. if(hom_is_active(sd->hd))
  9599. {
  9600. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9601. hom_evolution(sd->hd);
  9602. else
  9603. clif_emotion(&sd->hd->bl, E_SWT);
  9604. }
  9605. return SCRIPT_CMD_SUCCESS;
  9606. }
  9607. /*==========================================
  9608. * Checks for vaporized morph state
  9609. * and deletes ITEMID_STRANGE_EMBRYO.
  9610. *------------------------------------------*/
  9611. BUILDIN_FUNC(homunculus_mutate)
  9612. {
  9613. int homun_id;
  9614. TBL_PC *sd;
  9615. sd = script_rid2sd(st);
  9616. if( sd == NULL || sd->hd == NULL )
  9617. return 0;
  9618. if(script_hasdata(st,2))
  9619. homun_id = script_getnum(st,2);
  9620. else
  9621. homun_id = 6048 + (rnd() % 4);
  9622. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9623. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9624. int m_id = hom_class2mapid(homun_id);
  9625. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9626. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9627. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9628. hom_call(sd); // Respawn homunculus.
  9629. hom_mutate(sd->hd, homun_id);
  9630. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9631. script_pushint(st, 1);
  9632. return 0;
  9633. } else
  9634. clif_emotion(&sd->bl, E_SWT);
  9635. } else
  9636. clif_emotion(&sd->bl, E_SWT);
  9637. script_pushint(st, 0);
  9638. return SCRIPT_CMD_SUCCESS;
  9639. }
  9640. /*==========================================
  9641. * Puts homunculus into morph state
  9642. * and gives ITEMID_STRANGE_EMBRYO.
  9643. *------------------------------------------*/
  9644. BUILDIN_FUNC(morphembryo)
  9645. {
  9646. struct item item_tmp;
  9647. TBL_PC *sd;
  9648. sd = script_rid2sd(st);
  9649. if( sd == NULL || sd->hd == NULL )
  9650. return 0;
  9651. if( hom_is_active(sd->hd) ) {
  9652. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9653. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9654. char i;
  9655. memset(&item_tmp, 0, sizeof(item_tmp));
  9656. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9657. item_tmp.identify = 1;
  9658. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9659. clif_additem(sd, 0, 0, i);
  9660. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9661. } else {
  9662. hom_vaporize(sd, HOM_ST_MORPH);
  9663. script_pushint(st, 1);
  9664. return 0;
  9665. }
  9666. } else
  9667. clif_emotion(&sd->hd->bl, E_SWT);
  9668. } else
  9669. clif_emotion(&sd->bl, E_SWT);
  9670. script_pushint(st, 0);
  9671. return SCRIPT_CMD_SUCCESS;
  9672. }
  9673. // [Zephyrus]
  9674. BUILDIN_FUNC(homunculus_shuffle)
  9675. {
  9676. TBL_PC *sd;
  9677. sd=script_rid2sd(st);
  9678. if( sd == NULL )
  9679. return 0;
  9680. if(hom_is_active(sd->hd))
  9681. hom_shuffle(sd->hd);
  9682. return SCRIPT_CMD_SUCCESS;
  9683. }
  9684. /*==========================================
  9685. * Check for homunculus state.
  9686. * Return: -1 = No homunculus
  9687. * 0 = Homunculus is active
  9688. * 1 = Homunculus is vaporized (rest)
  9689. * 2 = Homunculus is in morph state
  9690. *------------------------------------------*/
  9691. BUILDIN_FUNC(checkhomcall)
  9692. {
  9693. TBL_PC *sd = script_rid2sd(st);
  9694. TBL_HOM *hd;
  9695. if( sd == NULL )
  9696. return 0;
  9697. hd = sd->hd;
  9698. if( !hd )
  9699. script_pushint(st, -1);
  9700. else
  9701. script_pushint(st, hd->homunculus.vaporize);
  9702. return SCRIPT_CMD_SUCCESS;
  9703. }
  9704. //These two functions bring the eA MAPID_* class functionality to scripts.
  9705. BUILDIN_FUNC(eaclass)
  9706. {
  9707. int class_;
  9708. if( script_hasdata(st,2) )
  9709. class_ = script_getnum(st,2);
  9710. else {
  9711. TBL_PC *sd;
  9712. sd=script_rid2sd(st);
  9713. if (!sd) {
  9714. script_pushint(st,-1);
  9715. return 0;
  9716. }
  9717. class_ = sd->status.class_;
  9718. }
  9719. script_pushint(st,pc_jobid2mapid(class_));
  9720. return SCRIPT_CMD_SUCCESS;
  9721. }
  9722. BUILDIN_FUNC(roclass)
  9723. {
  9724. int class_ =script_getnum(st,2);
  9725. int sex;
  9726. if( script_hasdata(st,3) )
  9727. sex = script_getnum(st,3);
  9728. else {
  9729. TBL_PC *sd;
  9730. if (st->rid && (sd=script_rid2sd(st)))
  9731. sex = sd->status.sex;
  9732. else
  9733. sex = 1; //Just use male when not found.
  9734. }
  9735. script_pushint(st,pc_mapid2jobid(class_, sex));
  9736. return SCRIPT_CMD_SUCCESS;
  9737. }
  9738. /*==========================================
  9739. * Tells client to open a hatching window, used for pet incubator
  9740. *------------------------------------------*/
  9741. BUILDIN_FUNC(birthpet)
  9742. {
  9743. TBL_PC *sd;
  9744. sd=script_rid2sd(st);
  9745. if( sd == NULL )
  9746. return 0;
  9747. if( sd->status.pet_id )
  9748. {// do not send egg list, when you already have a pet
  9749. return 0;
  9750. }
  9751. clif_sendegg(sd);
  9752. return SCRIPT_CMD_SUCCESS;
  9753. }
  9754. /**
  9755. * resetlvl <action type>{,<char_id>};
  9756. * @param action_type:
  9757. * 1 : make like after rebirth
  9758. * 2 : blvl,jlvl=1, skillpoint=0
  9759. * 3 : don't reset skill, blvl=1
  9760. * 4 : jlvl=0
  9761. * @author AppleGirl
  9762. **/
  9763. BUILDIN_FUNC(resetlvl)
  9764. {
  9765. TBL_PC *sd;
  9766. int type=script_getnum(st,2);
  9767. if (!script_charid2sd(3,sd))
  9768. return SCRIPT_CMD_FAILURE;
  9769. pc_resetlvl(sd,type);
  9770. return SCRIPT_CMD_SUCCESS;
  9771. }
  9772. /**
  9773. * Reset a player status point
  9774. * resetstatus({<char_id>});
  9775. **/
  9776. BUILDIN_FUNC(resetstatus)
  9777. {
  9778. TBL_PC *sd;
  9779. if (!script_charid2sd(2,sd))
  9780. return SCRIPT_CMD_FAILURE;
  9781. pc_resetstate(sd);
  9782. return SCRIPT_CMD_SUCCESS;
  9783. }
  9784. /**
  9785. * Reset player's skill
  9786. * resetskill({<char_id>});
  9787. **/
  9788. BUILDIN_FUNC(resetskill)
  9789. {
  9790. TBL_PC *sd;
  9791. if (!script_charid2sd(2,sd))
  9792. return SCRIPT_CMD_FAILURE;
  9793. pc_resetskill(sd,1);
  9794. return SCRIPT_CMD_SUCCESS;
  9795. }
  9796. /**
  9797. * Counts total amount of skill points.
  9798. * skillpointcount({<char_id>})
  9799. **/
  9800. BUILDIN_FUNC(skillpointcount)
  9801. {
  9802. TBL_PC *sd;
  9803. if (!script_charid2sd(2,sd))
  9804. return SCRIPT_CMD_FAILURE;
  9805. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9806. return SCRIPT_CMD_SUCCESS;
  9807. }
  9808. /*==========================================
  9809. *
  9810. *------------------------------------------*/
  9811. BUILDIN_FUNC(changebase)
  9812. {
  9813. TBL_PC *sd=NULL;
  9814. int vclass;
  9815. if( script_hasdata(st,3) )
  9816. sd=map_id2sd(script_getnum(st,3));
  9817. else
  9818. sd=script_rid2sd(st);
  9819. if(sd == NULL)
  9820. return 0;
  9821. vclass = script_getnum(st,2);
  9822. if(vclass == JOB_WEDDING)
  9823. {
  9824. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9825. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9826. )
  9827. return 0;
  9828. }
  9829. if(!sd->disguise && vclass != sd->vd.class_) {
  9830. status_set_viewdata(&sd->bl, vclass);
  9831. //Updated client view. Base, Weapon and Cloth Colors.
  9832. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9833. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9834. if (sd->vd.cloth_color)
  9835. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9836. clif_skillinfoblock(sd);
  9837. }
  9838. return SCRIPT_CMD_SUCCESS;
  9839. }
  9840. /**
  9841. * Change account sex and unequip all item and request for a changesex to char-serv
  9842. * changesex({<char_id>});
  9843. */
  9844. BUILDIN_FUNC(changesex)
  9845. {
  9846. int i;
  9847. TBL_PC *sd = NULL;
  9848. if (!script_charid2sd(2,sd))
  9849. return SCRIPT_CMD_FAILURE;
  9850. pc_resetskill(sd,4);
  9851. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9852. for(i = 0; i < EQI_MAX; i++) {
  9853. if (sd->equip_index[i] >= 0)
  9854. pc_unequipitem(sd, sd->equip_index[i], 3);
  9855. }
  9856. chrif_changesex(sd, true);
  9857. return SCRIPT_CMD_SUCCESS;
  9858. }
  9859. /**
  9860. * Change character's sex and unequip all item and request for a changesex to char-serv
  9861. * changecharsex({<char_id>});
  9862. */
  9863. BUILDIN_FUNC(changecharsex)
  9864. {
  9865. #if PACKETVER >= 20141016
  9866. int i;
  9867. TBL_PC *sd = NULL;
  9868. if (!script_charid2sd(2,sd))
  9869. return SCRIPT_CMD_FAILURE;
  9870. pc_resetskill(sd,4);
  9871. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9872. for (i = 0; i < EQI_MAX; i++) {
  9873. if (sd->equip_index[i] >= 0)
  9874. pc_unequipitem(sd, sd->equip_index[i], 3);
  9875. }
  9876. chrif_changesex(sd, false);
  9877. return SCRIPT_CMD_SUCCESS;
  9878. #else
  9879. return SCRIPT_CMD_FAILURE;
  9880. #endif
  9881. }
  9882. /*==========================================
  9883. * Works like 'announce' but outputs in the common chat window
  9884. *------------------------------------------*/
  9885. BUILDIN_FUNC(globalmes)
  9886. {
  9887. struct block_list *bl = map_id2bl(st->oid);
  9888. struct npc_data *nd = (struct npc_data *)bl;
  9889. const char *name=NULL,*mes;
  9890. mes=script_getstr(st,2);
  9891. if(mes==NULL) return 0;
  9892. if(script_hasdata(st,3)){ // npc name to display
  9893. name=script_getstr(st,3);
  9894. } else {
  9895. name=nd->name; //use current npc name
  9896. }
  9897. npc_globalmessage(name,mes); // broadcast to all players connected
  9898. return SCRIPT_CMD_SUCCESS;
  9899. }
  9900. /////////////////////////////////////////////////////////////////////
  9901. // NPC waiting room (chat room)
  9902. //
  9903. /// Creates a waiting room (chat room) for this npc.
  9904. ///
  9905. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9906. BUILDIN_FUNC(waitingroom)
  9907. {
  9908. struct npc_data* nd;
  9909. const char* title = script_getstr(st, 2);
  9910. int limit = script_getnum(st, 3);
  9911. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9912. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9913. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9914. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9915. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9916. nd = (struct npc_data *)map_id2bl(st->oid);
  9917. if( nd != NULL )
  9918. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  9919. return SCRIPT_CMD_SUCCESS;
  9920. }
  9921. /// Removes the waiting room of the current or target npc.
  9922. ///
  9923. /// delwaitingroom "<npc_name>";
  9924. /// delwaitingroom;
  9925. BUILDIN_FUNC(delwaitingroom)
  9926. {
  9927. struct npc_data* nd;
  9928. if( script_hasdata(st,2) )
  9929. nd = npc_name2id(script_getstr(st, 2));
  9930. else
  9931. nd = (struct npc_data *)map_id2bl(st->oid);
  9932. if( nd != NULL )
  9933. chat_deletenpcchat(nd);
  9934. return SCRIPT_CMD_SUCCESS;
  9935. }
  9936. /// Kick the specified player from the waiting room of the target npc.
  9937. ///
  9938. /// waitingroomkick "<npc_name>", <kickusername>;
  9939. BUILDIN_FUNC(waitingroomkick)
  9940. {
  9941. struct npc_data* nd;
  9942. struct chat_data* cd;
  9943. const char* kickusername;
  9944. nd = npc_name2id(script_getstr(st,2));
  9945. kickusername = script_getstr(st,3);
  9946. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9947. chat_npckickchat(cd, kickusername);
  9948. return SCRIPT_CMD_SUCCESS;
  9949. }
  9950. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  9951. /// Num users stored in .@waitingroom_usercount
  9952. ///
  9953. /// getwaitingroomusers "<npc_name>";
  9954. BUILDIN_FUNC(getwaitingroomusers)
  9955. {
  9956. struct npc_data* nd;
  9957. struct chat_data* cd;
  9958. int i, j=0;
  9959. if( script_hasdata(st,2) )
  9960. nd = npc_name2id(script_getstr(st, 2));
  9961. else
  9962. nd = (struct npc_data *)map_id2bl(st->oid);
  9963. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  9964. for(i = 0; i < cd->users; ++i) {
  9965. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  9966. j++;
  9967. }
  9968. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  9969. }
  9970. return SCRIPT_CMD_SUCCESS;
  9971. }
  9972. /// Kicks all the players from the waiting room of the current or target npc.
  9973. ///
  9974. /// kickwaitingroomall "<npc_name>";
  9975. /// kickwaitingroomall;
  9976. BUILDIN_FUNC(waitingroomkickall)
  9977. {
  9978. struct npc_data* nd;
  9979. struct chat_data* cd;
  9980. if( script_hasdata(st,2) )
  9981. nd = npc_name2id(script_getstr(st,2));
  9982. else
  9983. nd = (struct npc_data *)map_id2bl(st->oid);
  9984. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9985. chat_npckickall(cd);
  9986. return SCRIPT_CMD_SUCCESS;
  9987. }
  9988. /// Enables the waiting room event of the current or target npc.
  9989. ///
  9990. /// enablewaitingroomevent "<npc_name>";
  9991. /// enablewaitingroomevent;
  9992. BUILDIN_FUNC(enablewaitingroomevent)
  9993. {
  9994. struct npc_data* nd;
  9995. struct chat_data* cd;
  9996. if( script_hasdata(st,2) )
  9997. nd = npc_name2id(script_getstr(st, 2));
  9998. else
  9999. nd = (struct npc_data *)map_id2bl(st->oid);
  10000. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10001. chat_enableevent(cd);
  10002. return SCRIPT_CMD_SUCCESS;
  10003. }
  10004. /// Disables the waiting room event of the current or target npc.
  10005. ///
  10006. /// disablewaitingroomevent "<npc_name>";
  10007. /// disablewaitingroomevent;
  10008. BUILDIN_FUNC(disablewaitingroomevent)
  10009. {
  10010. struct npc_data *nd;
  10011. struct chat_data *cd;
  10012. if( script_hasdata(st,2) )
  10013. nd = npc_name2id(script_getstr(st, 2));
  10014. else
  10015. nd = (struct npc_data *)map_id2bl(st->oid);
  10016. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10017. chat_disableevent(cd);
  10018. return SCRIPT_CMD_SUCCESS;
  10019. }
  10020. /// Returns info on the waiting room of the current or target npc.
  10021. /// Returns -1 if the type unknown
  10022. /// <type>=0 : current number of users
  10023. /// <type>=1 : maximum number of users allowed
  10024. /// <type>=2 : the number of users that trigger the event
  10025. /// <type>=3 : if the trigger is disabled
  10026. /// <type>=4 : the title of the waiting room
  10027. /// <type>=5 : the password of the waiting room
  10028. /// <type>=16 : the name of the waiting room event
  10029. /// <type>=32 : if the waiting room is full
  10030. /// <type>=33 : if there are enough users to trigger the event
  10031. ///
  10032. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10033. /// getwaitingroomstate(<type>) -> <info>
  10034. BUILDIN_FUNC(getwaitingroomstate)
  10035. {
  10036. struct npc_data *nd;
  10037. struct chat_data *cd;
  10038. int type;
  10039. type = script_getnum(st,2);
  10040. if( script_hasdata(st,3) )
  10041. nd = npc_name2id(script_getstr(st, 3));
  10042. else
  10043. nd = (struct npc_data *)map_id2bl(st->oid);
  10044. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10045. {
  10046. script_pushint(st, -1);
  10047. return 0;
  10048. }
  10049. switch(type)
  10050. {
  10051. case 0: script_pushint(st, cd->users); break;
  10052. case 1: script_pushint(st, cd->limit); break;
  10053. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10054. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10055. case 4: script_pushstrcopy(st, cd->title); break;
  10056. case 5: script_pushstrcopy(st, cd->pass); break;
  10057. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10058. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10059. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10060. default: script_pushint(st, -1); break;
  10061. }
  10062. return SCRIPT_CMD_SUCCESS;
  10063. }
  10064. /// Warps the trigger or target amount of players to the target map and position.
  10065. /// Players are automatically removed from the waiting room.
  10066. /// Those waiting the longest will get warped first.
  10067. /// The target map can be "Random" for a random position in the current map,
  10068. /// and "SavePoint" for the savepoint map+position.
  10069. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10070. ///
  10071. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10072. /// The total number of teleported players is put into $@warpwaitingpcnum
  10073. ///
  10074. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10075. /// warpwaitingpc "<map name>",<x>,<y>;
  10076. BUILDIN_FUNC(warpwaitingpc)
  10077. {
  10078. int x;
  10079. int y;
  10080. int i;
  10081. int n;
  10082. const char* map_name;
  10083. struct npc_data* nd;
  10084. struct chat_data* cd;
  10085. nd = (struct npc_data *)map_id2bl(st->oid);
  10086. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10087. return 0;
  10088. map_name = script_getstr(st,2);
  10089. x = script_getnum(st,3);
  10090. y = script_getnum(st,4);
  10091. n = cd->trigger&0x7f;
  10092. if( script_hasdata(st,5) )
  10093. n = script_getnum(st,5);
  10094. for( i = 0; i < n && cd->users > 0; i++ )
  10095. {
  10096. TBL_PC* sd = cd->usersd[0];
  10097. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  10098. {// can't teleport on this map
  10099. break;
  10100. }
  10101. if( cd->zeny )
  10102. {// fee set
  10103. if( (uint32)sd->status.zeny < cd->zeny )
  10104. {// no zeny to cover set fee
  10105. break;
  10106. }
  10107. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10108. }
  10109. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10110. if( strcmp(map_name,"Random") == 0 )
  10111. pc_randomwarp(sd,CLR_TELEPORT);
  10112. else if( strcmp(map_name,"SavePoint") == 0 )
  10113. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10114. else
  10115. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10116. }
  10117. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10118. return SCRIPT_CMD_SUCCESS;
  10119. }
  10120. /////////////////////////////////////////////////////////////////////
  10121. // ...
  10122. //
  10123. /// Detaches a character from a script.
  10124. ///
  10125. /// @param st Script state to detach the character from.
  10126. static void script_detach_rid(struct script_state* st)
  10127. {
  10128. if(st->rid)
  10129. {
  10130. script_detach_state(st, false);
  10131. st->rid = 0;
  10132. }
  10133. }
  10134. /*=========================================================================
  10135. * Attaches a set of RIDs to the current script. [digitalhamster]
  10136. * addrid(<type>{,<flag>{,<parameters>}});
  10137. * <type>:
  10138. * 0 : All players in the server.
  10139. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10140. * 2 : Party members of a specified party ID.
  10141. * [ Parameters: <party id> ]
  10142. * 3 : Guild members of a specified guild ID.
  10143. * [ Parameters: <guild id> ]
  10144. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10145. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10146. * Account ID: The specified account ID.
  10147. * <flag>:
  10148. * 0 : Players are always attached. (default)
  10149. * 1 : Players currently running another script will not be attached.
  10150. *-------------------------------------------------------------------------*/
  10151. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10152. {
  10153. int forceflag;
  10154. struct map_session_data *sd = (TBL_PC *)bl;
  10155. struct script_state* st;
  10156. st = va_arg(ap,struct script_state*);
  10157. forceflag = va_arg(ap,int);
  10158. if(!forceflag || !sd->st)
  10159. if(sd->status.account_id != st->rid)
  10160. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10161. return 0;
  10162. }
  10163. BUILDIN_FUNC(addrid)
  10164. {
  10165. struct s_mapiterator* iter;
  10166. struct block_list *bl;
  10167. TBL_PC *sd;
  10168. if(st->rid < 1) {
  10169. st->state = END;
  10170. bl = map_id2bl(st->oid);
  10171. } else
  10172. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10173. iter = mapit_getallusers();
  10174. switch(script_getnum(st,2)) {
  10175. case 0:
  10176. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10177. if(!script_getnum(st,3) || !sd->st)
  10178. if(sd->status.account_id != st->rid) //attached player already runs.
  10179. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10180. }
  10181. break;
  10182. case 1:
  10183. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10184. if(!script_getnum(st,3) || !sd->st)
  10185. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10186. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10187. }
  10188. break;
  10189. case 2:
  10190. if(script_getnum(st,4) == 0) {
  10191. script_pushint(st,0);
  10192. return 0;
  10193. }
  10194. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10195. if(!script_getnum(st,3) || !sd->st)
  10196. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10197. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10198. }
  10199. break;
  10200. case 3:
  10201. if(script_getnum(st,4) == 0) {
  10202. script_pushint(st,0);
  10203. return 0;
  10204. }
  10205. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10206. if(!script_getnum(st,3) || !sd->st)
  10207. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10208. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10209. }
  10210. break;
  10211. case 4:
  10212. map_foreachinarea(buildin_addrid_sub,
  10213. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10214. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10215. break;
  10216. default:
  10217. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10218. script_pushint(st,0);
  10219. return 0;
  10220. }
  10221. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10222. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10223. script_pushint(st,1);
  10224. }
  10225. return 0;
  10226. }
  10227. mapit_free(iter);
  10228. script_pushint(st,1);
  10229. return SCRIPT_CMD_SUCCESS;
  10230. }
  10231. /*==========================================
  10232. * Attach sd char id to script and detach current one if any
  10233. *------------------------------------------*/
  10234. BUILDIN_FUNC(attachrid)
  10235. {
  10236. int rid = script_getnum(st,2);
  10237. if (map_id2sd(rid) != NULL) {
  10238. script_detach_rid(st);
  10239. st->rid = rid;
  10240. script_attach_state(st);
  10241. script_pushint(st,1);
  10242. } else
  10243. script_pushint(st,0);
  10244. return SCRIPT_CMD_SUCCESS;
  10245. }
  10246. /*==========================================
  10247. * Detach script to rid
  10248. *------------------------------------------*/
  10249. BUILDIN_FUNC(detachrid)
  10250. {
  10251. script_detach_rid(st);
  10252. return SCRIPT_CMD_SUCCESS;
  10253. }
  10254. /*==========================================
  10255. * Chk if account connected, (and charid from account if specified)
  10256. *------------------------------------------*/
  10257. BUILDIN_FUNC(isloggedin)
  10258. {
  10259. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10260. if (script_hasdata(st,3) && sd &&
  10261. sd->status.char_id != script_getnum(st,3))
  10262. sd = NULL;
  10263. push_val(st->stack,C_INT,sd!=NULL);
  10264. return SCRIPT_CMD_SUCCESS;
  10265. }
  10266. /*==========================================
  10267. *
  10268. *------------------------------------------*/
  10269. BUILDIN_FUNC(setmapflagnosave)
  10270. {
  10271. int16 m,x,y;
  10272. unsigned short mapindex;
  10273. const char *str,*str2;
  10274. str=script_getstr(st,2);
  10275. str2=script_getstr(st,3);
  10276. x=script_getnum(st,4);
  10277. y=script_getnum(st,5);
  10278. m = map_mapname2mapid(str);
  10279. mapindex = mapindex_name2id(str2);
  10280. if(m >= 0 && mapindex) {
  10281. map[m].flag.nosave=1;
  10282. map[m].save.map=mapindex;
  10283. map[m].save.x=x;
  10284. map[m].save.y=y;
  10285. }
  10286. return SCRIPT_CMD_SUCCESS;
  10287. }
  10288. BUILDIN_FUNC(getmapflag)
  10289. {
  10290. int16 m,i;
  10291. const char *str;
  10292. str=script_getstr(st,2);
  10293. i=script_getnum(st,3);
  10294. m = map_mapname2mapid(str);
  10295. if(m >= 0) {
  10296. switch(i) {
  10297. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10298. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10299. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10300. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10301. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10302. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10303. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10304. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10305. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10306. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10307. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10308. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10309. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10310. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10311. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10312. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10313. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10314. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10315. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10316. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10317. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10318. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10319. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10320. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10321. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10322. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10323. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10324. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10325. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10326. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10327. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10328. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10329. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10330. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10331. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10332. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10333. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10334. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10335. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10336. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10337. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10338. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10339. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10340. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10341. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10342. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10343. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10344. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10345. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10346. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10347. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10348. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10349. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10350. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10351. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10352. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10353. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10354. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10355. #ifdef ADJUST_SKILL_DAMAGE
  10356. case MF_SKILL_DAMAGE:
  10357. {
  10358. int ret_val=0, type=0;
  10359. FETCH(4,type);
  10360. switch (type) {
  10361. case 1: ret_val = map[m].adjust.damage.pc; break;
  10362. case 2: ret_val = map[m].adjust.damage.mob; break;
  10363. case 3: ret_val = map[m].adjust.damage.boss; break;
  10364. case 4: ret_val = map[m].adjust.damage.other; break;
  10365. case 5: ret_val = map[m].adjust.damage.caster; break;
  10366. default: ret_val = map[m].flag.skill_damage; break;
  10367. }
  10368. script_pushint(st,ret_val); break;
  10369. } break;
  10370. #endif
  10371. }
  10372. }
  10373. return SCRIPT_CMD_SUCCESS;
  10374. }
  10375. /* pvp timer handling */
  10376. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10377. TBL_PC* sd = (TBL_PC*)bl;
  10378. if (sd->pvp_timer == INVALID_TIMER) {
  10379. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10380. sd->pvp_rank = 0;
  10381. sd->pvp_lastusers = 0;
  10382. sd->pvp_point = 5;
  10383. sd->pvp_won = 0;
  10384. sd->pvp_lost = 0;
  10385. }
  10386. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  10387. clif_maptypeproperty2(&sd->bl,SELF);
  10388. return 0;
  10389. }
  10390. BUILDIN_FUNC(setmapflag)
  10391. {
  10392. int16 m,i;
  10393. const char *str;
  10394. int val=0;
  10395. str=script_getstr(st,2);
  10396. i=script_getnum(st,3);
  10397. FETCH(4,val);
  10398. m = map_mapname2mapid(str);
  10399. if(m >= 0) {
  10400. switch(i) {
  10401. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10402. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10403. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10404. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10405. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10406. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10407. case MF_PVP:
  10408. map[m].flag.pvp = 1;
  10409. if( !battle_config.pk_mode ) {
  10410. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10411. }
  10412. break;
  10413. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10414. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10415. case MF_GVG: {
  10416. struct block_list bl;
  10417. map[m].flag.gvg = 1;
  10418. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10419. bl.type = BL_NUL;
  10420. bl.m = m;
  10421. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10422. }
  10423. break;
  10424. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10425. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10426. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10427. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10428. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10429. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10430. case MF_SNOW: map[m].flag.snow = 1; break;
  10431. case MF_FOG: map[m].flag.fog = 1; break;
  10432. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10433. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10434. case MF_NOGO: map[m].flag.nogo = 1; break;
  10435. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10436. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10437. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10438. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10439. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10440. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10441. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10442. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10443. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10444. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10445. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10446. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10447. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10448. case MF_RESTRICTED:
  10449. map[m].zone |= 1<<(val+1);
  10450. map[m].flag.restricted=1;
  10451. break;
  10452. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10453. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10454. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10455. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10456. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10457. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10458. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10459. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10460. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10461. case MF_TOWN: map[m].flag.town = 1; break;
  10462. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10463. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10464. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10465. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10466. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10467. case MF_RESET: map[m].flag.reset = 1; break;
  10468. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10469. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10470. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10471. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10472. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10473. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10474. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10475. #ifdef ADJUST_SKILL_DAMAGE
  10476. case MF_SKILL_DAMAGE:
  10477. {
  10478. int type=0;
  10479. FETCH(5,type);
  10480. switch (type) {
  10481. case 1: map[m].adjust.damage.pc = val; break;
  10482. case 2: map[m].adjust.damage.mob = val; break;
  10483. case 3: map[m].adjust.damage.boss = val; break;
  10484. case 4: map[m].adjust.damage.other = val; break;
  10485. case 5: map[m].adjust.damage.caster = val; break;
  10486. }
  10487. map[m].flag.skill_damage = 1;
  10488. } break;
  10489. #endif
  10490. }
  10491. }
  10492. return SCRIPT_CMD_SUCCESS;
  10493. }
  10494. BUILDIN_FUNC(removemapflag)
  10495. {
  10496. int16 m,i;
  10497. const char *str;
  10498. int val=0;
  10499. str=script_getstr(st,2);
  10500. i=script_getnum(st,3);
  10501. FETCH(4,val);
  10502. m = map_mapname2mapid(str);
  10503. if(m >= 0) {
  10504. switch(i) {
  10505. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10506. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10507. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10508. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10509. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10510. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10511. case MF_PVP: {
  10512. struct block_list bl;
  10513. bl.type = BL_NUL;
  10514. bl.m = m;
  10515. map[m].flag.pvp = 0;
  10516. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10517. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10518. }
  10519. break;
  10520. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10521. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10522. case MF_GVG: {
  10523. struct block_list bl;
  10524. bl.type = BL_NUL;
  10525. bl.m = m;
  10526. map[m].flag.gvg = 0;
  10527. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10528. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10529. }
  10530. break;
  10531. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10532. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10533. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10534. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10535. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10536. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10537. case MF_SNOW: map[m].flag.snow = 0; break;
  10538. case MF_FOG: map[m].flag.fog = 0; break;
  10539. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10540. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10541. case MF_NOGO: map[m].flag.nogo = 0; break;
  10542. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10543. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10544. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10545. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10546. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10547. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10548. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10549. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10550. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10551. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10552. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10553. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10554. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10555. case MF_RESTRICTED:
  10556. map[m].zone ^= 1<<(val+1);
  10557. if (map[m].zone == 0){
  10558. map[m].flag.restricted=0;
  10559. }
  10560. break;
  10561. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10562. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10563. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10564. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10565. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10566. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10567. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10568. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10569. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10570. case MF_TOWN: map[m].flag.town = 0; break;
  10571. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10572. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10573. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10574. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10575. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10576. case MF_RESET: map[m].flag.reset = 0; break;
  10577. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10578. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10579. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10580. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10581. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10582. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10583. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10584. #ifdef ADJUST_SKILL_DAMAGE
  10585. case MF_SKILL_DAMAGE:
  10586. {
  10587. map[m].flag.skill_damage = 0;
  10588. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10589. if (map[m].skill_damage.count)
  10590. map_skill_damage_free(&map[m]);
  10591. } break;
  10592. #endif
  10593. }
  10594. }
  10595. return SCRIPT_CMD_SUCCESS;
  10596. }
  10597. BUILDIN_FUNC(pvpon)
  10598. {
  10599. int16 m;
  10600. const char *str;
  10601. TBL_PC* sd = NULL;
  10602. struct s_mapiterator* iter;
  10603. struct block_list bl;
  10604. str = script_getstr(st,2);
  10605. m = map_mapname2mapid(str);
  10606. if( m < 0 || map[m].flag.pvp )
  10607. return 0; // nothing to do
  10608. map[m].flag.pvp = 1;
  10609. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10610. bl.type = BL_NUL;
  10611. bl.m = m;
  10612. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10613. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10614. return 0;
  10615. iter = mapit_getallusers();
  10616. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10617. {
  10618. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10619. continue; // not applicable
  10620. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10621. sd->pvp_rank = 0;
  10622. sd->pvp_lastusers = 0;
  10623. sd->pvp_point = 5;
  10624. sd->pvp_won = 0;
  10625. sd->pvp_lost = 0;
  10626. }
  10627. mapit_free(iter);
  10628. return SCRIPT_CMD_SUCCESS;
  10629. }
  10630. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10631. {
  10632. TBL_PC* sd = (TBL_PC*)bl;
  10633. clif_pvpset(sd, 0, 0, 2);
  10634. if (sd->pvp_timer != INVALID_TIMER) {
  10635. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10636. sd->pvp_timer = INVALID_TIMER;
  10637. }
  10638. return 0;
  10639. }
  10640. BUILDIN_FUNC(pvpoff)
  10641. {
  10642. int16 m;
  10643. const char *str;
  10644. struct block_list bl;
  10645. str=script_getstr(st,2);
  10646. m = map_mapname2mapid(str);
  10647. if(m < 0 || !map[m].flag.pvp)
  10648. return 0; //fixed Lupus
  10649. map[m].flag.pvp = 0;
  10650. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10651. bl.type = BL_NUL;
  10652. bl.m = m;
  10653. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10654. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10655. return 0;
  10656. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10657. return SCRIPT_CMD_SUCCESS;
  10658. }
  10659. BUILDIN_FUNC(gvgon)
  10660. {
  10661. int16 m;
  10662. const char *str;
  10663. struct block_list bl;
  10664. str=script_getstr(st,2);
  10665. m = map_mapname2mapid(str);
  10666. if(m >= 0 && !map[m].flag.gvg) {
  10667. map[m].flag.gvg = 1;
  10668. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10669. bl.type = BL_NUL;
  10670. bl.m = m;
  10671. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10672. }
  10673. return SCRIPT_CMD_SUCCESS;
  10674. }
  10675. BUILDIN_FUNC(gvgoff)
  10676. {
  10677. int16 m;
  10678. const char *str;
  10679. str=script_getstr(st,2);
  10680. m = map_mapname2mapid(str);
  10681. if(m >= 0 && map[m].flag.gvg) {
  10682. struct block_list bl;
  10683. map[m].flag.gvg = 0;
  10684. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10685. bl.type = BL_NUL;
  10686. bl.m = m;
  10687. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10688. }
  10689. return SCRIPT_CMD_SUCCESS;
  10690. }
  10691. /*==========================================
  10692. * Shows an emoticon on top of the player/npc
  10693. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10694. *------------------------------------------*/
  10695. //Optional second parameter added by [Skotlex]
  10696. BUILDIN_FUNC(emotion)
  10697. {
  10698. int type;
  10699. int player=0;
  10700. type=script_getnum(st,2);
  10701. if(type < 0 || type > 100)
  10702. return 0;
  10703. if( script_hasdata(st,3) )
  10704. player=script_getnum(st,3);
  10705. if (player) {
  10706. TBL_PC *sd = NULL;
  10707. if( script_hasdata(st,4) )
  10708. sd = map_nick2sd(script_getstr(st,4));
  10709. else
  10710. sd = script_rid2sd(st);
  10711. if (sd)
  10712. clif_emotion(&sd->bl,type);
  10713. } else
  10714. if( script_hasdata(st,4) )
  10715. {
  10716. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10717. if(nd)
  10718. clif_emotion(&nd->bl,type);
  10719. }
  10720. else
  10721. clif_emotion(map_id2bl(st->oid),type);
  10722. return SCRIPT_CMD_SUCCESS;
  10723. }
  10724. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10725. {
  10726. int16 m=va_arg(ap,int);
  10727. int g_id=va_arg(ap,int);
  10728. int flag=va_arg(ap,int);
  10729. if(!sd || sd->bl.m != m)
  10730. return 0;
  10731. if(
  10732. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10733. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10734. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10735. )
  10736. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10737. return 1;
  10738. }
  10739. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10740. {
  10741. struct mob_data *md=(struct mob_data *)bl;
  10742. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM)
  10743. status_kill(bl);
  10744. return 0;
  10745. }
  10746. /*
  10747. * Function to kick guild members out of a map and to their save points.
  10748. * m : mapid
  10749. * g_id : owner guild id
  10750. * flag & 1 : Warp out owners
  10751. * flag & 2 : Warp out outsiders
  10752. * flag & 4 : reserved for mobs
  10753. * */
  10754. BUILDIN_FUNC(maprespawnguildid)
  10755. {
  10756. const char *mapname=script_getstr(st,2);
  10757. int g_id=script_getnum(st,3);
  10758. int flag=script_getnum(st,4);
  10759. int16 m=map_mapname2mapid(mapname);
  10760. if(m == -1)
  10761. return 0;
  10762. //Catch ALL players (in case some are 'between maps' on execution time)
  10763. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10764. if (flag&4) //Remove script mobs.
  10765. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10766. return SCRIPT_CMD_SUCCESS;
  10767. }
  10768. BUILDIN_FUNC(agitstart)
  10769. {
  10770. if(agit_flag==1) return SCRIPT_CMD_SUCCESS; // Agit already Start.
  10771. agit_flag=1;
  10772. guild_agit_start();
  10773. return SCRIPT_CMD_SUCCESS;
  10774. }
  10775. BUILDIN_FUNC(agitend)
  10776. {
  10777. if(agit_flag==0) return SCRIPT_CMD_SUCCESS; // Agit already End.
  10778. agit_flag=0;
  10779. guild_agit_end();
  10780. return SCRIPT_CMD_SUCCESS;
  10781. }
  10782. BUILDIN_FUNC(agitstart2)
  10783. {
  10784. if(agit2_flag==1) return SCRIPT_CMD_SUCCESS; // Agit2 already Start.
  10785. agit2_flag=1;
  10786. guild_agit2_start();
  10787. return SCRIPT_CMD_SUCCESS;
  10788. }
  10789. BUILDIN_FUNC(agitend2)
  10790. {
  10791. if(agit2_flag==0) return SCRIPT_CMD_SUCCESS; // Agit2 already End.
  10792. agit2_flag=0;
  10793. guild_agit2_end();
  10794. return SCRIPT_CMD_SUCCESS;
  10795. }
  10796. /*==========================================
  10797. * Returns whether woe is on or off. // choice script
  10798. *------------------------------------------*/
  10799. BUILDIN_FUNC(agitcheck)
  10800. {
  10801. script_pushint(st,agit_flag);
  10802. return SCRIPT_CMD_SUCCESS;
  10803. }
  10804. /*==========================================
  10805. * Returns whether woese is on or off. // choice script
  10806. *------------------------------------------*/
  10807. BUILDIN_FUNC(agitcheck2)
  10808. {
  10809. script_pushint(st,agit2_flag);
  10810. return SCRIPT_CMD_SUCCESS;
  10811. }
  10812. /// Sets the guild_id of this npc.
  10813. ///
  10814. /// flagemblem <guild_id>;
  10815. BUILDIN_FUNC(flagemblem)
  10816. {
  10817. TBL_NPC* nd;
  10818. int g_id = script_getnum(st,2);
  10819. if(g_id < 0) return 0;
  10820. nd = (TBL_NPC*)map_id2nd(st->oid);
  10821. if( nd == NULL ) {
  10822. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10823. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  10824. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10825. } else {
  10826. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10827. nd->u.scr.guild_id = g_id;
  10828. clif_guild_emblem_area(&nd->bl);
  10829. /* guild flag caching */
  10830. if( g_id ) /* adding a id */
  10831. guild_flag_add(nd);
  10832. else if( changed ) /* removing a flag */
  10833. guild_flag_remove(nd);
  10834. }
  10835. return SCRIPT_CMD_SUCCESS;
  10836. }
  10837. BUILDIN_FUNC(getcastlename)
  10838. {
  10839. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10840. struct guild_castle* gc = guild_mapname2gc(mapname);
  10841. const char* name = (gc) ? gc->castle_name : "";
  10842. script_pushstrcopy(st,name);
  10843. return SCRIPT_CMD_SUCCESS;
  10844. }
  10845. BUILDIN_FUNC(getcastledata)
  10846. {
  10847. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10848. int index = script_getnum(st,3);
  10849. struct guild_castle *gc = guild_mapname2gc(mapname);
  10850. if (gc == NULL) {
  10851. script_pushint(st,0);
  10852. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10853. return 1;
  10854. }
  10855. switch (index) {
  10856. case 1:
  10857. script_pushint(st,gc->guild_id); break;
  10858. case 2:
  10859. script_pushint(st,gc->economy); break;
  10860. case 3:
  10861. script_pushint(st,gc->defense); break;
  10862. case 4:
  10863. script_pushint(st,gc->triggerE); break;
  10864. case 5:
  10865. script_pushint(st,gc->triggerD); break;
  10866. case 6:
  10867. script_pushint(st,gc->nextTime); break;
  10868. case 7:
  10869. script_pushint(st,gc->payTime); break;
  10870. case 8:
  10871. script_pushint(st,gc->createTime); break;
  10872. case 9:
  10873. script_pushint(st,gc->visibleC); break;
  10874. default:
  10875. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10876. script_pushint(st,gc->guardian[index-10].visible);
  10877. break;
  10878. }
  10879. script_pushint(st,0);
  10880. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10881. return 1;
  10882. }
  10883. return SCRIPT_CMD_SUCCESS;
  10884. }
  10885. BUILDIN_FUNC(setcastledata)
  10886. {
  10887. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10888. int index = script_getnum(st,3);
  10889. int value = script_getnum(st,4);
  10890. struct guild_castle *gc = guild_mapname2gc(mapname);
  10891. if (gc == NULL) {
  10892. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10893. return 1;
  10894. }
  10895. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10896. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10897. return 1;
  10898. }
  10899. guild_castledatasave(gc->castle_id, index, value);
  10900. return SCRIPT_CMD_SUCCESS;
  10901. }
  10902. /* =====================================================================
  10903. * ---------------------------------------------------------------------*/
  10904. BUILDIN_FUNC(requestguildinfo)
  10905. {
  10906. int guild_id=script_getnum(st,2);
  10907. const char *event=NULL;
  10908. if( script_hasdata(st,3) ){
  10909. event=script_getstr(st,3);
  10910. check_event(st, event);
  10911. }
  10912. if(guild_id>0)
  10913. guild_npc_request_info(guild_id,event);
  10914. return SCRIPT_CMD_SUCCESS;
  10915. }
  10916. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10917. /// getequipcardcnt(<equipment slot>);
  10918. BUILDIN_FUNC(getequipcardcnt)
  10919. {
  10920. int i=-1,j,num;
  10921. TBL_PC *sd;
  10922. int count;
  10923. num=script_getnum(st,2);
  10924. sd=script_rid2sd(st);
  10925. if (num > 0 && num <= ARRAYLENGTH(equip))
  10926. i=pc_checkequip(sd,equip[num-1]);
  10927. if (i < 0 || !sd->inventory_data[i]) {
  10928. script_pushint(st,0);
  10929. return 0;
  10930. }
  10931. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10932. {
  10933. script_pushint(st,0);
  10934. return 0;
  10935. }
  10936. count = 0;
  10937. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10938. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  10939. count++;
  10940. script_pushint(st,count);
  10941. return SCRIPT_CMD_SUCCESS;
  10942. }
  10943. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10944. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10945. /// successremovecards <slot>;
  10946. BUILDIN_FUNC(successremovecards) {
  10947. int i=-1,c,cardflag=0;
  10948. TBL_PC* sd = script_rid2sd(st);
  10949. int num = script_getnum(st,2);
  10950. if (num > 0 && num <= ARRAYLENGTH(equip))
  10951. i=pc_checkequip(sd,equip[num-1]);
  10952. if (i < 0 || !sd->inventory_data[i]) {
  10953. return 0;
  10954. }
  10955. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10956. return 0;
  10957. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10958. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10959. unsigned char flag = 0;
  10960. struct item item_tmp;
  10961. memset(&item_tmp,0,sizeof(item_tmp));
  10962. cardflag = 1;
  10963. item_tmp.nameid = sd->status.inventory[i].card[c];
  10964. item_tmp.identify = 1;
  10965. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10966. clif_additem(sd,0,0,flag);
  10967. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  10968. }
  10969. }
  10970. }
  10971. if(cardflag == 1) {//if card was remove remplace item with no card
  10972. unsigned char flag = 0, j;
  10973. struct item item_tmp;
  10974. memset(&item_tmp,0,sizeof(item_tmp));
  10975. item_tmp.nameid = sd->status.inventory[i].nameid;
  10976. item_tmp.identify = 1;
  10977. item_tmp.refine = sd->status.inventory[i].refine;
  10978. item_tmp.attribute = sd->status.inventory[i].attribute;
  10979. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10980. item_tmp.bound = sd->status.inventory[i].bound;
  10981. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10982. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10983. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10984. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10985. clif_additem(sd,0,0,flag);
  10986. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  10987. }
  10988. clif_misceffect(&sd->bl,3);
  10989. }
  10990. return SCRIPT_CMD_SUCCESS;
  10991. }
  10992. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10993. /// failedremovecards <slot>, <type>;
  10994. /// <type>=0 : will destroy both the item and the cards.
  10995. /// <type>=1 : will keep the item, but destroy the cards.
  10996. /// <type>=2 : will keep the cards, but destroy the item.
  10997. /// <type>=? : will just display the failure effect.
  10998. BUILDIN_FUNC(failedremovecards) {
  10999. int i=-1,c,cardflag=0;
  11000. TBL_PC* sd = script_rid2sd(st);
  11001. int num = script_getnum(st,2);
  11002. int typefail = script_getnum(st,3);
  11003. if (num > 0 && num <= ARRAYLENGTH(equip))
  11004. i=pc_checkequip(sd,equip[num-1]);
  11005. if (i < 0 || !sd->inventory_data[i])
  11006. return 0;
  11007. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  11008. return 0;
  11009. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11010. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  11011. cardflag = 1;
  11012. if(typefail == 2) {// add cards to inventory, clear
  11013. unsigned char flag = 0;
  11014. struct item item_tmp;
  11015. memset(&item_tmp,0,sizeof(item_tmp));
  11016. item_tmp.nameid = sd->status.inventory[i].card[c];
  11017. item_tmp.identify = 1;
  11018. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11019. clif_additem(sd,0,0,flag);
  11020. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11021. }
  11022. }
  11023. }
  11024. }
  11025. if(cardflag == 1) {
  11026. if(typefail == 0 || typefail == 2){ // destroy the item
  11027. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11028. }
  11029. if(typefail == 1){ // destroy the card
  11030. unsigned char flag = 0, j;
  11031. struct item item_tmp;
  11032. memset(&item_tmp,0,sizeof(item_tmp));
  11033. item_tmp.nameid = sd->status.inventory[i].nameid;
  11034. item_tmp.identify = 1;
  11035. item_tmp.refine = sd->status.inventory[i].refine;
  11036. item_tmp.attribute = sd->status.inventory[i].attribute;
  11037. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  11038. item_tmp.bound = sd->status.inventory[i].bound;
  11039. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11040. item_tmp.card[j]=sd->status.inventory[i].card[j];
  11041. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11042. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11043. clif_additem(sd,0,0,flag);
  11044. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11045. }
  11046. }
  11047. clif_misceffect(&sd->bl,2);
  11048. }
  11049. return SCRIPT_CMD_SUCCESS;
  11050. }
  11051. /* ================================================================
  11052. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11053. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11054. * improved by [Lance]
  11055. * ================================================================*/
  11056. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11057. {
  11058. int x,y,m,check_val=0,check_ID=0,i=0;
  11059. struct guild *g = NULL;
  11060. struct party_data *p = NULL;
  11061. const char *str;
  11062. const char *mapname;
  11063. unsigned int index;
  11064. mapname=script_getstr(st,2);
  11065. str=script_getstr(st,3);
  11066. x=script_getnum(st,4);
  11067. y=script_getnum(st,5);
  11068. if(script_hasdata(st,7)){
  11069. check_val=script_getnum(st,6);
  11070. check_ID=script_getnum(st,7);
  11071. }
  11072. if((m=map_mapname2mapid(mapname))< 0)
  11073. return 0;
  11074. if(!(index=mapindex_name2id(str)))
  11075. return 0;
  11076. switch(check_val){
  11077. case 1:
  11078. g = guild_search(check_ID);
  11079. if (g){
  11080. for( i=0; i < g->max_member; i++)
  11081. {
  11082. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11083. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11084. }
  11085. }
  11086. }
  11087. break;
  11088. case 2:
  11089. p = party_search(check_ID);
  11090. if(p){
  11091. for(i=0;i<MAX_PARTY; i++){
  11092. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11093. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11094. }
  11095. }
  11096. }
  11097. break;
  11098. default:
  11099. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11100. break;
  11101. }
  11102. return SCRIPT_CMD_SUCCESS;
  11103. }
  11104. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11105. {
  11106. char *event=va_arg(ap,char *);
  11107. struct mob_data *md = ((struct mob_data *)bl);
  11108. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11109. return 1;
  11110. return SCRIPT_CMD_SUCCESS;
  11111. }
  11112. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11113. {
  11114. const char *mapname,*event;
  11115. int16 m;
  11116. mapname=script_getstr(st,2);
  11117. event=script_getstr(st,3);
  11118. if( strcmp(event, "all") == 0 )
  11119. event = NULL;
  11120. else
  11121. check_event(st, event);
  11122. if( strcmp(mapname, "this") == 0 ) {
  11123. struct map_session_data *sd = script_rid2sd(st);
  11124. if( sd )
  11125. m = sd->bl.m;
  11126. else {
  11127. script_pushint(st,-1);
  11128. return 0;
  11129. }
  11130. }
  11131. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11132. script_pushint(st,-1);
  11133. return 0;
  11134. }
  11135. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11136. return SCRIPT_CMD_SUCCESS;
  11137. }
  11138. BUILDIN_FUNC(marriage)
  11139. {
  11140. const char *partner=script_getstr(st,2);
  11141. TBL_PC *sd=script_rid2sd(st);
  11142. TBL_PC *p_sd=map_nick2sd(partner);
  11143. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  11144. script_pushint(st,0);
  11145. return 0;
  11146. }
  11147. script_pushint(st,1);
  11148. return SCRIPT_CMD_SUCCESS;
  11149. }
  11150. BUILDIN_FUNC(wedding_effect)
  11151. {
  11152. TBL_PC *sd=script_rid2sd(st);
  11153. struct block_list *bl;
  11154. if(sd==NULL) {
  11155. bl=map_id2bl(st->oid);
  11156. } else
  11157. bl=&sd->bl;
  11158. clif_wedding_effect(bl);
  11159. return SCRIPT_CMD_SUCCESS;
  11160. }
  11161. /**
  11162. * divorce({<char_id>})
  11163. **/
  11164. BUILDIN_FUNC(divorce)
  11165. {
  11166. TBL_PC *sd;
  11167. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11168. script_pushint(st,0);
  11169. return SCRIPT_CMD_FAILURE;
  11170. }
  11171. script_pushint(st,1);
  11172. return SCRIPT_CMD_SUCCESS;
  11173. }
  11174. /**
  11175. * ispartneron({<char_id>})
  11176. **/
  11177. BUILDIN_FUNC(ispartneron)
  11178. {
  11179. TBL_PC *sd;
  11180. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11181. map_charid2sd(sd->status.partner_id) == NULL)
  11182. {
  11183. script_pushint(st,0);
  11184. return SCRIPT_CMD_FAILURE;
  11185. }
  11186. script_pushint(st,1);
  11187. return SCRIPT_CMD_SUCCESS;
  11188. }
  11189. /**
  11190. * getpartnerid({<char_id>})
  11191. **/
  11192. BUILDIN_FUNC(getpartnerid)
  11193. {
  11194. TBL_PC *sd;
  11195. if (!script_charid2sd(2,sd)) {
  11196. script_pushint(st,0);
  11197. return SCRIPT_CMD_FAILURE;
  11198. }
  11199. script_pushint(st,sd->status.partner_id);
  11200. return SCRIPT_CMD_SUCCESS;
  11201. }
  11202. /**
  11203. * getchildid({<char_id>})
  11204. **/
  11205. BUILDIN_FUNC(getchildid)
  11206. {
  11207. TBL_PC *sd;
  11208. if (!script_charid2sd(2,sd)) {
  11209. script_pushint(st,0);
  11210. return SCRIPT_CMD_FAILURE;
  11211. }
  11212. script_pushint(st,sd->status.child);
  11213. return SCRIPT_CMD_SUCCESS;
  11214. }
  11215. /**
  11216. * getmotherid({<char_id>})
  11217. **/
  11218. BUILDIN_FUNC(getmotherid)
  11219. {
  11220. TBL_PC *sd;
  11221. if (!script_charid2sd(2,sd)) {
  11222. script_pushint(st,0);
  11223. return SCRIPT_CMD_FAILURE;
  11224. }
  11225. script_pushint(st,sd->status.mother);
  11226. return SCRIPT_CMD_SUCCESS;
  11227. }
  11228. /**
  11229. * getfatherid({<char_id>})
  11230. **/
  11231. BUILDIN_FUNC(getfatherid)
  11232. {
  11233. TBL_PC *sd;
  11234. if (!script_charid2sd(2,sd)) {
  11235. script_pushint(st,0);
  11236. return SCRIPT_CMD_FAILURE;
  11237. }
  11238. script_pushint(st,sd->status.father);
  11239. return SCRIPT_CMD_SUCCESS;
  11240. }
  11241. BUILDIN_FUNC(warppartner)
  11242. {
  11243. int x,y;
  11244. unsigned short mapindex;
  11245. const char *str;
  11246. TBL_PC *sd=script_rid2sd(st);
  11247. TBL_PC *p_sd=NULL;
  11248. if(sd==NULL || !pc_ismarried(sd) ||
  11249. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11250. script_pushint(st,0);
  11251. return 0;
  11252. }
  11253. str=script_getstr(st,2);
  11254. x=script_getnum(st,3);
  11255. y=script_getnum(st,4);
  11256. mapindex = mapindex_name2id(str);
  11257. if (mapindex) {
  11258. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11259. script_pushint(st,1);
  11260. } else
  11261. script_pushint(st,0);
  11262. return SCRIPT_CMD_SUCCESS;
  11263. }
  11264. /*================================================
  11265. * Script for Displaying MOB Information [Valaris]
  11266. *------------------------------------------------*/
  11267. BUILDIN_FUNC(strmobinfo)
  11268. {
  11269. int num=script_getnum(st,2);
  11270. int class_=script_getnum(st,3);
  11271. if(!mobdb_checkid(class_))
  11272. {
  11273. if (num < 3) //requested a string
  11274. script_pushconststr(st,"");
  11275. else
  11276. script_pushint(st,0);
  11277. return 0;
  11278. }
  11279. switch (num) {
  11280. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11281. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11282. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11283. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11284. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11285. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11286. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11287. default:
  11288. script_pushint(st,0);
  11289. break;
  11290. }
  11291. return SCRIPT_CMD_SUCCESS;
  11292. }
  11293. /*==========================================
  11294. * Summon guardians [Valaris]
  11295. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11296. *------------------------------------------*/
  11297. BUILDIN_FUNC(guardian)
  11298. {
  11299. int class_=0,x=0,y=0,guardian=0;
  11300. const char *str,*mapname,*evt="";
  11301. bool has_index = false;
  11302. mapname =script_getstr(st,2);
  11303. x =script_getnum(st,3);
  11304. y =script_getnum(st,4);
  11305. str =script_getstr(st,5);
  11306. class_=script_getnum(st,6);
  11307. if( script_hasdata(st,8) )
  11308. {// "<event label>",<guardian index>
  11309. evt=script_getstr(st,7);
  11310. guardian=script_getnum(st,8);
  11311. has_index = true;
  11312. } else if( script_hasdata(st,7) ){
  11313. struct script_data *data = script_getdata(st,7);
  11314. get_val(st,data);
  11315. if( data_isstring(data) )
  11316. {// "<event label>"
  11317. evt=script_getstr(st,7);
  11318. } else if( data_isint(data) )
  11319. {// <guardian index>
  11320. guardian=script_getnum(st,7);
  11321. has_index = true;
  11322. } else {
  11323. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11324. script_reportdata(data);
  11325. return 1;
  11326. }
  11327. }
  11328. check_event(st, evt);
  11329. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11330. return SCRIPT_CMD_SUCCESS;
  11331. }
  11332. /*==========================================
  11333. * Invisible Walls [Zephyrus]
  11334. *------------------------------------------*/
  11335. BUILDIN_FUNC(setwall)
  11336. {
  11337. const char *mapname, *name;
  11338. int x, y, m, size, dir;
  11339. bool shootable;
  11340. mapname = script_getstr(st,2);
  11341. x = script_getnum(st,3);
  11342. y = script_getnum(st,4);
  11343. size = script_getnum(st,5);
  11344. dir = script_getnum(st,6);
  11345. shootable = script_getnum(st,7);
  11346. name = script_getstr(st,8);
  11347. if( (m = map_mapname2mapid(mapname)) < 0 )
  11348. return 0; // Invalid Map
  11349. map_iwall_set(m, x, y, size, dir, shootable, name);
  11350. return SCRIPT_CMD_SUCCESS;
  11351. }
  11352. BUILDIN_FUNC(delwall)
  11353. {
  11354. const char *name = script_getstr(st,2);
  11355. map_iwall_remove(name);
  11356. return SCRIPT_CMD_SUCCESS;
  11357. }
  11358. /// Retrieves various information about the specified guardian.
  11359. ///
  11360. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11361. /// type: 0 - whether it is deployed or not
  11362. /// 1 - maximum hp
  11363. /// 2 - current hp
  11364. ///
  11365. BUILDIN_FUNC(guardianinfo)
  11366. {
  11367. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11368. int id = script_getnum(st,3);
  11369. int type = script_getnum(st,4);
  11370. struct guild_castle* gc = guild_mapname2gc(mapname);
  11371. struct mob_data* gd;
  11372. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11373. {
  11374. script_pushint(st,-1);
  11375. return 0;
  11376. }
  11377. if( type == 0 )
  11378. script_pushint(st, gc->guardian[id].visible);
  11379. else
  11380. if( !gc->guardian[id].visible )
  11381. script_pushint(st,-1);
  11382. else
  11383. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11384. script_pushint(st,-1);
  11385. else
  11386. {
  11387. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11388. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11389. else
  11390. script_pushint(st,-1);
  11391. }
  11392. return SCRIPT_CMD_SUCCESS;
  11393. }
  11394. /*==========================================
  11395. * Get the item name by item_id or null
  11396. *------------------------------------------*/
  11397. BUILDIN_FUNC(getitemname)
  11398. {
  11399. unsigned short item_id = 0;
  11400. struct item_data *i_data;
  11401. char *item_name;
  11402. struct script_data *data;
  11403. data=script_getdata(st,2);
  11404. get_val(st,data);
  11405. if( data_isstring(data) ){
  11406. const char *name=conv_str(st,data);
  11407. struct item_data *item_data = itemdb_searchname(name);
  11408. if( item_data )
  11409. item_id=item_data->nameid;
  11410. }else
  11411. item_id=conv_num(st,data);
  11412. i_data = itemdb_exists(item_id);
  11413. if (i_data == NULL)
  11414. {
  11415. script_pushconststr(st,"null");
  11416. return 0;
  11417. }
  11418. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11419. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11420. script_pushstr(st,item_name);
  11421. return SCRIPT_CMD_SUCCESS;
  11422. }
  11423. /*==========================================
  11424. * Returns number of slots an item has. [Skotlex]
  11425. *------------------------------------------*/
  11426. BUILDIN_FUNC(getitemslots)
  11427. {
  11428. unsigned short item_id;
  11429. struct item_data *i_data;
  11430. item_id=script_getnum(st,2);
  11431. i_data = itemdb_exists(item_id);
  11432. if (i_data)
  11433. script_pushint(st,i_data->slot);
  11434. else
  11435. script_pushint(st,-1);
  11436. return SCRIPT_CMD_SUCCESS;
  11437. }
  11438. // TODO: add matk here if needed/once we get rid of RENEWAL
  11439. /*==========================================
  11440. * Returns some values of an item [Lupus]
  11441. * Price, Weight, etc...
  11442. getiteminfo(itemID,n), where n
  11443. 0 value_buy;
  11444. 1 value_sell;
  11445. 2 type;
  11446. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11447. if = 0, then monsters don't drop it at all (rare or a quest item)
  11448. if = -1, then this item is sold in NPC shops only
  11449. 4 sex;
  11450. 5 equip;
  11451. 6 weight;
  11452. 7 atk;
  11453. 8 def;
  11454. 9 range;
  11455. 10 slot;
  11456. 11 look;
  11457. 12 elv;
  11458. 13 wlv;
  11459. 14 view id
  11460. 15 eLvmax
  11461. 16 matk (renewal)
  11462. *------------------------------------------*/
  11463. BUILDIN_FUNC(getiteminfo)
  11464. {
  11465. unsigned short item_id,n;
  11466. struct item_data *i_data;
  11467. item_id = script_getnum(st,2);
  11468. n = script_getnum(st,3);
  11469. i_data = itemdb_exists(item_id);
  11470. if (i_data && n <= 16) {
  11471. int *item_arr = (int*)&i_data->value_buy;
  11472. #ifndef RENEWAL
  11473. if (n == 16)
  11474. script_pushint(st,0);
  11475. else
  11476. #endif
  11477. script_pushint(st,item_arr[n]);
  11478. } else
  11479. script_pushint(st,-1);
  11480. return SCRIPT_CMD_SUCCESS;
  11481. }
  11482. /*==========================================
  11483. * Set some values of an item [Lupus]
  11484. * Price, Weight, etc...
  11485. setiteminfo(itemID,n,Value), where n
  11486. 0 value_buy;
  11487. 1 value_sell;
  11488. 2 type;
  11489. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11490. if = 0, then monsters don't drop it at all (rare or a quest item)
  11491. if = -1, then this item is sold in NPC shops only
  11492. 4 sex;
  11493. 5 equip;
  11494. 6 weight;
  11495. 7 atk;
  11496. 8 def;
  11497. 9 range;
  11498. 10 slot;
  11499. 11 look;
  11500. 12 elv;
  11501. 13 wlv;
  11502. 14 view id
  11503. * Returns Value or -1 if the wrong field's been set
  11504. *------------------------------------------*/
  11505. BUILDIN_FUNC(setiteminfo)
  11506. {
  11507. unsigned short item_id;
  11508. int n,value;
  11509. struct item_data *i_data;
  11510. item_id = script_getnum(st,2);
  11511. n = script_getnum(st,3);
  11512. value = script_getnum(st,4);
  11513. i_data = itemdb_exists(item_id);
  11514. if (i_data && n>=0 && n<=14) {
  11515. int *item_arr = (int*)&i_data->value_buy;
  11516. item_arr[n] = value;
  11517. script_pushint(st,value);
  11518. } else
  11519. script_pushint(st,-1);
  11520. return SCRIPT_CMD_SUCCESS;
  11521. }
  11522. /*==========================================
  11523. * Returns value from equipped item slot n [Lupus]
  11524. getequipcardid(num,slot)
  11525. where
  11526. num = eqip position slot
  11527. slot = 0,1,2,3 (Card Slot N)
  11528. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11529. it's useful when you want to check item cards or if it's signed
  11530. Useful for such quests as "Sign this refined item with players name" etc
  11531. Hat[0] +4 -> Player's Hat[0] +4
  11532. *------------------------------------------*/
  11533. BUILDIN_FUNC(getequipcardid)
  11534. {
  11535. int i=-1,num,slot;
  11536. TBL_PC *sd;
  11537. num=script_getnum(st,2);
  11538. slot=script_getnum(st,3);
  11539. sd=script_rid2sd(st);
  11540. if (num > 0 && num <= ARRAYLENGTH(equip))
  11541. i=pc_checkequip(sd,equip[num-1]);
  11542. if(i >= 0 && slot>=0 && slot<4)
  11543. script_pushint(st,sd->status.inventory[i].card[slot]);
  11544. else
  11545. script_pushint(st,0);
  11546. return SCRIPT_CMD_SUCCESS;
  11547. }
  11548. /*==========================================
  11549. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11550. *------------------------------------------*/
  11551. BUILDIN_FUNC(petskillbonus)
  11552. {
  11553. struct pet_data *pd;
  11554. TBL_PC *sd = script_rid2sd(st);
  11555. if(sd == NULL || sd->pd == NULL)
  11556. return SCRIPT_CMD_FAILURE;
  11557. pd = sd->pd;
  11558. if (pd->bonus)
  11559. { //Clear previous bonus
  11560. if (pd->bonus->timer != INVALID_TIMER)
  11561. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11562. } else //init
  11563. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11564. pd->bonus->type = script_getnum(st,2);
  11565. pd->bonus->val = script_getnum(st,3);
  11566. pd->bonus->duration = script_getnum(st,4);
  11567. pd->bonus->delay = script_getnum(st,5);
  11568. if (pd->state.skillbonus == 1)
  11569. pd->state.skillbonus = 0; // waiting state
  11570. // wait for timer to start
  11571. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11572. pd->bonus->timer = INVALID_TIMER;
  11573. else
  11574. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11575. return SCRIPT_CMD_SUCCESS;
  11576. }
  11577. /*==========================================
  11578. * pet looting [Valaris] //Rewritten by [Skotlex]
  11579. *------------------------------------------*/
  11580. BUILDIN_FUNC(petloot)
  11581. {
  11582. int max;
  11583. struct pet_data *pd;
  11584. TBL_PC *sd=script_rid2sd(st);
  11585. if(sd==NULL || sd->pd==NULL)
  11586. return 0;
  11587. max=script_getnum(st,2);
  11588. if(max < 1)
  11589. max = 1; //Let'em loot at least 1 item.
  11590. else if (max > MAX_PETLOOT_SIZE)
  11591. max = MAX_PETLOOT_SIZE;
  11592. pd = sd->pd;
  11593. if (pd->loot != NULL)
  11594. { //Release whatever was there already and reallocate memory
  11595. pet_lootitem_drop(pd, pd->master);
  11596. aFree(pd->loot->item);
  11597. }
  11598. else
  11599. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11600. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11601. pd->loot->max=max;
  11602. pd->loot->count = 0;
  11603. pd->loot->weight = 0;
  11604. return SCRIPT_CMD_SUCCESS;
  11605. }
  11606. /*==========================================
  11607. * Set arrays with info of all sd inventory :
  11608. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11609. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11610. * @inventorylist_card(0..3), @inventorylist_expire
  11611. * @inventorylist_count = scalar
  11612. *------------------------------------------*/
  11613. BUILDIN_FUNC(getinventorylist)
  11614. {
  11615. TBL_PC *sd;
  11616. char card_var[NAME_LENGTH];
  11617. int i,j=0,k;
  11618. if (!script_charid2sd(2,sd))
  11619. return SCRIPT_CMD_FAILURE;
  11620. for(i=0;i<MAX_INVENTORY;i++){
  11621. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  11622. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  11623. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  11624. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  11625. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  11626. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  11627. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  11628. for (k = 0; k < MAX_SLOTS; k++)
  11629. {
  11630. sprintf(card_var, "@inventorylist_card%d",k+1);
  11631. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  11632. }
  11633. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  11634. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  11635. j++;
  11636. }
  11637. }
  11638. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11639. return SCRIPT_CMD_SUCCESS;
  11640. }
  11641. /**
  11642. * getskilllist ({<char_id>});
  11643. **/
  11644. BUILDIN_FUNC(getskilllist)
  11645. {
  11646. TBL_PC *sd;
  11647. int i,j=0;
  11648. if (!script_charid2sd(2,sd))
  11649. return SCRIPT_CMD_FAILURE;
  11650. for(i=0;i<MAX_SKILL;i++){
  11651. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11652. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11653. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11654. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11655. j++;
  11656. }
  11657. }
  11658. pc_setreg(sd,add_str("@skilllist_count"),j);
  11659. return SCRIPT_CMD_SUCCESS;
  11660. }
  11661. /**
  11662. * clearitem ({<char_id>});
  11663. **/
  11664. BUILDIN_FUNC(clearitem)
  11665. {
  11666. TBL_PC *sd;
  11667. int i;
  11668. if (!script_charid2sd(2,sd))
  11669. return SCRIPT_CMD_FAILURE;
  11670. for (i=0; i<MAX_INVENTORY; i++) {
  11671. if (sd->status.inventory[i].amount) {
  11672. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11673. }
  11674. }
  11675. return SCRIPT_CMD_SUCCESS;
  11676. }
  11677. /**
  11678. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11679. * disguise <Monster ID>{,<char_id>};
  11680. **/
  11681. BUILDIN_FUNC(disguise)
  11682. {
  11683. int id;
  11684. TBL_PC* sd;
  11685. if (!script_charid2sd(3,sd))
  11686. return SCRIPT_CMD_FAILURE;
  11687. id = script_getnum(st,2);
  11688. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11689. pc_disguise(sd, id);
  11690. script_pushint(st,id);
  11691. } else
  11692. script_pushint(st,0);
  11693. return SCRIPT_CMD_SUCCESS;
  11694. }
  11695. /**
  11696. * Undisguise Player (returns 1 if success, 0 on fail)
  11697. * undisguise {<char_id>};
  11698. **/
  11699. BUILDIN_FUNC(undisguise)
  11700. {
  11701. TBL_PC* sd;
  11702. if (!script_charid2sd(2,sd))
  11703. return SCRIPT_CMD_FAILURE;
  11704. if (sd->disguise) {
  11705. pc_disguise(sd, 0);
  11706. script_pushint(st,0);
  11707. } else {
  11708. script_pushint(st,1);
  11709. }
  11710. return SCRIPT_CMD_SUCCESS;
  11711. }
  11712. /*==========================================
  11713. * Transform a bl to another _class,
  11714. * @type unused
  11715. *------------------------------------------*/
  11716. BUILDIN_FUNC(classchange)
  11717. {
  11718. int _class,type;
  11719. struct block_list *bl=map_id2bl(st->oid);
  11720. if(bl==NULL) return 0;
  11721. _class=script_getnum(st,2);
  11722. type=script_getnum(st,3);
  11723. clif_class_change(bl,_class,type);
  11724. return SCRIPT_CMD_SUCCESS;
  11725. }
  11726. /*==========================================
  11727. * Display an effect
  11728. *------------------------------------------*/
  11729. BUILDIN_FUNC(misceffect)
  11730. {
  11731. int type;
  11732. type=script_getnum(st,2);
  11733. if(st->oid && st->oid != fake_nd->bl.id) {
  11734. struct block_list *bl = map_id2bl(st->oid);
  11735. if (bl)
  11736. clif_specialeffect(bl,type,AREA);
  11737. } else{
  11738. TBL_PC *sd=script_rid2sd(st);
  11739. if(sd)
  11740. clif_specialeffect(&sd->bl,type,AREA);
  11741. }
  11742. return SCRIPT_CMD_SUCCESS;
  11743. }
  11744. /*==========================================
  11745. * Play a BGM on a single client [Rikter/Yommy]
  11746. *------------------------------------------*/
  11747. BUILDIN_FUNC(playBGM)
  11748. {
  11749. struct map_session_data* sd;
  11750. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11751. clif_playBGM(sd, script_getstr(st,2));
  11752. }
  11753. return SCRIPT_CMD_SUCCESS;
  11754. }
  11755. static int playBGM_sub(struct block_list* bl,va_list ap)
  11756. {
  11757. const char* name = va_arg(ap,const char*);
  11758. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11759. return 0;
  11760. }
  11761. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11762. {
  11763. const char* name = va_arg(args, const char*);
  11764. clif_playBGM(sd, name);
  11765. return 0;
  11766. }
  11767. /*==========================================
  11768. * Play a BGM on multiple client [Rikter/Yommy]
  11769. *------------------------------------------*/
  11770. BUILDIN_FUNC(playBGMall)
  11771. {
  11772. const char* name;
  11773. name = script_getstr(st,2);
  11774. if( script_hasdata(st,7) ) {// specified part of map
  11775. const char* mapname = script_getstr(st,3);
  11776. int x0 = script_getnum(st,4);
  11777. int y0 = script_getnum(st,5);
  11778. int x1 = script_getnum(st,6);
  11779. int y1 = script_getnum(st,7);
  11780. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  11781. }
  11782. else if( script_hasdata(st,3) ) {// entire map
  11783. const char* mapname = script_getstr(st,3);
  11784. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  11785. }
  11786. else {// entire server
  11787. map_foreachpc(&playBGM_foreachpc_sub, name);
  11788. }
  11789. return SCRIPT_CMD_SUCCESS;
  11790. }
  11791. /*==========================================
  11792. * Play a .wav sound for sd
  11793. *------------------------------------------*/
  11794. BUILDIN_FUNC(soundeffect)
  11795. {
  11796. TBL_PC* sd = script_rid2sd(st);
  11797. const char* name = script_getstr(st,2);
  11798. int type = script_getnum(st,3);
  11799. if(sd) {
  11800. clif_soundeffect(sd,&sd->bl,name,type);
  11801. }
  11802. return SCRIPT_CMD_SUCCESS;
  11803. }
  11804. int soundeffect_sub(struct block_list* bl,va_list ap)
  11805. {
  11806. char* name = va_arg(ap,char*);
  11807. int type = va_arg(ap,int);
  11808. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11809. return 0;
  11810. }
  11811. /*==========================================
  11812. * Play a sound effect (.wav) on multiple clients
  11813. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11814. *------------------------------------------*/
  11815. BUILDIN_FUNC(soundeffectall)
  11816. {
  11817. struct block_list* bl;
  11818. const char* name;
  11819. int type;
  11820. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11821. if (!bl)
  11822. return 0;
  11823. name = script_getstr(st,2);
  11824. type = script_getnum(st,3);
  11825. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11826. if(!script_hasdata(st,4))
  11827. { // area around
  11828. clif_soundeffectall(bl, name, type, AREA);
  11829. }
  11830. else
  11831. if(!script_hasdata(st,5))
  11832. { // entire map
  11833. const char* mapname = script_getstr(st,4);
  11834. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  11835. }
  11836. else
  11837. if(script_hasdata(st,8))
  11838. { // specified part of map
  11839. const char* mapname = script_getstr(st,4);
  11840. int x0 = script_getnum(st,5);
  11841. int y0 = script_getnum(st,6);
  11842. int x1 = script_getnum(st,7);
  11843. int y1 = script_getnum(st,8);
  11844. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  11845. }
  11846. else
  11847. {
  11848. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11849. }
  11850. return SCRIPT_CMD_SUCCESS;
  11851. }
  11852. /*==========================================
  11853. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11854. *------------------------------------------*/
  11855. BUILDIN_FUNC(petrecovery)
  11856. {
  11857. struct pet_data *pd;
  11858. TBL_PC *sd=script_rid2sd(st);
  11859. int sc;
  11860. if(sd == NULL || sd->pd == NULL)
  11861. return SCRIPT_CMD_FAILURE;
  11862. sc = script_getnum(st,2);
  11863. if (sc <= SC_NONE || sc >= SC_MAX) {
  11864. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  11865. return SCRIPT_CMD_FAILURE;
  11866. }
  11867. pd = sd->pd;
  11868. if (pd->recovery)
  11869. { //Halt previous bonus
  11870. if (pd->recovery->timer != INVALID_TIMER)
  11871. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11872. } else //Init
  11873. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11874. pd->recovery->type = (sc_type)sc;
  11875. pd->recovery->delay = script_getnum(st,3);
  11876. pd->recovery->timer = INVALID_TIMER;
  11877. return SCRIPT_CMD_SUCCESS;
  11878. }
  11879. /*==========================================
  11880. * pet healing [Valaris] //Rewritten by [Skotlex]
  11881. *------------------------------------------*/
  11882. BUILDIN_FUNC(petheal)
  11883. {
  11884. struct pet_data *pd;
  11885. TBL_PC *sd=script_rid2sd(st);
  11886. if(sd==NULL || sd->pd==NULL)
  11887. return 0;
  11888. pd=sd->pd;
  11889. if (pd->s_skill)
  11890. { //Clear previous skill
  11891. if (pd->s_skill->timer != INVALID_TIMER)
  11892. {
  11893. if (pd->s_skill->id)
  11894. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11895. else
  11896. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11897. }
  11898. } else //init memory
  11899. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11900. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11901. //Use the lv as the amount to heal
  11902. pd->s_skill->lv=script_getnum(st,2);
  11903. pd->s_skill->delay=script_getnum(st,3);
  11904. pd->s_skill->hp=script_getnum(st,4);
  11905. pd->s_skill->sp=script_getnum(st,5);
  11906. //Use delay as initial offset to avoid skill/heal exploits
  11907. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11908. pd->s_skill->timer = INVALID_TIMER;
  11909. else
  11910. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11911. return SCRIPT_CMD_SUCCESS;
  11912. }
  11913. /*==========================================
  11914. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11915. *------------------------------------------*/
  11916. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11917. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11918. BUILDIN_FUNC(petskillattack)
  11919. {
  11920. struct pet_data *pd;
  11921. struct script_data *data;
  11922. TBL_PC *sd = script_rid2sd(st);
  11923. int id = 0;
  11924. if(sd == NULL || sd->pd == NULL)
  11925. return SCRIPT_CMD_FAILURE;
  11926. data = script_getdata(st, 2);
  11927. get_val(st, data);
  11928. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11929. if (!id) {
  11930. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  11931. return SCRIPT_CMD_FAILURE;
  11932. }
  11933. pd = sd->pd;
  11934. if (pd->a_skill == NULL)
  11935. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11936. pd->a_skill->id = id;
  11937. pd->a_skill->damage = 0;
  11938. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  11939. pd->a_skill->div_ = 0;
  11940. pd->a_skill->rate = script_getnum(st,4);
  11941. pd->a_skill->bonusrate = script_getnum(st,5);
  11942. return SCRIPT_CMD_SUCCESS;
  11943. }
  11944. /*==========================================
  11945. * pet attack skills [Valaris]
  11946. *------------------------------------------*/
  11947. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  11948. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  11949. BUILDIN_FUNC(petskillattack2)
  11950. {
  11951. struct pet_data *pd;
  11952. struct script_data *data;
  11953. TBL_PC *sd = script_rid2sd(st);
  11954. int id = 0;
  11955. if(sd == NULL || sd->pd == NULL)
  11956. return SCRIPT_CMD_FAILURE;
  11957. data = script_getdata(st, 2);
  11958. get_val(st, data);
  11959. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11960. if (!id) {
  11961. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  11962. return SCRIPT_CMD_FAILURE;
  11963. }
  11964. pd = sd->pd;
  11965. if (pd->a_skill == NULL)
  11966. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11967. pd->a_skill->id = id;
  11968. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  11969. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  11970. pd->a_skill->div_ = script_getnum(st,4);
  11971. pd->a_skill->rate = script_getnum(st,5);
  11972. pd->a_skill->bonusrate = script_getnum(st,6);
  11973. return SCRIPT_CMD_SUCCESS;
  11974. }
  11975. /*==========================================
  11976. * pet support skills [Skotlex]
  11977. *------------------------------------------*/
  11978. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  11979. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  11980. BUILDIN_FUNC(petskillsupport)
  11981. {
  11982. struct pet_data *pd;
  11983. struct script_data *data;
  11984. TBL_PC *sd = script_rid2sd(st);
  11985. int id = 0;
  11986. if(sd == NULL || sd->pd == NULL)
  11987. return SCRIPT_CMD_FAILURE;
  11988. data = script_getdata(st, 2);
  11989. get_val(st, data);
  11990. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11991. if (!id) {
  11992. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  11993. return SCRIPT_CMD_FAILURE;
  11994. }
  11995. pd = sd->pd;
  11996. if (pd->s_skill)
  11997. { //Clear previous skill
  11998. if (pd->s_skill->timer != INVALID_TIMER)
  11999. {
  12000. if (pd->s_skill->id)
  12001. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12002. else
  12003. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12004. }
  12005. } else //init memory
  12006. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12007. pd->s_skill->id = id;
  12008. pd->s_skill->lv = script_getnum(st,3);
  12009. pd->s_skill->delay = script_getnum(st,4);
  12010. pd->s_skill->hp = script_getnum(st,5);
  12011. pd->s_skill->sp = script_getnum(st,6);
  12012. //Use delay as initial offset to avoid skill/heal exploits
  12013. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12014. pd->s_skill->timer = INVALID_TIMER;
  12015. else
  12016. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12017. return SCRIPT_CMD_SUCCESS;
  12018. }
  12019. /*==========================================
  12020. * Scripted skill effects [Celest]
  12021. *------------------------------------------*/
  12022. /// skilleffect <skill id>,<level>
  12023. /// skilleffect "<skill name>",<level>
  12024. BUILDIN_FUNC(skilleffect)
  12025. {
  12026. TBL_PC *sd;
  12027. uint16 skill_id, skill_lv;
  12028. struct script_data *data = script_getdata(st, 2);
  12029. get_val(st, data); // Convert into value in case of a variable
  12030. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12031. skill_lv = script_getnum(st,3);
  12032. sd = script_rid2sd(st);
  12033. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12034. * which leaves the server thinking it still is sitting. */
  12035. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12036. skill_sit(sd, 0);
  12037. clif_standing(&sd->bl);
  12038. }
  12039. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  12040. return SCRIPT_CMD_SUCCESS;
  12041. }
  12042. /*==========================================
  12043. * NPC skill effects [Valaris]
  12044. *------------------------------------------*/
  12045. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12046. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12047. BUILDIN_FUNC(npcskilleffect)
  12048. {
  12049. struct block_list *bl= map_id2bl(st->oid);
  12050. uint16 skill_id, skill_lv;
  12051. int x, y;
  12052. struct script_data *data = script_getdata(st, 2);
  12053. get_val(st, data); // Convert into value in case of a variable
  12054. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12055. skill_lv=script_getnum(st,3);
  12056. x=script_getnum(st,4);
  12057. y=script_getnum(st,5);
  12058. if (bl)
  12059. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12060. return SCRIPT_CMD_SUCCESS;
  12061. }
  12062. /*==========================================
  12063. * Special effects [Valaris]
  12064. *------------------------------------------*/
  12065. BUILDIN_FUNC(specialeffect)
  12066. {
  12067. struct block_list *bl=map_id2bl(st->oid);
  12068. int type = script_getnum(st,2);
  12069. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12070. if(bl==NULL)
  12071. return 0;
  12072. if( script_hasdata(st,4) )
  12073. {
  12074. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12075. if(nd)
  12076. clif_specialeffect(&nd->bl, type, target);
  12077. }
  12078. else
  12079. {
  12080. if (target == SELF) {
  12081. TBL_PC *sd=script_rid2sd(st);
  12082. if (sd)
  12083. clif_specialeffect_single(bl,type,sd->fd);
  12084. } else {
  12085. clif_specialeffect(bl, type, target);
  12086. }
  12087. }
  12088. return SCRIPT_CMD_SUCCESS;
  12089. }
  12090. BUILDIN_FUNC(specialeffect2)
  12091. {
  12092. TBL_PC *sd=script_rid2sd(st);
  12093. int type = script_getnum(st,2);
  12094. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12095. if( script_hasdata(st,4) )
  12096. sd = map_nick2sd(script_getstr(st,4));
  12097. if (sd)
  12098. clif_specialeffect(&sd->bl, type, target);
  12099. return SCRIPT_CMD_SUCCESS;
  12100. }
  12101. /**
  12102. * nude({<char_id>});
  12103. * @author [Valaris]
  12104. **/
  12105. BUILDIN_FUNC(nude)
  12106. {
  12107. TBL_PC *sd;
  12108. int i, calcflag = 0;
  12109. if (!script_charid2sd(2,sd))
  12110. return SCRIPT_CMD_FAILURE;
  12111. for( i = 0 ; i < EQI_MAX; i++ ) {
  12112. if( sd->equip_index[ i ] >= 0 ) {
  12113. if( !calcflag )
  12114. calcflag = 1;
  12115. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12116. }
  12117. }
  12118. if( calcflag )
  12119. status_calc_pc(sd,SCO_NONE);
  12120. return SCRIPT_CMD_SUCCESS;
  12121. }
  12122. int atcommand_sub(struct script_state* st,int type) {
  12123. TBL_PC *sd, dummy_sd;
  12124. int fd;
  12125. const char *cmd;
  12126. cmd = script_getstr(st,2);
  12127. if (st->rid) {
  12128. sd = script_rid2sd(st);
  12129. fd = sd->fd;
  12130. } else { //Use a dummy character.
  12131. sd = &dummy_sd;
  12132. fd = 0;
  12133. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12134. if (st->oid) {
  12135. struct block_list* bl = map_id2bl(st->oid);
  12136. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12137. if (bl->type == BL_NPC)
  12138. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12139. sd->mapindex = (bl->m > 0) ? bl->m : mapindex_name2id(map_default.mapname);
  12140. }
  12141. // Init Group ID, Level, & permissions
  12142. sd->group_id = sd->group_level = 99;
  12143. sd->permissions |= PC_PERM_ALLPERMISSION;
  12144. }
  12145. if (!is_atcommand(fd, sd, cmd, type)) {
  12146. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12147. script_reportsrc(st);
  12148. return SCRIPT_CMD_FAILURE;
  12149. }
  12150. return SCRIPT_CMD_SUCCESS;
  12151. }
  12152. /*==========================================
  12153. * gmcommand [MouseJstr]
  12154. *------------------------------------------*/
  12155. BUILDIN_FUNC(atcommand) {
  12156. return atcommand_sub(st,0);
  12157. }
  12158. /** Displays a message for the player only (like system messages like "you got an apple" )
  12159. * dispbottom("<message>"{,<color>{,<char_id>}})
  12160. * @param message
  12161. * @param color Hex color default (Green)
  12162. */
  12163. BUILDIN_FUNC(dispbottom)
  12164. {
  12165. TBL_PC *sd;
  12166. int color = 0;
  12167. const char *message;
  12168. if (!script_charid2sd(4,sd))
  12169. return SCRIPT_CMD_FAILURE;
  12170. message = script_getstr(st,2);
  12171. if (script_hasdata(st,3))
  12172. color = script_getnum(st,3); // <color>
  12173. if(sd) {
  12174. if (script_hasdata(st,3))
  12175. clif_messagecolor2(sd, color, message); // [Napster]
  12176. else
  12177. clif_disp_onlyself(sd, message, (int)strlen(message));
  12178. }
  12179. return SCRIPT_CMD_SUCCESS;
  12180. }
  12181. /*===================================
  12182. * Heal portion of recovery command
  12183. *-----------------------------------*/
  12184. int recovery_sub(struct map_session_data* sd, int revive)
  12185. {
  12186. if(revive&(1|4) && pc_isdead(sd)) {
  12187. status_revive(&sd->bl, 100, 100);
  12188. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12189. clif_specialeffect(&sd->bl, 77, AREA);
  12190. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12191. status_percent_heal(&sd->bl, 100, 100);
  12192. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12193. }
  12194. return SCRIPT_CMD_SUCCESS;
  12195. }
  12196. /*=========================================================================
  12197. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12198. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12199. * <type> determines <option>:
  12200. * 0 : char_id
  12201. * 1 : party_id
  12202. * 2 : guild_id
  12203. * 3 : map_name
  12204. * 4 : all characters
  12205. * <revive_flag>:
  12206. * 1 : Revive and heal all players (default)
  12207. * 2 : Heal living players only
  12208. * 4 : Revive dead players only
  12209. * <map name>:
  12210. * for types 1-2 : map_name (null = all maps)
  12211. *-------------------------------------------------------------------------*/
  12212. BUILDIN_FUNC(recovery)
  12213. {
  12214. TBL_PC *sd;
  12215. int map_idx = 0, type = 0, revive = 1;
  12216. type = script_getnum(st,2);
  12217. if(script_hasdata(st,4))
  12218. revive = script_getnum(st,4);
  12219. switch(type) {
  12220. case 0:
  12221. if(script_hasdata(st,3))
  12222. sd=map_charid2sd(script_getnum(st,3));
  12223. else if((sd = script_rid2sd(st)) == NULL)
  12224. return 0; //If we don't have sd by now, bail out
  12225. recovery_sub(sd, revive);
  12226. break;
  12227. case 1:
  12228. {
  12229. struct party_data* p;
  12230. //When no party given, we use invoker party
  12231. int p_id = 0, i;
  12232. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12233. map_idx = map_mapname2mapid(script_getstr(st,5));
  12234. if(map_idx < 1) { //But we'll check anyways
  12235. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12236. return 1;
  12237. }
  12238. }
  12239. if(script_hasdata(st,3))
  12240. p_id = script_getnum(st,3);
  12241. else if((sd = script_rid2sd(st)))
  12242. p_id = sd->status.party_id;
  12243. p = party_search(p_id);
  12244. if(p == NULL)
  12245. return 0;
  12246. for (i = 0; i < MAX_PARTY; i++) {
  12247. struct map_session_data* pl_sd;
  12248. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12249. || (map_idx && pl_sd->bl.m != map_idx))
  12250. continue;
  12251. recovery_sub(pl_sd, revive);
  12252. }
  12253. break;
  12254. }
  12255. case 2:
  12256. {
  12257. struct guild* g;
  12258. //When no guild given, we use invoker guild
  12259. int g_id = 0, i;
  12260. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12261. map_idx = map_mapname2mapid(script_getstr(st,5));
  12262. if(map_idx < 1) { //But we'll check anyways
  12263. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12264. return 1;
  12265. }
  12266. }
  12267. if(script_hasdata(st,3))
  12268. g_id = script_getnum(st,3);
  12269. else if((sd = script_rid2sd(st)))
  12270. g_id = sd->status.guild_id;
  12271. g = guild_search(g_id);
  12272. if(g == NULL)
  12273. return 0;
  12274. for (i = 0; i < MAX_GUILD; i++) {
  12275. struct map_session_data* pl_sd;
  12276. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12277. || (map_idx && pl_sd->bl.m != map_idx))
  12278. continue;
  12279. recovery_sub(pl_sd, revive);
  12280. }
  12281. break;
  12282. }
  12283. case 3:
  12284. if(script_hasdata(st,3))
  12285. map_idx = map_mapname2mapid(script_getstr(st,3));
  12286. else if((sd = script_rid2sd(st)))
  12287. map_idx = sd->bl.m;
  12288. if(map_idx < 1)
  12289. return 1; //No sd and no map given - return
  12290. case 4:
  12291. {
  12292. struct s_mapiterator *iter;
  12293. struct script_data *data = script_getdata(st, 3);
  12294. get_val(st, data); // Convert into value in case of a variable
  12295. if(script_hasdata(st,3) && !data_isstring(data))
  12296. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12297. iter = mapit_getallusers();
  12298. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12299. if(type == 3 && sd->bl.m != map_idx)
  12300. continue;
  12301. recovery_sub(sd, revive);
  12302. }
  12303. mapit_free(iter);
  12304. break;
  12305. }
  12306. default:
  12307. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12308. script_pushint(st,-1);
  12309. return 1;
  12310. }
  12311. script_pushint(st,1); //Successfully executed without errors
  12312. return SCRIPT_CMD_SUCCESS;
  12313. }
  12314. /*==========================================
  12315. * Get your pet info: getpetinfo(n)
  12316. * n -> 0:pet_id 1:pet_class 2:pet_name
  12317. * 3:friendly 4:hungry, 5: rename flag.6:level
  12318. *------------------------------------------*/
  12319. BUILDIN_FUNC(getpetinfo)
  12320. {
  12321. TBL_PC *sd=script_rid2sd(st);
  12322. TBL_PET *pd;
  12323. int type=script_getnum(st,2);
  12324. if(!sd || !sd->pd) {
  12325. if (type == 2)
  12326. script_pushconststr(st,"null");
  12327. else
  12328. script_pushint(st,0);
  12329. return 0;
  12330. }
  12331. pd = sd->pd;
  12332. switch(type){
  12333. case 0: script_pushint(st,pd->pet.pet_id); break;
  12334. case 1: script_pushint(st,pd->pet.class_); break;
  12335. case 2: script_pushstrcopy(st,pd->pet.name); break;
  12336. case 3: script_pushint(st,pd->pet.intimate); break;
  12337. case 4: script_pushint(st,pd->pet.hungry); break;
  12338. case 5: script_pushint(st,pd->pet.rename_flag); break;
  12339. case 6: script_pushint(st,(int)pd->pet.level); break;
  12340. default:
  12341. script_pushint(st,0);
  12342. break;
  12343. }
  12344. return SCRIPT_CMD_SUCCESS;
  12345. }
  12346. /*==========================================
  12347. * Get your homunculus info: gethominfo(n)
  12348. * n -> 0:hom_id 1:class 2:name
  12349. * 3:friendly 4:hungry, 5: rename flag.
  12350. * 6: level
  12351. *------------------------------------------*/
  12352. BUILDIN_FUNC(gethominfo)
  12353. {
  12354. TBL_PC *sd=script_rid2sd(st);
  12355. TBL_HOM *hd;
  12356. int type=script_getnum(st,2);
  12357. hd = sd?sd->hd:NULL;
  12358. if(!hd) {
  12359. if (type == 2)
  12360. script_pushconststr(st,"null");
  12361. else
  12362. script_pushint(st,0);
  12363. return 0;
  12364. }
  12365. switch(type){
  12366. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12367. case 1: script_pushint(st,hd->homunculus.class_); break;
  12368. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12369. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12370. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12371. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12372. case 6: script_pushint(st,hd->homunculus.level); break;
  12373. default:
  12374. script_pushint(st,0);
  12375. break;
  12376. }
  12377. return SCRIPT_CMD_SUCCESS;
  12378. }
  12379. /// Retrieves information about character's mercenary
  12380. /// getmercinfo <type>[,<char id>];
  12381. BUILDIN_FUNC(getmercinfo)
  12382. {
  12383. int type;
  12384. struct map_session_data* sd;
  12385. struct mercenary_data* md;
  12386. type = script_getnum(st,2);
  12387. if( script_hasdata(st,3) )
  12388. {
  12389. int char_id = script_getnum(st,3);
  12390. if( ( sd = map_charid2sd(char_id) ) == NULL )
  12391. {
  12392. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  12393. script_pushnil(st);
  12394. return SCRIPT_CMD_FAILURE;
  12395. }
  12396. }
  12397. else
  12398. {
  12399. if( ( sd = script_rid2sd(st) ) == NULL )
  12400. {
  12401. script_pushnil(st);
  12402. return SCRIPT_CMD_SUCCESS;
  12403. }
  12404. }
  12405. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12406. switch( type )
  12407. {
  12408. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12409. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12410. case 2:
  12411. if( md )
  12412. script_pushstrcopy(st,md->db->name);
  12413. else
  12414. script_pushconststr(st,"");
  12415. break;
  12416. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12417. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12418. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12419. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12420. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12421. default:
  12422. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12423. script_pushnil(st);
  12424. return SCRIPT_CMD_FAILURE;
  12425. }
  12426. return SCRIPT_CMD_SUCCESS;
  12427. }
  12428. /*==========================================
  12429. * Shows wether your inventory(and equips) contain
  12430. selected card or not.
  12431. checkequipedcard(4001);
  12432. *------------------------------------------*/
  12433. BUILDIN_FUNC(checkequipedcard)
  12434. {
  12435. TBL_PC *sd=script_rid2sd(st);
  12436. if(sd){
  12437. int n,i,c=0;
  12438. c=script_getnum(st,2);
  12439. for(i=0;i<MAX_INVENTORY;i++){
  12440. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  12441. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  12442. continue;
  12443. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12444. if(sd->status.inventory[i].card[n]==c){
  12445. script_pushint(st,1);
  12446. return 0;
  12447. }
  12448. }
  12449. }
  12450. }
  12451. }
  12452. script_pushint(st,0);
  12453. return SCRIPT_CMD_SUCCESS;
  12454. }
  12455. BUILDIN_FUNC(jump_zero)
  12456. {
  12457. int sel;
  12458. sel=script_getnum(st,2);
  12459. if(!sel) {
  12460. int pos;
  12461. if( !data_islabel(script_getdata(st,3)) ){
  12462. ShowError("script: jump_zero: not label !\n");
  12463. st->state=END;
  12464. return 1;
  12465. }
  12466. pos=script_getnum(st,3);
  12467. st->pos=pos;
  12468. st->state=GOTO;
  12469. }
  12470. return SCRIPT_CMD_SUCCESS;
  12471. }
  12472. /*==========================================
  12473. * movenpc [MouseJstr]
  12474. *------------------------------------------*/
  12475. BUILDIN_FUNC(movenpc)
  12476. {
  12477. TBL_NPC *nd = NULL;
  12478. const char *npc;
  12479. int x,y;
  12480. npc = script_getstr(st,2);
  12481. x = script_getnum(st,3);
  12482. y = script_getnum(st,4);
  12483. if ((nd = npc_name2id(npc)) == NULL){
  12484. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12485. return -1;
  12486. }
  12487. if (script_hasdata(st,5))
  12488. nd->ud.dir = script_getnum(st,5) % 8;
  12489. npc_movenpc(nd, x, y);
  12490. return SCRIPT_CMD_SUCCESS;
  12491. }
  12492. /*==========================================
  12493. * message [MouseJstr]
  12494. *------------------------------------------*/
  12495. BUILDIN_FUNC(message)
  12496. {
  12497. const char *msg,*player;
  12498. TBL_PC *pl_sd = NULL;
  12499. player = script_getstr(st,2);
  12500. msg = script_getstr(st,3);
  12501. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  12502. return 0;
  12503. clif_displaymessage(pl_sd->fd, msg);
  12504. return SCRIPT_CMD_SUCCESS;
  12505. }
  12506. /*==========================================
  12507. * npctalk (sends message to surrounding area)
  12508. *------------------------------------------*/
  12509. BUILDIN_FUNC(npctalk)
  12510. {
  12511. const char* str;
  12512. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  12513. str = script_getstr(st,2);
  12514. if(nd)
  12515. {
  12516. char name[NAME_LENGTH], message[256];
  12517. safestrncpy(name, nd->name, sizeof(name));
  12518. strtok(name, "#"); // discard extra name identifier if present
  12519. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  12520. clif_disp_overhead(&nd->bl, message);
  12521. }
  12522. return SCRIPT_CMD_SUCCESS;
  12523. }
  12524. // change npc walkspeed [Valaris]
  12525. BUILDIN_FUNC(npcspeed)
  12526. {
  12527. struct npc_data* nd;
  12528. int speed;
  12529. speed = script_getnum(st,2);
  12530. nd =(struct npc_data *)map_id2bl(st->oid);
  12531. if( nd ) {
  12532. nd->speed = speed;
  12533. nd->ud.state.speed_changed = 1;
  12534. }
  12535. return SCRIPT_CMD_SUCCESS;
  12536. }
  12537. // make an npc walk to a position [Valaris]
  12538. BUILDIN_FUNC(npcwalkto)
  12539. {
  12540. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12541. int x=0,y=0;
  12542. x=script_getnum(st,2);
  12543. y=script_getnum(st,3);
  12544. if(nd) {
  12545. if (!nd->status.hp)
  12546. status_calc_npc(nd, SCO_FIRST);
  12547. else
  12548. status_calc_npc(nd, SCO_NONE);
  12549. unit_walktoxy(&nd->bl,x,y,0);
  12550. }
  12551. return SCRIPT_CMD_SUCCESS;
  12552. }
  12553. // stop an npc's movement [Valaris]
  12554. BUILDIN_FUNC(npcstop)
  12555. {
  12556. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12557. if(nd) {
  12558. unit_stop_walking(&nd->bl,1|4);
  12559. }
  12560. return SCRIPT_CMD_SUCCESS;
  12561. }
  12562. /**
  12563. * getlook(<type>{,<char_id>})
  12564. **/
  12565. BUILDIN_FUNC(getlook)
  12566. {
  12567. int type,val;
  12568. TBL_PC *sd;
  12569. if (!script_charid2sd(3,sd)) {
  12570. script_pushint(st,-1);
  12571. return SCRIPT_CMD_FAILURE;
  12572. }
  12573. type=script_getnum(st,2);
  12574. val=-1;
  12575. switch(type) {
  12576. case LOOK_HAIR: val=sd->status.hair; break; //1
  12577. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12578. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12579. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12580. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12581. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12582. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12583. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12584. case LOOK_SHOES: break; //9
  12585. case LOOK_ROBE: val=sd->status.robe; break; //12
  12586. }
  12587. script_pushint(st,val);
  12588. return SCRIPT_CMD_SUCCESS;
  12589. }
  12590. /**
  12591. * getsavepoint(<information type>{,<char_id>})
  12592. * @param type 0- map name, 1- x, 2- y
  12593. **/
  12594. BUILDIN_FUNC(getsavepoint)
  12595. {
  12596. TBL_PC* sd;
  12597. int type;
  12598. if (!script_charid2sd(3,sd)) {
  12599. script_pushint(st,0);
  12600. return 0;
  12601. }
  12602. type = script_getnum(st,2);
  12603. switch(type) {
  12604. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12605. case 1: script_pushint(st,sd->status.save_point.x); break;
  12606. case 2: script_pushint(st,sd->status.save_point.y); break;
  12607. default:
  12608. script_pushint(st,0);
  12609. break;
  12610. }
  12611. return SCRIPT_CMD_SUCCESS;
  12612. }
  12613. /*==========================================
  12614. * Get position for char/NPC/pet/hom/merc/elem objects. Added by Lorky
  12615. *
  12616. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  12617. * where type:
  12618. * MapName$ - String variable for output map name
  12619. * MapX - Integer variable for output coord X
  12620. * MapY - Integer variable for output coord Y
  12621. * type - type of object
  12622. * 0 - Character coord
  12623. * 1 - NPC coord
  12624. * 2 - Pet coord
  12625. * 3 - Mob coord (see 'getunitdata')
  12626. * 4 - Homun coord
  12627. * 5 - Mercenary coord
  12628. * 6 - Elemental coord
  12629. * CharName$ - Name object. If miss or "this" the current object
  12630. *
  12631. * Return:
  12632. * 0 - success
  12633. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  12634. *------------------------------------------*/
  12635. BUILDIN_FUNC(getmapxy)
  12636. {
  12637. struct block_list *bl = NULL;
  12638. TBL_PC *sd=NULL;
  12639. int64 num;
  12640. const char *name;
  12641. char prefix;
  12642. int x,y,type;
  12643. char mapname[MAP_NAME_LENGTH];
  12644. if( !data_isreference(script_getdata(st,2)) ) {
  12645. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  12646. script_pushint(st,-1);
  12647. return 1;
  12648. }
  12649. if( !data_isreference(script_getdata(st,3)) ) {
  12650. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  12651. script_pushint(st,-1);
  12652. return 1;
  12653. }
  12654. if( !data_isreference(script_getdata(st,4)) ) {
  12655. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  12656. script_pushint(st,-1);
  12657. return 1;
  12658. }
  12659. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  12660. ShowWarning("script: buildin_getmapxy: %s is not a string variable\n",reference_getname(script_getdata(st, 2)));
  12661. script_pushint(st,-1);
  12662. return 1;
  12663. }
  12664. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  12665. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be int\n",reference_getname(script_getdata(st, 3)));
  12666. script_pushint(st,-1);
  12667. return 1;
  12668. }
  12669. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  12670. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be int\n",reference_getname(script_getdata(st, 4)));
  12671. script_pushint(st,-1);
  12672. return 1;
  12673. }
  12674. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12675. type=script_getnum(st,5);
  12676. switch (type) {
  12677. case 0: //Get Character Position
  12678. if( script_hasdata(st,6) )
  12679. sd=map_nick2sd(script_getstr(st,6));
  12680. else
  12681. sd=script_rid2sd(st);
  12682. if (sd)
  12683. bl = &sd->bl;
  12684. break;
  12685. case 1: //Get NPC Position
  12686. if( script_hasdata(st,6) )
  12687. {
  12688. struct npc_data *nd;
  12689. nd=npc_name2id(script_getstr(st,6));
  12690. if (nd)
  12691. bl = &nd->bl;
  12692. } else //In case the origin is not an npc?
  12693. bl=map_id2bl(st->oid);
  12694. break;
  12695. case 2: //Get Pet Position
  12696. if(script_hasdata(st,6))
  12697. sd=map_nick2sd(script_getstr(st,6));
  12698. else
  12699. sd=script_rid2sd(st);
  12700. if (sd && sd->pd)
  12701. bl = &sd->pd->bl;
  12702. break;
  12703. case 3: //Get Mob Position
  12704. break; //see 'getunitdata'
  12705. case 4: //Get Homun Position
  12706. if(script_hasdata(st,6))
  12707. sd=map_nick2sd(script_getstr(st,6));
  12708. else
  12709. sd=script_rid2sd(st);
  12710. if (sd && sd->hd)
  12711. bl = &sd->hd->bl;
  12712. break;
  12713. case 5: //Get Mercenary Position
  12714. if(script_hasdata(st,6))
  12715. sd=map_nick2sd(script_getstr(st,6));
  12716. else
  12717. sd=script_rid2sd(st);
  12718. if (sd && sd->md)
  12719. bl = &sd->md->bl;
  12720. break;
  12721. case 6: //Get Elemental Position
  12722. if(script_hasdata(st,6))
  12723. sd=map_nick2sd(script_getstr(st,6));
  12724. else
  12725. sd=script_rid2sd(st);
  12726. if (sd && sd->ed)
  12727. bl = &sd->ed->bl;
  12728. break;
  12729. default:
  12730. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  12731. script_pushint(st,-1);
  12732. return 1;
  12733. }
  12734. if (!bl) { //No object found.
  12735. script_pushint(st,-1);
  12736. return 0;
  12737. }
  12738. x= bl->x;
  12739. y= bl->y;
  12740. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12741. //Set MapName$
  12742. num=st->stack->stack_data[st->start+2].u.num;
  12743. name=get_str(script_getvarid(num));
  12744. prefix=*name;
  12745. if(not_server_variable(prefix))
  12746. sd=script_rid2sd(st);
  12747. else
  12748. sd=NULL;
  12749. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12750. //Set MapX
  12751. num=st->stack->stack_data[st->start+3].u.num;
  12752. name=get_str(script_getvarid(num));
  12753. prefix=*name;
  12754. if(not_server_variable(prefix))
  12755. sd=script_rid2sd(st);
  12756. else
  12757. sd=NULL;
  12758. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12759. //Set MapY
  12760. num=st->stack->stack_data[st->start+4].u.num;
  12761. name=get_str(script_getvarid(num));
  12762. prefix=*name;
  12763. if(not_server_variable(prefix))
  12764. sd=script_rid2sd(st);
  12765. else
  12766. sd=NULL;
  12767. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12768. //Return Success value
  12769. script_pushint(st,0);
  12770. return SCRIPT_CMD_SUCCESS;
  12771. }
  12772. /// Returns the map name of given map ID.
  12773. ///
  12774. /// mapid2name <map ID>;
  12775. BUILDIN_FUNC(mapid2name)
  12776. {
  12777. uint16 m = script_getnum(st, 2);
  12778. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  12779. script_pushstr(st, "");
  12780. return SCRIPT_CMD_FAILURE;
  12781. }
  12782. script_pushstrcopy(st, map_mapid2mapname(m));
  12783. return SCRIPT_CMD_SUCCESS;
  12784. }
  12785. /*==========================================
  12786. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12787. *------------------------------------------*/
  12788. BUILDIN_FUNC(logmes)
  12789. {
  12790. const char *str;
  12791. TBL_PC* sd;
  12792. sd = script_rid2sd(st);
  12793. if( sd == NULL )
  12794. return 1;
  12795. str = script_getstr(st,2);
  12796. log_npc(sd,str);
  12797. return SCRIPT_CMD_SUCCESS;
  12798. }
  12799. BUILDIN_FUNC(summon)
  12800. {
  12801. int _class, timeout=0;
  12802. const char *str,*event="";
  12803. TBL_PC *sd;
  12804. struct mob_data *md;
  12805. int tick = gettick();
  12806. sd=script_rid2sd(st);
  12807. if (!sd) return 0;
  12808. str =script_getstr(st,2);
  12809. _class=script_getnum(st,3);
  12810. if( script_hasdata(st,4) )
  12811. timeout=script_getnum(st,4);
  12812. if( script_hasdata(st,5) ){
  12813. event=script_getstr(st,5);
  12814. check_event(st, event);
  12815. }
  12816. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12817. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12818. if (md) {
  12819. md->master_id=sd->bl.id;
  12820. md->special_state.ai = AI_ATTACK;
  12821. if( md->deletetimer != INVALID_TIMER )
  12822. delete_timer(md->deletetimer, mob_timer_delete);
  12823. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12824. mob_spawn (md); //Now it is ready for spawning.
  12825. clif_specialeffect(&md->bl,344,AREA);
  12826. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12827. }
  12828. script_pushint(st, md->bl.id);
  12829. return SCRIPT_CMD_SUCCESS;
  12830. }
  12831. /*==========================================
  12832. * Checks whether it is daytime/nighttime
  12833. *------------------------------------------*/
  12834. BUILDIN_FUNC(isnight)
  12835. {
  12836. script_pushint(st,(night_flag == 1));
  12837. return SCRIPT_CMD_SUCCESS;
  12838. }
  12839. BUILDIN_FUNC(isday)
  12840. {
  12841. script_pushint(st,(night_flag == 0));
  12842. return SCRIPT_CMD_SUCCESS;
  12843. }
  12844. /*================================================
  12845. * Check how many items/cards in the list are
  12846. * equipped - used for 2/15's cards patch [celest]
  12847. *------------------------------------------------*/
  12848. BUILDIN_FUNC(isequippedcnt)
  12849. {
  12850. TBL_PC *sd;
  12851. int i, id = 1;
  12852. int ret = 0;
  12853. sd = script_rid2sd(st);
  12854. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12855. script_pushint(st,0);
  12856. return 0;
  12857. }
  12858. for (i=0; id!=0; i++) {
  12859. short j;
  12860. FETCH (i+2, id) else id = 0;
  12861. if (id <= 0)
  12862. continue;
  12863. for (j=0; j<EQI_MAX; j++) {
  12864. short index = sd->equip_index[j];
  12865. if(index < 0)
  12866. continue;
  12867. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  12868. continue;
  12869. if(!sd->inventory_data[index])
  12870. continue;
  12871. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  12872. if (sd->inventory_data[index]->nameid == id)
  12873. ret+= sd->status.inventory[index].amount;
  12874. } else { //Count cards.
  12875. short k;
  12876. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12877. continue; //No cards
  12878. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12879. if (sd->status.inventory[index].card[k] == id)
  12880. ret++; //[Lupus]
  12881. }
  12882. }
  12883. }
  12884. }
  12885. script_pushint(st,ret);
  12886. return SCRIPT_CMD_SUCCESS;
  12887. }
  12888. /*================================================
  12889. * Check whether another card has been
  12890. * equipped - used for 2/15's cards patch [celest]
  12891. * -- Items checked cannot be reused in another
  12892. * card set to prevent exploits
  12893. *------------------------------------------------*/
  12894. BUILDIN_FUNC(isequipped)
  12895. {
  12896. TBL_PC *sd;
  12897. int i, id = 1;
  12898. int ret = -1;
  12899. //Original hash to reverse it when full check fails.
  12900. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  12901. sd = script_rid2sd(st);
  12902. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12903. script_pushint(st,0);
  12904. return SCRIPT_CMD_SUCCESS;
  12905. }
  12906. setitem_hash = sd->bonus.setitem_hash;
  12907. setitem_hash2 = sd->bonus.setitem_hash2;
  12908. for (i=0; id!=0; i++) {
  12909. int flag = 0;
  12910. short j;
  12911. FETCH (i+2, id) else id = 0;
  12912. if (id <= 0)
  12913. continue;
  12914. for (j=0; j<EQI_MAX; j++) {
  12915. short index = sd->equip_index[j];
  12916. if(index < 0)
  12917. continue;
  12918. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  12919. continue;
  12920. if(!sd->inventory_data[index])
  12921. continue;
  12922. if (itemdb_type(id) != IT_CARD) {
  12923. if (sd->inventory_data[index]->nameid != id)
  12924. continue;
  12925. flag = 1;
  12926. break;
  12927. } else { //Cards
  12928. short k;
  12929. if (sd->inventory_data[index]->slot == 0 ||
  12930. itemdb_isspecial(sd->status.inventory[index].card[0]))
  12931. continue;
  12932. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  12933. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  12934. unsigned int hash = 0;
  12935. if (sd->status.inventory[index].card[k] != id)
  12936. continue;
  12937. hash = 1<<((j<5?j:j-5)*4 + k);
  12938. // check if card is already used by another set
  12939. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  12940. continue;
  12941. // We have found a match
  12942. flag = 1;
  12943. // Set hash so this card cannot be used by another
  12944. if (j<5)
  12945. sd->bonus.setitem_hash |= hash;
  12946. else
  12947. sd->bonus.setitem_hash2 |= hash;
  12948. break;
  12949. }
  12950. }
  12951. if (flag) break; //Card found
  12952. }
  12953. if (ret == -1)
  12954. ret = flag;
  12955. else
  12956. ret &= flag;
  12957. if (!ret) break;
  12958. }
  12959. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  12960. sd->bonus.setitem_hash = setitem_hash;
  12961. sd->bonus.setitem_hash2 = setitem_hash2;
  12962. }
  12963. script_pushint(st,ret);
  12964. return SCRIPT_CMD_SUCCESS;
  12965. }
  12966. /*================================================
  12967. * Check how many given inserted cards in the CURRENT
  12968. * weapon - used for 2/15's cards patch [Lupus]
  12969. *------------------------------------------------*/
  12970. BUILDIN_FUNC(cardscnt)
  12971. {
  12972. TBL_PC *sd;
  12973. int i, k, id = 1;
  12974. int ret = 0;
  12975. int index;
  12976. sd = script_rid2sd(st);
  12977. for (i=0; id!=0; i++) {
  12978. FETCH (i+2, id) else id = 0;
  12979. if (id <= 0)
  12980. continue;
  12981. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  12982. if(index < 0) continue;
  12983. if(!sd->inventory_data[index])
  12984. continue;
  12985. if(itemdb_type(id) != IT_CARD) {
  12986. if (sd->inventory_data[index]->nameid == id)
  12987. ret+= sd->status.inventory[index].amount;
  12988. } else {
  12989. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12990. continue;
  12991. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12992. if (sd->status.inventory[index].card[k] == id)
  12993. ret++;
  12994. }
  12995. }
  12996. }
  12997. script_pushint(st,ret);
  12998. // script_pushint(st,current_equip_item_index);
  12999. return SCRIPT_CMD_SUCCESS;
  13000. }
  13001. /*=======================================================
  13002. * Returns the refined number of the current item, or an
  13003. * item with inventory index specified
  13004. *-------------------------------------------------------*/
  13005. BUILDIN_FUNC(getrefine)
  13006. {
  13007. TBL_PC *sd;
  13008. if ((sd = script_rid2sd(st))!= NULL)
  13009. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  13010. else
  13011. script_pushint(st,0);
  13012. return SCRIPT_CMD_SUCCESS;
  13013. }
  13014. /*=======================================================
  13015. * Day/Night controls
  13016. *-------------------------------------------------------*/
  13017. BUILDIN_FUNC(night)
  13018. {
  13019. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13020. return SCRIPT_CMD_SUCCESS;
  13021. }
  13022. BUILDIN_FUNC(day)
  13023. {
  13024. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13025. return SCRIPT_CMD_SUCCESS;
  13026. }
  13027. /**
  13028. * unequip <equipment slot>{,<char_id>};
  13029. * @author [Spectre]
  13030. **/
  13031. BUILDIN_FUNC(unequip) {
  13032. int pos;
  13033. TBL_PC *sd;
  13034. if (!script_charid2sd(3,sd))
  13035. return SCRIPT_CMD_FAILURE;
  13036. pos = script_getnum(st,2);
  13037. if (pos >= 1 && pos <= ARRAYLENGTH(equip)) {
  13038. short i = pc_checkequip(sd,equip[pos-1]);
  13039. if (i >= 0) {
  13040. pc_unequipitem(sd,i,1|2);
  13041. script_pushint(st, 1);
  13042. return SCRIPT_CMD_SUCCESS;
  13043. }
  13044. }
  13045. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13046. script_pushint(st, 0);
  13047. return SCRIPT_CMD_FAILURE;
  13048. }
  13049. /**
  13050. * equip <item id>{,<char_id>};
  13051. **/
  13052. BUILDIN_FUNC(equip) {
  13053. unsigned short nameid = 0;
  13054. TBL_PC *sd;
  13055. struct item_data *item_data;
  13056. if (!script_charid2sd(3,sd))
  13057. return SCRIPT_CMD_FAILURE;
  13058. nameid = script_getnum(st,2);
  13059. if ((item_data = itemdb_exists(nameid))) {
  13060. int i;
  13061. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  13062. if (i < MAX_INVENTORY) {
  13063. pc_equipitem(sd,i,item_data->equip);
  13064. script_pushint(st,1);
  13065. return SCRIPT_CMD_SUCCESS;
  13066. }
  13067. }
  13068. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13069. script_pushint(st,0);
  13070. return SCRIPT_CMD_FAILURE;
  13071. }
  13072. BUILDIN_FUNC(autoequip)
  13073. {
  13074. unsigned short nameid;
  13075. int flag;
  13076. struct item_data *item_data;
  13077. nameid=script_getnum(st,2);
  13078. flag=script_getnum(st,3);
  13079. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13080. {
  13081. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13082. return SCRIPT_CMD_FAILURE;
  13083. }
  13084. if( !itemdb_isequip2(item_data) )
  13085. {
  13086. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13087. return SCRIPT_CMD_FAILURE;
  13088. }
  13089. item_data->flag.autoequip = flag>0?1:0;
  13090. return SCRIPT_CMD_SUCCESS;
  13091. }
  13092. BUILDIN_FUNC(setbattleflag)
  13093. {
  13094. const char *flag, *value;
  13095. flag = script_getstr(st,2);
  13096. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13097. if (battle_set_value(flag, value) == 0)
  13098. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13099. else
  13100. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13101. return SCRIPT_CMD_SUCCESS;
  13102. }
  13103. BUILDIN_FUNC(getbattleflag)
  13104. {
  13105. const char *flag;
  13106. flag = script_getstr(st,2);
  13107. script_pushint(st,battle_get_value(flag));
  13108. return SCRIPT_CMD_SUCCESS;
  13109. }
  13110. //=======================================================
  13111. // strlen [Valaris]
  13112. //-------------------------------------------------------
  13113. BUILDIN_FUNC(getstrlen)
  13114. {
  13115. const char *str = script_getstr(st,2);
  13116. int len = (str) ? (int)strlen(str) : 0;
  13117. script_pushint(st,len);
  13118. return SCRIPT_CMD_SUCCESS;
  13119. }
  13120. //=======================================================
  13121. // isalpha [Valaris]
  13122. //-------------------------------------------------------
  13123. BUILDIN_FUNC(charisalpha)
  13124. {
  13125. const char *str=script_getstr(st,2);
  13126. int pos=script_getnum(st,3);
  13127. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13128. script_pushint(st,val);
  13129. return SCRIPT_CMD_SUCCESS;
  13130. }
  13131. //=======================================================
  13132. // charisupper <str>, <index>
  13133. //-------------------------------------------------------
  13134. BUILDIN_FUNC(charisupper)
  13135. {
  13136. const char *str = script_getstr(st,2);
  13137. int pos = script_getnum(st,3);
  13138. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13139. script_pushint(st,val);
  13140. return SCRIPT_CMD_SUCCESS;
  13141. }
  13142. //=======================================================
  13143. // charislower <str>, <index>
  13144. //-------------------------------------------------------
  13145. BUILDIN_FUNC(charislower)
  13146. {
  13147. const char *str = script_getstr(st,2);
  13148. int pos = script_getnum(st,3);
  13149. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13150. script_pushint(st,val);
  13151. return SCRIPT_CMD_SUCCESS;
  13152. }
  13153. //=======================================================
  13154. // charat <str>, <index>
  13155. //-------------------------------------------------------
  13156. BUILDIN_FUNC(charat) {
  13157. const char *str = script_getstr(st,2);
  13158. int pos = script_getnum(st,3);
  13159. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13160. char output[2];
  13161. output[0] = str[pos];
  13162. output[1] = '\0';
  13163. script_pushstrcopy(st, output);
  13164. } else
  13165. script_pushconststr(st, "");
  13166. return SCRIPT_CMD_SUCCESS;
  13167. }
  13168. //=======================================================
  13169. // setchar <string>, <char>, <index>
  13170. //-------------------------------------------------------
  13171. BUILDIN_FUNC(setchar)
  13172. {
  13173. const char *str = script_getstr(st,2);
  13174. const char *c = script_getstr(st,3);
  13175. int index = script_getnum(st,4);
  13176. char *output = aStrdup(str);
  13177. if(index >= 0 && index < strlen(output))
  13178. output[index] = *c;
  13179. script_pushstr(st, output);
  13180. return SCRIPT_CMD_SUCCESS;
  13181. }
  13182. //=======================================================
  13183. // insertchar <string>, <char>, <index>
  13184. //-------------------------------------------------------
  13185. BUILDIN_FUNC(insertchar)
  13186. {
  13187. const char *str = script_getstr(st,2);
  13188. const char *c = script_getstr(st,3);
  13189. int index = script_getnum(st,4);
  13190. char *output;
  13191. size_t len = strlen(str);
  13192. if(index < 0)
  13193. index = 0;
  13194. else if(index > len)
  13195. index = len;
  13196. output = (char*)aMalloc(len + 2);
  13197. memcpy(output, str, index);
  13198. output[index] = c[0];
  13199. memcpy(&output[index+1], &str[index], len - index);
  13200. output[len+1] = '\0';
  13201. script_pushstr(st, output);
  13202. return SCRIPT_CMD_SUCCESS;
  13203. }
  13204. //=======================================================
  13205. // delchar <string>, <index>
  13206. //-------------------------------------------------------
  13207. BUILDIN_FUNC(delchar)
  13208. {
  13209. const char *str = script_getstr(st,2);
  13210. int index = script_getnum(st,3);
  13211. char *output;
  13212. size_t len = strlen(str);
  13213. if(index < 0 || index > len) {
  13214. //return original
  13215. output = aStrdup(str);
  13216. script_pushstr(st, output);
  13217. return 0;
  13218. }
  13219. output = (char*)aMalloc(len);
  13220. memcpy(output, str, index);
  13221. memcpy(&output[index], &str[index+1], len - index);
  13222. script_pushstr(st, output);
  13223. return SCRIPT_CMD_SUCCESS;
  13224. }
  13225. //=======================================================
  13226. // strtoupper <str>
  13227. //-------------------------------------------------------
  13228. BUILDIN_FUNC(strtoupper)
  13229. {
  13230. const char *str = script_getstr(st,2);
  13231. char *output = aStrdup(str);
  13232. char *cursor = output;
  13233. while (*cursor != '\0') {
  13234. *cursor = TOUPPER(*cursor);
  13235. cursor++;
  13236. }
  13237. script_pushstr(st, output);
  13238. return SCRIPT_CMD_SUCCESS;
  13239. }
  13240. //=======================================================
  13241. // strtolower <str>
  13242. //-------------------------------------------------------
  13243. BUILDIN_FUNC(strtolower)
  13244. {
  13245. const char *str = script_getstr(st,2);
  13246. char *output = aStrdup(str);
  13247. char *cursor = output;
  13248. while (*cursor != '\0') {
  13249. *cursor = TOLOWER(*cursor);
  13250. cursor++;
  13251. }
  13252. script_pushstr(st, output);
  13253. return SCRIPT_CMD_SUCCESS;
  13254. }
  13255. //=======================================================
  13256. // substr <str>, <start>, <end>
  13257. //-------------------------------------------------------
  13258. BUILDIN_FUNC(substr)
  13259. {
  13260. const char *str = script_getstr(st,2);
  13261. char *output;
  13262. int start = script_getnum(st,3);
  13263. int end = script_getnum(st,4);
  13264. int len = 0;
  13265. if(start >= 0 && end < strlen(str) && start <= end) {
  13266. len = end - start + 1;
  13267. output = (char*)aMalloc(len + 1);
  13268. memcpy(output, &str[start], len);
  13269. } else
  13270. output = (char*)aMalloc(1);
  13271. output[len] = '\0';
  13272. script_pushstr(st, output);
  13273. return SCRIPT_CMD_SUCCESS;
  13274. }
  13275. //=======================================================
  13276. // explode <dest_string_array>, <str>, <delimiter>
  13277. // Note: delimiter is limited to 1 char
  13278. //-------------------------------------------------------
  13279. BUILDIN_FUNC(explode)
  13280. {
  13281. struct script_data* data = script_getdata(st, 2);
  13282. const char *str = script_getstr(st,3);
  13283. const char delimiter = script_getstr(st, 4)[0];
  13284. int32 id;
  13285. size_t len = strlen(str);
  13286. int i = 0, j = 0;
  13287. int start;
  13288. char *temp;
  13289. const char* name;
  13290. TBL_PC* sd = NULL;
  13291. temp = (char*)aMalloc(len + 1);
  13292. if( !data_isreference(data) ) {
  13293. ShowError("script:explode: not a variable\n");
  13294. script_reportdata(data);
  13295. st->state = END;
  13296. return 1;// not a variable
  13297. }
  13298. id = reference_getid(data);
  13299. start = reference_getindex(data);
  13300. name = reference_getname(data);
  13301. if( !is_string_variable(name) ) {
  13302. ShowError("script:explode: not string array\n");
  13303. script_reportdata(data);
  13304. st->state = END;
  13305. return 1;// data type mismatch
  13306. }
  13307. if( not_server_variable(*name) ) {
  13308. sd = script_rid2sd(st);
  13309. if( sd == NULL )
  13310. return 0;// no player attached
  13311. }
  13312. while(str[i] != '\0') {
  13313. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13314. temp[j] = '\0';
  13315. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13316. j = 0;
  13317. ++i;
  13318. } else {
  13319. temp[j++] = str[i++];
  13320. }
  13321. }
  13322. //set last string
  13323. temp[j] = '\0';
  13324. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13325. aFree(temp);
  13326. return SCRIPT_CMD_SUCCESS;
  13327. }
  13328. //=======================================================
  13329. // implode <string_array>
  13330. // implode <string_array>, <glue>
  13331. //-------------------------------------------------------
  13332. BUILDIN_FUNC(implode)
  13333. {
  13334. struct script_data* data = script_getdata(st, 2);
  13335. const char *name;
  13336. uint32 glue_len = 0, array_size, id;
  13337. char *output;
  13338. TBL_PC* sd = NULL;
  13339. if( !data_isreference(data) ) {
  13340. ShowError("script:implode: not a variable\n");
  13341. script_reportdata(data);
  13342. st->state = END;
  13343. return 1;// not a variable
  13344. }
  13345. id = reference_getid(data);
  13346. name = reference_getname(data);
  13347. if( !is_string_variable(name) ) {
  13348. ShowError("script:implode: not string array\n");
  13349. script_reportdata(data);
  13350. st->state = END;
  13351. return 1;// data type mismatch
  13352. }
  13353. if( not_server_variable(*name) ) {
  13354. sd = script_rid2sd(st);
  13355. if( sd == NULL )
  13356. return 0;// no player attached
  13357. }
  13358. //count chars
  13359. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  13360. if(array_size == -1) { //empty array check (AmsTaff)
  13361. ShowWarning("script:implode: array length = 0\n");
  13362. output = (char*)aMalloc(sizeof(char)*5);
  13363. sprintf(output,"%s","NULL");
  13364. } else {
  13365. const char *glue = NULL, *temp;
  13366. size_t len = 0;
  13367. int i, k = 0;
  13368. for(i = 0; i <= array_size; ++i) {
  13369. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13370. len += strlen(temp);
  13371. script_removetop(st, -1, 0);
  13372. }
  13373. //allocate mem
  13374. if( script_hasdata(st,3) ) {
  13375. glue = script_getstr(st,3);
  13376. glue_len = strlen(glue);
  13377. len += glue_len * (array_size);
  13378. }
  13379. output = (char*)aMalloc(len + 1);
  13380. //build output
  13381. for(i = 0; i < array_size; ++i) {
  13382. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13383. len = strlen(temp);
  13384. memcpy(&output[k], temp, len);
  13385. k += len;
  13386. if(glue_len != 0) {
  13387. memcpy(&output[k], glue, glue_len);
  13388. k += glue_len;
  13389. }
  13390. script_removetop(st, -1, 0);
  13391. }
  13392. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13393. len = strlen(temp);
  13394. memcpy(&output[k], temp, len);
  13395. k += len;
  13396. script_removetop(st, -1, 0);
  13397. output[k] = '\0';
  13398. }
  13399. script_pushstr(st, output);
  13400. return SCRIPT_CMD_SUCCESS;
  13401. }
  13402. //=======================================================
  13403. // sprintf(<format>, ...);
  13404. // Implements C sprintf, except format %n. The resulting string is
  13405. // returned, instead of being saved in variable by reference.
  13406. //-------------------------------------------------------
  13407. BUILDIN_FUNC(sprintf)
  13408. {
  13409. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13410. const char* format;
  13411. char* p;
  13412. char* q;
  13413. char* buf = NULL;
  13414. char* buf2 = NULL;
  13415. struct script_data* data;
  13416. StringBuf final_buf;
  13417. // Fetch init data
  13418. format = script_getstr(st, 2);
  13419. argc = script_lastdata(st)-2;
  13420. len = strlen(format);
  13421. // Skip parsing, where no parsing is required.
  13422. if(len == 0) {
  13423. script_pushconststr(st,"");
  13424. return 0;
  13425. }
  13426. // Pessimistic alloc
  13427. CREATE(buf, char, len+1);
  13428. // Need not be parsed, just solve stuff like %%.
  13429. if(argc == 0) {
  13430. memcpy(buf,format,len+1);
  13431. script_pushstrcopy(st, buf);
  13432. aFree(buf);
  13433. return 0;
  13434. }
  13435. safestrncpy(buf, format, len+1);
  13436. // Issue sprintf for each parameter
  13437. StringBuf_Init(&final_buf);
  13438. q = buf;
  13439. while((p = strchr(q, '%')) != NULL) {
  13440. if(p != q) {
  13441. len = p - q + 1;
  13442. if(buf2_len < len) {
  13443. RECREATE(buf2, char, len);
  13444. buf2_len = len;
  13445. }
  13446. safestrncpy(buf2, q, len);
  13447. StringBuf_AppendStr(&final_buf, buf2);
  13448. q = p;
  13449. }
  13450. p = q + 1;
  13451. if(*p == '%') { // %%
  13452. StringBuf_AppendStr(&final_buf, "%");
  13453. q+=2;
  13454. continue;
  13455. }
  13456. if(*p == 'n') { // %n
  13457. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13458. script_reportsrc(st);
  13459. q+=2;
  13460. continue;
  13461. }
  13462. if(arg >= argc) {
  13463. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13464. if(buf) aFree(buf);
  13465. if(buf2) aFree(buf2);
  13466. StringBuf_Destroy(&final_buf);
  13467. script_pushconststr(st,"");
  13468. return 1;
  13469. }
  13470. if((p = strchr(q+1, '%')) == NULL)
  13471. p = strchr(q, 0); // EOS
  13472. len = p - q + 1;
  13473. if(buf2_len < len) {
  13474. RECREATE(buf2, char, len);
  13475. buf2_len = len;
  13476. }
  13477. safestrncpy(buf2, q, len);
  13478. q = p;
  13479. // Note: This assumes the passed value being the correct
  13480. // type to the current format specifier. If not, the server
  13481. // probably crashes or returns anything else, than expected,
  13482. // but it would behave in normal code the same way so it's
  13483. // the scripter's responsibility.
  13484. data = script_getdata(st, arg+3);
  13485. if(data_isstring(data)) // String
  13486. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13487. else if(data_isint(data)) // Number
  13488. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13489. else if(data_isreference(data)) { // Variable
  13490. char* name = reference_getname(data);
  13491. if(name[strlen(name)-1]=='$') // var Str
  13492. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13493. else // var Int
  13494. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13495. } else { // Unsupported type
  13496. ShowError("buildin_sprintf: Unknown argument type!\n");
  13497. if(buf) aFree(buf);
  13498. if(buf2) aFree(buf2);
  13499. StringBuf_Destroy(&final_buf);
  13500. script_pushconststr(st,"");
  13501. return 1;
  13502. }
  13503. arg++;
  13504. }
  13505. // Append anything left
  13506. if(*q)
  13507. StringBuf_AppendStr(&final_buf, q);
  13508. // Passed more, than needed
  13509. if(arg < argc) {
  13510. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13511. script_reportsrc(st);
  13512. }
  13513. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13514. if(buf) aFree(buf);
  13515. if(buf2) aFree(buf2);
  13516. StringBuf_Destroy(&final_buf);
  13517. return SCRIPT_CMD_SUCCESS;
  13518. }
  13519. //=======================================================
  13520. // sscanf(<str>, <format>, ...);
  13521. // Implements C sscanf.
  13522. //-------------------------------------------------------
  13523. BUILDIN_FUNC(sscanf){
  13524. unsigned int argc, arg = 0, len;
  13525. struct script_data* data;
  13526. struct map_session_data* sd = NULL;
  13527. const char* str;
  13528. const char* format;
  13529. const char* p;
  13530. const char* q;
  13531. char* buf = NULL;
  13532. char* buf_p;
  13533. char* ref_str = NULL;
  13534. int ref_int;
  13535. // Get data
  13536. str = script_getstr(st, 2);
  13537. format = script_getstr(st, 3);
  13538. argc = script_lastdata(st)-3;
  13539. len = strlen(format);
  13540. if (len != 0 && strlen(str) == 0) {
  13541. // If the source string is empty but the format string is not, we return -1
  13542. // according to the C specs. (if the format string is also empty, we shall
  13543. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13544. script_pushint(st, -1);
  13545. return SCRIPT_CMD_SUCCESS;
  13546. }
  13547. CREATE(buf, char, len*2+1);
  13548. // Issue sscanf for each parameter
  13549. *buf = 0;
  13550. q = format;
  13551. while((p = strchr(q, '%'))){
  13552. if(p!=q){
  13553. strncat(buf, q, (size_t)(p-q));
  13554. q = p;
  13555. }
  13556. p = q+1;
  13557. if(*p=='*' || *p=='%'){ // Skip
  13558. strncat(buf, q, 2);
  13559. q+=2;
  13560. continue;
  13561. }
  13562. if(arg>=argc){
  13563. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13564. script_pushint(st, -1);
  13565. if(buf) aFree(buf);
  13566. if(ref_str) aFree(ref_str);
  13567. return 1;
  13568. }
  13569. if((p = strchr(q+1, '%'))==NULL){
  13570. p = strchr(q, 0); // EOS
  13571. }
  13572. len = p-q;
  13573. strncat(buf, q, len);
  13574. q = p;
  13575. // Validate output
  13576. data = script_getdata(st, arg+4);
  13577. if(!data_isreference(data) || !reference_tovariable(data)){
  13578. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13579. script_pushint(st, -1);
  13580. if(buf) aFree(buf);
  13581. if(ref_str) aFree(ref_str);
  13582. return 1;
  13583. }
  13584. buf_p = reference_getname(data);
  13585. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13586. script_pushint(st, -1);
  13587. if(buf) aFree(buf);
  13588. if(ref_str) aFree(ref_str);
  13589. return 0;
  13590. }
  13591. // Save value if any
  13592. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13593. if(ref_str==NULL){
  13594. CREATE(ref_str, char, strlen(str)+1);
  13595. }
  13596. if(sscanf(str, buf, ref_str)==0){
  13597. break;
  13598. }
  13599. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13600. } else { // Number
  13601. if(sscanf(str, buf, &ref_int)==0){
  13602. break;
  13603. }
  13604. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13605. }
  13606. arg++;
  13607. // Disable used format (%... -> %*...)
  13608. buf_p = strchr(buf, 0);
  13609. memmove(buf_p-len+2, buf_p-len+1, len);
  13610. *(buf_p-len+1) = '*';
  13611. }
  13612. script_pushint(st, arg);
  13613. if(buf) aFree(buf);
  13614. if(ref_str) aFree(ref_str);
  13615. return SCRIPT_CMD_SUCCESS;
  13616. }
  13617. //=======================================================
  13618. // strpos(<haystack>, <needle>)
  13619. // strpos(<haystack>, <needle>, <offset>)
  13620. //
  13621. // Implements PHP style strpos. Adapted from code from
  13622. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13623. //-------------------------------------------------------
  13624. BUILDIN_FUNC(strpos) {
  13625. const char *haystack = script_getstr(st,2);
  13626. const char *needle = script_getstr(st,3);
  13627. int i;
  13628. size_t len;
  13629. if( script_hasdata(st,4) )
  13630. i = script_getnum(st,4);
  13631. else
  13632. i = 0;
  13633. if (needle[0] == '\0') {
  13634. script_pushint(st, -1);
  13635. return 0;
  13636. }
  13637. len = strlen(haystack);
  13638. for ( ; i < len; ++i ) {
  13639. if ( haystack[i] == *needle ) {
  13640. // matched starting char -- loop through remaining chars
  13641. const char *h, *n;
  13642. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13643. if ( *h != *n ) {
  13644. break;
  13645. }
  13646. }
  13647. if ( !*n ) { // matched all of 'needle' to null termination
  13648. script_pushint(st, i);
  13649. return 0;
  13650. }
  13651. }
  13652. }
  13653. script_pushint(st, -1);
  13654. return SCRIPT_CMD_SUCCESS;
  13655. }
  13656. //===============================================================
  13657. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13658. //
  13659. // Note: Finds all instances of <search> in <input> and replaces
  13660. // with <replace>. If specified will only replace as many
  13661. // instances as specified in <count>. By default will be case
  13662. // sensitive.
  13663. //---------------------------------------------------------------
  13664. BUILDIN_FUNC(replacestr)
  13665. {
  13666. const char *input = script_getstr(st, 2);
  13667. const char *find = script_getstr(st, 3);
  13668. const char *replace = script_getstr(st, 4);
  13669. size_t inputlen = strlen(input);
  13670. size_t findlen = strlen(find);
  13671. struct StringBuf output;
  13672. bool usecase = true;
  13673. int count = 0;
  13674. int numFinds = 0;
  13675. int i = 0, f = 0;
  13676. if(findlen == 0) {
  13677. ShowError("script:replacestr: Invalid search length.\n");
  13678. st->state = END;
  13679. return 1;
  13680. }
  13681. if(script_hasdata(st, 5)) {
  13682. struct script_data *data = script_getdata(st, 5);
  13683. get_val(st, data); // Convert into value in case of a variable
  13684. if( !data_isstring(data) )
  13685. usecase = script_getnum(st, 5) != 0;
  13686. else {
  13687. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13688. st->state = END;
  13689. return 1;
  13690. }
  13691. }
  13692. if(script_hasdata(st, 6)) {
  13693. count = script_getnum(st, 6);
  13694. if(count == 0) {
  13695. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13696. st->state = END;
  13697. return 1;
  13698. }
  13699. }
  13700. StringBuf_Init(&output);
  13701. for(; i < inputlen; i++) {
  13702. if(count && count == numFinds) { //found enough, stop looking
  13703. break;
  13704. }
  13705. for(f = 0; f <= findlen; f++) {
  13706. if(f == findlen) { //complete match
  13707. numFinds++;
  13708. StringBuf_AppendStr(&output, replace);
  13709. i += findlen - 1;
  13710. break;
  13711. } else {
  13712. if(usecase) {
  13713. if((i + f) > inputlen || input[i + f] != find[f]) {
  13714. StringBuf_Printf(&output, "%c", input[i]);
  13715. break;
  13716. }
  13717. } else {
  13718. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13719. StringBuf_Printf(&output, "%c", input[i]);
  13720. break;
  13721. }
  13722. }
  13723. }
  13724. }
  13725. }
  13726. //append excess after enough found
  13727. if(i < inputlen)
  13728. StringBuf_AppendStr(&output, &(input[i]));
  13729. script_pushstrcopy(st, StringBuf_Value(&output));
  13730. StringBuf_Destroy(&output);
  13731. return SCRIPT_CMD_SUCCESS;
  13732. }
  13733. //========================================================
  13734. // countstr <input>, <search>{, <usecase>}
  13735. //
  13736. // Note: Counts the number of times <search> occurs in
  13737. // <input>. By default will be case sensitive.
  13738. //--------------------------------------------------------
  13739. BUILDIN_FUNC(countstr)
  13740. {
  13741. const char *input = script_getstr(st, 2);
  13742. const char *find = script_getstr(st, 3);
  13743. size_t inputlen = strlen(input);
  13744. size_t findlen = strlen(find);
  13745. bool usecase = true;
  13746. int numFinds = 0;
  13747. int i = 0, f = 0;
  13748. if(findlen == 0) {
  13749. ShowError("script:countstr: Invalid search length.\n");
  13750. st->state = END;
  13751. return 1;
  13752. }
  13753. if(script_hasdata(st, 4)) {
  13754. struct script_data *data;
  13755. data = script_getdata(st, 4);
  13756. get_val(st, data); // Convert into value in case of a variable
  13757. if( !data_isstring(data) )
  13758. usecase = script_getnum(st, 4) != 0;
  13759. else {
  13760. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13761. st->state = END;
  13762. return 1;
  13763. }
  13764. }
  13765. for(; i < inputlen; i++) {
  13766. for(f = 0; f <= findlen; f++) {
  13767. if(f == findlen) { //complete match
  13768. numFinds++;
  13769. i += findlen - 1;
  13770. break;
  13771. } else {
  13772. if(usecase) {
  13773. if((i + f) > inputlen || input[i + f] != find[f]) {
  13774. break;
  13775. }
  13776. } else {
  13777. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13778. break;
  13779. }
  13780. }
  13781. }
  13782. }
  13783. }
  13784. script_pushint(st, numFinds);
  13785. return SCRIPT_CMD_SUCCESS;
  13786. }
  13787. /// Changes the display name and/or display class of the npc.
  13788. /// Returns 0 is successful, 1 if the npc does not exist.
  13789. ///
  13790. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13791. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13792. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13793. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13794. BUILDIN_FUNC(setnpcdisplay)
  13795. {
  13796. const char* name;
  13797. const char* newname = NULL;
  13798. int class_ = -1, size = -1;
  13799. struct script_data* data;
  13800. struct npc_data* nd;
  13801. name = script_getstr(st,2);
  13802. data = script_getdata(st,3);
  13803. if( script_hasdata(st,4) )
  13804. class_ = script_getnum(st,4);
  13805. if( script_hasdata(st,5) )
  13806. size = script_getnum(st,5);
  13807. get_val(st, data);
  13808. if( data_isstring(data) )
  13809. newname = conv_str(st,data);
  13810. else if( data_isint(data) )
  13811. class_ = conv_num(st,data);
  13812. else
  13813. {
  13814. ShowError("script:setnpcdisplay: expected string or number\n");
  13815. script_reportdata(data);
  13816. return 1;
  13817. }
  13818. nd = npc_name2id(name);
  13819. if( nd == NULL )
  13820. {// not found
  13821. script_pushint(st,1);
  13822. return 0;
  13823. }
  13824. // update npc
  13825. if( newname )
  13826. npc_setdisplayname(nd, newname);
  13827. if( size != -1 && size != (int)nd->size )
  13828. nd->size = size;
  13829. else
  13830. size = -1;
  13831. if( class_ != -1 && nd->class_ != class_ )
  13832. npc_setclass(nd, class_);
  13833. else if( size != -1 )
  13834. { // Required to update the visual size
  13835. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13836. clif_spawn(&nd->bl);
  13837. }
  13838. script_pushint(st,0);
  13839. return SCRIPT_CMD_SUCCESS;
  13840. }
  13841. BUILDIN_FUNC(atoi)
  13842. {
  13843. const char *value;
  13844. value = script_getstr(st,2);
  13845. script_pushint(st,atoi(value));
  13846. return SCRIPT_CMD_SUCCESS;
  13847. }
  13848. BUILDIN_FUNC(axtoi)
  13849. {
  13850. const char *hex = script_getstr(st,2);
  13851. long value = strtol(hex, NULL, 16);
  13852. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13853. value = cap_value(value, INT_MIN, INT_MAX);
  13854. #endif
  13855. script_pushint(st, (int)value);
  13856. return SCRIPT_CMD_SUCCESS;
  13857. }
  13858. BUILDIN_FUNC(strtol)
  13859. {
  13860. const char *string = script_getstr(st, 2);
  13861. int base = script_getnum(st, 3);
  13862. long value = strtol(string, NULL, base);
  13863. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13864. value = cap_value(value, INT_MIN, INT_MAX);
  13865. #endif
  13866. script_pushint(st, (int)value);
  13867. return SCRIPT_CMD_SUCCESS;
  13868. }
  13869. // case-insensitive substring search [lordalfa]
  13870. BUILDIN_FUNC(compare)
  13871. {
  13872. const char *message;
  13873. const char *cmpstring;
  13874. message = script_getstr(st,2);
  13875. cmpstring = script_getstr(st,3);
  13876. script_pushint(st,(stristr(message,cmpstring) != NULL));
  13877. return SCRIPT_CMD_SUCCESS;
  13878. }
  13879. BUILDIN_FUNC(strcmp)
  13880. {
  13881. const char *str1;
  13882. const char *str2;
  13883. str1 = script_getstr(st,2);
  13884. str2 = script_getstr(st,3);
  13885. script_pushint(st,strcmp(str1, str2));
  13886. return SCRIPT_CMD_SUCCESS;
  13887. }
  13888. // [zBuffer] List of mathematics commands --->
  13889. BUILDIN_FUNC(sqrt)
  13890. {
  13891. double i, a;
  13892. i = script_getnum(st,2);
  13893. a = sqrt(i);
  13894. script_pushint(st,(int)a);
  13895. return SCRIPT_CMD_SUCCESS;
  13896. }
  13897. BUILDIN_FUNC(pow)
  13898. {
  13899. double i, a, b;
  13900. a = script_getnum(st,2);
  13901. b = script_getnum(st,3);
  13902. i = pow(a,b);
  13903. script_pushint(st,(int)i);
  13904. return SCRIPT_CMD_SUCCESS;
  13905. }
  13906. BUILDIN_FUNC(distance)
  13907. {
  13908. int x0, y0, x1, y1;
  13909. x0 = script_getnum(st,2);
  13910. y0 = script_getnum(st,3);
  13911. x1 = script_getnum(st,4);
  13912. y1 = script_getnum(st,5);
  13913. script_pushint(st,distance_xy(x0,y0,x1,y1));
  13914. return SCRIPT_CMD_SUCCESS;
  13915. }
  13916. // <--- [zBuffer] List of mathematics commands
  13917. BUILDIN_FUNC(md5)
  13918. {
  13919. const char *tmpstr;
  13920. char *md5str;
  13921. tmpstr = script_getstr(st,2);
  13922. md5str = (char *)aMalloc((32+1)*sizeof(char));
  13923. MD5_String(tmpstr, md5str);
  13924. script_pushstr(st, md5str);
  13925. return SCRIPT_CMD_SUCCESS;
  13926. }
  13927. // [zBuffer] List of dynamic var commands --->
  13928. /**
  13929. * setd "<variable name>",<value>{,<char_id>};
  13930. **/
  13931. BUILDIN_FUNC(setd)
  13932. {
  13933. TBL_PC *sd = NULL;
  13934. char varname[100];
  13935. const char *buffer;
  13936. int elem;
  13937. buffer = script_getstr(st, 2);
  13938. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  13939. elem = 0;
  13940. if( not_server_variable(*varname) ) {
  13941. if (!script_charid2sd(4,sd))
  13942. return SCRIPT_CMD_FAILURE;
  13943. }
  13944. if( is_string_variable(varname) ) {
  13945. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  13946. } else {
  13947. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  13948. }
  13949. return SCRIPT_CMD_SUCCESS;
  13950. }
  13951. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  13952. {
  13953. int i, j;
  13954. TBL_PC* sd = NULL;
  13955. const char* query;
  13956. struct script_data* data;
  13957. const char* name;
  13958. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  13959. int num_vars;
  13960. int num_cols;
  13961. // check target variables
  13962. for( i = 3; script_hasdata(st,i); ++i ) {
  13963. data = script_getdata(st, i);
  13964. if( data_isreference(data) ) { // it's a variable
  13965. name = reference_getname(data);
  13966. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  13967. sd = script_rid2sd(st);
  13968. if( sd == NULL ) { // no player attached
  13969. script_reportdata(data);
  13970. st->state = END;
  13971. return 1;
  13972. }
  13973. }
  13974. } else {
  13975. ShowError("script:query_sql: not a variable\n");
  13976. script_reportdata(data);
  13977. st->state = END;
  13978. return 1;
  13979. }
  13980. }
  13981. num_vars = i - 3;
  13982. // Execute the query
  13983. query = script_getstr(st,2);
  13984. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  13985. Sql_ShowDebug(handle);
  13986. script_pushint(st, -1);
  13987. return 1;
  13988. }
  13989. if( Sql_NumRows(handle) == 0 ) { // No data received
  13990. Sql_FreeResult(handle);
  13991. script_pushint(st, -1);
  13992. return 0;
  13993. }
  13994. // Count the number of columns to store
  13995. num_cols = Sql_NumColumns(handle);
  13996. if( num_vars < num_cols ) {
  13997. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  13998. script_reportsrc(st);
  13999. } else if( num_vars > num_cols ) {
  14000. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14001. script_reportsrc(st);
  14002. }
  14003. // Store data
  14004. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14005. for( j = 0; j < num_vars; ++j ) {
  14006. char* str = NULL;
  14007. if( j < num_cols )
  14008. Sql_GetData(handle, j, &str, NULL);
  14009. data = script_getdata(st, j+3);
  14010. name = reference_getname(data);
  14011. if( is_string_variable(name) )
  14012. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  14013. else
  14014. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  14015. }
  14016. }
  14017. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14018. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14019. script_reportsrc(st);
  14020. }
  14021. // Free data
  14022. Sql_FreeResult(handle);
  14023. script_pushint(st, i);
  14024. return SCRIPT_CMD_SUCCESS;
  14025. }
  14026. BUILDIN_FUNC(query_sql) {
  14027. #ifdef BETA_THREAD_TEST
  14028. if( st->state != RERUNLINE ) {
  14029. queryThread_add(st,false);
  14030. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14031. } else
  14032. st->state = RUN;
  14033. return 0;
  14034. #else
  14035. return buildin_query_sql_sub(st, qsmysql_handle);
  14036. #endif
  14037. }
  14038. BUILDIN_FUNC(query_logsql) {
  14039. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14040. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14041. script_pushint(st,-1);
  14042. return 1;
  14043. }
  14044. #ifdef BETA_THREAD_TEST
  14045. if( st->state != RERUNLINE ) {
  14046. queryThread_add(st,true);
  14047. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14048. } else
  14049. st->state = RUN;
  14050. return 0;
  14051. #else
  14052. return buildin_query_sql_sub(st, logmysql_handle);
  14053. #endif
  14054. }
  14055. //Allows escaping of a given string.
  14056. BUILDIN_FUNC(escape_sql)
  14057. {
  14058. const char *str;
  14059. char *esc_str;
  14060. size_t len;
  14061. str = script_getstr(st,2);
  14062. len = strlen(str);
  14063. esc_str = (char*)aMalloc(len*2+1);
  14064. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14065. script_pushstr(st, esc_str);
  14066. return SCRIPT_CMD_SUCCESS;
  14067. }
  14068. BUILDIN_FUNC(getd)
  14069. {
  14070. char varname[100];
  14071. const char *buffer;
  14072. int elem;
  14073. buffer = script_getstr(st, 2);
  14074. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  14075. elem = 0;
  14076. // Push the 'pointer' so it's more flexible [Lance]
  14077. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14078. return SCRIPT_CMD_SUCCESS;
  14079. }
  14080. BUILDIN_FUNC(callshop)
  14081. {
  14082. TBL_PC *sd = NULL;
  14083. struct npc_data *nd;
  14084. const char *shopname;
  14085. int flag = 0;
  14086. sd = script_rid2sd(st);
  14087. if (!sd) {
  14088. script_pushint(st,0);
  14089. return 0;
  14090. }
  14091. shopname = script_getstr(st, 2);
  14092. if (script_hasdata(st,3))
  14093. flag = script_getnum(st,3);
  14094. nd = npc_name2id(shopname);
  14095. 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) ) {
  14096. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14097. script_pushint(st,0);
  14098. return 1;
  14099. }
  14100. if (nd->subtype == NPCTYPE_SHOP || nd->subtype == NPCTYPE_ITEMSHOP || nd->subtype == NPCTYPE_POINTSHOP) {
  14101. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14102. sd->state.callshop = 1;
  14103. switch (flag) {
  14104. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14105. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14106. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14107. }
  14108. }
  14109. #if PACKETVER >= 20131223
  14110. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14111. unsigned short i;
  14112. for (i = 0; i < nd->u.shop.count; i++) {
  14113. if (nd->u.shop.shop_item[i].qty)
  14114. break;
  14115. }
  14116. if (i == nd->u.shop.count) {
  14117. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 534));
  14118. return false;
  14119. }
  14120. sd->npc_shopid = nd->bl.id;
  14121. clif_npc_market_open(sd, nd);
  14122. script_pushint(st,1);
  14123. return SCRIPT_CMD_SUCCESS;
  14124. }
  14125. #endif
  14126. else
  14127. clif_cashshop_show(sd, nd);
  14128. sd->npc_shopid = nd->bl.id;
  14129. script_pushint(st,1);
  14130. return SCRIPT_CMD_SUCCESS;
  14131. }
  14132. BUILDIN_FUNC(npcshopitem)
  14133. {
  14134. const char* npcname = script_getstr(st, 2);
  14135. struct npc_data* nd = npc_name2id(npcname);
  14136. int n, i;
  14137. int amount;
  14138. uint16 offs = 2;
  14139. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14140. script_pushint(st,0);
  14141. return 0;
  14142. }
  14143. #if PACKETVER >= 20131223
  14144. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14145. offs = 3;
  14146. npc_market_delfromsql_(nd->exname, 0, true);
  14147. }
  14148. #endif
  14149. // get the count of new entries
  14150. amount = (script_lastdata(st)-2)/offs;
  14151. // generate new shop item list
  14152. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14153. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14154. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14155. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14156. #if PACKETVER >= 20131223
  14157. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14158. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14159. nd->u.shop.shop_item[n].flag = 1;
  14160. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14161. }
  14162. #endif
  14163. }
  14164. nd->u.shop.count = n;
  14165. script_pushint(st,1);
  14166. return SCRIPT_CMD_SUCCESS;
  14167. }
  14168. BUILDIN_FUNC(npcshopadditem)
  14169. {
  14170. const char* npcname = script_getstr(st,2);
  14171. struct npc_data* nd = npc_name2id(npcname);
  14172. int n, i;
  14173. int amount;
  14174. uint16 offs = 2;
  14175. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14176. script_pushint(st,0);
  14177. return 0;
  14178. }
  14179. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14180. offs = 3;
  14181. // get the count of new entries
  14182. amount = (script_lastdata(st)-2)/offs;
  14183. #if PACKETVER >= 20131223
  14184. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14185. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14186. uint16 nameid = script_getnum(st,i), j;
  14187. // Check existing entries
  14188. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14189. if (j == nd->u.shop.count) {
  14190. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14191. j = nd->u.shop.count;
  14192. nd->u.shop.shop_item[j].flag = 1;
  14193. nd->u.shop.count++;
  14194. }
  14195. nd->u.shop.shop_item[j].nameid = nameid;
  14196. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14197. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14198. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14199. }
  14200. script_pushint(st,1);
  14201. return SCRIPT_CMD_SUCCESS;
  14202. }
  14203. #endif
  14204. // append new items to existing shop item list
  14205. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14206. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14207. {
  14208. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14209. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14210. }
  14211. nd->u.shop.count = n;
  14212. script_pushint(st,1);
  14213. return SCRIPT_CMD_SUCCESS;
  14214. }
  14215. BUILDIN_FUNC(npcshopdelitem)
  14216. {
  14217. const char* npcname = script_getstr(st,2);
  14218. struct npc_data* nd = npc_name2id(npcname);
  14219. int n, i;
  14220. int amount;
  14221. int size;
  14222. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14223. script_pushint(st,0);
  14224. return 0;
  14225. }
  14226. amount = script_lastdata(st)-2;
  14227. size = nd->u.shop.count;
  14228. // remove specified items from the shop item list
  14229. for( i = 3; i < 3 + amount; i++ ) {
  14230. unsigned short nameid = script_getnum(st,i);
  14231. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14232. if( n < size ) {
  14233. if (n+1 != size)
  14234. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14235. #if PACKETVER >= 20131223
  14236. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14237. npc_market_delfromsql_(nd->exname, nameid, false);
  14238. #endif
  14239. size--;
  14240. }
  14241. }
  14242. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14243. nd->u.shop.count = size;
  14244. script_pushint(st,1);
  14245. return SCRIPT_CMD_SUCCESS;
  14246. }
  14247. /**
  14248. * Sets a script to attach to a shop npc.
  14249. * npcshopattach "<npc_name>";
  14250. **/
  14251. BUILDIN_FUNC(npcshopattach)
  14252. {
  14253. const char* npcname = script_getstr(st,2);
  14254. struct npc_data* nd = npc_name2id(npcname);
  14255. int flag = 1;
  14256. if( script_hasdata(st,3) )
  14257. flag = script_getnum(st,3);
  14258. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14259. script_pushint(st,0);
  14260. return 0;
  14261. }
  14262. if (flag)
  14263. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14264. else
  14265. nd->master_nd = NULL;
  14266. script_pushint(st,1);
  14267. return SCRIPT_CMD_SUCCESS;
  14268. }
  14269. /*==========================================
  14270. * Returns some values of an item [Lupus]
  14271. * Price, Weight, etc...
  14272. setitemscript(itemID,"{new item bonus script}",[n]);
  14273. Where n:
  14274. 0 - script
  14275. 1 - Equip script
  14276. 2 - Unequip script
  14277. *------------------------------------------*/
  14278. BUILDIN_FUNC(setitemscript)
  14279. {
  14280. unsigned short item_id;
  14281. int n = 0;
  14282. const char *script;
  14283. struct item_data *i_data;
  14284. struct script_code **dstscript;
  14285. item_id = script_getnum(st,2);
  14286. script = script_getstr(st,3);
  14287. if( script_hasdata(st,4) )
  14288. n=script_getnum(st,4);
  14289. i_data = itemdb_exists(item_id);
  14290. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14291. script_pushint(st,0);
  14292. return 0;
  14293. }
  14294. switch (n) {
  14295. case 2:
  14296. dstscript = &i_data->unequip_script;
  14297. break;
  14298. case 1:
  14299. dstscript = &i_data->equip_script;
  14300. break;
  14301. default:
  14302. dstscript = &i_data->script;
  14303. break;
  14304. }
  14305. if(*dstscript)
  14306. script_free_code(*dstscript);
  14307. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14308. script_pushint(st,1);
  14309. return SCRIPT_CMD_SUCCESS;
  14310. }
  14311. /*=======================================================
  14312. * Add or Update a mob drop temporarily [Akinari]
  14313. * Original Idea By: [Lupus]
  14314. *
  14315. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14316. *
  14317. * If given an item the mob already drops, the rate
  14318. * is updated to the new rate. Rate cannot exceed 10000
  14319. * Returns 1 if succeeded (added/updated a mob drop)
  14320. *-------------------------------------------------------*/
  14321. BUILDIN_FUNC(addmonsterdrop)
  14322. {
  14323. struct mob_db *mob;
  14324. struct script_data *data;
  14325. unsigned short item_id;
  14326. int rate;
  14327. data = script_getdata(st, 2);
  14328. get_val(st, data); // Convert into value in case of a variable
  14329. if(data_isstring(data))
  14330. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14331. else
  14332. mob = mob_db(script_getnum(st,2));
  14333. item_id=script_getnum(st,3);
  14334. rate=script_getnum(st,4);
  14335. if(!itemdb_exists(item_id)){
  14336. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14337. return 1;
  14338. }
  14339. if(mob) { //We got a valid monster, check for available drop slot
  14340. unsigned char i, c = 0;
  14341. for(i = 0; i < MAX_MOB_DROP; i++) {
  14342. if(mob->dropitem[i].nameid) {
  14343. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14344. c = i;
  14345. break;
  14346. }
  14347. continue;
  14348. }
  14349. if(!c) //Accept first available slot only
  14350. c = i;
  14351. }
  14352. if(c) { //Fill in the slot with the item and rate
  14353. mob->dropitem[c].nameid = item_id;
  14354. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14355. script_pushint(st,1);
  14356. } else //No place to put the new drop
  14357. script_pushint(st,0);
  14358. } else {
  14359. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14360. return 1;
  14361. }
  14362. return SCRIPT_CMD_SUCCESS;
  14363. }
  14364. /*=======================================================
  14365. * Delete a mob drop temporarily [Akinari]
  14366. * Original Idea By: [Lupus]
  14367. *
  14368. * delmonsterdrop <mob_id or name>,<item_id>;
  14369. *
  14370. * Returns 1 if succeeded (deleted a mob drop)
  14371. *-------------------------------------------------------*/
  14372. BUILDIN_FUNC(delmonsterdrop)
  14373. {
  14374. struct mob_db *mob;
  14375. struct script_data *data;
  14376. unsigned short item_id;
  14377. data = script_getdata(st, 2);
  14378. get_val(st, data); // Convert into value in case of a variable
  14379. if(data_isstring(data))
  14380. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14381. else
  14382. mob = mob_db(script_getnum(st,2));
  14383. item_id=script_getnum(st,3);
  14384. if(!itemdb_exists(item_id)){
  14385. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14386. return 1;
  14387. }
  14388. if(mob) { //We got a valid monster, check for item drop on monster
  14389. unsigned char i;
  14390. for(i = 0; i < MAX_MOB_DROP; i++) {
  14391. if(mob->dropitem[i].nameid == item_id) {
  14392. mob->dropitem[i].nameid = 0;
  14393. mob->dropitem[i].p = 0;
  14394. script_pushint(st,1);
  14395. return 0;
  14396. }
  14397. }
  14398. //No drop on that monster
  14399. script_pushint(st,0);
  14400. } else {
  14401. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14402. return 1;
  14403. }
  14404. return SCRIPT_CMD_SUCCESS;
  14405. }
  14406. /*==========================================
  14407. * Returns some values of a monster [Lupus]
  14408. * Name, Level, race, size, etc...
  14409. getmonsterinfo(monsterID,queryIndex);
  14410. *------------------------------------------*/
  14411. BUILDIN_FUNC(getmonsterinfo)
  14412. {
  14413. struct mob_db *mob;
  14414. int mob_id;
  14415. mob_id = script_getnum(st,2);
  14416. if (!mobdb_checkid(mob_id)) {
  14417. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14418. if ( !script_getnum(st,3) ) //requested a string
  14419. script_pushconststr(st,"null");
  14420. else
  14421. script_pushint(st,-1);
  14422. return 0;
  14423. }
  14424. mob = mob_db(mob_id);
  14425. switch ( script_getnum(st,3) ) {
  14426. case 0: script_pushstrcopy(st,mob->jname); break;
  14427. case 1: script_pushint(st,mob->lv); break;
  14428. case 2: script_pushint(st,mob->status.max_hp); break;
  14429. case 3: script_pushint(st,mob->base_exp); break;
  14430. case 4: script_pushint(st,mob->job_exp); break;
  14431. case 5: script_pushint(st,mob->status.rhw.atk); break;
  14432. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  14433. case 7: script_pushint(st,mob->status.def); break;
  14434. case 8: script_pushint(st,mob->status.mdef); break;
  14435. case 9: script_pushint(st,mob->status.str); break;
  14436. case 10: script_pushint(st,mob->status.agi); break;
  14437. case 11: script_pushint(st,mob->status.vit); break;
  14438. case 12: script_pushint(st,mob->status.int_); break;
  14439. case 13: script_pushint(st,mob->status.dex); break;
  14440. case 14: script_pushint(st,mob->status.luk); break;
  14441. case 15: script_pushint(st,mob->status.rhw.range); break;
  14442. case 16: script_pushint(st,mob->range2); break;
  14443. case 17: script_pushint(st,mob->range3); break;
  14444. case 18: script_pushint(st,mob->status.size); break;
  14445. case 19: script_pushint(st,mob->status.race); break;
  14446. case 20: script_pushint(st,mob->status.def_ele); break;
  14447. case 21: script_pushint(st,mob->status.mode); break;
  14448. case 22: script_pushint(st,mob->mexp); break;
  14449. default: script_pushint(st,-1); //wrong Index
  14450. }
  14451. return SCRIPT_CMD_SUCCESS;
  14452. }
  14453. /**
  14454. * Check player's vending/buyingstore/autotrade state
  14455. * checkvending({"<Player Name>"})
  14456. * @author [Nab4]
  14457. */
  14458. BUILDIN_FUNC(checkvending) {
  14459. TBL_PC *sd = NULL;
  14460. if (script_hasdata(st,2)) {
  14461. if (!(sd = map_nick2sd(script_getstr(st,2)))) {
  14462. ShowError("buildin_checkvending: Player '%s' is not online!\n", script_getstr(st,2));
  14463. return SCRIPT_CMD_FAILURE;
  14464. }
  14465. }
  14466. else
  14467. sd = script_rid2sd(st);
  14468. if (!sd) {
  14469. script_pushint(st,0);
  14470. return SCRIPT_CMD_SUCCESS;
  14471. }
  14472. else {
  14473. int8 ret = 0;
  14474. if (sd->state.vending)
  14475. ret = 1;
  14476. else if (sd->state.buyingstore)
  14477. ret = 4;
  14478. if (sd->state.autotrade)
  14479. ret |= 2;
  14480. script_pushint(st, ret);
  14481. }
  14482. return SCRIPT_CMD_SUCCESS;
  14483. }
  14484. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14485. {
  14486. TBL_PC *sd = NULL;
  14487. if(script_hasdata(st,2))
  14488. sd = map_nick2sd(script_getstr(st,2));
  14489. else
  14490. sd = script_rid2sd(st);
  14491. if(sd)
  14492. script_pushint(st,(sd->chatID != 0));
  14493. else
  14494. script_pushint(st,0);
  14495. return SCRIPT_CMD_SUCCESS;
  14496. }
  14497. BUILDIN_FUNC(checkidle)
  14498. {
  14499. TBL_PC *sd = NULL;
  14500. if (script_hasdata(st, 2))
  14501. sd = map_nick2sd(script_getstr(st, 2));
  14502. else
  14503. sd = script_rid2sd(st);
  14504. if (sd)
  14505. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14506. else
  14507. script_pushint(st, 0);
  14508. return SCRIPT_CMD_SUCCESS;
  14509. }
  14510. BUILDIN_FUNC(searchitem)
  14511. {
  14512. struct script_data* data = script_getdata(st, 2);
  14513. const char *itemname = script_getstr(st,3);
  14514. struct item_data *items[MAX_SEARCH];
  14515. int count;
  14516. char* name;
  14517. int32 start;
  14518. int32 id;
  14519. int32 i;
  14520. TBL_PC* sd = NULL;
  14521. if ((items[0] = itemdb_exists(atoi(itemname))))
  14522. count = 1;
  14523. else {
  14524. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14525. if (count > MAX_SEARCH) count = MAX_SEARCH;
  14526. }
  14527. if (!count) {
  14528. script_pushint(st, 0);
  14529. return 0;
  14530. }
  14531. if( !data_isreference(data) )
  14532. {
  14533. ShowError("script:searchitem: not a variable\n");
  14534. script_reportdata(data);
  14535. st->state = END;
  14536. return 1;// not a variable
  14537. }
  14538. id = reference_getid(data);
  14539. start = reference_getindex(data);
  14540. name = reference_getname(data);
  14541. if( not_server_variable(*name) )
  14542. {
  14543. sd = script_rid2sd(st);
  14544. if( sd == NULL )
  14545. return 0;// no player attached
  14546. }
  14547. if( is_string_variable(name) )
  14548. {// string array
  14549. ShowError("script:searchitem: not an integer array reference\n");
  14550. script_reportdata(data);
  14551. st->state = END;
  14552. return 1;// not supported
  14553. }
  14554. for( i = 0; i < count; ++start, ++i )
  14555. {// Set array
  14556. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14557. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14558. }
  14559. script_pushint(st, count);
  14560. return SCRIPT_CMD_SUCCESS;
  14561. }
  14562. // [zBuffer] List of player cont commands --->
  14563. BUILDIN_FUNC(rid2name)
  14564. {
  14565. struct block_list *bl = NULL;
  14566. int rid = script_getnum(st,2);
  14567. if((bl = map_id2bl(rid)))
  14568. {
  14569. switch(bl->type) {
  14570. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14571. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14572. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14573. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14574. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14575. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14576. default:
  14577. ShowError("buildin_rid2name: BL type unknown.\n");
  14578. script_pushconststr(st,"");
  14579. break;
  14580. }
  14581. } else {
  14582. ShowError("buildin_rid2name: invalid RID\n");
  14583. script_pushconststr(st,"(null)");
  14584. }
  14585. return SCRIPT_CMD_SUCCESS;
  14586. }
  14587. BUILDIN_FUNC(pcblockmove)
  14588. {
  14589. struct block_list *bl = NULL;
  14590. if (script_getnum(st, 2))
  14591. bl = map_id2bl(script_getnum(st,2));
  14592. else
  14593. bl = map_id2bl(st->rid);
  14594. if (bl) {
  14595. struct unit_data *ud = unit_bl2ud(bl);
  14596. if (ud)
  14597. ud->state.blockedmove = script_getnum(st,3) > 0;
  14598. }
  14599. return SCRIPT_CMD_SUCCESS;
  14600. }
  14601. BUILDIN_FUNC(pcfollow)
  14602. {
  14603. int id, targetid;
  14604. TBL_PC *sd = NULL;
  14605. id = script_getnum(st,2);
  14606. targetid = script_getnum(st,3);
  14607. if(id)
  14608. sd = map_id2sd(id);
  14609. else
  14610. sd = script_rid2sd(st);
  14611. if(sd)
  14612. pc_follow(sd, targetid);
  14613. return SCRIPT_CMD_SUCCESS;
  14614. }
  14615. BUILDIN_FUNC(pcstopfollow)
  14616. {
  14617. int id;
  14618. TBL_PC *sd = NULL;
  14619. id = script_getnum(st,2);
  14620. if(id)
  14621. sd = map_id2sd(id);
  14622. else
  14623. sd = script_rid2sd(st);
  14624. if(sd)
  14625. pc_stop_following(sd);
  14626. return SCRIPT_CMD_SUCCESS;
  14627. }
  14628. // <--- [zBuffer] List of player cont commands
  14629. // [zBuffer] List of unit control commands --->
  14630. /// Gets the type of the given Game ID.
  14631. ///
  14632. /// getunittype <unit id>;
  14633. BUILDIN_FUNC(getunittype)
  14634. {
  14635. struct block_list* bl;
  14636. uint8 value = 0;
  14637. bl = map_id2bl(script_getnum(st, 2));
  14638. if (!bl) {
  14639. ShowWarning("buildin_getunittype: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14640. script_pushint(st, -1);
  14641. return SCRIPT_CMD_FAILURE;
  14642. }
  14643. switch (bl->type) {
  14644. case BL_MOB: value = 0; break;
  14645. case BL_HOM: value = 1; break;
  14646. case BL_PET: value = 2; break;
  14647. case BL_MER: value = 3; break;
  14648. case BL_ELEM: value = 4; break;
  14649. case BL_NPC: value = 5; break;
  14650. }
  14651. script_pushint(st, value);
  14652. return SCRIPT_CMD_SUCCESS;
  14653. }
  14654. /// Gets specific live information of a bl.
  14655. ///
  14656. /// getunitdata <unit id>,<arrayname>;
  14657. BUILDIN_FUNC(getunitdata)
  14658. {
  14659. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14660. struct block_list* bl;
  14661. TBL_MOB* md = NULL;
  14662. TBL_HOM* hd = NULL;
  14663. TBL_MER* mc = NULL;
  14664. TBL_PET* pd = NULL;
  14665. TBL_ELEM* ed = NULL;
  14666. TBL_NPC* nd = NULL;
  14667. char* name;
  14668. struct script_data *data = script_getdata(st, 3);
  14669. if (!data_isreference(data)) {
  14670. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14671. return SCRIPT_CMD_FAILURE;
  14672. }
  14673. bl = map_id2bl(script_getnum(st, 2));
  14674. if (!bl) {
  14675. ShowWarning("buildin_getunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14676. return SCRIPT_CMD_FAILURE;
  14677. }
  14678. switch (bl->type) {
  14679. case BL_MOB: md = map_id2md(bl->id); break;
  14680. case BL_HOM: hd = map_id2hd(bl->id); break;
  14681. case BL_PET: pd = map_id2pd(bl->id); break;
  14682. case BL_MER: mc = map_id2mc(bl->id); break;
  14683. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14684. case BL_NPC: nd = map_id2nd(bl->id); break;
  14685. }
  14686. name = reference_getname(data);
  14687. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  14688. switch(bl->type) {
  14689. case BL_MOB:
  14690. if (!md) {
  14691. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14692. return SCRIPT_CMD_FAILURE;
  14693. }
  14694. getunitdata_sub(0, md->status.size);
  14695. getunitdata_sub(1, md->level);
  14696. getunitdata_sub(2, md->status.hp);
  14697. getunitdata_sub(3, md->status.max_hp);
  14698. getunitdata_sub(4, md->master_id);
  14699. getunitdata_sub(5, md->bl.m);
  14700. getunitdata_sub(6, md->bl.x);
  14701. getunitdata_sub(7, md->bl.y);
  14702. getunitdata_sub(8, md->status.speed);
  14703. getunitdata_sub(9, md->status.mode);
  14704. getunitdata_sub(10,md->special_state.ai);
  14705. getunitdata_sub(11,md->sc.option);
  14706. getunitdata_sub(12,md->vd->sex);
  14707. getunitdata_sub(13,md->vd->class_);
  14708. getunitdata_sub(14,md->vd->hair_style);
  14709. getunitdata_sub(15,md->vd->hair_color);
  14710. getunitdata_sub(16,md->vd->head_bottom);
  14711. getunitdata_sub(17,md->vd->head_mid);
  14712. getunitdata_sub(18,md->vd->head_top);
  14713. getunitdata_sub(19,md->vd->cloth_color);
  14714. getunitdata_sub(20,md->vd->shield);
  14715. getunitdata_sub(21,md->vd->weapon);
  14716. getunitdata_sub(22,md->ud.dir);
  14717. getunitdata_sub(23,md->status.str);
  14718. getunitdata_sub(24,md->status.agi);
  14719. getunitdata_sub(25,md->status.vit);
  14720. getunitdata_sub(26,md->status.int_);
  14721. getunitdata_sub(27,md->status.dex);
  14722. getunitdata_sub(28,md->status.luk);
  14723. getunitdata_sub(29,md->state.copy_master_mode);
  14724. getunitdata_sub(30,md->ud.immune_attack);
  14725. break;
  14726. case BL_HOM:
  14727. if (!hd) {
  14728. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  14729. return SCRIPT_CMD_FAILURE;
  14730. }
  14731. getunitdata_sub(0, hd->base_status.size);
  14732. getunitdata_sub(1, hd->homunculus.level);
  14733. getunitdata_sub(2, hd->homunculus.hp);
  14734. getunitdata_sub(3, hd->homunculus.max_hp);
  14735. getunitdata_sub(4, hd->homunculus.sp);
  14736. getunitdata_sub(5, hd->homunculus.max_sp);
  14737. getunitdata_sub(6, hd->homunculus.char_id);
  14738. getunitdata_sub(7, hd->bl.m);
  14739. getunitdata_sub(8, hd->bl.x);
  14740. getunitdata_sub(9, hd->bl.y);
  14741. getunitdata_sub(10,hd->homunculus.hunger);
  14742. getunitdata_sub(11,hd->homunculus.intimacy);
  14743. getunitdata_sub(12,hd->base_status.speed);
  14744. getunitdata_sub(13,hd->ud.dir);
  14745. getunitdata_sub(14,hd->ud.canmove_tick);
  14746. getunitdata_sub(15,hd->base_status.str);
  14747. getunitdata_sub(16,hd->base_status.agi);
  14748. getunitdata_sub(17,hd->base_status.vit);
  14749. getunitdata_sub(18,hd->base_status.int_);
  14750. getunitdata_sub(19,hd->base_status.dex);
  14751. getunitdata_sub(20,hd->base_status.luk);
  14752. getunitdata_sub(21,hd->ud.immune_attack);
  14753. break;
  14754. case BL_PET:
  14755. if (!pd) {
  14756. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  14757. return SCRIPT_CMD_FAILURE;
  14758. }
  14759. getunitdata_sub(0, pd->status.size);
  14760. getunitdata_sub(1, pd->pet.level);
  14761. getunitdata_sub(2, pd->status.hp);
  14762. getunitdata_sub(3, pd->status.max_hp);
  14763. getunitdata_sub(4, pd->pet.account_id);
  14764. getunitdata_sub(5, pd->bl.m);
  14765. getunitdata_sub(6, pd->bl.x);
  14766. getunitdata_sub(7, pd->bl.y);
  14767. getunitdata_sub(8, pd->pet.hungry);
  14768. getunitdata_sub(9, pd->pet.intimate);
  14769. getunitdata_sub(10,pd->status.speed);
  14770. getunitdata_sub(11,pd->ud.dir);
  14771. getunitdata_sub(12,pd->ud.canmove_tick);
  14772. getunitdata_sub(13,pd->status.str);
  14773. getunitdata_sub(14,pd->status.agi);
  14774. getunitdata_sub(15,pd->status.vit);
  14775. getunitdata_sub(16,pd->status.int_);
  14776. getunitdata_sub(17,pd->status.dex);
  14777. getunitdata_sub(18,pd->status.luk);
  14778. getunitdata_sub(19,pd->ud.immune_attack);
  14779. break;
  14780. case BL_MER:
  14781. if (!mc) {
  14782. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  14783. return SCRIPT_CMD_FAILURE;
  14784. }
  14785. getunitdata_sub(0, mc->base_status.size);
  14786. getunitdata_sub(1, mc->base_status.hp);
  14787. getunitdata_sub(2, mc->base_status.max_hp);
  14788. getunitdata_sub(3, mc->mercenary.char_id);
  14789. getunitdata_sub(4, mc->bl.m);
  14790. getunitdata_sub(5, mc->bl.x);
  14791. getunitdata_sub(6, mc->bl.y);
  14792. getunitdata_sub(7, mc->mercenary.kill_count);
  14793. getunitdata_sub(8, mc->mercenary.life_time);
  14794. getunitdata_sub(9, mc->base_status.speed);
  14795. getunitdata_sub(10,mc->ud.dir);
  14796. getunitdata_sub(11,mc->ud.canmove_tick);
  14797. getunitdata_sub(12,mc->base_status.str);
  14798. getunitdata_sub(13,mc->base_status.agi);
  14799. getunitdata_sub(14,mc->base_status.vit);
  14800. getunitdata_sub(15,mc->base_status.int_);
  14801. getunitdata_sub(16,mc->base_status.dex);
  14802. getunitdata_sub(17,mc->base_status.luk);
  14803. getunitdata_sub(18,mc->ud.immune_attack);
  14804. break;
  14805. case BL_ELEM:
  14806. if (!ed) {
  14807. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  14808. return SCRIPT_CMD_FAILURE;
  14809. }
  14810. getunitdata_sub(0, ed->base_status.size);
  14811. getunitdata_sub(1, ed->elemental.hp);
  14812. getunitdata_sub(2, ed->elemental.max_hp);
  14813. getunitdata_sub(3, ed->elemental.sp);
  14814. getunitdata_sub(4, ed->elemental.max_sp);
  14815. getunitdata_sub(5, ed->elemental.char_id);
  14816. getunitdata_sub(6, ed->bl.m);
  14817. getunitdata_sub(7, ed->bl.x);
  14818. getunitdata_sub(8, ed->bl.y);
  14819. getunitdata_sub(9, ed->elemental.life_time);
  14820. getunitdata_sub(10,ed->elemental.mode);
  14821. getunitdata_sub(11,ed->base_status.speed);
  14822. getunitdata_sub(12,ed->ud.dir);
  14823. getunitdata_sub(13,ed->ud.canmove_tick);
  14824. getunitdata_sub(14,ed->base_status.str);
  14825. getunitdata_sub(15,ed->base_status.agi);
  14826. getunitdata_sub(16,ed->base_status.vit);
  14827. getunitdata_sub(17,ed->base_status.int_);
  14828. getunitdata_sub(18,ed->base_status.dex);
  14829. getunitdata_sub(19,ed->base_status.luk);
  14830. getunitdata_sub(20,ed->ud.immune_attack);
  14831. break;
  14832. case BL_NPC:
  14833. if (!nd) {
  14834. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  14835. return SCRIPT_CMD_FAILURE;
  14836. }
  14837. getunitdata_sub(0, nd->class_);
  14838. getunitdata_sub(1, nd->level);
  14839. getunitdata_sub(2, nd->status.hp);
  14840. getunitdata_sub(3, nd->status.max_hp);
  14841. getunitdata_sub(4, nd->bl.m);
  14842. getunitdata_sub(5, nd->bl.x);
  14843. getunitdata_sub(6, nd->bl.y);
  14844. getunitdata_sub(7, nd->ud.dir);
  14845. getunitdata_sub(8, nd->status.str);
  14846. getunitdata_sub(9, nd->status.agi);
  14847. getunitdata_sub(10,nd->status.vit);
  14848. getunitdata_sub(11,nd->status.int_);
  14849. getunitdata_sub(12,nd->status.dex);
  14850. getunitdata_sub(13,nd->status.luk);
  14851. break;
  14852. default:
  14853. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  14854. return SCRIPT_CMD_FAILURE;
  14855. }
  14856. #undef getunitdata_sub
  14857. return SCRIPT_CMD_SUCCESS;
  14858. }
  14859. /// Changes the live data of a bl.
  14860. ///
  14861. /// setunitdata <unit id>,<type>,<value>;
  14862. BUILDIN_FUNC(setunitdata)
  14863. {
  14864. struct block_list* bl = NULL;
  14865. struct script_data* data;
  14866. const char *mapname = NULL;
  14867. TBL_MOB* md = NULL;
  14868. TBL_HOM* hd = NULL;
  14869. TBL_MER* mc = NULL;
  14870. TBL_PET* pd = NULL;
  14871. TBL_ELEM* ed = NULL;
  14872. TBL_NPC* nd = NULL;
  14873. int type, value = 0;
  14874. bl = map_id2bl(script_getnum(st, 2));
  14875. if (!bl) {
  14876. ShowWarning("buildin_setunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14877. return SCRIPT_CMD_FAILURE;
  14878. }
  14879. switch (bl->type) {
  14880. case BL_MOB: md = map_id2md(bl->id); break;
  14881. case BL_HOM: hd = map_id2hd(bl->id); break;
  14882. case BL_PET: pd = map_id2pd(bl->id); break;
  14883. case BL_MER: mc = map_id2mc(bl->id); break;
  14884. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14885. case BL_NPC:
  14886. nd = map_id2nd(bl->id);
  14887. if (!nd->status.hp)
  14888. status_calc_npc(nd, SCO_FIRST);
  14889. else
  14890. status_calc_npc(nd, SCO_NONE);
  14891. break;
  14892. default:
  14893. ShowError("buildin_setunitdata: Invalid object!");
  14894. return SCRIPT_CMD_FAILURE;
  14895. }
  14896. type = script_getnum(st, 3);
  14897. data = script_getdata(st, 4);
  14898. get_val(st, data);
  14899. if (type == 5 && data_isstring(data))
  14900. mapname = conv_str(st, data);
  14901. else if (data_isint(data))
  14902. value = conv_num(st, data);
  14903. else {
  14904. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).", data->type);
  14905. return SCRIPT_CMD_FAILURE;
  14906. }
  14907. switch (bl->type) {
  14908. case BL_MOB:
  14909. if (!md) {
  14910. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  14911. return SCRIPT_CMD_FAILURE;
  14912. }
  14913. switch (type) {
  14914. case 0: md->status.size = (unsigned char)value; break;
  14915. case 1: md->level = (unsigned short)value; break;
  14916. case 2: status_set_hp(bl, (unsigned int)value, 0); break;
  14917. case 3: status_set_maxhp(bl, (unsigned int)value, 0); break;
  14918. case 4: md->master_id = value; break;
  14919. case 5: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  14920. case 6: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  14921. case 7: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  14922. case 8: md->status.speed = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14923. case 9: md->status.mode = (enum e_mode)value; status_calc_bl(bl, SCB_MODE); break;
  14924. case 10: md->special_state.ai = (enum mob_ai)value; break;
  14925. case 11: md->sc.option = (unsigned short)value; break;
  14926. case 12: md->vd->sex = (char)value; break;
  14927. case 13: status_set_viewdata(bl, (unsigned short)value); break;
  14928. case 14: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  14929. case 15: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  14930. case 16: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  14931. case 17: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  14932. case 18: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  14933. case 19: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  14934. case 20: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  14935. case 21: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  14936. case 22: unit_setdir(bl, (uint8)value); break;
  14937. case 23: md->status.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14938. case 24: md->status.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14939. case 25: md->status.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14940. case 26: md->status.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14941. case 27: md->status.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14942. case 28: md->status.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14943. case 29: md->state.copy_master_mode = value > 0 ? 1 : 0; status_calc_bl(bl, SCB_MODE); break;
  14944. case 30: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14945. default:
  14946. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  14947. return SCRIPT_CMD_FAILURE;
  14948. }
  14949. break;
  14950. case BL_HOM:
  14951. if (!hd) {
  14952. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  14953. return SCRIPT_CMD_FAILURE;
  14954. }
  14955. switch (type) {
  14956. case 0: hd->base_status.size = (unsigned char)value; break;
  14957. case 1: hd->homunculus.level = (unsigned short)value; break;
  14958. case 2: status_set_hp(bl, (unsigned int)value, 0); break;
  14959. case 3: status_set_maxhp(bl, (unsigned int)value, 0); break;
  14960. case 4: status_set_sp(bl, (unsigned int)value, 0); break;
  14961. case 5: status_set_maxsp(bl, (unsigned int)value, 0); break;
  14962. case 6: hd->homunculus.char_id = (uint32)value; break;
  14963. case 7: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  14964. case 8: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  14965. case 9: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  14966. case 10: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  14967. case 11: hom_increase_intimacy(hd, (unsigned int)value); clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_INTIMATE, hd->homunculus.intimacy / 100); break;
  14968. case 12: hd->base_status.speed = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14969. case 13: unit_setdir(bl, (uint8)value); break;
  14970. case 14: hd->ud.canmove_tick = value > 0 ? 1 : 0; break;
  14971. case 15: hd->base_status.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14972. case 16: hd->base_status.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14973. case 17: hd->base_status.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14974. case 18: hd->base_status.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14975. case 19: hd->base_status.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14976. case 20: hd->base_status.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14977. case 21: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14978. default:
  14979. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  14980. return SCRIPT_CMD_FAILURE;
  14981. }
  14982. break;
  14983. case BL_PET:
  14984. if (!pd) {
  14985. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  14986. return SCRIPT_CMD_FAILURE;
  14987. }
  14988. switch (type) {
  14989. case 0: pd->status.size = (unsigned char)value; break;
  14990. case 1: pd->pet.level = (unsigned short)value; break;
  14991. case 2: status_set_hp(bl, (unsigned int)value, 0); break;
  14992. case 3: status_set_maxhp(bl, (unsigned int)value, 0); break;
  14993. case 4: pd->pet.account_id = (unsigned int)value; break;
  14994. case 5: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  14995. case 6: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  14996. case 7: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  14997. case 8: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  14998. case 9: pet_set_intimate(pd, (unsigned int)value); clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 1, pd->pet.intimate); break;
  14999. case 10: pd->status.speed = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15000. case 11: unit_setdir(bl, (uint8)value); break;
  15001. case 12: pd->ud.canmove_tick = value > 0 ? 1 : 0; break;
  15002. case 13: pd->status.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15003. case 14: pd->status.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15004. case 15: pd->status.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15005. case 16: pd->status.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15006. case 17: pd->status.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15007. case 18: pd->status.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15008. case 19: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15009. default:
  15010. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15011. return SCRIPT_CMD_FAILURE;
  15012. }
  15013. break;
  15014. case BL_MER:
  15015. if (!mc) {
  15016. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15017. return SCRIPT_CMD_FAILURE;
  15018. }
  15019. switch (type) {
  15020. case 0: mc->base_status.size = (unsigned char)value; break;
  15021. case 1: status_set_hp(bl, (unsigned int)value, 0); break;
  15022. case 2: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15023. case 3: mc->mercenary.char_id = (uint32)value; break;
  15024. case 4: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15025. case 5: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15026. case 6: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15027. case 7: mc->mercenary.kill_count = (unsigned int)value; break;
  15028. case 8: mc->mercenary.life_time = (unsigned int)value; break;
  15029. case 9: mc->base_status.speed = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15030. case 10: unit_setdir(bl, (uint8)value); break;
  15031. case 11: mc->ud.canmove_tick = value > 0 ? 1 : 0; break;
  15032. case 12: mc->base_status.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15033. case 13: mc->base_status.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15034. case 14: mc->base_status.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15035. case 15: mc->base_status.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15036. case 16: mc->base_status.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15037. case 17: mc->base_status.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15038. case 18: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15039. default:
  15040. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  15041. return SCRIPT_CMD_FAILURE;
  15042. }
  15043. break;
  15044. case BL_ELEM:
  15045. if (!ed) {
  15046. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  15047. return SCRIPT_CMD_FAILURE;
  15048. }
  15049. switch (type) {
  15050. case 0: ed->base_status.size = (unsigned char)value; break;
  15051. case 1: status_set_hp(bl, (unsigned int)value, 0); break;
  15052. case 2: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15053. case 3: status_set_sp(bl, (unsigned int)value, 0); break;
  15054. case 4: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15055. case 5: ed->elemental.char_id = (uint32)value; break;
  15056. case 6: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15057. case 7: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  15058. case 8: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  15059. case 9: ed->elemental.life_time = (unsigned int)value; break;
  15060. case 10: ed->elemental.mode = (unsigned int)value; status_calc_bl(bl, SCB_MODE); break;
  15061. case 11: ed->base_status.speed = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15062. case 12: unit_setdir(bl, (uint8)value); break;
  15063. case 13: ed->ud.canmove_tick = value > 0 ? 1 : 0; break;
  15064. case 14: ed->base_status.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15065. case 15: ed->base_status.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15066. case 16: ed->base_status.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15067. case 17: ed->base_status.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15068. case 18: ed->base_status.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15069. case 19: ed->base_status.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15070. case 20: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15071. default:
  15072. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  15073. return SCRIPT_CMD_FAILURE;
  15074. }
  15075. break;
  15076. case BL_NPC:
  15077. if (!nd) {
  15078. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  15079. return SCRIPT_CMD_FAILURE;
  15080. }
  15081. switch (type) {
  15082. case 0: status_set_viewdata(bl, (unsigned short)value); break;
  15083. case 1: nd->level = (unsigned int)value; break;
  15084. case 2: status_set_hp(bl, (unsigned int)value, 0); break;
  15085. case 3: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15086. case 4: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15087. case 5: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  15088. case 6: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  15089. case 7: unit_setdir(bl, (uint8)value); break;
  15090. case 8: nd->params.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15091. case 9: nd->params.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15092. case 10: nd->params.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15093. case 11: nd->params.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15094. case 12: nd->params.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15095. case 13: nd->params.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  15096. default:
  15097. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  15098. return SCRIPT_CMD_FAILURE;
  15099. }
  15100. break;
  15101. default:
  15102. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  15103. return SCRIPT_CMD_FAILURE;
  15104. }
  15105. return SCRIPT_CMD_SUCCESS;
  15106. }
  15107. /// Gets the name of a bl.
  15108. /// Supported types are [MOB|HOM|PET|NPC].
  15109. /// MER and ELEM don't support custom names.
  15110. ///
  15111. /// getunitname <unit id>;
  15112. BUILDIN_FUNC(getunitname)
  15113. {
  15114. struct block_list* bl = NULL;
  15115. bl = map_id2bl(script_getnum(st, 2));
  15116. if (!bl) {
  15117. ShowWarning("buildin_getunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15118. return SCRIPT_CMD_FAILURE;
  15119. }
  15120. script_pushstrcopy(st, status_get_name(bl));
  15121. return SCRIPT_CMD_SUCCESS;
  15122. }
  15123. /// Changes the name of a bl.
  15124. /// Supported types are [MOB|HOM|PET].
  15125. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  15126. ///
  15127. /// setunitname <unit id>,<name>;
  15128. BUILDIN_FUNC(setunitname)
  15129. {
  15130. struct block_list* bl = NULL;
  15131. TBL_MOB* md = NULL;
  15132. TBL_HOM* hd = NULL;
  15133. TBL_PET* pd = NULL;
  15134. bl = map_id2bl(script_getnum(st, 2));
  15135. if (!bl) {
  15136. ShowWarning("buildin_setunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15137. return SCRIPT_CMD_FAILURE;
  15138. }
  15139. switch (bl->type) {
  15140. case BL_MOB: md = map_id2md(bl->id); break;
  15141. case BL_HOM: hd = map_id2hd(bl->id); break;
  15142. case BL_PET: pd = map_id2pd(bl->id); break;
  15143. }
  15144. switch (bl->type) {
  15145. case BL_MOB:
  15146. if (!md) {
  15147. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  15148. return SCRIPT_CMD_FAILURE;
  15149. }
  15150. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  15151. break;
  15152. case BL_HOM:
  15153. if (!hd) {
  15154. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  15155. return SCRIPT_CMD_FAILURE;
  15156. }
  15157. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  15158. break;
  15159. case BL_PET:
  15160. if (!pd) {
  15161. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  15162. return SCRIPT_CMD_FAILURE;
  15163. }
  15164. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  15165. break;
  15166. default:
  15167. ShowWarning("buildin_setunitname: Unknown object type!\n");
  15168. return SCRIPT_CMD_FAILURE;
  15169. }
  15170. clif_charnameack(0, bl); // Send update to client.
  15171. return SCRIPT_CMD_SUCCESS;
  15172. }
  15173. /// Makes the unit walk to target position or map.
  15174. /// Returns if it was successful.
  15175. ///
  15176. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  15177. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  15178. BUILDIN_FUNC(unitwalk)
  15179. {
  15180. struct block_list* bl;
  15181. struct unit_data *ud = NULL;
  15182. const char *cmd = script_getfuncname(st), *done_label = "";
  15183. uint8 off = 5;
  15184. bl = map_id2bl(script_getnum(st,2));
  15185. if (!bl) {
  15186. ShowError("buildin_unitwalk: Invalid unit with ID '%d'.\n", script_getnum(st,2));
  15187. script_pushint(st, 0);
  15188. return SCRIPT_CMD_FAILURE;
  15189. }
  15190. ud = unit_bl2ud(bl);
  15191. if (!strcmp(cmd,"unitwalk")) {
  15192. int x = script_getnum(st,3);
  15193. int y = script_getnum(st,4);
  15194. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  15195. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  15196. } else {
  15197. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  15198. if (!tbl) {
  15199. ShowError("buildin_unitwalk: Bad target destination.\n");
  15200. script_pushint(st, 0);
  15201. return SCRIPT_CMD_FAILURE;
  15202. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  15203. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  15204. off = 4;
  15205. }
  15206. if (ud && script_hasdata(st, off)) {
  15207. done_label = script_getstr(st, off);
  15208. check_event(st, done_label);
  15209. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  15210. }
  15211. return SCRIPT_CMD_SUCCESS;
  15212. }
  15213. /// Kills the unit.
  15214. ///
  15215. /// unitkill <unit_id>;
  15216. BUILDIN_FUNC(unitkill)
  15217. {
  15218. struct block_list* bl = map_id2bl(script_getnum(st,2));
  15219. if (bl != NULL)
  15220. status_kill(bl);
  15221. return SCRIPT_CMD_SUCCESS;
  15222. }
  15223. /// Warps the unit to the target position in the target map.
  15224. /// Returns if it was successful.
  15225. ///
  15226. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  15227. BUILDIN_FUNC(unitwarp)
  15228. {
  15229. int unit_id;
  15230. int map_idx;
  15231. short x;
  15232. short y;
  15233. struct block_list* bl;
  15234. const char *mapname;
  15235. unit_id = script_getnum(st,2);
  15236. mapname = script_getstr(st, 3);
  15237. x = (short)script_getnum(st,4);
  15238. y = (short)script_getnum(st,5);
  15239. if (!unit_id) //Warp the script's runner
  15240. bl = map_id2bl(st->rid);
  15241. else
  15242. bl = map_id2bl(unit_id);
  15243. if (!strcmp(mapname,"this"))
  15244. map_idx = bl?bl->m:-1;
  15245. else
  15246. map_idx = map_mapname2mapid(mapname);
  15247. if (map_idx >= 0 && bl != NULL)
  15248. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15249. else
  15250. script_pushint(st, 0);
  15251. return SCRIPT_CMD_SUCCESS;
  15252. }
  15253. /// Makes the unit attack the target.
  15254. /// If the unit is a player and <action type> is not 0, it does a continuous
  15255. /// attack instead of a single attack.
  15256. /// Returns if the request was successful.
  15257. ///
  15258. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15259. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15260. BUILDIN_FUNC(unitattack)
  15261. {
  15262. struct block_list* unit_bl;
  15263. struct block_list* target_bl = NULL;
  15264. struct script_data* data;
  15265. int actiontype = 0;
  15266. unit_bl = map_id2bl(script_getnum(st,2));
  15267. if (!unit_bl) {
  15268. script_pushint(st, 0);
  15269. return SCRIPT_CMD_FAILURE;
  15270. }
  15271. data = script_getdata(st, 3);
  15272. get_val(st, data);
  15273. if (data_isstring(data)) {
  15274. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  15275. if( sd != NULL )
  15276. target_bl = &sd->bl;
  15277. } else
  15278. target_bl = map_id2bl(conv_num(st, data));
  15279. if (!target_bl) {
  15280. script_pushint(st, 0);
  15281. return SCRIPT_CMD_FAILURE;
  15282. }
  15283. if (script_hasdata(st,4))
  15284. actiontype = script_getnum(st,4);
  15285. switch(unit_bl->type) {
  15286. case BL_PC:
  15287. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15288. script_pushint(st, 1);
  15289. return SCRIPT_CMD_SUCCESS;
  15290. case BL_MOB:
  15291. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15292. break;
  15293. case BL_PET:
  15294. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15295. break;
  15296. default:
  15297. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  15298. script_pushint(st, 0);
  15299. return SCRIPT_CMD_FAILURE;
  15300. }
  15301. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  15302. return SCRIPT_CMD_SUCCESS;
  15303. }
  15304. /// Makes the unit stop attacking.
  15305. ///
  15306. /// unitstopattack <unit_id>;
  15307. BUILDIN_FUNC(unitstopattack)
  15308. {
  15309. int unit_id;
  15310. struct block_list* bl;
  15311. unit_id = script_getnum(st,2);
  15312. bl = map_id2bl(unit_id);
  15313. if (bl != NULL) {
  15314. unit_stop_attack(bl);
  15315. if (bl->type == BL_MOB)
  15316. ((TBL_MOB*)bl)->target_id = 0;
  15317. }
  15318. return SCRIPT_CMD_SUCCESS;
  15319. }
  15320. /// Makes the unit stop walking.
  15321. ///
  15322. /// unitstopwalk <unit_id>;
  15323. BUILDIN_FUNC(unitstopwalk)
  15324. {
  15325. int unit_id;
  15326. struct block_list* bl;
  15327. unit_id = script_getnum(st,2);
  15328. bl = map_id2bl(unit_id);
  15329. if (bl != NULL)
  15330. unit_stop_walking(bl, 0);
  15331. return SCRIPT_CMD_SUCCESS;
  15332. }
  15333. /// Makes the unit say the given message.
  15334. ///
  15335. /// unittalk <unit_id>,"<message>";
  15336. BUILDIN_FUNC(unittalk)
  15337. {
  15338. int unit_id;
  15339. const char* message;
  15340. struct block_list* bl;
  15341. unit_id = script_getnum(st,2);
  15342. message = script_getstr(st, 3);
  15343. bl = map_id2bl(unit_id);
  15344. if (bl != NULL) {
  15345. struct StringBuf sbuf;
  15346. StringBuf_Init(&sbuf);
  15347. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  15348. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  15349. StringBuf_Destroy(&sbuf);
  15350. }
  15351. return SCRIPT_CMD_SUCCESS;
  15352. }
  15353. /// Makes the unit do an emotion.
  15354. ///
  15355. /// unitemote <unit_id>,<emotion>;
  15356. ///
  15357. /// @see e_* in db/const.txt
  15358. BUILDIN_FUNC(unitemote)
  15359. {
  15360. int unit_id;
  15361. int emotion;
  15362. struct block_list* bl;
  15363. unit_id = script_getnum(st,2);
  15364. emotion = script_getnum(st,3);
  15365. bl = map_id2bl(unit_id);
  15366. if (bl != NULL)
  15367. clif_emotion(bl, emotion);
  15368. return SCRIPT_CMD_SUCCESS;
  15369. }
  15370. /// Makes the unit cast the skill on the target or self if no target is specified.
  15371. ///
  15372. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  15373. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  15374. BUILDIN_FUNC(unitskilluseid)
  15375. {
  15376. int unit_id, target_id, casttime;
  15377. uint16 skill_id, skill_lv;
  15378. struct block_list* bl;
  15379. struct script_data *data;
  15380. unit_id = script_getnum(st,2);
  15381. data = script_getdata(st, 3);
  15382. get_val(st, data); // Convert into value in case of a variable
  15383. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15384. skill_lv = script_getnum(st,4);
  15385. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  15386. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  15387. bl = map_id2bl(unit_id);
  15388. if (bl != NULL) {
  15389. if (bl->type == BL_NPC) {
  15390. if (!((TBL_NPC*)bl)->status.hp)
  15391. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15392. else
  15393. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15394. }
  15395. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  15396. }
  15397. return SCRIPT_CMD_SUCCESS;
  15398. }
  15399. /// Makes the unit cast the skill on the target position.
  15400. ///
  15401. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15402. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15403. BUILDIN_FUNC(unitskillusepos)
  15404. {
  15405. int unit_id, skill_x, skill_y, casttime;
  15406. uint16 skill_id, skill_lv;
  15407. struct block_list* bl;
  15408. struct script_data *data;
  15409. unit_id = script_getnum(st,2);
  15410. data = script_getdata(st, 3);
  15411. get_val(st, data); // Convert into value in case of a variable
  15412. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15413. skill_lv = script_getnum(st,4);
  15414. skill_x = script_getnum(st,5);
  15415. skill_y = script_getnum(st,6);
  15416. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  15417. bl = map_id2bl(unit_id);
  15418. if (bl != NULL) {
  15419. if (bl->type == BL_NPC) {
  15420. if (!((TBL_NPC*)bl)->status.hp)
  15421. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15422. else
  15423. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15424. }
  15425. 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));
  15426. }
  15427. return SCRIPT_CMD_SUCCESS;
  15428. }
  15429. // <--- [zBuffer] List of unit control commands
  15430. /// Pauses the execution of the script, detaching the player
  15431. ///
  15432. /// sleep <mili seconds>;
  15433. BUILDIN_FUNC(sleep)
  15434. {
  15435. int ticks;
  15436. ticks = script_getnum(st,2);
  15437. // detach the player
  15438. script_detach_rid(st);
  15439. if( ticks <= 0 )
  15440. {// do nothing
  15441. }
  15442. else if( st->sleep.tick == 0 )
  15443. {// sleep for the target amount of time
  15444. st->state = RERUNLINE;
  15445. st->sleep.tick = ticks;
  15446. }
  15447. else
  15448. {// sleep time is over
  15449. st->state = RUN;
  15450. st->sleep.tick = 0;
  15451. }
  15452. return SCRIPT_CMD_SUCCESS;
  15453. }
  15454. /// Pauses the execution of the script, keeping the player attached
  15455. /// Returns if a player is still attached
  15456. ///
  15457. /// sleep2(<mili secconds>) -> <bool>
  15458. BUILDIN_FUNC(sleep2)
  15459. {
  15460. int ticks;
  15461. ticks = script_getnum(st,2);
  15462. if( ticks <= 0 )
  15463. {// do nothing
  15464. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15465. }
  15466. else if( !st->sleep.tick )
  15467. {// sleep for the target amount of time
  15468. st->state = RERUNLINE;
  15469. st->sleep.tick = ticks;
  15470. }
  15471. else
  15472. {// sleep time is over
  15473. st->state = RUN;
  15474. st->sleep.tick = 0;
  15475. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15476. }
  15477. return SCRIPT_CMD_SUCCESS;
  15478. }
  15479. /// Awakes all the sleep timers of the target npc
  15480. ///
  15481. /// awake "<npc name>";
  15482. BUILDIN_FUNC(awake)
  15483. {
  15484. DBIterator *iter;
  15485. struct script_state *tst;
  15486. struct npc_data* nd;
  15487. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  15488. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  15489. return 1;
  15490. }
  15491. iter = db_iterator(st_db);
  15492. for (tst = dbi_first(iter); dbi_exists(iter); tst = dbi_next(iter)) {
  15493. if (tst->oid == nd->bl.id) {
  15494. TBL_PC* sd = map_id2sd(tst->rid);
  15495. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  15496. continue;
  15497. }
  15498. if ((sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd)) {
  15499. // char not online anymore / another char of the same account is online - Cancel execution
  15500. tst->state = END;
  15501. tst->rid = 0;
  15502. }
  15503. delete_timer(tst->sleep.timer, run_script_timer);
  15504. tst->sleep.timer = INVALID_TIMER;
  15505. if (tst->state != RERUNLINE)
  15506. tst->sleep.tick = 0;
  15507. run_script_main(tst);
  15508. }
  15509. }
  15510. dbi_destroy(iter);
  15511. return SCRIPT_CMD_SUCCESS;
  15512. }
  15513. /// Returns a reference to a variable of the target NPC.
  15514. /// Returns 0 if an error occurs.
  15515. ///
  15516. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  15517. BUILDIN_FUNC(getvariableofnpc)
  15518. {
  15519. struct script_data* data;
  15520. const char* name;
  15521. struct npc_data* nd;
  15522. data = script_getdata(st,2);
  15523. if( !data_isreference(data) )
  15524. {// Not a reference (aka varaible name)
  15525. ShowError("script:getvariableofnpc: not a variable\n");
  15526. script_reportdata(data);
  15527. script_pushnil(st);
  15528. st->state = END;
  15529. return 1;
  15530. }
  15531. name = reference_getname(data);
  15532. if( *name != '.' || name[1] == '@' )
  15533. {// not a npc variable
  15534. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  15535. script_reportdata(data);
  15536. script_pushnil(st);
  15537. st->state = END;
  15538. return 1;
  15539. }
  15540. nd = npc_name2id(script_getstr(st,3));
  15541. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  15542. {// NPC not found or has no script
  15543. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  15544. script_pushnil(st);
  15545. st->state = END;
  15546. return 1;
  15547. }
  15548. if (!nd->u.scr.script->local.vars)
  15549. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  15550. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  15551. return SCRIPT_CMD_SUCCESS;
  15552. }
  15553. /// Opens a warp portal.
  15554. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  15555. ///
  15556. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  15557. ///
  15558. /// @author blackhole89
  15559. BUILDIN_FUNC(warpportal)
  15560. {
  15561. int spx;
  15562. int spy;
  15563. unsigned short mapindex;
  15564. int tpx;
  15565. int tpy;
  15566. struct skill_unit_group* group;
  15567. struct block_list* bl;
  15568. bl = map_id2bl(st->oid);
  15569. if( bl == NULL ) {
  15570. ShowError("buildin_warpportal: NPC is needed\n");
  15571. return SCRIPT_CMD_FAILURE;
  15572. }
  15573. spx = script_getnum(st,2);
  15574. spy = script_getnum(st,3);
  15575. mapindex = mapindex_name2id(script_getstr(st, 4));
  15576. tpx = script_getnum(st,5);
  15577. tpy = script_getnum(st,6);
  15578. if( mapindex == 0 ) {
  15579. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  15580. return SCRIPT_CMD_FAILURE;
  15581. }
  15582. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  15583. if( group == NULL )
  15584. return SCRIPT_CMD_FAILURE;// failed
  15585. group->val1 = (group->val1<<16)|(short)0;
  15586. group->val2 = (tpx<<16) | tpy;
  15587. group->val3 = mapindex;
  15588. return SCRIPT_CMD_SUCCESS;
  15589. }
  15590. /**
  15591. * openmail({<char_id>});
  15592. **/
  15593. BUILDIN_FUNC(openmail)
  15594. {
  15595. TBL_PC* sd;
  15596. if (!script_charid2sd(2,sd))
  15597. return SCRIPT_CMD_FAILURE;
  15598. mail_openmail(sd);
  15599. return SCRIPT_CMD_SUCCESS;
  15600. }
  15601. /**
  15602. * openauction({<char_id>});
  15603. **/
  15604. BUILDIN_FUNC(openauction)
  15605. {
  15606. TBL_PC* sd;
  15607. if (!script_charid2sd(2,sd))
  15608. return SCRIPT_CMD_FAILURE;
  15609. if( !battle_config.feature_auction ) {
  15610. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 517));
  15611. return 0;
  15612. }
  15613. clif_Auction_openwindow(sd);
  15614. return SCRIPT_CMD_SUCCESS;
  15615. }
  15616. /// Retrieves the value of the specified flag of the specified cell.
  15617. ///
  15618. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  15619. ///
  15620. /// @see cell_chk* constants in const.txt for the types
  15621. BUILDIN_FUNC(checkcell)
  15622. {
  15623. int16 m = map_mapname2mapid(script_getstr(st,2));
  15624. int16 x = script_getnum(st,3);
  15625. int16 y = script_getnum(st,4);
  15626. cell_chk type = (cell_chk)script_getnum(st,5);
  15627. script_pushint(st, map_getcell(m, x, y, type));
  15628. return SCRIPT_CMD_SUCCESS;
  15629. }
  15630. /// Modifies flags of cells in the specified area.
  15631. ///
  15632. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  15633. ///
  15634. /// @see cell_* constants in const.txt for the types
  15635. BUILDIN_FUNC(setcell)
  15636. {
  15637. int16 m = map_mapname2mapid(script_getstr(st,2));
  15638. int16 x1 = script_getnum(st,3);
  15639. int16 y1 = script_getnum(st,4);
  15640. int16 x2 = script_getnum(st,5);
  15641. int16 y2 = script_getnum(st,6);
  15642. cell_t type = (cell_t)script_getnum(st,7);
  15643. bool flag = (bool)script_getnum(st,8);
  15644. int x,y;
  15645. if( x1 > x2 ) swap(x1,x2);
  15646. if( y1 > y2 ) swap(y1,y2);
  15647. for( y = y1; y <= y2; ++y )
  15648. for( x = x1; x <= x2; ++x )
  15649. map_setcell(m, x, y, type, flag);
  15650. return SCRIPT_CMD_SUCCESS;
  15651. }
  15652. /*==========================================
  15653. * Mercenary Commands
  15654. *------------------------------------------*/
  15655. BUILDIN_FUNC(mercenary_create)
  15656. {
  15657. struct map_session_data *sd;
  15658. int class_, contract_time;
  15659. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  15660. return 0;
  15661. class_ = script_getnum(st,2);
  15662. if( !mercenary_class(class_) )
  15663. return 0;
  15664. contract_time = script_getnum(st,3);
  15665. mercenary_create(sd, class_, contract_time);
  15666. return SCRIPT_CMD_SUCCESS;
  15667. }
  15668. BUILDIN_FUNC(mercenary_heal)
  15669. {
  15670. struct map_session_data *sd = script_rid2sd(st);
  15671. int hp, sp;
  15672. if( sd == NULL || sd->md == NULL )
  15673. return 0;
  15674. hp = script_getnum(st,2);
  15675. sp = script_getnum(st,3);
  15676. status_heal(&sd->md->bl, hp, sp, 0);
  15677. return SCRIPT_CMD_SUCCESS;
  15678. }
  15679. BUILDIN_FUNC(mercenary_sc_start)
  15680. {
  15681. struct map_session_data *sd = script_rid2sd(st);
  15682. enum sc_type type;
  15683. int tick, val1;
  15684. if( sd == NULL || sd->md == NULL )
  15685. return 0;
  15686. type = (sc_type)script_getnum(st,2);
  15687. tick = script_getnum(st,3);
  15688. val1 = script_getnum(st,4);
  15689. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  15690. return SCRIPT_CMD_SUCCESS;
  15691. }
  15692. BUILDIN_FUNC(mercenary_get_calls)
  15693. {
  15694. struct map_session_data *sd = script_rid2sd(st);
  15695. int guild;
  15696. if( sd == NULL )
  15697. return 0;
  15698. guild = script_getnum(st,2);
  15699. switch( guild )
  15700. {
  15701. case ARCH_MERC_GUILD:
  15702. script_pushint(st,sd->status.arch_calls);
  15703. break;
  15704. case SPEAR_MERC_GUILD:
  15705. script_pushint(st,sd->status.spear_calls);
  15706. break;
  15707. case SWORD_MERC_GUILD:
  15708. script_pushint(st,sd->status.sword_calls);
  15709. break;
  15710. default:
  15711. script_pushint(st,0);
  15712. break;
  15713. }
  15714. return SCRIPT_CMD_SUCCESS;
  15715. }
  15716. BUILDIN_FUNC(mercenary_set_calls)
  15717. {
  15718. struct map_session_data *sd = script_rid2sd(st);
  15719. int guild, value, *calls;
  15720. if( sd == NULL )
  15721. return 0;
  15722. guild = script_getnum(st,2);
  15723. value = script_getnum(st,3);
  15724. switch( guild )
  15725. {
  15726. case ARCH_MERC_GUILD:
  15727. calls = &sd->status.arch_calls;
  15728. break;
  15729. case SPEAR_MERC_GUILD:
  15730. calls = &sd->status.spear_calls;
  15731. break;
  15732. case SWORD_MERC_GUILD:
  15733. calls = &sd->status.sword_calls;
  15734. break;
  15735. default:
  15736. return 0; // Invalid Guild
  15737. }
  15738. *calls += value;
  15739. *calls = cap_value(*calls, 0, INT_MAX);
  15740. return SCRIPT_CMD_SUCCESS;
  15741. }
  15742. BUILDIN_FUNC(mercenary_get_faith)
  15743. {
  15744. struct map_session_data *sd = script_rid2sd(st);
  15745. int guild;
  15746. if( sd == NULL )
  15747. return 0;
  15748. guild = script_getnum(st,2);
  15749. switch( guild )
  15750. {
  15751. case ARCH_MERC_GUILD:
  15752. script_pushint(st,sd->status.arch_faith);
  15753. break;
  15754. case SPEAR_MERC_GUILD:
  15755. script_pushint(st,sd->status.spear_faith);
  15756. break;
  15757. case SWORD_MERC_GUILD:
  15758. script_pushint(st,sd->status.sword_faith);
  15759. break;
  15760. default:
  15761. script_pushint(st,0);
  15762. break;
  15763. }
  15764. return SCRIPT_CMD_SUCCESS;
  15765. }
  15766. BUILDIN_FUNC(mercenary_set_faith)
  15767. {
  15768. struct map_session_data *sd = script_rid2sd(st);
  15769. int guild, value, *calls;
  15770. if( sd == NULL )
  15771. return 0;
  15772. guild = script_getnum(st,2);
  15773. value = script_getnum(st,3);
  15774. switch( guild )
  15775. {
  15776. case ARCH_MERC_GUILD:
  15777. calls = &sd->status.arch_faith;
  15778. break;
  15779. case SPEAR_MERC_GUILD:
  15780. calls = &sd->status.spear_faith;
  15781. break;
  15782. case SWORD_MERC_GUILD:
  15783. calls = &sd->status.sword_faith;
  15784. break;
  15785. default:
  15786. return 0; // Invalid Guild
  15787. }
  15788. *calls += value;
  15789. *calls = cap_value(*calls, 0, INT_MAX);
  15790. if( mercenary_get_guild(sd->md) == guild )
  15791. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  15792. return SCRIPT_CMD_SUCCESS;
  15793. }
  15794. /*------------------------------------------
  15795. * Book Reading
  15796. *------------------------------------------*/
  15797. BUILDIN_FUNC(readbook)
  15798. {
  15799. struct map_session_data *sd;
  15800. int book_id, page;
  15801. if( (sd = script_rid2sd(st)) == NULL )
  15802. return 0;
  15803. book_id = script_getnum(st,2);
  15804. page = script_getnum(st,3);
  15805. clif_readbook(sd->fd, book_id, page);
  15806. return SCRIPT_CMD_SUCCESS;
  15807. }
  15808. /******************
  15809. Questlog script commands
  15810. *******************/
  15811. /**
  15812. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  15813. **/
  15814. BUILDIN_FUNC(questinfo)
  15815. {
  15816. TBL_NPC* nd = map_id2nd(st->oid);
  15817. int quest_id, icon;
  15818. struct questinfo qi;
  15819. if( nd == NULL || nd->bl.m == -1 ) {
  15820. ShowError("buildin_questinfo: No NPC attached.\n");
  15821. return SCRIPT_CMD_FAILURE;
  15822. }
  15823. quest_id = script_getnum(st, 2);
  15824. icon = script_getnum(st, 3);
  15825. #if PACKETVER >= 20120410
  15826. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  15827. icon = 9999; // Default to nothing if icon id is invalid.
  15828. #else
  15829. if(icon < 0 || icon > 7)
  15830. icon = 0;
  15831. else
  15832. icon = icon + 1;
  15833. #endif
  15834. qi.quest_id = quest_id;
  15835. qi.icon = (unsigned char)icon;
  15836. qi.nd = nd;
  15837. if( script_hasdata(st, 4) ) {
  15838. int color = script_getnum(st, 4);
  15839. if( color < 0 || color > 3 ) {
  15840. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  15841. script_reportfunc(st);
  15842. color = 0;
  15843. }
  15844. qi.color = (unsigned char)color;
  15845. }
  15846. qi.hasJob = false;
  15847. if(script_hasdata(st, 5)) {
  15848. int job = script_getnum(st, 5);
  15849. if (!pcdb_checkid(job))
  15850. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  15851. else {
  15852. qi.hasJob = true;
  15853. qi.job = (unsigned short)job;
  15854. }
  15855. }
  15856. map_add_questinfo(nd->bl.m,&qi);
  15857. return SCRIPT_CMD_SUCCESS;
  15858. }
  15859. /**
  15860. * setquest <ID>{,<char_id>};
  15861. **/
  15862. BUILDIN_FUNC(setquest)
  15863. {
  15864. struct map_session_data *sd;
  15865. unsigned short i;
  15866. int quest_id;
  15867. quest_id = script_getnum(st, 2);
  15868. if (!script_charid2sd(3,sd))
  15869. return SCRIPT_CMD_FAILURE;
  15870. quest_add(sd, quest_id);
  15871. // If questinfo is set, remove quest bubble once quest is set.
  15872. for(i = 0; i < map[sd->bl.m].qi_count; i++) {
  15873. struct questinfo *qi = &map[sd->bl.m].qi_data[i];
  15874. if( qi->quest_id == quest_id ) {
  15875. #if PACKETVER >= 20120410
  15876. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  15877. #else
  15878. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  15879. #endif
  15880. }
  15881. }
  15882. return SCRIPT_CMD_SUCCESS;
  15883. }
  15884. /**
  15885. * erasequest <ID>{,<char_id>};
  15886. **/
  15887. BUILDIN_FUNC(erasequest)
  15888. {
  15889. struct map_session_data *sd;
  15890. if (!script_charid2sd(3,sd))
  15891. return SCRIPT_CMD_FAILURE;
  15892. quest_delete(sd, script_getnum(st, 2));
  15893. return SCRIPT_CMD_SUCCESS;
  15894. }
  15895. /**
  15896. * completequest <ID>{,<char_id>};
  15897. **/
  15898. BUILDIN_FUNC(completequest)
  15899. {
  15900. struct map_session_data *sd;
  15901. if (!script_charid2sd(3,sd))
  15902. return SCRIPT_CMD_FAILURE;
  15903. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  15904. return SCRIPT_CMD_SUCCESS;
  15905. }
  15906. /**
  15907. * changequest <ID>,<ID2>{,<char_id>};
  15908. **/
  15909. BUILDIN_FUNC(changequest)
  15910. {
  15911. struct map_session_data *sd;
  15912. if (!script_charid2sd(4,sd))
  15913. return SCRIPT_CMD_FAILURE;
  15914. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  15915. return SCRIPT_CMD_SUCCESS;
  15916. }
  15917. /**
  15918. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  15919. **/
  15920. BUILDIN_FUNC(checkquest)
  15921. {
  15922. struct map_session_data *sd;
  15923. enum quest_check_type type = HAVEQUEST;
  15924. if( script_hasdata(st, 3) )
  15925. type = (enum quest_check_type)script_getnum(st, 3);
  15926. if (!script_charid2sd(4,sd))
  15927. return SCRIPT_CMD_FAILURE;
  15928. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  15929. return SCRIPT_CMD_SUCCESS;
  15930. }
  15931. /**
  15932. * isbegin_quest(<ID>{,<char_id>})
  15933. **/
  15934. BUILDIN_FUNC(isbegin_quest)
  15935. {
  15936. struct map_session_data *sd;
  15937. int i;
  15938. if (!script_charid2sd(3,sd))
  15939. return SCRIPT_CMD_FAILURE;
  15940. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  15941. script_pushint(st, i + (i < 1));
  15942. return SCRIPT_CMD_SUCCESS;
  15943. }
  15944. /**
  15945. * showevent <icon>{,<mark color>{,<char_id>}}
  15946. **/
  15947. BUILDIN_FUNC(showevent)
  15948. {
  15949. TBL_PC *sd;
  15950. struct npc_data *nd = map_id2nd(st->oid);
  15951. int icon, color = 0;
  15952. if (!script_charid2sd(4,sd))
  15953. return SCRIPT_CMD_FAILURE;
  15954. if( sd == NULL || nd == NULL )
  15955. return 0;
  15956. icon = script_getnum(st, 2);
  15957. if( script_hasdata(st, 3) ) {
  15958. color = script_getnum(st, 3);
  15959. if( color < 0 || color > 3 ) {
  15960. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  15961. script_reportfunc(st);
  15962. color = 0;
  15963. }
  15964. }
  15965. #if PACKETVER >= 20120410
  15966. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  15967. icon = 9999; // Default to nothing if icon id is invalid.
  15968. #else
  15969. if(icon < 0 || icon > 7)
  15970. icon = 0;
  15971. else
  15972. icon = icon + 1;
  15973. #endif
  15974. clif_quest_show_event(sd, &nd->bl, icon, color);
  15975. return SCRIPT_CMD_SUCCESS;
  15976. }
  15977. /*==========================================
  15978. * BattleGround System
  15979. *------------------------------------------*/
  15980. BUILDIN_FUNC(waitingroom2bg)
  15981. {
  15982. struct npc_data *nd;
  15983. struct chat_data *cd;
  15984. const char *map_name, *ev = "", *dev = "";
  15985. int x, y, mapindex = 0, bg_id, n;
  15986. unsigned char i;
  15987. if( script_hasdata(st,7) )
  15988. nd = npc_name2id(script_getstr(st,7));
  15989. else
  15990. nd = (struct npc_data *)map_id2bl(st->oid);
  15991. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  15992. {
  15993. script_pushint(st,0);
  15994. return 0;
  15995. }
  15996. map_name = script_getstr(st,2);
  15997. if( strcmp(map_name,"-") != 0 )
  15998. {
  15999. mapindex = mapindex_name2id(map_name);
  16000. if( mapindex == 0 )
  16001. { // Invalid Map
  16002. script_pushint(st,0);
  16003. return 0;
  16004. }
  16005. }
  16006. x = script_getnum(st,3);
  16007. y = script_getnum(st,4);
  16008. ev = script_getstr(st,5); // Logout Event
  16009. dev = script_getstr(st,6); // Die Event
  16010. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  16011. { // Creation failed
  16012. script_pushint(st,0);
  16013. return 0;
  16014. }
  16015. n = cd->users;
  16016. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  16017. {
  16018. struct map_session_data *sd;
  16019. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  16020. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  16021. else
  16022. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  16023. }
  16024. mapreg_setreg(add_str("$@arenamembersnum"), i);
  16025. script_pushint(st,bg_id);
  16026. return SCRIPT_CMD_SUCCESS;
  16027. }
  16028. BUILDIN_FUNC(waitingroom2bg_single)
  16029. {
  16030. const char* map_name;
  16031. struct npc_data *nd;
  16032. struct chat_data *cd;
  16033. struct map_session_data *sd;
  16034. int x, y, mapindex, bg_id;
  16035. bg_id = script_getnum(st,2);
  16036. map_name = script_getstr(st,3);
  16037. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16038. return 0; // Invalid Map
  16039. x = script_getnum(st,4);
  16040. y = script_getnum(st,5);
  16041. nd = npc_name2id(script_getstr(st,6));
  16042. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  16043. return 0;
  16044. if( (sd = cd->usersd[0]) == NULL )
  16045. return 0;
  16046. if( bg_team_join(bg_id, sd) )
  16047. {
  16048. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  16049. script_pushint(st,1);
  16050. }
  16051. else
  16052. script_pushint(st,0);
  16053. return SCRIPT_CMD_SUCCESS;
  16054. }
  16055. BUILDIN_FUNC(bg_team_setxy)
  16056. {
  16057. struct battleground_data *bg;
  16058. int bg_id;
  16059. bg_id = script_getnum(st,2);
  16060. if( (bg = bg_team_search(bg_id)) == NULL )
  16061. return 0;
  16062. bg->x = script_getnum(st,3);
  16063. bg->y = script_getnum(st,4);
  16064. return SCRIPT_CMD_SUCCESS;
  16065. }
  16066. BUILDIN_FUNC(bg_warp)
  16067. {
  16068. int x, y, mapindex, bg_id;
  16069. const char* map_name;
  16070. bg_id = script_getnum(st,2);
  16071. map_name = script_getstr(st,3);
  16072. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16073. return 0; // Invalid Map
  16074. x = script_getnum(st,4);
  16075. y = script_getnum(st,5);
  16076. bg_team_warp(bg_id, mapindex, x, y);
  16077. return SCRIPT_CMD_SUCCESS;
  16078. }
  16079. BUILDIN_FUNC(bg_monster)
  16080. {
  16081. int class_ = 0, x = 0, y = 0, bg_id = 0;
  16082. const char *str,*mapname, *evt="";
  16083. bg_id = script_getnum(st,2);
  16084. mapname = script_getstr(st,3);
  16085. x = script_getnum(st,4);
  16086. y = script_getnum(st,5);
  16087. str = script_getstr(st,6);
  16088. class_ = script_getnum(st,7);
  16089. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  16090. check_event(st, evt);
  16091. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  16092. return SCRIPT_CMD_SUCCESS;
  16093. }
  16094. BUILDIN_FUNC(bg_monster_set_team)
  16095. {
  16096. struct mob_data *md;
  16097. struct block_list *mbl;
  16098. int id = script_getnum(st,2),
  16099. bg_id = script_getnum(st,3);
  16100. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  16101. return 0;
  16102. md = (TBL_MOB *)mbl;
  16103. md->bg_id = bg_id;
  16104. mob_stop_attack(md);
  16105. mob_stop_walking(md, 0);
  16106. md->target_id = md->attacked_id = 0;
  16107. clif_charnameack(0, &md->bl);
  16108. return SCRIPT_CMD_SUCCESS;
  16109. }
  16110. BUILDIN_FUNC(bg_leave)
  16111. {
  16112. struct map_session_data *sd = script_rid2sd(st);
  16113. if( sd == NULL || !sd->bg_id )
  16114. return 0;
  16115. bg_team_leave(sd,0);
  16116. return SCRIPT_CMD_SUCCESS;
  16117. }
  16118. BUILDIN_FUNC(bg_destroy)
  16119. {
  16120. int bg_id = script_getnum(st,2);
  16121. bg_team_delete(bg_id);
  16122. return SCRIPT_CMD_SUCCESS;
  16123. }
  16124. BUILDIN_FUNC(bg_getareausers)
  16125. {
  16126. const char *str;
  16127. int16 m, x0, y0, x1, y1;
  16128. int bg_id;
  16129. int i = 0, c = 0;
  16130. struct battleground_data *bg = NULL;
  16131. bg_id = script_getnum(st,2);
  16132. str = script_getstr(st,3);
  16133. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  16134. {
  16135. script_pushint(st,0);
  16136. return 0;
  16137. }
  16138. x0 = script_getnum(st,4);
  16139. y0 = script_getnum(st,5);
  16140. x1 = script_getnum(st,6);
  16141. y1 = script_getnum(st,7);
  16142. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  16143. {
  16144. struct map_session_data *sd;
  16145. if( (sd = bg->members[i].sd) == NULL )
  16146. continue;
  16147. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  16148. continue;
  16149. c++;
  16150. }
  16151. script_pushint(st,c);
  16152. return SCRIPT_CMD_SUCCESS;
  16153. }
  16154. BUILDIN_FUNC(bg_updatescore)
  16155. {
  16156. const char *str;
  16157. int16 m;
  16158. str = script_getstr(st,2);
  16159. if( (m = map_mapname2mapid(str)) < 0 )
  16160. return 0;
  16161. map[m].bgscore_lion = script_getnum(st,3);
  16162. map[m].bgscore_eagle = script_getnum(st,4);
  16163. clif_bg_updatescore(m);
  16164. return SCRIPT_CMD_SUCCESS;
  16165. }
  16166. BUILDIN_FUNC(bg_get_data)
  16167. {
  16168. struct battleground_data *bg;
  16169. int bg_id = script_getnum(st,2),
  16170. type = script_getnum(st,3);
  16171. if( (bg = bg_team_search(bg_id)) == NULL )
  16172. {
  16173. script_pushint(st,0);
  16174. return 0;
  16175. }
  16176. switch( type )
  16177. {
  16178. case 0: script_pushint(st, bg->count); break;
  16179. default:
  16180. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  16181. break;
  16182. }
  16183. return SCRIPT_CMD_SUCCESS;
  16184. }
  16185. /*==========================================
  16186. * Instancing System
  16187. *------------------------------------------*/
  16188. //Returns an Instance ID
  16189. //Checks NPC first, then if player is attached we check party
  16190. int script_instancegetid(struct script_state* st)
  16191. {
  16192. short instance_id = 0;
  16193. struct npc_data *nd;
  16194. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  16195. instance_id = nd->instance_id;
  16196. else {
  16197. struct map_session_data *sd;
  16198. struct party_data *p;
  16199. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  16200. instance_id = p->instance_id;
  16201. }
  16202. return instance_id;
  16203. }
  16204. /*==========================================
  16205. * Creates the instance
  16206. * Returns Instance ID if created successfully
  16207. *------------------------------------------*/
  16208. BUILDIN_FUNC(instance_create)
  16209. {
  16210. struct map_session_data *sd;
  16211. if((sd = script_rid2sd(st)) == NULL)
  16212. return -1;
  16213. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  16214. return SCRIPT_CMD_SUCCESS;
  16215. }
  16216. /*==========================================
  16217. * Destroys an instance (unofficial)
  16218. * Officially instances are only destroyed by timeout
  16219. *
  16220. * instance_destroy {<instance_id>};
  16221. *------------------------------------------*/
  16222. BUILDIN_FUNC(instance_destroy)
  16223. {
  16224. short instance_id;
  16225. if( script_hasdata(st,2) )
  16226. instance_id = script_getnum(st,2);
  16227. else
  16228. instance_id = script_instancegetid(st);
  16229. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  16230. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  16231. return 0;
  16232. }
  16233. instance_destroy(instance_id);
  16234. return SCRIPT_CMD_SUCCESS;
  16235. }
  16236. /*==========================================
  16237. * Warps player to instance
  16238. * Results:
  16239. * 0: Success
  16240. * 1: Character not in party
  16241. * 2: Party doesn't have instance
  16242. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  16243. *------------------------------------------*/
  16244. BUILDIN_FUNC(instance_enter)
  16245. {
  16246. struct map_session_data *sd;
  16247. if((sd = script_rid2sd(st)) != NULL){
  16248. if (script_hasdata(st,3) && script_hasdata(st,4))
  16249. script_pushint(st,instance_enter_position(sd,script_getstr(st, 2),script_getnum(st, 3),script_getnum(st, 4)));
  16250. else
  16251. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  16252. }
  16253. else
  16254. return 1;
  16255. return SCRIPT_CMD_SUCCESS;
  16256. }
  16257. /*==========================================
  16258. * Returns the name of a duplicated NPC
  16259. *
  16260. * instance_npcname <npc_name>{,<instance_id>};
  16261. * <npc_name> is the full name of an NPC.
  16262. *------------------------------------------*/
  16263. BUILDIN_FUNC(instance_npcname)
  16264. {
  16265. const char *str;
  16266. short instance_id = 0;
  16267. struct npc_data *nd;
  16268. str = script_getstr(st,2);
  16269. if( script_hasdata(st,3) )
  16270. instance_id = script_getnum(st,3);
  16271. else
  16272. instance_id = script_instancegetid(st);
  16273. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  16274. static char npcname[NAME_LENGTH];
  16275. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  16276. script_pushconststr(st,npcname);
  16277. } else {
  16278. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  16279. st->state = END;
  16280. return 1;
  16281. }
  16282. return SCRIPT_CMD_SUCCESS;
  16283. }
  16284. /*==========================================
  16285. * Returns the name of a duplicated map
  16286. *
  16287. * instance_mapname <map_name>{,<instance_id>};
  16288. *------------------------------------------*/
  16289. BUILDIN_FUNC(instance_mapname)
  16290. {
  16291. const char *str;
  16292. int16 m;
  16293. short instance_id = 0;
  16294. str = script_getstr(st,2);
  16295. if( script_hasdata(st,3) )
  16296. instance_id = script_getnum(st,3);
  16297. else
  16298. instance_id = script_instancegetid(st);
  16299. // Check that instance mapname is a valid map
  16300. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  16301. script_pushconststr(st, "");
  16302. else
  16303. script_pushconststr(st, map[m].name);
  16304. return SCRIPT_CMD_SUCCESS;
  16305. }
  16306. /*==========================================
  16307. * Returns an Instance ID
  16308. *------------------------------------------*/
  16309. BUILDIN_FUNC(instance_id)
  16310. {
  16311. short instance_id;
  16312. instance_id = script_instancegetid(st);
  16313. if(!instance_id) {
  16314. //ShowError("script:instance_id: No instance attached to NPC or player");
  16315. script_pushint(st, 0);
  16316. return SCRIPT_CMD_SUCCESS;
  16317. }
  16318. script_pushint(st, instance_id);
  16319. return SCRIPT_CMD_SUCCESS;
  16320. }
  16321. /*==========================================
  16322. * Warps all players inside an instance
  16323. *
  16324. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  16325. *------------------------------------------*/
  16326. BUILDIN_FUNC(instance_warpall)
  16327. {
  16328. struct party_data *p;
  16329. int16 m, i;
  16330. short instance_id;
  16331. const char *mapn;
  16332. int x, y;
  16333. mapn = script_getstr(st,2);
  16334. x = script_getnum(st,3);
  16335. y = script_getnum(st,4);
  16336. if( script_hasdata(st,5) )
  16337. instance_id = script_getnum(st,5);
  16338. else
  16339. instance_id = script_instancegetid(st);
  16340. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  16341. return 0;
  16342. if( !(p = party_search(instance_data[instance_id].party_id)) )
  16343. return 0;
  16344. for( i = 0; i < MAX_PARTY; i++ ) {
  16345. struct map_session_data *pl_sd;
  16346. if( (pl_sd = p->data[i].sd) && map[pl_sd->bl.m].instance_id == instance_id ) pc_setpos(pl_sd,map_id2index(m),x,y,CLR_TELEPORT);
  16347. }
  16348. return SCRIPT_CMD_SUCCESS;
  16349. }
  16350. /*==========================================
  16351. * Broadcasts to all maps inside an instance
  16352. *
  16353. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  16354. * Using -1 for <instance id> will auto-detect the id.
  16355. *------------------------------------------*/
  16356. BUILDIN_FUNC(instance_announce) {
  16357. int instance_id = script_getnum(st,2);
  16358. const char *mes = script_getstr(st,3);
  16359. int flag = script_getnum(st,4);
  16360. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  16361. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  16362. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  16363. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  16364. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  16365. int i;
  16366. if( instance_id == -1 ) {
  16367. instance_id = script_instancegetid(st);
  16368. }
  16369. if( !instance_id && &instance_data[instance_id] != NULL)
  16370. return true;
  16371. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  16372. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  16373. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  16374. return SCRIPT_CMD_SUCCESS;
  16375. }
  16376. /*==========================================
  16377. * instance_check_party [malufett]
  16378. * Values:
  16379. * party_id : Party ID of the invoking character. [Required Parameter]
  16380. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  16381. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16382. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16383. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  16384. * Example 2: instance_check_party (getcharid(1),1,1,99);
  16385. *------------------------------------------*/
  16386. BUILDIN_FUNC(instance_check_party)
  16387. {
  16388. int amount, min, max, i, party_id, c = 0;
  16389. struct party_data *p = NULL;
  16390. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  16391. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16392. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16393. if( min < 1 || min > MAX_LEVEL) {
  16394. ShowError("script:check_party: Invalid min level, %d\n", min);
  16395. return 1;
  16396. } else if( max < 1 || max > MAX_LEVEL) {
  16397. ShowError("script:check_party: Invalid max level, %d\n", max);
  16398. return 1;
  16399. }
  16400. if( script_hasdata(st,2) )
  16401. party_id = script_getnum(st,2);
  16402. else return 0;
  16403. if( !(p = party_search(party_id)) ) {
  16404. script_pushint(st, 0); // Returns false if party does not exist.
  16405. return 0;
  16406. }
  16407. for( i = 0; i < MAX_PARTY; i++ ) {
  16408. struct map_session_data *pl_sd;
  16409. if( (pl_sd = p->data[i].sd) )
  16410. if(map_id2bl(pl_sd->bl.id)) {
  16411. if(pl_sd->status.base_level < min) {
  16412. script_pushint(st, 0);
  16413. return 0;
  16414. } else if(pl_sd->status.base_level > max) {
  16415. script_pushint(st, 0);
  16416. return 0;
  16417. }
  16418. c++;
  16419. }
  16420. }
  16421. if(c < amount)
  16422. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  16423. else
  16424. script_pushint(st, 1);
  16425. return SCRIPT_CMD_SUCCESS;
  16426. }
  16427. /*==========================================
  16428. * Custom Fonts
  16429. *------------------------------------------*/
  16430. BUILDIN_FUNC(setfont)
  16431. {
  16432. struct map_session_data *sd = script_rid2sd(st);
  16433. int font = script_getnum(st,2);
  16434. if( sd == NULL )
  16435. return 0;
  16436. if( sd->status.font != font )
  16437. sd->status.font = font;
  16438. else
  16439. sd->status.font = 0;
  16440. clif_font(sd);
  16441. return SCRIPT_CMD_SUCCESS;
  16442. }
  16443. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  16444. {
  16445. TBL_MOB* md = (TBL_MOB*)bl;
  16446. struct block_list *tbl;
  16447. int mobid = va_arg(ap,int);
  16448. uint16 skill_id = va_arg(ap,int);
  16449. uint16 skill_lv = va_arg(ap,int);
  16450. int casttime = va_arg(ap,int);
  16451. int cancel = va_arg(ap,int);
  16452. int emotion = va_arg(ap,int);
  16453. int target = va_arg(ap,int);
  16454. if( md->mob_id != mobid )
  16455. return 0;
  16456. // 0:self, 1:target, 2:master, default:random
  16457. switch( target ) {
  16458. case 0: tbl = map_id2bl(md->bl.id); break;
  16459. case 1: tbl = map_id2bl(md->target_id); break;
  16460. case 2: tbl = map_id2bl(md->master_id); break;
  16461. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  16462. }
  16463. if( !tbl )
  16464. return 0;
  16465. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  16466. unit_skillcastcancel(bl,0);
  16467. if( skill_get_casttype(skill_id) == CAST_GROUND )
  16468. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  16469. else
  16470. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  16471. clif_emotion(&md->bl, emotion);
  16472. return SCRIPT_CMD_SUCCESS;
  16473. }
  16474. /*==========================================
  16475. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  16476. *------------------------------------------*/
  16477. BUILDIN_FUNC(areamobuseskill)
  16478. {
  16479. struct block_list center;
  16480. struct script_data *data;
  16481. int16 m;
  16482. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  16483. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  16484. ShowError("areamobuseskill: invalid map name.\n");
  16485. return 0;
  16486. }
  16487. center.m = m;
  16488. center.x = script_getnum(st,3);
  16489. center.y = script_getnum(st,4);
  16490. range = script_getnum(st,5);
  16491. mobid = script_getnum(st,6);
  16492. data = script_getdata(st, 7);
  16493. get_val(st, data); // Convert into value in case of a variable
  16494. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  16495. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  16496. skill_lv = battle_config.mob_max_skilllvl;
  16497. casttime = script_getnum(st,9);
  16498. cancel = script_getnum(st,10);
  16499. emotion = script_getnum(st,11);
  16500. target = script_getnum(st,12);
  16501. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  16502. return SCRIPT_CMD_SUCCESS;
  16503. }
  16504. BUILDIN_FUNC(progressbar)
  16505. {
  16506. struct map_session_data * sd = script_rid2sd(st);
  16507. const char * color;
  16508. unsigned int second;
  16509. if( !st || !sd )
  16510. return 0;
  16511. st->state = STOP;
  16512. color = script_getstr(st,2);
  16513. second = script_getnum(st,3);
  16514. sd->progressbar.npc_id = st->oid;
  16515. sd->progressbar.timeout = gettick() + second*1000;
  16516. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  16517. return SCRIPT_CMD_SUCCESS;
  16518. }
  16519. BUILDIN_FUNC(pushpc)
  16520. {
  16521. uint8 dir;
  16522. int cells, dx, dy;
  16523. struct map_session_data* sd;
  16524. if((sd = script_rid2sd(st))==NULL)
  16525. {
  16526. return 0;
  16527. }
  16528. dir = script_getnum(st,2);
  16529. cells = script_getnum(st,3);
  16530. if(dir>7)
  16531. {
  16532. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  16533. script_reportsrc(st);
  16534. dir%= 8; // trim spin-over
  16535. }
  16536. if(!cells)
  16537. {// zero distance
  16538. return 0;
  16539. }
  16540. else if(cells<0)
  16541. {// pushing backwards
  16542. dir = (dir+4)%8; // turn around
  16543. cells = -cells;
  16544. }
  16545. dx = dirx[dir];
  16546. dy = diry[dir];
  16547. unit_blown(&sd->bl, dx, dy, cells, 0);
  16548. return SCRIPT_CMD_SUCCESS;
  16549. }
  16550. /// Invokes buying store preparation window
  16551. /// buyingstore <slots>;
  16552. BUILDIN_FUNC(buyingstore)
  16553. {
  16554. struct map_session_data* sd;
  16555. if( ( sd = script_rid2sd(st) ) == NULL )
  16556. {
  16557. return 0;
  16558. }
  16559. if( npc_isnear(&sd->bl) ) {
  16560. char output[150];
  16561. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  16562. clif_displaymessage(sd->fd, output);
  16563. return 0;
  16564. }
  16565. buyingstore_setup(sd, script_getnum(st,2));
  16566. return SCRIPT_CMD_SUCCESS;
  16567. }
  16568. /// Invokes search store info window
  16569. /// searchstores <uses>,<effect>;
  16570. BUILDIN_FUNC(searchstores)
  16571. {
  16572. unsigned short effect;
  16573. unsigned int uses;
  16574. struct map_session_data* sd;
  16575. if( ( sd = script_rid2sd(st) ) == NULL )
  16576. {
  16577. return 0;
  16578. }
  16579. uses = script_getnum(st,2);
  16580. effect = script_getnum(st,3);
  16581. if( !uses )
  16582. {
  16583. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  16584. return 1;
  16585. }
  16586. if( effect > 1 )
  16587. {
  16588. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  16589. return 1;
  16590. }
  16591. searchstore_open(sd, uses, effect);
  16592. return SCRIPT_CMD_SUCCESS;
  16593. }
  16594. /// Displays a number as large digital clock.
  16595. /// showdigit <value>[,<type>];
  16596. BUILDIN_FUNC(showdigit)
  16597. {
  16598. unsigned int type = 0;
  16599. int value;
  16600. struct map_session_data* sd;
  16601. if( ( sd = script_rid2sd(st) ) == NULL )
  16602. {
  16603. return 0;
  16604. }
  16605. value = script_getnum(st,2);
  16606. if( script_hasdata(st,3) )
  16607. {
  16608. type = script_getnum(st,3);
  16609. if( type > 3 )
  16610. {
  16611. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  16612. return 1;
  16613. }
  16614. }
  16615. clif_showdigit(sd, (unsigned char)type, value);
  16616. return SCRIPT_CMD_SUCCESS;
  16617. }
  16618. /**
  16619. * Rune Knight
  16620. * makerune <% success bonus>{,<char_id>};
  16621. **/
  16622. BUILDIN_FUNC(makerune) {
  16623. TBL_PC* sd;
  16624. if (!script_charid2sd(3,sd))
  16625. return SCRIPT_CMD_FAILURE;
  16626. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  16627. sd->itemid = script_getnum(st,2);
  16628. return SCRIPT_CMD_SUCCESS;
  16629. }
  16630. /**
  16631. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  16632. **/
  16633. BUILDIN_FUNC(checkdragon) {
  16634. TBL_PC* sd;
  16635. if (!script_charid2sd(2,sd))
  16636. return SCRIPT_CMD_FAILURE;
  16637. if( pc_isridingdragon(sd) )
  16638. script_pushint(st,1);
  16639. else
  16640. script_pushint(st,0);
  16641. return SCRIPT_CMD_SUCCESS;
  16642. }
  16643. /**
  16644. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  16645. * - Toggles the dragon on a RK if he can mount;
  16646. * @param Color - when not provided uses the green dragon;
  16647. * - 1 : Green Dragon
  16648. * - 2 : Brown Dragon
  16649. * - 3 : Gray Dragon
  16650. * - 4 : Blue Dragon
  16651. * - 5 : Red Dragon
  16652. **/
  16653. BUILDIN_FUNC(setdragon) {
  16654. TBL_PC* sd;
  16655. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  16656. if (!script_charid2sd(3,sd))
  16657. return SCRIPT_CMD_FAILURE;
  16658. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  16659. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  16660. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  16661. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  16662. script_pushint(st,1);
  16663. } else {//Not mounted; Mount now.
  16664. unsigned int option = OPTION_DRAGON1;
  16665. if( color ) {
  16666. option = ( color == 1 ? OPTION_DRAGON1 :
  16667. color == 2 ? OPTION_DRAGON2 :
  16668. color == 3 ? OPTION_DRAGON3 :
  16669. color == 4 ? OPTION_DRAGON4 :
  16670. color == 5 ? OPTION_DRAGON5 : 0);
  16671. if( !option ) {
  16672. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  16673. option = OPTION_DRAGON1;
  16674. }
  16675. }
  16676. pc_setoption(sd, sd->sc.option|option);
  16677. script_pushint(st,1);
  16678. }
  16679. return SCRIPT_CMD_SUCCESS;
  16680. }
  16681. /**
  16682. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  16683. **/
  16684. BUILDIN_FUNC(ismounting) {
  16685. TBL_PC* sd;
  16686. if (!script_charid2sd(2,sd))
  16687. return SCRIPT_CMD_FAILURE;
  16688. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  16689. script_pushint(st,1);
  16690. else
  16691. script_pushint(st,0);
  16692. return SCRIPT_CMD_SUCCESS;
  16693. }
  16694. /**
  16695. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  16696. * - Toggles new mounts on a player when he can mount
  16697. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  16698. * - Will unmount the player is he is already mounting
  16699. **/
  16700. BUILDIN_FUNC(setmounting) {
  16701. TBL_PC* sd;
  16702. if (!script_charid2sd(2,sd))
  16703. return SCRIPT_CMD_FAILURE;
  16704. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  16705. clif_msg(sd, NEED_REINS_OF_MOUNT);
  16706. script_pushint(st,0); //can't mount with one of these
  16707. } else {
  16708. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  16709. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  16710. else
  16711. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  16712. script_pushint(st,1);//in both cases, return 1.
  16713. }
  16714. return SCRIPT_CMD_SUCCESS;
  16715. }
  16716. /**
  16717. * Retrieves quantity of arguments provided to callfunc/callsub.
  16718. * getargcount() -> amount of arguments received in a function
  16719. **/
  16720. BUILDIN_FUNC(getargcount) {
  16721. struct script_retinfo* ri;
  16722. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  16723. ShowError("script:getargcount: used out of function or callsub label!\n");
  16724. st->state = END;
  16725. return 1;
  16726. }
  16727. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  16728. script_pushint(st, ri->nargs);
  16729. return SCRIPT_CMD_SUCCESS;
  16730. }
  16731. /**
  16732. * getcharip(<account ID>/<character ID>/<character name>)
  16733. **/
  16734. BUILDIN_FUNC(getcharip)
  16735. {
  16736. struct map_session_data* sd = NULL;
  16737. /* check if a character name is specified */
  16738. if( script_hasdata(st, 2) )
  16739. {
  16740. struct script_data *data;
  16741. data = script_getdata(st, 2);
  16742. get_val(st, data); // Convert into value in case of a variable
  16743. if (data_isstring(data))
  16744. sd = map_nick2sd(script_getstr(st, 2));
  16745. else if (data_isint(data) || script_getnum(st, 2))
  16746. {
  16747. int id = 0;
  16748. id = script_getnum(st, 2);
  16749. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  16750. }
  16751. }
  16752. else
  16753. sd = script_rid2sd(st);
  16754. /* check for sd and IP */
  16755. if (!sd || !session[sd->fd]->client_addr)
  16756. {
  16757. script_pushconststr(st, "");
  16758. return 0;
  16759. }
  16760. /* return the client ip_addr converted for output */
  16761. if (sd && sd->fd && session[sd->fd])
  16762. {
  16763. /* initiliaze */
  16764. const char *ip_addr = NULL;
  16765. uint32 ip;
  16766. /* set ip, ip_addr and convert to ip and push str */
  16767. ip = session[sd->fd]->client_addr;
  16768. ip_addr = ip2str(ip, NULL);
  16769. script_pushstrcopy(st, ip_addr);
  16770. }
  16771. return SCRIPT_CMD_SUCCESS;
  16772. }
  16773. /**
  16774. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  16775. **/
  16776. BUILDIN_FUNC(is_function) {
  16777. const char* str = script_getstr(st,2);
  16778. if( strdb_exists(userfunc_db, str) )
  16779. script_pushint(st,1);
  16780. else
  16781. script_pushint(st,0);
  16782. return SCRIPT_CMD_SUCCESS;
  16783. }
  16784. /**
  16785. * get_revision() -> retrieves the current svn revision (if available)
  16786. **/
  16787. BUILDIN_FUNC(get_revision) {
  16788. const char *svn = get_svn_revision();
  16789. if ( svn[0] != UNKNOWN_VERSION )
  16790. script_pushint(st,atoi(svn));
  16791. else
  16792. script_pushint(st,-1); //unknown
  16793. return SCRIPT_CMD_SUCCESS;
  16794. }
  16795. /* get_hash() -> retrieves the current git hash (if available)*/
  16796. BUILDIN_FUNC(get_githash) {
  16797. const char* git = get_git_hash();
  16798. char buf[CHAT_SIZE_MAX];
  16799. safestrncpy(buf,git,strlen(git)+1);
  16800. if ( git[0] != UNKNOWN_VERSION )
  16801. script_pushstr(st,buf);
  16802. else
  16803. script_pushstr(st,"Unknown"); //unknown
  16804. return SCRIPT_CMD_SUCCESS;
  16805. }
  16806. /**
  16807. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  16808. **/
  16809. BUILDIN_FUNC(freeloop) {
  16810. if( script_hasdata(st,2) ) {
  16811. if( script_getnum(st,2) )
  16812. st->freeloop = 1;
  16813. else
  16814. st->freeloop = 0;
  16815. }
  16816. script_pushint(st, st->freeloop);
  16817. return SCRIPT_CMD_SUCCESS;
  16818. }
  16819. /**
  16820. * @commands (script based)
  16821. **/
  16822. BUILDIN_FUNC(bindatcmd) {
  16823. const char* atcmd;
  16824. const char* eventName;
  16825. int i, level = 0, level2 = 0;
  16826. bool create = false;
  16827. atcmd = script_getstr(st,2);
  16828. eventName = script_getstr(st,3);
  16829. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  16830. atcmd++;
  16831. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  16832. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  16833. if( atcmd_binding_count == 0 ) {
  16834. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  16835. create = true;
  16836. } else {
  16837. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  16838. if( i < atcmd_binding_count ) {/* update existent entry */
  16839. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  16840. atcmd_binding[i]->level = level;
  16841. atcmd_binding[i]->level2 = level2;
  16842. } else
  16843. create = true;
  16844. }
  16845. if( create ) {
  16846. i = atcmd_binding_count;
  16847. if( atcmd_binding_count++ != 0 )
  16848. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  16849. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  16850. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  16851. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  16852. atcmd_binding[i]->level = level;
  16853. atcmd_binding[i]->level2 = level2;
  16854. }
  16855. return SCRIPT_CMD_SUCCESS;
  16856. }
  16857. BUILDIN_FUNC(unbindatcmd) {
  16858. const char* atcmd;
  16859. int i = 0;
  16860. atcmd = script_getstr(st, 2);
  16861. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  16862. atcmd++;
  16863. if( atcmd_binding_count == 0 ) {
  16864. script_pushint(st, 0);
  16865. return 0;
  16866. }
  16867. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  16868. if( i < atcmd_binding_count ) {
  16869. int cursor = 0;
  16870. aFree(atcmd_binding[i]);
  16871. atcmd_binding[i] = NULL;
  16872. /* compact the list now that we freed a slot somewhere */
  16873. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  16874. if( atcmd_binding[i] == NULL )
  16875. continue;
  16876. if( cursor != i ) {
  16877. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  16878. }
  16879. cursor++;
  16880. }
  16881. if( (atcmd_binding_count = cursor) == 0 )
  16882. aFree(atcmd_binding);
  16883. script_pushint(st, 1);
  16884. } else
  16885. script_pushint(st, 0);/* not found */
  16886. return SCRIPT_CMD_SUCCESS;
  16887. }
  16888. BUILDIN_FUNC(useatcmd) {
  16889. return atcommand_sub(st,3);
  16890. }
  16891. BUILDIN_FUNC(checkre)
  16892. {
  16893. int num;
  16894. num=script_getnum(st,2);
  16895. switch(num){
  16896. case 0:
  16897. #ifdef RENEWAL
  16898. script_pushint(st, 1);
  16899. #else
  16900. script_pushint(st, 0);
  16901. #endif
  16902. break;
  16903. case 1:
  16904. #ifdef RENEWAL_CAST
  16905. script_pushint(st, 1);
  16906. #else
  16907. script_pushint(st, 0);
  16908. #endif
  16909. break;
  16910. case 2:
  16911. #ifdef RENEWAL_DROP
  16912. script_pushint(st, 1);
  16913. #else
  16914. script_pushint(st, 0);
  16915. #endif
  16916. break;
  16917. case 3:
  16918. #ifdef RENEWAL_EXP
  16919. script_pushint(st, 1);
  16920. #else
  16921. script_pushint(st, 0);
  16922. #endif
  16923. break;
  16924. case 4:
  16925. #ifdef RENEWAL_LVDMG
  16926. script_pushint(st, 1);
  16927. #else
  16928. script_pushint(st, 0);
  16929. #endif
  16930. break;
  16931. case 5:
  16932. #ifdef RENEWAL_ASPD
  16933. script_pushint(st, 1);
  16934. #else
  16935. script_pushint(st, 0);
  16936. #endif
  16937. break;
  16938. default:
  16939. ShowWarning("buildin_checkre: unknown parameter.\n");
  16940. break;
  16941. }
  16942. return SCRIPT_CMD_SUCCESS;
  16943. }
  16944. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>}} */
  16945. BUILDIN_FUNC(getrandgroupitem) {
  16946. TBL_PC* sd;
  16947. int i, get_count = 0;
  16948. uint16 group = script_getnum(st,2), qty = 0;
  16949. uint8 sub_group = 1;
  16950. struct item item_tmp;
  16951. struct s_item_group_entry *entry = NULL;
  16952. if (!(sd = script_rid2sd(st)))
  16953. return SCRIPT_CMD_SUCCESS;
  16954. if (!group) {
  16955. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  16956. return SCRIPT_CMD_FAILURE;
  16957. }
  16958. FETCH(3, qty);
  16959. FETCH(4, sub_group);
  16960. entry = itemdb_get_randgroupitem(group,sub_group);
  16961. if (!entry)
  16962. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  16963. memset(&item_tmp,0,sizeof(item_tmp));
  16964. item_tmp.nameid = entry->nameid;
  16965. item_tmp.identify = itemdb_isidentified(entry->nameid);
  16966. if (!qty)
  16967. qty = entry->amount;
  16968. //Check if it's stackable.
  16969. if (!itemdb_isstackable(entry->nameid)) {
  16970. item_tmp.amount = 1;
  16971. get_count = qty;
  16972. }
  16973. else {
  16974. item_tmp.amount = qty;
  16975. get_count = 1;
  16976. }
  16977. for (i = 0; i < get_count; i++) {
  16978. // if not pet egg
  16979. if (!pet_create_egg(sd, entry->nameid)) {
  16980. unsigned char flag = 0;
  16981. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  16982. clif_additem(sd,0,0,flag);
  16983. if (pc_candrop(sd,&item_tmp))
  16984. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  16985. }
  16986. }
  16987. }
  16988. return SCRIPT_CMD_SUCCESS;
  16989. }
  16990. /* getgroupitem <group_id>;
  16991. * Gives item(s) to the attached player based on item group contents
  16992. */
  16993. BUILDIN_FUNC(getgroupitem) {
  16994. TBL_PC *sd;
  16995. int group_id = script_getnum(st,2);
  16996. if (!(sd = script_rid2sd(st)))
  16997. return SCRIPT_CMD_SUCCESS;
  16998. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  16999. ShowError("getgroupitem: Invalid group id '%d' specified.\n",group_id);
  17000. return SCRIPT_CMD_FAILURE;
  17001. }
  17002. return SCRIPT_CMD_SUCCESS;
  17003. }
  17004. /* cleanmap <map_name>;
  17005. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  17006. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  17007. {
  17008. nullpo_ret(bl);
  17009. map_clearflooritem(bl);
  17010. return SCRIPT_CMD_SUCCESS;
  17011. }
  17012. BUILDIN_FUNC(cleanmap)
  17013. {
  17014. const char *mapname;
  17015. int16 m;
  17016. int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  17017. mapname = script_getstr(st, 2);
  17018. m = map_mapname2mapid(mapname);
  17019. if (!m)
  17020. return 1;
  17021. if ((script_lastdata(st) - 2) < 4) {
  17022. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  17023. } else {
  17024. x0 = script_getnum(st, 3);
  17025. y0 = script_getnum(st, 4);
  17026. x1 = script_getnum(st, 5);
  17027. y1 = script_getnum(st, 6);
  17028. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  17029. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  17030. } else {
  17031. ShowError("cleanarea: invalid coordinate defined!\n");
  17032. return 1;
  17033. }
  17034. }
  17035. return SCRIPT_CMD_SUCCESS;
  17036. }
  17037. /* Cast a skill on the attached player.
  17038. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  17039. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  17040. BUILDIN_FUNC(npcskill)
  17041. {
  17042. uint16 skill_id;
  17043. unsigned short skill_level;
  17044. unsigned int stat_point;
  17045. unsigned int npc_level;
  17046. struct npc_data *nd;
  17047. struct map_session_data *sd;
  17048. struct script_data *data;
  17049. data = script_getdata(st, 2);
  17050. get_val(st, data); // Convert into value in case of a variable
  17051. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  17052. skill_level = script_getnum(st, 3);
  17053. stat_point = script_getnum(st, 4);
  17054. npc_level = script_getnum(st, 5);
  17055. sd = script_rid2sd(st);
  17056. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  17057. if (stat_point > battle_config.max_third_parameter) {
  17058. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  17059. return 1;
  17060. }
  17061. if (npc_level > MAX_LEVEL) {
  17062. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  17063. return 1;
  17064. }
  17065. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  17066. return 1;
  17067. }
  17068. nd->level = npc_level;
  17069. nd->stat_point = stat_point;
  17070. if (!nd->status.hp)
  17071. status_calc_npc(nd, SCO_FIRST);
  17072. else
  17073. status_calc_npc(nd, SCO_NONE);
  17074. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  17075. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  17076. else
  17077. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  17078. return SCRIPT_CMD_SUCCESS;
  17079. }
  17080. /* Consumes an item.
  17081. * consumeitem <item id>{,<char_id>};
  17082. * consumeitem "<item name>"{,<char_id>};
  17083. * @param item: Item ID or name
  17084. */
  17085. BUILDIN_FUNC(consumeitem)
  17086. {
  17087. TBL_PC *sd;
  17088. struct script_data *data;
  17089. struct item_data *item_data;
  17090. if (!script_charid2sd(3, sd))
  17091. return SCRIPT_CMD_FAILURE;
  17092. data = script_getdata( st, 2 );
  17093. get_val( st, data );
  17094. if( data_isstring( data ) ){
  17095. const char *name = conv_str( st, data );
  17096. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  17097. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  17098. return SCRIPT_CMD_FAILURE;
  17099. }
  17100. }else if( data_isint( data ) ){
  17101. unsigned short nameid = conv_num( st, data );
  17102. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  17103. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  17104. return SCRIPT_CMD_FAILURE;
  17105. }
  17106. }else{
  17107. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  17108. return SCRIPT_CMD_FAILURE;
  17109. }
  17110. run_script( item_data->script, 0, sd->bl.id, 0 );
  17111. return SCRIPT_CMD_SUCCESS;
  17112. }
  17113. /** Makes a player sit/stand.
  17114. * sit {"<character name>"};
  17115. * stand {"<character name>"};
  17116. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  17117. * @param name: Player name that will be invoked
  17118. */
  17119. BUILDIN_FUNC(sit)
  17120. {
  17121. TBL_PC *sd;
  17122. if( script_hasdata(st, 2) )
  17123. sd = map_nick2sd(script_getstr(st, 2));
  17124. else
  17125. sd = script_rid2sd(st);
  17126. if( sd == NULL)
  17127. return SCRIPT_CMD_FAILURE;
  17128. if( !pc_issit(sd) ) {
  17129. pc_setsit(sd);
  17130. skill_sit(sd, 1);
  17131. clif_sitting(&sd->bl);
  17132. }
  17133. return SCRIPT_CMD_SUCCESS;
  17134. }
  17135. /** Makes player to stand
  17136. * @param name: Player name that will be set
  17137. */
  17138. BUILDIN_FUNC(stand)
  17139. {
  17140. TBL_PC *sd;
  17141. if( script_hasdata(st, 2) )
  17142. sd = map_nick2sd(script_getstr(st, 2));
  17143. else
  17144. sd = script_rid2sd(st);
  17145. if( sd == NULL)
  17146. return SCRIPT_CMD_FAILURE;
  17147. if( pc_issit(sd) && pc_setstand(sd, false)) {
  17148. skill_sit(sd, 0);
  17149. clif_standing(&sd->bl);
  17150. }
  17151. return SCRIPT_CMD_SUCCESS;
  17152. }
  17153. /** Creates an array of bounded item IDs
  17154. * countbound {<type>{,<char_id>}};
  17155. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  17156. * @return amt: Amount of items found
  17157. */
  17158. BUILDIN_FUNC(countbound)
  17159. {
  17160. int i, type, j = 0, k = 0;
  17161. TBL_PC *sd;
  17162. if (!script_charid2sd(3,sd))
  17163. return SCRIPT_CMD_FAILURE;
  17164. type = script_getnum(st,2);
  17165. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  17166. if( sd->status.inventory[i].nameid > 0 && (
  17167. (!type && sd->status.inventory[i].bound) || (type && sd->status.inventory[i].bound == type)
  17168. ))
  17169. {
  17170. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  17171. k++;
  17172. j += sd->status.inventory[i].amount;
  17173. }
  17174. }
  17175. script_pushint(st,j);
  17176. return SCRIPT_CMD_SUCCESS;
  17177. }
  17178. /** Creates new party
  17179. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  17180. * @param party_name: String of party name that will be created
  17181. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  17182. * @param item_share: 0-Each Take. 1-Party Share
  17183. * @param item_share_type: 0-Each Take. 1-Even Share
  17184. * @return val: Result value
  17185. * -3 - party name is exist
  17186. * -2 - player is in party already
  17187. * -1 - player is not found
  17188. * 0 - unknown error
  17189. * 1 - success, will return party id $@party_create_id
  17190. */
  17191. BUILDIN_FUNC(party_create)
  17192. {
  17193. char party_name[NAME_LENGTH];
  17194. int item1 = 0, item2 = 0;
  17195. TBL_PC *sd;
  17196. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  17197. script_pushint(st,-1);
  17198. return SCRIPT_CMD_FAILURE;
  17199. }
  17200. if( sd->status.party_id ) {
  17201. script_pushint(st,-2);
  17202. return SCRIPT_CMD_FAILURE;
  17203. }
  17204. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  17205. trim(party_name);
  17206. if( party_searchname(party_name) ) {
  17207. script_pushint(st,-3);
  17208. return SCRIPT_CMD_FAILURE;
  17209. }
  17210. if( script_getnum(st,4) )
  17211. item1 = 1;
  17212. if( script_getnum(st,5) )
  17213. item2 = 1;
  17214. party_create_byscript = 1;
  17215. script_pushint(st,party_create(sd,party_name,item1,item2));
  17216. return SCRIPT_CMD_SUCCESS;
  17217. }
  17218. /** Adds player to specified party
  17219. * party_addmember <party id>,<char id>;
  17220. * @param party_id: The party that will be entered by player
  17221. * @param char_id: Char id of player that will be joined to the party
  17222. * @return val: Result value
  17223. * -4 - party is full
  17224. * -3 - party is not found
  17225. * -2 - player is in party already
  17226. * -1 - player is not found
  17227. * 0 - unknown error
  17228. * 1 - success
  17229. */
  17230. BUILDIN_FUNC(party_addmember)
  17231. {
  17232. int party_id = script_getnum(st,2);
  17233. TBL_PC *sd;
  17234. struct party_data *party;
  17235. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  17236. script_pushint(st,-1);
  17237. return SCRIPT_CMD_FAILURE;
  17238. }
  17239. if( sd->status.party_id ) {
  17240. script_pushint(st,-2);
  17241. return SCRIPT_CMD_FAILURE;
  17242. }
  17243. if( !(party = party_search(party_id)) ) {
  17244. script_pushint(st,-3);
  17245. return SCRIPT_CMD_FAILURE;
  17246. }
  17247. if( party->party.count >= MAX_PARTY ) {
  17248. script_pushint(st,-4);
  17249. return SCRIPT_CMD_FAILURE;
  17250. }
  17251. sd->party_invite = party_id;
  17252. script_pushint(st,party_add_member(party_id,sd));
  17253. return SCRIPT_CMD_SUCCESS;
  17254. }
  17255. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  17256. * party_delmember {<char id>,<party_id>};
  17257. * @param: char_id
  17258. * @param: party_id
  17259. * @return val: Result value
  17260. * -3 - player is not in party
  17261. * -2 - party is not found
  17262. * -1 - player is not found
  17263. * 0 - unknown error
  17264. * 1 - success
  17265. */
  17266. BUILDIN_FUNC(party_delmember)
  17267. {
  17268. TBL_PC *sd = NULL;
  17269. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  17270. script_pushint(st,-1);
  17271. return SCRIPT_CMD_FAILURE;
  17272. }
  17273. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  17274. script_pushint(st,party_removemember2(sd,0,0));
  17275. else
  17276. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  17277. return SCRIPT_CMD_SUCCESS;
  17278. }
  17279. /** Changes party leader of specified party (even the leader is offline)
  17280. * party_changeleader <party id>,<char id>;
  17281. * @param party_id: ID of party
  17282. * @param char_id: Char ID of new leader
  17283. * @return val: Result value
  17284. * -4 - player is party leader already
  17285. * -3 - player is not in this party
  17286. * -2 - player is not found
  17287. * -1 - party is not found
  17288. * 0 - unknown error
  17289. * 1 - success */
  17290. BUILDIN_FUNC(party_changeleader)
  17291. {
  17292. int i, party_id = script_getnum(st,2);
  17293. TBL_PC *sd = NULL;
  17294. TBL_PC *tsd = NULL;
  17295. struct party_data *party = NULL;
  17296. if( !(party = party_search(party_id)) ) {
  17297. script_pushint(st,-1);
  17298. return SCRIPT_CMD_FAILURE;
  17299. }
  17300. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  17301. script_pushint(st,-2);
  17302. return SCRIPT_CMD_FAILURE;
  17303. }
  17304. if( tsd->status.party_id != party_id ) {
  17305. script_pushint(st,-3);
  17306. return SCRIPT_CMD_FAILURE;
  17307. }
  17308. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17309. if( i >= MAX_PARTY ) { //this is should impossible!
  17310. script_pushint(st,0);
  17311. return SCRIPT_CMD_FAILURE;
  17312. }
  17313. if( party->data[i].sd == tsd ) {
  17314. script_pushint(st,-4);
  17315. return SCRIPT_CMD_FAILURE;
  17316. }
  17317. script_pushint(st,party_changeleader(sd,tsd,party));
  17318. return SCRIPT_CMD_SUCCESS;
  17319. }
  17320. /** Changes party option
  17321. * party_changeoption <party id>,<option>,<flag>;
  17322. * @param party_id: ID of party that will be changed
  17323. * @param option: Type of option
  17324. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  17325. */
  17326. BUILDIN_FUNC(party_changeoption)
  17327. {
  17328. struct party_data *party;
  17329. if( !(party = party_search(script_getnum(st,2))) ) {
  17330. script_pushint(st,-1);
  17331. return SCRIPT_CMD_FAILURE;
  17332. }
  17333. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  17334. return SCRIPT_CMD_SUCCESS;
  17335. }
  17336. /** Destroys party with party id.
  17337. * party_destroy <party id>;
  17338. * @param party_id: ID of party that will be destroyed
  17339. */
  17340. BUILDIN_FUNC(party_destroy)
  17341. {
  17342. int i;
  17343. struct party_data *party;
  17344. if( !(party = party_search(script_getnum(st,2))) ) {
  17345. script_pushint(st,0);
  17346. return SCRIPT_CMD_FAILURE;
  17347. }
  17348. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17349. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  17350. int j;
  17351. for( j = 0; j < MAX_PARTY; j++ ) {
  17352. TBL_PC *sd = party->data[j].sd;
  17353. if(sd)
  17354. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  17355. else if( party->party.member[j].char_id )
  17356. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  17357. }
  17358. party_broken(party->party.party_id);
  17359. script_pushint(st,1);
  17360. }
  17361. else //leader leave = party broken
  17362. script_pushint(st,party_leave(party->data[i].sd));
  17363. return SCRIPT_CMD_SUCCESS;
  17364. }
  17365. /** Checks if a player's client version meets a required version or date.
  17366. * @param type: 0 - check by version number; 1 - check by date
  17367. * @param data: Input
  17368. */
  17369. BUILDIN_FUNC(is_clientver) {
  17370. TBL_PC *sd = NULL;
  17371. int type = script_getnum(st,2);
  17372. int data = script_getnum(st,3);
  17373. int ret = 0;
  17374. if (script_hasdata(st,4))
  17375. sd = map_charid2sd(script_getnum(st,4));
  17376. else
  17377. sd = script_rid2sd(st);
  17378. if (sd == NULL) {
  17379. script_pushint(st,0);
  17380. return SCRIPT_CMD_FAILURE;
  17381. }
  17382. switch(type){
  17383. case 0:
  17384. ret = (sd->packet_ver >= data)?1:0;
  17385. break;
  17386. case 1:
  17387. ret = (sd->packet_ver >= date2version(data))?1:0;
  17388. break;
  17389. }
  17390. script_pushint(st,ret);
  17391. return SCRIPT_CMD_SUCCESS;
  17392. }
  17393. /** Returns various information about a player's VIP status. Need to enable VIP system
  17394. * vip_status <type>,{"<character name>"};
  17395. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  17396. * @param name: Character name (Optional)
  17397. */
  17398. BUILDIN_FUNC(vip_status) {
  17399. #ifdef VIP_ENABLE
  17400. TBL_PC *sd;
  17401. char vip_str[26];
  17402. time_t now = time(NULL);
  17403. int type = script_getnum(st, 2);
  17404. if (script_hasdata(st, 3))
  17405. sd = map_nick2sd(script_getstr(st, 3));
  17406. else
  17407. sd = script_rid2sd(st);
  17408. if (sd == NULL)
  17409. return SCRIPT_CMD_FAILURE;
  17410. switch(type) {
  17411. case 1: // Get VIP status.
  17412. script_pushint(st, pc_isvip(sd));
  17413. break;
  17414. case 2: // Get VIP expire date.
  17415. if (pc_isvip(sd)) {
  17416. time_t viptime = sd->vip.time;
  17417. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  17418. vip_str[25] = '\0';
  17419. script_pushstrcopy(st, vip_str);
  17420. } else
  17421. script_pushint(st, 0);
  17422. break;
  17423. case 3: // Get remaining time.
  17424. if (pc_isvip(sd)) {
  17425. time_t viptime_remain = sd->vip.time - now;
  17426. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  17427. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  17428. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  17429. script_pushstrcopy(st, vip_str);
  17430. } else
  17431. script_pushint(st, 0);
  17432. break;
  17433. }
  17434. #else
  17435. script_pushint(st, 0);
  17436. #endif
  17437. return SCRIPT_CMD_SUCCESS;
  17438. }
  17439. /** Adds or removes VIP time in minutes. Need to enable VIP system
  17440. * vip_time <time in mn>,{"<character name>"};
  17441. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  17442. * @param name: Character name (optional)
  17443. */
  17444. BUILDIN_FUNC(vip_time) {
  17445. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  17446. TBL_PC *sd;
  17447. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  17448. if (script_hasdata(st, 3))
  17449. sd = map_nick2sd(script_getstr(st, 3));
  17450. else
  17451. sd = script_rid2sd(st);
  17452. if (sd == NULL)
  17453. return SCRIPT_CMD_FAILURE;
  17454. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  17455. #endif
  17456. return SCRIPT_CMD_SUCCESS;
  17457. }
  17458. /** Turns a player into a monster and grants SC attribute effect. [malufett/Hercules]
  17459. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  17460. * @param monster: Monster ID or name
  17461. * @param duration: Transform duration in millisecond (ms)
  17462. * @param sc_type: Type of SC that will be affected during the transformation
  17463. * @param val1: Value for SC
  17464. * @param val2: Value for SC
  17465. * @param val3: Value for SC
  17466. * @param val4: Value for SC
  17467. */
  17468. BUILDIN_FUNC(montransform) {
  17469. TBL_PC *sd;
  17470. enum sc_type type;
  17471. int tick, mob_id, val1, val2, val3, val4;
  17472. struct script_data *data;
  17473. val1 = val2 = val3 = val4 = 0;
  17474. if( (sd = script_rid2sd(st)) == NULL )
  17475. return 1;
  17476. data = script_getdata(st, 2);
  17477. get_val(st, data); // Convert into value in case of a variable
  17478. if( data_isstring(data) )
  17479. mob_id = mobdb_searchname(script_getstr(st, 2));
  17480. else
  17481. mob_id = mobdb_checkid(script_getnum(st, 2));
  17482. tick = script_getnum(st, 3);
  17483. if (script_hasdata(st, 4))
  17484. type = (sc_type)script_getnum(st, 4);
  17485. else
  17486. type = SC_NONE;
  17487. if (mob_id == 0) {
  17488. if( data_isstring(data) )
  17489. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  17490. else
  17491. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  17492. return SCRIPT_CMD_FAILURE;
  17493. }
  17494. if (mob_id == MOBID_EMPERIUM) {
  17495. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  17496. return SCRIPT_CMD_FAILURE;
  17497. }
  17498. if (!(type >= SC_NONE && type < SC_MAX)) {
  17499. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  17500. return SCRIPT_CMD_FAILURE;
  17501. }
  17502. if (script_hasdata(st, 5))
  17503. val1 = script_getnum(st, 5);
  17504. if (script_hasdata(st, 6))
  17505. val2 = script_getnum(st, 6);
  17506. if (script_hasdata(st, 7))
  17507. val3 = script_getnum(st, 7);
  17508. if (script_hasdata(st, 8))
  17509. val4 = script_getnum(st, 8);
  17510. if (tick != 0) {
  17511. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  17512. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  17513. return SCRIPT_CMD_FAILURE;
  17514. }
  17515. if (sd->disguise){
  17516. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  17517. return SCRIPT_CMD_FAILURE;
  17518. }
  17519. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  17520. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  17521. if (type != SC_NONE)
  17522. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  17523. }
  17524. return SCRIPT_CMD_SUCCESS;
  17525. }
  17526. /**
  17527. * Attach script to player for certain duration
  17528. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  17529. * @param "script code"
  17530. * @param duration
  17531. * @param flag
  17532. * @param icon
  17533. * @param char_id
  17534. * @author [Cydh]
  17535. **/
  17536. BUILDIN_FUNC(bonus_script) {
  17537. uint16 flag = 0;
  17538. int16 icon = SI_BLANK;
  17539. uint32 dur;
  17540. uint8 type = 0;
  17541. TBL_PC* sd;
  17542. const char *script_str = NULL;
  17543. struct s_bonus_script_entry *entry = NULL;
  17544. if (script_hasdata(st,7)) {
  17545. if (!(sd = map_charid2sd(script_getnum(st,7)))) {
  17546. ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7));
  17547. return SCRIPT_CMD_FAILURE;
  17548. }
  17549. }
  17550. else
  17551. sd = script_rid2sd(st);
  17552. if (sd == NULL)
  17553. return SCRIPT_CMD_FAILURE;
  17554. script_str = script_getstr(st,2);
  17555. dur = 1000 * abs(script_getnum(st,3));
  17556. FETCH(4, flag);
  17557. FETCH(5, type);
  17558. FETCH(6, icon);
  17559. // No Script string, No Duration!
  17560. if (script_str[0] == '\0' || !dur) {
  17561. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  17562. return SCRIPT_CMD_FAILURE;
  17563. }
  17564. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  17565. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  17566. return SCRIPT_CMD_FAILURE;
  17567. }
  17568. if (icon <= SI_BLANK || icon >= SI_MAX)
  17569. icon = SI_BLANK;
  17570. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  17571. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  17572. status_calc_pc(sd,SCO_NONE);
  17573. }
  17574. return SCRIPT_CMD_SUCCESS;
  17575. }
  17576. /**
  17577. * Removes all bonus script from player
  17578. * bonus_script_clear {<flag>,{<char_id>}};
  17579. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  17580. * @param char_id Clear script from this character
  17581. * @author [Cydh]
  17582. */
  17583. BUILDIN_FUNC(bonus_script_clear) {
  17584. TBL_PC* sd;
  17585. bool flag = false;
  17586. if (script_hasdata(st,2))
  17587. flag = script_getnum(st,2);
  17588. if (script_hasdata(st,3))
  17589. sd = map_charid2sd(script_getnum(st,3));
  17590. else
  17591. sd = script_rid2sd(st);
  17592. if (sd == NULL)
  17593. return SCRIPT_CMD_FAILURE;
  17594. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  17595. return SCRIPT_CMD_SUCCESS;
  17596. }
  17597. /** Allows player to use atcommand while talking with NPC
  17598. * enable_command;
  17599. * @author [Cydh], [Kichi]
  17600. */
  17601. BUILDIN_FUNC(enable_command) {
  17602. TBL_PC* sd = script_rid2sd(st);
  17603. if (!sd)
  17604. return SCRIPT_CMD_FAILURE;
  17605. sd->state.disable_atcommand_on_npc = 0;
  17606. return SCRIPT_CMD_SUCCESS;
  17607. }
  17608. /** Prevents player to use atcommand while talking with NPC
  17609. * disable_command;
  17610. * @author [Cydh], [Kichi]
  17611. */
  17612. BUILDIN_FUNC(disable_command) {
  17613. TBL_PC* sd = script_rid2sd(st);
  17614. if (!sd)
  17615. return SCRIPT_CMD_FAILURE;
  17616. sd->state.disable_atcommand_on_npc = 1;
  17617. return SCRIPT_CMD_SUCCESS;
  17618. }
  17619. /** Get the information of the members of a guild by type.
  17620. * getguildmember <guild_id>{,<type>};
  17621. * @param guild_id: ID of guild
  17622. * @param type: Type of option (optional)
  17623. */
  17624. BUILDIN_FUNC(getguildmember)
  17625. {
  17626. struct guild *g = NULL;
  17627. uint8 j = 0;
  17628. g = guild_search(script_getnum(st,2));
  17629. if (g) {
  17630. uint8 i, type = 0;
  17631. struct script_data *data = NULL;
  17632. char *varname = NULL;
  17633. if (script_hasdata(st,3))
  17634. type = script_getnum(st,3);
  17635. if (script_hasdata(st,4)) {
  17636. data = script_getdata(st, 4);
  17637. if (!data_isreference(data)) {
  17638. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  17639. return SCRIPT_CMD_FAILURE;
  17640. }
  17641. varname = reference_getname(data);
  17642. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  17643. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  17644. return SCRIPT_CMD_FAILURE;
  17645. }
  17646. }
  17647. for (i = 0; i < MAX_GUILD; i++) {
  17648. if (g->member[i].account_id) {
  17649. switch (type) {
  17650. case 2:
  17651. if (data)
  17652. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  17653. else
  17654. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  17655. break;
  17656. case 1:
  17657. if (data)
  17658. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  17659. else
  17660. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  17661. break;
  17662. default:
  17663. if (data)
  17664. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  17665. else
  17666. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  17667. break;
  17668. }
  17669. j++;
  17670. }
  17671. }
  17672. }
  17673. mapreg_setreg(add_str("$@guildmembercount"), j);
  17674. script_pushint(st, j);
  17675. return SCRIPT_CMD_SUCCESS;
  17676. }
  17677. /** Adds spirit ball to player for 'duration' in milisecond
  17678. * addspiritball <count>,<duration>{,<char_id>};
  17679. * @param count How many spirit ball will be added
  17680. * @param duration How long spiritball is active until it disappears
  17681. * @param char_id Target player (Optional)
  17682. * @author [Cydh]
  17683. */
  17684. BUILDIN_FUNC(addspiritball) {
  17685. uint8 i, count = script_getnum(st,2);
  17686. uint16 duration = script_getnum(st,3);
  17687. struct map_session_data *sd = NULL;
  17688. if (count == 0)
  17689. return SCRIPT_CMD_SUCCESS;
  17690. if (script_hasdata(st,4)) {
  17691. if (!script_isstring(st,4))
  17692. sd = map_charid2sd(script_getnum(st,4));
  17693. else
  17694. sd = map_nick2sd(script_getstr(st,4));
  17695. }
  17696. else
  17697. sd = script_rid2sd(st);
  17698. if (!sd)
  17699. return SCRIPT_CMD_FAILURE;
  17700. for (i = 0; i < count; i++)
  17701. pc_addspiritball(sd,duration,10);
  17702. return SCRIPT_CMD_SUCCESS;
  17703. }
  17704. /** Deletes the spirit ball(s) from player
  17705. * delspiritball <count>{,<char_id>};
  17706. * @param count How many spirit ball will be deleted
  17707. * @param char_id Target player (Optional)
  17708. * @author [Cydh]
  17709. */
  17710. BUILDIN_FUNC(delspiritball) {
  17711. uint8 count = script_getnum(st,2);
  17712. struct map_session_data *sd = NULL;
  17713. if (count == 0)
  17714. count = 1;
  17715. if (script_hasdata(st,3)) {
  17716. if (!script_isstring(st,3))
  17717. sd = map_charid2sd(script_getnum(st,3));
  17718. else
  17719. sd = map_nick2sd(script_getstr(st,3));
  17720. }
  17721. else
  17722. sd = script_rid2sd(st);
  17723. if (!sd)
  17724. return SCRIPT_CMD_FAILURE;
  17725. pc_delspiritball(sd,count,0);
  17726. return SCRIPT_CMD_SUCCESS;
  17727. }
  17728. /** Counts the spirit ball that player has
  17729. * countspiritball {<char_id>};
  17730. * @param char_id Target player (Optional)
  17731. * @author [Cydh]
  17732. */
  17733. BUILDIN_FUNC(countspiritball) {
  17734. struct map_session_data *sd;
  17735. if (script_hasdata(st,2)) {
  17736. if (!script_isstring(st,2))
  17737. sd = map_charid2sd(script_getnum(st,2));
  17738. else
  17739. sd = map_nick2sd(script_getstr(st,2));
  17740. }
  17741. else
  17742. sd = script_rid2sd(st);
  17743. if (!sd)
  17744. return SCRIPT_CMD_FAILURE;
  17745. script_pushint(st,sd->spiritball);
  17746. return SCRIPT_CMD_SUCCESS;
  17747. }
  17748. /** Merges separated stackable items because of guid
  17749. * mergeitem {<char_id>};
  17750. * @param char_id Char ID (Optional)
  17751. * @author [Cydh]
  17752. */
  17753. BUILDIN_FUNC(mergeitem) {
  17754. struct map_session_data *sd;
  17755. if (!script_charid2sd(2, sd))
  17756. return SCRIPT_CMD_FAILURE;
  17757. clif_merge_item_open(sd);
  17758. return SCRIPT_CMD_SUCCESS;
  17759. }
  17760. /** Merges separated stackable items because of guid (Unofficial)
  17761. * mergeitem2 {<item_id>,{<char_id>}};
  17762. * mergeitem2 {"<item name>",{<char_id>}};
  17763. * @param item Item ID/Name for merging specific item (Optional)
  17764. * @author [Cydh]
  17765. */
  17766. BUILDIN_FUNC(mergeitem2) {
  17767. struct map_session_data *sd;
  17768. struct item *items = NULL;
  17769. uint16 i, count = 0;
  17770. int nameid = 0;
  17771. if (!script_charid2sd(3, sd))
  17772. return SCRIPT_CMD_FAILURE;
  17773. if (script_hasdata(st, 2)) {
  17774. struct script_data *data = script_getdata(st, 2);
  17775. struct item_data *id;
  17776. get_val(st, data);
  17777. if (data_isstring(data)) {// "<item name>"
  17778. const char *name = conv_str(st,data);
  17779. if (!(id = itemdb_searchname(name))) {
  17780. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  17781. script_pushint(st, count);
  17782. return SCRIPT_CMD_FAILURE;
  17783. }
  17784. nameid = id->nameid;
  17785. }
  17786. else if (data_isint(data)) {// <item id>
  17787. nameid = conv_num(st,data);
  17788. if (!(id = itemdb_exists(nameid))) {
  17789. ShowError("buildin_mergeitem2: Nonexistant item %d requested.\n", nameid);
  17790. script_pushint(st, count);
  17791. return SCRIPT_CMD_FAILURE;
  17792. }
  17793. }
  17794. }
  17795. for (i = 0; i < MAX_INVENTORY; i++) {
  17796. struct item *it = &sd->status.inventory[i];
  17797. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  17798. continue;
  17799. if ((!nameid || (nameid == it->nameid))) {
  17800. uint8 k;
  17801. if (!count) {
  17802. CREATE(items, struct item, 1);
  17803. memcpy(&items[count++], it, sizeof(struct item));
  17804. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  17805. continue;
  17806. }
  17807. for (k = 0; k < count; k++) {
  17808. // Find Match
  17809. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  17810. items[k].amount += it->amount;
  17811. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  17812. break;
  17813. }
  17814. }
  17815. if (k >= count) {
  17816. // New entry
  17817. RECREATE(items, struct item, count+1);
  17818. memcpy(&items[count++], it, sizeof(struct item));
  17819. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  17820. }
  17821. }
  17822. }
  17823. if (!items) // Nothing todo here
  17824. return SCRIPT_CMD_SUCCESS;
  17825. // Retrieve the items
  17826. for (i = 0; i < count; i++) {
  17827. uint8 flag = 0;
  17828. if (!&items[i])
  17829. continue;
  17830. items[i].id = 0;
  17831. items[i].unique_id = 0;
  17832. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  17833. clif_additem(sd, i, items[i].amount, flag);
  17834. }
  17835. aFree(items);
  17836. script_pushint(st, count);
  17837. return SCRIPT_CMD_SUCCESS;
  17838. }
  17839. /**
  17840. * Update an entry from NPC shop.
  17841. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  17842. **/
  17843. BUILDIN_FUNC(npcshopupdate) {
  17844. const char* npcname = script_getstr(st, 2);
  17845. struct npc_data* nd = npc_name2id(npcname);
  17846. uint16 nameid = script_getnum(st, 3);
  17847. int price = script_getnum(st, 4);
  17848. #if PACKETVER >= 20131223
  17849. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  17850. #endif
  17851. int i;
  17852. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  17853. script_pushint(st,0);
  17854. return SCRIPT_CMD_FAILURE;
  17855. }
  17856. if (!nd->u.shop.count) {
  17857. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  17858. script_pushint(st,0);
  17859. return SCRIPT_CMD_FAILURE;
  17860. }
  17861. for (i = 0; i < nd->u.shop.count; i++) {
  17862. if (nd->u.shop.shop_item[i].nameid == nameid) {
  17863. if (price != 0)
  17864. nd->u.shop.shop_item[i].value = price;
  17865. #if PACKETVER >= 20131223
  17866. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  17867. nd->u.shop.shop_item[i].qty = stock;
  17868. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  17869. }
  17870. #endif
  17871. }
  17872. }
  17873. script_pushint(st,1);
  17874. return SCRIPT_CMD_SUCCESS;
  17875. }
  17876. /**
  17877. * Get rid from running script.
  17878. * getattachedrid();
  17879. **/
  17880. BUILDIN_FUNC(getattachedrid) {
  17881. script_pushint(st, st->rid);
  17882. return SCRIPT_CMD_SUCCESS;
  17883. }
  17884. /**
  17885. * Get variable from a Player
  17886. * getvar <variable>,<char_id>;
  17887. */
  17888. BUILDIN_FUNC(getvar) {
  17889. int char_id = script_getnum(st, 3);
  17890. struct map_session_data *sd = map_charid2sd(char_id);
  17891. struct script_data *data = NULL;
  17892. const char *name = NULL;
  17893. if (!sd) {
  17894. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  17895. return SCRIPT_CMD_FAILURE;
  17896. }
  17897. data = script_getdata(st, 2);
  17898. if (!data_isreference(data)) {
  17899. ShowError("buildin_getvar: Not a variable\n");
  17900. script_reportdata(data);
  17901. script_pushnil(st);
  17902. st->state = END;
  17903. return SCRIPT_CMD_FAILURE;
  17904. }
  17905. name = reference_getname(data);
  17906. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  17907. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  17908. script_reportdata(data);
  17909. script_pushnil(st);
  17910. st->state = END;
  17911. return SCRIPT_CMD_FAILURE;
  17912. }
  17913. get_val_(st, data, sd);
  17914. if (data_isint(data))
  17915. script_pushint(st, conv_num_(st, data, sd));
  17916. else
  17917. script_pushstrcopy(st, conv_str_(st, data, sd));
  17918. push_val2(st->stack, C_NAME, reference_getuid(data), reference_getref(data));
  17919. return SCRIPT_CMD_SUCCESS;
  17920. }
  17921. /**
  17922. * Display script message
  17923. * showscript "<message>"{,<GID>};
  17924. **/
  17925. BUILDIN_FUNC(showscript) {
  17926. struct block_list *bl = NULL;
  17927. const char *msg = script_getstr(st,2);
  17928. int id = 0;
  17929. if (script_hasdata(st,3)) {
  17930. id = script_getnum(st,3);
  17931. bl = map_id2bl(id);
  17932. }
  17933. else {
  17934. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  17935. }
  17936. if (!bl) {
  17937. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  17938. script_pushint(st,0);
  17939. return SCRIPT_CMD_FAILURE;
  17940. }
  17941. clif_showscript(bl, msg);
  17942. script_pushint(st,1);
  17943. return SCRIPT_CMD_SUCCESS;
  17944. }
  17945. /**
  17946. * Ignore the SECURE_NPCTIMEOUT function.
  17947. * ignoretimeout <flag>{,<char_id>};
  17948. */
  17949. BUILDIN_FUNC(ignoretimeout)
  17950. {
  17951. #ifdef SECURE_NPCTIMEOUT
  17952. struct map_session_data *sd = NULL;
  17953. if (script_hasdata(st,3)) {
  17954. if (!script_isstring(st,3))
  17955. sd = map_charid2sd(script_getnum(st,3));
  17956. else
  17957. sd = map_nick2sd(script_getstr(st,3));
  17958. } else
  17959. sd = script_rid2sd(st);
  17960. if (!sd)
  17961. return SCRIPT_CMD_FAILURE;
  17962. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  17963. #endif
  17964. return SCRIPT_CMD_SUCCESS;
  17965. }
  17966. #include "../custom/script.inc"
  17967. // declarations that were supposed to be exported from npc_chat.c
  17968. #ifdef PCRE_SUPPORT
  17969. BUILDIN_FUNC(defpattern);
  17970. BUILDIN_FUNC(activatepset);
  17971. BUILDIN_FUNC(deactivatepset);
  17972. BUILDIN_FUNC(deletepset);
  17973. #endif
  17974. /** Regular expression matching
  17975. * preg_match(<pattern>,<string>{,<offset>})
  17976. */
  17977. BUILDIN_FUNC(preg_match) {
  17978. #ifdef PCRE_SUPPORT
  17979. pcre *re;
  17980. pcre_extra *pcreExtra;
  17981. const char *error;
  17982. int erroffset, r, offset = 0;
  17983. int subStrVec[30];
  17984. const char* pattern = script_getstr(st,2);
  17985. const char* subject = script_getstr(st,3);
  17986. if (script_hasdata(st,4))
  17987. offset = script_getnum(st,4);
  17988. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  17989. pcreExtra = pcre_study(re, 0, &error);
  17990. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  17991. pcre_free(re);
  17992. if (pcreExtra != NULL)
  17993. pcre_free(pcreExtra);
  17994. if (r < 0)
  17995. script_pushint(st,0);
  17996. else
  17997. script_pushint(st,(r > 0) ? r : 30 / 3);
  17998. return SCRIPT_CMD_SUCCESS;
  17999. #else
  18000. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  18001. script_pushint(st,0);
  18002. return SCRIPT_CMD_SUCCESS;
  18003. #endif
  18004. }
  18005. /// script command definitions
  18006. /// for an explanation on args, see add_buildin_func
  18007. struct script_function buildin_func[] = {
  18008. // NPC interaction
  18009. BUILDIN_DEF(mes,"s*"),
  18010. BUILDIN_DEF(next,""),
  18011. BUILDIN_DEF(close,""),
  18012. BUILDIN_DEF(close2,""),
  18013. BUILDIN_DEF(menu,"sl*"),
  18014. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  18015. BUILDIN_DEF(prompt,"s*"),
  18016. //
  18017. BUILDIN_DEF(goto,"l"),
  18018. BUILDIN_DEF(callsub,"l*"),
  18019. BUILDIN_DEF(callfunc,"s*"),
  18020. BUILDIN_DEF(return,"?"),
  18021. BUILDIN_DEF(getarg,"i?"),
  18022. BUILDIN_DEF(jobchange,"i?"),
  18023. BUILDIN_DEF(jobname,"i"),
  18024. BUILDIN_DEF(input,"r??"),
  18025. BUILDIN_DEF(warp,"sii"),
  18026. BUILDIN_DEF(areawarp,"siiiisii??"),
  18027. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  18028. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  18029. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  18030. BUILDIN_DEF(setlook,"ii"),
  18031. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  18032. BUILDIN_DEF(set,"rv?"),
  18033. BUILDIN_DEF(setarray,"rv*"),
  18034. BUILDIN_DEF(cleararray,"rvi"),
  18035. BUILDIN_DEF(copyarray,"rri"),
  18036. BUILDIN_DEF(getarraysize,"r"),
  18037. BUILDIN_DEF(deletearray,"r?"),
  18038. BUILDIN_DEF(getelementofarray,"ri"),
  18039. BUILDIN_DEF(getitem,"vi?"),
  18040. BUILDIN_DEF(rentitem,"vi?"),
  18041. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  18042. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  18043. BUILDIN_DEF(getnameditem,"vv"),
  18044. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  18045. BUILDIN_DEF(makeitem,"visii"),
  18046. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  18047. BUILDIN_DEF(delitem,"vi?"),
  18048. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  18049. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  18050. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  18051. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  18052. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  18053. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  18054. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  18055. BUILDIN_DEF(cutin,"si"),
  18056. BUILDIN_DEF(viewpoint,"iiiii"),
  18057. BUILDIN_DEF(heal,"ii"),
  18058. BUILDIN_DEF(itemheal,"ii"),
  18059. BUILDIN_DEF(percentheal,"ii"),
  18060. BUILDIN_DEF(rand,"i?"),
  18061. BUILDIN_DEF(countitem,"v?"),
  18062. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  18063. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  18064. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  18065. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  18066. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  18067. BUILDIN_DEF(checkweight,"vi*"),
  18068. BUILDIN_DEF(checkweight2,"rr"),
  18069. BUILDIN_DEF(readparam,"i?"),
  18070. BUILDIN_DEF(getcharid,"i?"),
  18071. BUILDIN_DEF(getnpcid,"i?"),
  18072. BUILDIN_DEF(getpartyname,"i"),
  18073. BUILDIN_DEF(getpartymember,"i??"),
  18074. BUILDIN_DEF(getpartyleader,"i?"),
  18075. BUILDIN_DEF(getguildname,"i"),
  18076. BUILDIN_DEF(getguildmaster,"i"),
  18077. BUILDIN_DEF(getguildmasterid,"i"),
  18078. BUILDIN_DEF(strcharinfo,"i?"),
  18079. BUILDIN_DEF(strnpcinfo,"i"),
  18080. BUILDIN_DEF(getequipid,"i?"),
  18081. BUILDIN_DEF(getequipuniqueid,"i?"),
  18082. BUILDIN_DEF(getequipname,"i?"),
  18083. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  18084. BUILDIN_DEF(repair,"i?"), // [Valaris]
  18085. BUILDIN_DEF(repairall,"?"),
  18086. BUILDIN_DEF(getequipisequiped,"i?"),
  18087. BUILDIN_DEF(getequipisenableref,"i?"),
  18088. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  18089. BUILDIN_DEF(getequipweaponlv,"i?"),
  18090. BUILDIN_DEF(getequippercentrefinery,"i?"),
  18091. BUILDIN_DEF(successrefitem,"i??"),
  18092. BUILDIN_DEF(failedrefitem,"i?"),
  18093. BUILDIN_DEF(downrefitem,"i??"),
  18094. BUILDIN_DEF(statusup,"i?"),
  18095. BUILDIN_DEF(statusup2,"ii?"),
  18096. BUILDIN_DEF(bonus,"iv"),
  18097. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  18098. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  18099. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  18100. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  18101. BUILDIN_DEF(autobonus,"sii??"),
  18102. BUILDIN_DEF(autobonus2,"sii??"),
  18103. BUILDIN_DEF(autobonus3,"siiv?"),
  18104. BUILDIN_DEF(skill,"vi?"),
  18105. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  18106. BUILDIN_DEF(guildskill,"vi"),
  18107. BUILDIN_DEF(getskilllv,"v"),
  18108. BUILDIN_DEF(getgdskilllv,"iv"),
  18109. BUILDIN_DEF(basicskillcheck,""),
  18110. BUILDIN_DEF(getgmlevel,"?"),
  18111. BUILDIN_DEF(getgroupid,"?"),
  18112. BUILDIN_DEF(end,""),
  18113. BUILDIN_DEF(checkoption,"i?"),
  18114. BUILDIN_DEF(setoption,"i??"),
  18115. BUILDIN_DEF(setcart,"??"),
  18116. BUILDIN_DEF(checkcart,"?"),
  18117. BUILDIN_DEF(setfalcon,"??"),
  18118. BUILDIN_DEF(checkfalcon,"?"),
  18119. BUILDIN_DEF(setriding,"??"),
  18120. BUILDIN_DEF(checkriding,"?"),
  18121. BUILDIN_DEF(checkwug,"?"),
  18122. BUILDIN_DEF(checkmadogear,"?"),
  18123. BUILDIN_DEF(setmadogear,"??"),
  18124. BUILDIN_DEF2(savepoint,"save","sii?"),
  18125. BUILDIN_DEF(savepoint,"sii?"),
  18126. BUILDIN_DEF(gettimetick,"i"),
  18127. BUILDIN_DEF(gettime,"i"),
  18128. BUILDIN_DEF(gettimestr,"si"),
  18129. BUILDIN_DEF(openstorage,""),
  18130. BUILDIN_DEF(guildopenstorage,""),
  18131. BUILDIN_DEF(itemskill,"vi"),
  18132. BUILDIN_DEF(produce,"i"),
  18133. BUILDIN_DEF(cooking,"i"),
  18134. BUILDIN_DEF(monster,"siisii???"),
  18135. BUILDIN_DEF(getmobdrops,"i"),
  18136. BUILDIN_DEF(areamonster,"siiiisii???"),
  18137. BUILDIN_DEF(killmonster,"ss?"),
  18138. BUILDIN_DEF(killmonsterall,"s?"),
  18139. BUILDIN_DEF(clone,"siisi????"),
  18140. BUILDIN_DEF(doevent,"s"),
  18141. BUILDIN_DEF(donpcevent,"s"),
  18142. BUILDIN_DEF(cmdothernpc,"ss"),
  18143. BUILDIN_DEF(addtimer,"is"),
  18144. BUILDIN_DEF(deltimer,"s"),
  18145. BUILDIN_DEF(addtimercount,"is"),
  18146. BUILDIN_DEF(initnpctimer,"??"),
  18147. BUILDIN_DEF(stopnpctimer,"??"),
  18148. BUILDIN_DEF(startnpctimer,"??"),
  18149. BUILDIN_DEF(setnpctimer,"i?"),
  18150. BUILDIN_DEF(getnpctimer,"i?"),
  18151. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  18152. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  18153. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  18154. BUILDIN_DEF(announce,"si?????"),
  18155. BUILDIN_DEF(mapannounce,"ssi?????"),
  18156. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  18157. BUILDIN_DEF(getusers,"i"),
  18158. BUILDIN_DEF(getmapguildusers,"si"),
  18159. BUILDIN_DEF(getmapusers,"s"),
  18160. BUILDIN_DEF(getareausers,"siiii"),
  18161. BUILDIN_DEF(getareadropitem,"siiiiv"),
  18162. BUILDIN_DEF(enablenpc,"s"),
  18163. BUILDIN_DEF(disablenpc,"s"),
  18164. BUILDIN_DEF(hideoffnpc,"s"),
  18165. BUILDIN_DEF(hideonnpc,"s"),
  18166. BUILDIN_DEF(sc_start,"iii???"),
  18167. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  18168. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  18169. BUILDIN_DEF(sc_end,"i?"),
  18170. BUILDIN_DEF(getstatus, "i??"),
  18171. BUILDIN_DEF(getscrate,"ii?"),
  18172. BUILDIN_DEF(debugmes,"s"),
  18173. BUILDIN_DEF2(catchpet,"pet","i"),
  18174. BUILDIN_DEF2(birthpet,"bpet",""),
  18175. BUILDIN_DEF(resetlvl,"i?"),
  18176. BUILDIN_DEF(resetstatus,"?"),
  18177. BUILDIN_DEF(resetskill,"?"),
  18178. BUILDIN_DEF(skillpointcount,"?"),
  18179. BUILDIN_DEF(changebase,"i?"),
  18180. BUILDIN_DEF(changesex,"?"),
  18181. BUILDIN_DEF(changecharsex,"?"),
  18182. BUILDIN_DEF(waitingroom,"si?????"),
  18183. BUILDIN_DEF(delwaitingroom,"?"),
  18184. BUILDIN_DEF(waitingroomkick,"ss"),
  18185. BUILDIN_DEF(getwaitingroomusers, "?"),
  18186. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  18187. BUILDIN_DEF(enablewaitingroomevent,"?"),
  18188. BUILDIN_DEF(disablewaitingroomevent,"?"),
  18189. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  18190. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  18191. BUILDIN_DEF(getwaitingroomstate,"i?"),
  18192. BUILDIN_DEF(warpwaitingpc,"sii?"),
  18193. BUILDIN_DEF(attachrid,"i"),
  18194. BUILDIN_DEF(addrid,"i?????"),
  18195. BUILDIN_DEF(detachrid,""),
  18196. BUILDIN_DEF(isloggedin,"i?"),
  18197. BUILDIN_DEF(setmapflagnosave,"ssii"),
  18198. BUILDIN_DEF(getmapflag,"si?"),
  18199. BUILDIN_DEF(setmapflag,"si??"),
  18200. BUILDIN_DEF(removemapflag,"si?"),
  18201. BUILDIN_DEF(pvpon,"s"),
  18202. BUILDIN_DEF(pvpoff,"s"),
  18203. BUILDIN_DEF(gvgon,"s"),
  18204. BUILDIN_DEF(gvgoff,"s"),
  18205. BUILDIN_DEF(emotion,"i??"),
  18206. BUILDIN_DEF(maprespawnguildid,"sii"),
  18207. BUILDIN_DEF(agitstart,""), // <Agit>
  18208. BUILDIN_DEF(agitend,""),
  18209. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  18210. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  18211. BUILDIN_DEF(getcastlename,"s"),
  18212. BUILDIN_DEF(getcastledata,"si"),
  18213. BUILDIN_DEF(setcastledata,"sii"),
  18214. BUILDIN_DEF(requestguildinfo,"i?"),
  18215. BUILDIN_DEF(getequipcardcnt,"i"),
  18216. BUILDIN_DEF(successremovecards,"i"),
  18217. BUILDIN_DEF(failedremovecards,"ii"),
  18218. BUILDIN_DEF(marriage,"s"),
  18219. BUILDIN_DEF2(wedding_effect,"wedding",""),
  18220. BUILDIN_DEF(divorce,"?"),
  18221. BUILDIN_DEF(ispartneron,"?"),
  18222. BUILDIN_DEF(getpartnerid,"?"),
  18223. BUILDIN_DEF(getchildid,"?"),
  18224. BUILDIN_DEF(getmotherid,"?"),
  18225. BUILDIN_DEF(getfatherid,"?"),
  18226. BUILDIN_DEF(warppartner,"sii"),
  18227. BUILDIN_DEF(getitemname,"v"),
  18228. BUILDIN_DEF(getitemslots,"i"),
  18229. BUILDIN_DEF(makepet,"i"),
  18230. BUILDIN_DEF(getexp,"ii?"),
  18231. BUILDIN_DEF(getinventorylist,"?"),
  18232. BUILDIN_DEF(getskilllist,"?"),
  18233. BUILDIN_DEF(clearitem,"?"),
  18234. BUILDIN_DEF(classchange,"ii"),
  18235. BUILDIN_DEF(misceffect,"i"),
  18236. BUILDIN_DEF(playBGM,"s"),
  18237. BUILDIN_DEF(playBGMall,"s?????"),
  18238. BUILDIN_DEF(soundeffect,"si"),
  18239. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  18240. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  18241. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  18242. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  18243. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  18244. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  18245. BUILDIN_DEF(petloot,"i"), // [Valaris]
  18246. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  18247. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  18248. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  18249. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  18250. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  18251. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  18252. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  18253. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  18254. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  18255. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  18256. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  18257. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  18258. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  18259. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  18260. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  18261. BUILDIN_DEF(mobcount,"ss"),
  18262. BUILDIN_DEF(getlook,"i?"),
  18263. BUILDIN_DEF(getsavepoint,"i?"),
  18264. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  18265. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  18266. BUILDIN_DEF(npcstop,""), // [Valaris]
  18267. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  18268. BUILDIN_DEF(mapid2name,"i"),
  18269. BUILDIN_DEF(checkoption1,"i?"),
  18270. BUILDIN_DEF(checkoption2,"i?"),
  18271. BUILDIN_DEF(guildgetexp,"i"),
  18272. BUILDIN_DEF(guildchangegm,"is"),
  18273. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  18274. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  18275. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  18276. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  18277. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  18278. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  18279. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  18280. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  18281. BUILDIN_DEF(night,""), // sets the server to night time
  18282. BUILDIN_DEF(day,""), // sets the server to day time
  18283. #ifdef PCRE_SUPPORT
  18284. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  18285. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  18286. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  18287. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  18288. #endif
  18289. BUILDIN_DEF(preg_match,"ss?"),
  18290. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  18291. BUILDIN_DEF(recovery,"i???"),
  18292. BUILDIN_DEF(getpetinfo,"i"),
  18293. BUILDIN_DEF(gethominfo,"i"),
  18294. BUILDIN_DEF(getmercinfo,"i?"),
  18295. BUILDIN_DEF(checkequipedcard,"i"),
  18296. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  18297. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  18298. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  18299. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  18300. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  18301. BUILDIN_DEF(charat,"si"),
  18302. BUILDIN_DEF(setchar,"ssi"),
  18303. BUILDIN_DEF(insertchar,"ssi"),
  18304. BUILDIN_DEF(delchar,"si"),
  18305. BUILDIN_DEF(strtoupper,"s"),
  18306. BUILDIN_DEF(strtolower,"s"),
  18307. BUILDIN_DEF(charisupper, "si"),
  18308. BUILDIN_DEF(charislower, "si"),
  18309. BUILDIN_DEF(substr,"sii"),
  18310. BUILDIN_DEF(explode, "rss"),
  18311. BUILDIN_DEF(implode, "r?"),
  18312. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  18313. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  18314. BUILDIN_DEF(strpos,"ss?"),
  18315. BUILDIN_DEF(replacestr,"sss??"),
  18316. BUILDIN_DEF(countstr,"ss?"),
  18317. BUILDIN_DEF(setnpcdisplay,"sv??"),
  18318. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  18319. BUILDIN_DEF(strcmp,"ss"),
  18320. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  18321. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  18322. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  18323. // [zBuffer] List of mathematics commands --->
  18324. BUILDIN_DEF(sqrt,"i"),
  18325. BUILDIN_DEF(pow,"ii"),
  18326. BUILDIN_DEF(distance,"iiii"),
  18327. // <--- [zBuffer] List of mathematics commands
  18328. BUILDIN_DEF(md5,"s"),
  18329. // [zBuffer] List of dynamic var commands --->
  18330. BUILDIN_DEF(getd,"s"),
  18331. BUILDIN_DEF(setd,"sv?"),
  18332. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  18333. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  18334. BUILDIN_DEF(npcshopadditem,"sii*"),
  18335. BUILDIN_DEF(npcshopdelitem,"si*"),
  18336. BUILDIN_DEF(npcshopattach,"s?"),
  18337. BUILDIN_DEF(equip,"i?"),
  18338. BUILDIN_DEF(autoequip,"ii"),
  18339. BUILDIN_DEF(setbattleflag,"si"),
  18340. BUILDIN_DEF(getbattleflag,"s"),
  18341. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  18342. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  18343. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  18344. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  18345. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  18346. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  18347. BUILDIN_DEF(axtoi,"s"),
  18348. BUILDIN_DEF(query_sql,"s*"),
  18349. BUILDIN_DEF(query_logsql,"s*"),
  18350. BUILDIN_DEF(escape_sql,"v"),
  18351. BUILDIN_DEF(atoi,"s"),
  18352. BUILDIN_DEF(strtol,"si"),
  18353. // [zBuffer] List of player cont commands --->
  18354. BUILDIN_DEF(rid2name,"i"),
  18355. BUILDIN_DEF(pcfollow,"ii"),
  18356. BUILDIN_DEF(pcstopfollow,"i"),
  18357. BUILDIN_DEF(pcblockmove,"ii"),
  18358. // <--- [zBuffer] List of player cont commands
  18359. // [zBuffer] List of unit control commands --->
  18360. BUILDIN_DEF(getunittype,"i"),
  18361. BUILDIN_DEF(getunitname,"i"),
  18362. BUILDIN_DEF(setunitname,"is"),
  18363. BUILDIN_DEF(getunitdata,"i*"),
  18364. BUILDIN_DEF(setunitdata,"iii"),
  18365. BUILDIN_DEF(unitwalk,"iii?"),
  18366. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  18367. BUILDIN_DEF(unitkill,"i"),
  18368. BUILDIN_DEF(unitwarp,"isii"),
  18369. BUILDIN_DEF(unitattack,"iv?"),
  18370. BUILDIN_DEF(unitstopattack,"i"),
  18371. BUILDIN_DEF(unitstopwalk,"i"),
  18372. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  18373. BUILDIN_DEF(unittalk,"is"),
  18374. BUILDIN_DEF(unitemote,"ii"),
  18375. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  18376. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  18377. // <--- [zBuffer] List of unit control commands
  18378. BUILDIN_DEF(sleep,"i"),
  18379. BUILDIN_DEF(sleep2,"i"),
  18380. BUILDIN_DEF(awake,"s"),
  18381. BUILDIN_DEF(getvariableofnpc,"rs"),
  18382. BUILDIN_DEF(warpportal,"iisii"),
  18383. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  18384. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  18385. BUILDIN_DEF(morphembryo,""),
  18386. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  18387. BUILDIN_DEF(checkhomcall,""),
  18388. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  18389. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  18390. BUILDIN_DEF(checkvending,"?"),
  18391. BUILDIN_DEF(checkchatting,"?"),
  18392. BUILDIN_DEF(checkidle,"?"),
  18393. BUILDIN_DEF(openmail,"?"),
  18394. BUILDIN_DEF(openauction,"?"),
  18395. BUILDIN_DEF(checkcell,"siii"),
  18396. BUILDIN_DEF(setcell,"siiiiii"),
  18397. BUILDIN_DEF(setwall,"siiiiis"),
  18398. BUILDIN_DEF(delwall,"s"),
  18399. BUILDIN_DEF(searchitem,"rs"),
  18400. BUILDIN_DEF(mercenary_create,"ii"),
  18401. BUILDIN_DEF(mercenary_heal,"ii"),
  18402. BUILDIN_DEF(mercenary_sc_start,"iii"),
  18403. BUILDIN_DEF(mercenary_get_calls,"i"),
  18404. BUILDIN_DEF(mercenary_get_faith,"i"),
  18405. BUILDIN_DEF(mercenary_set_calls,"ii"),
  18406. BUILDIN_DEF(mercenary_set_faith,"ii"),
  18407. BUILDIN_DEF(readbook,"ii"),
  18408. BUILDIN_DEF(setfont,"i"),
  18409. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  18410. BUILDIN_DEF(progressbar,"si"),
  18411. BUILDIN_DEF(pushpc,"ii"),
  18412. BUILDIN_DEF(buyingstore,"i"),
  18413. BUILDIN_DEF(searchstores,"ii"),
  18414. BUILDIN_DEF(showdigit,"i?"),
  18415. // WoE SE
  18416. BUILDIN_DEF(agitstart2,""),
  18417. BUILDIN_DEF(agitend2,""),
  18418. BUILDIN_DEF(agitcheck2,""),
  18419. // BattleGround
  18420. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  18421. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  18422. BUILDIN_DEF(bg_team_setxy,"iii"),
  18423. BUILDIN_DEF(bg_warp,"isii"),
  18424. BUILDIN_DEF(bg_monster,"isiisi?"),
  18425. BUILDIN_DEF(bg_monster_set_team,"ii"),
  18426. BUILDIN_DEF(bg_leave,""),
  18427. BUILDIN_DEF(bg_destroy,"i"),
  18428. BUILDIN_DEF(areapercentheal,"siiiiii"),
  18429. BUILDIN_DEF(bg_get_data,"ii"),
  18430. BUILDIN_DEF(bg_getareausers,"isiiii"),
  18431. BUILDIN_DEF(bg_updatescore,"sii"),
  18432. // Instancing
  18433. BUILDIN_DEF(instance_create,"s"),
  18434. BUILDIN_DEF(instance_destroy,"?"),
  18435. BUILDIN_DEF(instance_id,""),
  18436. BUILDIN_DEF(instance_enter,"s??"),
  18437. BUILDIN_DEF(instance_npcname,"s?"),
  18438. BUILDIN_DEF(instance_mapname,"s?"),
  18439. BUILDIN_DEF(instance_warpall,"sii?"),
  18440. BUILDIN_DEF(instance_announce,"isi?????"),
  18441. BUILDIN_DEF(instance_check_party,"i???"),
  18442. /**
  18443. * 3rd-related
  18444. **/
  18445. BUILDIN_DEF(makerune,"i?"),
  18446. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  18447. BUILDIN_DEF(setdragon,"??"),//[Ind]
  18448. BUILDIN_DEF(ismounting,"?"),//[Ind]
  18449. BUILDIN_DEF(setmounting,"?"),//[Ind]
  18450. BUILDIN_DEF(checkre,"i"),
  18451. /**
  18452. * rAthena and beyond!
  18453. **/
  18454. BUILDIN_DEF(getargcount,""),
  18455. BUILDIN_DEF(getcharip,"?"),
  18456. BUILDIN_DEF(is_function,"s"),
  18457. BUILDIN_DEF(get_revision,""),
  18458. BUILDIN_DEF(get_githash,""),
  18459. BUILDIN_DEF(freeloop,"?"),
  18460. BUILDIN_DEF(getrandgroupitem,"i??"),
  18461. BUILDIN_DEF(cleanmap,"s"),
  18462. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  18463. BUILDIN_DEF(npcskill,"viii"),
  18464. BUILDIN_DEF(consumeitem,"v?"),
  18465. BUILDIN_DEF(delequip,"i?"),
  18466. BUILDIN_DEF(breakequip,"i?"),
  18467. BUILDIN_DEF(sit,"?"),
  18468. BUILDIN_DEF(stand,"?"),
  18469. //@commands (script based)
  18470. BUILDIN_DEF(bindatcmd, "ss??"),
  18471. BUILDIN_DEF(unbindatcmd, "s"),
  18472. BUILDIN_DEF(useatcmd, "s"),
  18473. //Quest Log System [Inkfish]
  18474. BUILDIN_DEF(questinfo, "ii??"),
  18475. BUILDIN_DEF(setquest, "i?"),
  18476. BUILDIN_DEF(erasequest, "i?"),
  18477. BUILDIN_DEF(completequest, "i?"),
  18478. BUILDIN_DEF(checkquest, "i??"),
  18479. BUILDIN_DEF(isbegin_quest,"i?"),
  18480. BUILDIN_DEF(changequest, "ii?"),
  18481. BUILDIN_DEF(showevent, "i??"),
  18482. //Bound items [Xantara] & [Akinari]
  18483. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  18484. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  18485. BUILDIN_DEF(countbound, "??"),
  18486. // Party related
  18487. BUILDIN_DEF(party_create,"s???"),
  18488. BUILDIN_DEF(party_addmember,"ii"),
  18489. BUILDIN_DEF(party_delmember,"??"),
  18490. BUILDIN_DEF(party_changeleader,"ii"),
  18491. BUILDIN_DEF(party_changeoption,"iii"),
  18492. BUILDIN_DEF(party_destroy,"i"),
  18493. BUILDIN_DEF(is_clientver,"ii?"),
  18494. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  18495. BUILDIN_DEF(vip_status,"i?"),
  18496. BUILDIN_DEF(vip_time,"i?"),
  18497. BUILDIN_DEF(bonus_script,"si????"),
  18498. BUILDIN_DEF(bonus_script_clear,"??"),
  18499. BUILDIN_DEF(getgroupitem,"i"),
  18500. BUILDIN_DEF(enable_command,""),
  18501. BUILDIN_DEF(disable_command,""),
  18502. BUILDIN_DEF(getguildmember,"i??"),
  18503. BUILDIN_DEF(addspiritball,"ii?"),
  18504. BUILDIN_DEF(delspiritball,"i?"),
  18505. BUILDIN_DEF(countspiritball,"?"),
  18506. BUILDIN_DEF(mergeitem,"?"),
  18507. BUILDIN_DEF(mergeitem2,"??"),
  18508. BUILDIN_DEF(npcshopupdate,"sii?"),
  18509. BUILDIN_DEF(getattachedrid,""),
  18510. BUILDIN_DEF(getvar,"vi"),
  18511. BUILDIN_DEF(showscript,"s?"),
  18512. BUILDIN_DEF(ignoretimeout,"i?"),
  18513. #include "../custom/script_def.inc"
  18514. {NULL,NULL,NULL},
  18515. };