script.c 493 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430
  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 "status.h"
  25. #include "storage.h"
  26. #include "mob.h"
  27. #include "npc.h"
  28. #include "pet.h"
  29. #include "mapreg.h"
  30. #include "homunculus.h"
  31. #include "instance.h"
  32. #include "mercenary.h"
  33. #include "intif.h"
  34. #include "skill.h"
  35. #include "status.h"
  36. #include "chat.h"
  37. #include "battle.h"
  38. #include "battleground.h"
  39. #include "party.h"
  40. #include "guild.h"
  41. #include "atcommand.h"
  42. #include "log.h"
  43. #include "unit.h"
  44. #include "pet.h"
  45. #include "mail.h"
  46. #include "script.h"
  47. #include "quest.h"
  48. #include "elemental.h"
  49. #include "../config/core.h"
  50. #ifdef PCRE_SUPPORT
  51. #include "../../3rdparty/pcre/include/pcre.h" // preg_match
  52. #endif
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <math.h>
  57. #ifndef WIN32
  58. #include <sys/time.h>
  59. #endif
  60. #include <time.h>
  61. #include <setjmp.h>
  62. #include <errno.h>
  63. #ifdef BETA_THREAD_TEST
  64. #include "../common/atomic.h"
  65. #include "../common/spinlock.h"
  66. #include "../common/thread.h"
  67. #include "../common/mutex.h"
  68. #endif
  69. ///////////////////////////////////////////////////////////////////////////////
  70. //## TODO possible enhancements: [FlavioJS]
  71. // - 'callfunc' supporting labels in the current npc "::LabelName"
  72. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  73. // - 'function FuncName;' function declarations reverting to global functions
  74. // if local label isn't found
  75. // - join callfunc and callsub's functionality
  76. // - remove dynamic allocation in add_word()
  77. // - remove GETVALUE / SETVALUE
  78. // - clean up the set_reg / set_val / setd_sub mess
  79. // - detect invalid label references at parse-time
  80. //
  81. // struct script_state* st;
  82. //
  83. /// Returns the script_data at the target index
  84. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  85. /// Returns if the stack contains data at the target index
  86. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  87. /// Returns the index of the last data in the stack
  88. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  89. /// Pushes an int into the stack
  90. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  91. /// Pushes a string into the stack (script engine frees it automatically)
  92. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  93. /// Pushes a copy of a string into the stack
  94. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  95. /// Pushes a constant string into the stack (must never change or be freed)
  96. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  97. /// Pushes a nil into the stack
  98. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  99. /// Pushes a copy of the data in the target index
  100. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  101. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  102. #define script_isint(st,i) data_isint(script_getdata(st,i))
  103. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  104. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  105. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  106. // Returns name of currently running function
  107. #define script_getfuncname(st) ( st->funcname )
  108. // Note: "top" functions/defines use indexes relative to the top of the stack
  109. // -1 is the index of the data at the top
  110. /// Returns the script_data at the target index relative to the top of the stack
  111. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  112. /// Pushes a copy of the data in the target index relative to the top of the stack
  113. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  114. /// Removes the range of values [start,end[ relative to the top of the stack
  115. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  116. //
  117. // struct script_data* data;
  118. //
  119. /// Returns if the script data is a string
  120. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  121. /// Returns if the script data is an int
  122. #define data_isint(data) ( (data)->type == C_INT )
  123. /// Returns if the script data is a reference
  124. #define data_isreference(data) ( (data)->type == C_NAME )
  125. /// Returns if the script data is a label
  126. #define data_islabel(data) ( (data)->type == C_POS )
  127. /// Returns if the script data is an internal script function label
  128. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  129. /// Returns if this is a reference to a constant
  130. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  131. /// Returns if this a reference to a param
  132. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  133. /// Returns if this a reference to a variable
  134. //##TODO confirm it's C_NAME [FlavioJS]
  135. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  136. /// Returns the unique id of the reference (id and index)
  137. #define reference_getuid(data) ( (data)->u.num )
  138. /// Returns the id of the reference
  139. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  140. /// Returns the array index of the reference
  141. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  142. /// Returns the name of the reference
  143. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  144. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  145. #define reference_getref(data) ( (data)->ref )
  146. /// Returns the value of the constant
  147. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  148. /// Returns the type of param
  149. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  150. /// Composes the uid of a reference from the id and the index
  151. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  152. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  153. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  154. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  155. #define FETCH(n, t) \
  156. if( script_hasdata(st,n) ) \
  157. (t)=script_getnum(st,n);
  158. /// Maximum amount of elements in script arrays
  159. #define SCRIPT_MAX_ARRAYSIZE 128
  160. #define SCRIPT_CMD_SUCCESS 0 ///when a buildin cmd was correctly done
  161. #define SCRIPT_CMD_FAILURE 1 ///when an errors appear in cmd, show_debug will follow
  162. #define SCRIPT_BLOCK_SIZE 512
  163. enum { LABEL_NEXTLINE=1,LABEL_START };
  164. /// temporary buffer for passing around compiled bytecode
  165. /// @see add_scriptb, set_label, parse_script
  166. static unsigned char* script_buf = NULL;
  167. static int script_pos = 0, script_size = 0;
  168. static inline int GETVALUE(const unsigned char* buf, int i)
  169. {
  170. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  171. }
  172. static inline void SETVALUE(unsigned char* buf, int i, int n)
  173. {
  174. buf[i] = GetByte(n, 0);
  175. buf[i+1] = GetByte(n, 1);
  176. buf[i+2] = GetByte(n, 2);
  177. }
  178. // String buffer structures.
  179. // str_data stores string information
  180. static struct str_data_struct {
  181. enum c_op type;
  182. int str;
  183. int backpatch;
  184. int label;
  185. int (*func)(struct script_state *st);
  186. int val;
  187. int next;
  188. } *str_data = NULL;
  189. static int str_data_size = 0; // size of the data
  190. static int str_num = LABEL_START; // next id to be assigned
  191. // str_buf holds the strings themselves
  192. static char *str_buf;
  193. static int str_size = 0; // size of the buffer
  194. static int str_pos = 0; // next position to be assigned
  195. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  196. #define SCRIPT_HASH_SIZE 1021
  197. int str_hash[SCRIPT_HASH_SIZE];
  198. // Specifies which string hashing method to use
  199. //#define SCRIPT_HASH_DJB2
  200. //#define SCRIPT_HASH_SDBM
  201. #define SCRIPT_HASH_ELF
  202. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  203. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  204. static int parse_options=0;
  205. DBMap* script_get_label_db(void){ return scriptlabel_db; }
  206. DBMap* script_get_userfunc_db(void){ return userfunc_db; }
  207. // important buildin function references for usage in scripts
  208. static int buildin_set_ref = 0;
  209. static int buildin_callsub_ref = 0;
  210. static int buildin_callfunc_ref = 0;
  211. static int buildin_getelementofarray_ref = 0;
  212. // Caches compiled autoscript item code.
  213. // Note: This is not cleared when reloading itemdb.
  214. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  215. struct Script_Config script_config = {
  216. 1, // warn_func_mismatch_argtypes
  217. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  218. 0, INT_MAX, // input_min_value/input_max_value
  219. "OnPCDieEvent", //die_event_name
  220. "OnPCKillEvent", //kill_pc_event_name
  221. "OnNPCKillEvent", //kill_mob_event_name
  222. "OnPCLoginEvent", //login_event_name
  223. "OnPCLogoutEvent", //logout_event_name
  224. "OnPCLoadMapEvent", //loadmap_event_name
  225. "OnPCBaseLvUpEvent", //baselvup_event_name
  226. "OnPCJobLvUpEvent", //joblvup_event_name
  227. "OnPCStatCalcEvent", //stat_calc_event_name
  228. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  229. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  230. };
  231. static jmp_buf error_jump;
  232. static char* error_msg;
  233. static const char* error_pos;
  234. static int error_report; // if the error should produce output
  235. // Used by disp_warning_message
  236. static const char* parser_current_src;
  237. static const char* parser_current_file;
  238. static int parser_current_line;
  239. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  240. // [Eoe / jA 1080, 1081, 1094, 1164]
  241. enum curly_type {
  242. TYPE_NULL = 0,
  243. TYPE_IF,
  244. TYPE_SWITCH,
  245. TYPE_WHILE,
  246. TYPE_FOR,
  247. TYPE_DO,
  248. TYPE_USERFUNC,
  249. TYPE_ARGLIST // function argument list
  250. };
  251. enum e_arglist
  252. {
  253. ARGLIST_UNDEFINED = 0,
  254. ARGLIST_NO_PAREN = 1,
  255. ARGLIST_PAREN = 2,
  256. };
  257. static struct {
  258. struct {
  259. enum curly_type type;
  260. int index;
  261. int count;
  262. int flag;
  263. struct linkdb_node *case_label;
  264. } curly[256]; // Information right parenthesis
  265. int curly_count; // The number of right brackets
  266. int index; // Number of the syntax used in the script
  267. } syntax;
  268. const char* parse_curly_close(const char* p);
  269. const char* parse_syntax_close(const char* p);
  270. const char* parse_syntax_close_sub(const char* p,int* flag);
  271. const char* parse_syntax(const char* p);
  272. static int parse_syntax_for_flag = 0;
  273. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  274. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  275. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  276. int potion_target=0;
  277. c_op get_com(unsigned char *script,int *pos);
  278. int get_num(unsigned char *script,int *pos);
  279. typedef struct script_function {
  280. int (*func)(struct script_state *st);
  281. const char *name;
  282. const char *arg;
  283. } script_function;
  284. extern script_function buildin_func[];
  285. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  286. #ifdef BETA_THREAD_TEST
  287. /**
  288. * MySQL Query Slave
  289. **/
  290. static SPIN_LOCK queryThreadLock;
  291. static rAthread queryThread = NULL;
  292. static ramutex queryThreadMutex = NULL;
  293. static racond queryThreadCond = NULL;
  294. static volatile int32 queryThreadTerminate = 0;
  295. struct queryThreadEntry {
  296. bool ok;
  297. bool type; /* main db or log db? */
  298. struct script_state *st;
  299. };
  300. /* Ladies and Gentleman the Manager! */
  301. struct {
  302. struct queryThreadEntry **entry;/* array of structs */
  303. int count;
  304. int timer;/* used to receive processed entries */
  305. } queryThreadData;
  306. #endif
  307. /*==========================================
  308. * (Only those needed) local declaration prototype
  309. *------------------------------------------*/
  310. const char* parse_subexpr(const char* p,int limit);
  311. int run_func(struct script_state *st);
  312. int script_instancegetid(struct script_state *st);
  313. enum {
  314. MF_NOMEMO, //0
  315. MF_NOTELEPORT,
  316. MF_NOSAVE,
  317. MF_NOBRANCH,
  318. MF_NOPENALTY,
  319. MF_NOZENYPENALTY,
  320. MF_PVP,
  321. MF_PVP_NOPARTY,
  322. MF_PVP_NOGUILD,
  323. MF_GVG,
  324. MF_GVG_NOPARTY, //10
  325. MF_NOTRADE,
  326. MF_NOSKILL,
  327. MF_NOWARP,
  328. MF_PARTYLOCK,
  329. MF_NOICEWALL,
  330. MF_SNOW,
  331. MF_FOG,
  332. MF_SAKURA,
  333. MF_LEAVES,
  334. /**
  335. * No longer available, keeping here just in case it's back someday. [Ind]
  336. **/
  337. //MF_RAIN, //20
  338. // 21 free
  339. MF_NOGO = 22,
  340. MF_CLOUDS,
  341. MF_CLOUDS2,
  342. MF_FIREWORKS,
  343. MF_GVG_CASTLE,
  344. MF_GVG_DUNGEON,
  345. MF_NIGHTENABLED,
  346. MF_NOBASEEXP,
  347. MF_NOJOBEXP, //30
  348. MF_NOMOBLOOT,
  349. MF_NOMVPLOOT,
  350. MF_NORETURN,
  351. MF_NOWARPTO,
  352. MF_NIGHTMAREDROP,
  353. MF_RESTRICTED,
  354. MF_NOCOMMAND,
  355. MF_NODROP,
  356. MF_JEXP,
  357. MF_BEXP, //40
  358. MF_NOVENDING,
  359. MF_LOADEVENT,
  360. MF_NOCHAT,
  361. MF_NOEXPPENALTY,
  362. MF_GUILDLOCK,
  363. MF_TOWN,
  364. MF_AUTOTRADE,
  365. MF_ALLOWKS,
  366. MF_MONSTER_NOTELEPORT,
  367. MF_PVP_NOCALCRANK, //50
  368. MF_BATTLEGROUND,
  369. MF_RESET,
  370. MF_CHANNELAUTOJOIN,
  371. MF_NOUSECART,
  372. MF_NOITEMCONSUMPTION,
  373. MF_SUMSTARTMIRACLE,
  374. MF_NOMINEEFFECT,
  375. MF_NOLOCKON,
  376. MF_NOTOMB,
  377. MF_SKILL_DAMAGE //60
  378. };
  379. const char* script_op2name(int op)
  380. {
  381. #define RETURN_OP_NAME(type) case type: return #type
  382. switch( op )
  383. {
  384. RETURN_OP_NAME(C_NOP);
  385. RETURN_OP_NAME(C_POS);
  386. RETURN_OP_NAME(C_INT);
  387. RETURN_OP_NAME(C_PARAM);
  388. RETURN_OP_NAME(C_FUNC);
  389. RETURN_OP_NAME(C_STR);
  390. RETURN_OP_NAME(C_CONSTSTR);
  391. RETURN_OP_NAME(C_ARG);
  392. RETURN_OP_NAME(C_NAME);
  393. RETURN_OP_NAME(C_EOL);
  394. RETURN_OP_NAME(C_RETINFO);
  395. RETURN_OP_NAME(C_USERFUNC);
  396. RETURN_OP_NAME(C_USERFUNC_POS);
  397. // operators
  398. RETURN_OP_NAME(C_OP3);
  399. RETURN_OP_NAME(C_LOR);
  400. RETURN_OP_NAME(C_LAND);
  401. RETURN_OP_NAME(C_LE);
  402. RETURN_OP_NAME(C_LT);
  403. RETURN_OP_NAME(C_GE);
  404. RETURN_OP_NAME(C_GT);
  405. RETURN_OP_NAME(C_EQ);
  406. RETURN_OP_NAME(C_NE);
  407. RETURN_OP_NAME(C_XOR);
  408. RETURN_OP_NAME(C_OR);
  409. RETURN_OP_NAME(C_AND);
  410. RETURN_OP_NAME(C_ADD);
  411. RETURN_OP_NAME(C_SUB);
  412. RETURN_OP_NAME(C_MUL);
  413. RETURN_OP_NAME(C_DIV);
  414. RETURN_OP_NAME(C_MOD);
  415. RETURN_OP_NAME(C_NEG);
  416. RETURN_OP_NAME(C_LNOT);
  417. RETURN_OP_NAME(C_NOT);
  418. RETURN_OP_NAME(C_R_SHIFT);
  419. RETURN_OP_NAME(C_L_SHIFT);
  420. default:
  421. ShowDebug("script_op2name: unexpected op=%d\n", op);
  422. return "???";
  423. }
  424. #undef RETURN_OP_NAME
  425. }
  426. #ifdef DEBUG_DUMP_STACK
  427. static void script_dump_stack(struct script_state* st)
  428. {
  429. int i;
  430. ShowMessage("\tstart = %d\n", st->start);
  431. ShowMessage("\tend = %d\n", st->end);
  432. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  433. ShowMessage("\tsp = %d\n", st->stack->sp);
  434. for( i = 0; i < st->stack->sp; ++i )
  435. {
  436. struct script_data* data = &st->stack->stack_data[i];
  437. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  438. switch( data->type )
  439. {
  440. case C_INT:
  441. case C_POS:
  442. ShowMessage(" %d\n", data->u.num);
  443. break;
  444. case C_STR:
  445. case C_CONSTSTR:
  446. ShowMessage(" \"%s\"\n", data->u.str);
  447. break;
  448. case C_NAME:
  449. 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));
  450. break;
  451. case C_RETINFO:
  452. {
  453. struct script_retinfo* ri = data->u.ri;
  454. ShowMessage(" %p {var_function=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->var_function, ri->script, ri->pos, ri->nargs, ri->defsp);
  455. }
  456. break;
  457. default:
  458. ShowMessage("\n");
  459. break;
  460. }
  461. }
  462. }
  463. #endif
  464. /// Reports on the console the src of a script error.
  465. static void script_reportsrc(struct script_state *st)
  466. {
  467. struct block_list* bl;
  468. if( st->oid == 0 )
  469. return; //Can't report source.
  470. bl = map_id2bl(st->oid);
  471. if( bl == NULL )
  472. return;
  473. switch( bl->type )
  474. {
  475. case BL_NPC:
  476. if( bl->m >= 0 )
  477. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  478. else
  479. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  480. break;
  481. default:
  482. if( bl->m >= 0 )
  483. 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);
  484. else
  485. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  486. break;
  487. }
  488. }
  489. /// Reports on the console information about the script data.
  490. static void script_reportdata(struct script_data* data)
  491. {
  492. if( data == NULL )
  493. return;
  494. switch( data->type )
  495. {
  496. case C_NOP:// no value
  497. ShowDebug("Data: nothing (nil)\n");
  498. break;
  499. case C_INT:// number
  500. ShowDebug("Data: number value=%d\n", data->u.num);
  501. break;
  502. case C_STR:
  503. case C_CONSTSTR:// string
  504. if( data->u.str )
  505. {
  506. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  507. }
  508. else
  509. {
  510. ShowDebug("Data: string value=NULL\n");
  511. }
  512. break;
  513. case C_NAME:// reference
  514. if( reference_tovariable(data) )
  515. {// variable
  516. const char* name = reference_getname(data);
  517. if( not_array_variable(*name) )
  518. ShowDebug("Data: variable name='%s'\n", name);
  519. else
  520. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  521. }
  522. else if( reference_toconstant(data) )
  523. {// constant
  524. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  525. }
  526. else if( reference_toparam(data) )
  527. {// param
  528. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  529. }
  530. else
  531. {// ???
  532. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  533. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  534. }
  535. break;
  536. case C_POS:// label
  537. ShowDebug("Data: label pos=%d\n", data->u.num);
  538. break;
  539. default:
  540. ShowDebug("Data: %s\n", script_op2name(data->type));
  541. break;
  542. }
  543. }
  544. /// Reports on the console information about the current built-in function.
  545. static void script_reportfunc(struct script_state* st)
  546. {
  547. int i, params, id;
  548. struct script_data* data;
  549. if( !script_hasdata(st,0) )
  550. {// no stack
  551. return;
  552. }
  553. data = script_getdata(st,0);
  554. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  555. {// script currently not executing a built-in function or corrupt stack
  556. return;
  557. }
  558. id = reference_getid(data);
  559. params = script_lastdata(st)-1;
  560. if( params > 0 )
  561. {
  562. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  563. for( i = 2; i <= script_lastdata(st); i++ )
  564. {
  565. script_reportdata(script_getdata(st,i));
  566. }
  567. }
  568. else
  569. {
  570. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  571. }
  572. }
  573. /*==========================================
  574. * Output error message
  575. *------------------------------------------*/
  576. static void disp_error_message2(const char *mes,const char *pos,int report)
  577. {
  578. error_msg = aStrdup(mes);
  579. error_pos = pos;
  580. error_report = report;
  581. longjmp( error_jump, 1 );
  582. }
  583. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  584. static void disp_warning_message(const char *mes, const char *pos) {
  585. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  586. }
  587. /// Checks event parameter validity
  588. static void check_event(struct script_state *st, const char *evt)
  589. {
  590. if( evt && evt[0] && !stristr(evt, "::On") )
  591. {
  592. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  593. script_reportsrc(st);
  594. }
  595. }
  596. /*==========================================
  597. * Hashes the input string
  598. *------------------------------------------*/
  599. static unsigned int calc_hash(const char* p)
  600. {
  601. unsigned int h;
  602. #if defined(SCRIPT_HASH_DJB2)
  603. h = 5381;
  604. while( *p ) // hash*33 + c
  605. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  606. #elif defined(SCRIPT_HASH_SDBM)
  607. h = 0;
  608. while( *p ) // hash*65599 + c
  609. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  610. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  611. h = 0;
  612. while( *p ){
  613. unsigned int g;
  614. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  615. g = h & 0xF0000000;
  616. if( g )
  617. {
  618. h ^= g >> 24;
  619. h &= ~g;
  620. }
  621. }
  622. #else // athena hash
  623. h = 0;
  624. while( *p )
  625. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  626. #endif
  627. return h % SCRIPT_HASH_SIZE;
  628. }
  629. /*==========================================
  630. * str_data manipulation functions
  631. *------------------------------------------*/
  632. /// Looks up string using the provided id.
  633. const char* get_str(int id)
  634. {
  635. Assert( id >= LABEL_START && id < str_size );
  636. return str_buf+str_data[id].str;
  637. }
  638. /// Returns the uid of the string, or -1.
  639. static int search_str(const char* p)
  640. {
  641. int i;
  642. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  643. if( strcasecmp(get_str(i),p) == 0 )
  644. return i;
  645. return -1;
  646. }
  647. /// Stores a copy of the string and returns its id.
  648. /// If an identical string is already present, returns its id instead.
  649. int add_str(const char* p)
  650. {
  651. int i, h;
  652. int len;
  653. h = calc_hash(p);
  654. if( str_hash[h] == 0 )
  655. {// empty bucket, add new node here
  656. str_hash[h] = str_num;
  657. }
  658. else
  659. {// scan for end of list, or occurence of identical string
  660. for( i = str_hash[h]; ; i = str_data[i].next )
  661. {
  662. if( strcasecmp(get_str(i),p) == 0 )
  663. return i; // string already in list
  664. if( str_data[i].next == 0 )
  665. break; // reached the end
  666. }
  667. // append node to end of list
  668. str_data[i].next = str_num;
  669. }
  670. // grow list if neccessary
  671. if( str_num >= str_data_size )
  672. {
  673. str_data_size += 128;
  674. RECREATE(str_data,struct str_data_struct,str_data_size);
  675. memset(str_data + (str_data_size - 128), '\0', 128);
  676. }
  677. len=(int)strlen(p);
  678. // grow string buffer if neccessary
  679. while( str_pos+len+1 >= str_size )
  680. {
  681. str_size += 256;
  682. RECREATE(str_buf,char,str_size);
  683. memset(str_buf + (str_size - 256), '\0', 256);
  684. }
  685. safestrncpy(str_buf+str_pos, p, len+1);
  686. str_data[str_num].type = C_NOP;
  687. str_data[str_num].str = str_pos;
  688. str_data[str_num].next = 0;
  689. str_data[str_num].func = NULL;
  690. str_data[str_num].backpatch = -1;
  691. str_data[str_num].label = -1;
  692. str_pos += len+1;
  693. return str_num++;
  694. }
  695. /// Appends 1 byte to the script buffer.
  696. static void add_scriptb(int a)
  697. {
  698. if( script_pos+1 >= script_size )
  699. {
  700. script_size += SCRIPT_BLOCK_SIZE;
  701. RECREATE(script_buf,unsigned char,script_size);
  702. }
  703. script_buf[script_pos++] = (uint8)(a);
  704. }
  705. /// Appends a c_op value to the script buffer.
  706. /// The value is variable-length encoded into 8-bit blocks.
  707. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  708. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  709. static void add_scriptc(int a)
  710. {
  711. while( a >= 0x40 )
  712. {
  713. add_scriptb((a&0x3f)|0x40);
  714. a = (a - 0x40) >> 6;
  715. }
  716. add_scriptb(a);
  717. }
  718. /// Appends an integer value to the script buffer.
  719. /// The value is variable-length encoded into 8-bit blocks.
  720. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  721. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  722. static void add_scripti(int a)
  723. {
  724. while( a >= 0x40 )
  725. {
  726. add_scriptb((a&0x3f)|0xc0);
  727. a = (a - 0x40) >> 6;
  728. }
  729. add_scriptb(a|0x80);
  730. }
  731. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  732. ///
  733. /// @param l The id of the str_data entry
  734. // Maximum up to 16M
  735. static void add_scriptl(int l)
  736. {
  737. int backpatch = str_data[l].backpatch;
  738. switch(str_data[l].type){
  739. case C_POS:
  740. case C_USERFUNC_POS:
  741. add_scriptc(C_POS);
  742. add_scriptb(str_data[l].label);
  743. add_scriptb(str_data[l].label>>8);
  744. add_scriptb(str_data[l].label>>16);
  745. break;
  746. case C_NOP:
  747. case C_USERFUNC:
  748. // Embedded data backpatch there is a possibility of label
  749. add_scriptc(C_NAME);
  750. str_data[l].backpatch = script_pos;
  751. add_scriptb(backpatch);
  752. add_scriptb(backpatch>>8);
  753. add_scriptb(backpatch>>16);
  754. break;
  755. case C_INT:
  756. add_scripti(abs(str_data[l].val));
  757. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  758. add_scriptc(C_NEG);
  759. break;
  760. default: // assume C_NAME
  761. add_scriptc(C_NAME);
  762. add_scriptb(l);
  763. add_scriptb(l>>8);
  764. add_scriptb(l>>16);
  765. break;
  766. }
  767. }
  768. /*==========================================
  769. * Resolve the label
  770. *------------------------------------------*/
  771. void set_label(int l,int pos, const char* script_pos)
  772. {
  773. int i,next;
  774. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  775. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  776. disp_error_message("set_label: invalid label name",script_pos);
  777. return;
  778. }
  779. if(str_data[l].label!=-1){
  780. disp_error_message("set_label: dup label ",script_pos);
  781. return;
  782. }
  783. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  784. str_data[l].label=pos;
  785. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  786. next=GETVALUE(script_buf,i);
  787. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  788. SETVALUE(script_buf,i,pos);
  789. i=next;
  790. }
  791. }
  792. /// Skips spaces and/or comments.
  793. const char* skip_space(const char* p)
  794. {
  795. if( p == NULL )
  796. return NULL;
  797. for(;;)
  798. {
  799. while( ISSPACE(*p) )
  800. ++p;
  801. if( *p == '/' && p[1] == '/' )
  802. {// line comment
  803. while(*p && *p!='\n')
  804. ++p;
  805. }
  806. else if( *p == '/' && p[1] == '*' )
  807. {// block comment
  808. p += 2;
  809. for(;;)
  810. {
  811. if( *p == '\0' ) {
  812. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  813. return p;
  814. }
  815. if( *p == '*' && p[1] == '/' )
  816. {// end of block comment
  817. p += 2;
  818. break;
  819. }
  820. ++p;
  821. }
  822. }
  823. else
  824. break;
  825. }
  826. return p;
  827. }
  828. /// Skips a word.
  829. /// A word consists of undercores and/or alphanumeric characters,
  830. /// and valid variable prefixes/postfixes.
  831. static
  832. const char* skip_word(const char* p)
  833. {
  834. // prefix
  835. switch( *p )
  836. {
  837. case '@':// temporary char variable
  838. ++p; break;
  839. case '#':// account variable
  840. p += ( p[1] == '#' ? 2 : 1 ); break;
  841. case '\'':// instance variable
  842. ++p; break;
  843. case '.':// npc variable
  844. p += ( p[1] == '@' ? 2 : 1 ); break;
  845. case '$':// global variable
  846. p += ( p[1] == '@' ? 2 : 1 ); break;
  847. }
  848. while( ISALNUM(*p) || *p == '_' )
  849. ++p;
  850. // postfix
  851. if( *p == '$' )// string
  852. p++;
  853. return p;
  854. }
  855. /// Adds a word to str_data.
  856. /// @see skip_word
  857. /// @see add_str
  858. static
  859. int add_word(const char* p)
  860. {
  861. char* word;
  862. int len;
  863. int i;
  864. // Check for a word
  865. len = skip_word(p) - p;
  866. if( len == 0 )
  867. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  868. // Duplicate the word
  869. word = (char*)aMalloc(len+1);
  870. memcpy(word, p, len);
  871. word[len] = 0;
  872. // add the word
  873. i = add_str(word);
  874. aFree(word);
  875. return i;
  876. }
  877. /// Parses a function call.
  878. /// The argument list can have parenthesis or not.
  879. /// The number of arguments is checked.
  880. static
  881. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  882. {
  883. const char* p2;
  884. const char* arg=NULL;
  885. int func;
  886. func = add_word(p);
  887. if( str_data[func].type == C_FUNC ){
  888. // buildin function
  889. add_scriptl(func);
  890. add_scriptc(C_ARG);
  891. arg = buildin_func[str_data[func].val].arg;
  892. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  893. // script defined function
  894. add_scriptl(buildin_callsub_ref);
  895. add_scriptc(C_ARG);
  896. add_scriptl(func);
  897. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  898. if( *arg == 0 )
  899. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  900. if( *arg != '*' )
  901. ++arg; // count func as argument
  902. } else {
  903. #ifdef SCRIPT_CALLFUNC_CHECK
  904. const char* name = get_str(func);
  905. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  906. #endif
  907. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  908. #ifdef SCRIPT_CALLFUNC_CHECK
  909. } else {;
  910. add_scriptl(buildin_callfunc_ref);
  911. add_scriptc(C_ARG);
  912. add_scriptc(C_STR);
  913. while( *name ) add_scriptb(*name ++);
  914. add_scriptb(0);
  915. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  916. if( *arg != '*' ) ++ arg;
  917. }
  918. #endif
  919. }
  920. p = skip_word(p);
  921. p = skip_space(p);
  922. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  923. syntax.curly[syntax.curly_count].count = 0;
  924. if( *p == ';' )
  925. {// <func name> ';'
  926. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  927. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  928. {// <func name> '(' ')'
  929. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  930. p = p2;
  931. /*
  932. } else if( 0 && require_paren && *p != '(' )
  933. {// <func name>
  934. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  935. */
  936. } else
  937. {// <func name> <arg list>
  938. if( require_paren ){
  939. if( *p != '(' )
  940. disp_error_message("need '('",p);
  941. ++p; // skip '('
  942. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  943. } else if( *p == '(' ){
  944. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  945. } else {
  946. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  947. }
  948. ++syntax.curly_count;
  949. while( *arg ) {
  950. p2=parse_subexpr(p,-1);
  951. if( p == p2 )
  952. break; // not an argument
  953. if( *arg != '*' )
  954. ++arg; // next argument
  955. p=skip_space(p2);
  956. if( *arg == 0 || *p != ',' )
  957. break; // no more arguments
  958. ++p; // skip comma
  959. }
  960. --syntax.curly_count;
  961. }
  962. if( *arg && *arg != '?' && *arg != '*' )
  963. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  964. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  965. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  966. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  967. if( *p != ')' )
  968. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  969. ++p;
  970. }
  971. add_scriptc(C_FUNC);
  972. return p;
  973. }
  974. /// Processes end of logical script line.
  975. /// @param first When true, only fix up scheduling data is initialized
  976. /// @param p Script position for error reporting in set_label
  977. static void parse_nextline(bool first, const char* p)
  978. {
  979. if( !first )
  980. {
  981. add_scriptc(C_EOL); // mark end of line for stack cleanup
  982. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  983. }
  984. // initialize data for new '-' label fix up scheduling
  985. str_data[LABEL_NEXTLINE].type = C_NOP;
  986. str_data[LABEL_NEXTLINE].backpatch = -1;
  987. str_data[LABEL_NEXTLINE].label = -1;
  988. }
  989. /// Parse a variable assignment using the direct equals operator
  990. /// @param p script position where the function should run from
  991. /// @return NULL if not a variable assignment, the new position otherwise
  992. const char* parse_variable(const char* p) {
  993. int i, j, word;
  994. c_op type = C_NOP;
  995. const char *p2 = NULL;
  996. const char *var = p;
  997. // skip the variable where applicable
  998. p = skip_word(p);
  999. p = skip_space(p);
  1000. if( p == NULL ) {// end of the line or invalid buffer
  1001. return NULL;
  1002. }
  1003. if( *p == '[' ) {// array variable so process the array as appropriate
  1004. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1005. if( *p ++ == ']' && --(j) == 0 ) break;
  1006. if( *p == '[' ) ++ j;
  1007. }
  1008. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1009. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1010. }
  1011. }
  1012. if( type == C_NOP &&
  1013. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1014. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1015. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1016. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1017. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1018. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1019. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1020. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1021. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1022. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1023. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  1024. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  1025. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1026. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1027. ) )
  1028. {// failed to find a matching operator combination so invalid
  1029. return NULL;
  1030. }
  1031. switch( type ) {
  1032. case C_EQ: {// incremental modifier
  1033. p = skip_space( &p[1] );
  1034. }
  1035. break;
  1036. case C_L_SHIFT:
  1037. case C_R_SHIFT: {// left or right shift modifier
  1038. p = skip_space( &p[3] );
  1039. }
  1040. break;
  1041. default: {// normal incremental command
  1042. p = skip_space( &p[2] );
  1043. }
  1044. }
  1045. if( p == NULL ) {// end of line or invalid buffer
  1046. return NULL;
  1047. }
  1048. // push the set function onto the stack
  1049. add_scriptl(buildin_set_ref);
  1050. add_scriptc(C_ARG);
  1051. // always append parenthesis to avoid errors
  1052. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1053. syntax.curly[syntax.curly_count].count = 0;
  1054. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1055. // increment the total curly count for the position in the script
  1056. ++ syntax.curly_count;
  1057. // parse the variable currently being modified
  1058. word = add_word(var);
  1059. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  1060. {// cannot assign a variable which exists as a function or label
  1061. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1062. }
  1063. if( p2 ) {// process the variable index
  1064. const char* p3 = NULL;
  1065. // push the getelementofarray method into the stack
  1066. add_scriptl(buildin_getelementofarray_ref);
  1067. add_scriptc(C_ARG);
  1068. add_scriptl(word);
  1069. // process the sub-expression for this assignment
  1070. p3 = parse_subexpr(p2 + 1, 1);
  1071. p3 = skip_space(p3);
  1072. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1073. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1074. }
  1075. // push the closing function stack operator onto the stack
  1076. add_scriptc(C_FUNC);
  1077. p3 ++;
  1078. } else {// simply push the variable or value onto the stack
  1079. add_scriptl(word);
  1080. }
  1081. if( type != C_EQ )
  1082. add_scriptc(C_REF);
  1083. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1084. add_scripti(1);
  1085. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1086. } else {// process the value as an expression
  1087. p = parse_subexpr(p, -1);
  1088. if( type != C_EQ )
  1089. {// push the type of modifier onto the stack
  1090. add_scriptc(type);
  1091. }
  1092. }
  1093. // decrement the curly count for the position within the script
  1094. -- syntax.curly_count;
  1095. // close the script by appending the function operator
  1096. add_scriptc(C_FUNC);
  1097. // push the buffer from the method
  1098. return p;
  1099. }
  1100. /*==========================================
  1101. * Analysis section
  1102. *------------------------------------------*/
  1103. const char* parse_simpleexpr(const char *p)
  1104. {
  1105. long long i;
  1106. p=skip_space(p);
  1107. if(*p==';' || *p==',')
  1108. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1109. if(*p=='('){
  1110. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1111. ++syntax.curly[i].count;
  1112. p=parse_subexpr(p+1,-1);
  1113. p=skip_space(p);
  1114. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1115. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1116. ){
  1117. if( *p == ',' ){
  1118. syntax.curly[i].flag = ARGLIST_PAREN;
  1119. return p;
  1120. } else
  1121. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1122. }
  1123. if( *p != ')' )
  1124. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1125. ++p;
  1126. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  1127. char *np;
  1128. while(*p == '0' && ISDIGIT(p[1])) p++;
  1129. i=strtoll(p,&np,0);
  1130. if( i < INT_MIN ) {
  1131. i = INT_MIN;
  1132. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1133. } else if( i > INT_MAX ) {
  1134. i = INT_MAX;
  1135. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1136. }
  1137. add_scripti((int)i);
  1138. p=np;
  1139. } else if(*p=='"'){
  1140. add_scriptc(C_STR);
  1141. p++;
  1142. while( *p && *p != '"' ){
  1143. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1144. {
  1145. char buf[8];
  1146. size_t len = skip_escaped_c(p) - p;
  1147. size_t n = sv_unescape_c(buf, p, len);
  1148. if( n != 1 )
  1149. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1150. p += len;
  1151. add_scriptb(*buf);
  1152. continue;
  1153. }
  1154. else if( *p == '\n' )
  1155. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1156. add_scriptb(*p++);
  1157. }
  1158. if(!*p)
  1159. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1160. add_scriptb(0);
  1161. p++; //'"'
  1162. } else {
  1163. int l;
  1164. const char* pv;
  1165. // label , register , function etc
  1166. if(skip_word(p)==p)
  1167. disp_error_message("parse_simpleexpr: unexpected character",p);
  1168. l=add_word(p);
  1169. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1170. return parse_callfunc(p,1,0);
  1171. #ifdef SCRIPT_CALLFUNC_CHECK
  1172. else {
  1173. const char* name = get_str(l);
  1174. if( strdb_get(userfunc_db,name) != NULL ) {
  1175. return parse_callfunc(p,1,1);
  1176. }
  1177. }
  1178. #endif
  1179. if( (pv = parse_variable(p)) )
  1180. {// successfully processed a variable assignment
  1181. return pv;
  1182. }
  1183. p=skip_word(p);
  1184. if( *p == '[' ){
  1185. // array(name[i] => getelementofarray(name,i) )
  1186. add_scriptl(buildin_getelementofarray_ref);
  1187. add_scriptc(C_ARG);
  1188. add_scriptl(l);
  1189. p=parse_subexpr(p+1,-1);
  1190. p=skip_space(p);
  1191. if( *p != ']' )
  1192. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1193. ++p;
  1194. add_scriptc(C_FUNC);
  1195. }else
  1196. add_scriptl(l);
  1197. }
  1198. return p;
  1199. }
  1200. /*==========================================
  1201. * Analysis of the expression
  1202. *------------------------------------------*/
  1203. const char* parse_subexpr(const char* p,int limit)
  1204. {
  1205. int op,opl,len;
  1206. const char* tmpp;
  1207. p=skip_space(p);
  1208. if( *p == '-' ){
  1209. tmpp = skip_space(p+1);
  1210. if( *tmpp == ';' || *tmpp == ',' ){
  1211. add_scriptl(LABEL_NEXTLINE);
  1212. p++;
  1213. return p;
  1214. }
  1215. }
  1216. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1217. p=parse_subexpr(p+1,10);
  1218. add_scriptc(op);
  1219. } else
  1220. p=parse_simpleexpr(p);
  1221. p=skip_space(p);
  1222. while((
  1223. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1224. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1225. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1226. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1227. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1228. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1229. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1230. (op=C_AND,opl=6,len=1,*p=='&') ||
  1231. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1232. (op=C_OR,opl=5,len=1,*p=='|') ||
  1233. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1234. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1235. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1236. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1237. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1238. (op=C_GT,opl=3,len=1,*p=='>') ||
  1239. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1240. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1241. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1242. p+=len;
  1243. if(op == C_OP3) {
  1244. p=parse_subexpr(p,-1);
  1245. p=skip_space(p);
  1246. if( *(p++) != ':')
  1247. disp_error_message("parse_subexpr: expected ':'", p-1);
  1248. p=parse_subexpr(p,-1);
  1249. } else {
  1250. p=parse_subexpr(p,opl);
  1251. }
  1252. add_scriptc(op);
  1253. p=skip_space(p);
  1254. }
  1255. return p; /* return first untreated operator */
  1256. }
  1257. /*==========================================
  1258. * Evaluation of the expression
  1259. *------------------------------------------*/
  1260. const char* parse_expr(const char *p)
  1261. {
  1262. switch(*p){
  1263. case ')': case ';': case ':': case '[': case ']':
  1264. case '}':
  1265. disp_error_message("parse_expr: unexpected character",p);
  1266. }
  1267. p=parse_subexpr(p,-1);
  1268. return p;
  1269. }
  1270. /*==========================================
  1271. * Analysis of the line
  1272. *------------------------------------------*/
  1273. const char* parse_line(const char* p)
  1274. {
  1275. const char* p2;
  1276. p=skip_space(p);
  1277. if(*p==';') {
  1278. //Close decision for if(); for(); while();
  1279. p = parse_syntax_close(p + 1);
  1280. return p;
  1281. }
  1282. if(*p==')' && parse_syntax_for_flag)
  1283. return p+1;
  1284. p = skip_space(p);
  1285. if(p[0] == '{') {
  1286. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1287. syntax.curly[syntax.curly_count].count = -1;
  1288. syntax.curly[syntax.curly_count].index = -1;
  1289. syntax.curly_count++;
  1290. return p + 1;
  1291. } else if(p[0] == '}') {
  1292. return parse_curly_close(p);
  1293. }
  1294. // Syntax-related processing
  1295. p2 = parse_syntax(p);
  1296. if(p2 != NULL)
  1297. return p2;
  1298. // attempt to process a variable assignment
  1299. p2 = parse_variable(p);
  1300. if( p2 != NULL )
  1301. {// variable assignment processed so leave the method
  1302. return parse_syntax_close(p2 + 1);
  1303. }
  1304. p = parse_callfunc(p,0,0);
  1305. p = skip_space(p);
  1306. if(parse_syntax_for_flag) {
  1307. if( *p != ')' )
  1308. disp_error_message("parse_line: expected ')'",p);
  1309. } else {
  1310. if( *p != ';' )
  1311. disp_error_message("parse_line: expected ';'",p);
  1312. }
  1313. //Binding decision for if(), for(), while()
  1314. p = parse_syntax_close(p+1);
  1315. return p;
  1316. }
  1317. // { ... } Closing process
  1318. const char* parse_curly_close(const char* p)
  1319. {
  1320. if(syntax.curly_count <= 0) {
  1321. disp_error_message("parse_curly_close: unexpected string",p);
  1322. return p + 1;
  1323. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1324. syntax.curly_count--;
  1325. //Close decision if, for , while
  1326. p = parse_syntax_close(p + 1);
  1327. return p;
  1328. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1329. //Closing switch()
  1330. int pos = syntax.curly_count-1;
  1331. char label[256];
  1332. int l;
  1333. // Remove temporary variables
  1334. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1335. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1336. parse_line(label);
  1337. syntax.curly_count--;
  1338. // Go to the end pointer unconditionally
  1339. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1340. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1341. parse_line(label);
  1342. syntax.curly_count--;
  1343. // You are here labeled
  1344. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1345. l=add_str(label);
  1346. set_label(l,script_pos, p);
  1347. if(syntax.curly[pos].flag) {
  1348. //Exists default
  1349. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1350. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1351. parse_line(label);
  1352. syntax.curly_count--;
  1353. }
  1354. // Label end
  1355. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1356. l=add_str(label);
  1357. set_label(l,script_pos, p);
  1358. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1359. syntax.curly_count--;
  1360. //Closing decision if, for , while
  1361. p = parse_syntax_close(p + 1);
  1362. return p;
  1363. } else {
  1364. disp_error_message("parse_curly_close: unexpected string",p);
  1365. return p + 1;
  1366. }
  1367. }
  1368. // Syntax-related processing
  1369. // break, case, continue, default, do, for, function,
  1370. // if, switch, while ? will handle this internally.
  1371. const char* parse_syntax(const char* p)
  1372. {
  1373. const char *p2 = skip_word(p);
  1374. switch(*p) {
  1375. case 'B':
  1376. case 'b':
  1377. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1378. // break Processing
  1379. char label[256];
  1380. int pos = syntax.curly_count - 1;
  1381. while(pos >= 0) {
  1382. if(syntax.curly[pos].type == TYPE_DO) {
  1383. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1384. break;
  1385. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1386. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1387. break;
  1388. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1389. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1390. break;
  1391. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1392. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1393. break;
  1394. }
  1395. pos--;
  1396. }
  1397. if(pos < 0) {
  1398. disp_error_message("parse_syntax: unexpected 'break'",p);
  1399. } else {
  1400. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1401. parse_line(label);
  1402. syntax.curly_count--;
  1403. }
  1404. p = skip_space(p2);
  1405. if(*p != ';')
  1406. disp_error_message("parse_syntax: expected ';'",p);
  1407. // Closing decision if, for , while
  1408. p = parse_syntax_close(p + 1);
  1409. return p;
  1410. }
  1411. break;
  1412. case 'c':
  1413. case 'C':
  1414. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1415. //Processing case
  1416. int pos = syntax.curly_count-1;
  1417. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1418. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1419. return p+1;
  1420. } else {
  1421. char label[256];
  1422. int l,v;
  1423. char *np;
  1424. if(syntax.curly[pos].count != 1) {
  1425. //Jump for FALLTHRU
  1426. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1427. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1428. parse_line(label);
  1429. syntax.curly_count--;
  1430. // You are here labeled
  1431. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1432. l=add_str(label);
  1433. set_label(l,script_pos, p);
  1434. }
  1435. //Decision statement switch
  1436. p = skip_space(p2);
  1437. if(p == p2) {
  1438. disp_error_message("parse_syntax: expected a space ' '",p);
  1439. }
  1440. // check whether case label is integer or not
  1441. v = strtol(p,&np,0);
  1442. if(np == p) { //Check for constants
  1443. p2 = skip_word(p);
  1444. v = p2-p; // length of word at p2
  1445. memcpy(label,p,v);
  1446. label[v]='\0';
  1447. if( !script_get_constant(label, &v) )
  1448. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1449. p = skip_word(p);
  1450. } else { //Numeric value
  1451. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1452. p++;
  1453. p = skip_word(p);
  1454. if(np != p)
  1455. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1456. }
  1457. p = skip_space(p);
  1458. if(*p != ':')
  1459. disp_error_message("parse_syntax: expect ':'",p);
  1460. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1461. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1462. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1463. // Bad I do not parse twice
  1464. p2 = parse_line(label);
  1465. parse_line(p2);
  1466. syntax.curly_count--;
  1467. if(syntax.curly[pos].count != 1) {
  1468. // Label after the completion of FALLTHRU
  1469. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1470. l=add_str(label);
  1471. set_label(l,script_pos,p);
  1472. }
  1473. // check duplication of case label [Rayce]
  1474. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1475. disp_error_message("parse_syntax: dup 'case'",p);
  1476. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1477. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1478. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1479. parse_line(label);
  1480. syntax.curly_count--;
  1481. syntax.curly[pos].count++;
  1482. }
  1483. return p + 1;
  1484. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1485. // Processing continue
  1486. char label[256];
  1487. int pos = syntax.curly_count - 1;
  1488. while(pos >= 0) {
  1489. if(syntax.curly[pos].type == TYPE_DO) {
  1490. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1491. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1492. break;
  1493. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1494. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1495. break;
  1496. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1497. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1498. break;
  1499. }
  1500. pos--;
  1501. }
  1502. if(pos < 0) {
  1503. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1504. } else {
  1505. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1506. parse_line(label);
  1507. syntax.curly_count--;
  1508. }
  1509. p = skip_space(p2);
  1510. if(*p != ';')
  1511. disp_error_message("parse_syntax: expected ';'",p);
  1512. //Closing decision if, for , while
  1513. p = parse_syntax_close(p + 1);
  1514. return p;
  1515. }
  1516. break;
  1517. case 'd':
  1518. case 'D':
  1519. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1520. // Switch - default processing
  1521. int pos = syntax.curly_count-1;
  1522. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1523. disp_error_message("parse_syntax: unexpected 'default'",p);
  1524. } else if(syntax.curly[pos].flag) {
  1525. disp_error_message("parse_syntax: dup 'default'",p);
  1526. } else {
  1527. char label[256];
  1528. int l;
  1529. // Put the label location
  1530. p = skip_space(p2);
  1531. if(*p != ':') {
  1532. disp_error_message("parse_syntax: expected ':'",p);
  1533. }
  1534. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1535. l=add_str(label);
  1536. set_label(l,script_pos,p);
  1537. // Skip to the next link w/o condition
  1538. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1539. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1540. parse_line(label);
  1541. syntax.curly_count--;
  1542. // The default label
  1543. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1544. l=add_str(label);
  1545. set_label(l,script_pos,p);
  1546. syntax.curly[syntax.curly_count - 1].flag = 1;
  1547. syntax.curly[pos].count++;
  1548. }
  1549. return p + 1;
  1550. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1551. int l;
  1552. char label[256];
  1553. p=skip_space(p2);
  1554. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1555. syntax.curly[syntax.curly_count].count = 1;
  1556. syntax.curly[syntax.curly_count].index = syntax.index++;
  1557. syntax.curly[syntax.curly_count].flag = 0;
  1558. // Label of the (do) form here
  1559. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1560. l=add_str(label);
  1561. set_label(l,script_pos,p);
  1562. syntax.curly_count++;
  1563. return p;
  1564. }
  1565. break;
  1566. case 'f':
  1567. case 'F':
  1568. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1569. int l;
  1570. char label[256];
  1571. int pos = syntax.curly_count;
  1572. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1573. syntax.curly[syntax.curly_count].count = 1;
  1574. syntax.curly[syntax.curly_count].index = syntax.index++;
  1575. syntax.curly[syntax.curly_count].flag = 0;
  1576. syntax.curly_count++;
  1577. p=skip_space(p2);
  1578. if(*p != '(')
  1579. disp_error_message("parse_syntax: expected '('",p);
  1580. p++;
  1581. // Execute the initialization statement
  1582. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1583. p=parse_line(p);
  1584. syntax.curly_count--;
  1585. // Form the start of label decision
  1586. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1587. l=add_str(label);
  1588. set_label(l,script_pos,p);
  1589. p=skip_space(p);
  1590. if(*p == ';') {
  1591. // For (; Because the pattern of always true ;)
  1592. ;
  1593. } else {
  1594. // Skip to the end point if the condition is false
  1595. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1596. add_scriptl(add_str("jump_zero"));
  1597. add_scriptc(C_ARG);
  1598. p=parse_expr(p);
  1599. p=skip_space(p);
  1600. add_scriptl(add_str(label));
  1601. add_scriptc(C_FUNC);
  1602. }
  1603. if(*p != ';')
  1604. disp_error_message("parse_syntax: expected ';'",p);
  1605. p++;
  1606. // Skip to the beginning of the loop
  1607. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1608. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1609. parse_line(label);
  1610. syntax.curly_count--;
  1611. // Labels to form the next loop
  1612. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1613. l=add_str(label);
  1614. set_label(l,script_pos,p);
  1615. // Process the next time you enter the loop
  1616. // A ')' last for; flag to be treated as'
  1617. parse_syntax_for_flag = 1;
  1618. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1619. p=parse_line(p);
  1620. syntax.curly_count--;
  1621. parse_syntax_for_flag = 0;
  1622. // Skip to the determination process conditions
  1623. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1624. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1625. parse_line(label);
  1626. syntax.curly_count--;
  1627. // Loop start labeling
  1628. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1629. l=add_str(label);
  1630. set_label(l,script_pos,p);
  1631. return p;
  1632. }
  1633. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1634. {// internal script function
  1635. const char *func_name;
  1636. func_name = skip_space(p2);
  1637. p = skip_word(func_name);
  1638. if( p == func_name )
  1639. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1640. p2 = skip_space(p);
  1641. if( *p2 == ';' )
  1642. {// function <name> ;
  1643. // function declaration - just register the name
  1644. int l;
  1645. l = add_word(func_name);
  1646. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1647. str_data[l].type = C_USERFUNC;
  1648. else if( str_data[l].type == C_USERFUNC )
  1649. ; // already registered
  1650. else
  1651. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1652. // Close condition of if, for, while
  1653. p = parse_syntax_close(p2 + 1);
  1654. return p;
  1655. }
  1656. else if(*p2 == '{')
  1657. {// function <name> <line/block of code>
  1658. char label[256];
  1659. int l;
  1660. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1661. syntax.curly[syntax.curly_count].count = 1;
  1662. syntax.curly[syntax.curly_count].index = syntax.index++;
  1663. syntax.curly[syntax.curly_count].flag = 0;
  1664. ++syntax.curly_count;
  1665. // Jump over the function code
  1666. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1667. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1668. ++syntax.curly_count;
  1669. parse_line(label);
  1670. --syntax.curly_count;
  1671. // Set the position of the function (label)
  1672. l=add_word(func_name);
  1673. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1674. {
  1675. str_data[l].type = C_USERFUNC;
  1676. set_label(l, script_pos, p);
  1677. if( parse_options&SCRIPT_USE_LABEL_DB )
  1678. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1679. }
  1680. else
  1681. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1682. return skip_space(p);
  1683. }
  1684. else
  1685. {
  1686. disp_error_message("expect ';' or '{' at function syntax",p);
  1687. }
  1688. }
  1689. break;
  1690. case 'i':
  1691. case 'I':
  1692. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1693. // If process
  1694. char label[256];
  1695. p=skip_space(p2);
  1696. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1697. disp_error_message("need '('",p);
  1698. }
  1699. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1700. syntax.curly[syntax.curly_count].count = 1;
  1701. syntax.curly[syntax.curly_count].index = syntax.index++;
  1702. syntax.curly[syntax.curly_count].flag = 0;
  1703. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1704. syntax.curly_count++;
  1705. add_scriptl(add_str("jump_zero"));
  1706. add_scriptc(C_ARG);
  1707. p=parse_expr(p);
  1708. p=skip_space(p);
  1709. add_scriptl(add_str(label));
  1710. add_scriptc(C_FUNC);
  1711. return p;
  1712. }
  1713. break;
  1714. case 's':
  1715. case 'S':
  1716. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1717. // Processing of switch ()
  1718. char label[256];
  1719. p=skip_space(p2);
  1720. if(*p != '(') {
  1721. disp_error_message("need '('",p);
  1722. }
  1723. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1724. syntax.curly[syntax.curly_count].count = 1;
  1725. syntax.curly[syntax.curly_count].index = syntax.index++;
  1726. syntax.curly[syntax.curly_count].flag = 0;
  1727. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1728. syntax.curly_count++;
  1729. add_scriptl(add_str("set"));
  1730. add_scriptc(C_ARG);
  1731. add_scriptl(add_str(label));
  1732. p=parse_expr(p);
  1733. p=skip_space(p);
  1734. if(*p != '{') {
  1735. disp_error_message("parse_syntax: expected '{'",p);
  1736. }
  1737. add_scriptc(C_FUNC);
  1738. return p + 1;
  1739. }
  1740. break;
  1741. case 'w':
  1742. case 'W':
  1743. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1744. int l;
  1745. char label[256];
  1746. p=skip_space(p2);
  1747. if(*p != '(') {
  1748. disp_error_message("need '('",p);
  1749. }
  1750. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1751. syntax.curly[syntax.curly_count].count = 1;
  1752. syntax.curly[syntax.curly_count].index = syntax.index++;
  1753. syntax.curly[syntax.curly_count].flag = 0;
  1754. // Form the start of label decision
  1755. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1756. l=add_str(label);
  1757. set_label(l,script_pos,p);
  1758. // Skip to the end point if the condition is false
  1759. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1760. syntax.curly_count++;
  1761. add_scriptl(add_str("jump_zero"));
  1762. add_scriptc(C_ARG);
  1763. p=parse_expr(p);
  1764. p=skip_space(p);
  1765. add_scriptl(add_str(label));
  1766. add_scriptc(C_FUNC);
  1767. return p;
  1768. }
  1769. break;
  1770. }
  1771. return NULL;
  1772. }
  1773. const char* parse_syntax_close(const char *p) {
  1774. // If (...) for (...) hoge (); as to make sure closed closed once again
  1775. int flag;
  1776. do {
  1777. p = parse_syntax_close_sub(p,&flag);
  1778. } while(flag);
  1779. return p;
  1780. }
  1781. // Close judgment if, for, while, of do
  1782. // flag == 1 : closed
  1783. // flag == 0 : not closed
  1784. const char* parse_syntax_close_sub(const char* p,int* flag)
  1785. {
  1786. char label[256];
  1787. int pos = syntax.curly_count - 1;
  1788. int l;
  1789. *flag = 1;
  1790. if(syntax.curly_count <= 0) {
  1791. *flag = 0;
  1792. return p;
  1793. } else if(syntax.curly[pos].type == TYPE_IF) {
  1794. const char *bp = p;
  1795. const char *p2;
  1796. // if-block and else-block end is a new line
  1797. parse_nextline(false, p);
  1798. // Skip to the last location if
  1799. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1800. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1801. parse_line(label);
  1802. syntax.curly_count--;
  1803. // Put the label of the location
  1804. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1805. l=add_str(label);
  1806. set_label(l,script_pos,p);
  1807. syntax.curly[pos].count++;
  1808. p = skip_space(p);
  1809. p2 = skip_word(p);
  1810. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1811. // else or else - if
  1812. p = skip_space(p2);
  1813. p2 = skip_word(p);
  1814. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1815. // else - if
  1816. p=skip_space(p2);
  1817. if(*p != '(') {
  1818. disp_error_message("need '('",p);
  1819. }
  1820. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1821. add_scriptl(add_str("jump_zero"));
  1822. add_scriptc(C_ARG);
  1823. p=parse_expr(p);
  1824. p=skip_space(p);
  1825. add_scriptl(add_str(label));
  1826. add_scriptc(C_FUNC);
  1827. *flag = 0;
  1828. return p;
  1829. } else {
  1830. // else
  1831. if(!syntax.curly[pos].flag) {
  1832. syntax.curly[pos].flag = 1;
  1833. *flag = 0;
  1834. return p;
  1835. }
  1836. }
  1837. }
  1838. // Close if
  1839. syntax.curly_count--;
  1840. // Put the label of the final location
  1841. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1842. l=add_str(label);
  1843. set_label(l,script_pos,p);
  1844. if(syntax.curly[pos].flag == 1) {
  1845. // Because the position of the pointer is the same if not else for this
  1846. return bp;
  1847. }
  1848. return p;
  1849. } else if(syntax.curly[pos].type == TYPE_DO) {
  1850. int l;
  1851. char label[256];
  1852. const char *p2;
  1853. if(syntax.curly[pos].flag) {
  1854. // (Come here continue) to form the label here
  1855. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1856. l=add_str(label);
  1857. set_label(l,script_pos,p);
  1858. }
  1859. // Skip to the end point if the condition is false
  1860. p = skip_space(p);
  1861. p2 = skip_word(p);
  1862. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1863. disp_error_message("parse_syntax: expected 'while'",p);
  1864. p = skip_space(p2);
  1865. if(*p != '(') {
  1866. disp_error_message("need '('",p);
  1867. }
  1868. // do-block end is a new line
  1869. parse_nextline(false, p);
  1870. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1871. add_scriptl(add_str("jump_zero"));
  1872. add_scriptc(C_ARG);
  1873. p=parse_expr(p);
  1874. p=skip_space(p);
  1875. add_scriptl(add_str(label));
  1876. add_scriptc(C_FUNC);
  1877. // Skip to the starting point
  1878. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1879. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1880. parse_line(label);
  1881. syntax.curly_count--;
  1882. // Form label of the end point conditions
  1883. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1884. l=add_str(label);
  1885. set_label(l,script_pos,p);
  1886. p = skip_space(p);
  1887. if(*p != ';') {
  1888. disp_error_message("parse_syntax: expected ';'",p);
  1889. return p+1;
  1890. }
  1891. p++;
  1892. syntax.curly_count--;
  1893. return p;
  1894. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1895. // for-block end is a new line
  1896. parse_nextline(false, p);
  1897. // Skip to the next loop
  1898. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1899. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1900. parse_line(label);
  1901. syntax.curly_count--;
  1902. // End for labeling
  1903. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1904. l=add_str(label);
  1905. set_label(l,script_pos,p);
  1906. syntax.curly_count--;
  1907. return p;
  1908. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1909. // while-block end is a new line
  1910. parse_nextline(false, p);
  1911. // Skip to the decision while
  1912. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1913. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1914. parse_line(label);
  1915. syntax.curly_count--;
  1916. // End while labeling
  1917. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1918. l=add_str(label);
  1919. set_label(l,script_pos,p);
  1920. syntax.curly_count--;
  1921. return p;
  1922. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1923. int pos = syntax.curly_count-1;
  1924. char label[256];
  1925. int l;
  1926. // Back
  1927. sprintf(label,"return;");
  1928. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1929. parse_line(label);
  1930. syntax.curly_count--;
  1931. // Put the label of the location
  1932. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1933. l=add_str(label);
  1934. set_label(l,script_pos,p);
  1935. syntax.curly_count--;
  1936. return p;
  1937. } else {
  1938. *flag = 0;
  1939. return p;
  1940. }
  1941. }
  1942. /*==========================================
  1943. * Added built-in functions
  1944. *------------------------------------------*/
  1945. static void add_buildin_func(void)
  1946. {
  1947. int i,n;
  1948. const char* p;
  1949. for( i = 0; buildin_func[i].func; i++ )
  1950. {
  1951. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1952. // 'v' - value (either string or int or reference)
  1953. // 's' - string
  1954. // 'i' - int
  1955. // 'r' - reference (of a variable)
  1956. // 'l' - label
  1957. // '?' - one optional parameter
  1958. // '*' - unknown number of optional parameters
  1959. p = buildin_func[i].arg;
  1960. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1961. while( *p == '?' ) ++p;
  1962. if( *p == '*' ) ++p;
  1963. if( *p != 0){
  1964. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1965. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1966. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1967. } else {
  1968. n = add_str(buildin_func[i].name);
  1969. str_data[n].type = C_FUNC;
  1970. str_data[n].val = i;
  1971. str_data[n].func = buildin_func[i].func;
  1972. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  1973. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1974. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1975. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1976. }
  1977. }
  1978. }
  1979. /// Retrieves the value of a constant.
  1980. bool script_get_constant(const char* name, int* value)
  1981. {
  1982. int n = search_str(name);
  1983. if( n == -1 || str_data[n].type != C_INT )
  1984. {// not found or not a constant
  1985. return false;
  1986. }
  1987. value[0] = str_data[n].val;
  1988. return true;
  1989. }
  1990. /// Creates new constant or parameter with given value.
  1991. void script_set_constant(const char* name, int value, bool isparameter)
  1992. {
  1993. int n = add_str(name);
  1994. if( str_data[n].type == C_NOP )
  1995. {// new
  1996. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1997. str_data[n].val = value;
  1998. }
  1999. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2000. {// existing parameter or constant
  2001. 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);
  2002. }
  2003. else
  2004. {// existing name
  2005. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2006. }
  2007. }
  2008. /*==========================================
  2009. * Reading constant databases
  2010. * const.txt
  2011. *------------------------------------------*/
  2012. static void read_constdb(void)
  2013. {
  2014. FILE *fp;
  2015. char line[1024],name[1024],val[1024];
  2016. int type;
  2017. sprintf(line, "%s/const.txt", db_path);
  2018. fp=fopen(line, "r");
  2019. if(fp==NULL){
  2020. ShowError("can't read %s\n", line);
  2021. return ;
  2022. }
  2023. while(fgets(line, sizeof(line), fp))
  2024. {
  2025. if(line[0]=='/' && line[1]=='/')
  2026. continue;
  2027. type=0;
  2028. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  2029. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  2030. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2031. }
  2032. }
  2033. fclose(fp);
  2034. }
  2035. /**
  2036. * Sets source-end constants for NPC scripts to access.
  2037. **/
  2038. void script_hardcoded_constants(void) {
  2039. /* server defines */
  2040. script_set_constant("PACKETVER",PACKETVER,false);
  2041. script_set_constant("MAX_LEVEL",MAX_LEVEL,false);
  2042. script_set_constant("MAX_STORAGE",MAX_STORAGE,false);
  2043. script_set_constant("MAX_INVENTORY",MAX_INVENTORY,false);
  2044. script_set_constant("MAX_CART",MAX_INVENTORY,false);
  2045. script_set_constant("MAX_ZENY",MAX_ZENY,false);
  2046. script_set_constant("MAX_PARTY",MAX_PARTY,false);
  2047. script_set_constant("MAX_GUILD",MAX_GUILD,false);
  2048. script_set_constant("MAX_GUILDLEVEL",MAX_GUILDLEVEL,false);
  2049. script_set_constant("MAX_GUILD_STORAGE",MAX_GUILD_STORAGE,false);
  2050. script_set_constant("MAX_BG_MEMBERS",MAX_BG_MEMBERS,false);
  2051. script_set_constant("MAX_CHAT_USERS",MAX_CHAT_USERS,false);
  2052. script_set_constant("VIP_SCRIPT",VIP_SCRIPT,false);
  2053. script_set_constant("MIN_STORAGE",MIN_STORAGE,false);
  2054. /* status options */
  2055. script_set_constant("Option_Nothing",OPTION_NOTHING,false);
  2056. script_set_constant("Option_Sight",OPTION_SIGHT,false);
  2057. script_set_constant("Option_Hide",OPTION_HIDE,false);
  2058. script_set_constant("Option_Cloak",OPTION_CLOAK,false);
  2059. script_set_constant("Option_Falcon",OPTION_FALCON,false);
  2060. script_set_constant("Option_Riding",OPTION_RIDING,false);
  2061. script_set_constant("Option_Invisible",OPTION_INVISIBLE,false);
  2062. script_set_constant("Option_Orcish",OPTION_ORCISH,false);
  2063. script_set_constant("Option_Wedding",OPTION_WEDDING,false);
  2064. script_set_constant("Option_Ruwach",OPTION_RUWACH,false);
  2065. script_set_constant("Option_Chasewalk",OPTION_CHASEWALK,false);
  2066. script_set_constant("Option_Flying",OPTION_FLYING,false);
  2067. script_set_constant("Option_Xmas",OPTION_XMAS,false);
  2068. script_set_constant("Option_Transform",OPTION_TRANSFORM,false);
  2069. script_set_constant("Option_Summer",OPTION_SUMMER,false);
  2070. script_set_constant("Option_Dragon1",OPTION_DRAGON1,false);
  2071. script_set_constant("Option_Wug",OPTION_WUG,false);
  2072. script_set_constant("Option_Wugrider",OPTION_WUGRIDER,false);
  2073. script_set_constant("Option_Madogear",OPTION_MADOGEAR,false);
  2074. script_set_constant("Option_Dragon2",OPTION_DRAGON2,false);
  2075. script_set_constant("Option_Dragon3",OPTION_DRAGON3,false);
  2076. script_set_constant("Option_Dragon4",OPTION_DRAGON4,false);
  2077. script_set_constant("Option_Dragon5",OPTION_DRAGON5,false);
  2078. script_set_constant("Option_Hanbok",OPTION_HANBOK,false);
  2079. script_set_constant("Option_Oktoberfest",OPTION_OKTOBERFEST,false);
  2080. /* status option compounds */
  2081. script_set_constant("Option_Dragon",OPTION_DRAGON,false);
  2082. script_set_constant("Option_Costume",OPTION_COSTUME,false);
  2083. }
  2084. /*==========================================
  2085. * Display emplacement line of script
  2086. *------------------------------------------*/
  2087. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2088. {
  2089. int i;
  2090. if( p == NULL || !p[0] ) return NULL;
  2091. if( line < 0 )
  2092. StringBuf_Printf(buf, "*% 5d : ", -line);
  2093. else
  2094. StringBuf_Printf(buf, " % 5d : ", line);
  2095. for(i=0;p[i] && p[i] != '\n';i++){
  2096. if(p + i != mark)
  2097. StringBuf_Printf(buf, "%c", p[i]);
  2098. else
  2099. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2100. }
  2101. StringBuf_AppendStr(buf, "\n");
  2102. return p+i+(p[i] == '\n' ? 1 : 0);
  2103. }
  2104. void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) {
  2105. // Find the line where the error occurred
  2106. int j;
  2107. int line = start_line;
  2108. const char *p;
  2109. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2110. for(p=src;p && *p;line++){
  2111. const char *lineend=strchr(p,'\n');
  2112. if(lineend==NULL || error_pos<lineend){
  2113. break;
  2114. }
  2115. for( j = 0; j < 4; j++ ) {
  2116. linestart[j] = linestart[j+1];
  2117. }
  2118. linestart[4] = p;
  2119. p=lineend+1;
  2120. }
  2121. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2122. StringBuf_Printf(buf, " %s\n", error_msg);
  2123. for(j = 0; j < 5; j++ ) {
  2124. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2125. }
  2126. p = script_print_line(buf, p, error_pos, -line);
  2127. for(j = 0; j < 5; j++) {
  2128. p = script_print_line(buf, p, NULL, line + j + 1 );
  2129. }
  2130. }
  2131. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) {
  2132. StringBuf buf;
  2133. StringBuf_Init(&buf);
  2134. StringBuf_AppendStr(&buf, "\a\n");
  2135. script_errorwarning_sub(&buf, src, file, start_line, error_msg, error_pos);
  2136. ShowError("%s", StringBuf_Value(&buf));
  2137. StringBuf_Destroy(&buf);
  2138. }
  2139. void script_warning(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) {
  2140. StringBuf buf;
  2141. StringBuf_Init(&buf);
  2142. script_errorwarning_sub(&buf, src, file, start_line, error_msg, error_pos);
  2143. ShowWarning("%s", StringBuf_Value(&buf));
  2144. StringBuf_Destroy(&buf);
  2145. }
  2146. /*==========================================
  2147. * Analysis of the script
  2148. *------------------------------------------*/
  2149. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2150. {
  2151. const char *p,*tmpp;
  2152. int i;
  2153. struct script_code* code = NULL;
  2154. static int first=1;
  2155. char end;
  2156. bool unresolved_names = false;
  2157. parser_current_src = src;
  2158. parser_current_file = file;
  2159. parser_current_line = line;
  2160. if( src == NULL )
  2161. return NULL;// empty script
  2162. memset(&syntax,0,sizeof(syntax));
  2163. if(first){
  2164. add_buildin_func();
  2165. read_constdb();
  2166. script_hardcoded_constants();
  2167. first=0;
  2168. }
  2169. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2170. script_pos=0;
  2171. script_size=SCRIPT_BLOCK_SIZE;
  2172. parse_nextline(true, NULL);
  2173. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2174. if( options&SCRIPT_USE_LABEL_DB )
  2175. db_clear(scriptlabel_db);
  2176. parse_options = options;
  2177. if( setjmp( error_jump ) != 0 ) {
  2178. //Restore program state when script has problems. [from jA]
  2179. int i;
  2180. const int size = ARRAYLENGTH(syntax.curly);
  2181. if( error_report )
  2182. script_error(src,file,line,error_msg,error_pos);
  2183. aFree( error_msg );
  2184. aFree( script_buf );
  2185. script_pos = 0;
  2186. script_size = 0;
  2187. script_buf = NULL;
  2188. for(i=LABEL_START;i<str_num;i++)
  2189. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  2190. for(i=0; i<size; i++)
  2191. linkdb_final(&syntax.curly[i].case_label);
  2192. return NULL;
  2193. }
  2194. parse_syntax_for_flag=0;
  2195. p=src;
  2196. p=skip_space(p);
  2197. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2198. {// does not require brackets around the script
  2199. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2200. {// empty script and can return NULL
  2201. aFree( script_buf );
  2202. script_pos = 0;
  2203. script_size = 0;
  2204. script_buf = NULL;
  2205. return NULL;
  2206. }
  2207. end = '\0';
  2208. }
  2209. else
  2210. {// requires brackets around the script
  2211. if( *p != '{' )
  2212. disp_error_message("not found '{'",p);
  2213. p = skip_space(p+1);
  2214. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2215. {// empty script and can return NULL
  2216. aFree( script_buf );
  2217. script_pos = 0;
  2218. script_size = 0;
  2219. script_buf = NULL;
  2220. return NULL;
  2221. }
  2222. end = '}';
  2223. }
  2224. // clear references of labels, variables and internal functions
  2225. for(i=LABEL_START;i<str_num;i++){
  2226. if(
  2227. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2228. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2229. ){
  2230. str_data[i].type=C_NOP;
  2231. str_data[i].backpatch=-1;
  2232. str_data[i].label=-1;
  2233. }
  2234. }
  2235. while( syntax.curly_count != 0 || *p != end )
  2236. {
  2237. if( *p == '\0' )
  2238. disp_error_message("unexpected end of script",p);
  2239. // Special handling only label
  2240. tmpp=skip_space(skip_word(p));
  2241. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2242. i=add_word(p);
  2243. set_label(i,script_pos,p);
  2244. if( parse_options&SCRIPT_USE_LABEL_DB )
  2245. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2246. p=tmpp+1;
  2247. p=skip_space(p);
  2248. continue;
  2249. }
  2250. // All other lumped
  2251. p=parse_line(p);
  2252. p=skip_space(p);
  2253. parse_nextline(false, p);
  2254. }
  2255. add_scriptc(C_NOP);
  2256. // trim code to size
  2257. script_size = script_pos;
  2258. RECREATE(script_buf,unsigned char,script_pos);
  2259. // default unknown references to variables
  2260. for(i=LABEL_START;i<str_num;i++){
  2261. if(str_data[i].type==C_NOP){
  2262. int j,next;
  2263. str_data[i].type=C_NAME;
  2264. str_data[i].label=i;
  2265. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2266. next=GETVALUE(script_buf,j);
  2267. SETVALUE(script_buf,j,i);
  2268. j=next;
  2269. }
  2270. }
  2271. else if( str_data[i].type == C_USERFUNC )
  2272. {// 'function name;' without follow-up code
  2273. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2274. unresolved_names = true;
  2275. }
  2276. }
  2277. if( unresolved_names )
  2278. {
  2279. disp_error_message("parse_script: unresolved function references", p);
  2280. }
  2281. #ifdef DEBUG_DISP
  2282. for(i=0;i<script_pos;i++){
  2283. if((i&15)==0) ShowMessage("%04x : ",i);
  2284. ShowMessage("%02x ",script_buf[i]);
  2285. if((i&15)==15) ShowMessage("\n");
  2286. }
  2287. ShowMessage("\n");
  2288. #endif
  2289. #ifdef DEBUG_DISASM
  2290. {
  2291. int i = 0,j;
  2292. while(i < script_pos) {
  2293. c_op op = get_com(script_buf,&i);
  2294. ShowMessage("%06x %s", i, script_op2name(op));
  2295. j = i;
  2296. switch(op) {
  2297. case C_INT:
  2298. ShowMessage(" %d", get_num(script_buf,&i));
  2299. break;
  2300. case C_POS:
  2301. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2302. i += 3;
  2303. break;
  2304. case C_NAME:
  2305. j = (*(int*)(script_buf+i)&0xffffff);
  2306. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2307. i += 3;
  2308. break;
  2309. case C_STR:
  2310. j = strlen(script_buf + i);
  2311. ShowMessage(" %s", script_buf + i);
  2312. i += j+1;
  2313. break;
  2314. }
  2315. ShowMessage(CL_CLL"\n");
  2316. }
  2317. }
  2318. #endif
  2319. CREATE(code,struct script_code,1);
  2320. code->script_buf = script_buf;
  2321. code->script_size = script_size;
  2322. code->script_vars = idb_alloc(DB_OPT_RELEASE_DATA);
  2323. return code;
  2324. }
  2325. /// Returns the player attached to this script, identified by the rid.
  2326. /// If there is no player attached, the script is terminated.
  2327. TBL_PC *script_rid2sd(struct script_state *st)
  2328. {
  2329. TBL_PC *sd=map_id2sd(st->rid);
  2330. if(!sd){
  2331. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2332. script_reportfunc(st);
  2333. script_reportsrc(st);
  2334. st->state = END;
  2335. }
  2336. return sd;
  2337. }
  2338. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2339. ///
  2340. /// @param st Script state
  2341. /// @param data Variable/constant
  2342. void get_val(struct script_state* st, struct script_data* data)
  2343. {
  2344. const char* name;
  2345. char prefix;
  2346. char postfix;
  2347. TBL_PC* sd = NULL;
  2348. if( !data_isreference(data) )
  2349. return;// not a variable/constant
  2350. name = reference_getname(data);
  2351. prefix = name[0];
  2352. postfix = name[strlen(name) - 1];
  2353. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2354. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2355. {
  2356. sd = script_rid2sd(st);
  2357. if( sd == NULL )
  2358. {// needs player attached
  2359. if( postfix == '$' )
  2360. {// string variable
  2361. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2362. data->type = C_CONSTSTR;
  2363. data->u.str = "";
  2364. }
  2365. else
  2366. {// integer variable
  2367. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2368. data->type = C_INT;
  2369. data->u.num = 0;
  2370. }
  2371. return;
  2372. }
  2373. }
  2374. if( postfix == '$' )
  2375. {// string variable
  2376. switch( prefix )
  2377. {
  2378. case '@':
  2379. data->u.str = pc_readregstr(sd, data->u.num);
  2380. break;
  2381. case '$':
  2382. data->u.str = mapreg_readregstr(data->u.num);
  2383. break;
  2384. case '#':
  2385. if( name[1] == '#' )
  2386. data->u.str = pc_readaccountreg2str(sd, name);// global
  2387. else
  2388. data->u.str = pc_readaccountregstr(sd, name);// local
  2389. break;
  2390. case '.':
  2391. {
  2392. struct DBMap* n =
  2393. data->ref ? *data->ref:
  2394. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2395. st->script->script_vars;// npc variable
  2396. if( n )
  2397. data->u.str = (char*)idb_get(n,reference_getuid(data));
  2398. else
  2399. data->u.str = NULL;
  2400. }
  2401. break;
  2402. case '\'':
  2403. {
  2404. int instance_id = script_instancegetid(st);
  2405. if( instance_id )
  2406. data->u.str = (char*)idb_get(instance_data[instance_id].vars,reference_getuid(data));
  2407. else {
  2408. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2409. data->u.str = NULL;
  2410. }
  2411. }
  2412. break;
  2413. default:
  2414. data->u.str = pc_readglobalreg_str(sd, name);
  2415. break;
  2416. }
  2417. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2418. {// empty string
  2419. data->type = C_CONSTSTR;
  2420. data->u.str = "";
  2421. }
  2422. else
  2423. {// duplicate string
  2424. data->type = C_STR;
  2425. data->u.str = aStrdup(data->u.str);
  2426. }
  2427. }
  2428. else
  2429. {// integer variable
  2430. data->type = C_INT;
  2431. if( reference_toconstant(data) )
  2432. {
  2433. data->u.num = reference_getconstant(data);
  2434. }
  2435. else if( reference_toparam(data) )
  2436. {
  2437. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2438. }
  2439. else
  2440. switch( prefix )
  2441. {
  2442. case '@':
  2443. data->u.num = pc_readreg(sd, data->u.num);
  2444. break;
  2445. case '$':
  2446. data->u.num = mapreg_readreg(data->u.num);
  2447. break;
  2448. case '#':
  2449. if( name[1] == '#' )
  2450. data->u.num = pc_readaccountreg2(sd, name);// global
  2451. else
  2452. data->u.num = pc_readaccountreg(sd, name);// local
  2453. break;
  2454. case '.':
  2455. {
  2456. struct DBMap* n =
  2457. data->ref ? *data->ref:
  2458. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2459. st->script->script_vars;// npc variable
  2460. if( n )
  2461. data->u.num = (int)idb_iget(n,reference_getuid(data));
  2462. else
  2463. data->u.num = 0;
  2464. }
  2465. break;
  2466. case '\'':
  2467. {
  2468. int instance_id = script_instancegetid(st);
  2469. if( instance_id )
  2470. data->u.num = (int)idb_iget(instance_data[instance_id].vars,reference_getuid(data));
  2471. else {
  2472. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2473. data->u.num = 0;
  2474. }
  2475. }
  2476. break;
  2477. default:
  2478. data->u.num = pc_readglobalreg(sd, name);
  2479. break;
  2480. }
  2481. }
  2482. return;
  2483. }
  2484. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
  2485. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2486. /// The value is left in the top of the stack and needs to be removed manually.
  2487. void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
  2488. {
  2489. struct script_data* data;
  2490. push_val2(st->stack, C_NAME, uid, ref);
  2491. data = script_getdatatop(st, -1);
  2492. get_val(st, data);
  2493. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2494. }
  2495. /*==========================================
  2496. * Stores the value of a script variable
  2497. * Return value is 0 on fail, 1 on success.
  2498. *------------------------------------------*/
  2499. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
  2500. {
  2501. char prefix = name[0];
  2502. if( is_string_variable(name) ) {// string variable
  2503. const char* str = (const char*)value;
  2504. switch (prefix) {
  2505. case '@':
  2506. return pc_setregstr(sd, num, str);
  2507. case '$':
  2508. return mapreg_setregstr(num, str);
  2509. case '#':
  2510. return (name[1] == '#') ?
  2511. pc_setaccountreg2str(sd, name, str) :
  2512. pc_setaccountregstr(sd, name, str);
  2513. case '.':
  2514. {
  2515. struct DBMap* n;
  2516. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2517. if( n ) {
  2518. idb_remove(n, num);
  2519. if (str[0]) idb_put(n, num, aStrdup(str));
  2520. }
  2521. }
  2522. return 1;
  2523. case '\'':
  2524. {
  2525. int instance_id = script_instancegetid(st);
  2526. if( instance_id ) {
  2527. idb_remove(instance_data[instance_id].vars, num);
  2528. if( str[0] ) idb_put(instance_data[instance_id].vars, num, aStrdup(str));
  2529. }
  2530. return 1;
  2531. }
  2532. default:
  2533. return pc_setglobalreg_str(sd, name, str);
  2534. }
  2535. } else {// integer variable
  2536. int val = (int)__64BPRTSIZE(value);
  2537. if(str_data[num&0x00ffffff].type == C_PARAM)
  2538. {
  2539. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2540. {
  2541. if( st != NULL )
  2542. {
  2543. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2544. script_reportsrc(st);
  2545. st->state = END;
  2546. }
  2547. return 0;
  2548. }
  2549. return 1;
  2550. }
  2551. switch (prefix) {
  2552. case '@':
  2553. return pc_setreg(sd, num, val);
  2554. case '$':
  2555. return mapreg_setreg(num, val);
  2556. case '#':
  2557. return (name[1] == '#') ?
  2558. pc_setaccountreg2(sd, name, val) :
  2559. pc_setaccountreg(sd, name, val);
  2560. case '.':
  2561. {
  2562. struct DBMap* n;
  2563. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2564. if( n ) {
  2565. idb_remove(n, num);
  2566. if( val != 0 )
  2567. idb_iput(n, num, val);
  2568. }
  2569. }
  2570. return 1;
  2571. case '\'':
  2572. {
  2573. int instance_id = script_instancegetid(st);
  2574. if( instance_id ) {
  2575. idb_remove(instance_data[instance_id].vars, num);
  2576. if( val != 0 )
  2577. idb_iput(instance_data[instance_id].vars, num, val);
  2578. }
  2579. return 1;
  2580. }
  2581. default:
  2582. return pc_setglobalreg(sd, name, val);
  2583. }
  2584. }
  2585. }
  2586. int set_var(TBL_PC* sd, char* name, void* val)
  2587. {
  2588. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2589. }
  2590. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
  2591. {
  2592. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2593. }
  2594. /// Converts the data to a string
  2595. const char* conv_str(struct script_state* st, struct script_data* data)
  2596. {
  2597. char* p;
  2598. get_val(st, data);
  2599. if( data_isstring(data) )
  2600. {// nothing to convert
  2601. }
  2602. else if( data_isint(data) )
  2603. {// int -> string
  2604. CREATE(p, char, ITEM_NAME_LENGTH);
  2605. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2606. p[ITEM_NAME_LENGTH-1] = '\0';
  2607. data->type = C_STR;
  2608. data->u.str = p;
  2609. }
  2610. else if( data_isreference(data) )
  2611. {// reference -> string
  2612. //##TODO when does this happen (check get_val) [FlavioJS]
  2613. data->type = C_CONSTSTR;
  2614. data->u.str = reference_getname(data);
  2615. }
  2616. else
  2617. {// unsupported data type
  2618. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2619. script_reportdata(data);
  2620. script_reportsrc(st);
  2621. data->type = C_CONSTSTR;
  2622. data->u.str = "";
  2623. }
  2624. return data->u.str;
  2625. }
  2626. /// Converts the data to an int
  2627. int conv_num(struct script_state* st, struct script_data* data)
  2628. {
  2629. char* p;
  2630. long num;
  2631. get_val(st, data);
  2632. if( data_isint(data) )
  2633. {// nothing to convert
  2634. }
  2635. else if( data_isstring(data) )
  2636. {// string -> int
  2637. // the result does not overflow or underflow, it is capped instead
  2638. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2639. p = data->u.str;
  2640. errno = 0;
  2641. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2642. if( errno == ERANGE
  2643. #if LONG_MAX > INT_MAX
  2644. || num < INT_MIN || num > INT_MAX
  2645. #endif
  2646. )
  2647. {
  2648. if( num <= INT_MIN )
  2649. {
  2650. num = INT_MIN;
  2651. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2652. }
  2653. else//if( num >= INT_MAX )
  2654. {
  2655. num = INT_MAX;
  2656. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2657. }
  2658. script_reportdata(data);
  2659. script_reportsrc(st);
  2660. }
  2661. if( data->type == C_STR )
  2662. aFree(p);
  2663. data->type = C_INT;
  2664. data->u.num = (int)num;
  2665. }
  2666. #if 0
  2667. // FIXME this function is being used to retrieve the position of labels and
  2668. // probably other stuff [FlavioJS]
  2669. else
  2670. {// unsupported data type
  2671. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2672. script_reportdata(data);
  2673. script_reportsrc(st);
  2674. data->type = C_INT;
  2675. data->u.num = 0;
  2676. }
  2677. #endif
  2678. return data->u.num;
  2679. }
  2680. //
  2681. // Stack operations
  2682. //
  2683. /// Increases the size of the stack
  2684. void stack_expand(struct script_stack* stack)
  2685. {
  2686. stack->sp_max += 64;
  2687. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2688. stack->sp_max * sizeof(stack->stack_data[0]) );
  2689. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2690. 64 * sizeof(stack->stack_data[0]) );
  2691. }
  2692. /// Pushes a value into the stack
  2693. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2694. /// Pushes a value into the stack (with reference)
  2695. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
  2696. {
  2697. if( stack->sp >= stack->sp_max )
  2698. stack_expand(stack);
  2699. stack->stack_data[stack->sp].type = type;
  2700. stack->stack_data[stack->sp].u.num = val;
  2701. stack->stack_data[stack->sp].ref = ref;
  2702. stack->sp++;
  2703. return &stack->stack_data[stack->sp-1];
  2704. }
  2705. /// Pushes a string into the stack
  2706. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2707. {
  2708. if( stack->sp >= stack->sp_max )
  2709. stack_expand(stack);
  2710. stack->stack_data[stack->sp].type = type;
  2711. stack->stack_data[stack->sp].u.str = str;
  2712. stack->stack_data[stack->sp].ref = NULL;
  2713. stack->sp++;
  2714. return &stack->stack_data[stack->sp-1];
  2715. }
  2716. /// Pushes a retinfo into the stack
  2717. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, DBMap **ref)
  2718. {
  2719. if( stack->sp >= stack->sp_max )
  2720. stack_expand(stack);
  2721. stack->stack_data[stack->sp].type = C_RETINFO;
  2722. stack->stack_data[stack->sp].u.ri = ri;
  2723. stack->stack_data[stack->sp].ref = ref;
  2724. stack->sp++;
  2725. return &stack->stack_data[stack->sp-1];
  2726. }
  2727. /// Pushes a copy of the target position into the stack
  2728. struct script_data* push_copy(struct script_stack* stack, int pos)
  2729. {
  2730. switch( stack->stack_data[pos].type )
  2731. {
  2732. case C_CONSTSTR:
  2733. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2734. break;
  2735. case C_STR:
  2736. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2737. break;
  2738. case C_RETINFO:
  2739. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2740. exit(1);
  2741. break;
  2742. default:
  2743. return push_val2(
  2744. stack,stack->stack_data[pos].type,
  2745. stack->stack_data[pos].u.num,
  2746. stack->stack_data[pos].ref
  2747. );
  2748. break;
  2749. }
  2750. }
  2751. /// Removes the values in indexes [start,end[ from the stack.
  2752. /// Adjusts all stack pointers.
  2753. void pop_stack(struct script_state* st, int start, int end)
  2754. {
  2755. struct script_stack* stack = st->stack;
  2756. struct script_data* data;
  2757. int i;
  2758. if( start < 0 )
  2759. start = 0;
  2760. if( end > stack->sp )
  2761. end = stack->sp;
  2762. if( start >= end )
  2763. return;// nothing to pop
  2764. // free stack elements
  2765. for( i = start; i < end; i++ )
  2766. {
  2767. data = &stack->stack_data[i];
  2768. if( data->type == C_STR )
  2769. aFree(data->u.str);
  2770. if( data->type == C_RETINFO )
  2771. {
  2772. struct script_retinfo* ri = data->u.ri;
  2773. if( ri->var_function )
  2774. script_free_vars(ri->var_function);
  2775. if( data->ref )
  2776. aFree(data->ref);
  2777. aFree(ri);
  2778. }
  2779. data->type = C_NOP;
  2780. }
  2781. // move the rest of the elements
  2782. if( stack->sp > end )
  2783. {
  2784. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2785. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2786. stack->stack_data[i].type = C_NOP;
  2787. }
  2788. // adjust stack pointers
  2789. if( st->start > end ) st->start -= end - start;
  2790. else if( st->start > start ) st->start = start;
  2791. if( st->end > end ) st->end -= end - start;
  2792. else if( st->end > start ) st->end = start;
  2793. if( stack->defsp > end ) stack->defsp -= end - start;
  2794. else if( stack->defsp > start ) stack->defsp = start;
  2795. stack->sp -= end - start;
  2796. }
  2797. ///
  2798. ///
  2799. ///
  2800. /*==========================================
  2801. * Release script dependent variable, dependent variable of function
  2802. *------------------------------------------*/
  2803. void script_free_vars(struct DBMap* storage)
  2804. {
  2805. if( storage )
  2806. {// destroy the storage construct containing the variables
  2807. db_destroy(storage);
  2808. }
  2809. }
  2810. void script_free_code(struct script_code* code)
  2811. {
  2812. script_free_vars( code->script_vars );
  2813. aFree( code->script_buf );
  2814. aFree( code );
  2815. }
  2816. /// Creates a new script state.
  2817. ///
  2818. /// @param script Script code
  2819. /// @param pos Position in the code
  2820. /// @param rid Who is running the script (attached player)
  2821. /// @param oid Where the code is being run (npc 'object')
  2822. /// @return Script state
  2823. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2824. {
  2825. struct script_state* st;
  2826. CREATE(st, struct script_state, 1);
  2827. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2828. st->stack->sp = 0;
  2829. st->stack->sp_max = 64;
  2830. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2831. st->stack->defsp = st->stack->sp;
  2832. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  2833. st->state = RUN;
  2834. st->script = script;
  2835. //st->scriptroot = script;
  2836. st->pos = pos;
  2837. st->rid = rid;
  2838. st->oid = oid;
  2839. st->sleep.timer = INVALID_TIMER;
  2840. st->npc_item_flag = battle_config.item_enabled_npc;
  2841. st->atcommand_enable_npc = battle_config.atcommand_enable_npc;
  2842. return st;
  2843. }
  2844. /// Frees a script state.
  2845. ///
  2846. /// @param st Script state
  2847. void script_free_state(struct script_state* st)
  2848. {
  2849. if(st->bk_st)
  2850. {// backup was not restored
  2851. 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);
  2852. }
  2853. if( st->sleep.timer != INVALID_TIMER )
  2854. delete_timer(st->sleep.timer, run_script_timer);
  2855. script_free_vars(st->stack->var_function);
  2856. pop_stack(st, 0, st->stack->sp);
  2857. aFree(st->stack->stack_data);
  2858. aFree(st->stack);
  2859. st->stack = NULL;
  2860. st->pos = -1;
  2861. aFree(st);
  2862. }
  2863. //
  2864. // Main execution unit
  2865. //
  2866. /*==========================================
  2867. * Read command
  2868. *------------------------------------------*/
  2869. c_op get_com(unsigned char *script,int *pos)
  2870. {
  2871. int i = 0, j = 0;
  2872. if(script[*pos]>=0x80){
  2873. return C_INT;
  2874. }
  2875. while(script[*pos]>=0x40){
  2876. i=script[(*pos)++]<<j;
  2877. j+=6;
  2878. }
  2879. return (c_op)(i+(script[(*pos)++]<<j));
  2880. }
  2881. /*==========================================
  2882. * Income figures
  2883. *------------------------------------------*/
  2884. int get_num(unsigned char *script,int *pos)
  2885. {
  2886. int i,j;
  2887. i=0; j=0;
  2888. while(script[*pos]>=0xc0){
  2889. i+=(script[(*pos)++]&0x7f)<<j;
  2890. j+=6;
  2891. }
  2892. return i+((script[(*pos)++]&0x7f)<<j);
  2893. }
  2894. /*==========================================
  2895. * Remove the value from the stack
  2896. *------------------------------------------*/
  2897. int pop_val(struct script_state* st)
  2898. {
  2899. if(st->stack->sp<=0)
  2900. return 0;
  2901. st->stack->sp--;
  2902. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2903. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2904. return st->stack->stack_data[st->stack->sp].u.num;
  2905. return 0;
  2906. }
  2907. /// Ternary operators
  2908. /// test ? if_true : if_false
  2909. void op_3(struct script_state* st, int op)
  2910. {
  2911. struct script_data* data;
  2912. int flag = 0;
  2913. data = script_getdatatop(st, -3);
  2914. get_val(st, data);
  2915. if( data_isstring(data) )
  2916. flag = data->u.str[0];// "" -> false
  2917. else if( data_isint(data) )
  2918. flag = data->u.num;// 0 -> false
  2919. else
  2920. {
  2921. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2922. script_reportdata(data);
  2923. script_reportsrc(st);
  2924. script_removetop(st, -3, 0);
  2925. script_pushnil(st);
  2926. return;
  2927. }
  2928. if( flag )
  2929. script_pushcopytop(st, -2);
  2930. else
  2931. script_pushcopytop(st, -1);
  2932. script_removetop(st, -4, -1);
  2933. }
  2934. /// Binary string operators
  2935. /// s1 EQ s2 -> i
  2936. /// s1 NE s2 -> i
  2937. /// s1 GT s2 -> i
  2938. /// s1 GE s2 -> i
  2939. /// s1 LT s2 -> i
  2940. /// s1 LE s2 -> i
  2941. /// s1 ADD s2 -> s
  2942. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2943. {
  2944. int a = 0;
  2945. switch(op){
  2946. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2947. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2948. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2949. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2950. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2951. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2952. case C_ADD:
  2953. {
  2954. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2955. strcpy(buf, s1);
  2956. strcat(buf, s2);
  2957. script_pushstr(st, buf);
  2958. return;
  2959. }
  2960. default:
  2961. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2962. script_reportsrc(st);
  2963. script_pushnil(st);
  2964. st->state = END;
  2965. return;
  2966. }
  2967. script_pushint(st,a);
  2968. }
  2969. /// Binary number operators
  2970. /// i OP i -> i
  2971. void op_2num(struct script_state* st, int op, int i1, int i2)
  2972. {
  2973. int ret;
  2974. double ret_double;
  2975. switch( op )
  2976. {
  2977. case C_AND: ret = i1 & i2; break;
  2978. case C_OR: ret = i1 | i2; break;
  2979. case C_XOR: ret = i1 ^ i2; break;
  2980. case C_LAND: ret = (i1 && i2); break;
  2981. case C_LOR: ret = (i1 || i2); break;
  2982. case C_EQ: ret = (i1 == i2); break;
  2983. case C_NE: ret = (i1 != i2); break;
  2984. case C_GT: ret = (i1 > i2); break;
  2985. case C_GE: ret = (i1 >= i2); break;
  2986. case C_LT: ret = (i1 < i2); break;
  2987. case C_LE: ret = (i1 <= i2); break;
  2988. case C_R_SHIFT: ret = i1>>i2; break;
  2989. case C_L_SHIFT: ret = i1<<i2; break;
  2990. case C_DIV:
  2991. case C_MOD:
  2992. if( i2 == 0 )
  2993. {
  2994. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2995. script_reportsrc(st);
  2996. script_pushnil(st);
  2997. st->state = END;
  2998. return;
  2999. }
  3000. else if( op == C_DIV )
  3001. ret = i1 / i2;
  3002. else//if( op == C_MOD )
  3003. ret = i1 % i2;
  3004. break;
  3005. default:
  3006. switch( op )
  3007. {// operators that can overflow/underflow
  3008. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3009. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3010. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3011. default:
  3012. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3013. script_reportsrc(st);
  3014. script_pushnil(st);
  3015. return;
  3016. }
  3017. if( ret_double < (double)INT_MIN )
  3018. {
  3019. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3020. script_reportsrc(st);
  3021. ret = INT_MIN;
  3022. }
  3023. else if( ret_double > (double)INT_MAX )
  3024. {
  3025. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3026. script_reportsrc(st);
  3027. ret = INT_MAX;
  3028. }
  3029. }
  3030. script_pushint(st, ret);
  3031. }
  3032. /// Binary operators
  3033. void op_2(struct script_state *st, int op)
  3034. {
  3035. struct script_data* left, leftref;
  3036. struct script_data* right;
  3037. leftref.type = C_NOP;
  3038. left = script_getdatatop(st, -2);
  3039. right = script_getdatatop(st, -1);
  3040. if (st->op2ref) {
  3041. if (data_isreference(left)) {
  3042. leftref = *left;
  3043. }
  3044. st->op2ref = 0;
  3045. }
  3046. get_val(st, left);
  3047. get_val(st, right);
  3048. // automatic conversions
  3049. switch( op )
  3050. {
  3051. case C_ADD:
  3052. if( data_isint(left) && data_isstring(right) )
  3053. {// convert int-string to string-string
  3054. conv_str(st, left);
  3055. }
  3056. else if( data_isstring(left) && data_isint(right) )
  3057. {// convert string-int to string-string
  3058. conv_str(st, right);
  3059. }
  3060. break;
  3061. }
  3062. if( data_isstring(left) && data_isstring(right) )
  3063. {// ss => op_2str
  3064. op_2str(st, op, left->u.str, right->u.str);
  3065. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3066. if (leftref.type != C_NOP)
  3067. {
  3068. if (left->type == C_STR) // don't free C_CONSTSTR
  3069. aFree(left->u.str);
  3070. *left = leftref;
  3071. }
  3072. }
  3073. else if( data_isint(left) && data_isint(right) )
  3074. {// ii => op_2num
  3075. int i1 = left->u.num;
  3076. int i2 = right->u.num;
  3077. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3078. op_2num(st, op, i1, i2);
  3079. if (leftref.type != C_NOP)
  3080. *left = leftref;
  3081. }
  3082. else
  3083. {// invalid argument
  3084. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3085. script_reportdata(left);
  3086. script_reportdata(right);
  3087. script_reportsrc(st);
  3088. script_removetop(st, -2, 0);
  3089. script_pushnil(st);
  3090. st->state = END;
  3091. }
  3092. }
  3093. /// Unary operators
  3094. /// NEG i -> i
  3095. /// NOT i -> i
  3096. /// LNOT i -> i
  3097. void op_1(struct script_state* st, int op)
  3098. {
  3099. struct script_data* data;
  3100. int i1;
  3101. data = script_getdatatop(st, -1);
  3102. get_val(st, data);
  3103. if( !data_isint(data) )
  3104. {// not a number
  3105. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3106. script_reportdata(data);
  3107. script_reportsrc(st);
  3108. script_pushnil(st);
  3109. st->state = END;
  3110. return;
  3111. }
  3112. i1 = data->u.num;
  3113. script_removetop(st, -1, 0);
  3114. switch( op )
  3115. {
  3116. case C_NEG: i1 = -i1; break;
  3117. case C_NOT: i1 = ~i1; break;
  3118. case C_LNOT: i1 = !i1; break;
  3119. default:
  3120. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3121. script_reportsrc(st);
  3122. script_pushnil(st);
  3123. st->state = END;
  3124. return;
  3125. }
  3126. script_pushint(st, i1);
  3127. }
  3128. /// Checks the type of all arguments passed to a built-in function.
  3129. ///
  3130. /// @param st Script state whose stack arguments should be inspected.
  3131. /// @param func Built-in function for which the arguments are intended.
  3132. static void script_check_buildin_argtype(struct script_state* st, int func)
  3133. {
  3134. char type;
  3135. int idx, invalid = 0;
  3136. script_function* sf = &buildin_func[str_data[func].val];
  3137. for( idx = 2; script_hasdata(st, idx); idx++ )
  3138. {
  3139. struct script_data* data = script_getdata(st, idx);
  3140. type = sf->arg[idx-2];
  3141. if( type == '?' || type == '*' )
  3142. {// optional argument or unknown number of optional parameters ( no types are after this )
  3143. break;
  3144. }
  3145. else if( type == 0 )
  3146. {// more arguments than necessary ( should not happen, as it is checked before )
  3147. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3148. invalid++;
  3149. break;
  3150. }
  3151. else
  3152. {
  3153. const char* name = NULL;
  3154. if( data_isreference(data) )
  3155. {// get name for variables to determine the type they refer to
  3156. name = reference_getname(data);
  3157. }
  3158. switch( type )
  3159. {
  3160. case 'v':
  3161. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3162. {// variant
  3163. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3164. script_reportdata(data);
  3165. invalid++;
  3166. }
  3167. break;
  3168. case 's':
  3169. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3170. {// string
  3171. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3172. script_reportdata(data);
  3173. invalid++;
  3174. }
  3175. break;
  3176. case 'i':
  3177. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3178. {// int ( params and constants are always int )
  3179. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3180. script_reportdata(data);
  3181. invalid++;
  3182. }
  3183. break;
  3184. case 'r':
  3185. if( !data_isreference(data) )
  3186. {// variables
  3187. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3188. script_reportdata(data);
  3189. invalid++;
  3190. }
  3191. break;
  3192. case 'l':
  3193. if( !data_islabel(data) && !data_isfunclabel(data) )
  3194. {// label
  3195. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3196. script_reportdata(data);
  3197. invalid++;
  3198. }
  3199. break;
  3200. }
  3201. }
  3202. }
  3203. if(invalid)
  3204. {
  3205. ShowDebug("Function: %s\n", get_str(func));
  3206. script_reportsrc(st);
  3207. }
  3208. }
  3209. /// Executes a buildin command.
  3210. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3211. int run_func(struct script_state *st)
  3212. {
  3213. struct script_data* data;
  3214. int i,start_sp,end_sp,func;
  3215. end_sp = st->stack->sp;// position after the last argument
  3216. for( i = end_sp-1; i > 0 ; --i )
  3217. if( st->stack->stack_data[i].type == C_ARG )
  3218. break;
  3219. if( i == 0 ) {
  3220. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3221. st->state = END;
  3222. script_reportsrc(st);
  3223. return 1;
  3224. }
  3225. start_sp = i-1;// C_NAME of the command
  3226. st->start = start_sp;
  3227. st->end = end_sp;
  3228. data = &st->stack->stack_data[st->start];
  3229. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3230. func = data->u.num;
  3231. st->funcname = reference_getname(data);
  3232. } else {
  3233. ShowError("script:run_func: not a buildin command.\n");
  3234. script_reportdata(data);
  3235. script_reportsrc(st);
  3236. st->state = END;
  3237. return 1;
  3238. }
  3239. if( script_config.warn_func_mismatch_argtypes ) {
  3240. script_check_buildin_argtype(st, func);
  3241. }
  3242. if(str_data[func].func) {
  3243. if (str_data[func].func(st)) //Report error
  3244. script_reportsrc(st);
  3245. } else {
  3246. 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));
  3247. script_reportsrc(st);
  3248. st->state = END;
  3249. }
  3250. // Stack's datum are used when re-running functions [Eoe]
  3251. if( st->state == RERUNLINE )
  3252. return 0;
  3253. pop_stack(st, st->start, st->end);
  3254. if( st->state == RETFUNC ) {// return from a user-defined function
  3255. struct script_retinfo* ri;
  3256. int olddefsp = st->stack->defsp;
  3257. int nargs;
  3258. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3259. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3260. {
  3261. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3262. script_reportsrc(st);
  3263. st->state = END;
  3264. return 1;
  3265. }
  3266. script_free_vars( st->stack->var_function );
  3267. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3268. nargs = ri->nargs;
  3269. st->pos = ri->pos;
  3270. st->script = ri->script;
  3271. st->stack->var_function = ri->var_function;
  3272. st->stack->defsp = ri->defsp;
  3273. memset(ri, 0, sizeof(struct script_retinfo));
  3274. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3275. st->state = GOTO;
  3276. }
  3277. return 0;
  3278. }
  3279. /*==========================================
  3280. * script execution
  3281. *------------------------------------------*/
  3282. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3283. {
  3284. struct script_state *st;
  3285. if( rootscript == NULL || pos < 0 )
  3286. return;
  3287. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3288. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3289. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3290. st = script_alloc_state(rootscript, pos, rid, oid);
  3291. run_script_main(st);
  3292. }
  3293. void script_stop_sleeptimers(int id)
  3294. {
  3295. struct script_state* st;
  3296. for(;;)
  3297. {
  3298. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3299. if( st == NULL )
  3300. break; // no more sleep timers
  3301. script_free_state(st);
  3302. }
  3303. }
  3304. /*==========================================
  3305. * Delete the specified node from sleep_db
  3306. *------------------------------------------*/
  3307. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3308. {
  3309. struct linkdb_node *retnode;
  3310. if( n == NULL)
  3311. return NULL;
  3312. if( n->prev == NULL )
  3313. sleep_db = n->next;
  3314. else
  3315. n->prev->next = n->next;
  3316. if( n->next )
  3317. n->next->prev = n->prev;
  3318. retnode = n->next;
  3319. aFree( n );
  3320. return retnode; // The following; return retnode
  3321. }
  3322. /*==========================================
  3323. * Timer function for sleep
  3324. *------------------------------------------*/
  3325. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3326. {
  3327. struct script_state *st = (struct script_state *)data;
  3328. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3329. TBL_PC *sd = map_id2sd(st->rid);
  3330. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3331. { //Character mismatch. Cancel execution.
  3332. st->rid = 0;
  3333. st->state = END;
  3334. }
  3335. while( node && st->sleep.timer != INVALID_TIMER ) {
  3336. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3337. script_erase_sleepdb(node);
  3338. st->sleep.timer = INVALID_TIMER;
  3339. break;
  3340. }
  3341. node = node->next;
  3342. }
  3343. if(st->state != RERUNLINE)
  3344. st->sleep.tick = 0;
  3345. run_script_main(st);
  3346. return 0;
  3347. }
  3348. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3349. ///
  3350. /// @param st Script state to detach.
  3351. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3352. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3353. {
  3354. struct map_session_data* sd;
  3355. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3356. sd->st = st->bk_st;
  3357. sd->npc_id = st->bk_npcid;
  3358. if(st->bk_st) {
  3359. //Remove tag for removal.
  3360. st->bk_st = NULL;
  3361. st->bk_npcid = 0;
  3362. } else if(dequeue_event) {
  3363. #ifdef SECURE_NPCTIMEOUT
  3364. /**
  3365. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3366. **/
  3367. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3368. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3369. sd->npc_idle_timer = INVALID_TIMER;
  3370. }
  3371. #endif
  3372. npc_event_dequeue(sd);
  3373. }
  3374. }
  3375. else if(st->bk_st)
  3376. {// rid was set to 0, before detaching the script state
  3377. 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);
  3378. script_reportsrc(st->bk_st);
  3379. script_free_state(st->bk_st);
  3380. st->bk_st = NULL;
  3381. }
  3382. }
  3383. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3384. ///
  3385. /// @param st Script state to attach.
  3386. static void script_attach_state(struct script_state* st)
  3387. {
  3388. struct map_session_data* sd;
  3389. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3390. {
  3391. if(st!=sd->st)
  3392. {
  3393. if(st->bk_st)
  3394. {// there is already a backup
  3395. 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);
  3396. }
  3397. st->bk_st = sd->st;
  3398. st->bk_npcid = sd->npc_id;
  3399. }
  3400. sd->st = st;
  3401. sd->npc_id = st->oid;
  3402. sd->npc_item_flag = st->npc_item_flag; // load default.
  3403. sd->state.disable_atcommand_on_npc = (pc_get_group_level(sd) >= st->atcommand_enable_npc) ? false : true;
  3404. #ifdef SECURE_NPCTIMEOUT
  3405. if( sd->npc_idle_timer == INVALID_TIMER )
  3406. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3407. sd->npc_idle_tick = gettick();
  3408. #endif
  3409. }
  3410. }
  3411. /*==========================================
  3412. * The main part of the script execution
  3413. *------------------------------------------*/
  3414. void run_script_main(struct script_state *st)
  3415. {
  3416. int cmdcount = script_config.check_cmdcount;
  3417. int gotocount = script_config.check_gotocount;
  3418. TBL_PC *sd;
  3419. struct script_stack *stack=st->stack;
  3420. script_attach_state(st);
  3421. if(st->state == RERUNLINE) {
  3422. run_func(st);
  3423. if(st->state == GOTO)
  3424. st->state = RUN;
  3425. } else if(st->state != END)
  3426. st->state = RUN;
  3427. while(st->state == RUN)
  3428. {
  3429. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3430. switch(c){
  3431. case C_EOL:
  3432. if( stack->defsp > stack->sp )
  3433. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3434. else
  3435. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3436. break;
  3437. case C_INT:
  3438. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3439. break;
  3440. case C_POS:
  3441. case C_NAME:
  3442. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3443. st->pos+=3;
  3444. break;
  3445. case C_ARG:
  3446. push_val(stack,c,0);
  3447. break;
  3448. case C_STR:
  3449. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3450. while(st->script->script_buf[st->pos++]);
  3451. break;
  3452. case C_FUNC:
  3453. run_func(st);
  3454. if(st->state==GOTO){
  3455. st->state = RUN;
  3456. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3457. ShowError("run_script: infinity loop !\n");
  3458. script_reportsrc(st);
  3459. st->state=END;
  3460. }
  3461. }
  3462. break;
  3463. case C_REF:
  3464. st->op2ref = 1;
  3465. break;
  3466. case C_NEG:
  3467. case C_NOT:
  3468. case C_LNOT:
  3469. op_1(st ,c);
  3470. break;
  3471. case C_ADD:
  3472. case C_SUB:
  3473. case C_MUL:
  3474. case C_DIV:
  3475. case C_MOD:
  3476. case C_EQ:
  3477. case C_NE:
  3478. case C_GT:
  3479. case C_GE:
  3480. case C_LT:
  3481. case C_LE:
  3482. case C_AND:
  3483. case C_OR:
  3484. case C_XOR:
  3485. case C_LAND:
  3486. case C_LOR:
  3487. case C_R_SHIFT:
  3488. case C_L_SHIFT:
  3489. op_2(st, c);
  3490. break;
  3491. case C_OP3:
  3492. op_3(st, c);
  3493. break;
  3494. case C_NOP:
  3495. st->state=END;
  3496. break;
  3497. default:
  3498. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3499. st->state=END;
  3500. break;
  3501. }
  3502. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3503. ShowError("run_script: infinity loop !\n");
  3504. script_reportsrc(st);
  3505. st->state=END;
  3506. }
  3507. }
  3508. if(st->sleep.tick > 0) {
  3509. //Restore previous script
  3510. script_detach_state(st, false);
  3511. //Delay execution
  3512. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3513. st->sleep.charid = sd?sd->status.char_id:0;
  3514. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3515. run_script_timer, st->sleep.charid, (intptr_t)st);
  3516. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3517. }
  3518. else if(st->state != END && st->rid){
  3519. //Resume later (st is already attached to player).
  3520. if(st->bk_st) {
  3521. ShowWarning("Unable to restore stack! Double continuation!\n");
  3522. //Report BOTH scripts to see if that can help somehow.
  3523. ShowDebug("Previous script (lost):\n");
  3524. script_reportsrc(st->bk_st);
  3525. ShowDebug("Current script:\n");
  3526. script_reportsrc(st);
  3527. script_free_state(st->bk_st);
  3528. st->bk_st = NULL;
  3529. }
  3530. } else {
  3531. //Dispose of script.
  3532. if ((sd = map_id2sd(st->rid))!=NULL)
  3533. { //Restore previous stack and save char.
  3534. if(sd->state.using_fake_npc){
  3535. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3536. sd->state.using_fake_npc = 0;
  3537. }
  3538. //Restore previous script if any.
  3539. script_detach_state(st, true);
  3540. if (sd->state.reg_dirty&2)
  3541. intif_saveregistry(sd,2);
  3542. if (sd->state.reg_dirty&1)
  3543. intif_saveregistry(sd,1);
  3544. }
  3545. script_free_state(st);
  3546. st = NULL;
  3547. }
  3548. }
  3549. int script_config_read(char *cfgName)
  3550. {
  3551. int i;
  3552. char line[1024],w1[1024],w2[1024];
  3553. FILE *fp;
  3554. fp=fopen(cfgName,"r");
  3555. if(fp==NULL){
  3556. ShowError("File not found: %s\n", cfgName);
  3557. return 1;
  3558. }
  3559. while(fgets(line, sizeof(line), fp))
  3560. {
  3561. if(line[0] == '/' && line[1] == '/')
  3562. continue;
  3563. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3564. if(i!=2)
  3565. continue;
  3566. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3567. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3568. }
  3569. else if(strcmpi(w1,"check_cmdcount")==0) {
  3570. script_config.check_cmdcount = config_switch(w2);
  3571. }
  3572. else if(strcmpi(w1,"check_gotocount")==0) {
  3573. script_config.check_gotocount = config_switch(w2);
  3574. }
  3575. else if(strcmpi(w1,"input_min_value")==0) {
  3576. script_config.input_min_value = config_switch(w2);
  3577. }
  3578. else if(strcmpi(w1,"input_max_value")==0) {
  3579. script_config.input_max_value = config_switch(w2);
  3580. }
  3581. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3582. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3583. }
  3584. else if(strcmpi(w1,"import")==0){
  3585. script_config_read(w2);
  3586. }
  3587. else {
  3588. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3589. }
  3590. }
  3591. fclose(fp);
  3592. return 0;
  3593. }
  3594. /**
  3595. * @see DBApply
  3596. */
  3597. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3598. {
  3599. struct script_code *code = db_data2ptr(data);
  3600. if (code)
  3601. script_free_code(code);
  3602. return 0;
  3603. }
  3604. void script_run_autobonus(const char *autobonus, int id, int pos)
  3605. {
  3606. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3607. if( script )
  3608. {
  3609. current_equip_item_index = pos;
  3610. run_script(script,0,id,0);
  3611. }
  3612. }
  3613. void script_add_autobonus(const char *autobonus)
  3614. {
  3615. if( strdb_get(autobonus_db, autobonus) == NULL )
  3616. {
  3617. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3618. if( script )
  3619. strdb_put(autobonus_db, autobonus, script);
  3620. }
  3621. }
  3622. /// resets a temporary character array variable to given value
  3623. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3624. {
  3625. int key;
  3626. uint8 idx;
  3627. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3628. {
  3629. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3630. return;
  3631. }
  3632. key = add_str(varname);
  3633. if( is_string_variable(varname) )
  3634. {
  3635. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3636. {
  3637. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3638. }
  3639. }
  3640. else
  3641. {
  3642. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3643. {
  3644. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3645. }
  3646. }
  3647. }
  3648. /// sets a temporary character array variable element idx to given value
  3649. /// @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.
  3650. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3651. {
  3652. int key;
  3653. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3654. {
  3655. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3656. return;
  3657. }
  3658. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3659. {
  3660. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3661. return;
  3662. }
  3663. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3664. if( is_string_variable(varname) )
  3665. {
  3666. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3667. }
  3668. else
  3669. {
  3670. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3671. }
  3672. if( refcache )
  3673. {// save to avoid repeated add_str calls
  3674. refcache[0] = key;
  3675. }
  3676. }
  3677. #ifdef BETA_THREAD_TEST
  3678. int buildin_query_sql_sub(struct script_state* st, Sql* handle);
  3679. /* used to receive items the queryThread has already processed */
  3680. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3681. int i, cursor = 0;
  3682. bool allOk = true;
  3683. EnterSpinLock(&queryThreadLock);
  3684. for( i = 0; i < queryThreadData.count; i++ ) {
  3685. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3686. if( !entry->ok ) {
  3687. allOk = false;
  3688. continue;
  3689. }
  3690. run_script_main(entry->st);
  3691. entry->st = NULL;/* empty entries */
  3692. aFree(entry);
  3693. queryThreadData.entry[i] = NULL;
  3694. }
  3695. if( allOk ) {
  3696. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3697. delete_timer(queryThreadData.timer, queryThread_timer);
  3698. queryThreadData.timer = INVALID_TIMER;
  3699. }
  3700. /* now lets clear the mess. */
  3701. for( i = 0; i < queryThreadData.count; i++ ) {
  3702. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3703. if( entry == NULL )
  3704. continue;/* entry on hold */
  3705. /* move */
  3706. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3707. cursor++;
  3708. }
  3709. queryThreadData.count = cursor;
  3710. LeaveSpinLock(&queryThreadLock);
  3711. return 0;
  3712. }
  3713. void queryThread_add(struct script_state *st, bool type) {
  3714. int idx = 0;
  3715. struct queryThreadEntry* entry = NULL;
  3716. EnterSpinLock(&queryThreadLock);
  3717. if( queryThreadData.count++ != 0 )
  3718. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3719. idx = queryThreadData.count-1;
  3720. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3721. entry = queryThreadData.entry[idx];
  3722. entry->st = st;
  3723. entry->ok = false;
  3724. entry->type = type;
  3725. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3726. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3727. }
  3728. LeaveSpinLock(&queryThreadLock);
  3729. /* unlock the queryThread */
  3730. racond_signal(queryThreadCond);
  3731. }
  3732. /* adds a new log to the queue */
  3733. void queryThread_log(char * entry, int length) {
  3734. int idx = logThreadData.count;
  3735. EnterSpinLock(&queryThreadLock);
  3736. if( logThreadData.count++ != 0 )
  3737. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3738. CREATE(logThreadData.entry[idx], char, length + 1 );
  3739. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3740. LeaveSpinLock(&queryThreadLock);
  3741. /* unlock the queryThread */
  3742. racond_signal(queryThreadCond);
  3743. }
  3744. /* queryThread_main */
  3745. static void *queryThread_main(void *x) {
  3746. Sql *queryThread_handle = Sql_Malloc();
  3747. int i;
  3748. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) )
  3749. exit(EXIT_FAILURE);
  3750. if( strlen(default_codepage) > 0 )
  3751. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3752. Sql_ShowDebug(queryThread_handle);
  3753. if( log_config.sql_logs ) {
  3754. logmysql_handle = Sql_Malloc();
  3755. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) )
  3756. exit(EXIT_FAILURE);
  3757. if( strlen(default_codepage) > 0 )
  3758. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3759. Sql_ShowDebug(logmysql_handle);
  3760. }
  3761. while( 1 ) {
  3762. if(queryThreadTerminate > 0)
  3763. break;
  3764. EnterSpinLock(&queryThreadLock);
  3765. /* mess with queryThreadData within the lock */
  3766. for( i = 0; i < queryThreadData.count; i++ ) {
  3767. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3768. if( entry->ok )
  3769. continue;
  3770. else if ( !entry->st || !entry->st->stack ) {
  3771. entry->ok = true;/* dispose */
  3772. continue;
  3773. }
  3774. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  3775. entry->ok = true;/* we're done with this */
  3776. }
  3777. /* also check for any logs in need to be sent */
  3778. if( log_config.sql_logs ) {
  3779. for( i = 0; i < logThreadData.count; i++ ) {
  3780. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  3781. Sql_ShowDebug(logmysql_handle);
  3782. aFree(logThreadData.entry[i]);
  3783. }
  3784. logThreadData.count = 0;
  3785. }
  3786. LeaveSpinLock(&queryThreadLock);
  3787. ramutex_lock( queryThreadMutex );
  3788. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  3789. ramutex_unlock( queryThreadMutex );
  3790. }
  3791. Sql_Free(queryThread_handle);
  3792. if( log_config.sql_logs ) {
  3793. Sql_Free(logmysql_handle);
  3794. }
  3795. return NULL;
  3796. }
  3797. #endif
  3798. /*==========================================
  3799. * Destructor
  3800. *------------------------------------------*/
  3801. void do_final_script() {
  3802. int i;
  3803. #ifdef DEBUG_HASH
  3804. if (battle_config.etc_log)
  3805. {
  3806. FILE *fp = fopen("hash_dump.txt","wt");
  3807. if(fp) {
  3808. int count[SCRIPT_HASH_SIZE];
  3809. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3810. int n=0;
  3811. int min=INT_MAX,max=0,zero=0;
  3812. double mean=0.0f;
  3813. double median=0.0f;
  3814. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3815. memset(count, 0, sizeof(count));
  3816. fprintf(fp,"num : hash : data_name\n");
  3817. fprintf(fp,"---------------------------------------------------------------\n");
  3818. for(i=LABEL_START; i<str_num; i++) {
  3819. unsigned int h = calc_hash(get_str(i));
  3820. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3821. ++count[h];
  3822. }
  3823. fprintf(fp,"--------------------\n\n");
  3824. memset(count2, 0, sizeof(count2));
  3825. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3826. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3827. if(min > count[i])
  3828. min = count[i]; // minimun count of collision
  3829. if(max < count[i])
  3830. max = count[i]; // maximun count of collision
  3831. if(count[i] == 0)
  3832. zero++;
  3833. ++count2[count[i]];
  3834. }
  3835. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3836. for( i=min; i <= max; ++i ){
  3837. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3838. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3839. }
  3840. for( i=min; i <= max; ++i ){
  3841. n += count2[i];
  3842. if( n*2 >= SCRIPT_HASH_SIZE )
  3843. {
  3844. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3845. median = (i+i+1)/2.0f;
  3846. else
  3847. median = i;
  3848. break;
  3849. }
  3850. }
  3851. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3852. fclose(fp);
  3853. }
  3854. }
  3855. #endif
  3856. mapreg_final();
  3857. db_destroy(scriptlabel_db);
  3858. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3859. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3860. if(sleep_db) {
  3861. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3862. while(n) {
  3863. struct script_state *st = (struct script_state *)n->data;
  3864. script_free_state(st);
  3865. n = n->next;
  3866. }
  3867. linkdb_final(&sleep_db);
  3868. }
  3869. if (str_data)
  3870. aFree(str_data);
  3871. if (str_buf)
  3872. aFree(str_buf);
  3873. for( i = 0; i < atcmd_binding_count; i++ ) {
  3874. aFree(atcmd_binding[i]);
  3875. }
  3876. if( atcmd_binding_count != 0 )
  3877. aFree(atcmd_binding);
  3878. #ifdef BETA_THREAD_TEST
  3879. /* QueryThread */
  3880. InterlockedIncrement(&queryThreadTerminate);
  3881. racond_signal(queryThreadCond);
  3882. rathread_wait(queryThread, NULL);
  3883. // Destroy cond var and mutex.
  3884. racond_destroy( queryThreadCond );
  3885. ramutex_destroy( queryThreadMutex );
  3886. /* Clear missing vars */
  3887. for( i = 0; i < queryThreadData.count; i++ ) {
  3888. aFree(queryThreadData.entry[i]);
  3889. }
  3890. aFree(queryThreadData.entry);
  3891. for( i = 0; i < logThreadData.count; i++ ) {
  3892. aFree(logThreadData.entry[i]);
  3893. }
  3894. aFree(logThreadData.entry);
  3895. #endif
  3896. }
  3897. /*==========================================
  3898. * Initialization
  3899. *------------------------------------------*/
  3900. void do_init_script(void) {
  3901. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3902. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3903. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3904. mapreg_init();
  3905. #ifdef BETA_THREAD_TEST
  3906. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  3907. queryThreadData.count = 0;
  3908. CREATE(logThreadData.entry, char *, 1);
  3909. logThreadData.count = 0;
  3910. /* QueryThread Start */
  3911. InitializeSpinLock(&queryThreadLock);
  3912. queryThreadData.timer = INVALID_TIMER;
  3913. queryThreadTerminate = 0;
  3914. queryThreadMutex = ramutex_create();
  3915. queryThreadCond = racond_create();
  3916. queryThread = rathread_create(queryThread_main, NULL);
  3917. if(queryThread == NULL){
  3918. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  3919. exit(EXIT_FAILURE);
  3920. }
  3921. add_timer_func_list(queryThread_timer, "queryThread_timer");
  3922. #endif
  3923. }
  3924. void script_reload(void) {
  3925. int i;
  3926. #ifdef BETA_THREAD_TEST
  3927. /* we're reloading so any queries undergoing should be...exterminated. */
  3928. EnterSpinLock(&queryThreadLock);
  3929. for( i = 0; i < queryThreadData.count; i++ ) {
  3930. aFree(queryThreadData.entry[i]);
  3931. }
  3932. queryThreadData.count = 0;
  3933. if( queryThreadData.timer != INVALID_TIMER ) {
  3934. delete_timer(queryThreadData.timer, queryThread_timer);
  3935. queryThreadData.timer = INVALID_TIMER;
  3936. }
  3937. LeaveSpinLock(&queryThreadLock);
  3938. #endif
  3939. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  3940. db_clear(scriptlabel_db);
  3941. // @commands (script based)
  3942. // Clear bindings
  3943. for( i = 0; i < atcmd_binding_count; i++ ) {
  3944. aFree(atcmd_binding[i]);
  3945. }
  3946. if( atcmd_binding_count != 0 )
  3947. aFree(atcmd_binding);
  3948. atcmd_binding_count = 0;
  3949. if(sleep_db) {
  3950. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3951. while(n) {
  3952. struct script_state *st = (struct script_state *)n->data;
  3953. script_free_state(st);
  3954. n = n->next;
  3955. }
  3956. linkdb_final(&sleep_db);
  3957. }
  3958. mapreg_reload();
  3959. }
  3960. //-----------------------------------------------------------------------------
  3961. // buildin functions
  3962. //
  3963. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3964. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3965. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3966. /////////////////////////////////////////////////////////////////////
  3967. // NPC interaction
  3968. //
  3969. /// Appends a message to the npc dialog.
  3970. /// If a dialog doesn't exist yet, one is created.
  3971. ///
  3972. /// mes "<message>";
  3973. BUILDIN_FUNC(mes)
  3974. {
  3975. TBL_PC* sd = script_rid2sd(st);
  3976. if( sd == NULL )
  3977. return 0;
  3978. if( !script_hasdata(st, 3) )
  3979. {// only a single line detected in the script
  3980. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3981. }
  3982. else
  3983. {// parse multiple lines as they exist
  3984. int i;
  3985. for( i = 2; script_hasdata(st, i); i++ )
  3986. {
  3987. // send the message to the client
  3988. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3989. }
  3990. }
  3991. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  3992. return SCRIPT_CMD_SUCCESS;
  3993. }
  3994. /// Displays the button 'next' in the npc dialog.
  3995. /// The dialog text is cleared and the script continues when the button is pressed.
  3996. ///
  3997. /// next;
  3998. BUILDIN_FUNC(next)
  3999. {
  4000. TBL_PC* sd;
  4001. sd = script_rid2sd(st);
  4002. if( sd == NULL )
  4003. return 0;
  4004. #ifdef SECURE_NPCTIMEOUT
  4005. sd->npc_idle_type = NPCT_WAIT;
  4006. #endif
  4007. st->state = STOP;
  4008. clif_scriptnext(sd, st->oid);
  4009. return SCRIPT_CMD_SUCCESS;
  4010. }
  4011. /// Ends the script and displays the button 'close' on the npc dialog.
  4012. /// The dialog is closed when the button is pressed.
  4013. ///
  4014. /// close;
  4015. BUILDIN_FUNC(close)
  4016. {
  4017. TBL_PC* sd;
  4018. sd = script_rid2sd(st);
  4019. if( sd == NULL )
  4020. return 0;
  4021. if( !st->mes_active ) {
  4022. TBL_NPC* nd = map_id2nd(st->oid);
  4023. st->state = END; // Keep backwards compatibility.
  4024. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4025. } else {
  4026. st->state = CLOSE;
  4027. st->mes_active = 0;
  4028. }
  4029. clif_scriptclose(sd, st->oid);
  4030. return SCRIPT_CMD_SUCCESS;
  4031. }
  4032. /// Displays the button 'close' on the npc dialog.
  4033. /// The dialog is closed and the script continues when the button is pressed.
  4034. ///
  4035. /// close2;
  4036. BUILDIN_FUNC(close2)
  4037. {
  4038. TBL_PC* sd;
  4039. sd = script_rid2sd(st);
  4040. if( sd == NULL )
  4041. return 0;
  4042. st->state = STOP;
  4043. if( st->mes_active )
  4044. st->mes_active = 0;
  4045. clif_scriptclose(sd, st->oid);
  4046. return SCRIPT_CMD_SUCCESS;
  4047. }
  4048. /// Counts the number of valid and total number of options in 'str'
  4049. /// If max_count > 0 the counting stops when that valid option is reached
  4050. /// total is incremented for each option (NULL is supported)
  4051. static int menu_countoptions(const char* str, int max_count, int* total)
  4052. {
  4053. int count = 0;
  4054. int bogus_total;
  4055. if( total == NULL )
  4056. total = &bogus_total;
  4057. ++(*total);
  4058. // initial empty options
  4059. while( *str == ':' )
  4060. {
  4061. ++str;
  4062. ++(*total);
  4063. }
  4064. // count menu options
  4065. while( *str != '\0' )
  4066. {
  4067. ++count;
  4068. --max_count;
  4069. if( max_count == 0 )
  4070. break;
  4071. while( *str != ':' && *str != '\0' )
  4072. ++str;
  4073. while( *str == ':' )
  4074. {
  4075. ++str;
  4076. ++(*total);
  4077. }
  4078. }
  4079. return count;
  4080. }
  4081. /// Displays a menu with options and goes to the target label.
  4082. /// The script is stopped if cancel is pressed.
  4083. /// Options with no text are not displayed in the client.
  4084. ///
  4085. /// Options can be grouped together, separated by the character ':' in the text:
  4086. /// ex: menu "A:B:C",L_target;
  4087. /// All these options go to the specified target label.
  4088. ///
  4089. /// The index of the selected option is put in the variable @menu.
  4090. /// Indexes start with 1 and are consistent with grouped and empty options.
  4091. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4092. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4093. ///
  4094. /// NOTE: the client closes the npc dialog when cancel is pressed
  4095. ///
  4096. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4097. BUILDIN_FUNC(menu)
  4098. {
  4099. int i;
  4100. const char* text;
  4101. TBL_PC* sd;
  4102. sd = script_rid2sd(st);
  4103. if( sd == NULL )
  4104. return 0;
  4105. #ifdef SECURE_NPCTIMEOUT
  4106. sd->npc_idle_type = NPCT_MENU;
  4107. #endif
  4108. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4109. if( sd->state.menu_or_input == 0 )
  4110. {
  4111. struct StringBuf buf;
  4112. struct script_data* data;
  4113. if( script_lastdata(st) % 2 == 0 )
  4114. {// argument count is not even (1st argument is at index 2)
  4115. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4116. st->state = END;
  4117. return SCRIPT_CMD_FAILURE;
  4118. }
  4119. StringBuf_Init(&buf);
  4120. sd->npc_menu = 0;
  4121. for( i = 2; i < script_lastdata(st); i += 2 )
  4122. {
  4123. // menu options
  4124. text = script_getstr(st, i);
  4125. // target label
  4126. data = script_getdata(st, i+1);
  4127. if( !data_islabel(data) )
  4128. {// not a label
  4129. StringBuf_Destroy(&buf);
  4130. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4131. script_reportdata(data);
  4132. st->state = END;
  4133. return SCRIPT_CMD_FAILURE;
  4134. }
  4135. // append option(s)
  4136. if( text[0] == '\0' )
  4137. continue;// empty string, ignore
  4138. if( sd->npc_menu > 0 )
  4139. StringBuf_AppendStr(&buf, ":");
  4140. StringBuf_AppendStr(&buf, text);
  4141. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4142. }
  4143. st->state = RERUNLINE;
  4144. sd->state.menu_or_input = 1;
  4145. /**
  4146. * menus beyond this length crash the client (see bugreport:6402)
  4147. **/
  4148. if( StringBuf_Length(&buf) >= 2047 ) {
  4149. struct npc_data * nd = map_id2nd(st->oid);
  4150. char* menu;
  4151. CREATE(menu, char, 2048);
  4152. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4153. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4154. clif_scriptmenu(sd, st->oid, menu);
  4155. aFree(menu);
  4156. } else
  4157. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4158. StringBuf_Destroy(&buf);
  4159. if( sd->npc_menu >= 0xff )
  4160. {// 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
  4161. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4162. script_reportsrc(st);
  4163. }
  4164. }
  4165. else if( sd->npc_menu == 0xff )
  4166. {// Cancel was pressed
  4167. sd->state.menu_or_input = 0;
  4168. st->state = END;
  4169. }
  4170. else
  4171. {// goto target label
  4172. int menu = 0;
  4173. sd->state.menu_or_input = 0;
  4174. if( sd->npc_menu <= 0 )
  4175. {
  4176. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4177. st->state = END;
  4178. return SCRIPT_CMD_FAILURE;
  4179. }
  4180. // get target label
  4181. for( i = 2; i < script_lastdata(st); i += 2 )
  4182. {
  4183. text = script_getstr(st, i);
  4184. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4185. if( sd->npc_menu <= 0 )
  4186. break;// entry found
  4187. }
  4188. if( sd->npc_menu > 0 )
  4189. {// Invalid selection
  4190. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4191. st->state = END;
  4192. return SCRIPT_CMD_FAILURE;
  4193. }
  4194. if( !data_islabel(script_getdata(st, i + 1)) )
  4195. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4196. ShowError("script:menu: unexpected data in label argument\n");
  4197. script_reportdata(script_getdata(st, i + 1));
  4198. st->state = END;
  4199. return SCRIPT_CMD_FAILURE;
  4200. }
  4201. pc_setreg(sd, add_str("@menu"), menu);
  4202. st->pos = script_getnum(st, i + 1);
  4203. st->state = GOTO;
  4204. }
  4205. return SCRIPT_CMD_SUCCESS;
  4206. }
  4207. /// Displays a menu with options and returns the selected option.
  4208. /// Behaves like 'menu' without the target labels.
  4209. ///
  4210. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4211. ///
  4212. /// @see menu
  4213. BUILDIN_FUNC(select)
  4214. {
  4215. int i;
  4216. const char* text;
  4217. TBL_PC* sd;
  4218. sd = script_rid2sd(st);
  4219. if( sd == NULL )
  4220. return 0;
  4221. #ifdef SECURE_NPCTIMEOUT
  4222. sd->npc_idle_type = NPCT_MENU;
  4223. #endif
  4224. if( sd->state.menu_or_input == 0 ) {
  4225. struct StringBuf buf;
  4226. StringBuf_Init(&buf);
  4227. sd->npc_menu = 0;
  4228. for( i = 2; i <= script_lastdata(st); ++i ) {
  4229. text = script_getstr(st, i);
  4230. if( sd->npc_menu > 0 )
  4231. StringBuf_AppendStr(&buf, ":");
  4232. StringBuf_AppendStr(&buf, text);
  4233. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4234. }
  4235. st->state = RERUNLINE;
  4236. sd->state.menu_or_input = 1;
  4237. /**
  4238. * menus beyond this length crash the client (see bugreport:6402)
  4239. **/
  4240. if( StringBuf_Length(&buf) >= 2047 ) {
  4241. struct npc_data * nd = map_id2nd(st->oid);
  4242. char* menu;
  4243. CREATE(menu, char, 2048);
  4244. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4245. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4246. clif_scriptmenu(sd, st->oid, menu);
  4247. aFree(menu);
  4248. } else
  4249. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4250. StringBuf_Destroy(&buf);
  4251. if( sd->npc_menu >= 0xff ) {
  4252. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4253. script_reportsrc(st);
  4254. }
  4255. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4256. sd->state.menu_or_input = 0;
  4257. st->state = END;
  4258. } else {// return selected option
  4259. int menu = 0;
  4260. sd->state.menu_or_input = 0;
  4261. for( i = 2; i <= script_lastdata(st); ++i ) {
  4262. text = script_getstr(st, i);
  4263. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4264. if( sd->npc_menu <= 0 )
  4265. break;// entry found
  4266. }
  4267. pc_setreg(sd, add_str("@menu"), menu);
  4268. script_pushint(st, menu);
  4269. st->state = RUN;
  4270. }
  4271. return SCRIPT_CMD_SUCCESS;
  4272. }
  4273. /// Displays a menu with options and returns the selected option.
  4274. /// Behaves like 'menu' without the target labels, except when cancel is
  4275. /// pressed.
  4276. /// When cancel is pressed, the script continues and 255 is returned.
  4277. ///
  4278. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4279. ///
  4280. /// @see menu
  4281. BUILDIN_FUNC(prompt)
  4282. {
  4283. int i;
  4284. const char *text;
  4285. TBL_PC* sd;
  4286. sd = script_rid2sd(st);
  4287. if( sd == NULL )
  4288. return 0;
  4289. #ifdef SECURE_NPCTIMEOUT
  4290. sd->npc_idle_type = NPCT_MENU;
  4291. #endif
  4292. if( sd->state.menu_or_input == 0 )
  4293. {
  4294. struct StringBuf buf;
  4295. StringBuf_Init(&buf);
  4296. sd->npc_menu = 0;
  4297. for( i = 2; i <= script_lastdata(st); ++i )
  4298. {
  4299. text = script_getstr(st, i);
  4300. if( sd->npc_menu > 0 )
  4301. StringBuf_AppendStr(&buf, ":");
  4302. StringBuf_AppendStr(&buf, text);
  4303. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4304. }
  4305. st->state = RERUNLINE;
  4306. sd->state.menu_or_input = 1;
  4307. /**
  4308. * menus beyond this length crash the client (see bugreport:6402)
  4309. **/
  4310. if( StringBuf_Length(&buf) >= 2047 ) {
  4311. struct npc_data * nd = map_id2nd(st->oid);
  4312. char* menu;
  4313. CREATE(menu, char, 2048);
  4314. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4315. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4316. clif_scriptmenu(sd, st->oid, menu);
  4317. aFree(menu);
  4318. } else
  4319. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4320. StringBuf_Destroy(&buf);
  4321. if( sd->npc_menu >= 0xff )
  4322. {
  4323. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4324. script_reportsrc(st);
  4325. }
  4326. }
  4327. else if( sd->npc_menu == 0xff )
  4328. {// Cancel was pressed
  4329. sd->state.menu_or_input = 0;
  4330. pc_setreg(sd, add_str("@menu"), 0xff);
  4331. script_pushint(st, 0xff);
  4332. st->state = END;
  4333. }
  4334. else
  4335. {// return selected option
  4336. int menu = 0;
  4337. sd->state.menu_or_input = 0;
  4338. for( i = 2; i <= script_lastdata(st); ++i )
  4339. {
  4340. text = script_getstr(st, i);
  4341. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4342. if( sd->npc_menu <= 0 )
  4343. break;// entry found
  4344. }
  4345. pc_setreg(sd, add_str("@menu"), menu);
  4346. script_pushint(st, menu);
  4347. st->state = RUN;
  4348. }
  4349. return SCRIPT_CMD_SUCCESS;
  4350. }
  4351. /////////////////////////////////////////////////////////////////////
  4352. // ...
  4353. //
  4354. /// Jumps to the target script label.
  4355. ///
  4356. /// goto <label>;
  4357. BUILDIN_FUNC(goto)
  4358. {
  4359. if( !data_islabel(script_getdata(st,2)) )
  4360. {
  4361. ShowError("script:goto: not a label\n");
  4362. script_reportdata(script_getdata(st,2));
  4363. st->state = END;
  4364. return 1;
  4365. }
  4366. st->pos = script_getnum(st,2);
  4367. st->state = GOTO;
  4368. return SCRIPT_CMD_SUCCESS;
  4369. }
  4370. /*==========================================
  4371. * user-defined function call
  4372. *------------------------------------------*/
  4373. BUILDIN_FUNC(callfunc)
  4374. {
  4375. int i, j;
  4376. struct script_retinfo* ri;
  4377. struct script_code* scr;
  4378. const char* str = script_getstr(st,2);
  4379. DBMap **ref = NULL;
  4380. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4381. if( !scr )
  4382. {
  4383. ShowError("script:callfunc: function not found! [%s]\n", str);
  4384. st->state = END;
  4385. return 1;
  4386. }
  4387. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4388. {
  4389. struct script_data* data = push_copy(st->stack,i);
  4390. if( data_isreference(data) && !data->ref )
  4391. {
  4392. const char* name = reference_getname(data);
  4393. if( name[0] == '.' ) {
  4394. if(!ref){
  4395. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4396. ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars);
  4397. }
  4398. data->ref = ref;
  4399. }
  4400. }
  4401. }
  4402. CREATE(ri, struct script_retinfo, 1);
  4403. ri->script = st->script;// script code
  4404. ri->var_function = st->stack->var_function;// scope variables
  4405. ri->pos = st->pos;// script location
  4406. ri->nargs = j;// argument count
  4407. ri->defsp = st->stack->defsp;// default stack pointer
  4408. push_retinfo(st->stack, ri, ref);
  4409. st->pos = 0;
  4410. st->script = scr;
  4411. st->stack->defsp = st->stack->sp;
  4412. st->state = GOTO;
  4413. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4414. return SCRIPT_CMD_SUCCESS;
  4415. }
  4416. /*==========================================
  4417. * subroutine call
  4418. *------------------------------------------*/
  4419. BUILDIN_FUNC(callsub)
  4420. {
  4421. int i,j;
  4422. struct script_retinfo* ri;
  4423. int pos = script_getnum(st,2);
  4424. DBMap **ref = NULL;
  4425. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4426. {
  4427. ShowError("script:callsub: argument is not a label\n");
  4428. script_reportdata(script_getdata(st,2));
  4429. st->state = END;
  4430. return 1;
  4431. }
  4432. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4433. {
  4434. struct script_data* data = push_copy(st->stack,i);
  4435. if( data_isreference(data) && !data->ref )
  4436. {
  4437. const char* name = reference_getname(data);
  4438. if( name[0] == '.' && name[1] == '@' ) {
  4439. if ( !ref ) {
  4440. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4441. ref[0] = st->stack->var_function;
  4442. }
  4443. data->ref = ref;
  4444. }
  4445. }
  4446. }
  4447. CREATE(ri, struct script_retinfo, 1);
  4448. ri->script = st->script;// script code
  4449. ri->var_function = st->stack->var_function;// scope variables
  4450. ri->pos = st->pos;// script location
  4451. ri->nargs = j;// argument count
  4452. ri->defsp = st->stack->defsp;// default stack pointer
  4453. push_retinfo(st->stack, ri, ref);
  4454. st->pos = pos;
  4455. st->stack->defsp = st->stack->sp;
  4456. st->state = GOTO;
  4457. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4458. return SCRIPT_CMD_SUCCESS;
  4459. }
  4460. /// Retrieves an argument provided to callfunc/callsub.
  4461. /// If the argument doesn't exist
  4462. ///
  4463. /// getarg(<index>{,<default_value>}) -> <value>
  4464. BUILDIN_FUNC(getarg)
  4465. {
  4466. struct script_retinfo* ri;
  4467. int idx;
  4468. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4469. {
  4470. ShowError("script:getarg: no callfunc or callsub!\n");
  4471. st->state = END;
  4472. return 1;
  4473. }
  4474. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4475. idx = script_getnum(st,2);
  4476. if( idx >= 0 && idx < ri->nargs )
  4477. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4478. else if( script_hasdata(st,3) )
  4479. script_pushcopy(st, 3);
  4480. else
  4481. {
  4482. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4483. st->state = END;
  4484. return 1;
  4485. }
  4486. return SCRIPT_CMD_SUCCESS;
  4487. }
  4488. /// Returns from the current function, optionaly returning a value from the functions.
  4489. /// Don't use outside script functions.
  4490. ///
  4491. /// return;
  4492. /// return <value>;
  4493. BUILDIN_FUNC(return)
  4494. {
  4495. if( script_hasdata(st,2) )
  4496. {// return value
  4497. struct script_data* data;
  4498. script_pushcopy(st, 2);
  4499. data = script_getdatatop(st, -1);
  4500. if( data_isreference(data) )
  4501. {
  4502. const char* name = reference_getname(data);
  4503. if( name[0] == '.' && name[1] == '@' )
  4504. {// scope variable
  4505. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4506. get_val(st, data);// current scope, convert to value
  4507. }
  4508. else if( name[0] == '.' && !data->ref )
  4509. {// script variable, link to current script
  4510. data->ref = &st->script->script_vars;
  4511. }
  4512. }
  4513. }
  4514. else
  4515. {// no return value
  4516. script_pushnil(st);
  4517. }
  4518. st->state = RETFUNC;
  4519. return SCRIPT_CMD_SUCCESS;
  4520. }
  4521. /// Returns a random number from 0 to <range>-1.
  4522. /// Or returns a random number from <min> to <max>.
  4523. /// If <min> is greater than <max>, their numbers are switched.
  4524. /// rand(<range>) -> <int>
  4525. /// rand(<min>,<max>) -> <int>
  4526. BUILDIN_FUNC(rand)
  4527. {
  4528. int range;
  4529. int min;
  4530. int max;
  4531. if( script_hasdata(st,3) )
  4532. {// min,max
  4533. min = script_getnum(st,2);
  4534. max = script_getnum(st,3);
  4535. if( max < min )
  4536. swap(min, max);
  4537. range = max - min + 1;
  4538. }
  4539. else
  4540. {// range
  4541. min = 0;
  4542. range = script_getnum(st,2);
  4543. }
  4544. if( range <= 1 )
  4545. script_pushint(st, min);
  4546. else
  4547. script_pushint(st, rnd()%range + min);
  4548. return SCRIPT_CMD_SUCCESS;
  4549. }
  4550. /*==========================================
  4551. * Warp sd to str,x,y or Random or SavePoint/Save
  4552. *------------------------------------------*/
  4553. BUILDIN_FUNC(warp)
  4554. {
  4555. int ret;
  4556. int x,y;
  4557. const char* str;
  4558. TBL_PC* sd;
  4559. sd = script_rid2sd(st);
  4560. if( sd == NULL )
  4561. return 0;
  4562. str = script_getstr(st,2);
  4563. x = script_getnum(st,3);
  4564. y = script_getnum(st,4);
  4565. if(strcmp(str,"Random")==0)
  4566. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4567. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4568. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4569. else
  4570. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4571. if( ret ) {
  4572. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4573. return 1;
  4574. }
  4575. return SCRIPT_CMD_SUCCESS;
  4576. }
  4577. /*==========================================
  4578. * Warp a specified area
  4579. *------------------------------------------*/
  4580. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4581. {
  4582. int x2,y2,x3,y3;
  4583. unsigned int index;
  4584. index = va_arg(ap,unsigned int);
  4585. x2 = va_arg(ap,int);
  4586. y2 = va_arg(ap,int);
  4587. x3 = va_arg(ap,int);
  4588. y3 = va_arg(ap,int);
  4589. if(index == 0)
  4590. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4591. else if(x3 && y3) {
  4592. int max, tx, ty, j = 0;
  4593. // choose a suitable max number of attempts
  4594. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4595. max = 1000;
  4596. // find a suitable map cell
  4597. do {
  4598. tx = rnd()%(x3-x2+1)+x2;
  4599. ty = rnd()%(y3-y2+1)+y2;
  4600. j++;
  4601. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4602. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4603. }
  4604. else
  4605. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4606. return 0;
  4607. }
  4608. BUILDIN_FUNC(areawarp)
  4609. {
  4610. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4611. unsigned int index;
  4612. const char *str;
  4613. const char *mapname;
  4614. mapname = script_getstr(st,2);
  4615. x0 = script_getnum(st,3);
  4616. y0 = script_getnum(st,4);
  4617. x1 = script_getnum(st,5);
  4618. y1 = script_getnum(st,6);
  4619. str = script_getstr(st,7);
  4620. x2 = script_getnum(st,8);
  4621. y2 = script_getnum(st,9);
  4622. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4623. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4624. x3 = 0;
  4625. y3 = 0;
  4626. } else if( x3 && y3 ) {
  4627. // normalize x3/y3 coordinates
  4628. if( x3 < x2 ) swap(x3,x2);
  4629. if( y3 < y2 ) swap(y3,y2);
  4630. }
  4631. }
  4632. if( (m = map_mapname2mapid(mapname)) < 0 )
  4633. return 0;
  4634. if( strcmp(str,"Random") == 0 )
  4635. index = 0;
  4636. else if( !(index=mapindex_name2id(str)) )
  4637. return 0;
  4638. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4639. return SCRIPT_CMD_SUCCESS;
  4640. }
  4641. /*==========================================
  4642. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4643. *------------------------------------------*/
  4644. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4645. {
  4646. int hp, sp;
  4647. hp = va_arg(ap, int);
  4648. sp = va_arg(ap, int);
  4649. pc_percentheal((TBL_PC *)bl,hp,sp);
  4650. return 0;
  4651. }
  4652. BUILDIN_FUNC(areapercentheal)
  4653. {
  4654. int hp,sp,m;
  4655. const char *mapname;
  4656. int x0,y0,x1,y1;
  4657. mapname=script_getstr(st,2);
  4658. x0=script_getnum(st,3);
  4659. y0=script_getnum(st,4);
  4660. x1=script_getnum(st,5);
  4661. y1=script_getnum(st,6);
  4662. hp=script_getnum(st,7);
  4663. sp=script_getnum(st,8);
  4664. if( (m=map_mapname2mapid(mapname))< 0)
  4665. return 0;
  4666. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4667. return SCRIPT_CMD_SUCCESS;
  4668. }
  4669. /*==========================================
  4670. * warpchar [LuzZza]
  4671. * Useful for warp one player from
  4672. * another player npc-session.
  4673. * Using: warpchar "mapname",x,y,Char_ID;
  4674. *------------------------------------------*/
  4675. BUILDIN_FUNC(warpchar)
  4676. {
  4677. int x,y,a;
  4678. const char *str;
  4679. TBL_PC *sd;
  4680. str=script_getstr(st,2);
  4681. x=script_getnum(st,3);
  4682. y=script_getnum(st,4);
  4683. a=script_getnum(st,5);
  4684. sd = map_charid2sd(a);
  4685. if( sd == NULL )
  4686. return 0;
  4687. if(strcmp(str, "Random") == 0)
  4688. pc_randomwarp(sd, CLR_TELEPORT);
  4689. else
  4690. if(strcmp(str, "SavePoint") == 0)
  4691. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4692. else
  4693. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4694. return SCRIPT_CMD_SUCCESS;
  4695. }
  4696. /*==========================================
  4697. * Warpparty - [Fredzilla] [Paradox924X]
  4698. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4699. * If 'from_mapname' is specified, only the party members on that map will be warped
  4700. *------------------------------------------*/
  4701. BUILDIN_FUNC(warpparty)
  4702. {
  4703. TBL_PC *sd = NULL;
  4704. TBL_PC *pl_sd;
  4705. struct party_data* p;
  4706. int type;
  4707. int mapindex;
  4708. int i;
  4709. const char* str = script_getstr(st,2);
  4710. int x = script_getnum(st,3);
  4711. int y = script_getnum(st,4);
  4712. int p_id = script_getnum(st,5);
  4713. const char* str2 = NULL;
  4714. if ( script_hasdata(st,6) )
  4715. str2 = script_getstr(st,6);
  4716. p = party_search(p_id);
  4717. if(!p)
  4718. return 0;
  4719. type = ( strcmp(str,"Random")==0 ) ? 0
  4720. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4721. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4722. : ( strcmp(str,"Leader")==0 ) ? 3
  4723. : 4;
  4724. switch (type)
  4725. {
  4726. case 3:
  4727. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4728. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4729. return 0;
  4730. pl_sd = p->data[i].sd;
  4731. mapindex = pl_sd->mapindex;
  4732. x = pl_sd->bl.x;
  4733. y = pl_sd->bl.y;
  4734. break;
  4735. case 4:
  4736. mapindex = mapindex_name2id(str);
  4737. break;
  4738. case 2:
  4739. //"SavePoint" uses save point of the currently attached player
  4740. if (( sd = script_rid2sd(st) ) == NULL )
  4741. return 0;
  4742. default:
  4743. mapindex = 0;
  4744. break;
  4745. }
  4746. for (i = 0; i < MAX_PARTY; i++)
  4747. {
  4748. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4749. continue;
  4750. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4751. continue;
  4752. if( pc_isdead(pl_sd) )
  4753. continue;
  4754. switch( type )
  4755. {
  4756. case 0: // Random
  4757. if(!map[pl_sd->bl.m].flag.nowarp)
  4758. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4759. break;
  4760. case 1: // SavePointAll
  4761. if(!map[pl_sd->bl.m].flag.noreturn)
  4762. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4763. break;
  4764. case 2: // SavePoint
  4765. if(!map[pl_sd->bl.m].flag.noreturn)
  4766. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4767. break;
  4768. case 3: // Leader
  4769. case 4: // m,x,y
  4770. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4771. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4772. break;
  4773. }
  4774. }
  4775. return SCRIPT_CMD_SUCCESS;
  4776. }
  4777. /*==========================================
  4778. * Warpguild - [Fredzilla]
  4779. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4780. *------------------------------------------*/
  4781. BUILDIN_FUNC(warpguild)
  4782. {
  4783. TBL_PC *sd = NULL;
  4784. TBL_PC *pl_sd;
  4785. struct guild* g;
  4786. struct s_mapiterator* iter;
  4787. int type;
  4788. const char* str = script_getstr(st,2);
  4789. int x = script_getnum(st,3);
  4790. int y = script_getnum(st,4);
  4791. int gid = script_getnum(st,5);
  4792. g = guild_search(gid);
  4793. if( g == NULL )
  4794. return 0;
  4795. type = ( strcmp(str,"Random")==0 ) ? 0
  4796. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4797. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4798. : 3;
  4799. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4800. {// "SavePoint" uses save point of the currently attached player
  4801. return 0;
  4802. }
  4803. iter = mapit_getallusers();
  4804. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4805. {
  4806. if( pl_sd->status.guild_id != gid )
  4807. continue;
  4808. switch( type )
  4809. {
  4810. case 0: // Random
  4811. if(!map[pl_sd->bl.m].flag.nowarp)
  4812. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4813. break;
  4814. case 1: // SavePointAll
  4815. if(!map[pl_sd->bl.m].flag.noreturn)
  4816. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4817. break;
  4818. case 2: // SavePoint
  4819. if(!map[pl_sd->bl.m].flag.noreturn)
  4820. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4821. break;
  4822. case 3: // m,x,y
  4823. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4824. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4825. break;
  4826. }
  4827. }
  4828. mapit_free(iter);
  4829. return SCRIPT_CMD_SUCCESS;
  4830. }
  4831. /*==========================================
  4832. * Force Heal a player (hp and sp)
  4833. *------------------------------------------*/
  4834. BUILDIN_FUNC(heal)
  4835. {
  4836. TBL_PC *sd;
  4837. int hp,sp;
  4838. sd = script_rid2sd(st);
  4839. if (!sd) return 0;
  4840. hp=script_getnum(st,2);
  4841. sp=script_getnum(st,3);
  4842. status_heal(&sd->bl, hp, sp, 1);
  4843. return SCRIPT_CMD_SUCCESS;
  4844. }
  4845. /*==========================================
  4846. * Heal a player by item (get vit bonus etc)
  4847. *------------------------------------------*/
  4848. BUILDIN_FUNC(itemheal)
  4849. {
  4850. TBL_PC *sd;
  4851. int hp,sp;
  4852. hp=script_getnum(st,2);
  4853. sp=script_getnum(st,3);
  4854. if(potion_flag==1) {
  4855. potion_hp = hp;
  4856. potion_sp = sp;
  4857. return 0;
  4858. }
  4859. sd = script_rid2sd(st);
  4860. if (!sd) return 0;
  4861. pc_itemheal(sd,sd->itemid,hp,sp);
  4862. return SCRIPT_CMD_SUCCESS;
  4863. }
  4864. /*==========================================
  4865. *
  4866. *------------------------------------------*/
  4867. BUILDIN_FUNC(percentheal)
  4868. {
  4869. int hp,sp;
  4870. TBL_PC* sd;
  4871. hp=script_getnum(st,2);
  4872. sp=script_getnum(st,3);
  4873. if(potion_flag==1) {
  4874. potion_per_hp = hp;
  4875. potion_per_sp = sp;
  4876. return 0;
  4877. }
  4878. sd = script_rid2sd(st);
  4879. if( sd == NULL )
  4880. return 0;
  4881. #ifdef RENEWAL
  4882. if( sd->sc.data[SC_EXTREMITYFIST2] )
  4883. sp = 0;
  4884. #endif
  4885. pc_percentheal(sd,hp,sp);
  4886. return SCRIPT_CMD_SUCCESS;
  4887. }
  4888. /*==========================================
  4889. *
  4890. *------------------------------------------*/
  4891. BUILDIN_FUNC(jobchange)
  4892. {
  4893. int job, upper=-1;
  4894. job=script_getnum(st,2);
  4895. if( script_hasdata(st,3) )
  4896. upper=script_getnum(st,3);
  4897. if (pcdb_checkid(job))
  4898. {
  4899. TBL_PC* sd;
  4900. sd = script_rid2sd(st);
  4901. if( sd == NULL )
  4902. return 0;
  4903. pc_jobchange(sd, job, upper);
  4904. }
  4905. return SCRIPT_CMD_SUCCESS;
  4906. }
  4907. /*==========================================
  4908. *
  4909. *------------------------------------------*/
  4910. BUILDIN_FUNC(jobname)
  4911. {
  4912. int class_=script_getnum(st,2);
  4913. script_pushconststr(st, (char*)job_name(class_));
  4914. return SCRIPT_CMD_SUCCESS;
  4915. }
  4916. /// Get input from the player.
  4917. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4918. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4919. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4920. /// shorter than 'min' and 0 otherwise.
  4921. ///
  4922. /// input(<var>{,<min>{,<max>}}) -> <int>
  4923. BUILDIN_FUNC(input)
  4924. {
  4925. TBL_PC* sd;
  4926. struct script_data* data;
  4927. int uid;
  4928. const char* name;
  4929. int min;
  4930. int max;
  4931. sd = script_rid2sd(st);
  4932. if( sd == NULL )
  4933. return 0;
  4934. data = script_getdata(st,2);
  4935. if( !data_isreference(data) ){
  4936. ShowError("script:input: not a variable\n");
  4937. script_reportdata(data);
  4938. st->state = END;
  4939. return 1;
  4940. }
  4941. uid = reference_getuid(data);
  4942. name = reference_getname(data);
  4943. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4944. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4945. #ifdef SECURE_NPCTIMEOUT
  4946. sd->npc_idle_type = NPCT_WAIT;
  4947. #endif
  4948. if( !sd->state.menu_or_input )
  4949. { // first invocation, display npc input box
  4950. sd->state.menu_or_input = 1;
  4951. st->state = RERUNLINE;
  4952. if( is_string_variable(name) )
  4953. clif_scriptinputstr(sd,st->oid);
  4954. else
  4955. clif_scriptinput(sd,st->oid);
  4956. }
  4957. else
  4958. { // take received text/value and store it in the designated variable
  4959. sd->state.menu_or_input = 0;
  4960. if( is_string_variable(name) )
  4961. {
  4962. int len = (int)strlen(sd->npc_str);
  4963. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4964. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4965. }
  4966. else
  4967. {
  4968. int amount = sd->npc_amount;
  4969. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  4970. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4971. }
  4972. st->state = RUN;
  4973. }
  4974. return SCRIPT_CMD_SUCCESS;
  4975. }
  4976. // declare the copyarray method here for future reference
  4977. BUILDIN_FUNC(copyarray);
  4978. /// Sets the value of a variable.
  4979. /// The value is converted to the type of the variable.
  4980. ///
  4981. /// set(<variable>,<value>) -> <variable>
  4982. BUILDIN_FUNC(set)
  4983. {
  4984. TBL_PC* sd = NULL;
  4985. struct script_data* data;
  4986. //struct script_data* datavalue;
  4987. int num;
  4988. const char* name;
  4989. char prefix;
  4990. data = script_getdata(st,2);
  4991. //datavalue = script_getdata(st,3);
  4992. if( !data_isreference(data) )
  4993. {
  4994. ShowError("script:set: not a variable\n");
  4995. script_reportdata(script_getdata(st,2));
  4996. st->state = END;
  4997. return 1;
  4998. }
  4999. num = reference_getuid(data);
  5000. name = reference_getname(data);
  5001. prefix = *name;
  5002. if( not_server_variable(prefix) )
  5003. {
  5004. sd = script_rid2sd(st);
  5005. if( sd == NULL )
  5006. {
  5007. ShowError("script:set: no player attached for player variable '%s'\n", name);
  5008. return 0;
  5009. }
  5010. }
  5011. #if 0
  5012. if( data_isreference(datavalue) )
  5013. {// the value being referenced is a variable
  5014. const char* namevalue = reference_getname(datavalue);
  5015. if( !not_array_variable(*namevalue) )
  5016. {// array variable being copied into another array variable
  5017. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5018. {// player must be attached in order to copy a player variable
  5019. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5020. return 0;
  5021. }
  5022. if( is_string_variable(namevalue) != is_string_variable(name) )
  5023. {// non-matching array value types
  5024. ShowWarning("script:set: two array variables do not match in type.\n");
  5025. return 0;
  5026. }
  5027. // push the maximum number of array values to the stack
  5028. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5029. // call the copy array method directly
  5030. return buildin_copyarray(st);
  5031. }
  5032. }
  5033. #endif
  5034. if( is_string_variable(name) )
  5035. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5036. else
  5037. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5038. // return a copy of the variable reference
  5039. script_pushcopy(st,2);
  5040. return SCRIPT_CMD_SUCCESS;
  5041. }
  5042. /////////////////////////////////////////////////////////////////////
  5043. /// Array variables
  5044. ///
  5045. /// Returns the size of the specified array
  5046. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  5047. {
  5048. int32 ret = idx;
  5049. if( isstring )
  5050. {
  5051. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5052. {
  5053. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  5054. if( str && *str )
  5055. ret = idx + 1;
  5056. script_removetop(st, -1, 0);
  5057. }
  5058. }
  5059. else
  5060. {
  5061. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5062. {
  5063. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  5064. if( num )
  5065. ret = idx + 1;
  5066. script_removetop(st, -1, 0);
  5067. }
  5068. }
  5069. return ret;
  5070. }
  5071. /// Sets values of an array, from the starting index.
  5072. /// ex: setarray arr[1],1,2,3;
  5073. ///
  5074. /// setarray <array variable>,<value1>{,<value2>...};
  5075. BUILDIN_FUNC(setarray)
  5076. {
  5077. struct script_data* data;
  5078. const char* name;
  5079. int32 start;
  5080. int32 end;
  5081. int32 id;
  5082. int32 i;
  5083. TBL_PC* sd = NULL;
  5084. data = script_getdata(st, 2);
  5085. if( !data_isreference(data) )
  5086. {
  5087. ShowError("script:setarray: not a variable\n");
  5088. script_reportdata(data);
  5089. st->state = END;
  5090. return 1;// not a variable
  5091. }
  5092. id = reference_getid(data);
  5093. start = reference_getindex(data);
  5094. name = reference_getname(data);
  5095. if( not_array_variable(*name) )
  5096. {
  5097. ShowError("script:setarray: illegal scope\n");
  5098. script_reportdata(data);
  5099. st->state = END;
  5100. return 1;// not supported
  5101. }
  5102. if( not_server_variable(*name) )
  5103. {
  5104. sd = script_rid2sd(st);
  5105. if( sd == NULL )
  5106. return 0;// no player attached
  5107. }
  5108. end = start + script_lastdata(st) - 2;
  5109. if( end > SCRIPT_MAX_ARRAYSIZE )
  5110. end = SCRIPT_MAX_ARRAYSIZE;
  5111. if( is_string_variable(name) )
  5112. {// string array
  5113. for( i = 3; start < end; ++start, ++i )
  5114. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5115. }
  5116. else
  5117. {// int array
  5118. for( i = 3; start < end; ++start, ++i )
  5119. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5120. }
  5121. return SCRIPT_CMD_SUCCESS;
  5122. }
  5123. /// Sets count values of an array, from the starting index.
  5124. /// ex: cleararray arr[0],0,1;
  5125. ///
  5126. /// cleararray <array variable>,<value>,<count>;
  5127. BUILDIN_FUNC(cleararray)
  5128. {
  5129. struct script_data* data;
  5130. const char* name;
  5131. int32 start;
  5132. int32 end;
  5133. int32 id;
  5134. void* v;
  5135. TBL_PC* sd = NULL;
  5136. data = script_getdata(st, 2);
  5137. if( !data_isreference(data) )
  5138. {
  5139. ShowError("script:cleararray: not a variable\n");
  5140. script_reportdata(data);
  5141. st->state = END;
  5142. return 1;// not a variable
  5143. }
  5144. id = reference_getid(data);
  5145. start = reference_getindex(data);
  5146. name = reference_getname(data);
  5147. if( not_array_variable(*name) )
  5148. {
  5149. ShowError("script:cleararray: illegal scope\n");
  5150. script_reportdata(data);
  5151. st->state = END;
  5152. return 1;// not supported
  5153. }
  5154. if( not_server_variable(*name) )
  5155. {
  5156. sd = script_rid2sd(st);
  5157. if( sd == NULL )
  5158. return 0;// no player attached
  5159. }
  5160. if( is_string_variable(name) )
  5161. v = (void*)script_getstr(st, 3);
  5162. else
  5163. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5164. end = start + script_getnum(st, 4);
  5165. if( end > SCRIPT_MAX_ARRAYSIZE )
  5166. end = SCRIPT_MAX_ARRAYSIZE;
  5167. for( ; start < end; ++start )
  5168. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5169. return SCRIPT_CMD_SUCCESS;
  5170. }
  5171. /// Copies data from one array to another.
  5172. /// ex: copyarray arr[0],arr[2],2;
  5173. ///
  5174. /// copyarray <destination array variable>,<source array variable>,<count>;
  5175. BUILDIN_FUNC(copyarray)
  5176. {
  5177. struct script_data* data1;
  5178. struct script_data* data2;
  5179. const char* name1;
  5180. const char* name2;
  5181. int32 idx1;
  5182. int32 idx2;
  5183. int32 id1;
  5184. int32 id2;
  5185. void* v;
  5186. int32 i;
  5187. int32 count;
  5188. TBL_PC* sd = NULL;
  5189. data1 = script_getdata(st, 2);
  5190. data2 = script_getdata(st, 3);
  5191. if( !data_isreference(data1) || !data_isreference(data2) )
  5192. {
  5193. ShowError("script:copyarray: not a variable\n");
  5194. script_reportdata(data1);
  5195. script_reportdata(data2);
  5196. st->state = END;
  5197. return 1;// not a variable
  5198. }
  5199. id1 = reference_getid(data1);
  5200. id2 = reference_getid(data2);
  5201. idx1 = reference_getindex(data1);
  5202. idx2 = reference_getindex(data2);
  5203. name1 = reference_getname(data1);
  5204. name2 = reference_getname(data2);
  5205. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5206. {
  5207. ShowError("script:copyarray: illegal scope\n");
  5208. script_reportdata(data1);
  5209. script_reportdata(data2);
  5210. st->state = END;
  5211. return 1;// not supported
  5212. }
  5213. if( is_string_variable(name1) != is_string_variable(name2) )
  5214. {
  5215. ShowError("script:copyarray: type mismatch\n");
  5216. script_reportdata(data1);
  5217. script_reportdata(data2);
  5218. st->state = END;
  5219. return 1;// data type mismatch
  5220. }
  5221. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5222. {
  5223. sd = script_rid2sd(st);
  5224. if( sd == NULL )
  5225. return 0;// no player attached
  5226. }
  5227. count = script_getnum(st, 4);
  5228. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5229. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5230. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5231. return 0;// nothing to copy
  5232. if( id1 == id2 && idx1 > idx2 )
  5233. {// destination might be overlapping the source - copy in reverse order
  5234. for( i = count - 1; i >= 0; --i )
  5235. {
  5236. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5237. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5238. script_removetop(st, -1, 0);
  5239. }
  5240. }
  5241. else
  5242. {// normal copy
  5243. for( i = 0; i < count; ++i )
  5244. {
  5245. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5246. {
  5247. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5248. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5249. script_removetop(st, -1, 0);
  5250. }
  5251. else// out of range - assume ""/0
  5252. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5253. }
  5254. }
  5255. return SCRIPT_CMD_SUCCESS;
  5256. }
  5257. /// Returns the size of the array.
  5258. /// Assumes that everything before the starting index exists.
  5259. /// ex: getarraysize(arr[3])
  5260. ///
  5261. /// getarraysize(<array variable>) -> <int>
  5262. BUILDIN_FUNC(getarraysize)
  5263. {
  5264. struct script_data* data;
  5265. const char* name;
  5266. data = script_getdata(st, 2);
  5267. if( !data_isreference(data) )
  5268. {
  5269. ShowError("script:getarraysize: not a variable\n");
  5270. script_reportdata(data);
  5271. script_pushnil(st);
  5272. st->state = END;
  5273. return 1;// not a variable
  5274. }
  5275. name = reference_getname(data);
  5276. if( not_array_variable(*name) )
  5277. {
  5278. ShowError("script:getarraysize: illegal scope\n");
  5279. script_reportdata(data);
  5280. script_pushnil(st);
  5281. st->state = END;
  5282. return 1;// not supported
  5283. }
  5284. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5285. return SCRIPT_CMD_SUCCESS;
  5286. }
  5287. /// Deletes count or all the elements in an array, from the starting index.
  5288. /// ex: deletearray arr[4],2;
  5289. ///
  5290. /// deletearray <array variable>;
  5291. /// deletearray <array variable>,<count>;
  5292. BUILDIN_FUNC(deletearray)
  5293. {
  5294. struct script_data* data;
  5295. const char* name;
  5296. int start;
  5297. int end;
  5298. int id;
  5299. TBL_PC *sd = NULL;
  5300. data = script_getdata(st, 2);
  5301. if( !data_isreference(data) )
  5302. {
  5303. ShowError("script:deletearray: not a variable\n");
  5304. script_reportdata(data);
  5305. st->state = END;
  5306. return 1;// not a variable
  5307. }
  5308. id = reference_getid(data);
  5309. start = reference_getindex(data);
  5310. name = reference_getname(data);
  5311. if( not_array_variable(*name) )
  5312. {
  5313. ShowError("script:deletearray: illegal scope\n");
  5314. script_reportdata(data);
  5315. st->state = END;
  5316. return 1;// not supported
  5317. }
  5318. if( not_server_variable(*name) )
  5319. {
  5320. sd = script_rid2sd(st);
  5321. if( sd == NULL )
  5322. return 0;// no player attached
  5323. }
  5324. end = SCRIPT_MAX_ARRAYSIZE;
  5325. if( start >= end )
  5326. return 0;// nothing to free
  5327. if( script_hasdata(st,3) )
  5328. {
  5329. int count = script_getnum(st, 3);
  5330. if( count > end - start )
  5331. count = end - start;
  5332. if( count <= 0 )
  5333. return 0;// nothing to free
  5334. // move rest of the elements backward
  5335. for( ; start + count < end; ++start )
  5336. {
  5337. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5338. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5339. script_removetop(st, -1, 0);
  5340. }
  5341. }
  5342. // clear the rest of the array
  5343. if( is_string_variable(name) )
  5344. {
  5345. for( ; start < end; ++start )
  5346. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5347. }
  5348. else
  5349. {
  5350. for( ; start < end; ++start )
  5351. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5352. }
  5353. return SCRIPT_CMD_SUCCESS;
  5354. }
  5355. /// Returns a reference to the target index of the array variable.
  5356. /// Equivalent to var[index].
  5357. ///
  5358. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5359. BUILDIN_FUNC(getelementofarray)
  5360. {
  5361. struct script_data* data;
  5362. const char* name;
  5363. int32 id;
  5364. int i;
  5365. data = script_getdata(st, 2);
  5366. if( !data_isreference(data) )
  5367. {
  5368. ShowError("script:getelementofarray: not a variable\n");
  5369. script_reportdata(data);
  5370. script_pushnil(st);
  5371. st->state = END;
  5372. return 1;// not a variable
  5373. }
  5374. id = reference_getid(data);
  5375. name = reference_getname(data);
  5376. if( not_array_variable(*name) )
  5377. {
  5378. ShowError("script:getelementofarray: illegal scope\n");
  5379. script_reportdata(data);
  5380. script_pushnil(st);
  5381. st->state = END;
  5382. return 1;// not supported
  5383. }
  5384. i = script_getnum(st, 3);
  5385. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  5386. {
  5387. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5388. script_reportdata(data);
  5389. script_pushnil(st);
  5390. st->state = END;
  5391. return 1;// out of range
  5392. }
  5393. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5394. return SCRIPT_CMD_SUCCESS;
  5395. }
  5396. /////////////////////////////////////////////////////////////////////
  5397. /// ...
  5398. ///
  5399. /*==========================================
  5400. *
  5401. *------------------------------------------*/
  5402. BUILDIN_FUNC(setlook)
  5403. {
  5404. int type,val;
  5405. TBL_PC* sd;
  5406. type=script_getnum(st,2);
  5407. val=script_getnum(st,3);
  5408. sd = script_rid2sd(st);
  5409. if( sd == NULL )
  5410. return 0;
  5411. pc_changelook(sd,type,val);
  5412. return SCRIPT_CMD_SUCCESS;
  5413. }
  5414. BUILDIN_FUNC(changelook)
  5415. { // As setlook but only client side
  5416. int type,val;
  5417. TBL_PC* sd;
  5418. type=script_getnum(st,2);
  5419. val=script_getnum(st,3);
  5420. sd = script_rid2sd(st);
  5421. if( sd == NULL )
  5422. return 0;
  5423. clif_changelook(&sd->bl,type,val);
  5424. return SCRIPT_CMD_SUCCESS;
  5425. }
  5426. /*==========================================
  5427. *
  5428. *------------------------------------------*/
  5429. BUILDIN_FUNC(cutin)
  5430. {
  5431. TBL_PC* sd;
  5432. sd = script_rid2sd(st);
  5433. if( sd == NULL )
  5434. return 0;
  5435. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5436. return SCRIPT_CMD_SUCCESS;
  5437. }
  5438. /*==========================================
  5439. *
  5440. *------------------------------------------*/
  5441. BUILDIN_FUNC(viewpoint)
  5442. {
  5443. int type,x,y,id,color;
  5444. TBL_PC* sd;
  5445. type=script_getnum(st,2);
  5446. x=script_getnum(st,3);
  5447. y=script_getnum(st,4);
  5448. id=script_getnum(st,5);
  5449. color=script_getnum(st,6);
  5450. sd = script_rid2sd(st);
  5451. if( sd == NULL )
  5452. return 0;
  5453. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5454. return SCRIPT_CMD_SUCCESS;
  5455. }
  5456. /// Returns number of items in inventory/cart/storage
  5457. /// countitem <nameID>{,<accountID>});
  5458. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5459. /// cartcountitem <nameID>{,<accountID>});
  5460. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5461. /// storagecountitem <nameID>{,<accountID>});
  5462. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5463. BUILDIN_FUNC(countitem)
  5464. {
  5465. int i = 0, count = 0, aid = 3;
  5466. struct item_data* id = NULL;
  5467. struct script_data* data;
  5468. char *command = (char *)script_getfuncname(st);
  5469. uint8 loc = 0;
  5470. uint16 size;
  5471. struct item *items;
  5472. TBL_PC *sd = NULL;
  5473. if( command[strlen(command)-1] == '2' ) {
  5474. i = 1;
  5475. aid = 10;
  5476. }
  5477. if( script_hasdata(st,aid) ) {
  5478. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5479. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5480. st->state = END;
  5481. return SCRIPT_CMD_FAILURE;
  5482. }
  5483. }
  5484. else {
  5485. if( !(sd = script_rid2sd(st)) )
  5486. return SCRIPT_CMD_SUCCESS;
  5487. }
  5488. if( !strncmp(command, "cart", 4) ) {
  5489. loc = 1;
  5490. size = MAX_CART;
  5491. items = sd->status.cart;
  5492. }
  5493. else if( !strncmp(command, "storage", 7) ) {
  5494. loc = 2;
  5495. size = MAX_STORAGE;
  5496. items = sd->status.storage.items;
  5497. }
  5498. //TODO: 3 - Guild Storage
  5499. else {
  5500. size = MAX_INVENTORY;
  5501. items = sd->status.inventory;
  5502. }
  5503. if( loc == 1 && !pc_iscarton(sd) ) {
  5504. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5505. script_pushint(st,-1);
  5506. return SCRIPT_CMD_SUCCESS;
  5507. }
  5508. data = script_getdata(st, 2);
  5509. get_val(st, data); // Convert into value in case of a variable
  5510. if( data_isstring(data) ) // item name
  5511. id = itemdb_searchname(conv_str(st, data));
  5512. else // item id
  5513. id = itemdb_exists(conv_num(st, data));
  5514. if( id == NULL ) {
  5515. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5516. script_pushint(st,0);
  5517. return 1;
  5518. }
  5519. if( !i ) { // For count/cart/storagecountitem function
  5520. unsigned short nameid = id->nameid;
  5521. for( i = 0; i < size; i++ )
  5522. if( &items[i] && items[i].nameid == nameid )
  5523. count += items[i].amount;
  5524. }
  5525. else { // For count/cart/storagecountitem2 function
  5526. unsigned short nameid;
  5527. int iden, ref, attr, c1, c2, c3, c4;
  5528. nameid = id->nameid;
  5529. iden = script_getnum(st,3);
  5530. ref = script_getnum(st,4);
  5531. attr = script_getnum(st,5);
  5532. c1 = script_getnum(st,6);
  5533. c2 = script_getnum(st,7);
  5534. c3 = script_getnum(st,8);
  5535. c4 = script_getnum(st,9);
  5536. for( i = 0; i < size; i++ )
  5537. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5538. items[i].amount > 0 && items[i].identify == iden &&
  5539. items[i].refine == ref && items[i].attribute == attr &&
  5540. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5541. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5542. {
  5543. count += items[i].amount;
  5544. }
  5545. }
  5546. script_pushint(st, count);
  5547. return SCRIPT_CMD_SUCCESS;
  5548. }
  5549. int checkweight_sub(TBL_PC *sd, int nbargs, unsigned short *eitemid, int32 *eamount)
  5550. {
  5551. struct item_data* id = NULL;
  5552. unsigned short nameid;
  5553. int amount;
  5554. uint32 weight=0;
  5555. uint16 amount2=0,slots,i;
  5556. slots = pc_inventoryblank(sd); //nb of empty slot
  5557. for(i=0; i<nbargs; i++) {
  5558. if(!eitemid[i])
  5559. continue;
  5560. id = itemdb_exists(eitemid[i]);
  5561. if( id == NULL ) {
  5562. ShowError("checkweight_sub: Invalid item '%hu'.\n", eitemid[i]);
  5563. return 0;
  5564. }
  5565. nameid = id->nameid;
  5566. amount = eamount[i];
  5567. if( amount < 1 ) {
  5568. ShowError("checkweight_sub: Invalid amount '%d'.\n", eamount[i]);
  5569. return 0;
  5570. }
  5571. weight += (id->weight)*amount; //total weight for all chk
  5572. if( weight + sd->weight > sd->max_weight ) // too heavy
  5573. return 0;
  5574. switch( pc_checkadditem(sd, nameid, amount) ) {
  5575. case CHKADDITEM_EXIST: // item is already in inventory, but there is still space for the requested amount
  5576. break;
  5577. case CHKADDITEM_NEW:
  5578. if( itemdb_isstackable(nameid) )
  5579. amount2++; // stackable
  5580. else
  5581. amount2 += amount; // non-stackable
  5582. if( slots < amount2)
  5583. return 0;
  5584. break;
  5585. case CHKADDITEM_OVERAMOUNT:
  5586. return 0;
  5587. }
  5588. }
  5589. return 1;
  5590. }
  5591. /*==========================================
  5592. * Check if item with this amount can fit in inventory
  5593. * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY)
  5594. * Return
  5595. * 0 : fail
  5596. * 1 : success (npc side only)
  5597. *------------------------------------------*/
  5598. BUILDIN_FUNC(checkweight)
  5599. {
  5600. struct map_session_data* sd;
  5601. struct script_data* data;
  5602. struct item_data* id = NULL;
  5603. unsigned short nameid[SCRIPT_MAX_ARRAYSIZE];
  5604. int32 amount[SCRIPT_MAX_ARRAYSIZE];
  5605. uint16 nbargs,i,j=0;
  5606. if( ( sd = script_rid2sd(st) ) == NULL )
  5607. return 0;
  5608. nbargs = script_lastdata(st)+1;
  5609. if(nbargs%2) {
  5610. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5611. script_pushint(st,0);
  5612. return 1;
  5613. }
  5614. for(i=2; i<nbargs; i=i+2) {
  5615. data = script_getdata(st,i);
  5616. get_val(st, data); // Convert into value in case of a variable
  5617. if( data_isstring(data) ) // item name
  5618. id = itemdb_searchname(conv_str(st, data));
  5619. else // item id
  5620. id = itemdb_exists(conv_num(st, data));
  5621. if( id == NULL ) {
  5622. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5623. script_pushint(st,0);
  5624. return 1;
  5625. }
  5626. nameid[j] = id->nameid;
  5627. amount[j] = script_getnum(st,i+1);
  5628. j++;
  5629. }
  5630. script_pushint(st,checkweight_sub(sd,(nbargs-2)/2,nameid,amount));
  5631. return SCRIPT_CMD_SUCCESS;
  5632. }
  5633. BUILDIN_FUNC(checkweight2){
  5634. //variable sub checkweight
  5635. unsigned short nameid[SCRIPT_MAX_ARRAYSIZE];
  5636. int32 amount[SCRIPT_MAX_ARRAYSIZE], i;
  5637. //variable for array parsing
  5638. struct script_data* data_it;
  5639. struct script_data* data_nb;
  5640. const char* name_it;
  5641. const char* name_nb;
  5642. int32 id_it, id_nb;
  5643. int32 idx_it, idx_nb;
  5644. int nb_it, nb_nb; //array size
  5645. TBL_PC *sd = script_rid2sd(st);
  5646. nullpo_retr(1,sd);
  5647. data_it = script_getdata(st, 2);
  5648. data_nb = script_getdata(st, 3);
  5649. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5650. ShowError("buildin_checkweight2: parameter not a variable\n");
  5651. script_pushint(st,0);
  5652. return 1;// not a variable
  5653. }
  5654. id_it = reference_getid(data_it);
  5655. id_nb = reference_getid(data_nb);
  5656. idx_it = reference_getindex(data_it);
  5657. idx_nb = reference_getindex(data_nb);
  5658. name_it = reference_getname(data_it);
  5659. name_nb = reference_getname(data_nb);
  5660. if( not_array_variable(*name_it) || not_array_variable(*name_nb)) {
  5661. ShowError("buildin_checkweight2: illegal scope\n");
  5662. script_pushint(st,0);
  5663. return 1;// not supported
  5664. }
  5665. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5666. ShowError("buildin_checkweight2: illegal type, need int\n");
  5667. script_pushint(st,0);
  5668. return 1;// not supported
  5669. }
  5670. nb_it = getarraysize(st, id_it, idx_it, 0, reference_getref(data_it));
  5671. nb_nb = getarraysize(st, id_nb, idx_nb, 0, reference_getref(data_nb));
  5672. if(nb_it != nb_nb) {
  5673. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5674. script_pushint(st,0);
  5675. return 1;
  5676. }
  5677. for(i=0; i<nb_it; i++) {
  5678. nameid[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5679. script_removetop(st, -1, 0);
  5680. amount[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5681. script_removetop(st, -1, 0);
  5682. } //end loop DO NOT break it prematurly we need to depop all stack
  5683. script_pushint(st,checkweight_sub(sd,nb_it,nameid,amount)); //push result of sub to script
  5684. return SCRIPT_CMD_SUCCESS;
  5685. }
  5686. /*==========================================
  5687. * getitem <item id>,<amount>{,<account ID>};
  5688. * getitem "<item name>",<amount>{,<account ID>};
  5689. *
  5690. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5691. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5692. * Type:
  5693. * 1 - Account Bound
  5694. * 2 - Guild Bound
  5695. * 3 - Party Bound
  5696. * 4 - Character Bound
  5697. *------------------------------------------*/
  5698. BUILDIN_FUNC(getitem)
  5699. {
  5700. int nameid,amount,get_count,i,flag = 0;
  5701. struct item it;
  5702. TBL_PC *sd;
  5703. struct script_data *data;
  5704. data=script_getdata(st,2);
  5705. get_val(st,data);
  5706. if( data_isstring(data) ) {// "<item name>"
  5707. const char *name=conv_str(st,data);
  5708. struct item_data *item_data = itemdb_searchname(name);
  5709. if( item_data == NULL ){
  5710. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5711. return 1; //No item created.
  5712. }
  5713. nameid=item_data->nameid;
  5714. } else if( data_isint(data) ) {// <item id>
  5715. nameid=conv_num(st,data);
  5716. //Violet Box, Blue Box, etc - random item pick
  5717. if( nameid < 0 ) {
  5718. nameid = -nameid;
  5719. flag = 1;
  5720. }
  5721. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5722. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5723. return 1; //No item created.
  5724. }
  5725. } else {
  5726. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5727. return 1;
  5728. }
  5729. // <amount>
  5730. if( (amount=script_getnum(st,3)) <= 0)
  5731. return 0; //return if amount <=0, skip the useles iteration
  5732. memset(&it,0,sizeof(it));
  5733. it.nameid=nameid;
  5734. if(!flag)
  5735. it.identify=1;
  5736. else
  5737. it.identify=itemdb_isidentified(nameid);
  5738. if( !strcmp(script_getfuncname(st),"getitembound") ) {
  5739. char bound = script_getnum(st,4);
  5740. if( bound > BOUND_NONE && bound < BOUND_MAX ) {
  5741. it.bound = bound;
  5742. if( script_hasdata(st,5) )
  5743. sd=map_id2sd(script_getnum(st,5));
  5744. else
  5745. sd=script_rid2sd(st); // Attached player
  5746. }
  5747. else { //Not a correct bound type
  5748. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5749. return SCRIPT_CMD_FAILURE;
  5750. }
  5751. } else if( script_hasdata(st,4) )
  5752. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5753. else
  5754. sd=script_rid2sd(st); // Attached player
  5755. if( sd == NULL ) // no target
  5756. return SCRIPT_CMD_SUCCESS;
  5757. //Check if it's stackable.
  5758. if (!itemdb_isstackable(nameid))
  5759. get_count = 1;
  5760. else
  5761. get_count = amount;
  5762. for (i = 0; i < amount; i += get_count)
  5763. {
  5764. // if not pet egg
  5765. if (!pet_create_egg(sd, nameid))
  5766. {
  5767. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5768. {
  5769. clif_additem(sd, 0, 0, flag);
  5770. if( pc_candrop(sd,&it) )
  5771. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5772. }
  5773. }
  5774. }
  5775. return SCRIPT_CMD_SUCCESS;
  5776. }
  5777. /*==========================================
  5778. *
  5779. *------------------------------------------*/
  5780. BUILDIN_FUNC(getitem2)
  5781. {
  5782. int nameid, amount, get_count, i, flag = 0;
  5783. int iden, ref, attr, c1, c2, c3, c4;
  5784. char bound = BOUND_NONE;
  5785. struct item_data *item_data;
  5786. struct item item_tmp;
  5787. TBL_PC *sd;
  5788. struct script_data *data;
  5789. if( !strcmp(script_getfuncname(st),"getitembound2") ) {
  5790. bound = script_getnum(st,11);
  5791. if( bound > BOUND_NONE && bound < BOUND_MAX ) {
  5792. if( script_hasdata(st,12) )
  5793. sd=map_id2sd(script_getnum(st,12));
  5794. else
  5795. sd=script_rid2sd(st); // Attached player
  5796. }
  5797. else {
  5798. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  5799. return SCRIPT_CMD_FAILURE;
  5800. }
  5801. } else if( script_hasdata(st,11) )
  5802. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5803. else
  5804. sd=script_rid2sd(st); // Attached player
  5805. if( sd == NULL ) // no target
  5806. return SCRIPT_CMD_SUCCESS;
  5807. data=script_getdata(st,2);
  5808. get_val(st,data);
  5809. if( data_isstring(data) ) {
  5810. const char *name=conv_str(st,data);
  5811. struct item_data *item_data = itemdb_searchname(name);
  5812. if( item_data )
  5813. nameid=item_data->nameid;
  5814. else
  5815. nameid=UNKNOWN_ITEM_ID;
  5816. } else
  5817. nameid=conv_num(st,data);
  5818. amount=script_getnum(st,3);
  5819. iden=script_getnum(st,4);
  5820. ref=script_getnum(st,5);
  5821. attr=script_getnum(st,6);
  5822. c1=(short)script_getnum(st,7);
  5823. c2=(short)script_getnum(st,8);
  5824. c3=(short)script_getnum(st,9);
  5825. c4=(short)script_getnum(st,10);
  5826. if(nameid<0) { // Invalide nameid
  5827. nameid = -nameid;
  5828. flag = 1;
  5829. }
  5830. if(nameid > 0) {
  5831. memset(&item_tmp,0,sizeof(item_tmp));
  5832. item_data=itemdb_exists(nameid);
  5833. if (item_data == NULL)
  5834. return -1;
  5835. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR || item_data->type==IT_SHADOWGEAR ) {
  5836. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5837. }
  5838. else if(item_data->type==IT_PETEGG) {
  5839. iden = 1;
  5840. ref = 0;
  5841. }
  5842. else {
  5843. iden = 1;
  5844. ref = attr = 0;
  5845. }
  5846. item_tmp.nameid=nameid;
  5847. if(!flag)
  5848. item_tmp.identify=iden;
  5849. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR || item_data->type==IT_SHADOWGEAR )
  5850. item_tmp.identify=0;
  5851. item_tmp.refine=ref;
  5852. item_tmp.attribute=attr;
  5853. item_tmp.card[0]=(short)c1;
  5854. item_tmp.card[1]=(short)c2;
  5855. item_tmp.card[2]=(short)c3;
  5856. item_tmp.card[3]=(short)c4;
  5857. item_tmp.bound=bound;
  5858. //Check if it's stackable.
  5859. if (!itemdb_isstackable(nameid))
  5860. get_count = 1;
  5861. else
  5862. get_count = amount;
  5863. for (i = 0; i < amount; i += get_count)
  5864. {
  5865. // if not pet egg
  5866. if (!pet_create_egg(sd, nameid))
  5867. {
  5868. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5869. {
  5870. clif_additem(sd, 0, 0, flag);
  5871. if( pc_candrop(sd,&item_tmp) )
  5872. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5873. }
  5874. }
  5875. }
  5876. }
  5877. return SCRIPT_CMD_SUCCESS;
  5878. }
  5879. /** Gives rental item to player
  5880. * rentitem <item id>,<seconds>
  5881. * rentitem "<item name>",<seconds>
  5882. */
  5883. BUILDIN_FUNC(rentitem) {
  5884. struct map_session_data *sd;
  5885. struct script_data *data;
  5886. struct item it;
  5887. int seconds;
  5888. unsigned short nameid = 0;
  5889. int flag;
  5890. data = script_getdata(st,2);
  5891. get_val(st,data);
  5892. if( (sd = script_rid2sd(st)) == NULL )
  5893. return SCRIPT_CMD_SUCCESS;
  5894. if( data_isstring(data) )
  5895. {
  5896. const char *name = conv_str(st,data);
  5897. struct item_data *itd = itemdb_searchname(name);
  5898. if( itd == NULL )
  5899. {
  5900. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5901. return SCRIPT_CMD_FAILURE;
  5902. }
  5903. nameid = itd->nameid;
  5904. }
  5905. else if( data_isint(data) )
  5906. {
  5907. nameid = conv_num(st,data);
  5908. if( nameid == 0 || !itemdb_exists(nameid) )
  5909. {
  5910. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  5911. return SCRIPT_CMD_FAILURE;
  5912. }
  5913. }
  5914. else
  5915. {
  5916. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5917. return SCRIPT_CMD_FAILURE;
  5918. }
  5919. seconds = script_getnum(st,3);
  5920. memset(&it, 0, sizeof(it));
  5921. it.nameid = nameid;
  5922. it.identify = 1;
  5923. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5924. it.bound = BOUND_NONE;
  5925. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5926. {
  5927. clif_additem(sd, 0, 0, flag);
  5928. return SCRIPT_CMD_FAILURE;
  5929. }
  5930. return SCRIPT_CMD_SUCCESS;
  5931. }
  5932. /** Gives rental item to player with advanced option
  5933. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  5934. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  5935. */
  5936. BUILDIN_FUNC(rentitem2) {
  5937. struct map_session_data *sd;
  5938. struct script_data *data;
  5939. struct item it;
  5940. struct item_data *id;
  5941. int seconds, flag;
  5942. unsigned short nameid = 0;
  5943. int iden,ref,attr,c1,c2,c3,c4;
  5944. data = script_getdata(st,2);
  5945. get_val(st,data);
  5946. if( (sd = script_rid2sd(st)) == NULL )
  5947. return SCRIPT_CMD_SUCCESS;
  5948. if( data_isstring(data) ) {
  5949. const char *name = conv_str(st,data);
  5950. id = itemdb_searchname(name);
  5951. if( id == NULL ) {
  5952. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  5953. return SCRIPT_CMD_FAILURE;
  5954. }
  5955. nameid = id->nameid;
  5956. }
  5957. else if( data_isint(data) ) {
  5958. nameid = conv_num(st,data);
  5959. if( !(id = itemdb_search(nameid))) {
  5960. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  5961. return SCRIPT_CMD_FAILURE;
  5962. }
  5963. }
  5964. else {
  5965. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  5966. return SCRIPT_CMD_FAILURE;
  5967. }
  5968. seconds = script_getnum(st,3);
  5969. iden = script_getnum(st,4);
  5970. ref = script_getnum(st,5);
  5971. attr = script_getnum(st,6);
  5972. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  5973. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5974. }
  5975. else if (id->type==IT_PETEGG) {
  5976. iden = 1;
  5977. ref = 0;
  5978. }
  5979. else {
  5980. iden = 1;
  5981. ref = attr = 0;
  5982. }
  5983. c1 = (short)script_getnum(st,7);
  5984. c2 = (short)script_getnum(st,8);
  5985. c3 = (short)script_getnum(st,9);
  5986. c4 = (short)script_getnum(st,10);
  5987. memset(&it, 0, sizeof(it));
  5988. it.nameid = nameid;
  5989. it.identify = iden;
  5990. it.refine = ref;
  5991. it.attribute = attr;
  5992. it.card[0] = (short)c1;
  5993. it.card[1] = (short)c2;
  5994. it.card[2] = (short)c3;
  5995. it.card[3] = (short)c4;
  5996. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5997. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  5998. clif_additem(sd, 0, 0, flag);
  5999. return SCRIPT_CMD_FAILURE;
  6000. }
  6001. return SCRIPT_CMD_SUCCESS;
  6002. }
  6003. /*==========================================
  6004. * gets an item with someone's name inscribed [Skotlex]
  6005. * getinscribeditem item_num, character_name
  6006. * Returned Qty is always 1, only works on equip-able
  6007. * equipment
  6008. *------------------------------------------*/
  6009. BUILDIN_FUNC(getnameditem)
  6010. {
  6011. unsigned short nameid;
  6012. struct item item_tmp;
  6013. TBL_PC *sd, *tsd;
  6014. struct script_data *data;
  6015. sd = script_rid2sd(st);
  6016. if (sd == NULL)
  6017. { //Player not attached!
  6018. script_pushint(st,0);
  6019. return 0;
  6020. }
  6021. data=script_getdata(st,2);
  6022. get_val(st,data);
  6023. if( data_isstring(data) ){
  6024. const char *name=conv_str(st,data);
  6025. struct item_data *item_data = itemdb_searchname(name);
  6026. if( item_data == NULL)
  6027. { //Failed
  6028. script_pushint(st,0);
  6029. return 0;
  6030. }
  6031. nameid = item_data->nameid;
  6032. }else
  6033. nameid = conv_num(st,data);
  6034. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6035. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6036. script_pushint(st,0);
  6037. return 0;
  6038. }
  6039. data=script_getdata(st,3);
  6040. get_val(st,data);
  6041. if( data_isstring(data) ) //Char Name
  6042. tsd=map_nick2sd(conv_str(st,data));
  6043. else //Char Id was given
  6044. tsd=map_charid2sd(conv_num(st,data));
  6045. if( tsd == NULL )
  6046. { //Failed
  6047. script_pushint(st,0);
  6048. return 0;
  6049. }
  6050. memset(&item_tmp,0,sizeof(item_tmp));
  6051. item_tmp.nameid=nameid;
  6052. item_tmp.amount=1;
  6053. item_tmp.identify=1;
  6054. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6055. item_tmp.card[2]=tsd->status.char_id;
  6056. item_tmp.card[3]=tsd->status.char_id >> 16;
  6057. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6058. script_pushint(st,0);
  6059. return 0; //Failed to add item, we will not drop if they don't fit
  6060. }
  6061. script_pushint(st,1);
  6062. return SCRIPT_CMD_SUCCESS;
  6063. }
  6064. /*==========================================
  6065. * gets a random item ID from an item group [Skotlex]
  6066. * groupranditem <group_num>{,<sub_group>};
  6067. *------------------------------------------*/
  6068. BUILDIN_FUNC(grouprandomitem)
  6069. {
  6070. script_pushint(st,itemdb_searchrandomid(script_getnum(st,2),script_getnum(st,3)));
  6071. return SCRIPT_CMD_SUCCESS;
  6072. }
  6073. /**
  6074. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6075. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6076. */
  6077. BUILDIN_FUNC(makeitem) {
  6078. int16 nameid;
  6079. uint16 amount, flag = 0, x, y;
  6080. const char *mapname;
  6081. int m;
  6082. struct item item_tmp;
  6083. struct script_data *data;
  6084. data = script_getdata(st,2);
  6085. get_val(st,data);
  6086. if( data_isstring(data) ){
  6087. const char *name = conv_str(st,data);
  6088. struct item_data *item_data = itemdb_searchname(name);
  6089. if( item_data )
  6090. nameid = item_data->nameid;
  6091. else
  6092. nameid = UNKNOWN_ITEM_ID;
  6093. }
  6094. else
  6095. nameid = conv_num(st,data);
  6096. amount = script_getnum(st,3);
  6097. mapname = script_getstr(st,4);
  6098. x = script_getnum(st,5);
  6099. y = script_getnum(st,6);
  6100. if(strcmp(mapname,"this")==0) {
  6101. TBL_PC *sd;
  6102. sd = script_rid2sd(st);
  6103. if (!sd)
  6104. return SCRIPT_CMD_SUCCESS; //Failed...
  6105. m = sd->bl.m;
  6106. } else
  6107. m = map_mapname2mapid(mapname);
  6108. if(nameid<0) {
  6109. nameid = -nameid;
  6110. flag = 1;
  6111. }
  6112. if(nameid > 0) {
  6113. memset(&item_tmp,0,sizeof(item_tmp));
  6114. item_tmp.nameid = nameid;
  6115. if(!flag)
  6116. item_tmp.identify = 1;
  6117. else
  6118. item_tmp.identify = itemdb_isidentified(nameid);
  6119. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  6120. }
  6121. return SCRIPT_CMD_SUCCESS;
  6122. }
  6123. /**
  6124. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6125. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6126. */
  6127. BUILDIN_FUNC(makeitem2) {
  6128. uint16 nameid, amount, x, y;
  6129. const char *mapname;
  6130. int m;
  6131. struct item item_tmp;
  6132. struct script_data *data;
  6133. struct item_data *id;
  6134. data = script_getdata(st,2);
  6135. get_val(st,data);
  6136. if( data_isstring(data) ){
  6137. const char *name = conv_str(st,data);
  6138. struct item_data *item_data = itemdb_searchname(name);
  6139. if( item_data )
  6140. nameid = item_data->nameid;
  6141. else
  6142. nameid = UNKNOWN_ITEM_ID;
  6143. }
  6144. else
  6145. nameid = conv_num(st,data);
  6146. amount = script_getnum(st,3);
  6147. mapname = script_getstr(st,4);
  6148. x = script_getnum(st,5);
  6149. y = script_getnum(st,6);
  6150. if (strcmp(mapname,"this")==0) {
  6151. TBL_PC *sd;
  6152. sd = script_rid2sd(st);
  6153. if (!sd)
  6154. return SCRIPT_CMD_SUCCESS; //Failed...
  6155. m = sd->bl.m;
  6156. }
  6157. else
  6158. m = map_mapname2mapid(mapname);
  6159. if ((id = itemdb_search(nameid))) {
  6160. char iden, ref, attr;
  6161. memset(&item_tmp,0,sizeof(item_tmp));
  6162. item_tmp.nameid = nameid;
  6163. iden = (char)script_getnum(st,7);
  6164. ref = (char)script_getnum(st,8);
  6165. attr = (char)script_getnum(st,9);
  6166. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6167. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6168. }
  6169. else if (id->type==IT_PETEGG) {
  6170. iden = 1;
  6171. ref = 0;
  6172. }
  6173. else {
  6174. iden = 1;
  6175. ref = attr = 0;
  6176. }
  6177. item_tmp.identify = iden;
  6178. item_tmp.refine = ref;
  6179. item_tmp.attribute = attr;
  6180. item_tmp.card[0] = script_getnum(st,10);
  6181. item_tmp.card[1] = script_getnum(st,11);
  6182. item_tmp.card[2] = script_getnum(st,12);
  6183. item_tmp.card[3] = script_getnum(st,13);
  6184. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  6185. }
  6186. else
  6187. return SCRIPT_CMD_FAILURE;
  6188. return SCRIPT_CMD_SUCCESS;
  6189. }
  6190. /// Counts / deletes the current item given by idx.
  6191. /// Used by buildin_delitem_search
  6192. /// Relies on all input data being already fully valid.
  6193. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6194. {
  6195. int delamount;
  6196. struct item *itm = NULL;
  6197. switch(loc) {
  6198. case 1: // cart
  6199. itm = &sd->status.cart[idx];
  6200. break;
  6201. case 2: // storage
  6202. itm = &sd->status.storage.items[idx];
  6203. break;
  6204. default: //inventory
  6205. itm = &sd->status.inventory[idx];
  6206. break;
  6207. }
  6208. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6209. if( delete_items )
  6210. {
  6211. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6212. {// delete associated pet
  6213. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6214. }
  6215. switch(loc) {
  6216. case 1:
  6217. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6218. break;
  6219. case 2:
  6220. storage_delitem(sd,idx,delamount);
  6221. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6222. break;
  6223. default:
  6224. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6225. break;
  6226. }
  6227. }
  6228. amount[0]-= delamount;
  6229. }
  6230. /// Searches for item(s) and checks, if there is enough of them.
  6231. /// Used by delitem and delitem2
  6232. /// Relies on all input data being already fully valid.
  6233. /// @param exact_match will also match item attributes and cards, not just name id
  6234. /// @return true when all items could be deleted, false when there were not enough items to delete
  6235. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6236. {
  6237. bool delete_items = false;
  6238. int i, amount, important, size;
  6239. struct item *items;
  6240. // prefer always non-equipped items
  6241. it->equip = 0;
  6242. // when searching for nameid only, prefer additionally
  6243. if( !exact_match )
  6244. {
  6245. // non-refined items
  6246. it->refine = 0;
  6247. // card-less items
  6248. memset(it->card, 0, sizeof(it->card));
  6249. }
  6250. switch(loc) {
  6251. case 1: // cart
  6252. size = MAX_CART;
  6253. items = sd->status.cart;
  6254. break;
  6255. case 2: // storage
  6256. size = MAX_STORAGE;
  6257. items = sd->status.storage.items;
  6258. break;
  6259. default: //inventory
  6260. size = MAX_INVENTORY;
  6261. items = sd->status.inventory;
  6262. break;
  6263. }
  6264. for(;;)
  6265. {
  6266. amount = it->amount;
  6267. important = 0;
  6268. // 1st pass -- less important items / exact match
  6269. for( i = 0; amount && i < size; i++ )
  6270. {
  6271. struct item *itm = NULL;
  6272. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6273. {// wrong/invalid item
  6274. continue;
  6275. }
  6276. if( itm->equip != it->equip || itm->refine != it->refine )
  6277. {// not matching attributes
  6278. important++;
  6279. continue;
  6280. }
  6281. if( exact_match )
  6282. {
  6283. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6284. {// not matching exact attributes
  6285. continue;
  6286. }
  6287. }
  6288. else
  6289. {
  6290. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6291. {
  6292. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6293. {// pet which cannot be deleted
  6294. continue;
  6295. }
  6296. }
  6297. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6298. {// named/carded item
  6299. important++;
  6300. continue;
  6301. }
  6302. }
  6303. // count / delete item
  6304. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6305. }
  6306. // 2nd pass -- any matching item
  6307. if( amount == 0 || important == 0 )
  6308. {// either everything was already consumed or no items were skipped
  6309. ;
  6310. }
  6311. else for( i = 0; amount && i < size; i++ )
  6312. {
  6313. struct item *itm = NULL;
  6314. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6315. {// wrong/invalid item
  6316. continue;
  6317. }
  6318. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6319. {// pet which cannot be deleted
  6320. continue;
  6321. }
  6322. if( exact_match )
  6323. {
  6324. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6325. {// not matching attributes
  6326. continue;
  6327. }
  6328. }
  6329. // count / delete item
  6330. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6331. }
  6332. if( amount )
  6333. {// not enough items
  6334. return false;
  6335. }
  6336. else if( delete_items )
  6337. {// we are done with the work
  6338. return true;
  6339. }
  6340. else
  6341. {// get rid of the items now
  6342. delete_items = true;
  6343. }
  6344. }
  6345. }
  6346. /// Deletes items from the target/attached player.
  6347. /// Prioritizes ordinary items.
  6348. ///
  6349. /// delitem <item id>,<amount>{,<account id>}
  6350. /// delitem "<item name>",<amount>{,<account id>}
  6351. /// cartdelitem <item id>,<amount>{,<account id>}
  6352. /// cartdelitem "<item name>",<amount>{,<account id>}
  6353. /// storagedelitem <item id>,<amount>{,<account id>}
  6354. /// storagedelitem "<item name>",<amount>{,<account id>}
  6355. BUILDIN_FUNC(delitem)
  6356. {
  6357. TBL_PC *sd;
  6358. struct item it;
  6359. struct script_data *data;
  6360. uint8 loc = 0;
  6361. char* command = (char*)script_getfuncname(st);
  6362. if(!strncmp(command, "cart", 4))
  6363. loc = 1;
  6364. else if(!strncmp(command, "storage", 7))
  6365. loc = 2;
  6366. //TODO: 3 - Guild Storage
  6367. if( script_hasdata(st,4) )
  6368. {
  6369. int account_id = script_getnum(st,4);
  6370. sd = map_id2sd(account_id); // <account id>
  6371. if( sd == NULL )
  6372. {
  6373. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6374. st->state = END;
  6375. return 1;
  6376. }
  6377. }
  6378. else
  6379. {
  6380. sd = script_rid2sd(st);// attached player
  6381. if( sd == NULL )
  6382. return 0;
  6383. }
  6384. if (loc == 1 && !pc_iscarton(sd)) {
  6385. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6386. return SCRIPT_CMD_FAILURE;
  6387. }
  6388. data = script_getdata(st,2);
  6389. get_val(st,data);
  6390. if( data_isstring(data) )
  6391. {
  6392. const char* item_name = conv_str(st,data);
  6393. struct item_data* id = itemdb_searchname(item_name);
  6394. if( id == NULL )
  6395. {
  6396. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6397. st->state = END;
  6398. return 1;
  6399. }
  6400. it.nameid = id->nameid;// "<item name>"
  6401. }
  6402. else
  6403. {
  6404. it.nameid = conv_num(st,data);// <item id>
  6405. if( !itemdb_exists( it.nameid ) )
  6406. {
  6407. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6408. st->state = END;
  6409. return 1;
  6410. }
  6411. }
  6412. it.amount=script_getnum(st,3);
  6413. if( it.amount <= 0 )
  6414. return 0;// nothing to do
  6415. if( buildin_delitem_search(sd, &it, false, loc) )
  6416. {// success
  6417. return SCRIPT_CMD_SUCCESS;
  6418. }
  6419. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6420. st->state = END;
  6421. st->mes_active = 0;
  6422. clif_scriptclose(sd, st->oid);
  6423. return SCRIPT_CMD_FAILURE;
  6424. }
  6425. /// Deletes items from the target/attached player.
  6426. ///
  6427. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6428. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6429. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6430. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6431. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6432. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6433. BUILDIN_FUNC(delitem2)
  6434. {
  6435. TBL_PC *sd;
  6436. struct item it;
  6437. struct script_data *data;
  6438. uint8 loc = 0;
  6439. char* command = (char*)script_getfuncname(st);
  6440. if(!strncmp(command, "cart", 4))
  6441. loc = 1;
  6442. else if(!strncmp(command, "storage", 7))
  6443. loc = 2;
  6444. //TODO: 3 - Guild Storage
  6445. if( script_hasdata(st,11) )
  6446. {
  6447. int account_id = script_getnum(st,11);
  6448. sd = map_id2sd(account_id); // <account id>
  6449. if( sd == NULL )
  6450. {
  6451. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6452. st->state = END;
  6453. return 1;
  6454. }
  6455. }
  6456. else
  6457. {
  6458. sd = script_rid2sd(st);// attached player
  6459. if( sd == NULL )
  6460. return 0;
  6461. }
  6462. if (loc == 1 && !pc_iscarton(sd)) {
  6463. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6464. script_pushint(st,-1);
  6465. return SCRIPT_CMD_FAILURE;
  6466. }
  6467. data = script_getdata(st,2);
  6468. get_val(st,data);
  6469. if( data_isstring(data) )
  6470. {
  6471. const char* item_name = conv_str(st,data);
  6472. struct item_data* id = itemdb_searchname(item_name);
  6473. if( id == NULL )
  6474. {
  6475. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6476. st->state = END;
  6477. return 1;
  6478. }
  6479. it.nameid = id->nameid;// "<item name>"
  6480. }
  6481. else
  6482. {
  6483. it.nameid = conv_num(st,data);// <item id>
  6484. if( !itemdb_exists( it.nameid ) )
  6485. {
  6486. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6487. st->state = END;
  6488. return 1;
  6489. }
  6490. }
  6491. it.amount=script_getnum(st,3);
  6492. it.identify=script_getnum(st,4);
  6493. it.refine=script_getnum(st,5);
  6494. it.attribute=script_getnum(st,6);
  6495. it.card[0]=(short)script_getnum(st,7);
  6496. it.card[1]=(short)script_getnum(st,8);
  6497. it.card[2]=(short)script_getnum(st,9);
  6498. it.card[3]=(short)script_getnum(st,10);
  6499. if( it.amount <= 0 )
  6500. return 0;// nothing to do
  6501. if( buildin_delitem_search(sd, &it, true, loc) )
  6502. {// success
  6503. return SCRIPT_CMD_SUCCESS;
  6504. }
  6505. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6506. st->state = END;
  6507. st->mes_active = 0;
  6508. clif_scriptclose(sd, st->oid);
  6509. return SCRIPT_CMD_FAILURE;
  6510. }
  6511. /*==========================================
  6512. * Enables/Disables use of items while in an NPC [Skotlex]
  6513. *------------------------------------------*/
  6514. BUILDIN_FUNC(enableitemuse)
  6515. {
  6516. TBL_PC *sd;
  6517. sd=script_rid2sd(st);
  6518. if (sd)
  6519. st->npc_item_flag = sd->npc_item_flag = 1;
  6520. return SCRIPT_CMD_SUCCESS;
  6521. }
  6522. BUILDIN_FUNC(disableitemuse)
  6523. {
  6524. TBL_PC *sd;
  6525. sd=script_rid2sd(st);
  6526. if (sd)
  6527. st->npc_item_flag = sd->npc_item_flag = 0;
  6528. return SCRIPT_CMD_SUCCESS;
  6529. }
  6530. /*==========================================
  6531. * Returns a character's specified stat.
  6532. * Check pc_readparam for available options.
  6533. *------------------------------------------*/
  6534. BUILDIN_FUNC(readparam)
  6535. {
  6536. int type;
  6537. TBL_PC *sd;
  6538. type=script_getnum(st,2);
  6539. if( script_hasdata(st,3) )
  6540. sd=map_nick2sd(script_getstr(st,3));
  6541. else
  6542. sd=script_rid2sd(st);
  6543. if(sd==NULL){
  6544. script_pushint(st,-1);
  6545. return 0;
  6546. }
  6547. script_pushint(st,pc_readparam(sd,type));
  6548. return SCRIPT_CMD_SUCCESS;
  6549. }
  6550. /*==========================================
  6551. * Return charid identification
  6552. * return by @num :
  6553. * 0 : char_id
  6554. * 1 : party_id
  6555. * 2 : guild_id
  6556. * 3 : account_id
  6557. * 4 : bg_id
  6558. *------------------------------------------*/
  6559. BUILDIN_FUNC(getcharid)
  6560. {
  6561. int num;
  6562. TBL_PC *sd;
  6563. num = script_getnum(st,2);
  6564. if( script_hasdata(st,3) )
  6565. sd=map_nick2sd(script_getstr(st,3));
  6566. else
  6567. sd=script_rid2sd(st);
  6568. if(sd==NULL){
  6569. script_pushint(st,0); //return 0, according docs
  6570. return 0;
  6571. }
  6572. switch( num ) {
  6573. case 0: script_pushint(st,sd->status.char_id); break;
  6574. case 1: script_pushint(st,sd->status.party_id); break;
  6575. case 2: script_pushint(st,sd->status.guild_id); break;
  6576. case 3: script_pushint(st,sd->status.account_id); break;
  6577. case 4: script_pushint(st,sd->bg_id); break;
  6578. default:
  6579. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6580. script_pushint(st,0);
  6581. break;
  6582. }
  6583. return SCRIPT_CMD_SUCCESS;
  6584. }
  6585. /*==========================================
  6586. * returns the GID of an NPC
  6587. *------------------------------------------*/
  6588. BUILDIN_FUNC(getnpcid)
  6589. {
  6590. int num = script_getnum(st,2);
  6591. struct npc_data* nd = NULL;
  6592. if( script_hasdata(st,3) )
  6593. {// unique npc name
  6594. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6595. {
  6596. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6597. script_pushint(st,0);
  6598. return 1;
  6599. }
  6600. }
  6601. switch (num) {
  6602. case 0:
  6603. script_pushint(st,nd ? nd->bl.id : st->oid);
  6604. break;
  6605. default:
  6606. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6607. script_pushint(st,0);
  6608. return 1;
  6609. }
  6610. return SCRIPT_CMD_SUCCESS;
  6611. }
  6612. /*==========================================
  6613. * Return the name of the party_id
  6614. * null if not found
  6615. *------------------------------------------*/
  6616. BUILDIN_FUNC(getpartyname)
  6617. {
  6618. int party_id;
  6619. struct party_data* p;
  6620. party_id = script_getnum(st,2);
  6621. if( ( p = party_search(party_id) ) != NULL )
  6622. {
  6623. script_pushstrcopy(st,p->party.name);
  6624. }
  6625. else
  6626. {
  6627. script_pushconststr(st,"null");
  6628. }
  6629. return SCRIPT_CMD_SUCCESS;
  6630. }
  6631. /*==========================================
  6632. * Get the information of the members of a party by type
  6633. * @party_id, @type
  6634. * return by @type :
  6635. * - : nom des membres
  6636. * 1 : char_id des membres
  6637. * 2 : account_id des membres
  6638. *------------------------------------------*/
  6639. BUILDIN_FUNC(getpartymember)
  6640. {
  6641. struct party_data *p;
  6642. int i,j=0,type=0;
  6643. p=party_search(script_getnum(st,2));
  6644. if( script_hasdata(st,3) )
  6645. type=script_getnum(st,3);
  6646. if(p!=NULL){
  6647. for(i=0;i<MAX_PARTY;i++){
  6648. if(p->party.member[i].account_id){
  6649. switch (type) {
  6650. case 2:
  6651. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6652. break;
  6653. case 1:
  6654. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6655. break;
  6656. default:
  6657. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6658. }
  6659. j++;
  6660. }
  6661. }
  6662. }
  6663. mapreg_setreg(add_str("$@partymembercount"),j);
  6664. return SCRIPT_CMD_SUCCESS;
  6665. }
  6666. /*==========================================
  6667. * Retrieves party leader. if flag is specified,
  6668. * return some of the leader data. Otherwise, return name.
  6669. *------------------------------------------*/
  6670. BUILDIN_FUNC(getpartyleader)
  6671. {
  6672. int party_id, type = 0, i=0;
  6673. struct party_data *p;
  6674. party_id=script_getnum(st,2);
  6675. if( script_hasdata(st,3) )
  6676. type=script_getnum(st,3);
  6677. p=party_search(party_id);
  6678. if (p) //Search leader
  6679. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6680. if (!p || i == MAX_PARTY) { //leader not found
  6681. if (type)
  6682. script_pushint(st,-1);
  6683. else
  6684. script_pushconststr(st,"null");
  6685. return 0;
  6686. }
  6687. switch (type) {
  6688. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6689. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6690. case 3: script_pushint(st,p->party.member[i].class_); break;
  6691. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6692. case 5: script_pushint(st,p->party.member[i].lv); break;
  6693. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6694. }
  6695. return SCRIPT_CMD_SUCCESS;
  6696. }
  6697. /*==========================================
  6698. * Return the name of the @guild_id
  6699. * null if not found
  6700. *------------------------------------------*/
  6701. BUILDIN_FUNC(getguildname)
  6702. {
  6703. int guild_id;
  6704. struct guild* g;
  6705. guild_id = script_getnum(st,2);
  6706. if( ( g = guild_search(guild_id) ) != NULL )
  6707. script_pushstrcopy(st,g->name);
  6708. else
  6709. script_pushconststr(st,"null");
  6710. return SCRIPT_CMD_SUCCESS;
  6711. }
  6712. /*==========================================
  6713. * Return the name of the guild master of @guild_id
  6714. * null if not found
  6715. *------------------------------------------*/
  6716. BUILDIN_FUNC(getguildmaster)
  6717. {
  6718. int guild_id;
  6719. struct guild* g;
  6720. guild_id = script_getnum(st,2);
  6721. if( ( g = guild_search(guild_id) ) != NULL )
  6722. script_pushstrcopy(st,g->member[0].name);
  6723. else
  6724. script_pushconststr(st,"null");
  6725. return SCRIPT_CMD_SUCCESS;
  6726. }
  6727. BUILDIN_FUNC(getguildmasterid)
  6728. {
  6729. int guild_id;
  6730. struct guild* g;
  6731. guild_id = script_getnum(st,2);
  6732. if( ( g = guild_search(guild_id) ) != NULL )
  6733. script_pushint(st,g->member[0].char_id);
  6734. else
  6735. script_pushint(st,0);
  6736. return SCRIPT_CMD_SUCCESS;
  6737. }
  6738. /*==========================================
  6739. * Get char string information by type :
  6740. * Return by @type :
  6741. * 0 : char_name
  6742. * 1 : party_name or ""
  6743. * 2 : guild_name or ""
  6744. * 3 : map_name
  6745. * - : ""
  6746. *------------------------------------------*/
  6747. BUILDIN_FUNC(strcharinfo)
  6748. {
  6749. TBL_PC *sd;
  6750. int num;
  6751. struct guild* g;
  6752. struct party_data* p;
  6753. sd=script_rid2sd(st);
  6754. if (!sd) { //Avoid crashing....
  6755. script_pushconststr(st,"");
  6756. return 0;
  6757. }
  6758. num=script_getnum(st,2);
  6759. switch(num){
  6760. case 0:
  6761. script_pushstrcopy(st,sd->status.name);
  6762. break;
  6763. case 1:
  6764. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  6765. script_pushstrcopy(st,p->party.name);
  6766. } else {
  6767. script_pushconststr(st,"");
  6768. }
  6769. break;
  6770. case 2:
  6771. if( ( g = sd->guild ) != NULL ) {
  6772. script_pushstrcopy(st,g->name);
  6773. } else {
  6774. script_pushconststr(st,"");
  6775. }
  6776. break;
  6777. case 3:
  6778. script_pushconststr(st,map[sd->bl.m].name);
  6779. break;
  6780. default:
  6781. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6782. script_pushconststr(st,"");
  6783. break;
  6784. }
  6785. return SCRIPT_CMD_SUCCESS;
  6786. }
  6787. /*==========================================
  6788. * Get npc string information by type
  6789. * return by @type:
  6790. * 0 : name
  6791. * 1 : str#
  6792. * 2 : #str
  6793. * 3 : ::str
  6794. * 4 : map name
  6795. *------------------------------------------*/
  6796. BUILDIN_FUNC(strnpcinfo)
  6797. {
  6798. TBL_NPC* nd;
  6799. int num;
  6800. char *buf,*name=NULL;
  6801. nd = map_id2nd(st->oid);
  6802. if (!nd) {
  6803. script_pushconststr(st, "");
  6804. return 0;
  6805. }
  6806. num = script_getnum(st,2);
  6807. switch(num){
  6808. case 0: // display name
  6809. name = aStrdup(nd->name);
  6810. break;
  6811. case 1: // visible part of display name
  6812. if((buf = strchr(nd->name,'#')) != NULL)
  6813. {
  6814. name = aStrdup(nd->name);
  6815. name[buf - nd->name] = 0;
  6816. } else // Return the name, there is no '#' present
  6817. name = aStrdup(nd->name);
  6818. break;
  6819. case 2: // # fragment
  6820. if((buf = strchr(nd->name,'#')) != NULL)
  6821. name = aStrdup(buf+1);
  6822. break;
  6823. case 3: // unique name
  6824. name = aStrdup(nd->exname);
  6825. break;
  6826. case 4: // map name
  6827. name = aStrdup(map[nd->bl.m].name);
  6828. break;
  6829. }
  6830. if(name)
  6831. script_pushstr(st, name);
  6832. else
  6833. script_pushconststr(st, "");
  6834. return SCRIPT_CMD_SUCCESS;
  6835. }
  6836. // aegis->athena slot position conversion table
  6837. static unsigned int equip[] = {EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_GARMENT,EQP_SHOES,EQP_ACC_L,EQP_ACC_R,EQP_HEAD_MID,EQP_HEAD_LOW,EQP_COSTUME_HEAD_LOW,EQP_COSTUME_HEAD_MID,EQP_COSTUME_HEAD_TOP,EQP_COSTUME_GARMENT,EQP_AMMO,EQP_SHADOW_ARMOR,EQP_SHADOW_WEAPON,EQP_SHADOW_SHIELD,EQP_SHADOW_SHOES,EQP_SHADOW_ACC_R,EQP_SHADOW_ACC_L};
  6838. /*==========================================
  6839. * GetEquipID(Pos); Pos: 1-14
  6840. *------------------------------------------*/
  6841. BUILDIN_FUNC(getequipid)
  6842. {
  6843. int i, num;
  6844. TBL_PC* sd;
  6845. struct item_data* item;
  6846. sd = script_rid2sd(st);
  6847. if( sd == NULL )
  6848. return 0;
  6849. num = script_getnum(st,2) - 1;
  6850. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6851. {
  6852. script_pushint(st,-1);
  6853. return 0;
  6854. }
  6855. // get inventory position of item
  6856. i = pc_checkequip(sd,equip[num]);
  6857. if( i < 0 )
  6858. {
  6859. script_pushint(st,-1);
  6860. return 0;
  6861. }
  6862. item = sd->inventory_data[i];
  6863. if( item != 0 )
  6864. script_pushint(st,item->nameid);
  6865. else
  6866. script_pushint(st,0);
  6867. return SCRIPT_CMD_SUCCESS;
  6868. }
  6869. /*==========================================
  6870. * Get the equipement name at pos
  6871. * return item jname or ""
  6872. *------------------------------------------*/
  6873. BUILDIN_FUNC(getequipname)
  6874. {
  6875. int i, num;
  6876. TBL_PC* sd;
  6877. struct item_data* item;
  6878. sd = script_rid2sd(st);
  6879. if( sd == NULL )
  6880. return 0;
  6881. num = script_getnum(st,2) - 1;
  6882. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6883. {
  6884. script_pushconststr(st,"");
  6885. return 0;
  6886. }
  6887. // get inventory position of item
  6888. i = pc_checkequip(sd,equip[num]);
  6889. if( i < 0 )
  6890. {
  6891. script_pushconststr(st,"");
  6892. return 0;
  6893. }
  6894. item = sd->inventory_data[i];
  6895. if( item != 0 )
  6896. script_pushstrcopy(st,item->jname);
  6897. else
  6898. script_pushconststr(st,"");
  6899. return SCRIPT_CMD_SUCCESS;
  6900. }
  6901. /*==========================================
  6902. * getbrokenid [Valaris]
  6903. *------------------------------------------*/
  6904. BUILDIN_FUNC(getbrokenid)
  6905. {
  6906. int i,num,id=0,brokencounter=0;
  6907. TBL_PC *sd;
  6908. sd = script_rid2sd(st);
  6909. if( sd == NULL )
  6910. return 0;
  6911. num=script_getnum(st,2);
  6912. for(i=0; i<MAX_INVENTORY; i++) {
  6913. if(sd->status.inventory[i].attribute){
  6914. brokencounter++;
  6915. if(num==brokencounter){
  6916. id=sd->status.inventory[i].nameid;
  6917. break;
  6918. }
  6919. }
  6920. }
  6921. script_pushint(st,id);
  6922. return SCRIPT_CMD_SUCCESS;
  6923. }
  6924. /*==========================================
  6925. * repair [Valaris]
  6926. *------------------------------------------*/
  6927. BUILDIN_FUNC(repair)
  6928. {
  6929. int i,num;
  6930. int repaircounter=0;
  6931. TBL_PC *sd;
  6932. sd = script_rid2sd(st);
  6933. if( sd == NULL )
  6934. return 0;
  6935. num=script_getnum(st,2);
  6936. for(i=0; i<MAX_INVENTORY; i++) {
  6937. if(sd->status.inventory[i].attribute){
  6938. repaircounter++;
  6939. if(num==repaircounter){
  6940. sd->status.inventory[i].attribute=0;
  6941. clif_equiplist(sd);
  6942. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6943. clif_misceffect(&sd->bl, 3);
  6944. break;
  6945. }
  6946. }
  6947. }
  6948. return SCRIPT_CMD_SUCCESS;
  6949. }
  6950. /*==========================================
  6951. * repairall
  6952. *------------------------------------------*/
  6953. BUILDIN_FUNC(repairall)
  6954. {
  6955. int i, repaircounter = 0;
  6956. TBL_PC *sd;
  6957. sd = script_rid2sd(st);
  6958. if(sd == NULL)
  6959. return 0;
  6960. for(i = 0; i < MAX_INVENTORY; i++)
  6961. {
  6962. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  6963. {
  6964. sd->status.inventory[i].attribute = 0;
  6965. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  6966. repaircounter++;
  6967. }
  6968. }
  6969. if(repaircounter)
  6970. {
  6971. clif_misceffect(&sd->bl, 3);
  6972. clif_equiplist(sd);
  6973. }
  6974. return SCRIPT_CMD_SUCCESS;
  6975. }
  6976. /*==========================================
  6977. * Chk if player have something equiped at pos
  6978. *------------------------------------------*/
  6979. BUILDIN_FUNC(getequipisequiped)
  6980. {
  6981. int i = -1,num;
  6982. TBL_PC *sd;
  6983. num = script_getnum(st,2);
  6984. sd = script_rid2sd(st);
  6985. if( sd == NULL )
  6986. return 0;
  6987. if (num > 0 && num <= ARRAYLENGTH(equip))
  6988. i=pc_checkequip(sd,equip[num-1]);
  6989. if(i >= 0)
  6990. script_pushint(st,1);
  6991. else
  6992. script_pushint(st,0);
  6993. return SCRIPT_CMD_SUCCESS;
  6994. }
  6995. /*==========================================
  6996. * Chk if the player have something equiped at pos
  6997. * if so chk if this item ain't marked not refinable or rental
  6998. * return (npc)
  6999. * 1 : true
  7000. * 0 : false
  7001. *------------------------------------------*/
  7002. BUILDIN_FUNC(getequipisenableref)
  7003. {
  7004. int i = -1,num;
  7005. TBL_PC *sd;
  7006. num = script_getnum(st,2);
  7007. sd = script_rid2sd(st);
  7008. if( sd == NULL )
  7009. return 0;
  7010. if( num > 0 && num <= ARRAYLENGTH(equip) )
  7011. i = pc_checkequip(sd,equip[num-1]);
  7012. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  7013. script_pushint(st,1);
  7014. else
  7015. script_pushint(st,0);
  7016. return SCRIPT_CMD_SUCCESS;
  7017. }
  7018. /*==========================================
  7019. * Get the item refined value at pos
  7020. * return (npc)
  7021. * x : refine amount
  7022. * 0 : false (not refined)
  7023. *------------------------------------------*/
  7024. BUILDIN_FUNC(getequiprefinerycnt)
  7025. {
  7026. int i = -1,num;
  7027. TBL_PC *sd;
  7028. num = script_getnum(st,2);
  7029. sd = script_rid2sd(st);
  7030. if( sd == NULL )
  7031. return 0;
  7032. if (num > 0 && num <= ARRAYLENGTH(equip))
  7033. i=pc_checkequip(sd,equip[num-1]);
  7034. if(i >= 0)
  7035. script_pushint(st,sd->status.inventory[i].refine);
  7036. else
  7037. script_pushint(st,0);
  7038. return SCRIPT_CMD_SUCCESS;
  7039. }
  7040. /*==========================================
  7041. * Get the weapon level value at pos
  7042. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7043. * return (npc)
  7044. * x : weapon level
  7045. * 0 : false
  7046. *------------------------------------------*/
  7047. BUILDIN_FUNC(getequipweaponlv)
  7048. {
  7049. int i = -1,num;
  7050. TBL_PC *sd;
  7051. num = script_getnum(st,2);
  7052. sd = script_rid2sd(st);
  7053. if( sd == NULL )
  7054. return 0;
  7055. if (num > 0 && num <= ARRAYLENGTH(equip))
  7056. i=pc_checkequip(sd,equip[num-1]);
  7057. if(i >= 0 && sd->inventory_data[i])
  7058. script_pushint(st,sd->inventory_data[i]->wlv);
  7059. else
  7060. script_pushint(st,0);
  7061. return SCRIPT_CMD_SUCCESS;
  7062. }
  7063. /*==========================================
  7064. * Get the item refine chance (from refine.txt) for item at pos
  7065. * return (npc)
  7066. * x : refine chance
  7067. * 0 : false (max refine level or unequip..)
  7068. *------------------------------------------*/
  7069. BUILDIN_FUNC(getequippercentrefinery)
  7070. {
  7071. int i = -1,num;
  7072. TBL_PC *sd;
  7073. num = script_getnum(st,2);
  7074. sd = script_rid2sd(st);
  7075. if( sd == NULL )
  7076. return 0;
  7077. if (num > 0 && num <= ARRAYLENGTH(equip))
  7078. i=pc_checkequip(sd,equip[num-1]);
  7079. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  7080. script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  7081. else
  7082. script_pushint(st,0);
  7083. return SCRIPT_CMD_SUCCESS;
  7084. }
  7085. /*==========================================
  7086. * Refine +1 item at pos and log and display refine
  7087. *------------------------------------------*/
  7088. BUILDIN_FUNC(successrefitem)
  7089. {
  7090. int i = -1, num, ep, up = 1;
  7091. TBL_PC *sd;
  7092. num = script_getnum(st,2);
  7093. sd = script_rid2sd(st);
  7094. if( sd == NULL )
  7095. return 0;
  7096. if( script_hasdata(st, 3) )
  7097. up = script_getnum(st, 3);
  7098. if (num > 0 && num <= ARRAYLENGTH(equip))
  7099. i=pc_checkequip(sd,equip[num-1]);
  7100. if(i >= 0) {
  7101. ep=sd->status.inventory[i].equip;
  7102. //Logs items, got from (N)PC scripts [Lupus]
  7103. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7104. if (sd->status.inventory[i].refine >= MAX_REFINE)
  7105. return SCRIPT_CMD_SUCCESS;
  7106. sd->status.inventory[i].refine += up;
  7107. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7108. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7109. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  7110. clif_delitem(sd,i,1,3);
  7111. //Logs items, got from (N)PC scripts [Lupus]
  7112. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7113. clif_additem(sd,i,1,0);
  7114. pc_equipitem(sd,i,ep);
  7115. clif_misceffect(&sd->bl,3);
  7116. if(sd->status.inventory[i].refine == MAX_REFINE &&
  7117. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  7118. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  7119. ){ // Fame point system [DracoRPG]
  7120. switch (sd->inventory_data[i]->wlv){
  7121. case 1:
  7122. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7123. break;
  7124. case 2:
  7125. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7126. break;
  7127. case 3:
  7128. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7129. break;
  7130. }
  7131. }
  7132. }
  7133. return SCRIPT_CMD_SUCCESS;
  7134. }
  7135. /*==========================================
  7136. * Show a failed Refine +1 attempt
  7137. *------------------------------------------*/
  7138. BUILDIN_FUNC(failedrefitem)
  7139. {
  7140. int i=-1,num;
  7141. TBL_PC *sd;
  7142. num = script_getnum(st,2);
  7143. sd = script_rid2sd(st);
  7144. if( sd == NULL )
  7145. return 0;
  7146. if (num > 0 && num <= ARRAYLENGTH(equip))
  7147. i=pc_checkequip(sd,equip[num-1]);
  7148. if(i >= 0) {
  7149. sd->status.inventory[i].refine = 0;
  7150. pc_unequipitem(sd,i,3); //recalculate bonus
  7151. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  7152. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7153. clif_misceffect(&sd->bl,2); // display failure effect
  7154. }
  7155. return SCRIPT_CMD_SUCCESS;
  7156. }
  7157. /*==========================================
  7158. * Downgrades an Equipment Part by -1 . [Masao]
  7159. *------------------------------------------*/
  7160. BUILDIN_FUNC(downrefitem)
  7161. {
  7162. int i = -1, num, ep, down = 1;
  7163. TBL_PC *sd;
  7164. sd = script_rid2sd(st);
  7165. if( sd == NULL )
  7166. return 0;
  7167. num = script_getnum(st,2);
  7168. if( script_hasdata(st, 3) )
  7169. down = script_getnum(st, 3);
  7170. if (num > 0 && num <= ARRAYLENGTH(equip))
  7171. i = pc_checkequip(sd,equip[num-1]);
  7172. if(i >= 0) {
  7173. ep = sd->status.inventory[i].equip;
  7174. //Logs items, got from (N)PC scripts [Lupus]
  7175. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7176. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7177. sd->status.inventory[i].refine -= down;
  7178. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7179. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine);
  7180. clif_delitem(sd,i,1,3);
  7181. //Logs items, got from (N)PC scripts [Lupus]
  7182. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7183. clif_additem(sd,i,1,0);
  7184. pc_equipitem(sd,i,ep);
  7185. clif_misceffect(&sd->bl,2);
  7186. }
  7187. return SCRIPT_CMD_SUCCESS;
  7188. }
  7189. /*==========================================
  7190. * Delete the item equipped at pos.
  7191. *------------------------------------------*/
  7192. BUILDIN_FUNC(delequip)
  7193. {
  7194. int i=-1,num,ret=0;
  7195. TBL_PC *sd;
  7196. num = script_getnum(st,2);
  7197. sd = script_rid2sd(st);
  7198. if( sd == NULL )
  7199. return 0;
  7200. if (num > 0 && num <= ARRAYLENGTH(equip))
  7201. i=pc_checkequip(sd,equip[num-1]);
  7202. if(i >= 0) {
  7203. pc_unequipitem(sd,i,3); //recalculate bonus
  7204. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7205. }
  7206. script_pushint(st,ret);
  7207. return SCRIPT_CMD_SUCCESS;
  7208. }
  7209. /*==========================================
  7210. * Break the item equipped at pos.
  7211. *------------------------------------------*/
  7212. BUILDIN_FUNC(breakequip)
  7213. {
  7214. int i=-1,num;
  7215. TBL_PC *sd;
  7216. num = script_getnum(st,2);
  7217. sd = script_rid2sd(st);
  7218. if( sd == NULL )
  7219. return 0;
  7220. if (num > 0 && num <= ARRAYLENGTH(equip))
  7221. i = pc_checkequip(sd,equip[num-1]);
  7222. if (i >= 0) {
  7223. sd->status.inventory[i].attribute = 1;
  7224. pc_unequipitem(sd,i,3);
  7225. clif_equiplist(sd);
  7226. script_pushint(st,1);
  7227. } else
  7228. script_pushint(st,0);
  7229. return SCRIPT_CMD_SUCCESS;
  7230. }
  7231. /*==========================================
  7232. *
  7233. *------------------------------------------*/
  7234. BUILDIN_FUNC(statusup)
  7235. {
  7236. int type;
  7237. TBL_PC *sd;
  7238. type=script_getnum(st,2);
  7239. sd = script_rid2sd(st);
  7240. if( sd == NULL )
  7241. return 0;
  7242. pc_statusup(sd, type, 1);
  7243. return SCRIPT_CMD_SUCCESS;
  7244. }
  7245. /*==========================================
  7246. *
  7247. *------------------------------------------*/
  7248. BUILDIN_FUNC(statusup2)
  7249. {
  7250. int type,val;
  7251. TBL_PC *sd;
  7252. type=script_getnum(st,2);
  7253. val=script_getnum(st,3);
  7254. sd = script_rid2sd(st);
  7255. if( sd == NULL )
  7256. return 0;
  7257. pc_statusup2(sd,type,val);
  7258. return SCRIPT_CMD_SUCCESS;
  7259. }
  7260. /// See 'doc/item_bonus.txt'
  7261. ///
  7262. /// bonus <bonus type>,<val1>;
  7263. /// bonus2 <bonus type>,<val1>,<val2>;
  7264. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7265. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7266. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7267. BUILDIN_FUNC(bonus)
  7268. {
  7269. int type;
  7270. int val1;
  7271. int val2 = 0;
  7272. int val3 = 0;
  7273. int val4 = 0;
  7274. int val5 = 0;
  7275. TBL_PC* sd;
  7276. struct script_data *data;
  7277. sd = script_rid2sd(st);
  7278. if( sd == NULL )
  7279. return 0; // no player attached
  7280. type = script_getnum(st,2);
  7281. switch( type ) {
  7282. case SP_AUTOSPELL:
  7283. case SP_AUTOSPELL_WHENHIT:
  7284. case SP_AUTOSPELL_ONSKILL:
  7285. case SP_SKILL_ATK:
  7286. case SP_SKILL_HEAL:
  7287. case SP_SKILL_HEAL2:
  7288. case SP_ADD_SKILL_BLOW:
  7289. case SP_CASTRATE:
  7290. case SP_ADDEFF_ONSKILL:
  7291. case SP_SKILL_USE_SP_RATE:
  7292. case SP_SKILL_COOLDOWN:
  7293. case SP_SKILL_FIXEDCAST:
  7294. case SP_SKILL_VARIABLECAST:
  7295. case SP_VARCASTRATE:
  7296. case SP_FIXCASTRATE:
  7297. case SP_SKILL_USE_SP:
  7298. // these bonuses support skill names
  7299. data = script_getdata(st, 3);
  7300. get_val(st, data); // Convert into value in case of a variable
  7301. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7302. break;
  7303. default:
  7304. val1 = script_getnum(st,3);
  7305. break;
  7306. }
  7307. switch( script_lastdata(st)-2 ) {
  7308. case 1:
  7309. pc_bonus(sd, type, val1);
  7310. break;
  7311. case 2:
  7312. val2 = script_getnum(st,4);
  7313. pc_bonus2(sd, type, val1, val2);
  7314. break;
  7315. case 3:
  7316. val2 = script_getnum(st,4);
  7317. val3 = script_getnum(st,5);
  7318. pc_bonus3(sd, type, val1, val2, val3);
  7319. break;
  7320. case 4:
  7321. data = script_getdata(st, 4);
  7322. get_val(st, data); // Convert into value in case of a variable
  7323. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7324. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7325. else
  7326. val2 = script_getnum(st,4);
  7327. val3 = script_getnum(st,5);
  7328. val4 = script_getnum(st,6);
  7329. pc_bonus4(sd, type, val1, val2, val3, val4);
  7330. break;
  7331. case 5:
  7332. data = script_getdata(st, 4);
  7333. get_val(st, data); // Convert into value in case of a variable
  7334. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7335. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7336. else
  7337. val2 = script_getnum(st,4);
  7338. val3 = script_getnum(st,5);
  7339. val4 = script_getnum(st,6);
  7340. val5 = script_getnum(st,7);
  7341. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7342. break;
  7343. default:
  7344. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7345. break;
  7346. }
  7347. return SCRIPT_CMD_SUCCESS;
  7348. }
  7349. BUILDIN_FUNC(autobonus)
  7350. {
  7351. unsigned int dur;
  7352. short rate;
  7353. short atk_type = 0;
  7354. TBL_PC* sd;
  7355. const char *bonus_script, *other_script = NULL;
  7356. sd = script_rid2sd(st);
  7357. if( sd == NULL )
  7358. return 0; // no player attached
  7359. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7360. return 0;
  7361. rate = script_getnum(st,3);
  7362. dur = script_getnum(st,4);
  7363. bonus_script = script_getstr(st,2);
  7364. if( !rate || !dur || !bonus_script )
  7365. return 0;
  7366. if( script_hasdata(st,5) )
  7367. atk_type = script_getnum(st,5);
  7368. if( script_hasdata(st,6) )
  7369. other_script = script_getstr(st,6);
  7370. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7371. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7372. {
  7373. script_add_autobonus(bonus_script);
  7374. if( other_script )
  7375. script_add_autobonus(other_script);
  7376. }
  7377. return SCRIPT_CMD_SUCCESS;
  7378. }
  7379. BUILDIN_FUNC(autobonus2)
  7380. {
  7381. unsigned int dur;
  7382. short rate;
  7383. short atk_type = 0;
  7384. TBL_PC* sd;
  7385. const char *bonus_script, *other_script = NULL;
  7386. sd = script_rid2sd(st);
  7387. if( sd == NULL )
  7388. return 0; // no player attached
  7389. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7390. return 0;
  7391. rate = script_getnum(st,3);
  7392. dur = script_getnum(st,4);
  7393. bonus_script = script_getstr(st,2);
  7394. if( !rate || !dur || !bonus_script )
  7395. return 0;
  7396. if( script_hasdata(st,5) )
  7397. atk_type = script_getnum(st,5);
  7398. if( script_hasdata(st,6) )
  7399. other_script = script_getstr(st,6);
  7400. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7401. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7402. {
  7403. script_add_autobonus(bonus_script);
  7404. if( other_script )
  7405. script_add_autobonus(other_script);
  7406. }
  7407. return SCRIPT_CMD_SUCCESS;
  7408. }
  7409. BUILDIN_FUNC(autobonus3)
  7410. {
  7411. unsigned int dur;
  7412. short rate,atk_type;
  7413. TBL_PC* sd;
  7414. const char *bonus_script, *other_script = NULL;
  7415. struct script_data *data;
  7416. sd = script_rid2sd(st);
  7417. if( sd == NULL )
  7418. return 0; // no player attached
  7419. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7420. return 0;
  7421. rate = script_getnum(st,3);
  7422. dur = script_getnum(st,4);
  7423. data = script_getdata(st, 5);
  7424. get_val(st, data); // Convert into value in case of a variable
  7425. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7426. bonus_script = script_getstr(st,2);
  7427. if( !rate || !dur || !atk_type || !bonus_script )
  7428. return 0;
  7429. if( script_hasdata(st,6) )
  7430. other_script = script_getstr(st,6);
  7431. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7432. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  7433. {
  7434. script_add_autobonus(bonus_script);
  7435. if( other_script )
  7436. script_add_autobonus(other_script);
  7437. }
  7438. return SCRIPT_CMD_SUCCESS;
  7439. }
  7440. /// Changes the level of a player skill.
  7441. /// <flag> defaults to 1
  7442. /// <flag>=0 : set the level of the skill
  7443. /// <flag>=1 : set the temporary level of the skill
  7444. /// <flag>=2 : add to the level of the skill
  7445. ///
  7446. /// skill <skill id>,<level>,<flag>
  7447. /// skill <skill id>,<level>
  7448. /// skill "<skill name>",<level>,<flag>
  7449. /// skill "<skill name>",<level>
  7450. BUILDIN_FUNC(skill)
  7451. {
  7452. int id;
  7453. int level;
  7454. int flag = 1;
  7455. TBL_PC* sd;
  7456. struct script_data *data;
  7457. sd = script_rid2sd(st);
  7458. if( sd == NULL )
  7459. return 0;// no player attached, report source
  7460. data = script_getdata(st, 2);
  7461. get_val(st, data); // Convert into value in case of a variable
  7462. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7463. level = script_getnum(st,3);
  7464. if( script_hasdata(st,4) )
  7465. flag = script_getnum(st,4);
  7466. pc_skill(sd, id, level, flag);
  7467. return SCRIPT_CMD_SUCCESS;
  7468. }
  7469. /// Changes the level of a player skill.
  7470. /// like skill, but <flag> defaults to 2
  7471. ///
  7472. /// addtoskill <skill id>,<amount>,<flag>
  7473. /// addtoskill <skill id>,<amount>
  7474. /// addtoskill "<skill name>",<amount>,<flag>
  7475. /// addtoskill "<skill name>",<amount>
  7476. ///
  7477. /// @see skill
  7478. BUILDIN_FUNC(addtoskill)
  7479. {
  7480. int id;
  7481. int level;
  7482. int flag = 2;
  7483. TBL_PC* sd;
  7484. struct script_data *data;
  7485. sd = script_rid2sd(st);
  7486. if( sd == NULL )
  7487. return 0;// no player attached, report source
  7488. data = script_getdata(st, 2);
  7489. get_val(st, data); // Convert into value in case of a variable
  7490. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7491. level = script_getnum(st,3);
  7492. if( script_hasdata(st,4) )
  7493. flag = script_getnum(st,4);
  7494. pc_skill(sd, id, level, flag);
  7495. return SCRIPT_CMD_SUCCESS;
  7496. }
  7497. /// Increases the level of a guild skill.
  7498. ///
  7499. /// guildskill <skill id>,<amount>;
  7500. /// guildskill "<skill name>",<amount>;
  7501. BUILDIN_FUNC(guildskill)
  7502. {
  7503. int id;
  7504. int level;
  7505. TBL_PC* sd;
  7506. int i;
  7507. struct script_data *data;
  7508. sd = script_rid2sd(st);
  7509. if( sd == NULL )
  7510. return 0;// no player attached, report source
  7511. data = script_getdata(st, 2);
  7512. get_val(st, data); // Convert into value in case of a variable
  7513. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7514. level = script_getnum(st,3);
  7515. for( i=0; i < level; i++ )
  7516. guild_skillup(sd, id);
  7517. return SCRIPT_CMD_SUCCESS;
  7518. }
  7519. /// Returns the level of the player skill.
  7520. ///
  7521. /// getskilllv(<skill id>) -> <level>
  7522. /// getskilllv("<skill name>") -> <level>
  7523. BUILDIN_FUNC(getskilllv)
  7524. {
  7525. int id;
  7526. TBL_PC* sd;
  7527. struct script_data *data;
  7528. sd = script_rid2sd(st);
  7529. if( sd == NULL )
  7530. return 0;// no player attached, report source
  7531. data = script_getdata(st, 2);
  7532. get_val(st, data); // Convert into value in case of a variable
  7533. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7534. script_pushint(st, pc_checkskill(sd,id));
  7535. return SCRIPT_CMD_SUCCESS;
  7536. }
  7537. /// Returns the level of the guild skill.
  7538. ///
  7539. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7540. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7541. BUILDIN_FUNC(getgdskilllv)
  7542. {
  7543. int guild_id;
  7544. uint16 skill_id;
  7545. struct guild* g;
  7546. struct script_data *data;
  7547. guild_id = script_getnum(st,2);
  7548. data = script_getdata(st, 3);
  7549. get_val(st, data); // Convert into value in case of a variable
  7550. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7551. g = guild_search(guild_id);
  7552. if( g == NULL )
  7553. script_pushint(st, -1);
  7554. else
  7555. script_pushint(st, guild_checkskill(g,skill_id));
  7556. return SCRIPT_CMD_SUCCESS;
  7557. }
  7558. /// Returns the 'basic_skill_check' setting.
  7559. /// This config determines if the server checks the skill level of NV_BASIC
  7560. /// before allowing the basic actions.
  7561. ///
  7562. /// basicskillcheck() -> <bool>
  7563. BUILDIN_FUNC(basicskillcheck)
  7564. {
  7565. script_pushint(st, battle_config.basic_skill_check);
  7566. return SCRIPT_CMD_SUCCESS;
  7567. }
  7568. /// Returns the GM level of the player.
  7569. ///
  7570. /// getgmlevel() -> <level>
  7571. BUILDIN_FUNC(getgmlevel)
  7572. {
  7573. TBL_PC* sd;
  7574. sd = script_rid2sd(st);
  7575. if( sd == NULL )
  7576. return 0;// no player attached, report source
  7577. script_pushint(st, pc_get_group_level(sd));
  7578. return SCRIPT_CMD_SUCCESS;
  7579. }
  7580. /// Returns the group ID of the player.
  7581. ///
  7582. /// getgroupid() -> <int>
  7583. BUILDIN_FUNC(getgroupid)
  7584. {
  7585. TBL_PC* sd;
  7586. sd = script_rid2sd(st);
  7587. if (sd == NULL)
  7588. return 1; // no player attached, report source
  7589. script_pushint(st, pc_get_group_id(sd));
  7590. return SCRIPT_CMD_SUCCESS;
  7591. }
  7592. /// Terminates the execution of this script instance.
  7593. ///
  7594. /// end
  7595. BUILDIN_FUNC(end)
  7596. {
  7597. TBL_PC* sd;
  7598. sd = map_id2sd(st->rid);
  7599. st->state = END;
  7600. if( st->mes_active )
  7601. st->mes_active = 0;
  7602. if( sd )
  7603. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  7604. return SCRIPT_CMD_SUCCESS;
  7605. }
  7606. /// Checks if the player has that effect state (option).
  7607. ///
  7608. /// checkoption(<option>) -> <bool>
  7609. BUILDIN_FUNC(checkoption)
  7610. {
  7611. int option;
  7612. TBL_PC* sd;
  7613. sd = script_rid2sd(st);
  7614. if( sd == NULL )
  7615. return 0;// no player attached, report source
  7616. option = script_getnum(st,2);
  7617. if( sd->sc.option&option )
  7618. script_pushint(st, 1);
  7619. else
  7620. script_pushint(st, 0);
  7621. return SCRIPT_CMD_SUCCESS;
  7622. }
  7623. /// Checks if the player is in that body state (opt1).
  7624. ///
  7625. /// checkoption1(<opt1>) -> <bool>
  7626. BUILDIN_FUNC(checkoption1)
  7627. {
  7628. int opt1;
  7629. TBL_PC* sd;
  7630. sd = script_rid2sd(st);
  7631. if( sd == NULL )
  7632. return 0;// no player attached, report source
  7633. opt1 = script_getnum(st,2);
  7634. if( sd->sc.opt1 == opt1 )
  7635. script_pushint(st, 1);
  7636. else
  7637. script_pushint(st, 0);
  7638. return SCRIPT_CMD_SUCCESS;
  7639. }
  7640. /// Checks if the player has that health state (opt2).
  7641. ///
  7642. /// checkoption2(<opt2>) -> <bool>
  7643. BUILDIN_FUNC(checkoption2)
  7644. {
  7645. int opt2;
  7646. TBL_PC* sd;
  7647. sd = script_rid2sd(st);
  7648. if( sd == NULL )
  7649. return 0;// no player attached, report source
  7650. opt2 = script_getnum(st,2);
  7651. if( sd->sc.opt2&opt2 )
  7652. script_pushint(st, 1);
  7653. else
  7654. script_pushint(st, 0);
  7655. return SCRIPT_CMD_SUCCESS;
  7656. }
  7657. /// Changes the effect state (option) of the player.
  7658. /// <flag> defaults to 1
  7659. /// <flag>=0 : removes the option
  7660. /// <flag>=other : adds the option
  7661. ///
  7662. /// setoption <option>,<flag>;
  7663. /// setoption <option>;
  7664. BUILDIN_FUNC(setoption)
  7665. {
  7666. int option;
  7667. int flag = 1;
  7668. TBL_PC* sd;
  7669. sd = script_rid2sd(st);
  7670. if( sd == NULL )
  7671. return 0;// no player attached, report source
  7672. option = script_getnum(st,2);
  7673. if( script_hasdata(st,3) )
  7674. flag = script_getnum(st,3);
  7675. else if( !option ){// Request to remove everything.
  7676. flag = 0;
  7677. option = OPTION_FALCON|OPTION_RIDING;
  7678. #ifndef NEW_CARTS
  7679. option |= OPTION_CART;
  7680. #endif
  7681. }
  7682. if( flag ){// Add option
  7683. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  7684. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  7685. pc_setoption(sd, sd->sc.option|option);
  7686. } else// Remove option
  7687. pc_setoption(sd, sd->sc.option&~option);
  7688. return SCRIPT_CMD_SUCCESS;
  7689. }
  7690. /// Returns if the player has a cart.
  7691. ///
  7692. /// checkcart() -> <bool>
  7693. ///
  7694. /// @author Valaris
  7695. BUILDIN_FUNC(checkcart)
  7696. {
  7697. TBL_PC* sd;
  7698. sd = script_rid2sd(st);
  7699. if( sd == NULL )
  7700. return 0;// no player attached, report source
  7701. if( pc_iscarton(sd) )
  7702. script_pushint(st, 1);
  7703. else
  7704. script_pushint(st, 0);
  7705. return SCRIPT_CMD_SUCCESS;
  7706. }
  7707. /// Sets the cart of the player.
  7708. /// <type> defaults to 1
  7709. /// <type>=0 : removes the cart
  7710. /// <type>=1 : Normal cart
  7711. /// <type>=2 : Wooden cart
  7712. /// <type>=3 : Covered cart with flowers and ferns
  7713. /// <type>=4 : Wooden cart with a Panda doll on the back
  7714. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  7715. ///
  7716. /// setcart <type>;
  7717. /// setcart;
  7718. BUILDIN_FUNC(setcart)
  7719. {
  7720. int type = 1;
  7721. TBL_PC* sd;
  7722. sd = script_rid2sd(st);
  7723. if( sd == NULL )
  7724. return 0;// no player attached, report source
  7725. if( script_hasdata(st,2) )
  7726. type = script_getnum(st,2);
  7727. pc_setcart(sd, type);
  7728. return SCRIPT_CMD_SUCCESS;
  7729. }
  7730. /// Returns if the player has a falcon.
  7731. ///
  7732. /// checkfalcon() -> <bool>
  7733. ///
  7734. /// @author Valaris
  7735. BUILDIN_FUNC(checkfalcon)
  7736. {
  7737. TBL_PC* sd;
  7738. sd = script_rid2sd(st);
  7739. if( sd == NULL )
  7740. return 0;// no player attached, report source
  7741. if( pc_isfalcon(sd) )
  7742. script_pushint(st, 1);
  7743. else
  7744. script_pushint(st, 0);
  7745. return SCRIPT_CMD_SUCCESS;
  7746. }
  7747. /// Sets if the player has a falcon or not.
  7748. /// <flag> defaults to 1
  7749. ///
  7750. /// setfalcon <flag>;
  7751. /// setfalcon;
  7752. BUILDIN_FUNC(setfalcon)
  7753. {
  7754. int flag = 1;
  7755. TBL_PC* sd;
  7756. sd = script_rid2sd(st);
  7757. if( sd == NULL )
  7758. return 0;// no player attached, report source
  7759. if( script_hasdata(st,2) )
  7760. flag = script_getnum(st,2);
  7761. pc_setfalcon(sd, flag);
  7762. return SCRIPT_CMD_SUCCESS;
  7763. }
  7764. /// Returns if the player is riding.
  7765. ///
  7766. /// checkriding() -> <bool>
  7767. ///
  7768. /// @author Valaris
  7769. BUILDIN_FUNC(checkriding)
  7770. {
  7771. TBL_PC* sd;
  7772. sd = script_rid2sd(st);
  7773. if( sd == NULL )
  7774. return 0;// no player attached, report source
  7775. if( pc_isriding(sd) )
  7776. script_pushint(st, 1);
  7777. else
  7778. script_pushint(st, 0);
  7779. return SCRIPT_CMD_SUCCESS;
  7780. }
  7781. /// Sets if the player is riding.
  7782. /// <flag> defaults to 1
  7783. ///
  7784. /// setriding <flag>;
  7785. /// setriding;
  7786. BUILDIN_FUNC(setriding)
  7787. {
  7788. int flag = 1;
  7789. TBL_PC* sd;
  7790. sd = script_rid2sd(st);
  7791. if( sd == NULL )
  7792. return 0;// no player attached, report source
  7793. if( script_hasdata(st,2) )
  7794. flag = script_getnum(st,2);
  7795. pc_setriding(sd, flag);
  7796. return SCRIPT_CMD_SUCCESS;
  7797. }
  7798. /// Returns if the player has a warg.
  7799. ///
  7800. /// checkwug() -> <bool>
  7801. ///
  7802. BUILDIN_FUNC(checkwug)
  7803. {
  7804. TBL_PC* sd;
  7805. sd = script_rid2sd(st);
  7806. if( sd == NULL )
  7807. return 0;// no player attached, report source
  7808. if( pc_iswug(sd) || pc_isridingwug(sd) )
  7809. script_pushint(st, 1);
  7810. else
  7811. script_pushint(st, 0);
  7812. return SCRIPT_CMD_SUCCESS;
  7813. }
  7814. /// Returns if the player is wearing MADO Gear.
  7815. ///
  7816. /// checkmadogear() -> <bool>
  7817. ///
  7818. BUILDIN_FUNC(checkmadogear)
  7819. {
  7820. TBL_PC* sd;
  7821. sd = script_rid2sd(st);
  7822. if( sd == NULL )
  7823. return 0;// no player attached, report source
  7824. if( pc_ismadogear(sd) )
  7825. script_pushint(st, 1);
  7826. else
  7827. script_pushint(st, 0);
  7828. return SCRIPT_CMD_SUCCESS;
  7829. }
  7830. /// Sets if the player is riding MADO Gear.
  7831. /// <flag> defaults to 1
  7832. ///
  7833. /// setmadogear <flag>;
  7834. /// setmadogear;
  7835. BUILDIN_FUNC(setmadogear)
  7836. {
  7837. int flag = 1;
  7838. TBL_PC* sd;
  7839. sd = script_rid2sd(st);
  7840. if( sd == NULL )
  7841. return 0;// no player attached, report source
  7842. if( script_hasdata(st,2) )
  7843. flag = script_getnum(st,2);
  7844. pc_setmadogear(sd, flag);
  7845. return SCRIPT_CMD_SUCCESS;
  7846. }
  7847. /// Sets the save point of the player.
  7848. ///
  7849. /// save "<map name>",<x>,<y>
  7850. /// savepoint "<map name>",<x>,<y>
  7851. BUILDIN_FUNC(savepoint)
  7852. {
  7853. int x;
  7854. int y;
  7855. short map;
  7856. const char* str;
  7857. TBL_PC* sd;
  7858. sd = script_rid2sd(st);
  7859. if( sd == NULL )
  7860. return 0;// no player attached, report source
  7861. str = script_getstr(st, 2);
  7862. x = script_getnum(st,3);
  7863. y = script_getnum(st,4);
  7864. map = mapindex_name2id(str);
  7865. if( map )
  7866. pc_setsavepoint(sd, map, x, y);
  7867. return SCRIPT_CMD_SUCCESS;
  7868. }
  7869. /*==========================================
  7870. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  7871. *------------------------------------------*/
  7872. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  7873. {
  7874. int type;
  7875. time_t timer;
  7876. struct tm *t;
  7877. type=script_getnum(st,2);
  7878. switch(type){
  7879. case 2:
  7880. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  7881. // from the system clock.)
  7882. script_pushint(st,(int)time(NULL));
  7883. break;
  7884. case 1:
  7885. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  7886. time(&timer);
  7887. t=localtime(&timer);
  7888. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  7889. break;
  7890. case 0:
  7891. default:
  7892. //type 0:(System Ticks)
  7893. script_pushint(st,gettick());
  7894. break;
  7895. }
  7896. return SCRIPT_CMD_SUCCESS;
  7897. }
  7898. /*==========================================
  7899. * GetTime(Type);
  7900. * 1: Sec 2: Min 3: Hour
  7901. * 4: WeekDay 5: MonthDay 6: Month
  7902. * 7: Year
  7903. *------------------------------------------*/
  7904. BUILDIN_FUNC(gettime) /* Asgard Version */
  7905. {
  7906. int type;
  7907. time_t timer;
  7908. struct tm *t;
  7909. type=script_getnum(st,2);
  7910. time(&timer);
  7911. t=localtime(&timer);
  7912. switch(type){
  7913. case 1://Sec(0~59)
  7914. script_pushint(st,t->tm_sec);
  7915. break;
  7916. case 2://Min(0~59)
  7917. script_pushint(st,t->tm_min);
  7918. break;
  7919. case 3://Hour(0~23)
  7920. script_pushint(st,t->tm_hour);
  7921. break;
  7922. case 4://WeekDay(0~6)
  7923. script_pushint(st,t->tm_wday);
  7924. break;
  7925. case 5://MonthDay(01~31)
  7926. script_pushint(st,t->tm_mday);
  7927. break;
  7928. case 6://Month(01~12)
  7929. script_pushint(st,t->tm_mon+1);
  7930. break;
  7931. case 7://Year(20xx)
  7932. script_pushint(st,t->tm_year+1900);
  7933. break;
  7934. case 8://Year Day(01~366)
  7935. script_pushint(st,t->tm_yday+1);
  7936. break;
  7937. default://(format error)
  7938. script_pushint(st,-1);
  7939. break;
  7940. }
  7941. return SCRIPT_CMD_SUCCESS;
  7942. }
  7943. /*==========================================
  7944. * GetTimeStr("TimeFMT", Length);
  7945. *------------------------------------------*/
  7946. BUILDIN_FUNC(gettimestr)
  7947. {
  7948. char *tmpstr;
  7949. const char *fmtstr;
  7950. int maxlen;
  7951. time_t now = time(NULL);
  7952. fmtstr=script_getstr(st,2);
  7953. maxlen=script_getnum(st,3);
  7954. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  7955. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  7956. tmpstr[maxlen]='\0';
  7957. script_pushstr(st,tmpstr);
  7958. return SCRIPT_CMD_SUCCESS;
  7959. }
  7960. /*==========================================
  7961. * Open player storage
  7962. *------------------------------------------*/
  7963. BUILDIN_FUNC(openstorage)
  7964. {
  7965. TBL_PC* sd;
  7966. sd = script_rid2sd(st);
  7967. if( sd == NULL )
  7968. return 0;
  7969. storage_storageopen(sd);
  7970. return SCRIPT_CMD_SUCCESS;
  7971. }
  7972. BUILDIN_FUNC(guildopenstorage)
  7973. {
  7974. TBL_PC* sd;
  7975. int ret;
  7976. sd = script_rid2sd(st);
  7977. if( sd == NULL )
  7978. return 0;
  7979. ret = storage_guild_storageopen(sd);
  7980. script_pushint(st,ret);
  7981. return SCRIPT_CMD_SUCCESS;
  7982. }
  7983. /*==========================================
  7984. * Make player use a skill trought item usage
  7985. *------------------------------------------*/
  7986. /// itemskill <skill id>,<level>
  7987. /// itemskill "<skill name>",<level>
  7988. BUILDIN_FUNC(itemskill)
  7989. {
  7990. int id;
  7991. int lv;
  7992. TBL_PC* sd;
  7993. struct script_data *data;
  7994. sd = script_rid2sd(st);
  7995. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  7996. return 0;
  7997. data = script_getdata(st, 2);
  7998. get_val(st, data); // Convert into value in case of a variable
  7999. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8000. lv = script_getnum(st,3);
  8001. sd->skillitem=id;
  8002. sd->skillitemlv=lv;
  8003. clif_item_skill(sd,id,lv);
  8004. return SCRIPT_CMD_SUCCESS;
  8005. }
  8006. /*==========================================
  8007. * Attempt to create an item
  8008. *------------------------------------------*/
  8009. BUILDIN_FUNC(produce)
  8010. {
  8011. int trigger;
  8012. TBL_PC* sd;
  8013. sd = script_rid2sd(st);
  8014. if( sd == NULL )
  8015. return 0;
  8016. trigger=script_getnum(st,2);
  8017. clif_skill_produce_mix_list(sd, -1, trigger);
  8018. return SCRIPT_CMD_SUCCESS;
  8019. }
  8020. /*==========================================
  8021. *
  8022. *------------------------------------------*/
  8023. BUILDIN_FUNC(cooking)
  8024. {
  8025. int trigger;
  8026. TBL_PC* sd;
  8027. sd = script_rid2sd(st);
  8028. if( sd == NULL )
  8029. return 0;
  8030. trigger=script_getnum(st,2);
  8031. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8032. return SCRIPT_CMD_SUCCESS;
  8033. }
  8034. /*==========================================
  8035. * Create a pet
  8036. *------------------------------------------*/
  8037. BUILDIN_FUNC(makepet)
  8038. {
  8039. TBL_PC* sd;
  8040. int id,pet_id;
  8041. id=script_getnum(st,2);
  8042. sd = script_rid2sd(st);
  8043. if( sd == NULL )
  8044. return 0;
  8045. pet_id = search_petDB_index(id, PET_CLASS);
  8046. if (pet_id < 0)
  8047. pet_id = search_petDB_index(id, PET_EGG);
  8048. if (pet_id >= 0 && sd) {
  8049. sd->catch_target_class = pet_db[pet_id].class_;
  8050. intif_create_pet(
  8051. sd->status.account_id, sd->status.char_id,
  8052. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8053. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8054. 100, 0, 1, pet_db[pet_id].jname);
  8055. }
  8056. return SCRIPT_CMD_SUCCESS;
  8057. }
  8058. /*==========================================
  8059. * Give player exp base,job * quest_exp_rate/100
  8060. *------------------------------------------*/
  8061. BUILDIN_FUNC(getexp)
  8062. {
  8063. TBL_PC* sd;
  8064. int base=0,job=0;
  8065. double bonus;
  8066. sd = script_rid2sd(st);
  8067. if( sd == NULL )
  8068. return 0;
  8069. base=script_getnum(st,2);
  8070. job =script_getnum(st,3);
  8071. if(base<0 || job<0)
  8072. return 0;
  8073. // bonus for npc-given exp
  8074. bonus = battle_config.quest_exp_rate / 100.;
  8075. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8076. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8077. pc_gainexp(sd, NULL, base, job, true);
  8078. return SCRIPT_CMD_SUCCESS;
  8079. }
  8080. /*==========================================
  8081. * Gain guild exp [Celest]
  8082. *------------------------------------------*/
  8083. BUILDIN_FUNC(guildgetexp)
  8084. {
  8085. TBL_PC* sd;
  8086. int exp;
  8087. sd = script_rid2sd(st);
  8088. if( sd == NULL )
  8089. return 0;
  8090. exp = script_getnum(st,2);
  8091. if(exp < 0)
  8092. return 0;
  8093. if(sd && sd->status.guild_id > 0)
  8094. guild_getexp (sd, exp);
  8095. return SCRIPT_CMD_SUCCESS;
  8096. }
  8097. /*==========================================
  8098. * Changes the guild master of a guild [Skotlex]
  8099. *------------------------------------------*/
  8100. BUILDIN_FUNC(guildchangegm)
  8101. {
  8102. TBL_PC *sd;
  8103. int guild_id;
  8104. const char *name;
  8105. guild_id = script_getnum(st,2);
  8106. name = script_getstr(st,3);
  8107. sd=map_nick2sd(name);
  8108. if (!sd)
  8109. script_pushint(st,0);
  8110. else
  8111. script_pushint(st,guild_gm_change(guild_id, sd));
  8112. return SCRIPT_CMD_SUCCESS;
  8113. }
  8114. /*==========================================
  8115. * Spawn a monster :
  8116. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8117. @mapn,x,y : location
  8118. @str : monster name
  8119. @class_ : mob_id
  8120. @amount : nb to spawn
  8121. @event : event to attach to mob
  8122. *------------------------------------------*/
  8123. BUILDIN_FUNC(monster)
  8124. {
  8125. const char* mapn = script_getstr(st,2);
  8126. int x = script_getnum(st,3);
  8127. int y = script_getnum(st,4);
  8128. const char* str = script_getstr(st,5);
  8129. int class_ = script_getnum(st,6);
  8130. int amount = script_getnum(st,7);
  8131. const char* event = "";
  8132. unsigned int size = SZ_SMALL;
  8133. unsigned int ai = AI_NONE;
  8134. struct map_session_data* sd;
  8135. int16 m;
  8136. int i;
  8137. if (script_hasdata(st, 8)) {
  8138. event = script_getstr(st, 8);
  8139. check_event(st, event);
  8140. }
  8141. if (script_hasdata(st, 9)) {
  8142. size = script_getnum(st, 9);
  8143. if (size > 3) {
  8144. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8145. return 1;
  8146. }
  8147. }
  8148. if (script_hasdata(st, 10)) {
  8149. ai = script_getnum(st, 10);
  8150. if (ai >= AI_MAX) {
  8151. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8152. return 1;
  8153. }
  8154. }
  8155. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8156. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8157. return 1;
  8158. }
  8159. sd = map_id2sd(st->rid);
  8160. if (sd && strcmp(mapn, "this") == 0)
  8161. m = sd->bl.m;
  8162. else
  8163. m = map_mapname2mapid(mapn);
  8164. for(i=0; i<amount; i++){ //not optimised
  8165. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8166. if(mobid) mapreg_setreg(reference_uid(add_str("$@mobid"), i),mobid);
  8167. }
  8168. return SCRIPT_CMD_SUCCESS;
  8169. }
  8170. /*==========================================
  8171. * Request List of Monster Drops
  8172. *------------------------------------------*/
  8173. BUILDIN_FUNC(getmobdrops)
  8174. {
  8175. int class_ = script_getnum(st,2);
  8176. int i, j = 0;
  8177. struct mob_db *mob;
  8178. if( !mobdb_checkid(class_) )
  8179. {
  8180. script_pushint(st, 0);
  8181. return 0;
  8182. }
  8183. mob = mob_db(class_);
  8184. for( i = 0; i < MAX_MOB_DROP; i++ )
  8185. {
  8186. if( mob->dropitem[i].nameid < 1 )
  8187. continue;
  8188. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8189. continue;
  8190. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8191. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8192. j++;
  8193. }
  8194. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8195. script_pushint(st, 1);
  8196. return SCRIPT_CMD_SUCCESS;
  8197. }
  8198. /*==========================================
  8199. * Same as monster but randomize location in x0,x1,y0,y1 area
  8200. *------------------------------------------*/
  8201. BUILDIN_FUNC(areamonster)
  8202. {
  8203. const char* mapn = script_getstr(st,2);
  8204. int x0 = script_getnum(st,3);
  8205. int y0 = script_getnum(st,4);
  8206. int x1 = script_getnum(st,5);
  8207. int y1 = script_getnum(st,6);
  8208. const char* str = script_getstr(st,7);
  8209. int class_ = script_getnum(st,8);
  8210. int amount = script_getnum(st,9);
  8211. const char* event = "";
  8212. unsigned int size = SZ_SMALL;
  8213. unsigned int ai = AI_NONE;
  8214. struct map_session_data* sd;
  8215. int16 m;
  8216. if (script_hasdata(st,10)) {
  8217. event = script_getstr(st, 10);
  8218. check_event(st, event);
  8219. }
  8220. if (script_hasdata(st, 11)) {
  8221. size = script_getnum(st, 11);
  8222. if (size > 3) {
  8223. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8224. return 1;
  8225. }
  8226. }
  8227. if (script_hasdata(st, 12)) {
  8228. ai = script_getnum(st, 12);
  8229. if (ai >= AI_MAX) {
  8230. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8231. return 1;
  8232. }
  8233. }
  8234. sd = map_id2sd(st->rid);
  8235. if (sd && strcmp(mapn, "this") == 0)
  8236. m = sd->bl.m;
  8237. else
  8238. m = map_mapname2mapid(mapn);
  8239. mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, amount, event, size, ai);
  8240. return SCRIPT_CMD_SUCCESS;
  8241. }
  8242. /*==========================================
  8243. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8244. *------------------------------------------*/
  8245. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8246. { //same fix but with killmonster instead - stripping events from mobs.
  8247. TBL_MOB* md = (TBL_MOB*)bl;
  8248. char *event=va_arg(ap,char *);
  8249. int allflag=va_arg(ap,int);
  8250. md->state.npc_killmonster = 1;
  8251. if(!allflag){
  8252. if(strcmp(event,md->npc_event)==0)
  8253. status_kill(bl);
  8254. }else{
  8255. if(!md->spawn)
  8256. status_kill(bl);
  8257. }
  8258. md->state.npc_killmonster = 0;
  8259. return 0;
  8260. }
  8261. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8262. {
  8263. TBL_MOB* md = (TBL_MOB*)bl;
  8264. char *event=va_arg(ap,char *);
  8265. int allflag=va_arg(ap,int);
  8266. if(!allflag){
  8267. if(strcmp(event,md->npc_event)==0)
  8268. status_kill(bl);
  8269. }else{
  8270. if(!md->spawn)
  8271. status_kill(bl);
  8272. }
  8273. return 0;
  8274. }
  8275. BUILDIN_FUNC(killmonster)
  8276. {
  8277. const char *mapname,*event;
  8278. int16 m,allflag=0;
  8279. mapname=script_getstr(st,2);
  8280. event=script_getstr(st,3);
  8281. if(strcmp(event,"All")==0)
  8282. allflag = 1;
  8283. else
  8284. check_event(st, event);
  8285. if( (m=map_mapname2mapid(mapname))<0 )
  8286. return 0;
  8287. if( script_hasdata(st,4) ) {
  8288. if ( script_getnum(st,4) == 1 ) {
  8289. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8290. return 0;
  8291. }
  8292. }
  8293. map_freeblock_lock();
  8294. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8295. map_freeblock_unlock();
  8296. return SCRIPT_CMD_SUCCESS;
  8297. }
  8298. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8299. { //Strips the event from the mob if it's killed the old method.
  8300. struct mob_data *md;
  8301. md = BL_CAST(BL_MOB, bl);
  8302. if (md->npc_event[0])
  8303. md->npc_event[0] = 0;
  8304. status_kill(bl);
  8305. return 0;
  8306. }
  8307. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8308. {
  8309. status_kill(bl);
  8310. return 0;
  8311. }
  8312. BUILDIN_FUNC(killmonsterall)
  8313. {
  8314. const char *mapname;
  8315. int16 m;
  8316. mapname=script_getstr(st,2);
  8317. if( (m = map_mapname2mapid(mapname))<0 )
  8318. return 0;
  8319. if( script_hasdata(st,3) ) {
  8320. if ( script_getnum(st,3) == 1 ) {
  8321. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8322. return 0;
  8323. }
  8324. }
  8325. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8326. return SCRIPT_CMD_SUCCESS;
  8327. }
  8328. /*==========================================
  8329. * Creates a clone of a player.
  8330. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8331. *------------------------------------------*/
  8332. BUILDIN_FUNC(clone)
  8333. {
  8334. TBL_PC *sd, *msd=NULL;
  8335. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  8336. unsigned int duration = 0;
  8337. const char *map,*event;
  8338. map=script_getstr(st,2);
  8339. x=script_getnum(st,3);
  8340. y=script_getnum(st,4);
  8341. event=script_getstr(st,5);
  8342. char_id=script_getnum(st,6);
  8343. if( script_hasdata(st,7) )
  8344. master_id=script_getnum(st,7);
  8345. if( script_hasdata(st,8) )
  8346. mode=script_getnum(st,8);
  8347. if( script_hasdata(st,9) )
  8348. flag=script_getnum(st,9);
  8349. if( script_hasdata(st,10) )
  8350. duration=script_getnum(st,10);
  8351. check_event(st, event);
  8352. m = map_mapname2mapid(map);
  8353. if (m < 0) return 0;
  8354. sd = map_charid2sd(char_id);
  8355. if (master_id) {
  8356. msd = map_charid2sd(master_id);
  8357. if (msd)
  8358. master_id = msd->bl.id;
  8359. else
  8360. master_id = 0;
  8361. }
  8362. if (sd) //Return ID of newly crafted clone.
  8363. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8364. else //Failed to create clone.
  8365. script_pushint(st,0);
  8366. return SCRIPT_CMD_SUCCESS;
  8367. }
  8368. /*==========================================
  8369. *------------------------------------------*/
  8370. BUILDIN_FUNC(doevent)
  8371. {
  8372. const char* event = script_getstr(st,2);
  8373. struct map_session_data* sd;
  8374. if( ( sd = script_rid2sd(st) ) == NULL )
  8375. {
  8376. return 0;
  8377. }
  8378. check_event(st, event);
  8379. npc_event(sd, event, 0);
  8380. return SCRIPT_CMD_SUCCESS;
  8381. }
  8382. /*==========================================
  8383. *------------------------------------------*/
  8384. BUILDIN_FUNC(donpcevent)
  8385. {
  8386. const char* event = script_getstr(st,2);
  8387. check_event(st, event);
  8388. if( !npc_event_do(event) ) {
  8389. struct npc_data * nd = map_id2nd(st->oid);
  8390. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8391. script_pushint(st, 0);
  8392. } else
  8393. script_pushint(st, 1);
  8394. return SCRIPT_CMD_SUCCESS;
  8395. }
  8396. /// for Aegis compatibility
  8397. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8398. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8399. {
  8400. const char* npc = script_getstr(st,2);
  8401. const char* command = script_getstr(st,3);
  8402. char event[EVENT_NAME_LENGTH];
  8403. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8404. check_event(st, event);
  8405. npc_event_do(event);
  8406. return SCRIPT_CMD_SUCCESS;
  8407. }
  8408. /*==========================================
  8409. *------------------------------------------*/
  8410. BUILDIN_FUNC(addtimer)
  8411. {
  8412. int tick = script_getnum(st,2);
  8413. const char* event = script_getstr(st, 3);
  8414. TBL_PC* sd;
  8415. check_event(st, event);
  8416. sd = script_rid2sd(st);
  8417. if( sd == NULL )
  8418. return 0;
  8419. if (!pc_addeventtimer(sd,tick,event)) {
  8420. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8421. return SCRIPT_CMD_FAILURE;
  8422. }
  8423. return SCRIPT_CMD_SUCCESS;
  8424. }
  8425. /*==========================================
  8426. *------------------------------------------*/
  8427. BUILDIN_FUNC(deltimer)
  8428. {
  8429. const char *event;
  8430. TBL_PC* sd;
  8431. event=script_getstr(st, 2);
  8432. sd = script_rid2sd(st);
  8433. if( sd == NULL )
  8434. return 0;
  8435. check_event(st, event);
  8436. pc_deleventtimer(sd,event);
  8437. return SCRIPT_CMD_SUCCESS;
  8438. }
  8439. /*==========================================
  8440. *------------------------------------------*/
  8441. BUILDIN_FUNC(addtimercount)
  8442. {
  8443. const char *event;
  8444. int tick;
  8445. TBL_PC* sd;
  8446. tick=script_getnum(st,2);
  8447. event=script_getstr(st,3);
  8448. sd = script_rid2sd(st);
  8449. if( sd == NULL )
  8450. return 0;
  8451. check_event(st, event);
  8452. pc_addeventtimercount(sd,event,tick);
  8453. return SCRIPT_CMD_SUCCESS;
  8454. }
  8455. /*==========================================
  8456. *------------------------------------------*/
  8457. BUILDIN_FUNC(initnpctimer)
  8458. {
  8459. struct npc_data *nd;
  8460. int flag = 0;
  8461. if( script_hasdata(st,3) )
  8462. { //Two arguments: NPC name and attach flag.
  8463. nd = npc_name2id(script_getstr(st, 2));
  8464. flag = script_getnum(st,3);
  8465. }
  8466. else if( script_hasdata(st,2) )
  8467. { //Check if argument is numeric (flag) or string (npc name)
  8468. struct script_data *data;
  8469. data = script_getdata(st,2);
  8470. get_val(st,data);
  8471. if( data_isstring(data) ) //NPC name
  8472. nd = npc_name2id(conv_str(st, data));
  8473. else if( data_isint(data) ) //Flag
  8474. {
  8475. nd = (struct npc_data *)map_id2bl(st->oid);
  8476. flag = conv_num(st,data);
  8477. }
  8478. else
  8479. {
  8480. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8481. return 1;
  8482. }
  8483. }
  8484. else
  8485. nd = (struct npc_data *)map_id2bl(st->oid);
  8486. if( !nd )
  8487. return 0;
  8488. if( flag ) //Attach
  8489. {
  8490. TBL_PC* sd = script_rid2sd(st);
  8491. if( sd == NULL )
  8492. return 0;
  8493. nd->u.scr.rid = sd->bl.id;
  8494. }
  8495. nd->u.scr.timertick = 0;
  8496. npc_settimerevent_tick(nd,0);
  8497. npc_timerevent_start(nd, st->rid);
  8498. return SCRIPT_CMD_SUCCESS;
  8499. }
  8500. /*==========================================
  8501. *------------------------------------------*/
  8502. BUILDIN_FUNC(startnpctimer)
  8503. {
  8504. struct npc_data *nd;
  8505. int flag = 0;
  8506. if( script_hasdata(st,3) )
  8507. { //Two arguments: NPC name and attach flag.
  8508. nd = npc_name2id(script_getstr(st, 2));
  8509. flag = script_getnum(st,3);
  8510. }
  8511. else if( script_hasdata(st,2) )
  8512. { //Check if argument is numeric (flag) or string (npc name)
  8513. struct script_data *data;
  8514. data = script_getdata(st,2);
  8515. get_val(st,data);
  8516. if( data_isstring(data) ) //NPC name
  8517. nd = npc_name2id(conv_str(st, data));
  8518. else if( data_isint(data) ) //Flag
  8519. {
  8520. nd = (struct npc_data *)map_id2bl(st->oid);
  8521. flag = conv_num(st,data);
  8522. }
  8523. else
  8524. {
  8525. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8526. return 1;
  8527. }
  8528. }
  8529. else
  8530. nd=(struct npc_data *)map_id2bl(st->oid);
  8531. if( !nd )
  8532. return 0;
  8533. if( flag ) //Attach
  8534. {
  8535. TBL_PC* sd = script_rid2sd(st);
  8536. if( sd == NULL )
  8537. return 0;
  8538. nd->u.scr.rid = sd->bl.id;
  8539. }
  8540. npc_timerevent_start(nd, st->rid);
  8541. return SCRIPT_CMD_SUCCESS;
  8542. }
  8543. /*==========================================
  8544. *------------------------------------------*/
  8545. BUILDIN_FUNC(stopnpctimer)
  8546. {
  8547. struct npc_data *nd;
  8548. int flag = 0;
  8549. if( script_hasdata(st,3) )
  8550. { //Two arguments: NPC name and attach flag.
  8551. nd = npc_name2id(script_getstr(st, 2));
  8552. flag = script_getnum(st,3);
  8553. }
  8554. else if( script_hasdata(st,2) )
  8555. { //Check if argument is numeric (flag) or string (npc name)
  8556. struct script_data *data;
  8557. data = script_getdata(st,2);
  8558. get_val(st,data);
  8559. if( data_isstring(data) ) //NPC name
  8560. nd = npc_name2id(conv_str(st, data));
  8561. else if( data_isint(data) ) //Flag
  8562. {
  8563. nd = (struct npc_data *)map_id2bl(st->oid);
  8564. flag = conv_num(st,data);
  8565. }
  8566. else
  8567. {
  8568. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8569. return 1;
  8570. }
  8571. }
  8572. else
  8573. nd=(struct npc_data *)map_id2bl(st->oid);
  8574. if( !nd )
  8575. return 0;
  8576. if( flag ) //Detach
  8577. nd->u.scr.rid = 0;
  8578. npc_timerevent_stop(nd);
  8579. return SCRIPT_CMD_SUCCESS;
  8580. }
  8581. /*==========================================
  8582. *------------------------------------------*/
  8583. BUILDIN_FUNC(getnpctimer)
  8584. {
  8585. struct npc_data *nd;
  8586. TBL_PC *sd;
  8587. int type = script_getnum(st,2);
  8588. int val = 0;
  8589. if( script_hasdata(st,3) )
  8590. nd = npc_name2id(script_getstr(st,3));
  8591. else
  8592. nd = (struct npc_data *)map_id2bl(st->oid);
  8593. if( !nd || nd->bl.type != BL_NPC )
  8594. {
  8595. script_pushint(st,0);
  8596. ShowError("getnpctimer: Invalid NPC.\n");
  8597. return 1;
  8598. }
  8599. switch( type )
  8600. {
  8601. case 0: val = npc_gettimerevent_tick(nd); break;
  8602. case 1:
  8603. if( nd->u.scr.rid )
  8604. {
  8605. sd = map_id2sd(nd->u.scr.rid);
  8606. if( !sd )
  8607. {
  8608. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8609. break;
  8610. }
  8611. val = (sd->npc_timer_id != INVALID_TIMER);
  8612. }
  8613. else
  8614. val = (nd->u.scr.timerid != INVALID_TIMER);
  8615. break;
  8616. case 2: val = nd->u.scr.timeramount; break;
  8617. }
  8618. script_pushint(st,val);
  8619. return SCRIPT_CMD_SUCCESS;
  8620. }
  8621. /*==========================================
  8622. *------------------------------------------*/
  8623. BUILDIN_FUNC(setnpctimer)
  8624. {
  8625. int tick;
  8626. struct npc_data *nd;
  8627. tick = script_getnum(st,2);
  8628. if( script_hasdata(st,3) )
  8629. nd = npc_name2id(script_getstr(st,3));
  8630. else
  8631. nd = (struct npc_data *)map_id2bl(st->oid);
  8632. if( !nd || nd->bl.type != BL_NPC )
  8633. {
  8634. script_pushint(st,1);
  8635. ShowError("setnpctimer: Invalid NPC.\n");
  8636. return 1;
  8637. }
  8638. npc_settimerevent_tick(nd,tick);
  8639. script_pushint(st,0);
  8640. return SCRIPT_CMD_SUCCESS;
  8641. }
  8642. /*==========================================
  8643. * attaches the player rid to the timer [Celest]
  8644. *------------------------------------------*/
  8645. BUILDIN_FUNC(attachnpctimer)
  8646. {
  8647. TBL_PC *sd;
  8648. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  8649. if( !nd || nd->bl.type != BL_NPC )
  8650. {
  8651. script_pushint(st,1);
  8652. ShowError("setnpctimer: Invalid NPC.\n");
  8653. return 1;
  8654. }
  8655. if( script_hasdata(st,2) )
  8656. sd = map_nick2sd(script_getstr(st,2));
  8657. else
  8658. sd = script_rid2sd(st);
  8659. if( !sd )
  8660. {
  8661. script_pushint(st,1);
  8662. ShowWarning("attachnpctimer: Invalid player.\n");
  8663. return 1;
  8664. }
  8665. nd->u.scr.rid = sd->bl.id;
  8666. script_pushint(st,0);
  8667. return SCRIPT_CMD_SUCCESS;
  8668. }
  8669. /*==========================================
  8670. * detaches a player rid from the timer [Celest]
  8671. *------------------------------------------*/
  8672. BUILDIN_FUNC(detachnpctimer)
  8673. {
  8674. struct npc_data *nd;
  8675. if( script_hasdata(st,2) )
  8676. nd = npc_name2id(script_getstr(st,2));
  8677. else
  8678. nd = (struct npc_data *)map_id2bl(st->oid);
  8679. if( !nd || nd->bl.type != BL_NPC )
  8680. {
  8681. script_pushint(st,1);
  8682. ShowError("detachnpctimer: Invalid NPC.\n");
  8683. return 1;
  8684. }
  8685. nd->u.scr.rid = 0;
  8686. script_pushint(st,0);
  8687. return SCRIPT_CMD_SUCCESS;
  8688. }
  8689. /*==========================================
  8690. * To avoid "player not attached" script errors, this function is provided,
  8691. * it checks if there is a player attached to the current script. [Skotlex]
  8692. * If no, returns 0, if yes, returns the account_id of the attached player.
  8693. *------------------------------------------*/
  8694. BUILDIN_FUNC(playerattached)
  8695. {
  8696. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  8697. script_pushint(st,0);
  8698. else
  8699. script_pushint(st,st->rid);
  8700. return SCRIPT_CMD_SUCCESS;
  8701. }
  8702. /*==========================================
  8703. *------------------------------------------*/
  8704. BUILDIN_FUNC(announce)
  8705. {
  8706. const char *mes = script_getstr(st,2);
  8707. int flag = script_getnum(st,3);
  8708. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  8709. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  8710. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  8711. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  8712. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  8713. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  8714. {
  8715. send_target target;
  8716. 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
  8717. if (bl == NULL)
  8718. return 0;
  8719. switch (flag&BC_TARGET_MASK) {
  8720. case BC_MAP: target = ALL_SAMEMAP; break;
  8721. case BC_AREA: target = AREA; break;
  8722. case BC_SELF: target = SELF; break;
  8723. default: target = ALL_CLIENT; break; // BC_ALL
  8724. }
  8725. if (fontColor)
  8726. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  8727. else
  8728. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  8729. }
  8730. else
  8731. {
  8732. if (fontColor)
  8733. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  8734. else
  8735. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  8736. }
  8737. return SCRIPT_CMD_SUCCESS;
  8738. }
  8739. /*==========================================
  8740. *------------------------------------------*/
  8741. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  8742. {
  8743. char *mes = va_arg(ap, char *);
  8744. int len = va_arg(ap, int);
  8745. int type = va_arg(ap, int);
  8746. char *fontColor = va_arg(ap, char *);
  8747. short fontType = (short)va_arg(ap, int);
  8748. short fontSize = (short)va_arg(ap, int);
  8749. short fontAlign = (short)va_arg(ap, int);
  8750. short fontY = (short)va_arg(ap, int);
  8751. if (fontColor)
  8752. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  8753. else
  8754. clif_broadcast(bl, mes, len, type, SELF);
  8755. return 0;
  8756. }
  8757. BUILDIN_FUNC(mapannounce)
  8758. {
  8759. const char *mapname = script_getstr(st,2);
  8760. const char *mes = script_getstr(st,3);
  8761. int flag = script_getnum(st,4);
  8762. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  8763. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  8764. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  8765. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  8766. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  8767. int16 m;
  8768. if ((m = map_mapname2mapid(mapname)) < 0)
  8769. return 0;
  8770. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  8771. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  8772. return SCRIPT_CMD_SUCCESS;
  8773. }
  8774. /*==========================================
  8775. *------------------------------------------*/
  8776. BUILDIN_FUNC(areaannounce)
  8777. {
  8778. const char *mapname = script_getstr(st,2);
  8779. int x0 = script_getnum(st,3);
  8780. int y0 = script_getnum(st,4);
  8781. int x1 = script_getnum(st,5);
  8782. int y1 = script_getnum(st,6);
  8783. const char *mes = script_getstr(st,7);
  8784. int flag = script_getnum(st,8);
  8785. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  8786. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  8787. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  8788. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  8789. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  8790. int16 m;
  8791. if ((m = map_mapname2mapid(mapname)) < 0)
  8792. return 0;
  8793. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  8794. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  8795. return SCRIPT_CMD_SUCCESS;
  8796. }
  8797. /*==========================================
  8798. *------------------------------------------*/
  8799. BUILDIN_FUNC(getusers)
  8800. {
  8801. int flag, val = 0;
  8802. struct map_session_data* sd;
  8803. struct block_list* bl = NULL;
  8804. flag = script_getnum(st,2);
  8805. switch(flag&0x07)
  8806. {
  8807. case 0:
  8808. if(flag&0x8)
  8809. {// npc
  8810. bl = map_id2bl(st->oid);
  8811. }
  8812. else if((sd = script_rid2sd(st))!=NULL)
  8813. {// pc
  8814. bl = &sd->bl;
  8815. }
  8816. if(bl)
  8817. {
  8818. val = map[bl->m].users;
  8819. }
  8820. break;
  8821. case 1:
  8822. val = map_getusers();
  8823. break;
  8824. default:
  8825. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  8826. script_pushint(st,0);
  8827. return 1;
  8828. }
  8829. script_pushint(st,val);
  8830. return SCRIPT_CMD_SUCCESS;
  8831. }
  8832. /*==========================================
  8833. * Works like @WHO - displays all online users names in window
  8834. *------------------------------------------*/
  8835. BUILDIN_FUNC(getusersname)
  8836. {
  8837. TBL_PC *sd, *pl_sd;
  8838. int /*disp_num=1,*/ group_level = 0;
  8839. struct s_mapiterator* iter;
  8840. sd = script_rid2sd(st);
  8841. if (!sd) return 0;
  8842. group_level = pc_get_group_level(sd);
  8843. iter = mapit_getallusers();
  8844. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  8845. {
  8846. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  8847. continue; // skip hidden sessions
  8848. /* Temporary fix for bugreport:1023.
  8849. * Do not uncomment unless you want thousands of 'next' buttons.
  8850. if((disp_num++)%10==0)
  8851. clif_scriptnext(sd,st->oid);*/
  8852. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  8853. }
  8854. mapit_free(iter);
  8855. return SCRIPT_CMD_SUCCESS;
  8856. }
  8857. /*==========================================
  8858. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  8859. *------------------------------------------*/
  8860. BUILDIN_FUNC(getmapguildusers)
  8861. {
  8862. const char *str;
  8863. int16 m;
  8864. int gid;
  8865. int i=0,c=0;
  8866. struct guild *g = NULL;
  8867. str=script_getstr(st,2);
  8868. gid=script_getnum(st,3);
  8869. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  8870. script_pushint(st,-1);
  8871. return 0;
  8872. }
  8873. g = guild_search(gid);
  8874. if (g){
  8875. for(i = 0; i < g->max_member; i++)
  8876. {
  8877. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  8878. c++;
  8879. }
  8880. }
  8881. script_pushint(st,c);
  8882. return SCRIPT_CMD_SUCCESS;
  8883. }
  8884. /*==========================================
  8885. *------------------------------------------*/
  8886. BUILDIN_FUNC(getmapusers)
  8887. {
  8888. const char *str;
  8889. int16 m;
  8890. str=script_getstr(st,2);
  8891. if( (m=map_mapname2mapid(str))< 0){
  8892. script_pushint(st,-1);
  8893. return 0;
  8894. }
  8895. script_pushint(st,map[m].users);
  8896. return SCRIPT_CMD_SUCCESS;
  8897. }
  8898. /*==========================================
  8899. *------------------------------------------*/
  8900. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  8901. {
  8902. int *users=va_arg(ap,int *);
  8903. (*users)++;
  8904. return 0;
  8905. }
  8906. BUILDIN_FUNC(getareausers)
  8907. {
  8908. const char *str;
  8909. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  8910. str=script_getstr(st,2);
  8911. x0=script_getnum(st,3);
  8912. y0=script_getnum(st,4);
  8913. x1=script_getnum(st,5);
  8914. y1=script_getnum(st,6);
  8915. if( (m=map_mapname2mapid(str))< 0){
  8916. script_pushint(st,-1);
  8917. return 0;
  8918. }
  8919. map_foreachinarea(buildin_getareausers_sub,
  8920. m,x0,y0,x1,y1,BL_PC,&users);
  8921. script_pushint(st,users);
  8922. return SCRIPT_CMD_SUCCESS;
  8923. }
  8924. /*==========================================
  8925. *------------------------------------------*/
  8926. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  8927. {
  8928. int item=va_arg(ap,int);
  8929. int *amount=va_arg(ap,int *);
  8930. struct flooritem_data *drop=(struct flooritem_data *)bl;
  8931. if(drop->item_data.nameid==item)
  8932. (*amount)+=drop->item_data.amount;
  8933. return 0;
  8934. }
  8935. BUILDIN_FUNC(getareadropitem)
  8936. {
  8937. const char *str;
  8938. int16 m,x0,y0,x1,y1;
  8939. int item,amount=0;
  8940. struct script_data *data;
  8941. str=script_getstr(st,2);
  8942. x0=script_getnum(st,3);
  8943. y0=script_getnum(st,4);
  8944. x1=script_getnum(st,5);
  8945. y1=script_getnum(st,6);
  8946. data=script_getdata(st,7);
  8947. get_val(st,data);
  8948. if( data_isstring(data) ){
  8949. const char *name=conv_str(st,data);
  8950. struct item_data *item_data = itemdb_searchname(name);
  8951. item=UNKNOWN_ITEM_ID;
  8952. if( item_data )
  8953. item=item_data->nameid;
  8954. }else
  8955. item=conv_num(st,data);
  8956. if( (m=map_mapname2mapid(str))< 0){
  8957. script_pushint(st,-1);
  8958. return 0;
  8959. }
  8960. map_foreachinarea(buildin_getareadropitem_sub,
  8961. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  8962. script_pushint(st,amount);
  8963. return SCRIPT_CMD_SUCCESS;
  8964. }
  8965. /*==========================================
  8966. *------------------------------------------*/
  8967. BUILDIN_FUNC(enablenpc)
  8968. {
  8969. const char *str;
  8970. str=script_getstr(st,2);
  8971. npc_enable(str,1);
  8972. return SCRIPT_CMD_SUCCESS;
  8973. }
  8974. /*==========================================
  8975. *------------------------------------------*/
  8976. BUILDIN_FUNC(disablenpc)
  8977. {
  8978. const char *str;
  8979. str=script_getstr(st,2);
  8980. npc_enable(str,0);
  8981. return SCRIPT_CMD_SUCCESS;
  8982. }
  8983. /*==========================================
  8984. *------------------------------------------*/
  8985. BUILDIN_FUNC(hideoffnpc)
  8986. {
  8987. const char *str;
  8988. str=script_getstr(st,2);
  8989. npc_enable(str,2);
  8990. return SCRIPT_CMD_SUCCESS;
  8991. }
  8992. /*==========================================
  8993. *------------------------------------------*/
  8994. BUILDIN_FUNC(hideonnpc)
  8995. {
  8996. const char *str;
  8997. str=script_getstr(st,2);
  8998. npc_enable(str,4);
  8999. return SCRIPT_CMD_SUCCESS;
  9000. }
  9001. /* Starts a status effect on the target unit or on the attached player.
  9002. *
  9003. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9004. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9005. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9006. * <flag>
  9007. * &1: Cannot be avoided (it has to start)
  9008. * &2: Tick should not be reduced (by vit, luk, lv, etc)
  9009. * &4: sc_data loaded, no value has to be altered.
  9010. * &8: rate should not be reduced
  9011. */
  9012. BUILDIN_FUNC(sc_start)
  9013. {
  9014. TBL_NPC * nd = map_id2nd(st->oid);
  9015. struct block_list* bl;
  9016. enum sc_type type;
  9017. int tick, val1, val2, val3, val4=0, rate, flag;
  9018. char start_type;
  9019. const char* command = script_getfuncname(st);
  9020. if(strstr(command, "4"))
  9021. start_type = 4;
  9022. else if(strstr(command, "2"))
  9023. start_type = 2;
  9024. else
  9025. start_type = 1;
  9026. type = (sc_type)script_getnum(st,2);
  9027. tick = script_getnum(st,3);
  9028. val1 = script_getnum(st,4);
  9029. //If from NPC we make default flag 1 to be unavoidable
  9030. if(nd && nd->bl.id == fake_nd->bl.id)
  9031. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):2;
  9032. else
  9033. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):1;
  9034. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9035. if(script_hasdata(st,(6+start_type)))
  9036. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9037. else
  9038. bl = map_id2bl(st->rid);
  9039. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9040. {// When there isn't a duration specified, try to get it from the skill_db
  9041. tick = skill_get_time(status_sc2skill(type), val1);
  9042. }
  9043. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9044. bl = map_id2bl(potion_target);
  9045. tick /= 2;// Thrown potions only last half.
  9046. val4 = 1;// Mark that this was a thrown sc_effect
  9047. }
  9048. if(!bl)
  9049. return 0;
  9050. switch(start_type) {
  9051. case 1:
  9052. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9053. break;
  9054. case 2:
  9055. val2 = script_getnum(st,5);
  9056. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9057. break;
  9058. case 4:
  9059. val2 = script_getnum(st,5);
  9060. val3 = script_getnum(st,6);
  9061. val4 = script_getnum(st,7);
  9062. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9063. break;
  9064. }
  9065. return SCRIPT_CMD_SUCCESS;
  9066. }
  9067. /// Ends one or all status effects on the target unit or on the attached player.
  9068. ///
  9069. /// sc_end <effect_id>{,<unit_id>};
  9070. BUILDIN_FUNC(sc_end)
  9071. {
  9072. struct block_list* bl;
  9073. int type;
  9074. type = script_getnum(st, 2);
  9075. if (script_hasdata(st, 3))
  9076. bl = map_id2bl(script_getnum(st, 3));
  9077. else
  9078. bl = map_id2bl(st->rid);
  9079. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9080. bl = map_id2bl(potion_target);
  9081. if (!bl)
  9082. return 0;
  9083. if (type >= 0 && type < SC_MAX)
  9084. {
  9085. struct status_change *sc = status_get_sc(bl);
  9086. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9087. if (!sce)
  9088. return 0;
  9089. switch (type)
  9090. {
  9091. case SC_WEIGHT50:
  9092. case SC_WEIGHT90:
  9093. case SC_NOCHAT:
  9094. case SC_PUSH_CART:
  9095. return 0;
  9096. default:
  9097. break;
  9098. }
  9099. //This should help status_change_end force disabling the SC in case it has no limit.
  9100. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9101. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9102. }
  9103. else
  9104. status_change_clear(bl, 3); // remove all effects
  9105. return SCRIPT_CMD_SUCCESS;
  9106. }
  9107. /*==========================================
  9108. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9109. *------------------------------------------*/
  9110. BUILDIN_FUNC(getscrate)
  9111. {
  9112. struct block_list *bl;
  9113. int type,rate;
  9114. type=script_getnum(st,2);
  9115. rate=script_getnum(st,3);
  9116. if( script_hasdata(st,4) ) //get for the bl assigned
  9117. bl = map_id2bl(script_getnum(st,4));
  9118. else
  9119. bl = map_id2bl(st->rid);
  9120. if (bl)
  9121. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, 0);
  9122. script_pushint(st,rate);
  9123. return SCRIPT_CMD_SUCCESS;
  9124. }
  9125. /*==========================================
  9126. * getstatus <type>{, <info>};
  9127. *------------------------------------------*/
  9128. BUILDIN_FUNC(getstatus)
  9129. {
  9130. int id, type;
  9131. struct map_session_data* sd = script_rid2sd(st);
  9132. if( sd == NULL )
  9133. {// no player attached
  9134. return 0;
  9135. }
  9136. id = script_getnum(st, 2);
  9137. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9138. if( id <= SC_NONE || id >= SC_MAX )
  9139. {// invalid status type given
  9140. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9141. return 0;
  9142. }
  9143. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9144. {// no status is active
  9145. script_pushint(st, 0);
  9146. return 0;
  9147. }
  9148. switch( type )
  9149. {
  9150. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9151. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9152. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9153. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9154. case 5:
  9155. {
  9156. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9157. if( timer )
  9158. {// return the amount of time remaining
  9159. script_pushint(st, timer->tick - gettick());
  9160. }
  9161. }
  9162. break;
  9163. default: script_pushint(st, 1); break;
  9164. }
  9165. return SCRIPT_CMD_SUCCESS;
  9166. }
  9167. /*==========================================
  9168. *
  9169. *------------------------------------------*/
  9170. BUILDIN_FUNC(debugmes)
  9171. {
  9172. const char *str;
  9173. str=script_getstr(st,2);
  9174. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9175. return SCRIPT_CMD_SUCCESS;
  9176. }
  9177. /*==========================================
  9178. *------------------------------------------*/
  9179. BUILDIN_FUNC(catchpet)
  9180. {
  9181. int pet_id;
  9182. TBL_PC *sd;
  9183. pet_id= script_getnum(st,2);
  9184. sd=script_rid2sd(st);
  9185. if( sd == NULL )
  9186. return 0;
  9187. pet_catch_process1(sd,pet_id);
  9188. return SCRIPT_CMD_SUCCESS;
  9189. }
  9190. /*==========================================
  9191. * [orn]
  9192. *------------------------------------------*/
  9193. BUILDIN_FUNC(homunculus_evolution)
  9194. {
  9195. TBL_PC *sd;
  9196. sd=script_rid2sd(st);
  9197. if( sd == NULL )
  9198. return 0;
  9199. if(hom_is_active(sd->hd))
  9200. {
  9201. if (sd->hd->homunculus.intimacy > 91000)
  9202. hom_evolution(sd->hd);
  9203. else
  9204. clif_emotion(&sd->hd->bl, E_SWT);
  9205. }
  9206. return SCRIPT_CMD_SUCCESS;
  9207. }
  9208. /*==========================================
  9209. * Checks for vaporized morph state
  9210. * and deletes ITEMID_STRANGE_EMBRYO.
  9211. *------------------------------------------*/
  9212. BUILDIN_FUNC(homunculus_mutate)
  9213. {
  9214. int homun_id, m_class, m_id, i;
  9215. TBL_PC *sd;
  9216. sd = script_rid2sd(st);
  9217. if( sd == NULL || sd->hd == NULL )
  9218. return 0;
  9219. if(script_hasdata(st,2))
  9220. homun_id = script_getnum(st,2);
  9221. else
  9222. homun_id = 6048 + (rnd() % 4);
  9223. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9224. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9225. m_id = hom_class2mapid(homun_id);
  9226. i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9227. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9228. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9229. hom_call(sd); // Respawn homunculus.
  9230. hom_mutate(sd->hd, homun_id);
  9231. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9232. script_pushint(st, 1);
  9233. return 0;
  9234. } else
  9235. clif_emotion(&sd->bl, E_SWT);
  9236. } else
  9237. clif_emotion(&sd->bl, E_SWT);
  9238. script_pushint(st, 0);
  9239. return SCRIPT_CMD_SUCCESS;
  9240. }
  9241. /*==========================================
  9242. * Puts homunculus into morph state
  9243. * and gives ITEMID_STRANGE_EMBRYO.
  9244. *------------------------------------------*/
  9245. BUILDIN_FUNC(morphembryo)
  9246. {
  9247. struct item item_tmp;
  9248. int m_class, i=0;
  9249. TBL_PC *sd;
  9250. sd = script_rid2sd(st);
  9251. if( sd == NULL || sd->hd == NULL )
  9252. return 0;
  9253. if( hom_is_active(sd->hd) ) {
  9254. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9255. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9256. memset(&item_tmp, 0, sizeof(item_tmp));
  9257. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9258. item_tmp.identify = 1;
  9259. if( item_tmp.nameid == 0 || (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9260. clif_additem(sd, 0, 0, i);
  9261. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9262. } else {
  9263. hom_vaporize(sd, HOM_ST_MORPH);
  9264. script_pushint(st, 1);
  9265. return 0;
  9266. }
  9267. } else
  9268. clif_emotion(&sd->hd->bl, E_SWT);
  9269. } else
  9270. clif_emotion(&sd->bl, E_SWT);
  9271. script_pushint(st, 0);
  9272. return SCRIPT_CMD_SUCCESS;
  9273. }
  9274. // [Zephyrus]
  9275. BUILDIN_FUNC(homunculus_shuffle)
  9276. {
  9277. TBL_PC *sd;
  9278. sd=script_rid2sd(st);
  9279. if( sd == NULL )
  9280. return 0;
  9281. if(hom_is_active(sd->hd))
  9282. hom_shuffle(sd->hd);
  9283. return SCRIPT_CMD_SUCCESS;
  9284. }
  9285. /*==========================================
  9286. * Check for homunculus state.
  9287. * Return: -1 = No homunculus
  9288. * 0 = Homunculus is active
  9289. * 1 = Homunculus is vaporized (rest)
  9290. * 2 = Homunculus is in morph state
  9291. *------------------------------------------*/
  9292. BUILDIN_FUNC(checkhomcall)
  9293. {
  9294. TBL_PC *sd = script_rid2sd(st);
  9295. TBL_HOM *hd;
  9296. if( sd == NULL )
  9297. return 0;
  9298. hd = sd->hd;
  9299. if( !hd )
  9300. script_pushint(st, -1);
  9301. else
  9302. script_pushint(st, hd->homunculus.vaporize);
  9303. return SCRIPT_CMD_SUCCESS;
  9304. }
  9305. //These two functions bring the eA MAPID_* class functionality to scripts.
  9306. BUILDIN_FUNC(eaclass)
  9307. {
  9308. int class_;
  9309. if( script_hasdata(st,2) )
  9310. class_ = script_getnum(st,2);
  9311. else {
  9312. TBL_PC *sd;
  9313. sd=script_rid2sd(st);
  9314. if (!sd) {
  9315. script_pushint(st,-1);
  9316. return 0;
  9317. }
  9318. class_ = sd->status.class_;
  9319. }
  9320. script_pushint(st,pc_jobid2mapid(class_));
  9321. return SCRIPT_CMD_SUCCESS;
  9322. }
  9323. BUILDIN_FUNC(roclass)
  9324. {
  9325. int class_ =script_getnum(st,2);
  9326. int sex;
  9327. if( script_hasdata(st,3) )
  9328. sex = script_getnum(st,3);
  9329. else {
  9330. TBL_PC *sd;
  9331. if (st->rid && (sd=script_rid2sd(st)))
  9332. sex = sd->status.sex;
  9333. else
  9334. sex = 1; //Just use male when not found.
  9335. }
  9336. script_pushint(st,pc_mapid2jobid(class_, sex));
  9337. return SCRIPT_CMD_SUCCESS;
  9338. }
  9339. /*==========================================
  9340. * Tells client to open a hatching window, used for pet incubator
  9341. *------------------------------------------*/
  9342. BUILDIN_FUNC(birthpet)
  9343. {
  9344. TBL_PC *sd;
  9345. sd=script_rid2sd(st);
  9346. if( sd == NULL )
  9347. return 0;
  9348. if( sd->status.pet_id )
  9349. {// do not send egg list, when you already have a pet
  9350. return 0;
  9351. }
  9352. clif_sendegg(sd);
  9353. return SCRIPT_CMD_SUCCESS;
  9354. }
  9355. /*==========================================
  9356. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  9357. * @type
  9358. * 1 : make like after rebirth
  9359. * 2 : blvl,jlvl=1, skillpoint=0
  9360. * 3 : don't reset skill, blvl=1
  9361. * 4 : jlvl=0
  9362. *------------------------------------------*/
  9363. BUILDIN_FUNC(resetlvl)
  9364. {
  9365. TBL_PC *sd;
  9366. int type=script_getnum(st,2);
  9367. sd=script_rid2sd(st);
  9368. if( sd == NULL )
  9369. return 0;
  9370. pc_resetlvl(sd,type);
  9371. return SCRIPT_CMD_SUCCESS;
  9372. }
  9373. /*==========================================
  9374. * Reset a player status point
  9375. *------------------------------------------*/
  9376. BUILDIN_FUNC(resetstatus)
  9377. {
  9378. TBL_PC *sd;
  9379. sd=script_rid2sd(st);
  9380. pc_resetstate(sd);
  9381. return SCRIPT_CMD_SUCCESS;
  9382. }
  9383. /*==========================================
  9384. * script command resetskill
  9385. *------------------------------------------*/
  9386. BUILDIN_FUNC(resetskill)
  9387. {
  9388. TBL_PC *sd;
  9389. sd=script_rid2sd(st);
  9390. pc_resetskill(sd,1);
  9391. return SCRIPT_CMD_SUCCESS;
  9392. }
  9393. /*==========================================
  9394. * Counts total amount of skill points.
  9395. *------------------------------------------*/
  9396. BUILDIN_FUNC(skillpointcount)
  9397. {
  9398. TBL_PC *sd;
  9399. sd=script_rid2sd(st);
  9400. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9401. return SCRIPT_CMD_SUCCESS;
  9402. }
  9403. /*==========================================
  9404. *
  9405. *------------------------------------------*/
  9406. BUILDIN_FUNC(changebase)
  9407. {
  9408. TBL_PC *sd=NULL;
  9409. int vclass;
  9410. if( script_hasdata(st,3) )
  9411. sd=map_id2sd(script_getnum(st,3));
  9412. else
  9413. sd=script_rid2sd(st);
  9414. if(sd == NULL)
  9415. return 0;
  9416. vclass = script_getnum(st,2);
  9417. if(vclass == JOB_WEDDING)
  9418. {
  9419. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9420. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9421. )
  9422. return 0;
  9423. }
  9424. if(!sd->disguise && vclass != sd->vd.class_) {
  9425. status_set_viewdata(&sd->bl, vclass);
  9426. //Updated client view. Base, Weapon and Cloth Colors.
  9427. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9428. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9429. if (sd->vd.cloth_color)
  9430. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9431. clif_skillinfoblock(sd);
  9432. }
  9433. return SCRIPT_CMD_SUCCESS;
  9434. }
  9435. /*==========================================
  9436. * Unequip all item and request for a changesex to char-serv
  9437. *------------------------------------------*/
  9438. BUILDIN_FUNC(changesex)
  9439. {
  9440. int i;
  9441. TBL_PC *sd = NULL;
  9442. sd = script_rid2sd(st);
  9443. pc_resetskill(sd,4);
  9444. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9445. for( i=0; i<EQI_MAX; i++ )
  9446. if( sd->equip_index[i] >= 0 ) pc_unequipitem(sd, sd->equip_index[i], 3);
  9447. chrif_changesex(sd);
  9448. return SCRIPT_CMD_SUCCESS;
  9449. }
  9450. /*==========================================
  9451. * Works like 'announce' but outputs in the common chat window
  9452. *------------------------------------------*/
  9453. BUILDIN_FUNC(globalmes)
  9454. {
  9455. struct block_list *bl = map_id2bl(st->oid);
  9456. struct npc_data *nd = (struct npc_data *)bl;
  9457. const char *name=NULL,*mes;
  9458. mes=script_getstr(st,2);
  9459. if(mes==NULL) return 0;
  9460. if(script_hasdata(st,3)){ // npc name to display
  9461. name=script_getstr(st,3);
  9462. } else {
  9463. name=nd->name; //use current npc name
  9464. }
  9465. npc_globalmessage(name,mes); // broadcast to all players connected
  9466. return SCRIPT_CMD_SUCCESS;
  9467. }
  9468. /////////////////////////////////////////////////////////////////////
  9469. // NPC waiting room (chat room)
  9470. //
  9471. /// Creates a waiting room (chat room) for this npc.
  9472. ///
  9473. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9474. BUILDIN_FUNC(waitingroom)
  9475. {
  9476. struct npc_data* nd;
  9477. int pub = 1;
  9478. const char* title = script_getstr(st, 2);
  9479. int limit = script_getnum(st, 3);
  9480. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9481. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9482. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9483. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9484. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9485. nd = (struct npc_data *)map_id2bl(st->oid);
  9486. if( nd != NULL )
  9487. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  9488. return SCRIPT_CMD_SUCCESS;
  9489. }
  9490. /// Removes the waiting room of the current or target npc.
  9491. ///
  9492. /// delwaitingroom "<npc_name>";
  9493. /// delwaitingroom;
  9494. BUILDIN_FUNC(delwaitingroom)
  9495. {
  9496. struct npc_data* nd;
  9497. if( script_hasdata(st,2) )
  9498. nd = npc_name2id(script_getstr(st, 2));
  9499. else
  9500. nd = (struct npc_data *)map_id2bl(st->oid);
  9501. if( nd != NULL )
  9502. chat_deletenpcchat(nd);
  9503. return SCRIPT_CMD_SUCCESS;
  9504. }
  9505. /// Kicks all the players from the waiting room of the current or target npc.
  9506. ///
  9507. /// kickwaitingroomall "<npc_name>";
  9508. /// kickwaitingroomall;
  9509. BUILDIN_FUNC(waitingroomkickall)
  9510. {
  9511. struct npc_data* nd;
  9512. struct chat_data* cd;
  9513. if( script_hasdata(st,2) )
  9514. nd = npc_name2id(script_getstr(st,2));
  9515. else
  9516. nd = (struct npc_data *)map_id2bl(st->oid);
  9517. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9518. chat_npckickall(cd);
  9519. return SCRIPT_CMD_SUCCESS;
  9520. }
  9521. /// Enables the waiting room event of the current or target npc.
  9522. ///
  9523. /// enablewaitingroomevent "<npc_name>";
  9524. /// enablewaitingroomevent;
  9525. BUILDIN_FUNC(enablewaitingroomevent)
  9526. {
  9527. struct npc_data* nd;
  9528. struct chat_data* cd;
  9529. if( script_hasdata(st,2) )
  9530. nd = npc_name2id(script_getstr(st, 2));
  9531. else
  9532. nd = (struct npc_data *)map_id2bl(st->oid);
  9533. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9534. chat_enableevent(cd);
  9535. return SCRIPT_CMD_SUCCESS;
  9536. }
  9537. /// Disables the waiting room event of the current or target npc.
  9538. ///
  9539. /// disablewaitingroomevent "<npc_name>";
  9540. /// disablewaitingroomevent;
  9541. BUILDIN_FUNC(disablewaitingroomevent)
  9542. {
  9543. struct npc_data *nd;
  9544. struct chat_data *cd;
  9545. if( script_hasdata(st,2) )
  9546. nd = npc_name2id(script_getstr(st, 2));
  9547. else
  9548. nd = (struct npc_data *)map_id2bl(st->oid);
  9549. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9550. chat_disableevent(cd);
  9551. return SCRIPT_CMD_SUCCESS;
  9552. }
  9553. /// Returns info on the waiting room of the current or target npc.
  9554. /// Returns -1 if the type unknown
  9555. /// <type>=0 : current number of users
  9556. /// <type>=1 : maximum number of users allowed
  9557. /// <type>=2 : the number of users that trigger the event
  9558. /// <type>=3 : if the trigger is disabled
  9559. /// <type>=4 : the title of the waiting room
  9560. /// <type>=5 : the password of the waiting room
  9561. /// <type>=16 : the name of the waiting room event
  9562. /// <type>=32 : if the waiting room is full
  9563. /// <type>=33 : if there are enough users to trigger the event
  9564. ///
  9565. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9566. /// getwaitingroomstate(<type>) -> <info>
  9567. BUILDIN_FUNC(getwaitingroomstate)
  9568. {
  9569. struct npc_data *nd;
  9570. struct chat_data *cd;
  9571. int type;
  9572. type = script_getnum(st,2);
  9573. if( script_hasdata(st,3) )
  9574. nd = npc_name2id(script_getstr(st, 3));
  9575. else
  9576. nd = (struct npc_data *)map_id2bl(st->oid);
  9577. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9578. {
  9579. script_pushint(st, -1);
  9580. return 0;
  9581. }
  9582. switch(type)
  9583. {
  9584. case 0: script_pushint(st, cd->users); break;
  9585. case 1: script_pushint(st, cd->limit); break;
  9586. case 2: script_pushint(st, cd->trigger&0x7f); break;
  9587. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  9588. case 4: script_pushstrcopy(st, cd->title); break;
  9589. case 5: script_pushstrcopy(st, cd->pass); break;
  9590. case 16: script_pushstrcopy(st, cd->npc_event);break;
  9591. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  9592. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  9593. default: script_pushint(st, -1); break;
  9594. }
  9595. return SCRIPT_CMD_SUCCESS;
  9596. }
  9597. /// Warps the trigger or target amount of players to the target map and position.
  9598. /// Players are automatically removed from the waiting room.
  9599. /// Those waiting the longest will get warped first.
  9600. /// The target map can be "Random" for a random position in the current map,
  9601. /// and "SavePoint" for the savepoint map+position.
  9602. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  9603. ///
  9604. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  9605. /// The total number of teleported players is put into $@warpwaitingpcnum
  9606. ///
  9607. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  9608. /// warpwaitingpc "<map name>",<x>,<y>;
  9609. BUILDIN_FUNC(warpwaitingpc)
  9610. {
  9611. int x;
  9612. int y;
  9613. int i;
  9614. int n;
  9615. const char* map_name;
  9616. struct npc_data* nd;
  9617. struct chat_data* cd;
  9618. TBL_PC* sd;
  9619. nd = (struct npc_data *)map_id2bl(st->oid);
  9620. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9621. return 0;
  9622. map_name = script_getstr(st,2);
  9623. x = script_getnum(st,3);
  9624. y = script_getnum(st,4);
  9625. n = cd->trigger&0x7f;
  9626. if( script_hasdata(st,5) )
  9627. n = script_getnum(st,5);
  9628. for( i = 0; i < n && cd->users > 0; i++ )
  9629. {
  9630. sd = cd->usersd[0];
  9631. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  9632. {// can't teleport on this map
  9633. break;
  9634. }
  9635. if( cd->zeny )
  9636. {// fee set
  9637. if( (uint32)sd->status.zeny < cd->zeny )
  9638. {// no zeny to cover set fee
  9639. break;
  9640. }
  9641. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  9642. }
  9643. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  9644. if( strcmp(map_name,"Random") == 0 )
  9645. pc_randomwarp(sd,CLR_TELEPORT);
  9646. else if( strcmp(map_name,"SavePoint") == 0 )
  9647. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  9648. else
  9649. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  9650. }
  9651. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  9652. return SCRIPT_CMD_SUCCESS;
  9653. }
  9654. /////////////////////////////////////////////////////////////////////
  9655. // ...
  9656. //
  9657. /// Detaches a character from a script.
  9658. ///
  9659. /// @param st Script state to detach the character from.
  9660. static void script_detach_rid(struct script_state* st)
  9661. {
  9662. if(st->rid)
  9663. {
  9664. script_detach_state(st, false);
  9665. st->rid = 0;
  9666. }
  9667. }
  9668. /*=========================================================================
  9669. * Attaches a set of RIDs to the current script. [digitalhamster]
  9670. * addrid(<type>{,<flag>{,<parameters>}});
  9671. * <type>:
  9672. * 0 : All players in the server.
  9673. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  9674. * 2 : Party members of a specified party ID.
  9675. * [ Parameters: <party id> ]
  9676. * 3 : Guild members of a specified guild ID.
  9677. * [ Parameters: <guild id> ]
  9678. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  9679. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  9680. * Account ID: The specified account ID.
  9681. * <flag>:
  9682. * 0 : Players are always attached. (default)
  9683. * 1 : Players currently running another script will not be attached.
  9684. *-------------------------------------------------------------------------*/
  9685. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  9686. {
  9687. int forceflag;
  9688. struct map_session_data *sd = (TBL_PC *)bl;
  9689. struct script_state* st;
  9690. st = va_arg(ap,struct script_state*);
  9691. forceflag = va_arg(ap,int);
  9692. if(!forceflag || !sd->st)
  9693. if(sd->status.account_id != st->rid)
  9694. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9695. return 0;
  9696. }
  9697. BUILDIN_FUNC(addrid)
  9698. {
  9699. struct s_mapiterator* iter;
  9700. struct block_list *bl;
  9701. TBL_PC *sd;
  9702. if(st->rid < 1) {
  9703. st->state = END;
  9704. bl = map_id2bl(st->oid);
  9705. } else
  9706. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  9707. iter = mapit_getallusers();
  9708. switch(script_getnum(st,2)) {
  9709. case 0:
  9710. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9711. if(!script_getnum(st,3) || !sd->st)
  9712. if(sd->status.account_id != st->rid) //attached player already runs.
  9713. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9714. }
  9715. break;
  9716. case 1:
  9717. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9718. if(!script_getnum(st,3) || !sd->st)
  9719. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  9720. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9721. }
  9722. break;
  9723. case 2:
  9724. if(script_getnum(st,4) == 0) {
  9725. script_pushint(st,0);
  9726. return 0;
  9727. }
  9728. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9729. if(!script_getnum(st,3) || !sd->st)
  9730. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  9731. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9732. }
  9733. break;
  9734. case 3:
  9735. if(script_getnum(st,4) == 0) {
  9736. script_pushint(st,0);
  9737. return 0;
  9738. }
  9739. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9740. if(!script_getnum(st,3) || !sd->st)
  9741. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  9742. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9743. }
  9744. break;
  9745. case 4:
  9746. map_foreachinarea(buildin_addrid_sub,
  9747. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  9748. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  9749. break;
  9750. default:
  9751. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  9752. script_pushint(st,0);
  9753. return 0;
  9754. }
  9755. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  9756. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  9757. script_pushint(st,1);
  9758. }
  9759. return 0;
  9760. }
  9761. mapit_free(iter);
  9762. script_pushint(st,1);
  9763. return SCRIPT_CMD_SUCCESS;
  9764. }
  9765. /*==========================================
  9766. * Attach sd char id to script and detach current one if any
  9767. *------------------------------------------*/
  9768. BUILDIN_FUNC(attachrid)
  9769. {
  9770. int rid = script_getnum(st,2);
  9771. if (map_id2sd(rid) != NULL) {
  9772. script_detach_rid(st);
  9773. st->rid = rid;
  9774. script_attach_state(st);
  9775. script_pushint(st,1);
  9776. } else
  9777. script_pushint(st,0);
  9778. return SCRIPT_CMD_SUCCESS;
  9779. }
  9780. /*==========================================
  9781. * Detach script to rid
  9782. *------------------------------------------*/
  9783. BUILDIN_FUNC(detachrid)
  9784. {
  9785. script_detach_rid(st);
  9786. return SCRIPT_CMD_SUCCESS;
  9787. }
  9788. /*==========================================
  9789. * Chk if account connected, (and charid from account if specified)
  9790. *------------------------------------------*/
  9791. BUILDIN_FUNC(isloggedin)
  9792. {
  9793. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  9794. if (script_hasdata(st,3) && sd &&
  9795. sd->status.char_id != script_getnum(st,3))
  9796. sd = NULL;
  9797. push_val(st->stack,C_INT,sd!=NULL);
  9798. return SCRIPT_CMD_SUCCESS;
  9799. }
  9800. /*==========================================
  9801. *
  9802. *------------------------------------------*/
  9803. BUILDIN_FUNC(setmapflagnosave)
  9804. {
  9805. int16 m,x,y;
  9806. unsigned short mapindex;
  9807. const char *str,*str2;
  9808. str=script_getstr(st,2);
  9809. str2=script_getstr(st,3);
  9810. x=script_getnum(st,4);
  9811. y=script_getnum(st,5);
  9812. m = map_mapname2mapid(str);
  9813. mapindex = mapindex_name2id(str2);
  9814. if(m >= 0 && mapindex) {
  9815. map[m].flag.nosave=1;
  9816. map[m].save.map=mapindex;
  9817. map[m].save.x=x;
  9818. map[m].save.y=y;
  9819. }
  9820. return SCRIPT_CMD_SUCCESS;
  9821. }
  9822. BUILDIN_FUNC(getmapflag)
  9823. {
  9824. int16 m,i;
  9825. const char *str;
  9826. str=script_getstr(st,2);
  9827. i=script_getnum(st,3);
  9828. m = map_mapname2mapid(str);
  9829. if(m >= 0) {
  9830. switch(i) {
  9831. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  9832. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  9833. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  9834. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  9835. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  9836. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  9837. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  9838. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  9839. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  9840. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  9841. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  9842. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  9843. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  9844. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  9845. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  9846. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  9847. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  9848. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  9849. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  9850. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  9851. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  9852. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  9853. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  9854. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  9855. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  9856. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  9857. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  9858. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  9859. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  9860. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  9861. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  9862. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  9863. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  9864. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  9865. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  9866. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  9867. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  9868. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  9869. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  9870. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  9871. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  9872. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  9873. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  9874. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  9875. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  9876. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  9877. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  9878. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  9879. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  9880. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  9881. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  9882. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  9883. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  9884. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  9885. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  9886. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  9887. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  9888. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  9889. #ifdef ADJUST_SKILL_DAMAGE
  9890. case MF_SKILL_DAMAGE:
  9891. {
  9892. int ret_val=0, type=0;
  9893. FETCH(4,type);
  9894. switch (type) {
  9895. case 1: ret_val = map[m].adjust.damage.pc; break;
  9896. case 2: ret_val = map[m].adjust.damage.mob; break;
  9897. case 3: ret_val = map[m].adjust.damage.boss; break;
  9898. case 4: ret_val = map[m].adjust.damage.other; break;
  9899. case 5: ret_val = map[m].adjust.damage.caster; break;
  9900. default: ret_val = map[m].flag.skill_damage; break;
  9901. }
  9902. script_pushint(st,ret_val); break;
  9903. } break;
  9904. #endif
  9905. }
  9906. }
  9907. return SCRIPT_CMD_SUCCESS;
  9908. }
  9909. /* pvp timer handling */
  9910. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  9911. TBL_PC* sd = (TBL_PC*)bl;
  9912. if (sd->pvp_timer == INVALID_TIMER) {
  9913. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  9914. sd->pvp_rank = 0;
  9915. sd->pvp_lastusers = 0;
  9916. sd->pvp_point = 5;
  9917. sd->pvp_won = 0;
  9918. sd->pvp_lost = 0;
  9919. }
  9920. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  9921. clif_maptypeproperty2(&sd->bl,SELF);
  9922. return 0;
  9923. }
  9924. BUILDIN_FUNC(setmapflag)
  9925. {
  9926. int16 m,i;
  9927. const char *str;
  9928. int val=0;
  9929. str=script_getstr(st,2);
  9930. i=script_getnum(st,3);
  9931. FETCH(4,val);
  9932. m = map_mapname2mapid(str);
  9933. if(m >= 0) {
  9934. switch(i) {
  9935. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  9936. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  9937. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  9938. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  9939. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  9940. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  9941. case MF_PVP:
  9942. map[m].flag.pvp = 1;
  9943. if( !battle_config.pk_mode ) {
  9944. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  9945. }
  9946. break;
  9947. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  9948. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  9949. case MF_GVG: {
  9950. struct block_list bl;
  9951. map[m].flag.gvg = 1;
  9952. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9953. bl.type = BL_NUL;
  9954. bl.m = m;
  9955. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9956. }
  9957. break;
  9958. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  9959. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  9960. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  9961. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  9962. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  9963. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  9964. case MF_SNOW: map[m].flag.snow = 1; break;
  9965. case MF_FOG: map[m].flag.fog = 1; break;
  9966. case MF_SAKURA: map[m].flag.sakura = 1; break;
  9967. case MF_LEAVES: map[m].flag.leaves = 1; break;
  9968. case MF_NOGO: map[m].flag.nogo = 1; break;
  9969. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  9970. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  9971. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  9972. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  9973. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  9974. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  9975. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  9976. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  9977. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  9978. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  9979. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  9980. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  9981. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  9982. case MF_RESTRICTED:
  9983. map[m].zone |= 1<<(val+1);
  9984. map[m].flag.restricted=1;
  9985. break;
  9986. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  9987. case MF_NODROP: map[m].flag.nodrop = 1; break;
  9988. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  9989. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  9990. case MF_NOVENDING: map[m].flag.novending = 1; break;
  9991. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  9992. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  9993. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  9994. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  9995. case MF_TOWN: map[m].flag.town = 1; break;
  9996. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  9997. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  9998. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  9999. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10000. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10001. case MF_RESET: map[m].flag.reset = 1; break;
  10002. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10003. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10004. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10005. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10006. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10007. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10008. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10009. #ifdef ADJUST_SKILL_DAMAGE
  10010. case MF_SKILL_DAMAGE:
  10011. {
  10012. int type=0;
  10013. FETCH(5,type);
  10014. switch (type) {
  10015. case 1: map[m].adjust.damage.pc = val; break;
  10016. case 2: map[m].adjust.damage.mob = val; break;
  10017. case 3: map[m].adjust.damage.boss = val; break;
  10018. case 4: map[m].adjust.damage.other = val; break;
  10019. case 5: map[m].adjust.damage.caster = val; break;
  10020. }
  10021. map[m].flag.skill_damage = 1;
  10022. } break;
  10023. #endif
  10024. }
  10025. }
  10026. return SCRIPT_CMD_SUCCESS;
  10027. }
  10028. BUILDIN_FUNC(removemapflag)
  10029. {
  10030. int16 m,i;
  10031. const char *str;
  10032. int val=0;
  10033. str=script_getstr(st,2);
  10034. i=script_getnum(st,3);
  10035. FETCH(4,val);
  10036. m = map_mapname2mapid(str);
  10037. if(m >= 0) {
  10038. switch(i) {
  10039. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10040. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10041. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10042. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10043. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10044. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10045. case MF_PVP: {
  10046. struct block_list bl;
  10047. bl.type = BL_NUL;
  10048. bl.m = m;
  10049. map[m].flag.pvp = 0;
  10050. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10051. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10052. }
  10053. break;
  10054. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10055. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10056. case MF_GVG: {
  10057. struct block_list bl;
  10058. bl.type = BL_NUL;
  10059. bl.m = m;
  10060. map[m].flag.gvg = 0;
  10061. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10062. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10063. }
  10064. break;
  10065. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10066. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10067. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10068. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10069. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10070. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10071. case MF_SNOW: map[m].flag.snow = 0; break;
  10072. case MF_FOG: map[m].flag.fog = 0; break;
  10073. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10074. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10075. case MF_NOGO: map[m].flag.nogo = 0; break;
  10076. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10077. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10078. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10079. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10080. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10081. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10082. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10083. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10084. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10085. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10086. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10087. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10088. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10089. case MF_RESTRICTED:
  10090. map[m].zone ^= 1<<(val+1);
  10091. if (map[m].zone == 0){
  10092. map[m].flag.restricted=0;
  10093. }
  10094. break;
  10095. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10096. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10097. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10098. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10099. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10100. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10101. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10102. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10103. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10104. case MF_TOWN: map[m].flag.town = 0; break;
  10105. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10106. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10107. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10108. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10109. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10110. case MF_RESET: map[m].flag.reset = 0; break;
  10111. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10112. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10113. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10114. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10115. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10116. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10117. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10118. #ifdef ADJUST_SKILL_DAMAGE
  10119. case MF_SKILL_DAMAGE:
  10120. {
  10121. map[m].flag.skill_damage = 0;
  10122. memset(map[m].skill_damage, 0, sizeof(map[m].skill_damage));
  10123. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10124. } break;
  10125. #endif
  10126. }
  10127. }
  10128. return SCRIPT_CMD_SUCCESS;
  10129. }
  10130. BUILDIN_FUNC(pvpon)
  10131. {
  10132. int16 m;
  10133. const char *str;
  10134. TBL_PC* sd = NULL;
  10135. struct s_mapiterator* iter;
  10136. struct block_list bl;
  10137. str = script_getstr(st,2);
  10138. m = map_mapname2mapid(str);
  10139. if( m < 0 || map[m].flag.pvp )
  10140. return 0; // nothing to do
  10141. map[m].flag.pvp = 1;
  10142. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10143. bl.type = BL_NUL;
  10144. bl.m = m;
  10145. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10146. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10147. return 0;
  10148. iter = mapit_getallusers();
  10149. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10150. {
  10151. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10152. continue; // not applicable
  10153. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10154. sd->pvp_rank = 0;
  10155. sd->pvp_lastusers = 0;
  10156. sd->pvp_point = 5;
  10157. sd->pvp_won = 0;
  10158. sd->pvp_lost = 0;
  10159. }
  10160. mapit_free(iter);
  10161. return SCRIPT_CMD_SUCCESS;
  10162. }
  10163. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10164. {
  10165. TBL_PC* sd = (TBL_PC*)bl;
  10166. clif_pvpset(sd, 0, 0, 2);
  10167. if (sd->pvp_timer != INVALID_TIMER) {
  10168. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10169. sd->pvp_timer = INVALID_TIMER;
  10170. }
  10171. return 0;
  10172. }
  10173. BUILDIN_FUNC(pvpoff)
  10174. {
  10175. int16 m;
  10176. const char *str;
  10177. struct block_list bl;
  10178. str=script_getstr(st,2);
  10179. m = map_mapname2mapid(str);
  10180. if(m < 0 || !map[m].flag.pvp)
  10181. return 0; //fixed Lupus
  10182. map[m].flag.pvp = 0;
  10183. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10184. bl.type = BL_NUL;
  10185. bl.m = m;
  10186. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10187. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10188. return 0;
  10189. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10190. return SCRIPT_CMD_SUCCESS;
  10191. }
  10192. BUILDIN_FUNC(gvgon)
  10193. {
  10194. int16 m;
  10195. const char *str;
  10196. struct block_list bl;
  10197. str=script_getstr(st,2);
  10198. m = map_mapname2mapid(str);
  10199. if(m >= 0 && !map[m].flag.gvg) {
  10200. map[m].flag.gvg = 1;
  10201. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10202. bl.type = BL_NUL;
  10203. bl.m = m;
  10204. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10205. }
  10206. return SCRIPT_CMD_SUCCESS;
  10207. }
  10208. BUILDIN_FUNC(gvgoff)
  10209. {
  10210. int16 m;
  10211. const char *str;
  10212. str=script_getstr(st,2);
  10213. m = map_mapname2mapid(str);
  10214. if(m >= 0 && map[m].flag.gvg) {
  10215. struct block_list bl;
  10216. map[m].flag.gvg = 0;
  10217. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10218. bl.type = BL_NUL;
  10219. bl.m = m;
  10220. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10221. }
  10222. return SCRIPT_CMD_SUCCESS;
  10223. }
  10224. /*==========================================
  10225. * Shows an emoticon on top of the player/npc
  10226. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10227. *------------------------------------------*/
  10228. //Optional second parameter added by [Skotlex]
  10229. BUILDIN_FUNC(emotion)
  10230. {
  10231. int type;
  10232. int player=0;
  10233. type=script_getnum(st,2);
  10234. if(type < 0 || type > 100)
  10235. return 0;
  10236. if( script_hasdata(st,3) )
  10237. player=script_getnum(st,3);
  10238. if (player) {
  10239. TBL_PC *sd = NULL;
  10240. if( script_hasdata(st,4) )
  10241. sd = map_nick2sd(script_getstr(st,4));
  10242. else
  10243. sd = script_rid2sd(st);
  10244. if (sd)
  10245. clif_emotion(&sd->bl,type);
  10246. } else
  10247. if( script_hasdata(st,4) )
  10248. {
  10249. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10250. if(nd)
  10251. clif_emotion(&nd->bl,type);
  10252. }
  10253. else
  10254. clif_emotion(map_id2bl(st->oid),type);
  10255. return SCRIPT_CMD_SUCCESS;
  10256. }
  10257. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10258. {
  10259. int16 m=va_arg(ap,int);
  10260. int g_id=va_arg(ap,int);
  10261. int flag=va_arg(ap,int);
  10262. if(!sd || sd->bl.m != m)
  10263. return 0;
  10264. if(
  10265. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10266. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10267. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10268. )
  10269. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10270. return 1;
  10271. }
  10272. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10273. {
  10274. struct mob_data *md=(struct mob_data *)bl;
  10275. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM)
  10276. status_kill(bl);
  10277. return 0;
  10278. }
  10279. /*
  10280. * Function to kick guild members out of a map and to their save points.
  10281. * m : mapid
  10282. * g_id : owner guild id
  10283. * flag & 1 : Warp out owners
  10284. * flag & 2 : Warp out outsiders
  10285. * flag & 4 : reserved for mobs
  10286. * */
  10287. BUILDIN_FUNC(maprespawnguildid)
  10288. {
  10289. const char *mapname=script_getstr(st,2);
  10290. int g_id=script_getnum(st,3);
  10291. int flag=script_getnum(st,4);
  10292. int16 m=map_mapname2mapid(mapname);
  10293. if(m == -1)
  10294. return 0;
  10295. //Catch ALL players (in case some are 'between maps' on execution time)
  10296. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10297. if (flag&4) //Remove script mobs.
  10298. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10299. return SCRIPT_CMD_SUCCESS;
  10300. }
  10301. BUILDIN_FUNC(agitstart)
  10302. {
  10303. if(agit_flag==1) return SCRIPT_CMD_SUCCESS; // Agit already Start.
  10304. agit_flag=1;
  10305. guild_agit_start();
  10306. return SCRIPT_CMD_SUCCESS;
  10307. }
  10308. BUILDIN_FUNC(agitend)
  10309. {
  10310. if(agit_flag==0) return SCRIPT_CMD_SUCCESS; // Agit already End.
  10311. agit_flag=0;
  10312. guild_agit_end();
  10313. return SCRIPT_CMD_SUCCESS;
  10314. }
  10315. BUILDIN_FUNC(agitstart2)
  10316. {
  10317. if(agit2_flag==1) return SCRIPT_CMD_SUCCESS; // Agit2 already Start.
  10318. agit2_flag=1;
  10319. guild_agit2_start();
  10320. return SCRIPT_CMD_SUCCESS;
  10321. }
  10322. BUILDIN_FUNC(agitend2)
  10323. {
  10324. if(agit2_flag==0) return SCRIPT_CMD_SUCCESS; // Agit2 already End.
  10325. agit2_flag=0;
  10326. guild_agit2_end();
  10327. return SCRIPT_CMD_SUCCESS;
  10328. }
  10329. /*==========================================
  10330. * Returns whether woe is on or off. // choice script
  10331. *------------------------------------------*/
  10332. BUILDIN_FUNC(agitcheck)
  10333. {
  10334. script_pushint(st,agit_flag);
  10335. return SCRIPT_CMD_SUCCESS;
  10336. }
  10337. /*==========================================
  10338. * Returns whether woese is on or off. // choice script
  10339. *------------------------------------------*/
  10340. BUILDIN_FUNC(agitcheck2)
  10341. {
  10342. script_pushint(st,agit2_flag);
  10343. return SCRIPT_CMD_SUCCESS;
  10344. }
  10345. /// Sets the guild_id of this npc.
  10346. ///
  10347. /// flagemblem <guild_id>;
  10348. BUILDIN_FUNC(flagemblem)
  10349. {
  10350. TBL_NPC* nd;
  10351. int g_id = script_getnum(st,2);
  10352. if(g_id < 0) return 0;
  10353. nd = (TBL_NPC*)map_id2nd(st->oid);
  10354. if( nd == NULL ) {
  10355. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10356. } else if( nd->subtype != SCRIPT ) {
  10357. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10358. } else {
  10359. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10360. nd->u.scr.guild_id = g_id;
  10361. clif_guild_emblem_area(&nd->bl);
  10362. /* guild flag caching */
  10363. if( g_id ) /* adding a id */
  10364. guild_flag_add(nd);
  10365. else if( changed ) /* removing a flag */
  10366. guild_flag_remove(nd);
  10367. }
  10368. return SCRIPT_CMD_SUCCESS;
  10369. }
  10370. BUILDIN_FUNC(getcastlename)
  10371. {
  10372. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10373. struct guild_castle* gc = guild_mapname2gc(mapname);
  10374. const char* name = (gc) ? gc->castle_name : "";
  10375. script_pushstrcopy(st,name);
  10376. return SCRIPT_CMD_SUCCESS;
  10377. }
  10378. BUILDIN_FUNC(getcastledata)
  10379. {
  10380. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10381. int index = script_getnum(st,3);
  10382. struct guild_castle *gc = guild_mapname2gc(mapname);
  10383. if (gc == NULL) {
  10384. script_pushint(st,0);
  10385. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10386. return 1;
  10387. }
  10388. switch (index) {
  10389. case 1:
  10390. script_pushint(st,gc->guild_id); break;
  10391. case 2:
  10392. script_pushint(st,gc->economy); break;
  10393. case 3:
  10394. script_pushint(st,gc->defense); break;
  10395. case 4:
  10396. script_pushint(st,gc->triggerE); break;
  10397. case 5:
  10398. script_pushint(st,gc->triggerD); break;
  10399. case 6:
  10400. script_pushint(st,gc->nextTime); break;
  10401. case 7:
  10402. script_pushint(st,gc->payTime); break;
  10403. case 8:
  10404. script_pushint(st,gc->createTime); break;
  10405. case 9:
  10406. script_pushint(st,gc->visibleC); break;
  10407. default:
  10408. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10409. script_pushint(st,gc->guardian[index-10].visible);
  10410. break;
  10411. }
  10412. script_pushint(st,0);
  10413. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10414. return 1;
  10415. }
  10416. return SCRIPT_CMD_SUCCESS;
  10417. }
  10418. BUILDIN_FUNC(setcastledata)
  10419. {
  10420. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10421. int index = script_getnum(st,3);
  10422. int value = script_getnum(st,4);
  10423. struct guild_castle *gc = guild_mapname2gc(mapname);
  10424. if (gc == NULL) {
  10425. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10426. return 1;
  10427. }
  10428. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10429. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10430. return 1;
  10431. }
  10432. guild_castledatasave(gc->castle_id, index, value);
  10433. return SCRIPT_CMD_SUCCESS;
  10434. }
  10435. /* =====================================================================
  10436. * ---------------------------------------------------------------------*/
  10437. BUILDIN_FUNC(requestguildinfo)
  10438. {
  10439. int guild_id=script_getnum(st,2);
  10440. const char *event=NULL;
  10441. if( script_hasdata(st,3) ){
  10442. event=script_getstr(st,3);
  10443. check_event(st, event);
  10444. }
  10445. if(guild_id>0)
  10446. guild_npc_request_info(guild_id,event);
  10447. return SCRIPT_CMD_SUCCESS;
  10448. }
  10449. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10450. /// getequipcardcnt(<equipment slot>);
  10451. BUILDIN_FUNC(getequipcardcnt)
  10452. {
  10453. int i=-1,j,num;
  10454. TBL_PC *sd;
  10455. int count;
  10456. num=script_getnum(st,2);
  10457. sd=script_rid2sd(st);
  10458. if (num > 0 && num <= ARRAYLENGTH(equip))
  10459. i=pc_checkequip(sd,equip[num-1]);
  10460. if (i < 0 || !sd->inventory_data[i]) {
  10461. script_pushint(st,0);
  10462. return 0;
  10463. }
  10464. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10465. {
  10466. script_pushint(st,0);
  10467. return 0;
  10468. }
  10469. count = 0;
  10470. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10471. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  10472. count++;
  10473. script_pushint(st,count);
  10474. return SCRIPT_CMD_SUCCESS;
  10475. }
  10476. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10477. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10478. /// successremovecards <slot>;
  10479. BUILDIN_FUNC(successremovecards) {
  10480. int i=-1,j,c,cardflag=0;
  10481. TBL_PC* sd = script_rid2sd(st);
  10482. int num = script_getnum(st,2);
  10483. if (num > 0 && num <= ARRAYLENGTH(equip))
  10484. i=pc_checkequip(sd,equip[num-1]);
  10485. if (i < 0 || !sd->inventory_data[i]) {
  10486. return 0;
  10487. }
  10488. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10489. return 0;
  10490. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10491. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10492. int flag;
  10493. struct item item_tmp;
  10494. memset(&item_tmp,0,sizeof(item_tmp));
  10495. cardflag = 1;
  10496. item_tmp.nameid = sd->status.inventory[i].card[c];
  10497. item_tmp.identify = 1;
  10498. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10499. clif_additem(sd,0,0,flag);
  10500. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10501. }
  10502. }
  10503. }
  10504. if(cardflag == 1) {//if card was remove remplace item with no card
  10505. int flag;
  10506. struct item item_tmp;
  10507. memset(&item_tmp,0,sizeof(item_tmp));
  10508. item_tmp.nameid = sd->status.inventory[i].nameid;
  10509. item_tmp.identify = 1;
  10510. item_tmp.refine = sd->status.inventory[i].refine;
  10511. item_tmp.attribute = sd->status.inventory[i].attribute;
  10512. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10513. item_tmp.bound = sd->status.inventory[i].bound;
  10514. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10515. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10516. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10517. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10518. clif_additem(sd,0,0,flag);
  10519. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10520. }
  10521. clif_misceffect(&sd->bl,3);
  10522. }
  10523. return SCRIPT_CMD_SUCCESS;
  10524. }
  10525. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10526. /// failedremovecards <slot>, <type>;
  10527. /// <type>=0 : will destroy both the item and the cards.
  10528. /// <type>=1 : will keep the item, but destroy the cards.
  10529. /// <type>=2 : will keep the cards, but destroy the item.
  10530. /// <type>=? : will just display the failure effect.
  10531. BUILDIN_FUNC(failedremovecards) {
  10532. int i=-1,j,c,cardflag=0;
  10533. TBL_PC* sd = script_rid2sd(st);
  10534. int num = script_getnum(st,2);
  10535. int typefail = script_getnum(st,3);
  10536. if (num > 0 && num <= ARRAYLENGTH(equip))
  10537. i=pc_checkequip(sd,equip[num-1]);
  10538. if (i < 0 || !sd->inventory_data[i])
  10539. return 0;
  10540. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10541. return 0;
  10542. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10543. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  10544. cardflag = 1;
  10545. if(typefail == 2) {// add cards to inventory, clear
  10546. int flag;
  10547. struct item item_tmp;
  10548. memset(&item_tmp,0,sizeof(item_tmp));
  10549. item_tmp.nameid = sd->status.inventory[i].card[c];
  10550. item_tmp.identify = 1;
  10551. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10552. clif_additem(sd,0,0,flag);
  10553. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10554. }
  10555. }
  10556. }
  10557. }
  10558. if(cardflag == 1) {
  10559. if(typefail == 0 || typefail == 2){ // destroy the item
  10560. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10561. }
  10562. if(typefail == 1){ // destroy the card
  10563. int flag;
  10564. struct item item_tmp;
  10565. memset(&item_tmp,0,sizeof(item_tmp));
  10566. item_tmp.nameid = sd->status.inventory[i].nameid;
  10567. item_tmp.identify = 1;
  10568. item_tmp.refine = sd->status.inventory[i].refine;
  10569. item_tmp.attribute = sd->status.inventory[i].attribute;
  10570. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10571. item_tmp.bound = sd->status.inventory[i].bound;
  10572. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10573. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10574. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10575. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10576. clif_additem(sd,0,0,flag);
  10577. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10578. }
  10579. }
  10580. clif_misceffect(&sd->bl,2);
  10581. }
  10582. return SCRIPT_CMD_SUCCESS;
  10583. }
  10584. /* ================================================================
  10585. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  10586. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  10587. * improved by [Lance]
  10588. * ================================================================*/
  10589. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  10590. {
  10591. int x,y,m,check_val=0,check_ID=0,i=0;
  10592. struct guild *g = NULL;
  10593. struct party_data *p = NULL;
  10594. const char *str;
  10595. const char *mapname;
  10596. unsigned int index;
  10597. mapname=script_getstr(st,2);
  10598. str=script_getstr(st,3);
  10599. x=script_getnum(st,4);
  10600. y=script_getnum(st,5);
  10601. if(script_hasdata(st,7)){
  10602. check_val=script_getnum(st,6);
  10603. check_ID=script_getnum(st,7);
  10604. }
  10605. if((m=map_mapname2mapid(mapname))< 0)
  10606. return 0;
  10607. if(!(index=mapindex_name2id(str)))
  10608. return 0;
  10609. switch(check_val){
  10610. case 1:
  10611. g = guild_search(check_ID);
  10612. if (g){
  10613. for( i=0; i < g->max_member; i++)
  10614. {
  10615. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  10616. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  10617. }
  10618. }
  10619. }
  10620. break;
  10621. case 2:
  10622. p = party_search(check_ID);
  10623. if(p){
  10624. for(i=0;i<MAX_PARTY; i++){
  10625. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  10626. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  10627. }
  10628. }
  10629. }
  10630. break;
  10631. default:
  10632. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  10633. break;
  10634. }
  10635. return SCRIPT_CMD_SUCCESS;
  10636. }
  10637. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  10638. {
  10639. char *event=va_arg(ap,char *);
  10640. struct mob_data *md = ((struct mob_data *)bl);
  10641. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  10642. return 1;
  10643. return SCRIPT_CMD_SUCCESS;
  10644. }
  10645. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  10646. {
  10647. const char *mapname,*event;
  10648. int16 m;
  10649. mapname=script_getstr(st,2);
  10650. event=script_getstr(st,3);
  10651. if( strcmp(event, "all") == 0 )
  10652. event = NULL;
  10653. else
  10654. check_event(st, event);
  10655. if( strcmp(mapname, "this") == 0 ) {
  10656. struct map_session_data *sd = script_rid2sd(st);
  10657. if( sd )
  10658. m = sd->bl.m;
  10659. else {
  10660. script_pushint(st,-1);
  10661. return 0;
  10662. }
  10663. }
  10664. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  10665. script_pushint(st,-1);
  10666. return 0;
  10667. }
  10668. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  10669. return SCRIPT_CMD_SUCCESS;
  10670. }
  10671. BUILDIN_FUNC(marriage)
  10672. {
  10673. const char *partner=script_getstr(st,2);
  10674. TBL_PC *sd=script_rid2sd(st);
  10675. TBL_PC *p_sd=map_nick2sd(partner);
  10676. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  10677. script_pushint(st,0);
  10678. return 0;
  10679. }
  10680. script_pushint(st,1);
  10681. return SCRIPT_CMD_SUCCESS;
  10682. }
  10683. BUILDIN_FUNC(wedding_effect)
  10684. {
  10685. TBL_PC *sd=script_rid2sd(st);
  10686. struct block_list *bl;
  10687. if(sd==NULL) {
  10688. bl=map_id2bl(st->oid);
  10689. } else
  10690. bl=&sd->bl;
  10691. clif_wedding_effect(bl);
  10692. return SCRIPT_CMD_SUCCESS;
  10693. }
  10694. BUILDIN_FUNC(divorce)
  10695. {
  10696. TBL_PC *sd=script_rid2sd(st);
  10697. if(!sd || !pc_divorce(sd)){
  10698. script_pushint(st,0);
  10699. return 0;
  10700. }
  10701. script_pushint(st,1);
  10702. return SCRIPT_CMD_SUCCESS;
  10703. }
  10704. BUILDIN_FUNC(ispartneron)
  10705. {
  10706. TBL_PC *sd=script_rid2sd(st);
  10707. if(sd==NULL || !pc_ismarried(sd) ||
  10708. map_charid2sd(sd->status.partner_id) == NULL) {
  10709. script_pushint(st,0);
  10710. return 0;
  10711. }
  10712. script_pushint(st,1);
  10713. return SCRIPT_CMD_SUCCESS;
  10714. }
  10715. BUILDIN_FUNC(getpartnerid)
  10716. {
  10717. TBL_PC *sd=script_rid2sd(st);
  10718. if (sd == NULL) {
  10719. script_pushint(st,0);
  10720. return 0;
  10721. }
  10722. script_pushint(st,sd->status.partner_id);
  10723. return SCRIPT_CMD_SUCCESS;
  10724. }
  10725. BUILDIN_FUNC(getchildid)
  10726. {
  10727. TBL_PC *sd=script_rid2sd(st);
  10728. if (sd == NULL) {
  10729. script_pushint(st,0);
  10730. return 0;
  10731. }
  10732. script_pushint(st,sd->status.child);
  10733. return SCRIPT_CMD_SUCCESS;
  10734. }
  10735. BUILDIN_FUNC(getmotherid)
  10736. {
  10737. TBL_PC *sd=script_rid2sd(st);
  10738. if (sd == NULL) {
  10739. script_pushint(st,0);
  10740. return 0;
  10741. }
  10742. script_pushint(st,sd->status.mother);
  10743. return SCRIPT_CMD_SUCCESS;
  10744. }
  10745. BUILDIN_FUNC(getfatherid)
  10746. {
  10747. TBL_PC *sd=script_rid2sd(st);
  10748. if (sd == NULL) {
  10749. script_pushint(st,0);
  10750. return 0;
  10751. }
  10752. script_pushint(st,sd->status.father);
  10753. return SCRIPT_CMD_SUCCESS;
  10754. }
  10755. BUILDIN_FUNC(warppartner)
  10756. {
  10757. int x,y;
  10758. unsigned short mapindex;
  10759. const char *str;
  10760. TBL_PC *sd=script_rid2sd(st);
  10761. TBL_PC *p_sd=NULL;
  10762. if(sd==NULL || !pc_ismarried(sd) ||
  10763. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  10764. script_pushint(st,0);
  10765. return 0;
  10766. }
  10767. str=script_getstr(st,2);
  10768. x=script_getnum(st,3);
  10769. y=script_getnum(st,4);
  10770. mapindex = mapindex_name2id(str);
  10771. if (mapindex) {
  10772. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  10773. script_pushint(st,1);
  10774. } else
  10775. script_pushint(st,0);
  10776. return SCRIPT_CMD_SUCCESS;
  10777. }
  10778. /*================================================
  10779. * Script for Displaying MOB Information [Valaris]
  10780. *------------------------------------------------*/
  10781. BUILDIN_FUNC(strmobinfo)
  10782. {
  10783. int num=script_getnum(st,2);
  10784. int class_=script_getnum(st,3);
  10785. if(!mobdb_checkid(class_))
  10786. {
  10787. if (num < 3) //requested a string
  10788. script_pushconststr(st,"");
  10789. else
  10790. script_pushint(st,0);
  10791. return 0;
  10792. }
  10793. switch (num) {
  10794. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  10795. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  10796. case 3: script_pushint(st,mob_db(class_)->lv); break;
  10797. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  10798. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  10799. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  10800. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  10801. default:
  10802. script_pushint(st,0);
  10803. break;
  10804. }
  10805. return SCRIPT_CMD_SUCCESS;
  10806. }
  10807. /*==========================================
  10808. * Summon guardians [Valaris]
  10809. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  10810. *------------------------------------------*/
  10811. BUILDIN_FUNC(guardian)
  10812. {
  10813. int class_=0,x=0,y=0,guardian=0;
  10814. const char *str,*map,*evt="";
  10815. struct script_data *data;
  10816. bool has_index = false;
  10817. map =script_getstr(st,2);
  10818. x =script_getnum(st,3);
  10819. y =script_getnum(st,4);
  10820. str =script_getstr(st,5);
  10821. class_=script_getnum(st,6);
  10822. if( script_hasdata(st,8) )
  10823. {// "<event label>",<guardian index>
  10824. evt=script_getstr(st,7);
  10825. guardian=script_getnum(st,8);
  10826. has_index = true;
  10827. } else if( script_hasdata(st,7) ){
  10828. data=script_getdata(st,7);
  10829. get_val(st,data);
  10830. if( data_isstring(data) )
  10831. {// "<event label>"
  10832. evt=script_getstr(st,7);
  10833. } else if( data_isint(data) )
  10834. {// <guardian index>
  10835. guardian=script_getnum(st,7);
  10836. has_index = true;
  10837. } else {
  10838. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  10839. script_reportdata(data);
  10840. return 1;
  10841. }
  10842. }
  10843. check_event(st, evt);
  10844. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  10845. return SCRIPT_CMD_SUCCESS;
  10846. }
  10847. /*==========================================
  10848. * Invisible Walls [Zephyrus]
  10849. *------------------------------------------*/
  10850. BUILDIN_FUNC(setwall)
  10851. {
  10852. const char *map, *name;
  10853. int x, y, m, size, dir;
  10854. bool shootable;
  10855. map = script_getstr(st,2);
  10856. x = script_getnum(st,3);
  10857. y = script_getnum(st,4);
  10858. size = script_getnum(st,5);
  10859. dir = script_getnum(st,6);
  10860. shootable = script_getnum(st,7);
  10861. name = script_getstr(st,8);
  10862. if( (m = map_mapname2mapid(map)) < 0 )
  10863. return 0; // Invalid Map
  10864. map_iwall_set(m, x, y, size, dir, shootable, name);
  10865. return SCRIPT_CMD_SUCCESS;
  10866. }
  10867. BUILDIN_FUNC(delwall)
  10868. {
  10869. const char *name = script_getstr(st,2);
  10870. map_iwall_remove(name);
  10871. return SCRIPT_CMD_SUCCESS;
  10872. }
  10873. /// Retrieves various information about the specified guardian.
  10874. ///
  10875. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  10876. /// type: 0 - whether it is deployed or not
  10877. /// 1 - maximum hp
  10878. /// 2 - current hp
  10879. ///
  10880. BUILDIN_FUNC(guardianinfo)
  10881. {
  10882. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10883. int id = script_getnum(st,3);
  10884. int type = script_getnum(st,4);
  10885. struct guild_castle* gc = guild_mapname2gc(mapname);
  10886. struct mob_data* gd;
  10887. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  10888. {
  10889. script_pushint(st,-1);
  10890. return 0;
  10891. }
  10892. if( type == 0 )
  10893. script_pushint(st, gc->guardian[id].visible);
  10894. else
  10895. if( !gc->guardian[id].visible )
  10896. script_pushint(st,-1);
  10897. else
  10898. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  10899. script_pushint(st,-1);
  10900. else
  10901. {
  10902. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  10903. else if( type == 2 ) script_pushint(st,gd->status.hp);
  10904. else
  10905. script_pushint(st,-1);
  10906. }
  10907. return SCRIPT_CMD_SUCCESS;
  10908. }
  10909. /*==========================================
  10910. * Get the item name by item_id or null
  10911. *------------------------------------------*/
  10912. BUILDIN_FUNC(getitemname)
  10913. {
  10914. unsigned short item_id = 0;
  10915. struct item_data *i_data;
  10916. char *item_name;
  10917. struct script_data *data;
  10918. data=script_getdata(st,2);
  10919. get_val(st,data);
  10920. if( data_isstring(data) ){
  10921. const char *name=conv_str(st,data);
  10922. struct item_data *item_data = itemdb_searchname(name);
  10923. if( item_data )
  10924. item_id=item_data->nameid;
  10925. }else
  10926. item_id=conv_num(st,data);
  10927. i_data = itemdb_exists(item_id);
  10928. if (i_data == NULL)
  10929. {
  10930. script_pushconststr(st,"null");
  10931. return 0;
  10932. }
  10933. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  10934. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  10935. script_pushstr(st,item_name);
  10936. return SCRIPT_CMD_SUCCESS;
  10937. }
  10938. /*==========================================
  10939. * Returns number of slots an item has. [Skotlex]
  10940. *------------------------------------------*/
  10941. BUILDIN_FUNC(getitemslots)
  10942. {
  10943. unsigned short item_id;
  10944. struct item_data *i_data;
  10945. item_id=script_getnum(st,2);
  10946. i_data = itemdb_exists(item_id);
  10947. if (i_data)
  10948. script_pushint(st,i_data->slot);
  10949. else
  10950. script_pushint(st,-1);
  10951. return SCRIPT_CMD_SUCCESS;
  10952. }
  10953. // TODO: add matk here if needed/once we get rid of RENEWAL
  10954. /*==========================================
  10955. * Returns some values of an item [Lupus]
  10956. * Price, Weight, etc...
  10957. getiteminfo(itemID,n), where n
  10958. 0 value_buy;
  10959. 1 value_sell;
  10960. 2 type;
  10961. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10962. if = 0, then monsters don't drop it at all (rare or a quest item)
  10963. if = -1, then this item is sold in NPC shops only
  10964. 4 sex;
  10965. 5 equip;
  10966. 6 weight;
  10967. 7 atk;
  10968. 8 def;
  10969. 9 range;
  10970. 10 slot;
  10971. 11 look;
  10972. 12 elv;
  10973. 13 wlv;
  10974. 14 view id
  10975. *------------------------------------------*/
  10976. BUILDIN_FUNC(getiteminfo)
  10977. {
  10978. unsigned short item_id,n;
  10979. int *item_arr;
  10980. struct item_data *i_data;
  10981. item_id = script_getnum(st,2);
  10982. n = script_getnum(st,3);
  10983. i_data = itemdb_exists(item_id);
  10984. if (i_data && n>=0 && n<=14) {
  10985. item_arr = (int*)&i_data->value_buy;
  10986. script_pushint(st,item_arr[n]);
  10987. } else
  10988. script_pushint(st,-1);
  10989. return SCRIPT_CMD_SUCCESS;
  10990. }
  10991. /*==========================================
  10992. * Set some values of an item [Lupus]
  10993. * Price, Weight, etc...
  10994. setiteminfo(itemID,n,Value), where n
  10995. 0 value_buy;
  10996. 1 value_sell;
  10997. 2 type;
  10998. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10999. if = 0, then monsters don't drop it at all (rare or a quest item)
  11000. if = -1, then this item is sold in NPC shops only
  11001. 4 sex;
  11002. 5 equip;
  11003. 6 weight;
  11004. 7 atk;
  11005. 8 def;
  11006. 9 range;
  11007. 10 slot;
  11008. 11 look;
  11009. 12 elv;
  11010. 13 wlv;
  11011. 14 view id
  11012. * Returns Value or -1 if the wrong field's been set
  11013. *------------------------------------------*/
  11014. BUILDIN_FUNC(setiteminfo)
  11015. {
  11016. unsigned short item_id;
  11017. int n,value;
  11018. int *item_arr;
  11019. struct item_data *i_data;
  11020. item_id = script_getnum(st,2);
  11021. n = script_getnum(st,3);
  11022. value = script_getnum(st,4);
  11023. i_data = itemdb_exists(item_id);
  11024. if (i_data && n>=0 && n<=14) {
  11025. item_arr = (int*)&i_data->value_buy;
  11026. item_arr[n] = value;
  11027. script_pushint(st,value);
  11028. } else
  11029. script_pushint(st,-1);
  11030. return SCRIPT_CMD_SUCCESS;
  11031. }
  11032. /*==========================================
  11033. * Returns value from equipped item slot n [Lupus]
  11034. getequipcardid(num,slot)
  11035. where
  11036. num = eqip position slot
  11037. slot = 0,1,2,3 (Card Slot N)
  11038. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11039. it's useful when you want to check item cards or if it's signed
  11040. Useful for such quests as "Sign this refined item with players name" etc
  11041. Hat[0] +4 -> Player's Hat[0] +4
  11042. *------------------------------------------*/
  11043. BUILDIN_FUNC(getequipcardid)
  11044. {
  11045. int i=-1,num,slot;
  11046. TBL_PC *sd;
  11047. num=script_getnum(st,2);
  11048. slot=script_getnum(st,3);
  11049. sd=script_rid2sd(st);
  11050. if (num > 0 && num <= ARRAYLENGTH(equip))
  11051. i=pc_checkequip(sd,equip[num-1]);
  11052. if(i >= 0 && slot>=0 && slot<4)
  11053. script_pushint(st,sd->status.inventory[i].card[slot]);
  11054. else
  11055. script_pushint(st,0);
  11056. return SCRIPT_CMD_SUCCESS;
  11057. }
  11058. /*==========================================
  11059. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11060. *------------------------------------------*/
  11061. BUILDIN_FUNC(petskillbonus)
  11062. {
  11063. struct pet_data *pd;
  11064. TBL_PC *sd=script_rid2sd(st);
  11065. if(sd==NULL || sd->pd==NULL)
  11066. return 0;
  11067. pd=sd->pd;
  11068. if (pd->bonus)
  11069. { //Clear previous bonus
  11070. if (pd->bonus->timer != INVALID_TIMER)
  11071. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11072. } else //init
  11073. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11074. pd->bonus->type=script_getnum(st,2);
  11075. pd->bonus->val=script_getnum(st,3);
  11076. pd->bonus->duration=script_getnum(st,4);
  11077. pd->bonus->delay=script_getnum(st,5);
  11078. if (pd->state.skillbonus == 1)
  11079. pd->state.skillbonus=0; // waiting state
  11080. // wait for timer to start
  11081. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11082. pd->bonus->timer = INVALID_TIMER;
  11083. else
  11084. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11085. return SCRIPT_CMD_SUCCESS;
  11086. }
  11087. /*==========================================
  11088. * pet looting [Valaris] //Rewritten by [Skotlex]
  11089. *------------------------------------------*/
  11090. BUILDIN_FUNC(petloot)
  11091. {
  11092. int max;
  11093. struct pet_data *pd;
  11094. TBL_PC *sd=script_rid2sd(st);
  11095. if(sd==NULL || sd->pd==NULL)
  11096. return 0;
  11097. max=script_getnum(st,2);
  11098. if(max < 1)
  11099. max = 1; //Let'em loot at least 1 item.
  11100. else if (max > MAX_PETLOOT_SIZE)
  11101. max = MAX_PETLOOT_SIZE;
  11102. pd = sd->pd;
  11103. if (pd->loot != NULL)
  11104. { //Release whatever was there already and reallocate memory
  11105. pet_lootitem_drop(pd, pd->master);
  11106. aFree(pd->loot->item);
  11107. }
  11108. else
  11109. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11110. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11111. pd->loot->max=max;
  11112. pd->loot->count = 0;
  11113. pd->loot->weight = 0;
  11114. return SCRIPT_CMD_SUCCESS;
  11115. }
  11116. /*==========================================
  11117. * Set arrays with info of all sd inventory :
  11118. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11119. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11120. * @inventorylist_card(0..3), @inventorylist_expire
  11121. * @inventorylist_count = scalar
  11122. *------------------------------------------*/
  11123. BUILDIN_FUNC(getinventorylist)
  11124. {
  11125. TBL_PC *sd=script_rid2sd(st);
  11126. char card_var[NAME_LENGTH];
  11127. int i,j=0,k;
  11128. if(!sd) return 0;
  11129. for(i=0;i<MAX_INVENTORY;i++){
  11130. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  11131. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  11132. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  11133. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  11134. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  11135. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  11136. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  11137. for (k = 0; k < MAX_SLOTS; k++)
  11138. {
  11139. sprintf(card_var, "@inventorylist_card%d",k+1);
  11140. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  11141. }
  11142. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  11143. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  11144. j++;
  11145. }
  11146. }
  11147. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11148. return SCRIPT_CMD_SUCCESS;
  11149. }
  11150. BUILDIN_FUNC(getskilllist)
  11151. {
  11152. TBL_PC *sd=script_rid2sd(st);
  11153. int i,j=0;
  11154. if(!sd) return 0;
  11155. for(i=0;i<MAX_SKILL;i++){
  11156. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11157. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11158. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11159. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11160. j++;
  11161. }
  11162. }
  11163. pc_setreg(sd,add_str("@skilllist_count"),j);
  11164. return SCRIPT_CMD_SUCCESS;
  11165. }
  11166. BUILDIN_FUNC(clearitem)
  11167. {
  11168. TBL_PC *sd=script_rid2sd(st);
  11169. int i;
  11170. if(sd==NULL) return 0;
  11171. for (i=0; i<MAX_INVENTORY; i++) {
  11172. if (sd->status.inventory[i].amount) {
  11173. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11174. }
  11175. }
  11176. return SCRIPT_CMD_SUCCESS;
  11177. }
  11178. /*==========================================
  11179. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11180. *------------------------------------------*/
  11181. BUILDIN_FUNC(disguise)
  11182. {
  11183. int id;
  11184. TBL_PC* sd = script_rid2sd(st);
  11185. if (sd == NULL) return 0;
  11186. id = script_getnum(st,2);
  11187. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11188. pc_disguise(sd, id);
  11189. script_pushint(st,id);
  11190. } else
  11191. script_pushint(st,0);
  11192. return SCRIPT_CMD_SUCCESS;
  11193. }
  11194. /*==========================================
  11195. * Undisguise Player (returns 1 if success, 0 on fail)
  11196. *------------------------------------------*/
  11197. BUILDIN_FUNC(undisguise)
  11198. {
  11199. TBL_PC* sd = script_rid2sd(st);
  11200. if (sd == NULL) return 0;
  11201. if (sd->disguise) {
  11202. pc_disguise(sd, 0);
  11203. script_pushint(st,0);
  11204. } else {
  11205. script_pushint(st,1);
  11206. }
  11207. return SCRIPT_CMD_SUCCESS;
  11208. }
  11209. /*==========================================
  11210. * Transform a bl to another _class,
  11211. * @type unused
  11212. *------------------------------------------*/
  11213. BUILDIN_FUNC(classchange)
  11214. {
  11215. int _class,type;
  11216. struct block_list *bl=map_id2bl(st->oid);
  11217. if(bl==NULL) return 0;
  11218. _class=script_getnum(st,2);
  11219. type=script_getnum(st,3);
  11220. clif_class_change(bl,_class,type);
  11221. return SCRIPT_CMD_SUCCESS;
  11222. }
  11223. /*==========================================
  11224. * Display an effect
  11225. *------------------------------------------*/
  11226. BUILDIN_FUNC(misceffect)
  11227. {
  11228. int type;
  11229. type=script_getnum(st,2);
  11230. if(st->oid && st->oid != fake_nd->bl.id) {
  11231. struct block_list *bl = map_id2bl(st->oid);
  11232. if (bl)
  11233. clif_specialeffect(bl,type,AREA);
  11234. } else{
  11235. TBL_PC *sd=script_rid2sd(st);
  11236. if(sd)
  11237. clif_specialeffect(&sd->bl,type,AREA);
  11238. }
  11239. return SCRIPT_CMD_SUCCESS;
  11240. }
  11241. /*==========================================
  11242. * Play a BGM on a single client [Rikter/Yommy]
  11243. *------------------------------------------*/
  11244. BUILDIN_FUNC(playBGM)
  11245. {
  11246. const char* name;
  11247. struct map_session_data* sd;
  11248. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11249. name = script_getstr(st,2);
  11250. clif_playBGM(sd, name);
  11251. }
  11252. return SCRIPT_CMD_SUCCESS;
  11253. }
  11254. static int playBGM_sub(struct block_list* bl,va_list ap)
  11255. {
  11256. const char* name = va_arg(ap,const char*);
  11257. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11258. return 0;
  11259. }
  11260. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11261. {
  11262. const char* name = va_arg(args, const char*);
  11263. clif_playBGM(sd, name);
  11264. return 0;
  11265. }
  11266. /*==========================================
  11267. * Play a BGM on multiple client [Rikter/Yommy]
  11268. *------------------------------------------*/
  11269. BUILDIN_FUNC(playBGMall)
  11270. {
  11271. const char* name;
  11272. name = script_getstr(st,2);
  11273. if( script_hasdata(st,7) ) {// specified part of map
  11274. const char* map = script_getstr(st,3);
  11275. int x0 = script_getnum(st,4);
  11276. int y0 = script_getnum(st,5);
  11277. int x1 = script_getnum(st,6);
  11278. int y1 = script_getnum(st,7);
  11279. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  11280. }
  11281. else if( script_hasdata(st,3) ) {// entire map
  11282. const char* map = script_getstr(st,3);
  11283. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  11284. }
  11285. else {// entire server
  11286. map_foreachpc(&playBGM_foreachpc_sub, name);
  11287. }
  11288. return SCRIPT_CMD_SUCCESS;
  11289. }
  11290. /*==========================================
  11291. * Play a .wav sound for sd
  11292. *------------------------------------------*/
  11293. BUILDIN_FUNC(soundeffect)
  11294. {
  11295. TBL_PC* sd = script_rid2sd(st);
  11296. const char* name = script_getstr(st,2);
  11297. int type = script_getnum(st,3);
  11298. if(sd) {
  11299. clif_soundeffect(sd,&sd->bl,name,type);
  11300. }
  11301. return SCRIPT_CMD_SUCCESS;
  11302. }
  11303. int soundeffect_sub(struct block_list* bl,va_list ap)
  11304. {
  11305. char* name = va_arg(ap,char*);
  11306. int type = va_arg(ap,int);
  11307. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11308. return 0;
  11309. }
  11310. /*==========================================
  11311. * Play a sound effect (.wav) on multiple clients
  11312. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11313. *------------------------------------------*/
  11314. BUILDIN_FUNC(soundeffectall)
  11315. {
  11316. struct block_list* bl;
  11317. const char* name;
  11318. int type;
  11319. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11320. if (!bl)
  11321. return 0;
  11322. name = script_getstr(st,2);
  11323. type = script_getnum(st,3);
  11324. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11325. if(!script_hasdata(st,4))
  11326. { // area around
  11327. clif_soundeffectall(bl, name, type, AREA);
  11328. }
  11329. else
  11330. if(!script_hasdata(st,5))
  11331. { // entire map
  11332. const char* map = script_getstr(st,4);
  11333. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  11334. }
  11335. else
  11336. if(script_hasdata(st,8))
  11337. { // specified part of map
  11338. const char* map = script_getstr(st,4);
  11339. int x0 = script_getnum(st,5);
  11340. int y0 = script_getnum(st,6);
  11341. int x1 = script_getnum(st,7);
  11342. int y1 = script_getnum(st,8);
  11343. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  11344. }
  11345. else
  11346. {
  11347. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11348. }
  11349. return SCRIPT_CMD_SUCCESS;
  11350. }
  11351. /*==========================================
  11352. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11353. *------------------------------------------*/
  11354. BUILDIN_FUNC(petrecovery)
  11355. {
  11356. struct pet_data *pd;
  11357. TBL_PC *sd=script_rid2sd(st);
  11358. if(sd==NULL || sd->pd==NULL)
  11359. return 0;
  11360. pd=sd->pd;
  11361. if (pd->recovery)
  11362. { //Halt previous bonus
  11363. if (pd->recovery->timer != INVALID_TIMER)
  11364. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11365. } else //Init
  11366. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11367. pd->recovery->type = (sc_type)script_getnum(st,2);
  11368. pd->recovery->delay = script_getnum(st,3);
  11369. pd->recovery->timer = INVALID_TIMER;
  11370. return SCRIPT_CMD_SUCCESS;
  11371. }
  11372. /*==========================================
  11373. * pet healing [Valaris] //Rewritten by [Skotlex]
  11374. *------------------------------------------*/
  11375. BUILDIN_FUNC(petheal)
  11376. {
  11377. struct pet_data *pd;
  11378. TBL_PC *sd=script_rid2sd(st);
  11379. if(sd==NULL || sd->pd==NULL)
  11380. return 0;
  11381. pd=sd->pd;
  11382. if (pd->s_skill)
  11383. { //Clear previous skill
  11384. if (pd->s_skill->timer != INVALID_TIMER)
  11385. {
  11386. if (pd->s_skill->id)
  11387. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11388. else
  11389. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11390. }
  11391. } else //init memory
  11392. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11393. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11394. //Use the lv as the amount to heal
  11395. pd->s_skill->lv=script_getnum(st,2);
  11396. pd->s_skill->delay=script_getnum(st,3);
  11397. pd->s_skill->hp=script_getnum(st,4);
  11398. pd->s_skill->sp=script_getnum(st,5);
  11399. //Use delay as initial offset to avoid skill/heal exploits
  11400. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11401. pd->s_skill->timer = INVALID_TIMER;
  11402. else
  11403. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11404. return SCRIPT_CMD_SUCCESS;
  11405. }
  11406. /*==========================================
  11407. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11408. *------------------------------------------*/
  11409. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11410. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11411. BUILDIN_FUNC(petskillattack)
  11412. {
  11413. struct pet_data *pd;
  11414. struct script_data *data;
  11415. TBL_PC *sd=script_rid2sd(st);
  11416. if(sd==NULL || sd->pd==NULL)
  11417. return 0;
  11418. pd=sd->pd;
  11419. if (pd->a_skill == NULL)
  11420. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11421. data = script_getdata(st, 2);
  11422. get_val(st, data); // Convert into value in case of a variable
  11423. pd->a_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11424. pd->a_skill->lv=script_getnum(st,3);
  11425. pd->a_skill->div_ = 0;
  11426. pd->a_skill->rate=script_getnum(st,4);
  11427. pd->a_skill->bonusrate=script_getnum(st,5);
  11428. return SCRIPT_CMD_SUCCESS;
  11429. }
  11430. /*==========================================
  11431. * pet attack skills [Valaris]
  11432. *------------------------------------------*/
  11433. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  11434. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  11435. BUILDIN_FUNC(petskillattack2)
  11436. {
  11437. struct pet_data *pd;
  11438. struct script_data *data;
  11439. TBL_PC *sd=script_rid2sd(st);
  11440. if(sd==NULL || sd->pd==NULL)
  11441. return 0;
  11442. pd=sd->pd;
  11443. if (pd->a_skill == NULL)
  11444. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11445. data = script_getdata(st, 2);
  11446. get_val(st, data); // Convert into value in case of a variable
  11447. pd->a_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11448. pd->a_skill->lv=script_getnum(st,3);
  11449. pd->a_skill->div_ = script_getnum(st,4);
  11450. pd->a_skill->rate=script_getnum(st,5);
  11451. pd->a_skill->bonusrate=script_getnum(st,6);
  11452. return SCRIPT_CMD_SUCCESS;
  11453. }
  11454. /*==========================================
  11455. * pet support skills [Skotlex]
  11456. *------------------------------------------*/
  11457. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  11458. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  11459. BUILDIN_FUNC(petskillsupport)
  11460. {
  11461. struct pet_data *pd;
  11462. struct script_data *data;
  11463. TBL_PC *sd=script_rid2sd(st);
  11464. if(sd==NULL || sd->pd==NULL)
  11465. return 0;
  11466. pd=sd->pd;
  11467. if (pd->s_skill)
  11468. { //Clear previous skill
  11469. if (pd->s_skill->timer != INVALID_TIMER)
  11470. {
  11471. if (pd->s_skill->id)
  11472. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11473. else
  11474. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11475. }
  11476. } else //init memory
  11477. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11478. data = script_getdata(st, 2);
  11479. get_val(st, data); // Convert into value in case of a variable
  11480. pd->s_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11481. pd->s_skill->lv=script_getnum(st,3);
  11482. pd->s_skill->delay=script_getnum(st,4);
  11483. pd->s_skill->hp=script_getnum(st,5);
  11484. pd->s_skill->sp=script_getnum(st,6);
  11485. //Use delay as initial offset to avoid skill/heal exploits
  11486. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11487. pd->s_skill->timer = INVALID_TIMER;
  11488. else
  11489. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  11490. return SCRIPT_CMD_SUCCESS;
  11491. }
  11492. /*==========================================
  11493. * Scripted skill effects [Celest]
  11494. *------------------------------------------*/
  11495. /// skilleffect <skill id>,<level>
  11496. /// skilleffect "<skill name>",<level>
  11497. BUILDIN_FUNC(skilleffect)
  11498. {
  11499. TBL_PC *sd;
  11500. uint16 skill_id, skill_lv;
  11501. struct script_data *data = script_getdata(st, 2);
  11502. get_val(st, data); // Convert into value in case of a variable
  11503. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11504. skill_lv = script_getnum(st,3);
  11505. sd = script_rid2sd(st);
  11506. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  11507. * which leaves the server thinking it still is sitting. */
  11508. if( pc_issit(sd) ) {
  11509. pc_setstand(sd);
  11510. skill_sit(sd, 0);
  11511. clif_standing(&sd->bl);
  11512. }
  11513. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  11514. return SCRIPT_CMD_SUCCESS;
  11515. }
  11516. /*==========================================
  11517. * NPC skill effects [Valaris]
  11518. *------------------------------------------*/
  11519. /// npcskilleffect <skill id>,<level>,<x>,<y>
  11520. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  11521. BUILDIN_FUNC(npcskilleffect)
  11522. {
  11523. struct block_list *bl= map_id2bl(st->oid);
  11524. uint16 skill_id, skill_lv;
  11525. int x, y;
  11526. struct script_data *data = script_getdata(st, 2);
  11527. get_val(st, data); // Convert into value in case of a variable
  11528. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11529. skill_lv=script_getnum(st,3);
  11530. x=script_getnum(st,4);
  11531. y=script_getnum(st,5);
  11532. if (bl)
  11533. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  11534. return SCRIPT_CMD_SUCCESS;
  11535. }
  11536. /*==========================================
  11537. * Special effects [Valaris]
  11538. *------------------------------------------*/
  11539. BUILDIN_FUNC(specialeffect)
  11540. {
  11541. struct block_list *bl=map_id2bl(st->oid);
  11542. int type = script_getnum(st,2);
  11543. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11544. if(bl==NULL)
  11545. return 0;
  11546. if( script_hasdata(st,4) )
  11547. {
  11548. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  11549. if(nd)
  11550. clif_specialeffect(&nd->bl, type, target);
  11551. }
  11552. else
  11553. {
  11554. if (target == SELF) {
  11555. TBL_PC *sd=script_rid2sd(st);
  11556. if (sd)
  11557. clif_specialeffect_single(bl,type,sd->fd);
  11558. } else {
  11559. clif_specialeffect(bl, type, target);
  11560. }
  11561. }
  11562. return SCRIPT_CMD_SUCCESS;
  11563. }
  11564. BUILDIN_FUNC(specialeffect2)
  11565. {
  11566. TBL_PC *sd=script_rid2sd(st);
  11567. int type = script_getnum(st,2);
  11568. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11569. if( script_hasdata(st,4) )
  11570. sd = map_nick2sd(script_getstr(st,4));
  11571. if (sd)
  11572. clif_specialeffect(&sd->bl, type, target);
  11573. return SCRIPT_CMD_SUCCESS;
  11574. }
  11575. /*==========================================
  11576. * Nude [Valaris]
  11577. *------------------------------------------*/
  11578. BUILDIN_FUNC(nude)
  11579. {
  11580. TBL_PC *sd = script_rid2sd(st);
  11581. int i, calcflag = 0;
  11582. if( sd == NULL )
  11583. return 0;
  11584. for( i = 0 ; i < EQI_MAX; i++ ) {
  11585. if( sd->equip_index[ i ] >= 0 ) {
  11586. if( !calcflag )
  11587. calcflag = 1;
  11588. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  11589. }
  11590. }
  11591. if( calcflag )
  11592. status_calc_pc(sd,SCO_NONE);
  11593. return SCRIPT_CMD_SUCCESS;
  11594. }
  11595. int atcommand_sub(struct script_state* st,int type) {
  11596. TBL_PC dummy_sd;
  11597. TBL_PC* sd;
  11598. int fd;
  11599. const char* cmd;
  11600. cmd = script_getstr(st,2);
  11601. if (st->rid) {
  11602. sd = script_rid2sd(st);
  11603. fd = sd->fd;
  11604. } else { //Use a dummy character.
  11605. sd = &dummy_sd;
  11606. fd = 0;
  11607. memset(&dummy_sd, 0, sizeof(TBL_PC));
  11608. if (st->oid)
  11609. {
  11610. struct block_list* bl = map_id2bl(st->oid);
  11611. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  11612. if (bl->type == BL_NPC)
  11613. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  11614. }
  11615. }
  11616. if (!is_atcommand(fd, sd, cmd, type)) {
  11617. ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd);
  11618. script_reportsrc(st);
  11619. return 1;
  11620. }
  11621. return SCRIPT_CMD_SUCCESS;
  11622. }
  11623. /*==========================================
  11624. * gmcommand [MouseJstr]
  11625. *------------------------------------------*/
  11626. BUILDIN_FUNC(atcommand) {
  11627. return atcommand_sub(st,0);
  11628. }
  11629. /*==========================================
  11630. * Displays a message for the player only (like system messages like "you got an apple" )
  11631. *------------------------------------------*/
  11632. BUILDIN_FUNC(dispbottom)
  11633. {
  11634. TBL_PC *sd=script_rid2sd(st);
  11635. const char *message;
  11636. message=script_getstr(st,2);
  11637. if(sd)
  11638. clif_disp_onlyself(sd,message,(int)strlen(message));
  11639. return SCRIPT_CMD_SUCCESS;
  11640. }
  11641. /*===================================
  11642. * Heal portion of recovery command
  11643. *-----------------------------------*/
  11644. int recovery_sub(struct map_session_data* sd, int revive)
  11645. {
  11646. if(revive&(1|4) && pc_isdead(sd)) {
  11647. status_revive(&sd->bl, 100, 100);
  11648. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  11649. clif_specialeffect(&sd->bl, 77, AREA);
  11650. } else if(revive&(1|2) && !pc_isdead(sd)) {
  11651. status_percent_heal(&sd->bl, 100, 100);
  11652. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  11653. }
  11654. return SCRIPT_CMD_SUCCESS;
  11655. }
  11656. /*=========================================================================
  11657. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  11658. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  11659. * <type> determines <option>:
  11660. * 0 : char_id
  11661. * 1 : party_id
  11662. * 2 : guild_id
  11663. * 3 : map_name
  11664. * 4 : all characters
  11665. * <revive_flag>:
  11666. * 1 : Revive and heal all players (default)
  11667. * 2 : Heal living players only
  11668. * 4 : Revive dead players only
  11669. * <map name>:
  11670. * for types 1-2 : map_name (null = all maps)
  11671. *-------------------------------------------------------------------------*/
  11672. BUILDIN_FUNC(recovery)
  11673. {
  11674. TBL_PC *sd;
  11675. int map = 0, type = 0, revive = 1;
  11676. type = script_getnum(st,2);
  11677. if(script_hasdata(st,4))
  11678. revive = script_getnum(st,4);
  11679. switch(type) {
  11680. case 0:
  11681. if(script_hasdata(st,3))
  11682. sd=map_charid2sd(script_getnum(st,3));
  11683. else if((sd = script_rid2sd(st)) == NULL)
  11684. return 0; //If we don't have sd by now, bail out
  11685. recovery_sub(sd, revive);
  11686. break;
  11687. case 1:
  11688. {
  11689. struct party_data* p;
  11690. struct map_session_data* pl_sd;
  11691. //When no party given, we use invoker party
  11692. int p_id = 0, i;
  11693. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11694. map = map_mapname2mapid(script_getstr(st,5));
  11695. if(map < 1) { //But we'll check anyways
  11696. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11697. return 1;
  11698. }
  11699. }
  11700. if(script_hasdata(st,3))
  11701. p_id = script_getnum(st,3);
  11702. else if((sd = script_rid2sd(st)))
  11703. p_id = sd->status.party_id;
  11704. p = party_search(p_id);
  11705. if(p == NULL)
  11706. return 0;
  11707. for (i = 0; i < MAX_PARTY; i++) {
  11708. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  11709. || (map && pl_sd->bl.m != map))
  11710. continue;
  11711. recovery_sub(pl_sd, revive);
  11712. }
  11713. break;
  11714. }
  11715. case 2:
  11716. {
  11717. struct guild* g;
  11718. struct map_session_data* pl_sd;
  11719. //When no guild given, we use invoker guild
  11720. int g_id = 0, i;
  11721. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11722. map = map_mapname2mapid(script_getstr(st,5));
  11723. if(map < 1) { //But we'll check anyways
  11724. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11725. return 1;
  11726. }
  11727. }
  11728. if(script_hasdata(st,3))
  11729. g_id = script_getnum(st,3);
  11730. else if((sd = script_rid2sd(st)))
  11731. g_id = sd->status.guild_id;
  11732. g = guild_search(g_id);
  11733. if(g == NULL)
  11734. return 0;
  11735. for (i = 0; i < MAX_GUILD; i++) {
  11736. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  11737. || (map && pl_sd->bl.m != map))
  11738. continue;
  11739. recovery_sub(pl_sd, revive);
  11740. }
  11741. break;
  11742. }
  11743. case 3:
  11744. if(script_hasdata(st,3))
  11745. map = map_mapname2mapid(script_getstr(st,3));
  11746. else if((sd = script_rid2sd(st)))
  11747. map = sd->bl.m;
  11748. if(map < 1)
  11749. return 1; //No sd and no map given - return
  11750. case 4:
  11751. {
  11752. struct s_mapiterator *iter;
  11753. struct script_data *data = script_getdata(st, 3);
  11754. get_val(st, data); // Convert into value in case of a variable
  11755. if(script_hasdata(st,3) && !data_isstring(data))
  11756. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  11757. iter = mapit_getallusers();
  11758. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11759. if(type == 3 && sd->bl.m != map)
  11760. continue;
  11761. recovery_sub(sd, revive);
  11762. }
  11763. mapit_free(iter);
  11764. break;
  11765. }
  11766. default:
  11767. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  11768. script_pushint(st,-1);
  11769. return 1;
  11770. }
  11771. script_pushint(st,1); //Successfully executed without errors
  11772. return SCRIPT_CMD_SUCCESS;
  11773. }
  11774. /*==========================================
  11775. * Get your pet info: getpetinfo(n)
  11776. * n -> 0:pet_id 1:pet_class 2:pet_name
  11777. * 3:friendly 4:hungry, 5: rename flag.6:level
  11778. *------------------------------------------*/
  11779. BUILDIN_FUNC(getpetinfo)
  11780. {
  11781. TBL_PC *sd=script_rid2sd(st);
  11782. TBL_PET *pd;
  11783. int type=script_getnum(st,2);
  11784. if(!sd || !sd->pd) {
  11785. if (type == 2)
  11786. script_pushconststr(st,"null");
  11787. else
  11788. script_pushint(st,0);
  11789. return 0;
  11790. }
  11791. pd = sd->pd;
  11792. switch(type){
  11793. case 0: script_pushint(st,pd->pet.pet_id); break;
  11794. case 1: script_pushint(st,pd->pet.class_); break;
  11795. case 2: script_pushstrcopy(st,pd->pet.name); break;
  11796. case 3: script_pushint(st,pd->pet.intimate); break;
  11797. case 4: script_pushint(st,pd->pet.hungry); break;
  11798. case 5: script_pushint(st,pd->pet.rename_flag); break;
  11799. case 6: script_pushint(st,(int)pd->pet.level); break;
  11800. default:
  11801. script_pushint(st,0);
  11802. break;
  11803. }
  11804. return SCRIPT_CMD_SUCCESS;
  11805. }
  11806. /*==========================================
  11807. * Get your homunculus info: gethominfo(n)
  11808. * n -> 0:hom_id 1:class 2:name
  11809. * 3:friendly 4:hungry, 5: rename flag.
  11810. * 6: level
  11811. *------------------------------------------*/
  11812. BUILDIN_FUNC(gethominfo)
  11813. {
  11814. TBL_PC *sd=script_rid2sd(st);
  11815. TBL_HOM *hd;
  11816. int type=script_getnum(st,2);
  11817. hd = sd?sd->hd:NULL;
  11818. if(!hd) {
  11819. if (type == 2)
  11820. script_pushconststr(st,"null");
  11821. else
  11822. script_pushint(st,0);
  11823. return 0;
  11824. }
  11825. switch(type){
  11826. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  11827. case 1: script_pushint(st,hd->homunculus.class_); break;
  11828. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  11829. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  11830. case 4: script_pushint(st,hd->homunculus.hunger); break;
  11831. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  11832. case 6: script_pushint(st,hd->homunculus.level); break;
  11833. default:
  11834. script_pushint(st,0);
  11835. break;
  11836. }
  11837. return SCRIPT_CMD_SUCCESS;
  11838. }
  11839. /// Retrieves information about character's mercenary
  11840. /// getmercinfo <type>[,<char id>];
  11841. BUILDIN_FUNC(getmercinfo)
  11842. {
  11843. int type, char_id;
  11844. struct map_session_data* sd;
  11845. struct mercenary_data* md;
  11846. type = script_getnum(st,2);
  11847. if( script_hasdata(st,3) )
  11848. {
  11849. char_id = script_getnum(st,3);
  11850. if( ( sd = map_charid2sd(char_id) ) == NULL )
  11851. {
  11852. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  11853. script_pushnil(st);
  11854. return 1;
  11855. }
  11856. }
  11857. else
  11858. {
  11859. if( ( sd = script_rid2sd(st) ) == NULL )
  11860. {
  11861. script_pushnil(st);
  11862. return 0;
  11863. }
  11864. }
  11865. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  11866. switch( type )
  11867. {
  11868. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  11869. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  11870. case 2:
  11871. if( md )
  11872. script_pushstrcopy(st,md->db->name);
  11873. else
  11874. script_pushconststr(st,"");
  11875. break;
  11876. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  11877. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  11878. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  11879. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  11880. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  11881. default:
  11882. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  11883. script_pushnil(st);
  11884. return 1;
  11885. }
  11886. return SCRIPT_CMD_SUCCESS;
  11887. }
  11888. /*==========================================
  11889. * Shows wether your inventory(and equips) contain
  11890. selected card or not.
  11891. checkequipedcard(4001);
  11892. *------------------------------------------*/
  11893. BUILDIN_FUNC(checkequipedcard)
  11894. {
  11895. TBL_PC *sd=script_rid2sd(st);
  11896. if(sd){
  11897. int n,i,c=0;
  11898. c=script_getnum(st,2);
  11899. for(i=0;i<MAX_INVENTORY;i++){
  11900. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  11901. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  11902. continue;
  11903. for(n=0;n<sd->inventory_data[i]->slot;n++){
  11904. if(sd->status.inventory[i].card[n]==c){
  11905. script_pushint(st,1);
  11906. return 0;
  11907. }
  11908. }
  11909. }
  11910. }
  11911. }
  11912. script_pushint(st,0);
  11913. return SCRIPT_CMD_SUCCESS;
  11914. }
  11915. BUILDIN_FUNC(jump_zero)
  11916. {
  11917. int sel;
  11918. sel=script_getnum(st,2);
  11919. if(!sel) {
  11920. int pos;
  11921. if( !data_islabel(script_getdata(st,3)) ){
  11922. ShowError("script: jump_zero: not label !\n");
  11923. st->state=END;
  11924. return 1;
  11925. }
  11926. pos=script_getnum(st,3);
  11927. st->pos=pos;
  11928. st->state=GOTO;
  11929. }
  11930. return SCRIPT_CMD_SUCCESS;
  11931. }
  11932. /*==========================================
  11933. * movenpc [MouseJstr]
  11934. *------------------------------------------*/
  11935. BUILDIN_FUNC(movenpc)
  11936. {
  11937. TBL_NPC *nd = NULL;
  11938. const char *npc;
  11939. int x,y;
  11940. npc = script_getstr(st,2);
  11941. x = script_getnum(st,3);
  11942. y = script_getnum(st,4);
  11943. if ((nd = npc_name2id(npc)) == NULL)
  11944. return -1;
  11945. if (script_hasdata(st,5))
  11946. nd->ud.dir = script_getnum(st,5) % 8;
  11947. npc_movenpc(nd, x, y);
  11948. return SCRIPT_CMD_SUCCESS;
  11949. }
  11950. /*==========================================
  11951. * message [MouseJstr]
  11952. *------------------------------------------*/
  11953. BUILDIN_FUNC(message)
  11954. {
  11955. const char *msg,*player;
  11956. TBL_PC *pl_sd = NULL;
  11957. player = script_getstr(st,2);
  11958. msg = script_getstr(st,3);
  11959. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  11960. return 0;
  11961. clif_displaymessage(pl_sd->fd, msg);
  11962. return SCRIPT_CMD_SUCCESS;
  11963. }
  11964. /*==========================================
  11965. * npctalk (sends message to surrounding area)
  11966. *------------------------------------------*/
  11967. BUILDIN_FUNC(npctalk)
  11968. {
  11969. const char* str;
  11970. char name[NAME_LENGTH], message[256];
  11971. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  11972. str = script_getstr(st,2);
  11973. if(nd)
  11974. {
  11975. safestrncpy(name, nd->name, sizeof(name));
  11976. strtok(name, "#"); // discard extra name identifier if present
  11977. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  11978. clif_disp_overhead(&nd->bl, message);
  11979. }
  11980. return SCRIPT_CMD_SUCCESS;
  11981. }
  11982. // change npc walkspeed [Valaris]
  11983. BUILDIN_FUNC(npcspeed)
  11984. {
  11985. struct npc_data* nd;
  11986. int speed;
  11987. speed = script_getnum(st,2);
  11988. nd =(struct npc_data *)map_id2bl(st->oid);
  11989. if( nd ) {
  11990. nd->speed = speed;
  11991. nd->ud.state.speed_changed = 1;
  11992. }
  11993. return SCRIPT_CMD_SUCCESS;
  11994. }
  11995. // make an npc walk to a position [Valaris]
  11996. BUILDIN_FUNC(npcwalkto)
  11997. {
  11998. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11999. int x=0,y=0;
  12000. x=script_getnum(st,2);
  12001. y=script_getnum(st,3);
  12002. if(nd) {
  12003. if (!nd->status.hp)
  12004. status_calc_npc(nd, SCO_FIRST);
  12005. else
  12006. status_calc_npc(nd, SCO_NONE);
  12007. unit_walktoxy(&nd->bl,x,y,0);
  12008. }
  12009. return SCRIPT_CMD_SUCCESS;
  12010. }
  12011. // stop an npc's movement [Valaris]
  12012. BUILDIN_FUNC(npcstop)
  12013. {
  12014. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12015. if(nd) {
  12016. unit_stop_walking(&nd->bl,1|4);
  12017. }
  12018. return SCRIPT_CMD_SUCCESS;
  12019. }
  12020. /*==========================================
  12021. * getlook char info. getlook(arg)
  12022. *------------------------------------------*/
  12023. BUILDIN_FUNC(getlook)
  12024. {
  12025. int type,val;
  12026. TBL_PC *sd;
  12027. sd=script_rid2sd(st);
  12028. type=script_getnum(st,2);
  12029. val=-1;
  12030. switch(type) {
  12031. case LOOK_HAIR: val=sd->status.hair; break; //1
  12032. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12033. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12034. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12035. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12036. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12037. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12038. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12039. case LOOK_SHOES: break; //9
  12040. case LOOK_ROBE: val=sd->status.robe; break; //12
  12041. }
  12042. script_pushint(st,val);
  12043. return SCRIPT_CMD_SUCCESS;
  12044. }
  12045. /*==========================================
  12046. * get char save point. argument: 0- map name, 1- x, 2- y
  12047. *------------------------------------------*/
  12048. BUILDIN_FUNC(getsavepoint)
  12049. {
  12050. TBL_PC* sd;
  12051. int type;
  12052. sd = script_rid2sd(st);
  12053. if (sd == NULL) {
  12054. script_pushint(st,0);
  12055. return 0;
  12056. }
  12057. type = script_getnum(st,2);
  12058. switch(type) {
  12059. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12060. case 1: script_pushint(st,sd->status.save_point.x); break;
  12061. case 2: script_pushint(st,sd->status.save_point.y); break;
  12062. default:
  12063. script_pushint(st,0);
  12064. break;
  12065. }
  12066. return SCRIPT_CMD_SUCCESS;
  12067. }
  12068. /*==========================================
  12069. * Get position for char/npc/pet/mob objects. Added by Lorky
  12070. *
  12071. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  12072. * where type:
  12073. * MapName$ - String variable for output map name
  12074. * MapX - Integer variable for output coord X
  12075. * MapY - Integer variable for output coord Y
  12076. * type - type of object
  12077. * 0 - Character coord
  12078. * 1 - NPC coord
  12079. * 2 - Pet coord
  12080. * 3 - Mob coord (not released)
  12081. * 4 - Homun coord
  12082. * 5 - Mercenary coord
  12083. * 6 - Elemental coord
  12084. * CharName$ - Name object. If miss or "this" the current object
  12085. *
  12086. * Return:
  12087. * 0 - success
  12088. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  12089. *------------------------------------------*/
  12090. BUILDIN_FUNC(getmapxy)
  12091. {
  12092. struct block_list *bl = NULL;
  12093. TBL_PC *sd=NULL;
  12094. int num;
  12095. const char *name;
  12096. char prefix;
  12097. int x,y,type;
  12098. char mapname[MAP_NAME_LENGTH];
  12099. if( !data_isreference(script_getdata(st,2)) ){
  12100. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  12101. script_pushint(st,-1);
  12102. return 1;
  12103. }
  12104. if( !data_isreference(script_getdata(st,3)) ){
  12105. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  12106. script_pushint(st,-1);
  12107. return 1;
  12108. }
  12109. if( !data_isreference(script_getdata(st,4)) ){
  12110. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  12111. script_pushint(st,-1);
  12112. return 1;
  12113. }
  12114. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12115. type=script_getnum(st,5);
  12116. switch (type){
  12117. case 0: //Get Character Position
  12118. if( script_hasdata(st,6) )
  12119. sd=map_nick2sd(script_getstr(st,6));
  12120. else
  12121. sd=script_rid2sd(st);
  12122. if (sd)
  12123. bl = &sd->bl;
  12124. break;
  12125. case 1: //Get NPC Position
  12126. if( script_hasdata(st,6) )
  12127. {
  12128. struct npc_data *nd;
  12129. nd=npc_name2id(script_getstr(st,6));
  12130. if (nd)
  12131. bl = &nd->bl;
  12132. } else //In case the origin is not an npc?
  12133. bl=map_id2bl(st->oid);
  12134. break;
  12135. case 2: //Get Pet Position
  12136. if(script_hasdata(st,6))
  12137. sd=map_nick2sd(script_getstr(st,6));
  12138. else
  12139. sd=script_rid2sd(st);
  12140. if (sd && sd->pd)
  12141. bl = &sd->pd->bl;
  12142. break;
  12143. case 3: //Get Mob Position
  12144. break; //Not supported?
  12145. case 4: //Get Homun Position
  12146. if(script_hasdata(st,6))
  12147. sd=map_nick2sd(script_getstr(st,6));
  12148. else
  12149. sd=script_rid2sd(st);
  12150. if (sd && sd->hd)
  12151. bl = &sd->hd->bl;
  12152. break;
  12153. case 5: //Get Mercenary Position
  12154. if(script_hasdata(st,6))
  12155. sd=map_nick2sd(script_getstr(st,6));
  12156. else
  12157. sd=script_rid2sd(st);
  12158. if (sd && sd->md)
  12159. bl = &sd->md->bl;
  12160. break;
  12161. case 6: //Get Elemental Position
  12162. if(script_hasdata(st,6))
  12163. sd=map_nick2sd(script_getstr(st,6));
  12164. else
  12165. sd=script_rid2sd(st);
  12166. if (sd && sd->ed)
  12167. bl = &sd->ed->bl;
  12168. break;
  12169. default:
  12170. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  12171. script_pushint(st,-1);
  12172. return 1;
  12173. }
  12174. if (!bl) { //No object found.
  12175. script_pushint(st,-1);
  12176. return 0;
  12177. }
  12178. x= bl->x;
  12179. y= bl->y;
  12180. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12181. //Set MapName$
  12182. num=st->stack->stack_data[st->start+2].u.num;
  12183. name=get_str(num&0x00ffffff);
  12184. prefix=*name;
  12185. if(not_server_variable(prefix))
  12186. sd=script_rid2sd(st);
  12187. else
  12188. sd=NULL;
  12189. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12190. //Set MapX
  12191. num=st->stack->stack_data[st->start+3].u.num;
  12192. name=get_str(num&0x00ffffff);
  12193. prefix=*name;
  12194. if(not_server_variable(prefix))
  12195. sd=script_rid2sd(st);
  12196. else
  12197. sd=NULL;
  12198. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12199. //Set MapY
  12200. num=st->stack->stack_data[st->start+4].u.num;
  12201. name=get_str(num&0x00ffffff);
  12202. prefix=*name;
  12203. if(not_server_variable(prefix))
  12204. sd=script_rid2sd(st);
  12205. else
  12206. sd=NULL;
  12207. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12208. //Return Success value
  12209. script_pushint(st,0);
  12210. return SCRIPT_CMD_SUCCESS;
  12211. }
  12212. /*==========================================
  12213. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12214. *------------------------------------------*/
  12215. BUILDIN_FUNC(logmes)
  12216. {
  12217. const char *str;
  12218. TBL_PC* sd;
  12219. sd = script_rid2sd(st);
  12220. if( sd == NULL )
  12221. return 1;
  12222. str = script_getstr(st,2);
  12223. log_npc(sd,str);
  12224. return SCRIPT_CMD_SUCCESS;
  12225. }
  12226. BUILDIN_FUNC(summon)
  12227. {
  12228. int _class, timeout=0;
  12229. const char *str,*event="";
  12230. TBL_PC *sd;
  12231. struct mob_data *md;
  12232. int tick = gettick();
  12233. sd=script_rid2sd(st);
  12234. if (!sd) return 0;
  12235. str =script_getstr(st,2);
  12236. _class=script_getnum(st,3);
  12237. if( script_hasdata(st,4) )
  12238. timeout=script_getnum(st,4);
  12239. if( script_hasdata(st,5) ){
  12240. event=script_getstr(st,5);
  12241. check_event(st, event);
  12242. }
  12243. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12244. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12245. if (md) {
  12246. md->master_id=sd->bl.id;
  12247. md->special_state.ai = AI_ATTACK;
  12248. if( md->deletetimer != INVALID_TIMER )
  12249. delete_timer(md->deletetimer, mob_timer_delete);
  12250. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12251. mob_spawn (md); //Now it is ready for spawning.
  12252. clif_specialeffect(&md->bl,344,AREA);
  12253. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12254. }
  12255. return SCRIPT_CMD_SUCCESS;
  12256. }
  12257. /*==========================================
  12258. * Checks whether it is daytime/nighttime
  12259. *------------------------------------------*/
  12260. BUILDIN_FUNC(isnight)
  12261. {
  12262. script_pushint(st,(night_flag == 1));
  12263. return SCRIPT_CMD_SUCCESS;
  12264. }
  12265. BUILDIN_FUNC(isday)
  12266. {
  12267. script_pushint(st,(night_flag == 0));
  12268. return SCRIPT_CMD_SUCCESS;
  12269. }
  12270. /*================================================
  12271. * Check how many items/cards in the list are
  12272. * equipped - used for 2/15's cards patch [celest]
  12273. *------------------------------------------------*/
  12274. BUILDIN_FUNC(isequippedcnt)
  12275. {
  12276. TBL_PC *sd;
  12277. int i, j, k, id = 1;
  12278. int ret = 0;
  12279. sd = script_rid2sd(st);
  12280. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12281. script_pushint(st,0);
  12282. return 0;
  12283. }
  12284. for (i=0; id!=0; i++) {
  12285. FETCH (i+2, id) else id = 0;
  12286. if (id <= 0)
  12287. continue;
  12288. for (j=0; j<EQI_MAX; j++) {
  12289. int index;
  12290. index = sd->equip_index[j];
  12291. if(index < 0) continue;
  12292. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  12293. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  12294. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  12295. if(!sd->inventory_data[index])
  12296. continue;
  12297. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  12298. if (sd->inventory_data[index]->nameid == id)
  12299. ret+= sd->status.inventory[index].amount;
  12300. } else { //Count cards.
  12301. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12302. continue; //No cards
  12303. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12304. if (sd->status.inventory[index].card[k] == id)
  12305. ret++; //[Lupus]
  12306. }
  12307. }
  12308. }
  12309. }
  12310. script_pushint(st,ret);
  12311. return SCRIPT_CMD_SUCCESS;
  12312. }
  12313. /*================================================
  12314. * Check whether another card has been
  12315. * equipped - used for 2/15's cards patch [celest]
  12316. * -- Items checked cannot be reused in another
  12317. * card set to prevent exploits
  12318. *------------------------------------------------*/
  12319. BUILDIN_FUNC(isequipped)
  12320. {
  12321. TBL_PC *sd;
  12322. int i, j, k, id = 1;
  12323. int index, flag;
  12324. int ret = -1;
  12325. //Original hash to reverse it when full check fails.
  12326. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  12327. sd = script_rid2sd(st);
  12328. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12329. script_pushint(st,0);
  12330. return 0;
  12331. }
  12332. setitem_hash = sd->bonus.setitem_hash;
  12333. setitem_hash2 = sd->bonus.setitem_hash2;
  12334. for (i=0; id!=0; i++) {
  12335. FETCH (i+2, id) else id = 0;
  12336. if (id <= 0)
  12337. continue;
  12338. flag = 0;
  12339. for (j=0; j<EQI_MAX; j++) {
  12340. index = sd->equip_index[j];
  12341. if(index < 0) continue;
  12342. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  12343. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  12344. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  12345. if(!sd->inventory_data[index])
  12346. continue;
  12347. if (itemdb_type(id) != IT_CARD) {
  12348. if (sd->inventory_data[index]->nameid != id)
  12349. continue;
  12350. flag = 1;
  12351. break;
  12352. } else { //Cards
  12353. if (sd->inventory_data[index]->slot == 0 ||
  12354. itemdb_isspecial(sd->status.inventory[index].card[0]))
  12355. continue;
  12356. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  12357. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  12358. unsigned int hash = 0;
  12359. if (sd->status.inventory[index].card[k] != id)
  12360. continue;
  12361. hash = 1<<((j<5?j:j-5)*4 + k);
  12362. // check if card is already used by another set
  12363. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  12364. continue;
  12365. // We have found a match
  12366. flag = 1;
  12367. // Set hash so this card cannot be used by another
  12368. if (j<5)
  12369. sd->bonus.setitem_hash |= hash;
  12370. else
  12371. sd->bonus.setitem_hash2 |= hash;
  12372. break;
  12373. }
  12374. }
  12375. if (flag) break; //Card found
  12376. }
  12377. if (ret == -1)
  12378. ret = flag;
  12379. else
  12380. ret &= flag;
  12381. if (!ret) break;
  12382. }
  12383. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  12384. sd->bonus.setitem_hash = setitem_hash;
  12385. sd->bonus.setitem_hash2 = setitem_hash2;
  12386. }
  12387. script_pushint(st,ret);
  12388. return SCRIPT_CMD_SUCCESS;
  12389. }
  12390. /*================================================
  12391. * Check how many given inserted cards in the CURRENT
  12392. * weapon - used for 2/15's cards patch [Lupus]
  12393. *------------------------------------------------*/
  12394. BUILDIN_FUNC(cardscnt)
  12395. {
  12396. TBL_PC *sd;
  12397. int i, k, id = 1;
  12398. int ret = 0;
  12399. int index;
  12400. sd = script_rid2sd(st);
  12401. for (i=0; id!=0; i++) {
  12402. FETCH (i+2, id) else id = 0;
  12403. if (id <= 0)
  12404. continue;
  12405. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  12406. if(index < 0) continue;
  12407. if(!sd->inventory_data[index])
  12408. continue;
  12409. if(itemdb_type(id) != IT_CARD) {
  12410. if (sd->inventory_data[index]->nameid == id)
  12411. ret+= sd->status.inventory[index].amount;
  12412. } else {
  12413. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12414. continue;
  12415. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12416. if (sd->status.inventory[index].card[k] == id)
  12417. ret++;
  12418. }
  12419. }
  12420. }
  12421. script_pushint(st,ret);
  12422. // script_pushint(st,current_equip_item_index);
  12423. return SCRIPT_CMD_SUCCESS;
  12424. }
  12425. /*=======================================================
  12426. * Returns the refined number of the current item, or an
  12427. * item with inventory index specified
  12428. *-------------------------------------------------------*/
  12429. BUILDIN_FUNC(getrefine)
  12430. {
  12431. TBL_PC *sd;
  12432. if ((sd = script_rid2sd(st))!= NULL)
  12433. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  12434. else
  12435. script_pushint(st,0);
  12436. return SCRIPT_CMD_SUCCESS;
  12437. }
  12438. /*=======================================================
  12439. * Day/Night controls
  12440. *-------------------------------------------------------*/
  12441. BUILDIN_FUNC(night)
  12442. {
  12443. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  12444. return SCRIPT_CMD_SUCCESS;
  12445. }
  12446. BUILDIN_FUNC(day)
  12447. {
  12448. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  12449. return SCRIPT_CMD_SUCCESS;
  12450. }
  12451. //=======================================================
  12452. // Unequip [Spectre]
  12453. //-------------------------------------------------------
  12454. BUILDIN_FUNC(unequip)
  12455. {
  12456. int i;
  12457. size_t num;
  12458. TBL_PC *sd;
  12459. num = script_getnum(st,2);
  12460. sd = script_rid2sd(st);
  12461. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  12462. {
  12463. i = pc_checkequip(sd,equip[num-1]);
  12464. if (i >= 0)
  12465. pc_unequipitem(sd,i,1|2);
  12466. }
  12467. return SCRIPT_CMD_SUCCESS;
  12468. }
  12469. BUILDIN_FUNC(equip)
  12470. {
  12471. unsigned short nameid = 0;
  12472. int i;
  12473. TBL_PC *sd;
  12474. struct item_data *item_data;
  12475. sd = script_rid2sd(st);
  12476. nameid=script_getnum(st,2);
  12477. if((item_data = itemdb_exists(nameid)) == NULL)
  12478. {
  12479. ShowError("wrong item ID : equipitem(%hu)\n",nameid);
  12480. return 1;
  12481. }
  12482. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  12483. if( i < MAX_INVENTORY )
  12484. pc_equipitem(sd,i,item_data->equip);
  12485. return SCRIPT_CMD_SUCCESS;
  12486. }
  12487. BUILDIN_FUNC(autoequip)
  12488. {
  12489. unsigned short nameid;
  12490. int flag;
  12491. struct item_data *item_data;
  12492. nameid=script_getnum(st,2);
  12493. flag=script_getnum(st,3);
  12494. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  12495. {
  12496. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  12497. return SCRIPT_CMD_FAILURE;
  12498. }
  12499. if( !itemdb_isequip2(item_data) )
  12500. {
  12501. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  12502. return SCRIPT_CMD_FAILURE;
  12503. }
  12504. item_data->flag.autoequip = flag>0?1:0;
  12505. return SCRIPT_CMD_SUCCESS;
  12506. }
  12507. BUILDIN_FUNC(setbattleflag)
  12508. {
  12509. const char *flag, *value;
  12510. flag = script_getstr(st,2);
  12511. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  12512. if (battle_set_value(flag, value) == 0)
  12513. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  12514. else
  12515. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  12516. return SCRIPT_CMD_SUCCESS;
  12517. }
  12518. BUILDIN_FUNC(getbattleflag)
  12519. {
  12520. const char *flag;
  12521. flag = script_getstr(st,2);
  12522. script_pushint(st,battle_get_value(flag));
  12523. return SCRIPT_CMD_SUCCESS;
  12524. }
  12525. //=======================================================
  12526. // strlen [Valaris]
  12527. //-------------------------------------------------------
  12528. BUILDIN_FUNC(getstrlen)
  12529. {
  12530. const char *str = script_getstr(st,2);
  12531. int len = (str) ? (int)strlen(str) : 0;
  12532. script_pushint(st,len);
  12533. return SCRIPT_CMD_SUCCESS;
  12534. }
  12535. //=======================================================
  12536. // isalpha [Valaris]
  12537. //-------------------------------------------------------
  12538. BUILDIN_FUNC(charisalpha)
  12539. {
  12540. const char *str=script_getstr(st,2);
  12541. int pos=script_getnum(st,3);
  12542. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  12543. script_pushint(st,val);
  12544. return SCRIPT_CMD_SUCCESS;
  12545. }
  12546. //=======================================================
  12547. // charisupper <str>, <index>
  12548. //-------------------------------------------------------
  12549. BUILDIN_FUNC(charisupper)
  12550. {
  12551. const char *str = script_getstr(st,2);
  12552. int pos = script_getnum(st,3);
  12553. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  12554. script_pushint(st,val);
  12555. return SCRIPT_CMD_SUCCESS;
  12556. }
  12557. //=======================================================
  12558. // charislower <str>, <index>
  12559. //-------------------------------------------------------
  12560. BUILDIN_FUNC(charislower)
  12561. {
  12562. const char *str = script_getstr(st,2);
  12563. int pos = script_getnum(st,3);
  12564. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  12565. script_pushint(st,val);
  12566. return SCRIPT_CMD_SUCCESS;
  12567. }
  12568. //=======================================================
  12569. // charat <str>, <index>
  12570. //-------------------------------------------------------
  12571. BUILDIN_FUNC(charat) {
  12572. const char *str = script_getstr(st,2);
  12573. int pos = script_getnum(st,3);
  12574. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  12575. char output[2];
  12576. output[0] = str[pos];
  12577. output[1] = '\0';
  12578. script_pushstrcopy(st, output);
  12579. } else
  12580. script_pushconststr(st, "");
  12581. return SCRIPT_CMD_SUCCESS;
  12582. }
  12583. //=======================================================
  12584. // setchar <string>, <char>, <index>
  12585. //-------------------------------------------------------
  12586. BUILDIN_FUNC(setchar)
  12587. {
  12588. const char *str = script_getstr(st,2);
  12589. const char *c = script_getstr(st,3);
  12590. int index = script_getnum(st,4);
  12591. char *output = aStrdup(str);
  12592. if(index >= 0 && index < strlen(output))
  12593. output[index] = *c;
  12594. script_pushstr(st, output);
  12595. return SCRIPT_CMD_SUCCESS;
  12596. }
  12597. //=======================================================
  12598. // insertchar <string>, <char>, <index>
  12599. //-------------------------------------------------------
  12600. BUILDIN_FUNC(insertchar)
  12601. {
  12602. const char *str = script_getstr(st,2);
  12603. const char *c = script_getstr(st,3);
  12604. int index = script_getnum(st,4);
  12605. char *output;
  12606. size_t len = strlen(str);
  12607. if(index < 0)
  12608. index = 0;
  12609. else if(index > len)
  12610. index = len;
  12611. output = (char*)aMalloc(len + 2);
  12612. memcpy(output, str, index);
  12613. output[index] = c[0];
  12614. memcpy(&output[index+1], &str[index], len - index);
  12615. output[len+1] = '\0';
  12616. script_pushstr(st, output);
  12617. return SCRIPT_CMD_SUCCESS;
  12618. }
  12619. //=======================================================
  12620. // delchar <string>, <index>
  12621. //-------------------------------------------------------
  12622. BUILDIN_FUNC(delchar)
  12623. {
  12624. const char *str = script_getstr(st,2);
  12625. int index = script_getnum(st,3);
  12626. char *output;
  12627. size_t len = strlen(str);
  12628. if(index < 0 || index > len) {
  12629. //return original
  12630. output = aStrdup(str);
  12631. script_pushstr(st, output);
  12632. return 0;
  12633. }
  12634. output = (char*)aMalloc(len);
  12635. memcpy(output, str, index);
  12636. memcpy(&output[index], &str[index+1], len - index);
  12637. script_pushstr(st, output);
  12638. return SCRIPT_CMD_SUCCESS;
  12639. }
  12640. //=======================================================
  12641. // strtoupper <str>
  12642. //-------------------------------------------------------
  12643. BUILDIN_FUNC(strtoupper)
  12644. {
  12645. const char *str = script_getstr(st,2);
  12646. char *output = aStrdup(str);
  12647. char *cursor = output;
  12648. while (*cursor != '\0') {
  12649. *cursor = TOUPPER(*cursor);
  12650. cursor++;
  12651. }
  12652. script_pushstr(st, output);
  12653. return SCRIPT_CMD_SUCCESS;
  12654. }
  12655. //=======================================================
  12656. // strtolower <str>
  12657. //-------------------------------------------------------
  12658. BUILDIN_FUNC(strtolower)
  12659. {
  12660. const char *str = script_getstr(st,2);
  12661. char *output = aStrdup(str);
  12662. char *cursor = output;
  12663. while (*cursor != '\0') {
  12664. *cursor = TOLOWER(*cursor);
  12665. cursor++;
  12666. }
  12667. script_pushstr(st, output);
  12668. return SCRIPT_CMD_SUCCESS;
  12669. }
  12670. //=======================================================
  12671. // substr <str>, <start>, <end>
  12672. //-------------------------------------------------------
  12673. BUILDIN_FUNC(substr)
  12674. {
  12675. const char *str = script_getstr(st,2);
  12676. char *output;
  12677. int start = script_getnum(st,3);
  12678. int end = script_getnum(st,4);
  12679. int len = 0;
  12680. if(start >= 0 && end < strlen(str) && start <= end) {
  12681. len = end - start + 1;
  12682. output = (char*)aMalloc(len + 1);
  12683. memcpy(output, &str[start], len);
  12684. } else
  12685. output = (char*)aMalloc(1);
  12686. output[len] = '\0';
  12687. script_pushstr(st, output);
  12688. return SCRIPT_CMD_SUCCESS;
  12689. }
  12690. //=======================================================
  12691. // explode <dest_string_array>, <str>, <delimiter>
  12692. // Note: delimiter is limited to 1 char
  12693. //-------------------------------------------------------
  12694. BUILDIN_FUNC(explode)
  12695. {
  12696. struct script_data* data = script_getdata(st, 2);
  12697. const char *str = script_getstr(st,3);
  12698. const char delimiter = script_getstr(st, 4)[0];
  12699. int32 id;
  12700. size_t len = strlen(str);
  12701. int i = 0, j = 0;
  12702. int start;
  12703. char *temp;
  12704. const char* name;
  12705. TBL_PC* sd = NULL;
  12706. temp = (char*)aMalloc(len + 1);
  12707. if( !data_isreference(data) )
  12708. {
  12709. ShowError("script:explode: not a variable\n");
  12710. script_reportdata(data);
  12711. st->state = END;
  12712. return 1;// not a variable
  12713. }
  12714. id = reference_getid(data);
  12715. start = reference_getindex(data);
  12716. name = reference_getname(data);
  12717. if( not_array_variable(*name) )
  12718. {
  12719. ShowError("script:explode: illegal scope\n");
  12720. script_reportdata(data);
  12721. st->state = END;
  12722. return 1;// not supported
  12723. }
  12724. if( !is_string_variable(name) )
  12725. {
  12726. ShowError("script:explode: not string array\n");
  12727. script_reportdata(data);
  12728. st->state = END;
  12729. return 1;// data type mismatch
  12730. }
  12731. if( not_server_variable(*name) )
  12732. {
  12733. sd = script_rid2sd(st);
  12734. if( sd == NULL )
  12735. return 0;// no player attached
  12736. }
  12737. while(str[i] != '\0') {
  12738. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  12739. temp[j] = '\0';
  12740. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  12741. j = 0;
  12742. ++i;
  12743. } else {
  12744. temp[j++] = str[i++];
  12745. }
  12746. }
  12747. //set last string
  12748. temp[j] = '\0';
  12749. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  12750. aFree(temp);
  12751. return SCRIPT_CMD_SUCCESS;
  12752. }
  12753. //=======================================================
  12754. // implode <string_array>
  12755. // implode <string_array>, <glue>
  12756. //-------------------------------------------------------
  12757. BUILDIN_FUNC(implode)
  12758. {
  12759. struct script_data* data = script_getdata(st, 2);
  12760. const char *glue = NULL, *name, *temp;
  12761. int32 glue_len = 0, array_size, id;
  12762. size_t len = 0;
  12763. int i, k = 0;
  12764. TBL_PC* sd = NULL;
  12765. char *output;
  12766. if( !data_isreference(data) )
  12767. {
  12768. ShowError("script:implode: not a variable\n");
  12769. script_reportdata(data);
  12770. st->state = END;
  12771. return 1;// not a variable
  12772. }
  12773. id = reference_getid(data);
  12774. name = reference_getname(data);
  12775. if( not_array_variable(*name) )
  12776. {
  12777. ShowError("script:implode: illegal scope\n");
  12778. script_reportdata(data);
  12779. st->state = END;
  12780. return 1;// not supported
  12781. }
  12782. if( !is_string_variable(name) )
  12783. {
  12784. ShowError("script:implode: not string array\n");
  12785. script_reportdata(data);
  12786. st->state = END;
  12787. return 1;// data type mismatch
  12788. }
  12789. if( not_server_variable(*name) )
  12790. {
  12791. sd = script_rid2sd(st);
  12792. if( sd == NULL )
  12793. return 0;// no player attached
  12794. }
  12795. //count chars
  12796. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  12797. if(array_size == -1) //empty array check (AmsTaff)
  12798. {
  12799. ShowWarning("script:implode: array length = 0\n");
  12800. output = (char*)aMalloc(sizeof(char)*5);
  12801. sprintf(output,"%s","NULL");
  12802. } else {
  12803. for(i = 0; i <= array_size; ++i) {
  12804. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12805. len += strlen(temp);
  12806. script_removetop(st, -1, 0);
  12807. }
  12808. //allocate mem
  12809. if( script_hasdata(st,3) ) {
  12810. glue = script_getstr(st,3);
  12811. glue_len = strlen(glue);
  12812. len += glue_len * (array_size);
  12813. }
  12814. output = (char*)aMalloc(len + 1);
  12815. //build output
  12816. for(i = 0; i < array_size; ++i) {
  12817. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12818. len = strlen(temp);
  12819. memcpy(&output[k], temp, len);
  12820. k += len;
  12821. if(glue_len != 0) {
  12822. memcpy(&output[k], glue, glue_len);
  12823. k += glue_len;
  12824. }
  12825. script_removetop(st, -1, 0);
  12826. }
  12827. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  12828. len = strlen(temp);
  12829. memcpy(&output[k], temp, len);
  12830. k += len;
  12831. script_removetop(st, -1, 0);
  12832. output[k] = '\0';
  12833. }
  12834. script_pushstr(st, output);
  12835. return SCRIPT_CMD_SUCCESS;
  12836. }
  12837. //=======================================================
  12838. // sprintf(<format>, ...);
  12839. // Implements C sprintf, except format %n. The resulting string is
  12840. // returned, instead of being saved in variable by reference.
  12841. //-------------------------------------------------------
  12842. BUILDIN_FUNC(sprintf)
  12843. {
  12844. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  12845. const char* format;
  12846. char* p;
  12847. char* q;
  12848. char* buf = NULL;
  12849. char* buf2 = NULL;
  12850. struct script_data* data;
  12851. StringBuf final_buf;
  12852. // Fetch init data
  12853. format = script_getstr(st, 2);
  12854. argc = script_lastdata(st)-2;
  12855. len = strlen(format);
  12856. // Skip parsing, where no parsing is required.
  12857. if(len==0){
  12858. script_pushconststr(st,"");
  12859. return 0;
  12860. }
  12861. // Pessimistic alloc
  12862. CREATE(buf, char, len+1);
  12863. // Need not be parsed, just solve stuff like %%.
  12864. if(argc==0){
  12865. memcpy(buf,format,len+1);
  12866. script_pushstrcopy(st, buf);
  12867. aFree(buf);
  12868. return 0;
  12869. }
  12870. safestrncpy(buf, format, len+1);
  12871. // Issue sprintf for each parameter
  12872. StringBuf_Init(&final_buf);
  12873. q = buf;
  12874. while((p = strchr(q, '%'))!=NULL){
  12875. if(p!=q){
  12876. len = p-q+1;
  12877. if(buf2_len<len){
  12878. RECREATE(buf2, char, len);
  12879. buf2_len = len;
  12880. }
  12881. safestrncpy(buf2, q, len);
  12882. StringBuf_AppendStr(&final_buf, buf2);
  12883. q = p;
  12884. }
  12885. p = q+1;
  12886. if(*p=='%'){ // %%
  12887. StringBuf_AppendStr(&final_buf, "%");
  12888. q+=2;
  12889. continue;
  12890. }
  12891. if(*p=='n'){ // %n
  12892. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  12893. script_reportsrc(st);
  12894. q+=2;
  12895. continue;
  12896. }
  12897. if(arg>=argc){
  12898. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  12899. if(buf) aFree(buf);
  12900. if(buf2) aFree(buf2);
  12901. StringBuf_Destroy(&final_buf);
  12902. script_pushconststr(st,"");
  12903. return 1;
  12904. }
  12905. if((p = strchr(q+1, '%'))==NULL){
  12906. p = strchr(q, 0); // EOS
  12907. }
  12908. len = p-q+1;
  12909. if(buf2_len<len){
  12910. RECREATE(buf2, char, len);
  12911. buf2_len = len;
  12912. }
  12913. safestrncpy(buf2, q, len);
  12914. q = p;
  12915. // Note: This assumes the passed value being the correct
  12916. // type to the current format specifier. If not, the server
  12917. // probably crashes or returns anything else, than expected,
  12918. // but it would behave in normal code the same way so it's
  12919. // the scripter's responsibility.
  12920. data = script_getdata(st, arg+3);
  12921. if(data_isstring(data)){ // String
  12922. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12923. }else if(data_isint(data)){ // Number
  12924. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12925. }else if(data_isreference(data)){ // Variable
  12926. char* name = reference_getname(data);
  12927. if(name[strlen(name)-1]=='$'){ // var Str
  12928. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12929. }else{ // var Int
  12930. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12931. }
  12932. }else{ // Unsupported type
  12933. ShowError("buildin_sprintf: Unknown argument type!\n");
  12934. if(buf) aFree(buf);
  12935. if(buf2) aFree(buf2);
  12936. StringBuf_Destroy(&final_buf);
  12937. script_pushconststr(st,"");
  12938. return 1;
  12939. }
  12940. arg++;
  12941. }
  12942. // Append anything left
  12943. if(*q){
  12944. StringBuf_AppendStr(&final_buf, q);
  12945. }
  12946. // Passed more, than needed
  12947. if(arg<argc){
  12948. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  12949. script_reportsrc(st);
  12950. }
  12951. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  12952. if(buf) aFree(buf);
  12953. if(buf2) aFree(buf2);
  12954. StringBuf_Destroy(&final_buf);
  12955. return SCRIPT_CMD_SUCCESS;
  12956. }
  12957. //=======================================================
  12958. // sscanf(<str>, <format>, ...);
  12959. // Implements C sscanf.
  12960. //-------------------------------------------------------
  12961. BUILDIN_FUNC(sscanf){
  12962. unsigned int argc, arg = 0, len;
  12963. struct script_data* data;
  12964. struct map_session_data* sd = NULL;
  12965. const char* str;
  12966. const char* format;
  12967. const char* p;
  12968. const char* q;
  12969. char* buf = NULL;
  12970. char* buf_p;
  12971. char* ref_str = NULL;
  12972. int ref_int;
  12973. // Get data
  12974. str = script_getstr(st, 2);
  12975. format = script_getstr(st, 3);
  12976. argc = script_lastdata(st)-3;
  12977. len = strlen(format);
  12978. if (len != 0 && strlen(str) == 0) {
  12979. // If the source string is empty but the format string is not, we return -1
  12980. // according to the C specs. (if the format string is also empty, we shall
  12981. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  12982. script_pushint(st, -1);
  12983. return SCRIPT_CMD_SUCCESS;
  12984. }
  12985. CREATE(buf, char, len*2+1);
  12986. // Issue sscanf for each parameter
  12987. *buf = 0;
  12988. q = format;
  12989. while((p = strchr(q, '%'))){
  12990. if(p!=q){
  12991. strncat(buf, q, (size_t)(p-q));
  12992. q = p;
  12993. }
  12994. p = q+1;
  12995. if(*p=='*' || *p=='%'){ // Skip
  12996. strncat(buf, q, 2);
  12997. q+=2;
  12998. continue;
  12999. }
  13000. if(arg>=argc){
  13001. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13002. script_pushint(st, -1);
  13003. if(buf) aFree(buf);
  13004. if(ref_str) aFree(ref_str);
  13005. return 1;
  13006. }
  13007. if((p = strchr(q+1, '%'))==NULL){
  13008. p = strchr(q, 0); // EOS
  13009. }
  13010. len = p-q;
  13011. strncat(buf, q, len);
  13012. q = p;
  13013. // Validate output
  13014. data = script_getdata(st, arg+4);
  13015. if(!data_isreference(data) || !reference_tovariable(data)){
  13016. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13017. script_pushint(st, -1);
  13018. if(buf) aFree(buf);
  13019. if(ref_str) aFree(ref_str);
  13020. return 1;
  13021. }
  13022. buf_p = reference_getname(data);
  13023. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13024. script_pushint(st, -1);
  13025. if(buf) aFree(buf);
  13026. if(ref_str) aFree(ref_str);
  13027. return 0;
  13028. }
  13029. // Save value if any
  13030. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13031. if(ref_str==NULL){
  13032. CREATE(ref_str, char, strlen(str)+1);
  13033. }
  13034. if(sscanf(str, buf, ref_str)==0){
  13035. break;
  13036. }
  13037. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13038. } else { // Number
  13039. if(sscanf(str, buf, &ref_int)==0){
  13040. break;
  13041. }
  13042. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13043. }
  13044. arg++;
  13045. // Disable used format (%... -> %*...)
  13046. buf_p = strchr(buf, 0);
  13047. memmove(buf_p-len+2, buf_p-len+1, len);
  13048. *(buf_p-len+1) = '*';
  13049. }
  13050. script_pushint(st, arg);
  13051. if(buf) aFree(buf);
  13052. if(ref_str) aFree(ref_str);
  13053. return SCRIPT_CMD_SUCCESS;
  13054. }
  13055. //=======================================================
  13056. // strpos(<haystack>, <needle>)
  13057. // strpos(<haystack>, <needle>, <offset>)
  13058. //
  13059. // Implements PHP style strpos. Adapted from code from
  13060. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13061. //-------------------------------------------------------
  13062. BUILDIN_FUNC(strpos) {
  13063. const char *haystack = script_getstr(st,2);
  13064. const char *needle = script_getstr(st,3);
  13065. int i;
  13066. size_t len;
  13067. if( script_hasdata(st,4) )
  13068. i = script_getnum(st,4);
  13069. else
  13070. i = 0;
  13071. if (needle[0] == '\0') {
  13072. script_pushint(st, -1);
  13073. return 0;
  13074. }
  13075. len = strlen(haystack);
  13076. for ( ; i < len; ++i ) {
  13077. if ( haystack[i] == *needle ) {
  13078. // matched starting char -- loop through remaining chars
  13079. const char *h, *n;
  13080. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13081. if ( *h != *n ) {
  13082. break;
  13083. }
  13084. }
  13085. if ( !*n ) { // matched all of 'needle' to null termination
  13086. script_pushint(st, i);
  13087. return 0;
  13088. }
  13089. }
  13090. }
  13091. script_pushint(st, -1);
  13092. return SCRIPT_CMD_SUCCESS;
  13093. }
  13094. //===============================================================
  13095. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13096. //
  13097. // Note: Finds all instances of <search> in <input> and replaces
  13098. // with <replace>. If specified will only replace as many
  13099. // instances as specified in <count>. By default will be case
  13100. // sensitive.
  13101. //---------------------------------------------------------------
  13102. BUILDIN_FUNC(replacestr)
  13103. {
  13104. const char *input = script_getstr(st, 2);
  13105. const char *find = script_getstr(st, 3);
  13106. const char *replace = script_getstr(st, 4);
  13107. size_t inputlen = strlen(input);
  13108. size_t findlen = strlen(find);
  13109. struct StringBuf output;
  13110. bool usecase = true;
  13111. int count = 0;
  13112. int numFinds = 0;
  13113. int i = 0, f = 0;
  13114. if(findlen == 0) {
  13115. ShowError("script:replacestr: Invalid search length.\n");
  13116. st->state = END;
  13117. return 1;
  13118. }
  13119. if(script_hasdata(st, 5)) {
  13120. struct script_data *data = script_getdata(st, 5);
  13121. get_val(st, data); // Convert into value in case of a variable
  13122. if( !data_isstring(data) )
  13123. usecase = script_getnum(st, 5) != 0;
  13124. else {
  13125. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13126. st->state = END;
  13127. return 1;
  13128. }
  13129. }
  13130. if(script_hasdata(st, 6)) {
  13131. count = script_getnum(st, 6);
  13132. if(count == 0) {
  13133. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13134. st->state = END;
  13135. return 1;
  13136. }
  13137. }
  13138. StringBuf_Init(&output);
  13139. for(; i < inputlen; i++) {
  13140. if(count && count == numFinds) { //found enough, stop looking
  13141. break;
  13142. }
  13143. for(f = 0; f <= findlen; f++) {
  13144. if(f == findlen) { //complete match
  13145. numFinds++;
  13146. StringBuf_AppendStr(&output, replace);
  13147. i += findlen - 1;
  13148. break;
  13149. } else {
  13150. if(usecase) {
  13151. if((i + f) > inputlen || input[i + f] != find[f]) {
  13152. StringBuf_Printf(&output, "%c", input[i]);
  13153. break;
  13154. }
  13155. } else {
  13156. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13157. StringBuf_Printf(&output, "%c", input[i]);
  13158. break;
  13159. }
  13160. }
  13161. }
  13162. }
  13163. }
  13164. //append excess after enough found
  13165. if(i < inputlen)
  13166. StringBuf_AppendStr(&output, &(input[i]));
  13167. script_pushstrcopy(st, StringBuf_Value(&output));
  13168. StringBuf_Destroy(&output);
  13169. return SCRIPT_CMD_SUCCESS;
  13170. }
  13171. //========================================================
  13172. // countstr <input>, <search>{, <usecase>}
  13173. //
  13174. // Note: Counts the number of times <search> occurs in
  13175. // <input>. By default will be case sensitive.
  13176. //--------------------------------------------------------
  13177. BUILDIN_FUNC(countstr)
  13178. {
  13179. const char *input = script_getstr(st, 2);
  13180. const char *find = script_getstr(st, 3);
  13181. size_t inputlen = strlen(input);
  13182. size_t findlen = strlen(find);
  13183. bool usecase = true;
  13184. int numFinds = 0;
  13185. int i = 0, f = 0;
  13186. if(findlen == 0) {
  13187. ShowError("script:countstr: Invalid search length.\n");
  13188. st->state = END;
  13189. return 1;
  13190. }
  13191. if(script_hasdata(st, 4)) {
  13192. struct script_data *data;
  13193. data = script_getdata(st, 4);
  13194. get_val(st, data); // Convert into value in case of a variable
  13195. if( !data_isstring(data) )
  13196. usecase = script_getnum(st, 4) != 0;
  13197. else {
  13198. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13199. st->state = END;
  13200. return 1;
  13201. }
  13202. }
  13203. for(; i < inputlen; i++) {
  13204. for(f = 0; f <= findlen; f++) {
  13205. if(f == findlen) { //complete match
  13206. numFinds++;
  13207. i += findlen - 1;
  13208. break;
  13209. } else {
  13210. if(usecase) {
  13211. if((i + f) > inputlen || input[i + f] != find[f]) {
  13212. break;
  13213. }
  13214. } else {
  13215. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13216. break;
  13217. }
  13218. }
  13219. }
  13220. }
  13221. }
  13222. script_pushint(st, numFinds);
  13223. return SCRIPT_CMD_SUCCESS;
  13224. }
  13225. /// Changes the display name and/or display class of the npc.
  13226. /// Returns 0 is successful, 1 if the npc does not exist.
  13227. ///
  13228. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13229. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13230. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13231. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13232. BUILDIN_FUNC(setnpcdisplay)
  13233. {
  13234. const char* name;
  13235. const char* newname = NULL;
  13236. int class_ = -1, size = -1;
  13237. struct script_data* data;
  13238. struct npc_data* nd;
  13239. name = script_getstr(st,2);
  13240. data = script_getdata(st,3);
  13241. if( script_hasdata(st,4) )
  13242. class_ = script_getnum(st,4);
  13243. if( script_hasdata(st,5) )
  13244. size = script_getnum(st,5);
  13245. get_val(st, data);
  13246. if( data_isstring(data) )
  13247. newname = conv_str(st,data);
  13248. else if( data_isint(data) )
  13249. class_ = conv_num(st,data);
  13250. else
  13251. {
  13252. ShowError("script:setnpcdisplay: expected string or number\n");
  13253. script_reportdata(data);
  13254. return 1;
  13255. }
  13256. nd = npc_name2id(name);
  13257. if( nd == NULL )
  13258. {// not found
  13259. script_pushint(st,1);
  13260. return 0;
  13261. }
  13262. // update npc
  13263. if( newname )
  13264. npc_setdisplayname(nd, newname);
  13265. if( size != -1 && size != (int)nd->size )
  13266. nd->size = size;
  13267. else
  13268. size = -1;
  13269. if( class_ != -1 && nd->class_ != class_ )
  13270. npc_setclass(nd, class_);
  13271. else if( size != -1 )
  13272. { // Required to update the visual size
  13273. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13274. clif_spawn(&nd->bl);
  13275. }
  13276. script_pushint(st,0);
  13277. return SCRIPT_CMD_SUCCESS;
  13278. }
  13279. BUILDIN_FUNC(atoi)
  13280. {
  13281. const char *value;
  13282. value = script_getstr(st,2);
  13283. script_pushint(st,atoi(value));
  13284. return SCRIPT_CMD_SUCCESS;
  13285. }
  13286. BUILDIN_FUNC(axtoi)
  13287. {
  13288. const char *hex = script_getstr(st,2);
  13289. long value = strtol(hex, NULL, 16);
  13290. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13291. value = cap_value(value, INT_MIN, INT_MAX);
  13292. #endif
  13293. script_pushint(st, (int)value);
  13294. return SCRIPT_CMD_SUCCESS;
  13295. }
  13296. BUILDIN_FUNC(strtol)
  13297. {
  13298. const char *string = script_getstr(st, 2);
  13299. int base = script_getnum(st, 3);
  13300. long value = strtol(string, NULL, base);
  13301. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13302. value = cap_value(value, INT_MIN, INT_MAX);
  13303. #endif
  13304. script_pushint(st, (int)value);
  13305. return SCRIPT_CMD_SUCCESS;
  13306. }
  13307. // case-insensitive substring search [lordalfa]
  13308. BUILDIN_FUNC(compare)
  13309. {
  13310. const char *message;
  13311. const char *cmpstring;
  13312. message = script_getstr(st,2);
  13313. cmpstring = script_getstr(st,3);
  13314. script_pushint(st,(stristr(message,cmpstring) != NULL));
  13315. return SCRIPT_CMD_SUCCESS;
  13316. }
  13317. BUILDIN_FUNC(strcmp)
  13318. {
  13319. const char *str1;
  13320. const char *str2;
  13321. str1 = script_getstr(st,2);
  13322. str2 = script_getstr(st,3);
  13323. script_pushint(st,strcmp(str1, str2));
  13324. return SCRIPT_CMD_SUCCESS;
  13325. }
  13326. // [zBuffer] List of mathematics commands --->
  13327. BUILDIN_FUNC(sqrt)
  13328. {
  13329. double i, a;
  13330. i = script_getnum(st,2);
  13331. a = sqrt(i);
  13332. script_pushint(st,(int)a);
  13333. return SCRIPT_CMD_SUCCESS;
  13334. }
  13335. BUILDIN_FUNC(pow)
  13336. {
  13337. double i, a, b;
  13338. a = script_getnum(st,2);
  13339. b = script_getnum(st,3);
  13340. i = pow(a,b);
  13341. script_pushint(st,(int)i);
  13342. return SCRIPT_CMD_SUCCESS;
  13343. }
  13344. BUILDIN_FUNC(distance)
  13345. {
  13346. int x0, y0, x1, y1;
  13347. x0 = script_getnum(st,2);
  13348. y0 = script_getnum(st,3);
  13349. x1 = script_getnum(st,4);
  13350. y1 = script_getnum(st,5);
  13351. script_pushint(st,distance_xy(x0,y0,x1,y1));
  13352. return SCRIPT_CMD_SUCCESS;
  13353. }
  13354. // <--- [zBuffer] List of mathematics commands
  13355. BUILDIN_FUNC(md5)
  13356. {
  13357. const char *tmpstr;
  13358. char *md5str;
  13359. tmpstr = script_getstr(st,2);
  13360. md5str = (char *)aMalloc((32+1)*sizeof(char));
  13361. MD5_String(tmpstr, md5str);
  13362. script_pushstr(st, md5str);
  13363. return SCRIPT_CMD_SUCCESS;
  13364. }
  13365. // [zBuffer] List of dynamic var commands --->
  13366. BUILDIN_FUNC(setd)
  13367. {
  13368. TBL_PC *sd=NULL;
  13369. char varname[100];
  13370. const char *buffer;
  13371. int elem;
  13372. buffer = script_getstr(st, 2);
  13373. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  13374. elem = 0;
  13375. if( not_server_variable(*varname) )
  13376. {
  13377. sd = script_rid2sd(st);
  13378. if( sd == NULL )
  13379. {
  13380. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  13381. return 0;
  13382. }
  13383. }
  13384. if( is_string_variable(varname) ) {
  13385. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  13386. } else {
  13387. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  13388. }
  13389. return SCRIPT_CMD_SUCCESS;
  13390. }
  13391. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  13392. {
  13393. int i, j;
  13394. TBL_PC* sd = NULL;
  13395. const char* query;
  13396. struct script_data* data;
  13397. const char* name;
  13398. int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  13399. int num_vars;
  13400. int num_cols;
  13401. // check target variables
  13402. for( i = 3; script_hasdata(st,i); ++i ) {
  13403. data = script_getdata(st, i);
  13404. if( data_isreference(data) ) { // it's a variable
  13405. name = reference_getname(data);
  13406. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  13407. sd = script_rid2sd(st);
  13408. if( sd == NULL ) { // no player attached
  13409. script_reportdata(data);
  13410. st->state = END;
  13411. return 1;
  13412. }
  13413. }
  13414. if( not_array_variable(*name) )
  13415. max_rows = 1;// not an array, limit to one row
  13416. } else {
  13417. ShowError("script:query_sql: not a variable\n");
  13418. script_reportdata(data);
  13419. st->state = END;
  13420. return 1;
  13421. }
  13422. }
  13423. num_vars = i - 3;
  13424. // Execute the query
  13425. query = script_getstr(st,2);
  13426. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  13427. Sql_ShowDebug(handle);
  13428. script_pushint(st, 0);
  13429. return 1;
  13430. }
  13431. if( Sql_NumRows(handle) == 0 ) { // No data received
  13432. Sql_FreeResult(handle);
  13433. script_pushint(st, 0);
  13434. return 0;
  13435. }
  13436. // Count the number of columns to store
  13437. num_cols = Sql_NumColumns(handle);
  13438. if( num_vars < num_cols ) {
  13439. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  13440. script_reportsrc(st);
  13441. } else if( num_vars > num_cols ) {
  13442. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  13443. script_reportsrc(st);
  13444. }
  13445. // Store data
  13446. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  13447. for( j = 0; j < num_vars; ++j ) {
  13448. char* str = NULL;
  13449. if( j < num_cols )
  13450. Sql_GetData(handle, j, &str, NULL);
  13451. data = script_getdata(st, j+3);
  13452. name = reference_getname(data);
  13453. if( is_string_variable(name) )
  13454. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  13455. else
  13456. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  13457. }
  13458. }
  13459. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  13460. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  13461. script_reportsrc(st);
  13462. }
  13463. // Free data
  13464. Sql_FreeResult(handle);
  13465. script_pushint(st, i);
  13466. return SCRIPT_CMD_SUCCESS;
  13467. }
  13468. BUILDIN_FUNC(query_sql) {
  13469. #ifdef BETA_THREAD_TEST
  13470. if( st->state != RERUNLINE ) {
  13471. queryThread_add(st,false);
  13472. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13473. } else
  13474. st->state = RUN;
  13475. return 0;
  13476. #else
  13477. return buildin_query_sql_sub(st, mmysql_handle);
  13478. #endif
  13479. }
  13480. BUILDIN_FUNC(query_logsql) {
  13481. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  13482. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  13483. script_pushint(st,-1);
  13484. return 1;
  13485. }
  13486. #ifdef BETA_THREAD_TEST
  13487. if( st->state != RERUNLINE ) {
  13488. queryThread_add(st,true);
  13489. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13490. } else
  13491. st->state = RUN;
  13492. return 0;
  13493. #else
  13494. return buildin_query_sql_sub(st, logmysql_handle);
  13495. #endif
  13496. }
  13497. //Allows escaping of a given string.
  13498. BUILDIN_FUNC(escape_sql)
  13499. {
  13500. const char *str;
  13501. char *esc_str;
  13502. size_t len;
  13503. str = script_getstr(st,2);
  13504. len = strlen(str);
  13505. esc_str = (char*)aMalloc(len*2+1);
  13506. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  13507. script_pushstr(st, esc_str);
  13508. return SCRIPT_CMD_SUCCESS;
  13509. }
  13510. BUILDIN_FUNC(getd)
  13511. {
  13512. char varname[100];
  13513. const char *buffer;
  13514. int elem;
  13515. buffer = script_getstr(st, 2);
  13516. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  13517. elem = 0;
  13518. // Push the 'pointer' so it's more flexible [Lance]
  13519. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  13520. return SCRIPT_CMD_SUCCESS;
  13521. }
  13522. BUILDIN_FUNC(callshop)
  13523. {
  13524. TBL_PC *sd = NULL;
  13525. struct npc_data *nd;
  13526. const char *shopname;
  13527. int flag = 0;
  13528. sd = script_rid2sd(st);
  13529. if (!sd) {
  13530. script_pushint(st,0);
  13531. return 0;
  13532. }
  13533. shopname = script_getstr(st, 2);
  13534. if( script_hasdata(st,3) )
  13535. flag = script_getnum(st,3);
  13536. nd = npc_name2id(shopname);
  13537. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP) )
  13538. {
  13539. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  13540. script_pushint(st,0);
  13541. return 1;
  13542. }
  13543. if( nd->subtype == SHOP || nd->subtype == ITEMSHOP || nd->subtype == POINTSHOP )
  13544. {
  13545. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  13546. sd->state.callshop = 1;
  13547. switch( flag )
  13548. {
  13549. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  13550. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  13551. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  13552. }
  13553. }
  13554. else
  13555. clif_cashshop_show(sd, nd);
  13556. sd->npc_shopid = nd->bl.id;
  13557. script_pushint(st,1);
  13558. return SCRIPT_CMD_SUCCESS;
  13559. }
  13560. BUILDIN_FUNC(npcshopitem)
  13561. {
  13562. const char* npcname = script_getstr(st, 2);
  13563. struct npc_data* nd = npc_name2id(npcname);
  13564. int n, i;
  13565. int amount;
  13566. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP ) )
  13567. { //Not found.
  13568. script_pushint(st,0);
  13569. return 0;
  13570. }
  13571. // get the count of new entries
  13572. amount = (script_lastdata(st)-2)/2;
  13573. // generate new shop item list
  13574. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  13575. for( n = 0, i = 3; n < amount; n++, i+=2 )
  13576. {
  13577. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13578. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13579. }
  13580. nd->u.shop.count = n;
  13581. script_pushint(st,1);
  13582. return SCRIPT_CMD_SUCCESS;
  13583. }
  13584. BUILDIN_FUNC(npcshopadditem)
  13585. {
  13586. const char* npcname = script_getstr(st,2);
  13587. struct npc_data* nd = npc_name2id(npcname);
  13588. int n, i;
  13589. int amount;
  13590. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP ) )
  13591. { //Not found.
  13592. script_pushint(st,0);
  13593. return 0;
  13594. }
  13595. // get the count of new entries
  13596. amount = (script_lastdata(st)-2)/2;
  13597. // append new items to existing shop item list
  13598. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  13599. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  13600. {
  13601. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13602. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13603. }
  13604. nd->u.shop.count = n;
  13605. script_pushint(st,1);
  13606. return SCRIPT_CMD_SUCCESS;
  13607. }
  13608. BUILDIN_FUNC(npcshopdelitem)
  13609. {
  13610. const char* npcname = script_getstr(st,2);
  13611. struct npc_data* nd = npc_name2id(npcname);
  13612. unsigned short nameid;
  13613. int n, i;
  13614. int amount;
  13615. int size;
  13616. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP ) )
  13617. { //Not found.
  13618. script_pushint(st,0);
  13619. return 0;
  13620. }
  13621. amount = script_lastdata(st)-2;
  13622. size = nd->u.shop.count;
  13623. // remove specified items from the shop item list
  13624. for( i = 3; i < 3 + amount; i++ )
  13625. {
  13626. nameid = script_getnum(st,i);
  13627. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  13628. if( n < size )
  13629. {
  13630. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  13631. size--;
  13632. }
  13633. }
  13634. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  13635. nd->u.shop.count = size;
  13636. script_pushint(st,1);
  13637. return SCRIPT_CMD_SUCCESS;
  13638. }
  13639. //Sets a script to attach to a shop npc.
  13640. BUILDIN_FUNC(npcshopattach)
  13641. {
  13642. const char* npcname = script_getstr(st,2);
  13643. struct npc_data* nd = npc_name2id(npcname);
  13644. int flag = 1;
  13645. if( script_hasdata(st,3) )
  13646. flag = script_getnum(st,3);
  13647. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP ) )
  13648. { //Not found.
  13649. script_pushint(st,0);
  13650. return 0;
  13651. }
  13652. if (flag)
  13653. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  13654. else
  13655. nd->master_nd = NULL;
  13656. script_pushint(st,1);
  13657. return SCRIPT_CMD_SUCCESS;
  13658. }
  13659. /*==========================================
  13660. * Returns some values of an item [Lupus]
  13661. * Price, Weight, etc...
  13662. setitemscript(itemID,"{new item bonus script}",[n]);
  13663. Where n:
  13664. 0 - script
  13665. 1 - Equip script
  13666. 2 - Unequip script
  13667. *------------------------------------------*/
  13668. BUILDIN_FUNC(setitemscript)
  13669. {
  13670. unsigned short item_id;
  13671. int n = 0;
  13672. const char *script;
  13673. struct item_data *i_data;
  13674. struct script_code **dstscript;
  13675. item_id = script_getnum(st,2);
  13676. script = script_getstr(st,3);
  13677. if( script_hasdata(st,4) )
  13678. n=script_getnum(st,4);
  13679. i_data = itemdb_exists(item_id);
  13680. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  13681. script_pushint(st,0);
  13682. return 0;
  13683. }
  13684. switch (n) {
  13685. case 2:
  13686. dstscript = &i_data->unequip_script;
  13687. break;
  13688. case 1:
  13689. dstscript = &i_data->equip_script;
  13690. break;
  13691. default:
  13692. dstscript = &i_data->script;
  13693. break;
  13694. }
  13695. if(*dstscript)
  13696. script_free_code(*dstscript);
  13697. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  13698. script_pushint(st,1);
  13699. return SCRIPT_CMD_SUCCESS;
  13700. }
  13701. /*=======================================================
  13702. * Add or Update a mob drop temporarily [Akinari]
  13703. * Original Idea By: [Lupus]
  13704. *
  13705. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  13706. *
  13707. * If given an item the mob already drops, the rate
  13708. * is updated to the new rate. Rate cannot exceed 10000
  13709. * Returns 1 if succeeded (added/updated a mob drop)
  13710. *-------------------------------------------------------*/
  13711. BUILDIN_FUNC(addmonsterdrop)
  13712. {
  13713. struct mob_db *mob;
  13714. struct script_data *data;
  13715. unsigned short item_id;
  13716. int rate,i,c = 0;
  13717. data = script_getdata(st, 2);
  13718. get_val(st, data); // Convert into value in case of a variable
  13719. if(data_isstring(data))
  13720. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13721. else
  13722. mob = mob_db(script_getnum(st,2));
  13723. item_id=script_getnum(st,3);
  13724. rate=script_getnum(st,4);
  13725. if(!itemdb_exists(item_id)){
  13726. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  13727. return 1;
  13728. }
  13729. if(mob) { //We got a valid monster, check for available drop slot
  13730. for(i = 0; i < MAX_MOB_DROP; i++) {
  13731. if(mob->dropitem[i].nameid) {
  13732. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  13733. c = i;
  13734. break;
  13735. }
  13736. continue;
  13737. }
  13738. if(!c) //Accept first available slot only
  13739. c = i;
  13740. }
  13741. if(c) { //Fill in the slot with the item and rate
  13742. mob->dropitem[c].nameid = item_id;
  13743. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  13744. script_pushint(st,1);
  13745. } else //No place to put the new drop
  13746. script_pushint(st,0);
  13747. } else {
  13748. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13749. return 1;
  13750. }
  13751. return SCRIPT_CMD_SUCCESS;
  13752. }
  13753. /*=======================================================
  13754. * Delete a mob drop temporarily [Akinari]
  13755. * Original Idea By: [Lupus]
  13756. *
  13757. * delmonsterdrop <mob_id or name>,<item_id>;
  13758. *
  13759. * Returns 1 if succeeded (deleted a mob drop)
  13760. *-------------------------------------------------------*/
  13761. BUILDIN_FUNC(delmonsterdrop)
  13762. {
  13763. struct mob_db *mob;
  13764. struct script_data *data;
  13765. unsigned short item_id;
  13766. int i;
  13767. data = script_getdata(st, 2);
  13768. get_val(st, data); // Convert into value in case of a variable
  13769. if(data_isstring(data))
  13770. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13771. else
  13772. mob = mob_db(script_getnum(st,2));
  13773. item_id=script_getnum(st,3);
  13774. if(!itemdb_exists(item_id)){
  13775. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  13776. return 1;
  13777. }
  13778. if(mob) { //We got a valid monster, check for item drop on monster
  13779. for(i = 0; i < MAX_MOB_DROP; i++) {
  13780. if(mob->dropitem[i].nameid == item_id) {
  13781. mob->dropitem[i].nameid = 0;
  13782. mob->dropitem[i].p = 0;
  13783. script_pushint(st,1);
  13784. return 0;
  13785. }
  13786. }
  13787. //No drop on that monster
  13788. script_pushint(st,0);
  13789. } else {
  13790. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13791. return 1;
  13792. }
  13793. return SCRIPT_CMD_SUCCESS;
  13794. }
  13795. /*==========================================
  13796. * Returns some values of a monster [Lupus]
  13797. * Name, Level, race, size, etc...
  13798. getmonsterinfo(monsterID,queryIndex);
  13799. *------------------------------------------*/
  13800. BUILDIN_FUNC(getmonsterinfo)
  13801. {
  13802. struct mob_db *mob;
  13803. int mob_id;
  13804. mob_id = script_getnum(st,2);
  13805. if (!mobdb_checkid(mob_id)) {
  13806. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  13807. if ( !script_getnum(st,3) ) //requested a string
  13808. script_pushconststr(st,"null");
  13809. else
  13810. script_pushint(st,-1);
  13811. return 0;
  13812. }
  13813. mob = mob_db(mob_id);
  13814. switch ( script_getnum(st,3) ) {
  13815. case 0: script_pushstrcopy(st,mob->jname); break;
  13816. case 1: script_pushint(st,mob->lv); break;
  13817. case 2: script_pushint(st,mob->status.max_hp); break;
  13818. case 3: script_pushint(st,mob->base_exp); break;
  13819. case 4: script_pushint(st,mob->job_exp); break;
  13820. case 5: script_pushint(st,mob->status.rhw.atk); break;
  13821. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  13822. case 7: script_pushint(st,mob->status.def); break;
  13823. case 8: script_pushint(st,mob->status.mdef); break;
  13824. case 9: script_pushint(st,mob->status.str); break;
  13825. case 10: script_pushint(st,mob->status.agi); break;
  13826. case 11: script_pushint(st,mob->status.vit); break;
  13827. case 12: script_pushint(st,mob->status.int_); break;
  13828. case 13: script_pushint(st,mob->status.dex); break;
  13829. case 14: script_pushint(st,mob->status.luk); break;
  13830. case 15: script_pushint(st,mob->status.rhw.range); break;
  13831. case 16: script_pushint(st,mob->range2); break;
  13832. case 17: script_pushint(st,mob->range3); break;
  13833. case 18: script_pushint(st,mob->status.size); break;
  13834. case 19: script_pushint(st,mob->status.race); break;
  13835. case 20: script_pushint(st,mob->status.def_ele); break;
  13836. case 21: script_pushint(st,mob->status.mode); break;
  13837. case 22: script_pushint(st,mob->mexp); break;
  13838. default: script_pushint(st,-1); //wrong Index
  13839. }
  13840. return SCRIPT_CMD_SUCCESS;
  13841. }
  13842. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  13843. {
  13844. TBL_PC *sd = NULL;
  13845. if(script_hasdata(st,2))
  13846. sd = map_nick2sd(script_getstr(st,2));
  13847. else
  13848. sd = script_rid2sd(st);
  13849. if(sd)
  13850. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  13851. else
  13852. script_pushint(st,0);
  13853. return SCRIPT_CMD_SUCCESS;
  13854. }
  13855. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  13856. {
  13857. TBL_PC *sd = NULL;
  13858. if(script_hasdata(st,2))
  13859. sd = map_nick2sd(script_getstr(st,2));
  13860. else
  13861. sd = script_rid2sd(st);
  13862. if(sd)
  13863. script_pushint(st,(sd->chatID != 0));
  13864. else
  13865. script_pushint(st,0);
  13866. return SCRIPT_CMD_SUCCESS;
  13867. }
  13868. BUILDIN_FUNC(checkidle)
  13869. {
  13870. TBL_PC *sd = NULL;
  13871. if (script_hasdata(st, 2))
  13872. sd = map_nick2sd(script_getstr(st, 2));
  13873. else
  13874. sd = script_rid2sd(st);
  13875. if (sd)
  13876. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  13877. else
  13878. script_pushint(st, 0);
  13879. return SCRIPT_CMD_SUCCESS;
  13880. }
  13881. BUILDIN_FUNC(searchitem)
  13882. {
  13883. struct script_data* data = script_getdata(st, 2);
  13884. const char *itemname = script_getstr(st,3);
  13885. struct item_data *items[MAX_SEARCH];
  13886. int count;
  13887. char* name;
  13888. int32 start;
  13889. int32 id;
  13890. int32 i;
  13891. TBL_PC* sd = NULL;
  13892. if ((items[0] = itemdb_exists(atoi(itemname))))
  13893. count = 1;
  13894. else {
  13895. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  13896. if (count > MAX_SEARCH) count = MAX_SEARCH;
  13897. }
  13898. if (!count) {
  13899. script_pushint(st, 0);
  13900. return 0;
  13901. }
  13902. if( !data_isreference(data) )
  13903. {
  13904. ShowError("script:searchitem: not a variable\n");
  13905. script_reportdata(data);
  13906. st->state = END;
  13907. return 1;// not a variable
  13908. }
  13909. id = reference_getid(data);
  13910. start = reference_getindex(data);
  13911. name = reference_getname(data);
  13912. if( not_array_variable(*name) )
  13913. {
  13914. ShowError("script:searchitem: illegal scope\n");
  13915. script_reportdata(data);
  13916. st->state = END;
  13917. return 1;// not supported
  13918. }
  13919. if( not_server_variable(*name) )
  13920. {
  13921. sd = script_rid2sd(st);
  13922. if( sd == NULL )
  13923. return 0;// no player attached
  13924. }
  13925. if( is_string_variable(name) )
  13926. {// string array
  13927. ShowError("script:searchitem: not an integer array reference\n");
  13928. script_reportdata(data);
  13929. st->state = END;
  13930. return 1;// not supported
  13931. }
  13932. for( i = 0; i < count; ++start, ++i )
  13933. {// Set array
  13934. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  13935. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  13936. }
  13937. script_pushint(st, count);
  13938. return SCRIPT_CMD_SUCCESS;
  13939. }
  13940. // [zBuffer] List of player cont commands --->
  13941. BUILDIN_FUNC(rid2name)
  13942. {
  13943. struct block_list *bl = NULL;
  13944. int rid = script_getnum(st,2);
  13945. if((bl = map_id2bl(rid)))
  13946. {
  13947. switch(bl->type) {
  13948. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  13949. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  13950. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  13951. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  13952. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  13953. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  13954. default:
  13955. ShowError("buildin_rid2name: BL type unknown.\n");
  13956. script_pushconststr(st,"");
  13957. break;
  13958. }
  13959. } else {
  13960. ShowError("buildin_rid2name: invalid RID\n");
  13961. script_pushconststr(st,"(null)");
  13962. }
  13963. return SCRIPT_CMD_SUCCESS;
  13964. }
  13965. BUILDIN_FUNC(pcblockmove)
  13966. {
  13967. int id, flag;
  13968. TBL_PC *sd = NULL;
  13969. id = script_getnum(st,2);
  13970. flag = script_getnum(st,3);
  13971. if(id)
  13972. sd = map_id2sd(id);
  13973. else
  13974. sd = script_rid2sd(st);
  13975. if(sd)
  13976. sd->state.blockedmove = flag > 0;
  13977. return SCRIPT_CMD_SUCCESS;
  13978. }
  13979. BUILDIN_FUNC(pcfollow)
  13980. {
  13981. int id, targetid;
  13982. TBL_PC *sd = NULL;
  13983. id = script_getnum(st,2);
  13984. targetid = script_getnum(st,3);
  13985. if(id)
  13986. sd = map_id2sd(id);
  13987. else
  13988. sd = script_rid2sd(st);
  13989. if(sd)
  13990. pc_follow(sd, targetid);
  13991. return SCRIPT_CMD_SUCCESS;
  13992. }
  13993. BUILDIN_FUNC(pcstopfollow)
  13994. {
  13995. int id;
  13996. TBL_PC *sd = NULL;
  13997. id = script_getnum(st,2);
  13998. if(id)
  13999. sd = map_id2sd(id);
  14000. else
  14001. sd = script_rid2sd(st);
  14002. if(sd)
  14003. pc_stop_following(sd);
  14004. return SCRIPT_CMD_SUCCESS;
  14005. }
  14006. // <--- [zBuffer] List of player cont commands
  14007. // [zBuffer] List of mob control commands --->
  14008. /// Makes the unit walk to target position or map
  14009. /// Returns if it was successfull
  14010. ///
  14011. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  14012. /// unitwalk(<unit_id>,<target_id>) -> <bool>
  14013. BUILDIN_FUNC(unitwalk)
  14014. {
  14015. struct block_list* bl;
  14016. bl = map_id2bl(script_getnum(st,2));
  14017. if( bl == NULL )
  14018. script_pushint(st, 0);
  14019. else if( script_hasdata(st,4) ) {
  14020. int x = script_getnum(st,3);
  14021. int y = script_getnum(st,4);
  14022. if( script_pushint(st, unit_can_reach_pos(bl,x,y,0)) )
  14023. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  14024. } else {
  14025. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  14026. if( tbl == NULL ) {
  14027. ShowError("script:unitwalk: bad target destination\n");
  14028. script_pushint(st, 0);
  14029. return 1;
  14030. }
  14031. else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  14032. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  14033. }
  14034. return SCRIPT_CMD_SUCCESS;
  14035. }
  14036. /// Kills the unit
  14037. ///
  14038. /// unitkill <unit_id>;
  14039. BUILDIN_FUNC(unitkill)
  14040. {
  14041. struct block_list* bl = map_id2bl(script_getnum(st,2));
  14042. if( bl != NULL )
  14043. status_kill(bl);
  14044. return SCRIPT_CMD_SUCCESS;
  14045. }
  14046. /// Warps the unit to the target position in the target map
  14047. /// Returns if it was successfull
  14048. ///
  14049. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  14050. BUILDIN_FUNC(unitwarp)
  14051. {
  14052. int unit_id;
  14053. int map;
  14054. short x;
  14055. short y;
  14056. struct block_list* bl;
  14057. const char *mapname;
  14058. unit_id = script_getnum(st,2);
  14059. mapname = script_getstr(st, 3);
  14060. x = (short)script_getnum(st,4);
  14061. y = (short)script_getnum(st,5);
  14062. if (!unit_id) //Warp the script's runner
  14063. bl = map_id2bl(st->rid);
  14064. else
  14065. bl = map_id2bl(unit_id);
  14066. if( strcmp(mapname,"this") == 0 )
  14067. map = bl?bl->m:-1;
  14068. else
  14069. map = map_mapname2mapid(mapname);
  14070. if( map >= 0 && bl != NULL )
  14071. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  14072. else
  14073. script_pushint(st, 0);
  14074. return SCRIPT_CMD_SUCCESS;
  14075. }
  14076. /// Makes the unit attack the target.
  14077. /// If the unit is a player and <action type> is not 0, it does a continuous
  14078. /// attack instead of a single attack.
  14079. /// Returns if the request was successful.
  14080. ///
  14081. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  14082. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  14083. BUILDIN_FUNC(unitattack)
  14084. {
  14085. struct block_list* unit_bl;
  14086. struct block_list* target_bl = NULL;
  14087. struct script_data* data;
  14088. int actiontype = 0;
  14089. // get unit
  14090. unit_bl = map_id2bl(script_getnum(st,2));
  14091. if( unit_bl == NULL ) {
  14092. script_pushint(st, 0);
  14093. return 0;
  14094. }
  14095. data = script_getdata(st, 3);
  14096. get_val(st, data);
  14097. if( data_isstring(data) )
  14098. {
  14099. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  14100. if( sd != NULL )
  14101. target_bl = &sd->bl;
  14102. } else
  14103. target_bl = map_id2bl(conv_num(st, data));
  14104. // request the attack
  14105. if( target_bl == NULL )
  14106. {
  14107. script_pushint(st, 0);
  14108. return 0;
  14109. }
  14110. // get actiontype
  14111. if( script_hasdata(st,4) )
  14112. actiontype = script_getnum(st,4);
  14113. switch( unit_bl->type )
  14114. {
  14115. case BL_PC:
  14116. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  14117. script_pushint(st, 1);
  14118. return 0;
  14119. case BL_MOB:
  14120. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  14121. break;
  14122. case BL_PET:
  14123. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  14124. break;
  14125. default:
  14126. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  14127. script_pushint(st, 0);
  14128. return 1;
  14129. }
  14130. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  14131. return SCRIPT_CMD_SUCCESS;
  14132. }
  14133. /// Makes the unit stop attacking and moving
  14134. ///
  14135. /// unitstop <unit_id>;
  14136. BUILDIN_FUNC(unitstop)
  14137. {
  14138. int unit_id;
  14139. struct block_list* bl;
  14140. unit_id = script_getnum(st,2);
  14141. bl = map_id2bl(unit_id);
  14142. if( bl != NULL )
  14143. {
  14144. unit_stop_attack(bl);
  14145. unit_stop_walking(bl,4);
  14146. if( bl->type == BL_MOB )
  14147. ((TBL_MOB*)bl)->target_id = 0;
  14148. }
  14149. return SCRIPT_CMD_SUCCESS;
  14150. }
  14151. /// Makes the unit say the message
  14152. ///
  14153. /// unittalk <unit_id>,"<message>";
  14154. BUILDIN_FUNC(unittalk)
  14155. {
  14156. int unit_id;
  14157. const char* message;
  14158. struct block_list* bl;
  14159. unit_id = script_getnum(st,2);
  14160. message = script_getstr(st, 3);
  14161. bl = map_id2bl(unit_id);
  14162. if( bl != NULL )
  14163. {
  14164. struct StringBuf sbuf;
  14165. StringBuf_Init(&sbuf);
  14166. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  14167. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  14168. StringBuf_Destroy(&sbuf);
  14169. }
  14170. return SCRIPT_CMD_SUCCESS;
  14171. }
  14172. /// Makes the unit do an emotion
  14173. ///
  14174. /// unitemote <unit_id>,<emotion>;
  14175. ///
  14176. /// @see e_* in const.txt
  14177. BUILDIN_FUNC(unitemote)
  14178. {
  14179. int unit_id;
  14180. int emotion;
  14181. struct block_list* bl;
  14182. unit_id = script_getnum(st,2);
  14183. emotion = script_getnum(st,3);
  14184. bl = map_id2bl(unit_id);
  14185. if( bl != NULL )
  14186. clif_emotion(bl, emotion);
  14187. return SCRIPT_CMD_SUCCESS;
  14188. }
  14189. /// Makes the unit cast the skill on the target or self if no target is specified
  14190. ///
  14191. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  14192. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  14193. BUILDIN_FUNC(unitskilluseid)
  14194. {
  14195. int unit_id;
  14196. uint16 skill_id;
  14197. uint16 skill_lv;
  14198. int target_id;
  14199. struct block_list* bl;
  14200. struct script_data *data;
  14201. unit_id = script_getnum(st,2);
  14202. data = script_getdata(st, 3);
  14203. get_val(st, data); // Convert into value in case of a variable
  14204. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  14205. skill_lv = script_getnum(st,4);
  14206. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  14207. bl = map_id2bl(unit_id);
  14208. if( bl != NULL )
  14209. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  14210. return SCRIPT_CMD_SUCCESS;
  14211. }
  14212. /// Makes the unit cast the skill on the target position.
  14213. ///
  14214. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  14215. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  14216. BUILDIN_FUNC(unitskillusepos)
  14217. {
  14218. int unit_id;
  14219. uint16 skill_id;
  14220. uint16 skill_lv;
  14221. int skill_x;
  14222. int skill_y;
  14223. struct block_list* bl;
  14224. struct script_data *data;
  14225. unit_id = script_getnum(st,2);
  14226. data = script_getdata(st, 3);
  14227. get_val(st, data); // Convert into value in case of a variable
  14228. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  14229. skill_lv = script_getnum(st,4);
  14230. skill_x = script_getnum(st,5);
  14231. skill_y = script_getnum(st,6);
  14232. bl = map_id2bl(unit_id);
  14233. if( bl != NULL )
  14234. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  14235. return SCRIPT_CMD_SUCCESS;
  14236. }
  14237. // <--- [zBuffer] List of mob control commands
  14238. /// Pauses the execution of the script, detaching the player
  14239. ///
  14240. /// sleep <mili seconds>;
  14241. BUILDIN_FUNC(sleep)
  14242. {
  14243. int ticks;
  14244. ticks = script_getnum(st,2);
  14245. // detach the player
  14246. script_detach_rid(st);
  14247. if( ticks <= 0 )
  14248. {// do nothing
  14249. }
  14250. else if( st->sleep.tick == 0 )
  14251. {// sleep for the target amount of time
  14252. st->state = RERUNLINE;
  14253. st->sleep.tick = ticks;
  14254. }
  14255. else
  14256. {// sleep time is over
  14257. st->state = RUN;
  14258. st->sleep.tick = 0;
  14259. }
  14260. return SCRIPT_CMD_SUCCESS;
  14261. }
  14262. /// Pauses the execution of the script, keeping the player attached
  14263. /// Returns if a player is still attached
  14264. ///
  14265. /// sleep2(<mili secconds>) -> <bool>
  14266. BUILDIN_FUNC(sleep2)
  14267. {
  14268. int ticks;
  14269. ticks = script_getnum(st,2);
  14270. if( ticks <= 0 )
  14271. {// do nothing
  14272. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  14273. }
  14274. else if( !st->sleep.tick )
  14275. {// sleep for the target amount of time
  14276. st->state = RERUNLINE;
  14277. st->sleep.tick = ticks;
  14278. }
  14279. else
  14280. {// sleep time is over
  14281. st->state = RUN;
  14282. st->sleep.tick = 0;
  14283. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  14284. }
  14285. return SCRIPT_CMD_SUCCESS;
  14286. }
  14287. /// Awakes all the sleep timers of the target npc
  14288. ///
  14289. /// awake "<npc name>";
  14290. BUILDIN_FUNC(awake)
  14291. {
  14292. struct npc_data* nd;
  14293. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  14294. nd = npc_name2id(script_getstr(st, 2));
  14295. if( nd == NULL ) {
  14296. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  14297. return 1;
  14298. }
  14299. while( node )
  14300. {
  14301. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  14302. {// sleep timer for the npc
  14303. struct script_state* tst = (struct script_state*)node->data;
  14304. TBL_PC* sd = map_id2sd(tst->rid);
  14305. if( tst->sleep.timer == INVALID_TIMER )
  14306. {// already awake ???
  14307. node = node->next;
  14308. continue;
  14309. }
  14310. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  14311. {// char not online anymore / another char of the same account is online - Cancel execution
  14312. tst->state = END;
  14313. tst->rid = 0;
  14314. }
  14315. delete_timer(tst->sleep.timer, run_script_timer);
  14316. node = script_erase_sleepdb(node);
  14317. tst->sleep.timer = INVALID_TIMER;
  14318. if(tst->state != RERUNLINE)
  14319. tst->sleep.tick = 0;
  14320. run_script_main(tst);
  14321. }
  14322. else
  14323. {
  14324. node = node->next;
  14325. }
  14326. }
  14327. return SCRIPT_CMD_SUCCESS;
  14328. }
  14329. /// Returns a reference to a variable of the target NPC.
  14330. /// Returns 0 if an error occurs.
  14331. ///
  14332. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  14333. BUILDIN_FUNC(getvariableofnpc)
  14334. {
  14335. struct script_data* data;
  14336. const char* name;
  14337. struct npc_data* nd;
  14338. data = script_getdata(st,2);
  14339. if( !data_isreference(data) )
  14340. {// Not a reference (aka varaible name)
  14341. ShowError("script:getvariableofnpc: not a variable\n");
  14342. script_reportdata(data);
  14343. script_pushnil(st);
  14344. st->state = END;
  14345. return 1;
  14346. }
  14347. name = reference_getname(data);
  14348. if( *name != '.' || name[1] == '@' )
  14349. {// not a npc variable
  14350. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  14351. script_reportdata(data);
  14352. script_pushnil(st);
  14353. st->state = END;
  14354. return 1;
  14355. }
  14356. nd = npc_name2id(script_getstr(st,3));
  14357. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  14358. {// NPC not found or has no script
  14359. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  14360. script_pushnil(st);
  14361. st->state = END;
  14362. return 1;
  14363. }
  14364. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  14365. return SCRIPT_CMD_SUCCESS;
  14366. }
  14367. /// Opens a warp portal.
  14368. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  14369. ///
  14370. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  14371. ///
  14372. /// @author blackhole89
  14373. BUILDIN_FUNC(warpportal)
  14374. {
  14375. int spx;
  14376. int spy;
  14377. unsigned short mapindex;
  14378. int tpx;
  14379. int tpy;
  14380. struct skill_unit_group* group;
  14381. struct block_list* bl;
  14382. bl = map_id2bl(st->oid);
  14383. if( bl == NULL )
  14384. {
  14385. ShowError("script:warpportal: npc is needed\n");
  14386. return 1;
  14387. }
  14388. spx = script_getnum(st,2);
  14389. spy = script_getnum(st,3);
  14390. mapindex = mapindex_name2id(script_getstr(st, 4));
  14391. tpx = script_getnum(st,5);
  14392. tpy = script_getnum(st,6);
  14393. if( mapindex == 0 )
  14394. return 0;// map not found
  14395. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  14396. if( group == NULL )
  14397. return 0;// failed
  14398. group->val1 = (group->val1<<16)|(short)0;
  14399. group->val2 = (tpx<<16) | tpy;
  14400. group->val3 = mapindex;
  14401. return SCRIPT_CMD_SUCCESS;
  14402. }
  14403. BUILDIN_FUNC(openmail)
  14404. {
  14405. TBL_PC* sd;
  14406. sd = script_rid2sd(st);
  14407. if( sd == NULL )
  14408. return 0;
  14409. mail_openmail(sd);
  14410. return SCRIPT_CMD_SUCCESS;
  14411. }
  14412. BUILDIN_FUNC(openauction)
  14413. {
  14414. TBL_PC* sd;
  14415. sd = script_rid2sd(st);
  14416. if( sd == NULL )
  14417. return 0;
  14418. if( !battle_config.feature_auction ) {
  14419. clif_colormes(sd, color_table[COLOR_RED], msg_txt(sd, 517));
  14420. return 0;
  14421. }
  14422. clif_Auction_openwindow(sd);
  14423. return SCRIPT_CMD_SUCCESS;
  14424. }
  14425. /// Retrieves the value of the specified flag of the specified cell.
  14426. ///
  14427. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  14428. ///
  14429. /// @see cell_chk* constants in const.txt for the types
  14430. BUILDIN_FUNC(checkcell)
  14431. {
  14432. int16 m = map_mapname2mapid(script_getstr(st,2));
  14433. int16 x = script_getnum(st,3);
  14434. int16 y = script_getnum(st,4);
  14435. cell_chk type = (cell_chk)script_getnum(st,5);
  14436. script_pushint(st, map_getcell(m, x, y, type));
  14437. return SCRIPT_CMD_SUCCESS;
  14438. }
  14439. /// Modifies flags of cells in the specified area.
  14440. ///
  14441. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  14442. ///
  14443. /// @see cell_* constants in const.txt for the types
  14444. BUILDIN_FUNC(setcell)
  14445. {
  14446. int16 m = map_mapname2mapid(script_getstr(st,2));
  14447. int16 x1 = script_getnum(st,3);
  14448. int16 y1 = script_getnum(st,4);
  14449. int16 x2 = script_getnum(st,5);
  14450. int16 y2 = script_getnum(st,6);
  14451. cell_t type = (cell_t)script_getnum(st,7);
  14452. bool flag = (bool)script_getnum(st,8);
  14453. int x,y;
  14454. if( x1 > x2 ) swap(x1,x2);
  14455. if( y1 > y2 ) swap(y1,y2);
  14456. for( y = y1; y <= y2; ++y )
  14457. for( x = x1; x <= x2; ++x )
  14458. map_setcell(m, x, y, type, flag);
  14459. return SCRIPT_CMD_SUCCESS;
  14460. }
  14461. /*==========================================
  14462. * Mercenary Commands
  14463. *------------------------------------------*/
  14464. BUILDIN_FUNC(mercenary_create)
  14465. {
  14466. struct map_session_data *sd;
  14467. int class_, contract_time;
  14468. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  14469. return 0;
  14470. class_ = script_getnum(st,2);
  14471. if( !mercenary_class(class_) )
  14472. return 0;
  14473. contract_time = script_getnum(st,3);
  14474. mercenary_create(sd, class_, contract_time);
  14475. return SCRIPT_CMD_SUCCESS;
  14476. }
  14477. BUILDIN_FUNC(mercenary_heal)
  14478. {
  14479. struct map_session_data *sd = script_rid2sd(st);
  14480. int hp, sp;
  14481. if( sd == NULL || sd->md == NULL )
  14482. return 0;
  14483. hp = script_getnum(st,2);
  14484. sp = script_getnum(st,3);
  14485. status_heal(&sd->md->bl, hp, sp, 0);
  14486. return SCRIPT_CMD_SUCCESS;
  14487. }
  14488. BUILDIN_FUNC(mercenary_sc_start)
  14489. {
  14490. struct map_session_data *sd = script_rid2sd(st);
  14491. enum sc_type type;
  14492. int tick, val1;
  14493. if( sd == NULL || sd->md == NULL )
  14494. return 0;
  14495. type = (sc_type)script_getnum(st,2);
  14496. tick = script_getnum(st,3);
  14497. val1 = script_getnum(st,4);
  14498. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  14499. return SCRIPT_CMD_SUCCESS;
  14500. }
  14501. BUILDIN_FUNC(mercenary_get_calls)
  14502. {
  14503. struct map_session_data *sd = script_rid2sd(st);
  14504. int guild;
  14505. if( sd == NULL )
  14506. return 0;
  14507. guild = script_getnum(st,2);
  14508. switch( guild )
  14509. {
  14510. case ARCH_MERC_GUILD:
  14511. script_pushint(st,sd->status.arch_calls);
  14512. break;
  14513. case SPEAR_MERC_GUILD:
  14514. script_pushint(st,sd->status.spear_calls);
  14515. break;
  14516. case SWORD_MERC_GUILD:
  14517. script_pushint(st,sd->status.sword_calls);
  14518. break;
  14519. default:
  14520. script_pushint(st,0);
  14521. break;
  14522. }
  14523. return SCRIPT_CMD_SUCCESS;
  14524. }
  14525. BUILDIN_FUNC(mercenary_set_calls)
  14526. {
  14527. struct map_session_data *sd = script_rid2sd(st);
  14528. int guild, value, *calls;
  14529. if( sd == NULL )
  14530. return 0;
  14531. guild = script_getnum(st,2);
  14532. value = script_getnum(st,3);
  14533. switch( guild )
  14534. {
  14535. case ARCH_MERC_GUILD:
  14536. calls = &sd->status.arch_calls;
  14537. break;
  14538. case SPEAR_MERC_GUILD:
  14539. calls = &sd->status.spear_calls;
  14540. break;
  14541. case SWORD_MERC_GUILD:
  14542. calls = &sd->status.sword_calls;
  14543. break;
  14544. default:
  14545. return 0; // Invalid Guild
  14546. }
  14547. *calls += value;
  14548. *calls = cap_value(*calls, 0, INT_MAX);
  14549. return SCRIPT_CMD_SUCCESS;
  14550. }
  14551. BUILDIN_FUNC(mercenary_get_faith)
  14552. {
  14553. struct map_session_data *sd = script_rid2sd(st);
  14554. int guild;
  14555. if( sd == NULL )
  14556. return 0;
  14557. guild = script_getnum(st,2);
  14558. switch( guild )
  14559. {
  14560. case ARCH_MERC_GUILD:
  14561. script_pushint(st,sd->status.arch_faith);
  14562. break;
  14563. case SPEAR_MERC_GUILD:
  14564. script_pushint(st,sd->status.spear_faith);
  14565. break;
  14566. case SWORD_MERC_GUILD:
  14567. script_pushint(st,sd->status.sword_faith);
  14568. break;
  14569. default:
  14570. script_pushint(st,0);
  14571. break;
  14572. }
  14573. return SCRIPT_CMD_SUCCESS;
  14574. }
  14575. BUILDIN_FUNC(mercenary_set_faith)
  14576. {
  14577. struct map_session_data *sd = script_rid2sd(st);
  14578. int guild, value, *calls;
  14579. if( sd == NULL )
  14580. return 0;
  14581. guild = script_getnum(st,2);
  14582. value = script_getnum(st,3);
  14583. switch( guild )
  14584. {
  14585. case ARCH_MERC_GUILD:
  14586. calls = &sd->status.arch_faith;
  14587. break;
  14588. case SPEAR_MERC_GUILD:
  14589. calls = &sd->status.spear_faith;
  14590. break;
  14591. case SWORD_MERC_GUILD:
  14592. calls = &sd->status.sword_faith;
  14593. break;
  14594. default:
  14595. return 0; // Invalid Guild
  14596. }
  14597. *calls += value;
  14598. *calls = cap_value(*calls, 0, INT_MAX);
  14599. if( mercenary_get_guild(sd->md) == guild )
  14600. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  14601. return SCRIPT_CMD_SUCCESS;
  14602. }
  14603. /*------------------------------------------
  14604. * Book Reading
  14605. *------------------------------------------*/
  14606. BUILDIN_FUNC(readbook)
  14607. {
  14608. struct map_session_data *sd;
  14609. int book_id, page;
  14610. if( (sd = script_rid2sd(st)) == NULL )
  14611. return 0;
  14612. book_id = script_getnum(st,2);
  14613. page = script_getnum(st,3);
  14614. clif_readbook(sd->fd, book_id, page);
  14615. return SCRIPT_CMD_SUCCESS;
  14616. }
  14617. /******************
  14618. Questlog script commands
  14619. *******************/
  14620. BUILDIN_FUNC(questinfo)
  14621. {
  14622. TBL_NPC* nd = map_id2nd(st->oid);
  14623. int quest_id, icon, job, color = 0;
  14624. struct questinfo qi;
  14625. if( nd == NULL || nd->bl.m == -1 )
  14626. return true;
  14627. quest_id = script_getnum(st, 2);
  14628. icon = script_getnum(st, 3);
  14629. #if PACKETVER >= 20120410
  14630. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  14631. icon = 9999; // Default to nothing if icon id is invalid.
  14632. #else
  14633. if(icon < 0 || icon > 7)
  14634. icon = 0;
  14635. else
  14636. icon = icon + 1;
  14637. #endif
  14638. qi.quest_id = quest_id;
  14639. qi.icon = (unsigned char)icon;
  14640. qi.nd = nd;
  14641. if( script_hasdata(st, 4) ) {
  14642. color = script_getnum(st, 4);
  14643. if( color < 0 || color > 3 ) {
  14644. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  14645. script_reportfunc(st);
  14646. color = 0;
  14647. }
  14648. qi.color = (unsigned char)color;
  14649. }
  14650. qi.hasJob = false;
  14651. if(script_hasdata(st, 5)) {
  14652. job = script_getnum(st, 5);
  14653. if (!pcdb_checkid(job))
  14654. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  14655. else {
  14656. qi.hasJob = true;
  14657. qi.job = (unsigned short)job;
  14658. }
  14659. }
  14660. map_add_questinfo(nd->bl.m,&qi);
  14661. return true;
  14662. }
  14663. BUILDIN_FUNC(setquest)
  14664. {
  14665. struct map_session_data *sd = script_rid2sd(st);
  14666. unsigned short i;
  14667. int quest_id;
  14668. nullpo_retr(1, sd);
  14669. quest_id = script_getnum(st, 2);
  14670. quest_add(sd, quest_id);
  14671. // If questinfo is set, remove quest bubble once quest is set.
  14672. for(i = 0; i < map[sd->bl.m].qi_count; i++) {
  14673. struct questinfo *qi = &map[sd->bl.m].qi_data[i];
  14674. if( qi->quest_id == quest_id ) {
  14675. #if PACKETVER >= 20120410
  14676. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  14677. #else
  14678. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  14679. #endif
  14680. }
  14681. }
  14682. return SCRIPT_CMD_SUCCESS;
  14683. }
  14684. BUILDIN_FUNC(erasequest)
  14685. {
  14686. struct map_session_data *sd = script_rid2sd(st);
  14687. nullpo_ret(sd);
  14688. quest_delete(sd, script_getnum(st, 2));
  14689. return SCRIPT_CMD_SUCCESS;
  14690. }
  14691. BUILDIN_FUNC(completequest)
  14692. {
  14693. struct map_session_data *sd = script_rid2sd(st);
  14694. nullpo_ret(sd);
  14695. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  14696. return SCRIPT_CMD_SUCCESS;
  14697. }
  14698. BUILDIN_FUNC(changequest)
  14699. {
  14700. struct map_session_data *sd = script_rid2sd(st);
  14701. nullpo_ret(sd);
  14702. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  14703. return SCRIPT_CMD_SUCCESS;
  14704. }
  14705. BUILDIN_FUNC(checkquest)
  14706. {
  14707. struct map_session_data *sd = script_rid2sd(st);
  14708. enum quest_check_type type = HAVEQUEST;
  14709. nullpo_ret(sd);
  14710. if( script_hasdata(st, 3) )
  14711. type = (enum quest_check_type)script_getnum(st, 3);
  14712. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  14713. return SCRIPT_CMD_SUCCESS;
  14714. }
  14715. BUILDIN_FUNC(isbegin_quest)
  14716. {
  14717. struct map_session_data *sd = script_rid2sd(st);
  14718. int i;
  14719. nullpo_ret(sd);
  14720. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  14721. script_pushint(st, i + (i < 1));
  14722. return SCRIPT_CMD_SUCCESS;
  14723. }
  14724. BUILDIN_FUNC(showevent)
  14725. {
  14726. TBL_PC *sd = script_rid2sd(st);
  14727. struct npc_data *nd = map_id2nd(st->oid);
  14728. int icon, color = 0;
  14729. if( sd == NULL || nd == NULL )
  14730. return 0;
  14731. icon = script_getnum(st, 2);
  14732. if( script_hasdata(st, 3) ) {
  14733. color = script_getnum(st, 3);
  14734. if( color < 0 || color > 3 ) {
  14735. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  14736. script_reportfunc(st);
  14737. color = 0;
  14738. }
  14739. }
  14740. #if PACKETVER >= 20120410
  14741. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  14742. icon = 9999; // Default to nothing if icon id is invalid.
  14743. #else
  14744. if(icon < 0 || icon > 7)
  14745. icon = 0;
  14746. else
  14747. icon = icon + 1;
  14748. #endif
  14749. clif_quest_show_event(sd, &nd->bl, icon, color);
  14750. return SCRIPT_CMD_SUCCESS;
  14751. }
  14752. /*==========================================
  14753. * BattleGround System
  14754. *------------------------------------------*/
  14755. BUILDIN_FUNC(waitingroom2bg)
  14756. {
  14757. struct npc_data *nd;
  14758. struct chat_data *cd;
  14759. const char *map_name, *ev = "", *dev = "";
  14760. int x, y, i, mapindex = 0, bg_id, n;
  14761. struct map_session_data *sd;
  14762. if( script_hasdata(st,7) )
  14763. nd = npc_name2id(script_getstr(st,7));
  14764. else
  14765. nd = (struct npc_data *)map_id2bl(st->oid);
  14766. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  14767. {
  14768. script_pushint(st,0);
  14769. return 0;
  14770. }
  14771. map_name = script_getstr(st,2);
  14772. if( strcmp(map_name,"-") != 0 )
  14773. {
  14774. mapindex = mapindex_name2id(map_name);
  14775. if( mapindex == 0 )
  14776. { // Invalid Map
  14777. script_pushint(st,0);
  14778. return 0;
  14779. }
  14780. }
  14781. x = script_getnum(st,3);
  14782. y = script_getnum(st,4);
  14783. ev = script_getstr(st,5); // Logout Event
  14784. dev = script_getstr(st,6); // Die Event
  14785. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  14786. { // Creation failed
  14787. script_pushint(st,0);
  14788. return 0;
  14789. }
  14790. n = cd->users;
  14791. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  14792. {
  14793. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  14794. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  14795. else
  14796. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  14797. }
  14798. mapreg_setreg(add_str("$@arenamembersnum"), i);
  14799. script_pushint(st,bg_id);
  14800. return SCRIPT_CMD_SUCCESS;
  14801. }
  14802. BUILDIN_FUNC(waitingroom2bg_single)
  14803. {
  14804. const char* map_name;
  14805. struct npc_data *nd;
  14806. struct chat_data *cd;
  14807. struct map_session_data *sd;
  14808. int x, y, mapindex, bg_id;
  14809. bg_id = script_getnum(st,2);
  14810. map_name = script_getstr(st,3);
  14811. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14812. return 0; // Invalid Map
  14813. x = script_getnum(st,4);
  14814. y = script_getnum(st,5);
  14815. nd = npc_name2id(script_getstr(st,6));
  14816. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  14817. return 0;
  14818. if( (sd = cd->usersd[0]) == NULL )
  14819. return 0;
  14820. if( bg_team_join(bg_id, sd) )
  14821. {
  14822. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  14823. script_pushint(st,1);
  14824. }
  14825. else
  14826. script_pushint(st,0);
  14827. return SCRIPT_CMD_SUCCESS;
  14828. }
  14829. BUILDIN_FUNC(bg_team_setxy)
  14830. {
  14831. struct battleground_data *bg;
  14832. int bg_id;
  14833. bg_id = script_getnum(st,2);
  14834. if( (bg = bg_team_search(bg_id)) == NULL )
  14835. return 0;
  14836. bg->x = script_getnum(st,3);
  14837. bg->y = script_getnum(st,4);
  14838. return SCRIPT_CMD_SUCCESS;
  14839. }
  14840. BUILDIN_FUNC(bg_warp)
  14841. {
  14842. int x, y, mapindex, bg_id;
  14843. const char* map_name;
  14844. bg_id = script_getnum(st,2);
  14845. map_name = script_getstr(st,3);
  14846. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14847. return 0; // Invalid Map
  14848. x = script_getnum(st,4);
  14849. y = script_getnum(st,5);
  14850. bg_team_warp(bg_id, mapindex, x, y);
  14851. return SCRIPT_CMD_SUCCESS;
  14852. }
  14853. BUILDIN_FUNC(bg_monster)
  14854. {
  14855. int class_ = 0, x = 0, y = 0, bg_id = 0;
  14856. const char *str,*map, *evt="";
  14857. bg_id = script_getnum(st,2);
  14858. map = script_getstr(st,3);
  14859. x = script_getnum(st,4);
  14860. y = script_getnum(st,5);
  14861. str = script_getstr(st,6);
  14862. class_ = script_getnum(st,7);
  14863. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  14864. check_event(st, evt);
  14865. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  14866. return SCRIPT_CMD_SUCCESS;
  14867. }
  14868. BUILDIN_FUNC(bg_monster_set_team)
  14869. {
  14870. struct mob_data *md;
  14871. struct block_list *mbl;
  14872. int id = script_getnum(st,2),
  14873. bg_id = script_getnum(st,3);
  14874. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  14875. return 0;
  14876. md = (TBL_MOB *)mbl;
  14877. md->bg_id = bg_id;
  14878. mob_stop_attack(md);
  14879. mob_stop_walking(md, 0);
  14880. md->target_id = md->attacked_id = 0;
  14881. clif_charnameack(0, &md->bl);
  14882. return SCRIPT_CMD_SUCCESS;
  14883. }
  14884. BUILDIN_FUNC(bg_leave)
  14885. {
  14886. struct map_session_data *sd = script_rid2sd(st);
  14887. if( sd == NULL || !sd->bg_id )
  14888. return 0;
  14889. bg_team_leave(sd,0);
  14890. return SCRIPT_CMD_SUCCESS;
  14891. }
  14892. BUILDIN_FUNC(bg_destroy)
  14893. {
  14894. int bg_id = script_getnum(st,2);
  14895. bg_team_delete(bg_id);
  14896. return SCRIPT_CMD_SUCCESS;
  14897. }
  14898. BUILDIN_FUNC(bg_getareausers)
  14899. {
  14900. const char *str;
  14901. int16 m, x0, y0, x1, y1;
  14902. int bg_id;
  14903. int i = 0, c = 0;
  14904. struct battleground_data *bg = NULL;
  14905. struct map_session_data *sd;
  14906. bg_id = script_getnum(st,2);
  14907. str = script_getstr(st,3);
  14908. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  14909. {
  14910. script_pushint(st,0);
  14911. return 0;
  14912. }
  14913. x0 = script_getnum(st,4);
  14914. y0 = script_getnum(st,5);
  14915. x1 = script_getnum(st,6);
  14916. y1 = script_getnum(st,7);
  14917. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  14918. {
  14919. if( (sd = bg->members[i].sd) == NULL )
  14920. continue;
  14921. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  14922. continue;
  14923. c++;
  14924. }
  14925. script_pushint(st,c);
  14926. return SCRIPT_CMD_SUCCESS;
  14927. }
  14928. BUILDIN_FUNC(bg_updatescore)
  14929. {
  14930. const char *str;
  14931. int16 m;
  14932. str = script_getstr(st,2);
  14933. if( (m = map_mapname2mapid(str)) < 0 )
  14934. return 0;
  14935. map[m].bgscore_lion = script_getnum(st,3);
  14936. map[m].bgscore_eagle = script_getnum(st,4);
  14937. clif_bg_updatescore(m);
  14938. return SCRIPT_CMD_SUCCESS;
  14939. }
  14940. BUILDIN_FUNC(bg_get_data)
  14941. {
  14942. struct battleground_data *bg;
  14943. int bg_id = script_getnum(st,2),
  14944. type = script_getnum(st,3);
  14945. if( (bg = bg_team_search(bg_id)) == NULL )
  14946. {
  14947. script_pushint(st,0);
  14948. return 0;
  14949. }
  14950. switch( type )
  14951. {
  14952. case 0: script_pushint(st, bg->count); break;
  14953. default:
  14954. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  14955. break;
  14956. }
  14957. return SCRIPT_CMD_SUCCESS;
  14958. }
  14959. /*==========================================
  14960. * Instancing System
  14961. *------------------------------------------*/
  14962. //Returns an Instance ID
  14963. //Checks NPC first, then if player is attached we check party
  14964. int script_instancegetid(struct script_state* st)
  14965. {
  14966. short instance_id = 0;
  14967. struct npc_data *nd;
  14968. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  14969. instance_id = nd->instance_id;
  14970. else {
  14971. struct map_session_data *sd;
  14972. struct party_data *p;
  14973. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14974. instance_id = p->instance_id;
  14975. }
  14976. return instance_id;
  14977. }
  14978. /*==========================================
  14979. * Creates the instance
  14980. * Returns Instance ID if created successfully
  14981. *------------------------------------------*/
  14982. BUILDIN_FUNC(instance_create)
  14983. {
  14984. struct map_session_data *sd;
  14985. if((sd = script_rid2sd(st)) == NULL)
  14986. return -1;
  14987. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  14988. return SCRIPT_CMD_SUCCESS;
  14989. }
  14990. /*==========================================
  14991. * Destroys an instance (unofficial)
  14992. * Officially instances are only destroyed by timeout
  14993. *
  14994. * instance_destroy {<instance_id>};
  14995. *------------------------------------------*/
  14996. BUILDIN_FUNC(instance_destroy)
  14997. {
  14998. short instance_id;
  14999. if( script_hasdata(st,2) )
  15000. instance_id = script_getnum(st,2);
  15001. else
  15002. instance_id = script_instancegetid(st);
  15003. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  15004. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  15005. return 0;
  15006. }
  15007. instance_destroy(instance_id);
  15008. return SCRIPT_CMD_SUCCESS;
  15009. }
  15010. /*==========================================
  15011. * Warps player to instance
  15012. * Results:
  15013. * 0: Success
  15014. * 1: Character not in party
  15015. * 2: Party doesn't have instance
  15016. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  15017. *------------------------------------------*/
  15018. BUILDIN_FUNC(instance_enter)
  15019. {
  15020. struct map_session_data *sd;
  15021. if((sd = script_rid2sd(st)) != NULL){
  15022. if (script_hasdata(st,3) && script_hasdata(st,4))
  15023. script_pushint(st,instance_enter_position(sd,script_getstr(st, 2),script_getnum(st, 3),script_getnum(st, 4)));
  15024. else
  15025. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  15026. }
  15027. else
  15028. return 1;
  15029. return SCRIPT_CMD_SUCCESS;
  15030. }
  15031. /*==========================================
  15032. * Returns the name of a duplicated NPC
  15033. *
  15034. * instance_npcname <npc_name>{,<instance_id>};
  15035. * <npc_name> is the full name of an NPC.
  15036. *------------------------------------------*/
  15037. BUILDIN_FUNC(instance_npcname)
  15038. {
  15039. const char *str;
  15040. short instance_id = 0;
  15041. struct npc_data *nd;
  15042. str = script_getstr(st,2);
  15043. if( script_hasdata(st,3) )
  15044. instance_id = script_getnum(st,3);
  15045. else
  15046. instance_id = script_instancegetid(st);
  15047. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  15048. static char npcname[NAME_LENGTH];
  15049. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  15050. script_pushconststr(st,npcname);
  15051. } else {
  15052. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  15053. st->state = END;
  15054. return 1;
  15055. }
  15056. return SCRIPT_CMD_SUCCESS;
  15057. }
  15058. /*==========================================
  15059. * Returns the name of a duplicated map
  15060. *
  15061. * instance_mapname <map_name>{,<instance_id>};
  15062. *------------------------------------------*/
  15063. BUILDIN_FUNC(instance_mapname)
  15064. {
  15065. const char *str;
  15066. int16 m;
  15067. short instance_id = 0;
  15068. str = script_getstr(st,2);
  15069. if( script_hasdata(st,3) )
  15070. instance_id = script_getnum(st,3);
  15071. else
  15072. instance_id = script_instancegetid(st);
  15073. // Check that instance mapname is a valid map
  15074. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  15075. script_pushconststr(st, "");
  15076. else
  15077. script_pushconststr(st, map[m].name);
  15078. return SCRIPT_CMD_SUCCESS;
  15079. }
  15080. /*==========================================
  15081. * Returns an Instance ID
  15082. *------------------------------------------*/
  15083. BUILDIN_FUNC(instance_id)
  15084. {
  15085. short instance_id;
  15086. instance_id = script_instancegetid(st);
  15087. if(!instance_id) {
  15088. //ShowError("script:instance_id: No instance attached to NPC or player");
  15089. script_pushint(st, 0);
  15090. return 1;
  15091. }
  15092. script_pushint(st, instance_id);
  15093. return SCRIPT_CMD_SUCCESS;
  15094. }
  15095. /*==========================================
  15096. * Warps all players inside an instance
  15097. *
  15098. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  15099. *------------------------------------------*/
  15100. BUILDIN_FUNC(instance_warpall)
  15101. {
  15102. struct party_data *p;
  15103. struct map_session_data *pl_sd;
  15104. int16 m, i;
  15105. short instance_id;
  15106. const char *mapn;
  15107. int x, y;
  15108. mapn = script_getstr(st,2);
  15109. x = script_getnum(st,3);
  15110. y = script_getnum(st,4);
  15111. if( script_hasdata(st,5) )
  15112. instance_id = script_getnum(st,5);
  15113. else
  15114. instance_id = script_instancegetid(st);
  15115. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  15116. return 0;
  15117. if( !(p = party_search(instance_data[instance_id].party_id)) )
  15118. return 0;
  15119. for( i = 0; i < MAX_PARTY; i++ )
  15120. 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);
  15121. return SCRIPT_CMD_SUCCESS;
  15122. }
  15123. /*==========================================
  15124. * Broadcasts to all maps inside an instance
  15125. *
  15126. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  15127. * Using -1 for <instance id> will auto-detect the id.
  15128. *------------------------------------------*/
  15129. BUILDIN_FUNC(instance_announce) {
  15130. int instance_id = script_getnum(st,2);
  15131. const char *mes = script_getstr(st,3);
  15132. int flag = script_getnum(st,4);
  15133. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  15134. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  15135. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  15136. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  15137. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  15138. int i;
  15139. if( instance_id == -1 ) {
  15140. instance_id = script_instancegetid(st);
  15141. }
  15142. if( !instance_id && &instance_data[instance_id] != NULL)
  15143. return true;
  15144. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  15145. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  15146. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  15147. return SCRIPT_CMD_SUCCESS;
  15148. }
  15149. /*==========================================
  15150. * instance_check_party [malufett]
  15151. * Values:
  15152. * party_id : Party ID of the invoking character. [Required Parameter]
  15153. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  15154. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  15155. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  15156. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  15157. * Example 2: instance_check_party (getcharid(1),1,1,99);
  15158. *------------------------------------------*/
  15159. BUILDIN_FUNC(instance_check_party)
  15160. {
  15161. struct map_session_data *pl_sd;
  15162. int amount, min, max, i, party_id, c = 0;
  15163. struct party_data *p = NULL;
  15164. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  15165. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  15166. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  15167. if( min < 1 || min > MAX_LEVEL) {
  15168. ShowError("script:check_party: Invalid min level, %d\n", min);
  15169. return 1;
  15170. } else if( max < 1 || max > MAX_LEVEL) {
  15171. ShowError("script:check_party: Invalid max level, %d\n", max);
  15172. return 1;
  15173. }
  15174. if( script_hasdata(st,2) )
  15175. party_id = script_getnum(st,2);
  15176. else return 0;
  15177. if( !(p = party_search(party_id)) ) {
  15178. script_pushint(st, 0); // Returns false if party does not exist.
  15179. return 0;
  15180. }
  15181. for( i = 0; i < MAX_PARTY; i++ )
  15182. if( (pl_sd = p->data[i].sd) )
  15183. if(map_id2bl(pl_sd->bl.id)) {
  15184. if(pl_sd->status.base_level < min) {
  15185. script_pushint(st, 0);
  15186. return 0;
  15187. } else if(pl_sd->status.base_level > max) {
  15188. script_pushint(st, 0);
  15189. return 0;
  15190. }
  15191. c++;
  15192. }
  15193. if(c < amount)
  15194. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  15195. else
  15196. script_pushint(st, 1);
  15197. return SCRIPT_CMD_SUCCESS;
  15198. }
  15199. /*==========================================
  15200. * Custom Fonts
  15201. *------------------------------------------*/
  15202. BUILDIN_FUNC(setfont)
  15203. {
  15204. struct map_session_data *sd = script_rid2sd(st);
  15205. int font = script_getnum(st,2);
  15206. if( sd == NULL )
  15207. return 0;
  15208. if( sd->status.font != font )
  15209. sd->status.font = font;
  15210. else
  15211. sd->status.font = 0;
  15212. clif_font(sd);
  15213. return SCRIPT_CMD_SUCCESS;
  15214. }
  15215. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  15216. {
  15217. TBL_MOB* md = (TBL_MOB*)bl;
  15218. struct block_list *tbl;
  15219. int mobid = va_arg(ap,int);
  15220. uint16 skill_id = va_arg(ap,int);
  15221. uint16 skill_lv = va_arg(ap,int);
  15222. int casttime = va_arg(ap,int);
  15223. int cancel = va_arg(ap,int);
  15224. int emotion = va_arg(ap,int);
  15225. int target = va_arg(ap,int);
  15226. if( md->mob_id != mobid )
  15227. return 0;
  15228. // 0:self, 1:target, 2:master, default:random
  15229. switch( target ) {
  15230. case 0: tbl = map_id2bl(md->bl.id); break;
  15231. case 1: tbl = map_id2bl(md->target_id); break;
  15232. case 2: tbl = map_id2bl(md->master_id); break;
  15233. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  15234. }
  15235. if( !tbl )
  15236. return 0;
  15237. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  15238. unit_skillcastcancel(bl,0);
  15239. if( skill_get_casttype(skill_id) == CAST_GROUND )
  15240. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  15241. else
  15242. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  15243. clif_emotion(&md->bl, emotion);
  15244. return SCRIPT_CMD_SUCCESS;
  15245. }
  15246. /*==========================================
  15247. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  15248. *------------------------------------------*/
  15249. BUILDIN_FUNC(areamobuseskill)
  15250. {
  15251. struct block_list center;
  15252. struct script_data *data;
  15253. int16 m;
  15254. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  15255. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  15256. ShowError("areamobuseskill: invalid map name.\n");
  15257. return 0;
  15258. }
  15259. center.m = m;
  15260. center.x = script_getnum(st,3);
  15261. center.y = script_getnum(st,4);
  15262. range = script_getnum(st,5);
  15263. mobid = script_getnum(st,6);
  15264. data = script_getdata(st, 7);
  15265. get_val(st, data); // Convert into value in case of a variable
  15266. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  15267. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  15268. skill_lv = battle_config.mob_max_skilllvl;
  15269. casttime = script_getnum(st,9);
  15270. cancel = script_getnum(st,10);
  15271. emotion = script_getnum(st,11);
  15272. target = script_getnum(st,12);
  15273. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  15274. return SCRIPT_CMD_SUCCESS;
  15275. }
  15276. BUILDIN_FUNC(progressbar)
  15277. {
  15278. struct map_session_data * sd = script_rid2sd(st);
  15279. const char * color;
  15280. unsigned int second;
  15281. if( !st || !sd )
  15282. return 0;
  15283. st->state = STOP;
  15284. color = script_getstr(st,2);
  15285. second = script_getnum(st,3);
  15286. sd->progressbar.npc_id = st->oid;
  15287. sd->progressbar.timeout = gettick() + second*1000;
  15288. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  15289. return SCRIPT_CMD_SUCCESS;
  15290. }
  15291. BUILDIN_FUNC(pushpc)
  15292. {
  15293. uint8 dir;
  15294. int cells, dx, dy;
  15295. struct map_session_data* sd;
  15296. if((sd = script_rid2sd(st))==NULL)
  15297. {
  15298. return 0;
  15299. }
  15300. dir = script_getnum(st,2);
  15301. cells = script_getnum(st,3);
  15302. if(dir>7)
  15303. {
  15304. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  15305. script_reportsrc(st);
  15306. dir%= 8; // trim spin-over
  15307. }
  15308. if(!cells)
  15309. {// zero distance
  15310. return 0;
  15311. }
  15312. else if(cells<0)
  15313. {// pushing backwards
  15314. dir = (dir+4)%8; // turn around
  15315. cells = -cells;
  15316. }
  15317. dx = dirx[dir];
  15318. dy = diry[dir];
  15319. unit_blown(&sd->bl, dx, dy, cells, 0);
  15320. return SCRIPT_CMD_SUCCESS;
  15321. }
  15322. /// Invokes buying store preparation window
  15323. /// buyingstore <slots>;
  15324. BUILDIN_FUNC(buyingstore)
  15325. {
  15326. struct map_session_data* sd;
  15327. if( ( sd = script_rid2sd(st) ) == NULL )
  15328. {
  15329. return 0;
  15330. }
  15331. if( npc_isnear(&sd->bl) ) {
  15332. char output[150];
  15333. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  15334. clif_displaymessage(sd->fd, output);
  15335. return 0;
  15336. }
  15337. buyingstore_setup(sd, script_getnum(st,2));
  15338. return SCRIPT_CMD_SUCCESS;
  15339. }
  15340. /// Invokes search store info window
  15341. /// searchstores <uses>,<effect>;
  15342. BUILDIN_FUNC(searchstores)
  15343. {
  15344. unsigned short effect;
  15345. unsigned int uses;
  15346. struct map_session_data* sd;
  15347. if( ( sd = script_rid2sd(st) ) == NULL )
  15348. {
  15349. return 0;
  15350. }
  15351. uses = script_getnum(st,2);
  15352. effect = script_getnum(st,3);
  15353. if( !uses )
  15354. {
  15355. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  15356. return 1;
  15357. }
  15358. if( effect > 1 )
  15359. {
  15360. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  15361. return 1;
  15362. }
  15363. searchstore_open(sd, uses, effect);
  15364. return SCRIPT_CMD_SUCCESS;
  15365. }
  15366. /// Displays a number as large digital clock.
  15367. /// showdigit <value>[,<type>];
  15368. BUILDIN_FUNC(showdigit)
  15369. {
  15370. unsigned int type = 0;
  15371. int value;
  15372. struct map_session_data* sd;
  15373. if( ( sd = script_rid2sd(st) ) == NULL )
  15374. {
  15375. return 0;
  15376. }
  15377. value = script_getnum(st,2);
  15378. if( script_hasdata(st,3) )
  15379. {
  15380. type = script_getnum(st,3);
  15381. if( type > 3 )
  15382. {
  15383. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  15384. return 1;
  15385. }
  15386. }
  15387. clif_showdigit(sd, (unsigned char)type, value);
  15388. return SCRIPT_CMD_SUCCESS;
  15389. }
  15390. /**
  15391. * Rune Knight
  15392. **/
  15393. BUILDIN_FUNC(makerune) {
  15394. TBL_PC* sd;
  15395. if( (sd = script_rid2sd(st)) == NULL )
  15396. return 0;
  15397. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  15398. sd->itemid = script_getnum(st,2);
  15399. return SCRIPT_CMD_SUCCESS;
  15400. }
  15401. /**
  15402. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  15403. **/
  15404. BUILDIN_FUNC(checkdragon) {
  15405. TBL_PC* sd;
  15406. if( (sd = script_rid2sd(st)) == NULL )
  15407. return 0;
  15408. if( pc_isridingdragon(sd) )
  15409. script_pushint(st,1);
  15410. else
  15411. script_pushint(st,0);
  15412. return SCRIPT_CMD_SUCCESS;
  15413. }
  15414. /**
  15415. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  15416. * - Toggles the dragon on a RK if he can mount;
  15417. * @param Color - when not provided uses the green dragon;
  15418. * - 1 : Green Dragon
  15419. * - 2 : Brown Dragon
  15420. * - 3 : Gray Dragon
  15421. * - 4 : Blue Dragon
  15422. * - 5 : Red Dragon
  15423. **/
  15424. BUILDIN_FUNC(setdragon) {
  15425. TBL_PC* sd;
  15426. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  15427. if( (sd = script_rid2sd(st)) == NULL )
  15428. return 0;
  15429. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  15430. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  15431. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  15432. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  15433. script_pushint(st,1);
  15434. } else {//Not mounted; Mount now.
  15435. unsigned int option = OPTION_DRAGON1;
  15436. if( color ) {
  15437. option = ( color == 1 ? OPTION_DRAGON1 :
  15438. color == 2 ? OPTION_DRAGON2 :
  15439. color == 3 ? OPTION_DRAGON3 :
  15440. color == 4 ? OPTION_DRAGON4 :
  15441. color == 5 ? OPTION_DRAGON5 : 0);
  15442. if( !option ) {
  15443. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  15444. option = OPTION_DRAGON1;
  15445. }
  15446. }
  15447. pc_setoption(sd, sd->sc.option|option);
  15448. script_pushint(st,1);
  15449. }
  15450. return SCRIPT_CMD_SUCCESS;
  15451. }
  15452. /**
  15453. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  15454. **/
  15455. BUILDIN_FUNC(ismounting) {
  15456. TBL_PC* sd;
  15457. if( (sd = script_rid2sd(st)) == NULL )
  15458. return 0;
  15459. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  15460. script_pushint(st,1);
  15461. else
  15462. script_pushint(st,0);
  15463. return SCRIPT_CMD_SUCCESS;
  15464. }
  15465. /**
  15466. * setmounting() returns 1 on success or 0 otherwise
  15467. * - Toggles new mounts on a player when he can mount
  15468. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  15469. * - Will unmount the player is he is already mounting
  15470. **/
  15471. BUILDIN_FUNC(setmounting) {
  15472. TBL_PC* sd;
  15473. if( (sd = script_rid2sd(st)) == NULL )
  15474. return 0;
  15475. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  15476. clif_msgtable(sd->fd, NEED_REINS_OF_MOUNT);
  15477. script_pushint(st,0); //can't mount with one of these
  15478. } else {
  15479. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  15480. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  15481. else
  15482. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  15483. script_pushint(st,1);//in both cases, return 1.
  15484. }
  15485. return SCRIPT_CMD_SUCCESS;
  15486. }
  15487. /**
  15488. * Retrieves quantity of arguments provided to callfunc/callsub.
  15489. * getargcount() -> amount of arguments received in a function
  15490. **/
  15491. BUILDIN_FUNC(getargcount) {
  15492. struct script_retinfo* ri;
  15493. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  15494. ShowError("script:getargcount: used out of function or callsub label!\n");
  15495. st->state = END;
  15496. return 1;
  15497. }
  15498. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  15499. script_pushint(st, ri->nargs);
  15500. return SCRIPT_CMD_SUCCESS;
  15501. }
  15502. /**
  15503. * getcharip(<account ID>/<character ID>/<character name>)
  15504. **/
  15505. BUILDIN_FUNC(getcharip)
  15506. {
  15507. struct map_session_data* sd = NULL;
  15508. /* check if a character name is specified */
  15509. if( script_hasdata(st, 2) )
  15510. {
  15511. struct script_data *data;
  15512. data = script_getdata(st, 2);
  15513. get_val(st, data); // Convert into value in case of a variable
  15514. if (data_isstring(data))
  15515. sd = map_nick2sd(script_getstr(st, 2));
  15516. else if (data_isint(data) || script_getnum(st, 2))
  15517. {
  15518. int id = 0;
  15519. id = script_getnum(st, 2);
  15520. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  15521. }
  15522. }
  15523. else
  15524. sd = script_rid2sd(st);
  15525. /* check for sd and IP */
  15526. if (!sd || !session[sd->fd]->client_addr)
  15527. {
  15528. script_pushconststr(st, "");
  15529. return 0;
  15530. }
  15531. /* return the client ip_addr converted for output */
  15532. if (sd && sd->fd && session[sd->fd])
  15533. {
  15534. /* initiliaze */
  15535. const char *ip_addr = NULL;
  15536. uint32 ip;
  15537. /* set ip, ip_addr and convert to ip and push str */
  15538. ip = session[sd->fd]->client_addr;
  15539. ip_addr = ip2str(ip, NULL);
  15540. script_pushstrcopy(st, ip_addr);
  15541. }
  15542. return SCRIPT_CMD_SUCCESS;
  15543. }
  15544. /**
  15545. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  15546. **/
  15547. BUILDIN_FUNC(is_function) {
  15548. const char* str = script_getstr(st,2);
  15549. if( strdb_exists(userfunc_db, str) )
  15550. script_pushint(st,1);
  15551. else
  15552. script_pushint(st,0);
  15553. return SCRIPT_CMD_SUCCESS;
  15554. }
  15555. /**
  15556. * get_revision() -> retrieves the current svn revision (if available)
  15557. **/
  15558. BUILDIN_FUNC(get_revision) {
  15559. const char *svn = get_svn_revision();
  15560. if ( svn[0] != UNKNOWN_VERSION )
  15561. script_pushint(st,atoi(svn));
  15562. else
  15563. script_pushint(st,-1); //unknown
  15564. return SCRIPT_CMD_SUCCESS;
  15565. }
  15566. /* get_hash() -> retrieves the current git hash (if available)*/
  15567. BUILDIN_FUNC(get_githash) {
  15568. const char* git = get_git_hash();
  15569. char buf[CHAT_SIZE_MAX];
  15570. safestrncpy(buf,git,strlen(git)+1);
  15571. if ( git[0] != UNKNOWN_VERSION )
  15572. script_pushstr(st,buf);
  15573. else
  15574. script_pushstr(st,"Unknown"); //unknown
  15575. return SCRIPT_CMD_SUCCESS;
  15576. }
  15577. /**
  15578. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  15579. **/
  15580. BUILDIN_FUNC(freeloop) {
  15581. if( script_hasdata(st,2) ) {
  15582. if( script_getnum(st,2) )
  15583. st->freeloop = 1;
  15584. else
  15585. st->freeloop = 0;
  15586. }
  15587. script_pushint(st, st->freeloop);
  15588. return SCRIPT_CMD_SUCCESS;
  15589. }
  15590. /**
  15591. * @commands (script based)
  15592. **/
  15593. BUILDIN_FUNC(bindatcmd) {
  15594. const char* atcmd;
  15595. const char* eventName;
  15596. int i, level = 0, level2 = 0;
  15597. bool create = false;
  15598. atcmd = script_getstr(st,2);
  15599. eventName = script_getstr(st,3);
  15600. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15601. atcmd++;
  15602. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  15603. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  15604. if( atcmd_binding_count == 0 ) {
  15605. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  15606. create = true;
  15607. } else {
  15608. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  15609. if( i < atcmd_binding_count ) {/* update existent entry */
  15610. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15611. atcmd_binding[i]->level = level;
  15612. atcmd_binding[i]->level2 = level2;
  15613. } else
  15614. create = true;
  15615. }
  15616. if( create ) {
  15617. i = atcmd_binding_count;
  15618. if( atcmd_binding_count++ != 0 )
  15619. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  15620. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  15621. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  15622. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15623. atcmd_binding[i]->level = level;
  15624. atcmd_binding[i]->level2 = level2;
  15625. }
  15626. return SCRIPT_CMD_SUCCESS;
  15627. }
  15628. BUILDIN_FUNC(unbindatcmd) {
  15629. const char* atcmd;
  15630. int i = 0;
  15631. atcmd = script_getstr(st, 2);
  15632. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15633. atcmd++;
  15634. if( atcmd_binding_count == 0 ) {
  15635. script_pushint(st, 0);
  15636. return 0;
  15637. }
  15638. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  15639. if( i < atcmd_binding_count ) {
  15640. int cursor = 0;
  15641. aFree(atcmd_binding[i]);
  15642. atcmd_binding[i] = NULL;
  15643. /* compact the list now that we freed a slot somewhere */
  15644. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  15645. if( atcmd_binding[i] == NULL )
  15646. continue;
  15647. if( cursor != i ) {
  15648. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  15649. }
  15650. cursor++;
  15651. }
  15652. if( (atcmd_binding_count = cursor) == 0 )
  15653. aFree(atcmd_binding);
  15654. script_pushint(st, 1);
  15655. } else
  15656. script_pushint(st, 0);/* not found */
  15657. return SCRIPT_CMD_SUCCESS;
  15658. }
  15659. BUILDIN_FUNC(useatcmd) {
  15660. return atcommand_sub(st,3);
  15661. }
  15662. BUILDIN_FUNC(checkre)
  15663. {
  15664. int num;
  15665. num=script_getnum(st,2);
  15666. switch(num){
  15667. case 0:
  15668. #ifdef RENEWAL
  15669. script_pushint(st, 1);
  15670. #else
  15671. script_pushint(st, 0);
  15672. #endif
  15673. break;
  15674. case 1:
  15675. #ifdef RENEWAL_CAST
  15676. script_pushint(st, 1);
  15677. #else
  15678. script_pushint(st, 0);
  15679. #endif
  15680. break;
  15681. case 2:
  15682. #ifdef RENEWAL_DROP
  15683. script_pushint(st, 1);
  15684. #else
  15685. script_pushint(st, 0);
  15686. #endif
  15687. break;
  15688. case 3:
  15689. #ifdef RENEWAL_EXP
  15690. script_pushint(st, 1);
  15691. #else
  15692. script_pushint(st, 0);
  15693. #endif
  15694. break;
  15695. case 4:
  15696. #ifdef RENEWAL_LVDMG
  15697. script_pushint(st, 1);
  15698. #else
  15699. script_pushint(st, 0);
  15700. #endif
  15701. break;
  15702. case 5:
  15703. #ifdef RENEWAL_ASPD
  15704. script_pushint(st, 1);
  15705. #else
  15706. script_pushint(st, 0);
  15707. #endif
  15708. break;
  15709. default:
  15710. ShowWarning("buildin_checkre: unknown parameter.\n");
  15711. break;
  15712. }
  15713. return SCRIPT_CMD_SUCCESS;
  15714. }
  15715. /* getrandgroupitem <group_id>,<quantity>{,<sub_group>} */
  15716. BUILDIN_FUNC(getrandgroupitem) {
  15717. TBL_PC* sd;
  15718. int i, get_count = 0, flag;
  15719. unsigned short nameid;
  15720. uint16 group = script_getnum(st,2), qty = script_getnum(st,3);
  15721. uint8 sub_group = script_getnum(st,4);
  15722. struct item item_tmp;
  15723. if (!( sd = script_rid2sd(st)))
  15724. return SCRIPT_CMD_SUCCESS;
  15725. if (!group) {
  15726. ShowError("getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  15727. return SCRIPT_CMD_FAILURE;
  15728. }
  15729. if ((nameid = itemdb_searchrandomid(group,sub_group)) == UNKNOWN_ITEM_ID) {
  15730. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  15731. }
  15732. memset(&item_tmp,0,sizeof(item_tmp));
  15733. item_tmp.nameid = nameid;
  15734. item_tmp.identify = itemdb_isidentified(nameid);
  15735. if (!qty)
  15736. qty = itemdb_get_randgroupitem_count(group,sub_group,nameid);
  15737. //Check if it's stackable.
  15738. if (!itemdb_isstackable(nameid)) {
  15739. item_tmp.amount = 1;
  15740. get_count = qty;
  15741. }
  15742. else {
  15743. item_tmp.amount = qty;
  15744. get_count = 1;
  15745. }
  15746. for (i = 0; i < get_count; i++) {
  15747. // if not pet egg
  15748. if (!pet_create_egg(sd, nameid)) {
  15749. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  15750. clif_additem(sd,0,0,flag);
  15751. if (pc_candrop(sd,&item_tmp))
  15752. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  15753. }
  15754. }
  15755. }
  15756. return SCRIPT_CMD_SUCCESS;
  15757. }
  15758. /* getgroupitem <group_id>;
  15759. * Gives item(s) to the attached player based on item group contents
  15760. */
  15761. BUILDIN_FUNC(getgroupitem) {
  15762. TBL_PC *sd;
  15763. int group_id = script_getnum(st,2);
  15764. if (!(sd = script_rid2sd(st)))
  15765. return SCRIPT_CMD_SUCCESS;
  15766. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  15767. ShowError("getgroupitem: Invalid group id '%d' specified.",group_id);
  15768. return SCRIPT_CMD_FAILURE;
  15769. }
  15770. return SCRIPT_CMD_SUCCESS;
  15771. }
  15772. /* cleanmap <map_name>;
  15773. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  15774. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  15775. {
  15776. nullpo_ret(bl);
  15777. map_clearflooritem(bl);
  15778. return SCRIPT_CMD_SUCCESS;
  15779. }
  15780. BUILDIN_FUNC(cleanmap)
  15781. {
  15782. const char *map;
  15783. int16 m;
  15784. int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  15785. map = script_getstr(st, 2);
  15786. m = map_mapname2mapid(map);
  15787. if (!m)
  15788. return 1;
  15789. if ((script_lastdata(st) - 2) < 4) {
  15790. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  15791. } else {
  15792. x0 = script_getnum(st, 3);
  15793. y0 = script_getnum(st, 4);
  15794. x1 = script_getnum(st, 5);
  15795. y1 = script_getnum(st, 6);
  15796. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  15797. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  15798. } else {
  15799. ShowError("cleanarea: invalid coordinate defined!\n");
  15800. return 1;
  15801. }
  15802. }
  15803. return SCRIPT_CMD_SUCCESS;
  15804. }
  15805. /* Cast a skill on the attached player.
  15806. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  15807. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  15808. BUILDIN_FUNC(npcskill)
  15809. {
  15810. uint16 skill_id;
  15811. unsigned short skill_level;
  15812. unsigned int stat_point;
  15813. unsigned int npc_level;
  15814. struct npc_data *nd;
  15815. struct map_session_data *sd;
  15816. struct script_data *data;
  15817. data = script_getdata(st, 2);
  15818. get_val(st, data); // Convert into value in case of a variable
  15819. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  15820. skill_level = script_getnum(st, 3);
  15821. stat_point = script_getnum(st, 4);
  15822. npc_level = script_getnum(st, 5);
  15823. sd = script_rid2sd(st);
  15824. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  15825. if (stat_point > battle_config.max_third_parameter) {
  15826. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  15827. return 1;
  15828. }
  15829. if (npc_level > MAX_LEVEL) {
  15830. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  15831. return 1;
  15832. }
  15833. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  15834. return 1;
  15835. }
  15836. nd->level = npc_level;
  15837. nd->stat_point = stat_point;
  15838. if (!nd->status.hp)
  15839. status_calc_npc(nd, SCO_FIRST);
  15840. else
  15841. status_calc_npc(nd, SCO_NONE);
  15842. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  15843. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  15844. else
  15845. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  15846. return SCRIPT_CMD_SUCCESS;
  15847. }
  15848. /* Consumes an item.
  15849. * consumeitem <item id>;
  15850. * consumeitem "<item name>";
  15851. * @param item: Item ID or name
  15852. */
  15853. BUILDIN_FUNC(consumeitem)
  15854. {
  15855. TBL_PC *sd;
  15856. struct script_data *data;
  15857. struct item_data *item_data;
  15858. nullpo_retr( 1, ( sd = script_rid2sd( st ) ) );
  15859. data = script_getdata( st, 2 );
  15860. get_val( st, data );
  15861. if( data_isstring( data ) ){
  15862. const char *name = conv_str( st, data );
  15863. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  15864. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  15865. return SCRIPT_CMD_FAILURE;
  15866. }
  15867. }else if( data_isint( data ) ){
  15868. unsigned short nameid = conv_num( st, data );
  15869. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  15870. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  15871. return SCRIPT_CMD_FAILURE;
  15872. }
  15873. }else{
  15874. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).", data->type );
  15875. return SCRIPT_CMD_FAILURE;
  15876. }
  15877. run_script( item_data->script, 0, sd->bl.id, 0 );
  15878. return SCRIPT_CMD_SUCCESS;
  15879. }
  15880. /** Makes a player sit/stand.
  15881. * sit {"<character name>"};
  15882. * stand {"<character name>"};
  15883. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  15884. * @param name: Player name that will be invoked
  15885. */
  15886. BUILDIN_FUNC(sit)
  15887. {
  15888. TBL_PC *sd;
  15889. if( script_hasdata(st, 2) )
  15890. sd = map_nick2sd(script_getstr(st, 2));
  15891. else
  15892. sd = script_rid2sd(st);
  15893. if( sd == NULL)
  15894. return SCRIPT_CMD_FAILURE;
  15895. if( !pc_issit(sd) ) {
  15896. skill_sit(sd, 1);
  15897. pc_setsit(sd);
  15898. clif_sitting(&sd->bl);
  15899. }
  15900. return SCRIPT_CMD_SUCCESS;
  15901. }
  15902. /** Makes player to stand
  15903. * @param name: Player name that will be set
  15904. */
  15905. BUILDIN_FUNC(stand)
  15906. {
  15907. TBL_PC *sd;
  15908. if( script_hasdata(st, 2) )
  15909. sd = map_nick2sd(script_getstr(st, 2));
  15910. else
  15911. sd = script_rid2sd(st);
  15912. if( sd == NULL)
  15913. return SCRIPT_CMD_FAILURE;
  15914. if( pc_issit(sd) ) {
  15915. pc_setstand(sd);
  15916. skill_sit(sd, 0);
  15917. clif_standing(&sd->bl);
  15918. }
  15919. return SCRIPT_CMD_SUCCESS;
  15920. }
  15921. /** Creates an array of bounded item IDs
  15922. * countbound {<type>};
  15923. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  15924. * @return amt: Amount of items found
  15925. */
  15926. BUILDIN_FUNC(countbound)
  15927. {
  15928. int i, type, j = 0, k = 0;
  15929. TBL_PC *sd;
  15930. if( (sd = script_rid2sd(st)) == NULL )
  15931. return SCRIPT_CMD_FAILURE;
  15932. type = script_getnum(st,2);
  15933. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  15934. if( sd->status.inventory[i].nameid > 0 && (
  15935. (!type && sd->status.inventory[i].bound) || (type && sd->status.inventory[i].bound == type)
  15936. ))
  15937. {
  15938. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  15939. k++;
  15940. j += sd->status.inventory[i].amount;
  15941. }
  15942. }
  15943. script_pushint(st,j);
  15944. return SCRIPT_CMD_SUCCESS;
  15945. }
  15946. /** Creates new party
  15947. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  15948. * @param party_name: String of party name that will be created
  15949. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  15950. * @param item_share: 0-Each Take. 1-Party Share
  15951. * @param item_share_type: 0-Each Take. 1-Even Share
  15952. * @return val: Result value
  15953. * -3 - party name is exist
  15954. * -2 - player is in party already
  15955. * -1 - player is not found
  15956. * 0 - unknown error
  15957. * 1 - success, will return party id $@party_create_id
  15958. */
  15959. BUILDIN_FUNC(party_create)
  15960. {
  15961. char party_name[NAME_LENGTH];
  15962. int item1 = 0, item2 = 0;
  15963. TBL_PC *sd;
  15964. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  15965. script_pushint(st,-1);
  15966. return SCRIPT_CMD_FAILURE;
  15967. }
  15968. if( sd->status.party_id ) {
  15969. script_pushint(st,-2);
  15970. return SCRIPT_CMD_FAILURE;
  15971. }
  15972. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  15973. trim(party_name);
  15974. if( party_searchname(party_name) ) {
  15975. script_pushint(st,-3);
  15976. return SCRIPT_CMD_FAILURE;
  15977. }
  15978. if( script_getnum(st,4) )
  15979. item1 = 1;
  15980. if( script_getnum(st,5) )
  15981. item2 = 1;
  15982. party_create_byscript = 1;
  15983. script_pushint(st,party_create(sd,party_name,item1,item2));
  15984. return SCRIPT_CMD_SUCCESS;
  15985. }
  15986. /** Adds player to specified party
  15987. * party_addmember <party id>,<char id>;
  15988. * @param party_id: The party that will be entered by player
  15989. * @param char_id: Char id of player that will be joined to the party
  15990. * @return val: Result value
  15991. * -4 - party is full
  15992. * -3 - party is not found
  15993. * -2 - player is in party already
  15994. * -1 - player is not found
  15995. * 0 - unknown error
  15996. * 1 - success
  15997. */
  15998. BUILDIN_FUNC(party_addmember)
  15999. {
  16000. int party_id = script_getnum(st,2);
  16001. TBL_PC *sd;
  16002. struct party_data *party;
  16003. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  16004. script_pushint(st,-1);
  16005. return SCRIPT_CMD_FAILURE;
  16006. }
  16007. if( sd->status.party_id ) {
  16008. script_pushint(st,-2);
  16009. return SCRIPT_CMD_FAILURE;
  16010. }
  16011. if( !(party = party_search(party_id)) ) {
  16012. script_pushint(st,-3);
  16013. return SCRIPT_CMD_FAILURE;
  16014. }
  16015. if( party->party.count >= MAX_PARTY ) {
  16016. script_pushint(st,-4);
  16017. return SCRIPT_CMD_FAILURE;
  16018. }
  16019. sd->party_invite = party_id;
  16020. script_pushint(st,party_add_member(party_id,sd));
  16021. return SCRIPT_CMD_SUCCESS;
  16022. }
  16023. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  16024. * party_delmember {<char id>,<party_id>};
  16025. * @param: char_id
  16026. * @param: party_id
  16027. * @return val: Result value
  16028. * -3 - player is not in party
  16029. * -2 - party is not found
  16030. * -1 - player is not found
  16031. * 0 - unknown error
  16032. * 1 - success
  16033. */
  16034. BUILDIN_FUNC(party_delmember)
  16035. {
  16036. TBL_PC *sd = NULL;
  16037. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  16038. script_pushint(st,-1);
  16039. return SCRIPT_CMD_FAILURE;
  16040. }
  16041. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  16042. script_pushint(st,party_removemember2(sd,0,0));
  16043. else
  16044. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  16045. return SCRIPT_CMD_SUCCESS;
  16046. }
  16047. /** Changes party leader of specified party (even the leader is offline)
  16048. * party_changeleader <party id>,<char id>;
  16049. * @param party_id: ID of party
  16050. * @param char_id: Char ID of new leader
  16051. * @return val: Result value
  16052. * -4 - player is party leader already
  16053. * -3 - player is not in this party
  16054. * -2 - player is not found
  16055. * -1 - party is not found
  16056. * 0 - unknown error
  16057. * 1 - success */
  16058. BUILDIN_FUNC(party_changeleader)
  16059. {
  16060. int i, party_id = script_getnum(st,2);
  16061. TBL_PC *sd = NULL;
  16062. TBL_PC *tsd = NULL;
  16063. struct party_data *party = NULL;
  16064. if( !(party = party_search(party_id)) ) {
  16065. script_pushint(st,-1);
  16066. return SCRIPT_CMD_FAILURE;
  16067. }
  16068. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  16069. script_pushint(st,-2);
  16070. return SCRIPT_CMD_FAILURE;
  16071. }
  16072. if( tsd->status.party_id != party_id ) {
  16073. script_pushint(st,-3);
  16074. return SCRIPT_CMD_FAILURE;
  16075. }
  16076. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  16077. if( i >= MAX_PARTY ) { //this is should impossible!
  16078. script_pushint(st,0);
  16079. return SCRIPT_CMD_FAILURE;
  16080. }
  16081. if( party->data[i].sd == tsd ) {
  16082. script_pushint(st,-4);
  16083. return SCRIPT_CMD_FAILURE;
  16084. }
  16085. script_pushint(st,party_changeleader(sd,tsd,party));
  16086. return SCRIPT_CMD_SUCCESS;
  16087. }
  16088. /** Changes party option
  16089. * party_changeoption <party id>,<option>,<flag>;
  16090. * @param party_id: ID of party that will be changed
  16091. * @param option: Type of option
  16092. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  16093. */
  16094. BUILDIN_FUNC(party_changeoption)
  16095. {
  16096. struct party_data *party;
  16097. if( !(party = party_search(script_getnum(st,2))) ) {
  16098. script_pushint(st,-1);
  16099. return SCRIPT_CMD_FAILURE;
  16100. }
  16101. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  16102. return SCRIPT_CMD_SUCCESS;
  16103. }
  16104. /** Destroys party with party id.
  16105. * party_destroy <party id>;
  16106. * @param party_id: ID of party that will be destroyed
  16107. */
  16108. BUILDIN_FUNC(party_destroy)
  16109. {
  16110. int i;
  16111. struct party_data *party;
  16112. if( !(party = party_search(script_getnum(st,2))) ) {
  16113. script_pushint(st,0);
  16114. return SCRIPT_CMD_FAILURE;
  16115. }
  16116. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  16117. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  16118. int j;
  16119. for( j = 0; j < MAX_PARTY; j++ ) {
  16120. TBL_PC *sd = party->data[j].sd;
  16121. if(sd)
  16122. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  16123. else if( party->party.member[j].char_id )
  16124. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  16125. }
  16126. party_broken(party->party.party_id);
  16127. script_pushint(st,1);
  16128. }
  16129. else //leader leave = party broken
  16130. script_pushint(st,party_leave(party->data[i].sd));
  16131. return SCRIPT_CMD_SUCCESS;
  16132. }
  16133. /** Checks if a player's client version meets a required version or date.
  16134. * @param type: 0 - check by version number; 1 - check by date
  16135. * @param data: Input
  16136. */
  16137. BUILDIN_FUNC(is_clientver) {
  16138. TBL_PC *sd = NULL;
  16139. int type = script_getnum(st,2);
  16140. int data = script_getnum(st,3);
  16141. int ret = 0;
  16142. if (script_hasdata(st,4))
  16143. sd = map_charid2sd(script_getnum(st,4));
  16144. else
  16145. sd = script_rid2sd(st);
  16146. if (sd == NULL) {
  16147. script_pushint(st,0);
  16148. return SCRIPT_CMD_FAILURE;
  16149. }
  16150. switch(type){
  16151. case 0:
  16152. ret = (sd->packet_ver >= data)?1:0;
  16153. break;
  16154. case 1:
  16155. ret = (sd->packet_ver >= date2version(data))?1:0;
  16156. break;
  16157. }
  16158. script_pushint(st,ret);
  16159. return SCRIPT_CMD_SUCCESS;
  16160. }
  16161. /** Returns various information about a player's VIP status. Need to enable VIP system
  16162. * vip_status <type>,{"<character name>"};
  16163. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  16164. * @param name: Character name (Optional)
  16165. */
  16166. BUILDIN_FUNC(vip_status) {
  16167. #ifdef VIP_ENABLE
  16168. TBL_PC *sd;
  16169. char vip_str[26];
  16170. time_t now = time(NULL);
  16171. int type = script_getnum(st, 2);
  16172. if (script_hasdata(st, 3))
  16173. sd = map_nick2sd(script_getstr(st, 3));
  16174. else
  16175. sd = script_rid2sd(st);
  16176. if (sd == NULL)
  16177. return SCRIPT_CMD_FAILURE;
  16178. switch(type) {
  16179. case 1: // Get VIP status.
  16180. script_pushint(st, pc_isvip(sd));
  16181. break;
  16182. case 2: // Get VIP expire date.
  16183. if (pc_isvip(sd)) {
  16184. time_t viptime = sd->vip.time;
  16185. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  16186. vip_str[25] = '\0';
  16187. script_pushstrcopy(st, vip_str);
  16188. } else
  16189. script_pushint(st, 0);
  16190. break;
  16191. case 3: // Get remaining time.
  16192. if (pc_isvip(sd)) {
  16193. time_t viptime_remain = sd->vip.time - now;
  16194. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  16195. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  16196. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  16197. script_pushstrcopy(st, vip_str);
  16198. } else
  16199. script_pushint(st, 0);
  16200. break;
  16201. }
  16202. #else
  16203. script_pushint(st, 0);
  16204. #endif
  16205. return SCRIPT_CMD_SUCCESS;
  16206. }
  16207. /** Adds or removes VIP time in minutes. Need to enable VIP system
  16208. * vip_time <time in mn>,{"<character name>"};
  16209. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  16210. * @param name: Character name (optional)
  16211. */
  16212. BUILDIN_FUNC(vip_time) {
  16213. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  16214. TBL_PC *sd;
  16215. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  16216. if (script_hasdata(st, 3))
  16217. sd = map_nick2sd(script_getstr(st, 3));
  16218. else
  16219. sd = script_rid2sd(st);
  16220. if (sd == NULL)
  16221. return SCRIPT_CMD_FAILURE;
  16222. chrif_req_login_operation(sd->status.account_id, sd->status.name, 6, viptime, 7, 0);
  16223. #endif
  16224. return SCRIPT_CMD_SUCCESS;
  16225. }
  16226. /** Turns a player into a monster and grants SC attribute effect. [malufett/Hercules]
  16227. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  16228. * @param monster: Monster ID or name
  16229. * @param duration: Transform duration in millisecond (ms)
  16230. * @param sc_type: Type of SC that will be affected during the transformation
  16231. * @param val1: Value for SC
  16232. * @param val2: Value for SC
  16233. * @param val3: Value for SC
  16234. * @param val4: Value for SC
  16235. */
  16236. BUILDIN_FUNC(montransform) {
  16237. TBL_PC *sd;
  16238. enum sc_type type;
  16239. char msg[CHAT_SIZE_MAX];
  16240. int tick, mob_id, val1, val2, val3, val4;
  16241. struct script_data *data;
  16242. if( (sd = script_rid2sd(st)) == NULL )
  16243. return 1;
  16244. data = script_getdata(st, 2);
  16245. get_val(st, data); // Convert into value in case of a variable
  16246. if( data_isstring(data) )
  16247. mob_id = mobdb_searchname(script_getstr(st, 2));
  16248. else
  16249. mob_id = mobdb_checkid(script_getnum(st, 2));
  16250. tick = script_getnum(st, 3);
  16251. type = (sc_type)script_getnum(st, 4);
  16252. val1 = val2 = val3 = val4 = 0;
  16253. if (mob_id == 0) {
  16254. if( data_isstring(data) )
  16255. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  16256. else
  16257. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  16258. return SCRIPT_CMD_FAILURE;
  16259. }
  16260. if (mob_id == MOBID_EMPERIUM) {
  16261. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  16262. return SCRIPT_CMD_FAILURE;
  16263. }
  16264. if (!(type > SC_NONE && type < SC_MAX)) {
  16265. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  16266. return SCRIPT_CMD_FAILURE;
  16267. }
  16268. if (script_hasdata(st, 5))
  16269. val1 = script_getnum(st, 5);
  16270. if (script_hasdata(st, 6))
  16271. val2 = script_getnum(st, 6);
  16272. if (script_hasdata(st, 7))
  16273. val3 = script_getnum(st, 7);
  16274. if (script_hasdata(st, 8))
  16275. val4 = script_getnum(st, 8);
  16276. if (tick != 0) {
  16277. struct mob_db *monster = mob_db(mob_id);
  16278. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  16279. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  16280. return SCRIPT_CMD_FAILURE;
  16281. }
  16282. if (sd->disguise){
  16283. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  16284. return SCRIPT_CMD_FAILURE;
  16285. }
  16286. sprintf(msg, msg_txt(sd,728), monster->name); // Traaaansformation-!! %s form!!
  16287. clif_disp_overhead(&sd->bl, msg);
  16288. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  16289. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  16290. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  16291. }
  16292. return SCRIPT_CMD_SUCCESS;
  16293. }
  16294. /** [Cydh]
  16295. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  16296. * @param "script code"
  16297. * @param duration
  16298. * @param flag
  16299. * @param icon
  16300. * @param char_id
  16301. **/
  16302. BUILDIN_FUNC(bonus_script) {
  16303. uint8 i, flag = 0;
  16304. int16 icon = SI_BLANK;
  16305. uint32 dur;
  16306. char type = 0;
  16307. TBL_PC* sd;
  16308. const char *script_str = NULL;
  16309. struct script_code *script = NULL;
  16310. if (script_hasdata(st,7))
  16311. sd = map_charid2sd(script_getnum(st,7));
  16312. else
  16313. sd = script_rid2sd(st);
  16314. if (sd == NULL)
  16315. return SCRIPT_CMD_FAILURE;
  16316. script_str = script_getstr(st,2);
  16317. dur = 1000 * abs(script_getnum(st,3));
  16318. FETCH(4,flag);
  16319. FETCH(5,type);
  16320. FETCH(6,icon);
  16321. if (script_str[0] == '\0' || !dur) {
  16322. //ShowWarning("buildin_bonus_script: Invalid script. Skipping...\n");
  16323. return SCRIPT_CMD_FAILURE;
  16324. }
  16325. //Skip duplicate entry
  16326. ARR_FIND(0,MAX_PC_BONUS_SCRIPT,i,&sd->bonus_script[i] && sd->bonus_script[i].script_str && strcmp(sd->bonus_script[i].script_str,script_str) == 0);
  16327. if (i < MAX_PC_BONUS_SCRIPT) {
  16328. //ShowWarning("buildin_bonus_script: Duplicate entry with bonus '%d'. Skipping...\n",i);
  16329. return SCRIPT_CMD_SUCCESS;
  16330. }
  16331. if (!(script = parse_script(script_str,"bonus_script",0,1))) {
  16332. ShowWarning("buildin_bonus_script: Failed to parse script '%s' (cid:%d). Skipping...\n",script_str,sd->status.char_id);
  16333. return SCRIPT_CMD_FAILURE;
  16334. }
  16335. //Find the empty slot
  16336. ARR_FIND(0,MAX_PC_BONUS_SCRIPT,i,!sd->bonus_script[i].script);
  16337. if (i >= MAX_PC_BONUS_SCRIPT) {
  16338. ShowWarning("buildin_itemscript: Maximum script_bonus is reached (cid:%d max: %d). Skipping...\n",sd->status.char_id,MAX_PC_BONUS_SCRIPT);
  16339. return SCRIPT_CMD_SUCCESS;
  16340. }
  16341. //Add the script data
  16342. memcpy(sd->bonus_script[i].script_str,script_str,strlen(script_str)+1);
  16343. sd->bonus_script[i].script = script;
  16344. sd->bonus_script[i].tick = gettick() + dur;
  16345. sd->bonus_script[i].flag = flag;
  16346. sd->bonus_script[i].type = type;
  16347. sd->bonus_script[i].icon = icon;
  16348. if (sd->bonus_script[i].icon != SI_BLANK) //Gives status icon if exist
  16349. clif_status_change(&sd->bl,sd->bonus_script[i].icon,1,dur,1,0,0);
  16350. status_calc_pc(sd,SCO_NONE);
  16351. return SCRIPT_CMD_SUCCESS;
  16352. }
  16353. /** Allows player to use atcommand while talking with NPC
  16354. * @author [Cydh], [Kichi] */
  16355. BUILDIN_FUNC(enable_command) {
  16356. TBL_PC* sd = script_rid2sd(st);
  16357. if (!sd)
  16358. return SCRIPT_CMD_FAILURE;
  16359. sd->state.disable_atcommand_on_npc = false;
  16360. return SCRIPT_CMD_SUCCESS;
  16361. }
  16362. /** Prevents player to use atcommand while talking with NPC
  16363. * @author [Cydh], [Kichi] */
  16364. BUILDIN_FUNC(disable_command) {
  16365. TBL_PC* sd = script_rid2sd(st);
  16366. if (!sd)
  16367. return SCRIPT_CMD_FAILURE;
  16368. sd->state.disable_atcommand_on_npc = true;
  16369. return SCRIPT_CMD_SUCCESS;
  16370. }
  16371. /** Get the information of the members of a guild by type.
  16372. * getguildmember <guild_id>{,<type>};
  16373. * @param guild_id: ID of guild
  16374. * @param type: Type of option (optional)
  16375. */
  16376. BUILDIN_FUNC(getguildmember)
  16377. {
  16378. int i, j = 0, type = 0;
  16379. struct guild *g = NULL;
  16380. g = guild_search(script_getnum(st,2));
  16381. if (script_hasdata(st,3))
  16382. type = script_getnum(st,3);
  16383. if (g) {
  16384. for (i = 0; i < MAX_GUILD; i++) {
  16385. if (g->member[i].account_id) {
  16386. switch (type) {
  16387. case 2:
  16388. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  16389. break;
  16390. case 1:
  16391. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  16392. break;
  16393. default:
  16394. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  16395. break;
  16396. }
  16397. j++;
  16398. }
  16399. }
  16400. }
  16401. mapreg_setreg(add_str("$@guildmembercount"), j);
  16402. return SCRIPT_CMD_SUCCESS;
  16403. }
  16404. /** Adds spirit ball to player for 'duration' in milisecond
  16405. * @param count How many spirit ball will be added
  16406. * @param duration How long spiritball is active until it disappears
  16407. * @param char_id Target player (Optional)
  16408. * @author [Cydh]
  16409. */
  16410. BUILDIN_FUNC(addspiritball) {
  16411. uint8 i, count = script_getnum(st,2);
  16412. uint16 duration = script_getnum(st,3);
  16413. struct map_session_data *sd = NULL;
  16414. if (count == 0)
  16415. return SCRIPT_CMD_SUCCESS;
  16416. if (script_hasdata(st,4)) {
  16417. if (script_isstring(st,4))
  16418. sd = map_charid2sd(script_getnum(st,4));
  16419. else
  16420. sd = map_nick2sd(script_getstr(st,4));
  16421. }
  16422. else
  16423. sd = script_rid2sd(st);
  16424. if (!sd)
  16425. return SCRIPT_CMD_FAILURE;
  16426. for (i = 0; i < count; i++)
  16427. pc_addspiritball(sd,duration,10);
  16428. return SCRIPT_CMD_SUCCESS;
  16429. }
  16430. /** Deletes the spirit ball(s) from player
  16431. * @param count How many spirit ball will be deleted
  16432. * @param char_id Target player (Optional)
  16433. * @author [Cydh]
  16434. */
  16435. BUILDIN_FUNC(delspiritball) {
  16436. uint8 count = script_getnum(st,2);
  16437. struct map_session_data *sd = NULL;
  16438. if (count == 0)
  16439. count = 1;
  16440. if (script_hasdata(st,3)) {
  16441. if (script_isstring(st,3))
  16442. sd = map_charid2sd(script_getnum(st,3));
  16443. else
  16444. sd = map_nick2sd(script_getstr(st,3));
  16445. }
  16446. else
  16447. sd = script_rid2sd(st);
  16448. if (!sd)
  16449. return SCRIPT_CMD_FAILURE;
  16450. pc_delspiritball(sd,count,0);
  16451. return SCRIPT_CMD_SUCCESS;
  16452. }
  16453. /** Counts the spirit ball that player has
  16454. * @param char_id Target player (Optional)
  16455. * @author [Cydh]
  16456. */
  16457. BUILDIN_FUNC(countspiritball) {
  16458. struct map_session_data *sd;
  16459. if (script_hasdata(st,2)) {
  16460. if (script_isstring(st,2))
  16461. sd = map_charid2sd(script_getnum(st,2));
  16462. else
  16463. sd = map_nick2sd(script_getstr(st,2));
  16464. }
  16465. else
  16466. sd = script_rid2sd(st);
  16467. if (!sd)
  16468. return SCRIPT_CMD_FAILURE;
  16469. script_pushint(st,sd->spiritball);
  16470. return SCRIPT_CMD_SUCCESS;
  16471. }
  16472. #include "../custom/script.inc"
  16473. // declarations that were supposed to be exported from npc_chat.c
  16474. #ifdef PCRE_SUPPORT
  16475. BUILDIN_FUNC(defpattern);
  16476. BUILDIN_FUNC(activatepset);
  16477. BUILDIN_FUNC(deactivatepset);
  16478. BUILDIN_FUNC(deletepset);
  16479. #endif
  16480. /** Regular expression matching
  16481. * preg_match(<pattern>,<string>{,<offset>})
  16482. */
  16483. BUILDIN_FUNC(preg_match) {
  16484. #ifdef PCRE_SUPPORT
  16485. pcre *re;
  16486. pcre_extra *pcreExtra;
  16487. const char *error;
  16488. int erroffset, r, offset = 0;
  16489. int subStrVec[30];
  16490. const char* pattern = script_getstr(st,2);
  16491. const char* subject = script_getstr(st,3);
  16492. if (script_hasdata(st,4))
  16493. offset = script_getnum(st,4);
  16494. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  16495. pcreExtra = pcre_study(re, 0, &error);
  16496. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  16497. pcre_free(re);
  16498. if (pcreExtra != NULL)
  16499. pcre_free(pcreExtra);
  16500. if (r < 0)
  16501. script_pushint(st,0);
  16502. else
  16503. script_pushint(st,(r > 0) ? r : 30 / 3);
  16504. return SCRIPT_CMD_SUCCESS;
  16505. #else
  16506. ShowError("script:preg_match: cannot run without PCRE library enabled.\n");
  16507. script_pushint(st,0);
  16508. return SCRIPT_CMD_FAILURE;
  16509. #endif
  16510. }
  16511. /// script command definitions
  16512. /// for an explanation on args, see add_buildin_func
  16513. struct script_function buildin_func[] = {
  16514. // NPC interaction
  16515. BUILDIN_DEF(mes,"s*"),
  16516. BUILDIN_DEF(next,""),
  16517. BUILDIN_DEF(close,""),
  16518. BUILDIN_DEF(close2,""),
  16519. BUILDIN_DEF(menu,"sl*"),
  16520. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  16521. BUILDIN_DEF(prompt,"s*"),
  16522. //
  16523. BUILDIN_DEF(goto,"l"),
  16524. BUILDIN_DEF(callsub,"l*"),
  16525. BUILDIN_DEF(callfunc,"s*"),
  16526. BUILDIN_DEF(return,"?"),
  16527. BUILDIN_DEF(getarg,"i?"),
  16528. BUILDIN_DEF(jobchange,"i?"),
  16529. BUILDIN_DEF(jobname,"i"),
  16530. BUILDIN_DEF(input,"r??"),
  16531. BUILDIN_DEF(warp,"sii"),
  16532. BUILDIN_DEF(areawarp,"siiiisii??"),
  16533. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  16534. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  16535. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  16536. BUILDIN_DEF(setlook,"ii"),
  16537. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  16538. BUILDIN_DEF(set,"rv"),
  16539. BUILDIN_DEF(setarray,"rv*"),
  16540. BUILDIN_DEF(cleararray,"rvi"),
  16541. BUILDIN_DEF(copyarray,"rri"),
  16542. BUILDIN_DEF(getarraysize,"r"),
  16543. BUILDIN_DEF(deletearray,"r?"),
  16544. BUILDIN_DEF(getelementofarray,"ri"),
  16545. BUILDIN_DEF(getitem,"vi?"),
  16546. BUILDIN_DEF(rentitem,"vi"),
  16547. BUILDIN_DEF(rentitem2,"viiiiiiii"),
  16548. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  16549. BUILDIN_DEF(getnameditem,"vv"),
  16550. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  16551. BUILDIN_DEF(makeitem,"visii"),
  16552. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  16553. BUILDIN_DEF(delitem,"vi?"),
  16554. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  16555. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  16556. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  16557. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  16558. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  16559. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  16560. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  16561. BUILDIN_DEF(cutin,"si"),
  16562. BUILDIN_DEF(viewpoint,"iiiii"),
  16563. BUILDIN_DEF(heal,"ii"),
  16564. BUILDIN_DEF(itemheal,"ii"),
  16565. BUILDIN_DEF(percentheal,"ii"),
  16566. BUILDIN_DEF(rand,"i?"),
  16567. BUILDIN_DEF(countitem,"v?"),
  16568. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  16569. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  16570. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  16571. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  16572. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  16573. BUILDIN_DEF(checkweight,"vi*"),
  16574. BUILDIN_DEF(checkweight2,"rr"),
  16575. BUILDIN_DEF(readparam,"i?"),
  16576. BUILDIN_DEF(getcharid,"i?"),
  16577. BUILDIN_DEF(getnpcid,"i?"),
  16578. BUILDIN_DEF(getpartyname,"i"),
  16579. BUILDIN_DEF(getpartymember,"i?"),
  16580. BUILDIN_DEF(getpartyleader,"i?"),
  16581. BUILDIN_DEF(getguildname,"i"),
  16582. BUILDIN_DEF(getguildmaster,"i"),
  16583. BUILDIN_DEF(getguildmasterid,"i"),
  16584. BUILDIN_DEF(strcharinfo,"i"),
  16585. BUILDIN_DEF(strnpcinfo,"i"),
  16586. BUILDIN_DEF(getequipid,"i"),
  16587. BUILDIN_DEF(getequipname,"i"),
  16588. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  16589. BUILDIN_DEF(repair,"i"), // [Valaris]
  16590. BUILDIN_DEF(repairall,""),
  16591. BUILDIN_DEF(getequipisequiped,"i"),
  16592. BUILDIN_DEF(getequipisenableref,"i"),
  16593. BUILDIN_DEF(getequiprefinerycnt,"i"),
  16594. BUILDIN_DEF(getequipweaponlv,"i"),
  16595. BUILDIN_DEF(getequippercentrefinery,"i"),
  16596. BUILDIN_DEF(successrefitem,"i?"),
  16597. BUILDIN_DEF(failedrefitem,"i"),
  16598. BUILDIN_DEF(downrefitem,"i?"),
  16599. BUILDIN_DEF(statusup,"i"),
  16600. BUILDIN_DEF(statusup2,"ii"),
  16601. BUILDIN_DEF(bonus,"iv"),
  16602. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  16603. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  16604. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  16605. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  16606. BUILDIN_DEF(autobonus,"sii??"),
  16607. BUILDIN_DEF(autobonus2,"sii??"),
  16608. BUILDIN_DEF(autobonus3,"siiv?"),
  16609. BUILDIN_DEF(skill,"vi?"),
  16610. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  16611. BUILDIN_DEF(guildskill,"vi"),
  16612. BUILDIN_DEF(getskilllv,"v"),
  16613. BUILDIN_DEF(getgdskilllv,"iv"),
  16614. BUILDIN_DEF(basicskillcheck,""),
  16615. BUILDIN_DEF(getgmlevel,""),
  16616. BUILDIN_DEF(getgroupid,""),
  16617. BUILDIN_DEF(end,""),
  16618. BUILDIN_DEF(checkoption,"i"),
  16619. BUILDIN_DEF(setoption,"i?"),
  16620. BUILDIN_DEF(setcart,"?"),
  16621. BUILDIN_DEF(checkcart,""),
  16622. BUILDIN_DEF(setfalcon,"?"),
  16623. BUILDIN_DEF(checkfalcon,""),
  16624. BUILDIN_DEF(setriding,"?"),
  16625. BUILDIN_DEF(checkriding,""),
  16626. BUILDIN_DEF(checkwug,""),
  16627. BUILDIN_DEF(checkmadogear,""),
  16628. BUILDIN_DEF(setmadogear,"?"),
  16629. BUILDIN_DEF2(savepoint,"save","sii"),
  16630. BUILDIN_DEF(savepoint,"sii"),
  16631. BUILDIN_DEF(gettimetick,"i"),
  16632. BUILDIN_DEF(gettime,"i"),
  16633. BUILDIN_DEF(gettimestr,"si"),
  16634. BUILDIN_DEF(openstorage,""),
  16635. BUILDIN_DEF(guildopenstorage,""),
  16636. BUILDIN_DEF(itemskill,"vi"),
  16637. BUILDIN_DEF(produce,"i"),
  16638. BUILDIN_DEF(cooking,"i"),
  16639. BUILDIN_DEF(monster,"siisii???"),
  16640. BUILDIN_DEF(getmobdrops,"i"),
  16641. BUILDIN_DEF(areamonster,"siiiisii???"),
  16642. BUILDIN_DEF(killmonster,"ss?"),
  16643. BUILDIN_DEF(killmonsterall,"s?"),
  16644. BUILDIN_DEF(clone,"siisi????"),
  16645. BUILDIN_DEF(doevent,"s"),
  16646. BUILDIN_DEF(donpcevent,"s"),
  16647. BUILDIN_DEF(cmdothernpc,"ss"),
  16648. BUILDIN_DEF(addtimer,"is"),
  16649. BUILDIN_DEF(deltimer,"s"),
  16650. BUILDIN_DEF(addtimercount,"is"),
  16651. BUILDIN_DEF(initnpctimer,"??"),
  16652. BUILDIN_DEF(stopnpctimer,"??"),
  16653. BUILDIN_DEF(startnpctimer,"??"),
  16654. BUILDIN_DEF(setnpctimer,"i?"),
  16655. BUILDIN_DEF(getnpctimer,"i?"),
  16656. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  16657. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  16658. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  16659. BUILDIN_DEF(announce,"si?????"),
  16660. BUILDIN_DEF(mapannounce,"ssi?????"),
  16661. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  16662. BUILDIN_DEF(getusers,"i"),
  16663. BUILDIN_DEF(getmapguildusers,"si"),
  16664. BUILDIN_DEF(getmapusers,"s"),
  16665. BUILDIN_DEF(getareausers,"siiii"),
  16666. BUILDIN_DEF(getareadropitem,"siiiiv"),
  16667. BUILDIN_DEF(enablenpc,"s"),
  16668. BUILDIN_DEF(disablenpc,"s"),
  16669. BUILDIN_DEF(hideoffnpc,"s"),
  16670. BUILDIN_DEF(hideonnpc,"s"),
  16671. BUILDIN_DEF(sc_start,"iii???"),
  16672. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  16673. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  16674. BUILDIN_DEF(sc_end,"i?"),
  16675. BUILDIN_DEF(getstatus, "i?"),
  16676. BUILDIN_DEF(getscrate,"ii?"),
  16677. BUILDIN_DEF(debugmes,"s"),
  16678. BUILDIN_DEF2(catchpet,"pet","i"),
  16679. BUILDIN_DEF2(birthpet,"bpet",""),
  16680. BUILDIN_DEF(resetlvl,"i"),
  16681. BUILDIN_DEF(resetstatus,""),
  16682. BUILDIN_DEF(resetskill,""),
  16683. BUILDIN_DEF(skillpointcount,""),
  16684. BUILDIN_DEF(changebase,"i?"),
  16685. BUILDIN_DEF(changesex,""),
  16686. BUILDIN_DEF(waitingroom,"si?????"),
  16687. BUILDIN_DEF(delwaitingroom,"?"),
  16688. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  16689. BUILDIN_DEF(enablewaitingroomevent,"?"),
  16690. BUILDIN_DEF(disablewaitingroomevent,"?"),
  16691. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  16692. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  16693. BUILDIN_DEF(getwaitingroomstate,"i?"),
  16694. BUILDIN_DEF(warpwaitingpc,"sii?"),
  16695. BUILDIN_DEF(attachrid,"i"),
  16696. BUILDIN_DEF(addrid,"i?????"),
  16697. BUILDIN_DEF(detachrid,""),
  16698. BUILDIN_DEF(isloggedin,"i?"),
  16699. BUILDIN_DEF(setmapflagnosave,"ssii"),
  16700. BUILDIN_DEF(getmapflag,"si?"),
  16701. BUILDIN_DEF(setmapflag,"si??"),
  16702. BUILDIN_DEF(removemapflag,"si?"),
  16703. BUILDIN_DEF(pvpon,"s"),
  16704. BUILDIN_DEF(pvpoff,"s"),
  16705. BUILDIN_DEF(gvgon,"s"),
  16706. BUILDIN_DEF(gvgoff,"s"),
  16707. BUILDIN_DEF(emotion,"i??"),
  16708. BUILDIN_DEF(maprespawnguildid,"sii"),
  16709. BUILDIN_DEF(agitstart,""), // <Agit>
  16710. BUILDIN_DEF(agitend,""),
  16711. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  16712. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  16713. BUILDIN_DEF(getcastlename,"s"),
  16714. BUILDIN_DEF(getcastledata,"si"),
  16715. BUILDIN_DEF(setcastledata,"sii"),
  16716. BUILDIN_DEF(requestguildinfo,"i?"),
  16717. BUILDIN_DEF(getequipcardcnt,"i"),
  16718. BUILDIN_DEF(successremovecards,"i"),
  16719. BUILDIN_DEF(failedremovecards,"ii"),
  16720. BUILDIN_DEF(marriage,"s"),
  16721. BUILDIN_DEF2(wedding_effect,"wedding",""),
  16722. BUILDIN_DEF(divorce,""),
  16723. BUILDIN_DEF(ispartneron,""),
  16724. BUILDIN_DEF(getpartnerid,""),
  16725. BUILDIN_DEF(getchildid,""),
  16726. BUILDIN_DEF(getmotherid,""),
  16727. BUILDIN_DEF(getfatherid,""),
  16728. BUILDIN_DEF(warppartner,"sii"),
  16729. BUILDIN_DEF(getitemname,"v"),
  16730. BUILDIN_DEF(getitemslots,"i"),
  16731. BUILDIN_DEF(makepet,"i"),
  16732. BUILDIN_DEF(getexp,"ii"),
  16733. BUILDIN_DEF(getinventorylist,""),
  16734. BUILDIN_DEF(getskilllist,""),
  16735. BUILDIN_DEF(clearitem,""),
  16736. BUILDIN_DEF(classchange,"ii"),
  16737. BUILDIN_DEF(misceffect,"i"),
  16738. BUILDIN_DEF(playBGM,"s"),
  16739. BUILDIN_DEF(playBGMall,"s?????"),
  16740. BUILDIN_DEF(soundeffect,"si"),
  16741. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  16742. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  16743. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  16744. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  16745. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  16746. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  16747. BUILDIN_DEF(petloot,"i"), // [Valaris]
  16748. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  16749. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  16750. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  16751. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  16752. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  16753. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  16754. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  16755. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  16756. BUILDIN_DEF(nude,""), // nude command [Valaris]
  16757. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  16758. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  16759. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  16760. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  16761. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  16762. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  16763. BUILDIN_DEF(mobcount,"ss"),
  16764. BUILDIN_DEF(getlook,"i"),
  16765. BUILDIN_DEF(getsavepoint,"i"),
  16766. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  16767. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  16768. BUILDIN_DEF(npcstop,""), // [Valaris]
  16769. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  16770. BUILDIN_DEF(checkoption1,"i"),
  16771. BUILDIN_DEF(checkoption2,"i"),
  16772. BUILDIN_DEF(guildgetexp,"i"),
  16773. BUILDIN_DEF(guildchangegm,"is"),
  16774. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  16775. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  16776. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  16777. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  16778. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  16779. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  16780. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  16781. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  16782. BUILDIN_DEF(night,""), // sets the server to night time
  16783. BUILDIN_DEF(day,""), // sets the server to day time
  16784. #ifdef PCRE_SUPPORT
  16785. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  16786. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  16787. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  16788. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  16789. #endif
  16790. BUILDIN_DEF(preg_match,"ss?"),
  16791. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  16792. BUILDIN_DEF(getusersname,""),
  16793. BUILDIN_DEF(recovery,"i???"),
  16794. BUILDIN_DEF(getpetinfo,"i"),
  16795. BUILDIN_DEF(gethominfo,"i"),
  16796. BUILDIN_DEF(getmercinfo,"i?"),
  16797. BUILDIN_DEF(checkequipedcard,"i"),
  16798. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  16799. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  16800. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  16801. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  16802. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  16803. BUILDIN_DEF(charat,"si"),
  16804. BUILDIN_DEF(setchar,"ssi"),
  16805. BUILDIN_DEF(insertchar,"ssi"),
  16806. BUILDIN_DEF(delchar,"si"),
  16807. BUILDIN_DEF(strtoupper,"s"),
  16808. BUILDIN_DEF(strtolower,"s"),
  16809. BUILDIN_DEF(charisupper, "si"),
  16810. BUILDIN_DEF(charislower, "si"),
  16811. BUILDIN_DEF(substr,"sii"),
  16812. BUILDIN_DEF(explode, "rss"),
  16813. BUILDIN_DEF(implode, "r?"),
  16814. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  16815. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  16816. BUILDIN_DEF(strpos,"ss?"),
  16817. BUILDIN_DEF(replacestr,"sss??"),
  16818. BUILDIN_DEF(countstr,"ss?"),
  16819. BUILDIN_DEF(setnpcdisplay,"sv??"),
  16820. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  16821. BUILDIN_DEF(strcmp,"ss"),
  16822. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  16823. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  16824. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  16825. // [zBuffer] List of mathematics commands --->
  16826. BUILDIN_DEF(sqrt,"i"),
  16827. BUILDIN_DEF(pow,"ii"),
  16828. BUILDIN_DEF(distance,"iiii"),
  16829. // <--- [zBuffer] List of mathematics commands
  16830. BUILDIN_DEF(md5,"s"),
  16831. // [zBuffer] List of dynamic var commands --->
  16832. BUILDIN_DEF(getd,"s"),
  16833. BUILDIN_DEF(setd,"sv"),
  16834. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  16835. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  16836. BUILDIN_DEF(npcshopadditem,"sii*"),
  16837. BUILDIN_DEF(npcshopdelitem,"si*"),
  16838. BUILDIN_DEF(npcshopattach,"s?"),
  16839. BUILDIN_DEF(equip,"i"),
  16840. BUILDIN_DEF(autoequip,"ii"),
  16841. BUILDIN_DEF(setbattleflag,"si"),
  16842. BUILDIN_DEF(getbattleflag,"s"),
  16843. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  16844. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  16845. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  16846. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  16847. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  16848. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  16849. BUILDIN_DEF(axtoi,"s"),
  16850. BUILDIN_DEF(query_sql,"s*"),
  16851. BUILDIN_DEF(query_logsql,"s*"),
  16852. BUILDIN_DEF(escape_sql,"v"),
  16853. BUILDIN_DEF(atoi,"s"),
  16854. BUILDIN_DEF(strtol,"si"),
  16855. // [zBuffer] List of player cont commands --->
  16856. BUILDIN_DEF(rid2name,"i"),
  16857. BUILDIN_DEF(pcfollow,"ii"),
  16858. BUILDIN_DEF(pcstopfollow,"i"),
  16859. BUILDIN_DEF(pcblockmove,"ii"),
  16860. // <--- [zBuffer] List of player cont commands
  16861. // [zBuffer] List of mob control commands --->
  16862. BUILDIN_DEF(unitwalk,"ii?"),
  16863. BUILDIN_DEF(unitkill,"i"),
  16864. BUILDIN_DEF(unitwarp,"isii"),
  16865. BUILDIN_DEF(unitattack,"iv?"),
  16866. BUILDIN_DEF(unitstop,"i"),
  16867. BUILDIN_DEF(unittalk,"is"),
  16868. BUILDIN_DEF(unitemote,"ii"),
  16869. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  16870. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  16871. // <--- [zBuffer] List of mob control commands
  16872. BUILDIN_DEF(sleep,"i"),
  16873. BUILDIN_DEF(sleep2,"i"),
  16874. BUILDIN_DEF(awake,"s"),
  16875. BUILDIN_DEF(getvariableofnpc,"rs"),
  16876. BUILDIN_DEF(warpportal,"iisii"),
  16877. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  16878. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  16879. BUILDIN_DEF(morphembryo,""),
  16880. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  16881. BUILDIN_DEF(checkhomcall,""),
  16882. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  16883. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  16884. BUILDIN_DEF(checkvending,"?"),
  16885. BUILDIN_DEF(checkchatting,"?"),
  16886. BUILDIN_DEF(checkidle,"?"),
  16887. BUILDIN_DEF(openmail,""),
  16888. BUILDIN_DEF(openauction,""),
  16889. BUILDIN_DEF(checkcell,"siii"),
  16890. BUILDIN_DEF(setcell,"siiiiii"),
  16891. BUILDIN_DEF(setwall,"siiiiis"),
  16892. BUILDIN_DEF(delwall,"s"),
  16893. BUILDIN_DEF(searchitem,"rs"),
  16894. BUILDIN_DEF(mercenary_create,"ii"),
  16895. BUILDIN_DEF(mercenary_heal,"ii"),
  16896. BUILDIN_DEF(mercenary_sc_start,"iii"),
  16897. BUILDIN_DEF(mercenary_get_calls,"i"),
  16898. BUILDIN_DEF(mercenary_get_faith,"i"),
  16899. BUILDIN_DEF(mercenary_set_calls,"ii"),
  16900. BUILDIN_DEF(mercenary_set_faith,"ii"),
  16901. BUILDIN_DEF(readbook,"ii"),
  16902. BUILDIN_DEF(setfont,"i"),
  16903. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  16904. BUILDIN_DEF(progressbar,"si"),
  16905. BUILDIN_DEF(pushpc,"ii"),
  16906. BUILDIN_DEF(buyingstore,"i"),
  16907. BUILDIN_DEF(searchstores,"ii"),
  16908. BUILDIN_DEF(showdigit,"i?"),
  16909. // WoE SE
  16910. BUILDIN_DEF(agitstart2,""),
  16911. BUILDIN_DEF(agitend2,""),
  16912. BUILDIN_DEF(agitcheck2,""),
  16913. // BattleGround
  16914. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  16915. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  16916. BUILDIN_DEF(bg_team_setxy,"iii"),
  16917. BUILDIN_DEF(bg_warp,"isii"),
  16918. BUILDIN_DEF(bg_monster,"isiisi?"),
  16919. BUILDIN_DEF(bg_monster_set_team,"ii"),
  16920. BUILDIN_DEF(bg_leave,""),
  16921. BUILDIN_DEF(bg_destroy,"i"),
  16922. BUILDIN_DEF(areapercentheal,"siiiiii"),
  16923. BUILDIN_DEF(bg_get_data,"ii"),
  16924. BUILDIN_DEF(bg_getareausers,"isiiii"),
  16925. BUILDIN_DEF(bg_updatescore,"sii"),
  16926. // Instancing
  16927. BUILDIN_DEF(instance_create,"s"),
  16928. BUILDIN_DEF(instance_destroy,"?"),
  16929. BUILDIN_DEF(instance_id,""),
  16930. BUILDIN_DEF(instance_enter,"s??"),
  16931. BUILDIN_DEF(instance_npcname,"s?"),
  16932. BUILDIN_DEF(instance_mapname,"s?"),
  16933. BUILDIN_DEF(instance_warpall,"sii?"),
  16934. BUILDIN_DEF(instance_announce,"isi?????"),
  16935. BUILDIN_DEF(instance_check_party,"i???"),
  16936. /**
  16937. * 3rd-related
  16938. **/
  16939. BUILDIN_DEF(makerune,"i"),
  16940. BUILDIN_DEF(checkdragon,""),//[Ind]
  16941. BUILDIN_DEF(setdragon,"?"),//[Ind]
  16942. BUILDIN_DEF(ismounting,""),//[Ind]
  16943. BUILDIN_DEF(setmounting,""),//[Ind]
  16944. BUILDIN_DEF(checkre,"i"),
  16945. /**
  16946. * rAthena and beyond!
  16947. **/
  16948. BUILDIN_DEF(getargcount,""),
  16949. BUILDIN_DEF(getcharip,"?"),
  16950. BUILDIN_DEF(is_function,"s"),
  16951. BUILDIN_DEF(get_revision,""),
  16952. BUILDIN_DEF(get_githash,""),
  16953. BUILDIN_DEF(freeloop,"?"),
  16954. BUILDIN_DEF(getrandgroupitem,"ii?"),
  16955. BUILDIN_DEF(cleanmap,"s"),
  16956. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  16957. BUILDIN_DEF(npcskill,"viii"),
  16958. BUILDIN_DEF(consumeitem,"v"),
  16959. BUILDIN_DEF(delequip,"i"),
  16960. BUILDIN_DEF(breakequip,"i"),
  16961. BUILDIN_DEF(sit,"?"),
  16962. BUILDIN_DEF(stand,"?"),
  16963. //@commands (script based)
  16964. BUILDIN_DEF(bindatcmd, "ss??"),
  16965. BUILDIN_DEF(unbindatcmd, "s"),
  16966. BUILDIN_DEF(useatcmd, "s"),
  16967. //Quest Log System [Inkfish]
  16968. BUILDIN_DEF(questinfo, "ii??"),
  16969. BUILDIN_DEF(setquest, "i"),
  16970. BUILDIN_DEF(erasequest, "i"),
  16971. BUILDIN_DEF(completequest, "i"),
  16972. BUILDIN_DEF(checkquest, "i?"),
  16973. BUILDIN_DEF(isbegin_quest,"i"),
  16974. BUILDIN_DEF(changequest, "ii"),
  16975. BUILDIN_DEF(showevent, "i?"),
  16976. //Bound items [Xantara] & [Akinari]
  16977. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  16978. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  16979. BUILDIN_DEF(countbound, "?"),
  16980. // Party related
  16981. BUILDIN_DEF(party_create,"s???"),
  16982. BUILDIN_DEF(party_addmember,"ii"),
  16983. BUILDIN_DEF(party_delmember,"??"),
  16984. BUILDIN_DEF(party_changeleader,"ii"),
  16985. BUILDIN_DEF(party_changeoption,"iii"),
  16986. BUILDIN_DEF(party_destroy,"i"),
  16987. BUILDIN_DEF(is_clientver,"ii?"),
  16988. BUILDIN_DEF2(montransform, "transform", "vii????"), // Monster Transform [malufett/Hercules]
  16989. BUILDIN_DEF(vip_status,"i?"),
  16990. BUILDIN_DEF(vip_time,"i?"),
  16991. BUILDIN_DEF(bonus_script,"si????"),
  16992. BUILDIN_DEF(getgroupitem,"i"),
  16993. BUILDIN_DEF(enable_command,""),
  16994. BUILDIN_DEF(disable_command,""),
  16995. BUILDIN_DEF(getguildmember,"i?"),
  16996. BUILDIN_DEF(addspiritball,"ii?"),
  16997. BUILDIN_DEF(delspiritball,"i?"),
  16998. BUILDIN_DEF(countspiritball,"?"),
  16999. #include "../custom/script_def.inc"
  17000. {NULL,NULL,NULL},
  17001. };