script.c 499 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629
  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 short 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 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. int i;
  563. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  564. for( i = 2; i <= script_lastdata(st); i++ )
  565. {
  566. script_reportdata(script_getdata(st,i));
  567. }
  568. }
  569. else
  570. {
  571. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  572. }
  573. }
  574. /*==========================================
  575. * Output error message
  576. *------------------------------------------*/
  577. static void disp_error_message2(const char *mes,const char *pos,int report)
  578. {
  579. error_msg = aStrdup(mes);
  580. error_pos = pos;
  581. error_report = report;
  582. longjmp( error_jump, 1 );
  583. }
  584. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  585. static void disp_warning_message(const char *mes, const char *pos) {
  586. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  587. }
  588. /// Checks event parameter validity
  589. static void check_event(struct script_state *st, const char *evt)
  590. {
  591. if( evt && evt[0] && !stristr(evt, "::On") )
  592. {
  593. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  594. script_reportsrc(st);
  595. }
  596. }
  597. /*==========================================
  598. * Hashes the input string
  599. *------------------------------------------*/
  600. static unsigned int calc_hash(const char* p)
  601. {
  602. unsigned int h;
  603. #if defined(SCRIPT_HASH_DJB2)
  604. h = 5381;
  605. while( *p ) // hash*33 + c
  606. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  607. #elif defined(SCRIPT_HASH_SDBM)
  608. h = 0;
  609. while( *p ) // hash*65599 + c
  610. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  611. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  612. h = 0;
  613. while( *p ){
  614. unsigned int g;
  615. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  616. g = h & 0xF0000000;
  617. if( g )
  618. {
  619. h ^= g >> 24;
  620. h &= ~g;
  621. }
  622. }
  623. #else // athena hash
  624. h = 0;
  625. while( *p )
  626. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  627. #endif
  628. return h % SCRIPT_HASH_SIZE;
  629. }
  630. /*==========================================
  631. * str_data manipulation functions
  632. *------------------------------------------*/
  633. /// Looks up string using the provided id.
  634. const char* get_str(int id)
  635. {
  636. Assert( id >= LABEL_START && id < str_size );
  637. return str_buf+str_data[id].str;
  638. }
  639. /// Returns the uid of the string, or -1.
  640. static int search_str(const char* p)
  641. {
  642. int i;
  643. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  644. if( strcasecmp(get_str(i),p) == 0 )
  645. return i;
  646. return -1;
  647. }
  648. /// Stores a copy of the string and returns its id.
  649. /// If an identical string is already present, returns its id instead.
  650. int add_str(const char* p)
  651. {
  652. int h;
  653. int len;
  654. h = calc_hash(p);
  655. if( str_hash[h] == 0 )
  656. {// empty bucket, add new node here
  657. str_hash[h] = str_num;
  658. }
  659. else
  660. {// scan for end of list, or occurence of identical string
  661. int i;
  662. for( i = str_hash[h]; ; i = str_data[i].next )
  663. {
  664. if( strcasecmp(get_str(i),p) == 0 )
  665. return i; // string already in list
  666. if( str_data[i].next == 0 )
  667. break; // reached the end
  668. }
  669. // append node to end of list
  670. str_data[i].next = str_num;
  671. }
  672. // grow list if neccessary
  673. if( str_num >= str_data_size )
  674. {
  675. str_data_size += 128;
  676. RECREATE(str_data,struct str_data_struct,str_data_size);
  677. memset(str_data + (str_data_size - 128), '\0', 128);
  678. }
  679. len=(int)strlen(p);
  680. // grow string buffer if neccessary
  681. while( str_pos+len+1 >= str_size )
  682. {
  683. str_size += 256;
  684. RECREATE(str_buf,char,str_size);
  685. memset(str_buf + (str_size - 256), '\0', 256);
  686. }
  687. safestrncpy(str_buf+str_pos, p, len+1);
  688. str_data[str_num].type = C_NOP;
  689. str_data[str_num].str = str_pos;
  690. str_data[str_num].next = 0;
  691. str_data[str_num].func = NULL;
  692. str_data[str_num].backpatch = -1;
  693. str_data[str_num].label = -1;
  694. str_pos += len+1;
  695. return str_num++;
  696. }
  697. /// Appends 1 byte to the script buffer.
  698. static void add_scriptb(int a)
  699. {
  700. if( script_pos+1 >= script_size )
  701. {
  702. script_size += SCRIPT_BLOCK_SIZE;
  703. RECREATE(script_buf,unsigned char,script_size);
  704. }
  705. script_buf[script_pos++] = (uint8)(a);
  706. }
  707. /// Appends a c_op value to the script buffer.
  708. /// The value is variable-length encoded into 8-bit blocks.
  709. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  710. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  711. static void add_scriptc(int a)
  712. {
  713. while( a >= 0x40 )
  714. {
  715. add_scriptb((a&0x3f)|0x40);
  716. a = (a - 0x40) >> 6;
  717. }
  718. add_scriptb(a);
  719. }
  720. /// Appends an integer value to the script buffer.
  721. /// The value is variable-length encoded into 8-bit blocks.
  722. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  723. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  724. static void add_scripti(int a)
  725. {
  726. while( a >= 0x40 )
  727. {
  728. add_scriptb((a&0x3f)|0xc0);
  729. a = (a - 0x40) >> 6;
  730. }
  731. add_scriptb(a|0x80);
  732. }
  733. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  734. ///
  735. /// @param l The id of the str_data entry
  736. // Maximum up to 16M
  737. static void add_scriptl(int l)
  738. {
  739. int backpatch = str_data[l].backpatch;
  740. switch(str_data[l].type){
  741. case C_POS:
  742. case C_USERFUNC_POS:
  743. add_scriptc(C_POS);
  744. add_scriptb(str_data[l].label);
  745. add_scriptb(str_data[l].label>>8);
  746. add_scriptb(str_data[l].label>>16);
  747. break;
  748. case C_NOP:
  749. case C_USERFUNC:
  750. // Embedded data backpatch there is a possibility of label
  751. add_scriptc(C_NAME);
  752. str_data[l].backpatch = script_pos;
  753. add_scriptb(backpatch);
  754. add_scriptb(backpatch>>8);
  755. add_scriptb(backpatch>>16);
  756. break;
  757. case C_INT:
  758. add_scripti(abs(str_data[l].val));
  759. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  760. add_scriptc(C_NEG);
  761. break;
  762. default: // assume C_NAME
  763. add_scriptc(C_NAME);
  764. add_scriptb(l);
  765. add_scriptb(l>>8);
  766. add_scriptb(l>>16);
  767. break;
  768. }
  769. }
  770. /*==========================================
  771. * Resolve the label
  772. *------------------------------------------*/
  773. void set_label(int l,int pos, const char* script_pos_cur)
  774. {
  775. int i;
  776. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  777. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  778. disp_error_message("set_label: invalid label name",script_pos_cur);
  779. return;
  780. }
  781. if(str_data[l].label!=-1){
  782. disp_error_message("set_label: dup label ",script_pos_cur);
  783. return;
  784. }
  785. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  786. str_data[l].label=pos;
  787. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  788. int next=GETVALUE(script_buf,i);
  789. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  790. SETVALUE(script_buf,i,pos);
  791. i=next;
  792. }
  793. }
  794. /// Skips spaces and/or comments.
  795. const char* skip_space(const char* p)
  796. {
  797. if( p == NULL )
  798. return NULL;
  799. for(;;)
  800. {
  801. while( ISSPACE(*p) )
  802. ++p;
  803. if( *p == '/' && p[1] == '/' )
  804. {// line comment
  805. while(*p && *p!='\n')
  806. ++p;
  807. }
  808. else if( *p == '/' && p[1] == '*' )
  809. {// block comment
  810. p += 2;
  811. for(;;)
  812. {
  813. if( *p == '\0' ) {
  814. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  815. return p;
  816. }
  817. if( *p == '*' && p[1] == '/' )
  818. {// end of block comment
  819. p += 2;
  820. break;
  821. }
  822. ++p;
  823. }
  824. }
  825. else
  826. break;
  827. }
  828. return p;
  829. }
  830. /// Skips a word.
  831. /// A word consists of undercores and/or alphanumeric characters,
  832. /// and valid variable prefixes/postfixes.
  833. static
  834. const char* skip_word(const char* p)
  835. {
  836. // prefix
  837. switch( *p )
  838. {
  839. case '@':// temporary char variable
  840. ++p; break;
  841. case '#':// account variable
  842. p += ( p[1] == '#' ? 2 : 1 ); break;
  843. case '\'':// instance variable
  844. ++p; break;
  845. case '.':// npc variable
  846. p += ( p[1] == '@' ? 2 : 1 ); break;
  847. case '$':// global variable
  848. p += ( p[1] == '@' ? 2 : 1 ); break;
  849. }
  850. while( ISALNUM(*p) || *p == '_' )
  851. ++p;
  852. // postfix
  853. if( *p == '$' )// string
  854. p++;
  855. return p;
  856. }
  857. /// Adds a word to str_data.
  858. /// @see skip_word
  859. /// @see add_str
  860. static
  861. int add_word(const char* p)
  862. {
  863. char* word;
  864. int len;
  865. int i;
  866. // Check for a word
  867. len = skip_word(p) - p;
  868. if( len == 0 )
  869. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  870. // Duplicate the word
  871. word = (char*)aMalloc(len+1);
  872. memcpy(word, p, len);
  873. word[len] = 0;
  874. // add the word
  875. i = add_str(word);
  876. aFree(word);
  877. return i;
  878. }
  879. /// Parses a function call.
  880. /// The argument list can have parenthesis or not.
  881. /// The number of arguments is checked.
  882. static
  883. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  884. {
  885. const char* p2;
  886. const char* arg=NULL;
  887. int func;
  888. func = add_word(p);
  889. if( str_data[func].type == C_FUNC ){
  890. // buildin function
  891. add_scriptl(func);
  892. add_scriptc(C_ARG);
  893. arg = buildin_func[str_data[func].val].arg;
  894. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  895. // script defined function
  896. add_scriptl(buildin_callsub_ref);
  897. add_scriptc(C_ARG);
  898. add_scriptl(func);
  899. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  900. if( *arg == 0 )
  901. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  902. if( *arg != '*' )
  903. ++arg; // count func as argument
  904. } else {
  905. #ifdef SCRIPT_CALLFUNC_CHECK
  906. const char* name = get_str(func);
  907. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  908. #endif
  909. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  910. #ifdef SCRIPT_CALLFUNC_CHECK
  911. } else {;
  912. add_scriptl(buildin_callfunc_ref);
  913. add_scriptc(C_ARG);
  914. add_scriptc(C_STR);
  915. while( *name ) add_scriptb(*name ++);
  916. add_scriptb(0);
  917. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  918. if( *arg != '*' ) ++ arg;
  919. }
  920. #endif
  921. }
  922. p = skip_word(p);
  923. p = skip_space(p);
  924. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  925. syntax.curly[syntax.curly_count].count = 0;
  926. if( *p == ';' )
  927. {// <func name> ';'
  928. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  929. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  930. {// <func name> '(' ')'
  931. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  932. p = p2;
  933. /*
  934. } else if( 0 && require_paren && *p != '(' )
  935. {// <func name>
  936. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  937. */
  938. } else
  939. {// <func name> <arg list>
  940. if( require_paren ){
  941. if( *p != '(' )
  942. disp_error_message("need '('",p);
  943. ++p; // skip '('
  944. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  945. } else if( *p == '(' ){
  946. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  947. } else {
  948. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  949. }
  950. ++syntax.curly_count;
  951. while( *arg ) {
  952. p2=parse_subexpr(p,-1);
  953. if( p == p2 )
  954. break; // not an argument
  955. if( *arg != '*' )
  956. ++arg; // next argument
  957. p=skip_space(p2);
  958. if( *arg == 0 || *p != ',' )
  959. break; // no more arguments
  960. ++p; // skip comma
  961. }
  962. --syntax.curly_count;
  963. }
  964. if( *arg && *arg != '?' && *arg != '*' )
  965. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  966. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  967. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  968. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  969. if( *p != ')' )
  970. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  971. ++p;
  972. }
  973. add_scriptc(C_FUNC);
  974. return p;
  975. }
  976. /// Processes end of logical script line.
  977. /// @param first When true, only fix up scheduling data is initialized
  978. /// @param p Script position for error reporting in set_label
  979. static void parse_nextline(bool first, const char* p)
  980. {
  981. if( !first )
  982. {
  983. add_scriptc(C_EOL); // mark end of line for stack cleanup
  984. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  985. }
  986. // initialize data for new '-' label fix up scheduling
  987. str_data[LABEL_NEXTLINE].type = C_NOP;
  988. str_data[LABEL_NEXTLINE].backpatch = -1;
  989. str_data[LABEL_NEXTLINE].label = -1;
  990. }
  991. /// Parse a variable assignment using the direct equals operator
  992. /// @param p script position where the function should run from
  993. /// @return NULL if not a variable assignment, the new position otherwise
  994. const char* parse_variable(const char* p) {
  995. int word;
  996. c_op type = C_NOP;
  997. const char *p2 = NULL;
  998. const char *var = p;
  999. // skip the variable where applicable
  1000. p = skip_word(p);
  1001. p = skip_space(p);
  1002. if( p == NULL ) {// end of the line or invalid buffer
  1003. return NULL;
  1004. }
  1005. if( *p == '[' ) {// array variable so process the array as appropriate
  1006. int i,j;
  1007. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1008. if( *p ++ == ']' && --(j) == 0 ) break;
  1009. if( *p == '[' ) ++ j;
  1010. }
  1011. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1012. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1013. }
  1014. }
  1015. if( type == C_NOP &&
  1016. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1017. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1018. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1019. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1020. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1021. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1022. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1023. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1024. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1025. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1026. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  1027. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  1028. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1029. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1030. ) )
  1031. {// failed to find a matching operator combination so invalid
  1032. return NULL;
  1033. }
  1034. switch( type ) {
  1035. case C_EQ: {// incremental modifier
  1036. p = skip_space( &p[1] );
  1037. }
  1038. break;
  1039. case C_L_SHIFT:
  1040. case C_R_SHIFT: {// left or right shift modifier
  1041. p = skip_space( &p[3] );
  1042. }
  1043. break;
  1044. default: {// normal incremental command
  1045. p = skip_space( &p[2] );
  1046. }
  1047. }
  1048. if( p == NULL ) {// end of line or invalid buffer
  1049. return NULL;
  1050. }
  1051. // push the set function onto the stack
  1052. add_scriptl(buildin_set_ref);
  1053. add_scriptc(C_ARG);
  1054. // always append parenthesis to avoid errors
  1055. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1056. syntax.curly[syntax.curly_count].count = 0;
  1057. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1058. // increment the total curly count for the position in the script
  1059. ++ syntax.curly_count;
  1060. // parse the variable currently being modified
  1061. word = add_word(var);
  1062. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  1063. {// cannot assign a variable which exists as a function or label
  1064. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1065. }
  1066. if( p2 ) {// process the variable index
  1067. const char* p3 = NULL;
  1068. // push the getelementofarray method into the stack
  1069. add_scriptl(buildin_getelementofarray_ref);
  1070. add_scriptc(C_ARG);
  1071. add_scriptl(word);
  1072. // process the sub-expression for this assignment
  1073. p3 = parse_subexpr(p2 + 1, 1);
  1074. p3 = skip_space(p3);
  1075. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1076. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1077. }
  1078. // push the closing function stack operator onto the stack
  1079. add_scriptc(C_FUNC);
  1080. p3 ++;
  1081. } else {// simply push the variable or value onto the stack
  1082. add_scriptl(word);
  1083. }
  1084. if( type != C_EQ )
  1085. add_scriptc(C_REF);
  1086. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1087. add_scripti(1);
  1088. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1089. } else {// process the value as an expression
  1090. p = parse_subexpr(p, -1);
  1091. if( type != C_EQ )
  1092. {// push the type of modifier onto the stack
  1093. add_scriptc(type);
  1094. }
  1095. }
  1096. // decrement the curly count for the position within the script
  1097. -- syntax.curly_count;
  1098. // close the script by appending the function operator
  1099. add_scriptc(C_FUNC);
  1100. // push the buffer from the method
  1101. return p;
  1102. }
  1103. /*
  1104. * Checks whether the gives string is a number literal
  1105. *
  1106. * Mainly necessary to differentiate between number literals and NPC name
  1107. * constants, since several of those start with a digit.
  1108. *
  1109. * All this does is to check if the string begins with an optional + or - sign,
  1110. * followed by a hexadecimal or decimal number literal literal and is NOT
  1111. * followed by a underscore or letter.
  1112. *
  1113. * @author : Hercules.ws
  1114. * @param p Pointer to the string to check
  1115. * @return Whether the string is a number literal
  1116. */
  1117. bool is_number(const char *p) {
  1118. const char *np;
  1119. if (!p)
  1120. return false;
  1121. if (*p == '-' || *p == '+')
  1122. p++;
  1123. np = p;
  1124. if (*p == '0' && p[1] == 'x') {
  1125. p+=2;
  1126. np = p;
  1127. // Hexadecimal
  1128. while (ISXDIGIT(*np))
  1129. np++;
  1130. } else {
  1131. // Decimal
  1132. while (ISDIGIT(*np))
  1133. np++;
  1134. }
  1135. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1136. return true;
  1137. return false;
  1138. }
  1139. /*==========================================
  1140. * Analysis section
  1141. *------------------------------------------*/
  1142. const char* parse_simpleexpr(const char *p)
  1143. {
  1144. long long i;
  1145. p=skip_space(p);
  1146. if(*p==';' || *p==',')
  1147. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1148. if(*p=='('){
  1149. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1150. ++syntax.curly[i].count;
  1151. p=parse_subexpr(p+1,-1);
  1152. p=skip_space(p);
  1153. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1154. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1155. ){
  1156. if( *p == ',' ){
  1157. syntax.curly[i].flag = ARGLIST_PAREN;
  1158. return p;
  1159. } else
  1160. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1161. }
  1162. if( *p != ')' )
  1163. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1164. ++p;
  1165. } else if(is_number(p)) {
  1166. char *np;
  1167. while(*p == '0' && ISDIGIT(p[1])) p++;
  1168. i=strtoll(p,&np,0);
  1169. if( i < INT_MIN ) {
  1170. i = INT_MIN;
  1171. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1172. } else if( i > INT_MAX ) {
  1173. i = INT_MAX;
  1174. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1175. }
  1176. add_scripti((int)i);
  1177. p=np;
  1178. } else if(*p=='"'){
  1179. add_scriptc(C_STR);
  1180. p++;
  1181. while( *p && *p != '"' ){
  1182. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1183. {
  1184. char buf[8];
  1185. size_t len = skip_escaped_c(p) - p;
  1186. size_t n = sv_unescape_c(buf, p, len);
  1187. if( n != 1 )
  1188. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1189. p += len;
  1190. add_scriptb(*buf);
  1191. continue;
  1192. }
  1193. else if( *p == '\n' )
  1194. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1195. add_scriptb(*p++);
  1196. }
  1197. if(!*p)
  1198. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1199. add_scriptb(0);
  1200. p++; //'"'
  1201. } else {
  1202. int l;
  1203. const char* pv;
  1204. // label , register , function etc
  1205. if(skip_word(p)==p)
  1206. disp_error_message("parse_simpleexpr: unexpected character",p);
  1207. l=add_word(p);
  1208. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1209. return parse_callfunc(p,1,0);
  1210. #ifdef SCRIPT_CALLFUNC_CHECK
  1211. else {
  1212. const char* name = get_str(l);
  1213. if( strdb_get(userfunc_db,name) != NULL ) {
  1214. return parse_callfunc(p,1,1);
  1215. }
  1216. }
  1217. #endif
  1218. if( (pv = parse_variable(p)) )
  1219. {// successfully processed a variable assignment
  1220. return pv;
  1221. }
  1222. p=skip_word(p);
  1223. if( *p == '[' ){
  1224. // array(name[i] => getelementofarray(name,i) )
  1225. add_scriptl(buildin_getelementofarray_ref);
  1226. add_scriptc(C_ARG);
  1227. add_scriptl(l);
  1228. p=parse_subexpr(p+1,-1);
  1229. p=skip_space(p);
  1230. if( *p != ']' )
  1231. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1232. ++p;
  1233. add_scriptc(C_FUNC);
  1234. }else
  1235. add_scriptl(l);
  1236. }
  1237. return p;
  1238. }
  1239. /*==========================================
  1240. * Analysis of the expression
  1241. *------------------------------------------*/
  1242. const char* parse_subexpr(const char* p,int limit)
  1243. {
  1244. int op,opl,len;
  1245. p=skip_space(p);
  1246. if( *p == '-' ){
  1247. const char* tmpp = skip_space(p+1);
  1248. if( *tmpp == ';' || *tmpp == ',' ){
  1249. add_scriptl(LABEL_NEXTLINE);
  1250. p++;
  1251. return p;
  1252. }
  1253. }
  1254. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1255. p=parse_subexpr(p+1,10);
  1256. add_scriptc(op);
  1257. } else
  1258. p=parse_simpleexpr(p);
  1259. p=skip_space(p);
  1260. while((
  1261. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1262. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1263. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1264. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1265. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1266. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1267. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1268. (op=C_AND,opl=6,len=1,*p=='&') ||
  1269. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1270. (op=C_OR,opl=5,len=1,*p=='|') ||
  1271. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1272. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1273. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1274. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1275. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1276. (op=C_GT,opl=3,len=1,*p=='>') ||
  1277. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1278. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1279. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1280. p+=len;
  1281. if(op == C_OP3) {
  1282. p=parse_subexpr(p,-1);
  1283. p=skip_space(p);
  1284. if( *(p++) != ':')
  1285. disp_error_message("parse_subexpr: expected ':'", p-1);
  1286. p=parse_subexpr(p,-1);
  1287. } else {
  1288. p=parse_subexpr(p,opl);
  1289. }
  1290. add_scriptc(op);
  1291. p=skip_space(p);
  1292. }
  1293. return p; /* return first untreated operator */
  1294. }
  1295. /*==========================================
  1296. * Evaluation of the expression
  1297. *------------------------------------------*/
  1298. const char* parse_expr(const char *p)
  1299. {
  1300. switch(*p){
  1301. case ')': case ';': case ':': case '[': case ']':
  1302. case '}':
  1303. disp_error_message("parse_expr: unexpected character",p);
  1304. }
  1305. p=parse_subexpr(p,-1);
  1306. return p;
  1307. }
  1308. /*==========================================
  1309. * Analysis of the line
  1310. *------------------------------------------*/
  1311. const char* parse_line(const char* p)
  1312. {
  1313. const char* p2;
  1314. p=skip_space(p);
  1315. if(*p==';') {
  1316. //Close decision for if(); for(); while();
  1317. p = parse_syntax_close(p + 1);
  1318. return p;
  1319. }
  1320. if(*p==')' && parse_syntax_for_flag)
  1321. return p+1;
  1322. p = skip_space(p);
  1323. if(p[0] == '{') {
  1324. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1325. syntax.curly[syntax.curly_count].count = -1;
  1326. syntax.curly[syntax.curly_count].index = -1;
  1327. syntax.curly_count++;
  1328. return p + 1;
  1329. } else if(p[0] == '}') {
  1330. return parse_curly_close(p);
  1331. }
  1332. // Syntax-related processing
  1333. p2 = parse_syntax(p);
  1334. if(p2 != NULL)
  1335. return p2;
  1336. // attempt to process a variable assignment
  1337. p2 = parse_variable(p);
  1338. if( p2 != NULL )
  1339. {// variable assignment processed so leave the method
  1340. return parse_syntax_close(p2 + 1);
  1341. }
  1342. p = parse_callfunc(p,0,0);
  1343. p = skip_space(p);
  1344. if(parse_syntax_for_flag) {
  1345. if( *p != ')' )
  1346. disp_error_message("parse_line: expected ')'",p);
  1347. } else {
  1348. if( *p != ';' )
  1349. disp_error_message("parse_line: expected ';'",p);
  1350. }
  1351. //Binding decision for if(), for(), while()
  1352. p = parse_syntax_close(p+1);
  1353. return p;
  1354. }
  1355. // { ... } Closing process
  1356. const char* parse_curly_close(const char* p)
  1357. {
  1358. if(syntax.curly_count <= 0) {
  1359. disp_error_message("parse_curly_close: unexpected string",p);
  1360. return p + 1;
  1361. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1362. syntax.curly_count--;
  1363. //Close decision if, for , while
  1364. p = parse_syntax_close(p + 1);
  1365. return p;
  1366. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1367. //Closing switch()
  1368. int pos = syntax.curly_count-1;
  1369. char label[256];
  1370. int l;
  1371. // Remove temporary variables
  1372. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1373. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1374. parse_line(label);
  1375. syntax.curly_count--;
  1376. // Go to the end pointer unconditionally
  1377. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1378. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1379. parse_line(label);
  1380. syntax.curly_count--;
  1381. // You are here labeled
  1382. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1383. l=add_str(label);
  1384. set_label(l,script_pos, p);
  1385. if(syntax.curly[pos].flag) {
  1386. //Exists default
  1387. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1388. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1389. parse_line(label);
  1390. syntax.curly_count--;
  1391. }
  1392. // Label end
  1393. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1394. l=add_str(label);
  1395. set_label(l,script_pos, p);
  1396. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1397. syntax.curly_count--;
  1398. //Closing decision if, for , while
  1399. p = parse_syntax_close(p + 1);
  1400. return p;
  1401. } else {
  1402. disp_error_message("parse_curly_close: unexpected string",p);
  1403. return p + 1;
  1404. }
  1405. }
  1406. // Syntax-related processing
  1407. // break, case, continue, default, do, for, function,
  1408. // if, switch, while ? will handle this internally.
  1409. const char* parse_syntax(const char* p)
  1410. {
  1411. const char *p2 = skip_word(p);
  1412. switch(*p) {
  1413. case 'B':
  1414. case 'b':
  1415. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1416. // break Processing
  1417. char label[256];
  1418. int pos = syntax.curly_count - 1;
  1419. while(pos >= 0) {
  1420. if(syntax.curly[pos].type == TYPE_DO) {
  1421. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1422. break;
  1423. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1424. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1425. break;
  1426. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1427. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1428. break;
  1429. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1430. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1431. break;
  1432. }
  1433. pos--;
  1434. }
  1435. if(pos < 0) {
  1436. disp_error_message("parse_syntax: unexpected 'break'",p);
  1437. } else {
  1438. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1439. parse_line(label);
  1440. syntax.curly_count--;
  1441. }
  1442. p = skip_space(p2);
  1443. if(*p != ';')
  1444. disp_error_message("parse_syntax: expected ';'",p);
  1445. // Closing decision if, for , while
  1446. p = parse_syntax_close(p + 1);
  1447. return p;
  1448. }
  1449. break;
  1450. case 'c':
  1451. case 'C':
  1452. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1453. //Processing case
  1454. int pos = syntax.curly_count-1;
  1455. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1456. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1457. return p+1;
  1458. } else {
  1459. char label[256];
  1460. int l,v;
  1461. char *np;
  1462. if(syntax.curly[pos].count != 1) {
  1463. //Jump for FALLTHRU
  1464. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1465. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1466. parse_line(label);
  1467. syntax.curly_count--;
  1468. // You are here labeled
  1469. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1470. l=add_str(label);
  1471. set_label(l,script_pos, p);
  1472. }
  1473. //Decision statement switch
  1474. p = skip_space(p2);
  1475. if(p == p2) {
  1476. disp_error_message("parse_syntax: expected a space ' '",p);
  1477. }
  1478. // check whether case label is integer or not
  1479. if(is_number(p)) {
  1480. //Numeric value
  1481. v = (int)strtol(p,&np,0);
  1482. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1483. p++;
  1484. p = skip_word(p);
  1485. if(np != p)
  1486. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1487. } else {
  1488. //Check for constants
  1489. p2 = skip_word(p);
  1490. v = (int)(size_t) (p2-p); // length of word at p2
  1491. memcpy(label,p,v);
  1492. label[v]='\0';
  1493. if( !script_get_constant(label, &v) )
  1494. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1495. p = skip_word(p);
  1496. }
  1497. p = skip_space(p);
  1498. if(*p != ':')
  1499. disp_error_message("parse_syntax: expect ':'",p);
  1500. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1501. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1502. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1503. // Bad I do not parse twice
  1504. p2 = parse_line(label);
  1505. parse_line(p2);
  1506. syntax.curly_count--;
  1507. if(syntax.curly[pos].count != 1) {
  1508. // Label after the completion of FALLTHRU
  1509. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1510. l=add_str(label);
  1511. set_label(l,script_pos,p);
  1512. }
  1513. // check duplication of case label [Rayce]
  1514. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1515. disp_error_message("parse_syntax: dup 'case'",p);
  1516. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1517. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1518. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1519. parse_line(label);
  1520. syntax.curly_count--;
  1521. syntax.curly[pos].count++;
  1522. }
  1523. return p + 1;
  1524. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1525. // Processing continue
  1526. char label[256];
  1527. int pos = syntax.curly_count - 1;
  1528. while(pos >= 0) {
  1529. if(syntax.curly[pos].type == TYPE_DO) {
  1530. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1531. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1532. break;
  1533. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1534. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1535. break;
  1536. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1537. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1538. break;
  1539. }
  1540. pos--;
  1541. }
  1542. if(pos < 0) {
  1543. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1544. } else {
  1545. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1546. parse_line(label);
  1547. syntax.curly_count--;
  1548. }
  1549. p = skip_space(p2);
  1550. if(*p != ';')
  1551. disp_error_message("parse_syntax: expected ';'",p);
  1552. //Closing decision if, for , while
  1553. p = parse_syntax_close(p + 1);
  1554. return p;
  1555. }
  1556. break;
  1557. case 'd':
  1558. case 'D':
  1559. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1560. // Switch - default processing
  1561. int pos = syntax.curly_count-1;
  1562. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1563. disp_error_message("parse_syntax: unexpected 'default'",p);
  1564. } else if(syntax.curly[pos].flag) {
  1565. disp_error_message("parse_syntax: dup 'default'",p);
  1566. } else {
  1567. char label[256];
  1568. int l;
  1569. // Put the label location
  1570. p = skip_space(p2);
  1571. if(*p != ':') {
  1572. disp_error_message("parse_syntax: expected ':'",p);
  1573. }
  1574. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1575. l=add_str(label);
  1576. set_label(l,script_pos,p);
  1577. // Skip to the next link w/o condition
  1578. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1579. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1580. parse_line(label);
  1581. syntax.curly_count--;
  1582. // The default label
  1583. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1584. l=add_str(label);
  1585. set_label(l,script_pos,p);
  1586. syntax.curly[syntax.curly_count - 1].flag = 1;
  1587. syntax.curly[pos].count++;
  1588. }
  1589. return p + 1;
  1590. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1591. int l;
  1592. char label[256];
  1593. p=skip_space(p2);
  1594. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1595. syntax.curly[syntax.curly_count].count = 1;
  1596. syntax.curly[syntax.curly_count].index = syntax.index++;
  1597. syntax.curly[syntax.curly_count].flag = 0;
  1598. // Label of the (do) form here
  1599. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1600. l=add_str(label);
  1601. set_label(l,script_pos,p);
  1602. syntax.curly_count++;
  1603. return p;
  1604. }
  1605. break;
  1606. case 'f':
  1607. case 'F':
  1608. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1609. int l;
  1610. char label[256];
  1611. int pos = syntax.curly_count;
  1612. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1613. syntax.curly[syntax.curly_count].count = 1;
  1614. syntax.curly[syntax.curly_count].index = syntax.index++;
  1615. syntax.curly[syntax.curly_count].flag = 0;
  1616. syntax.curly_count++;
  1617. p=skip_space(p2);
  1618. if(*p != '(')
  1619. disp_error_message("parse_syntax: expected '('",p);
  1620. p++;
  1621. // Execute the initialization statement
  1622. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1623. p=parse_line(p);
  1624. syntax.curly_count--;
  1625. // Form the start of label decision
  1626. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1627. l=add_str(label);
  1628. set_label(l,script_pos,p);
  1629. p=skip_space(p);
  1630. if(*p == ';') {
  1631. // For (; Because the pattern of always true ;)
  1632. ;
  1633. } else {
  1634. // Skip to the end point if the condition is false
  1635. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1636. add_scriptl(add_str("jump_zero"));
  1637. add_scriptc(C_ARG);
  1638. p=parse_expr(p);
  1639. p=skip_space(p);
  1640. add_scriptl(add_str(label));
  1641. add_scriptc(C_FUNC);
  1642. }
  1643. if(*p != ';')
  1644. disp_error_message("parse_syntax: expected ';'",p);
  1645. p++;
  1646. // Skip to the beginning of the loop
  1647. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1648. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1649. parse_line(label);
  1650. syntax.curly_count--;
  1651. // Labels to form the next loop
  1652. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1653. l=add_str(label);
  1654. set_label(l,script_pos,p);
  1655. // Process the next time you enter the loop
  1656. // A ')' last for; flag to be treated as'
  1657. parse_syntax_for_flag = 1;
  1658. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1659. p=parse_line(p);
  1660. syntax.curly_count--;
  1661. parse_syntax_for_flag = 0;
  1662. // Skip to the determination process conditions
  1663. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1664. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1665. parse_line(label);
  1666. syntax.curly_count--;
  1667. // Loop start labeling
  1668. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1669. l=add_str(label);
  1670. set_label(l,script_pos,p);
  1671. return p;
  1672. }
  1673. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1674. {// internal script function
  1675. const char *func_name;
  1676. func_name = skip_space(p2);
  1677. p = skip_word(func_name);
  1678. if( p == func_name )
  1679. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1680. p2 = skip_space(p);
  1681. if( *p2 == ';' )
  1682. {// function <name> ;
  1683. // function declaration - just register the name
  1684. int l;
  1685. l = add_word(func_name);
  1686. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1687. str_data[l].type = C_USERFUNC;
  1688. else if( str_data[l].type == C_USERFUNC )
  1689. ; // already registered
  1690. else
  1691. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1692. // Close condition of if, for, while
  1693. p = parse_syntax_close(p2 + 1);
  1694. return p;
  1695. }
  1696. else if(*p2 == '{')
  1697. {// function <name> <line/block of code>
  1698. char label[256];
  1699. int l;
  1700. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1701. syntax.curly[syntax.curly_count].count = 1;
  1702. syntax.curly[syntax.curly_count].index = syntax.index++;
  1703. syntax.curly[syntax.curly_count].flag = 0;
  1704. ++syntax.curly_count;
  1705. // Jump over the function code
  1706. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1707. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1708. ++syntax.curly_count;
  1709. parse_line(label);
  1710. --syntax.curly_count;
  1711. // Set the position of the function (label)
  1712. l=add_word(func_name);
  1713. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1714. {
  1715. str_data[l].type = C_USERFUNC;
  1716. set_label(l, script_pos, p);
  1717. if( parse_options&SCRIPT_USE_LABEL_DB )
  1718. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1719. }
  1720. else
  1721. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1722. return skip_space(p);
  1723. }
  1724. else
  1725. {
  1726. disp_error_message("expect ';' or '{' at function syntax",p);
  1727. }
  1728. }
  1729. break;
  1730. case 'i':
  1731. case 'I':
  1732. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1733. // If process
  1734. char label[256];
  1735. p=skip_space(p2);
  1736. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1737. disp_error_message("need '('",p);
  1738. }
  1739. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1740. syntax.curly[syntax.curly_count].count = 1;
  1741. syntax.curly[syntax.curly_count].index = syntax.index++;
  1742. syntax.curly[syntax.curly_count].flag = 0;
  1743. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1744. syntax.curly_count++;
  1745. add_scriptl(add_str("jump_zero"));
  1746. add_scriptc(C_ARG);
  1747. p=parse_expr(p);
  1748. p=skip_space(p);
  1749. add_scriptl(add_str(label));
  1750. add_scriptc(C_FUNC);
  1751. return p;
  1752. }
  1753. break;
  1754. case 's':
  1755. case 'S':
  1756. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1757. // Processing of switch ()
  1758. char label[256];
  1759. p=skip_space(p2);
  1760. if(*p != '(') {
  1761. disp_error_message("need '('",p);
  1762. }
  1763. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1764. syntax.curly[syntax.curly_count].count = 1;
  1765. syntax.curly[syntax.curly_count].index = syntax.index++;
  1766. syntax.curly[syntax.curly_count].flag = 0;
  1767. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1768. syntax.curly_count++;
  1769. add_scriptl(add_str("set"));
  1770. add_scriptc(C_ARG);
  1771. add_scriptl(add_str(label));
  1772. p=parse_expr(p);
  1773. p=skip_space(p);
  1774. if(*p != '{') {
  1775. disp_error_message("parse_syntax: expected '{'",p);
  1776. }
  1777. add_scriptc(C_FUNC);
  1778. return p + 1;
  1779. }
  1780. break;
  1781. case 'w':
  1782. case 'W':
  1783. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1784. int l;
  1785. char label[256];
  1786. p=skip_space(p2);
  1787. if(*p != '(') {
  1788. disp_error_message("need '('",p);
  1789. }
  1790. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1791. syntax.curly[syntax.curly_count].count = 1;
  1792. syntax.curly[syntax.curly_count].index = syntax.index++;
  1793. syntax.curly[syntax.curly_count].flag = 0;
  1794. // Form the start of label decision
  1795. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1796. l=add_str(label);
  1797. set_label(l,script_pos,p);
  1798. // Skip to the end point if the condition is false
  1799. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1800. syntax.curly_count++;
  1801. add_scriptl(add_str("jump_zero"));
  1802. add_scriptc(C_ARG);
  1803. p=parse_expr(p);
  1804. p=skip_space(p);
  1805. add_scriptl(add_str(label));
  1806. add_scriptc(C_FUNC);
  1807. return p;
  1808. }
  1809. break;
  1810. }
  1811. return NULL;
  1812. }
  1813. const char* parse_syntax_close(const char *p) {
  1814. // If (...) for (...) hoge (); as to make sure closed closed once again
  1815. int flag;
  1816. do {
  1817. p = parse_syntax_close_sub(p,&flag);
  1818. } while(flag);
  1819. return p;
  1820. }
  1821. // Close judgment if, for, while, of do
  1822. // flag == 1 : closed
  1823. // flag == 0 : not closed
  1824. const char* parse_syntax_close_sub(const char* p,int* flag)
  1825. {
  1826. char label[256];
  1827. int pos = syntax.curly_count - 1;
  1828. int l;
  1829. *flag = 1;
  1830. if(syntax.curly_count <= 0) {
  1831. *flag = 0;
  1832. return p;
  1833. } else if(syntax.curly[pos].type == TYPE_IF) {
  1834. const char *bp = p;
  1835. const char *p2;
  1836. // if-block and else-block end is a new line
  1837. parse_nextline(false, p);
  1838. // Skip to the last location if
  1839. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1840. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1841. parse_line(label);
  1842. syntax.curly_count--;
  1843. // Put the label of the location
  1844. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1845. l=add_str(label);
  1846. set_label(l,script_pos,p);
  1847. syntax.curly[pos].count++;
  1848. p = skip_space(p);
  1849. p2 = skip_word(p);
  1850. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1851. // else or else - if
  1852. p = skip_space(p2);
  1853. p2 = skip_word(p);
  1854. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1855. // else - if
  1856. p=skip_space(p2);
  1857. if(*p != '(') {
  1858. disp_error_message("need '('",p);
  1859. }
  1860. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1861. add_scriptl(add_str("jump_zero"));
  1862. add_scriptc(C_ARG);
  1863. p=parse_expr(p);
  1864. p=skip_space(p);
  1865. add_scriptl(add_str(label));
  1866. add_scriptc(C_FUNC);
  1867. *flag = 0;
  1868. return p;
  1869. } else {
  1870. // else
  1871. if(!syntax.curly[pos].flag) {
  1872. syntax.curly[pos].flag = 1;
  1873. *flag = 0;
  1874. return p;
  1875. }
  1876. }
  1877. }
  1878. // Close if
  1879. syntax.curly_count--;
  1880. // Put the label of the final location
  1881. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1882. l=add_str(label);
  1883. set_label(l,script_pos,p);
  1884. if(syntax.curly[pos].flag == 1) {
  1885. // Because the position of the pointer is the same if not else for this
  1886. return bp;
  1887. }
  1888. return p;
  1889. } else if(syntax.curly[pos].type == TYPE_DO) {
  1890. int l2;
  1891. char label2[256];
  1892. const char *p2;
  1893. if(syntax.curly[pos].flag) {
  1894. // (Come here continue) to form the label here
  1895. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1896. l2=add_str(label2);
  1897. set_label(l2,script_pos,p);
  1898. }
  1899. // Skip to the end point if the condition is false
  1900. p = skip_space(p);
  1901. p2 = skip_word(p);
  1902. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1903. disp_error_message("parse_syntax: expected 'while'",p);
  1904. p = skip_space(p2);
  1905. if(*p != '(') {
  1906. disp_error_message("need '('",p);
  1907. }
  1908. // do-block end is a new line
  1909. parse_nextline(false, p);
  1910. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1911. add_scriptl(add_str("jump_zero"));
  1912. add_scriptc(C_ARG);
  1913. p=parse_expr(p);
  1914. p=skip_space(p);
  1915. add_scriptl(add_str(label2));
  1916. add_scriptc(C_FUNC);
  1917. // Skip to the starting point
  1918. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1919. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1920. parse_line(label2);
  1921. syntax.curly_count--;
  1922. // Form label of the end point conditions
  1923. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1924. l2=add_str(label2);
  1925. set_label(l2,script_pos,p);
  1926. p = skip_space(p);
  1927. if(*p != ';') {
  1928. disp_error_message("parse_syntax: expected ';'",p);
  1929. return p+1;
  1930. }
  1931. p++;
  1932. syntax.curly_count--;
  1933. return p;
  1934. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1935. // for-block end is a new line
  1936. parse_nextline(false, p);
  1937. // Skip to the next loop
  1938. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1939. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1940. parse_line(label);
  1941. syntax.curly_count--;
  1942. // End for labeling
  1943. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1944. l=add_str(label);
  1945. set_label(l,script_pos,p);
  1946. syntax.curly_count--;
  1947. return p;
  1948. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1949. // while-block end is a new line
  1950. parse_nextline(false, p);
  1951. // Skip to the decision while
  1952. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1953. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1954. parse_line(label);
  1955. syntax.curly_count--;
  1956. // End while labeling
  1957. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1958. l=add_str(label);
  1959. set_label(l,script_pos,p);
  1960. syntax.curly_count--;
  1961. return p;
  1962. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1963. int pos2 = syntax.curly_count-1;
  1964. char label2[256];
  1965. int l2;
  1966. // Back
  1967. sprintf(label2,"return;");
  1968. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1969. parse_line(label2);
  1970. syntax.curly_count--;
  1971. // Put the label of the location
  1972. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1973. l2=add_str(label2);
  1974. set_label(l2,script_pos,p);
  1975. syntax.curly_count--;
  1976. return p;
  1977. } else {
  1978. *flag = 0;
  1979. return p;
  1980. }
  1981. }
  1982. /*==========================================
  1983. * Added built-in functions
  1984. *------------------------------------------*/
  1985. static void add_buildin_func(void)
  1986. {
  1987. int i;
  1988. for( i = 0; buildin_func[i].func; i++ )
  1989. {
  1990. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1991. // 'v' - value (either string or int or reference)
  1992. // 's' - string
  1993. // 'i' - int
  1994. // 'r' - reference (of a variable)
  1995. // 'l' - label
  1996. // '?' - one optional parameter
  1997. // '*' - unknown number of optional parameters
  1998. const char* p = buildin_func[i].arg;
  1999. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  2000. while( *p == '?' ) ++p;
  2001. if( *p == '*' ) ++p;
  2002. if( *p != 0){
  2003. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  2004. } else if( *skip_word(buildin_func[i].name) != 0 ){
  2005. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  2006. } else {
  2007. int n = add_str(buildin_func[i].name);
  2008. str_data[n].type = C_FUNC;
  2009. str_data[n].val = i;
  2010. str_data[n].func = buildin_func[i].func;
  2011. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  2012. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2013. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2014. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2015. }
  2016. }
  2017. }
  2018. /// Retrieves the value of a constant.
  2019. bool script_get_constant(const char* name, int* value)
  2020. {
  2021. int n = search_str(name);
  2022. if( n == -1 || str_data[n].type != C_INT )
  2023. {// not found or not a constant
  2024. return false;
  2025. }
  2026. value[0] = str_data[n].val;
  2027. return true;
  2028. }
  2029. /// Creates new constant or parameter with given value.
  2030. void script_set_constant(const char* name, int value, bool isparameter)
  2031. {
  2032. int n = add_str(name);
  2033. if( str_data[n].type == C_NOP )
  2034. {// new
  2035. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2036. str_data[n].val = value;
  2037. }
  2038. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2039. {// existing parameter or constant
  2040. 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);
  2041. }
  2042. else
  2043. {// existing name
  2044. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2045. }
  2046. }
  2047. /*==========================================
  2048. * Reading constant databases
  2049. * const.txt
  2050. *------------------------------------------*/
  2051. static void read_constdb(void)
  2052. {
  2053. FILE *fp;
  2054. char line[1024],name[1024],val[1024];
  2055. int type;
  2056. int entries=0, skipped=0, linenum=0;
  2057. sprintf(line, "%s/const.txt", db_path);
  2058. fp=fopen(line, "r");
  2059. if(fp==NULL){
  2060. ShowError("can't read %s\n", line);
  2061. return ;
  2062. }
  2063. while(fgets(line, sizeof(line), fp))
  2064. {
  2065. linenum++;
  2066. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  2067. continue;
  2068. if(line[0]=='/' && line[1]=='/') //ignore commented line
  2069. continue;
  2070. type=0;
  2071. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%d",name,val,&type)>=2 ||
  2072. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %d",name,val,&type)>=2){
  2073. entries++;
  2074. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2075. }
  2076. else {
  2077. skipped++;
  2078. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2079. }
  2080. }
  2081. fclose(fp);
  2082. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2083. if(skipped){
  2084. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2085. }
  2086. }
  2087. /**
  2088. * Sets source-end constants for NPC scripts to access.
  2089. **/
  2090. void script_hardcoded_constants(void) {
  2091. /* server defines */
  2092. script_set_constant("PACKETVER",PACKETVER,false);
  2093. script_set_constant("MAX_LEVEL",MAX_LEVEL,false);
  2094. script_set_constant("MAX_STORAGE",MAX_STORAGE,false);
  2095. script_set_constant("MAX_INVENTORY",MAX_INVENTORY,false);
  2096. script_set_constant("MAX_CART",MAX_INVENTORY,false);
  2097. script_set_constant("MAX_ZENY",MAX_ZENY,false);
  2098. script_set_constant("MAX_PARTY",MAX_PARTY,false);
  2099. script_set_constant("MAX_GUILD",MAX_GUILD,false);
  2100. script_set_constant("MAX_GUILDLEVEL",MAX_GUILDLEVEL,false);
  2101. script_set_constant("MAX_GUILD_STORAGE",MAX_GUILD_STORAGE,false);
  2102. script_set_constant("MAX_BG_MEMBERS",MAX_BG_MEMBERS,false);
  2103. script_set_constant("MAX_CHAT_USERS",MAX_CHAT_USERS,false);
  2104. script_set_constant("VIP_SCRIPT",VIP_SCRIPT,false);
  2105. script_set_constant("MIN_STORAGE",MIN_STORAGE,false);
  2106. /* status options */
  2107. script_set_constant("Option_Nothing",OPTION_NOTHING,false);
  2108. script_set_constant("Option_Sight",OPTION_SIGHT,false);
  2109. script_set_constant("Option_Hide",OPTION_HIDE,false);
  2110. script_set_constant("Option_Cloak",OPTION_CLOAK,false);
  2111. script_set_constant("Option_Falcon",OPTION_FALCON,false);
  2112. script_set_constant("Option_Riding",OPTION_RIDING,false);
  2113. script_set_constant("Option_Invisible",OPTION_INVISIBLE,false);
  2114. script_set_constant("Option_Orcish",OPTION_ORCISH,false);
  2115. script_set_constant("Option_Wedding",OPTION_WEDDING,false);
  2116. script_set_constant("Option_Ruwach",OPTION_RUWACH,false);
  2117. script_set_constant("Option_Chasewalk",OPTION_CHASEWALK,false);
  2118. script_set_constant("Option_Flying",OPTION_FLYING,false);
  2119. script_set_constant("Option_Xmas",OPTION_XMAS,false);
  2120. script_set_constant("Option_Transform",OPTION_TRANSFORM,false);
  2121. script_set_constant("Option_Summer",OPTION_SUMMER,false);
  2122. script_set_constant("Option_Dragon1",OPTION_DRAGON1,false);
  2123. script_set_constant("Option_Wug",OPTION_WUG,false);
  2124. script_set_constant("Option_Wugrider",OPTION_WUGRIDER,false);
  2125. script_set_constant("Option_Madogear",OPTION_MADOGEAR,false);
  2126. script_set_constant("Option_Dragon2",OPTION_DRAGON2,false);
  2127. script_set_constant("Option_Dragon3",OPTION_DRAGON3,false);
  2128. script_set_constant("Option_Dragon4",OPTION_DRAGON4,false);
  2129. script_set_constant("Option_Dragon5",OPTION_DRAGON5,false);
  2130. script_set_constant("Option_Hanbok",OPTION_HANBOK,false);
  2131. script_set_constant("Option_Oktoberfest",OPTION_OKTOBERFEST,false);
  2132. /* status option compounds */
  2133. script_set_constant("Option_Dragon",OPTION_DRAGON,false);
  2134. script_set_constant("Option_Costume",OPTION_COSTUME,false);
  2135. /* bonus_script commands */
  2136. script_set_constant("BSF_REM_BUFF",BSF_REM_BUFF,false);
  2137. script_set_constant("BSF_REM_DEBUFF",BSF_REM_DEBUFF,false);
  2138. script_set_constant("BSF_ALL",BSF_ALL,false);
  2139. script_set_constant("BSF_CLEARALL",BSF_CLEARALL,false);
  2140. /* sc_start flags */
  2141. script_set_constant("SCSTART_NONE",SCSTART_NONE,false);
  2142. script_set_constant("SCSTART_NOAVOID",SCSTART_NOAVOID,false);
  2143. script_set_constant("SCSTART_NOTICKDEF",SCSTART_NOTICKDEF,false);
  2144. script_set_constant("SCSTART_LOADED",SCSTART_LOADED,false);
  2145. script_set_constant("SCSTART_NORATEDEF",SCSTART_NORATEDEF,false);
  2146. script_set_constant("SCSTART_NOICON",SCSTART_NOICON,false);
  2147. }
  2148. /*==========================================
  2149. * Display emplacement line of script
  2150. *------------------------------------------*/
  2151. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2152. {
  2153. int i;
  2154. if( p == NULL || !p[0] ) return NULL;
  2155. if( line < 0 )
  2156. StringBuf_Printf(buf, "*% 5d : ", -line);
  2157. else
  2158. StringBuf_Printf(buf, " % 5d : ", line);
  2159. for(i=0;p[i] && p[i] != '\n';i++){
  2160. if(p + i != mark)
  2161. StringBuf_Printf(buf, "%c", p[i]);
  2162. else
  2163. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2164. }
  2165. StringBuf_AppendStr(buf, "\n");
  2166. return p+i+(p[i] == '\n' ? 1 : 0);
  2167. }
  2168. void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2169. // Find the line where the error occurred
  2170. int j;
  2171. int line = start_line;
  2172. const char *p;
  2173. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2174. for(p=src;p && *p;line++){
  2175. const char *lineend=strchr(p,'\n');
  2176. if(lineend==NULL || error_pos_cur<lineend){
  2177. break;
  2178. }
  2179. for( j = 0; j < 4; j++ ) {
  2180. linestart[j] = linestart[j+1];
  2181. }
  2182. linestart[4] = p;
  2183. p=lineend+1;
  2184. }
  2185. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2186. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2187. for(j = 0; j < 5; j++ ) {
  2188. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2189. }
  2190. p = script_print_line(buf, p, error_pos_cur, -line);
  2191. for(j = 0; j < 5; j++) {
  2192. p = script_print_line(buf, p, NULL, line + j + 1 );
  2193. }
  2194. }
  2195. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2196. StringBuf buf;
  2197. StringBuf_Init(&buf);
  2198. StringBuf_AppendStr(&buf, "\a\n");
  2199. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2200. ShowError("%s", StringBuf_Value(&buf));
  2201. StringBuf_Destroy(&buf);
  2202. }
  2203. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2204. StringBuf buf;
  2205. StringBuf_Init(&buf);
  2206. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2207. ShowWarning("%s", StringBuf_Value(&buf));
  2208. StringBuf_Destroy(&buf);
  2209. }
  2210. /*==========================================
  2211. * Analysis of the script
  2212. *------------------------------------------*/
  2213. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2214. {
  2215. const char *p,*tmpp;
  2216. int i;
  2217. struct script_code* code = NULL;
  2218. static int first=1;
  2219. char end;
  2220. bool unresolved_names = false;
  2221. parser_current_src = src;
  2222. parser_current_file = file;
  2223. parser_current_line = line;
  2224. if( src == NULL )
  2225. return NULL;// empty script
  2226. memset(&syntax,0,sizeof(syntax));
  2227. if(first){
  2228. add_buildin_func();
  2229. read_constdb();
  2230. script_hardcoded_constants();
  2231. first=0;
  2232. }
  2233. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2234. script_pos=0;
  2235. script_size=SCRIPT_BLOCK_SIZE;
  2236. parse_nextline(true, NULL);
  2237. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2238. if( options&SCRIPT_USE_LABEL_DB )
  2239. db_clear(scriptlabel_db);
  2240. parse_options = options;
  2241. if( setjmp( error_jump ) != 0 ) {
  2242. //Restore program state when script has problems. [from jA]
  2243. int j;
  2244. const int size = ARRAYLENGTH(syntax.curly);
  2245. if( error_report )
  2246. script_error(src,file,line,error_msg,error_pos);
  2247. aFree( error_msg );
  2248. aFree( script_buf );
  2249. script_pos = 0;
  2250. script_size = 0;
  2251. script_buf = NULL;
  2252. for(j=LABEL_START;j<str_num;j++)
  2253. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2254. for(j=0; j<size; j++)
  2255. linkdb_final(&syntax.curly[j].case_label);
  2256. return NULL;
  2257. }
  2258. parse_syntax_for_flag=0;
  2259. p=src;
  2260. p=skip_space(p);
  2261. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2262. {// does not require brackets around the script
  2263. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2264. {// empty script and can return NULL
  2265. aFree( script_buf );
  2266. script_pos = 0;
  2267. script_size = 0;
  2268. script_buf = NULL;
  2269. return NULL;
  2270. }
  2271. end = '\0';
  2272. }
  2273. else
  2274. {// requires brackets around the script
  2275. if( *p != '{' )
  2276. disp_error_message("not found '{'",p);
  2277. p = skip_space(p+1);
  2278. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2279. {// empty script and can return NULL
  2280. aFree( script_buf );
  2281. script_pos = 0;
  2282. script_size = 0;
  2283. script_buf = NULL;
  2284. return NULL;
  2285. }
  2286. end = '}';
  2287. }
  2288. // clear references of labels, variables and internal functions
  2289. for(i=LABEL_START;i<str_num;i++){
  2290. if(
  2291. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2292. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2293. ){
  2294. str_data[i].type=C_NOP;
  2295. str_data[i].backpatch=-1;
  2296. str_data[i].label=-1;
  2297. }
  2298. }
  2299. while( syntax.curly_count != 0 || *p != end )
  2300. {
  2301. if( *p == '\0' )
  2302. disp_error_message("unexpected end of script",p);
  2303. // Special handling only label
  2304. tmpp=skip_space(skip_word(p));
  2305. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2306. i=add_word(p);
  2307. set_label(i,script_pos,p);
  2308. if( parse_options&SCRIPT_USE_LABEL_DB )
  2309. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2310. p=tmpp+1;
  2311. p=skip_space(p);
  2312. continue;
  2313. }
  2314. // All other lumped
  2315. p=parse_line(p);
  2316. p=skip_space(p);
  2317. parse_nextline(false, p);
  2318. }
  2319. add_scriptc(C_NOP);
  2320. // trim code to size
  2321. script_size = script_pos;
  2322. RECREATE(script_buf,unsigned char,script_pos);
  2323. // default unknown references to variables
  2324. for(i=LABEL_START;i<str_num;i++){
  2325. if(str_data[i].type==C_NOP){
  2326. int j;
  2327. str_data[i].type=C_NAME;
  2328. str_data[i].label=i;
  2329. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2330. int next=GETVALUE(script_buf,j);
  2331. SETVALUE(script_buf,j,i);
  2332. j=next;
  2333. }
  2334. }
  2335. else if( str_data[i].type == C_USERFUNC )
  2336. {// 'function name;' without follow-up code
  2337. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2338. unresolved_names = true;
  2339. }
  2340. }
  2341. if( unresolved_names )
  2342. {
  2343. disp_error_message("parse_script: unresolved function references", p);
  2344. }
  2345. #ifdef DEBUG_DISP
  2346. for(i=0;i<script_pos;i++){
  2347. if((i&15)==0) ShowMessage("%04x : ",i);
  2348. ShowMessage("%02x ",script_buf[i]);
  2349. if((i&15)==15) ShowMessage("\n");
  2350. }
  2351. ShowMessage("\n");
  2352. #endif
  2353. #ifdef DEBUG_DISASM
  2354. {
  2355. int i = 0,j;
  2356. while(i < script_pos) {
  2357. c_op op = get_com(script_buf,&i);
  2358. ShowMessage("%06x %s", i, script_op2name(op));
  2359. j = i;
  2360. switch(op) {
  2361. case C_INT:
  2362. ShowMessage(" %d", get_num(script_buf,&i));
  2363. break;
  2364. case C_POS:
  2365. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2366. i += 3;
  2367. break;
  2368. case C_NAME:
  2369. j = (*(int*)(script_buf+i)&0xffffff);
  2370. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2371. i += 3;
  2372. break;
  2373. case C_STR:
  2374. j = strlen(script_buf + i);
  2375. ShowMessage(" %s", script_buf + i);
  2376. i += j+1;
  2377. break;
  2378. }
  2379. ShowMessage(CL_CLL"\n");
  2380. }
  2381. }
  2382. #endif
  2383. CREATE(code,struct script_code,1);
  2384. code->script_buf = script_buf;
  2385. code->script_size = script_size;
  2386. code->script_vars = idb_alloc(DB_OPT_RELEASE_DATA);
  2387. return code;
  2388. }
  2389. /// Returns the player attached to this script, identified by the rid.
  2390. /// If there is no player attached, the script is terminated.
  2391. TBL_PC *script_rid2sd(struct script_state *st)
  2392. {
  2393. TBL_PC *sd=map_id2sd(st->rid);
  2394. if(!sd){
  2395. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2396. script_reportfunc(st);
  2397. script_reportsrc(st);
  2398. st->state = END;
  2399. }
  2400. return sd;
  2401. }
  2402. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2403. ///
  2404. /// @param st Script state
  2405. /// @param data Variable/constant
  2406. void get_val(struct script_state* st, struct script_data* data)
  2407. {
  2408. const char* name;
  2409. char prefix;
  2410. char postfix;
  2411. TBL_PC* sd = NULL;
  2412. if( !data_isreference(data) )
  2413. return;// not a variable/constant
  2414. name = reference_getname(data);
  2415. prefix = name[0];
  2416. postfix = name[strlen(name) - 1];
  2417. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2418. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2419. {
  2420. sd = script_rid2sd(st);
  2421. if( sd == NULL )
  2422. {// needs player attached
  2423. if( postfix == '$' )
  2424. {// string variable
  2425. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2426. data->type = C_CONSTSTR;
  2427. data->u.str = "";
  2428. }
  2429. else
  2430. {// integer variable
  2431. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2432. data->type = C_INT;
  2433. data->u.num = 0;
  2434. }
  2435. return;
  2436. }
  2437. }
  2438. if( postfix == '$' )
  2439. {// string variable
  2440. switch( prefix )
  2441. {
  2442. case '@':
  2443. data->u.str = pc_readregstr(sd, data->u.num);
  2444. break;
  2445. case '$':
  2446. data->u.str = mapreg_readregstr(data->u.num);
  2447. break;
  2448. case '#':
  2449. if( name[1] == '#' )
  2450. data->u.str = pc_readaccountreg2str(sd, name);// global
  2451. else
  2452. data->u.str = pc_readaccountregstr(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.str = (char*)idb_get(n,reference_getuid(data));
  2462. else
  2463. data->u.str = NULL;
  2464. }
  2465. break;
  2466. case '\'':
  2467. {
  2468. int instance_id = script_instancegetid(st);
  2469. if( instance_id )
  2470. data->u.str = (char*)idb_get(instance_data[instance_id].vars,reference_getuid(data));
  2471. else {
  2472. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2473. data->u.str = NULL;
  2474. }
  2475. }
  2476. break;
  2477. default:
  2478. data->u.str = pc_readglobalreg_str(sd, name);
  2479. break;
  2480. }
  2481. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2482. {// empty string
  2483. data->type = C_CONSTSTR;
  2484. data->u.str = "";
  2485. }
  2486. else
  2487. {// duplicate string
  2488. data->type = C_STR;
  2489. data->u.str = aStrdup(data->u.str);
  2490. }
  2491. }
  2492. else
  2493. {// integer variable
  2494. data->type = C_INT;
  2495. if( reference_toconstant(data) )
  2496. {
  2497. data->u.num = reference_getconstant(data);
  2498. }
  2499. else if( reference_toparam(data) )
  2500. {
  2501. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2502. }
  2503. else
  2504. switch( prefix )
  2505. {
  2506. case '@':
  2507. data->u.num = pc_readreg(sd, data->u.num);
  2508. break;
  2509. case '$':
  2510. data->u.num = mapreg_readreg(data->u.num);
  2511. break;
  2512. case '#':
  2513. if( name[1] == '#' )
  2514. data->u.num = pc_readaccountreg2(sd, name);// global
  2515. else
  2516. data->u.num = pc_readaccountreg(sd, name);// local
  2517. break;
  2518. case '.':
  2519. {
  2520. struct DBMap* n =
  2521. data->ref ? *data->ref:
  2522. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2523. st->script->script_vars;// npc variable
  2524. if( n )
  2525. data->u.num = (int)idb_iget(n,reference_getuid(data));
  2526. else
  2527. data->u.num = 0;
  2528. }
  2529. break;
  2530. case '\'':
  2531. {
  2532. int instance_id = script_instancegetid(st);
  2533. if( instance_id )
  2534. data->u.num = (int)idb_iget(instance_data[instance_id].vars,reference_getuid(data));
  2535. else {
  2536. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2537. data->u.num = 0;
  2538. }
  2539. }
  2540. break;
  2541. default:
  2542. data->u.num = pc_readglobalreg(sd, name);
  2543. break;
  2544. }
  2545. }
  2546. return;
  2547. }
  2548. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
  2549. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2550. /// The value is left in the top of the stack and needs to be removed manually.
  2551. void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
  2552. {
  2553. struct script_data* data;
  2554. push_val2(st->stack, C_NAME, uid, ref);
  2555. data = script_getdatatop(st, -1);
  2556. get_val(st, data);
  2557. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2558. }
  2559. /*==========================================
  2560. * Stores the value of a script variable
  2561. * Return value is 0 on fail, 1 on success.
  2562. *------------------------------------------*/
  2563. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
  2564. {
  2565. char prefix = name[0];
  2566. if( is_string_variable(name) ) {// string variable
  2567. const char* str = (const char*)value;
  2568. switch (prefix) {
  2569. case '@':
  2570. return pc_setregstr(sd, num, str);
  2571. case '$':
  2572. return mapreg_setregstr(num, str);
  2573. case '#':
  2574. return (name[1] == '#') ?
  2575. pc_setaccountreg2str(sd, name, str) :
  2576. pc_setaccountregstr(sd, name, str);
  2577. case '.':
  2578. {
  2579. struct DBMap* n;
  2580. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2581. if( n ) {
  2582. idb_remove(n, num);
  2583. if (str[0]) idb_put(n, num, aStrdup(str));
  2584. }
  2585. }
  2586. return 1;
  2587. case '\'':
  2588. {
  2589. int instance_id = script_instancegetid(st);
  2590. if( instance_id ) {
  2591. idb_remove(instance_data[instance_id].vars, num);
  2592. if( str[0] ) idb_put(instance_data[instance_id].vars, num, aStrdup(str));
  2593. }
  2594. return 1;
  2595. }
  2596. default:
  2597. return pc_setglobalreg_str(sd, name, str);
  2598. }
  2599. } else {// integer variable
  2600. int val = (int)__64BPRTSIZE(value);
  2601. if(str_data[num&0x00ffffff].type == C_PARAM)
  2602. {
  2603. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2604. {
  2605. if( st != NULL )
  2606. {
  2607. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2608. script_reportsrc(st);
  2609. st->state = END;
  2610. }
  2611. return 0;
  2612. }
  2613. return 1;
  2614. }
  2615. switch (prefix) {
  2616. case '@':
  2617. return pc_setreg(sd, num, val);
  2618. case '$':
  2619. return mapreg_setreg(num, val);
  2620. case '#':
  2621. return (name[1] == '#') ?
  2622. pc_setaccountreg2(sd, name, val) :
  2623. pc_setaccountreg(sd, name, val);
  2624. case '.':
  2625. {
  2626. struct DBMap* n;
  2627. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2628. if( n ) {
  2629. idb_remove(n, num);
  2630. if( val != 0 )
  2631. idb_iput(n, num, val);
  2632. }
  2633. }
  2634. return 1;
  2635. case '\'':
  2636. {
  2637. int instance_id = script_instancegetid(st);
  2638. if( instance_id ) {
  2639. idb_remove(instance_data[instance_id].vars, num);
  2640. if( val != 0 )
  2641. idb_iput(instance_data[instance_id].vars, num, val);
  2642. }
  2643. return 1;
  2644. }
  2645. default:
  2646. return pc_setglobalreg(sd, name, val);
  2647. }
  2648. }
  2649. }
  2650. int set_var(TBL_PC* sd, char* name, void* val)
  2651. {
  2652. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2653. }
  2654. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
  2655. {
  2656. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2657. }
  2658. /// Converts the data to a string
  2659. const char* conv_str(struct script_state* st, struct script_data* data)
  2660. {
  2661. char* p;
  2662. get_val(st, data);
  2663. if( data_isstring(data) )
  2664. {// nothing to convert
  2665. }
  2666. else if( data_isint(data) )
  2667. {// int -> string
  2668. CREATE(p, char, ITEM_NAME_LENGTH);
  2669. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2670. p[ITEM_NAME_LENGTH-1] = '\0';
  2671. data->type = C_STR;
  2672. data->u.str = p;
  2673. }
  2674. else if( data_isreference(data) )
  2675. {// reference -> string
  2676. //##TODO when does this happen (check get_val) [FlavioJS]
  2677. data->type = C_CONSTSTR;
  2678. data->u.str = reference_getname(data);
  2679. }
  2680. else
  2681. {// unsupported data type
  2682. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2683. script_reportdata(data);
  2684. script_reportsrc(st);
  2685. data->type = C_CONSTSTR;
  2686. data->u.str = "";
  2687. }
  2688. return data->u.str;
  2689. }
  2690. /// Converts the data to an int
  2691. int conv_num(struct script_state* st, struct script_data* data)
  2692. {
  2693. get_val(st, data);
  2694. if( data_isint(data) )
  2695. {// nothing to convert
  2696. }
  2697. else if( data_isstring(data) )
  2698. {// string -> int
  2699. // the result does not overflow or underflow, it is capped instead
  2700. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2701. char* p = data->u.str;
  2702. long num;
  2703. errno = 0;
  2704. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2705. if( errno == ERANGE
  2706. #if LONG_MAX > INT_MAX
  2707. || num < INT_MIN || num > INT_MAX
  2708. #endif
  2709. )
  2710. {
  2711. if( num <= INT_MIN )
  2712. {
  2713. num = INT_MIN;
  2714. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2715. }
  2716. else//if( num >= INT_MAX )
  2717. {
  2718. num = INT_MAX;
  2719. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2720. }
  2721. script_reportdata(data);
  2722. script_reportsrc(st);
  2723. }
  2724. if( data->type == C_STR )
  2725. aFree(p);
  2726. data->type = C_INT;
  2727. data->u.num = (int)num;
  2728. }
  2729. #if 0
  2730. // FIXME this function is being used to retrieve the position of labels and
  2731. // probably other stuff [FlavioJS]
  2732. else
  2733. {// unsupported data type
  2734. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2735. script_reportdata(data);
  2736. script_reportsrc(st);
  2737. data->type = C_INT;
  2738. data->u.num = 0;
  2739. }
  2740. #endif
  2741. return data->u.num;
  2742. }
  2743. //
  2744. // Stack operations
  2745. //
  2746. /// Increases the size of the stack
  2747. void stack_expand(struct script_stack* stack)
  2748. {
  2749. stack->sp_max += 64;
  2750. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2751. stack->sp_max * sizeof(stack->stack_data[0]) );
  2752. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2753. 64 * sizeof(stack->stack_data[0]) );
  2754. }
  2755. /// Pushes a value into the stack
  2756. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2757. /// Pushes a value into the stack (with reference)
  2758. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
  2759. {
  2760. if( stack->sp >= stack->sp_max )
  2761. stack_expand(stack);
  2762. stack->stack_data[stack->sp].type = type;
  2763. stack->stack_data[stack->sp].u.num = val;
  2764. stack->stack_data[stack->sp].ref = ref;
  2765. stack->sp++;
  2766. return &stack->stack_data[stack->sp-1];
  2767. }
  2768. /// Pushes a string into the stack
  2769. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2770. {
  2771. if( stack->sp >= stack->sp_max )
  2772. stack_expand(stack);
  2773. stack->stack_data[stack->sp].type = type;
  2774. stack->stack_data[stack->sp].u.str = str;
  2775. stack->stack_data[stack->sp].ref = NULL;
  2776. stack->sp++;
  2777. return &stack->stack_data[stack->sp-1];
  2778. }
  2779. /// Pushes a retinfo into the stack
  2780. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, DBMap **ref)
  2781. {
  2782. if( stack->sp >= stack->sp_max )
  2783. stack_expand(stack);
  2784. stack->stack_data[stack->sp].type = C_RETINFO;
  2785. stack->stack_data[stack->sp].u.ri = ri;
  2786. stack->stack_data[stack->sp].ref = ref;
  2787. stack->sp++;
  2788. return &stack->stack_data[stack->sp-1];
  2789. }
  2790. /// Pushes a copy of the target position into the stack
  2791. struct script_data* push_copy(struct script_stack* stack, int pos)
  2792. {
  2793. switch( stack->stack_data[pos].type )
  2794. {
  2795. case C_CONSTSTR:
  2796. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2797. break;
  2798. case C_STR:
  2799. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2800. break;
  2801. case C_RETINFO:
  2802. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2803. exit(1);
  2804. break;
  2805. default:
  2806. return push_val2(
  2807. stack,stack->stack_data[pos].type,
  2808. stack->stack_data[pos].u.num,
  2809. stack->stack_data[pos].ref
  2810. );
  2811. break;
  2812. }
  2813. }
  2814. /// Removes the values in indexes [start,end] from the stack.
  2815. /// Adjusts all stack pointers.
  2816. void pop_stack(struct script_state* st, int start, int end)
  2817. {
  2818. struct script_stack* stack = st->stack;
  2819. struct script_data* data;
  2820. int i;
  2821. if( start < 0 )
  2822. start = 0;
  2823. if( end > stack->sp )
  2824. end = stack->sp;
  2825. if( start >= end )
  2826. return;// nothing to pop
  2827. // free stack elements
  2828. for( i = start; i < end; i++ )
  2829. {
  2830. data = &stack->stack_data[i];
  2831. if( data->type == C_STR )
  2832. aFree(data->u.str);
  2833. if( data->type == C_RETINFO )
  2834. {
  2835. struct script_retinfo* ri = data->u.ri;
  2836. if( ri->var_function )
  2837. script_free_vars(ri->var_function);
  2838. if( data->ref )
  2839. aFree(data->ref);
  2840. aFree(ri);
  2841. }
  2842. data->type = C_NOP;
  2843. }
  2844. // move the rest of the elements
  2845. if( stack->sp > end )
  2846. {
  2847. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2848. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2849. stack->stack_data[i].type = C_NOP;
  2850. }
  2851. // adjust stack pointers
  2852. if( st->start > end ) st->start -= end - start;
  2853. else if( st->start > start ) st->start = start;
  2854. if( st->end > end ) st->end -= end - start;
  2855. else if( st->end > start ) st->end = start;
  2856. if( stack->defsp > end ) stack->defsp -= end - start;
  2857. else if( stack->defsp > start ) stack->defsp = start;
  2858. stack->sp -= end - start;
  2859. }
  2860. ///
  2861. ///
  2862. ///
  2863. /*==========================================
  2864. * Release script dependent variable, dependent variable of function
  2865. *------------------------------------------*/
  2866. void script_free_vars(struct DBMap* storage)
  2867. {
  2868. if( storage )
  2869. {// destroy the storage construct containing the variables
  2870. db_destroy(storage);
  2871. }
  2872. }
  2873. void script_free_code(struct script_code* code)
  2874. {
  2875. script_free_vars( code->script_vars );
  2876. aFree( code->script_buf );
  2877. aFree( code );
  2878. }
  2879. /// Creates a new script state.
  2880. ///
  2881. /// @param script Script code
  2882. /// @param pos Position in the code
  2883. /// @param rid Who is running the script (attached player)
  2884. /// @param oid Where the code is being run (npc 'object')
  2885. /// @return Script state
  2886. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2887. {
  2888. struct script_state* st;
  2889. CREATE(st, struct script_state, 1);
  2890. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2891. st->stack->sp = 0;
  2892. st->stack->sp_max = 64;
  2893. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2894. st->stack->defsp = st->stack->sp;
  2895. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  2896. st->state = RUN;
  2897. st->script = script;
  2898. //st->scriptroot = script;
  2899. st->pos = pos;
  2900. st->rid = rid;
  2901. st->oid = oid;
  2902. st->sleep.timer = INVALID_TIMER;
  2903. st->npc_item_flag = battle_config.item_enabled_npc;
  2904. return st;
  2905. }
  2906. /// Frees a script state.
  2907. ///
  2908. /// @param st Script state
  2909. void script_free_state(struct script_state* st)
  2910. {
  2911. if(st->bk_st)
  2912. {// backup was not restored
  2913. 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);
  2914. }
  2915. if( st->sleep.timer != INVALID_TIMER )
  2916. delete_timer(st->sleep.timer, run_script_timer);
  2917. script_free_vars(st->stack->var_function);
  2918. pop_stack(st, 0, st->stack->sp);
  2919. aFree(st->stack->stack_data);
  2920. aFree(st->stack);
  2921. st->stack = NULL;
  2922. st->pos = -1;
  2923. aFree(st);
  2924. }
  2925. //
  2926. // Main execution unit
  2927. //
  2928. /*==========================================
  2929. * Read command
  2930. *------------------------------------------*/
  2931. c_op get_com(unsigned char *script,int *pos)
  2932. {
  2933. int i = 0, j = 0;
  2934. if(script[*pos]>=0x80){
  2935. return C_INT;
  2936. }
  2937. while(script[*pos]>=0x40){
  2938. i=script[(*pos)++]<<j;
  2939. j+=6;
  2940. }
  2941. return (c_op)(i+(script[(*pos)++]<<j));
  2942. }
  2943. /*==========================================
  2944. * Income figures
  2945. *------------------------------------------*/
  2946. int get_num(unsigned char *script,int *pos)
  2947. {
  2948. int i,j;
  2949. i=0; j=0;
  2950. while(script[*pos]>=0xc0){
  2951. i+=(script[(*pos)++]&0x7f)<<j;
  2952. j+=6;
  2953. }
  2954. return i+((script[(*pos)++]&0x7f)<<j);
  2955. }
  2956. /*==========================================
  2957. * Remove the value from the stack
  2958. *------------------------------------------*/
  2959. int pop_val(struct script_state* st)
  2960. {
  2961. if(st->stack->sp<=0)
  2962. return 0;
  2963. st->stack->sp--;
  2964. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2965. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2966. return st->stack->stack_data[st->stack->sp].u.num;
  2967. return 0;
  2968. }
  2969. /// Ternary operators
  2970. /// test ? if_true : if_false
  2971. void op_3(struct script_state* st, int op)
  2972. {
  2973. struct script_data* data;
  2974. int flag = 0;
  2975. data = script_getdatatop(st, -3);
  2976. get_val(st, data);
  2977. if( data_isstring(data) )
  2978. flag = data->u.str[0];// "" -> false
  2979. else if( data_isint(data) )
  2980. flag = data->u.num;// 0 -> false
  2981. else
  2982. {
  2983. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2984. script_reportdata(data);
  2985. script_reportsrc(st);
  2986. script_removetop(st, -3, 0);
  2987. script_pushnil(st);
  2988. return;
  2989. }
  2990. if( flag )
  2991. script_pushcopytop(st, -2);
  2992. else
  2993. script_pushcopytop(st, -1);
  2994. script_removetop(st, -4, -1);
  2995. }
  2996. /// Binary string operators
  2997. /// s1 EQ s2 -> i
  2998. /// s1 NE s2 -> i
  2999. /// s1 GT s2 -> i
  3000. /// s1 GE s2 -> i
  3001. /// s1 LT s2 -> i
  3002. /// s1 LE s2 -> i
  3003. /// s1 ADD s2 -> s
  3004. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3005. {
  3006. int a = 0;
  3007. switch(op){
  3008. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3009. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3010. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3011. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3012. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3013. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3014. case C_ADD:
  3015. {
  3016. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3017. strcpy(buf, s1);
  3018. strcat(buf, s2);
  3019. script_pushstr(st, buf);
  3020. return;
  3021. }
  3022. default:
  3023. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3024. script_reportsrc(st);
  3025. script_pushnil(st);
  3026. st->state = END;
  3027. return;
  3028. }
  3029. script_pushint(st,a);
  3030. }
  3031. /// Binary number operators
  3032. /// i OP i -> i
  3033. void op_2num(struct script_state* st, int op, int i1, int i2)
  3034. {
  3035. int ret;
  3036. double ret_double;
  3037. switch( op )
  3038. {
  3039. case C_AND: ret = i1 & i2; break;
  3040. case C_OR: ret = i1 | i2; break;
  3041. case C_XOR: ret = i1 ^ i2; break;
  3042. case C_LAND: ret = (i1 && i2); break;
  3043. case C_LOR: ret = (i1 || i2); break;
  3044. case C_EQ: ret = (i1 == i2); break;
  3045. case C_NE: ret = (i1 != i2); break;
  3046. case C_GT: ret = (i1 > i2); break;
  3047. case C_GE: ret = (i1 >= i2); break;
  3048. case C_LT: ret = (i1 < i2); break;
  3049. case C_LE: ret = (i1 <= i2); break;
  3050. case C_R_SHIFT: ret = i1>>i2; break;
  3051. case C_L_SHIFT: ret = i1<<i2; break;
  3052. case C_DIV:
  3053. case C_MOD:
  3054. if( i2 == 0 )
  3055. {
  3056. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3057. script_reportsrc(st);
  3058. script_pushnil(st);
  3059. st->state = END;
  3060. return;
  3061. }
  3062. else if( op == C_DIV )
  3063. ret = i1 / i2;
  3064. else//if( op == C_MOD )
  3065. ret = i1 % i2;
  3066. break;
  3067. default:
  3068. switch( op )
  3069. {// operators that can overflow/underflow
  3070. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3071. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3072. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3073. default:
  3074. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3075. script_reportsrc(st);
  3076. script_pushnil(st);
  3077. return;
  3078. }
  3079. if( ret_double < (double)INT_MIN )
  3080. {
  3081. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3082. script_reportsrc(st);
  3083. ret = INT_MIN;
  3084. }
  3085. else if( ret_double > (double)INT_MAX )
  3086. {
  3087. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3088. script_reportsrc(st);
  3089. ret = INT_MAX;
  3090. }
  3091. }
  3092. script_pushint(st, ret);
  3093. }
  3094. /// Binary operators
  3095. void op_2(struct script_state *st, int op)
  3096. {
  3097. struct script_data* left, leftref;
  3098. struct script_data* right;
  3099. leftref.type = C_NOP;
  3100. left = script_getdatatop(st, -2);
  3101. right = script_getdatatop(st, -1);
  3102. if (st->op2ref) {
  3103. if (data_isreference(left)) {
  3104. leftref = *left;
  3105. }
  3106. st->op2ref = 0;
  3107. }
  3108. get_val(st, left);
  3109. get_val(st, right);
  3110. // automatic conversions
  3111. switch( op )
  3112. {
  3113. case C_ADD:
  3114. if( data_isint(left) && data_isstring(right) )
  3115. {// convert int-string to string-string
  3116. conv_str(st, left);
  3117. }
  3118. else if( data_isstring(left) && data_isint(right) )
  3119. {// convert string-int to string-string
  3120. conv_str(st, right);
  3121. }
  3122. break;
  3123. }
  3124. if( data_isstring(left) && data_isstring(right) )
  3125. {// ss => op_2str
  3126. op_2str(st, op, left->u.str, right->u.str);
  3127. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3128. if (leftref.type != C_NOP)
  3129. {
  3130. if (left->type == C_STR) // don't free C_CONSTSTR
  3131. aFree(left->u.str);
  3132. *left = leftref;
  3133. }
  3134. }
  3135. else if( data_isint(left) && data_isint(right) )
  3136. {// ii => op_2num
  3137. int i1 = left->u.num;
  3138. int i2 = right->u.num;
  3139. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3140. op_2num(st, op, i1, i2);
  3141. if (leftref.type != C_NOP)
  3142. *left = leftref;
  3143. }
  3144. else
  3145. {// invalid argument
  3146. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3147. script_reportdata(left);
  3148. script_reportdata(right);
  3149. script_reportsrc(st);
  3150. script_removetop(st, -2, 0);
  3151. script_pushnil(st);
  3152. st->state = END;
  3153. }
  3154. }
  3155. /// Unary operators
  3156. /// NEG i -> i
  3157. /// NOT i -> i
  3158. /// LNOT i -> i
  3159. void op_1(struct script_state* st, int op)
  3160. {
  3161. struct script_data* data;
  3162. int i1;
  3163. data = script_getdatatop(st, -1);
  3164. get_val(st, data);
  3165. if( !data_isint(data) )
  3166. {// not a number
  3167. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3168. script_reportdata(data);
  3169. script_reportsrc(st);
  3170. script_pushnil(st);
  3171. st->state = END;
  3172. return;
  3173. }
  3174. i1 = data->u.num;
  3175. script_removetop(st, -1, 0);
  3176. switch( op )
  3177. {
  3178. case C_NEG: i1 = -i1; break;
  3179. case C_NOT: i1 = ~i1; break;
  3180. case C_LNOT: i1 = !i1; break;
  3181. default:
  3182. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3183. script_reportsrc(st);
  3184. script_pushnil(st);
  3185. st->state = END;
  3186. return;
  3187. }
  3188. script_pushint(st, i1);
  3189. }
  3190. /// Checks the type of all arguments passed to a built-in function.
  3191. ///
  3192. /// @param st Script state whose stack arguments should be inspected.
  3193. /// @param func Built-in function for which the arguments are intended.
  3194. static void script_check_buildin_argtype(struct script_state* st, int func)
  3195. {
  3196. int idx, invalid = 0;
  3197. for( idx = 2; script_hasdata(st, idx); idx++ )
  3198. {
  3199. struct script_data* data = script_getdata(st, idx);
  3200. script_function* sf = &buildin_func[str_data[func].val];
  3201. char type = sf->arg[idx-2];
  3202. if( type == '?' || type == '*' )
  3203. {// optional argument or unknown number of optional parameters ( no types are after this )
  3204. break;
  3205. }
  3206. else if( type == 0 )
  3207. {// more arguments than necessary ( should not happen, as it is checked before )
  3208. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3209. invalid++;
  3210. break;
  3211. }
  3212. else
  3213. {
  3214. const char* name = NULL;
  3215. if( data_isreference(data) )
  3216. {// get name for variables to determine the type they refer to
  3217. name = reference_getname(data);
  3218. }
  3219. switch( type )
  3220. {
  3221. case 'v':
  3222. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3223. {// variant
  3224. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3225. script_reportdata(data);
  3226. invalid++;
  3227. }
  3228. break;
  3229. case 's':
  3230. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3231. {// string
  3232. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3233. script_reportdata(data);
  3234. invalid++;
  3235. }
  3236. break;
  3237. case 'i':
  3238. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3239. {// int ( params and constants are always int )
  3240. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3241. script_reportdata(data);
  3242. invalid++;
  3243. }
  3244. break;
  3245. case 'r':
  3246. if( !data_isreference(data) )
  3247. {// variables
  3248. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3249. script_reportdata(data);
  3250. invalid++;
  3251. }
  3252. break;
  3253. case 'l':
  3254. if( !data_islabel(data) && !data_isfunclabel(data) )
  3255. {// label
  3256. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3257. script_reportdata(data);
  3258. invalid++;
  3259. }
  3260. break;
  3261. }
  3262. }
  3263. }
  3264. if(invalid)
  3265. {
  3266. ShowDebug("Function: %s\n", get_str(func));
  3267. script_reportsrc(st);
  3268. }
  3269. }
  3270. /// Executes a buildin command.
  3271. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3272. int run_func(struct script_state *st)
  3273. {
  3274. struct script_data* data;
  3275. int i,start_sp,end_sp,func;
  3276. end_sp = st->stack->sp;// position after the last argument
  3277. for( i = end_sp-1; i > 0 ; --i )
  3278. if( st->stack->stack_data[i].type == C_ARG )
  3279. break;
  3280. if( i == 0 ) {
  3281. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3282. st->state = END;
  3283. script_reportsrc(st);
  3284. return 1;
  3285. }
  3286. start_sp = i-1;// C_NAME of the command
  3287. st->start = start_sp;
  3288. st->end = end_sp;
  3289. data = &st->stack->stack_data[st->start];
  3290. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3291. func = data->u.num;
  3292. st->funcname = reference_getname(data);
  3293. } else {
  3294. ShowError("script:run_func: not a buildin command.\n");
  3295. script_reportdata(data);
  3296. script_reportsrc(st);
  3297. st->state = END;
  3298. return 1;
  3299. }
  3300. if( script_config.warn_func_mismatch_argtypes ) {
  3301. script_check_buildin_argtype(st, func);
  3302. }
  3303. if(str_data[func].func) {
  3304. if (str_data[func].func(st)) //Report error
  3305. script_reportsrc(st);
  3306. } else {
  3307. 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));
  3308. script_reportsrc(st);
  3309. st->state = END;
  3310. }
  3311. // Stack's datum are used when re-running functions [Eoe]
  3312. if( st->state == RERUNLINE )
  3313. return 0;
  3314. pop_stack(st, st->start, st->end);
  3315. if( st->state == RETFUNC ) {// return from a user-defined function
  3316. struct script_retinfo* ri;
  3317. int olddefsp = st->stack->defsp;
  3318. int nargs;
  3319. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3320. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3321. {
  3322. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3323. script_reportsrc(st);
  3324. st->state = END;
  3325. return 1;
  3326. }
  3327. script_free_vars( st->stack->var_function );
  3328. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3329. nargs = ri->nargs;
  3330. st->pos = ri->pos;
  3331. st->script = ri->script;
  3332. st->stack->var_function = ri->var_function;
  3333. st->stack->defsp = ri->defsp;
  3334. memset(ri, 0, sizeof(struct script_retinfo));
  3335. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3336. st->state = GOTO;
  3337. }
  3338. return 0;
  3339. }
  3340. /*==========================================
  3341. * script execution
  3342. *------------------------------------------*/
  3343. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3344. {
  3345. struct script_state *st;
  3346. if( rootscript == NULL || pos < 0 )
  3347. return;
  3348. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3349. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3350. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3351. st = script_alloc_state(rootscript, pos, rid, oid);
  3352. run_script_main(st);
  3353. }
  3354. void script_stop_sleeptimers(int id)
  3355. {
  3356. for(;;)
  3357. {
  3358. struct script_state* st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3359. if( st == NULL )
  3360. break; // no more sleep timers
  3361. script_free_state(st);
  3362. }
  3363. }
  3364. /*==========================================
  3365. * Delete the specified node from sleep_db
  3366. *------------------------------------------*/
  3367. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3368. {
  3369. struct linkdb_node *retnode;
  3370. if( n == NULL)
  3371. return NULL;
  3372. if( n->prev == NULL )
  3373. sleep_db = n->next;
  3374. else
  3375. n->prev->next = n->next;
  3376. if( n->next )
  3377. n->next->prev = n->prev;
  3378. retnode = n->next;
  3379. aFree( n );
  3380. return retnode; // The following; return retnode
  3381. }
  3382. /*==========================================
  3383. * Timer function for sleep
  3384. *------------------------------------------*/
  3385. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3386. {
  3387. struct script_state *st = (struct script_state *)data;
  3388. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3389. TBL_PC *sd = map_id2sd(st->rid);
  3390. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3391. { //Character mismatch. Cancel execution.
  3392. st->rid = 0;
  3393. st->state = END;
  3394. }
  3395. while( node && st->sleep.timer != INVALID_TIMER ) {
  3396. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3397. script_erase_sleepdb(node);
  3398. st->sleep.timer = INVALID_TIMER;
  3399. break;
  3400. }
  3401. node = node->next;
  3402. }
  3403. if(st->state != RERUNLINE)
  3404. st->sleep.tick = 0;
  3405. run_script_main(st);
  3406. return 0;
  3407. }
  3408. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3409. ///
  3410. /// @param st Script state to detach.
  3411. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3412. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3413. {
  3414. struct map_session_data* sd;
  3415. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3416. sd->st = st->bk_st;
  3417. sd->npc_id = st->bk_npcid;
  3418. sd->state.disable_atcommand_on_npc = 0;
  3419. if(st->bk_st) {
  3420. //Remove tag for removal.
  3421. st->bk_st = NULL;
  3422. st->bk_npcid = 0;
  3423. } else if(dequeue_event) {
  3424. #ifdef SECURE_NPCTIMEOUT
  3425. /**
  3426. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3427. **/
  3428. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3429. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3430. sd->npc_idle_timer = INVALID_TIMER;
  3431. }
  3432. #endif
  3433. npc_event_dequeue(sd);
  3434. }
  3435. }
  3436. else if(st->bk_st)
  3437. {// rid was set to 0, before detaching the script state
  3438. 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);
  3439. script_reportsrc(st->bk_st);
  3440. script_free_state(st->bk_st);
  3441. st->bk_st = NULL;
  3442. }
  3443. }
  3444. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3445. ///
  3446. /// @param st Script state to attach.
  3447. static void script_attach_state(struct script_state* st)
  3448. {
  3449. struct map_session_data* sd;
  3450. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3451. {
  3452. if(st!=sd->st)
  3453. {
  3454. if(st->bk_st)
  3455. {// there is already a backup
  3456. 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);
  3457. }
  3458. st->bk_st = sd->st;
  3459. st->bk_npcid = sd->npc_id;
  3460. }
  3461. sd->st = st;
  3462. sd->npc_id = st->oid;
  3463. sd->npc_item_flag = st->npc_item_flag; // load default.
  3464. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3465. #ifdef SECURE_NPCTIMEOUT
  3466. if( sd->npc_idle_timer == INVALID_TIMER )
  3467. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3468. sd->npc_idle_tick = gettick();
  3469. #endif
  3470. }
  3471. }
  3472. /*==========================================
  3473. * The main part of the script execution
  3474. *------------------------------------------*/
  3475. void run_script_main(struct script_state *st)
  3476. {
  3477. int cmdcount = script_config.check_cmdcount;
  3478. int gotocount = script_config.check_gotocount;
  3479. TBL_PC *sd;
  3480. struct script_stack *stack=st->stack;
  3481. script_attach_state(st);
  3482. if(st->state == RERUNLINE) {
  3483. run_func(st);
  3484. if(st->state == GOTO)
  3485. st->state = RUN;
  3486. } else if(st->state != END)
  3487. st->state = RUN;
  3488. while(st->state == RUN)
  3489. {
  3490. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3491. switch(c){
  3492. case C_EOL:
  3493. if( stack->defsp > stack->sp )
  3494. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3495. else
  3496. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3497. break;
  3498. case C_INT:
  3499. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3500. break;
  3501. case C_POS:
  3502. case C_NAME:
  3503. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3504. st->pos+=3;
  3505. break;
  3506. case C_ARG:
  3507. push_val(stack,c,0);
  3508. break;
  3509. case C_STR:
  3510. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3511. while(st->script->script_buf[st->pos++]);
  3512. break;
  3513. case C_FUNC:
  3514. run_func(st);
  3515. if(st->state==GOTO){
  3516. st->state = RUN;
  3517. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3518. ShowError("run_script: infinity loop !\n");
  3519. script_reportsrc(st);
  3520. st->state=END;
  3521. }
  3522. }
  3523. break;
  3524. case C_REF:
  3525. st->op2ref = 1;
  3526. break;
  3527. case C_NEG:
  3528. case C_NOT:
  3529. case C_LNOT:
  3530. op_1(st ,c);
  3531. break;
  3532. case C_ADD:
  3533. case C_SUB:
  3534. case C_MUL:
  3535. case C_DIV:
  3536. case C_MOD:
  3537. case C_EQ:
  3538. case C_NE:
  3539. case C_GT:
  3540. case C_GE:
  3541. case C_LT:
  3542. case C_LE:
  3543. case C_AND:
  3544. case C_OR:
  3545. case C_XOR:
  3546. case C_LAND:
  3547. case C_LOR:
  3548. case C_R_SHIFT:
  3549. case C_L_SHIFT:
  3550. op_2(st, c);
  3551. break;
  3552. case C_OP3:
  3553. op_3(st, c);
  3554. break;
  3555. case C_NOP:
  3556. st->state=END;
  3557. break;
  3558. default:
  3559. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3560. st->state=END;
  3561. break;
  3562. }
  3563. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3564. ShowError("run_script: infinity loop !\n");
  3565. script_reportsrc(st);
  3566. st->state=END;
  3567. }
  3568. }
  3569. if(st->sleep.tick > 0) {
  3570. //Restore previous script
  3571. script_detach_state(st, false);
  3572. //Delay execution
  3573. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3574. st->sleep.charid = sd?sd->status.char_id:0;
  3575. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3576. run_script_timer, st->sleep.charid, (intptr_t)st);
  3577. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3578. }
  3579. else if(st->state != END && st->rid){
  3580. //Resume later (st is already attached to player).
  3581. if(st->bk_st) {
  3582. ShowWarning("Unable to restore stack! Double continuation!\n");
  3583. //Report BOTH scripts to see if that can help somehow.
  3584. ShowDebug("Previous script (lost):\n");
  3585. script_reportsrc(st->bk_st);
  3586. ShowDebug("Current script:\n");
  3587. script_reportsrc(st);
  3588. script_free_state(st->bk_st);
  3589. st->bk_st = NULL;
  3590. }
  3591. } else {
  3592. //Dispose of script.
  3593. if ((sd = map_id2sd(st->rid))!=NULL)
  3594. { //Restore previous stack and save char.
  3595. if(sd->state.using_fake_npc){
  3596. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3597. sd->state.using_fake_npc = 0;
  3598. }
  3599. //Restore previous script if any.
  3600. script_detach_state(st, true);
  3601. if (sd->state.reg_dirty&2)
  3602. intif_saveregistry(sd,2);
  3603. if (sd->state.reg_dirty&1)
  3604. intif_saveregistry(sd,1);
  3605. }
  3606. script_free_state(st);
  3607. }
  3608. }
  3609. int script_config_read(char *cfgName)
  3610. {
  3611. int i;
  3612. char line[1024],w1[1024],w2[1024];
  3613. FILE *fp;
  3614. fp=fopen(cfgName,"r");
  3615. if(fp==NULL){
  3616. ShowError("File not found: %s\n", cfgName);
  3617. return 1;
  3618. }
  3619. while(fgets(line, sizeof(line), fp))
  3620. {
  3621. if(line[0] == '/' && line[1] == '/')
  3622. continue;
  3623. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3624. if(i!=2)
  3625. continue;
  3626. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3627. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3628. }
  3629. else if(strcmpi(w1,"check_cmdcount")==0) {
  3630. script_config.check_cmdcount = config_switch(w2);
  3631. }
  3632. else if(strcmpi(w1,"check_gotocount")==0) {
  3633. script_config.check_gotocount = config_switch(w2);
  3634. }
  3635. else if(strcmpi(w1,"input_min_value")==0) {
  3636. script_config.input_min_value = config_switch(w2);
  3637. }
  3638. else if(strcmpi(w1,"input_max_value")==0) {
  3639. script_config.input_max_value = config_switch(w2);
  3640. }
  3641. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3642. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3643. }
  3644. else if(strcmpi(w1,"import")==0){
  3645. script_config_read(w2);
  3646. }
  3647. else {
  3648. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3649. }
  3650. }
  3651. fclose(fp);
  3652. return 0;
  3653. }
  3654. /**
  3655. * @see DBApply
  3656. */
  3657. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3658. {
  3659. struct script_code *code = db_data2ptr(data);
  3660. if (code)
  3661. script_free_code(code);
  3662. return 0;
  3663. }
  3664. void script_run_autobonus(const char *autobonus, int id, int pos)
  3665. {
  3666. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3667. if( script )
  3668. {
  3669. current_equip_item_index = pos;
  3670. run_script(script,0,id,0);
  3671. }
  3672. }
  3673. void script_add_autobonus(const char *autobonus)
  3674. {
  3675. if( strdb_get(autobonus_db, autobonus) == NULL )
  3676. {
  3677. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3678. if( script )
  3679. strdb_put(autobonus_db, autobonus, script);
  3680. }
  3681. }
  3682. /// resets a temporary character array variable to given value
  3683. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3684. {
  3685. int key;
  3686. uint8 idx;
  3687. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3688. {
  3689. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3690. return;
  3691. }
  3692. key = add_str(varname);
  3693. if( is_string_variable(varname) )
  3694. {
  3695. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3696. {
  3697. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3698. }
  3699. }
  3700. else
  3701. {
  3702. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3703. {
  3704. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3705. }
  3706. }
  3707. }
  3708. /// sets a temporary character array variable element idx to given value
  3709. /// @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.
  3710. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3711. {
  3712. int key;
  3713. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3714. {
  3715. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3716. return;
  3717. }
  3718. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3719. {
  3720. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3721. return;
  3722. }
  3723. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3724. if( is_string_variable(varname) )
  3725. {
  3726. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3727. }
  3728. else
  3729. {
  3730. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3731. }
  3732. if( refcache )
  3733. {// save to avoid repeated add_str calls
  3734. refcache[0] = key;
  3735. }
  3736. }
  3737. #ifdef BETA_THREAD_TEST
  3738. int buildin_query_sql_sub(struct script_state* st, Sql* handle);
  3739. /* used to receive items the queryThread has already processed */
  3740. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3741. int i, cursor = 0;
  3742. bool allOk = true;
  3743. EnterSpinLock(&queryThreadLock);
  3744. for( i = 0; i < queryThreadData.count; i++ ) {
  3745. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3746. if( !entry->ok ) {
  3747. allOk = false;
  3748. continue;
  3749. }
  3750. run_script_main(entry->st);
  3751. entry->st = NULL;/* empty entries */
  3752. aFree(entry);
  3753. queryThreadData.entry[i] = NULL;
  3754. }
  3755. if( allOk ) {
  3756. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3757. delete_timer(queryThreadData.timer, queryThread_timer);
  3758. queryThreadData.timer = INVALID_TIMER;
  3759. }
  3760. /* now lets clear the mess. */
  3761. for( i = 0; i < queryThreadData.count; i++ ) {
  3762. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3763. if( entry == NULL )
  3764. continue;/* entry on hold */
  3765. /* move */
  3766. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3767. cursor++;
  3768. }
  3769. queryThreadData.count = cursor;
  3770. LeaveSpinLock(&queryThreadLock);
  3771. return 0;
  3772. }
  3773. void queryThread_add(struct script_state *st, bool type) {
  3774. int idx = 0;
  3775. struct queryThreadEntry* entry = NULL;
  3776. EnterSpinLock(&queryThreadLock);
  3777. if( queryThreadData.count++ != 0 )
  3778. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3779. idx = queryThreadData.count-1;
  3780. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3781. entry = queryThreadData.entry[idx];
  3782. entry->st = st;
  3783. entry->ok = false;
  3784. entry->type = type;
  3785. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3786. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3787. }
  3788. LeaveSpinLock(&queryThreadLock);
  3789. /* unlock the queryThread */
  3790. racond_signal(queryThreadCond);
  3791. }
  3792. /* adds a new log to the queue */
  3793. void queryThread_log(char * entry, int length) {
  3794. int idx = logThreadData.count;
  3795. EnterSpinLock(&queryThreadLock);
  3796. if( logThreadData.count++ != 0 )
  3797. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3798. CREATE(logThreadData.entry[idx], char, length + 1 );
  3799. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3800. LeaveSpinLock(&queryThreadLock);
  3801. /* unlock the queryThread */
  3802. racond_signal(queryThreadCond);
  3803. }
  3804. /* queryThread_main */
  3805. static void *queryThread_main(void *x) {
  3806. Sql *queryThread_handle = Sql_Malloc();
  3807. int i;
  3808. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) )
  3809. exit(EXIT_FAILURE);
  3810. if( strlen(default_codepage) > 0 )
  3811. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3812. Sql_ShowDebug(queryThread_handle);
  3813. if( log_config.sql_logs ) {
  3814. logmysql_handle = Sql_Malloc();
  3815. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) )
  3816. exit(EXIT_FAILURE);
  3817. if( strlen(default_codepage) > 0 )
  3818. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3819. Sql_ShowDebug(logmysql_handle);
  3820. }
  3821. while( 1 ) {
  3822. if(queryThreadTerminate > 0)
  3823. break;
  3824. EnterSpinLock(&queryThreadLock);
  3825. /* mess with queryThreadData within the lock */
  3826. for( i = 0; i < queryThreadData.count; i++ ) {
  3827. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3828. if( entry->ok )
  3829. continue;
  3830. else if ( !entry->st || !entry->st->stack ) {
  3831. entry->ok = true;/* dispose */
  3832. continue;
  3833. }
  3834. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  3835. entry->ok = true;/* we're done with this */
  3836. }
  3837. /* also check for any logs in need to be sent */
  3838. if( log_config.sql_logs ) {
  3839. for( i = 0; i < logThreadData.count; i++ ) {
  3840. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  3841. Sql_ShowDebug(logmysql_handle);
  3842. aFree(logThreadData.entry[i]);
  3843. }
  3844. logThreadData.count = 0;
  3845. }
  3846. LeaveSpinLock(&queryThreadLock);
  3847. ramutex_lock( queryThreadMutex );
  3848. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  3849. ramutex_unlock( queryThreadMutex );
  3850. }
  3851. Sql_Free(queryThread_handle);
  3852. if( log_config.sql_logs ) {
  3853. Sql_Free(logmysql_handle);
  3854. }
  3855. return NULL;
  3856. }
  3857. #endif
  3858. /*==========================================
  3859. * Destructor
  3860. *------------------------------------------*/
  3861. void do_final_script() {
  3862. int i;
  3863. #ifdef DEBUG_HASH
  3864. if (battle_config.etc_log)
  3865. {
  3866. FILE *fp = fopen("hash_dump.txt","wt");
  3867. if(fp) {
  3868. int count[SCRIPT_HASH_SIZE];
  3869. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3870. int n=0;
  3871. int min=INT_MAX,max=0,zero=0;
  3872. double mean=0.0f;
  3873. double median=0.0f;
  3874. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3875. memset(count, 0, sizeof(count));
  3876. fprintf(fp,"num : hash : data_name\n");
  3877. fprintf(fp,"---------------------------------------------------------------\n");
  3878. for(i=LABEL_START; i<str_num; i++) {
  3879. unsigned int h = calc_hash(get_str(i));
  3880. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3881. ++count[h];
  3882. }
  3883. fprintf(fp,"--------------------\n\n");
  3884. memset(count2, 0, sizeof(count2));
  3885. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3886. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3887. if(min > count[i])
  3888. min = count[i]; // minimun count of collision
  3889. if(max < count[i])
  3890. max = count[i]; // maximun count of collision
  3891. if(count[i] == 0)
  3892. zero++;
  3893. ++count2[count[i]];
  3894. }
  3895. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3896. for( i=min; i <= max; ++i ){
  3897. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3898. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3899. }
  3900. for( i=min; i <= max; ++i ){
  3901. n += count2[i];
  3902. if( n*2 >= SCRIPT_HASH_SIZE )
  3903. {
  3904. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3905. median = (i+i+1)/2.0f;
  3906. else
  3907. median = i;
  3908. break;
  3909. }
  3910. }
  3911. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3912. fclose(fp);
  3913. }
  3914. }
  3915. #endif
  3916. mapreg_final();
  3917. db_destroy(scriptlabel_db);
  3918. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3919. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3920. if(sleep_db) {
  3921. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3922. while(n) {
  3923. struct script_state *st = (struct script_state *)n->data;
  3924. script_free_state(st);
  3925. n = n->next;
  3926. }
  3927. linkdb_final(&sleep_db);
  3928. }
  3929. if (str_data)
  3930. aFree(str_data);
  3931. if (str_buf)
  3932. aFree(str_buf);
  3933. for( i = 0; i < atcmd_binding_count; i++ ) {
  3934. aFree(atcmd_binding[i]);
  3935. }
  3936. if( atcmd_binding_count != 0 )
  3937. aFree(atcmd_binding);
  3938. #ifdef BETA_THREAD_TEST
  3939. /* QueryThread */
  3940. InterlockedIncrement(&queryThreadTerminate);
  3941. racond_signal(queryThreadCond);
  3942. rathread_wait(queryThread, NULL);
  3943. // Destroy cond var and mutex.
  3944. racond_destroy( queryThreadCond );
  3945. ramutex_destroy( queryThreadMutex );
  3946. /* Clear missing vars */
  3947. for( i = 0; i < queryThreadData.count; i++ ) {
  3948. aFree(queryThreadData.entry[i]);
  3949. }
  3950. aFree(queryThreadData.entry);
  3951. for( i = 0; i < logThreadData.count; i++ ) {
  3952. aFree(logThreadData.entry[i]);
  3953. }
  3954. aFree(logThreadData.entry);
  3955. #endif
  3956. }
  3957. /*==========================================
  3958. * Initialization
  3959. *------------------------------------------*/
  3960. void do_init_script(void) {
  3961. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3962. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3963. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3964. mapreg_init();
  3965. #ifdef BETA_THREAD_TEST
  3966. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  3967. queryThreadData.count = 0;
  3968. CREATE(logThreadData.entry, char *, 1);
  3969. logThreadData.count = 0;
  3970. /* QueryThread Start */
  3971. InitializeSpinLock(&queryThreadLock);
  3972. queryThreadData.timer = INVALID_TIMER;
  3973. queryThreadTerminate = 0;
  3974. queryThreadMutex = ramutex_create();
  3975. queryThreadCond = racond_create();
  3976. queryThread = rathread_create(queryThread_main, NULL);
  3977. if(queryThread == NULL){
  3978. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  3979. exit(EXIT_FAILURE);
  3980. }
  3981. add_timer_func_list(queryThread_timer, "queryThread_timer");
  3982. #endif
  3983. }
  3984. void script_reload(void) {
  3985. int i;
  3986. #ifdef BETA_THREAD_TEST
  3987. /* we're reloading so any queries undergoing should be...exterminated. */
  3988. EnterSpinLock(&queryThreadLock);
  3989. for( i = 0; i < queryThreadData.count; i++ ) {
  3990. aFree(queryThreadData.entry[i]);
  3991. }
  3992. queryThreadData.count = 0;
  3993. if( queryThreadData.timer != INVALID_TIMER ) {
  3994. delete_timer(queryThreadData.timer, queryThread_timer);
  3995. queryThreadData.timer = INVALID_TIMER;
  3996. }
  3997. LeaveSpinLock(&queryThreadLock);
  3998. #endif
  3999. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4000. db_clear(scriptlabel_db);
  4001. // @commands (script based)
  4002. // Clear bindings
  4003. for( i = 0; i < atcmd_binding_count; i++ ) {
  4004. aFree(atcmd_binding[i]);
  4005. }
  4006. if( atcmd_binding_count != 0 )
  4007. aFree(atcmd_binding);
  4008. atcmd_binding_count = 0;
  4009. if(sleep_db) {
  4010. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  4011. while(n) {
  4012. struct script_state *st = (struct script_state *)n->data;
  4013. script_free_state(st);
  4014. n = n->next;
  4015. }
  4016. linkdb_final(&sleep_db);
  4017. }
  4018. mapreg_reload();
  4019. }
  4020. //-----------------------------------------------------------------------------
  4021. // buildin functions
  4022. //
  4023. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4024. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4025. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4026. /////////////////////////////////////////////////////////////////////
  4027. // NPC interaction
  4028. //
  4029. /// Appends a message to the npc dialog.
  4030. /// If a dialog doesn't exist yet, one is created.
  4031. ///
  4032. /// mes "<message>";
  4033. BUILDIN_FUNC(mes)
  4034. {
  4035. TBL_PC* sd = script_rid2sd(st);
  4036. if( sd == NULL )
  4037. return 0;
  4038. if( !script_hasdata(st, 3) )
  4039. {// only a single line detected in the script
  4040. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4041. }
  4042. else
  4043. {// parse multiple lines as they exist
  4044. int i;
  4045. for( i = 2; script_hasdata(st, i); i++ )
  4046. {
  4047. // send the message to the client
  4048. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4049. }
  4050. }
  4051. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4052. return SCRIPT_CMD_SUCCESS;
  4053. }
  4054. /// Displays the button 'next' in the npc dialog.
  4055. /// The dialog text is cleared and the script continues when the button is pressed.
  4056. ///
  4057. /// next;
  4058. BUILDIN_FUNC(next)
  4059. {
  4060. TBL_PC* sd;
  4061. sd = script_rid2sd(st);
  4062. if( sd == NULL )
  4063. return 0;
  4064. #ifdef SECURE_NPCTIMEOUT
  4065. sd->npc_idle_type = NPCT_WAIT;
  4066. #endif
  4067. st->state = STOP;
  4068. clif_scriptnext(sd, st->oid);
  4069. return SCRIPT_CMD_SUCCESS;
  4070. }
  4071. /// Ends the script and displays the button 'close' on the npc dialog.
  4072. /// The dialog is closed when the button is pressed.
  4073. ///
  4074. /// close;
  4075. BUILDIN_FUNC(close)
  4076. {
  4077. TBL_PC* sd;
  4078. sd = script_rid2sd(st);
  4079. if( sd == NULL )
  4080. return 0;
  4081. if( !st->mes_active ) {
  4082. TBL_NPC* nd = map_id2nd(st->oid);
  4083. st->state = END; // Keep backwards compatibility.
  4084. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4085. } else {
  4086. st->state = CLOSE;
  4087. st->mes_active = 0;
  4088. }
  4089. clif_scriptclose(sd, st->oid);
  4090. return SCRIPT_CMD_SUCCESS;
  4091. }
  4092. /// Displays the button 'close' on the npc dialog.
  4093. /// The dialog is closed and the script continues when the button is pressed.
  4094. ///
  4095. /// close2;
  4096. BUILDIN_FUNC(close2)
  4097. {
  4098. TBL_PC* sd;
  4099. sd = script_rid2sd(st);
  4100. if( sd == NULL )
  4101. return 0;
  4102. st->state = STOP;
  4103. if( st->mes_active )
  4104. st->mes_active = 0;
  4105. clif_scriptclose(sd, st->oid);
  4106. return SCRIPT_CMD_SUCCESS;
  4107. }
  4108. /// Counts the number of valid and total number of options in 'str'
  4109. /// If max_count > 0 the counting stops when that valid option is reached
  4110. /// total is incremented for each option (NULL is supported)
  4111. static int menu_countoptions(const char* str, int max_count, int* total)
  4112. {
  4113. int count = 0;
  4114. int bogus_total;
  4115. if( total == NULL )
  4116. total = &bogus_total;
  4117. ++(*total);
  4118. // initial empty options
  4119. while( *str == ':' )
  4120. {
  4121. ++str;
  4122. ++(*total);
  4123. }
  4124. // count menu options
  4125. while( *str != '\0' )
  4126. {
  4127. ++count;
  4128. --max_count;
  4129. if( max_count == 0 )
  4130. break;
  4131. while( *str != ':' && *str != '\0' )
  4132. ++str;
  4133. while( *str == ':' )
  4134. {
  4135. ++str;
  4136. ++(*total);
  4137. }
  4138. }
  4139. return count;
  4140. }
  4141. /// Displays a menu with options and goes to the target label.
  4142. /// The script is stopped if cancel is pressed.
  4143. /// Options with no text are not displayed in the client.
  4144. ///
  4145. /// Options can be grouped together, separated by the character ':' in the text:
  4146. /// ex: menu "A:B:C",L_target;
  4147. /// All these options go to the specified target label.
  4148. ///
  4149. /// The index of the selected option is put in the variable @menu.
  4150. /// Indexes start with 1 and are consistent with grouped and empty options.
  4151. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4152. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4153. ///
  4154. /// NOTE: the client closes the npc dialog when cancel is pressed
  4155. ///
  4156. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4157. BUILDIN_FUNC(menu)
  4158. {
  4159. int i;
  4160. const char* text;
  4161. TBL_PC* sd;
  4162. sd = script_rid2sd(st);
  4163. if( sd == NULL )
  4164. return 0;
  4165. #ifdef SECURE_NPCTIMEOUT
  4166. sd->npc_idle_type = NPCT_MENU;
  4167. #endif
  4168. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4169. if( sd->state.menu_or_input == 0 )
  4170. {
  4171. struct StringBuf buf;
  4172. if( script_lastdata(st) % 2 == 0 )
  4173. {// argument count is not even (1st argument is at index 2)
  4174. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4175. st->state = END;
  4176. return SCRIPT_CMD_FAILURE;
  4177. }
  4178. StringBuf_Init(&buf);
  4179. sd->npc_menu = 0;
  4180. for( i = 2; i < script_lastdata(st); i += 2 )
  4181. {
  4182. struct script_data* data;
  4183. // menu options
  4184. text = script_getstr(st, i);
  4185. // target label
  4186. data = script_getdata(st, i+1);
  4187. if( !data_islabel(data) )
  4188. {// not a label
  4189. StringBuf_Destroy(&buf);
  4190. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4191. script_reportdata(data);
  4192. st->state = END;
  4193. return SCRIPT_CMD_FAILURE;
  4194. }
  4195. // append option(s)
  4196. if( text[0] == '\0' )
  4197. continue;// empty string, ignore
  4198. if( sd->npc_menu > 0 )
  4199. StringBuf_AppendStr(&buf, ":");
  4200. StringBuf_AppendStr(&buf, text);
  4201. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4202. }
  4203. st->state = RERUNLINE;
  4204. sd->state.menu_or_input = 1;
  4205. /**
  4206. * menus beyond this length crash the client (see bugreport:6402)
  4207. **/
  4208. if( StringBuf_Length(&buf) >= 2047 ) {
  4209. struct npc_data * nd = map_id2nd(st->oid);
  4210. char* menu;
  4211. CREATE(menu, char, 2048);
  4212. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4213. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4214. clif_scriptmenu(sd, st->oid, menu);
  4215. aFree(menu);
  4216. } else
  4217. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4218. StringBuf_Destroy(&buf);
  4219. if( sd->npc_menu >= 0xff )
  4220. {// 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
  4221. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4222. script_reportsrc(st);
  4223. }
  4224. }
  4225. else if( sd->npc_menu == 0xff )
  4226. {// Cancel was pressed
  4227. sd->state.menu_or_input = 0;
  4228. st->state = END;
  4229. }
  4230. else
  4231. {// goto target label
  4232. int menu = 0;
  4233. sd->state.menu_or_input = 0;
  4234. if( sd->npc_menu <= 0 )
  4235. {
  4236. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4237. st->state = END;
  4238. return SCRIPT_CMD_FAILURE;
  4239. }
  4240. // get target label
  4241. for( i = 2; i < script_lastdata(st); i += 2 )
  4242. {
  4243. text = script_getstr(st, i);
  4244. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4245. if( sd->npc_menu <= 0 )
  4246. break;// entry found
  4247. }
  4248. if( sd->npc_menu > 0 )
  4249. {// Invalid selection
  4250. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4251. st->state = END;
  4252. return SCRIPT_CMD_FAILURE;
  4253. }
  4254. if( !data_islabel(script_getdata(st, i + 1)) )
  4255. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4256. ShowError("script:menu: unexpected data in label argument\n");
  4257. script_reportdata(script_getdata(st, i + 1));
  4258. st->state = END;
  4259. return SCRIPT_CMD_FAILURE;
  4260. }
  4261. pc_setreg(sd, add_str("@menu"), menu);
  4262. st->pos = script_getnum(st, i + 1);
  4263. st->state = GOTO;
  4264. }
  4265. return SCRIPT_CMD_SUCCESS;
  4266. }
  4267. /// Displays a menu with options and returns the selected option.
  4268. /// Behaves like 'menu' without the target labels.
  4269. ///
  4270. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4271. ///
  4272. /// @see menu
  4273. BUILDIN_FUNC(select)
  4274. {
  4275. int i;
  4276. const char* text;
  4277. TBL_PC* sd;
  4278. sd = script_rid2sd(st);
  4279. if( sd == NULL )
  4280. return 0;
  4281. #ifdef SECURE_NPCTIMEOUT
  4282. sd->npc_idle_type = NPCT_MENU;
  4283. #endif
  4284. if( sd->state.menu_or_input == 0 ) {
  4285. struct StringBuf buf;
  4286. StringBuf_Init(&buf);
  4287. sd->npc_menu = 0;
  4288. for( i = 2; i <= script_lastdata(st); ++i ) {
  4289. text = script_getstr(st, i);
  4290. if( sd->npc_menu > 0 )
  4291. StringBuf_AppendStr(&buf, ":");
  4292. StringBuf_AppendStr(&buf, text);
  4293. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4294. }
  4295. st->state = RERUNLINE;
  4296. sd->state.menu_or_input = 1;
  4297. /**
  4298. * menus beyond this length crash the client (see bugreport:6402)
  4299. **/
  4300. if( StringBuf_Length(&buf) >= 2047 ) {
  4301. struct npc_data * nd = map_id2nd(st->oid);
  4302. char* menu;
  4303. CREATE(menu, char, 2048);
  4304. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4305. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4306. clif_scriptmenu(sd, st->oid, menu);
  4307. aFree(menu);
  4308. } else
  4309. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4310. StringBuf_Destroy(&buf);
  4311. if( sd->npc_menu >= 0xff ) {
  4312. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4313. script_reportsrc(st);
  4314. }
  4315. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4316. sd->state.menu_or_input = 0;
  4317. st->state = END;
  4318. } else {// return selected option
  4319. int menu = 0;
  4320. sd->state.menu_or_input = 0;
  4321. for( i = 2; i <= script_lastdata(st); ++i ) {
  4322. text = script_getstr(st, i);
  4323. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4324. if( sd->npc_menu <= 0 )
  4325. break;// entry found
  4326. }
  4327. pc_setreg(sd, add_str("@menu"), menu);
  4328. script_pushint(st, menu);
  4329. st->state = RUN;
  4330. }
  4331. return SCRIPT_CMD_SUCCESS;
  4332. }
  4333. /// Displays a menu with options and returns the selected option.
  4334. /// Behaves like 'menu' without the target labels, except when cancel is
  4335. /// pressed.
  4336. /// When cancel is pressed, the script continues and 255 is returned.
  4337. ///
  4338. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4339. ///
  4340. /// @see menu
  4341. BUILDIN_FUNC(prompt)
  4342. {
  4343. int i;
  4344. const char *text;
  4345. TBL_PC* sd;
  4346. sd = script_rid2sd(st);
  4347. if( sd == NULL )
  4348. return 0;
  4349. #ifdef SECURE_NPCTIMEOUT
  4350. sd->npc_idle_type = NPCT_MENU;
  4351. #endif
  4352. if( sd->state.menu_or_input == 0 )
  4353. {
  4354. struct StringBuf buf;
  4355. StringBuf_Init(&buf);
  4356. sd->npc_menu = 0;
  4357. for( i = 2; i <= script_lastdata(st); ++i )
  4358. {
  4359. text = script_getstr(st, i);
  4360. if( sd->npc_menu > 0 )
  4361. StringBuf_AppendStr(&buf, ":");
  4362. StringBuf_AppendStr(&buf, text);
  4363. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4364. }
  4365. st->state = RERUNLINE;
  4366. sd->state.menu_or_input = 1;
  4367. /**
  4368. * menus beyond this length crash the client (see bugreport:6402)
  4369. **/
  4370. if( StringBuf_Length(&buf) >= 2047 ) {
  4371. struct npc_data * nd = map_id2nd(st->oid);
  4372. char* menu;
  4373. CREATE(menu, char, 2048);
  4374. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4375. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4376. clif_scriptmenu(sd, st->oid, menu);
  4377. aFree(menu);
  4378. } else
  4379. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4380. StringBuf_Destroy(&buf);
  4381. if( sd->npc_menu >= 0xff )
  4382. {
  4383. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4384. script_reportsrc(st);
  4385. }
  4386. }
  4387. else if( sd->npc_menu == 0xff )
  4388. {// Cancel was pressed
  4389. sd->state.menu_or_input = 0;
  4390. pc_setreg(sd, add_str("@menu"), 0xff);
  4391. script_pushint(st, 0xff);
  4392. st->state = RUN;
  4393. }
  4394. else
  4395. {// return selected option
  4396. int menu = 0;
  4397. sd->state.menu_or_input = 0;
  4398. for( i = 2; i <= script_lastdata(st); ++i )
  4399. {
  4400. text = script_getstr(st, i);
  4401. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4402. if( sd->npc_menu <= 0 )
  4403. break;// entry found
  4404. }
  4405. pc_setreg(sd, add_str("@menu"), menu);
  4406. script_pushint(st, menu);
  4407. st->state = RUN;
  4408. }
  4409. return SCRIPT_CMD_SUCCESS;
  4410. }
  4411. /////////////////////////////////////////////////////////////////////
  4412. // ...
  4413. //
  4414. /// Jumps to the target script label.
  4415. ///
  4416. /// goto <label>;
  4417. BUILDIN_FUNC(goto)
  4418. {
  4419. if( !data_islabel(script_getdata(st,2)) )
  4420. {
  4421. ShowError("script:goto: not a label\n");
  4422. script_reportdata(script_getdata(st,2));
  4423. st->state = END;
  4424. return 1;
  4425. }
  4426. st->pos = script_getnum(st,2);
  4427. st->state = GOTO;
  4428. return SCRIPT_CMD_SUCCESS;
  4429. }
  4430. /*==========================================
  4431. * user-defined function call
  4432. *------------------------------------------*/
  4433. BUILDIN_FUNC(callfunc)
  4434. {
  4435. int i, j;
  4436. struct script_retinfo* ri;
  4437. struct script_code* scr;
  4438. const char* str = script_getstr(st,2);
  4439. DBMap **ref = NULL;
  4440. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4441. if( !scr )
  4442. {
  4443. ShowError("script:callfunc: function not found! [%s]\n", str);
  4444. st->state = END;
  4445. return 1;
  4446. }
  4447. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4448. {
  4449. struct script_data* data = push_copy(st->stack,i);
  4450. if( data_isreference(data) && !data->ref )
  4451. {
  4452. const char* name = reference_getname(data);
  4453. if( name[0] == '.' ) {
  4454. if(!ref){
  4455. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4456. ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars);
  4457. }
  4458. data->ref = ref;
  4459. }
  4460. }
  4461. }
  4462. CREATE(ri, struct script_retinfo, 1);
  4463. ri->script = st->script;// script code
  4464. ri->var_function = st->stack->var_function;// scope variables
  4465. ri->pos = st->pos;// script location
  4466. ri->nargs = j;// argument count
  4467. ri->defsp = st->stack->defsp;// default stack pointer
  4468. push_retinfo(st->stack, ri, ref);
  4469. st->pos = 0;
  4470. st->script = scr;
  4471. st->stack->defsp = st->stack->sp;
  4472. st->state = GOTO;
  4473. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4474. return SCRIPT_CMD_SUCCESS;
  4475. }
  4476. /*==========================================
  4477. * subroutine call
  4478. *------------------------------------------*/
  4479. BUILDIN_FUNC(callsub)
  4480. {
  4481. int i,j;
  4482. struct script_retinfo* ri;
  4483. int pos = script_getnum(st,2);
  4484. DBMap **ref = NULL;
  4485. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4486. {
  4487. ShowError("script:callsub: argument is not a label\n");
  4488. script_reportdata(script_getdata(st,2));
  4489. st->state = END;
  4490. return 1;
  4491. }
  4492. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4493. {
  4494. struct script_data* data = push_copy(st->stack,i);
  4495. if( data_isreference(data) && !data->ref )
  4496. {
  4497. const char* name = reference_getname(data);
  4498. if( name[0] == '.' && name[1] == '@' ) {
  4499. if ( !ref ) {
  4500. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4501. ref[0] = st->stack->var_function;
  4502. }
  4503. data->ref = ref;
  4504. }
  4505. }
  4506. }
  4507. CREATE(ri, struct script_retinfo, 1);
  4508. ri->script = st->script;// script code
  4509. ri->var_function = st->stack->var_function;// scope variables
  4510. ri->pos = st->pos;// script location
  4511. ri->nargs = j;// argument count
  4512. ri->defsp = st->stack->defsp;// default stack pointer
  4513. push_retinfo(st->stack, ri, ref);
  4514. st->pos = pos;
  4515. st->stack->defsp = st->stack->sp;
  4516. st->state = GOTO;
  4517. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4518. return SCRIPT_CMD_SUCCESS;
  4519. }
  4520. /// Retrieves an argument provided to callfunc/callsub.
  4521. /// If the argument doesn't exist
  4522. ///
  4523. /// getarg(<index>{,<default_value>}) -> <value>
  4524. BUILDIN_FUNC(getarg)
  4525. {
  4526. struct script_retinfo* ri;
  4527. int idx;
  4528. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4529. {
  4530. ShowError("script:getarg: no callfunc or callsub!\n");
  4531. st->state = END;
  4532. return 1;
  4533. }
  4534. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4535. idx = script_getnum(st,2);
  4536. if( idx >= 0 && idx < ri->nargs )
  4537. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4538. else if( script_hasdata(st,3) )
  4539. script_pushcopy(st, 3);
  4540. else
  4541. {
  4542. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4543. st->state = END;
  4544. return 1;
  4545. }
  4546. return SCRIPT_CMD_SUCCESS;
  4547. }
  4548. /// Returns from the current function, optionaly returning a value from the functions.
  4549. /// Don't use outside script functions.
  4550. ///
  4551. /// return;
  4552. /// return <value>;
  4553. BUILDIN_FUNC(return)
  4554. {
  4555. if( script_hasdata(st,2) )
  4556. {// return value
  4557. struct script_data* data;
  4558. script_pushcopy(st, 2);
  4559. data = script_getdatatop(st, -1);
  4560. if( data_isreference(data) )
  4561. {
  4562. const char* name = reference_getname(data);
  4563. if( name[0] == '.' && name[1] == '@' )
  4564. {// scope variable
  4565. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4566. get_val(st, data);// current scope, convert to value
  4567. }
  4568. else if( name[0] == '.' && !data->ref )
  4569. {// script variable, link to current script
  4570. data->ref = &st->script->script_vars;
  4571. }
  4572. }
  4573. }
  4574. else
  4575. {// no return value
  4576. script_pushnil(st);
  4577. }
  4578. st->state = RETFUNC;
  4579. return SCRIPT_CMD_SUCCESS;
  4580. }
  4581. /// Returns a random number from 0 to <range>-1.
  4582. /// Or returns a random number from <min> to <max>.
  4583. /// If <min> is greater than <max>, their numbers are switched.
  4584. /// rand(<range>) -> <int>
  4585. /// rand(<min>,<max>) -> <int>
  4586. BUILDIN_FUNC(rand)
  4587. {
  4588. int range;
  4589. int min;
  4590. if( script_hasdata(st,3) )
  4591. {// min,max
  4592. int max = script_getnum(st,3);
  4593. min = script_getnum(st,2);
  4594. if( max < min )
  4595. swap(min, max);
  4596. range = max - min + 1;
  4597. }
  4598. else
  4599. {// range
  4600. min = 0;
  4601. range = script_getnum(st,2);
  4602. }
  4603. if( range <= 1 )
  4604. script_pushint(st, min);
  4605. else
  4606. script_pushint(st, rnd()%range + min);
  4607. return SCRIPT_CMD_SUCCESS;
  4608. }
  4609. /*==========================================
  4610. * Warp sd to str,x,y or Random or SavePoint/Save
  4611. *------------------------------------------*/
  4612. BUILDIN_FUNC(warp)
  4613. {
  4614. int ret;
  4615. int x,y;
  4616. const char* str;
  4617. TBL_PC* sd;
  4618. sd = script_rid2sd(st);
  4619. if( sd == NULL )
  4620. return 0;
  4621. str = script_getstr(st,2);
  4622. x = script_getnum(st,3);
  4623. y = script_getnum(st,4);
  4624. if(strcmp(str,"Random")==0)
  4625. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4626. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4627. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4628. else
  4629. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4630. if( ret ) {
  4631. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4632. return 1;
  4633. }
  4634. return SCRIPT_CMD_SUCCESS;
  4635. }
  4636. /*==========================================
  4637. * Warp a specified area
  4638. *------------------------------------------*/
  4639. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4640. {
  4641. int x2,y2,x3,y3;
  4642. unsigned int index;
  4643. index = va_arg(ap,unsigned int);
  4644. x2 = va_arg(ap,int);
  4645. y2 = va_arg(ap,int);
  4646. x3 = va_arg(ap,int);
  4647. y3 = va_arg(ap,int);
  4648. if(index == 0)
  4649. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4650. else if(x3 && y3) {
  4651. int max, tx, ty, j = 0;
  4652. // choose a suitable max number of attempts
  4653. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4654. max = 1000;
  4655. // find a suitable map cell
  4656. do {
  4657. tx = rnd()%(x3-x2+1)+x2;
  4658. ty = rnd()%(y3-y2+1)+y2;
  4659. j++;
  4660. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4661. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4662. }
  4663. else
  4664. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4665. return 0;
  4666. }
  4667. BUILDIN_FUNC(areawarp)
  4668. {
  4669. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4670. unsigned int index;
  4671. const char *str;
  4672. const char *mapname;
  4673. mapname = script_getstr(st,2);
  4674. x0 = script_getnum(st,3);
  4675. y0 = script_getnum(st,4);
  4676. x1 = script_getnum(st,5);
  4677. y1 = script_getnum(st,6);
  4678. str = script_getstr(st,7);
  4679. x2 = script_getnum(st,8);
  4680. y2 = script_getnum(st,9);
  4681. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4682. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4683. x3 = 0;
  4684. y3 = 0;
  4685. } else if( x3 && y3 ) {
  4686. // normalize x3/y3 coordinates
  4687. if( x3 < x2 ) swap(x3,x2);
  4688. if( y3 < y2 ) swap(y3,y2);
  4689. }
  4690. }
  4691. if( (m = map_mapname2mapid(mapname)) < 0 )
  4692. return 0;
  4693. if( strcmp(str,"Random") == 0 )
  4694. index = 0;
  4695. else if( !(index=mapindex_name2id(str)) )
  4696. return 0;
  4697. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4698. return SCRIPT_CMD_SUCCESS;
  4699. }
  4700. /*==========================================
  4701. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4702. *------------------------------------------*/
  4703. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4704. {
  4705. int hp, sp;
  4706. hp = va_arg(ap, int);
  4707. sp = va_arg(ap, int);
  4708. pc_percentheal((TBL_PC *)bl,hp,sp);
  4709. return 0;
  4710. }
  4711. BUILDIN_FUNC(areapercentheal)
  4712. {
  4713. int hp,sp,m;
  4714. const char *mapname;
  4715. int x0,y0,x1,y1;
  4716. mapname=script_getstr(st,2);
  4717. x0=script_getnum(st,3);
  4718. y0=script_getnum(st,4);
  4719. x1=script_getnum(st,5);
  4720. y1=script_getnum(st,6);
  4721. hp=script_getnum(st,7);
  4722. sp=script_getnum(st,8);
  4723. if( (m=map_mapname2mapid(mapname))< 0)
  4724. return 0;
  4725. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4726. return SCRIPT_CMD_SUCCESS;
  4727. }
  4728. /*==========================================
  4729. * warpchar [LuzZza]
  4730. * Useful for warp one player from
  4731. * another player npc-session.
  4732. * Using: warpchar "mapname",x,y,Char_ID;
  4733. *------------------------------------------*/
  4734. BUILDIN_FUNC(warpchar)
  4735. {
  4736. int x,y,a;
  4737. const char *str;
  4738. TBL_PC *sd;
  4739. str=script_getstr(st,2);
  4740. x=script_getnum(st,3);
  4741. y=script_getnum(st,4);
  4742. a=script_getnum(st,5);
  4743. sd = map_charid2sd(a);
  4744. if( sd == NULL )
  4745. return 0;
  4746. if(strcmp(str, "Random") == 0)
  4747. pc_randomwarp(sd, CLR_TELEPORT);
  4748. else
  4749. if(strcmp(str, "SavePoint") == 0)
  4750. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4751. else
  4752. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4753. return SCRIPT_CMD_SUCCESS;
  4754. }
  4755. /*==========================================
  4756. * Warpparty - [Fredzilla] [Paradox924X]
  4757. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4758. * If 'from_mapname' is specified, only the party members on that map will be warped
  4759. *------------------------------------------*/
  4760. BUILDIN_FUNC(warpparty)
  4761. {
  4762. TBL_PC *sd = NULL;
  4763. TBL_PC *pl_sd;
  4764. struct party_data* p;
  4765. int type;
  4766. int mapindex;
  4767. int i;
  4768. const char* str = script_getstr(st,2);
  4769. int x = script_getnum(st,3);
  4770. int y = script_getnum(st,4);
  4771. int p_id = script_getnum(st,5);
  4772. const char* str2 = NULL;
  4773. if ( script_hasdata(st,6) )
  4774. str2 = script_getstr(st,6);
  4775. p = party_search(p_id);
  4776. if(!p)
  4777. return 0;
  4778. type = ( strcmp(str,"Random")==0 ) ? 0
  4779. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4780. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4781. : ( strcmp(str,"Leader")==0 ) ? 3
  4782. : 4;
  4783. switch (type)
  4784. {
  4785. case 3:
  4786. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4787. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4788. return 0;
  4789. pl_sd = p->data[i].sd;
  4790. mapindex = pl_sd->mapindex;
  4791. x = pl_sd->bl.x;
  4792. y = pl_sd->bl.y;
  4793. break;
  4794. case 4:
  4795. mapindex = mapindex_name2id(str);
  4796. break;
  4797. case 2:
  4798. //"SavePoint" uses save point of the currently attached player
  4799. if (( sd = script_rid2sd(st) ) == NULL )
  4800. return 0;
  4801. default:
  4802. mapindex = 0;
  4803. break;
  4804. }
  4805. for (i = 0; i < MAX_PARTY; i++)
  4806. {
  4807. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4808. continue;
  4809. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4810. continue;
  4811. if( pc_isdead(pl_sd) )
  4812. continue;
  4813. switch( type )
  4814. {
  4815. case 0: // Random
  4816. if(!map[pl_sd->bl.m].flag.nowarp)
  4817. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4818. break;
  4819. case 1: // SavePointAll
  4820. if(!map[pl_sd->bl.m].flag.noreturn)
  4821. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4822. break;
  4823. case 2: // SavePoint
  4824. if(!map[pl_sd->bl.m].flag.noreturn)
  4825. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4826. break;
  4827. case 3: // Leader
  4828. case 4: // m,x,y
  4829. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4830. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4831. break;
  4832. }
  4833. }
  4834. return SCRIPT_CMD_SUCCESS;
  4835. }
  4836. /*==========================================
  4837. * Warpguild - [Fredzilla]
  4838. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4839. *------------------------------------------*/
  4840. BUILDIN_FUNC(warpguild)
  4841. {
  4842. TBL_PC *sd = NULL;
  4843. TBL_PC *pl_sd;
  4844. struct guild* g;
  4845. struct s_mapiterator* iter;
  4846. int type;
  4847. const char* str = script_getstr(st,2);
  4848. int x = script_getnum(st,3);
  4849. int y = script_getnum(st,4);
  4850. int gid = script_getnum(st,5);
  4851. g = guild_search(gid);
  4852. if( g == NULL )
  4853. return 0;
  4854. type = ( strcmp(str,"Random")==0 ) ? 0
  4855. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4856. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4857. : 3;
  4858. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4859. {// "SavePoint" uses save point of the currently attached player
  4860. return 0;
  4861. }
  4862. iter = mapit_getallusers();
  4863. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4864. {
  4865. if( pl_sd->status.guild_id != gid )
  4866. continue;
  4867. switch( type )
  4868. {
  4869. case 0: // Random
  4870. if(!map[pl_sd->bl.m].flag.nowarp)
  4871. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4872. break;
  4873. case 1: // SavePointAll
  4874. if(!map[pl_sd->bl.m].flag.noreturn)
  4875. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4876. break;
  4877. case 2: // SavePoint
  4878. if(!map[pl_sd->bl.m].flag.noreturn)
  4879. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4880. break;
  4881. case 3: // m,x,y
  4882. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4883. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4884. break;
  4885. }
  4886. }
  4887. mapit_free(iter);
  4888. return SCRIPT_CMD_SUCCESS;
  4889. }
  4890. /*==========================================
  4891. * Force Heal a player (hp and sp)
  4892. *------------------------------------------*/
  4893. BUILDIN_FUNC(heal)
  4894. {
  4895. TBL_PC *sd;
  4896. int hp,sp;
  4897. sd = script_rid2sd(st);
  4898. if (!sd) return 0;
  4899. hp=script_getnum(st,2);
  4900. sp=script_getnum(st,3);
  4901. status_heal(&sd->bl, hp, sp, 1);
  4902. return SCRIPT_CMD_SUCCESS;
  4903. }
  4904. /*==========================================
  4905. * Heal a player by item (get vit bonus etc)
  4906. *------------------------------------------*/
  4907. BUILDIN_FUNC(itemheal)
  4908. {
  4909. TBL_PC *sd;
  4910. int hp,sp;
  4911. hp=script_getnum(st,2);
  4912. sp=script_getnum(st,3);
  4913. if(potion_flag==1) {
  4914. potion_hp = hp;
  4915. potion_sp = sp;
  4916. return 0;
  4917. }
  4918. sd = script_rid2sd(st);
  4919. if (!sd) return 0;
  4920. pc_itemheal(sd,sd->itemid,hp,sp);
  4921. return SCRIPT_CMD_SUCCESS;
  4922. }
  4923. /*==========================================
  4924. *
  4925. *------------------------------------------*/
  4926. BUILDIN_FUNC(percentheal)
  4927. {
  4928. int hp,sp;
  4929. TBL_PC* sd;
  4930. hp=script_getnum(st,2);
  4931. sp=script_getnum(st,3);
  4932. if(potion_flag==1) {
  4933. potion_per_hp = hp;
  4934. potion_per_sp = sp;
  4935. return 0;
  4936. }
  4937. sd = script_rid2sd(st);
  4938. if( sd == NULL )
  4939. return 0;
  4940. #ifdef RENEWAL
  4941. if( sd->sc.data[SC_EXTREMITYFIST2] )
  4942. sp = 0;
  4943. #endif
  4944. pc_percentheal(sd,hp,sp);
  4945. return SCRIPT_CMD_SUCCESS;
  4946. }
  4947. /*==========================================
  4948. *
  4949. *------------------------------------------*/
  4950. BUILDIN_FUNC(jobchange)
  4951. {
  4952. int job, upper=-1;
  4953. job=script_getnum(st,2);
  4954. if( script_hasdata(st,3) )
  4955. upper=script_getnum(st,3);
  4956. if (pcdb_checkid(job))
  4957. {
  4958. TBL_PC* sd;
  4959. sd = script_rid2sd(st);
  4960. if( sd == NULL )
  4961. return 0;
  4962. pc_jobchange(sd, job, upper);
  4963. }
  4964. return SCRIPT_CMD_SUCCESS;
  4965. }
  4966. /*==========================================
  4967. *
  4968. *------------------------------------------*/
  4969. BUILDIN_FUNC(jobname)
  4970. {
  4971. int class_=script_getnum(st,2);
  4972. script_pushconststr(st, (char*)job_name(class_));
  4973. return SCRIPT_CMD_SUCCESS;
  4974. }
  4975. /// Get input from the player.
  4976. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4977. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4978. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4979. /// shorter than 'min' and 0 otherwise.
  4980. ///
  4981. /// input(<var>{,<min>{,<max>}}) -> <int>
  4982. BUILDIN_FUNC(input)
  4983. {
  4984. TBL_PC* sd;
  4985. struct script_data* data;
  4986. int uid;
  4987. const char* name;
  4988. int min;
  4989. int max;
  4990. sd = script_rid2sd(st);
  4991. if( sd == NULL )
  4992. return 0;
  4993. data = script_getdata(st,2);
  4994. if( !data_isreference(data) ){
  4995. ShowError("script:input: not a variable\n");
  4996. script_reportdata(data);
  4997. st->state = END;
  4998. return 1;
  4999. }
  5000. uid = reference_getuid(data);
  5001. name = reference_getname(data);
  5002. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5003. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5004. #ifdef SECURE_NPCTIMEOUT
  5005. sd->npc_idle_type = NPCT_WAIT;
  5006. #endif
  5007. if( !sd->state.menu_or_input )
  5008. { // first invocation, display npc input box
  5009. sd->state.menu_or_input = 1;
  5010. st->state = RERUNLINE;
  5011. if( is_string_variable(name) )
  5012. clif_scriptinputstr(sd,st->oid);
  5013. else
  5014. clif_scriptinput(sd,st->oid);
  5015. }
  5016. else
  5017. { // take received text/value and store it in the designated variable
  5018. sd->state.menu_or_input = 0;
  5019. if( is_string_variable(name) )
  5020. {
  5021. int len = (int)strlen(sd->npc_str);
  5022. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5023. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5024. }
  5025. else
  5026. {
  5027. int amount = sd->npc_amount;
  5028. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5029. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5030. }
  5031. st->state = RUN;
  5032. }
  5033. return SCRIPT_CMD_SUCCESS;
  5034. }
  5035. // declare the copyarray method here for future reference
  5036. BUILDIN_FUNC(copyarray);
  5037. /// Sets the value of a variable.
  5038. /// The value is converted to the type of the variable.
  5039. ///
  5040. /// set(<variable>,<value>) -> <variable>
  5041. BUILDIN_FUNC(set)
  5042. {
  5043. TBL_PC* sd = NULL;
  5044. struct script_data* data;
  5045. //struct script_data* datavalue;
  5046. int num;
  5047. const char* name;
  5048. char prefix;
  5049. data = script_getdata(st,2);
  5050. //datavalue = script_getdata(st,3);
  5051. if( !data_isreference(data) )
  5052. {
  5053. ShowError("script:set: not a variable\n");
  5054. script_reportdata(script_getdata(st,2));
  5055. st->state = END;
  5056. return 1;
  5057. }
  5058. num = reference_getuid(data);
  5059. name = reference_getname(data);
  5060. prefix = *name;
  5061. if( not_server_variable(prefix) )
  5062. {
  5063. sd = script_rid2sd(st);
  5064. if( sd == NULL )
  5065. {
  5066. ShowError("script:set: no player attached for player variable '%s'\n", name);
  5067. return 0;
  5068. }
  5069. }
  5070. #if 0
  5071. if( data_isreference(datavalue) )
  5072. {// the value being referenced is a variable
  5073. const char* namevalue = reference_getname(datavalue);
  5074. if( !not_array_variable(*namevalue) )
  5075. {// array variable being copied into another array variable
  5076. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5077. {// player must be attached in order to copy a player variable
  5078. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5079. return 0;
  5080. }
  5081. if( is_string_variable(namevalue) != is_string_variable(name) )
  5082. {// non-matching array value types
  5083. ShowWarning("script:set: two array variables do not match in type.\n");
  5084. return 0;
  5085. }
  5086. // push the maximum number of array values to the stack
  5087. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5088. // call the copy array method directly
  5089. return buildin_copyarray(st);
  5090. }
  5091. }
  5092. #endif
  5093. if( is_string_variable(name) )
  5094. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5095. else
  5096. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5097. // return a copy of the variable reference
  5098. script_pushcopy(st,2);
  5099. return SCRIPT_CMD_SUCCESS;
  5100. }
  5101. /////////////////////////////////////////////////////////////////////
  5102. /// Array variables
  5103. ///
  5104. /// Returns the size of the specified array
  5105. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  5106. {
  5107. int32 ret = idx;
  5108. if( isstring )
  5109. {
  5110. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5111. {
  5112. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  5113. if( str && *str )
  5114. ret = idx + 1;
  5115. script_removetop(st, -1, 0);
  5116. }
  5117. }
  5118. else
  5119. {
  5120. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5121. {
  5122. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  5123. if( num )
  5124. ret = idx + 1;
  5125. script_removetop(st, -1, 0);
  5126. }
  5127. }
  5128. return ret;
  5129. }
  5130. /// Sets values of an array, from the starting index.
  5131. /// ex: setarray arr[1],1,2,3;
  5132. ///
  5133. /// setarray <array variable>,<value1>{,<value2>...};
  5134. BUILDIN_FUNC(setarray)
  5135. {
  5136. struct script_data* data;
  5137. const char* name;
  5138. int32 start;
  5139. int32 end;
  5140. int32 id;
  5141. int32 i;
  5142. TBL_PC* sd = NULL;
  5143. data = script_getdata(st, 2);
  5144. if( !data_isreference(data) )
  5145. {
  5146. ShowError("script:setarray: not a variable\n");
  5147. script_reportdata(data);
  5148. st->state = END;
  5149. return 1;// not a variable
  5150. }
  5151. id = reference_getid(data);
  5152. start = reference_getindex(data);
  5153. name = reference_getname(data);
  5154. if( not_array_variable(*name) )
  5155. {
  5156. ShowError("script:setarray: illegal scope\n");
  5157. script_reportdata(data);
  5158. st->state = END;
  5159. return 1;// not supported
  5160. }
  5161. if( not_server_variable(*name) )
  5162. {
  5163. sd = script_rid2sd(st);
  5164. if( sd == NULL )
  5165. return 0;// no player attached
  5166. }
  5167. end = start + script_lastdata(st) - 2;
  5168. if( end > SCRIPT_MAX_ARRAYSIZE )
  5169. end = SCRIPT_MAX_ARRAYSIZE;
  5170. if( is_string_variable(name) )
  5171. {// string array
  5172. for( i = 3; start < end; ++start, ++i )
  5173. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5174. }
  5175. else
  5176. {// int array
  5177. for( i = 3; start < end; ++start, ++i )
  5178. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5179. }
  5180. return SCRIPT_CMD_SUCCESS;
  5181. }
  5182. /// Sets count values of an array, from the starting index.
  5183. /// ex: cleararray arr[0],0,1;
  5184. ///
  5185. /// cleararray <array variable>,<value>,<count>;
  5186. BUILDIN_FUNC(cleararray)
  5187. {
  5188. struct script_data* data;
  5189. const char* name;
  5190. int32 start;
  5191. int32 end;
  5192. int32 id;
  5193. void* v;
  5194. TBL_PC* sd = NULL;
  5195. data = script_getdata(st, 2);
  5196. if( !data_isreference(data) )
  5197. {
  5198. ShowError("script:cleararray: not a variable\n");
  5199. script_reportdata(data);
  5200. st->state = END;
  5201. return 1;// not a variable
  5202. }
  5203. id = reference_getid(data);
  5204. start = reference_getindex(data);
  5205. name = reference_getname(data);
  5206. if( not_array_variable(*name) )
  5207. {
  5208. ShowError("script:cleararray: illegal scope\n");
  5209. script_reportdata(data);
  5210. st->state = END;
  5211. return 1;// not supported
  5212. }
  5213. if( not_server_variable(*name) )
  5214. {
  5215. sd = script_rid2sd(st);
  5216. if( sd == NULL )
  5217. return 0;// no player attached
  5218. }
  5219. if( is_string_variable(name) )
  5220. v = (void*)script_getstr(st, 3);
  5221. else
  5222. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5223. end = start + script_getnum(st, 4);
  5224. if( end > SCRIPT_MAX_ARRAYSIZE )
  5225. end = SCRIPT_MAX_ARRAYSIZE;
  5226. for( ; start < end; ++start )
  5227. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5228. return SCRIPT_CMD_SUCCESS;
  5229. }
  5230. /// Copies data from one array to another.
  5231. /// ex: copyarray arr[0],arr[2],2;
  5232. ///
  5233. /// copyarray <destination array variable>,<source array variable>,<count>;
  5234. BUILDIN_FUNC(copyarray)
  5235. {
  5236. struct script_data* data1;
  5237. struct script_data* data2;
  5238. const char* name1;
  5239. const char* name2;
  5240. int32 idx1;
  5241. int32 idx2;
  5242. int32 id1;
  5243. int32 id2;
  5244. void* v;
  5245. int32 i;
  5246. int32 count;
  5247. TBL_PC* sd = NULL;
  5248. data1 = script_getdata(st, 2);
  5249. data2 = script_getdata(st, 3);
  5250. if( !data_isreference(data1) || !data_isreference(data2) )
  5251. {
  5252. ShowError("script:copyarray: not a variable\n");
  5253. script_reportdata(data1);
  5254. script_reportdata(data2);
  5255. st->state = END;
  5256. return 1;// not a variable
  5257. }
  5258. id1 = reference_getid(data1);
  5259. id2 = reference_getid(data2);
  5260. idx1 = reference_getindex(data1);
  5261. idx2 = reference_getindex(data2);
  5262. name1 = reference_getname(data1);
  5263. name2 = reference_getname(data2);
  5264. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5265. {
  5266. ShowError("script:copyarray: illegal scope\n");
  5267. script_reportdata(data1);
  5268. script_reportdata(data2);
  5269. st->state = END;
  5270. return 1;// not supported
  5271. }
  5272. if( is_string_variable(name1) != is_string_variable(name2) )
  5273. {
  5274. ShowError("script:copyarray: type mismatch\n");
  5275. script_reportdata(data1);
  5276. script_reportdata(data2);
  5277. st->state = END;
  5278. return 1;// data type mismatch
  5279. }
  5280. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5281. {
  5282. sd = script_rid2sd(st);
  5283. if( sd == NULL )
  5284. return 0;// no player attached
  5285. }
  5286. count = script_getnum(st, 4);
  5287. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5288. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5289. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5290. return 0;// nothing to copy
  5291. if( id1 == id2 && idx1 > idx2 )
  5292. {// destination might be overlapping the source - copy in reverse order
  5293. for( i = count - 1; i >= 0; --i )
  5294. {
  5295. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5296. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5297. script_removetop(st, -1, 0);
  5298. }
  5299. }
  5300. else
  5301. {// normal copy
  5302. for( i = 0; i < count; ++i )
  5303. {
  5304. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5305. {
  5306. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5307. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5308. script_removetop(st, -1, 0);
  5309. }
  5310. else// out of range - assume ""/0
  5311. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5312. }
  5313. }
  5314. return SCRIPT_CMD_SUCCESS;
  5315. }
  5316. /// Returns the size of the array.
  5317. /// Assumes that everything before the starting index exists.
  5318. /// ex: getarraysize(arr[3])
  5319. ///
  5320. /// getarraysize(<array variable>) -> <int>
  5321. BUILDIN_FUNC(getarraysize)
  5322. {
  5323. struct script_data* data;
  5324. const char* name;
  5325. data = script_getdata(st, 2);
  5326. if( !data_isreference(data) )
  5327. {
  5328. ShowError("script:getarraysize: not a variable\n");
  5329. script_reportdata(data);
  5330. script_pushnil(st);
  5331. st->state = END;
  5332. return 1;// not a variable
  5333. }
  5334. name = reference_getname(data);
  5335. if( not_array_variable(*name) )
  5336. {
  5337. ShowError("script:getarraysize: illegal scope\n");
  5338. script_reportdata(data);
  5339. script_pushnil(st);
  5340. st->state = END;
  5341. return 1;// not supported
  5342. }
  5343. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5344. return SCRIPT_CMD_SUCCESS;
  5345. }
  5346. /// Deletes count or all the elements in an array, from the starting index.
  5347. /// ex: deletearray arr[4],2;
  5348. ///
  5349. /// deletearray <array variable>;
  5350. /// deletearray <array variable>,<count>;
  5351. BUILDIN_FUNC(deletearray)
  5352. {
  5353. struct script_data* data;
  5354. const char* name;
  5355. int start;
  5356. int end;
  5357. int id;
  5358. TBL_PC *sd = NULL;
  5359. data = script_getdata(st, 2);
  5360. if( !data_isreference(data) )
  5361. {
  5362. ShowError("script:deletearray: not a variable\n");
  5363. script_reportdata(data);
  5364. st->state = END;
  5365. return 1;// not a variable
  5366. }
  5367. id = reference_getid(data);
  5368. start = reference_getindex(data);
  5369. name = reference_getname(data);
  5370. if( not_array_variable(*name) )
  5371. {
  5372. ShowError("script:deletearray: illegal scope\n");
  5373. script_reportdata(data);
  5374. st->state = END;
  5375. return 1;// not supported
  5376. }
  5377. if( not_server_variable(*name) )
  5378. {
  5379. sd = script_rid2sd(st);
  5380. if( sd == NULL )
  5381. return 0;// no player attached
  5382. }
  5383. end = SCRIPT_MAX_ARRAYSIZE;
  5384. if( start >= end )
  5385. return 0;// nothing to free
  5386. if( script_hasdata(st,3) )
  5387. {
  5388. int count = script_getnum(st, 3);
  5389. if( count > end - start )
  5390. count = end - start;
  5391. if( count <= 0 )
  5392. return 0;// nothing to free
  5393. // move rest of the elements backward
  5394. for( ; start + count < end; ++start )
  5395. {
  5396. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5397. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5398. script_removetop(st, -1, 0);
  5399. }
  5400. }
  5401. // clear the rest of the array
  5402. if( is_string_variable(name) )
  5403. {
  5404. for( ; start < end; ++start )
  5405. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5406. }
  5407. else
  5408. {
  5409. for( ; start < end; ++start )
  5410. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5411. }
  5412. return SCRIPT_CMD_SUCCESS;
  5413. }
  5414. /// Returns a reference to the target index of the array variable.
  5415. /// Equivalent to var[index].
  5416. ///
  5417. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5418. BUILDIN_FUNC(getelementofarray)
  5419. {
  5420. struct script_data* data;
  5421. const char* name;
  5422. int32 id;
  5423. int i;
  5424. data = script_getdata(st, 2);
  5425. if( !data_isreference(data) )
  5426. {
  5427. ShowError("script:getelementofarray: not a variable\n");
  5428. script_reportdata(data);
  5429. script_pushnil(st);
  5430. st->state = END;
  5431. return 1;// not a variable
  5432. }
  5433. id = reference_getid(data);
  5434. name = reference_getname(data);
  5435. if( not_array_variable(*name) )
  5436. {
  5437. ShowError("script:getelementofarray: illegal scope\n");
  5438. script_reportdata(data);
  5439. script_pushnil(st);
  5440. st->state = END;
  5441. return 1;// not supported
  5442. }
  5443. i = script_getnum(st, 3);
  5444. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  5445. {
  5446. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5447. script_reportdata(data);
  5448. script_pushnil(st);
  5449. st->state = END;
  5450. return 1;// out of range
  5451. }
  5452. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5453. return SCRIPT_CMD_SUCCESS;
  5454. }
  5455. /////////////////////////////////////////////////////////////////////
  5456. /// ...
  5457. ///
  5458. /*==========================================
  5459. *
  5460. *------------------------------------------*/
  5461. BUILDIN_FUNC(setlook)
  5462. {
  5463. int type,val;
  5464. TBL_PC* sd;
  5465. type=script_getnum(st,2);
  5466. val=script_getnum(st,3);
  5467. sd = script_rid2sd(st);
  5468. if( sd == NULL )
  5469. return 0;
  5470. pc_changelook(sd,type,val);
  5471. return SCRIPT_CMD_SUCCESS;
  5472. }
  5473. BUILDIN_FUNC(changelook)
  5474. { // As setlook but only client side
  5475. int type,val;
  5476. TBL_PC* sd;
  5477. type=script_getnum(st,2);
  5478. val=script_getnum(st,3);
  5479. sd = script_rid2sd(st);
  5480. if( sd == NULL )
  5481. return 0;
  5482. clif_changelook(&sd->bl,type,val);
  5483. return SCRIPT_CMD_SUCCESS;
  5484. }
  5485. /*==========================================
  5486. *
  5487. *------------------------------------------*/
  5488. BUILDIN_FUNC(cutin)
  5489. {
  5490. TBL_PC* sd;
  5491. sd = script_rid2sd(st);
  5492. if( sd == NULL )
  5493. return 0;
  5494. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5495. return SCRIPT_CMD_SUCCESS;
  5496. }
  5497. /*==========================================
  5498. *
  5499. *------------------------------------------*/
  5500. BUILDIN_FUNC(viewpoint)
  5501. {
  5502. int type,x,y,id,color;
  5503. TBL_PC* sd;
  5504. type=script_getnum(st,2);
  5505. x=script_getnum(st,3);
  5506. y=script_getnum(st,4);
  5507. id=script_getnum(st,5);
  5508. color=script_getnum(st,6);
  5509. sd = script_rid2sd(st);
  5510. if( sd == NULL )
  5511. return 0;
  5512. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5513. return SCRIPT_CMD_SUCCESS;
  5514. }
  5515. /// Returns number of items in inventory/cart/storage
  5516. /// countitem <nameID>{,<accountID>});
  5517. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5518. /// cartcountitem <nameID>{,<accountID>});
  5519. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5520. /// storagecountitem <nameID>{,<accountID>});
  5521. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5522. BUILDIN_FUNC(countitem)
  5523. {
  5524. int i = 0, count = 0, aid = 3;
  5525. struct item_data* id = NULL;
  5526. struct script_data* data;
  5527. char *command = (char *)script_getfuncname(st);
  5528. uint8 loc = 0;
  5529. uint16 size;
  5530. struct item *items;
  5531. TBL_PC *sd = NULL;
  5532. if( command[strlen(command)-1] == '2' ) {
  5533. i = 1;
  5534. aid = 10;
  5535. }
  5536. if( script_hasdata(st,aid) ) {
  5537. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5538. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5539. st->state = END;
  5540. return SCRIPT_CMD_FAILURE;
  5541. }
  5542. }
  5543. else {
  5544. if( !(sd = script_rid2sd(st)) )
  5545. return SCRIPT_CMD_SUCCESS;
  5546. }
  5547. if( !strncmp(command, "cart", 4) ) {
  5548. loc = 1;
  5549. size = MAX_CART;
  5550. items = sd->status.cart;
  5551. }
  5552. else if( !strncmp(command, "storage", 7) ) {
  5553. loc = 2;
  5554. size = MAX_STORAGE;
  5555. items = sd->status.storage.items;
  5556. }
  5557. //TODO: 3 - Guild Storage
  5558. else {
  5559. size = MAX_INVENTORY;
  5560. items = sd->status.inventory;
  5561. }
  5562. if( loc == 1 && !pc_iscarton(sd) ) {
  5563. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5564. script_pushint(st,-1);
  5565. return SCRIPT_CMD_SUCCESS;
  5566. }
  5567. data = script_getdata(st, 2);
  5568. get_val(st, data); // Convert into value in case of a variable
  5569. if( data_isstring(data) ) // item name
  5570. id = itemdb_searchname(conv_str(st, data));
  5571. else // item id
  5572. id = itemdb_exists(conv_num(st, data));
  5573. if( id == NULL ) {
  5574. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5575. script_pushint(st,0);
  5576. return 1;
  5577. }
  5578. if( !i ) { // For count/cart/storagecountitem function
  5579. unsigned short nameid = id->nameid;
  5580. for( i = 0; i < size; i++ )
  5581. if( &items[i] && items[i].nameid == nameid )
  5582. count += items[i].amount;
  5583. }
  5584. else { // For count/cart/storagecountitem2 function
  5585. unsigned short nameid;
  5586. int iden, ref, attr, c1, c2, c3, c4;
  5587. nameid = id->nameid;
  5588. iden = script_getnum(st,3);
  5589. ref = script_getnum(st,4);
  5590. attr = script_getnum(st,5);
  5591. c1 = script_getnum(st,6);
  5592. c2 = script_getnum(st,7);
  5593. c3 = script_getnum(st,8);
  5594. c4 = script_getnum(st,9);
  5595. for( i = 0; i < size; i++ )
  5596. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5597. items[i].amount > 0 && items[i].identify == iden &&
  5598. items[i].refine == ref && items[i].attribute == attr &&
  5599. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5600. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5601. {
  5602. count += items[i].amount;
  5603. }
  5604. }
  5605. script_pushint(st, count);
  5606. return SCRIPT_CMD_SUCCESS;
  5607. }
  5608. int checkweight_sub(TBL_PC *sd, int nbargs, unsigned short *eitemid, int32 *eamount)
  5609. {
  5610. struct item_data* id = NULL;
  5611. unsigned short nameid;
  5612. int amount;
  5613. uint32 weight=0;
  5614. uint16 amount2=0,slots,i;
  5615. slots = pc_inventoryblank(sd); //nb of empty slot
  5616. for(i=0; i<nbargs; i++) {
  5617. if(!eitemid[i])
  5618. continue;
  5619. id = itemdb_exists(eitemid[i]);
  5620. if( id == NULL ) {
  5621. ShowError("checkweight_sub: Invalid item '%hu'.\n", eitemid[i]);
  5622. return 0;
  5623. }
  5624. nameid = id->nameid;
  5625. amount = eamount[i];
  5626. if( amount < 1 ) {
  5627. ShowError("checkweight_sub: Invalid amount '%d'.\n", eamount[i]);
  5628. return 0;
  5629. }
  5630. weight += (id->weight)*amount; //total weight for all chk
  5631. if( weight + sd->weight > sd->max_weight ) // too heavy
  5632. return 0;
  5633. switch( pc_checkadditem(sd, nameid, amount) ) {
  5634. case CHKADDITEM_EXIST: // item is already in inventory, but there is still space for the requested amount
  5635. break;
  5636. case CHKADDITEM_NEW:
  5637. if( itemdb_isstackable(nameid) )
  5638. amount2++; // stackable
  5639. else
  5640. amount2 += amount; // non-stackable
  5641. if( slots < amount2)
  5642. return 0;
  5643. break;
  5644. case CHKADDITEM_OVERAMOUNT:
  5645. return 0;
  5646. }
  5647. }
  5648. return 1;
  5649. }
  5650. /*==========================================
  5651. * Check if item with this amount can fit in inventory
  5652. * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY)
  5653. * Return
  5654. * 0 : fail
  5655. * 1 : success (npc side only)
  5656. *------------------------------------------*/
  5657. BUILDIN_FUNC(checkweight)
  5658. {
  5659. struct map_session_data* sd;
  5660. struct item_data* id = NULL;
  5661. unsigned short nameid[SCRIPT_MAX_ARRAYSIZE];
  5662. int32 amount[SCRIPT_MAX_ARRAYSIZE];
  5663. uint16 nbargs,i,j=0;
  5664. if( ( sd = script_rid2sd(st) ) == NULL )
  5665. return 0;
  5666. nbargs = script_lastdata(st)+1;
  5667. if(nbargs%2) {
  5668. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5669. script_pushint(st,0);
  5670. return 1;
  5671. }
  5672. for(i=2; i<nbargs; i=i+2) {
  5673. struct script_data* data = script_getdata(st,i);
  5674. get_val(st, data); // Convert into value in case of a variable
  5675. if( data_isstring(data) ) // item name
  5676. id = itemdb_searchname(conv_str(st, data));
  5677. else // item id
  5678. id = itemdb_exists(conv_num(st, data));
  5679. if( id == NULL ) {
  5680. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5681. script_pushint(st,0);
  5682. return 1;
  5683. }
  5684. nameid[j] = id->nameid;
  5685. amount[j] = script_getnum(st,i+1);
  5686. j++;
  5687. }
  5688. script_pushint(st,checkweight_sub(sd,(nbargs-2)/2,nameid,amount));
  5689. return SCRIPT_CMD_SUCCESS;
  5690. }
  5691. BUILDIN_FUNC(checkweight2){
  5692. //variable sub checkweight
  5693. unsigned short nameid[SCRIPT_MAX_ARRAYSIZE];
  5694. int32 amount[SCRIPT_MAX_ARRAYSIZE], i;
  5695. //variable for array parsing
  5696. struct script_data* data_it;
  5697. struct script_data* data_nb;
  5698. const char* name_it;
  5699. const char* name_nb;
  5700. int32 id_it, id_nb;
  5701. int32 idx_it, idx_nb;
  5702. int nb_it, nb_nb; //array size
  5703. TBL_PC *sd = script_rid2sd(st);
  5704. nullpo_retr(1,sd);
  5705. data_it = script_getdata(st, 2);
  5706. data_nb = script_getdata(st, 3);
  5707. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5708. ShowError("buildin_checkweight2: parameter not a variable\n");
  5709. script_pushint(st,0);
  5710. return 1;// not a variable
  5711. }
  5712. id_it = reference_getid(data_it);
  5713. id_nb = reference_getid(data_nb);
  5714. idx_it = reference_getindex(data_it);
  5715. idx_nb = reference_getindex(data_nb);
  5716. name_it = reference_getname(data_it);
  5717. name_nb = reference_getname(data_nb);
  5718. if( not_array_variable(*name_it) || not_array_variable(*name_nb)) {
  5719. ShowError("buildin_checkweight2: illegal scope\n");
  5720. script_pushint(st,0);
  5721. return 1;// not supported
  5722. }
  5723. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5724. ShowError("buildin_checkweight2: illegal type, need int\n");
  5725. script_pushint(st,0);
  5726. return 1;// not supported
  5727. }
  5728. nb_it = getarraysize(st, id_it, idx_it, 0, reference_getref(data_it));
  5729. nb_nb = getarraysize(st, id_nb, idx_nb, 0, reference_getref(data_nb));
  5730. if(nb_it != nb_nb) {
  5731. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5732. script_pushint(st,0);
  5733. return 1;
  5734. }
  5735. for(i=0; i<nb_it; i++) {
  5736. nameid[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5737. script_removetop(st, -1, 0);
  5738. amount[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5739. script_removetop(st, -1, 0);
  5740. } //end loop DO NOT break it prematurly we need to depop all stack
  5741. script_pushint(st,checkweight_sub(sd,nb_it,nameid,amount)); //push result of sub to script
  5742. return SCRIPT_CMD_SUCCESS;
  5743. }
  5744. /*==========================================
  5745. * getitem <item id>,<amount>{,<account ID>};
  5746. * getitem "<item name>",<amount>{,<account ID>};
  5747. *
  5748. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5749. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5750. * Type:
  5751. * 0 - No bound
  5752. * 1 - Account Bound
  5753. * 2 - Guild Bound
  5754. * 3 - Party Bound
  5755. * 4 - Character Bound
  5756. *------------------------------------------*/
  5757. BUILDIN_FUNC(getitem)
  5758. {
  5759. int amount, get_count, i;
  5760. unsigned short nameid;
  5761. struct item it;
  5762. TBL_PC *sd;
  5763. struct script_data *data;
  5764. unsigned char flag = 0;
  5765. const char* command = script_getfuncname(st);
  5766. data=script_getdata(st,2);
  5767. get_val(st,data);
  5768. if( data_isstring(data) ) {// "<item name>"
  5769. const char *name = conv_str(st,data);
  5770. struct item_data *item_data = itemdb_searchname(name);
  5771. if( item_data == NULL ){
  5772. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5773. return SCRIPT_CMD_FAILURE; //No item created.
  5774. }
  5775. nameid = item_data->nameid;
  5776. } else if( data_isint(data) ) {// <item id>
  5777. nameid = conv_num(st,data);
  5778. if( !itemdb_exists(nameid) ){
  5779. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5780. return SCRIPT_CMD_FAILURE; //No item created.
  5781. }
  5782. } else {
  5783. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5784. return SCRIPT_CMD_FAILURE;
  5785. }
  5786. // <amount>
  5787. if( (amount = script_getnum(st,3)) <= 0)
  5788. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  5789. memset(&it,0,sizeof(it));
  5790. it.nameid = nameid;
  5791. it.identify = 1;
  5792. it.bound = BOUND_NONE;
  5793. if( !strcmp(command,"getitembound") ) {
  5794. char bound = script_getnum(st,4);
  5795. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  5796. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5797. return SCRIPT_CMD_FAILURE;
  5798. }
  5799. if( script_hasdata(st,5) )
  5800. sd = map_id2sd(script_getnum(st,5));
  5801. else
  5802. sd = script_rid2sd(st); // Attached player
  5803. it.bound = bound;
  5804. } else if( script_hasdata(st,4) )
  5805. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  5806. else
  5807. sd = script_rid2sd(st); // Attached player
  5808. if( sd == NULL ) // no target
  5809. return SCRIPT_CMD_SUCCESS;
  5810. //Check if it's stackable.
  5811. if (!itemdb_isstackable(nameid))
  5812. get_count = 1;
  5813. else
  5814. get_count = amount;
  5815. for (i = 0; i < amount; i += get_count)
  5816. {
  5817. // if not pet egg
  5818. if (!pet_create_egg(sd, nameid))
  5819. {
  5820. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5821. {
  5822. clif_additem(sd, 0, 0, flag);
  5823. if( pc_candrop(sd,&it) )
  5824. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5825. }
  5826. }
  5827. }
  5828. return SCRIPT_CMD_SUCCESS;
  5829. }
  5830. /*==========================================
  5831. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  5832. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  5833. *
  5834. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  5835. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  5836. * Type:
  5837. * 0 - No bound
  5838. * 1 - Account Bound
  5839. * 2 - Guild Bound
  5840. * 3 - Party Bound
  5841. * 4 - Character Bound
  5842. *------------------------------------------*/
  5843. BUILDIN_FUNC(getitem2)
  5844. {
  5845. int amount;
  5846. unsigned short nameid;
  5847. int iden, ref, attr;
  5848. unsigned short c1, c2, c3, c4;
  5849. char bound = BOUND_NONE;
  5850. struct item_data *item_data = NULL;
  5851. struct item item_tmp;
  5852. TBL_PC *sd;
  5853. struct script_data *data;
  5854. const char* command = script_getfuncname(st);
  5855. if( !strcmp(command,"getitembound2") ) {
  5856. bound = script_getnum(st,11);
  5857. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  5858. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  5859. return SCRIPT_CMD_FAILURE;
  5860. }
  5861. if( script_hasdata(st,12) )
  5862. sd = map_id2sd(script_getnum(st,12));
  5863. else
  5864. sd = script_rid2sd(st); // Attached player
  5865. } else if( script_hasdata(st,11) )
  5866. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  5867. else
  5868. sd = script_rid2sd(st); // Attached player
  5869. if( sd == NULL ) // no target
  5870. return SCRIPT_CMD_SUCCESS;
  5871. data = script_getdata(st,2);
  5872. get_val(st,data);
  5873. if( data_isstring(data) ) {
  5874. const char *name = conv_str(st,data);
  5875. if( (item_data = itemdb_searchname(name)) == NULL ){
  5876. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  5877. return SCRIPT_CMD_FAILURE; //No item created.
  5878. }
  5879. nameid = item_data->nameid;
  5880. } else {
  5881. nameid = conv_num(st,data);
  5882. if( (item_data = itemdb_exists(nameid)) == NULL ){
  5883. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  5884. return SCRIPT_CMD_FAILURE; //No item created.
  5885. }
  5886. }
  5887. amount = script_getnum(st,3);
  5888. iden = script_getnum(st,4);
  5889. ref = script_getnum(st,5);
  5890. attr = script_getnum(st,6);
  5891. c1 = (unsigned short)script_getnum(st,7);
  5892. c2 = (unsigned short)script_getnum(st,8);
  5893. c3 = (unsigned short)script_getnum(st,9);
  5894. c4 = (unsigned short)script_getnum(st,10);
  5895. if( item_data ) {
  5896. int get_count = 0, i;
  5897. memset(&item_tmp,0,sizeof(item_tmp));
  5898. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  5899. if(ref > MAX_REFINE)
  5900. ref = MAX_REFINE;
  5901. }
  5902. else if( item_data->type == IT_PETEGG ) {
  5903. iden = 1;
  5904. ref = 0;
  5905. }
  5906. else {
  5907. iden = 1;
  5908. ref = attr = 0;
  5909. }
  5910. item_tmp.nameid = nameid;
  5911. item_tmp.identify = iden;
  5912. item_tmp.refine = ref;
  5913. item_tmp.attribute = attr;
  5914. item_tmp.card[0] = c1;
  5915. item_tmp.card[1] = c2;
  5916. item_tmp.card[2] = c3;
  5917. item_tmp.card[3] = c4;
  5918. item_tmp.bound = bound;
  5919. //Check if it's stackable.
  5920. if (!itemdb_isstackable(nameid))
  5921. get_count = 1;
  5922. else
  5923. get_count = amount;
  5924. for (i = 0; i < amount; i += get_count)
  5925. {
  5926. // if not pet egg
  5927. if (!pet_create_egg(sd, nameid))
  5928. {
  5929. unsigned char flag = 0;
  5930. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5931. {
  5932. clif_additem(sd, 0, 0, flag);
  5933. if( pc_candrop(sd,&item_tmp) )
  5934. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5935. }
  5936. }
  5937. }
  5938. }
  5939. return SCRIPT_CMD_SUCCESS;
  5940. }
  5941. /** Gives rental item to player
  5942. * rentitem <item id>,<seconds>
  5943. * rentitem "<item name>",<seconds>
  5944. */
  5945. BUILDIN_FUNC(rentitem) {
  5946. struct map_session_data *sd;
  5947. struct script_data *data;
  5948. struct item it;
  5949. int seconds;
  5950. unsigned short nameid = 0;
  5951. unsigned char flag = 0;
  5952. data = script_getdata(st,2);
  5953. get_val(st,data);
  5954. if( (sd = script_rid2sd(st)) == NULL )
  5955. return SCRIPT_CMD_SUCCESS;
  5956. if( data_isstring(data) )
  5957. {
  5958. const char *name = conv_str(st,data);
  5959. struct item_data *itd = itemdb_searchname(name);
  5960. if( itd == NULL )
  5961. {
  5962. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5963. return SCRIPT_CMD_FAILURE;
  5964. }
  5965. nameid = itd->nameid;
  5966. }
  5967. else if( data_isint(data) )
  5968. {
  5969. nameid = conv_num(st,data);
  5970. if( nameid == 0 || !itemdb_exists(nameid) )
  5971. {
  5972. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  5973. return SCRIPT_CMD_FAILURE;
  5974. }
  5975. }
  5976. else
  5977. {
  5978. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5979. return SCRIPT_CMD_FAILURE;
  5980. }
  5981. seconds = script_getnum(st,3);
  5982. memset(&it, 0, sizeof(it));
  5983. it.nameid = nameid;
  5984. it.identify = 1;
  5985. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5986. it.bound = BOUND_NONE;
  5987. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5988. {
  5989. clif_additem(sd, 0, 0, flag);
  5990. return SCRIPT_CMD_FAILURE;
  5991. }
  5992. return SCRIPT_CMD_SUCCESS;
  5993. }
  5994. /** Gives rental item to player with advanced option
  5995. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  5996. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  5997. */
  5998. BUILDIN_FUNC(rentitem2) {
  5999. struct map_session_data *sd;
  6000. struct script_data *data;
  6001. struct item it;
  6002. struct item_data *id;
  6003. int seconds;
  6004. unsigned short nameid = 0;
  6005. unsigned char flag = 0;
  6006. int iden,ref,attr,c1,c2,c3,c4;
  6007. data = script_getdata(st,2);
  6008. get_val(st,data);
  6009. if( (sd = script_rid2sd(st)) == NULL )
  6010. return SCRIPT_CMD_SUCCESS;
  6011. if( data_isstring(data) ) {
  6012. const char *name = conv_str(st,data);
  6013. id = itemdb_searchname(name);
  6014. if( id == NULL ) {
  6015. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6016. return SCRIPT_CMD_FAILURE;
  6017. }
  6018. nameid = id->nameid;
  6019. }
  6020. else if( data_isint(data) ) {
  6021. nameid = conv_num(st,data);
  6022. if( !(id = itemdb_search(nameid))) {
  6023. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6024. return SCRIPT_CMD_FAILURE;
  6025. }
  6026. }
  6027. else {
  6028. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6029. return SCRIPT_CMD_FAILURE;
  6030. }
  6031. seconds = script_getnum(st,3);
  6032. iden = script_getnum(st,4);
  6033. ref = script_getnum(st,5);
  6034. attr = script_getnum(st,6);
  6035. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6036. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6037. }
  6038. else if (id->type==IT_PETEGG) {
  6039. iden = 1;
  6040. ref = 0;
  6041. }
  6042. else {
  6043. iden = 1;
  6044. ref = attr = 0;
  6045. }
  6046. c1 = (short)script_getnum(st,7);
  6047. c2 = (short)script_getnum(st,8);
  6048. c3 = (short)script_getnum(st,9);
  6049. c4 = (short)script_getnum(st,10);
  6050. memset(&it, 0, sizeof(it));
  6051. it.nameid = nameid;
  6052. it.identify = iden;
  6053. it.refine = ref;
  6054. it.attribute = attr;
  6055. it.card[0] = (short)c1;
  6056. it.card[1] = (short)c2;
  6057. it.card[2] = (short)c3;
  6058. it.card[3] = (short)c4;
  6059. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6060. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6061. clif_additem(sd, 0, 0, flag);
  6062. return SCRIPT_CMD_FAILURE;
  6063. }
  6064. return SCRIPT_CMD_SUCCESS;
  6065. }
  6066. /*==========================================
  6067. * gets an item with someone's name inscribed [Skotlex]
  6068. * getinscribeditem item_num, character_name
  6069. * Returned Qty is always 1, only works on equip-able
  6070. * equipment
  6071. *------------------------------------------*/
  6072. BUILDIN_FUNC(getnameditem)
  6073. {
  6074. unsigned short nameid;
  6075. struct item item_tmp;
  6076. TBL_PC *sd, *tsd;
  6077. struct script_data *data;
  6078. sd = script_rid2sd(st);
  6079. if (sd == NULL)
  6080. { //Player not attached!
  6081. script_pushint(st,0);
  6082. return 0;
  6083. }
  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 == NULL)
  6090. { //Failed
  6091. script_pushint(st,0);
  6092. return 0;
  6093. }
  6094. nameid = item_data->nameid;
  6095. }else
  6096. nameid = conv_num(st,data);
  6097. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6098. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6099. script_pushint(st,0);
  6100. return 0;
  6101. }
  6102. data=script_getdata(st,3);
  6103. get_val(st,data);
  6104. if( data_isstring(data) ) //Char Name
  6105. tsd=map_nick2sd(conv_str(st,data));
  6106. else //Char Id was given
  6107. tsd=map_charid2sd(conv_num(st,data));
  6108. if( tsd == NULL )
  6109. { //Failed
  6110. script_pushint(st,0);
  6111. return 0;
  6112. }
  6113. memset(&item_tmp,0,sizeof(item_tmp));
  6114. item_tmp.nameid=nameid;
  6115. item_tmp.amount=1;
  6116. item_tmp.identify=1;
  6117. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6118. item_tmp.card[2]=tsd->status.char_id;
  6119. item_tmp.card[3]=tsd->status.char_id >> 16;
  6120. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6121. script_pushint(st,0);
  6122. return 0; //Failed to add item, we will not drop if they don't fit
  6123. }
  6124. script_pushint(st,1);
  6125. return SCRIPT_CMD_SUCCESS;
  6126. }
  6127. /*==========================================
  6128. * gets a random item ID from an item group [Skotlex]
  6129. * groupranditem <group_num>{,<sub_group>};
  6130. *------------------------------------------*/
  6131. BUILDIN_FUNC(grouprandomitem)
  6132. {
  6133. script_pushint(st,itemdb_searchrandomid(script_getnum(st,2),script_getnum(st,3)));
  6134. return SCRIPT_CMD_SUCCESS;
  6135. }
  6136. /**
  6137. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6138. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6139. */
  6140. BUILDIN_FUNC(makeitem) {
  6141. int16 nameid;
  6142. uint16 amount, flag = 0, x, y;
  6143. const char *mapname;
  6144. int m;
  6145. struct item item_tmp;
  6146. struct script_data *data;
  6147. data = script_getdata(st,2);
  6148. get_val(st,data);
  6149. if( data_isstring(data) ){
  6150. const char *name = conv_str(st,data);
  6151. struct item_data *item_data = itemdb_searchname(name);
  6152. if( item_data )
  6153. nameid = item_data->nameid;
  6154. else
  6155. nameid = UNKNOWN_ITEM_ID;
  6156. }
  6157. else
  6158. nameid = conv_num(st,data);
  6159. amount = script_getnum(st,3);
  6160. mapname = script_getstr(st,4);
  6161. x = script_getnum(st,5);
  6162. y = script_getnum(st,6);
  6163. if(strcmp(mapname,"this")==0) {
  6164. TBL_PC *sd;
  6165. sd = script_rid2sd(st);
  6166. if (!sd)
  6167. return SCRIPT_CMD_SUCCESS; //Failed...
  6168. m = sd->bl.m;
  6169. } else
  6170. m = map_mapname2mapid(mapname);
  6171. if(nameid<0) {
  6172. nameid = -nameid;
  6173. flag = 1;
  6174. }
  6175. if(nameid > 0) {
  6176. memset(&item_tmp,0,sizeof(item_tmp));
  6177. item_tmp.nameid = nameid;
  6178. if(!flag)
  6179. item_tmp.identify = 1;
  6180. else
  6181. item_tmp.identify = itemdb_isidentified(nameid);
  6182. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  6183. }
  6184. return SCRIPT_CMD_SUCCESS;
  6185. }
  6186. /**
  6187. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6188. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6189. */
  6190. BUILDIN_FUNC(makeitem2) {
  6191. uint16 nameid, amount, x, y;
  6192. const char *mapname;
  6193. int m;
  6194. struct item item_tmp;
  6195. struct script_data *data;
  6196. struct item_data *id;
  6197. data = script_getdata(st,2);
  6198. get_val(st,data);
  6199. if( data_isstring(data) ){
  6200. const char *name = conv_str(st,data);
  6201. struct item_data *item_data = itemdb_searchname(name);
  6202. if( item_data )
  6203. nameid = item_data->nameid;
  6204. else
  6205. nameid = UNKNOWN_ITEM_ID;
  6206. }
  6207. else
  6208. nameid = conv_num(st,data);
  6209. amount = script_getnum(st,3);
  6210. mapname = script_getstr(st,4);
  6211. x = script_getnum(st,5);
  6212. y = script_getnum(st,6);
  6213. if (strcmp(mapname,"this")==0) {
  6214. TBL_PC *sd;
  6215. sd = script_rid2sd(st);
  6216. if (!sd)
  6217. return SCRIPT_CMD_SUCCESS; //Failed...
  6218. m = sd->bl.m;
  6219. }
  6220. else
  6221. m = map_mapname2mapid(mapname);
  6222. if ((id = itemdb_search(nameid))) {
  6223. char iden, ref, attr;
  6224. memset(&item_tmp,0,sizeof(item_tmp));
  6225. item_tmp.nameid = nameid;
  6226. iden = (char)script_getnum(st,7);
  6227. ref = (char)script_getnum(st,8);
  6228. attr = (char)script_getnum(st,9);
  6229. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6230. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6231. }
  6232. else if (id->type==IT_PETEGG) {
  6233. iden = 1;
  6234. ref = 0;
  6235. }
  6236. else {
  6237. iden = 1;
  6238. ref = attr = 0;
  6239. }
  6240. item_tmp.identify = iden;
  6241. item_tmp.refine = ref;
  6242. item_tmp.attribute = attr;
  6243. item_tmp.card[0] = script_getnum(st,10);
  6244. item_tmp.card[1] = script_getnum(st,11);
  6245. item_tmp.card[2] = script_getnum(st,12);
  6246. item_tmp.card[3] = script_getnum(st,13);
  6247. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  6248. }
  6249. else
  6250. return SCRIPT_CMD_FAILURE;
  6251. return SCRIPT_CMD_SUCCESS;
  6252. }
  6253. /// Counts / deletes the current item given by idx.
  6254. /// Used by buildin_delitem_search
  6255. /// Relies on all input data being already fully valid.
  6256. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6257. {
  6258. int delamount;
  6259. struct item *itm = NULL;
  6260. switch(loc) {
  6261. case 1: // cart
  6262. itm = &sd->status.cart[idx];
  6263. break;
  6264. case 2: // storage
  6265. itm = &sd->status.storage.items[idx];
  6266. break;
  6267. default: //inventory
  6268. itm = &sd->status.inventory[idx];
  6269. break;
  6270. }
  6271. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6272. if( delete_items )
  6273. {
  6274. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6275. {// delete associated pet
  6276. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6277. }
  6278. switch(loc) {
  6279. case 1:
  6280. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6281. break;
  6282. case 2:
  6283. storage_delitem(sd,idx,delamount);
  6284. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6285. break;
  6286. default:
  6287. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6288. break;
  6289. }
  6290. }
  6291. amount[0]-= delamount;
  6292. }
  6293. /// Searches for item(s) and checks, if there is enough of them.
  6294. /// Used by delitem and delitem2
  6295. /// Relies on all input data being already fully valid.
  6296. /// @param exact_match will also match item attributes and cards, not just name id
  6297. /// @return true when all items could be deleted, false when there were not enough items to delete
  6298. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6299. {
  6300. bool delete_items = false;
  6301. int i, amount, size;
  6302. struct item *items;
  6303. // prefer always non-equipped items
  6304. it->equip = 0;
  6305. // when searching for nameid only, prefer additionally
  6306. if( !exact_match )
  6307. {
  6308. // non-refined items
  6309. it->refine = 0;
  6310. // card-less items
  6311. memset(it->card, 0, sizeof(it->card));
  6312. }
  6313. switch(loc) {
  6314. case 1: // cart
  6315. size = MAX_CART;
  6316. items = sd->status.cart;
  6317. break;
  6318. case 2: // storage
  6319. size = MAX_STORAGE;
  6320. items = sd->status.storage.items;
  6321. break;
  6322. default: //inventory
  6323. size = MAX_INVENTORY;
  6324. items = sd->status.inventory;
  6325. break;
  6326. }
  6327. for(;;)
  6328. {
  6329. unsigned short important = 0;
  6330. amount = it->amount;
  6331. // 1st pass -- less important items / exact match
  6332. for( i = 0; amount && i < size; i++ )
  6333. {
  6334. struct item *itm = NULL;
  6335. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6336. {// wrong/invalid item
  6337. continue;
  6338. }
  6339. if( itm->equip != it->equip || itm->refine != it->refine )
  6340. {// not matching attributes
  6341. important++;
  6342. continue;
  6343. }
  6344. if( exact_match )
  6345. {
  6346. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6347. {// not matching exact attributes
  6348. continue;
  6349. }
  6350. }
  6351. else
  6352. {
  6353. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6354. {
  6355. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6356. {// pet which cannot be deleted
  6357. continue;
  6358. }
  6359. }
  6360. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6361. {// named/carded item
  6362. important++;
  6363. continue;
  6364. }
  6365. }
  6366. // count / delete item
  6367. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6368. }
  6369. // 2nd pass -- any matching item
  6370. if( amount == 0 || important == 0 )
  6371. {// either everything was already consumed or no items were skipped
  6372. ;
  6373. }
  6374. else for( i = 0; amount && i < size; i++ )
  6375. {
  6376. struct item *itm = NULL;
  6377. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6378. {// wrong/invalid item
  6379. continue;
  6380. }
  6381. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6382. {// pet which cannot be deleted
  6383. continue;
  6384. }
  6385. if( exact_match )
  6386. {
  6387. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6388. {// not matching attributes
  6389. continue;
  6390. }
  6391. }
  6392. // count / delete item
  6393. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6394. }
  6395. if( amount )
  6396. {// not enough items
  6397. return false;
  6398. }
  6399. else if( delete_items )
  6400. {// we are done with the work
  6401. return true;
  6402. }
  6403. else
  6404. {// get rid of the items now
  6405. delete_items = true;
  6406. }
  6407. }
  6408. }
  6409. /// Deletes items from the target/attached player.
  6410. /// Prioritizes ordinary items.
  6411. ///
  6412. /// delitem <item id>,<amount>{,<account id>}
  6413. /// delitem "<item name>",<amount>{,<account id>}
  6414. /// cartdelitem <item id>,<amount>{,<account id>}
  6415. /// cartdelitem "<item name>",<amount>{,<account id>}
  6416. /// storagedelitem <item id>,<amount>{,<account id>}
  6417. /// storagedelitem "<item name>",<amount>{,<account id>}
  6418. BUILDIN_FUNC(delitem)
  6419. {
  6420. TBL_PC *sd;
  6421. struct item it;
  6422. struct script_data *data;
  6423. uint8 loc = 0;
  6424. char* command = (char*)script_getfuncname(st);
  6425. if(!strncmp(command, "cart", 4))
  6426. loc = 1;
  6427. else if(!strncmp(command, "storage", 7))
  6428. loc = 2;
  6429. //TODO: 3 - Guild Storage
  6430. if( script_hasdata(st,4) )
  6431. {
  6432. int account_id = script_getnum(st,4);
  6433. sd = map_id2sd(account_id); // <account id>
  6434. if( sd == NULL )
  6435. {
  6436. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6437. st->state = END;
  6438. return 1;
  6439. }
  6440. }
  6441. else
  6442. {
  6443. sd = script_rid2sd(st);// attached player
  6444. if( sd == NULL )
  6445. return 0;
  6446. }
  6447. if (loc == 1 && !pc_iscarton(sd)) {
  6448. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6449. return SCRIPT_CMD_FAILURE;
  6450. }
  6451. data = script_getdata(st,2);
  6452. get_val(st,data);
  6453. if( data_isstring(data) )
  6454. {
  6455. const char* item_name = conv_str(st,data);
  6456. struct item_data* id = itemdb_searchname(item_name);
  6457. if( id == NULL )
  6458. {
  6459. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6460. st->state = END;
  6461. return 1;
  6462. }
  6463. it.nameid = id->nameid;// "<item name>"
  6464. }
  6465. else
  6466. {
  6467. it.nameid = conv_num(st,data);// <item id>
  6468. if( !itemdb_exists( it.nameid ) )
  6469. {
  6470. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6471. st->state = END;
  6472. return 1;
  6473. }
  6474. }
  6475. it.amount=script_getnum(st,3);
  6476. if( it.amount <= 0 )
  6477. return 0;// nothing to do
  6478. if( buildin_delitem_search(sd, &it, false, loc) )
  6479. {// success
  6480. return SCRIPT_CMD_SUCCESS;
  6481. }
  6482. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6483. st->state = END;
  6484. st->mes_active = 0;
  6485. clif_scriptclose(sd, st->oid);
  6486. return SCRIPT_CMD_FAILURE;
  6487. }
  6488. /// Deletes items from the target/attached player.
  6489. ///
  6490. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6491. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6492. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6493. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6494. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6495. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6496. BUILDIN_FUNC(delitem2)
  6497. {
  6498. TBL_PC *sd;
  6499. struct item it;
  6500. struct script_data *data;
  6501. uint8 loc = 0;
  6502. char* command = (char*)script_getfuncname(st);
  6503. if(!strncmp(command, "cart", 4))
  6504. loc = 1;
  6505. else if(!strncmp(command, "storage", 7))
  6506. loc = 2;
  6507. //TODO: 3 - Guild Storage
  6508. if( script_hasdata(st,11) )
  6509. {
  6510. int account_id = script_getnum(st,11);
  6511. sd = map_id2sd(account_id); // <account id>
  6512. if( sd == NULL )
  6513. {
  6514. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6515. st->state = END;
  6516. return 1;
  6517. }
  6518. }
  6519. else
  6520. {
  6521. sd = script_rid2sd(st);// attached player
  6522. if( sd == NULL )
  6523. return 0;
  6524. }
  6525. if (loc == 1 && !pc_iscarton(sd)) {
  6526. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6527. script_pushint(st,-1);
  6528. return SCRIPT_CMD_FAILURE;
  6529. }
  6530. data = script_getdata(st,2);
  6531. get_val(st,data);
  6532. if( data_isstring(data) )
  6533. {
  6534. const char* item_name = conv_str(st,data);
  6535. struct item_data* id = itemdb_searchname(item_name);
  6536. if( id == NULL )
  6537. {
  6538. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6539. st->state = END;
  6540. return 1;
  6541. }
  6542. it.nameid = id->nameid;// "<item name>"
  6543. }
  6544. else
  6545. {
  6546. it.nameid = conv_num(st,data);// <item id>
  6547. if( !itemdb_exists( it.nameid ) )
  6548. {
  6549. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6550. st->state = END;
  6551. return 1;
  6552. }
  6553. }
  6554. it.amount=script_getnum(st,3);
  6555. it.identify=script_getnum(st,4);
  6556. it.refine=script_getnum(st,5);
  6557. it.attribute=script_getnum(st,6);
  6558. it.card[0]=(short)script_getnum(st,7);
  6559. it.card[1]=(short)script_getnum(st,8);
  6560. it.card[2]=(short)script_getnum(st,9);
  6561. it.card[3]=(short)script_getnum(st,10);
  6562. if( it.amount <= 0 )
  6563. return 0;// nothing to do
  6564. if( buildin_delitem_search(sd, &it, true, loc) )
  6565. {// success
  6566. return SCRIPT_CMD_SUCCESS;
  6567. }
  6568. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6569. st->state = END;
  6570. st->mes_active = 0;
  6571. clif_scriptclose(sd, st->oid);
  6572. return SCRIPT_CMD_FAILURE;
  6573. }
  6574. /*==========================================
  6575. * Enables/Disables use of items while in an NPC [Skotlex]
  6576. *------------------------------------------*/
  6577. BUILDIN_FUNC(enableitemuse)
  6578. {
  6579. TBL_PC *sd;
  6580. sd=script_rid2sd(st);
  6581. if (sd)
  6582. st->npc_item_flag = sd->npc_item_flag = 1;
  6583. return SCRIPT_CMD_SUCCESS;
  6584. }
  6585. BUILDIN_FUNC(disableitemuse)
  6586. {
  6587. TBL_PC *sd;
  6588. sd=script_rid2sd(st);
  6589. if (sd)
  6590. st->npc_item_flag = sd->npc_item_flag = 0;
  6591. return SCRIPT_CMD_SUCCESS;
  6592. }
  6593. /*==========================================
  6594. * Returns a character's specified stat.
  6595. * Check pc_readparam for available options.
  6596. *------------------------------------------*/
  6597. BUILDIN_FUNC(readparam)
  6598. {
  6599. int type;
  6600. TBL_PC *sd;
  6601. type=script_getnum(st,2);
  6602. if( script_hasdata(st,3) )
  6603. sd=map_nick2sd(script_getstr(st,3));
  6604. else
  6605. sd=script_rid2sd(st);
  6606. if(sd==NULL){
  6607. script_pushint(st,-1);
  6608. return 0;
  6609. }
  6610. script_pushint(st,pc_readparam(sd,type));
  6611. return SCRIPT_CMD_SUCCESS;
  6612. }
  6613. /*==========================================
  6614. * Return charid identification
  6615. * return by @num :
  6616. * 0 : char_id
  6617. * 1 : party_id
  6618. * 2 : guild_id
  6619. * 3 : account_id
  6620. * 4 : bg_id
  6621. *------------------------------------------*/
  6622. BUILDIN_FUNC(getcharid)
  6623. {
  6624. int num;
  6625. TBL_PC *sd;
  6626. num = script_getnum(st,2);
  6627. if( script_hasdata(st,3) )
  6628. sd=map_nick2sd(script_getstr(st,3));
  6629. else
  6630. sd=script_rid2sd(st);
  6631. if(sd==NULL){
  6632. script_pushint(st,0); //return 0, according docs
  6633. return 0;
  6634. }
  6635. switch( num ) {
  6636. case 0: script_pushint(st,sd->status.char_id); break;
  6637. case 1: script_pushint(st,sd->status.party_id); break;
  6638. case 2: script_pushint(st,sd->status.guild_id); break;
  6639. case 3: script_pushint(st,sd->status.account_id); break;
  6640. case 4: script_pushint(st,sd->bg_id); break;
  6641. default:
  6642. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6643. script_pushint(st,0);
  6644. break;
  6645. }
  6646. return SCRIPT_CMD_SUCCESS;
  6647. }
  6648. /*==========================================
  6649. * returns the GID of an NPC
  6650. *------------------------------------------*/
  6651. BUILDIN_FUNC(getnpcid)
  6652. {
  6653. int num = script_getnum(st,2);
  6654. struct npc_data* nd = NULL;
  6655. if( script_hasdata(st,3) )
  6656. {// unique npc name
  6657. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6658. {
  6659. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6660. script_pushint(st,0);
  6661. return 1;
  6662. }
  6663. }
  6664. switch (num) {
  6665. case 0:
  6666. script_pushint(st,nd ? nd->bl.id : st->oid);
  6667. break;
  6668. default:
  6669. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6670. script_pushint(st,0);
  6671. return 1;
  6672. }
  6673. return SCRIPT_CMD_SUCCESS;
  6674. }
  6675. /*==========================================
  6676. * Return the name of the party_id
  6677. * null if not found
  6678. *------------------------------------------*/
  6679. BUILDIN_FUNC(getpartyname)
  6680. {
  6681. int party_id;
  6682. struct party_data* p;
  6683. party_id = script_getnum(st,2);
  6684. if( ( p = party_search(party_id) ) != NULL )
  6685. {
  6686. script_pushstrcopy(st,p->party.name);
  6687. }
  6688. else
  6689. {
  6690. script_pushconststr(st,"null");
  6691. }
  6692. return SCRIPT_CMD_SUCCESS;
  6693. }
  6694. /*==========================================
  6695. * Get the information of the members of a party by type
  6696. * @party_id, @type
  6697. * return by @type :
  6698. * - : nom des membres
  6699. * 1 : char_id des membres
  6700. * 2 : account_id des membres
  6701. *------------------------------------------*/
  6702. BUILDIN_FUNC(getpartymember)
  6703. {
  6704. struct party_data *p;
  6705. unsigned char j = 0;
  6706. p = party_search(script_getnum(st,2));
  6707. if (p != NULL) {
  6708. int type = 0;
  6709. unsigned char i;
  6710. if (script_hasdata(st,3))
  6711. type = script_getnum(st,3);
  6712. for (i = 0; i < MAX_PARTY; i++) {
  6713. if (p->party.member[i].account_id) {
  6714. switch (type) {
  6715. case 2:
  6716. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6717. break;
  6718. case 1:
  6719. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6720. break;
  6721. default:
  6722. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6723. break;
  6724. }
  6725. j++;
  6726. }
  6727. }
  6728. }
  6729. mapreg_setreg(add_str("$@partymembercount"),j);
  6730. return SCRIPT_CMD_SUCCESS;
  6731. }
  6732. /*==========================================
  6733. * Retrieves party leader. if flag is specified,
  6734. * return some of the leader data. Otherwise, return name.
  6735. *------------------------------------------*/
  6736. BUILDIN_FUNC(getpartyleader)
  6737. {
  6738. int party_id, type = 0, i=0;
  6739. struct party_data *p;
  6740. party_id=script_getnum(st,2);
  6741. if( script_hasdata(st,3) )
  6742. type=script_getnum(st,3);
  6743. p=party_search(party_id);
  6744. if (p) //Search leader
  6745. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6746. if (!p || i == MAX_PARTY) { //leader not found
  6747. if (type)
  6748. script_pushint(st,-1);
  6749. else
  6750. script_pushconststr(st,"null");
  6751. return 0;
  6752. }
  6753. switch (type) {
  6754. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6755. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6756. case 3: script_pushint(st,p->party.member[i].class_); break;
  6757. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6758. case 5: script_pushint(st,p->party.member[i].lv); break;
  6759. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6760. }
  6761. return SCRIPT_CMD_SUCCESS;
  6762. }
  6763. /*==========================================
  6764. * Return the name of the @guild_id
  6765. * null if not found
  6766. *------------------------------------------*/
  6767. BUILDIN_FUNC(getguildname)
  6768. {
  6769. int guild_id;
  6770. struct guild* g;
  6771. guild_id = script_getnum(st,2);
  6772. if( ( g = guild_search(guild_id) ) != NULL )
  6773. script_pushstrcopy(st,g->name);
  6774. else
  6775. script_pushconststr(st,"null");
  6776. return SCRIPT_CMD_SUCCESS;
  6777. }
  6778. /*==========================================
  6779. * Return the name of the guild master of @guild_id
  6780. * null if not found
  6781. *------------------------------------------*/
  6782. BUILDIN_FUNC(getguildmaster)
  6783. {
  6784. int guild_id;
  6785. struct guild* g;
  6786. guild_id = script_getnum(st,2);
  6787. if( ( g = guild_search(guild_id) ) != NULL )
  6788. script_pushstrcopy(st,g->member[0].name);
  6789. else
  6790. script_pushconststr(st,"null");
  6791. return SCRIPT_CMD_SUCCESS;
  6792. }
  6793. BUILDIN_FUNC(getguildmasterid)
  6794. {
  6795. int guild_id;
  6796. struct guild* g;
  6797. guild_id = script_getnum(st,2);
  6798. if( ( g = guild_search(guild_id) ) != NULL )
  6799. script_pushint(st,g->member[0].char_id);
  6800. else
  6801. script_pushint(st,0);
  6802. return SCRIPT_CMD_SUCCESS;
  6803. }
  6804. /*==========================================
  6805. * Get char string information by type :
  6806. * Return by @type :
  6807. * 0 : char_name
  6808. * 1 : party_name or ""
  6809. * 2 : guild_name or ""
  6810. * 3 : map_name
  6811. * - : ""
  6812. *------------------------------------------*/
  6813. BUILDIN_FUNC(strcharinfo)
  6814. {
  6815. TBL_PC *sd;
  6816. int num;
  6817. struct guild* g;
  6818. struct party_data* p;
  6819. sd=script_rid2sd(st);
  6820. if (!sd) { //Avoid crashing....
  6821. script_pushconststr(st,"");
  6822. return 0;
  6823. }
  6824. num=script_getnum(st,2);
  6825. switch(num){
  6826. case 0:
  6827. script_pushstrcopy(st,sd->status.name);
  6828. break;
  6829. case 1:
  6830. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  6831. script_pushstrcopy(st,p->party.name);
  6832. } else {
  6833. script_pushconststr(st,"");
  6834. }
  6835. break;
  6836. case 2:
  6837. if( ( g = sd->guild ) != NULL ) {
  6838. script_pushstrcopy(st,g->name);
  6839. } else {
  6840. script_pushconststr(st,"");
  6841. }
  6842. break;
  6843. case 3:
  6844. script_pushconststr(st,map[sd->bl.m].name);
  6845. break;
  6846. default:
  6847. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6848. script_pushconststr(st,"");
  6849. break;
  6850. }
  6851. return SCRIPT_CMD_SUCCESS;
  6852. }
  6853. /*==========================================
  6854. * Get npc string information by type
  6855. * return by @type:
  6856. * 0 : name
  6857. * 1 : str#
  6858. * 2 : #str
  6859. * 3 : ::str
  6860. * 4 : map name
  6861. *------------------------------------------*/
  6862. BUILDIN_FUNC(strnpcinfo)
  6863. {
  6864. TBL_NPC* nd;
  6865. int num;
  6866. char *buf,*name=NULL;
  6867. nd = map_id2nd(st->oid);
  6868. if (!nd) {
  6869. script_pushconststr(st, "");
  6870. return 0;
  6871. }
  6872. num = script_getnum(st,2);
  6873. switch(num){
  6874. case 0: // display name
  6875. name = aStrdup(nd->name);
  6876. break;
  6877. case 1: // visible part of display name
  6878. if((buf = strchr(nd->name,'#')) != NULL)
  6879. {
  6880. name = aStrdup(nd->name);
  6881. name[buf - nd->name] = 0;
  6882. } else // Return the name, there is no '#' present
  6883. name = aStrdup(nd->name);
  6884. break;
  6885. case 2: // # fragment
  6886. if((buf = strchr(nd->name,'#')) != NULL)
  6887. name = aStrdup(buf+1);
  6888. break;
  6889. case 3: // unique name
  6890. name = aStrdup(nd->exname);
  6891. break;
  6892. case 4: // map name
  6893. name = aStrdup(map[nd->bl.m].name);
  6894. break;
  6895. }
  6896. if(name)
  6897. script_pushstr(st, name);
  6898. else
  6899. script_pushconststr(st, "");
  6900. return SCRIPT_CMD_SUCCESS;
  6901. }
  6902. /// aegis->athena slot position conversion table
  6903. /// Index is used by EQI_ in db/const.txt
  6904. static unsigned int equip[] = {
  6905. EQP_HEAD_TOP,
  6906. EQP_ARMOR,
  6907. EQP_HAND_L,
  6908. EQP_HAND_R,
  6909. EQP_GARMENT,
  6910. EQP_SHOES,
  6911. EQP_ACC_L,
  6912. EQP_ACC_R,
  6913. EQP_HEAD_MID,
  6914. EQP_HEAD_LOW,
  6915. EQP_COSTUME_HEAD_LOW,
  6916. EQP_COSTUME_HEAD_MID,
  6917. EQP_COSTUME_HEAD_TOP,
  6918. EQP_COSTUME_GARMENT,
  6919. EQP_AMMO,
  6920. EQP_SHADOW_ARMOR,
  6921. EQP_SHADOW_WEAPON,
  6922. EQP_SHADOW_SHIELD,
  6923. EQP_SHADOW_SHOES,
  6924. EQP_SHADOW_ACC_R,
  6925. EQP_SHADOW_ACC_L
  6926. };
  6927. /*==========================================
  6928. * GetEquipID(Pos); Pos: 1-14
  6929. *------------------------------------------*/
  6930. BUILDIN_FUNC(getequipid)
  6931. {
  6932. int i, num;
  6933. TBL_PC* sd;
  6934. struct item_data* item;
  6935. sd = script_rid2sd(st);
  6936. if( sd == NULL )
  6937. return 0;
  6938. num = script_getnum(st,2) - 1;
  6939. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6940. {
  6941. script_pushint(st,-1);
  6942. return 0;
  6943. }
  6944. // get inventory position of item
  6945. i = pc_checkequip(sd,equip[num]);
  6946. if( i < 0 )
  6947. {
  6948. script_pushint(st,-1);
  6949. return 0;
  6950. }
  6951. item = sd->inventory_data[i];
  6952. if( item != 0 )
  6953. script_pushint(st,item->nameid);
  6954. else
  6955. script_pushint(st,0);
  6956. return SCRIPT_CMD_SUCCESS;
  6957. }
  6958. /*==========================================
  6959. * GetEquipUniqueID(Pos); Pos: 1-14
  6960. *------------------------------------------*/
  6961. BUILDIN_FUNC(getequipuniqueid)
  6962. {
  6963. int i, num;
  6964. TBL_PC* sd;
  6965. struct item* item;
  6966. sd = script_rid2sd(st);
  6967. if (sd == NULL)
  6968. return 0;
  6969. num = script_getnum(st,2) - 1;
  6970. if (num < 0 || num >= ARRAYLENGTH(equip)) {
  6971. script_pushconststr(st, "");
  6972. return 0;
  6973. }
  6974. // get inventory position of item
  6975. i = pc_checkequip(sd,equip[num]);
  6976. if (i < 0) {
  6977. script_pushconststr(st, "");
  6978. return 0;
  6979. }
  6980. item = &sd->status.inventory[i];
  6981. if (item != 0) {
  6982. char buf[256];
  6983. memset(buf, 0, sizeof(buf));
  6984. snprintf(buf, sizeof(buf)-1, "%llu", (unsigned long long)item->unique_id);
  6985. script_pushstr(st, buf);
  6986. } else
  6987. script_pushconststr(st, "");
  6988. return SCRIPT_CMD_SUCCESS;
  6989. }
  6990. /*==========================================
  6991. * Get the equipement name at pos
  6992. * return item jname or ""
  6993. *------------------------------------------*/
  6994. BUILDIN_FUNC(getequipname)
  6995. {
  6996. int i, num;
  6997. TBL_PC* sd;
  6998. struct item_data* item;
  6999. sd = script_rid2sd(st);
  7000. if( sd == NULL )
  7001. return 0;
  7002. num = script_getnum(st,2) - 1;
  7003. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7004. {
  7005. script_pushconststr(st,"");
  7006. return 0;
  7007. }
  7008. // get inventory position of item
  7009. i = pc_checkequip(sd,equip[num]);
  7010. if( i < 0 )
  7011. {
  7012. script_pushconststr(st,"");
  7013. return 0;
  7014. }
  7015. item = sd->inventory_data[i];
  7016. if( item != 0 )
  7017. script_pushstrcopy(st,item->jname);
  7018. else
  7019. script_pushconststr(st,"");
  7020. return SCRIPT_CMD_SUCCESS;
  7021. }
  7022. /*==========================================
  7023. * getbrokenid [Valaris]
  7024. *------------------------------------------*/
  7025. BUILDIN_FUNC(getbrokenid)
  7026. {
  7027. int i,num,id=0,brokencounter=0;
  7028. TBL_PC *sd;
  7029. sd = script_rid2sd(st);
  7030. if( sd == NULL )
  7031. return 0;
  7032. num=script_getnum(st,2);
  7033. for(i=0; i<MAX_INVENTORY; i++) {
  7034. if(sd->status.inventory[i].attribute){
  7035. brokencounter++;
  7036. if(num==brokencounter){
  7037. id=sd->status.inventory[i].nameid;
  7038. break;
  7039. }
  7040. }
  7041. }
  7042. script_pushint(st,id);
  7043. return SCRIPT_CMD_SUCCESS;
  7044. }
  7045. /*==========================================
  7046. * repair [Valaris]
  7047. *------------------------------------------*/
  7048. BUILDIN_FUNC(repair)
  7049. {
  7050. int i,num;
  7051. int repaircounter=0;
  7052. TBL_PC *sd;
  7053. sd = script_rid2sd(st);
  7054. if( sd == NULL )
  7055. return 0;
  7056. num=script_getnum(st,2);
  7057. for(i=0; i<MAX_INVENTORY; i++) {
  7058. if(sd->status.inventory[i].attribute){
  7059. repaircounter++;
  7060. if(num==repaircounter){
  7061. sd->status.inventory[i].attribute=0;
  7062. clif_equiplist(sd);
  7063. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  7064. clif_misceffect(&sd->bl, 3);
  7065. break;
  7066. }
  7067. }
  7068. }
  7069. return SCRIPT_CMD_SUCCESS;
  7070. }
  7071. /*==========================================
  7072. * repairall
  7073. *------------------------------------------*/
  7074. BUILDIN_FUNC(repairall)
  7075. {
  7076. int i, repaircounter = 0;
  7077. TBL_PC *sd;
  7078. sd = script_rid2sd(st);
  7079. if(sd == NULL)
  7080. return 0;
  7081. for(i = 0; i < MAX_INVENTORY; i++)
  7082. {
  7083. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  7084. {
  7085. sd->status.inventory[i].attribute = 0;
  7086. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  7087. repaircounter++;
  7088. }
  7089. }
  7090. if(repaircounter)
  7091. {
  7092. clif_misceffect(&sd->bl, 3);
  7093. clif_equiplist(sd);
  7094. }
  7095. return SCRIPT_CMD_SUCCESS;
  7096. }
  7097. /*==========================================
  7098. * Chk if player have something equiped at pos
  7099. *------------------------------------------*/
  7100. BUILDIN_FUNC(getequipisequiped)
  7101. {
  7102. int i = -1,num;
  7103. TBL_PC *sd;
  7104. num = script_getnum(st,2);
  7105. sd = script_rid2sd(st);
  7106. if( sd == NULL )
  7107. return 0;
  7108. if (num > 0 && num <= ARRAYLENGTH(equip))
  7109. i=pc_checkequip(sd,equip[num-1]);
  7110. if(i >= 0)
  7111. script_pushint(st,1);
  7112. else
  7113. script_pushint(st,0);
  7114. return SCRIPT_CMD_SUCCESS;
  7115. }
  7116. /*==========================================
  7117. * Chk if the player have something equiped at pos
  7118. * if so chk if this item ain't marked not refinable or rental
  7119. * return (npc)
  7120. * 1 : true
  7121. * 0 : false
  7122. *------------------------------------------*/
  7123. BUILDIN_FUNC(getequipisenableref)
  7124. {
  7125. int i = -1,num;
  7126. TBL_PC *sd;
  7127. num = script_getnum(st,2);
  7128. sd = script_rid2sd(st);
  7129. if( sd == NULL )
  7130. return 0;
  7131. if( num > 0 && num <= ARRAYLENGTH(equip) )
  7132. i = pc_checkequip(sd,equip[num-1]);
  7133. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  7134. script_pushint(st,1);
  7135. else
  7136. script_pushint(st,0);
  7137. return SCRIPT_CMD_SUCCESS;
  7138. }
  7139. /*==========================================
  7140. * Get the item refined value at pos
  7141. * return (npc)
  7142. * x : refine amount
  7143. * 0 : false (not refined)
  7144. *------------------------------------------*/
  7145. BUILDIN_FUNC(getequiprefinerycnt)
  7146. {
  7147. int i = -1,num;
  7148. TBL_PC *sd;
  7149. num = script_getnum(st,2);
  7150. sd = script_rid2sd(st);
  7151. if( sd == NULL )
  7152. return 0;
  7153. if (num > 0 && num <= ARRAYLENGTH(equip))
  7154. i=pc_checkequip(sd,equip[num-1]);
  7155. if(i >= 0)
  7156. script_pushint(st,sd->status.inventory[i].refine);
  7157. else
  7158. script_pushint(st,0);
  7159. return SCRIPT_CMD_SUCCESS;
  7160. }
  7161. /*==========================================
  7162. * Get the weapon level value at pos
  7163. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7164. * return (npc)
  7165. * x : weapon level
  7166. * 0 : false
  7167. *------------------------------------------*/
  7168. BUILDIN_FUNC(getequipweaponlv)
  7169. {
  7170. int i = -1,num;
  7171. TBL_PC *sd;
  7172. num = script_getnum(st,2);
  7173. sd = script_rid2sd(st);
  7174. if( sd == NULL )
  7175. return 0;
  7176. if (num > 0 && num <= ARRAYLENGTH(equip))
  7177. i=pc_checkequip(sd,equip[num-1]);
  7178. if(i >= 0 && sd->inventory_data[i])
  7179. script_pushint(st,sd->inventory_data[i]->wlv);
  7180. else
  7181. script_pushint(st,0);
  7182. return SCRIPT_CMD_SUCCESS;
  7183. }
  7184. /*==========================================
  7185. * Get the item refine chance (from refine.txt) for item at pos
  7186. * return (npc)
  7187. * x : refine chance
  7188. * 0 : false (max refine level or unequip..)
  7189. *------------------------------------------*/
  7190. BUILDIN_FUNC(getequippercentrefinery)
  7191. {
  7192. int i = -1,num;
  7193. TBL_PC *sd;
  7194. num = script_getnum(st,2);
  7195. sd = script_rid2sd(st);
  7196. if( sd == NULL )
  7197. return 0;
  7198. if (num > 0 && num <= ARRAYLENGTH(equip))
  7199. i = pc_checkequip(sd,equip[num-1]);
  7200. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  7201. script_pushint(st,status_get_refine_chance((enum refine_type)itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  7202. else
  7203. script_pushint(st,0);
  7204. return SCRIPT_CMD_SUCCESS;
  7205. }
  7206. /*==========================================
  7207. * Refine +1 item at pos and log and display refine
  7208. *------------------------------------------*/
  7209. BUILDIN_FUNC(successrefitem) {
  7210. short i = -1, up = 1;
  7211. int pos;
  7212. TBL_PC *sd;
  7213. pos = script_getnum(st,2);
  7214. sd = script_rid2sd(st);
  7215. if (sd == NULL)
  7216. return 0;
  7217. if (script_hasdata(st, 3))
  7218. up = script_getnum(st, 3);
  7219. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7220. i = pc_checkequip(sd,equip[pos-1]);
  7221. if (i >= 0) {
  7222. unsigned int ep = sd->status.inventory[i].equip;
  7223. //Logs items, got from (N)PC scripts [Lupus]
  7224. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7225. if (sd->status.inventory[i].refine >= MAX_REFINE) {
  7226. script_pushint(st, MAX_REFINE);
  7227. return SCRIPT_CMD_SUCCESS;
  7228. }
  7229. sd->status.inventory[i].refine += up;
  7230. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7231. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7232. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  7233. clif_delitem(sd,i,1,3);
  7234. //Logs items, got from (N)PC scripts [Lupus]
  7235. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7236. clif_additem(sd,i,1,0);
  7237. pc_equipitem(sd,i,ep);
  7238. clif_misceffect(&sd->bl,3);
  7239. if (sd->status.inventory[i].refine == MAX_REFINE &&
  7240. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  7241. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3]))
  7242. { // Fame point system [DracoRPG]
  7243. switch (sd->inventory_data[i]->wlv){
  7244. case 1:
  7245. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7246. break;
  7247. case 2:
  7248. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7249. break;
  7250. case 3:
  7251. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7252. break;
  7253. }
  7254. }
  7255. script_pushint(st, sd->status.inventory[i].refine);
  7256. return SCRIPT_CMD_SUCCESS;
  7257. }
  7258. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7259. script_pushint(st, -1);
  7260. return SCRIPT_CMD_FAILURE;
  7261. }
  7262. /*==========================================
  7263. * Show a failed Refine +1 attempt
  7264. *------------------------------------------*/
  7265. BUILDIN_FUNC(failedrefitem) {
  7266. short i = -1;
  7267. int pos;
  7268. TBL_PC *sd;
  7269. pos = script_getnum(st,2);
  7270. sd = script_rid2sd(st);
  7271. if (sd == NULL)
  7272. return 0;
  7273. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7274. i = pc_checkequip(sd,equip[pos-1]);
  7275. if (i >= 0) {
  7276. sd->status.inventory[i].refine = 0;
  7277. pc_unequipitem(sd,i,3); //recalculate bonus
  7278. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  7279. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7280. clif_misceffect(&sd->bl,2); // display failure effect
  7281. script_pushint(st, 1);
  7282. return SCRIPT_CMD_SUCCESS;
  7283. }
  7284. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7285. script_pushint(st, 0);
  7286. return SCRIPT_CMD_FAILURE;
  7287. }
  7288. /*==========================================
  7289. * Downgrades an Equipment Part by -1 . [Masao]
  7290. *------------------------------------------*/
  7291. BUILDIN_FUNC(downrefitem) {
  7292. short i = -1, down = 1;
  7293. int pos;
  7294. TBL_PC *sd;
  7295. sd = script_rid2sd(st);
  7296. if( sd == NULL )
  7297. return 0;
  7298. pos = script_getnum(st,2);
  7299. if (script_hasdata(st, 3))
  7300. down = script_getnum(st, 3);
  7301. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7302. i = pc_checkequip(sd,equip[pos-1]);
  7303. if (i >= 0) {
  7304. unsigned int ep = sd->status.inventory[i].equip;
  7305. //Logs items, got from (N)PC scripts [Lupus]
  7306. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7307. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7308. sd->status.inventory[i].refine -= down;
  7309. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7310. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine);
  7311. clif_delitem(sd,i,1,3);
  7312. //Logs items, got from (N)PC scripts [Lupus]
  7313. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7314. clif_additem(sd,i,1,0);
  7315. pc_equipitem(sd,i,ep);
  7316. clif_misceffect(&sd->bl,2);
  7317. script_pushint(st, sd->status.inventory[i].refine);
  7318. return SCRIPT_CMD_SUCCESS;
  7319. }
  7320. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7321. script_pushint(st, -1);
  7322. return SCRIPT_CMD_FAILURE;
  7323. }
  7324. /*==========================================
  7325. * Delete the item equipped at pos.
  7326. *------------------------------------------*/
  7327. BUILDIN_FUNC(delequip) {
  7328. short i = -1;
  7329. int pos;
  7330. int8 ret;
  7331. TBL_PC *sd;
  7332. pos = script_getnum(st,2);
  7333. sd = script_rid2sd(st);
  7334. if (sd == NULL)
  7335. return 0;
  7336. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7337. i = pc_checkequip(sd,equip[pos-1]);
  7338. if (i >= 0) {
  7339. pc_unequipitem(sd,i,3); //recalculate bonus
  7340. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7341. }
  7342. else {
  7343. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7344. st->state = END;
  7345. return SCRIPT_CMD_FAILURE;
  7346. }
  7347. script_pushint(st,ret);
  7348. return SCRIPT_CMD_SUCCESS;
  7349. }
  7350. /*==========================================
  7351. * Break the item equipped at pos.
  7352. *------------------------------------------*/
  7353. BUILDIN_FUNC(breakequip) {
  7354. short i = -1;
  7355. int pos;
  7356. TBL_PC *sd;
  7357. pos = script_getnum(st,2);
  7358. sd = script_rid2sd(st);
  7359. if (sd == NULL)
  7360. return 0;
  7361. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7362. i = pc_checkequip(sd,equip[pos-1]);
  7363. if (i >= 0) {
  7364. sd->status.inventory[i].attribute = 1;
  7365. pc_unequipitem(sd,i,3);
  7366. clif_equiplist(sd);
  7367. script_pushint(st,1);
  7368. return SCRIPT_CMD_SUCCESS;
  7369. }
  7370. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7371. script_pushint(st,0);
  7372. return SCRIPT_CMD_FAILURE;
  7373. }
  7374. /*==========================================
  7375. *
  7376. *------------------------------------------*/
  7377. BUILDIN_FUNC(statusup)
  7378. {
  7379. int type;
  7380. TBL_PC *sd;
  7381. type=script_getnum(st,2);
  7382. sd = script_rid2sd(st);
  7383. if( sd == NULL )
  7384. return 0;
  7385. pc_statusup(sd, type, 1);
  7386. return SCRIPT_CMD_SUCCESS;
  7387. }
  7388. /*==========================================
  7389. *
  7390. *------------------------------------------*/
  7391. BUILDIN_FUNC(statusup2)
  7392. {
  7393. int type,val;
  7394. TBL_PC *sd;
  7395. type=script_getnum(st,2);
  7396. val=script_getnum(st,3);
  7397. sd = script_rid2sd(st);
  7398. if( sd == NULL )
  7399. return 0;
  7400. pc_statusup2(sd,type,val);
  7401. return SCRIPT_CMD_SUCCESS;
  7402. }
  7403. /// See 'doc/item_bonus.txt'
  7404. ///
  7405. /// bonus <bonus type>,<val1>;
  7406. /// bonus2 <bonus type>,<val1>,<val2>;
  7407. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7408. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7409. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7410. BUILDIN_FUNC(bonus)
  7411. {
  7412. int type;
  7413. int val1;
  7414. int val2 = 0;
  7415. int val3 = 0;
  7416. int val4 = 0;
  7417. int val5 = 0;
  7418. TBL_PC* sd;
  7419. struct script_data *data;
  7420. sd = script_rid2sd(st);
  7421. if( sd == NULL )
  7422. return 0; // no player attached
  7423. type = script_getnum(st,2);
  7424. switch( type ) {
  7425. case SP_AUTOSPELL:
  7426. case SP_AUTOSPELL_WHENHIT:
  7427. case SP_AUTOSPELL_ONSKILL:
  7428. case SP_SKILL_ATK:
  7429. case SP_SKILL_HEAL:
  7430. case SP_SKILL_HEAL2:
  7431. case SP_ADD_SKILL_BLOW:
  7432. case SP_CASTRATE:
  7433. case SP_ADDEFF_ONSKILL:
  7434. case SP_SKILL_USE_SP_RATE:
  7435. case SP_SKILL_COOLDOWN:
  7436. case SP_SKILL_FIXEDCAST:
  7437. case SP_SKILL_VARIABLECAST:
  7438. case SP_VARCASTRATE:
  7439. case SP_FIXCASTRATE:
  7440. case SP_SKILL_USE_SP:
  7441. // these bonuses support skill names
  7442. data = script_getdata(st, 3);
  7443. get_val(st, data); // Convert into value in case of a variable
  7444. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7445. break;
  7446. default:
  7447. val1 = script_getnum(st,3);
  7448. break;
  7449. }
  7450. switch( script_lastdata(st)-2 ) {
  7451. case 1:
  7452. pc_bonus(sd, type, val1);
  7453. break;
  7454. case 2:
  7455. val2 = script_getnum(st,4);
  7456. pc_bonus2(sd, type, val1, val2);
  7457. break;
  7458. case 3:
  7459. val2 = script_getnum(st,4);
  7460. val3 = script_getnum(st,5);
  7461. pc_bonus3(sd, type, val1, val2, val3);
  7462. break;
  7463. case 4:
  7464. data = script_getdata(st, 4);
  7465. get_val(st, data); // Convert into value in case of a variable
  7466. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7467. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7468. else
  7469. val2 = script_getnum(st,4);
  7470. val3 = script_getnum(st,5);
  7471. val4 = script_getnum(st,6);
  7472. pc_bonus4(sd, type, val1, val2, val3, val4);
  7473. break;
  7474. case 5:
  7475. data = script_getdata(st, 4);
  7476. get_val(st, data); // Convert into value in case of a variable
  7477. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7478. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7479. else
  7480. val2 = script_getnum(st,4);
  7481. val3 = script_getnum(st,5);
  7482. val4 = script_getnum(st,6);
  7483. val5 = script_getnum(st,7);
  7484. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7485. break;
  7486. default:
  7487. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7488. break;
  7489. }
  7490. return SCRIPT_CMD_SUCCESS;
  7491. }
  7492. BUILDIN_FUNC(autobonus)
  7493. {
  7494. unsigned int dur;
  7495. short rate;
  7496. short atk_type = 0;
  7497. TBL_PC* sd;
  7498. const char *bonus_script, *other_script = NULL;
  7499. sd = script_rid2sd(st);
  7500. if( sd == NULL )
  7501. return 0; // no player attached
  7502. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7503. return 0;
  7504. rate = script_getnum(st,3);
  7505. dur = script_getnum(st,4);
  7506. bonus_script = script_getstr(st,2);
  7507. if( !rate || !dur || !bonus_script )
  7508. return 0;
  7509. if( script_hasdata(st,5) )
  7510. atk_type = script_getnum(st,5);
  7511. if( script_hasdata(st,6) )
  7512. other_script = script_getstr(st,6);
  7513. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7514. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7515. {
  7516. script_add_autobonus(bonus_script);
  7517. if( other_script )
  7518. script_add_autobonus(other_script);
  7519. }
  7520. return SCRIPT_CMD_SUCCESS;
  7521. }
  7522. BUILDIN_FUNC(autobonus2)
  7523. {
  7524. unsigned int dur;
  7525. short rate;
  7526. short atk_type = 0;
  7527. TBL_PC* sd;
  7528. const char *bonus_script, *other_script = NULL;
  7529. sd = script_rid2sd(st);
  7530. if( sd == NULL )
  7531. return 0; // no player attached
  7532. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7533. return 0;
  7534. rate = script_getnum(st,3);
  7535. dur = script_getnum(st,4);
  7536. bonus_script = script_getstr(st,2);
  7537. if( !rate || !dur || !bonus_script )
  7538. return 0;
  7539. if( script_hasdata(st,5) )
  7540. atk_type = script_getnum(st,5);
  7541. if( script_hasdata(st,6) )
  7542. other_script = script_getstr(st,6);
  7543. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7544. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7545. {
  7546. script_add_autobonus(bonus_script);
  7547. if( other_script )
  7548. script_add_autobonus(other_script);
  7549. }
  7550. return SCRIPT_CMD_SUCCESS;
  7551. }
  7552. BUILDIN_FUNC(autobonus3)
  7553. {
  7554. unsigned int dur;
  7555. short rate,atk_type;
  7556. TBL_PC* sd;
  7557. const char *bonus_script, *other_script = NULL;
  7558. struct script_data *data;
  7559. sd = script_rid2sd(st);
  7560. if( sd == NULL )
  7561. return 0; // no player attached
  7562. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7563. return 0;
  7564. rate = script_getnum(st,3);
  7565. dur = script_getnum(st,4);
  7566. data = script_getdata(st, 5);
  7567. get_val(st, data); // Convert into value in case of a variable
  7568. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7569. bonus_script = script_getstr(st,2);
  7570. if( !rate || !dur || !atk_type || !bonus_script )
  7571. return 0;
  7572. if( script_hasdata(st,6) )
  7573. other_script = script_getstr(st,6);
  7574. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7575. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  7576. {
  7577. script_add_autobonus(bonus_script);
  7578. if( other_script )
  7579. script_add_autobonus(other_script);
  7580. }
  7581. return SCRIPT_CMD_SUCCESS;
  7582. }
  7583. /// Changes the level of a player skill.
  7584. /// <flag> defaults to 1
  7585. /// <flag>=0 : set the level of the skill
  7586. /// <flag>=1 : set the temporary level of the skill
  7587. /// <flag>=2 : add to the level of the skill
  7588. ///
  7589. /// skill <skill id>,<level>,<flag>
  7590. /// skill <skill id>,<level>
  7591. /// skill "<skill name>",<level>,<flag>
  7592. /// skill "<skill name>",<level>
  7593. BUILDIN_FUNC(skill)
  7594. {
  7595. int id;
  7596. int level;
  7597. int flag = 1;
  7598. TBL_PC* sd;
  7599. struct script_data *data;
  7600. sd = script_rid2sd(st);
  7601. if( sd == NULL )
  7602. return 0;// no player attached, report source
  7603. data = script_getdata(st, 2);
  7604. get_val(st, data); // Convert into value in case of a variable
  7605. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7606. level = script_getnum(st,3);
  7607. if( script_hasdata(st,4) )
  7608. flag = script_getnum(st,4);
  7609. pc_skill(sd, id, level, flag);
  7610. return SCRIPT_CMD_SUCCESS;
  7611. }
  7612. /// Changes the level of a player skill.
  7613. /// like skill, but <flag> defaults to 2
  7614. ///
  7615. /// addtoskill <skill id>,<amount>,<flag>
  7616. /// addtoskill <skill id>,<amount>
  7617. /// addtoskill "<skill name>",<amount>,<flag>
  7618. /// addtoskill "<skill name>",<amount>
  7619. ///
  7620. /// @see skill
  7621. BUILDIN_FUNC(addtoskill)
  7622. {
  7623. int id;
  7624. int level;
  7625. int flag = 2;
  7626. TBL_PC* sd;
  7627. struct script_data *data;
  7628. sd = script_rid2sd(st);
  7629. if( sd == NULL )
  7630. return 0;// no player attached, report source
  7631. data = script_getdata(st, 2);
  7632. get_val(st, data); // Convert into value in case of a variable
  7633. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7634. level = script_getnum(st,3);
  7635. if( script_hasdata(st,4) )
  7636. flag = script_getnum(st,4);
  7637. pc_skill(sd, id, level, flag);
  7638. return SCRIPT_CMD_SUCCESS;
  7639. }
  7640. /// Increases the level of a guild skill.
  7641. ///
  7642. /// guildskill <skill id>,<amount>;
  7643. /// guildskill "<skill name>",<amount>;
  7644. BUILDIN_FUNC(guildskill)
  7645. {
  7646. int id;
  7647. int level;
  7648. TBL_PC* sd;
  7649. int i;
  7650. struct script_data *data;
  7651. sd = script_rid2sd(st);
  7652. if( sd == NULL )
  7653. return 0;// no player attached, report source
  7654. data = script_getdata(st, 2);
  7655. get_val(st, data); // Convert into value in case of a variable
  7656. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7657. level = script_getnum(st,3);
  7658. for( i=0; i < level; i++ )
  7659. guild_skillup(sd, id);
  7660. return SCRIPT_CMD_SUCCESS;
  7661. }
  7662. /// Returns the level of the player skill.
  7663. ///
  7664. /// getskilllv(<skill id>) -> <level>
  7665. /// getskilllv("<skill name>") -> <level>
  7666. BUILDIN_FUNC(getskilllv)
  7667. {
  7668. int id;
  7669. TBL_PC* sd;
  7670. struct script_data *data;
  7671. sd = script_rid2sd(st);
  7672. if( sd == NULL )
  7673. return 0;// no player attached, report source
  7674. data = script_getdata(st, 2);
  7675. get_val(st, data); // Convert into value in case of a variable
  7676. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7677. script_pushint(st, pc_checkskill(sd,id));
  7678. return SCRIPT_CMD_SUCCESS;
  7679. }
  7680. /// Returns the level of the guild skill.
  7681. ///
  7682. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7683. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7684. BUILDIN_FUNC(getgdskilllv)
  7685. {
  7686. int guild_id;
  7687. uint16 skill_id;
  7688. struct guild* g;
  7689. struct script_data *data;
  7690. guild_id = script_getnum(st,2);
  7691. data = script_getdata(st, 3);
  7692. get_val(st, data); // Convert into value in case of a variable
  7693. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7694. g = guild_search(guild_id);
  7695. if( g == NULL )
  7696. script_pushint(st, -1);
  7697. else
  7698. script_pushint(st, guild_checkskill(g,skill_id));
  7699. return SCRIPT_CMD_SUCCESS;
  7700. }
  7701. /// Returns the 'basic_skill_check' setting.
  7702. /// This config determines if the server checks the skill level of NV_BASIC
  7703. /// before allowing the basic actions.
  7704. ///
  7705. /// basicskillcheck() -> <bool>
  7706. BUILDIN_FUNC(basicskillcheck)
  7707. {
  7708. script_pushint(st, battle_config.basic_skill_check);
  7709. return SCRIPT_CMD_SUCCESS;
  7710. }
  7711. /// Returns the GM level of the player.
  7712. ///
  7713. /// getgmlevel() -> <level>
  7714. BUILDIN_FUNC(getgmlevel)
  7715. {
  7716. TBL_PC* sd;
  7717. sd = script_rid2sd(st);
  7718. if( sd == NULL )
  7719. return 0;// no player attached, report source
  7720. script_pushint(st, pc_get_group_level(sd));
  7721. return SCRIPT_CMD_SUCCESS;
  7722. }
  7723. /// Returns the group ID of the player.
  7724. ///
  7725. /// getgroupid() -> <int>
  7726. BUILDIN_FUNC(getgroupid)
  7727. {
  7728. TBL_PC* sd;
  7729. sd = script_rid2sd(st);
  7730. if (sd == NULL)
  7731. return 1; // no player attached, report source
  7732. script_pushint(st, pc_get_group_id(sd));
  7733. return SCRIPT_CMD_SUCCESS;
  7734. }
  7735. /// Terminates the execution of this script instance.
  7736. ///
  7737. /// end
  7738. BUILDIN_FUNC(end)
  7739. {
  7740. TBL_PC* sd;
  7741. sd = map_id2sd(st->rid);
  7742. st->state = END;
  7743. if( st->mes_active )
  7744. st->mes_active = 0;
  7745. if( sd )
  7746. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  7747. return SCRIPT_CMD_SUCCESS;
  7748. }
  7749. /// Checks if the player has that effect state (option).
  7750. ///
  7751. /// checkoption(<option>) -> <bool>
  7752. BUILDIN_FUNC(checkoption)
  7753. {
  7754. int option;
  7755. TBL_PC* sd;
  7756. sd = script_rid2sd(st);
  7757. if( sd == NULL )
  7758. return 0;// no player attached, report source
  7759. option = script_getnum(st,2);
  7760. if( sd->sc.option&option )
  7761. script_pushint(st, 1);
  7762. else
  7763. script_pushint(st, 0);
  7764. return SCRIPT_CMD_SUCCESS;
  7765. }
  7766. /// Checks if the player is in that body state (opt1).
  7767. ///
  7768. /// checkoption1(<opt1>) -> <bool>
  7769. BUILDIN_FUNC(checkoption1)
  7770. {
  7771. int opt1;
  7772. TBL_PC* sd;
  7773. sd = script_rid2sd(st);
  7774. if( sd == NULL )
  7775. return 0;// no player attached, report source
  7776. opt1 = script_getnum(st,2);
  7777. if( sd->sc.opt1 == opt1 )
  7778. script_pushint(st, 1);
  7779. else
  7780. script_pushint(st, 0);
  7781. return SCRIPT_CMD_SUCCESS;
  7782. }
  7783. /// Checks if the player has that health state (opt2).
  7784. ///
  7785. /// checkoption2(<opt2>) -> <bool>
  7786. BUILDIN_FUNC(checkoption2)
  7787. {
  7788. int opt2;
  7789. TBL_PC* sd;
  7790. sd = script_rid2sd(st);
  7791. if( sd == NULL )
  7792. return 0;// no player attached, report source
  7793. opt2 = script_getnum(st,2);
  7794. if( sd->sc.opt2&opt2 )
  7795. script_pushint(st, 1);
  7796. else
  7797. script_pushint(st, 0);
  7798. return SCRIPT_CMD_SUCCESS;
  7799. }
  7800. /// Changes the effect state (option) of the player.
  7801. /// <flag> defaults to 1
  7802. /// <flag>=0 : removes the option
  7803. /// <flag>=other : adds the option
  7804. ///
  7805. /// setoption <option>,<flag>;
  7806. /// setoption <option>;
  7807. BUILDIN_FUNC(setoption)
  7808. {
  7809. int option;
  7810. int flag = 1;
  7811. TBL_PC* sd;
  7812. sd = script_rid2sd(st);
  7813. if( sd == NULL )
  7814. return 0;// no player attached, report source
  7815. option = script_getnum(st,2);
  7816. if( script_hasdata(st,3) )
  7817. flag = script_getnum(st,3);
  7818. else if( !option ){// Request to remove everything.
  7819. flag = 0;
  7820. option = OPTION_FALCON|OPTION_RIDING;
  7821. #ifndef NEW_CARTS
  7822. option |= OPTION_CART;
  7823. #endif
  7824. }
  7825. if( flag ){// Add option
  7826. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  7827. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  7828. pc_setoption(sd, sd->sc.option|option);
  7829. } else// Remove option
  7830. pc_setoption(sd, sd->sc.option&~option);
  7831. return SCRIPT_CMD_SUCCESS;
  7832. }
  7833. /// Returns if the player has a cart.
  7834. ///
  7835. /// checkcart() -> <bool>
  7836. ///
  7837. /// @author Valaris
  7838. BUILDIN_FUNC(checkcart)
  7839. {
  7840. TBL_PC* sd;
  7841. sd = script_rid2sd(st);
  7842. if( sd == NULL )
  7843. return 0;// no player attached, report source
  7844. if( pc_iscarton(sd) )
  7845. script_pushint(st, 1);
  7846. else
  7847. script_pushint(st, 0);
  7848. return SCRIPT_CMD_SUCCESS;
  7849. }
  7850. /// Sets the cart of the player.
  7851. /// <type> defaults to 1
  7852. /// <type>=0 : removes the cart
  7853. /// <type>=1 : Normal cart
  7854. /// <type>=2 : Wooden cart
  7855. /// <type>=3 : Covered cart with flowers and ferns
  7856. /// <type>=4 : Wooden cart with a Panda doll on the back
  7857. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  7858. ///
  7859. /// setcart <type>;
  7860. /// setcart;
  7861. BUILDIN_FUNC(setcart)
  7862. {
  7863. int type = 1;
  7864. TBL_PC* sd;
  7865. sd = script_rid2sd(st);
  7866. if( sd == NULL )
  7867. return 0;// no player attached, report source
  7868. if( script_hasdata(st,2) )
  7869. type = script_getnum(st,2);
  7870. pc_setcart(sd, type);
  7871. return SCRIPT_CMD_SUCCESS;
  7872. }
  7873. /// Returns if the player has a falcon.
  7874. ///
  7875. /// checkfalcon() -> <bool>
  7876. ///
  7877. /// @author Valaris
  7878. BUILDIN_FUNC(checkfalcon)
  7879. {
  7880. TBL_PC* sd;
  7881. sd = script_rid2sd(st);
  7882. if( sd == NULL )
  7883. return 0;// no player attached, report source
  7884. if( pc_isfalcon(sd) )
  7885. script_pushint(st, 1);
  7886. else
  7887. script_pushint(st, 0);
  7888. return SCRIPT_CMD_SUCCESS;
  7889. }
  7890. /// Sets if the player has a falcon or not.
  7891. /// <flag> defaults to 1
  7892. ///
  7893. /// setfalcon <flag>;
  7894. /// setfalcon;
  7895. BUILDIN_FUNC(setfalcon)
  7896. {
  7897. int flag = 1;
  7898. TBL_PC* sd;
  7899. sd = script_rid2sd(st);
  7900. if( sd == NULL )
  7901. return 0;// no player attached, report source
  7902. if( script_hasdata(st,2) )
  7903. flag = script_getnum(st,2);
  7904. pc_setfalcon(sd, flag);
  7905. return SCRIPT_CMD_SUCCESS;
  7906. }
  7907. /// Returns if the player is riding.
  7908. ///
  7909. /// checkriding() -> <bool>
  7910. ///
  7911. /// @author Valaris
  7912. BUILDIN_FUNC(checkriding)
  7913. {
  7914. TBL_PC* sd;
  7915. sd = script_rid2sd(st);
  7916. if( sd == NULL )
  7917. return 0;// no player attached, report source
  7918. if( pc_isriding(sd) )
  7919. script_pushint(st, 1);
  7920. else
  7921. script_pushint(st, 0);
  7922. return SCRIPT_CMD_SUCCESS;
  7923. }
  7924. /// Sets if the player is riding.
  7925. /// <flag> defaults to 1
  7926. ///
  7927. /// setriding <flag>;
  7928. /// setriding;
  7929. BUILDIN_FUNC(setriding)
  7930. {
  7931. int flag = 1;
  7932. TBL_PC* sd;
  7933. sd = script_rid2sd(st);
  7934. if( sd == NULL )
  7935. return 0;// no player attached, report source
  7936. if( script_hasdata(st,2) )
  7937. flag = script_getnum(st,2);
  7938. pc_setriding(sd, flag);
  7939. return SCRIPT_CMD_SUCCESS;
  7940. }
  7941. /// Returns if the player has a warg.
  7942. ///
  7943. /// checkwug() -> <bool>
  7944. ///
  7945. BUILDIN_FUNC(checkwug)
  7946. {
  7947. TBL_PC* sd;
  7948. sd = script_rid2sd(st);
  7949. if( sd == NULL )
  7950. return 0;// no player attached, report source
  7951. if( pc_iswug(sd) || pc_isridingwug(sd) )
  7952. script_pushint(st, 1);
  7953. else
  7954. script_pushint(st, 0);
  7955. return SCRIPT_CMD_SUCCESS;
  7956. }
  7957. /// Returns if the player is wearing MADO Gear.
  7958. ///
  7959. /// checkmadogear() -> <bool>
  7960. ///
  7961. BUILDIN_FUNC(checkmadogear)
  7962. {
  7963. TBL_PC* sd;
  7964. sd = script_rid2sd(st);
  7965. if( sd == NULL )
  7966. return 0;// no player attached, report source
  7967. if( pc_ismadogear(sd) )
  7968. script_pushint(st, 1);
  7969. else
  7970. script_pushint(st, 0);
  7971. return SCRIPT_CMD_SUCCESS;
  7972. }
  7973. /// Sets if the player is riding MADO Gear.
  7974. /// <flag> defaults to 1
  7975. ///
  7976. /// setmadogear <flag>;
  7977. /// setmadogear;
  7978. BUILDIN_FUNC(setmadogear)
  7979. {
  7980. int flag = 1;
  7981. TBL_PC* sd;
  7982. sd = script_rid2sd(st);
  7983. if( sd == NULL )
  7984. return 0;// no player attached, report source
  7985. if( script_hasdata(st,2) )
  7986. flag = script_getnum(st,2);
  7987. pc_setmadogear(sd, flag);
  7988. return SCRIPT_CMD_SUCCESS;
  7989. }
  7990. /// Sets the save point of the player.
  7991. ///
  7992. /// save "<map name>",<x>,<y>
  7993. /// savepoint "<map name>",<x>,<y>
  7994. BUILDIN_FUNC(savepoint)
  7995. {
  7996. int x;
  7997. int y;
  7998. short map_idx;
  7999. const char* str;
  8000. TBL_PC* sd;
  8001. sd = script_rid2sd(st);
  8002. if( sd == NULL )
  8003. return 0;// no player attached, report source
  8004. str = script_getstr(st, 2);
  8005. x = script_getnum(st,3);
  8006. y = script_getnum(st,4);
  8007. map_idx = mapindex_name2id(str);
  8008. if( map_idx )
  8009. pc_setsavepoint(sd, map_idx, x, y);
  8010. return SCRIPT_CMD_SUCCESS;
  8011. }
  8012. /*==========================================
  8013. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  8014. *------------------------------------------*/
  8015. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8016. {
  8017. int type;
  8018. time_t timer;
  8019. struct tm *t;
  8020. type=script_getnum(st,2);
  8021. switch(type){
  8022. case 2:
  8023. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8024. // from the system clock.)
  8025. script_pushint(st,(int)time(NULL));
  8026. break;
  8027. case 1:
  8028. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8029. time(&timer);
  8030. t=localtime(&timer);
  8031. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8032. break;
  8033. case 0:
  8034. default:
  8035. //type 0:(System Ticks)
  8036. script_pushint(st,gettick());
  8037. break;
  8038. }
  8039. return SCRIPT_CMD_SUCCESS;
  8040. }
  8041. /*==========================================
  8042. * GetTime(Type);
  8043. * 1: Sec 2: Min 3: Hour
  8044. * 4: WeekDay 5: MonthDay 6: Month
  8045. * 7: Year
  8046. *------------------------------------------*/
  8047. BUILDIN_FUNC(gettime) /* Asgard Version */
  8048. {
  8049. int type;
  8050. time_t timer;
  8051. struct tm *t;
  8052. type=script_getnum(st,2);
  8053. time(&timer);
  8054. t=localtime(&timer);
  8055. switch(type){
  8056. case 1://Sec(0~59)
  8057. script_pushint(st,t->tm_sec);
  8058. break;
  8059. case 2://Min(0~59)
  8060. script_pushint(st,t->tm_min);
  8061. break;
  8062. case 3://Hour(0~23)
  8063. script_pushint(st,t->tm_hour);
  8064. break;
  8065. case 4://WeekDay(0~6)
  8066. script_pushint(st,t->tm_wday);
  8067. break;
  8068. case 5://MonthDay(01~31)
  8069. script_pushint(st,t->tm_mday);
  8070. break;
  8071. case 6://Month(01~12)
  8072. script_pushint(st,t->tm_mon+1);
  8073. break;
  8074. case 7://Year(20xx)
  8075. script_pushint(st,t->tm_year+1900);
  8076. break;
  8077. case 8://Year Day(01~366)
  8078. script_pushint(st,t->tm_yday+1);
  8079. break;
  8080. default://(format error)
  8081. script_pushint(st,-1);
  8082. break;
  8083. }
  8084. return SCRIPT_CMD_SUCCESS;
  8085. }
  8086. /*==========================================
  8087. * GetTimeStr("TimeFMT", Length);
  8088. *------------------------------------------*/
  8089. BUILDIN_FUNC(gettimestr)
  8090. {
  8091. char *tmpstr;
  8092. const char *fmtstr;
  8093. int maxlen;
  8094. time_t now = time(NULL);
  8095. fmtstr=script_getstr(st,2);
  8096. maxlen=script_getnum(st,3);
  8097. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8098. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8099. tmpstr[maxlen]='\0';
  8100. script_pushstr(st,tmpstr);
  8101. return SCRIPT_CMD_SUCCESS;
  8102. }
  8103. /*==========================================
  8104. * Open player storage
  8105. *------------------------------------------*/
  8106. BUILDIN_FUNC(openstorage)
  8107. {
  8108. TBL_PC* sd;
  8109. sd = script_rid2sd(st);
  8110. if( sd == NULL )
  8111. return 0;
  8112. storage_storageopen(sd);
  8113. return SCRIPT_CMD_SUCCESS;
  8114. }
  8115. BUILDIN_FUNC(guildopenstorage)
  8116. {
  8117. TBL_PC* sd;
  8118. int ret;
  8119. sd = script_rid2sd(st);
  8120. if( sd == NULL )
  8121. return 0;
  8122. ret = storage_guild_storageopen(sd);
  8123. script_pushint(st,ret);
  8124. return SCRIPT_CMD_SUCCESS;
  8125. }
  8126. /*==========================================
  8127. * Make player use a skill trought item usage
  8128. *------------------------------------------*/
  8129. /// itemskill <skill id>,<level>
  8130. /// itemskill "<skill name>",<level>
  8131. BUILDIN_FUNC(itemskill)
  8132. {
  8133. int id;
  8134. int lv;
  8135. TBL_PC* sd;
  8136. struct script_data *data;
  8137. sd = script_rid2sd(st);
  8138. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8139. return 0;
  8140. data = script_getdata(st, 2);
  8141. get_val(st, data); // Convert into value in case of a variable
  8142. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8143. lv = script_getnum(st,3);
  8144. sd->skillitem=id;
  8145. sd->skillitemlv=lv;
  8146. clif_item_skill(sd,id,lv);
  8147. return SCRIPT_CMD_SUCCESS;
  8148. }
  8149. /*==========================================
  8150. * Attempt to create an item
  8151. *------------------------------------------*/
  8152. BUILDIN_FUNC(produce)
  8153. {
  8154. int trigger;
  8155. TBL_PC* sd;
  8156. sd = script_rid2sd(st);
  8157. if( sd == NULL )
  8158. return 0;
  8159. trigger=script_getnum(st,2);
  8160. clif_skill_produce_mix_list(sd, -1, trigger);
  8161. return SCRIPT_CMD_SUCCESS;
  8162. }
  8163. /*==========================================
  8164. *
  8165. *------------------------------------------*/
  8166. BUILDIN_FUNC(cooking)
  8167. {
  8168. int trigger;
  8169. TBL_PC* sd;
  8170. sd = script_rid2sd(st);
  8171. if( sd == NULL )
  8172. return 0;
  8173. trigger=script_getnum(st,2);
  8174. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8175. return SCRIPT_CMD_SUCCESS;
  8176. }
  8177. /*==========================================
  8178. * Create a pet
  8179. *------------------------------------------*/
  8180. BUILDIN_FUNC(makepet)
  8181. {
  8182. TBL_PC* sd;
  8183. int id,pet_id;
  8184. id=script_getnum(st,2);
  8185. sd = script_rid2sd(st);
  8186. if( sd == NULL )
  8187. return 0;
  8188. pet_id = search_petDB_index(id, PET_CLASS);
  8189. if (pet_id < 0)
  8190. pet_id = search_petDB_index(id, PET_EGG);
  8191. if (pet_id >= 0 && sd) {
  8192. sd->catch_target_class = pet_db[pet_id].class_;
  8193. intif_create_pet(
  8194. sd->status.account_id, sd->status.char_id,
  8195. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8196. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8197. 100, 0, 1, pet_db[pet_id].jname);
  8198. }
  8199. return SCRIPT_CMD_SUCCESS;
  8200. }
  8201. /*==========================================
  8202. * Give player exp base,job * quest_exp_rate/100
  8203. *------------------------------------------*/
  8204. BUILDIN_FUNC(getexp)
  8205. {
  8206. TBL_PC* sd;
  8207. int base=0,job=0;
  8208. double bonus;
  8209. sd = script_rid2sd(st);
  8210. if( sd == NULL )
  8211. return 0;
  8212. base=script_getnum(st,2);
  8213. job =script_getnum(st,3);
  8214. if(base<0 || job<0)
  8215. return 0;
  8216. // bonus for npc-given exp
  8217. bonus = battle_config.quest_exp_rate / 100.;
  8218. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8219. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8220. pc_gainexp(sd, NULL, base, job, true);
  8221. return SCRIPT_CMD_SUCCESS;
  8222. }
  8223. /*==========================================
  8224. * Gain guild exp [Celest]
  8225. *------------------------------------------*/
  8226. BUILDIN_FUNC(guildgetexp)
  8227. {
  8228. TBL_PC* sd;
  8229. int exp;
  8230. sd = script_rid2sd(st);
  8231. if( sd == NULL )
  8232. return 0;
  8233. exp = script_getnum(st,2);
  8234. if(exp < 0)
  8235. return 0;
  8236. if(sd && sd->status.guild_id > 0)
  8237. guild_getexp (sd, exp);
  8238. return SCRIPT_CMD_SUCCESS;
  8239. }
  8240. /*==========================================
  8241. * Changes the guild master of a guild [Skotlex]
  8242. *------------------------------------------*/
  8243. BUILDIN_FUNC(guildchangegm)
  8244. {
  8245. TBL_PC *sd;
  8246. int guild_id;
  8247. const char *name;
  8248. guild_id = script_getnum(st,2);
  8249. name = script_getstr(st,3);
  8250. sd=map_nick2sd(name);
  8251. if (!sd)
  8252. script_pushint(st,0);
  8253. else
  8254. script_pushint(st,guild_gm_change(guild_id, sd));
  8255. return SCRIPT_CMD_SUCCESS;
  8256. }
  8257. /*==========================================
  8258. * Spawn a monster :
  8259. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8260. @mapn,x,y : location
  8261. @str : monster name
  8262. @class_ : mob_id
  8263. @amount : nb to spawn
  8264. @event : event to attach to mob
  8265. *------------------------------------------*/
  8266. BUILDIN_FUNC(monster)
  8267. {
  8268. const char* mapn = script_getstr(st,2);
  8269. int x = script_getnum(st,3);
  8270. int y = script_getnum(st,4);
  8271. const char* str = script_getstr(st,5);
  8272. int class_ = script_getnum(st,6);
  8273. int amount = script_getnum(st,7);
  8274. const char* event = "";
  8275. unsigned int size = SZ_SMALL;
  8276. unsigned int ai = AI_NONE;
  8277. struct map_session_data* sd;
  8278. int16 m;
  8279. int i;
  8280. if (script_hasdata(st, 8)) {
  8281. event = script_getstr(st, 8);
  8282. check_event(st, event);
  8283. }
  8284. if (script_hasdata(st, 9)) {
  8285. size = script_getnum(st, 9);
  8286. if (size > 3) {
  8287. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8288. return 1;
  8289. }
  8290. }
  8291. if (script_hasdata(st, 10)) {
  8292. ai = script_getnum(st, 10);
  8293. if (ai >= AI_MAX) {
  8294. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8295. return 1;
  8296. }
  8297. }
  8298. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8299. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8300. return 1;
  8301. }
  8302. sd = map_id2sd(st->rid);
  8303. if (sd && strcmp(mapn, "this") == 0)
  8304. m = sd->bl.m;
  8305. else
  8306. m = map_mapname2mapid(mapn);
  8307. for(i=0; i<amount; i++){ //not optimised
  8308. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8309. if(mobid) mapreg_setreg(reference_uid(add_str("$@mobid"), i),mobid);
  8310. }
  8311. return SCRIPT_CMD_SUCCESS;
  8312. }
  8313. /*==========================================
  8314. * Request List of Monster Drops
  8315. *------------------------------------------*/
  8316. BUILDIN_FUNC(getmobdrops)
  8317. {
  8318. int class_ = script_getnum(st,2);
  8319. int i, j = 0;
  8320. struct mob_db *mob;
  8321. if( !mobdb_checkid(class_) )
  8322. {
  8323. script_pushint(st, 0);
  8324. return 0;
  8325. }
  8326. mob = mob_db(class_);
  8327. for( i = 0; i < MAX_MOB_DROP; i++ )
  8328. {
  8329. if( mob->dropitem[i].nameid < 1 )
  8330. continue;
  8331. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8332. continue;
  8333. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8334. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8335. j++;
  8336. }
  8337. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8338. script_pushint(st, 1);
  8339. return SCRIPT_CMD_SUCCESS;
  8340. }
  8341. /*==========================================
  8342. * Same as monster but randomize location in x0,x1,y0,y1 area
  8343. *------------------------------------------*/
  8344. BUILDIN_FUNC(areamonster)
  8345. {
  8346. const char* mapn = script_getstr(st,2);
  8347. int x0 = script_getnum(st,3);
  8348. int y0 = script_getnum(st,4);
  8349. int x1 = script_getnum(st,5);
  8350. int y1 = script_getnum(st,6);
  8351. const char* str = script_getstr(st,7);
  8352. int class_ = script_getnum(st,8);
  8353. int amount = script_getnum(st,9);
  8354. const char* event = "";
  8355. unsigned int size = SZ_SMALL;
  8356. unsigned int ai = AI_NONE;
  8357. struct map_session_data* sd;
  8358. int16 m;
  8359. if (script_hasdata(st,10)) {
  8360. event = script_getstr(st, 10);
  8361. check_event(st, event);
  8362. }
  8363. if (script_hasdata(st, 11)) {
  8364. size = script_getnum(st, 11);
  8365. if (size > 3) {
  8366. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8367. return 1;
  8368. }
  8369. }
  8370. if (script_hasdata(st, 12)) {
  8371. ai = script_getnum(st, 12);
  8372. if (ai >= AI_MAX) {
  8373. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8374. return 1;
  8375. }
  8376. }
  8377. sd = map_id2sd(st->rid);
  8378. if (sd && strcmp(mapn, "this") == 0)
  8379. m = sd->bl.m;
  8380. else
  8381. m = map_mapname2mapid(mapn);
  8382. mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, amount, event, size, ai);
  8383. return SCRIPT_CMD_SUCCESS;
  8384. }
  8385. /*==========================================
  8386. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8387. *------------------------------------------*/
  8388. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8389. { //same fix but with killmonster instead - stripping events from mobs.
  8390. TBL_MOB* md = (TBL_MOB*)bl;
  8391. char *event=va_arg(ap,char *);
  8392. int allflag=va_arg(ap,int);
  8393. md->state.npc_killmonster = 1;
  8394. if(!allflag){
  8395. if(strcmp(event,md->npc_event)==0)
  8396. status_kill(bl);
  8397. }else{
  8398. if(!md->spawn)
  8399. status_kill(bl);
  8400. }
  8401. md->state.npc_killmonster = 0;
  8402. return 0;
  8403. }
  8404. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8405. {
  8406. TBL_MOB* md = (TBL_MOB*)bl;
  8407. char *event=va_arg(ap,char *);
  8408. int allflag=va_arg(ap,int);
  8409. if(!allflag){
  8410. if(strcmp(event,md->npc_event)==0)
  8411. status_kill(bl);
  8412. }else{
  8413. if(!md->spawn)
  8414. status_kill(bl);
  8415. }
  8416. return 0;
  8417. }
  8418. BUILDIN_FUNC(killmonster)
  8419. {
  8420. const char *mapname,*event;
  8421. int16 m,allflag=0;
  8422. mapname=script_getstr(st,2);
  8423. event=script_getstr(st,3);
  8424. if(strcmp(event,"All")==0)
  8425. allflag = 1;
  8426. else
  8427. check_event(st, event);
  8428. if( (m=map_mapname2mapid(mapname))<0 )
  8429. return 0;
  8430. if( script_hasdata(st,4) ) {
  8431. if ( script_getnum(st,4) == 1 ) {
  8432. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8433. return 0;
  8434. }
  8435. }
  8436. map_freeblock_lock();
  8437. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8438. map_freeblock_unlock();
  8439. return SCRIPT_CMD_SUCCESS;
  8440. }
  8441. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8442. { //Strips the event from the mob if it's killed the old method.
  8443. struct mob_data *md;
  8444. md = BL_CAST(BL_MOB, bl);
  8445. if (md->npc_event[0])
  8446. md->npc_event[0] = 0;
  8447. status_kill(bl);
  8448. return 0;
  8449. }
  8450. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8451. {
  8452. status_kill(bl);
  8453. return 0;
  8454. }
  8455. BUILDIN_FUNC(killmonsterall)
  8456. {
  8457. const char *mapname;
  8458. int16 m;
  8459. mapname=script_getstr(st,2);
  8460. if( (m = map_mapname2mapid(mapname))<0 )
  8461. return 0;
  8462. if( script_hasdata(st,3) ) {
  8463. if ( script_getnum(st,3) == 1 ) {
  8464. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8465. return 0;
  8466. }
  8467. }
  8468. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8469. return SCRIPT_CMD_SUCCESS;
  8470. }
  8471. /*==========================================
  8472. * Creates a clone of a player.
  8473. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8474. *------------------------------------------*/
  8475. BUILDIN_FUNC(clone)
  8476. {
  8477. TBL_PC *sd, *msd=NULL;
  8478. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  8479. unsigned int duration = 0;
  8480. const char *mapname,*event;
  8481. mapname=script_getstr(st,2);
  8482. x=script_getnum(st,3);
  8483. y=script_getnum(st,4);
  8484. event=script_getstr(st,5);
  8485. char_id=script_getnum(st,6);
  8486. if( script_hasdata(st,7) )
  8487. master_id=script_getnum(st,7);
  8488. if( script_hasdata(st,8) )
  8489. mode=script_getnum(st,8);
  8490. if( script_hasdata(st,9) )
  8491. flag=script_getnum(st,9);
  8492. if( script_hasdata(st,10) )
  8493. duration=script_getnum(st,10);
  8494. check_event(st, event);
  8495. m = map_mapname2mapid(mapname);
  8496. if (m < 0) return 0;
  8497. sd = map_charid2sd(char_id);
  8498. if (master_id) {
  8499. msd = map_charid2sd(master_id);
  8500. if (msd)
  8501. master_id = msd->bl.id;
  8502. else
  8503. master_id = 0;
  8504. }
  8505. if (sd) //Return ID of newly crafted clone.
  8506. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8507. else //Failed to create clone.
  8508. script_pushint(st,0);
  8509. return SCRIPT_CMD_SUCCESS;
  8510. }
  8511. /*==========================================
  8512. *------------------------------------------*/
  8513. BUILDIN_FUNC(doevent)
  8514. {
  8515. const char* event = script_getstr(st,2);
  8516. struct map_session_data* sd;
  8517. if( ( sd = script_rid2sd(st) ) == NULL )
  8518. {
  8519. return 0;
  8520. }
  8521. check_event(st, event);
  8522. npc_event(sd, event, 0);
  8523. return SCRIPT_CMD_SUCCESS;
  8524. }
  8525. /*==========================================
  8526. *------------------------------------------*/
  8527. BUILDIN_FUNC(donpcevent)
  8528. {
  8529. const char* event = script_getstr(st,2);
  8530. check_event(st, event);
  8531. if( !npc_event_do(event) ) {
  8532. struct npc_data * nd = map_id2nd(st->oid);
  8533. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8534. script_pushint(st, 0);
  8535. } else
  8536. script_pushint(st, 1);
  8537. return SCRIPT_CMD_SUCCESS;
  8538. }
  8539. /// for Aegis compatibility
  8540. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8541. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8542. {
  8543. const char* npc = script_getstr(st,2);
  8544. const char* command = script_getstr(st,3);
  8545. char event[EVENT_NAME_LENGTH];
  8546. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8547. check_event(st, event);
  8548. npc_event_do(event);
  8549. return SCRIPT_CMD_SUCCESS;
  8550. }
  8551. /*==========================================
  8552. *------------------------------------------*/
  8553. BUILDIN_FUNC(addtimer)
  8554. {
  8555. int tick = script_getnum(st,2);
  8556. const char* event = script_getstr(st, 3);
  8557. TBL_PC* sd;
  8558. check_event(st, event);
  8559. sd = script_rid2sd(st);
  8560. if( sd == NULL )
  8561. return 0;
  8562. if (!pc_addeventtimer(sd,tick,event)) {
  8563. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8564. return SCRIPT_CMD_FAILURE;
  8565. }
  8566. return SCRIPT_CMD_SUCCESS;
  8567. }
  8568. /*==========================================
  8569. *------------------------------------------*/
  8570. BUILDIN_FUNC(deltimer)
  8571. {
  8572. const char *event;
  8573. TBL_PC* sd;
  8574. event=script_getstr(st, 2);
  8575. sd = script_rid2sd(st);
  8576. if( sd == NULL )
  8577. return 0;
  8578. check_event(st, event);
  8579. pc_deleventtimer(sd,event);
  8580. return SCRIPT_CMD_SUCCESS;
  8581. }
  8582. /*==========================================
  8583. *------------------------------------------*/
  8584. BUILDIN_FUNC(addtimercount)
  8585. {
  8586. const char *event;
  8587. int tick;
  8588. TBL_PC* sd;
  8589. tick=script_getnum(st,2);
  8590. event=script_getstr(st,3);
  8591. sd = script_rid2sd(st);
  8592. if( sd == NULL )
  8593. return 0;
  8594. check_event(st, event);
  8595. pc_addeventtimercount(sd,event,tick);
  8596. return SCRIPT_CMD_SUCCESS;
  8597. }
  8598. /*==========================================
  8599. *------------------------------------------*/
  8600. BUILDIN_FUNC(initnpctimer)
  8601. {
  8602. struct npc_data *nd;
  8603. int flag = 0;
  8604. if( script_hasdata(st,3) )
  8605. { //Two arguments: NPC name and attach flag.
  8606. nd = npc_name2id(script_getstr(st, 2));
  8607. flag = script_getnum(st,3);
  8608. }
  8609. else if( script_hasdata(st,2) )
  8610. { //Check if argument is numeric (flag) or string (npc name)
  8611. struct script_data *data;
  8612. data = script_getdata(st,2);
  8613. get_val(st,data);
  8614. if( data_isstring(data) ) //NPC name
  8615. nd = npc_name2id(conv_str(st, data));
  8616. else if( data_isint(data) ) //Flag
  8617. {
  8618. nd = (struct npc_data *)map_id2bl(st->oid);
  8619. flag = conv_num(st,data);
  8620. }
  8621. else
  8622. {
  8623. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8624. return 1;
  8625. }
  8626. }
  8627. else
  8628. nd = (struct npc_data *)map_id2bl(st->oid);
  8629. if( !nd )
  8630. return 0;
  8631. if( flag ) //Attach
  8632. {
  8633. TBL_PC* sd = script_rid2sd(st);
  8634. if( sd == NULL )
  8635. return 0;
  8636. nd->u.scr.rid = sd->bl.id;
  8637. }
  8638. nd->u.scr.timertick = 0;
  8639. npc_settimerevent_tick(nd,0);
  8640. npc_timerevent_start(nd, st->rid);
  8641. return SCRIPT_CMD_SUCCESS;
  8642. }
  8643. /*==========================================
  8644. *------------------------------------------*/
  8645. BUILDIN_FUNC(startnpctimer)
  8646. {
  8647. struct npc_data *nd;
  8648. int flag = 0;
  8649. if( script_hasdata(st,3) )
  8650. { //Two arguments: NPC name and attach flag.
  8651. nd = npc_name2id(script_getstr(st, 2));
  8652. flag = script_getnum(st,3);
  8653. }
  8654. else if( script_hasdata(st,2) )
  8655. { //Check if argument is numeric (flag) or string (npc name)
  8656. struct script_data *data;
  8657. data = script_getdata(st,2);
  8658. get_val(st,data);
  8659. if( data_isstring(data) ) //NPC name
  8660. nd = npc_name2id(conv_str(st, data));
  8661. else if( data_isint(data) ) //Flag
  8662. {
  8663. nd = (struct npc_data *)map_id2bl(st->oid);
  8664. flag = conv_num(st,data);
  8665. }
  8666. else
  8667. {
  8668. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8669. return 1;
  8670. }
  8671. }
  8672. else
  8673. nd=(struct npc_data *)map_id2bl(st->oid);
  8674. if( !nd )
  8675. return 0;
  8676. if( flag ) //Attach
  8677. {
  8678. TBL_PC* sd = script_rid2sd(st);
  8679. if( sd == NULL )
  8680. return 0;
  8681. nd->u.scr.rid = sd->bl.id;
  8682. }
  8683. npc_timerevent_start(nd, st->rid);
  8684. return SCRIPT_CMD_SUCCESS;
  8685. }
  8686. /*==========================================
  8687. *------------------------------------------*/
  8688. BUILDIN_FUNC(stopnpctimer)
  8689. {
  8690. struct npc_data *nd;
  8691. int flag = 0;
  8692. if( script_hasdata(st,3) )
  8693. { //Two arguments: NPC name and attach flag.
  8694. nd = npc_name2id(script_getstr(st, 2));
  8695. flag = script_getnum(st,3);
  8696. }
  8697. else if( script_hasdata(st,2) )
  8698. { //Check if argument is numeric (flag) or string (npc name)
  8699. struct script_data *data;
  8700. data = script_getdata(st,2);
  8701. get_val(st,data);
  8702. if( data_isstring(data) ) //NPC name
  8703. nd = npc_name2id(conv_str(st, data));
  8704. else if( data_isint(data) ) //Flag
  8705. {
  8706. nd = (struct npc_data *)map_id2bl(st->oid);
  8707. flag = conv_num(st,data);
  8708. }
  8709. else
  8710. {
  8711. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8712. return 1;
  8713. }
  8714. }
  8715. else
  8716. nd=(struct npc_data *)map_id2bl(st->oid);
  8717. if( !nd )
  8718. return 0;
  8719. if( flag ) //Detach
  8720. nd->u.scr.rid = 0;
  8721. npc_timerevent_stop(nd);
  8722. return SCRIPT_CMD_SUCCESS;
  8723. }
  8724. /*==========================================
  8725. *------------------------------------------*/
  8726. BUILDIN_FUNC(getnpctimer)
  8727. {
  8728. struct npc_data *nd;
  8729. TBL_PC *sd;
  8730. int type = script_getnum(st,2);
  8731. int val = 0;
  8732. if( script_hasdata(st,3) )
  8733. nd = npc_name2id(script_getstr(st,3));
  8734. else
  8735. nd = (struct npc_data *)map_id2bl(st->oid);
  8736. if( !nd || nd->bl.type != BL_NPC )
  8737. {
  8738. script_pushint(st,0);
  8739. ShowError("getnpctimer: Invalid NPC.\n");
  8740. return 1;
  8741. }
  8742. switch( type )
  8743. {
  8744. case 0: val = npc_gettimerevent_tick(nd); break;
  8745. case 1:
  8746. if( nd->u.scr.rid )
  8747. {
  8748. sd = map_id2sd(nd->u.scr.rid);
  8749. if( !sd )
  8750. {
  8751. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8752. break;
  8753. }
  8754. val = (sd->npc_timer_id != INVALID_TIMER);
  8755. }
  8756. else
  8757. val = (nd->u.scr.timerid != INVALID_TIMER);
  8758. break;
  8759. case 2: val = nd->u.scr.timeramount; break;
  8760. }
  8761. script_pushint(st,val);
  8762. return SCRIPT_CMD_SUCCESS;
  8763. }
  8764. /*==========================================
  8765. *------------------------------------------*/
  8766. BUILDIN_FUNC(setnpctimer)
  8767. {
  8768. int tick;
  8769. struct npc_data *nd;
  8770. tick = script_getnum(st,2);
  8771. if( script_hasdata(st,3) )
  8772. nd = npc_name2id(script_getstr(st,3));
  8773. else
  8774. nd = (struct npc_data *)map_id2bl(st->oid);
  8775. if( !nd || nd->bl.type != BL_NPC )
  8776. {
  8777. script_pushint(st,1);
  8778. ShowError("setnpctimer: Invalid NPC.\n");
  8779. return 1;
  8780. }
  8781. npc_settimerevent_tick(nd,tick);
  8782. script_pushint(st,0);
  8783. return SCRIPT_CMD_SUCCESS;
  8784. }
  8785. /*==========================================
  8786. * attaches the player rid to the timer [Celest]
  8787. *------------------------------------------*/
  8788. BUILDIN_FUNC(attachnpctimer)
  8789. {
  8790. TBL_PC *sd;
  8791. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  8792. if( !nd || nd->bl.type != BL_NPC )
  8793. {
  8794. script_pushint(st,1);
  8795. ShowError("setnpctimer: Invalid NPC.\n");
  8796. return 1;
  8797. }
  8798. if( script_hasdata(st,2) )
  8799. sd = map_nick2sd(script_getstr(st,2));
  8800. else
  8801. sd = script_rid2sd(st);
  8802. if( !sd )
  8803. {
  8804. script_pushint(st,1);
  8805. ShowWarning("attachnpctimer: Invalid player.\n");
  8806. return 1;
  8807. }
  8808. nd->u.scr.rid = sd->bl.id;
  8809. script_pushint(st,0);
  8810. return SCRIPT_CMD_SUCCESS;
  8811. }
  8812. /*==========================================
  8813. * detaches a player rid from the timer [Celest]
  8814. *------------------------------------------*/
  8815. BUILDIN_FUNC(detachnpctimer)
  8816. {
  8817. struct npc_data *nd;
  8818. if( script_hasdata(st,2) )
  8819. nd = npc_name2id(script_getstr(st,2));
  8820. else
  8821. nd = (struct npc_data *)map_id2bl(st->oid);
  8822. if( !nd || nd->bl.type != BL_NPC )
  8823. {
  8824. script_pushint(st,1);
  8825. ShowError("detachnpctimer: Invalid NPC.\n");
  8826. return 1;
  8827. }
  8828. nd->u.scr.rid = 0;
  8829. script_pushint(st,0);
  8830. return SCRIPT_CMD_SUCCESS;
  8831. }
  8832. /*==========================================
  8833. * To avoid "player not attached" script errors, this function is provided,
  8834. * it checks if there is a player attached to the current script. [Skotlex]
  8835. * If no, returns 0, if yes, returns the account_id of the attached player.
  8836. *------------------------------------------*/
  8837. BUILDIN_FUNC(playerattached)
  8838. {
  8839. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  8840. script_pushint(st,0);
  8841. else
  8842. script_pushint(st,st->rid);
  8843. return SCRIPT_CMD_SUCCESS;
  8844. }
  8845. /*==========================================
  8846. *------------------------------------------*/
  8847. BUILDIN_FUNC(announce)
  8848. {
  8849. const char *mes = script_getstr(st,2);
  8850. int flag = script_getnum(st,3);
  8851. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  8852. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  8853. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  8854. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  8855. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  8856. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  8857. {
  8858. send_target target;
  8859. 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
  8860. if (bl == NULL)
  8861. return 0;
  8862. switch (flag&BC_TARGET_MASK) {
  8863. case BC_MAP: target = ALL_SAMEMAP; break;
  8864. case BC_AREA: target = AREA; break;
  8865. case BC_SELF: target = SELF; break;
  8866. default: target = ALL_CLIENT; break; // BC_ALL
  8867. }
  8868. if (fontColor)
  8869. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  8870. else
  8871. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  8872. }
  8873. else
  8874. {
  8875. if (fontColor)
  8876. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  8877. else
  8878. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  8879. }
  8880. return SCRIPT_CMD_SUCCESS;
  8881. }
  8882. /*==========================================
  8883. *------------------------------------------*/
  8884. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  8885. {
  8886. char *mes = va_arg(ap, char *);
  8887. int len = va_arg(ap, int);
  8888. int type = va_arg(ap, int);
  8889. char *fontColor = va_arg(ap, char *);
  8890. short fontType = (short)va_arg(ap, int);
  8891. short fontSize = (short)va_arg(ap, int);
  8892. short fontAlign = (short)va_arg(ap, int);
  8893. short fontY = (short)va_arg(ap, int);
  8894. if (fontColor)
  8895. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  8896. else
  8897. clif_broadcast(bl, mes, len, type, SELF);
  8898. return 0;
  8899. }
  8900. BUILDIN_FUNC(mapannounce)
  8901. {
  8902. const char *mapname = script_getstr(st,2);
  8903. const char *mes = script_getstr(st,3);
  8904. int flag = script_getnum(st,4);
  8905. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  8906. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  8907. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  8908. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  8909. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  8910. int16 m;
  8911. if ((m = map_mapname2mapid(mapname)) < 0)
  8912. return 0;
  8913. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  8914. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  8915. return SCRIPT_CMD_SUCCESS;
  8916. }
  8917. /*==========================================
  8918. *------------------------------------------*/
  8919. BUILDIN_FUNC(areaannounce)
  8920. {
  8921. const char *mapname = script_getstr(st,2);
  8922. int x0 = script_getnum(st,3);
  8923. int y0 = script_getnum(st,4);
  8924. int x1 = script_getnum(st,5);
  8925. int y1 = script_getnum(st,6);
  8926. const char *mes = script_getstr(st,7);
  8927. int flag = script_getnum(st,8);
  8928. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  8929. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  8930. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  8931. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  8932. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  8933. int16 m;
  8934. if ((m = map_mapname2mapid(mapname)) < 0)
  8935. return 0;
  8936. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  8937. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  8938. return SCRIPT_CMD_SUCCESS;
  8939. }
  8940. /*==========================================
  8941. *------------------------------------------*/
  8942. BUILDIN_FUNC(getusers)
  8943. {
  8944. int flag, val = 0;
  8945. struct map_session_data* sd;
  8946. struct block_list* bl = NULL;
  8947. flag = script_getnum(st,2);
  8948. switch(flag&0x07)
  8949. {
  8950. case 0:
  8951. if(flag&0x8)
  8952. {// npc
  8953. bl = map_id2bl(st->oid);
  8954. }
  8955. else if((sd = script_rid2sd(st))!=NULL)
  8956. {// pc
  8957. bl = &sd->bl;
  8958. }
  8959. if(bl)
  8960. {
  8961. val = map[bl->m].users;
  8962. }
  8963. break;
  8964. case 1:
  8965. val = map_getusers();
  8966. break;
  8967. default:
  8968. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  8969. script_pushint(st,0);
  8970. return 1;
  8971. }
  8972. script_pushint(st,val);
  8973. return SCRIPT_CMD_SUCCESS;
  8974. }
  8975. /*==========================================
  8976. * Works like @WHO - displays all online users names in window
  8977. *------------------------------------------*/
  8978. BUILDIN_FUNC(getusersname)
  8979. {
  8980. TBL_PC *sd, *pl_sd;
  8981. int /*disp_num=1,*/ group_level = 0;
  8982. struct s_mapiterator* iter;
  8983. sd = script_rid2sd(st);
  8984. if (!sd) return 0;
  8985. group_level = pc_get_group_level(sd);
  8986. iter = mapit_getallusers();
  8987. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  8988. {
  8989. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  8990. continue; // skip hidden sessions
  8991. /* Temporary fix for bugreport:1023.
  8992. * Do not uncomment unless you want thousands of 'next' buttons.
  8993. if((disp_num++)%10==0)
  8994. clif_scriptnext(sd,st->oid);*/
  8995. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  8996. }
  8997. mapit_free(iter);
  8998. return SCRIPT_CMD_SUCCESS;
  8999. }
  9000. /*==========================================
  9001. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9002. *------------------------------------------*/
  9003. BUILDIN_FUNC(getmapguildusers)
  9004. {
  9005. const char *str;
  9006. int16 m;
  9007. int gid;
  9008. int c=0;
  9009. struct guild *g = NULL;
  9010. str=script_getstr(st,2);
  9011. gid=script_getnum(st,3);
  9012. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9013. script_pushint(st,-1);
  9014. return 0;
  9015. }
  9016. g = guild_search(gid);
  9017. if (g){
  9018. unsigned short i;
  9019. for(i = 0; i < g->max_member; i++)
  9020. {
  9021. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9022. c++;
  9023. }
  9024. }
  9025. script_pushint(st,c);
  9026. return SCRIPT_CMD_SUCCESS;
  9027. }
  9028. /*==========================================
  9029. *------------------------------------------*/
  9030. BUILDIN_FUNC(getmapusers)
  9031. {
  9032. const char *str;
  9033. int16 m;
  9034. str=script_getstr(st,2);
  9035. if( (m=map_mapname2mapid(str))< 0){
  9036. script_pushint(st,-1);
  9037. return 0;
  9038. }
  9039. script_pushint(st,map[m].users);
  9040. return SCRIPT_CMD_SUCCESS;
  9041. }
  9042. /*==========================================
  9043. *------------------------------------------*/
  9044. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9045. {
  9046. int *users=va_arg(ap,int *);
  9047. (*users)++;
  9048. return 0;
  9049. }
  9050. BUILDIN_FUNC(getareausers)
  9051. {
  9052. const char *str;
  9053. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9054. str=script_getstr(st,2);
  9055. x0=script_getnum(st,3);
  9056. y0=script_getnum(st,4);
  9057. x1=script_getnum(st,5);
  9058. y1=script_getnum(st,6);
  9059. if( (m=map_mapname2mapid(str))< 0){
  9060. script_pushint(st,-1);
  9061. return 0;
  9062. }
  9063. map_foreachinarea(buildin_getareausers_sub,
  9064. m,x0,y0,x1,y1,BL_PC,&users);
  9065. script_pushint(st,users);
  9066. return SCRIPT_CMD_SUCCESS;
  9067. }
  9068. /*==========================================
  9069. *------------------------------------------*/
  9070. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9071. {
  9072. int nameid=va_arg(ap,int);
  9073. int *amount=va_arg(ap,int *);
  9074. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9075. if(drop->item.nameid==nameid)
  9076. (*amount)+=drop->item.amount;
  9077. return 0;
  9078. }
  9079. BUILDIN_FUNC(getareadropitem)
  9080. {
  9081. const char *str;
  9082. int16 m,x0,y0,x1,y1;
  9083. int nameid,amount=0;
  9084. struct script_data *data;
  9085. str=script_getstr(st,2);
  9086. x0=script_getnum(st,3);
  9087. y0=script_getnum(st,4);
  9088. x1=script_getnum(st,5);
  9089. y1=script_getnum(st,6);
  9090. data=script_getdata(st,7);
  9091. get_val(st,data);
  9092. if( data_isstring(data) ){
  9093. const char *name=conv_str(st,data);
  9094. struct item_data *item_data = itemdb_searchname(name);
  9095. nameid=UNKNOWN_ITEM_ID;
  9096. if( item_data )
  9097. nameid=item_data->nameid;
  9098. }else
  9099. nameid=conv_num(st,data);
  9100. if( (m=map_mapname2mapid(str))< 0){
  9101. script_pushint(st,-1);
  9102. return 0;
  9103. }
  9104. map_foreachinarea(buildin_getareadropitem_sub,
  9105. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9106. script_pushint(st,amount);
  9107. return SCRIPT_CMD_SUCCESS;
  9108. }
  9109. /*==========================================
  9110. *------------------------------------------*/
  9111. BUILDIN_FUNC(enablenpc)
  9112. {
  9113. const char *str;
  9114. str=script_getstr(st,2);
  9115. npc_enable(str,1);
  9116. return SCRIPT_CMD_SUCCESS;
  9117. }
  9118. /*==========================================
  9119. *------------------------------------------*/
  9120. BUILDIN_FUNC(disablenpc)
  9121. {
  9122. const char *str;
  9123. str=script_getstr(st,2);
  9124. npc_enable(str,0);
  9125. return SCRIPT_CMD_SUCCESS;
  9126. }
  9127. /*==========================================
  9128. *------------------------------------------*/
  9129. BUILDIN_FUNC(hideoffnpc)
  9130. {
  9131. const char *str;
  9132. str=script_getstr(st,2);
  9133. npc_enable(str,2);
  9134. return SCRIPT_CMD_SUCCESS;
  9135. }
  9136. /*==========================================
  9137. *------------------------------------------*/
  9138. BUILDIN_FUNC(hideonnpc)
  9139. {
  9140. const char *str;
  9141. str=script_getstr(st,2);
  9142. npc_enable(str,4);
  9143. return SCRIPT_CMD_SUCCESS;
  9144. }
  9145. /* Starts a status effect on the target unit or on the attached player.
  9146. *
  9147. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9148. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9149. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9150. * <flag>
  9151. * &1: Cannot be avoided (it has to start)
  9152. * &2: Tick should not be reduced (by vit, luk, lv, etc)
  9153. * &4: sc_data loaded, no value has to be altered.
  9154. * &8: rate should not be reduced
  9155. */
  9156. BUILDIN_FUNC(sc_start)
  9157. {
  9158. TBL_NPC * nd = map_id2nd(st->oid);
  9159. struct block_list* bl;
  9160. enum sc_type type;
  9161. int tick, val1, val2, val3, val4=0, rate, flag;
  9162. char start_type;
  9163. const char* command = script_getfuncname(st);
  9164. if(strstr(command, "4"))
  9165. start_type = 4;
  9166. else if(strstr(command, "2"))
  9167. start_type = 2;
  9168. else
  9169. start_type = 1;
  9170. type = (sc_type)script_getnum(st,2);
  9171. tick = script_getnum(st,3);
  9172. val1 = script_getnum(st,4);
  9173. //If from NPC we make default flag 1 to be unavoidable
  9174. if(nd && nd->bl.id == fake_nd->bl.id)
  9175. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):2;
  9176. else
  9177. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):1;
  9178. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9179. if(script_hasdata(st,(6+start_type)))
  9180. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9181. else
  9182. bl = map_id2bl(st->rid);
  9183. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9184. {// When there isn't a duration specified, try to get it from the skill_db
  9185. tick = skill_get_time(status_sc2skill(type), val1);
  9186. }
  9187. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9188. bl = map_id2bl(potion_target);
  9189. tick /= 2;// Thrown potions only last half.
  9190. val4 = 1;// Mark that this was a thrown sc_effect
  9191. }
  9192. if(!bl)
  9193. return 0;
  9194. switch(start_type) {
  9195. case 1:
  9196. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9197. break;
  9198. case 2:
  9199. val2 = script_getnum(st,5);
  9200. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9201. break;
  9202. case 4:
  9203. val2 = script_getnum(st,5);
  9204. val3 = script_getnum(st,6);
  9205. val4 = script_getnum(st,7);
  9206. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9207. break;
  9208. }
  9209. return SCRIPT_CMD_SUCCESS;
  9210. }
  9211. /// Ends one or all status effects on the target unit or on the attached player.
  9212. ///
  9213. /// sc_end <effect_id>{,<unit_id>};
  9214. BUILDIN_FUNC(sc_end)
  9215. {
  9216. struct block_list* bl;
  9217. int type;
  9218. type = script_getnum(st, 2);
  9219. if (script_hasdata(st, 3))
  9220. bl = map_id2bl(script_getnum(st, 3));
  9221. else
  9222. bl = map_id2bl(st->rid);
  9223. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9224. bl = map_id2bl(potion_target);
  9225. if (!bl)
  9226. return 0;
  9227. if (type >= 0 && type < SC_MAX)
  9228. {
  9229. struct status_change *sc = status_get_sc(bl);
  9230. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9231. if (!sce)
  9232. return 0;
  9233. switch (type)
  9234. {
  9235. case SC_WEIGHT50:
  9236. case SC_WEIGHT90:
  9237. case SC_NOCHAT:
  9238. case SC_PUSH_CART:
  9239. return 0;
  9240. default:
  9241. break;
  9242. }
  9243. //This should help status_change_end force disabling the SC in case it has no limit.
  9244. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9245. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9246. }
  9247. else
  9248. status_change_clear(bl, 3); // remove all effects
  9249. return SCRIPT_CMD_SUCCESS;
  9250. }
  9251. /*==========================================
  9252. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9253. *------------------------------------------*/
  9254. BUILDIN_FUNC(getscrate)
  9255. {
  9256. struct block_list *bl;
  9257. int type,rate;
  9258. type=script_getnum(st,2);
  9259. rate=script_getnum(st,3);
  9260. if( script_hasdata(st,4) ) //get for the bl assigned
  9261. bl = map_id2bl(script_getnum(st,4));
  9262. else
  9263. bl = map_id2bl(st->rid);
  9264. if (bl)
  9265. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9266. script_pushint(st,rate);
  9267. return SCRIPT_CMD_SUCCESS;
  9268. }
  9269. /*==========================================
  9270. * getstatus <type>{, <info>};
  9271. *------------------------------------------*/
  9272. BUILDIN_FUNC(getstatus)
  9273. {
  9274. int id, type;
  9275. struct map_session_data* sd = script_rid2sd(st);
  9276. if( sd == NULL )
  9277. {// no player attached
  9278. return 0;
  9279. }
  9280. id = script_getnum(st, 2);
  9281. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9282. if( id <= SC_NONE || id >= SC_MAX )
  9283. {// invalid status type given
  9284. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9285. return 0;
  9286. }
  9287. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9288. {// no status is active
  9289. script_pushint(st, 0);
  9290. return 0;
  9291. }
  9292. switch( type )
  9293. {
  9294. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9295. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9296. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9297. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9298. case 5:
  9299. {
  9300. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9301. if( timer )
  9302. {// return the amount of time remaining
  9303. script_pushint(st, timer->tick - gettick());
  9304. }
  9305. }
  9306. break;
  9307. default: script_pushint(st, 1); break;
  9308. }
  9309. return SCRIPT_CMD_SUCCESS;
  9310. }
  9311. /*==========================================
  9312. *
  9313. *------------------------------------------*/
  9314. BUILDIN_FUNC(debugmes)
  9315. {
  9316. const char *str;
  9317. str=script_getstr(st,2);
  9318. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9319. return SCRIPT_CMD_SUCCESS;
  9320. }
  9321. /*==========================================
  9322. *------------------------------------------*/
  9323. BUILDIN_FUNC(catchpet)
  9324. {
  9325. int pet_id;
  9326. TBL_PC *sd;
  9327. pet_id= script_getnum(st,2);
  9328. sd=script_rid2sd(st);
  9329. if( sd == NULL )
  9330. return 0;
  9331. pet_catch_process1(sd,pet_id);
  9332. return SCRIPT_CMD_SUCCESS;
  9333. }
  9334. /*==========================================
  9335. * [orn]
  9336. *------------------------------------------*/
  9337. BUILDIN_FUNC(homunculus_evolution)
  9338. {
  9339. TBL_PC *sd;
  9340. sd=script_rid2sd(st);
  9341. if( sd == NULL )
  9342. return 0;
  9343. if(hom_is_active(sd->hd))
  9344. {
  9345. if (sd->hd->homunculus.intimacy > 91000)
  9346. hom_evolution(sd->hd);
  9347. else
  9348. clif_emotion(&sd->hd->bl, E_SWT);
  9349. }
  9350. return SCRIPT_CMD_SUCCESS;
  9351. }
  9352. /*==========================================
  9353. * Checks for vaporized morph state
  9354. * and deletes ITEMID_STRANGE_EMBRYO.
  9355. *------------------------------------------*/
  9356. BUILDIN_FUNC(homunculus_mutate)
  9357. {
  9358. int homun_id;
  9359. TBL_PC *sd;
  9360. sd = script_rid2sd(st);
  9361. if( sd == NULL || sd->hd == NULL )
  9362. return 0;
  9363. if(script_hasdata(st,2))
  9364. homun_id = script_getnum(st,2);
  9365. else
  9366. homun_id = 6048 + (rnd() % 4);
  9367. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9368. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9369. int m_id = hom_class2mapid(homun_id);
  9370. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9371. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9372. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9373. hom_call(sd); // Respawn homunculus.
  9374. hom_mutate(sd->hd, homun_id);
  9375. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9376. script_pushint(st, 1);
  9377. return 0;
  9378. } else
  9379. clif_emotion(&sd->bl, E_SWT);
  9380. } else
  9381. clif_emotion(&sd->bl, E_SWT);
  9382. script_pushint(st, 0);
  9383. return SCRIPT_CMD_SUCCESS;
  9384. }
  9385. /*==========================================
  9386. * Puts homunculus into morph state
  9387. * and gives ITEMID_STRANGE_EMBRYO.
  9388. *------------------------------------------*/
  9389. BUILDIN_FUNC(morphembryo)
  9390. {
  9391. struct item item_tmp;
  9392. TBL_PC *sd;
  9393. sd = script_rid2sd(st);
  9394. if( sd == NULL || sd->hd == NULL )
  9395. return 0;
  9396. if( hom_is_active(sd->hd) ) {
  9397. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9398. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9399. char i;
  9400. memset(&item_tmp, 0, sizeof(item_tmp));
  9401. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9402. item_tmp.identify = 1;
  9403. if( item_tmp.nameid == 0 || (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9404. clif_additem(sd, 0, 0, i);
  9405. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9406. } else {
  9407. hom_vaporize(sd, HOM_ST_MORPH);
  9408. script_pushint(st, 1);
  9409. return 0;
  9410. }
  9411. } else
  9412. clif_emotion(&sd->hd->bl, E_SWT);
  9413. } else
  9414. clif_emotion(&sd->bl, E_SWT);
  9415. script_pushint(st, 0);
  9416. return SCRIPT_CMD_SUCCESS;
  9417. }
  9418. // [Zephyrus]
  9419. BUILDIN_FUNC(homunculus_shuffle)
  9420. {
  9421. TBL_PC *sd;
  9422. sd=script_rid2sd(st);
  9423. if( sd == NULL )
  9424. return 0;
  9425. if(hom_is_active(sd->hd))
  9426. hom_shuffle(sd->hd);
  9427. return SCRIPT_CMD_SUCCESS;
  9428. }
  9429. /*==========================================
  9430. * Check for homunculus state.
  9431. * Return: -1 = No homunculus
  9432. * 0 = Homunculus is active
  9433. * 1 = Homunculus is vaporized (rest)
  9434. * 2 = Homunculus is in morph state
  9435. *------------------------------------------*/
  9436. BUILDIN_FUNC(checkhomcall)
  9437. {
  9438. TBL_PC *sd = script_rid2sd(st);
  9439. TBL_HOM *hd;
  9440. if( sd == NULL )
  9441. return 0;
  9442. hd = sd->hd;
  9443. if( !hd )
  9444. script_pushint(st, -1);
  9445. else
  9446. script_pushint(st, hd->homunculus.vaporize);
  9447. return SCRIPT_CMD_SUCCESS;
  9448. }
  9449. //These two functions bring the eA MAPID_* class functionality to scripts.
  9450. BUILDIN_FUNC(eaclass)
  9451. {
  9452. int class_;
  9453. if( script_hasdata(st,2) )
  9454. class_ = script_getnum(st,2);
  9455. else {
  9456. TBL_PC *sd;
  9457. sd=script_rid2sd(st);
  9458. if (!sd) {
  9459. script_pushint(st,-1);
  9460. return 0;
  9461. }
  9462. class_ = sd->status.class_;
  9463. }
  9464. script_pushint(st,pc_jobid2mapid(class_));
  9465. return SCRIPT_CMD_SUCCESS;
  9466. }
  9467. BUILDIN_FUNC(roclass)
  9468. {
  9469. int class_ =script_getnum(st,2);
  9470. int sex;
  9471. if( script_hasdata(st,3) )
  9472. sex = script_getnum(st,3);
  9473. else {
  9474. TBL_PC *sd;
  9475. if (st->rid && (sd=script_rid2sd(st)))
  9476. sex = sd->status.sex;
  9477. else
  9478. sex = 1; //Just use male when not found.
  9479. }
  9480. script_pushint(st,pc_mapid2jobid(class_, sex));
  9481. return SCRIPT_CMD_SUCCESS;
  9482. }
  9483. /*==========================================
  9484. * Tells client to open a hatching window, used for pet incubator
  9485. *------------------------------------------*/
  9486. BUILDIN_FUNC(birthpet)
  9487. {
  9488. TBL_PC *sd;
  9489. sd=script_rid2sd(st);
  9490. if( sd == NULL )
  9491. return 0;
  9492. if( sd->status.pet_id )
  9493. {// do not send egg list, when you already have a pet
  9494. return 0;
  9495. }
  9496. clif_sendegg(sd);
  9497. return SCRIPT_CMD_SUCCESS;
  9498. }
  9499. /*==========================================
  9500. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  9501. * @type
  9502. * 1 : make like after rebirth
  9503. * 2 : blvl,jlvl=1, skillpoint=0
  9504. * 3 : don't reset skill, blvl=1
  9505. * 4 : jlvl=0
  9506. *------------------------------------------*/
  9507. BUILDIN_FUNC(resetlvl)
  9508. {
  9509. TBL_PC *sd;
  9510. int type=script_getnum(st,2);
  9511. sd=script_rid2sd(st);
  9512. if( sd == NULL )
  9513. return 0;
  9514. pc_resetlvl(sd,type);
  9515. return SCRIPT_CMD_SUCCESS;
  9516. }
  9517. /*==========================================
  9518. * Reset a player status point
  9519. *------------------------------------------*/
  9520. BUILDIN_FUNC(resetstatus)
  9521. {
  9522. TBL_PC *sd;
  9523. sd=script_rid2sd(st);
  9524. pc_resetstate(sd);
  9525. return SCRIPT_CMD_SUCCESS;
  9526. }
  9527. /*==========================================
  9528. * script command resetskill
  9529. *------------------------------------------*/
  9530. BUILDIN_FUNC(resetskill)
  9531. {
  9532. TBL_PC *sd;
  9533. sd=script_rid2sd(st);
  9534. pc_resetskill(sd,1);
  9535. return SCRIPT_CMD_SUCCESS;
  9536. }
  9537. /*==========================================
  9538. * Counts total amount of skill points.
  9539. *------------------------------------------*/
  9540. BUILDIN_FUNC(skillpointcount)
  9541. {
  9542. TBL_PC *sd;
  9543. sd=script_rid2sd(st);
  9544. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9545. return SCRIPT_CMD_SUCCESS;
  9546. }
  9547. /*==========================================
  9548. *
  9549. *------------------------------------------*/
  9550. BUILDIN_FUNC(changebase)
  9551. {
  9552. TBL_PC *sd=NULL;
  9553. int vclass;
  9554. if( script_hasdata(st,3) )
  9555. sd=map_id2sd(script_getnum(st,3));
  9556. else
  9557. sd=script_rid2sd(st);
  9558. if(sd == NULL)
  9559. return 0;
  9560. vclass = script_getnum(st,2);
  9561. if(vclass == JOB_WEDDING)
  9562. {
  9563. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9564. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9565. )
  9566. return 0;
  9567. }
  9568. if(!sd->disguise && vclass != sd->vd.class_) {
  9569. status_set_viewdata(&sd->bl, vclass);
  9570. //Updated client view. Base, Weapon and Cloth Colors.
  9571. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9572. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9573. if (sd->vd.cloth_color)
  9574. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9575. clif_skillinfoblock(sd);
  9576. }
  9577. return SCRIPT_CMD_SUCCESS;
  9578. }
  9579. /*==========================================
  9580. * Unequip all item and request for a changesex to char-serv
  9581. *------------------------------------------*/
  9582. BUILDIN_FUNC(changesex)
  9583. {
  9584. int i;
  9585. TBL_PC *sd = NULL;
  9586. sd = script_rid2sd(st);
  9587. pc_resetskill(sd,4);
  9588. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9589. for( i=0; i<EQI_MAX; i++ )
  9590. if( sd->equip_index[i] >= 0 ) pc_unequipitem(sd, sd->equip_index[i], 3);
  9591. chrif_changesex(sd);
  9592. return SCRIPT_CMD_SUCCESS;
  9593. }
  9594. /*==========================================
  9595. * Works like 'announce' but outputs in the common chat window
  9596. *------------------------------------------*/
  9597. BUILDIN_FUNC(globalmes)
  9598. {
  9599. struct block_list *bl = map_id2bl(st->oid);
  9600. struct npc_data *nd = (struct npc_data *)bl;
  9601. const char *name=NULL,*mes;
  9602. mes=script_getstr(st,2);
  9603. if(mes==NULL) return 0;
  9604. if(script_hasdata(st,3)){ // npc name to display
  9605. name=script_getstr(st,3);
  9606. } else {
  9607. name=nd->name; //use current npc name
  9608. }
  9609. npc_globalmessage(name,mes); // broadcast to all players connected
  9610. return SCRIPT_CMD_SUCCESS;
  9611. }
  9612. /////////////////////////////////////////////////////////////////////
  9613. // NPC waiting room (chat room)
  9614. //
  9615. /// Creates a waiting room (chat room) for this npc.
  9616. ///
  9617. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9618. BUILDIN_FUNC(waitingroom)
  9619. {
  9620. struct npc_data* nd;
  9621. const char* title = script_getstr(st, 2);
  9622. int limit = script_getnum(st, 3);
  9623. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9624. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9625. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9626. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9627. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9628. nd = (struct npc_data *)map_id2bl(st->oid);
  9629. if( nd != NULL )
  9630. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  9631. return SCRIPT_CMD_SUCCESS;
  9632. }
  9633. /// Removes the waiting room of the current or target npc.
  9634. ///
  9635. /// delwaitingroom "<npc_name>";
  9636. /// delwaitingroom;
  9637. BUILDIN_FUNC(delwaitingroom)
  9638. {
  9639. struct npc_data* nd;
  9640. if( script_hasdata(st,2) )
  9641. nd = npc_name2id(script_getstr(st, 2));
  9642. else
  9643. nd = (struct npc_data *)map_id2bl(st->oid);
  9644. if( nd != NULL )
  9645. chat_deletenpcchat(nd);
  9646. return SCRIPT_CMD_SUCCESS;
  9647. }
  9648. /// Kicks all the players from the waiting room of the current or target npc.
  9649. ///
  9650. /// kickwaitingroomall "<npc_name>";
  9651. /// kickwaitingroomall;
  9652. BUILDIN_FUNC(waitingroomkickall)
  9653. {
  9654. struct npc_data* nd;
  9655. struct chat_data* cd;
  9656. if( script_hasdata(st,2) )
  9657. nd = npc_name2id(script_getstr(st,2));
  9658. else
  9659. nd = (struct npc_data *)map_id2bl(st->oid);
  9660. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9661. chat_npckickall(cd);
  9662. return SCRIPT_CMD_SUCCESS;
  9663. }
  9664. /// Enables the waiting room event of the current or target npc.
  9665. ///
  9666. /// enablewaitingroomevent "<npc_name>";
  9667. /// enablewaitingroomevent;
  9668. BUILDIN_FUNC(enablewaitingroomevent)
  9669. {
  9670. struct npc_data* nd;
  9671. struct chat_data* cd;
  9672. if( script_hasdata(st,2) )
  9673. nd = npc_name2id(script_getstr(st, 2));
  9674. else
  9675. nd = (struct npc_data *)map_id2bl(st->oid);
  9676. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9677. chat_enableevent(cd);
  9678. return SCRIPT_CMD_SUCCESS;
  9679. }
  9680. /// Disables the waiting room event of the current or target npc.
  9681. ///
  9682. /// disablewaitingroomevent "<npc_name>";
  9683. /// disablewaitingroomevent;
  9684. BUILDIN_FUNC(disablewaitingroomevent)
  9685. {
  9686. struct npc_data *nd;
  9687. struct chat_data *cd;
  9688. if( script_hasdata(st,2) )
  9689. nd = npc_name2id(script_getstr(st, 2));
  9690. else
  9691. nd = (struct npc_data *)map_id2bl(st->oid);
  9692. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9693. chat_disableevent(cd);
  9694. return SCRIPT_CMD_SUCCESS;
  9695. }
  9696. /// Returns info on the waiting room of the current or target npc.
  9697. /// Returns -1 if the type unknown
  9698. /// <type>=0 : current number of users
  9699. /// <type>=1 : maximum number of users allowed
  9700. /// <type>=2 : the number of users that trigger the event
  9701. /// <type>=3 : if the trigger is disabled
  9702. /// <type>=4 : the title of the waiting room
  9703. /// <type>=5 : the password of the waiting room
  9704. /// <type>=16 : the name of the waiting room event
  9705. /// <type>=32 : if the waiting room is full
  9706. /// <type>=33 : if there are enough users to trigger the event
  9707. ///
  9708. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9709. /// getwaitingroomstate(<type>) -> <info>
  9710. BUILDIN_FUNC(getwaitingroomstate)
  9711. {
  9712. struct npc_data *nd;
  9713. struct chat_data *cd;
  9714. int type;
  9715. type = script_getnum(st,2);
  9716. if( script_hasdata(st,3) )
  9717. nd = npc_name2id(script_getstr(st, 3));
  9718. else
  9719. nd = (struct npc_data *)map_id2bl(st->oid);
  9720. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9721. {
  9722. script_pushint(st, -1);
  9723. return 0;
  9724. }
  9725. switch(type)
  9726. {
  9727. case 0: script_pushint(st, cd->users); break;
  9728. case 1: script_pushint(st, cd->limit); break;
  9729. case 2: script_pushint(st, cd->trigger&0x7f); break;
  9730. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  9731. case 4: script_pushstrcopy(st, cd->title); break;
  9732. case 5: script_pushstrcopy(st, cd->pass); break;
  9733. case 16: script_pushstrcopy(st, cd->npc_event);break;
  9734. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  9735. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  9736. default: script_pushint(st, -1); break;
  9737. }
  9738. return SCRIPT_CMD_SUCCESS;
  9739. }
  9740. /// Warps the trigger or target amount of players to the target map and position.
  9741. /// Players are automatically removed from the waiting room.
  9742. /// Those waiting the longest will get warped first.
  9743. /// The target map can be "Random" for a random position in the current map,
  9744. /// and "SavePoint" for the savepoint map+position.
  9745. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  9746. ///
  9747. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  9748. /// The total number of teleported players is put into $@warpwaitingpcnum
  9749. ///
  9750. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  9751. /// warpwaitingpc "<map name>",<x>,<y>;
  9752. BUILDIN_FUNC(warpwaitingpc)
  9753. {
  9754. int x;
  9755. int y;
  9756. int i;
  9757. int n;
  9758. const char* map_name;
  9759. struct npc_data* nd;
  9760. struct chat_data* cd;
  9761. nd = (struct npc_data *)map_id2bl(st->oid);
  9762. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9763. return 0;
  9764. map_name = script_getstr(st,2);
  9765. x = script_getnum(st,3);
  9766. y = script_getnum(st,4);
  9767. n = cd->trigger&0x7f;
  9768. if( script_hasdata(st,5) )
  9769. n = script_getnum(st,5);
  9770. for( i = 0; i < n && cd->users > 0; i++ )
  9771. {
  9772. TBL_PC* sd = cd->usersd[0];
  9773. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  9774. {// can't teleport on this map
  9775. break;
  9776. }
  9777. if( cd->zeny )
  9778. {// fee set
  9779. if( (uint32)sd->status.zeny < cd->zeny )
  9780. {// no zeny to cover set fee
  9781. break;
  9782. }
  9783. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  9784. }
  9785. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  9786. if( strcmp(map_name,"Random") == 0 )
  9787. pc_randomwarp(sd,CLR_TELEPORT);
  9788. else if( strcmp(map_name,"SavePoint") == 0 )
  9789. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  9790. else
  9791. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  9792. }
  9793. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  9794. return SCRIPT_CMD_SUCCESS;
  9795. }
  9796. /////////////////////////////////////////////////////////////////////
  9797. // ...
  9798. //
  9799. /// Detaches a character from a script.
  9800. ///
  9801. /// @param st Script state to detach the character from.
  9802. static void script_detach_rid(struct script_state* st)
  9803. {
  9804. if(st->rid)
  9805. {
  9806. script_detach_state(st, false);
  9807. st->rid = 0;
  9808. }
  9809. }
  9810. /*=========================================================================
  9811. * Attaches a set of RIDs to the current script. [digitalhamster]
  9812. * addrid(<type>{,<flag>{,<parameters>}});
  9813. * <type>:
  9814. * 0 : All players in the server.
  9815. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  9816. * 2 : Party members of a specified party ID.
  9817. * [ Parameters: <party id> ]
  9818. * 3 : Guild members of a specified guild ID.
  9819. * [ Parameters: <guild id> ]
  9820. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  9821. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  9822. * Account ID: The specified account ID.
  9823. * <flag>:
  9824. * 0 : Players are always attached. (default)
  9825. * 1 : Players currently running another script will not be attached.
  9826. *-------------------------------------------------------------------------*/
  9827. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  9828. {
  9829. int forceflag;
  9830. struct map_session_data *sd = (TBL_PC *)bl;
  9831. struct script_state* st;
  9832. st = va_arg(ap,struct script_state*);
  9833. forceflag = va_arg(ap,int);
  9834. if(!forceflag || !sd->st)
  9835. if(sd->status.account_id != st->rid)
  9836. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9837. return 0;
  9838. }
  9839. BUILDIN_FUNC(addrid)
  9840. {
  9841. struct s_mapiterator* iter;
  9842. struct block_list *bl;
  9843. TBL_PC *sd;
  9844. if(st->rid < 1) {
  9845. st->state = END;
  9846. bl = map_id2bl(st->oid);
  9847. } else
  9848. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  9849. iter = mapit_getallusers();
  9850. switch(script_getnum(st,2)) {
  9851. case 0:
  9852. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9853. if(!script_getnum(st,3) || !sd->st)
  9854. if(sd->status.account_id != st->rid) //attached player already runs.
  9855. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9856. }
  9857. break;
  9858. case 1:
  9859. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9860. if(!script_getnum(st,3) || !sd->st)
  9861. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  9862. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9863. }
  9864. break;
  9865. case 2:
  9866. if(script_getnum(st,4) == 0) {
  9867. script_pushint(st,0);
  9868. return 0;
  9869. }
  9870. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9871. if(!script_getnum(st,3) || !sd->st)
  9872. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  9873. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9874. }
  9875. break;
  9876. case 3:
  9877. if(script_getnum(st,4) == 0) {
  9878. script_pushint(st,0);
  9879. return 0;
  9880. }
  9881. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9882. if(!script_getnum(st,3) || !sd->st)
  9883. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  9884. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9885. }
  9886. break;
  9887. case 4:
  9888. map_foreachinarea(buildin_addrid_sub,
  9889. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  9890. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  9891. break;
  9892. default:
  9893. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  9894. script_pushint(st,0);
  9895. return 0;
  9896. }
  9897. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  9898. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  9899. script_pushint(st,1);
  9900. }
  9901. return 0;
  9902. }
  9903. mapit_free(iter);
  9904. script_pushint(st,1);
  9905. return SCRIPT_CMD_SUCCESS;
  9906. }
  9907. /*==========================================
  9908. * Attach sd char id to script and detach current one if any
  9909. *------------------------------------------*/
  9910. BUILDIN_FUNC(attachrid)
  9911. {
  9912. int rid = script_getnum(st,2);
  9913. if (map_id2sd(rid) != NULL) {
  9914. script_detach_rid(st);
  9915. st->rid = rid;
  9916. script_attach_state(st);
  9917. script_pushint(st,1);
  9918. } else
  9919. script_pushint(st,0);
  9920. return SCRIPT_CMD_SUCCESS;
  9921. }
  9922. /*==========================================
  9923. * Detach script to rid
  9924. *------------------------------------------*/
  9925. BUILDIN_FUNC(detachrid)
  9926. {
  9927. script_detach_rid(st);
  9928. return SCRIPT_CMD_SUCCESS;
  9929. }
  9930. /*==========================================
  9931. * Chk if account connected, (and charid from account if specified)
  9932. *------------------------------------------*/
  9933. BUILDIN_FUNC(isloggedin)
  9934. {
  9935. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  9936. if (script_hasdata(st,3) && sd &&
  9937. sd->status.char_id != script_getnum(st,3))
  9938. sd = NULL;
  9939. push_val(st->stack,C_INT,sd!=NULL);
  9940. return SCRIPT_CMD_SUCCESS;
  9941. }
  9942. /*==========================================
  9943. *
  9944. *------------------------------------------*/
  9945. BUILDIN_FUNC(setmapflagnosave)
  9946. {
  9947. int16 m,x,y;
  9948. unsigned short mapindex;
  9949. const char *str,*str2;
  9950. str=script_getstr(st,2);
  9951. str2=script_getstr(st,3);
  9952. x=script_getnum(st,4);
  9953. y=script_getnum(st,5);
  9954. m = map_mapname2mapid(str);
  9955. mapindex = mapindex_name2id(str2);
  9956. if(m >= 0 && mapindex) {
  9957. map[m].flag.nosave=1;
  9958. map[m].save.map=mapindex;
  9959. map[m].save.x=x;
  9960. map[m].save.y=y;
  9961. }
  9962. return SCRIPT_CMD_SUCCESS;
  9963. }
  9964. BUILDIN_FUNC(getmapflag)
  9965. {
  9966. int16 m,i;
  9967. const char *str;
  9968. str=script_getstr(st,2);
  9969. i=script_getnum(st,3);
  9970. m = map_mapname2mapid(str);
  9971. if(m >= 0) {
  9972. switch(i) {
  9973. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  9974. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  9975. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  9976. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  9977. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  9978. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  9979. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  9980. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  9981. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  9982. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  9983. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  9984. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  9985. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  9986. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  9987. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  9988. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  9989. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  9990. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  9991. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  9992. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  9993. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  9994. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  9995. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  9996. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  9997. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  9998. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  9999. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10000. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10001. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10002. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10003. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10004. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10005. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10006. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10007. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10008. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10009. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10010. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10011. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10012. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10013. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10014. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10015. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10016. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10017. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10018. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10019. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10020. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10021. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10022. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10023. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10024. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10025. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10026. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10027. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10028. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10029. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10030. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10031. #ifdef ADJUST_SKILL_DAMAGE
  10032. case MF_SKILL_DAMAGE:
  10033. {
  10034. int ret_val=0, type=0;
  10035. FETCH(4,type);
  10036. switch (type) {
  10037. case 1: ret_val = map[m].adjust.damage.pc; break;
  10038. case 2: ret_val = map[m].adjust.damage.mob; break;
  10039. case 3: ret_val = map[m].adjust.damage.boss; break;
  10040. case 4: ret_val = map[m].adjust.damage.other; break;
  10041. case 5: ret_val = map[m].adjust.damage.caster; break;
  10042. default: ret_val = map[m].flag.skill_damage; break;
  10043. }
  10044. script_pushint(st,ret_val); break;
  10045. } break;
  10046. #endif
  10047. }
  10048. }
  10049. return SCRIPT_CMD_SUCCESS;
  10050. }
  10051. /* pvp timer handling */
  10052. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10053. TBL_PC* sd = (TBL_PC*)bl;
  10054. if (sd->pvp_timer == INVALID_TIMER) {
  10055. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10056. sd->pvp_rank = 0;
  10057. sd->pvp_lastusers = 0;
  10058. sd->pvp_point = 5;
  10059. sd->pvp_won = 0;
  10060. sd->pvp_lost = 0;
  10061. }
  10062. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  10063. clif_maptypeproperty2(&sd->bl,SELF);
  10064. return 0;
  10065. }
  10066. BUILDIN_FUNC(setmapflag)
  10067. {
  10068. int16 m,i;
  10069. const char *str;
  10070. int val=0;
  10071. str=script_getstr(st,2);
  10072. i=script_getnum(st,3);
  10073. FETCH(4,val);
  10074. m = map_mapname2mapid(str);
  10075. if(m >= 0) {
  10076. switch(i) {
  10077. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10078. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10079. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10080. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10081. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10082. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10083. case MF_PVP:
  10084. map[m].flag.pvp = 1;
  10085. if( !battle_config.pk_mode ) {
  10086. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10087. }
  10088. break;
  10089. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10090. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10091. case MF_GVG: {
  10092. struct block_list bl;
  10093. map[m].flag.gvg = 1;
  10094. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10095. bl.type = BL_NUL;
  10096. bl.m = m;
  10097. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10098. }
  10099. break;
  10100. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10101. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10102. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10103. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10104. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10105. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10106. case MF_SNOW: map[m].flag.snow = 1; break;
  10107. case MF_FOG: map[m].flag.fog = 1; break;
  10108. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10109. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10110. case MF_NOGO: map[m].flag.nogo = 1; break;
  10111. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10112. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10113. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10114. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10115. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10116. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10117. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10118. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10119. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10120. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10121. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10122. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10123. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10124. case MF_RESTRICTED:
  10125. map[m].zone |= 1<<(val+1);
  10126. map[m].flag.restricted=1;
  10127. break;
  10128. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10129. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10130. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10131. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10132. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10133. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10134. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10135. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10136. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10137. case MF_TOWN: map[m].flag.town = 1; break;
  10138. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10139. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10140. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10141. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10142. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10143. case MF_RESET: map[m].flag.reset = 1; break;
  10144. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10145. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10146. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10147. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10148. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10149. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10150. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10151. #ifdef ADJUST_SKILL_DAMAGE
  10152. case MF_SKILL_DAMAGE:
  10153. {
  10154. int type=0;
  10155. FETCH(5,type);
  10156. switch (type) {
  10157. case 1: map[m].adjust.damage.pc = val; break;
  10158. case 2: map[m].adjust.damage.mob = val; break;
  10159. case 3: map[m].adjust.damage.boss = val; break;
  10160. case 4: map[m].adjust.damage.other = val; break;
  10161. case 5: map[m].adjust.damage.caster = val; break;
  10162. }
  10163. map[m].flag.skill_damage = 1;
  10164. } break;
  10165. #endif
  10166. }
  10167. }
  10168. return SCRIPT_CMD_SUCCESS;
  10169. }
  10170. BUILDIN_FUNC(removemapflag)
  10171. {
  10172. int16 m,i;
  10173. const char *str;
  10174. int val=0;
  10175. str=script_getstr(st,2);
  10176. i=script_getnum(st,3);
  10177. FETCH(4,val);
  10178. m = map_mapname2mapid(str);
  10179. if(m >= 0) {
  10180. switch(i) {
  10181. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10182. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10183. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10184. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10185. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10186. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10187. case MF_PVP: {
  10188. struct block_list bl;
  10189. bl.type = BL_NUL;
  10190. bl.m = m;
  10191. map[m].flag.pvp = 0;
  10192. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10193. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10194. }
  10195. break;
  10196. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10197. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10198. case MF_GVG: {
  10199. struct block_list bl;
  10200. bl.type = BL_NUL;
  10201. bl.m = m;
  10202. map[m].flag.gvg = 0;
  10203. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10204. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10205. }
  10206. break;
  10207. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10208. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10209. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10210. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10211. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10212. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10213. case MF_SNOW: map[m].flag.snow = 0; break;
  10214. case MF_FOG: map[m].flag.fog = 0; break;
  10215. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10216. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10217. case MF_NOGO: map[m].flag.nogo = 0; break;
  10218. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10219. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10220. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10221. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10222. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10223. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10224. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10225. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10226. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10227. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10228. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10229. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10230. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10231. case MF_RESTRICTED:
  10232. map[m].zone ^= 1<<(val+1);
  10233. if (map[m].zone == 0){
  10234. map[m].flag.restricted=0;
  10235. }
  10236. break;
  10237. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10238. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10239. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10240. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10241. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10242. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10243. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10244. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10245. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10246. case MF_TOWN: map[m].flag.town = 0; break;
  10247. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10248. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10249. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10250. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10251. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10252. case MF_RESET: map[m].flag.reset = 0; break;
  10253. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10254. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10255. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10256. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10257. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10258. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10259. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10260. #ifdef ADJUST_SKILL_DAMAGE
  10261. case MF_SKILL_DAMAGE:
  10262. {
  10263. map[m].flag.skill_damage = 0;
  10264. memset(map[m].skill_damage, 0, sizeof(map[m].skill_damage));
  10265. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10266. } break;
  10267. #endif
  10268. }
  10269. }
  10270. return SCRIPT_CMD_SUCCESS;
  10271. }
  10272. BUILDIN_FUNC(pvpon)
  10273. {
  10274. int16 m;
  10275. const char *str;
  10276. TBL_PC* sd = NULL;
  10277. struct s_mapiterator* iter;
  10278. struct block_list bl;
  10279. str = script_getstr(st,2);
  10280. m = map_mapname2mapid(str);
  10281. if( m < 0 || map[m].flag.pvp )
  10282. return 0; // nothing to do
  10283. map[m].flag.pvp = 1;
  10284. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10285. bl.type = BL_NUL;
  10286. bl.m = m;
  10287. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10288. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10289. return 0;
  10290. iter = mapit_getallusers();
  10291. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10292. {
  10293. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10294. continue; // not applicable
  10295. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10296. sd->pvp_rank = 0;
  10297. sd->pvp_lastusers = 0;
  10298. sd->pvp_point = 5;
  10299. sd->pvp_won = 0;
  10300. sd->pvp_lost = 0;
  10301. }
  10302. mapit_free(iter);
  10303. return SCRIPT_CMD_SUCCESS;
  10304. }
  10305. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10306. {
  10307. TBL_PC* sd = (TBL_PC*)bl;
  10308. clif_pvpset(sd, 0, 0, 2);
  10309. if (sd->pvp_timer != INVALID_TIMER) {
  10310. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10311. sd->pvp_timer = INVALID_TIMER;
  10312. }
  10313. return 0;
  10314. }
  10315. BUILDIN_FUNC(pvpoff)
  10316. {
  10317. int16 m;
  10318. const char *str;
  10319. struct block_list bl;
  10320. str=script_getstr(st,2);
  10321. m = map_mapname2mapid(str);
  10322. if(m < 0 || !map[m].flag.pvp)
  10323. return 0; //fixed Lupus
  10324. map[m].flag.pvp = 0;
  10325. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10326. bl.type = BL_NUL;
  10327. bl.m = m;
  10328. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10329. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10330. return 0;
  10331. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10332. return SCRIPT_CMD_SUCCESS;
  10333. }
  10334. BUILDIN_FUNC(gvgon)
  10335. {
  10336. int16 m;
  10337. const char *str;
  10338. struct block_list bl;
  10339. str=script_getstr(st,2);
  10340. m = map_mapname2mapid(str);
  10341. if(m >= 0 && !map[m].flag.gvg) {
  10342. map[m].flag.gvg = 1;
  10343. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10344. bl.type = BL_NUL;
  10345. bl.m = m;
  10346. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10347. }
  10348. return SCRIPT_CMD_SUCCESS;
  10349. }
  10350. BUILDIN_FUNC(gvgoff)
  10351. {
  10352. int16 m;
  10353. const char *str;
  10354. str=script_getstr(st,2);
  10355. m = map_mapname2mapid(str);
  10356. if(m >= 0 && map[m].flag.gvg) {
  10357. struct block_list bl;
  10358. map[m].flag.gvg = 0;
  10359. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10360. bl.type = BL_NUL;
  10361. bl.m = m;
  10362. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10363. }
  10364. return SCRIPT_CMD_SUCCESS;
  10365. }
  10366. /*==========================================
  10367. * Shows an emoticon on top of the player/npc
  10368. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10369. *------------------------------------------*/
  10370. //Optional second parameter added by [Skotlex]
  10371. BUILDIN_FUNC(emotion)
  10372. {
  10373. int type;
  10374. int player=0;
  10375. type=script_getnum(st,2);
  10376. if(type < 0 || type > 100)
  10377. return 0;
  10378. if( script_hasdata(st,3) )
  10379. player=script_getnum(st,3);
  10380. if (player) {
  10381. TBL_PC *sd = NULL;
  10382. if( script_hasdata(st,4) )
  10383. sd = map_nick2sd(script_getstr(st,4));
  10384. else
  10385. sd = script_rid2sd(st);
  10386. if (sd)
  10387. clif_emotion(&sd->bl,type);
  10388. } else
  10389. if( script_hasdata(st,4) )
  10390. {
  10391. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10392. if(nd)
  10393. clif_emotion(&nd->bl,type);
  10394. }
  10395. else
  10396. clif_emotion(map_id2bl(st->oid),type);
  10397. return SCRIPT_CMD_SUCCESS;
  10398. }
  10399. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10400. {
  10401. int16 m=va_arg(ap,int);
  10402. int g_id=va_arg(ap,int);
  10403. int flag=va_arg(ap,int);
  10404. if(!sd || sd->bl.m != m)
  10405. return 0;
  10406. if(
  10407. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10408. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10409. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10410. )
  10411. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10412. return 1;
  10413. }
  10414. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10415. {
  10416. struct mob_data *md=(struct mob_data *)bl;
  10417. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM)
  10418. status_kill(bl);
  10419. return 0;
  10420. }
  10421. /*
  10422. * Function to kick guild members out of a map and to their save points.
  10423. * m : mapid
  10424. * g_id : owner guild id
  10425. * flag & 1 : Warp out owners
  10426. * flag & 2 : Warp out outsiders
  10427. * flag & 4 : reserved for mobs
  10428. * */
  10429. BUILDIN_FUNC(maprespawnguildid)
  10430. {
  10431. const char *mapname=script_getstr(st,2);
  10432. int g_id=script_getnum(st,3);
  10433. int flag=script_getnum(st,4);
  10434. int16 m=map_mapname2mapid(mapname);
  10435. if(m == -1)
  10436. return 0;
  10437. //Catch ALL players (in case some are 'between maps' on execution time)
  10438. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10439. if (flag&4) //Remove script mobs.
  10440. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10441. return SCRIPT_CMD_SUCCESS;
  10442. }
  10443. BUILDIN_FUNC(agitstart)
  10444. {
  10445. if(agit_flag==1) return SCRIPT_CMD_SUCCESS; // Agit already Start.
  10446. agit_flag=1;
  10447. guild_agit_start();
  10448. return SCRIPT_CMD_SUCCESS;
  10449. }
  10450. BUILDIN_FUNC(agitend)
  10451. {
  10452. if(agit_flag==0) return SCRIPT_CMD_SUCCESS; // Agit already End.
  10453. agit_flag=0;
  10454. guild_agit_end();
  10455. return SCRIPT_CMD_SUCCESS;
  10456. }
  10457. BUILDIN_FUNC(agitstart2)
  10458. {
  10459. if(agit2_flag==1) return SCRIPT_CMD_SUCCESS; // Agit2 already Start.
  10460. agit2_flag=1;
  10461. guild_agit2_start();
  10462. return SCRIPT_CMD_SUCCESS;
  10463. }
  10464. BUILDIN_FUNC(agitend2)
  10465. {
  10466. if(agit2_flag==0) return SCRIPT_CMD_SUCCESS; // Agit2 already End.
  10467. agit2_flag=0;
  10468. guild_agit2_end();
  10469. return SCRIPT_CMD_SUCCESS;
  10470. }
  10471. /*==========================================
  10472. * Returns whether woe is on or off. // choice script
  10473. *------------------------------------------*/
  10474. BUILDIN_FUNC(agitcheck)
  10475. {
  10476. script_pushint(st,agit_flag);
  10477. return SCRIPT_CMD_SUCCESS;
  10478. }
  10479. /*==========================================
  10480. * Returns whether woese is on or off. // choice script
  10481. *------------------------------------------*/
  10482. BUILDIN_FUNC(agitcheck2)
  10483. {
  10484. script_pushint(st,agit2_flag);
  10485. return SCRIPT_CMD_SUCCESS;
  10486. }
  10487. /// Sets the guild_id of this npc.
  10488. ///
  10489. /// flagemblem <guild_id>;
  10490. BUILDIN_FUNC(flagemblem)
  10491. {
  10492. TBL_NPC* nd;
  10493. int g_id = script_getnum(st,2);
  10494. if(g_id < 0) return 0;
  10495. nd = (TBL_NPC*)map_id2nd(st->oid);
  10496. if( nd == NULL ) {
  10497. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10498. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  10499. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10500. } else {
  10501. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10502. nd->u.scr.guild_id = g_id;
  10503. clif_guild_emblem_area(&nd->bl);
  10504. /* guild flag caching */
  10505. if( g_id ) /* adding a id */
  10506. guild_flag_add(nd);
  10507. else if( changed ) /* removing a flag */
  10508. guild_flag_remove(nd);
  10509. }
  10510. return SCRIPT_CMD_SUCCESS;
  10511. }
  10512. BUILDIN_FUNC(getcastlename)
  10513. {
  10514. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10515. struct guild_castle* gc = guild_mapname2gc(mapname);
  10516. const char* name = (gc) ? gc->castle_name : "";
  10517. script_pushstrcopy(st,name);
  10518. return SCRIPT_CMD_SUCCESS;
  10519. }
  10520. BUILDIN_FUNC(getcastledata)
  10521. {
  10522. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10523. int index = script_getnum(st,3);
  10524. struct guild_castle *gc = guild_mapname2gc(mapname);
  10525. if (gc == NULL) {
  10526. script_pushint(st,0);
  10527. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10528. return 1;
  10529. }
  10530. switch (index) {
  10531. case 1:
  10532. script_pushint(st,gc->guild_id); break;
  10533. case 2:
  10534. script_pushint(st,gc->economy); break;
  10535. case 3:
  10536. script_pushint(st,gc->defense); break;
  10537. case 4:
  10538. script_pushint(st,gc->triggerE); break;
  10539. case 5:
  10540. script_pushint(st,gc->triggerD); break;
  10541. case 6:
  10542. script_pushint(st,gc->nextTime); break;
  10543. case 7:
  10544. script_pushint(st,gc->payTime); break;
  10545. case 8:
  10546. script_pushint(st,gc->createTime); break;
  10547. case 9:
  10548. script_pushint(st,gc->visibleC); break;
  10549. default:
  10550. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10551. script_pushint(st,gc->guardian[index-10].visible);
  10552. break;
  10553. }
  10554. script_pushint(st,0);
  10555. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10556. return 1;
  10557. }
  10558. return SCRIPT_CMD_SUCCESS;
  10559. }
  10560. BUILDIN_FUNC(setcastledata)
  10561. {
  10562. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10563. int index = script_getnum(st,3);
  10564. int value = script_getnum(st,4);
  10565. struct guild_castle *gc = guild_mapname2gc(mapname);
  10566. if (gc == NULL) {
  10567. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10568. return 1;
  10569. }
  10570. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10571. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10572. return 1;
  10573. }
  10574. guild_castledatasave(gc->castle_id, index, value);
  10575. return SCRIPT_CMD_SUCCESS;
  10576. }
  10577. /* =====================================================================
  10578. * ---------------------------------------------------------------------*/
  10579. BUILDIN_FUNC(requestguildinfo)
  10580. {
  10581. int guild_id=script_getnum(st,2);
  10582. const char *event=NULL;
  10583. if( script_hasdata(st,3) ){
  10584. event=script_getstr(st,3);
  10585. check_event(st, event);
  10586. }
  10587. if(guild_id>0)
  10588. guild_npc_request_info(guild_id,event);
  10589. return SCRIPT_CMD_SUCCESS;
  10590. }
  10591. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10592. /// getequipcardcnt(<equipment slot>);
  10593. BUILDIN_FUNC(getequipcardcnt)
  10594. {
  10595. int i=-1,j,num;
  10596. TBL_PC *sd;
  10597. int count;
  10598. num=script_getnum(st,2);
  10599. sd=script_rid2sd(st);
  10600. if (num > 0 && num <= ARRAYLENGTH(equip))
  10601. i=pc_checkequip(sd,equip[num-1]);
  10602. if (i < 0 || !sd->inventory_data[i]) {
  10603. script_pushint(st,0);
  10604. return 0;
  10605. }
  10606. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10607. {
  10608. script_pushint(st,0);
  10609. return 0;
  10610. }
  10611. count = 0;
  10612. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10613. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  10614. count++;
  10615. script_pushint(st,count);
  10616. return SCRIPT_CMD_SUCCESS;
  10617. }
  10618. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10619. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10620. /// successremovecards <slot>;
  10621. BUILDIN_FUNC(successremovecards) {
  10622. int i=-1,c,cardflag=0;
  10623. TBL_PC* sd = script_rid2sd(st);
  10624. int num = script_getnum(st,2);
  10625. if (num > 0 && num <= ARRAYLENGTH(equip))
  10626. i=pc_checkequip(sd,equip[num-1]);
  10627. if (i < 0 || !sd->inventory_data[i]) {
  10628. return 0;
  10629. }
  10630. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10631. return 0;
  10632. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10633. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10634. unsigned char flag = 0;
  10635. struct item item_tmp;
  10636. memset(&item_tmp,0,sizeof(item_tmp));
  10637. cardflag = 1;
  10638. item_tmp.nameid = sd->status.inventory[i].card[c];
  10639. item_tmp.identify = 1;
  10640. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10641. clif_additem(sd,0,0,flag);
  10642. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10643. }
  10644. }
  10645. }
  10646. if(cardflag == 1) {//if card was remove remplace item with no card
  10647. unsigned char flag = 0, j;
  10648. struct item item_tmp;
  10649. memset(&item_tmp,0,sizeof(item_tmp));
  10650. item_tmp.nameid = sd->status.inventory[i].nameid;
  10651. item_tmp.identify = 1;
  10652. item_tmp.refine = sd->status.inventory[i].refine;
  10653. item_tmp.attribute = sd->status.inventory[i].attribute;
  10654. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10655. item_tmp.bound = sd->status.inventory[i].bound;
  10656. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10657. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10658. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10659. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10660. clif_additem(sd,0,0,flag);
  10661. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10662. }
  10663. clif_misceffect(&sd->bl,3);
  10664. }
  10665. return SCRIPT_CMD_SUCCESS;
  10666. }
  10667. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10668. /// failedremovecards <slot>, <type>;
  10669. /// <type>=0 : will destroy both the item and the cards.
  10670. /// <type>=1 : will keep the item, but destroy the cards.
  10671. /// <type>=2 : will keep the cards, but destroy the item.
  10672. /// <type>=? : will just display the failure effect.
  10673. BUILDIN_FUNC(failedremovecards) {
  10674. int i=-1,c,cardflag=0;
  10675. TBL_PC* sd = script_rid2sd(st);
  10676. int num = script_getnum(st,2);
  10677. int typefail = script_getnum(st,3);
  10678. if (num > 0 && num <= ARRAYLENGTH(equip))
  10679. i=pc_checkequip(sd,equip[num-1]);
  10680. if (i < 0 || !sd->inventory_data[i])
  10681. return 0;
  10682. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10683. return 0;
  10684. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10685. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  10686. cardflag = 1;
  10687. if(typefail == 2) {// add cards to inventory, clear
  10688. unsigned char flag = 0;
  10689. struct item item_tmp;
  10690. memset(&item_tmp,0,sizeof(item_tmp));
  10691. item_tmp.nameid = sd->status.inventory[i].card[c];
  10692. item_tmp.identify = 1;
  10693. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10694. clif_additem(sd,0,0,flag);
  10695. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10696. }
  10697. }
  10698. }
  10699. }
  10700. if(cardflag == 1) {
  10701. if(typefail == 0 || typefail == 2){ // destroy the item
  10702. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10703. }
  10704. if(typefail == 1){ // destroy the card
  10705. unsigned char flag = 0, j;
  10706. struct item item_tmp;
  10707. memset(&item_tmp,0,sizeof(item_tmp));
  10708. item_tmp.nameid = sd->status.inventory[i].nameid;
  10709. item_tmp.identify = 1;
  10710. item_tmp.refine = sd->status.inventory[i].refine;
  10711. item_tmp.attribute = sd->status.inventory[i].attribute;
  10712. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10713. item_tmp.bound = sd->status.inventory[i].bound;
  10714. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10715. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10716. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10717. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10718. clif_additem(sd,0,0,flag);
  10719. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10720. }
  10721. }
  10722. clif_misceffect(&sd->bl,2);
  10723. }
  10724. return SCRIPT_CMD_SUCCESS;
  10725. }
  10726. /* ================================================================
  10727. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  10728. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  10729. * improved by [Lance]
  10730. * ================================================================*/
  10731. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  10732. {
  10733. int x,y,m,check_val=0,check_ID=0,i=0;
  10734. struct guild *g = NULL;
  10735. struct party_data *p = NULL;
  10736. const char *str;
  10737. const char *mapname;
  10738. unsigned int index;
  10739. mapname=script_getstr(st,2);
  10740. str=script_getstr(st,3);
  10741. x=script_getnum(st,4);
  10742. y=script_getnum(st,5);
  10743. if(script_hasdata(st,7)){
  10744. check_val=script_getnum(st,6);
  10745. check_ID=script_getnum(st,7);
  10746. }
  10747. if((m=map_mapname2mapid(mapname))< 0)
  10748. return 0;
  10749. if(!(index=mapindex_name2id(str)))
  10750. return 0;
  10751. switch(check_val){
  10752. case 1:
  10753. g = guild_search(check_ID);
  10754. if (g){
  10755. for( i=0; i < g->max_member; i++)
  10756. {
  10757. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  10758. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  10759. }
  10760. }
  10761. }
  10762. break;
  10763. case 2:
  10764. p = party_search(check_ID);
  10765. if(p){
  10766. for(i=0;i<MAX_PARTY; i++){
  10767. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  10768. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  10769. }
  10770. }
  10771. }
  10772. break;
  10773. default:
  10774. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  10775. break;
  10776. }
  10777. return SCRIPT_CMD_SUCCESS;
  10778. }
  10779. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  10780. {
  10781. char *event=va_arg(ap,char *);
  10782. struct mob_data *md = ((struct mob_data *)bl);
  10783. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  10784. return 1;
  10785. return SCRIPT_CMD_SUCCESS;
  10786. }
  10787. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  10788. {
  10789. const char *mapname,*event;
  10790. int16 m;
  10791. mapname=script_getstr(st,2);
  10792. event=script_getstr(st,3);
  10793. if( strcmp(event, "all") == 0 )
  10794. event = NULL;
  10795. else
  10796. check_event(st, event);
  10797. if( strcmp(mapname, "this") == 0 ) {
  10798. struct map_session_data *sd = script_rid2sd(st);
  10799. if( sd )
  10800. m = sd->bl.m;
  10801. else {
  10802. script_pushint(st,-1);
  10803. return 0;
  10804. }
  10805. }
  10806. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  10807. script_pushint(st,-1);
  10808. return 0;
  10809. }
  10810. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  10811. return SCRIPT_CMD_SUCCESS;
  10812. }
  10813. BUILDIN_FUNC(marriage)
  10814. {
  10815. const char *partner=script_getstr(st,2);
  10816. TBL_PC *sd=script_rid2sd(st);
  10817. TBL_PC *p_sd=map_nick2sd(partner);
  10818. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  10819. script_pushint(st,0);
  10820. return 0;
  10821. }
  10822. script_pushint(st,1);
  10823. return SCRIPT_CMD_SUCCESS;
  10824. }
  10825. BUILDIN_FUNC(wedding_effect)
  10826. {
  10827. TBL_PC *sd=script_rid2sd(st);
  10828. struct block_list *bl;
  10829. if(sd==NULL) {
  10830. bl=map_id2bl(st->oid);
  10831. } else
  10832. bl=&sd->bl;
  10833. clif_wedding_effect(bl);
  10834. return SCRIPT_CMD_SUCCESS;
  10835. }
  10836. BUILDIN_FUNC(divorce)
  10837. {
  10838. TBL_PC *sd=script_rid2sd(st);
  10839. if(!sd || !pc_divorce(sd)){
  10840. script_pushint(st,0);
  10841. return 0;
  10842. }
  10843. script_pushint(st,1);
  10844. return SCRIPT_CMD_SUCCESS;
  10845. }
  10846. BUILDIN_FUNC(ispartneron)
  10847. {
  10848. TBL_PC *sd=script_rid2sd(st);
  10849. if(sd==NULL || !pc_ismarried(sd) ||
  10850. map_charid2sd(sd->status.partner_id) == NULL) {
  10851. script_pushint(st,0);
  10852. return 0;
  10853. }
  10854. script_pushint(st,1);
  10855. return SCRIPT_CMD_SUCCESS;
  10856. }
  10857. BUILDIN_FUNC(getpartnerid)
  10858. {
  10859. TBL_PC *sd=script_rid2sd(st);
  10860. if (sd == NULL) {
  10861. script_pushint(st,0);
  10862. return 0;
  10863. }
  10864. script_pushint(st,sd->status.partner_id);
  10865. return SCRIPT_CMD_SUCCESS;
  10866. }
  10867. BUILDIN_FUNC(getchildid)
  10868. {
  10869. TBL_PC *sd=script_rid2sd(st);
  10870. if (sd == NULL) {
  10871. script_pushint(st,0);
  10872. return 0;
  10873. }
  10874. script_pushint(st,sd->status.child);
  10875. return SCRIPT_CMD_SUCCESS;
  10876. }
  10877. BUILDIN_FUNC(getmotherid)
  10878. {
  10879. TBL_PC *sd=script_rid2sd(st);
  10880. if (sd == NULL) {
  10881. script_pushint(st,0);
  10882. return 0;
  10883. }
  10884. script_pushint(st,sd->status.mother);
  10885. return SCRIPT_CMD_SUCCESS;
  10886. }
  10887. BUILDIN_FUNC(getfatherid)
  10888. {
  10889. TBL_PC *sd=script_rid2sd(st);
  10890. if (sd == NULL) {
  10891. script_pushint(st,0);
  10892. return 0;
  10893. }
  10894. script_pushint(st,sd->status.father);
  10895. return SCRIPT_CMD_SUCCESS;
  10896. }
  10897. BUILDIN_FUNC(warppartner)
  10898. {
  10899. int x,y;
  10900. unsigned short mapindex;
  10901. const char *str;
  10902. TBL_PC *sd=script_rid2sd(st);
  10903. TBL_PC *p_sd=NULL;
  10904. if(sd==NULL || !pc_ismarried(sd) ||
  10905. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  10906. script_pushint(st,0);
  10907. return 0;
  10908. }
  10909. str=script_getstr(st,2);
  10910. x=script_getnum(st,3);
  10911. y=script_getnum(st,4);
  10912. mapindex = mapindex_name2id(str);
  10913. if (mapindex) {
  10914. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  10915. script_pushint(st,1);
  10916. } else
  10917. script_pushint(st,0);
  10918. return SCRIPT_CMD_SUCCESS;
  10919. }
  10920. /*================================================
  10921. * Script for Displaying MOB Information [Valaris]
  10922. *------------------------------------------------*/
  10923. BUILDIN_FUNC(strmobinfo)
  10924. {
  10925. int num=script_getnum(st,2);
  10926. int class_=script_getnum(st,3);
  10927. if(!mobdb_checkid(class_))
  10928. {
  10929. if (num < 3) //requested a string
  10930. script_pushconststr(st,"");
  10931. else
  10932. script_pushint(st,0);
  10933. return 0;
  10934. }
  10935. switch (num) {
  10936. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  10937. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  10938. case 3: script_pushint(st,mob_db(class_)->lv); break;
  10939. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  10940. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  10941. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  10942. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  10943. default:
  10944. script_pushint(st,0);
  10945. break;
  10946. }
  10947. return SCRIPT_CMD_SUCCESS;
  10948. }
  10949. /*==========================================
  10950. * Summon guardians [Valaris]
  10951. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  10952. *------------------------------------------*/
  10953. BUILDIN_FUNC(guardian)
  10954. {
  10955. int class_=0,x=0,y=0,guardian=0;
  10956. const char *str,*mapname,*evt="";
  10957. bool has_index = false;
  10958. mapname =script_getstr(st,2);
  10959. x =script_getnum(st,3);
  10960. y =script_getnum(st,4);
  10961. str =script_getstr(st,5);
  10962. class_=script_getnum(st,6);
  10963. if( script_hasdata(st,8) )
  10964. {// "<event label>",<guardian index>
  10965. evt=script_getstr(st,7);
  10966. guardian=script_getnum(st,8);
  10967. has_index = true;
  10968. } else if( script_hasdata(st,7) ){
  10969. struct script_data *data = script_getdata(st,7);
  10970. get_val(st,data);
  10971. if( data_isstring(data) )
  10972. {// "<event label>"
  10973. evt=script_getstr(st,7);
  10974. } else if( data_isint(data) )
  10975. {// <guardian index>
  10976. guardian=script_getnum(st,7);
  10977. has_index = true;
  10978. } else {
  10979. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  10980. script_reportdata(data);
  10981. return 1;
  10982. }
  10983. }
  10984. check_event(st, evt);
  10985. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  10986. return SCRIPT_CMD_SUCCESS;
  10987. }
  10988. /*==========================================
  10989. * Invisible Walls [Zephyrus]
  10990. *------------------------------------------*/
  10991. BUILDIN_FUNC(setwall)
  10992. {
  10993. const char *mapname, *name;
  10994. int x, y, m, size, dir;
  10995. bool shootable;
  10996. mapname = script_getstr(st,2);
  10997. x = script_getnum(st,3);
  10998. y = script_getnum(st,4);
  10999. size = script_getnum(st,5);
  11000. dir = script_getnum(st,6);
  11001. shootable = script_getnum(st,7);
  11002. name = script_getstr(st,8);
  11003. if( (m = map_mapname2mapid(mapname)) < 0 )
  11004. return 0; // Invalid Map
  11005. map_iwall_set(m, x, y, size, dir, shootable, name);
  11006. return SCRIPT_CMD_SUCCESS;
  11007. }
  11008. BUILDIN_FUNC(delwall)
  11009. {
  11010. const char *name = script_getstr(st,2);
  11011. map_iwall_remove(name);
  11012. return SCRIPT_CMD_SUCCESS;
  11013. }
  11014. /// Retrieves various information about the specified guardian.
  11015. ///
  11016. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11017. /// type: 0 - whether it is deployed or not
  11018. /// 1 - maximum hp
  11019. /// 2 - current hp
  11020. ///
  11021. BUILDIN_FUNC(guardianinfo)
  11022. {
  11023. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11024. int id = script_getnum(st,3);
  11025. int type = script_getnum(st,4);
  11026. struct guild_castle* gc = guild_mapname2gc(mapname);
  11027. struct mob_data* gd;
  11028. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11029. {
  11030. script_pushint(st,-1);
  11031. return 0;
  11032. }
  11033. if( type == 0 )
  11034. script_pushint(st, gc->guardian[id].visible);
  11035. else
  11036. if( !gc->guardian[id].visible )
  11037. script_pushint(st,-1);
  11038. else
  11039. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11040. script_pushint(st,-1);
  11041. else
  11042. {
  11043. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11044. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11045. else
  11046. script_pushint(st,-1);
  11047. }
  11048. return SCRIPT_CMD_SUCCESS;
  11049. }
  11050. /*==========================================
  11051. * Get the item name by item_id or null
  11052. *------------------------------------------*/
  11053. BUILDIN_FUNC(getitemname)
  11054. {
  11055. unsigned short item_id = 0;
  11056. struct item_data *i_data;
  11057. char *item_name;
  11058. struct script_data *data;
  11059. data=script_getdata(st,2);
  11060. get_val(st,data);
  11061. if( data_isstring(data) ){
  11062. const char *name=conv_str(st,data);
  11063. struct item_data *item_data = itemdb_searchname(name);
  11064. if( item_data )
  11065. item_id=item_data->nameid;
  11066. }else
  11067. item_id=conv_num(st,data);
  11068. i_data = itemdb_exists(item_id);
  11069. if (i_data == NULL)
  11070. {
  11071. script_pushconststr(st,"null");
  11072. return 0;
  11073. }
  11074. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11075. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11076. script_pushstr(st,item_name);
  11077. return SCRIPT_CMD_SUCCESS;
  11078. }
  11079. /*==========================================
  11080. * Returns number of slots an item has. [Skotlex]
  11081. *------------------------------------------*/
  11082. BUILDIN_FUNC(getitemslots)
  11083. {
  11084. unsigned short item_id;
  11085. struct item_data *i_data;
  11086. item_id=script_getnum(st,2);
  11087. i_data = itemdb_exists(item_id);
  11088. if (i_data)
  11089. script_pushint(st,i_data->slot);
  11090. else
  11091. script_pushint(st,-1);
  11092. return SCRIPT_CMD_SUCCESS;
  11093. }
  11094. // TODO: add matk here if needed/once we get rid of RENEWAL
  11095. /*==========================================
  11096. * Returns some values of an item [Lupus]
  11097. * Price, Weight, etc...
  11098. getiteminfo(itemID,n), where n
  11099. 0 value_buy;
  11100. 1 value_sell;
  11101. 2 type;
  11102. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11103. if = 0, then monsters don't drop it at all (rare or a quest item)
  11104. if = -1, then this item is sold in NPC shops only
  11105. 4 sex;
  11106. 5 equip;
  11107. 6 weight;
  11108. 7 atk;
  11109. 8 def;
  11110. 9 range;
  11111. 10 slot;
  11112. 11 look;
  11113. 12 elv;
  11114. 13 wlv;
  11115. 14 view id
  11116. *------------------------------------------*/
  11117. BUILDIN_FUNC(getiteminfo)
  11118. {
  11119. unsigned short item_id,n;
  11120. struct item_data *i_data;
  11121. item_id = script_getnum(st,2);
  11122. n = script_getnum(st,3);
  11123. i_data = itemdb_exists(item_id);
  11124. if (i_data && n <= 14) {
  11125. int *item_arr = (int*)&i_data->value_buy;
  11126. script_pushint(st,item_arr[n]);
  11127. } else
  11128. script_pushint(st,-1);
  11129. return SCRIPT_CMD_SUCCESS;
  11130. }
  11131. /*==========================================
  11132. * Set some values of an item [Lupus]
  11133. * Price, Weight, etc...
  11134. setiteminfo(itemID,n,Value), where n
  11135. 0 value_buy;
  11136. 1 value_sell;
  11137. 2 type;
  11138. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11139. if = 0, then monsters don't drop it at all (rare or a quest item)
  11140. if = -1, then this item is sold in NPC shops only
  11141. 4 sex;
  11142. 5 equip;
  11143. 6 weight;
  11144. 7 atk;
  11145. 8 def;
  11146. 9 range;
  11147. 10 slot;
  11148. 11 look;
  11149. 12 elv;
  11150. 13 wlv;
  11151. 14 view id
  11152. * Returns Value or -1 if the wrong field's been set
  11153. *------------------------------------------*/
  11154. BUILDIN_FUNC(setiteminfo)
  11155. {
  11156. unsigned short item_id;
  11157. int n,value;
  11158. struct item_data *i_data;
  11159. item_id = script_getnum(st,2);
  11160. n = script_getnum(st,3);
  11161. value = script_getnum(st,4);
  11162. i_data = itemdb_exists(item_id);
  11163. if (i_data && n>=0 && n<=14) {
  11164. int *item_arr = (int*)&i_data->value_buy;
  11165. item_arr[n] = value;
  11166. script_pushint(st,value);
  11167. } else
  11168. script_pushint(st,-1);
  11169. return SCRIPT_CMD_SUCCESS;
  11170. }
  11171. /*==========================================
  11172. * Returns value from equipped item slot n [Lupus]
  11173. getequipcardid(num,slot)
  11174. where
  11175. num = eqip position slot
  11176. slot = 0,1,2,3 (Card Slot N)
  11177. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11178. it's useful when you want to check item cards or if it's signed
  11179. Useful for such quests as "Sign this refined item with players name" etc
  11180. Hat[0] +4 -> Player's Hat[0] +4
  11181. *------------------------------------------*/
  11182. BUILDIN_FUNC(getequipcardid)
  11183. {
  11184. int i=-1,num,slot;
  11185. TBL_PC *sd;
  11186. num=script_getnum(st,2);
  11187. slot=script_getnum(st,3);
  11188. sd=script_rid2sd(st);
  11189. if (num > 0 && num <= ARRAYLENGTH(equip))
  11190. i=pc_checkequip(sd,equip[num-1]);
  11191. if(i >= 0 && slot>=0 && slot<4)
  11192. script_pushint(st,sd->status.inventory[i].card[slot]);
  11193. else
  11194. script_pushint(st,0);
  11195. return SCRIPT_CMD_SUCCESS;
  11196. }
  11197. /*==========================================
  11198. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11199. *------------------------------------------*/
  11200. BUILDIN_FUNC(petskillbonus)
  11201. {
  11202. struct pet_data *pd;
  11203. TBL_PC *sd=script_rid2sd(st);
  11204. if(sd==NULL || sd->pd==NULL)
  11205. return 0;
  11206. pd=sd->pd;
  11207. if (pd->bonus)
  11208. { //Clear previous bonus
  11209. if (pd->bonus->timer != INVALID_TIMER)
  11210. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11211. } else //init
  11212. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11213. pd->bonus->type=script_getnum(st,2);
  11214. pd->bonus->val=script_getnum(st,3);
  11215. pd->bonus->duration=script_getnum(st,4);
  11216. pd->bonus->delay=script_getnum(st,5);
  11217. if (pd->state.skillbonus == 1)
  11218. pd->state.skillbonus=0; // waiting state
  11219. // wait for timer to start
  11220. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11221. pd->bonus->timer = INVALID_TIMER;
  11222. else
  11223. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11224. return SCRIPT_CMD_SUCCESS;
  11225. }
  11226. /*==========================================
  11227. * pet looting [Valaris] //Rewritten by [Skotlex]
  11228. *------------------------------------------*/
  11229. BUILDIN_FUNC(petloot)
  11230. {
  11231. int max;
  11232. struct pet_data *pd;
  11233. TBL_PC *sd=script_rid2sd(st);
  11234. if(sd==NULL || sd->pd==NULL)
  11235. return 0;
  11236. max=script_getnum(st,2);
  11237. if(max < 1)
  11238. max = 1; //Let'em loot at least 1 item.
  11239. else if (max > MAX_PETLOOT_SIZE)
  11240. max = MAX_PETLOOT_SIZE;
  11241. pd = sd->pd;
  11242. if (pd->loot != NULL)
  11243. { //Release whatever was there already and reallocate memory
  11244. pet_lootitem_drop(pd, pd->master);
  11245. aFree(pd->loot->item);
  11246. }
  11247. else
  11248. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11249. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11250. pd->loot->max=max;
  11251. pd->loot->count = 0;
  11252. pd->loot->weight = 0;
  11253. return SCRIPT_CMD_SUCCESS;
  11254. }
  11255. /*==========================================
  11256. * Set arrays with info of all sd inventory :
  11257. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11258. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11259. * @inventorylist_card(0..3), @inventorylist_expire
  11260. * @inventorylist_count = scalar
  11261. *------------------------------------------*/
  11262. BUILDIN_FUNC(getinventorylist)
  11263. {
  11264. TBL_PC *sd=script_rid2sd(st);
  11265. char card_var[NAME_LENGTH];
  11266. int i,j=0,k;
  11267. if(!sd) return 0;
  11268. for(i=0;i<MAX_INVENTORY;i++){
  11269. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  11270. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  11271. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  11272. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  11273. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  11274. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  11275. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  11276. for (k = 0; k < MAX_SLOTS; k++)
  11277. {
  11278. sprintf(card_var, "@inventorylist_card%d",k+1);
  11279. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  11280. }
  11281. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  11282. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  11283. j++;
  11284. }
  11285. }
  11286. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11287. return SCRIPT_CMD_SUCCESS;
  11288. }
  11289. BUILDIN_FUNC(getskilllist)
  11290. {
  11291. TBL_PC *sd=script_rid2sd(st);
  11292. int i,j=0;
  11293. if(!sd) return 0;
  11294. for(i=0;i<MAX_SKILL;i++){
  11295. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11296. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11297. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11298. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11299. j++;
  11300. }
  11301. }
  11302. pc_setreg(sd,add_str("@skilllist_count"),j);
  11303. return SCRIPT_CMD_SUCCESS;
  11304. }
  11305. BUILDIN_FUNC(clearitem)
  11306. {
  11307. TBL_PC *sd=script_rid2sd(st);
  11308. int i;
  11309. if(sd==NULL) return 0;
  11310. for (i=0; i<MAX_INVENTORY; i++) {
  11311. if (sd->status.inventory[i].amount) {
  11312. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11313. }
  11314. }
  11315. return SCRIPT_CMD_SUCCESS;
  11316. }
  11317. /*==========================================
  11318. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11319. *------------------------------------------*/
  11320. BUILDIN_FUNC(disguise)
  11321. {
  11322. int id;
  11323. TBL_PC* sd = script_rid2sd(st);
  11324. if (sd == NULL) return 0;
  11325. id = script_getnum(st,2);
  11326. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11327. pc_disguise(sd, id);
  11328. script_pushint(st,id);
  11329. } else
  11330. script_pushint(st,0);
  11331. return SCRIPT_CMD_SUCCESS;
  11332. }
  11333. /*==========================================
  11334. * Undisguise Player (returns 1 if success, 0 on fail)
  11335. *------------------------------------------*/
  11336. BUILDIN_FUNC(undisguise)
  11337. {
  11338. TBL_PC* sd = script_rid2sd(st);
  11339. if (sd == NULL) return 0;
  11340. if (sd->disguise) {
  11341. pc_disguise(sd, 0);
  11342. script_pushint(st,0);
  11343. } else {
  11344. script_pushint(st,1);
  11345. }
  11346. return SCRIPT_CMD_SUCCESS;
  11347. }
  11348. /*==========================================
  11349. * Transform a bl to another _class,
  11350. * @type unused
  11351. *------------------------------------------*/
  11352. BUILDIN_FUNC(classchange)
  11353. {
  11354. int _class,type;
  11355. struct block_list *bl=map_id2bl(st->oid);
  11356. if(bl==NULL) return 0;
  11357. _class=script_getnum(st,2);
  11358. type=script_getnum(st,3);
  11359. clif_class_change(bl,_class,type);
  11360. return SCRIPT_CMD_SUCCESS;
  11361. }
  11362. /*==========================================
  11363. * Display an effect
  11364. *------------------------------------------*/
  11365. BUILDIN_FUNC(misceffect)
  11366. {
  11367. int type;
  11368. type=script_getnum(st,2);
  11369. if(st->oid && st->oid != fake_nd->bl.id) {
  11370. struct block_list *bl = map_id2bl(st->oid);
  11371. if (bl)
  11372. clif_specialeffect(bl,type,AREA);
  11373. } else{
  11374. TBL_PC *sd=script_rid2sd(st);
  11375. if(sd)
  11376. clif_specialeffect(&sd->bl,type,AREA);
  11377. }
  11378. return SCRIPT_CMD_SUCCESS;
  11379. }
  11380. /*==========================================
  11381. * Play a BGM on a single client [Rikter/Yommy]
  11382. *------------------------------------------*/
  11383. BUILDIN_FUNC(playBGM)
  11384. {
  11385. struct map_session_data* sd;
  11386. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11387. clif_playBGM(sd, script_getstr(st,2));
  11388. }
  11389. return SCRIPT_CMD_SUCCESS;
  11390. }
  11391. static int playBGM_sub(struct block_list* bl,va_list ap)
  11392. {
  11393. const char* name = va_arg(ap,const char*);
  11394. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11395. return 0;
  11396. }
  11397. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11398. {
  11399. const char* name = va_arg(args, const char*);
  11400. clif_playBGM(sd, name);
  11401. return 0;
  11402. }
  11403. /*==========================================
  11404. * Play a BGM on multiple client [Rikter/Yommy]
  11405. *------------------------------------------*/
  11406. BUILDIN_FUNC(playBGMall)
  11407. {
  11408. const char* name;
  11409. name = script_getstr(st,2);
  11410. if( script_hasdata(st,7) ) {// specified part of map
  11411. const char* mapname = script_getstr(st,3);
  11412. int x0 = script_getnum(st,4);
  11413. int y0 = script_getnum(st,5);
  11414. int x1 = script_getnum(st,6);
  11415. int y1 = script_getnum(st,7);
  11416. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  11417. }
  11418. else if( script_hasdata(st,3) ) {// entire map
  11419. const char* mapname = script_getstr(st,3);
  11420. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  11421. }
  11422. else {// entire server
  11423. map_foreachpc(&playBGM_foreachpc_sub, name);
  11424. }
  11425. return SCRIPT_CMD_SUCCESS;
  11426. }
  11427. /*==========================================
  11428. * Play a .wav sound for sd
  11429. *------------------------------------------*/
  11430. BUILDIN_FUNC(soundeffect)
  11431. {
  11432. TBL_PC* sd = script_rid2sd(st);
  11433. const char* name = script_getstr(st,2);
  11434. int type = script_getnum(st,3);
  11435. if(sd) {
  11436. clif_soundeffect(sd,&sd->bl,name,type);
  11437. }
  11438. return SCRIPT_CMD_SUCCESS;
  11439. }
  11440. int soundeffect_sub(struct block_list* bl,va_list ap)
  11441. {
  11442. char* name = va_arg(ap,char*);
  11443. int type = va_arg(ap,int);
  11444. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11445. return 0;
  11446. }
  11447. /*==========================================
  11448. * Play a sound effect (.wav) on multiple clients
  11449. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11450. *------------------------------------------*/
  11451. BUILDIN_FUNC(soundeffectall)
  11452. {
  11453. struct block_list* bl;
  11454. const char* name;
  11455. int type;
  11456. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11457. if (!bl)
  11458. return 0;
  11459. name = script_getstr(st,2);
  11460. type = script_getnum(st,3);
  11461. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11462. if(!script_hasdata(st,4))
  11463. { // area around
  11464. clif_soundeffectall(bl, name, type, AREA);
  11465. }
  11466. else
  11467. if(!script_hasdata(st,5))
  11468. { // entire map
  11469. const char* mapname = script_getstr(st,4);
  11470. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  11471. }
  11472. else
  11473. if(script_hasdata(st,8))
  11474. { // specified part of map
  11475. const char* mapname = script_getstr(st,4);
  11476. int x0 = script_getnum(st,5);
  11477. int y0 = script_getnum(st,6);
  11478. int x1 = script_getnum(st,7);
  11479. int y1 = script_getnum(st,8);
  11480. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  11481. }
  11482. else
  11483. {
  11484. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11485. }
  11486. return SCRIPT_CMD_SUCCESS;
  11487. }
  11488. /*==========================================
  11489. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11490. *------------------------------------------*/
  11491. BUILDIN_FUNC(petrecovery)
  11492. {
  11493. struct pet_data *pd;
  11494. TBL_PC *sd=script_rid2sd(st);
  11495. if(sd==NULL || sd->pd==NULL)
  11496. return 0;
  11497. pd=sd->pd;
  11498. if (pd->recovery)
  11499. { //Halt previous bonus
  11500. if (pd->recovery->timer != INVALID_TIMER)
  11501. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11502. } else //Init
  11503. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11504. pd->recovery->type = (sc_type)script_getnum(st,2);
  11505. pd->recovery->delay = script_getnum(st,3);
  11506. pd->recovery->timer = INVALID_TIMER;
  11507. return SCRIPT_CMD_SUCCESS;
  11508. }
  11509. /*==========================================
  11510. * pet healing [Valaris] //Rewritten by [Skotlex]
  11511. *------------------------------------------*/
  11512. BUILDIN_FUNC(petheal)
  11513. {
  11514. struct pet_data *pd;
  11515. TBL_PC *sd=script_rid2sd(st);
  11516. if(sd==NULL || sd->pd==NULL)
  11517. return 0;
  11518. pd=sd->pd;
  11519. if (pd->s_skill)
  11520. { //Clear previous skill
  11521. if (pd->s_skill->timer != INVALID_TIMER)
  11522. {
  11523. if (pd->s_skill->id)
  11524. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11525. else
  11526. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11527. }
  11528. } else //init memory
  11529. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11530. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11531. //Use the lv as the amount to heal
  11532. pd->s_skill->lv=script_getnum(st,2);
  11533. pd->s_skill->delay=script_getnum(st,3);
  11534. pd->s_skill->hp=script_getnum(st,4);
  11535. pd->s_skill->sp=script_getnum(st,5);
  11536. //Use delay as initial offset to avoid skill/heal exploits
  11537. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11538. pd->s_skill->timer = INVALID_TIMER;
  11539. else
  11540. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11541. return SCRIPT_CMD_SUCCESS;
  11542. }
  11543. /*==========================================
  11544. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11545. *------------------------------------------*/
  11546. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11547. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11548. BUILDIN_FUNC(petskillattack)
  11549. {
  11550. struct pet_data *pd;
  11551. struct script_data *data;
  11552. TBL_PC *sd=script_rid2sd(st);
  11553. if(sd==NULL || sd->pd==NULL)
  11554. return 0;
  11555. pd=sd->pd;
  11556. if (pd->a_skill == NULL)
  11557. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11558. data = script_getdata(st, 2);
  11559. get_val(st, data); // Convert into value in case of a variable
  11560. pd->a_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11561. pd->a_skill->lv=script_getnum(st,3);
  11562. pd->a_skill->div_ = 0;
  11563. pd->a_skill->rate=script_getnum(st,4);
  11564. pd->a_skill->bonusrate=script_getnum(st,5);
  11565. return SCRIPT_CMD_SUCCESS;
  11566. }
  11567. /*==========================================
  11568. * pet attack skills [Valaris]
  11569. *------------------------------------------*/
  11570. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  11571. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  11572. BUILDIN_FUNC(petskillattack2)
  11573. {
  11574. struct pet_data *pd;
  11575. struct script_data *data;
  11576. TBL_PC *sd=script_rid2sd(st);
  11577. if(sd==NULL || sd->pd==NULL)
  11578. return 0;
  11579. pd=sd->pd;
  11580. if (pd->a_skill == NULL)
  11581. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11582. data = script_getdata(st, 2);
  11583. get_val(st, data); // Convert into value in case of a variable
  11584. pd->a_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11585. pd->a_skill->lv=script_getnum(st,3);
  11586. pd->a_skill->div_ = script_getnum(st,4);
  11587. pd->a_skill->rate=script_getnum(st,5);
  11588. pd->a_skill->bonusrate=script_getnum(st,6);
  11589. return SCRIPT_CMD_SUCCESS;
  11590. }
  11591. /*==========================================
  11592. * pet support skills [Skotlex]
  11593. *------------------------------------------*/
  11594. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  11595. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  11596. BUILDIN_FUNC(petskillsupport)
  11597. {
  11598. struct pet_data *pd;
  11599. struct script_data *data;
  11600. TBL_PC *sd=script_rid2sd(st);
  11601. if(sd==NULL || sd->pd==NULL)
  11602. return 0;
  11603. pd=sd->pd;
  11604. if (pd->s_skill)
  11605. { //Clear previous skill
  11606. if (pd->s_skill->timer != INVALID_TIMER)
  11607. {
  11608. if (pd->s_skill->id)
  11609. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11610. else
  11611. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11612. }
  11613. } else //init memory
  11614. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11615. data = script_getdata(st, 2);
  11616. get_val(st, data); // Convert into value in case of a variable
  11617. pd->s_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11618. pd->s_skill->lv=script_getnum(st,3);
  11619. pd->s_skill->delay=script_getnum(st,4);
  11620. pd->s_skill->hp=script_getnum(st,5);
  11621. pd->s_skill->sp=script_getnum(st,6);
  11622. //Use delay as initial offset to avoid skill/heal exploits
  11623. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11624. pd->s_skill->timer = INVALID_TIMER;
  11625. else
  11626. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  11627. return SCRIPT_CMD_SUCCESS;
  11628. }
  11629. /*==========================================
  11630. * Scripted skill effects [Celest]
  11631. *------------------------------------------*/
  11632. /// skilleffect <skill id>,<level>
  11633. /// skilleffect "<skill name>",<level>
  11634. BUILDIN_FUNC(skilleffect)
  11635. {
  11636. TBL_PC *sd;
  11637. uint16 skill_id, skill_lv;
  11638. struct script_data *data = script_getdata(st, 2);
  11639. get_val(st, data); // Convert into value in case of a variable
  11640. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11641. skill_lv = script_getnum(st,3);
  11642. sd = script_rid2sd(st);
  11643. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  11644. * which leaves the server thinking it still is sitting. */
  11645. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  11646. skill_sit(sd, 0);
  11647. clif_standing(&sd->bl);
  11648. }
  11649. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  11650. return SCRIPT_CMD_SUCCESS;
  11651. }
  11652. /*==========================================
  11653. * NPC skill effects [Valaris]
  11654. *------------------------------------------*/
  11655. /// npcskilleffect <skill id>,<level>,<x>,<y>
  11656. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  11657. BUILDIN_FUNC(npcskilleffect)
  11658. {
  11659. struct block_list *bl= map_id2bl(st->oid);
  11660. uint16 skill_id, skill_lv;
  11661. int x, y;
  11662. struct script_data *data = script_getdata(st, 2);
  11663. get_val(st, data); // Convert into value in case of a variable
  11664. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11665. skill_lv=script_getnum(st,3);
  11666. x=script_getnum(st,4);
  11667. y=script_getnum(st,5);
  11668. if (bl)
  11669. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  11670. return SCRIPT_CMD_SUCCESS;
  11671. }
  11672. /*==========================================
  11673. * Special effects [Valaris]
  11674. *------------------------------------------*/
  11675. BUILDIN_FUNC(specialeffect)
  11676. {
  11677. struct block_list *bl=map_id2bl(st->oid);
  11678. int type = script_getnum(st,2);
  11679. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11680. if(bl==NULL)
  11681. return 0;
  11682. if( script_hasdata(st,4) )
  11683. {
  11684. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  11685. if(nd)
  11686. clif_specialeffect(&nd->bl, type, target);
  11687. }
  11688. else
  11689. {
  11690. if (target == SELF) {
  11691. TBL_PC *sd=script_rid2sd(st);
  11692. if (sd)
  11693. clif_specialeffect_single(bl,type,sd->fd);
  11694. } else {
  11695. clif_specialeffect(bl, type, target);
  11696. }
  11697. }
  11698. return SCRIPT_CMD_SUCCESS;
  11699. }
  11700. BUILDIN_FUNC(specialeffect2)
  11701. {
  11702. TBL_PC *sd=script_rid2sd(st);
  11703. int type = script_getnum(st,2);
  11704. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11705. if( script_hasdata(st,4) )
  11706. sd = map_nick2sd(script_getstr(st,4));
  11707. if (sd)
  11708. clif_specialeffect(&sd->bl, type, target);
  11709. return SCRIPT_CMD_SUCCESS;
  11710. }
  11711. /*==========================================
  11712. * Nude [Valaris]
  11713. *------------------------------------------*/
  11714. BUILDIN_FUNC(nude)
  11715. {
  11716. TBL_PC *sd = script_rid2sd(st);
  11717. int i, calcflag = 0;
  11718. if( sd == NULL )
  11719. return 0;
  11720. for( i = 0 ; i < EQI_MAX; i++ ) {
  11721. if( sd->equip_index[ i ] >= 0 ) {
  11722. if( !calcflag )
  11723. calcflag = 1;
  11724. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  11725. }
  11726. }
  11727. if( calcflag )
  11728. status_calc_pc(sd,SCO_NONE);
  11729. return SCRIPT_CMD_SUCCESS;
  11730. }
  11731. int atcommand_sub(struct script_state* st,int type) {
  11732. TBL_PC *sd, dummy_sd;
  11733. int fd;
  11734. const char *cmd;
  11735. cmd = script_getstr(st,2);
  11736. if (st->rid) {
  11737. sd = script_rid2sd(st);
  11738. fd = sd->fd;
  11739. } else { //Use a dummy character.
  11740. sd = &dummy_sd;
  11741. fd = 0;
  11742. memset(&dummy_sd, 0, sizeof(TBL_PC));
  11743. if (st->oid) {
  11744. struct block_list* bl = map_id2bl(st->oid);
  11745. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  11746. if (bl->type == BL_NPC)
  11747. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  11748. sd->mapindex = (bl->m > 0) ? bl->m : mapindex_name2id(MAP_DEFAULT);
  11749. }
  11750. // Init Group ID, Level, & permissions
  11751. sd->group_id = sd->group_level = 99;
  11752. sd->permissions |= PC_PERM_ALLPERMISSION;
  11753. }
  11754. if (!is_atcommand(fd, sd, cmd, type)) {
  11755. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  11756. script_reportsrc(st);
  11757. return SCRIPT_CMD_FAILURE;
  11758. }
  11759. return SCRIPT_CMD_SUCCESS;
  11760. }
  11761. /*==========================================
  11762. * gmcommand [MouseJstr]
  11763. *------------------------------------------*/
  11764. BUILDIN_FUNC(atcommand) {
  11765. return atcommand_sub(st,0);
  11766. }
  11767. /*==========================================
  11768. * Displays a message for the player only (like system messages like "you got an apple" )
  11769. *------------------------------------------*/
  11770. BUILDIN_FUNC(dispbottom)
  11771. {
  11772. TBL_PC *sd=script_rid2sd(st);
  11773. const char *message;
  11774. message=script_getstr(st,2);
  11775. if(sd)
  11776. clif_disp_onlyself(sd,message,(int)strlen(message));
  11777. return SCRIPT_CMD_SUCCESS;
  11778. }
  11779. /*===================================
  11780. * Heal portion of recovery command
  11781. *-----------------------------------*/
  11782. int recovery_sub(struct map_session_data* sd, int revive)
  11783. {
  11784. if(revive&(1|4) && pc_isdead(sd)) {
  11785. status_revive(&sd->bl, 100, 100);
  11786. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  11787. clif_specialeffect(&sd->bl, 77, AREA);
  11788. } else if(revive&(1|2) && !pc_isdead(sd)) {
  11789. status_percent_heal(&sd->bl, 100, 100);
  11790. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  11791. }
  11792. return SCRIPT_CMD_SUCCESS;
  11793. }
  11794. /*=========================================================================
  11795. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  11796. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  11797. * <type> determines <option>:
  11798. * 0 : char_id
  11799. * 1 : party_id
  11800. * 2 : guild_id
  11801. * 3 : map_name
  11802. * 4 : all characters
  11803. * <revive_flag>:
  11804. * 1 : Revive and heal all players (default)
  11805. * 2 : Heal living players only
  11806. * 4 : Revive dead players only
  11807. * <map name>:
  11808. * for types 1-2 : map_name (null = all maps)
  11809. *-------------------------------------------------------------------------*/
  11810. BUILDIN_FUNC(recovery)
  11811. {
  11812. TBL_PC *sd;
  11813. int map_idx = 0, type = 0, revive = 1;
  11814. type = script_getnum(st,2);
  11815. if(script_hasdata(st,4))
  11816. revive = script_getnum(st,4);
  11817. switch(type) {
  11818. case 0:
  11819. if(script_hasdata(st,3))
  11820. sd=map_charid2sd(script_getnum(st,3));
  11821. else if((sd = script_rid2sd(st)) == NULL)
  11822. return 0; //If we don't have sd by now, bail out
  11823. recovery_sub(sd, revive);
  11824. break;
  11825. case 1:
  11826. {
  11827. struct party_data* p;
  11828. //When no party given, we use invoker party
  11829. int p_id = 0, i;
  11830. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11831. map_idx = map_mapname2mapid(script_getstr(st,5));
  11832. if(map_idx < 1) { //But we'll check anyways
  11833. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11834. return 1;
  11835. }
  11836. }
  11837. if(script_hasdata(st,3))
  11838. p_id = script_getnum(st,3);
  11839. else if((sd = script_rid2sd(st)))
  11840. p_id = sd->status.party_id;
  11841. p = party_search(p_id);
  11842. if(p == NULL)
  11843. return 0;
  11844. for (i = 0; i < MAX_PARTY; i++) {
  11845. struct map_session_data* pl_sd;
  11846. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  11847. || (map_idx && pl_sd->bl.m != map_idx))
  11848. continue;
  11849. recovery_sub(pl_sd, revive);
  11850. }
  11851. break;
  11852. }
  11853. case 2:
  11854. {
  11855. struct guild* g;
  11856. //When no guild given, we use invoker guild
  11857. int g_id = 0, i;
  11858. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11859. map_idx = map_mapname2mapid(script_getstr(st,5));
  11860. if(map_idx < 1) { //But we'll check anyways
  11861. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11862. return 1;
  11863. }
  11864. }
  11865. if(script_hasdata(st,3))
  11866. g_id = script_getnum(st,3);
  11867. else if((sd = script_rid2sd(st)))
  11868. g_id = sd->status.guild_id;
  11869. g = guild_search(g_id);
  11870. if(g == NULL)
  11871. return 0;
  11872. for (i = 0; i < MAX_GUILD; i++) {
  11873. struct map_session_data* pl_sd;
  11874. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  11875. || (map_idx && pl_sd->bl.m != map_idx))
  11876. continue;
  11877. recovery_sub(pl_sd, revive);
  11878. }
  11879. break;
  11880. }
  11881. case 3:
  11882. if(script_hasdata(st,3))
  11883. map_idx = map_mapname2mapid(script_getstr(st,3));
  11884. else if((sd = script_rid2sd(st)))
  11885. map_idx = sd->bl.m;
  11886. if(map_idx < 1)
  11887. return 1; //No sd and no map given - return
  11888. case 4:
  11889. {
  11890. struct s_mapiterator *iter;
  11891. struct script_data *data = script_getdata(st, 3);
  11892. get_val(st, data); // Convert into value in case of a variable
  11893. if(script_hasdata(st,3) && !data_isstring(data))
  11894. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  11895. iter = mapit_getallusers();
  11896. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11897. if(type == 3 && sd->bl.m != map_idx)
  11898. continue;
  11899. recovery_sub(sd, revive);
  11900. }
  11901. mapit_free(iter);
  11902. break;
  11903. }
  11904. default:
  11905. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  11906. script_pushint(st,-1);
  11907. return 1;
  11908. }
  11909. script_pushint(st,1); //Successfully executed without errors
  11910. return SCRIPT_CMD_SUCCESS;
  11911. }
  11912. /*==========================================
  11913. * Get your pet info: getpetinfo(n)
  11914. * n -> 0:pet_id 1:pet_class 2:pet_name
  11915. * 3:friendly 4:hungry, 5: rename flag.6:level
  11916. *------------------------------------------*/
  11917. BUILDIN_FUNC(getpetinfo)
  11918. {
  11919. TBL_PC *sd=script_rid2sd(st);
  11920. TBL_PET *pd;
  11921. int type=script_getnum(st,2);
  11922. if(!sd || !sd->pd) {
  11923. if (type == 2)
  11924. script_pushconststr(st,"null");
  11925. else
  11926. script_pushint(st,0);
  11927. return 0;
  11928. }
  11929. pd = sd->pd;
  11930. switch(type){
  11931. case 0: script_pushint(st,pd->pet.pet_id); break;
  11932. case 1: script_pushint(st,pd->pet.class_); break;
  11933. case 2: script_pushstrcopy(st,pd->pet.name); break;
  11934. case 3: script_pushint(st,pd->pet.intimate); break;
  11935. case 4: script_pushint(st,pd->pet.hungry); break;
  11936. case 5: script_pushint(st,pd->pet.rename_flag); break;
  11937. case 6: script_pushint(st,(int)pd->pet.level); break;
  11938. default:
  11939. script_pushint(st,0);
  11940. break;
  11941. }
  11942. return SCRIPT_CMD_SUCCESS;
  11943. }
  11944. /*==========================================
  11945. * Get your homunculus info: gethominfo(n)
  11946. * n -> 0:hom_id 1:class 2:name
  11947. * 3:friendly 4:hungry, 5: rename flag.
  11948. * 6: level
  11949. *------------------------------------------*/
  11950. BUILDIN_FUNC(gethominfo)
  11951. {
  11952. TBL_PC *sd=script_rid2sd(st);
  11953. TBL_HOM *hd;
  11954. int type=script_getnum(st,2);
  11955. hd = sd?sd->hd:NULL;
  11956. if(!hd) {
  11957. if (type == 2)
  11958. script_pushconststr(st,"null");
  11959. else
  11960. script_pushint(st,0);
  11961. return 0;
  11962. }
  11963. switch(type){
  11964. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  11965. case 1: script_pushint(st,hd->homunculus.class_); break;
  11966. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  11967. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  11968. case 4: script_pushint(st,hd->homunculus.hunger); break;
  11969. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  11970. case 6: script_pushint(st,hd->homunculus.level); break;
  11971. default:
  11972. script_pushint(st,0);
  11973. break;
  11974. }
  11975. return SCRIPT_CMD_SUCCESS;
  11976. }
  11977. /// Retrieves information about character's mercenary
  11978. /// getmercinfo <type>[,<char id>];
  11979. BUILDIN_FUNC(getmercinfo)
  11980. {
  11981. int type;
  11982. struct map_session_data* sd;
  11983. struct mercenary_data* md;
  11984. type = script_getnum(st,2);
  11985. if( script_hasdata(st,3) )
  11986. {
  11987. int char_id = script_getnum(st,3);
  11988. if( ( sd = map_charid2sd(char_id) ) == NULL )
  11989. {
  11990. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  11991. script_pushnil(st);
  11992. return 1;
  11993. }
  11994. }
  11995. else
  11996. {
  11997. if( ( sd = script_rid2sd(st) ) == NULL )
  11998. {
  11999. script_pushnil(st);
  12000. return 0;
  12001. }
  12002. }
  12003. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12004. switch( type )
  12005. {
  12006. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12007. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12008. case 2:
  12009. if( md )
  12010. script_pushstrcopy(st,md->db->name);
  12011. else
  12012. script_pushconststr(st,"");
  12013. break;
  12014. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12015. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12016. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12017. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12018. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12019. default:
  12020. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12021. script_pushnil(st);
  12022. return 1;
  12023. }
  12024. return SCRIPT_CMD_SUCCESS;
  12025. }
  12026. /*==========================================
  12027. * Shows wether your inventory(and equips) contain
  12028. selected card or not.
  12029. checkequipedcard(4001);
  12030. *------------------------------------------*/
  12031. BUILDIN_FUNC(checkequipedcard)
  12032. {
  12033. TBL_PC *sd=script_rid2sd(st);
  12034. if(sd){
  12035. int n,i,c=0;
  12036. c=script_getnum(st,2);
  12037. for(i=0;i<MAX_INVENTORY;i++){
  12038. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  12039. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  12040. continue;
  12041. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12042. if(sd->status.inventory[i].card[n]==c){
  12043. script_pushint(st,1);
  12044. return 0;
  12045. }
  12046. }
  12047. }
  12048. }
  12049. }
  12050. script_pushint(st,0);
  12051. return SCRIPT_CMD_SUCCESS;
  12052. }
  12053. BUILDIN_FUNC(jump_zero)
  12054. {
  12055. int sel;
  12056. sel=script_getnum(st,2);
  12057. if(!sel) {
  12058. int pos;
  12059. if( !data_islabel(script_getdata(st,3)) ){
  12060. ShowError("script: jump_zero: not label !\n");
  12061. st->state=END;
  12062. return 1;
  12063. }
  12064. pos=script_getnum(st,3);
  12065. st->pos=pos;
  12066. st->state=GOTO;
  12067. }
  12068. return SCRIPT_CMD_SUCCESS;
  12069. }
  12070. /*==========================================
  12071. * movenpc [MouseJstr]
  12072. *------------------------------------------*/
  12073. BUILDIN_FUNC(movenpc)
  12074. {
  12075. TBL_NPC *nd = NULL;
  12076. const char *npc;
  12077. int x,y;
  12078. npc = script_getstr(st,2);
  12079. x = script_getnum(st,3);
  12080. y = script_getnum(st,4);
  12081. if ((nd = npc_name2id(npc)) == NULL)
  12082. return -1;
  12083. if (script_hasdata(st,5))
  12084. nd->ud.dir = script_getnum(st,5) % 8;
  12085. npc_movenpc(nd, x, y);
  12086. return SCRIPT_CMD_SUCCESS;
  12087. }
  12088. /*==========================================
  12089. * message [MouseJstr]
  12090. *------------------------------------------*/
  12091. BUILDIN_FUNC(message)
  12092. {
  12093. const char *msg,*player;
  12094. TBL_PC *pl_sd = NULL;
  12095. player = script_getstr(st,2);
  12096. msg = script_getstr(st,3);
  12097. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  12098. return 0;
  12099. clif_displaymessage(pl_sd->fd, msg);
  12100. return SCRIPT_CMD_SUCCESS;
  12101. }
  12102. /*==========================================
  12103. * npctalk (sends message to surrounding area)
  12104. *------------------------------------------*/
  12105. BUILDIN_FUNC(npctalk)
  12106. {
  12107. const char* str;
  12108. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  12109. str = script_getstr(st,2);
  12110. if(nd)
  12111. {
  12112. char name[NAME_LENGTH], message[256];
  12113. safestrncpy(name, nd->name, sizeof(name));
  12114. strtok(name, "#"); // discard extra name identifier if present
  12115. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  12116. clif_disp_overhead(&nd->bl, message);
  12117. }
  12118. return SCRIPT_CMD_SUCCESS;
  12119. }
  12120. // change npc walkspeed [Valaris]
  12121. BUILDIN_FUNC(npcspeed)
  12122. {
  12123. struct npc_data* nd;
  12124. int speed;
  12125. speed = script_getnum(st,2);
  12126. nd =(struct npc_data *)map_id2bl(st->oid);
  12127. if( nd ) {
  12128. nd->speed = speed;
  12129. nd->ud.state.speed_changed = 1;
  12130. }
  12131. return SCRIPT_CMD_SUCCESS;
  12132. }
  12133. // make an npc walk to a position [Valaris]
  12134. BUILDIN_FUNC(npcwalkto)
  12135. {
  12136. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12137. int x=0,y=0;
  12138. x=script_getnum(st,2);
  12139. y=script_getnum(st,3);
  12140. if(nd) {
  12141. if (!nd->status.hp)
  12142. status_calc_npc(nd, SCO_FIRST);
  12143. else
  12144. status_calc_npc(nd, SCO_NONE);
  12145. unit_walktoxy(&nd->bl,x,y,0);
  12146. }
  12147. return SCRIPT_CMD_SUCCESS;
  12148. }
  12149. // stop an npc's movement [Valaris]
  12150. BUILDIN_FUNC(npcstop)
  12151. {
  12152. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12153. if(nd) {
  12154. unit_stop_walking(&nd->bl,1|4);
  12155. }
  12156. return SCRIPT_CMD_SUCCESS;
  12157. }
  12158. /*==========================================
  12159. * getlook char info. getlook(arg)
  12160. *------------------------------------------*/
  12161. BUILDIN_FUNC(getlook)
  12162. {
  12163. int type,val;
  12164. TBL_PC *sd;
  12165. sd=script_rid2sd(st);
  12166. type=script_getnum(st,2);
  12167. val=-1;
  12168. switch(type) {
  12169. case LOOK_HAIR: val=sd->status.hair; break; //1
  12170. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12171. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12172. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12173. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12174. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12175. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12176. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12177. case LOOK_SHOES: break; //9
  12178. case LOOK_ROBE: val=sd->status.robe; break; //12
  12179. }
  12180. script_pushint(st,val);
  12181. return SCRIPT_CMD_SUCCESS;
  12182. }
  12183. /*==========================================
  12184. * get char save point. argument: 0- map name, 1- x, 2- y
  12185. *------------------------------------------*/
  12186. BUILDIN_FUNC(getsavepoint)
  12187. {
  12188. TBL_PC* sd;
  12189. int type;
  12190. sd = script_rid2sd(st);
  12191. if (sd == NULL) {
  12192. script_pushint(st,0);
  12193. return 0;
  12194. }
  12195. type = script_getnum(st,2);
  12196. switch(type) {
  12197. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12198. case 1: script_pushint(st,sd->status.save_point.x); break;
  12199. case 2: script_pushint(st,sd->status.save_point.y); break;
  12200. default:
  12201. script_pushint(st,0);
  12202. break;
  12203. }
  12204. return SCRIPT_CMD_SUCCESS;
  12205. }
  12206. /*==========================================
  12207. * Get position for char/npc/pet/mob objects. Added by Lorky
  12208. *
  12209. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  12210. * where type:
  12211. * MapName$ - String variable for output map name
  12212. * MapX - Integer variable for output coord X
  12213. * MapY - Integer variable for output coord Y
  12214. * type - type of object
  12215. * 0 - Character coord
  12216. * 1 - NPC coord
  12217. * 2 - Pet coord
  12218. * 3 - Mob coord (not released)
  12219. * 4 - Homun coord
  12220. * 5 - Mercenary coord
  12221. * 6 - Elemental coord
  12222. * CharName$ - Name object. If miss or "this" the current object
  12223. *
  12224. * Return:
  12225. * 0 - success
  12226. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  12227. *------------------------------------------*/
  12228. BUILDIN_FUNC(getmapxy)
  12229. {
  12230. struct block_list *bl = NULL;
  12231. TBL_PC *sd=NULL;
  12232. int num;
  12233. const char *name;
  12234. char prefix;
  12235. int x,y,type;
  12236. char mapname[MAP_NAME_LENGTH];
  12237. if( !data_isreference(script_getdata(st,2)) ){
  12238. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  12239. script_pushint(st,-1);
  12240. return 1;
  12241. }
  12242. if( !data_isreference(script_getdata(st,3)) ){
  12243. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  12244. script_pushint(st,-1);
  12245. return 1;
  12246. }
  12247. if( !data_isreference(script_getdata(st,4)) ){
  12248. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  12249. script_pushint(st,-1);
  12250. return 1;
  12251. }
  12252. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12253. type=script_getnum(st,5);
  12254. switch (type){
  12255. case 0: //Get Character Position
  12256. if( script_hasdata(st,6) )
  12257. sd=map_nick2sd(script_getstr(st,6));
  12258. else
  12259. sd=script_rid2sd(st);
  12260. if (sd)
  12261. bl = &sd->bl;
  12262. break;
  12263. case 1: //Get NPC Position
  12264. if( script_hasdata(st,6) )
  12265. {
  12266. struct npc_data *nd;
  12267. nd=npc_name2id(script_getstr(st,6));
  12268. if (nd)
  12269. bl = &nd->bl;
  12270. } else //In case the origin is not an npc?
  12271. bl=map_id2bl(st->oid);
  12272. break;
  12273. case 2: //Get Pet Position
  12274. if(script_hasdata(st,6))
  12275. sd=map_nick2sd(script_getstr(st,6));
  12276. else
  12277. sd=script_rid2sd(st);
  12278. if (sd && sd->pd)
  12279. bl = &sd->pd->bl;
  12280. break;
  12281. case 3: //Get Mob Position
  12282. break; //Not supported?
  12283. case 4: //Get Homun Position
  12284. if(script_hasdata(st,6))
  12285. sd=map_nick2sd(script_getstr(st,6));
  12286. else
  12287. sd=script_rid2sd(st);
  12288. if (sd && sd->hd)
  12289. bl = &sd->hd->bl;
  12290. break;
  12291. case 5: //Get Mercenary Position
  12292. if(script_hasdata(st,6))
  12293. sd=map_nick2sd(script_getstr(st,6));
  12294. else
  12295. sd=script_rid2sd(st);
  12296. if (sd && sd->md)
  12297. bl = &sd->md->bl;
  12298. break;
  12299. case 6: //Get Elemental Position
  12300. if(script_hasdata(st,6))
  12301. sd=map_nick2sd(script_getstr(st,6));
  12302. else
  12303. sd=script_rid2sd(st);
  12304. if (sd && sd->ed)
  12305. bl = &sd->ed->bl;
  12306. break;
  12307. default:
  12308. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  12309. script_pushint(st,-1);
  12310. return 1;
  12311. }
  12312. if (!bl) { //No object found.
  12313. script_pushint(st,-1);
  12314. return 0;
  12315. }
  12316. x= bl->x;
  12317. y= bl->y;
  12318. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12319. //Set MapName$
  12320. num=st->stack->stack_data[st->start+2].u.num;
  12321. name=get_str(num&0x00ffffff);
  12322. prefix=*name;
  12323. if(not_server_variable(prefix))
  12324. sd=script_rid2sd(st);
  12325. else
  12326. sd=NULL;
  12327. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12328. //Set MapX
  12329. num=st->stack->stack_data[st->start+3].u.num;
  12330. name=get_str(num&0x00ffffff);
  12331. prefix=*name;
  12332. if(not_server_variable(prefix))
  12333. sd=script_rid2sd(st);
  12334. else
  12335. sd=NULL;
  12336. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12337. //Set MapY
  12338. num=st->stack->stack_data[st->start+4].u.num;
  12339. name=get_str(num&0x00ffffff);
  12340. prefix=*name;
  12341. if(not_server_variable(prefix))
  12342. sd=script_rid2sd(st);
  12343. else
  12344. sd=NULL;
  12345. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12346. //Return Success value
  12347. script_pushint(st,0);
  12348. return SCRIPT_CMD_SUCCESS;
  12349. }
  12350. /*==========================================
  12351. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12352. *------------------------------------------*/
  12353. BUILDIN_FUNC(logmes)
  12354. {
  12355. const char *str;
  12356. TBL_PC* sd;
  12357. sd = script_rid2sd(st);
  12358. if( sd == NULL )
  12359. return 1;
  12360. str = script_getstr(st,2);
  12361. log_npc(sd,str);
  12362. return SCRIPT_CMD_SUCCESS;
  12363. }
  12364. BUILDIN_FUNC(summon)
  12365. {
  12366. int _class, timeout=0;
  12367. const char *str,*event="";
  12368. TBL_PC *sd;
  12369. struct mob_data *md;
  12370. int tick = gettick();
  12371. sd=script_rid2sd(st);
  12372. if (!sd) return 0;
  12373. str =script_getstr(st,2);
  12374. _class=script_getnum(st,3);
  12375. if( script_hasdata(st,4) )
  12376. timeout=script_getnum(st,4);
  12377. if( script_hasdata(st,5) ){
  12378. event=script_getstr(st,5);
  12379. check_event(st, event);
  12380. }
  12381. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12382. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12383. if (md) {
  12384. md->master_id=sd->bl.id;
  12385. md->special_state.ai = AI_ATTACK;
  12386. if( md->deletetimer != INVALID_TIMER )
  12387. delete_timer(md->deletetimer, mob_timer_delete);
  12388. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12389. mob_spawn (md); //Now it is ready for spawning.
  12390. clif_specialeffect(&md->bl,344,AREA);
  12391. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12392. }
  12393. return SCRIPT_CMD_SUCCESS;
  12394. }
  12395. /*==========================================
  12396. * Checks whether it is daytime/nighttime
  12397. *------------------------------------------*/
  12398. BUILDIN_FUNC(isnight)
  12399. {
  12400. script_pushint(st,(night_flag == 1));
  12401. return SCRIPT_CMD_SUCCESS;
  12402. }
  12403. BUILDIN_FUNC(isday)
  12404. {
  12405. script_pushint(st,(night_flag == 0));
  12406. return SCRIPT_CMD_SUCCESS;
  12407. }
  12408. /*================================================
  12409. * Check how many items/cards in the list are
  12410. * equipped - used for 2/15's cards patch [celest]
  12411. *------------------------------------------------*/
  12412. BUILDIN_FUNC(isequippedcnt)
  12413. {
  12414. TBL_PC *sd;
  12415. int i, id = 1;
  12416. int ret = 0;
  12417. sd = script_rid2sd(st);
  12418. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12419. script_pushint(st,0);
  12420. return 0;
  12421. }
  12422. for (i=0; id!=0; i++) {
  12423. short j;
  12424. FETCH (i+2, id) else id = 0;
  12425. if (id <= 0)
  12426. continue;
  12427. for (j=0; j<EQI_MAX; j++) {
  12428. short index = sd->equip_index[j];
  12429. if(index < 0)
  12430. continue;
  12431. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  12432. continue;
  12433. if(!sd->inventory_data[index])
  12434. continue;
  12435. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  12436. if (sd->inventory_data[index]->nameid == id)
  12437. ret+= sd->status.inventory[index].amount;
  12438. } else { //Count cards.
  12439. short k;
  12440. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12441. continue; //No cards
  12442. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12443. if (sd->status.inventory[index].card[k] == id)
  12444. ret++; //[Lupus]
  12445. }
  12446. }
  12447. }
  12448. }
  12449. script_pushint(st,ret);
  12450. return SCRIPT_CMD_SUCCESS;
  12451. }
  12452. /*================================================
  12453. * Check whether another card has been
  12454. * equipped - used for 2/15's cards patch [celest]
  12455. * -- Items checked cannot be reused in another
  12456. * card set to prevent exploits
  12457. *------------------------------------------------*/
  12458. BUILDIN_FUNC(isequipped)
  12459. {
  12460. TBL_PC *sd;
  12461. int i, id = 1;
  12462. int ret = -1;
  12463. //Original hash to reverse it when full check fails.
  12464. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  12465. sd = script_rid2sd(st);
  12466. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12467. script_pushint(st,0);
  12468. return SCRIPT_CMD_SUCCESS;
  12469. }
  12470. setitem_hash = sd->bonus.setitem_hash;
  12471. setitem_hash2 = sd->bonus.setitem_hash2;
  12472. for (i=0; id!=0; i++) {
  12473. int flag = 0;
  12474. short j;
  12475. FETCH (i+2, id) else id = 0;
  12476. if (id <= 0)
  12477. continue;
  12478. for (j=0; j<EQI_MAX; j++) {
  12479. short index = sd->equip_index[j];
  12480. if(index < 0)
  12481. continue;
  12482. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  12483. continue;
  12484. if(!sd->inventory_data[index])
  12485. continue;
  12486. if (itemdb_type(id) != IT_CARD) {
  12487. if (sd->inventory_data[index]->nameid != id)
  12488. continue;
  12489. flag = 1;
  12490. break;
  12491. } else { //Cards
  12492. short k;
  12493. if (sd->inventory_data[index]->slot == 0 ||
  12494. itemdb_isspecial(sd->status.inventory[index].card[0]))
  12495. continue;
  12496. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  12497. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  12498. unsigned int hash = 0;
  12499. if (sd->status.inventory[index].card[k] != id)
  12500. continue;
  12501. hash = 1<<((j<5?j:j-5)*4 + k);
  12502. // check if card is already used by another set
  12503. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  12504. continue;
  12505. // We have found a match
  12506. flag = 1;
  12507. // Set hash so this card cannot be used by another
  12508. if (j<5)
  12509. sd->bonus.setitem_hash |= hash;
  12510. else
  12511. sd->bonus.setitem_hash2 |= hash;
  12512. break;
  12513. }
  12514. }
  12515. if (flag) break; //Card found
  12516. }
  12517. if (ret == -1)
  12518. ret = flag;
  12519. else
  12520. ret &= flag;
  12521. if (!ret) break;
  12522. }
  12523. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  12524. sd->bonus.setitem_hash = setitem_hash;
  12525. sd->bonus.setitem_hash2 = setitem_hash2;
  12526. }
  12527. script_pushint(st,ret);
  12528. return SCRIPT_CMD_SUCCESS;
  12529. }
  12530. /*================================================
  12531. * Check how many given inserted cards in the CURRENT
  12532. * weapon - used for 2/15's cards patch [Lupus]
  12533. *------------------------------------------------*/
  12534. BUILDIN_FUNC(cardscnt)
  12535. {
  12536. TBL_PC *sd;
  12537. int i, k, id = 1;
  12538. int ret = 0;
  12539. int index;
  12540. sd = script_rid2sd(st);
  12541. for (i=0; id!=0; i++) {
  12542. FETCH (i+2, id) else id = 0;
  12543. if (id <= 0)
  12544. continue;
  12545. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  12546. if(index < 0) continue;
  12547. if(!sd->inventory_data[index])
  12548. continue;
  12549. if(itemdb_type(id) != IT_CARD) {
  12550. if (sd->inventory_data[index]->nameid == id)
  12551. ret+= sd->status.inventory[index].amount;
  12552. } else {
  12553. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12554. continue;
  12555. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12556. if (sd->status.inventory[index].card[k] == id)
  12557. ret++;
  12558. }
  12559. }
  12560. }
  12561. script_pushint(st,ret);
  12562. // script_pushint(st,current_equip_item_index);
  12563. return SCRIPT_CMD_SUCCESS;
  12564. }
  12565. /*=======================================================
  12566. * Returns the refined number of the current item, or an
  12567. * item with inventory index specified
  12568. *-------------------------------------------------------*/
  12569. BUILDIN_FUNC(getrefine)
  12570. {
  12571. TBL_PC *sd;
  12572. if ((sd = script_rid2sd(st))!= NULL)
  12573. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  12574. else
  12575. script_pushint(st,0);
  12576. return SCRIPT_CMD_SUCCESS;
  12577. }
  12578. /*=======================================================
  12579. * Day/Night controls
  12580. *-------------------------------------------------------*/
  12581. BUILDIN_FUNC(night)
  12582. {
  12583. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  12584. return SCRIPT_CMD_SUCCESS;
  12585. }
  12586. BUILDIN_FUNC(day)
  12587. {
  12588. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  12589. return SCRIPT_CMD_SUCCESS;
  12590. }
  12591. //=======================================================
  12592. // Unequip [Spectre]
  12593. //-------------------------------------------------------
  12594. BUILDIN_FUNC(unequip) {
  12595. int pos;
  12596. TBL_PC *sd;
  12597. if (!(sd = script_rid2sd(st)))
  12598. return SCRIPT_CMD_SUCCESS;
  12599. pos = script_getnum(st,2);
  12600. if (pos >= 1 && pos <= ARRAYLENGTH(equip)) {
  12601. short i = pc_checkequip(sd,equip[pos-1]);
  12602. if (i >= 0) {
  12603. pc_unequipitem(sd,i,1|2);
  12604. script_pushint(st, 1);
  12605. return SCRIPT_CMD_SUCCESS;
  12606. }
  12607. }
  12608. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  12609. script_pushint(st, 0);
  12610. return SCRIPT_CMD_FAILURE;
  12611. }
  12612. BUILDIN_FUNC(equip) {
  12613. unsigned short nameid = 0;
  12614. TBL_PC *sd;
  12615. struct item_data *item_data;
  12616. if (!(sd = script_rid2sd(st)))
  12617. return SCRIPT_CMD_SUCCESS;
  12618. nameid = script_getnum(st,2);
  12619. if ((item_data = itemdb_exists(nameid))) {
  12620. int i;
  12621. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  12622. if (i < MAX_INVENTORY) {
  12623. pc_equipitem(sd,i,item_data->equip);
  12624. script_pushint(st,1);
  12625. return SCRIPT_CMD_SUCCESS;
  12626. }
  12627. }
  12628. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  12629. script_pushint(st,0);
  12630. return SCRIPT_CMD_FAILURE;
  12631. }
  12632. BUILDIN_FUNC(autoequip)
  12633. {
  12634. unsigned short nameid;
  12635. int flag;
  12636. struct item_data *item_data;
  12637. nameid=script_getnum(st,2);
  12638. flag=script_getnum(st,3);
  12639. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  12640. {
  12641. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  12642. return SCRIPT_CMD_FAILURE;
  12643. }
  12644. if( !itemdb_isequip2(item_data) )
  12645. {
  12646. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  12647. return SCRIPT_CMD_FAILURE;
  12648. }
  12649. item_data->flag.autoequip = flag>0?1:0;
  12650. return SCRIPT_CMD_SUCCESS;
  12651. }
  12652. BUILDIN_FUNC(setbattleflag)
  12653. {
  12654. const char *flag, *value;
  12655. flag = script_getstr(st,2);
  12656. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  12657. if (battle_set_value(flag, value) == 0)
  12658. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  12659. else
  12660. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  12661. return SCRIPT_CMD_SUCCESS;
  12662. }
  12663. BUILDIN_FUNC(getbattleflag)
  12664. {
  12665. const char *flag;
  12666. flag = script_getstr(st,2);
  12667. script_pushint(st,battle_get_value(flag));
  12668. return SCRIPT_CMD_SUCCESS;
  12669. }
  12670. //=======================================================
  12671. // strlen [Valaris]
  12672. //-------------------------------------------------------
  12673. BUILDIN_FUNC(getstrlen)
  12674. {
  12675. const char *str = script_getstr(st,2);
  12676. int len = (str) ? (int)strlen(str) : 0;
  12677. script_pushint(st,len);
  12678. return SCRIPT_CMD_SUCCESS;
  12679. }
  12680. //=======================================================
  12681. // isalpha [Valaris]
  12682. //-------------------------------------------------------
  12683. BUILDIN_FUNC(charisalpha)
  12684. {
  12685. const char *str=script_getstr(st,2);
  12686. int pos=script_getnum(st,3);
  12687. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  12688. script_pushint(st,val);
  12689. return SCRIPT_CMD_SUCCESS;
  12690. }
  12691. //=======================================================
  12692. // charisupper <str>, <index>
  12693. //-------------------------------------------------------
  12694. BUILDIN_FUNC(charisupper)
  12695. {
  12696. const char *str = script_getstr(st,2);
  12697. int pos = script_getnum(st,3);
  12698. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  12699. script_pushint(st,val);
  12700. return SCRIPT_CMD_SUCCESS;
  12701. }
  12702. //=======================================================
  12703. // charislower <str>, <index>
  12704. //-------------------------------------------------------
  12705. BUILDIN_FUNC(charislower)
  12706. {
  12707. const char *str = script_getstr(st,2);
  12708. int pos = script_getnum(st,3);
  12709. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  12710. script_pushint(st,val);
  12711. return SCRIPT_CMD_SUCCESS;
  12712. }
  12713. //=======================================================
  12714. // charat <str>, <index>
  12715. //-------------------------------------------------------
  12716. BUILDIN_FUNC(charat) {
  12717. const char *str = script_getstr(st,2);
  12718. int pos = script_getnum(st,3);
  12719. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  12720. char output[2];
  12721. output[0] = str[pos];
  12722. output[1] = '\0';
  12723. script_pushstrcopy(st, output);
  12724. } else
  12725. script_pushconststr(st, "");
  12726. return SCRIPT_CMD_SUCCESS;
  12727. }
  12728. //=======================================================
  12729. // setchar <string>, <char>, <index>
  12730. //-------------------------------------------------------
  12731. BUILDIN_FUNC(setchar)
  12732. {
  12733. const char *str = script_getstr(st,2);
  12734. const char *c = script_getstr(st,3);
  12735. int index = script_getnum(st,4);
  12736. char *output = aStrdup(str);
  12737. if(index >= 0 && index < strlen(output))
  12738. output[index] = *c;
  12739. script_pushstr(st, output);
  12740. return SCRIPT_CMD_SUCCESS;
  12741. }
  12742. //=======================================================
  12743. // insertchar <string>, <char>, <index>
  12744. //-------------------------------------------------------
  12745. BUILDIN_FUNC(insertchar)
  12746. {
  12747. const char *str = script_getstr(st,2);
  12748. const char *c = script_getstr(st,3);
  12749. int index = script_getnum(st,4);
  12750. char *output;
  12751. size_t len = strlen(str);
  12752. if(index < 0)
  12753. index = 0;
  12754. else if(index > len)
  12755. index = len;
  12756. output = (char*)aMalloc(len + 2);
  12757. memcpy(output, str, index);
  12758. output[index] = c[0];
  12759. memcpy(&output[index+1], &str[index], len - index);
  12760. output[len+1] = '\0';
  12761. script_pushstr(st, output);
  12762. return SCRIPT_CMD_SUCCESS;
  12763. }
  12764. //=======================================================
  12765. // delchar <string>, <index>
  12766. //-------------------------------------------------------
  12767. BUILDIN_FUNC(delchar)
  12768. {
  12769. const char *str = script_getstr(st,2);
  12770. int index = script_getnum(st,3);
  12771. char *output;
  12772. size_t len = strlen(str);
  12773. if(index < 0 || index > len) {
  12774. //return original
  12775. output = aStrdup(str);
  12776. script_pushstr(st, output);
  12777. return 0;
  12778. }
  12779. output = (char*)aMalloc(len);
  12780. memcpy(output, str, index);
  12781. memcpy(&output[index], &str[index+1], len - index);
  12782. script_pushstr(st, output);
  12783. return SCRIPT_CMD_SUCCESS;
  12784. }
  12785. //=======================================================
  12786. // strtoupper <str>
  12787. //-------------------------------------------------------
  12788. BUILDIN_FUNC(strtoupper)
  12789. {
  12790. const char *str = script_getstr(st,2);
  12791. char *output = aStrdup(str);
  12792. char *cursor = output;
  12793. while (*cursor != '\0') {
  12794. *cursor = TOUPPER(*cursor);
  12795. cursor++;
  12796. }
  12797. script_pushstr(st, output);
  12798. return SCRIPT_CMD_SUCCESS;
  12799. }
  12800. //=======================================================
  12801. // strtolower <str>
  12802. //-------------------------------------------------------
  12803. BUILDIN_FUNC(strtolower)
  12804. {
  12805. const char *str = script_getstr(st,2);
  12806. char *output = aStrdup(str);
  12807. char *cursor = output;
  12808. while (*cursor != '\0') {
  12809. *cursor = TOLOWER(*cursor);
  12810. cursor++;
  12811. }
  12812. script_pushstr(st, output);
  12813. return SCRIPT_CMD_SUCCESS;
  12814. }
  12815. //=======================================================
  12816. // substr <str>, <start>, <end>
  12817. //-------------------------------------------------------
  12818. BUILDIN_FUNC(substr)
  12819. {
  12820. const char *str = script_getstr(st,2);
  12821. char *output;
  12822. int start = script_getnum(st,3);
  12823. int end = script_getnum(st,4);
  12824. int len = 0;
  12825. if(start >= 0 && end < strlen(str) && start <= end) {
  12826. len = end - start + 1;
  12827. output = (char*)aMalloc(len + 1);
  12828. memcpy(output, &str[start], len);
  12829. } else
  12830. output = (char*)aMalloc(1);
  12831. output[len] = '\0';
  12832. script_pushstr(st, output);
  12833. return SCRIPT_CMD_SUCCESS;
  12834. }
  12835. //=======================================================
  12836. // explode <dest_string_array>, <str>, <delimiter>
  12837. // Note: delimiter is limited to 1 char
  12838. //-------------------------------------------------------
  12839. BUILDIN_FUNC(explode)
  12840. {
  12841. struct script_data* data = script_getdata(st, 2);
  12842. const char *str = script_getstr(st,3);
  12843. const char delimiter = script_getstr(st, 4)[0];
  12844. int32 id;
  12845. size_t len = strlen(str);
  12846. int i = 0, j = 0;
  12847. int start;
  12848. char *temp;
  12849. const char* name;
  12850. TBL_PC* sd = NULL;
  12851. temp = (char*)aMalloc(len + 1);
  12852. if( !data_isreference(data) ) {
  12853. ShowError("script:explode: not a variable\n");
  12854. script_reportdata(data);
  12855. st->state = END;
  12856. return 1;// not a variable
  12857. }
  12858. id = reference_getid(data);
  12859. start = reference_getindex(data);
  12860. name = reference_getname(data);
  12861. if( not_array_variable(*name) ) {
  12862. ShowError("script:explode: illegal scope\n");
  12863. script_reportdata(data);
  12864. st->state = END;
  12865. return 1;// not supported
  12866. }
  12867. if( !is_string_variable(name) ) {
  12868. ShowError("script:explode: not string array\n");
  12869. script_reportdata(data);
  12870. st->state = END;
  12871. return 1;// data type mismatch
  12872. }
  12873. if( not_server_variable(*name) ) {
  12874. sd = script_rid2sd(st);
  12875. if( sd == NULL )
  12876. return 0;// no player attached
  12877. }
  12878. while(str[i] != '\0') {
  12879. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  12880. temp[j] = '\0';
  12881. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  12882. j = 0;
  12883. ++i;
  12884. } else {
  12885. temp[j++] = str[i++];
  12886. }
  12887. }
  12888. //set last string
  12889. temp[j] = '\0';
  12890. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  12891. aFree(temp);
  12892. return SCRIPT_CMD_SUCCESS;
  12893. }
  12894. //=======================================================
  12895. // implode <string_array>
  12896. // implode <string_array>, <glue>
  12897. //-------------------------------------------------------
  12898. BUILDIN_FUNC(implode)
  12899. {
  12900. struct script_data* data = script_getdata(st, 2);
  12901. const char *name;
  12902. int32 glue_len = 0, array_size, id;
  12903. char *output;
  12904. if( !data_isreference(data) ) {
  12905. ShowError("script:implode: not a variable\n");
  12906. script_reportdata(data);
  12907. st->state = END;
  12908. return 1;// not a variable
  12909. }
  12910. id = reference_getid(data);
  12911. name = reference_getname(data);
  12912. if( not_array_variable(*name) ) {
  12913. ShowError("script:implode: illegal scope\n");
  12914. script_reportdata(data);
  12915. st->state = END;
  12916. return 1;// not supported
  12917. }
  12918. if( !is_string_variable(name) ) {
  12919. ShowError("script:implode: not string array\n");
  12920. script_reportdata(data);
  12921. st->state = END;
  12922. return 1;// data type mismatch
  12923. }
  12924. if( not_server_variable(*name) ) {
  12925. TBL_PC* sd = script_rid2sd(st);
  12926. if( sd == NULL )
  12927. return 0;// no player attached
  12928. }
  12929. //count chars
  12930. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  12931. if(array_size == -1) { //empty array check (AmsTaff)
  12932. ShowWarning("script:implode: array length = 0\n");
  12933. output = (char*)aMalloc(sizeof(char)*5);
  12934. sprintf(output,"%s","NULL");
  12935. } else {
  12936. const char *glue = NULL, *temp;
  12937. size_t len = 0;
  12938. int i, k = 0;
  12939. for(i = 0; i <= array_size; ++i) {
  12940. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12941. len += strlen(temp);
  12942. script_removetop(st, -1, 0);
  12943. }
  12944. //allocate mem
  12945. if( script_hasdata(st,3) ) {
  12946. glue = script_getstr(st,3);
  12947. glue_len = strlen(glue);
  12948. len += glue_len * (array_size);
  12949. }
  12950. output = (char*)aMalloc(len + 1);
  12951. //build output
  12952. for(i = 0; i < array_size; ++i) {
  12953. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12954. len = strlen(temp);
  12955. memcpy(&output[k], temp, len);
  12956. k += len;
  12957. if(glue_len != 0) {
  12958. memcpy(&output[k], glue, glue_len);
  12959. k += glue_len;
  12960. }
  12961. script_removetop(st, -1, 0);
  12962. }
  12963. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  12964. len = strlen(temp);
  12965. memcpy(&output[k], temp, len);
  12966. k += len;
  12967. script_removetop(st, -1, 0);
  12968. output[k] = '\0';
  12969. }
  12970. script_pushstr(st, output);
  12971. return SCRIPT_CMD_SUCCESS;
  12972. }
  12973. //=======================================================
  12974. // sprintf(<format>, ...);
  12975. // Implements C sprintf, except format %n. The resulting string is
  12976. // returned, instead of being saved in variable by reference.
  12977. //-------------------------------------------------------
  12978. BUILDIN_FUNC(sprintf)
  12979. {
  12980. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  12981. const char* format;
  12982. char* p;
  12983. char* q;
  12984. char* buf = NULL;
  12985. char* buf2 = NULL;
  12986. struct script_data* data;
  12987. StringBuf final_buf;
  12988. // Fetch init data
  12989. format = script_getstr(st, 2);
  12990. argc = script_lastdata(st)-2;
  12991. len = strlen(format);
  12992. // Skip parsing, where no parsing is required.
  12993. if(len == 0) {
  12994. script_pushconststr(st,"");
  12995. return 0;
  12996. }
  12997. // Pessimistic alloc
  12998. CREATE(buf, char, len+1);
  12999. // Need not be parsed, just solve stuff like %%.
  13000. if(argc == 0) {
  13001. memcpy(buf,format,len+1);
  13002. script_pushstrcopy(st, buf);
  13003. aFree(buf);
  13004. return 0;
  13005. }
  13006. safestrncpy(buf, format, len+1);
  13007. // Issue sprintf for each parameter
  13008. StringBuf_Init(&final_buf);
  13009. q = buf;
  13010. while((p = strchr(q, '%')) != NULL) {
  13011. if(p != q) {
  13012. len = p - q + 1;
  13013. if(buf2_len < len) {
  13014. RECREATE(buf2, char, len);
  13015. buf2_len = len;
  13016. }
  13017. safestrncpy(buf2, q, len);
  13018. StringBuf_AppendStr(&final_buf, buf2);
  13019. q = p;
  13020. }
  13021. p = q + 1;
  13022. if(*p == '%') { // %%
  13023. StringBuf_AppendStr(&final_buf, "%");
  13024. q+=2;
  13025. continue;
  13026. }
  13027. if(*p == 'n') { // %n
  13028. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13029. script_reportsrc(st);
  13030. q+=2;
  13031. continue;
  13032. }
  13033. if(arg >= argc) {
  13034. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13035. if(buf) aFree(buf);
  13036. if(buf2) aFree(buf2);
  13037. StringBuf_Destroy(&final_buf);
  13038. script_pushconststr(st,"");
  13039. return 1;
  13040. }
  13041. if((p = strchr(q+1, '%')) == NULL)
  13042. p = strchr(q, 0); // EOS
  13043. len = p - q + 1;
  13044. if(buf2_len < len) {
  13045. RECREATE(buf2, char, len);
  13046. buf2_len = len;
  13047. }
  13048. safestrncpy(buf2, q, len);
  13049. q = p;
  13050. // Note: This assumes the passed value being the correct
  13051. // type to the current format specifier. If not, the server
  13052. // probably crashes or returns anything else, than expected,
  13053. // but it would behave in normal code the same way so it's
  13054. // the scripter's responsibility.
  13055. data = script_getdata(st, arg+3);
  13056. if(data_isstring(data)) // String
  13057. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13058. else if(data_isint(data)) // Number
  13059. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13060. else if(data_isreference(data)) { // Variable
  13061. char* name = reference_getname(data);
  13062. if(name[strlen(name)-1]=='$') // var Str
  13063. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13064. else // var Int
  13065. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13066. } else { // Unsupported type
  13067. ShowError("buildin_sprintf: Unknown argument type!\n");
  13068. if(buf) aFree(buf);
  13069. if(buf2) aFree(buf2);
  13070. StringBuf_Destroy(&final_buf);
  13071. script_pushconststr(st,"");
  13072. return 1;
  13073. }
  13074. arg++;
  13075. }
  13076. // Append anything left
  13077. if(*q)
  13078. StringBuf_AppendStr(&final_buf, q);
  13079. // Passed more, than needed
  13080. if(arg < argc) {
  13081. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13082. script_reportsrc(st);
  13083. }
  13084. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13085. if(buf) aFree(buf);
  13086. if(buf2) aFree(buf2);
  13087. StringBuf_Destroy(&final_buf);
  13088. return SCRIPT_CMD_SUCCESS;
  13089. }
  13090. //=======================================================
  13091. // sscanf(<str>, <format>, ...);
  13092. // Implements C sscanf.
  13093. //-------------------------------------------------------
  13094. BUILDIN_FUNC(sscanf){
  13095. unsigned int argc, arg = 0, len;
  13096. struct script_data* data;
  13097. struct map_session_data* sd = NULL;
  13098. const char* str;
  13099. const char* format;
  13100. const char* p;
  13101. const char* q;
  13102. char* buf = NULL;
  13103. char* buf_p;
  13104. char* ref_str = NULL;
  13105. int ref_int;
  13106. // Get data
  13107. str = script_getstr(st, 2);
  13108. format = script_getstr(st, 3);
  13109. argc = script_lastdata(st)-3;
  13110. len = strlen(format);
  13111. if (len != 0 && strlen(str) == 0) {
  13112. // If the source string is empty but the format string is not, we return -1
  13113. // according to the C specs. (if the format string is also empty, we shall
  13114. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13115. script_pushint(st, -1);
  13116. return SCRIPT_CMD_SUCCESS;
  13117. }
  13118. CREATE(buf, char, len*2+1);
  13119. // Issue sscanf for each parameter
  13120. *buf = 0;
  13121. q = format;
  13122. while((p = strchr(q, '%'))){
  13123. if(p!=q){
  13124. strncat(buf, q, (size_t)(p-q));
  13125. q = p;
  13126. }
  13127. p = q+1;
  13128. if(*p=='*' || *p=='%'){ // Skip
  13129. strncat(buf, q, 2);
  13130. q+=2;
  13131. continue;
  13132. }
  13133. if(arg>=argc){
  13134. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13135. script_pushint(st, -1);
  13136. if(buf) aFree(buf);
  13137. if(ref_str) aFree(ref_str);
  13138. return 1;
  13139. }
  13140. if((p = strchr(q+1, '%'))==NULL){
  13141. p = strchr(q, 0); // EOS
  13142. }
  13143. len = p-q;
  13144. strncat(buf, q, len);
  13145. q = p;
  13146. // Validate output
  13147. data = script_getdata(st, arg+4);
  13148. if(!data_isreference(data) || !reference_tovariable(data)){
  13149. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13150. script_pushint(st, -1);
  13151. if(buf) aFree(buf);
  13152. if(ref_str) aFree(ref_str);
  13153. return 1;
  13154. }
  13155. buf_p = reference_getname(data);
  13156. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13157. script_pushint(st, -1);
  13158. if(buf) aFree(buf);
  13159. if(ref_str) aFree(ref_str);
  13160. return 0;
  13161. }
  13162. // Save value if any
  13163. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13164. if(ref_str==NULL){
  13165. CREATE(ref_str, char, strlen(str)+1);
  13166. }
  13167. if(sscanf(str, buf, ref_str)==0){
  13168. break;
  13169. }
  13170. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13171. } else { // Number
  13172. if(sscanf(str, buf, &ref_int)==0){
  13173. break;
  13174. }
  13175. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13176. }
  13177. arg++;
  13178. // Disable used format (%... -> %*...)
  13179. buf_p = strchr(buf, 0);
  13180. memmove(buf_p-len+2, buf_p-len+1, len);
  13181. *(buf_p-len+1) = '*';
  13182. }
  13183. script_pushint(st, arg);
  13184. if(buf) aFree(buf);
  13185. if(ref_str) aFree(ref_str);
  13186. return SCRIPT_CMD_SUCCESS;
  13187. }
  13188. //=======================================================
  13189. // strpos(<haystack>, <needle>)
  13190. // strpos(<haystack>, <needle>, <offset>)
  13191. //
  13192. // Implements PHP style strpos. Adapted from code from
  13193. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13194. //-------------------------------------------------------
  13195. BUILDIN_FUNC(strpos) {
  13196. const char *haystack = script_getstr(st,2);
  13197. const char *needle = script_getstr(st,3);
  13198. int i;
  13199. size_t len;
  13200. if( script_hasdata(st,4) )
  13201. i = script_getnum(st,4);
  13202. else
  13203. i = 0;
  13204. if (needle[0] == '\0') {
  13205. script_pushint(st, -1);
  13206. return 0;
  13207. }
  13208. len = strlen(haystack);
  13209. for ( ; i < len; ++i ) {
  13210. if ( haystack[i] == *needle ) {
  13211. // matched starting char -- loop through remaining chars
  13212. const char *h, *n;
  13213. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13214. if ( *h != *n ) {
  13215. break;
  13216. }
  13217. }
  13218. if ( !*n ) { // matched all of 'needle' to null termination
  13219. script_pushint(st, i);
  13220. return 0;
  13221. }
  13222. }
  13223. }
  13224. script_pushint(st, -1);
  13225. return SCRIPT_CMD_SUCCESS;
  13226. }
  13227. //===============================================================
  13228. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13229. //
  13230. // Note: Finds all instances of <search> in <input> and replaces
  13231. // with <replace>. If specified will only replace as many
  13232. // instances as specified in <count>. By default will be case
  13233. // sensitive.
  13234. //---------------------------------------------------------------
  13235. BUILDIN_FUNC(replacestr)
  13236. {
  13237. const char *input = script_getstr(st, 2);
  13238. const char *find = script_getstr(st, 3);
  13239. const char *replace = script_getstr(st, 4);
  13240. size_t inputlen = strlen(input);
  13241. size_t findlen = strlen(find);
  13242. struct StringBuf output;
  13243. bool usecase = true;
  13244. int count = 0;
  13245. int numFinds = 0;
  13246. int i = 0, f = 0;
  13247. if(findlen == 0) {
  13248. ShowError("script:replacestr: Invalid search length.\n");
  13249. st->state = END;
  13250. return 1;
  13251. }
  13252. if(script_hasdata(st, 5)) {
  13253. struct script_data *data = script_getdata(st, 5);
  13254. get_val(st, data); // Convert into value in case of a variable
  13255. if( !data_isstring(data) )
  13256. usecase = script_getnum(st, 5) != 0;
  13257. else {
  13258. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13259. st->state = END;
  13260. return 1;
  13261. }
  13262. }
  13263. if(script_hasdata(st, 6)) {
  13264. count = script_getnum(st, 6);
  13265. if(count == 0) {
  13266. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13267. st->state = END;
  13268. return 1;
  13269. }
  13270. }
  13271. StringBuf_Init(&output);
  13272. for(; i < inputlen; i++) {
  13273. if(count && count == numFinds) { //found enough, stop looking
  13274. break;
  13275. }
  13276. for(f = 0; f <= findlen; f++) {
  13277. if(f == findlen) { //complete match
  13278. numFinds++;
  13279. StringBuf_AppendStr(&output, replace);
  13280. i += findlen - 1;
  13281. break;
  13282. } else {
  13283. if(usecase) {
  13284. if((i + f) > inputlen || input[i + f] != find[f]) {
  13285. StringBuf_Printf(&output, "%c", input[i]);
  13286. break;
  13287. }
  13288. } else {
  13289. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13290. StringBuf_Printf(&output, "%c", input[i]);
  13291. break;
  13292. }
  13293. }
  13294. }
  13295. }
  13296. }
  13297. //append excess after enough found
  13298. if(i < inputlen)
  13299. StringBuf_AppendStr(&output, &(input[i]));
  13300. script_pushstrcopy(st, StringBuf_Value(&output));
  13301. StringBuf_Destroy(&output);
  13302. return SCRIPT_CMD_SUCCESS;
  13303. }
  13304. //========================================================
  13305. // countstr <input>, <search>{, <usecase>}
  13306. //
  13307. // Note: Counts the number of times <search> occurs in
  13308. // <input>. By default will be case sensitive.
  13309. //--------------------------------------------------------
  13310. BUILDIN_FUNC(countstr)
  13311. {
  13312. const char *input = script_getstr(st, 2);
  13313. const char *find = script_getstr(st, 3);
  13314. size_t inputlen = strlen(input);
  13315. size_t findlen = strlen(find);
  13316. bool usecase = true;
  13317. int numFinds = 0;
  13318. int i = 0, f = 0;
  13319. if(findlen == 0) {
  13320. ShowError("script:countstr: Invalid search length.\n");
  13321. st->state = END;
  13322. return 1;
  13323. }
  13324. if(script_hasdata(st, 4)) {
  13325. struct script_data *data;
  13326. data = script_getdata(st, 4);
  13327. get_val(st, data); // Convert into value in case of a variable
  13328. if( !data_isstring(data) )
  13329. usecase = script_getnum(st, 4) != 0;
  13330. else {
  13331. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13332. st->state = END;
  13333. return 1;
  13334. }
  13335. }
  13336. for(; i < inputlen; i++) {
  13337. for(f = 0; f <= findlen; f++) {
  13338. if(f == findlen) { //complete match
  13339. numFinds++;
  13340. i += findlen - 1;
  13341. break;
  13342. } else {
  13343. if(usecase) {
  13344. if((i + f) > inputlen || input[i + f] != find[f]) {
  13345. break;
  13346. }
  13347. } else {
  13348. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13349. break;
  13350. }
  13351. }
  13352. }
  13353. }
  13354. }
  13355. script_pushint(st, numFinds);
  13356. return SCRIPT_CMD_SUCCESS;
  13357. }
  13358. /// Changes the display name and/or display class of the npc.
  13359. /// Returns 0 is successful, 1 if the npc does not exist.
  13360. ///
  13361. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13362. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13363. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13364. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13365. BUILDIN_FUNC(setnpcdisplay)
  13366. {
  13367. const char* name;
  13368. const char* newname = NULL;
  13369. int class_ = -1, size = -1;
  13370. struct script_data* data;
  13371. struct npc_data* nd;
  13372. name = script_getstr(st,2);
  13373. data = script_getdata(st,3);
  13374. if( script_hasdata(st,4) )
  13375. class_ = script_getnum(st,4);
  13376. if( script_hasdata(st,5) )
  13377. size = script_getnum(st,5);
  13378. get_val(st, data);
  13379. if( data_isstring(data) )
  13380. newname = conv_str(st,data);
  13381. else if( data_isint(data) )
  13382. class_ = conv_num(st,data);
  13383. else
  13384. {
  13385. ShowError("script:setnpcdisplay: expected string or number\n");
  13386. script_reportdata(data);
  13387. return 1;
  13388. }
  13389. nd = npc_name2id(name);
  13390. if( nd == NULL )
  13391. {// not found
  13392. script_pushint(st,1);
  13393. return 0;
  13394. }
  13395. // update npc
  13396. if( newname )
  13397. npc_setdisplayname(nd, newname);
  13398. if( size != -1 && size != (int)nd->size )
  13399. nd->size = size;
  13400. else
  13401. size = -1;
  13402. if( class_ != -1 && nd->class_ != class_ )
  13403. npc_setclass(nd, class_);
  13404. else if( size != -1 )
  13405. { // Required to update the visual size
  13406. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13407. clif_spawn(&nd->bl);
  13408. }
  13409. script_pushint(st,0);
  13410. return SCRIPT_CMD_SUCCESS;
  13411. }
  13412. BUILDIN_FUNC(atoi)
  13413. {
  13414. const char *value;
  13415. value = script_getstr(st,2);
  13416. script_pushint(st,atoi(value));
  13417. return SCRIPT_CMD_SUCCESS;
  13418. }
  13419. BUILDIN_FUNC(axtoi)
  13420. {
  13421. const char *hex = script_getstr(st,2);
  13422. long value = strtol(hex, NULL, 16);
  13423. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13424. value = cap_value(value, INT_MIN, INT_MAX);
  13425. #endif
  13426. script_pushint(st, (int)value);
  13427. return SCRIPT_CMD_SUCCESS;
  13428. }
  13429. BUILDIN_FUNC(strtol)
  13430. {
  13431. const char *string = script_getstr(st, 2);
  13432. int base = script_getnum(st, 3);
  13433. long value = strtol(string, NULL, base);
  13434. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13435. value = cap_value(value, INT_MIN, INT_MAX);
  13436. #endif
  13437. script_pushint(st, (int)value);
  13438. return SCRIPT_CMD_SUCCESS;
  13439. }
  13440. // case-insensitive substring search [lordalfa]
  13441. BUILDIN_FUNC(compare)
  13442. {
  13443. const char *message;
  13444. const char *cmpstring;
  13445. message = script_getstr(st,2);
  13446. cmpstring = script_getstr(st,3);
  13447. script_pushint(st,(stristr(message,cmpstring) != NULL));
  13448. return SCRIPT_CMD_SUCCESS;
  13449. }
  13450. BUILDIN_FUNC(strcmp)
  13451. {
  13452. const char *str1;
  13453. const char *str2;
  13454. str1 = script_getstr(st,2);
  13455. str2 = script_getstr(st,3);
  13456. script_pushint(st,strcmp(str1, str2));
  13457. return SCRIPT_CMD_SUCCESS;
  13458. }
  13459. // [zBuffer] List of mathematics commands --->
  13460. BUILDIN_FUNC(sqrt)
  13461. {
  13462. double i, a;
  13463. i = script_getnum(st,2);
  13464. a = sqrt(i);
  13465. script_pushint(st,(int)a);
  13466. return SCRIPT_CMD_SUCCESS;
  13467. }
  13468. BUILDIN_FUNC(pow)
  13469. {
  13470. double i, a, b;
  13471. a = script_getnum(st,2);
  13472. b = script_getnum(st,3);
  13473. i = pow(a,b);
  13474. script_pushint(st,(int)i);
  13475. return SCRIPT_CMD_SUCCESS;
  13476. }
  13477. BUILDIN_FUNC(distance)
  13478. {
  13479. int x0, y0, x1, y1;
  13480. x0 = script_getnum(st,2);
  13481. y0 = script_getnum(st,3);
  13482. x1 = script_getnum(st,4);
  13483. y1 = script_getnum(st,5);
  13484. script_pushint(st,distance_xy(x0,y0,x1,y1));
  13485. return SCRIPT_CMD_SUCCESS;
  13486. }
  13487. // <--- [zBuffer] List of mathematics commands
  13488. BUILDIN_FUNC(md5)
  13489. {
  13490. const char *tmpstr;
  13491. char *md5str;
  13492. tmpstr = script_getstr(st,2);
  13493. md5str = (char *)aMalloc((32+1)*sizeof(char));
  13494. MD5_String(tmpstr, md5str);
  13495. script_pushstr(st, md5str);
  13496. return SCRIPT_CMD_SUCCESS;
  13497. }
  13498. // [zBuffer] List of dynamic var commands --->
  13499. BUILDIN_FUNC(setd)
  13500. {
  13501. TBL_PC *sd=NULL;
  13502. char varname[100];
  13503. const char *buffer;
  13504. int elem;
  13505. buffer = script_getstr(st, 2);
  13506. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  13507. elem = 0;
  13508. if( not_server_variable(*varname) )
  13509. {
  13510. sd = script_rid2sd(st);
  13511. if( sd == NULL )
  13512. {
  13513. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  13514. return 0;
  13515. }
  13516. }
  13517. if( is_string_variable(varname) ) {
  13518. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  13519. } else {
  13520. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  13521. }
  13522. return SCRIPT_CMD_SUCCESS;
  13523. }
  13524. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  13525. {
  13526. int i, j;
  13527. TBL_PC* sd = NULL;
  13528. const char* query;
  13529. struct script_data* data;
  13530. const char* name;
  13531. int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  13532. int num_vars;
  13533. int num_cols;
  13534. // check target variables
  13535. for( i = 3; script_hasdata(st,i); ++i ) {
  13536. data = script_getdata(st, i);
  13537. if( data_isreference(data) ) { // it's a variable
  13538. name = reference_getname(data);
  13539. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  13540. sd = script_rid2sd(st);
  13541. if( sd == NULL ) { // no player attached
  13542. script_reportdata(data);
  13543. st->state = END;
  13544. return 1;
  13545. }
  13546. }
  13547. if( not_array_variable(*name) )
  13548. max_rows = 1;// not an array, limit to one row
  13549. } else {
  13550. ShowError("script:query_sql: not a variable\n");
  13551. script_reportdata(data);
  13552. st->state = END;
  13553. return 1;
  13554. }
  13555. }
  13556. num_vars = i - 3;
  13557. // Execute the query
  13558. query = script_getstr(st,2);
  13559. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  13560. Sql_ShowDebug(handle);
  13561. script_pushint(st, -1);
  13562. return 1;
  13563. }
  13564. if( Sql_NumRows(handle) == 0 ) { // No data received
  13565. Sql_FreeResult(handle);
  13566. script_pushint(st, -1);
  13567. return 0;
  13568. }
  13569. // Count the number of columns to store
  13570. num_cols = Sql_NumColumns(handle);
  13571. if( num_vars < num_cols ) {
  13572. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  13573. script_reportsrc(st);
  13574. } else if( num_vars > num_cols ) {
  13575. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  13576. script_reportsrc(st);
  13577. }
  13578. // Store data
  13579. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  13580. for( j = 0; j < num_vars; ++j ) {
  13581. char* str = NULL;
  13582. if( j < num_cols )
  13583. Sql_GetData(handle, j, &str, NULL);
  13584. data = script_getdata(st, j+3);
  13585. name = reference_getname(data);
  13586. if( is_string_variable(name) )
  13587. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  13588. else
  13589. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  13590. }
  13591. }
  13592. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  13593. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  13594. script_reportsrc(st);
  13595. }
  13596. // Free data
  13597. Sql_FreeResult(handle);
  13598. script_pushint(st, i);
  13599. return SCRIPT_CMD_SUCCESS;
  13600. }
  13601. BUILDIN_FUNC(query_sql) {
  13602. #ifdef BETA_THREAD_TEST
  13603. if( st->state != RERUNLINE ) {
  13604. queryThread_add(st,false);
  13605. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13606. } else
  13607. st->state = RUN;
  13608. return 0;
  13609. #else
  13610. return buildin_query_sql_sub(st, mmysql_handle);
  13611. #endif
  13612. }
  13613. BUILDIN_FUNC(query_logsql) {
  13614. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  13615. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  13616. script_pushint(st,-1);
  13617. return 1;
  13618. }
  13619. #ifdef BETA_THREAD_TEST
  13620. if( st->state != RERUNLINE ) {
  13621. queryThread_add(st,true);
  13622. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13623. } else
  13624. st->state = RUN;
  13625. return 0;
  13626. #else
  13627. return buildin_query_sql_sub(st, logmysql_handle);
  13628. #endif
  13629. }
  13630. //Allows escaping of a given string.
  13631. BUILDIN_FUNC(escape_sql)
  13632. {
  13633. const char *str;
  13634. char *esc_str;
  13635. size_t len;
  13636. str = script_getstr(st,2);
  13637. len = strlen(str);
  13638. esc_str = (char*)aMalloc(len*2+1);
  13639. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  13640. script_pushstr(st, esc_str);
  13641. return SCRIPT_CMD_SUCCESS;
  13642. }
  13643. BUILDIN_FUNC(getd)
  13644. {
  13645. char varname[100];
  13646. const char *buffer;
  13647. int elem;
  13648. buffer = script_getstr(st, 2);
  13649. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  13650. elem = 0;
  13651. // Push the 'pointer' so it's more flexible [Lance]
  13652. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  13653. return SCRIPT_CMD_SUCCESS;
  13654. }
  13655. BUILDIN_FUNC(callshop)
  13656. {
  13657. TBL_PC *sd = NULL;
  13658. struct npc_data *nd;
  13659. const char *shopname;
  13660. int flag = 0;
  13661. sd = script_rid2sd(st);
  13662. if (!sd) {
  13663. script_pushint(st,0);
  13664. return 0;
  13665. }
  13666. shopname = script_getstr(st, 2);
  13667. if( script_hasdata(st,3) )
  13668. flag = script_getnum(st,3);
  13669. nd = npc_name2id(shopname);
  13670. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP) )
  13671. {
  13672. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  13673. script_pushint(st,0);
  13674. return 1;
  13675. }
  13676. if( nd->subtype == NPCTYPE_SHOP || nd->subtype == NPCTYPE_ITEMSHOP || nd->subtype == NPCTYPE_POINTSHOP )
  13677. {
  13678. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  13679. sd->state.callshop = 1;
  13680. switch( flag )
  13681. {
  13682. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  13683. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  13684. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  13685. }
  13686. }
  13687. else
  13688. clif_cashshop_show(sd, nd);
  13689. sd->npc_shopid = nd->bl.id;
  13690. script_pushint(st,1);
  13691. return SCRIPT_CMD_SUCCESS;
  13692. }
  13693. BUILDIN_FUNC(npcshopitem)
  13694. {
  13695. const char* npcname = script_getstr(st, 2);
  13696. struct npc_data* nd = npc_name2id(npcname);
  13697. int n, i;
  13698. int amount;
  13699. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP ) )
  13700. { //Not found.
  13701. script_pushint(st,0);
  13702. return 0;
  13703. }
  13704. // get the count of new entries
  13705. amount = (script_lastdata(st)-2)/2;
  13706. // generate new shop item list
  13707. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  13708. for( n = 0, i = 3; n < amount; n++, i+=2 )
  13709. {
  13710. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13711. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13712. }
  13713. nd->u.shop.count = n;
  13714. script_pushint(st,1);
  13715. return SCRIPT_CMD_SUCCESS;
  13716. }
  13717. BUILDIN_FUNC(npcshopadditem)
  13718. {
  13719. const char* npcname = script_getstr(st,2);
  13720. struct npc_data* nd = npc_name2id(npcname);
  13721. int n, i;
  13722. int amount;
  13723. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP ) )
  13724. { //Not found.
  13725. script_pushint(st,0);
  13726. return 0;
  13727. }
  13728. // get the count of new entries
  13729. amount = (script_lastdata(st)-2)/2;
  13730. // append new items to existing shop item list
  13731. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  13732. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  13733. {
  13734. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13735. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13736. }
  13737. nd->u.shop.count = n;
  13738. script_pushint(st,1);
  13739. return SCRIPT_CMD_SUCCESS;
  13740. }
  13741. BUILDIN_FUNC(npcshopdelitem)
  13742. {
  13743. const char* npcname = script_getstr(st,2);
  13744. struct npc_data* nd = npc_name2id(npcname);
  13745. int n, i;
  13746. int amount;
  13747. int size;
  13748. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP ) ) { // Not found.
  13749. script_pushint(st,0);
  13750. return 0;
  13751. }
  13752. amount = script_lastdata(st)-2;
  13753. size = nd->u.shop.count;
  13754. // remove specified items from the shop item list
  13755. for( i = 3; i < 3 + amount; i++ ) {
  13756. unsigned short nameid = script_getnum(st,i);
  13757. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  13758. if( n < size ) {
  13759. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  13760. size--;
  13761. }
  13762. }
  13763. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  13764. nd->u.shop.count = size;
  13765. script_pushint(st,1);
  13766. return SCRIPT_CMD_SUCCESS;
  13767. }
  13768. //Sets a script to attach to a shop npc.
  13769. BUILDIN_FUNC(npcshopattach)
  13770. {
  13771. const char* npcname = script_getstr(st,2);
  13772. struct npc_data* nd = npc_name2id(npcname);
  13773. int flag = 1;
  13774. if( script_hasdata(st,3) )
  13775. flag = script_getnum(st,3);
  13776. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP ) )
  13777. { //Not found.
  13778. script_pushint(st,0);
  13779. return 0;
  13780. }
  13781. if (flag)
  13782. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  13783. else
  13784. nd->master_nd = NULL;
  13785. script_pushint(st,1);
  13786. return SCRIPT_CMD_SUCCESS;
  13787. }
  13788. /*==========================================
  13789. * Returns some values of an item [Lupus]
  13790. * Price, Weight, etc...
  13791. setitemscript(itemID,"{new item bonus script}",[n]);
  13792. Where n:
  13793. 0 - script
  13794. 1 - Equip script
  13795. 2 - Unequip script
  13796. *------------------------------------------*/
  13797. BUILDIN_FUNC(setitemscript)
  13798. {
  13799. unsigned short item_id;
  13800. int n = 0;
  13801. const char *script;
  13802. struct item_data *i_data;
  13803. struct script_code **dstscript;
  13804. item_id = script_getnum(st,2);
  13805. script = script_getstr(st,3);
  13806. if( script_hasdata(st,4) )
  13807. n=script_getnum(st,4);
  13808. i_data = itemdb_exists(item_id);
  13809. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  13810. script_pushint(st,0);
  13811. return 0;
  13812. }
  13813. switch (n) {
  13814. case 2:
  13815. dstscript = &i_data->unequip_script;
  13816. break;
  13817. case 1:
  13818. dstscript = &i_data->equip_script;
  13819. break;
  13820. default:
  13821. dstscript = &i_data->script;
  13822. break;
  13823. }
  13824. if(*dstscript)
  13825. script_free_code(*dstscript);
  13826. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  13827. script_pushint(st,1);
  13828. return SCRIPT_CMD_SUCCESS;
  13829. }
  13830. /*=======================================================
  13831. * Add or Update a mob drop temporarily [Akinari]
  13832. * Original Idea By: [Lupus]
  13833. *
  13834. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  13835. *
  13836. * If given an item the mob already drops, the rate
  13837. * is updated to the new rate. Rate cannot exceed 10000
  13838. * Returns 1 if succeeded (added/updated a mob drop)
  13839. *-------------------------------------------------------*/
  13840. BUILDIN_FUNC(addmonsterdrop)
  13841. {
  13842. struct mob_db *mob;
  13843. struct script_data *data;
  13844. unsigned short item_id;
  13845. int rate;
  13846. data = script_getdata(st, 2);
  13847. get_val(st, data); // Convert into value in case of a variable
  13848. if(data_isstring(data))
  13849. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13850. else
  13851. mob = mob_db(script_getnum(st,2));
  13852. item_id=script_getnum(st,3);
  13853. rate=script_getnum(st,4);
  13854. if(!itemdb_exists(item_id)){
  13855. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  13856. return 1;
  13857. }
  13858. if(mob) { //We got a valid monster, check for available drop slot
  13859. unsigned char i, c = 0;
  13860. for(i = 0; i < MAX_MOB_DROP; i++) {
  13861. if(mob->dropitem[i].nameid) {
  13862. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  13863. c = i;
  13864. break;
  13865. }
  13866. continue;
  13867. }
  13868. if(!c) //Accept first available slot only
  13869. c = i;
  13870. }
  13871. if(c) { //Fill in the slot with the item and rate
  13872. mob->dropitem[c].nameid = item_id;
  13873. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  13874. script_pushint(st,1);
  13875. } else //No place to put the new drop
  13876. script_pushint(st,0);
  13877. } else {
  13878. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13879. return 1;
  13880. }
  13881. return SCRIPT_CMD_SUCCESS;
  13882. }
  13883. /*=======================================================
  13884. * Delete a mob drop temporarily [Akinari]
  13885. * Original Idea By: [Lupus]
  13886. *
  13887. * delmonsterdrop <mob_id or name>,<item_id>;
  13888. *
  13889. * Returns 1 if succeeded (deleted a mob drop)
  13890. *-------------------------------------------------------*/
  13891. BUILDIN_FUNC(delmonsterdrop)
  13892. {
  13893. struct mob_db *mob;
  13894. struct script_data *data;
  13895. unsigned short item_id;
  13896. data = script_getdata(st, 2);
  13897. get_val(st, data); // Convert into value in case of a variable
  13898. if(data_isstring(data))
  13899. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13900. else
  13901. mob = mob_db(script_getnum(st,2));
  13902. item_id=script_getnum(st,3);
  13903. if(!itemdb_exists(item_id)){
  13904. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  13905. return 1;
  13906. }
  13907. if(mob) { //We got a valid monster, check for item drop on monster
  13908. unsigned char i;
  13909. for(i = 0; i < MAX_MOB_DROP; i++) {
  13910. if(mob->dropitem[i].nameid == item_id) {
  13911. mob->dropitem[i].nameid = 0;
  13912. mob->dropitem[i].p = 0;
  13913. script_pushint(st,1);
  13914. return 0;
  13915. }
  13916. }
  13917. //No drop on that monster
  13918. script_pushint(st,0);
  13919. } else {
  13920. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13921. return 1;
  13922. }
  13923. return SCRIPT_CMD_SUCCESS;
  13924. }
  13925. /*==========================================
  13926. * Returns some values of a monster [Lupus]
  13927. * Name, Level, race, size, etc...
  13928. getmonsterinfo(monsterID,queryIndex);
  13929. *------------------------------------------*/
  13930. BUILDIN_FUNC(getmonsterinfo)
  13931. {
  13932. struct mob_db *mob;
  13933. int mob_id;
  13934. mob_id = script_getnum(st,2);
  13935. if (!mobdb_checkid(mob_id)) {
  13936. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  13937. if ( !script_getnum(st,3) ) //requested a string
  13938. script_pushconststr(st,"null");
  13939. else
  13940. script_pushint(st,-1);
  13941. return 0;
  13942. }
  13943. mob = mob_db(mob_id);
  13944. switch ( script_getnum(st,3) ) {
  13945. case 0: script_pushstrcopy(st,mob->jname); break;
  13946. case 1: script_pushint(st,mob->lv); break;
  13947. case 2: script_pushint(st,mob->status.max_hp); break;
  13948. case 3: script_pushint(st,mob->base_exp); break;
  13949. case 4: script_pushint(st,mob->job_exp); break;
  13950. case 5: script_pushint(st,mob->status.rhw.atk); break;
  13951. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  13952. case 7: script_pushint(st,mob->status.def); break;
  13953. case 8: script_pushint(st,mob->status.mdef); break;
  13954. case 9: script_pushint(st,mob->status.str); break;
  13955. case 10: script_pushint(st,mob->status.agi); break;
  13956. case 11: script_pushint(st,mob->status.vit); break;
  13957. case 12: script_pushint(st,mob->status.int_); break;
  13958. case 13: script_pushint(st,mob->status.dex); break;
  13959. case 14: script_pushint(st,mob->status.luk); break;
  13960. case 15: script_pushint(st,mob->status.rhw.range); break;
  13961. case 16: script_pushint(st,mob->range2); break;
  13962. case 17: script_pushint(st,mob->range3); break;
  13963. case 18: script_pushint(st,mob->status.size); break;
  13964. case 19: script_pushint(st,mob->status.race); break;
  13965. case 20: script_pushint(st,mob->status.def_ele); break;
  13966. case 21: script_pushint(st,mob->status.mode); break;
  13967. case 22: script_pushint(st,mob->mexp); break;
  13968. default: script_pushint(st,-1); //wrong Index
  13969. }
  13970. return SCRIPT_CMD_SUCCESS;
  13971. }
  13972. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  13973. {
  13974. TBL_PC *sd = NULL;
  13975. if(script_hasdata(st,2))
  13976. sd = map_nick2sd(script_getstr(st,2));
  13977. else
  13978. sd = script_rid2sd(st);
  13979. if(sd)
  13980. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  13981. else
  13982. script_pushint(st,0);
  13983. return SCRIPT_CMD_SUCCESS;
  13984. }
  13985. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  13986. {
  13987. TBL_PC *sd = NULL;
  13988. if(script_hasdata(st,2))
  13989. sd = map_nick2sd(script_getstr(st,2));
  13990. else
  13991. sd = script_rid2sd(st);
  13992. if(sd)
  13993. script_pushint(st,(sd->chatID != 0));
  13994. else
  13995. script_pushint(st,0);
  13996. return SCRIPT_CMD_SUCCESS;
  13997. }
  13998. BUILDIN_FUNC(checkidle)
  13999. {
  14000. TBL_PC *sd = NULL;
  14001. if (script_hasdata(st, 2))
  14002. sd = map_nick2sd(script_getstr(st, 2));
  14003. else
  14004. sd = script_rid2sd(st);
  14005. if (sd)
  14006. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14007. else
  14008. script_pushint(st, 0);
  14009. return SCRIPT_CMD_SUCCESS;
  14010. }
  14011. BUILDIN_FUNC(searchitem)
  14012. {
  14013. struct script_data* data = script_getdata(st, 2);
  14014. const char *itemname = script_getstr(st,3);
  14015. struct item_data *items[MAX_SEARCH];
  14016. int count;
  14017. char* name;
  14018. int32 start;
  14019. int32 id;
  14020. int32 i;
  14021. TBL_PC* sd = NULL;
  14022. if ((items[0] = itemdb_exists(atoi(itemname))))
  14023. count = 1;
  14024. else {
  14025. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14026. if (count > MAX_SEARCH) count = MAX_SEARCH;
  14027. }
  14028. if (!count) {
  14029. script_pushint(st, 0);
  14030. return 0;
  14031. }
  14032. if( !data_isreference(data) )
  14033. {
  14034. ShowError("script:searchitem: not a variable\n");
  14035. script_reportdata(data);
  14036. st->state = END;
  14037. return 1;// not a variable
  14038. }
  14039. id = reference_getid(data);
  14040. start = reference_getindex(data);
  14041. name = reference_getname(data);
  14042. if( not_array_variable(*name) )
  14043. {
  14044. ShowError("script:searchitem: illegal scope\n");
  14045. script_reportdata(data);
  14046. st->state = END;
  14047. return 1;// not supported
  14048. }
  14049. if( not_server_variable(*name) )
  14050. {
  14051. sd = script_rid2sd(st);
  14052. if( sd == NULL )
  14053. return 0;// no player attached
  14054. }
  14055. if( is_string_variable(name) )
  14056. {// string array
  14057. ShowError("script:searchitem: not an integer array reference\n");
  14058. script_reportdata(data);
  14059. st->state = END;
  14060. return 1;// not supported
  14061. }
  14062. for( i = 0; i < count; ++start, ++i )
  14063. {// Set array
  14064. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14065. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14066. }
  14067. script_pushint(st, count);
  14068. return SCRIPT_CMD_SUCCESS;
  14069. }
  14070. // [zBuffer] List of player cont commands --->
  14071. BUILDIN_FUNC(rid2name)
  14072. {
  14073. struct block_list *bl = NULL;
  14074. int rid = script_getnum(st,2);
  14075. if((bl = map_id2bl(rid)))
  14076. {
  14077. switch(bl->type) {
  14078. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14079. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14080. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14081. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14082. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14083. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14084. default:
  14085. ShowError("buildin_rid2name: BL type unknown.\n");
  14086. script_pushconststr(st,"");
  14087. break;
  14088. }
  14089. } else {
  14090. ShowError("buildin_rid2name: invalid RID\n");
  14091. script_pushconststr(st,"(null)");
  14092. }
  14093. return SCRIPT_CMD_SUCCESS;
  14094. }
  14095. BUILDIN_FUNC(pcblockmove)
  14096. {
  14097. int id, flag;
  14098. TBL_PC *sd = NULL;
  14099. id = script_getnum(st,2);
  14100. flag = script_getnum(st,3);
  14101. if(id)
  14102. sd = map_id2sd(id);
  14103. else
  14104. sd = script_rid2sd(st);
  14105. if(sd)
  14106. sd->state.blockedmove = flag > 0;
  14107. return SCRIPT_CMD_SUCCESS;
  14108. }
  14109. BUILDIN_FUNC(pcfollow)
  14110. {
  14111. int id, targetid;
  14112. TBL_PC *sd = NULL;
  14113. id = script_getnum(st,2);
  14114. targetid = script_getnum(st,3);
  14115. if(id)
  14116. sd = map_id2sd(id);
  14117. else
  14118. sd = script_rid2sd(st);
  14119. if(sd)
  14120. pc_follow(sd, targetid);
  14121. return SCRIPT_CMD_SUCCESS;
  14122. }
  14123. BUILDIN_FUNC(pcstopfollow)
  14124. {
  14125. int id;
  14126. TBL_PC *sd = NULL;
  14127. id = script_getnum(st,2);
  14128. if(id)
  14129. sd = map_id2sd(id);
  14130. else
  14131. sd = script_rid2sd(st);
  14132. if(sd)
  14133. pc_stop_following(sd);
  14134. return SCRIPT_CMD_SUCCESS;
  14135. }
  14136. // <--- [zBuffer] List of player cont commands
  14137. // [zBuffer] List of mob control commands --->
  14138. /// Makes the unit walk to target position or map
  14139. /// Returns if it was successfull
  14140. ///
  14141. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  14142. /// unitwalk(<unit_id>,<target_id>) -> <bool>
  14143. BUILDIN_FUNC(unitwalk)
  14144. {
  14145. struct block_list* bl;
  14146. bl = map_id2bl(script_getnum(st,2));
  14147. if( bl == NULL )
  14148. script_pushint(st, 0);
  14149. else if( script_hasdata(st,4) ) {
  14150. int x = script_getnum(st,3);
  14151. int y = script_getnum(st,4);
  14152. if( script_pushint(st, unit_can_reach_pos(bl,x,y,0)) )
  14153. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  14154. } else {
  14155. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  14156. if( tbl == NULL ) {
  14157. ShowError("script:unitwalk: bad target destination\n");
  14158. script_pushint(st, 0);
  14159. return 1;
  14160. }
  14161. else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  14162. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  14163. }
  14164. return SCRIPT_CMD_SUCCESS;
  14165. }
  14166. /// Kills the unit
  14167. ///
  14168. /// unitkill <unit_id>;
  14169. BUILDIN_FUNC(unitkill)
  14170. {
  14171. struct block_list* bl = map_id2bl(script_getnum(st,2));
  14172. if( bl != NULL )
  14173. status_kill(bl);
  14174. return SCRIPT_CMD_SUCCESS;
  14175. }
  14176. /// Warps the unit to the target position in the target map
  14177. /// Returns if it was successfull
  14178. ///
  14179. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  14180. BUILDIN_FUNC(unitwarp)
  14181. {
  14182. int unit_id;
  14183. int map_idx;
  14184. short x;
  14185. short y;
  14186. struct block_list* bl;
  14187. const char *mapname;
  14188. unit_id = script_getnum(st,2);
  14189. mapname = script_getstr(st, 3);
  14190. x = (short)script_getnum(st,4);
  14191. y = (short)script_getnum(st,5);
  14192. if (!unit_id) //Warp the script's runner
  14193. bl = map_id2bl(st->rid);
  14194. else
  14195. bl = map_id2bl(unit_id);
  14196. if( strcmp(mapname,"this") == 0 )
  14197. map_idx = bl?bl->m:-1;
  14198. else
  14199. map_idx = map_mapname2mapid(mapname);
  14200. if( map_idx >= 0 && bl != NULL )
  14201. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  14202. else
  14203. script_pushint(st, 0);
  14204. return SCRIPT_CMD_SUCCESS;
  14205. }
  14206. /// Makes the unit attack the target.
  14207. /// If the unit is a player and <action type> is not 0, it does a continuous
  14208. /// attack instead of a single attack.
  14209. /// Returns if the request was successful.
  14210. ///
  14211. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  14212. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  14213. BUILDIN_FUNC(unitattack)
  14214. {
  14215. struct block_list* unit_bl;
  14216. struct block_list* target_bl = NULL;
  14217. struct script_data* data;
  14218. int actiontype = 0;
  14219. // get unit
  14220. unit_bl = map_id2bl(script_getnum(st,2));
  14221. if( unit_bl == NULL ) {
  14222. script_pushint(st, 0);
  14223. return 0;
  14224. }
  14225. data = script_getdata(st, 3);
  14226. get_val(st, data);
  14227. if( data_isstring(data) )
  14228. {
  14229. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  14230. if( sd != NULL )
  14231. target_bl = &sd->bl;
  14232. } else
  14233. target_bl = map_id2bl(conv_num(st, data));
  14234. // request the attack
  14235. if( target_bl == NULL )
  14236. {
  14237. script_pushint(st, 0);
  14238. return 0;
  14239. }
  14240. // get actiontype
  14241. if( script_hasdata(st,4) )
  14242. actiontype = script_getnum(st,4);
  14243. switch( unit_bl->type )
  14244. {
  14245. case BL_PC:
  14246. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  14247. script_pushint(st, 1);
  14248. return 0;
  14249. case BL_MOB:
  14250. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  14251. break;
  14252. case BL_PET:
  14253. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  14254. break;
  14255. default:
  14256. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  14257. script_pushint(st, 0);
  14258. return 1;
  14259. }
  14260. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  14261. return SCRIPT_CMD_SUCCESS;
  14262. }
  14263. /// Makes the unit stop attacking and moving
  14264. ///
  14265. /// unitstop <unit_id>;
  14266. BUILDIN_FUNC(unitstop)
  14267. {
  14268. int unit_id;
  14269. struct block_list* bl;
  14270. unit_id = script_getnum(st,2);
  14271. bl = map_id2bl(unit_id);
  14272. if( bl != NULL )
  14273. {
  14274. unit_stop_attack(bl);
  14275. unit_stop_walking(bl,4);
  14276. if( bl->type == BL_MOB )
  14277. ((TBL_MOB*)bl)->target_id = 0;
  14278. }
  14279. return SCRIPT_CMD_SUCCESS;
  14280. }
  14281. /// Makes the unit say the message
  14282. ///
  14283. /// unittalk <unit_id>,"<message>";
  14284. BUILDIN_FUNC(unittalk)
  14285. {
  14286. int unit_id;
  14287. const char* message;
  14288. struct block_list* bl;
  14289. unit_id = script_getnum(st,2);
  14290. message = script_getstr(st, 3);
  14291. bl = map_id2bl(unit_id);
  14292. if( bl != NULL )
  14293. {
  14294. struct StringBuf sbuf;
  14295. StringBuf_Init(&sbuf);
  14296. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  14297. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  14298. StringBuf_Destroy(&sbuf);
  14299. }
  14300. return SCRIPT_CMD_SUCCESS;
  14301. }
  14302. /// Makes the unit do an emotion
  14303. ///
  14304. /// unitemote <unit_id>,<emotion>;
  14305. ///
  14306. /// @see e_* in const.txt
  14307. BUILDIN_FUNC(unitemote)
  14308. {
  14309. int unit_id;
  14310. int emotion;
  14311. struct block_list* bl;
  14312. unit_id = script_getnum(st,2);
  14313. emotion = script_getnum(st,3);
  14314. bl = map_id2bl(unit_id);
  14315. if( bl != NULL )
  14316. clif_emotion(bl, emotion);
  14317. return SCRIPT_CMD_SUCCESS;
  14318. }
  14319. /// Makes the unit cast the skill on the target or self if no target is specified
  14320. ///
  14321. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  14322. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  14323. BUILDIN_FUNC(unitskilluseid)
  14324. {
  14325. int unit_id;
  14326. uint16 skill_id;
  14327. uint16 skill_lv;
  14328. int target_id;
  14329. struct block_list* bl;
  14330. struct script_data *data;
  14331. unit_id = script_getnum(st,2);
  14332. data = script_getdata(st, 3);
  14333. get_val(st, data); // Convert into value in case of a variable
  14334. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  14335. skill_lv = script_getnum(st,4);
  14336. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  14337. bl = map_id2bl(unit_id);
  14338. if( bl != NULL )
  14339. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  14340. return SCRIPT_CMD_SUCCESS;
  14341. }
  14342. /// Makes the unit cast the skill on the target position.
  14343. ///
  14344. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  14345. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  14346. BUILDIN_FUNC(unitskillusepos)
  14347. {
  14348. int unit_id;
  14349. uint16 skill_id;
  14350. uint16 skill_lv;
  14351. int skill_x;
  14352. int skill_y;
  14353. struct block_list* bl;
  14354. struct script_data *data;
  14355. unit_id = script_getnum(st,2);
  14356. data = script_getdata(st, 3);
  14357. get_val(st, data); // Convert into value in case of a variable
  14358. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  14359. skill_lv = script_getnum(st,4);
  14360. skill_x = script_getnum(st,5);
  14361. skill_y = script_getnum(st,6);
  14362. bl = map_id2bl(unit_id);
  14363. if( bl != NULL )
  14364. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  14365. return SCRIPT_CMD_SUCCESS;
  14366. }
  14367. // <--- [zBuffer] List of mob control commands
  14368. /// Pauses the execution of the script, detaching the player
  14369. ///
  14370. /// sleep <mili seconds>;
  14371. BUILDIN_FUNC(sleep)
  14372. {
  14373. int ticks;
  14374. ticks = script_getnum(st,2);
  14375. // detach the player
  14376. script_detach_rid(st);
  14377. if( ticks <= 0 )
  14378. {// do nothing
  14379. }
  14380. else if( st->sleep.tick == 0 )
  14381. {// sleep for the target amount of time
  14382. st->state = RERUNLINE;
  14383. st->sleep.tick = ticks;
  14384. }
  14385. else
  14386. {// sleep time is over
  14387. st->state = RUN;
  14388. st->sleep.tick = 0;
  14389. }
  14390. return SCRIPT_CMD_SUCCESS;
  14391. }
  14392. /// Pauses the execution of the script, keeping the player attached
  14393. /// Returns if a player is still attached
  14394. ///
  14395. /// sleep2(<mili secconds>) -> <bool>
  14396. BUILDIN_FUNC(sleep2)
  14397. {
  14398. int ticks;
  14399. ticks = script_getnum(st,2);
  14400. if( ticks <= 0 )
  14401. {// do nothing
  14402. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  14403. }
  14404. else if( !st->sleep.tick )
  14405. {// sleep for the target amount of time
  14406. st->state = RERUNLINE;
  14407. st->sleep.tick = ticks;
  14408. }
  14409. else
  14410. {// sleep time is over
  14411. st->state = RUN;
  14412. st->sleep.tick = 0;
  14413. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  14414. }
  14415. return SCRIPT_CMD_SUCCESS;
  14416. }
  14417. /// Awakes all the sleep timers of the target npc
  14418. ///
  14419. /// awake "<npc name>";
  14420. BUILDIN_FUNC(awake)
  14421. {
  14422. struct npc_data* nd;
  14423. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  14424. nd = npc_name2id(script_getstr(st, 2));
  14425. if( nd == NULL ) {
  14426. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  14427. return 1;
  14428. }
  14429. while( node )
  14430. {
  14431. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  14432. {// sleep timer for the npc
  14433. struct script_state* tst = (struct script_state*)node->data;
  14434. TBL_PC* sd = map_id2sd(tst->rid);
  14435. if( tst->sleep.timer == INVALID_TIMER )
  14436. {// already awake ???
  14437. node = node->next;
  14438. continue;
  14439. }
  14440. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  14441. {// char not online anymore / another char of the same account is online - Cancel execution
  14442. tst->state = END;
  14443. tst->rid = 0;
  14444. }
  14445. delete_timer(tst->sleep.timer, run_script_timer);
  14446. node = script_erase_sleepdb(node);
  14447. tst->sleep.timer = INVALID_TIMER;
  14448. if(tst->state != RERUNLINE)
  14449. tst->sleep.tick = 0;
  14450. run_script_main(tst);
  14451. }
  14452. else
  14453. {
  14454. node = node->next;
  14455. }
  14456. }
  14457. return SCRIPT_CMD_SUCCESS;
  14458. }
  14459. /// Returns a reference to a variable of the target NPC.
  14460. /// Returns 0 if an error occurs.
  14461. ///
  14462. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  14463. BUILDIN_FUNC(getvariableofnpc)
  14464. {
  14465. struct script_data* data;
  14466. const char* name;
  14467. struct npc_data* nd;
  14468. data = script_getdata(st,2);
  14469. if( !data_isreference(data) )
  14470. {// Not a reference (aka varaible name)
  14471. ShowError("script:getvariableofnpc: not a variable\n");
  14472. script_reportdata(data);
  14473. script_pushnil(st);
  14474. st->state = END;
  14475. return 1;
  14476. }
  14477. name = reference_getname(data);
  14478. if( *name != '.' || name[1] == '@' )
  14479. {// not a npc variable
  14480. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  14481. script_reportdata(data);
  14482. script_pushnil(st);
  14483. st->state = END;
  14484. return 1;
  14485. }
  14486. nd = npc_name2id(script_getstr(st,3));
  14487. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  14488. {// NPC not found or has no script
  14489. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  14490. script_pushnil(st);
  14491. st->state = END;
  14492. return 1;
  14493. }
  14494. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  14495. return SCRIPT_CMD_SUCCESS;
  14496. }
  14497. /// Opens a warp portal.
  14498. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  14499. ///
  14500. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  14501. ///
  14502. /// @author blackhole89
  14503. BUILDIN_FUNC(warpportal)
  14504. {
  14505. int spx;
  14506. int spy;
  14507. unsigned short mapindex;
  14508. int tpx;
  14509. int tpy;
  14510. struct skill_unit_group* group;
  14511. struct block_list* bl;
  14512. bl = map_id2bl(st->oid);
  14513. if( bl == NULL ) {
  14514. ShowError("buildin_warpportal: NPC is needed\n");
  14515. return SCRIPT_CMD_FAILURE;
  14516. }
  14517. spx = script_getnum(st,2);
  14518. spy = script_getnum(st,3);
  14519. mapindex = mapindex_name2id(script_getstr(st, 4));
  14520. tpx = script_getnum(st,5);
  14521. tpy = script_getnum(st,6);
  14522. if( mapindex == 0 ) {
  14523. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  14524. return SCRIPT_CMD_FAILURE;
  14525. }
  14526. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  14527. if( group == NULL )
  14528. return SCRIPT_CMD_FAILURE;// failed
  14529. group->val1 = (group->val1<<16)|(short)0;
  14530. group->val2 = (tpx<<16) | tpy;
  14531. group->val3 = mapindex;
  14532. return SCRIPT_CMD_SUCCESS;
  14533. }
  14534. BUILDIN_FUNC(openmail)
  14535. {
  14536. TBL_PC* sd;
  14537. sd = script_rid2sd(st);
  14538. if( sd == NULL )
  14539. return 0;
  14540. mail_openmail(sd);
  14541. return SCRIPT_CMD_SUCCESS;
  14542. }
  14543. BUILDIN_FUNC(openauction)
  14544. {
  14545. TBL_PC* sd;
  14546. sd = script_rid2sd(st);
  14547. if( sd == NULL )
  14548. return 0;
  14549. if( !battle_config.feature_auction ) {
  14550. clif_colormes(sd, color_table[COLOR_RED], msg_txt(sd, 517));
  14551. return 0;
  14552. }
  14553. clif_Auction_openwindow(sd);
  14554. return SCRIPT_CMD_SUCCESS;
  14555. }
  14556. /// Retrieves the value of the specified flag of the specified cell.
  14557. ///
  14558. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  14559. ///
  14560. /// @see cell_chk* constants in const.txt for the types
  14561. BUILDIN_FUNC(checkcell)
  14562. {
  14563. int16 m = map_mapname2mapid(script_getstr(st,2));
  14564. int16 x = script_getnum(st,3);
  14565. int16 y = script_getnum(st,4);
  14566. cell_chk type = (cell_chk)script_getnum(st,5);
  14567. script_pushint(st, map_getcell(m, x, y, type));
  14568. return SCRIPT_CMD_SUCCESS;
  14569. }
  14570. /// Modifies flags of cells in the specified area.
  14571. ///
  14572. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  14573. ///
  14574. /// @see cell_* constants in const.txt for the types
  14575. BUILDIN_FUNC(setcell)
  14576. {
  14577. int16 m = map_mapname2mapid(script_getstr(st,2));
  14578. int16 x1 = script_getnum(st,3);
  14579. int16 y1 = script_getnum(st,4);
  14580. int16 x2 = script_getnum(st,5);
  14581. int16 y2 = script_getnum(st,6);
  14582. cell_t type = (cell_t)script_getnum(st,7);
  14583. bool flag = (bool)script_getnum(st,8);
  14584. int x,y;
  14585. if( x1 > x2 ) swap(x1,x2);
  14586. if( y1 > y2 ) swap(y1,y2);
  14587. for( y = y1; y <= y2; ++y )
  14588. for( x = x1; x <= x2; ++x )
  14589. map_setcell(m, x, y, type, flag);
  14590. return SCRIPT_CMD_SUCCESS;
  14591. }
  14592. /*==========================================
  14593. * Mercenary Commands
  14594. *------------------------------------------*/
  14595. BUILDIN_FUNC(mercenary_create)
  14596. {
  14597. struct map_session_data *sd;
  14598. int class_, contract_time;
  14599. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  14600. return 0;
  14601. class_ = script_getnum(st,2);
  14602. if( !mercenary_class(class_) )
  14603. return 0;
  14604. contract_time = script_getnum(st,3);
  14605. mercenary_create(sd, class_, contract_time);
  14606. return SCRIPT_CMD_SUCCESS;
  14607. }
  14608. BUILDIN_FUNC(mercenary_heal)
  14609. {
  14610. struct map_session_data *sd = script_rid2sd(st);
  14611. int hp, sp;
  14612. if( sd == NULL || sd->md == NULL )
  14613. return 0;
  14614. hp = script_getnum(st,2);
  14615. sp = script_getnum(st,3);
  14616. status_heal(&sd->md->bl, hp, sp, 0);
  14617. return SCRIPT_CMD_SUCCESS;
  14618. }
  14619. BUILDIN_FUNC(mercenary_sc_start)
  14620. {
  14621. struct map_session_data *sd = script_rid2sd(st);
  14622. enum sc_type type;
  14623. int tick, val1;
  14624. if( sd == NULL || sd->md == NULL )
  14625. return 0;
  14626. type = (sc_type)script_getnum(st,2);
  14627. tick = script_getnum(st,3);
  14628. val1 = script_getnum(st,4);
  14629. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  14630. return SCRIPT_CMD_SUCCESS;
  14631. }
  14632. BUILDIN_FUNC(mercenary_get_calls)
  14633. {
  14634. struct map_session_data *sd = script_rid2sd(st);
  14635. int guild;
  14636. if( sd == NULL )
  14637. return 0;
  14638. guild = script_getnum(st,2);
  14639. switch( guild )
  14640. {
  14641. case ARCH_MERC_GUILD:
  14642. script_pushint(st,sd->status.arch_calls);
  14643. break;
  14644. case SPEAR_MERC_GUILD:
  14645. script_pushint(st,sd->status.spear_calls);
  14646. break;
  14647. case SWORD_MERC_GUILD:
  14648. script_pushint(st,sd->status.sword_calls);
  14649. break;
  14650. default:
  14651. script_pushint(st,0);
  14652. break;
  14653. }
  14654. return SCRIPT_CMD_SUCCESS;
  14655. }
  14656. BUILDIN_FUNC(mercenary_set_calls)
  14657. {
  14658. struct map_session_data *sd = script_rid2sd(st);
  14659. int guild, value, *calls;
  14660. if( sd == NULL )
  14661. return 0;
  14662. guild = script_getnum(st,2);
  14663. value = script_getnum(st,3);
  14664. switch( guild )
  14665. {
  14666. case ARCH_MERC_GUILD:
  14667. calls = &sd->status.arch_calls;
  14668. break;
  14669. case SPEAR_MERC_GUILD:
  14670. calls = &sd->status.spear_calls;
  14671. break;
  14672. case SWORD_MERC_GUILD:
  14673. calls = &sd->status.sword_calls;
  14674. break;
  14675. default:
  14676. return 0; // Invalid Guild
  14677. }
  14678. *calls += value;
  14679. *calls = cap_value(*calls, 0, INT_MAX);
  14680. return SCRIPT_CMD_SUCCESS;
  14681. }
  14682. BUILDIN_FUNC(mercenary_get_faith)
  14683. {
  14684. struct map_session_data *sd = script_rid2sd(st);
  14685. int guild;
  14686. if( sd == NULL )
  14687. return 0;
  14688. guild = script_getnum(st,2);
  14689. switch( guild )
  14690. {
  14691. case ARCH_MERC_GUILD:
  14692. script_pushint(st,sd->status.arch_faith);
  14693. break;
  14694. case SPEAR_MERC_GUILD:
  14695. script_pushint(st,sd->status.spear_faith);
  14696. break;
  14697. case SWORD_MERC_GUILD:
  14698. script_pushint(st,sd->status.sword_faith);
  14699. break;
  14700. default:
  14701. script_pushint(st,0);
  14702. break;
  14703. }
  14704. return SCRIPT_CMD_SUCCESS;
  14705. }
  14706. BUILDIN_FUNC(mercenary_set_faith)
  14707. {
  14708. struct map_session_data *sd = script_rid2sd(st);
  14709. int guild, value, *calls;
  14710. if( sd == NULL )
  14711. return 0;
  14712. guild = script_getnum(st,2);
  14713. value = script_getnum(st,3);
  14714. switch( guild )
  14715. {
  14716. case ARCH_MERC_GUILD:
  14717. calls = &sd->status.arch_faith;
  14718. break;
  14719. case SPEAR_MERC_GUILD:
  14720. calls = &sd->status.spear_faith;
  14721. break;
  14722. case SWORD_MERC_GUILD:
  14723. calls = &sd->status.sword_faith;
  14724. break;
  14725. default:
  14726. return 0; // Invalid Guild
  14727. }
  14728. *calls += value;
  14729. *calls = cap_value(*calls, 0, INT_MAX);
  14730. if( mercenary_get_guild(sd->md) == guild )
  14731. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  14732. return SCRIPT_CMD_SUCCESS;
  14733. }
  14734. /*------------------------------------------
  14735. * Book Reading
  14736. *------------------------------------------*/
  14737. BUILDIN_FUNC(readbook)
  14738. {
  14739. struct map_session_data *sd;
  14740. int book_id, page;
  14741. if( (sd = script_rid2sd(st)) == NULL )
  14742. return 0;
  14743. book_id = script_getnum(st,2);
  14744. page = script_getnum(st,3);
  14745. clif_readbook(sd->fd, book_id, page);
  14746. return SCRIPT_CMD_SUCCESS;
  14747. }
  14748. /******************
  14749. Questlog script commands
  14750. *******************/
  14751. BUILDIN_FUNC(questinfo)
  14752. {
  14753. TBL_NPC* nd = map_id2nd(st->oid);
  14754. int quest_id, icon;
  14755. struct questinfo qi;
  14756. if( nd == NULL || nd->bl.m == -1 )
  14757. return true;
  14758. quest_id = script_getnum(st, 2);
  14759. icon = script_getnum(st, 3);
  14760. #if PACKETVER >= 20120410
  14761. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  14762. icon = 9999; // Default to nothing if icon id is invalid.
  14763. #else
  14764. if(icon < 0 || icon > 7)
  14765. icon = 0;
  14766. else
  14767. icon = icon + 1;
  14768. #endif
  14769. qi.quest_id = quest_id;
  14770. qi.icon = (unsigned char)icon;
  14771. qi.nd = nd;
  14772. if( script_hasdata(st, 4) ) {
  14773. int color = script_getnum(st, 4);
  14774. if( color < 0 || color > 3 ) {
  14775. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  14776. script_reportfunc(st);
  14777. color = 0;
  14778. }
  14779. qi.color = (unsigned char)color;
  14780. }
  14781. qi.hasJob = false;
  14782. if(script_hasdata(st, 5)) {
  14783. int job = script_getnum(st, 5);
  14784. if (!pcdb_checkid(job))
  14785. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  14786. else {
  14787. qi.hasJob = true;
  14788. qi.job = (unsigned short)job;
  14789. }
  14790. }
  14791. map_add_questinfo(nd->bl.m,&qi);
  14792. return true;
  14793. }
  14794. BUILDIN_FUNC(setquest)
  14795. {
  14796. struct map_session_data *sd = script_rid2sd(st);
  14797. unsigned short i;
  14798. int quest_id;
  14799. nullpo_retr(1, sd);
  14800. quest_id = script_getnum(st, 2);
  14801. quest_add(sd, quest_id);
  14802. // If questinfo is set, remove quest bubble once quest is set.
  14803. for(i = 0; i < map[sd->bl.m].qi_count; i++) {
  14804. struct questinfo *qi = &map[sd->bl.m].qi_data[i];
  14805. if( qi->quest_id == quest_id ) {
  14806. #if PACKETVER >= 20120410
  14807. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  14808. #else
  14809. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  14810. #endif
  14811. }
  14812. }
  14813. return SCRIPT_CMD_SUCCESS;
  14814. }
  14815. BUILDIN_FUNC(erasequest)
  14816. {
  14817. struct map_session_data *sd = script_rid2sd(st);
  14818. nullpo_ret(sd);
  14819. quest_delete(sd, script_getnum(st, 2));
  14820. return SCRIPT_CMD_SUCCESS;
  14821. }
  14822. BUILDIN_FUNC(completequest)
  14823. {
  14824. struct map_session_data *sd = script_rid2sd(st);
  14825. nullpo_ret(sd);
  14826. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  14827. return SCRIPT_CMD_SUCCESS;
  14828. }
  14829. BUILDIN_FUNC(changequest)
  14830. {
  14831. struct map_session_data *sd = script_rid2sd(st);
  14832. nullpo_ret(sd);
  14833. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  14834. return SCRIPT_CMD_SUCCESS;
  14835. }
  14836. BUILDIN_FUNC(checkquest)
  14837. {
  14838. struct map_session_data *sd = script_rid2sd(st);
  14839. enum quest_check_type type = HAVEQUEST;
  14840. nullpo_ret(sd);
  14841. if( script_hasdata(st, 3) )
  14842. type = (enum quest_check_type)script_getnum(st, 3);
  14843. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  14844. return SCRIPT_CMD_SUCCESS;
  14845. }
  14846. BUILDIN_FUNC(isbegin_quest)
  14847. {
  14848. struct map_session_data *sd = script_rid2sd(st);
  14849. int i;
  14850. nullpo_ret(sd);
  14851. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  14852. script_pushint(st, i + (i < 1));
  14853. return SCRIPT_CMD_SUCCESS;
  14854. }
  14855. BUILDIN_FUNC(showevent)
  14856. {
  14857. TBL_PC *sd = script_rid2sd(st);
  14858. struct npc_data *nd = map_id2nd(st->oid);
  14859. int icon, color = 0;
  14860. if( sd == NULL || nd == NULL )
  14861. return 0;
  14862. icon = script_getnum(st, 2);
  14863. if( script_hasdata(st, 3) ) {
  14864. color = script_getnum(st, 3);
  14865. if( color < 0 || color > 3 ) {
  14866. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  14867. script_reportfunc(st);
  14868. color = 0;
  14869. }
  14870. }
  14871. #if PACKETVER >= 20120410
  14872. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  14873. icon = 9999; // Default to nothing if icon id is invalid.
  14874. #else
  14875. if(icon < 0 || icon > 7)
  14876. icon = 0;
  14877. else
  14878. icon = icon + 1;
  14879. #endif
  14880. clif_quest_show_event(sd, &nd->bl, icon, color);
  14881. return SCRIPT_CMD_SUCCESS;
  14882. }
  14883. /*==========================================
  14884. * BattleGround System
  14885. *------------------------------------------*/
  14886. BUILDIN_FUNC(waitingroom2bg)
  14887. {
  14888. struct npc_data *nd;
  14889. struct chat_data *cd;
  14890. const char *map_name, *ev = "", *dev = "";
  14891. int x, y, mapindex = 0, bg_id, n;
  14892. unsigned char i;
  14893. if( script_hasdata(st,7) )
  14894. nd = npc_name2id(script_getstr(st,7));
  14895. else
  14896. nd = (struct npc_data *)map_id2bl(st->oid);
  14897. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  14898. {
  14899. script_pushint(st,0);
  14900. return 0;
  14901. }
  14902. map_name = script_getstr(st,2);
  14903. if( strcmp(map_name,"-") != 0 )
  14904. {
  14905. mapindex = mapindex_name2id(map_name);
  14906. if( mapindex == 0 )
  14907. { // Invalid Map
  14908. script_pushint(st,0);
  14909. return 0;
  14910. }
  14911. }
  14912. x = script_getnum(st,3);
  14913. y = script_getnum(st,4);
  14914. ev = script_getstr(st,5); // Logout Event
  14915. dev = script_getstr(st,6); // Die Event
  14916. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  14917. { // Creation failed
  14918. script_pushint(st,0);
  14919. return 0;
  14920. }
  14921. n = cd->users;
  14922. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  14923. {
  14924. struct map_session_data *sd;
  14925. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  14926. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  14927. else
  14928. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  14929. }
  14930. mapreg_setreg(add_str("$@arenamembersnum"), i);
  14931. script_pushint(st,bg_id);
  14932. return SCRIPT_CMD_SUCCESS;
  14933. }
  14934. BUILDIN_FUNC(waitingroom2bg_single)
  14935. {
  14936. const char* map_name;
  14937. struct npc_data *nd;
  14938. struct chat_data *cd;
  14939. struct map_session_data *sd;
  14940. int x, y, mapindex, bg_id;
  14941. bg_id = script_getnum(st,2);
  14942. map_name = script_getstr(st,3);
  14943. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14944. return 0; // Invalid Map
  14945. x = script_getnum(st,4);
  14946. y = script_getnum(st,5);
  14947. nd = npc_name2id(script_getstr(st,6));
  14948. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  14949. return 0;
  14950. if( (sd = cd->usersd[0]) == NULL )
  14951. return 0;
  14952. if( bg_team_join(bg_id, sd) )
  14953. {
  14954. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  14955. script_pushint(st,1);
  14956. }
  14957. else
  14958. script_pushint(st,0);
  14959. return SCRIPT_CMD_SUCCESS;
  14960. }
  14961. BUILDIN_FUNC(bg_team_setxy)
  14962. {
  14963. struct battleground_data *bg;
  14964. int bg_id;
  14965. bg_id = script_getnum(st,2);
  14966. if( (bg = bg_team_search(bg_id)) == NULL )
  14967. return 0;
  14968. bg->x = script_getnum(st,3);
  14969. bg->y = script_getnum(st,4);
  14970. return SCRIPT_CMD_SUCCESS;
  14971. }
  14972. BUILDIN_FUNC(bg_warp)
  14973. {
  14974. int x, y, mapindex, bg_id;
  14975. const char* map_name;
  14976. bg_id = script_getnum(st,2);
  14977. map_name = script_getstr(st,3);
  14978. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14979. return 0; // Invalid Map
  14980. x = script_getnum(st,4);
  14981. y = script_getnum(st,5);
  14982. bg_team_warp(bg_id, mapindex, x, y);
  14983. return SCRIPT_CMD_SUCCESS;
  14984. }
  14985. BUILDIN_FUNC(bg_monster)
  14986. {
  14987. int class_ = 0, x = 0, y = 0, bg_id = 0;
  14988. const char *str,*mapname, *evt="";
  14989. bg_id = script_getnum(st,2);
  14990. mapname = script_getstr(st,3);
  14991. x = script_getnum(st,4);
  14992. y = script_getnum(st,5);
  14993. str = script_getstr(st,6);
  14994. class_ = script_getnum(st,7);
  14995. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  14996. check_event(st, evt);
  14997. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  14998. return SCRIPT_CMD_SUCCESS;
  14999. }
  15000. BUILDIN_FUNC(bg_monster_set_team)
  15001. {
  15002. struct mob_data *md;
  15003. struct block_list *mbl;
  15004. int id = script_getnum(st,2),
  15005. bg_id = script_getnum(st,3);
  15006. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  15007. return 0;
  15008. md = (TBL_MOB *)mbl;
  15009. md->bg_id = bg_id;
  15010. mob_stop_attack(md);
  15011. mob_stop_walking(md, 0);
  15012. md->target_id = md->attacked_id = 0;
  15013. clif_charnameack(0, &md->bl);
  15014. return SCRIPT_CMD_SUCCESS;
  15015. }
  15016. BUILDIN_FUNC(bg_leave)
  15017. {
  15018. struct map_session_data *sd = script_rid2sd(st);
  15019. if( sd == NULL || !sd->bg_id )
  15020. return 0;
  15021. bg_team_leave(sd,0);
  15022. return SCRIPT_CMD_SUCCESS;
  15023. }
  15024. BUILDIN_FUNC(bg_destroy)
  15025. {
  15026. int bg_id = script_getnum(st,2);
  15027. bg_team_delete(bg_id);
  15028. return SCRIPT_CMD_SUCCESS;
  15029. }
  15030. BUILDIN_FUNC(bg_getareausers)
  15031. {
  15032. const char *str;
  15033. int16 m, x0, y0, x1, y1;
  15034. int bg_id;
  15035. int i = 0, c = 0;
  15036. struct battleground_data *bg = NULL;
  15037. bg_id = script_getnum(st,2);
  15038. str = script_getstr(st,3);
  15039. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  15040. {
  15041. script_pushint(st,0);
  15042. return 0;
  15043. }
  15044. x0 = script_getnum(st,4);
  15045. y0 = script_getnum(st,5);
  15046. x1 = script_getnum(st,6);
  15047. y1 = script_getnum(st,7);
  15048. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  15049. {
  15050. struct map_session_data *sd;
  15051. if( (sd = bg->members[i].sd) == NULL )
  15052. continue;
  15053. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  15054. continue;
  15055. c++;
  15056. }
  15057. script_pushint(st,c);
  15058. return SCRIPT_CMD_SUCCESS;
  15059. }
  15060. BUILDIN_FUNC(bg_updatescore)
  15061. {
  15062. const char *str;
  15063. int16 m;
  15064. str = script_getstr(st,2);
  15065. if( (m = map_mapname2mapid(str)) < 0 )
  15066. return 0;
  15067. map[m].bgscore_lion = script_getnum(st,3);
  15068. map[m].bgscore_eagle = script_getnum(st,4);
  15069. clif_bg_updatescore(m);
  15070. return SCRIPT_CMD_SUCCESS;
  15071. }
  15072. BUILDIN_FUNC(bg_get_data)
  15073. {
  15074. struct battleground_data *bg;
  15075. int bg_id = script_getnum(st,2),
  15076. type = script_getnum(st,3);
  15077. if( (bg = bg_team_search(bg_id)) == NULL )
  15078. {
  15079. script_pushint(st,0);
  15080. return 0;
  15081. }
  15082. switch( type )
  15083. {
  15084. case 0: script_pushint(st, bg->count); break;
  15085. default:
  15086. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  15087. break;
  15088. }
  15089. return SCRIPT_CMD_SUCCESS;
  15090. }
  15091. /*==========================================
  15092. * Instancing System
  15093. *------------------------------------------*/
  15094. //Returns an Instance ID
  15095. //Checks NPC first, then if player is attached we check party
  15096. int script_instancegetid(struct script_state* st)
  15097. {
  15098. short instance_id = 0;
  15099. struct npc_data *nd;
  15100. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  15101. instance_id = nd->instance_id;
  15102. else {
  15103. struct map_session_data *sd;
  15104. struct party_data *p;
  15105. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  15106. instance_id = p->instance_id;
  15107. }
  15108. return instance_id;
  15109. }
  15110. /*==========================================
  15111. * Creates the instance
  15112. * Returns Instance ID if created successfully
  15113. *------------------------------------------*/
  15114. BUILDIN_FUNC(instance_create)
  15115. {
  15116. struct map_session_data *sd;
  15117. if((sd = script_rid2sd(st)) == NULL)
  15118. return -1;
  15119. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  15120. return SCRIPT_CMD_SUCCESS;
  15121. }
  15122. /*==========================================
  15123. * Destroys an instance (unofficial)
  15124. * Officially instances are only destroyed by timeout
  15125. *
  15126. * instance_destroy {<instance_id>};
  15127. *------------------------------------------*/
  15128. BUILDIN_FUNC(instance_destroy)
  15129. {
  15130. short instance_id;
  15131. if( script_hasdata(st,2) )
  15132. instance_id = script_getnum(st,2);
  15133. else
  15134. instance_id = script_instancegetid(st);
  15135. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  15136. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  15137. return 0;
  15138. }
  15139. instance_destroy(instance_id);
  15140. return SCRIPT_CMD_SUCCESS;
  15141. }
  15142. /*==========================================
  15143. * Warps player to instance
  15144. * Results:
  15145. * 0: Success
  15146. * 1: Character not in party
  15147. * 2: Party doesn't have instance
  15148. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  15149. *------------------------------------------*/
  15150. BUILDIN_FUNC(instance_enter)
  15151. {
  15152. struct map_session_data *sd;
  15153. if((sd = script_rid2sd(st)) != NULL){
  15154. if (script_hasdata(st,3) && script_hasdata(st,4))
  15155. script_pushint(st,instance_enter_position(sd,script_getstr(st, 2),script_getnum(st, 3),script_getnum(st, 4)));
  15156. else
  15157. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  15158. }
  15159. else
  15160. return 1;
  15161. return SCRIPT_CMD_SUCCESS;
  15162. }
  15163. /*==========================================
  15164. * Returns the name of a duplicated NPC
  15165. *
  15166. * instance_npcname <npc_name>{,<instance_id>};
  15167. * <npc_name> is the full name of an NPC.
  15168. *------------------------------------------*/
  15169. BUILDIN_FUNC(instance_npcname)
  15170. {
  15171. const char *str;
  15172. short instance_id = 0;
  15173. struct npc_data *nd;
  15174. str = script_getstr(st,2);
  15175. if( script_hasdata(st,3) )
  15176. instance_id = script_getnum(st,3);
  15177. else
  15178. instance_id = script_instancegetid(st);
  15179. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  15180. static char npcname[NAME_LENGTH];
  15181. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  15182. script_pushconststr(st,npcname);
  15183. } else {
  15184. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  15185. st->state = END;
  15186. return 1;
  15187. }
  15188. return SCRIPT_CMD_SUCCESS;
  15189. }
  15190. /*==========================================
  15191. * Returns the name of a duplicated map
  15192. *
  15193. * instance_mapname <map_name>{,<instance_id>};
  15194. *------------------------------------------*/
  15195. BUILDIN_FUNC(instance_mapname)
  15196. {
  15197. const char *str;
  15198. int16 m;
  15199. short instance_id = 0;
  15200. str = script_getstr(st,2);
  15201. if( script_hasdata(st,3) )
  15202. instance_id = script_getnum(st,3);
  15203. else
  15204. instance_id = script_instancegetid(st);
  15205. // Check that instance mapname is a valid map
  15206. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  15207. script_pushconststr(st, "");
  15208. else
  15209. script_pushconststr(st, map[m].name);
  15210. return SCRIPT_CMD_SUCCESS;
  15211. }
  15212. /*==========================================
  15213. * Returns an Instance ID
  15214. *------------------------------------------*/
  15215. BUILDIN_FUNC(instance_id)
  15216. {
  15217. short instance_id;
  15218. instance_id = script_instancegetid(st);
  15219. if(!instance_id) {
  15220. //ShowError("script:instance_id: No instance attached to NPC or player");
  15221. script_pushint(st, 0);
  15222. return 1;
  15223. }
  15224. script_pushint(st, instance_id);
  15225. return SCRIPT_CMD_SUCCESS;
  15226. }
  15227. /*==========================================
  15228. * Warps all players inside an instance
  15229. *
  15230. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  15231. *------------------------------------------*/
  15232. BUILDIN_FUNC(instance_warpall)
  15233. {
  15234. struct party_data *p;
  15235. int16 m, i;
  15236. short instance_id;
  15237. const char *mapn;
  15238. int x, y;
  15239. mapn = script_getstr(st,2);
  15240. x = script_getnum(st,3);
  15241. y = script_getnum(st,4);
  15242. if( script_hasdata(st,5) )
  15243. instance_id = script_getnum(st,5);
  15244. else
  15245. instance_id = script_instancegetid(st);
  15246. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  15247. return 0;
  15248. if( !(p = party_search(instance_data[instance_id].party_id)) )
  15249. return 0;
  15250. for( i = 0; i < MAX_PARTY; i++ ) {
  15251. struct map_session_data *pl_sd;
  15252. 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);
  15253. }
  15254. return SCRIPT_CMD_SUCCESS;
  15255. }
  15256. /*==========================================
  15257. * Broadcasts to all maps inside an instance
  15258. *
  15259. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  15260. * Using -1 for <instance id> will auto-detect the id.
  15261. *------------------------------------------*/
  15262. BUILDIN_FUNC(instance_announce) {
  15263. int instance_id = script_getnum(st,2);
  15264. const char *mes = script_getstr(st,3);
  15265. int flag = script_getnum(st,4);
  15266. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  15267. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  15268. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  15269. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  15270. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  15271. int i;
  15272. if( instance_id == -1 ) {
  15273. instance_id = script_instancegetid(st);
  15274. }
  15275. if( !instance_id && &instance_data[instance_id] != NULL)
  15276. return true;
  15277. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  15278. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  15279. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  15280. return SCRIPT_CMD_SUCCESS;
  15281. }
  15282. /*==========================================
  15283. * instance_check_party [malufett]
  15284. * Values:
  15285. * party_id : Party ID of the invoking character. [Required Parameter]
  15286. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  15287. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  15288. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  15289. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  15290. * Example 2: instance_check_party (getcharid(1),1,1,99);
  15291. *------------------------------------------*/
  15292. BUILDIN_FUNC(instance_check_party)
  15293. {
  15294. int amount, min, max, i, party_id, c = 0;
  15295. struct party_data *p = NULL;
  15296. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  15297. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  15298. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  15299. if( min < 1 || min > MAX_LEVEL) {
  15300. ShowError("script:check_party: Invalid min level, %d\n", min);
  15301. return 1;
  15302. } else if( max < 1 || max > MAX_LEVEL) {
  15303. ShowError("script:check_party: Invalid max level, %d\n", max);
  15304. return 1;
  15305. }
  15306. if( script_hasdata(st,2) )
  15307. party_id = script_getnum(st,2);
  15308. else return 0;
  15309. if( !(p = party_search(party_id)) ) {
  15310. script_pushint(st, 0); // Returns false if party does not exist.
  15311. return 0;
  15312. }
  15313. for( i = 0; i < MAX_PARTY; i++ ) {
  15314. struct map_session_data *pl_sd;
  15315. if( (pl_sd = p->data[i].sd) )
  15316. if(map_id2bl(pl_sd->bl.id)) {
  15317. if(pl_sd->status.base_level < min) {
  15318. script_pushint(st, 0);
  15319. return 0;
  15320. } else if(pl_sd->status.base_level > max) {
  15321. script_pushint(st, 0);
  15322. return 0;
  15323. }
  15324. c++;
  15325. }
  15326. }
  15327. if(c < amount)
  15328. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  15329. else
  15330. script_pushint(st, 1);
  15331. return SCRIPT_CMD_SUCCESS;
  15332. }
  15333. /*==========================================
  15334. * Custom Fonts
  15335. *------------------------------------------*/
  15336. BUILDIN_FUNC(setfont)
  15337. {
  15338. struct map_session_data *sd = script_rid2sd(st);
  15339. int font = script_getnum(st,2);
  15340. if( sd == NULL )
  15341. return 0;
  15342. if( sd->status.font != font )
  15343. sd->status.font = font;
  15344. else
  15345. sd->status.font = 0;
  15346. clif_font(sd);
  15347. return SCRIPT_CMD_SUCCESS;
  15348. }
  15349. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  15350. {
  15351. TBL_MOB* md = (TBL_MOB*)bl;
  15352. struct block_list *tbl;
  15353. int mobid = va_arg(ap,int);
  15354. uint16 skill_id = va_arg(ap,int);
  15355. uint16 skill_lv = va_arg(ap,int);
  15356. int casttime = va_arg(ap,int);
  15357. int cancel = va_arg(ap,int);
  15358. int emotion = va_arg(ap,int);
  15359. int target = va_arg(ap,int);
  15360. if( md->mob_id != mobid )
  15361. return 0;
  15362. // 0:self, 1:target, 2:master, default:random
  15363. switch( target ) {
  15364. case 0: tbl = map_id2bl(md->bl.id); break;
  15365. case 1: tbl = map_id2bl(md->target_id); break;
  15366. case 2: tbl = map_id2bl(md->master_id); break;
  15367. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  15368. }
  15369. if( !tbl )
  15370. return 0;
  15371. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  15372. unit_skillcastcancel(bl,0);
  15373. if( skill_get_casttype(skill_id) == CAST_GROUND )
  15374. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  15375. else
  15376. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  15377. clif_emotion(&md->bl, emotion);
  15378. return SCRIPT_CMD_SUCCESS;
  15379. }
  15380. /*==========================================
  15381. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  15382. *------------------------------------------*/
  15383. BUILDIN_FUNC(areamobuseskill)
  15384. {
  15385. struct block_list center;
  15386. struct script_data *data;
  15387. int16 m;
  15388. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  15389. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  15390. ShowError("areamobuseskill: invalid map name.\n");
  15391. return 0;
  15392. }
  15393. center.m = m;
  15394. center.x = script_getnum(st,3);
  15395. center.y = script_getnum(st,4);
  15396. range = script_getnum(st,5);
  15397. mobid = script_getnum(st,6);
  15398. data = script_getdata(st, 7);
  15399. get_val(st, data); // Convert into value in case of a variable
  15400. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  15401. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  15402. skill_lv = battle_config.mob_max_skilllvl;
  15403. casttime = script_getnum(st,9);
  15404. cancel = script_getnum(st,10);
  15405. emotion = script_getnum(st,11);
  15406. target = script_getnum(st,12);
  15407. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  15408. return SCRIPT_CMD_SUCCESS;
  15409. }
  15410. BUILDIN_FUNC(progressbar)
  15411. {
  15412. struct map_session_data * sd = script_rid2sd(st);
  15413. const char * color;
  15414. unsigned int second;
  15415. if( !st || !sd )
  15416. return 0;
  15417. st->state = STOP;
  15418. color = script_getstr(st,2);
  15419. second = script_getnum(st,3);
  15420. sd->progressbar.npc_id = st->oid;
  15421. sd->progressbar.timeout = gettick() + second*1000;
  15422. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  15423. return SCRIPT_CMD_SUCCESS;
  15424. }
  15425. BUILDIN_FUNC(pushpc)
  15426. {
  15427. uint8 dir;
  15428. int cells, dx, dy;
  15429. struct map_session_data* sd;
  15430. if((sd = script_rid2sd(st))==NULL)
  15431. {
  15432. return 0;
  15433. }
  15434. dir = script_getnum(st,2);
  15435. cells = script_getnum(st,3);
  15436. if(dir>7)
  15437. {
  15438. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  15439. script_reportsrc(st);
  15440. dir%= 8; // trim spin-over
  15441. }
  15442. if(!cells)
  15443. {// zero distance
  15444. return 0;
  15445. }
  15446. else if(cells<0)
  15447. {// pushing backwards
  15448. dir = (dir+4)%8; // turn around
  15449. cells = -cells;
  15450. }
  15451. dx = dirx[dir];
  15452. dy = diry[dir];
  15453. unit_blown(&sd->bl, dx, dy, cells, 0);
  15454. return SCRIPT_CMD_SUCCESS;
  15455. }
  15456. /// Invokes buying store preparation window
  15457. /// buyingstore <slots>;
  15458. BUILDIN_FUNC(buyingstore)
  15459. {
  15460. struct map_session_data* sd;
  15461. if( ( sd = script_rid2sd(st) ) == NULL )
  15462. {
  15463. return 0;
  15464. }
  15465. if( npc_isnear(&sd->bl) ) {
  15466. char output[150];
  15467. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  15468. clif_displaymessage(sd->fd, output);
  15469. return 0;
  15470. }
  15471. buyingstore_setup(sd, script_getnum(st,2));
  15472. return SCRIPT_CMD_SUCCESS;
  15473. }
  15474. /// Invokes search store info window
  15475. /// searchstores <uses>,<effect>;
  15476. BUILDIN_FUNC(searchstores)
  15477. {
  15478. unsigned short effect;
  15479. unsigned int uses;
  15480. struct map_session_data* sd;
  15481. if( ( sd = script_rid2sd(st) ) == NULL )
  15482. {
  15483. return 0;
  15484. }
  15485. uses = script_getnum(st,2);
  15486. effect = script_getnum(st,3);
  15487. if( !uses )
  15488. {
  15489. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  15490. return 1;
  15491. }
  15492. if( effect > 1 )
  15493. {
  15494. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  15495. return 1;
  15496. }
  15497. searchstore_open(sd, uses, effect);
  15498. return SCRIPT_CMD_SUCCESS;
  15499. }
  15500. /// Displays a number as large digital clock.
  15501. /// showdigit <value>[,<type>];
  15502. BUILDIN_FUNC(showdigit)
  15503. {
  15504. unsigned int type = 0;
  15505. int value;
  15506. struct map_session_data* sd;
  15507. if( ( sd = script_rid2sd(st) ) == NULL )
  15508. {
  15509. return 0;
  15510. }
  15511. value = script_getnum(st,2);
  15512. if( script_hasdata(st,3) )
  15513. {
  15514. type = script_getnum(st,3);
  15515. if( type > 3 )
  15516. {
  15517. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  15518. return 1;
  15519. }
  15520. }
  15521. clif_showdigit(sd, (unsigned char)type, value);
  15522. return SCRIPT_CMD_SUCCESS;
  15523. }
  15524. /**
  15525. * Rune Knight
  15526. **/
  15527. BUILDIN_FUNC(makerune) {
  15528. TBL_PC* sd;
  15529. if( (sd = script_rid2sd(st)) == NULL )
  15530. return 0;
  15531. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  15532. sd->itemid = script_getnum(st,2);
  15533. return SCRIPT_CMD_SUCCESS;
  15534. }
  15535. /**
  15536. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  15537. **/
  15538. BUILDIN_FUNC(checkdragon) {
  15539. TBL_PC* sd;
  15540. if( (sd = script_rid2sd(st)) == NULL )
  15541. return 0;
  15542. if( pc_isridingdragon(sd) )
  15543. script_pushint(st,1);
  15544. else
  15545. script_pushint(st,0);
  15546. return SCRIPT_CMD_SUCCESS;
  15547. }
  15548. /**
  15549. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  15550. * - Toggles the dragon on a RK if he can mount;
  15551. * @param Color - when not provided uses the green dragon;
  15552. * - 1 : Green Dragon
  15553. * - 2 : Brown Dragon
  15554. * - 3 : Gray Dragon
  15555. * - 4 : Blue Dragon
  15556. * - 5 : Red Dragon
  15557. **/
  15558. BUILDIN_FUNC(setdragon) {
  15559. TBL_PC* sd;
  15560. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  15561. if( (sd = script_rid2sd(st)) == NULL )
  15562. return 0;
  15563. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  15564. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  15565. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  15566. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  15567. script_pushint(st,1);
  15568. } else {//Not mounted; Mount now.
  15569. unsigned int option = OPTION_DRAGON1;
  15570. if( color ) {
  15571. option = ( color == 1 ? OPTION_DRAGON1 :
  15572. color == 2 ? OPTION_DRAGON2 :
  15573. color == 3 ? OPTION_DRAGON3 :
  15574. color == 4 ? OPTION_DRAGON4 :
  15575. color == 5 ? OPTION_DRAGON5 : 0);
  15576. if( !option ) {
  15577. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  15578. option = OPTION_DRAGON1;
  15579. }
  15580. }
  15581. pc_setoption(sd, sd->sc.option|option);
  15582. script_pushint(st,1);
  15583. }
  15584. return SCRIPT_CMD_SUCCESS;
  15585. }
  15586. /**
  15587. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  15588. **/
  15589. BUILDIN_FUNC(ismounting) {
  15590. TBL_PC* sd;
  15591. if( (sd = script_rid2sd(st)) == NULL )
  15592. return 0;
  15593. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  15594. script_pushint(st,1);
  15595. else
  15596. script_pushint(st,0);
  15597. return SCRIPT_CMD_SUCCESS;
  15598. }
  15599. /**
  15600. * setmounting() returns 1 on success or 0 otherwise
  15601. * - Toggles new mounts on a player when he can mount
  15602. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  15603. * - Will unmount the player is he is already mounting
  15604. **/
  15605. BUILDIN_FUNC(setmounting) {
  15606. TBL_PC* sd;
  15607. if( (sd = script_rid2sd(st)) == NULL )
  15608. return 0;
  15609. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  15610. clif_msgtable(sd->fd, NEED_REINS_OF_MOUNT);
  15611. script_pushint(st,0); //can't mount with one of these
  15612. } else {
  15613. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  15614. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  15615. else
  15616. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  15617. script_pushint(st,1);//in both cases, return 1.
  15618. }
  15619. return SCRIPT_CMD_SUCCESS;
  15620. }
  15621. /**
  15622. * Retrieves quantity of arguments provided to callfunc/callsub.
  15623. * getargcount() -> amount of arguments received in a function
  15624. **/
  15625. BUILDIN_FUNC(getargcount) {
  15626. struct script_retinfo* ri;
  15627. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  15628. ShowError("script:getargcount: used out of function or callsub label!\n");
  15629. st->state = END;
  15630. return 1;
  15631. }
  15632. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  15633. script_pushint(st, ri->nargs);
  15634. return SCRIPT_CMD_SUCCESS;
  15635. }
  15636. /**
  15637. * getcharip(<account ID>/<character ID>/<character name>)
  15638. **/
  15639. BUILDIN_FUNC(getcharip)
  15640. {
  15641. struct map_session_data* sd = NULL;
  15642. /* check if a character name is specified */
  15643. if( script_hasdata(st, 2) )
  15644. {
  15645. struct script_data *data;
  15646. data = script_getdata(st, 2);
  15647. get_val(st, data); // Convert into value in case of a variable
  15648. if (data_isstring(data))
  15649. sd = map_nick2sd(script_getstr(st, 2));
  15650. else if (data_isint(data) || script_getnum(st, 2))
  15651. {
  15652. int id = 0;
  15653. id = script_getnum(st, 2);
  15654. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  15655. }
  15656. }
  15657. else
  15658. sd = script_rid2sd(st);
  15659. /* check for sd and IP */
  15660. if (!sd || !session[sd->fd]->client_addr)
  15661. {
  15662. script_pushconststr(st, "");
  15663. return 0;
  15664. }
  15665. /* return the client ip_addr converted for output */
  15666. if (sd && sd->fd && session[sd->fd])
  15667. {
  15668. /* initiliaze */
  15669. const char *ip_addr = NULL;
  15670. uint32 ip;
  15671. /* set ip, ip_addr and convert to ip and push str */
  15672. ip = session[sd->fd]->client_addr;
  15673. ip_addr = ip2str(ip, NULL);
  15674. script_pushstrcopy(st, ip_addr);
  15675. }
  15676. return SCRIPT_CMD_SUCCESS;
  15677. }
  15678. /**
  15679. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  15680. **/
  15681. BUILDIN_FUNC(is_function) {
  15682. const char* str = script_getstr(st,2);
  15683. if( strdb_exists(userfunc_db, str) )
  15684. script_pushint(st,1);
  15685. else
  15686. script_pushint(st,0);
  15687. return SCRIPT_CMD_SUCCESS;
  15688. }
  15689. /**
  15690. * get_revision() -> retrieves the current svn revision (if available)
  15691. **/
  15692. BUILDIN_FUNC(get_revision) {
  15693. const char *svn = get_svn_revision();
  15694. if ( svn[0] != UNKNOWN_VERSION )
  15695. script_pushint(st,atoi(svn));
  15696. else
  15697. script_pushint(st,-1); //unknown
  15698. return SCRIPT_CMD_SUCCESS;
  15699. }
  15700. /* get_hash() -> retrieves the current git hash (if available)*/
  15701. BUILDIN_FUNC(get_githash) {
  15702. const char* git = get_git_hash();
  15703. char buf[CHAT_SIZE_MAX];
  15704. safestrncpy(buf,git,strlen(git)+1);
  15705. if ( git[0] != UNKNOWN_VERSION )
  15706. script_pushstr(st,buf);
  15707. else
  15708. script_pushstr(st,"Unknown"); //unknown
  15709. return SCRIPT_CMD_SUCCESS;
  15710. }
  15711. /**
  15712. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  15713. **/
  15714. BUILDIN_FUNC(freeloop) {
  15715. if( script_hasdata(st,2) ) {
  15716. if( script_getnum(st,2) )
  15717. st->freeloop = 1;
  15718. else
  15719. st->freeloop = 0;
  15720. }
  15721. script_pushint(st, st->freeloop);
  15722. return SCRIPT_CMD_SUCCESS;
  15723. }
  15724. /**
  15725. * @commands (script based)
  15726. **/
  15727. BUILDIN_FUNC(bindatcmd) {
  15728. const char* atcmd;
  15729. const char* eventName;
  15730. int i, level = 0, level2 = 0;
  15731. bool create = false;
  15732. atcmd = script_getstr(st,2);
  15733. eventName = script_getstr(st,3);
  15734. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15735. atcmd++;
  15736. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  15737. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  15738. if( atcmd_binding_count == 0 ) {
  15739. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  15740. create = true;
  15741. } else {
  15742. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  15743. if( i < atcmd_binding_count ) {/* update existent entry */
  15744. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15745. atcmd_binding[i]->level = level;
  15746. atcmd_binding[i]->level2 = level2;
  15747. } else
  15748. create = true;
  15749. }
  15750. if( create ) {
  15751. i = atcmd_binding_count;
  15752. if( atcmd_binding_count++ != 0 )
  15753. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  15754. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  15755. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  15756. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15757. atcmd_binding[i]->level = level;
  15758. atcmd_binding[i]->level2 = level2;
  15759. }
  15760. return SCRIPT_CMD_SUCCESS;
  15761. }
  15762. BUILDIN_FUNC(unbindatcmd) {
  15763. const char* atcmd;
  15764. int i = 0;
  15765. atcmd = script_getstr(st, 2);
  15766. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15767. atcmd++;
  15768. if( atcmd_binding_count == 0 ) {
  15769. script_pushint(st, 0);
  15770. return 0;
  15771. }
  15772. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  15773. if( i < atcmd_binding_count ) {
  15774. int cursor = 0;
  15775. aFree(atcmd_binding[i]);
  15776. atcmd_binding[i] = NULL;
  15777. /* compact the list now that we freed a slot somewhere */
  15778. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  15779. if( atcmd_binding[i] == NULL )
  15780. continue;
  15781. if( cursor != i ) {
  15782. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  15783. }
  15784. cursor++;
  15785. }
  15786. if( (atcmd_binding_count = cursor) == 0 )
  15787. aFree(atcmd_binding);
  15788. script_pushint(st, 1);
  15789. } else
  15790. script_pushint(st, 0);/* not found */
  15791. return SCRIPT_CMD_SUCCESS;
  15792. }
  15793. BUILDIN_FUNC(useatcmd) {
  15794. return atcommand_sub(st,3);
  15795. }
  15796. BUILDIN_FUNC(checkre)
  15797. {
  15798. int num;
  15799. num=script_getnum(st,2);
  15800. switch(num){
  15801. case 0:
  15802. #ifdef RENEWAL
  15803. script_pushint(st, 1);
  15804. #else
  15805. script_pushint(st, 0);
  15806. #endif
  15807. break;
  15808. case 1:
  15809. #ifdef RENEWAL_CAST
  15810. script_pushint(st, 1);
  15811. #else
  15812. script_pushint(st, 0);
  15813. #endif
  15814. break;
  15815. case 2:
  15816. #ifdef RENEWAL_DROP
  15817. script_pushint(st, 1);
  15818. #else
  15819. script_pushint(st, 0);
  15820. #endif
  15821. break;
  15822. case 3:
  15823. #ifdef RENEWAL_EXP
  15824. script_pushint(st, 1);
  15825. #else
  15826. script_pushint(st, 0);
  15827. #endif
  15828. break;
  15829. case 4:
  15830. #ifdef RENEWAL_LVDMG
  15831. script_pushint(st, 1);
  15832. #else
  15833. script_pushint(st, 0);
  15834. #endif
  15835. break;
  15836. case 5:
  15837. #ifdef RENEWAL_ASPD
  15838. script_pushint(st, 1);
  15839. #else
  15840. script_pushint(st, 0);
  15841. #endif
  15842. break;
  15843. default:
  15844. ShowWarning("buildin_checkre: unknown parameter.\n");
  15845. break;
  15846. }
  15847. return SCRIPT_CMD_SUCCESS;
  15848. }
  15849. /* getrandgroupitem <group_id>,<quantity>{,<sub_group>} */
  15850. BUILDIN_FUNC(getrandgroupitem) {
  15851. TBL_PC* sd;
  15852. int i, get_count = 0;
  15853. unsigned short nameid;
  15854. uint16 group = script_getnum(st,2), qty = script_getnum(st,3);
  15855. uint8 sub_group = script_getnum(st,4);
  15856. struct item item_tmp;
  15857. if (!( sd = script_rid2sd(st)))
  15858. return SCRIPT_CMD_SUCCESS;
  15859. if (!group) {
  15860. ShowError("getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  15861. return SCRIPT_CMD_FAILURE;
  15862. }
  15863. if ((nameid = itemdb_searchrandomid(group,sub_group)) == UNKNOWN_ITEM_ID) {
  15864. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  15865. }
  15866. memset(&item_tmp,0,sizeof(item_tmp));
  15867. item_tmp.nameid = nameid;
  15868. item_tmp.identify = itemdb_isidentified(nameid);
  15869. if (!qty)
  15870. qty = itemdb_get_randgroupitem_count(group,sub_group,nameid);
  15871. //Check if it's stackable.
  15872. if (!itemdb_isstackable(nameid)) {
  15873. item_tmp.amount = 1;
  15874. get_count = qty;
  15875. }
  15876. else {
  15877. item_tmp.amount = qty;
  15878. get_count = 1;
  15879. }
  15880. for (i = 0; i < get_count; i++) {
  15881. // if not pet egg
  15882. if (!pet_create_egg(sd, nameid)) {
  15883. unsigned char flag = 0;
  15884. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  15885. clif_additem(sd,0,0,flag);
  15886. if (pc_candrop(sd,&item_tmp))
  15887. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  15888. }
  15889. }
  15890. }
  15891. return SCRIPT_CMD_SUCCESS;
  15892. }
  15893. /* getgroupitem <group_id>;
  15894. * Gives item(s) to the attached player based on item group contents
  15895. */
  15896. BUILDIN_FUNC(getgroupitem) {
  15897. TBL_PC *sd;
  15898. int group_id = script_getnum(st,2);
  15899. if (!(sd = script_rid2sd(st)))
  15900. return SCRIPT_CMD_SUCCESS;
  15901. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  15902. ShowError("getgroupitem: Invalid group id '%d' specified.",group_id);
  15903. return SCRIPT_CMD_FAILURE;
  15904. }
  15905. return SCRIPT_CMD_SUCCESS;
  15906. }
  15907. /* cleanmap <map_name>;
  15908. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  15909. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  15910. {
  15911. nullpo_ret(bl);
  15912. map_clearflooritem(bl);
  15913. return SCRIPT_CMD_SUCCESS;
  15914. }
  15915. BUILDIN_FUNC(cleanmap)
  15916. {
  15917. const char *mapname;
  15918. int16 m;
  15919. int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  15920. mapname = script_getstr(st, 2);
  15921. m = map_mapname2mapid(mapname);
  15922. if (!m)
  15923. return 1;
  15924. if ((script_lastdata(st) - 2) < 4) {
  15925. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  15926. } else {
  15927. x0 = script_getnum(st, 3);
  15928. y0 = script_getnum(st, 4);
  15929. x1 = script_getnum(st, 5);
  15930. y1 = script_getnum(st, 6);
  15931. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  15932. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  15933. } else {
  15934. ShowError("cleanarea: invalid coordinate defined!\n");
  15935. return 1;
  15936. }
  15937. }
  15938. return SCRIPT_CMD_SUCCESS;
  15939. }
  15940. /* Cast a skill on the attached player.
  15941. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  15942. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  15943. BUILDIN_FUNC(npcskill)
  15944. {
  15945. uint16 skill_id;
  15946. unsigned short skill_level;
  15947. unsigned int stat_point;
  15948. unsigned int npc_level;
  15949. struct npc_data *nd;
  15950. struct map_session_data *sd;
  15951. struct script_data *data;
  15952. data = script_getdata(st, 2);
  15953. get_val(st, data); // Convert into value in case of a variable
  15954. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  15955. skill_level = script_getnum(st, 3);
  15956. stat_point = script_getnum(st, 4);
  15957. npc_level = script_getnum(st, 5);
  15958. sd = script_rid2sd(st);
  15959. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  15960. if (stat_point > battle_config.max_third_parameter) {
  15961. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  15962. return 1;
  15963. }
  15964. if (npc_level > MAX_LEVEL) {
  15965. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  15966. return 1;
  15967. }
  15968. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  15969. return 1;
  15970. }
  15971. nd->level = npc_level;
  15972. nd->stat_point = stat_point;
  15973. if (!nd->status.hp)
  15974. status_calc_npc(nd, SCO_FIRST);
  15975. else
  15976. status_calc_npc(nd, SCO_NONE);
  15977. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  15978. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  15979. else
  15980. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  15981. return SCRIPT_CMD_SUCCESS;
  15982. }
  15983. /* Consumes an item.
  15984. * consumeitem <item id>;
  15985. * consumeitem "<item name>";
  15986. * @param item: Item ID or name
  15987. */
  15988. BUILDIN_FUNC(consumeitem)
  15989. {
  15990. TBL_PC *sd;
  15991. struct script_data *data;
  15992. struct item_data *item_data;
  15993. nullpo_retr( 1, ( sd = script_rid2sd( st ) ) );
  15994. data = script_getdata( st, 2 );
  15995. get_val( st, data );
  15996. if( data_isstring( data ) ){
  15997. const char *name = conv_str( st, data );
  15998. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  15999. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  16000. return SCRIPT_CMD_FAILURE;
  16001. }
  16002. }else if( data_isint( data ) ){
  16003. unsigned short nameid = conv_num( st, data );
  16004. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  16005. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  16006. return SCRIPT_CMD_FAILURE;
  16007. }
  16008. }else{
  16009. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).", data->type );
  16010. return SCRIPT_CMD_FAILURE;
  16011. }
  16012. run_script( item_data->script, 0, sd->bl.id, 0 );
  16013. return SCRIPT_CMD_SUCCESS;
  16014. }
  16015. /** Makes a player sit/stand.
  16016. * sit {"<character name>"};
  16017. * stand {"<character name>"};
  16018. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  16019. * @param name: Player name that will be invoked
  16020. */
  16021. BUILDIN_FUNC(sit)
  16022. {
  16023. TBL_PC *sd;
  16024. if( script_hasdata(st, 2) )
  16025. sd = map_nick2sd(script_getstr(st, 2));
  16026. else
  16027. sd = script_rid2sd(st);
  16028. if( sd == NULL)
  16029. return SCRIPT_CMD_FAILURE;
  16030. if( !pc_issit(sd) ) {
  16031. pc_setsit(sd);
  16032. skill_sit(sd, 1);
  16033. clif_sitting(&sd->bl);
  16034. }
  16035. return SCRIPT_CMD_SUCCESS;
  16036. }
  16037. /** Makes player to stand
  16038. * @param name: Player name that will be set
  16039. */
  16040. BUILDIN_FUNC(stand)
  16041. {
  16042. TBL_PC *sd;
  16043. if( script_hasdata(st, 2) )
  16044. sd = map_nick2sd(script_getstr(st, 2));
  16045. else
  16046. sd = script_rid2sd(st);
  16047. if( sd == NULL)
  16048. return SCRIPT_CMD_FAILURE;
  16049. if( pc_issit(sd) && pc_setstand(sd, false)) {
  16050. skill_sit(sd, 0);
  16051. clif_standing(&sd->bl);
  16052. }
  16053. return SCRIPT_CMD_SUCCESS;
  16054. }
  16055. /** Creates an array of bounded item IDs
  16056. * countbound {<type>};
  16057. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  16058. * @return amt: Amount of items found
  16059. */
  16060. BUILDIN_FUNC(countbound)
  16061. {
  16062. int i, type, j = 0, k = 0;
  16063. TBL_PC *sd;
  16064. if( (sd = script_rid2sd(st)) == NULL )
  16065. return SCRIPT_CMD_FAILURE;
  16066. type = script_getnum(st,2);
  16067. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  16068. if( sd->status.inventory[i].nameid > 0 && (
  16069. (!type && sd->status.inventory[i].bound) || (type && sd->status.inventory[i].bound == type)
  16070. ))
  16071. {
  16072. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  16073. k++;
  16074. j += sd->status.inventory[i].amount;
  16075. }
  16076. }
  16077. script_pushint(st,j);
  16078. return SCRIPT_CMD_SUCCESS;
  16079. }
  16080. /** Creates new party
  16081. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  16082. * @param party_name: String of party name that will be created
  16083. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  16084. * @param item_share: 0-Each Take. 1-Party Share
  16085. * @param item_share_type: 0-Each Take. 1-Even Share
  16086. * @return val: Result value
  16087. * -3 - party name is exist
  16088. * -2 - player is in party already
  16089. * -1 - player is not found
  16090. * 0 - unknown error
  16091. * 1 - success, will return party id $@party_create_id
  16092. */
  16093. BUILDIN_FUNC(party_create)
  16094. {
  16095. char party_name[NAME_LENGTH];
  16096. int item1 = 0, item2 = 0;
  16097. TBL_PC *sd;
  16098. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  16099. script_pushint(st,-1);
  16100. return SCRIPT_CMD_FAILURE;
  16101. }
  16102. if( sd->status.party_id ) {
  16103. script_pushint(st,-2);
  16104. return SCRIPT_CMD_FAILURE;
  16105. }
  16106. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  16107. trim(party_name);
  16108. if( party_searchname(party_name) ) {
  16109. script_pushint(st,-3);
  16110. return SCRIPT_CMD_FAILURE;
  16111. }
  16112. if( script_getnum(st,4) )
  16113. item1 = 1;
  16114. if( script_getnum(st,5) )
  16115. item2 = 1;
  16116. party_create_byscript = 1;
  16117. script_pushint(st,party_create(sd,party_name,item1,item2));
  16118. return SCRIPT_CMD_SUCCESS;
  16119. }
  16120. /** Adds player to specified party
  16121. * party_addmember <party id>,<char id>;
  16122. * @param party_id: The party that will be entered by player
  16123. * @param char_id: Char id of player that will be joined to the party
  16124. * @return val: Result value
  16125. * -4 - party is full
  16126. * -3 - party is not found
  16127. * -2 - player is in party already
  16128. * -1 - player is not found
  16129. * 0 - unknown error
  16130. * 1 - success
  16131. */
  16132. BUILDIN_FUNC(party_addmember)
  16133. {
  16134. int party_id = script_getnum(st,2);
  16135. TBL_PC *sd;
  16136. struct party_data *party;
  16137. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  16138. script_pushint(st,-1);
  16139. return SCRIPT_CMD_FAILURE;
  16140. }
  16141. if( sd->status.party_id ) {
  16142. script_pushint(st,-2);
  16143. return SCRIPT_CMD_FAILURE;
  16144. }
  16145. if( !(party = party_search(party_id)) ) {
  16146. script_pushint(st,-3);
  16147. return SCRIPT_CMD_FAILURE;
  16148. }
  16149. if( party->party.count >= MAX_PARTY ) {
  16150. script_pushint(st,-4);
  16151. return SCRIPT_CMD_FAILURE;
  16152. }
  16153. sd->party_invite = party_id;
  16154. script_pushint(st,party_add_member(party_id,sd));
  16155. return SCRIPT_CMD_SUCCESS;
  16156. }
  16157. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  16158. * party_delmember {<char id>,<party_id>};
  16159. * @param: char_id
  16160. * @param: party_id
  16161. * @return val: Result value
  16162. * -3 - player is not in party
  16163. * -2 - party is not found
  16164. * -1 - player is not found
  16165. * 0 - unknown error
  16166. * 1 - success
  16167. */
  16168. BUILDIN_FUNC(party_delmember)
  16169. {
  16170. TBL_PC *sd = NULL;
  16171. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  16172. script_pushint(st,-1);
  16173. return SCRIPT_CMD_FAILURE;
  16174. }
  16175. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  16176. script_pushint(st,party_removemember2(sd,0,0));
  16177. else
  16178. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  16179. return SCRIPT_CMD_SUCCESS;
  16180. }
  16181. /** Changes party leader of specified party (even the leader is offline)
  16182. * party_changeleader <party id>,<char id>;
  16183. * @param party_id: ID of party
  16184. * @param char_id: Char ID of new leader
  16185. * @return val: Result value
  16186. * -4 - player is party leader already
  16187. * -3 - player is not in this party
  16188. * -2 - player is not found
  16189. * -1 - party is not found
  16190. * 0 - unknown error
  16191. * 1 - success */
  16192. BUILDIN_FUNC(party_changeleader)
  16193. {
  16194. int i, party_id = script_getnum(st,2);
  16195. TBL_PC *sd = NULL;
  16196. TBL_PC *tsd = NULL;
  16197. struct party_data *party = NULL;
  16198. if( !(party = party_search(party_id)) ) {
  16199. script_pushint(st,-1);
  16200. return SCRIPT_CMD_FAILURE;
  16201. }
  16202. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  16203. script_pushint(st,-2);
  16204. return SCRIPT_CMD_FAILURE;
  16205. }
  16206. if( tsd->status.party_id != party_id ) {
  16207. script_pushint(st,-3);
  16208. return SCRIPT_CMD_FAILURE;
  16209. }
  16210. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  16211. if( i >= MAX_PARTY ) { //this is should impossible!
  16212. script_pushint(st,0);
  16213. return SCRIPT_CMD_FAILURE;
  16214. }
  16215. if( party->data[i].sd == tsd ) {
  16216. script_pushint(st,-4);
  16217. return SCRIPT_CMD_FAILURE;
  16218. }
  16219. script_pushint(st,party_changeleader(sd,tsd,party));
  16220. return SCRIPT_CMD_SUCCESS;
  16221. }
  16222. /** Changes party option
  16223. * party_changeoption <party id>,<option>,<flag>;
  16224. * @param party_id: ID of party that will be changed
  16225. * @param option: Type of option
  16226. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  16227. */
  16228. BUILDIN_FUNC(party_changeoption)
  16229. {
  16230. struct party_data *party;
  16231. if( !(party = party_search(script_getnum(st,2))) ) {
  16232. script_pushint(st,-1);
  16233. return SCRIPT_CMD_FAILURE;
  16234. }
  16235. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  16236. return SCRIPT_CMD_SUCCESS;
  16237. }
  16238. /** Destroys party with party id.
  16239. * party_destroy <party id>;
  16240. * @param party_id: ID of party that will be destroyed
  16241. */
  16242. BUILDIN_FUNC(party_destroy)
  16243. {
  16244. int i;
  16245. struct party_data *party;
  16246. if( !(party = party_search(script_getnum(st,2))) ) {
  16247. script_pushint(st,0);
  16248. return SCRIPT_CMD_FAILURE;
  16249. }
  16250. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  16251. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  16252. int j;
  16253. for( j = 0; j < MAX_PARTY; j++ ) {
  16254. TBL_PC *sd = party->data[j].sd;
  16255. if(sd)
  16256. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  16257. else if( party->party.member[j].char_id )
  16258. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  16259. }
  16260. party_broken(party->party.party_id);
  16261. script_pushint(st,1);
  16262. }
  16263. else //leader leave = party broken
  16264. script_pushint(st,party_leave(party->data[i].sd));
  16265. return SCRIPT_CMD_SUCCESS;
  16266. }
  16267. /** Checks if a player's client version meets a required version or date.
  16268. * @param type: 0 - check by version number; 1 - check by date
  16269. * @param data: Input
  16270. */
  16271. BUILDIN_FUNC(is_clientver) {
  16272. TBL_PC *sd = NULL;
  16273. int type = script_getnum(st,2);
  16274. int data = script_getnum(st,3);
  16275. int ret = 0;
  16276. if (script_hasdata(st,4))
  16277. sd = map_charid2sd(script_getnum(st,4));
  16278. else
  16279. sd = script_rid2sd(st);
  16280. if (sd == NULL) {
  16281. script_pushint(st,0);
  16282. return SCRIPT_CMD_FAILURE;
  16283. }
  16284. switch(type){
  16285. case 0:
  16286. ret = (sd->packet_ver >= data)?1:0;
  16287. break;
  16288. case 1:
  16289. ret = (sd->packet_ver >= date2version(data))?1:0;
  16290. break;
  16291. }
  16292. script_pushint(st,ret);
  16293. return SCRIPT_CMD_SUCCESS;
  16294. }
  16295. /** Returns various information about a player's VIP status. Need to enable VIP system
  16296. * vip_status <type>,{"<character name>"};
  16297. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  16298. * @param name: Character name (Optional)
  16299. */
  16300. BUILDIN_FUNC(vip_status) {
  16301. #ifdef VIP_ENABLE
  16302. TBL_PC *sd;
  16303. char vip_str[26];
  16304. time_t now = time(NULL);
  16305. int type = script_getnum(st, 2);
  16306. if (script_hasdata(st, 3))
  16307. sd = map_nick2sd(script_getstr(st, 3));
  16308. else
  16309. sd = script_rid2sd(st);
  16310. if (sd == NULL)
  16311. return SCRIPT_CMD_FAILURE;
  16312. switch(type) {
  16313. case 1: // Get VIP status.
  16314. script_pushint(st, pc_isvip(sd));
  16315. break;
  16316. case 2: // Get VIP expire date.
  16317. if (pc_isvip(sd)) {
  16318. time_t viptime = sd->vip.time;
  16319. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  16320. vip_str[25] = '\0';
  16321. script_pushstrcopy(st, vip_str);
  16322. } else
  16323. script_pushint(st, 0);
  16324. break;
  16325. case 3: // Get remaining time.
  16326. if (pc_isvip(sd)) {
  16327. time_t viptime_remain = sd->vip.time - now;
  16328. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  16329. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  16330. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  16331. script_pushstrcopy(st, vip_str);
  16332. } else
  16333. script_pushint(st, 0);
  16334. break;
  16335. }
  16336. #else
  16337. script_pushint(st, 0);
  16338. #endif
  16339. return SCRIPT_CMD_SUCCESS;
  16340. }
  16341. /** Adds or removes VIP time in minutes. Need to enable VIP system
  16342. * vip_time <time in mn>,{"<character name>"};
  16343. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  16344. * @param name: Character name (optional)
  16345. */
  16346. BUILDIN_FUNC(vip_time) {
  16347. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  16348. TBL_PC *sd;
  16349. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  16350. if (script_hasdata(st, 3))
  16351. sd = map_nick2sd(script_getstr(st, 3));
  16352. else
  16353. sd = script_rid2sd(st);
  16354. if (sd == NULL)
  16355. return SCRIPT_CMD_FAILURE;
  16356. chrif_req_login_operation(sd->status.account_id, sd->status.name, 6, viptime, 7, 0);
  16357. #endif
  16358. return SCRIPT_CMD_SUCCESS;
  16359. }
  16360. /** Turns a player into a monster and grants SC attribute effect. [malufett/Hercules]
  16361. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  16362. * @param monster: Monster ID or name
  16363. * @param duration: Transform duration in millisecond (ms)
  16364. * @param sc_type: Type of SC that will be affected during the transformation
  16365. * @param val1: Value for SC
  16366. * @param val2: Value for SC
  16367. * @param val3: Value for SC
  16368. * @param val4: Value for SC
  16369. */
  16370. BUILDIN_FUNC(montransform) {
  16371. TBL_PC *sd;
  16372. enum sc_type type;
  16373. int tick, mob_id, val1, val2, val3, val4;
  16374. struct script_data *data;
  16375. if( (sd = script_rid2sd(st)) == NULL )
  16376. return 1;
  16377. data = script_getdata(st, 2);
  16378. get_val(st, data); // Convert into value in case of a variable
  16379. if( data_isstring(data) )
  16380. mob_id = mobdb_searchname(script_getstr(st, 2));
  16381. else
  16382. mob_id = mobdb_checkid(script_getnum(st, 2));
  16383. tick = script_getnum(st, 3);
  16384. type = (sc_type)script_getnum(st, 4);
  16385. val1 = val2 = val3 = val4 = 0;
  16386. if (mob_id == 0) {
  16387. if( data_isstring(data) )
  16388. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  16389. else
  16390. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  16391. return SCRIPT_CMD_FAILURE;
  16392. }
  16393. if (mob_id == MOBID_EMPERIUM) {
  16394. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  16395. return SCRIPT_CMD_FAILURE;
  16396. }
  16397. if (!(type > SC_NONE && type < SC_MAX)) {
  16398. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  16399. return SCRIPT_CMD_FAILURE;
  16400. }
  16401. if (script_hasdata(st, 5))
  16402. val1 = script_getnum(st, 5);
  16403. if (script_hasdata(st, 6))
  16404. val2 = script_getnum(st, 6);
  16405. if (script_hasdata(st, 7))
  16406. val3 = script_getnum(st, 7);
  16407. if (script_hasdata(st, 8))
  16408. val4 = script_getnum(st, 8);
  16409. if (tick != 0) {
  16410. char msg[CHAT_SIZE_MAX];
  16411. struct mob_db *monster = mob_db(mob_id);
  16412. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  16413. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  16414. return SCRIPT_CMD_FAILURE;
  16415. }
  16416. if (sd->disguise){
  16417. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  16418. return SCRIPT_CMD_FAILURE;
  16419. }
  16420. sprintf(msg, msg_txt(sd,728), monster->name); // Traaaansformation-!! %s form!!
  16421. clif_disp_overhead(&sd->bl, msg);
  16422. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  16423. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  16424. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  16425. }
  16426. return SCRIPT_CMD_SUCCESS;
  16427. }
  16428. /**
  16429. * Attach script to player for certain duration
  16430. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  16431. * @param "script code"
  16432. * @param duration
  16433. * @param flag
  16434. * @param icon
  16435. * @param char_id
  16436. * @author [Cydh]
  16437. **/
  16438. BUILDIN_FUNC(bonus_script) {
  16439. uint8 i, flag = 0;
  16440. int16 icon = SI_BLANK;
  16441. uint32 dur;
  16442. char type = 0;
  16443. TBL_PC* sd;
  16444. const char *script_str = NULL;
  16445. struct script_code *script = NULL;
  16446. if (script_hasdata(st,7))
  16447. sd = map_charid2sd(script_getnum(st,7));
  16448. else
  16449. sd = script_rid2sd(st);
  16450. if (sd == NULL)
  16451. return SCRIPT_CMD_FAILURE;
  16452. script_str = script_getstr(st,2);
  16453. dur = 1000 * abs(script_getnum(st,3));
  16454. FETCH(4,flag);
  16455. FETCH(5,type);
  16456. FETCH(6,icon);
  16457. if (script_str[0] == '\0' || !dur) {
  16458. //ShowWarning("buildin_bonus_script: Invalid script. Skipping...\n");
  16459. return SCRIPT_CMD_FAILURE;
  16460. }
  16461. //Skip duplicate entry
  16462. 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);
  16463. if (i < MAX_PC_BONUS_SCRIPT) {
  16464. //ShowWarning("buildin_bonus_script: Duplicate entry with bonus '%d'. Skipping...\n",i);
  16465. return SCRIPT_CMD_SUCCESS;
  16466. }
  16467. if (!(script = parse_script(script_str,"bonus_script",0,1))) {
  16468. ShowWarning("buildin_bonus_script: Failed to parse script '%s' (cid:%d). Skipping...\n",script_str,sd->status.char_id);
  16469. return SCRIPT_CMD_FAILURE;
  16470. }
  16471. //Find the empty slot
  16472. ARR_FIND(0,MAX_PC_BONUS_SCRIPT,i,!sd->bonus_script[i].script);
  16473. if (i >= MAX_PC_BONUS_SCRIPT) {
  16474. ShowWarning("buildin_itemscript: Maximum script_bonus is reached (cid:%d max: %d). Skipping...\n",sd->status.char_id,MAX_PC_BONUS_SCRIPT);
  16475. return SCRIPT_CMD_SUCCESS;
  16476. }
  16477. //Add the script data
  16478. memcpy(sd->bonus_script[i].script_str,script_str,strlen(script_str)+1);
  16479. sd->bonus_script[i].script = script;
  16480. sd->bonus_script[i].tick = gettick() + dur;
  16481. sd->bonus_script[i].flag = flag;
  16482. sd->bonus_script[i].type = type;
  16483. sd->bonus_script[i].icon = icon;
  16484. if (sd->bonus_script[i].icon != SI_BLANK) //Gives status icon if exist
  16485. clif_status_change(&sd->bl,sd->bonus_script[i].icon,1,dur,1,0,0);
  16486. status_calc_pc(sd,SCO_NONE);
  16487. return SCRIPT_CMD_SUCCESS;
  16488. }
  16489. /**
  16490. * Removes all bonus script from player
  16491. * bonus_script_clear {<flag>,{<char_id>}};
  16492. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  16493. * @param char_id Clear script from this character
  16494. * @author [Cydh]
  16495. */
  16496. BUILDIN_FUNC(bonus_script_clear) {
  16497. TBL_PC* sd;
  16498. bool flag = 0;
  16499. if (script_hasdata(st,2))
  16500. flag = script_getnum(st,2);
  16501. if (script_hasdata(st,3))
  16502. sd = map_charid2sd(script_getnum(st,3));
  16503. else
  16504. sd = script_rid2sd(st);
  16505. if (sd == NULL)
  16506. return SCRIPT_CMD_FAILURE;
  16507. pc_bonus_script_clear_all(sd,flag); /// Don't remove permanent script
  16508. return SCRIPT_CMD_SUCCESS;
  16509. }
  16510. /** Allows player to use atcommand while talking with NPC
  16511. * enable_command;
  16512. * @author [Cydh], [Kichi]
  16513. */
  16514. BUILDIN_FUNC(enable_command) {
  16515. TBL_PC* sd = script_rid2sd(st);
  16516. if (!sd)
  16517. return SCRIPT_CMD_FAILURE;
  16518. sd->state.disable_atcommand_on_npc = 0;
  16519. return SCRIPT_CMD_SUCCESS;
  16520. }
  16521. /** Prevents player to use atcommand while talking with NPC
  16522. * disable_command;
  16523. * @author [Cydh], [Kichi]
  16524. */
  16525. BUILDIN_FUNC(disable_command) {
  16526. TBL_PC* sd = script_rid2sd(st);
  16527. if (!sd)
  16528. return SCRIPT_CMD_FAILURE;
  16529. sd->state.disable_atcommand_on_npc = 1;
  16530. return SCRIPT_CMD_SUCCESS;
  16531. }
  16532. /** Get the information of the members of a guild by type.
  16533. * getguildmember <guild_id>{,<type>};
  16534. * @param guild_id: ID of guild
  16535. * @param type: Type of option (optional)
  16536. */
  16537. BUILDIN_FUNC(getguildmember)
  16538. {
  16539. unsigned char j = 0;
  16540. struct guild *g = NULL;
  16541. g = guild_search(script_getnum(st,2));
  16542. if (g) {
  16543. unsigned char i, type = 0;
  16544. if (script_hasdata(st,3))
  16545. type = (unsigned char)script_getnum(st,3);
  16546. for (i = 0; i < MAX_GUILD; i++) {
  16547. if (g->member[i].account_id) {
  16548. switch (type) {
  16549. case 2:
  16550. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  16551. break;
  16552. case 1:
  16553. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  16554. break;
  16555. default:
  16556. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  16557. break;
  16558. }
  16559. j++;
  16560. }
  16561. }
  16562. }
  16563. mapreg_setreg(add_str("$@guildmembercount"), j);
  16564. return SCRIPT_CMD_SUCCESS;
  16565. }
  16566. /** Adds spirit ball to player for 'duration' in milisecond
  16567. * addspiritball <count>,<duration>{,<char_id>};
  16568. * @param count How many spirit ball will be added
  16569. * @param duration How long spiritball is active until it disappears
  16570. * @param char_id Target player (Optional)
  16571. * @author [Cydh]
  16572. */
  16573. BUILDIN_FUNC(addspiritball) {
  16574. uint8 i, count = script_getnum(st,2);
  16575. uint16 duration = script_getnum(st,3);
  16576. struct map_session_data *sd = NULL;
  16577. if (count == 0)
  16578. return SCRIPT_CMD_SUCCESS;
  16579. if (script_hasdata(st,4)) {
  16580. if (!script_isstring(st,4))
  16581. sd = map_charid2sd(script_getnum(st,4));
  16582. else
  16583. sd = map_nick2sd(script_getstr(st,4));
  16584. }
  16585. else
  16586. sd = script_rid2sd(st);
  16587. if (!sd)
  16588. return SCRIPT_CMD_FAILURE;
  16589. for (i = 0; i < count; i++)
  16590. pc_addspiritball(sd,duration,10);
  16591. return SCRIPT_CMD_SUCCESS;
  16592. }
  16593. /** Deletes the spirit ball(s) from player
  16594. * delspiritball <count>{,<char_id>};
  16595. * @param count How many spirit ball will be deleted
  16596. * @param char_id Target player (Optional)
  16597. * @author [Cydh]
  16598. */
  16599. BUILDIN_FUNC(delspiritball) {
  16600. uint8 count = script_getnum(st,2);
  16601. struct map_session_data *sd = NULL;
  16602. if (count == 0)
  16603. count = 1;
  16604. if (script_hasdata(st,3)) {
  16605. if (!script_isstring(st,3))
  16606. sd = map_charid2sd(script_getnum(st,3));
  16607. else
  16608. sd = map_nick2sd(script_getstr(st,3));
  16609. }
  16610. else
  16611. sd = script_rid2sd(st);
  16612. if (!sd)
  16613. return SCRIPT_CMD_FAILURE;
  16614. pc_delspiritball(sd,count,0);
  16615. return SCRIPT_CMD_SUCCESS;
  16616. }
  16617. /** Counts the spirit ball that player has
  16618. * countspiritball {,<char_id>};
  16619. * @param char_id Target player (Optional)
  16620. * @author [Cydh]
  16621. */
  16622. BUILDIN_FUNC(countspiritball) {
  16623. struct map_session_data *sd;
  16624. if (script_hasdata(st,2)) {
  16625. if (!script_isstring(st,2))
  16626. sd = map_charid2sd(script_getnum(st,2));
  16627. else
  16628. sd = map_nick2sd(script_getstr(st,2));
  16629. }
  16630. else
  16631. sd = script_rid2sd(st);
  16632. if (!sd)
  16633. return SCRIPT_CMD_FAILURE;
  16634. script_pushint(st,sd->spiritball);
  16635. return SCRIPT_CMD_SUCCESS;
  16636. }
  16637. #include "../custom/script.inc"
  16638. // declarations that were supposed to be exported from npc_chat.c
  16639. #ifdef PCRE_SUPPORT
  16640. BUILDIN_FUNC(defpattern);
  16641. BUILDIN_FUNC(activatepset);
  16642. BUILDIN_FUNC(deactivatepset);
  16643. BUILDIN_FUNC(deletepset);
  16644. #endif
  16645. /** Regular expression matching
  16646. * preg_match(<pattern>,<string>{,<offset>})
  16647. */
  16648. BUILDIN_FUNC(preg_match) {
  16649. #ifdef PCRE_SUPPORT
  16650. pcre *re;
  16651. pcre_extra *pcreExtra;
  16652. const char *error;
  16653. int erroffset, r, offset = 0;
  16654. int subStrVec[30];
  16655. const char* pattern = script_getstr(st,2);
  16656. const char* subject = script_getstr(st,3);
  16657. if (script_hasdata(st,4))
  16658. offset = script_getnum(st,4);
  16659. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  16660. pcreExtra = pcre_study(re, 0, &error);
  16661. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  16662. pcre_free(re);
  16663. if (pcreExtra != NULL)
  16664. pcre_free(pcreExtra);
  16665. if (r < 0)
  16666. script_pushint(st,0);
  16667. else
  16668. script_pushint(st,(r > 0) ? r : 30 / 3);
  16669. return SCRIPT_CMD_SUCCESS;
  16670. #else
  16671. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  16672. script_pushint(st,0);
  16673. return SCRIPT_CMD_SUCCESS;
  16674. #endif
  16675. }
  16676. /// script command definitions
  16677. /// for an explanation on args, see add_buildin_func
  16678. struct script_function buildin_func[] = {
  16679. // NPC interaction
  16680. BUILDIN_DEF(mes,"s*"),
  16681. BUILDIN_DEF(next,""),
  16682. BUILDIN_DEF(close,""),
  16683. BUILDIN_DEF(close2,""),
  16684. BUILDIN_DEF(menu,"sl*"),
  16685. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  16686. BUILDIN_DEF(prompt,"s*"),
  16687. //
  16688. BUILDIN_DEF(goto,"l"),
  16689. BUILDIN_DEF(callsub,"l*"),
  16690. BUILDIN_DEF(callfunc,"s*"),
  16691. BUILDIN_DEF(return,"?"),
  16692. BUILDIN_DEF(getarg,"i?"),
  16693. BUILDIN_DEF(jobchange,"i?"),
  16694. BUILDIN_DEF(jobname,"i"),
  16695. BUILDIN_DEF(input,"r??"),
  16696. BUILDIN_DEF(warp,"sii"),
  16697. BUILDIN_DEF(areawarp,"siiiisii??"),
  16698. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  16699. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  16700. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  16701. BUILDIN_DEF(setlook,"ii"),
  16702. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  16703. BUILDIN_DEF(set,"rv"),
  16704. BUILDIN_DEF(setarray,"rv*"),
  16705. BUILDIN_DEF(cleararray,"rvi"),
  16706. BUILDIN_DEF(copyarray,"rri"),
  16707. BUILDIN_DEF(getarraysize,"r"),
  16708. BUILDIN_DEF(deletearray,"r?"),
  16709. BUILDIN_DEF(getelementofarray,"ri"),
  16710. BUILDIN_DEF(getitem,"vi?"),
  16711. BUILDIN_DEF(rentitem,"vi"),
  16712. BUILDIN_DEF(rentitem2,"viiiiiiii"),
  16713. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  16714. BUILDIN_DEF(getnameditem,"vv"),
  16715. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  16716. BUILDIN_DEF(makeitem,"visii"),
  16717. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  16718. BUILDIN_DEF(delitem,"vi?"),
  16719. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  16720. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  16721. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  16722. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  16723. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  16724. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  16725. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  16726. BUILDIN_DEF(cutin,"si"),
  16727. BUILDIN_DEF(viewpoint,"iiiii"),
  16728. BUILDIN_DEF(heal,"ii"),
  16729. BUILDIN_DEF(itemheal,"ii"),
  16730. BUILDIN_DEF(percentheal,"ii"),
  16731. BUILDIN_DEF(rand,"i?"),
  16732. BUILDIN_DEF(countitem,"v?"),
  16733. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  16734. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  16735. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  16736. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  16737. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  16738. BUILDIN_DEF(checkweight,"vi*"),
  16739. BUILDIN_DEF(checkweight2,"rr"),
  16740. BUILDIN_DEF(readparam,"i?"),
  16741. BUILDIN_DEF(getcharid,"i?"),
  16742. BUILDIN_DEF(getnpcid,"i?"),
  16743. BUILDIN_DEF(getpartyname,"i"),
  16744. BUILDIN_DEF(getpartymember,"i?"),
  16745. BUILDIN_DEF(getpartyleader,"i?"),
  16746. BUILDIN_DEF(getguildname,"i"),
  16747. BUILDIN_DEF(getguildmaster,"i"),
  16748. BUILDIN_DEF(getguildmasterid,"i"),
  16749. BUILDIN_DEF(strcharinfo,"i"),
  16750. BUILDIN_DEF(strnpcinfo,"i"),
  16751. BUILDIN_DEF(getequipid,"i"),
  16752. BUILDIN_DEF(getequipuniqueid,"i"),
  16753. BUILDIN_DEF(getequipname,"i"),
  16754. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  16755. BUILDIN_DEF(repair,"i"), // [Valaris]
  16756. BUILDIN_DEF(repairall,""),
  16757. BUILDIN_DEF(getequipisequiped,"i"),
  16758. BUILDIN_DEF(getequipisenableref,"i"),
  16759. BUILDIN_DEF(getequiprefinerycnt,"i"),
  16760. BUILDIN_DEF(getequipweaponlv,"i"),
  16761. BUILDIN_DEF(getequippercentrefinery,"i"),
  16762. BUILDIN_DEF(successrefitem,"i?"),
  16763. BUILDIN_DEF(failedrefitem,"i"),
  16764. BUILDIN_DEF(downrefitem,"i?"),
  16765. BUILDIN_DEF(statusup,"i"),
  16766. BUILDIN_DEF(statusup2,"ii"),
  16767. BUILDIN_DEF(bonus,"iv"),
  16768. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  16769. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  16770. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  16771. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  16772. BUILDIN_DEF(autobonus,"sii??"),
  16773. BUILDIN_DEF(autobonus2,"sii??"),
  16774. BUILDIN_DEF(autobonus3,"siiv?"),
  16775. BUILDIN_DEF(skill,"vi?"),
  16776. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  16777. BUILDIN_DEF(guildskill,"vi"),
  16778. BUILDIN_DEF(getskilllv,"v"),
  16779. BUILDIN_DEF(getgdskilllv,"iv"),
  16780. BUILDIN_DEF(basicskillcheck,""),
  16781. BUILDIN_DEF(getgmlevel,""),
  16782. BUILDIN_DEF(getgroupid,""),
  16783. BUILDIN_DEF(end,""),
  16784. BUILDIN_DEF(checkoption,"i"),
  16785. BUILDIN_DEF(setoption,"i?"),
  16786. BUILDIN_DEF(setcart,"?"),
  16787. BUILDIN_DEF(checkcart,""),
  16788. BUILDIN_DEF(setfalcon,"?"),
  16789. BUILDIN_DEF(checkfalcon,""),
  16790. BUILDIN_DEF(setriding,"?"),
  16791. BUILDIN_DEF(checkriding,""),
  16792. BUILDIN_DEF(checkwug,""),
  16793. BUILDIN_DEF(checkmadogear,""),
  16794. BUILDIN_DEF(setmadogear,"?"),
  16795. BUILDIN_DEF2(savepoint,"save","sii"),
  16796. BUILDIN_DEF(savepoint,"sii"),
  16797. BUILDIN_DEF(gettimetick,"i"),
  16798. BUILDIN_DEF(gettime,"i"),
  16799. BUILDIN_DEF(gettimestr,"si"),
  16800. BUILDIN_DEF(openstorage,""),
  16801. BUILDIN_DEF(guildopenstorage,""),
  16802. BUILDIN_DEF(itemskill,"vi"),
  16803. BUILDIN_DEF(produce,"i"),
  16804. BUILDIN_DEF(cooking,"i"),
  16805. BUILDIN_DEF(monster,"siisii???"),
  16806. BUILDIN_DEF(getmobdrops,"i"),
  16807. BUILDIN_DEF(areamonster,"siiiisii???"),
  16808. BUILDIN_DEF(killmonster,"ss?"),
  16809. BUILDIN_DEF(killmonsterall,"s?"),
  16810. BUILDIN_DEF(clone,"siisi????"),
  16811. BUILDIN_DEF(doevent,"s"),
  16812. BUILDIN_DEF(donpcevent,"s"),
  16813. BUILDIN_DEF(cmdothernpc,"ss"),
  16814. BUILDIN_DEF(addtimer,"is"),
  16815. BUILDIN_DEF(deltimer,"s"),
  16816. BUILDIN_DEF(addtimercount,"is"),
  16817. BUILDIN_DEF(initnpctimer,"??"),
  16818. BUILDIN_DEF(stopnpctimer,"??"),
  16819. BUILDIN_DEF(startnpctimer,"??"),
  16820. BUILDIN_DEF(setnpctimer,"i?"),
  16821. BUILDIN_DEF(getnpctimer,"i?"),
  16822. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  16823. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  16824. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  16825. BUILDIN_DEF(announce,"si?????"),
  16826. BUILDIN_DEF(mapannounce,"ssi?????"),
  16827. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  16828. BUILDIN_DEF(getusers,"i"),
  16829. BUILDIN_DEF(getmapguildusers,"si"),
  16830. BUILDIN_DEF(getmapusers,"s"),
  16831. BUILDIN_DEF(getareausers,"siiii"),
  16832. BUILDIN_DEF(getareadropitem,"siiiiv"),
  16833. BUILDIN_DEF(enablenpc,"s"),
  16834. BUILDIN_DEF(disablenpc,"s"),
  16835. BUILDIN_DEF(hideoffnpc,"s"),
  16836. BUILDIN_DEF(hideonnpc,"s"),
  16837. BUILDIN_DEF(sc_start,"iii???"),
  16838. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  16839. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  16840. BUILDIN_DEF(sc_end,"i?"),
  16841. BUILDIN_DEF(getstatus, "i?"),
  16842. BUILDIN_DEF(getscrate,"ii?"),
  16843. BUILDIN_DEF(debugmes,"s"),
  16844. BUILDIN_DEF2(catchpet,"pet","i"),
  16845. BUILDIN_DEF2(birthpet,"bpet",""),
  16846. BUILDIN_DEF(resetlvl,"i"),
  16847. BUILDIN_DEF(resetstatus,""),
  16848. BUILDIN_DEF(resetskill,""),
  16849. BUILDIN_DEF(skillpointcount,""),
  16850. BUILDIN_DEF(changebase,"i?"),
  16851. BUILDIN_DEF(changesex,""),
  16852. BUILDIN_DEF(waitingroom,"si?????"),
  16853. BUILDIN_DEF(delwaitingroom,"?"),
  16854. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  16855. BUILDIN_DEF(enablewaitingroomevent,"?"),
  16856. BUILDIN_DEF(disablewaitingroomevent,"?"),
  16857. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  16858. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  16859. BUILDIN_DEF(getwaitingroomstate,"i?"),
  16860. BUILDIN_DEF(warpwaitingpc,"sii?"),
  16861. BUILDIN_DEF(attachrid,"i"),
  16862. BUILDIN_DEF(addrid,"i?????"),
  16863. BUILDIN_DEF(detachrid,""),
  16864. BUILDIN_DEF(isloggedin,"i?"),
  16865. BUILDIN_DEF(setmapflagnosave,"ssii"),
  16866. BUILDIN_DEF(getmapflag,"si?"),
  16867. BUILDIN_DEF(setmapflag,"si??"),
  16868. BUILDIN_DEF(removemapflag,"si?"),
  16869. BUILDIN_DEF(pvpon,"s"),
  16870. BUILDIN_DEF(pvpoff,"s"),
  16871. BUILDIN_DEF(gvgon,"s"),
  16872. BUILDIN_DEF(gvgoff,"s"),
  16873. BUILDIN_DEF(emotion,"i??"),
  16874. BUILDIN_DEF(maprespawnguildid,"sii"),
  16875. BUILDIN_DEF(agitstart,""), // <Agit>
  16876. BUILDIN_DEF(agitend,""),
  16877. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  16878. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  16879. BUILDIN_DEF(getcastlename,"s"),
  16880. BUILDIN_DEF(getcastledata,"si"),
  16881. BUILDIN_DEF(setcastledata,"sii"),
  16882. BUILDIN_DEF(requestguildinfo,"i?"),
  16883. BUILDIN_DEF(getequipcardcnt,"i"),
  16884. BUILDIN_DEF(successremovecards,"i"),
  16885. BUILDIN_DEF(failedremovecards,"ii"),
  16886. BUILDIN_DEF(marriage,"s"),
  16887. BUILDIN_DEF2(wedding_effect,"wedding",""),
  16888. BUILDIN_DEF(divorce,""),
  16889. BUILDIN_DEF(ispartneron,""),
  16890. BUILDIN_DEF(getpartnerid,""),
  16891. BUILDIN_DEF(getchildid,""),
  16892. BUILDIN_DEF(getmotherid,""),
  16893. BUILDIN_DEF(getfatherid,""),
  16894. BUILDIN_DEF(warppartner,"sii"),
  16895. BUILDIN_DEF(getitemname,"v"),
  16896. BUILDIN_DEF(getitemslots,"i"),
  16897. BUILDIN_DEF(makepet,"i"),
  16898. BUILDIN_DEF(getexp,"ii"),
  16899. BUILDIN_DEF(getinventorylist,""),
  16900. BUILDIN_DEF(getskilllist,""),
  16901. BUILDIN_DEF(clearitem,""),
  16902. BUILDIN_DEF(classchange,"ii"),
  16903. BUILDIN_DEF(misceffect,"i"),
  16904. BUILDIN_DEF(playBGM,"s"),
  16905. BUILDIN_DEF(playBGMall,"s?????"),
  16906. BUILDIN_DEF(soundeffect,"si"),
  16907. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  16908. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  16909. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  16910. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  16911. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  16912. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  16913. BUILDIN_DEF(petloot,"i"), // [Valaris]
  16914. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  16915. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  16916. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  16917. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  16918. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  16919. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  16920. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  16921. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  16922. BUILDIN_DEF(nude,""), // nude command [Valaris]
  16923. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  16924. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  16925. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  16926. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  16927. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  16928. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  16929. BUILDIN_DEF(mobcount,"ss"),
  16930. BUILDIN_DEF(getlook,"i"),
  16931. BUILDIN_DEF(getsavepoint,"i"),
  16932. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  16933. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  16934. BUILDIN_DEF(npcstop,""), // [Valaris]
  16935. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  16936. BUILDIN_DEF(checkoption1,"i"),
  16937. BUILDIN_DEF(checkoption2,"i"),
  16938. BUILDIN_DEF(guildgetexp,"i"),
  16939. BUILDIN_DEF(guildchangegm,"is"),
  16940. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  16941. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  16942. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  16943. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  16944. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  16945. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  16946. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  16947. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  16948. BUILDIN_DEF(night,""), // sets the server to night time
  16949. BUILDIN_DEF(day,""), // sets the server to day time
  16950. #ifdef PCRE_SUPPORT
  16951. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  16952. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  16953. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  16954. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  16955. #endif
  16956. BUILDIN_DEF(preg_match,"ss?"),
  16957. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  16958. BUILDIN_DEF(getusersname,""),
  16959. BUILDIN_DEF(recovery,"i???"),
  16960. BUILDIN_DEF(getpetinfo,"i"),
  16961. BUILDIN_DEF(gethominfo,"i"),
  16962. BUILDIN_DEF(getmercinfo,"i?"),
  16963. BUILDIN_DEF(checkequipedcard,"i"),
  16964. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  16965. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  16966. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  16967. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  16968. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  16969. BUILDIN_DEF(charat,"si"),
  16970. BUILDIN_DEF(setchar,"ssi"),
  16971. BUILDIN_DEF(insertchar,"ssi"),
  16972. BUILDIN_DEF(delchar,"si"),
  16973. BUILDIN_DEF(strtoupper,"s"),
  16974. BUILDIN_DEF(strtolower,"s"),
  16975. BUILDIN_DEF(charisupper, "si"),
  16976. BUILDIN_DEF(charislower, "si"),
  16977. BUILDIN_DEF(substr,"sii"),
  16978. BUILDIN_DEF(explode, "rss"),
  16979. BUILDIN_DEF(implode, "r?"),
  16980. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  16981. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  16982. BUILDIN_DEF(strpos,"ss?"),
  16983. BUILDIN_DEF(replacestr,"sss??"),
  16984. BUILDIN_DEF(countstr,"ss?"),
  16985. BUILDIN_DEF(setnpcdisplay,"sv??"),
  16986. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  16987. BUILDIN_DEF(strcmp,"ss"),
  16988. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  16989. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  16990. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  16991. // [zBuffer] List of mathematics commands --->
  16992. BUILDIN_DEF(sqrt,"i"),
  16993. BUILDIN_DEF(pow,"ii"),
  16994. BUILDIN_DEF(distance,"iiii"),
  16995. // <--- [zBuffer] List of mathematics commands
  16996. BUILDIN_DEF(md5,"s"),
  16997. // [zBuffer] List of dynamic var commands --->
  16998. BUILDIN_DEF(getd,"s"),
  16999. BUILDIN_DEF(setd,"sv"),
  17000. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  17001. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  17002. BUILDIN_DEF(npcshopadditem,"sii*"),
  17003. BUILDIN_DEF(npcshopdelitem,"si*"),
  17004. BUILDIN_DEF(npcshopattach,"s?"),
  17005. BUILDIN_DEF(equip,"i"),
  17006. BUILDIN_DEF(autoequip,"ii"),
  17007. BUILDIN_DEF(setbattleflag,"si"),
  17008. BUILDIN_DEF(getbattleflag,"s"),
  17009. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  17010. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  17011. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  17012. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  17013. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  17014. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  17015. BUILDIN_DEF(axtoi,"s"),
  17016. BUILDIN_DEF(query_sql,"s*"),
  17017. BUILDIN_DEF(query_logsql,"s*"),
  17018. BUILDIN_DEF(escape_sql,"v"),
  17019. BUILDIN_DEF(atoi,"s"),
  17020. BUILDIN_DEF(strtol,"si"),
  17021. // [zBuffer] List of player cont commands --->
  17022. BUILDIN_DEF(rid2name,"i"),
  17023. BUILDIN_DEF(pcfollow,"ii"),
  17024. BUILDIN_DEF(pcstopfollow,"i"),
  17025. BUILDIN_DEF(pcblockmove,"ii"),
  17026. // <--- [zBuffer] List of player cont commands
  17027. // [zBuffer] List of mob control commands --->
  17028. BUILDIN_DEF(unitwalk,"ii?"),
  17029. BUILDIN_DEF(unitkill,"i"),
  17030. BUILDIN_DEF(unitwarp,"isii"),
  17031. BUILDIN_DEF(unitattack,"iv?"),
  17032. BUILDIN_DEF(unitstop,"i"),
  17033. BUILDIN_DEF(unittalk,"is"),
  17034. BUILDIN_DEF(unitemote,"ii"),
  17035. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  17036. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  17037. // <--- [zBuffer] List of mob control commands
  17038. BUILDIN_DEF(sleep,"i"),
  17039. BUILDIN_DEF(sleep2,"i"),
  17040. BUILDIN_DEF(awake,"s"),
  17041. BUILDIN_DEF(getvariableofnpc,"rs"),
  17042. BUILDIN_DEF(warpportal,"iisii"),
  17043. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  17044. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  17045. BUILDIN_DEF(morphembryo,""),
  17046. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  17047. BUILDIN_DEF(checkhomcall,""),
  17048. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  17049. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  17050. BUILDIN_DEF(checkvending,"?"),
  17051. BUILDIN_DEF(checkchatting,"?"),
  17052. BUILDIN_DEF(checkidle,"?"),
  17053. BUILDIN_DEF(openmail,""),
  17054. BUILDIN_DEF(openauction,""),
  17055. BUILDIN_DEF(checkcell,"siii"),
  17056. BUILDIN_DEF(setcell,"siiiiii"),
  17057. BUILDIN_DEF(setwall,"siiiiis"),
  17058. BUILDIN_DEF(delwall,"s"),
  17059. BUILDIN_DEF(searchitem,"rs"),
  17060. BUILDIN_DEF(mercenary_create,"ii"),
  17061. BUILDIN_DEF(mercenary_heal,"ii"),
  17062. BUILDIN_DEF(mercenary_sc_start,"iii"),
  17063. BUILDIN_DEF(mercenary_get_calls,"i"),
  17064. BUILDIN_DEF(mercenary_get_faith,"i"),
  17065. BUILDIN_DEF(mercenary_set_calls,"ii"),
  17066. BUILDIN_DEF(mercenary_set_faith,"ii"),
  17067. BUILDIN_DEF(readbook,"ii"),
  17068. BUILDIN_DEF(setfont,"i"),
  17069. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  17070. BUILDIN_DEF(progressbar,"si"),
  17071. BUILDIN_DEF(pushpc,"ii"),
  17072. BUILDIN_DEF(buyingstore,"i"),
  17073. BUILDIN_DEF(searchstores,"ii"),
  17074. BUILDIN_DEF(showdigit,"i?"),
  17075. // WoE SE
  17076. BUILDIN_DEF(agitstart2,""),
  17077. BUILDIN_DEF(agitend2,""),
  17078. BUILDIN_DEF(agitcheck2,""),
  17079. // BattleGround
  17080. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  17081. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  17082. BUILDIN_DEF(bg_team_setxy,"iii"),
  17083. BUILDIN_DEF(bg_warp,"isii"),
  17084. BUILDIN_DEF(bg_monster,"isiisi?"),
  17085. BUILDIN_DEF(bg_monster_set_team,"ii"),
  17086. BUILDIN_DEF(bg_leave,""),
  17087. BUILDIN_DEF(bg_destroy,"i"),
  17088. BUILDIN_DEF(areapercentheal,"siiiiii"),
  17089. BUILDIN_DEF(bg_get_data,"ii"),
  17090. BUILDIN_DEF(bg_getareausers,"isiiii"),
  17091. BUILDIN_DEF(bg_updatescore,"sii"),
  17092. // Instancing
  17093. BUILDIN_DEF(instance_create,"s"),
  17094. BUILDIN_DEF(instance_destroy,"?"),
  17095. BUILDIN_DEF(instance_id,""),
  17096. BUILDIN_DEF(instance_enter,"s??"),
  17097. BUILDIN_DEF(instance_npcname,"s?"),
  17098. BUILDIN_DEF(instance_mapname,"s?"),
  17099. BUILDIN_DEF(instance_warpall,"sii?"),
  17100. BUILDIN_DEF(instance_announce,"isi?????"),
  17101. BUILDIN_DEF(instance_check_party,"i???"),
  17102. /**
  17103. * 3rd-related
  17104. **/
  17105. BUILDIN_DEF(makerune,"i"),
  17106. BUILDIN_DEF(checkdragon,""),//[Ind]
  17107. BUILDIN_DEF(setdragon,"?"),//[Ind]
  17108. BUILDIN_DEF(ismounting,""),//[Ind]
  17109. BUILDIN_DEF(setmounting,""),//[Ind]
  17110. BUILDIN_DEF(checkre,"i"),
  17111. /**
  17112. * rAthena and beyond!
  17113. **/
  17114. BUILDIN_DEF(getargcount,""),
  17115. BUILDIN_DEF(getcharip,"?"),
  17116. BUILDIN_DEF(is_function,"s"),
  17117. BUILDIN_DEF(get_revision,""),
  17118. BUILDIN_DEF(get_githash,""),
  17119. BUILDIN_DEF(freeloop,"?"),
  17120. BUILDIN_DEF(getrandgroupitem,"ii?"),
  17121. BUILDIN_DEF(cleanmap,"s"),
  17122. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  17123. BUILDIN_DEF(npcskill,"viii"),
  17124. BUILDIN_DEF(consumeitem,"v"),
  17125. BUILDIN_DEF(delequip,"i"),
  17126. BUILDIN_DEF(breakequip,"i"),
  17127. BUILDIN_DEF(sit,"?"),
  17128. BUILDIN_DEF(stand,"?"),
  17129. //@commands (script based)
  17130. BUILDIN_DEF(bindatcmd, "ss??"),
  17131. BUILDIN_DEF(unbindatcmd, "s"),
  17132. BUILDIN_DEF(useatcmd, "s"),
  17133. //Quest Log System [Inkfish]
  17134. BUILDIN_DEF(questinfo, "ii??"),
  17135. BUILDIN_DEF(setquest, "i"),
  17136. BUILDIN_DEF(erasequest, "i"),
  17137. BUILDIN_DEF(completequest, "i"),
  17138. BUILDIN_DEF(checkquest, "i?"),
  17139. BUILDIN_DEF(isbegin_quest,"i"),
  17140. BUILDIN_DEF(changequest, "ii"),
  17141. BUILDIN_DEF(showevent, "i?"),
  17142. //Bound items [Xantara] & [Akinari]
  17143. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  17144. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  17145. BUILDIN_DEF(countbound, "?"),
  17146. // Party related
  17147. BUILDIN_DEF(party_create,"s???"),
  17148. BUILDIN_DEF(party_addmember,"ii"),
  17149. BUILDIN_DEF(party_delmember,"??"),
  17150. BUILDIN_DEF(party_changeleader,"ii"),
  17151. BUILDIN_DEF(party_changeoption,"iii"),
  17152. BUILDIN_DEF(party_destroy,"i"),
  17153. BUILDIN_DEF(is_clientver,"ii?"),
  17154. BUILDIN_DEF2(montransform, "transform", "vii????"), // Monster Transform [malufett/Hercules]
  17155. BUILDIN_DEF(vip_status,"i?"),
  17156. BUILDIN_DEF(vip_time,"i?"),
  17157. BUILDIN_DEF(bonus_script,"si????"),
  17158. BUILDIN_DEF(bonus_script_clear,"??"),
  17159. BUILDIN_DEF(getgroupitem,"i"),
  17160. BUILDIN_DEF(enable_command,""),
  17161. BUILDIN_DEF(disable_command,""),
  17162. BUILDIN_DEF(getguildmember,"i?"),
  17163. BUILDIN_DEF(addspiritball,"ii?"),
  17164. BUILDIN_DEF(delspiritball,"i?"),
  17165. BUILDIN_DEF(countspiritball,"?"),
  17166. #include "../custom/script_def.inc"
  17167. {NULL,NULL,NULL},
  17168. };