script.c 474 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769
  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. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <math.h>
  54. #ifndef WIN32
  55. #include <sys/time.h>
  56. #endif
  57. #include <time.h>
  58. #include <setjmp.h>
  59. #include <errno.h>
  60. #ifdef BETA_THREAD_TEST
  61. #include "../common/atomic.h"
  62. #include "../common/spinlock.h"
  63. #include "../common/thread.h"
  64. #include "../common/mutex.h"
  65. #endif
  66. ///////////////////////////////////////////////////////////////////////////////
  67. //## TODO possible enhancements: [FlavioJS]
  68. // - 'callfunc' supporting labels in the current npc "::LabelName"
  69. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  70. // - 'function FuncName;' function declarations reverting to global functions
  71. // if local label isn't found
  72. // - join callfunc and callsub's functionality
  73. // - remove dynamic allocation in add_word()
  74. // - remove GETVALUE / SETVALUE
  75. // - clean up the set_reg / set_val / setd_sub mess
  76. // - detect invalid label references at parse-time
  77. //
  78. // struct script_state* st;
  79. //
  80. /// Returns the script_data at the target index
  81. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  82. /// Returns if the stack contains data at the target index
  83. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  84. /// Returns the index of the last data in the stack
  85. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  86. /// Pushes an int into the stack
  87. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  88. /// Pushes a string into the stack (script engine frees it automatically)
  89. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  90. /// Pushes a copy of a string into the stack
  91. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  92. /// Pushes a constant string into the stack (must never change or be freed)
  93. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  94. /// Pushes a nil into the stack
  95. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  96. /// Pushes a copy of the data in the target index
  97. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  98. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  99. #define script_isint(st,i) data_isint(script_getdata(st,i))
  100. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  101. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  102. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  103. // Returns name of currently running function
  104. #define script_getfuncname(st) ( st->funcname )
  105. // Note: "top" functions/defines use indexes relative to the top of the stack
  106. // -1 is the index of the data at the top
  107. /// Returns the script_data at the target index relative to the top of the stack
  108. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  109. /// Pushes a copy of the data in the target index relative to the top of the stack
  110. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  111. /// Removes the range of values [start,end[ relative to the top of the stack
  112. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  113. //
  114. // struct script_data* data;
  115. //
  116. /// Returns if the script data is a string
  117. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  118. /// Returns if the script data is an int
  119. #define data_isint(data) ( (data)->type == C_INT )
  120. /// Returns if the script data is a reference
  121. #define data_isreference(data) ( (data)->type == C_NAME )
  122. /// Returns if the script data is a label
  123. #define data_islabel(data) ( (data)->type == C_POS )
  124. /// Returns if the script data is an internal script function label
  125. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  126. /// Returns if this is a reference to a constant
  127. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  128. /// Returns if this a reference to a param
  129. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  130. /// Returns if this a reference to a variable
  131. //##TODO confirm it's C_NAME [FlavioJS]
  132. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  133. /// Returns the unique id of the reference (id and index)
  134. #define reference_getuid(data) ( (data)->u.num )
  135. /// Returns the id of the reference
  136. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  137. /// Returns the array index of the reference
  138. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  139. /// Returns the name of the reference
  140. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  141. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  142. #define reference_getref(data) ( (data)->ref )
  143. /// Returns the value of the constant
  144. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  145. /// Returns the type of param
  146. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  147. /// Composes the uid of a reference from the id and the index
  148. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  149. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  150. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  151. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  152. #define FETCH(n, t) \
  153. if( script_hasdata(st,n) ) \
  154. (t)=script_getnum(st,n);
  155. /// Maximum amount of elements in script arrays
  156. #define SCRIPT_MAX_ARRAYSIZE 128
  157. #define SCRIPT_CMD_SUCCESS 0 ///when a buildin cmd was correctly done
  158. #define SCRIPT_CMD_FAILURE 1 ///when an errors appear in cmd, show_debug will follow
  159. #define SCRIPT_BLOCK_SIZE 512
  160. enum { LABEL_NEXTLINE=1,LABEL_START };
  161. /// temporary buffer for passing around compiled bytecode
  162. /// @see add_scriptb, set_label, parse_script
  163. static unsigned char* script_buf = NULL;
  164. static int script_pos = 0, script_size = 0;
  165. static inline int GETVALUE(const unsigned char* buf, int i)
  166. {
  167. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  168. }
  169. static inline void SETVALUE(unsigned char* buf, int i, int n)
  170. {
  171. buf[i] = GetByte(n, 0);
  172. buf[i+1] = GetByte(n, 1);
  173. buf[i+2] = GetByte(n, 2);
  174. }
  175. // String buffer structures.
  176. // str_data stores string information
  177. static struct str_data_struct {
  178. enum c_op type;
  179. int str;
  180. int backpatch;
  181. int label;
  182. int (*func)(struct script_state *st);
  183. int val;
  184. int next;
  185. } *str_data = NULL;
  186. static int str_data_size = 0; // size of the data
  187. static int str_num = LABEL_START; // next id to be assigned
  188. // str_buf holds the strings themselves
  189. static char *str_buf;
  190. static int str_size = 0; // size of the buffer
  191. static int str_pos = 0; // next position to be assigned
  192. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  193. #define SCRIPT_HASH_SIZE 1021
  194. int str_hash[SCRIPT_HASH_SIZE];
  195. // Specifies which string hashing method to use
  196. //#define SCRIPT_HASH_DJB2
  197. //#define SCRIPT_HASH_SDBM
  198. #define SCRIPT_HASH_ELF
  199. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  200. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  201. static int parse_options=0;
  202. DBMap* script_get_label_db(void){ return scriptlabel_db; }
  203. DBMap* script_get_userfunc_db(void){ return userfunc_db; }
  204. // important buildin function references for usage in scripts
  205. static int buildin_set_ref = 0;
  206. static int buildin_callsub_ref = 0;
  207. static int buildin_callfunc_ref = 0;
  208. static int buildin_getelementofarray_ref = 0;
  209. // Caches compiled autoscript item code.
  210. // Note: This is not cleared when reloading itemdb.
  211. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  212. struct Script_Config script_config = {
  213. 1, // warn_func_mismatch_argtypes
  214. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  215. 0, INT_MAX, // input_min_value/input_max_value
  216. "OnPCDieEvent", //die_event_name
  217. "OnPCKillEvent", //kill_pc_event_name
  218. "OnNPCKillEvent", //kill_mob_event_name
  219. "OnPCLoginEvent", //login_event_name
  220. "OnPCLogoutEvent", //logout_event_name
  221. "OnPCLoadMapEvent", //loadmap_event_name
  222. "OnPCBaseLvUpEvent", //baselvup_event_name
  223. "OnPCJobLvUpEvent", //joblvup_event_name
  224. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  225. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  226. };
  227. static jmp_buf error_jump;
  228. static char* error_msg;
  229. static const char* error_pos;
  230. static int error_report; // if the error should produce output
  231. // Used by disp_warning_message
  232. static const char* parser_current_src;
  233. static const char* parser_current_file;
  234. static int parser_current_line;
  235. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  236. // [Eoe / jA 1080, 1081, 1094, 1164]
  237. enum curly_type {
  238. TYPE_NULL = 0,
  239. TYPE_IF,
  240. TYPE_SWITCH,
  241. TYPE_WHILE,
  242. TYPE_FOR,
  243. TYPE_DO,
  244. TYPE_USERFUNC,
  245. TYPE_ARGLIST // function argument list
  246. };
  247. enum e_arglist
  248. {
  249. ARGLIST_UNDEFINED = 0,
  250. ARGLIST_NO_PAREN = 1,
  251. ARGLIST_PAREN = 2,
  252. };
  253. static struct {
  254. struct {
  255. enum curly_type type;
  256. int index;
  257. int count;
  258. int flag;
  259. struct linkdb_node *case_label;
  260. } curly[256]; // Information right parenthesis
  261. int curly_count; // The number of right brackets
  262. int index; // Number of the syntax used in the script
  263. } syntax;
  264. const char* parse_curly_close(const char* p);
  265. const char* parse_syntax_close(const char* p);
  266. const char* parse_syntax_close_sub(const char* p,int* flag);
  267. const char* parse_syntax(const char* p);
  268. static int parse_syntax_for_flag = 0;
  269. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  270. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  271. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  272. int potion_target=0;
  273. c_op get_com(unsigned char *script,int *pos);
  274. int get_num(unsigned char *script,int *pos);
  275. typedef struct script_function {
  276. int (*func)(struct script_state *st);
  277. const char *name;
  278. const char *arg;
  279. } script_function;
  280. extern script_function buildin_func[];
  281. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  282. #ifdef BETA_THREAD_TEST
  283. /**
  284. * MySQL Query Slave
  285. **/
  286. static SPIN_LOCK queryThreadLock;
  287. static rAthread queryThread = NULL;
  288. static ramutex queryThreadMutex = NULL;
  289. static racond queryThreadCond = NULL;
  290. static volatile int32 queryThreadTerminate = 0;
  291. struct queryThreadEntry {
  292. bool ok;
  293. bool type; /* main db or log db? */
  294. struct script_state *st;
  295. };
  296. /* Ladies and Gentleman the Manager! */
  297. struct {
  298. struct queryThreadEntry **entry;/* array of structs */
  299. int count;
  300. int timer;/* used to receive processed entries */
  301. } queryThreadData;
  302. #endif
  303. /*==========================================
  304. * (Only those needed) local declaration prototype
  305. *------------------------------------------*/
  306. const char* parse_subexpr(const char* p,int limit);
  307. int run_func(struct script_state *st);
  308. int script_instancegetid(struct script_state *st);
  309. enum {
  310. MF_NOMEMO, //0
  311. MF_NOTELEPORT,
  312. MF_NOSAVE,
  313. MF_NOBRANCH,
  314. MF_NOPENALTY,
  315. MF_NOZENYPENALTY,
  316. MF_PVP,
  317. MF_PVP_NOPARTY,
  318. MF_PVP_NOGUILD,
  319. MF_GVG,
  320. MF_GVG_NOPARTY, //10
  321. MF_NOTRADE,
  322. MF_NOSKILL,
  323. MF_NOWARP,
  324. MF_PARTYLOCK,
  325. MF_NOICEWALL,
  326. MF_SNOW,
  327. MF_FOG,
  328. MF_SAKURA,
  329. MF_LEAVES,
  330. /**
  331. * No longer available, keeping here just in case it's back someday. [Ind]
  332. **/
  333. //MF_RAIN, //20
  334. // 21 free
  335. MF_NOGO = 22,
  336. MF_CLOUDS,
  337. MF_CLOUDS2,
  338. MF_FIREWORKS,
  339. MF_GVG_CASTLE,
  340. MF_GVG_DUNGEON,
  341. MF_NIGHTENABLED,
  342. MF_NOBASEEXP,
  343. MF_NOJOBEXP, //30
  344. MF_NOMOBLOOT,
  345. MF_NOMVPLOOT,
  346. MF_NORETURN,
  347. MF_NOWARPTO,
  348. MF_NIGHTMAREDROP,
  349. MF_RESTRICTED,
  350. MF_NOCOMMAND,
  351. MF_NODROP,
  352. MF_JEXP,
  353. MF_BEXP, //40
  354. MF_NOVENDING,
  355. MF_LOADEVENT,
  356. MF_NOCHAT,
  357. MF_NOEXPPENALTY,
  358. MF_GUILDLOCK,
  359. MF_TOWN,
  360. MF_AUTOTRADE,
  361. MF_ALLOWKS,
  362. MF_MONSTER_NOTELEPORT,
  363. MF_PVP_NOCALCRANK, //50
  364. MF_BATTLEGROUND,
  365. MF_RESET,
  366. MF_CHANNELAUTOJOIN,
  367. MF_NOUSECART,
  368. MF_NOITEMCONSUMPTION,
  369. MF_SUMSTARTMIRACLE,
  370. MF_NOMINEEFFECT,
  371. MF_NOLOCKON,
  372. MF_NOTOMB,
  373. MF_SKILL_DAMAGE //60
  374. };
  375. const char* script_op2name(int op)
  376. {
  377. #define RETURN_OP_NAME(type) case type: return #type
  378. switch( op )
  379. {
  380. RETURN_OP_NAME(C_NOP);
  381. RETURN_OP_NAME(C_POS);
  382. RETURN_OP_NAME(C_INT);
  383. RETURN_OP_NAME(C_PARAM);
  384. RETURN_OP_NAME(C_FUNC);
  385. RETURN_OP_NAME(C_STR);
  386. RETURN_OP_NAME(C_CONSTSTR);
  387. RETURN_OP_NAME(C_ARG);
  388. RETURN_OP_NAME(C_NAME);
  389. RETURN_OP_NAME(C_EOL);
  390. RETURN_OP_NAME(C_RETINFO);
  391. RETURN_OP_NAME(C_USERFUNC);
  392. RETURN_OP_NAME(C_USERFUNC_POS);
  393. // operators
  394. RETURN_OP_NAME(C_OP3);
  395. RETURN_OP_NAME(C_LOR);
  396. RETURN_OP_NAME(C_LAND);
  397. RETURN_OP_NAME(C_LE);
  398. RETURN_OP_NAME(C_LT);
  399. RETURN_OP_NAME(C_GE);
  400. RETURN_OP_NAME(C_GT);
  401. RETURN_OP_NAME(C_EQ);
  402. RETURN_OP_NAME(C_NE);
  403. RETURN_OP_NAME(C_XOR);
  404. RETURN_OP_NAME(C_OR);
  405. RETURN_OP_NAME(C_AND);
  406. RETURN_OP_NAME(C_ADD);
  407. RETURN_OP_NAME(C_SUB);
  408. RETURN_OP_NAME(C_MUL);
  409. RETURN_OP_NAME(C_DIV);
  410. RETURN_OP_NAME(C_MOD);
  411. RETURN_OP_NAME(C_NEG);
  412. RETURN_OP_NAME(C_LNOT);
  413. RETURN_OP_NAME(C_NOT);
  414. RETURN_OP_NAME(C_R_SHIFT);
  415. RETURN_OP_NAME(C_L_SHIFT);
  416. default:
  417. ShowDebug("script_op2name: unexpected op=%d\n", op);
  418. return "???";
  419. }
  420. #undef RETURN_OP_NAME
  421. }
  422. #ifdef DEBUG_DUMP_STACK
  423. static void script_dump_stack(struct script_state* st)
  424. {
  425. int i;
  426. ShowMessage("\tstart = %d\n", st->start);
  427. ShowMessage("\tend = %d\n", st->end);
  428. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  429. ShowMessage("\tsp = %d\n", st->stack->sp);
  430. for( i = 0; i < st->stack->sp; ++i )
  431. {
  432. struct script_data* data = &st->stack->stack_data[i];
  433. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  434. switch( data->type )
  435. {
  436. case C_INT:
  437. case C_POS:
  438. ShowMessage(" %d\n", data->u.num);
  439. break;
  440. case C_STR:
  441. case C_CONSTSTR:
  442. ShowMessage(" \"%s\"\n", data->u.str);
  443. break;
  444. case C_NAME:
  445. 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));
  446. break;
  447. case C_RETINFO:
  448. {
  449. struct script_retinfo* ri = data->u.ri;
  450. 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);
  451. }
  452. break;
  453. default:
  454. ShowMessage("\n");
  455. break;
  456. }
  457. }
  458. }
  459. #endif
  460. /// Reports on the console the src of a script error.
  461. static void script_reportsrc(struct script_state *st)
  462. {
  463. struct block_list* bl;
  464. if( st->oid == 0 )
  465. return; //Can't report source.
  466. bl = map_id2bl(st->oid);
  467. if( bl == NULL )
  468. return;
  469. switch( bl->type )
  470. {
  471. case BL_NPC:
  472. if( bl->m >= 0 )
  473. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  474. else
  475. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  476. break;
  477. default:
  478. if( bl->m >= 0 )
  479. 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);
  480. else
  481. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  482. break;
  483. }
  484. }
  485. /// Reports on the console information about the script data.
  486. static void script_reportdata(struct script_data* data)
  487. {
  488. if( data == NULL )
  489. return;
  490. switch( data->type )
  491. {
  492. case C_NOP:// no value
  493. ShowDebug("Data: nothing (nil)\n");
  494. break;
  495. case C_INT:// number
  496. ShowDebug("Data: number value=%d\n", data->u.num);
  497. break;
  498. case C_STR:
  499. case C_CONSTSTR:// string
  500. if( data->u.str )
  501. {
  502. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  503. }
  504. else
  505. {
  506. ShowDebug("Data: string value=NULL\n");
  507. }
  508. break;
  509. case C_NAME:// reference
  510. if( reference_tovariable(data) )
  511. {// variable
  512. const char* name = reference_getname(data);
  513. if( not_array_variable(*name) )
  514. ShowDebug("Data: variable name='%s'\n", name);
  515. else
  516. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  517. }
  518. else if( reference_toconstant(data) )
  519. {// constant
  520. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  521. }
  522. else if( reference_toparam(data) )
  523. {// param
  524. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  525. }
  526. else
  527. {// ???
  528. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  529. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  530. }
  531. break;
  532. case C_POS:// label
  533. ShowDebug("Data: label pos=%d\n", data->u.num);
  534. break;
  535. default:
  536. ShowDebug("Data: %s\n", script_op2name(data->type));
  537. break;
  538. }
  539. }
  540. /// Reports on the console information about the current built-in function.
  541. static void script_reportfunc(struct script_state* st)
  542. {
  543. int i, params, id;
  544. struct script_data* data;
  545. if( !script_hasdata(st,0) )
  546. {// no stack
  547. return;
  548. }
  549. data = script_getdata(st,0);
  550. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  551. {// script currently not executing a built-in function or corrupt stack
  552. return;
  553. }
  554. id = reference_getid(data);
  555. params = script_lastdata(st)-1;
  556. if( params > 0 )
  557. {
  558. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  559. for( i = 2; i <= script_lastdata(st); i++ )
  560. {
  561. script_reportdata(script_getdata(st,i));
  562. }
  563. }
  564. else
  565. {
  566. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  567. }
  568. }
  569. /*==========================================
  570. * Output error message
  571. *------------------------------------------*/
  572. static void disp_error_message2(const char *mes,const char *pos,int report)
  573. {
  574. error_msg = aStrdup(mes);
  575. error_pos = pos;
  576. error_report = report;
  577. longjmp( error_jump, 1 );
  578. }
  579. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  580. static void disp_warning_message(const char *mes, const char *pos) {
  581. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  582. }
  583. /// Checks event parameter validity
  584. static void check_event(struct script_state *st, const char *evt)
  585. {
  586. if( evt && evt[0] && !stristr(evt, "::On") )
  587. {
  588. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  589. script_reportsrc(st);
  590. }
  591. }
  592. /*==========================================
  593. * Hashes the input string
  594. *------------------------------------------*/
  595. static unsigned int calc_hash(const char* p)
  596. {
  597. unsigned int h;
  598. #if defined(SCRIPT_HASH_DJB2)
  599. h = 5381;
  600. while( *p ) // hash*33 + c
  601. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  602. #elif defined(SCRIPT_HASH_SDBM)
  603. h = 0;
  604. while( *p ) // hash*65599 + c
  605. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  606. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  607. h = 0;
  608. while( *p ){
  609. unsigned int g;
  610. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  611. g = h & 0xF0000000;
  612. if( g )
  613. {
  614. h ^= g >> 24;
  615. h &= ~g;
  616. }
  617. }
  618. #else // athena hash
  619. h = 0;
  620. while( *p )
  621. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  622. #endif
  623. return h % SCRIPT_HASH_SIZE;
  624. }
  625. /*==========================================
  626. * str_data manipulation functions
  627. *------------------------------------------*/
  628. /// Looks up string using the provided id.
  629. const char* get_str(int id)
  630. {
  631. Assert( id >= LABEL_START && id < str_size );
  632. return str_buf+str_data[id].str;
  633. }
  634. /// Returns the uid of the string, or -1.
  635. static int search_str(const char* p)
  636. {
  637. int i;
  638. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  639. if( strcasecmp(get_str(i),p) == 0 )
  640. return i;
  641. return -1;
  642. }
  643. /// Stores a copy of the string and returns its id.
  644. /// If an identical string is already present, returns its id instead.
  645. int add_str(const char* p)
  646. {
  647. int i, h;
  648. int len;
  649. h = calc_hash(p);
  650. if( str_hash[h] == 0 )
  651. {// empty bucket, add new node here
  652. str_hash[h] = str_num;
  653. }
  654. else
  655. {// scan for end of list, or occurence of identical string
  656. for( i = str_hash[h]; ; i = str_data[i].next )
  657. {
  658. if( strcasecmp(get_str(i),p) == 0 )
  659. return i; // string already in list
  660. if( str_data[i].next == 0 )
  661. break; // reached the end
  662. }
  663. // append node to end of list
  664. str_data[i].next = str_num;
  665. }
  666. // grow list if neccessary
  667. if( str_num >= str_data_size )
  668. {
  669. str_data_size += 128;
  670. RECREATE(str_data,struct str_data_struct,str_data_size);
  671. memset(str_data + (str_data_size - 128), '\0', 128);
  672. }
  673. len=(int)strlen(p);
  674. // grow string buffer if neccessary
  675. while( str_pos+len+1 >= str_size )
  676. {
  677. str_size += 256;
  678. RECREATE(str_buf,char,str_size);
  679. memset(str_buf + (str_size - 256), '\0', 256);
  680. }
  681. safestrncpy(str_buf+str_pos, p, len+1);
  682. str_data[str_num].type = C_NOP;
  683. str_data[str_num].str = str_pos;
  684. str_data[str_num].next = 0;
  685. str_data[str_num].func = NULL;
  686. str_data[str_num].backpatch = -1;
  687. str_data[str_num].label = -1;
  688. str_pos += len+1;
  689. return str_num++;
  690. }
  691. /// Appends 1 byte to the script buffer.
  692. static void add_scriptb(int a)
  693. {
  694. if( script_pos+1 >= script_size )
  695. {
  696. script_size += SCRIPT_BLOCK_SIZE;
  697. RECREATE(script_buf,unsigned char,script_size);
  698. }
  699. script_buf[script_pos++] = (uint8)(a);
  700. }
  701. /// Appends a c_op value to the script buffer.
  702. /// The value is variable-length encoded into 8-bit blocks.
  703. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  704. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  705. static void add_scriptc(int a)
  706. {
  707. while( a >= 0x40 )
  708. {
  709. add_scriptb((a&0x3f)|0x40);
  710. a = (a - 0x40) >> 6;
  711. }
  712. add_scriptb(a);
  713. }
  714. /// Appends an integer value to the script buffer.
  715. /// The value is variable-length encoded into 8-bit blocks.
  716. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  717. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  718. static void add_scripti(int a)
  719. {
  720. while( a >= 0x40 )
  721. {
  722. add_scriptb((a&0x3f)|0xc0);
  723. a = (a - 0x40) >> 6;
  724. }
  725. add_scriptb(a|0x80);
  726. }
  727. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  728. ///
  729. /// @param l The id of the str_data entry
  730. // Maximum up to 16M
  731. static void add_scriptl(int l)
  732. {
  733. int backpatch = str_data[l].backpatch;
  734. switch(str_data[l].type){
  735. case C_POS:
  736. case C_USERFUNC_POS:
  737. add_scriptc(C_POS);
  738. add_scriptb(str_data[l].label);
  739. add_scriptb(str_data[l].label>>8);
  740. add_scriptb(str_data[l].label>>16);
  741. break;
  742. case C_NOP:
  743. case C_USERFUNC:
  744. // Embedded data backpatch there is a possibility of label
  745. add_scriptc(C_NAME);
  746. str_data[l].backpatch = script_pos;
  747. add_scriptb(backpatch);
  748. add_scriptb(backpatch>>8);
  749. add_scriptb(backpatch>>16);
  750. break;
  751. case C_INT:
  752. add_scripti(abs(str_data[l].val));
  753. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  754. add_scriptc(C_NEG);
  755. break;
  756. default: // assume C_NAME
  757. add_scriptc(C_NAME);
  758. add_scriptb(l);
  759. add_scriptb(l>>8);
  760. add_scriptb(l>>16);
  761. break;
  762. }
  763. }
  764. /*==========================================
  765. * Resolve the label
  766. *------------------------------------------*/
  767. void set_label(int l,int pos, const char* script_pos)
  768. {
  769. int i,next;
  770. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  771. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  772. disp_error_message("set_label: invalid label name",script_pos);
  773. return;
  774. }
  775. if(str_data[l].label!=-1){
  776. disp_error_message("set_label: dup label ",script_pos);
  777. return;
  778. }
  779. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  780. str_data[l].label=pos;
  781. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  782. next=GETVALUE(script_buf,i);
  783. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  784. SETVALUE(script_buf,i,pos);
  785. i=next;
  786. }
  787. }
  788. /// Skips spaces and/or comments.
  789. const char* skip_space(const char* p)
  790. {
  791. if( p == NULL )
  792. return NULL;
  793. for(;;)
  794. {
  795. while( ISSPACE(*p) )
  796. ++p;
  797. if( *p == '/' && p[1] == '/' )
  798. {// line comment
  799. while(*p && *p!='\n')
  800. ++p;
  801. }
  802. else if( *p == '/' && p[1] == '*' )
  803. {// block comment
  804. p += 2;
  805. for(;;)
  806. {
  807. if( *p == '\0' ) {
  808. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  809. return p;
  810. }
  811. if( *p == '*' && p[1] == '/' )
  812. {// end of block comment
  813. p += 2;
  814. break;
  815. }
  816. ++p;
  817. }
  818. }
  819. else
  820. break;
  821. }
  822. return p;
  823. }
  824. /// Skips a word.
  825. /// A word consists of undercores and/or alphanumeric characters,
  826. /// and valid variable prefixes/postfixes.
  827. static
  828. const char* skip_word(const char* p)
  829. {
  830. // prefix
  831. switch( *p )
  832. {
  833. case '@':// temporary char variable
  834. ++p; break;
  835. case '#':// account variable
  836. p += ( p[1] == '#' ? 2 : 1 ); break;
  837. case '\'':// instance variable
  838. ++p; break;
  839. case '.':// npc variable
  840. p += ( p[1] == '@' ? 2 : 1 ); break;
  841. case '$':// global variable
  842. p += ( p[1] == '@' ? 2 : 1 ); break;
  843. }
  844. while( ISALNUM(*p) || *p == '_' )
  845. ++p;
  846. // postfix
  847. if( *p == '$' )// string
  848. p++;
  849. return p;
  850. }
  851. /// Adds a word to str_data.
  852. /// @see skip_word
  853. /// @see add_str
  854. static
  855. int add_word(const char* p)
  856. {
  857. char* word;
  858. int len;
  859. int i;
  860. // Check for a word
  861. len = skip_word(p) - p;
  862. if( len == 0 )
  863. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  864. // Duplicate the word
  865. word = (char*)aMalloc(len+1);
  866. memcpy(word, p, len);
  867. word[len] = 0;
  868. // add the word
  869. i = add_str(word);
  870. aFree(word);
  871. return i;
  872. }
  873. /// Parses a function call.
  874. /// The argument list can have parenthesis or not.
  875. /// The number of arguments is checked.
  876. static
  877. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  878. {
  879. const char* p2;
  880. const char* arg=NULL;
  881. int func;
  882. func = add_word(p);
  883. if( str_data[func].type == C_FUNC ){
  884. // buildin function
  885. add_scriptl(func);
  886. add_scriptc(C_ARG);
  887. arg = buildin_func[str_data[func].val].arg;
  888. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  889. // script defined function
  890. add_scriptl(buildin_callsub_ref);
  891. add_scriptc(C_ARG);
  892. add_scriptl(func);
  893. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  894. if( *arg == 0 )
  895. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  896. if( *arg != '*' )
  897. ++arg; // count func as argument
  898. } else {
  899. #ifdef SCRIPT_CALLFUNC_CHECK
  900. const char* name = get_str(func);
  901. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  902. #endif
  903. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  904. #ifdef SCRIPT_CALLFUNC_CHECK
  905. } else {;
  906. add_scriptl(buildin_callfunc_ref);
  907. add_scriptc(C_ARG);
  908. add_scriptc(C_STR);
  909. while( *name ) add_scriptb(*name ++);
  910. add_scriptb(0);
  911. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  912. if( *arg != '*' ) ++ arg;
  913. }
  914. #endif
  915. }
  916. p = skip_word(p);
  917. p = skip_space(p);
  918. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  919. syntax.curly[syntax.curly_count].count = 0;
  920. if( *p == ';' )
  921. {// <func name> ';'
  922. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  923. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  924. {// <func name> '(' ')'
  925. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  926. p = p2;
  927. /*
  928. } else if( 0 && require_paren && *p != '(' )
  929. {// <func name>
  930. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  931. */
  932. } else
  933. {// <func name> <arg list>
  934. if( require_paren ){
  935. if( *p != '(' )
  936. disp_error_message("need '('",p);
  937. ++p; // skip '('
  938. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  939. } else if( *p == '(' ){
  940. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  941. } else {
  942. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  943. }
  944. ++syntax.curly_count;
  945. while( *arg ) {
  946. p2=parse_subexpr(p,-1);
  947. if( p == p2 )
  948. break; // not an argument
  949. if( *arg != '*' )
  950. ++arg; // next argument
  951. p=skip_space(p2);
  952. if( *arg == 0 || *p != ',' )
  953. break; // no more arguments
  954. ++p; // skip comma
  955. }
  956. --syntax.curly_count;
  957. }
  958. if( *arg && *arg != '?' && *arg != '*' )
  959. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  960. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  961. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  962. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  963. if( *p != ')' )
  964. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  965. ++p;
  966. }
  967. add_scriptc(C_FUNC);
  968. return p;
  969. }
  970. /// Processes end of logical script line.
  971. /// @param first When true, only fix up scheduling data is initialized
  972. /// @param p Script position for error reporting in set_label
  973. static void parse_nextline(bool first, const char* p)
  974. {
  975. if( !first )
  976. {
  977. add_scriptc(C_EOL); // mark end of line for stack cleanup
  978. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  979. }
  980. // initialize data for new '-' label fix up scheduling
  981. str_data[LABEL_NEXTLINE].type = C_NOP;
  982. str_data[LABEL_NEXTLINE].backpatch = -1;
  983. str_data[LABEL_NEXTLINE].label = -1;
  984. }
  985. /// Parse a variable assignment using the direct equals operator
  986. /// @param p script position where the function should run from
  987. /// @return NULL if not a variable assignment, the new position otherwise
  988. const char* parse_variable(const char* p) {
  989. int i, j, word;
  990. c_op type = C_NOP;
  991. const char *p2 = NULL;
  992. const char *var = p;
  993. // skip the variable where applicable
  994. p = skip_word(p);
  995. p = skip_space(p);
  996. if( p == NULL ) {// end of the line or invalid buffer
  997. return NULL;
  998. }
  999. if( *p == '[' ) {// array variable so process the array as appropriate
  1000. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1001. if( *p ++ == ']' && --(j) == 0 ) break;
  1002. if( *p == '[' ) ++ j;
  1003. }
  1004. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1005. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1006. }
  1007. }
  1008. if( type == C_NOP &&
  1009. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1010. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1011. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1012. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1013. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1014. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1015. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1016. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1017. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1018. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1019. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  1020. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  1021. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1022. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1023. ) )
  1024. {// failed to find a matching operator combination so invalid
  1025. return NULL;
  1026. }
  1027. switch( type ) {
  1028. case C_EQ: {// incremental modifier
  1029. p = skip_space( &p[1] );
  1030. }
  1031. break;
  1032. case C_L_SHIFT:
  1033. case C_R_SHIFT: {// left or right shift modifier
  1034. p = skip_space( &p[3] );
  1035. }
  1036. break;
  1037. default: {// normal incremental command
  1038. p = skip_space( &p[2] );
  1039. }
  1040. }
  1041. if( p == NULL ) {// end of line or invalid buffer
  1042. return NULL;
  1043. }
  1044. // push the set function onto the stack
  1045. add_scriptl(buildin_set_ref);
  1046. add_scriptc(C_ARG);
  1047. // always append parenthesis to avoid errors
  1048. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1049. syntax.curly[syntax.curly_count].count = 0;
  1050. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1051. // increment the total curly count for the position in the script
  1052. ++ syntax.curly_count;
  1053. // parse the variable currently being modified
  1054. word = add_word(var);
  1055. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  1056. {// cannot assign a variable which exists as a function or label
  1057. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1058. }
  1059. if( p2 ) {// process the variable index
  1060. const char* p3 = NULL;
  1061. // push the getelementofarray method into the stack
  1062. add_scriptl(buildin_getelementofarray_ref);
  1063. add_scriptc(C_ARG);
  1064. add_scriptl(word);
  1065. // process the sub-expression for this assignment
  1066. p3 = parse_subexpr(p2 + 1, 1);
  1067. p3 = skip_space(p3);
  1068. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1069. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1070. }
  1071. // push the closing function stack operator onto the stack
  1072. add_scriptc(C_FUNC);
  1073. p3 ++;
  1074. } else {// simply push the variable or value onto the stack
  1075. add_scriptl(word);
  1076. }
  1077. if( type != C_EQ )
  1078. add_scriptc(C_REF);
  1079. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1080. add_scripti(1);
  1081. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1082. } else {// process the value as an expression
  1083. p = parse_subexpr(p, -1);
  1084. if( type != C_EQ )
  1085. {// push the type of modifier onto the stack
  1086. add_scriptc(type);
  1087. }
  1088. }
  1089. // decrement the curly count for the position within the script
  1090. -- syntax.curly_count;
  1091. // close the script by appending the function operator
  1092. add_scriptc(C_FUNC);
  1093. // push the buffer from the method
  1094. return p;
  1095. }
  1096. /*==========================================
  1097. * Analysis section
  1098. *------------------------------------------*/
  1099. const char* parse_simpleexpr(const char *p)
  1100. {
  1101. long long i;
  1102. p=skip_space(p);
  1103. if(*p==';' || *p==',')
  1104. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1105. if(*p=='('){
  1106. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1107. ++syntax.curly[i].count;
  1108. p=parse_subexpr(p+1,-1);
  1109. p=skip_space(p);
  1110. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1111. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1112. ){
  1113. if( *p == ',' ){
  1114. syntax.curly[i].flag = ARGLIST_PAREN;
  1115. return p;
  1116. } else
  1117. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1118. }
  1119. if( *p != ')' )
  1120. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1121. ++p;
  1122. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  1123. char *np;
  1124. while(*p == '0' && ISDIGIT(p[1])) p++;
  1125. i=strtoll(p,&np,0);
  1126. if( i < INT_MIN ) {
  1127. i = INT_MIN;
  1128. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1129. } else if( i > INT_MAX ) {
  1130. i = INT_MAX;
  1131. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1132. }
  1133. add_scripti((int)i);
  1134. p=np;
  1135. } else if(*p=='"'){
  1136. add_scriptc(C_STR);
  1137. p++;
  1138. while( *p && *p != '"' ){
  1139. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1140. {
  1141. char buf[8];
  1142. size_t len = skip_escaped_c(p) - p;
  1143. size_t n = sv_unescape_c(buf, p, len);
  1144. if( n != 1 )
  1145. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1146. p += len;
  1147. add_scriptb(*buf);
  1148. continue;
  1149. }
  1150. else if( *p == '\n' )
  1151. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1152. add_scriptb(*p++);
  1153. }
  1154. if(!*p)
  1155. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1156. add_scriptb(0);
  1157. p++; //'"'
  1158. } else {
  1159. int l;
  1160. const char* pv;
  1161. // label , register , function etc
  1162. if(skip_word(p)==p)
  1163. disp_error_message("parse_simpleexpr: unexpected character",p);
  1164. l=add_word(p);
  1165. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1166. return parse_callfunc(p,1,0);
  1167. #ifdef SCRIPT_CALLFUNC_CHECK
  1168. else {
  1169. const char* name = get_str(l);
  1170. if( strdb_get(userfunc_db,name) != NULL ) {
  1171. return parse_callfunc(p,1,1);
  1172. }
  1173. }
  1174. #endif
  1175. if( (pv = parse_variable(p)) )
  1176. {// successfully processed a variable assignment
  1177. return pv;
  1178. }
  1179. p=skip_word(p);
  1180. if( *p == '[' ){
  1181. // array(name[i] => getelementofarray(name,i) )
  1182. add_scriptl(buildin_getelementofarray_ref);
  1183. add_scriptc(C_ARG);
  1184. add_scriptl(l);
  1185. p=parse_subexpr(p+1,-1);
  1186. p=skip_space(p);
  1187. if( *p != ']' )
  1188. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1189. ++p;
  1190. add_scriptc(C_FUNC);
  1191. }else
  1192. add_scriptl(l);
  1193. }
  1194. return p;
  1195. }
  1196. /*==========================================
  1197. * Analysis of the expression
  1198. *------------------------------------------*/
  1199. const char* parse_subexpr(const char* p,int limit)
  1200. {
  1201. int op,opl,len;
  1202. const char* tmpp;
  1203. p=skip_space(p);
  1204. if( *p == '-' ){
  1205. tmpp = skip_space(p+1);
  1206. if( *tmpp == ';' || *tmpp == ',' ){
  1207. add_scriptl(LABEL_NEXTLINE);
  1208. p++;
  1209. return p;
  1210. }
  1211. }
  1212. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1213. p=parse_subexpr(p+1,10);
  1214. add_scriptc(op);
  1215. } else
  1216. p=parse_simpleexpr(p);
  1217. p=skip_space(p);
  1218. while((
  1219. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1220. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1221. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1222. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1223. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1224. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1225. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1226. (op=C_AND,opl=6,len=1,*p=='&') ||
  1227. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1228. (op=C_OR,opl=5,len=1,*p=='|') ||
  1229. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1230. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1231. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1232. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1233. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1234. (op=C_GT,opl=3,len=1,*p=='>') ||
  1235. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1236. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1237. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1238. p+=len;
  1239. if(op == C_OP3) {
  1240. p=parse_subexpr(p,-1);
  1241. p=skip_space(p);
  1242. if( *(p++) != ':')
  1243. disp_error_message("parse_subexpr: expected ':'", p-1);
  1244. p=parse_subexpr(p,-1);
  1245. } else {
  1246. p=parse_subexpr(p,opl);
  1247. }
  1248. add_scriptc(op);
  1249. p=skip_space(p);
  1250. }
  1251. return p; /* return first untreated operator */
  1252. }
  1253. /*==========================================
  1254. * Evaluation of the expression
  1255. *------------------------------------------*/
  1256. const char* parse_expr(const char *p)
  1257. {
  1258. switch(*p){
  1259. case ')': case ';': case ':': case '[': case ']':
  1260. case '}':
  1261. disp_error_message("parse_expr: unexpected character",p);
  1262. }
  1263. p=parse_subexpr(p,-1);
  1264. return p;
  1265. }
  1266. /*==========================================
  1267. * Analysis of the line
  1268. *------------------------------------------*/
  1269. const char* parse_line(const char* p)
  1270. {
  1271. const char* p2;
  1272. p=skip_space(p);
  1273. if(*p==';') {
  1274. //Close decision for if(); for(); while();
  1275. p = parse_syntax_close(p + 1);
  1276. return p;
  1277. }
  1278. if(*p==')' && parse_syntax_for_flag)
  1279. return p+1;
  1280. p = skip_space(p);
  1281. if(p[0] == '{') {
  1282. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1283. syntax.curly[syntax.curly_count].count = -1;
  1284. syntax.curly[syntax.curly_count].index = -1;
  1285. syntax.curly_count++;
  1286. return p + 1;
  1287. } else if(p[0] == '}') {
  1288. return parse_curly_close(p);
  1289. }
  1290. // Syntax-related processing
  1291. p2 = parse_syntax(p);
  1292. if(p2 != NULL)
  1293. return p2;
  1294. // attempt to process a variable assignment
  1295. p2 = parse_variable(p);
  1296. if( p2 != NULL )
  1297. {// variable assignment processed so leave the method
  1298. return parse_syntax_close(p2 + 1);
  1299. }
  1300. p = parse_callfunc(p,0,0);
  1301. p = skip_space(p);
  1302. if(parse_syntax_for_flag) {
  1303. if( *p != ')' )
  1304. disp_error_message("parse_line: expected ')'",p);
  1305. } else {
  1306. if( *p != ';' )
  1307. disp_error_message("parse_line: expected ';'",p);
  1308. }
  1309. //Binding decision for if(), for(), while()
  1310. p = parse_syntax_close(p+1);
  1311. return p;
  1312. }
  1313. // { ... } Closing process
  1314. const char* parse_curly_close(const char* p)
  1315. {
  1316. if(syntax.curly_count <= 0) {
  1317. disp_error_message("parse_curly_close: unexpected string",p);
  1318. return p + 1;
  1319. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1320. syntax.curly_count--;
  1321. //Close decision if, for , while
  1322. p = parse_syntax_close(p + 1);
  1323. return p;
  1324. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1325. //Closing switch()
  1326. int pos = syntax.curly_count-1;
  1327. char label[256];
  1328. int l;
  1329. // Remove temporary variables
  1330. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1331. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1332. parse_line(label);
  1333. syntax.curly_count--;
  1334. // Go to the end pointer unconditionally
  1335. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1336. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1337. parse_line(label);
  1338. syntax.curly_count--;
  1339. // You are here labeled
  1340. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1341. l=add_str(label);
  1342. set_label(l,script_pos, p);
  1343. if(syntax.curly[pos].flag) {
  1344. //Exists default
  1345. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1346. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1347. parse_line(label);
  1348. syntax.curly_count--;
  1349. }
  1350. // Label end
  1351. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1352. l=add_str(label);
  1353. set_label(l,script_pos, p);
  1354. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1355. syntax.curly_count--;
  1356. //Closing decision if, for , while
  1357. p = parse_syntax_close(p + 1);
  1358. return p;
  1359. } else {
  1360. disp_error_message("parse_curly_close: unexpected string",p);
  1361. return p + 1;
  1362. }
  1363. }
  1364. // Syntax-related processing
  1365. // break, case, continue, default, do, for, function,
  1366. // if, switch, while ? will handle this internally.
  1367. const char* parse_syntax(const char* p)
  1368. {
  1369. const char *p2 = skip_word(p);
  1370. switch(*p) {
  1371. case 'B':
  1372. case 'b':
  1373. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1374. // break Processing
  1375. char label[256];
  1376. int pos = syntax.curly_count - 1;
  1377. while(pos >= 0) {
  1378. if(syntax.curly[pos].type == TYPE_DO) {
  1379. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1380. break;
  1381. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1382. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1383. break;
  1384. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1385. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1386. break;
  1387. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1388. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1389. break;
  1390. }
  1391. pos--;
  1392. }
  1393. if(pos < 0) {
  1394. disp_error_message("parse_syntax: unexpected 'break'",p);
  1395. } else {
  1396. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1397. parse_line(label);
  1398. syntax.curly_count--;
  1399. }
  1400. p = skip_space(p2);
  1401. if(*p != ';')
  1402. disp_error_message("parse_syntax: expected ';'",p);
  1403. // Closing decision if, for , while
  1404. p = parse_syntax_close(p + 1);
  1405. return p;
  1406. }
  1407. break;
  1408. case 'c':
  1409. case 'C':
  1410. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1411. //Processing case
  1412. int pos = syntax.curly_count-1;
  1413. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1414. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1415. return p+1;
  1416. } else {
  1417. char label[256];
  1418. int l,v;
  1419. char *np;
  1420. if(syntax.curly[pos].count != 1) {
  1421. //Jump for FALLTHRU
  1422. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1423. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1424. parse_line(label);
  1425. syntax.curly_count--;
  1426. // You are here labeled
  1427. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1428. l=add_str(label);
  1429. set_label(l,script_pos, p);
  1430. }
  1431. //Decision statement switch
  1432. p = skip_space(p2);
  1433. if(p == p2) {
  1434. disp_error_message("parse_syntax: expected a space ' '",p);
  1435. }
  1436. // check whether case label is integer or not
  1437. v = strtol(p,&np,0);
  1438. if(np == p) { //Check for constants
  1439. p2 = skip_word(p);
  1440. v = p2-p; // length of word at p2
  1441. memcpy(label,p,v);
  1442. label[v]='\0';
  1443. if( !script_get_constant(label, &v) )
  1444. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1445. p = skip_word(p);
  1446. } else { //Numeric value
  1447. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1448. p++;
  1449. p = skip_word(p);
  1450. if(np != p)
  1451. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1452. }
  1453. p = skip_space(p);
  1454. if(*p != ':')
  1455. disp_error_message("parse_syntax: expect ':'",p);
  1456. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1457. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1458. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1459. // Bad I do not parse twice
  1460. p2 = parse_line(label);
  1461. parse_line(p2);
  1462. syntax.curly_count--;
  1463. if(syntax.curly[pos].count != 1) {
  1464. // Label after the completion of FALLTHRU
  1465. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1466. l=add_str(label);
  1467. set_label(l,script_pos,p);
  1468. }
  1469. // check duplication of case label [Rayce]
  1470. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1471. disp_error_message("parse_syntax: dup 'case'",p);
  1472. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1473. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1474. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1475. parse_line(label);
  1476. syntax.curly_count--;
  1477. syntax.curly[pos].count++;
  1478. }
  1479. return p + 1;
  1480. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1481. // Processing continue
  1482. char label[256];
  1483. int pos = syntax.curly_count - 1;
  1484. while(pos >= 0) {
  1485. if(syntax.curly[pos].type == TYPE_DO) {
  1486. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1487. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1488. break;
  1489. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1490. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1491. break;
  1492. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1493. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1494. break;
  1495. }
  1496. pos--;
  1497. }
  1498. if(pos < 0) {
  1499. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1500. } else {
  1501. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1502. parse_line(label);
  1503. syntax.curly_count--;
  1504. }
  1505. p = skip_space(p2);
  1506. if(*p != ';')
  1507. disp_error_message("parse_syntax: expected ';'",p);
  1508. //Closing decision if, for , while
  1509. p = parse_syntax_close(p + 1);
  1510. return p;
  1511. }
  1512. break;
  1513. case 'd':
  1514. case 'D':
  1515. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1516. // Switch - default processing
  1517. int pos = syntax.curly_count-1;
  1518. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1519. disp_error_message("parse_syntax: unexpected 'default'",p);
  1520. } else if(syntax.curly[pos].flag) {
  1521. disp_error_message("parse_syntax: dup 'default'",p);
  1522. } else {
  1523. char label[256];
  1524. int l;
  1525. // Put the label location
  1526. p = skip_space(p2);
  1527. if(*p != ':') {
  1528. disp_error_message("parse_syntax: expected ':'",p);
  1529. }
  1530. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1531. l=add_str(label);
  1532. set_label(l,script_pos,p);
  1533. // Skip to the next link w/o condition
  1534. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1535. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1536. parse_line(label);
  1537. syntax.curly_count--;
  1538. // The default label
  1539. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1540. l=add_str(label);
  1541. set_label(l,script_pos,p);
  1542. syntax.curly[syntax.curly_count - 1].flag = 1;
  1543. syntax.curly[pos].count++;
  1544. }
  1545. return p + 1;
  1546. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1547. int l;
  1548. char label[256];
  1549. p=skip_space(p2);
  1550. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1551. syntax.curly[syntax.curly_count].count = 1;
  1552. syntax.curly[syntax.curly_count].index = syntax.index++;
  1553. syntax.curly[syntax.curly_count].flag = 0;
  1554. // Label of the (do) form here
  1555. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1556. l=add_str(label);
  1557. set_label(l,script_pos,p);
  1558. syntax.curly_count++;
  1559. return p;
  1560. }
  1561. break;
  1562. case 'f':
  1563. case 'F':
  1564. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1565. int l;
  1566. char label[256];
  1567. int pos = syntax.curly_count;
  1568. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1569. syntax.curly[syntax.curly_count].count = 1;
  1570. syntax.curly[syntax.curly_count].index = syntax.index++;
  1571. syntax.curly[syntax.curly_count].flag = 0;
  1572. syntax.curly_count++;
  1573. p=skip_space(p2);
  1574. if(*p != '(')
  1575. disp_error_message("parse_syntax: expected '('",p);
  1576. p++;
  1577. // Execute the initialization statement
  1578. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1579. p=parse_line(p);
  1580. syntax.curly_count--;
  1581. // Form the start of label decision
  1582. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1583. l=add_str(label);
  1584. set_label(l,script_pos,p);
  1585. p=skip_space(p);
  1586. if(*p == ';') {
  1587. // For (; Because the pattern of always true ;)
  1588. ;
  1589. } else {
  1590. // Skip to the end point if the condition is false
  1591. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1592. add_scriptl(add_str("jump_zero"));
  1593. add_scriptc(C_ARG);
  1594. p=parse_expr(p);
  1595. p=skip_space(p);
  1596. add_scriptl(add_str(label));
  1597. add_scriptc(C_FUNC);
  1598. }
  1599. if(*p != ';')
  1600. disp_error_message("parse_syntax: expected ';'",p);
  1601. p++;
  1602. // Skip to the beginning of the loop
  1603. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1604. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1605. parse_line(label);
  1606. syntax.curly_count--;
  1607. // Labels to form the next loop
  1608. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1609. l=add_str(label);
  1610. set_label(l,script_pos,p);
  1611. // Process the next time you enter the loop
  1612. // A ')' last for; flag to be treated as'
  1613. parse_syntax_for_flag = 1;
  1614. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1615. p=parse_line(p);
  1616. syntax.curly_count--;
  1617. parse_syntax_for_flag = 0;
  1618. // Skip to the determination process conditions
  1619. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1620. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1621. parse_line(label);
  1622. syntax.curly_count--;
  1623. // Loop start labeling
  1624. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1625. l=add_str(label);
  1626. set_label(l,script_pos,p);
  1627. return p;
  1628. }
  1629. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1630. {// internal script function
  1631. const char *func_name;
  1632. func_name = skip_space(p2);
  1633. p = skip_word(func_name);
  1634. if( p == func_name )
  1635. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1636. p2 = skip_space(p);
  1637. if( *p2 == ';' )
  1638. {// function <name> ;
  1639. // function declaration - just register the name
  1640. int l;
  1641. l = add_word(func_name);
  1642. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1643. str_data[l].type = C_USERFUNC;
  1644. else if( str_data[l].type == C_USERFUNC )
  1645. ; // already registered
  1646. else
  1647. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1648. // Close condition of if, for, while
  1649. p = parse_syntax_close(p2 + 1);
  1650. return p;
  1651. }
  1652. else if(*p2 == '{')
  1653. {// function <name> <line/block of code>
  1654. char label[256];
  1655. int l;
  1656. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1657. syntax.curly[syntax.curly_count].count = 1;
  1658. syntax.curly[syntax.curly_count].index = syntax.index++;
  1659. syntax.curly[syntax.curly_count].flag = 0;
  1660. ++syntax.curly_count;
  1661. // Jump over the function code
  1662. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1663. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1664. ++syntax.curly_count;
  1665. parse_line(label);
  1666. --syntax.curly_count;
  1667. // Set the position of the function (label)
  1668. l=add_word(func_name);
  1669. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1670. {
  1671. str_data[l].type = C_USERFUNC;
  1672. set_label(l, script_pos, p);
  1673. if( parse_options&SCRIPT_USE_LABEL_DB )
  1674. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1675. }
  1676. else
  1677. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1678. return skip_space(p);
  1679. }
  1680. else
  1681. {
  1682. disp_error_message("expect ';' or '{' at function syntax",p);
  1683. }
  1684. }
  1685. break;
  1686. case 'i':
  1687. case 'I':
  1688. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1689. // If process
  1690. char label[256];
  1691. p=skip_space(p2);
  1692. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1693. disp_error_message("need '('",p);
  1694. }
  1695. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1696. syntax.curly[syntax.curly_count].count = 1;
  1697. syntax.curly[syntax.curly_count].index = syntax.index++;
  1698. syntax.curly[syntax.curly_count].flag = 0;
  1699. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1700. syntax.curly_count++;
  1701. add_scriptl(add_str("jump_zero"));
  1702. add_scriptc(C_ARG);
  1703. p=parse_expr(p);
  1704. p=skip_space(p);
  1705. add_scriptl(add_str(label));
  1706. add_scriptc(C_FUNC);
  1707. return p;
  1708. }
  1709. break;
  1710. case 's':
  1711. case 'S':
  1712. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1713. // Processing of switch ()
  1714. char label[256];
  1715. p=skip_space(p2);
  1716. if(*p != '(') {
  1717. disp_error_message("need '('",p);
  1718. }
  1719. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1720. syntax.curly[syntax.curly_count].count = 1;
  1721. syntax.curly[syntax.curly_count].index = syntax.index++;
  1722. syntax.curly[syntax.curly_count].flag = 0;
  1723. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1724. syntax.curly_count++;
  1725. add_scriptl(add_str("set"));
  1726. add_scriptc(C_ARG);
  1727. add_scriptl(add_str(label));
  1728. p=parse_expr(p);
  1729. p=skip_space(p);
  1730. if(*p != '{') {
  1731. disp_error_message("parse_syntax: expected '{'",p);
  1732. }
  1733. add_scriptc(C_FUNC);
  1734. return p + 1;
  1735. }
  1736. break;
  1737. case 'w':
  1738. case 'W':
  1739. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1740. int l;
  1741. char label[256];
  1742. p=skip_space(p2);
  1743. if(*p != '(') {
  1744. disp_error_message("need '('",p);
  1745. }
  1746. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1747. syntax.curly[syntax.curly_count].count = 1;
  1748. syntax.curly[syntax.curly_count].index = syntax.index++;
  1749. syntax.curly[syntax.curly_count].flag = 0;
  1750. // Form the start of label decision
  1751. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1752. l=add_str(label);
  1753. set_label(l,script_pos,p);
  1754. // Skip to the end point if the condition is false
  1755. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1756. syntax.curly_count++;
  1757. add_scriptl(add_str("jump_zero"));
  1758. add_scriptc(C_ARG);
  1759. p=parse_expr(p);
  1760. p=skip_space(p);
  1761. add_scriptl(add_str(label));
  1762. add_scriptc(C_FUNC);
  1763. return p;
  1764. }
  1765. break;
  1766. }
  1767. return NULL;
  1768. }
  1769. const char* parse_syntax_close(const char *p) {
  1770. // If (...) for (...) hoge (); as to make sure closed closed once again
  1771. int flag;
  1772. do {
  1773. p = parse_syntax_close_sub(p,&flag);
  1774. } while(flag);
  1775. return p;
  1776. }
  1777. // Close judgment if, for, while, of do
  1778. // flag == 1 : closed
  1779. // flag == 0 : not closed
  1780. const char* parse_syntax_close_sub(const char* p,int* flag)
  1781. {
  1782. char label[256];
  1783. int pos = syntax.curly_count - 1;
  1784. int l;
  1785. *flag = 1;
  1786. if(syntax.curly_count <= 0) {
  1787. *flag = 0;
  1788. return p;
  1789. } else if(syntax.curly[pos].type == TYPE_IF) {
  1790. const char *bp = p;
  1791. const char *p2;
  1792. // if-block and else-block end is a new line
  1793. parse_nextline(false, p);
  1794. // Skip to the last location if
  1795. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1796. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1797. parse_line(label);
  1798. syntax.curly_count--;
  1799. // Put the label of the location
  1800. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1801. l=add_str(label);
  1802. set_label(l,script_pos,p);
  1803. syntax.curly[pos].count++;
  1804. p = skip_space(p);
  1805. p2 = skip_word(p);
  1806. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1807. // else or else - if
  1808. p = skip_space(p2);
  1809. p2 = skip_word(p);
  1810. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1811. // else - if
  1812. p=skip_space(p2);
  1813. if(*p != '(') {
  1814. disp_error_message("need '('",p);
  1815. }
  1816. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1817. add_scriptl(add_str("jump_zero"));
  1818. add_scriptc(C_ARG);
  1819. p=parse_expr(p);
  1820. p=skip_space(p);
  1821. add_scriptl(add_str(label));
  1822. add_scriptc(C_FUNC);
  1823. *flag = 0;
  1824. return p;
  1825. } else {
  1826. // else
  1827. if(!syntax.curly[pos].flag) {
  1828. syntax.curly[pos].flag = 1;
  1829. *flag = 0;
  1830. return p;
  1831. }
  1832. }
  1833. }
  1834. // Close if
  1835. syntax.curly_count--;
  1836. // Put the label of the final location
  1837. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1838. l=add_str(label);
  1839. set_label(l,script_pos,p);
  1840. if(syntax.curly[pos].flag == 1) {
  1841. // Because the position of the pointer is the same if not else for this
  1842. return bp;
  1843. }
  1844. return p;
  1845. } else if(syntax.curly[pos].type == TYPE_DO) {
  1846. int l;
  1847. char label[256];
  1848. const char *p2;
  1849. if(syntax.curly[pos].flag) {
  1850. // (Come here continue) to form the label here
  1851. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1852. l=add_str(label);
  1853. set_label(l,script_pos,p);
  1854. }
  1855. // Skip to the end point if the condition is false
  1856. p = skip_space(p);
  1857. p2 = skip_word(p);
  1858. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1859. disp_error_message("parse_syntax: expected 'while'",p);
  1860. p = skip_space(p2);
  1861. if(*p != '(') {
  1862. disp_error_message("need '('",p);
  1863. }
  1864. // do-block end is a new line
  1865. parse_nextline(false, p);
  1866. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1867. add_scriptl(add_str("jump_zero"));
  1868. add_scriptc(C_ARG);
  1869. p=parse_expr(p);
  1870. p=skip_space(p);
  1871. add_scriptl(add_str(label));
  1872. add_scriptc(C_FUNC);
  1873. // Skip to the starting point
  1874. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1875. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1876. parse_line(label);
  1877. syntax.curly_count--;
  1878. // Form label of the end point conditions
  1879. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1880. l=add_str(label);
  1881. set_label(l,script_pos,p);
  1882. p = skip_space(p);
  1883. if(*p != ';') {
  1884. disp_error_message("parse_syntax: expected ';'",p);
  1885. return p+1;
  1886. }
  1887. p++;
  1888. syntax.curly_count--;
  1889. return p;
  1890. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1891. // for-block end is a new line
  1892. parse_nextline(false, p);
  1893. // Skip to the next loop
  1894. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1895. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1896. parse_line(label);
  1897. syntax.curly_count--;
  1898. // End for labeling
  1899. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1900. l=add_str(label);
  1901. set_label(l,script_pos,p);
  1902. syntax.curly_count--;
  1903. return p;
  1904. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1905. // while-block end is a new line
  1906. parse_nextline(false, p);
  1907. // Skip to the decision while
  1908. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1909. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1910. parse_line(label);
  1911. syntax.curly_count--;
  1912. // End while labeling
  1913. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1914. l=add_str(label);
  1915. set_label(l,script_pos,p);
  1916. syntax.curly_count--;
  1917. return p;
  1918. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1919. int pos = syntax.curly_count-1;
  1920. char label[256];
  1921. int l;
  1922. // Back
  1923. sprintf(label,"return;");
  1924. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1925. parse_line(label);
  1926. syntax.curly_count--;
  1927. // Put the label of the location
  1928. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1929. l=add_str(label);
  1930. set_label(l,script_pos,p);
  1931. syntax.curly_count--;
  1932. return p;
  1933. } else {
  1934. *flag = 0;
  1935. return p;
  1936. }
  1937. }
  1938. /*==========================================
  1939. * Added built-in functions
  1940. *------------------------------------------*/
  1941. static void add_buildin_func(void)
  1942. {
  1943. int i,n;
  1944. const char* p;
  1945. for( i = 0; buildin_func[i].func; i++ )
  1946. {
  1947. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1948. // 'v' - value (either string or int or reference)
  1949. // 's' - string
  1950. // 'i' - int
  1951. // 'r' - reference (of a variable)
  1952. // 'l' - label
  1953. // '?' - one optional parameter
  1954. // '*' - unknown number of optional parameters
  1955. p = buildin_func[i].arg;
  1956. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1957. while( *p == '?' ) ++p;
  1958. if( *p == '*' ) ++p;
  1959. if( *p != 0){
  1960. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1961. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1962. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1963. } else {
  1964. n = add_str(buildin_func[i].name);
  1965. str_data[n].type = C_FUNC;
  1966. str_data[n].val = i;
  1967. str_data[n].func = buildin_func[i].func;
  1968. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  1969. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1970. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1971. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1972. }
  1973. }
  1974. }
  1975. /// Retrieves the value of a constant.
  1976. bool script_get_constant(const char* name, int* value)
  1977. {
  1978. int n = search_str(name);
  1979. if( n == -1 || str_data[n].type != C_INT )
  1980. {// not found or not a constant
  1981. return false;
  1982. }
  1983. value[0] = str_data[n].val;
  1984. return true;
  1985. }
  1986. /// Creates new constant or parameter with given value.
  1987. void script_set_constant(const char* name, int value, bool isparameter)
  1988. {
  1989. int n = add_str(name);
  1990. if( str_data[n].type == C_NOP )
  1991. {// new
  1992. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1993. str_data[n].val = value;
  1994. }
  1995. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1996. {// existing parameter or constant
  1997. 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);
  1998. }
  1999. else
  2000. {// existing name
  2001. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2002. }
  2003. }
  2004. /*==========================================
  2005. * Reading constant databases
  2006. * const.txt
  2007. *------------------------------------------*/
  2008. static void read_constdb(void)
  2009. {
  2010. FILE *fp;
  2011. char line[1024],name[1024],val[1024];
  2012. int type;
  2013. sprintf(line, "%s/const.txt", db_path);
  2014. fp=fopen(line, "r");
  2015. if(fp==NULL){
  2016. ShowError("can't read %s\n", line);
  2017. return ;
  2018. }
  2019. while(fgets(line, sizeof(line), fp))
  2020. {
  2021. if(line[0]=='/' && line[1]=='/')
  2022. continue;
  2023. type=0;
  2024. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  2025. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  2026. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2027. }
  2028. }
  2029. fclose(fp);
  2030. }
  2031. /*==========================================
  2032. * Display emplacement line of script
  2033. *------------------------------------------*/
  2034. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2035. {
  2036. int i;
  2037. if( p == NULL || !p[0] ) return NULL;
  2038. if( line < 0 )
  2039. StringBuf_Printf(buf, "*% 5d : ", -line);
  2040. else
  2041. StringBuf_Printf(buf, " % 5d : ", line);
  2042. for(i=0;p[i] && p[i] != '\n';i++){
  2043. if(p + i != mark)
  2044. StringBuf_Printf(buf, "%c", p[i]);
  2045. else
  2046. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2047. }
  2048. StringBuf_AppendStr(buf, "\n");
  2049. return p+i+(p[i] == '\n' ? 1 : 0);
  2050. }
  2051. void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) {
  2052. // Find the line where the error occurred
  2053. int j;
  2054. int line = start_line;
  2055. const char *p;
  2056. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2057. for(p=src;p && *p;line++){
  2058. const char *lineend=strchr(p,'\n');
  2059. if(lineend==NULL || error_pos<lineend){
  2060. break;
  2061. }
  2062. for( j = 0; j < 4; j++ ) {
  2063. linestart[j] = linestart[j+1];
  2064. }
  2065. linestart[4] = p;
  2066. p=lineend+1;
  2067. }
  2068. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2069. StringBuf_Printf(buf, " %s\n", error_msg);
  2070. for(j = 0; j < 5; j++ ) {
  2071. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2072. }
  2073. p = script_print_line(buf, p, error_pos, -line);
  2074. for(j = 0; j < 5; j++) {
  2075. p = script_print_line(buf, p, NULL, line + j + 1 );
  2076. }
  2077. }
  2078. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) {
  2079. StringBuf buf;
  2080. StringBuf_Init(&buf);
  2081. StringBuf_AppendStr(&buf, "\a\n");
  2082. script_errorwarning_sub(&buf, src, file, start_line, error_msg, error_pos);
  2083. ShowError("%s", StringBuf_Value(&buf));
  2084. StringBuf_Destroy(&buf);
  2085. }
  2086. void script_warning(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) {
  2087. StringBuf buf;
  2088. StringBuf_Init(&buf);
  2089. script_errorwarning_sub(&buf, src, file, start_line, error_msg, error_pos);
  2090. ShowWarning("%s", StringBuf_Value(&buf));
  2091. StringBuf_Destroy(&buf);
  2092. }
  2093. /*==========================================
  2094. * Analysis of the script
  2095. *------------------------------------------*/
  2096. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2097. {
  2098. const char *p,*tmpp;
  2099. int i;
  2100. struct script_code* code = NULL;
  2101. static int first=1;
  2102. char end;
  2103. bool unresolved_names = false;
  2104. parser_current_src = src;
  2105. parser_current_file = file;
  2106. parser_current_line = line;
  2107. if( src == NULL )
  2108. return NULL;// empty script
  2109. memset(&syntax,0,sizeof(syntax));
  2110. if(first){
  2111. add_buildin_func();
  2112. read_constdb();
  2113. first=0;
  2114. }
  2115. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2116. script_pos=0;
  2117. script_size=SCRIPT_BLOCK_SIZE;
  2118. parse_nextline(true, NULL);
  2119. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2120. if( options&SCRIPT_USE_LABEL_DB )
  2121. db_clear(scriptlabel_db);
  2122. parse_options = options;
  2123. if( setjmp( error_jump ) != 0 ) {
  2124. //Restore program state when script has problems. [from jA]
  2125. int i;
  2126. const int size = ARRAYLENGTH(syntax.curly);
  2127. if( error_report )
  2128. script_error(src,file,line,error_msg,error_pos);
  2129. aFree( error_msg );
  2130. aFree( script_buf );
  2131. script_pos = 0;
  2132. script_size = 0;
  2133. script_buf = NULL;
  2134. for(i=LABEL_START;i<str_num;i++)
  2135. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  2136. for(i=0; i<size; i++)
  2137. linkdb_final(&syntax.curly[i].case_label);
  2138. return NULL;
  2139. }
  2140. parse_syntax_for_flag=0;
  2141. p=src;
  2142. p=skip_space(p);
  2143. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2144. {// does not require brackets around the script
  2145. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2146. {// empty script and can return NULL
  2147. aFree( script_buf );
  2148. script_pos = 0;
  2149. script_size = 0;
  2150. script_buf = NULL;
  2151. return NULL;
  2152. }
  2153. end = '\0';
  2154. }
  2155. else
  2156. {// requires brackets around the script
  2157. if( *p != '{' )
  2158. disp_error_message("not found '{'",p);
  2159. p = skip_space(p+1);
  2160. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2161. {// empty script and can return NULL
  2162. aFree( script_buf );
  2163. script_pos = 0;
  2164. script_size = 0;
  2165. script_buf = NULL;
  2166. return NULL;
  2167. }
  2168. end = '}';
  2169. }
  2170. // clear references of labels, variables and internal functions
  2171. for(i=LABEL_START;i<str_num;i++){
  2172. if(
  2173. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2174. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2175. ){
  2176. str_data[i].type=C_NOP;
  2177. str_data[i].backpatch=-1;
  2178. str_data[i].label=-1;
  2179. }
  2180. }
  2181. while( syntax.curly_count != 0 || *p != end )
  2182. {
  2183. if( *p == '\0' )
  2184. disp_error_message("unexpected end of script",p);
  2185. // Special handling only label
  2186. tmpp=skip_space(skip_word(p));
  2187. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2188. i=add_word(p);
  2189. set_label(i,script_pos,p);
  2190. if( parse_options&SCRIPT_USE_LABEL_DB )
  2191. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2192. p=tmpp+1;
  2193. p=skip_space(p);
  2194. continue;
  2195. }
  2196. // All other lumped
  2197. p=parse_line(p);
  2198. p=skip_space(p);
  2199. parse_nextline(false, p);
  2200. }
  2201. add_scriptc(C_NOP);
  2202. // trim code to size
  2203. script_size = script_pos;
  2204. RECREATE(script_buf,unsigned char,script_pos);
  2205. // default unknown references to variables
  2206. for(i=LABEL_START;i<str_num;i++){
  2207. if(str_data[i].type==C_NOP){
  2208. int j,next;
  2209. str_data[i].type=C_NAME;
  2210. str_data[i].label=i;
  2211. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2212. next=GETVALUE(script_buf,j);
  2213. SETVALUE(script_buf,j,i);
  2214. j=next;
  2215. }
  2216. }
  2217. else if( str_data[i].type == C_USERFUNC )
  2218. {// 'function name;' without follow-up code
  2219. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2220. unresolved_names = true;
  2221. }
  2222. }
  2223. if( unresolved_names )
  2224. {
  2225. disp_error_message("parse_script: unresolved function references", p);
  2226. }
  2227. #ifdef DEBUG_DISP
  2228. for(i=0;i<script_pos;i++){
  2229. if((i&15)==0) ShowMessage("%04x : ",i);
  2230. ShowMessage("%02x ",script_buf[i]);
  2231. if((i&15)==15) ShowMessage("\n");
  2232. }
  2233. ShowMessage("\n");
  2234. #endif
  2235. #ifdef DEBUG_DISASM
  2236. {
  2237. int i = 0,j;
  2238. while(i < script_pos) {
  2239. c_op op = get_com(script_buf,&i);
  2240. ShowMessage("%06x %s", i, script_op2name(op));
  2241. j = i;
  2242. switch(op) {
  2243. case C_INT:
  2244. ShowMessage(" %d", get_num(script_buf,&i));
  2245. break;
  2246. case C_POS:
  2247. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2248. i += 3;
  2249. break;
  2250. case C_NAME:
  2251. j = (*(int*)(script_buf+i)&0xffffff);
  2252. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2253. i += 3;
  2254. break;
  2255. case C_STR:
  2256. j = strlen(script_buf + i);
  2257. ShowMessage(" %s", script_buf + i);
  2258. i += j+1;
  2259. break;
  2260. }
  2261. ShowMessage(CL_CLL"\n");
  2262. }
  2263. }
  2264. #endif
  2265. CREATE(code,struct script_code,1);
  2266. code->script_buf = script_buf;
  2267. code->script_size = script_size;
  2268. code->script_vars = idb_alloc(DB_OPT_RELEASE_DATA);
  2269. return code;
  2270. }
  2271. /// Returns the player attached to this script, identified by the rid.
  2272. /// If there is no player attached, the script is terminated.
  2273. TBL_PC *script_rid2sd(struct script_state *st)
  2274. {
  2275. TBL_PC *sd=map_id2sd(st->rid);
  2276. if(!sd){
  2277. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2278. script_reportfunc(st);
  2279. script_reportsrc(st);
  2280. st->state = END;
  2281. }
  2282. return sd;
  2283. }
  2284. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2285. ///
  2286. /// @param st Script state
  2287. /// @param data Variable/constant
  2288. void get_val(struct script_state* st, struct script_data* data)
  2289. {
  2290. const char* name;
  2291. char prefix;
  2292. char postfix;
  2293. TBL_PC* sd = NULL;
  2294. if( !data_isreference(data) )
  2295. return;// not a variable/constant
  2296. name = reference_getname(data);
  2297. prefix = name[0];
  2298. postfix = name[strlen(name) - 1];
  2299. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2300. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2301. {
  2302. sd = script_rid2sd(st);
  2303. if( sd == NULL )
  2304. {// needs player attached
  2305. if( postfix == '$' )
  2306. {// string variable
  2307. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2308. data->type = C_CONSTSTR;
  2309. data->u.str = "";
  2310. }
  2311. else
  2312. {// integer variable
  2313. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2314. data->type = C_INT;
  2315. data->u.num = 0;
  2316. }
  2317. return;
  2318. }
  2319. }
  2320. if( postfix == '$' )
  2321. {// string variable
  2322. switch( prefix )
  2323. {
  2324. case '@':
  2325. data->u.str = pc_readregstr(sd, data->u.num);
  2326. break;
  2327. case '$':
  2328. data->u.str = mapreg_readregstr(data->u.num);
  2329. break;
  2330. case '#':
  2331. if( name[1] == '#' )
  2332. data->u.str = pc_readaccountreg2str(sd, name);// global
  2333. else
  2334. data->u.str = pc_readaccountregstr(sd, name);// local
  2335. break;
  2336. case '.':
  2337. {
  2338. struct DBMap* n =
  2339. data->ref ? *data->ref:
  2340. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2341. st->script->script_vars;// npc variable
  2342. if( n )
  2343. data->u.str = (char*)idb_get(n,reference_getuid(data));
  2344. else
  2345. data->u.str = NULL;
  2346. }
  2347. break;
  2348. case '\'':
  2349. {
  2350. int instance_id = script_instancegetid(st);
  2351. if( instance_id )
  2352. data->u.str = (char*)idb_get(instance_data[instance_id].vars,reference_getuid(data));
  2353. else {
  2354. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2355. data->u.str = NULL;
  2356. }
  2357. }
  2358. break;
  2359. default:
  2360. data->u.str = pc_readglobalreg_str(sd, name);
  2361. break;
  2362. }
  2363. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2364. {// empty string
  2365. data->type = C_CONSTSTR;
  2366. data->u.str = "";
  2367. }
  2368. else
  2369. {// duplicate string
  2370. data->type = C_STR;
  2371. data->u.str = aStrdup(data->u.str);
  2372. }
  2373. }
  2374. else
  2375. {// integer variable
  2376. data->type = C_INT;
  2377. if( reference_toconstant(data) )
  2378. {
  2379. data->u.num = reference_getconstant(data);
  2380. }
  2381. else if( reference_toparam(data) )
  2382. {
  2383. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2384. }
  2385. else
  2386. switch( prefix )
  2387. {
  2388. case '@':
  2389. data->u.num = pc_readreg(sd, data->u.num);
  2390. break;
  2391. case '$':
  2392. data->u.num = mapreg_readreg(data->u.num);
  2393. break;
  2394. case '#':
  2395. if( name[1] == '#' )
  2396. data->u.num = pc_readaccountreg2(sd, name);// global
  2397. else
  2398. data->u.num = pc_readaccountreg(sd, name);// local
  2399. break;
  2400. case '.':
  2401. {
  2402. struct DBMap* n =
  2403. data->ref ? *data->ref:
  2404. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2405. st->script->script_vars;// npc variable
  2406. if( n )
  2407. data->u.num = (int)idb_iget(n,reference_getuid(data));
  2408. else
  2409. data->u.num = 0;
  2410. }
  2411. break;
  2412. case '\'':
  2413. {
  2414. int instance_id = script_instancegetid(st);
  2415. if( instance_id )
  2416. data->u.num = (int)idb_iget(instance_data[instance_id].vars,reference_getuid(data));
  2417. else {
  2418. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2419. data->u.num = 0;
  2420. }
  2421. }
  2422. break;
  2423. default:
  2424. data->u.num = pc_readglobalreg(sd, name);
  2425. break;
  2426. }
  2427. }
  2428. return;
  2429. }
  2430. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
  2431. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2432. /// The value is left in the top of the stack and needs to be removed manually.
  2433. void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
  2434. {
  2435. struct script_data* data;
  2436. push_val2(st->stack, C_NAME, uid, ref);
  2437. data = script_getdatatop(st, -1);
  2438. get_val(st, data);
  2439. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2440. }
  2441. /*==========================================
  2442. * Stores the value of a script variable
  2443. * Return value is 0 on fail, 1 on success.
  2444. *------------------------------------------*/
  2445. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
  2446. {
  2447. char prefix = name[0];
  2448. if( is_string_variable(name) ) {// string variable
  2449. const char* str = (const char*)value;
  2450. switch (prefix) {
  2451. case '@':
  2452. return pc_setregstr(sd, num, str);
  2453. case '$':
  2454. return mapreg_setregstr(num, str);
  2455. case '#':
  2456. return (name[1] == '#') ?
  2457. pc_setaccountreg2str(sd, name, str) :
  2458. pc_setaccountregstr(sd, name, str);
  2459. case '.':
  2460. {
  2461. struct DBMap* n;
  2462. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2463. if( n ) {
  2464. idb_remove(n, num);
  2465. if (str[0]) idb_put(n, num, aStrdup(str));
  2466. }
  2467. }
  2468. return 1;
  2469. case '\'':
  2470. {
  2471. int instance_id = script_instancegetid(st);
  2472. if( instance_id ) {
  2473. idb_remove(instance_data[instance_id].vars, num);
  2474. if( str[0] ) idb_put(instance_data[instance_id].vars, num, aStrdup(str));
  2475. }
  2476. return 1;
  2477. }
  2478. default:
  2479. return pc_setglobalreg_str(sd, name, str);
  2480. }
  2481. } else {// integer variable
  2482. int val = (int)__64BPRTSIZE(value);
  2483. if(str_data[num&0x00ffffff].type == C_PARAM)
  2484. {
  2485. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2486. {
  2487. if( st != NULL )
  2488. {
  2489. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2490. script_reportsrc(st);
  2491. st->state = END;
  2492. }
  2493. return 0;
  2494. }
  2495. return 1;
  2496. }
  2497. switch (prefix) {
  2498. case '@':
  2499. return pc_setreg(sd, num, val);
  2500. case '$':
  2501. return mapreg_setreg(num, val);
  2502. case '#':
  2503. return (name[1] == '#') ?
  2504. pc_setaccountreg2(sd, name, val) :
  2505. pc_setaccountreg(sd, name, val);
  2506. case '.':
  2507. {
  2508. struct DBMap* n;
  2509. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2510. if( n ) {
  2511. idb_remove(n, num);
  2512. if( val != 0 )
  2513. idb_iput(n, num, val);
  2514. }
  2515. }
  2516. return 1;
  2517. case '\'':
  2518. {
  2519. int instance_id = script_instancegetid(st);
  2520. if( instance_id ) {
  2521. idb_remove(instance_data[instance_id].vars, num);
  2522. if( val != 0 )
  2523. idb_iput(instance_data[instance_id].vars, num, val);
  2524. }
  2525. return 1;
  2526. }
  2527. default:
  2528. return pc_setglobalreg(sd, name, val);
  2529. }
  2530. }
  2531. }
  2532. int set_var(TBL_PC* sd, char* name, void* val)
  2533. {
  2534. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2535. }
  2536. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
  2537. {
  2538. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2539. }
  2540. /// Converts the data to a string
  2541. const char* conv_str(struct script_state* st, struct script_data* data)
  2542. {
  2543. char* p;
  2544. get_val(st, data);
  2545. if( data_isstring(data) )
  2546. {// nothing to convert
  2547. }
  2548. else if( data_isint(data) )
  2549. {// int -> string
  2550. CREATE(p, char, ITEM_NAME_LENGTH);
  2551. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2552. p[ITEM_NAME_LENGTH-1] = '\0';
  2553. data->type = C_STR;
  2554. data->u.str = p;
  2555. }
  2556. else if( data_isreference(data) )
  2557. {// reference -> string
  2558. //##TODO when does this happen (check get_val) [FlavioJS]
  2559. data->type = C_CONSTSTR;
  2560. data->u.str = reference_getname(data);
  2561. }
  2562. else
  2563. {// unsupported data type
  2564. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2565. script_reportdata(data);
  2566. script_reportsrc(st);
  2567. data->type = C_CONSTSTR;
  2568. data->u.str = "";
  2569. }
  2570. return data->u.str;
  2571. }
  2572. /// Converts the data to an int
  2573. int conv_num(struct script_state* st, struct script_data* data)
  2574. {
  2575. char* p;
  2576. long num;
  2577. get_val(st, data);
  2578. if( data_isint(data) )
  2579. {// nothing to convert
  2580. }
  2581. else if( data_isstring(data) )
  2582. {// string -> int
  2583. // the result does not overflow or underflow, it is capped instead
  2584. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2585. p = data->u.str;
  2586. errno = 0;
  2587. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2588. if( errno == ERANGE
  2589. #if LONG_MAX > INT_MAX
  2590. || num < INT_MIN || num > INT_MAX
  2591. #endif
  2592. )
  2593. {
  2594. if( num <= INT_MIN )
  2595. {
  2596. num = INT_MIN;
  2597. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2598. }
  2599. else//if( num >= INT_MAX )
  2600. {
  2601. num = INT_MAX;
  2602. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2603. }
  2604. script_reportdata(data);
  2605. script_reportsrc(st);
  2606. }
  2607. if( data->type == C_STR )
  2608. aFree(p);
  2609. data->type = C_INT;
  2610. data->u.num = (int)num;
  2611. }
  2612. #if 0
  2613. // FIXME this function is being used to retrieve the position of labels and
  2614. // probably other stuff [FlavioJS]
  2615. else
  2616. {// unsupported data type
  2617. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2618. script_reportdata(data);
  2619. script_reportsrc(st);
  2620. data->type = C_INT;
  2621. data->u.num = 0;
  2622. }
  2623. #endif
  2624. return data->u.num;
  2625. }
  2626. //
  2627. // Stack operations
  2628. //
  2629. /// Increases the size of the stack
  2630. void stack_expand(struct script_stack* stack)
  2631. {
  2632. stack->sp_max += 64;
  2633. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2634. stack->sp_max * sizeof(stack->stack_data[0]) );
  2635. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2636. 64 * sizeof(stack->stack_data[0]) );
  2637. }
  2638. /// Pushes a value into the stack
  2639. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2640. /// Pushes a value into the stack (with reference)
  2641. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
  2642. {
  2643. if( stack->sp >= stack->sp_max )
  2644. stack_expand(stack);
  2645. stack->stack_data[stack->sp].type = type;
  2646. stack->stack_data[stack->sp].u.num = val;
  2647. stack->stack_data[stack->sp].ref = ref;
  2648. stack->sp++;
  2649. return &stack->stack_data[stack->sp-1];
  2650. }
  2651. /// Pushes a string into the stack
  2652. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2653. {
  2654. if( stack->sp >= stack->sp_max )
  2655. stack_expand(stack);
  2656. stack->stack_data[stack->sp].type = type;
  2657. stack->stack_data[stack->sp].u.str = str;
  2658. stack->stack_data[stack->sp].ref = NULL;
  2659. stack->sp++;
  2660. return &stack->stack_data[stack->sp-1];
  2661. }
  2662. /// Pushes a retinfo into the stack
  2663. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, DBMap **ref)
  2664. {
  2665. if( stack->sp >= stack->sp_max )
  2666. stack_expand(stack);
  2667. stack->stack_data[stack->sp].type = C_RETINFO;
  2668. stack->stack_data[stack->sp].u.ri = ri;
  2669. stack->stack_data[stack->sp].ref = ref;
  2670. stack->sp++;
  2671. return &stack->stack_data[stack->sp-1];
  2672. }
  2673. /// Pushes a copy of the target position into the stack
  2674. struct script_data* push_copy(struct script_stack* stack, int pos)
  2675. {
  2676. switch( stack->stack_data[pos].type )
  2677. {
  2678. case C_CONSTSTR:
  2679. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2680. break;
  2681. case C_STR:
  2682. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2683. break;
  2684. case C_RETINFO:
  2685. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2686. exit(1);
  2687. break;
  2688. default:
  2689. return push_val2(
  2690. stack,stack->stack_data[pos].type,
  2691. stack->stack_data[pos].u.num,
  2692. stack->stack_data[pos].ref
  2693. );
  2694. break;
  2695. }
  2696. }
  2697. /// Removes the values in indexes [start,end[ from the stack.
  2698. /// Adjusts all stack pointers.
  2699. void pop_stack(struct script_state* st, int start, int end)
  2700. {
  2701. struct script_stack* stack = st->stack;
  2702. struct script_data* data;
  2703. int i;
  2704. if( start < 0 )
  2705. start = 0;
  2706. if( end > stack->sp )
  2707. end = stack->sp;
  2708. if( start >= end )
  2709. return;// nothing to pop
  2710. // free stack elements
  2711. for( i = start; i < end; i++ )
  2712. {
  2713. data = &stack->stack_data[i];
  2714. if( data->type == C_STR )
  2715. aFree(data->u.str);
  2716. if( data->type == C_RETINFO )
  2717. {
  2718. struct script_retinfo* ri = data->u.ri;
  2719. if( ri->var_function )
  2720. script_free_vars(ri->var_function);
  2721. if( data->ref )
  2722. aFree(data->ref);
  2723. aFree(ri);
  2724. }
  2725. data->type = C_NOP;
  2726. }
  2727. // move the rest of the elements
  2728. if( stack->sp > end )
  2729. {
  2730. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2731. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2732. stack->stack_data[i].type = C_NOP;
  2733. }
  2734. // adjust stack pointers
  2735. if( st->start > end ) st->start -= end - start;
  2736. else if( st->start > start ) st->start = start;
  2737. if( st->end > end ) st->end -= end - start;
  2738. else if( st->end > start ) st->end = start;
  2739. if( stack->defsp > end ) stack->defsp -= end - start;
  2740. else if( stack->defsp > start ) stack->defsp = start;
  2741. stack->sp -= end - start;
  2742. }
  2743. ///
  2744. ///
  2745. ///
  2746. /*==========================================
  2747. * Release script dependent variable, dependent variable of function
  2748. *------------------------------------------*/
  2749. void script_free_vars(struct DBMap* storage)
  2750. {
  2751. if( storage )
  2752. {// destroy the storage construct containing the variables
  2753. db_destroy(storage);
  2754. }
  2755. }
  2756. void script_free_code(struct script_code* code)
  2757. {
  2758. script_free_vars( code->script_vars );
  2759. aFree( code->script_buf );
  2760. aFree( code );
  2761. }
  2762. /// Creates a new script state.
  2763. ///
  2764. /// @param script Script code
  2765. /// @param pos Position in the code
  2766. /// @param rid Who is running the script (attached player)
  2767. /// @param oid Where the code is being run (npc 'object')
  2768. /// @return Script state
  2769. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2770. {
  2771. struct script_state* st;
  2772. CREATE(st, struct script_state, 1);
  2773. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2774. st->stack->sp = 0;
  2775. st->stack->sp_max = 64;
  2776. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2777. st->stack->defsp = st->stack->sp;
  2778. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  2779. st->state = RUN;
  2780. st->script = script;
  2781. //st->scriptroot = script;
  2782. st->pos = pos;
  2783. st->rid = rid;
  2784. st->oid = oid;
  2785. st->sleep.timer = INVALID_TIMER;
  2786. st->npc_item_flag = battle_config.item_enabled_npc;
  2787. return st;
  2788. }
  2789. /// Frees a script state.
  2790. ///
  2791. /// @param st Script state
  2792. void script_free_state(struct script_state* st)
  2793. {
  2794. if(st->bk_st)
  2795. {// backup was not restored
  2796. 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);
  2797. }
  2798. if( st->sleep.timer != INVALID_TIMER )
  2799. delete_timer(st->sleep.timer, run_script_timer);
  2800. script_free_vars(st->stack->var_function);
  2801. pop_stack(st, 0, st->stack->sp);
  2802. aFree(st->stack->stack_data);
  2803. aFree(st->stack);
  2804. st->stack = NULL;
  2805. st->pos = -1;
  2806. aFree(st);
  2807. }
  2808. //
  2809. // Main execution unit
  2810. //
  2811. /*==========================================
  2812. * Read command
  2813. *------------------------------------------*/
  2814. c_op get_com(unsigned char *script,int *pos)
  2815. {
  2816. int i = 0, j = 0;
  2817. if(script[*pos]>=0x80){
  2818. return C_INT;
  2819. }
  2820. while(script[*pos]>=0x40){
  2821. i=script[(*pos)++]<<j;
  2822. j+=6;
  2823. }
  2824. return (c_op)(i+(script[(*pos)++]<<j));
  2825. }
  2826. /*==========================================
  2827. * Income figures
  2828. *------------------------------------------*/
  2829. int get_num(unsigned char *script,int *pos)
  2830. {
  2831. int i,j;
  2832. i=0; j=0;
  2833. while(script[*pos]>=0xc0){
  2834. i+=(script[(*pos)++]&0x7f)<<j;
  2835. j+=6;
  2836. }
  2837. return i+((script[(*pos)++]&0x7f)<<j);
  2838. }
  2839. /*==========================================
  2840. * Remove the value from the stack
  2841. *------------------------------------------*/
  2842. int pop_val(struct script_state* st)
  2843. {
  2844. if(st->stack->sp<=0)
  2845. return 0;
  2846. st->stack->sp--;
  2847. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2848. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2849. return st->stack->stack_data[st->stack->sp].u.num;
  2850. return 0;
  2851. }
  2852. /// Ternary operators
  2853. /// test ? if_true : if_false
  2854. void op_3(struct script_state* st, int op)
  2855. {
  2856. struct script_data* data;
  2857. int flag = 0;
  2858. data = script_getdatatop(st, -3);
  2859. get_val(st, data);
  2860. if( data_isstring(data) )
  2861. flag = data->u.str[0];// "" -> false
  2862. else if( data_isint(data) )
  2863. flag = data->u.num;// 0 -> false
  2864. else
  2865. {
  2866. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2867. script_reportdata(data);
  2868. script_reportsrc(st);
  2869. script_removetop(st, -3, 0);
  2870. script_pushnil(st);
  2871. return;
  2872. }
  2873. if( flag )
  2874. script_pushcopytop(st, -2);
  2875. else
  2876. script_pushcopytop(st, -1);
  2877. script_removetop(st, -4, -1);
  2878. }
  2879. /// Binary string operators
  2880. /// s1 EQ s2 -> i
  2881. /// s1 NE s2 -> i
  2882. /// s1 GT s2 -> i
  2883. /// s1 GE s2 -> i
  2884. /// s1 LT s2 -> i
  2885. /// s1 LE s2 -> i
  2886. /// s1 ADD s2 -> s
  2887. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2888. {
  2889. int a = 0;
  2890. switch(op){
  2891. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2892. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2893. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2894. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2895. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2896. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2897. case C_ADD:
  2898. {
  2899. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2900. strcpy(buf, s1);
  2901. strcat(buf, s2);
  2902. script_pushstr(st, buf);
  2903. return;
  2904. }
  2905. default:
  2906. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2907. script_reportsrc(st);
  2908. script_pushnil(st);
  2909. st->state = END;
  2910. return;
  2911. }
  2912. script_pushint(st,a);
  2913. }
  2914. /// Binary number operators
  2915. /// i OP i -> i
  2916. void op_2num(struct script_state* st, int op, int i1, int i2)
  2917. {
  2918. int ret;
  2919. double ret_double;
  2920. switch( op )
  2921. {
  2922. case C_AND: ret = i1 & i2; break;
  2923. case C_OR: ret = i1 | i2; break;
  2924. case C_XOR: ret = i1 ^ i2; break;
  2925. case C_LAND: ret = (i1 && i2); break;
  2926. case C_LOR: ret = (i1 || i2); break;
  2927. case C_EQ: ret = (i1 == i2); break;
  2928. case C_NE: ret = (i1 != i2); break;
  2929. case C_GT: ret = (i1 > i2); break;
  2930. case C_GE: ret = (i1 >= i2); break;
  2931. case C_LT: ret = (i1 < i2); break;
  2932. case C_LE: ret = (i1 <= i2); break;
  2933. case C_R_SHIFT: ret = i1>>i2; break;
  2934. case C_L_SHIFT: ret = i1<<i2; break;
  2935. case C_DIV:
  2936. case C_MOD:
  2937. if( i2 == 0 )
  2938. {
  2939. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2940. script_reportsrc(st);
  2941. script_pushnil(st);
  2942. st->state = END;
  2943. return;
  2944. }
  2945. else if( op == C_DIV )
  2946. ret = i1 / i2;
  2947. else//if( op == C_MOD )
  2948. ret = i1 % i2;
  2949. break;
  2950. default:
  2951. switch( op )
  2952. {// operators that can overflow/underflow
  2953. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2954. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2955. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2956. default:
  2957. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2958. script_reportsrc(st);
  2959. script_pushnil(st);
  2960. return;
  2961. }
  2962. if( ret_double < (double)INT_MIN )
  2963. {
  2964. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2965. script_reportsrc(st);
  2966. ret = INT_MIN;
  2967. }
  2968. else if( ret_double > (double)INT_MAX )
  2969. {
  2970. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2971. script_reportsrc(st);
  2972. ret = INT_MAX;
  2973. }
  2974. }
  2975. script_pushint(st, ret);
  2976. }
  2977. /// Binary operators
  2978. void op_2(struct script_state *st, int op)
  2979. {
  2980. struct script_data* left, leftref;
  2981. struct script_data* right;
  2982. leftref.type = C_NOP;
  2983. left = script_getdatatop(st, -2);
  2984. right = script_getdatatop(st, -1);
  2985. if (st->op2ref) {
  2986. if (data_isreference(left)) {
  2987. leftref = *left;
  2988. }
  2989. st->op2ref = 0;
  2990. }
  2991. get_val(st, left);
  2992. get_val(st, right);
  2993. // automatic conversions
  2994. switch( op )
  2995. {
  2996. case C_ADD:
  2997. if( data_isint(left) && data_isstring(right) )
  2998. {// convert int-string to string-string
  2999. conv_str(st, left);
  3000. }
  3001. else if( data_isstring(left) && data_isint(right) )
  3002. {// convert string-int to string-string
  3003. conv_str(st, right);
  3004. }
  3005. break;
  3006. }
  3007. if( data_isstring(left) && data_isstring(right) )
  3008. {// ss => op_2str
  3009. op_2str(st, op, left->u.str, right->u.str);
  3010. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3011. if (leftref.type != C_NOP)
  3012. {
  3013. if (left->type == C_STR) // don't free C_CONSTSTR
  3014. aFree(left->u.str);
  3015. *left = leftref;
  3016. }
  3017. }
  3018. else if( data_isint(left) && data_isint(right) )
  3019. {// ii => op_2num
  3020. int i1 = left->u.num;
  3021. int i2 = right->u.num;
  3022. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3023. op_2num(st, op, i1, i2);
  3024. if (leftref.type != C_NOP)
  3025. *left = leftref;
  3026. }
  3027. else
  3028. {// invalid argument
  3029. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3030. script_reportdata(left);
  3031. script_reportdata(right);
  3032. script_reportsrc(st);
  3033. script_removetop(st, -2, 0);
  3034. script_pushnil(st);
  3035. st->state = END;
  3036. }
  3037. }
  3038. /// Unary operators
  3039. /// NEG i -> i
  3040. /// NOT i -> i
  3041. /// LNOT i -> i
  3042. void op_1(struct script_state* st, int op)
  3043. {
  3044. struct script_data* data;
  3045. int i1;
  3046. data = script_getdatatop(st, -1);
  3047. get_val(st, data);
  3048. if( !data_isint(data) )
  3049. {// not a number
  3050. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3051. script_reportdata(data);
  3052. script_reportsrc(st);
  3053. script_pushnil(st);
  3054. st->state = END;
  3055. return;
  3056. }
  3057. i1 = data->u.num;
  3058. script_removetop(st, -1, 0);
  3059. switch( op )
  3060. {
  3061. case C_NEG: i1 = -i1; break;
  3062. case C_NOT: i1 = ~i1; break;
  3063. case C_LNOT: i1 = !i1; break;
  3064. default:
  3065. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3066. script_reportsrc(st);
  3067. script_pushnil(st);
  3068. st->state = END;
  3069. return;
  3070. }
  3071. script_pushint(st, i1);
  3072. }
  3073. /// Checks the type of all arguments passed to a built-in function.
  3074. ///
  3075. /// @param st Script state whose stack arguments should be inspected.
  3076. /// @param func Built-in function for which the arguments are intended.
  3077. static void script_check_buildin_argtype(struct script_state* st, int func)
  3078. {
  3079. char type;
  3080. int idx, invalid = 0;
  3081. script_function* sf = &buildin_func[str_data[func].val];
  3082. for( idx = 2; script_hasdata(st, idx); idx++ )
  3083. {
  3084. struct script_data* data = script_getdata(st, idx);
  3085. type = sf->arg[idx-2];
  3086. if( type == '?' || type == '*' )
  3087. {// optional argument or unknown number of optional parameters ( no types are after this )
  3088. break;
  3089. }
  3090. else if( type == 0 )
  3091. {// more arguments than necessary ( should not happen, as it is checked before )
  3092. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3093. invalid++;
  3094. break;
  3095. }
  3096. else
  3097. {
  3098. const char* name = NULL;
  3099. if( data_isreference(data) )
  3100. {// get name for variables to determine the type they refer to
  3101. name = reference_getname(data);
  3102. }
  3103. switch( type )
  3104. {
  3105. case 'v':
  3106. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3107. {// variant
  3108. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3109. script_reportdata(data);
  3110. invalid++;
  3111. }
  3112. break;
  3113. case 's':
  3114. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3115. {// string
  3116. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3117. script_reportdata(data);
  3118. invalid++;
  3119. }
  3120. break;
  3121. case 'i':
  3122. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3123. {// int ( params and constants are always int )
  3124. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3125. script_reportdata(data);
  3126. invalid++;
  3127. }
  3128. break;
  3129. case 'r':
  3130. if( !data_isreference(data) )
  3131. {// variables
  3132. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3133. script_reportdata(data);
  3134. invalid++;
  3135. }
  3136. break;
  3137. case 'l':
  3138. if( !data_islabel(data) && !data_isfunclabel(data) )
  3139. {// label
  3140. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3141. script_reportdata(data);
  3142. invalid++;
  3143. }
  3144. break;
  3145. }
  3146. }
  3147. }
  3148. if(invalid)
  3149. {
  3150. ShowDebug("Function: %s\n", get_str(func));
  3151. script_reportsrc(st);
  3152. }
  3153. }
  3154. /// Executes a buildin command.
  3155. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3156. int run_func(struct script_state *st)
  3157. {
  3158. struct script_data* data;
  3159. int i,start_sp,end_sp,func;
  3160. end_sp = st->stack->sp;// position after the last argument
  3161. for( i = end_sp-1; i > 0 ; --i )
  3162. if( st->stack->stack_data[i].type == C_ARG )
  3163. break;
  3164. if( i == 0 ) {
  3165. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3166. st->state = END;
  3167. script_reportsrc(st);
  3168. return 1;
  3169. }
  3170. start_sp = i-1;// C_NAME of the command
  3171. st->start = start_sp;
  3172. st->end = end_sp;
  3173. data = &st->stack->stack_data[st->start];
  3174. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3175. func = data->u.num;
  3176. st->funcname = reference_getname(data);
  3177. } else {
  3178. ShowError("script:run_func: not a buildin command.\n");
  3179. script_reportdata(data);
  3180. script_reportsrc(st);
  3181. st->state = END;
  3182. return 1;
  3183. }
  3184. if( script_config.warn_func_mismatch_argtypes ) {
  3185. script_check_buildin_argtype(st, func);
  3186. }
  3187. if(str_data[func].func) {
  3188. if (str_data[func].func(st)) //Report error
  3189. script_reportsrc(st);
  3190. } else {
  3191. 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));
  3192. script_reportsrc(st);
  3193. st->state = END;
  3194. }
  3195. // Stack's datum are used when re-running functions [Eoe]
  3196. if( st->state == RERUNLINE )
  3197. return 0;
  3198. pop_stack(st, st->start, st->end);
  3199. if( st->state == RETFUNC ) {// return from a user-defined function
  3200. struct script_retinfo* ri;
  3201. int olddefsp = st->stack->defsp;
  3202. int nargs;
  3203. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3204. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3205. {
  3206. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3207. script_reportsrc(st);
  3208. st->state = END;
  3209. return 1;
  3210. }
  3211. script_free_vars( st->stack->var_function );
  3212. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3213. nargs = ri->nargs;
  3214. st->pos = ri->pos;
  3215. st->script = ri->script;
  3216. st->stack->var_function = ri->var_function;
  3217. st->stack->defsp = ri->defsp;
  3218. memset(ri, 0, sizeof(struct script_retinfo));
  3219. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3220. st->state = GOTO;
  3221. }
  3222. return 0;
  3223. }
  3224. /*==========================================
  3225. * script execution
  3226. *------------------------------------------*/
  3227. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3228. {
  3229. struct script_state *st;
  3230. if( rootscript == NULL || pos < 0 )
  3231. return;
  3232. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3233. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3234. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3235. st = script_alloc_state(rootscript, pos, rid, oid);
  3236. run_script_main(st);
  3237. }
  3238. void script_stop_sleeptimers(int id)
  3239. {
  3240. struct script_state* st;
  3241. for(;;)
  3242. {
  3243. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3244. if( st == NULL )
  3245. break; // no more sleep timers
  3246. script_free_state(st);
  3247. }
  3248. }
  3249. /*==========================================
  3250. * Delete the specified node from sleep_db
  3251. *------------------------------------------*/
  3252. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3253. {
  3254. struct linkdb_node *retnode;
  3255. if( n == NULL)
  3256. return NULL;
  3257. if( n->prev == NULL )
  3258. sleep_db = n->next;
  3259. else
  3260. n->prev->next = n->next;
  3261. if( n->next )
  3262. n->next->prev = n->prev;
  3263. retnode = n->next;
  3264. aFree( n );
  3265. return retnode; // The following; return retnode
  3266. }
  3267. /*==========================================
  3268. * Timer function for sleep
  3269. *------------------------------------------*/
  3270. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3271. {
  3272. struct script_state *st = (struct script_state *)data;
  3273. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3274. TBL_PC *sd = map_id2sd(st->rid);
  3275. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3276. { //Character mismatch. Cancel execution.
  3277. st->rid = 0;
  3278. st->state = END;
  3279. }
  3280. while( node && st->sleep.timer != INVALID_TIMER ) {
  3281. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3282. script_erase_sleepdb(node);
  3283. st->sleep.timer = INVALID_TIMER;
  3284. break;
  3285. }
  3286. node = node->next;
  3287. }
  3288. if(st->state != RERUNLINE)
  3289. st->sleep.tick = 0;
  3290. run_script_main(st);
  3291. return 0;
  3292. }
  3293. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3294. ///
  3295. /// @param st Script state to detach.
  3296. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3297. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3298. {
  3299. struct map_session_data* sd;
  3300. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3301. sd->st = st->bk_st;
  3302. sd->npc_id = st->bk_npcid;
  3303. if(st->bk_st) {
  3304. //Remove tag for removal.
  3305. st->bk_st = NULL;
  3306. st->bk_npcid = 0;
  3307. } else if(dequeue_event) {
  3308. #ifdef SECURE_NPCTIMEOUT
  3309. /**
  3310. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3311. **/
  3312. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3313. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3314. sd->npc_idle_timer = INVALID_TIMER;
  3315. }
  3316. #endif
  3317. npc_event_dequeue(sd);
  3318. }
  3319. }
  3320. else if(st->bk_st)
  3321. {// rid was set to 0, before detaching the script state
  3322. 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);
  3323. script_reportsrc(st->bk_st);
  3324. script_free_state(st->bk_st);
  3325. st->bk_st = NULL;
  3326. }
  3327. }
  3328. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3329. ///
  3330. /// @param st Script state to attach.
  3331. static void script_attach_state(struct script_state* st)
  3332. {
  3333. struct map_session_data* sd;
  3334. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3335. {
  3336. if(st!=sd->st)
  3337. {
  3338. if(st->bk_st)
  3339. {// there is already a backup
  3340. 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);
  3341. }
  3342. st->bk_st = sd->st;
  3343. st->bk_npcid = sd->npc_id;
  3344. }
  3345. sd->st = st;
  3346. sd->npc_id = st->oid;
  3347. sd->npc_item_flag = st->npc_item_flag; // load default.
  3348. #ifdef SECURE_NPCTIMEOUT
  3349. if( sd->npc_idle_timer == INVALID_TIMER )
  3350. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3351. sd->npc_idle_tick = gettick();
  3352. #endif
  3353. }
  3354. }
  3355. /*==========================================
  3356. * The main part of the script execution
  3357. *------------------------------------------*/
  3358. void run_script_main(struct script_state *st)
  3359. {
  3360. int cmdcount = script_config.check_cmdcount;
  3361. int gotocount = script_config.check_gotocount;
  3362. TBL_PC *sd;
  3363. struct script_stack *stack=st->stack;
  3364. script_attach_state(st);
  3365. if(st->state == RERUNLINE) {
  3366. run_func(st);
  3367. if(st->state == GOTO)
  3368. st->state = RUN;
  3369. } else if(st->state != END)
  3370. st->state = RUN;
  3371. while(st->state == RUN)
  3372. {
  3373. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3374. switch(c){
  3375. case C_EOL:
  3376. if( stack->defsp > stack->sp )
  3377. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3378. else
  3379. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3380. break;
  3381. case C_INT:
  3382. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3383. break;
  3384. case C_POS:
  3385. case C_NAME:
  3386. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3387. st->pos+=3;
  3388. break;
  3389. case C_ARG:
  3390. push_val(stack,c,0);
  3391. break;
  3392. case C_STR:
  3393. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3394. while(st->script->script_buf[st->pos++]);
  3395. break;
  3396. case C_FUNC:
  3397. run_func(st);
  3398. if(st->state==GOTO){
  3399. st->state = RUN;
  3400. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3401. ShowError("run_script: infinity loop !\n");
  3402. script_reportsrc(st);
  3403. st->state=END;
  3404. }
  3405. }
  3406. break;
  3407. case C_REF:
  3408. st->op2ref = 1;
  3409. break;
  3410. case C_NEG:
  3411. case C_NOT:
  3412. case C_LNOT:
  3413. op_1(st ,c);
  3414. break;
  3415. case C_ADD:
  3416. case C_SUB:
  3417. case C_MUL:
  3418. case C_DIV:
  3419. case C_MOD:
  3420. case C_EQ:
  3421. case C_NE:
  3422. case C_GT:
  3423. case C_GE:
  3424. case C_LT:
  3425. case C_LE:
  3426. case C_AND:
  3427. case C_OR:
  3428. case C_XOR:
  3429. case C_LAND:
  3430. case C_LOR:
  3431. case C_R_SHIFT:
  3432. case C_L_SHIFT:
  3433. op_2(st, c);
  3434. break;
  3435. case C_OP3:
  3436. op_3(st, c);
  3437. break;
  3438. case C_NOP:
  3439. st->state=END;
  3440. break;
  3441. default:
  3442. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3443. st->state=END;
  3444. break;
  3445. }
  3446. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3447. ShowError("run_script: infinity loop !\n");
  3448. script_reportsrc(st);
  3449. st->state=END;
  3450. }
  3451. }
  3452. if(st->sleep.tick > 0) {
  3453. //Restore previous script
  3454. script_detach_state(st, false);
  3455. //Delay execution
  3456. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3457. st->sleep.charid = sd?sd->status.char_id:0;
  3458. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3459. run_script_timer, st->sleep.charid, (intptr_t)st);
  3460. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3461. }
  3462. else if(st->state != END && st->rid){
  3463. //Resume later (st is already attached to player).
  3464. if(st->bk_st) {
  3465. ShowWarning("Unable to restore stack! Double continuation!\n");
  3466. //Report BOTH scripts to see if that can help somehow.
  3467. ShowDebug("Previous script (lost):\n");
  3468. script_reportsrc(st->bk_st);
  3469. ShowDebug("Current script:\n");
  3470. script_reportsrc(st);
  3471. script_free_state(st->bk_st);
  3472. st->bk_st = NULL;
  3473. }
  3474. } else {
  3475. //Dispose of script.
  3476. if ((sd = map_id2sd(st->rid))!=NULL)
  3477. { //Restore previous stack and save char.
  3478. if(sd->state.using_fake_npc){
  3479. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3480. sd->state.using_fake_npc = 0;
  3481. }
  3482. //Restore previous script if any.
  3483. script_detach_state(st, true);
  3484. if (sd->state.reg_dirty&2)
  3485. intif_saveregistry(sd,2);
  3486. if (sd->state.reg_dirty&1)
  3487. intif_saveregistry(sd,1);
  3488. }
  3489. script_free_state(st);
  3490. st = NULL;
  3491. }
  3492. }
  3493. int script_config_read(char *cfgName)
  3494. {
  3495. int i;
  3496. char line[1024],w1[1024],w2[1024];
  3497. FILE *fp;
  3498. fp=fopen(cfgName,"r");
  3499. if(fp==NULL){
  3500. ShowError("File not found: %s\n", cfgName);
  3501. return 1;
  3502. }
  3503. while(fgets(line, sizeof(line), fp))
  3504. {
  3505. if(line[0] == '/' && line[1] == '/')
  3506. continue;
  3507. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3508. if(i!=2)
  3509. continue;
  3510. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3511. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3512. }
  3513. else if(strcmpi(w1,"check_cmdcount")==0) {
  3514. script_config.check_cmdcount = config_switch(w2);
  3515. }
  3516. else if(strcmpi(w1,"check_gotocount")==0) {
  3517. script_config.check_gotocount = config_switch(w2);
  3518. }
  3519. else if(strcmpi(w1,"input_min_value")==0) {
  3520. script_config.input_min_value = config_switch(w2);
  3521. }
  3522. else if(strcmpi(w1,"input_max_value")==0) {
  3523. script_config.input_max_value = config_switch(w2);
  3524. }
  3525. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3526. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3527. }
  3528. else if(strcmpi(w1,"import")==0){
  3529. script_config_read(w2);
  3530. }
  3531. else {
  3532. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3533. }
  3534. }
  3535. fclose(fp);
  3536. return 0;
  3537. }
  3538. /**
  3539. * @see DBApply
  3540. */
  3541. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3542. {
  3543. struct script_code *code = db_data2ptr(data);
  3544. if (code)
  3545. script_free_code(code);
  3546. return 0;
  3547. }
  3548. void script_run_autobonus(const char *autobonus, int id, int pos)
  3549. {
  3550. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3551. if( script )
  3552. {
  3553. current_equip_item_index = pos;
  3554. run_script(script,0,id,0);
  3555. }
  3556. }
  3557. void script_add_autobonus(const char *autobonus)
  3558. {
  3559. if( strdb_get(autobonus_db, autobonus) == NULL )
  3560. {
  3561. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3562. if( script )
  3563. strdb_put(autobonus_db, autobonus, script);
  3564. }
  3565. }
  3566. /// resets a temporary character array variable to given value
  3567. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3568. {
  3569. int key;
  3570. uint8 idx;
  3571. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3572. {
  3573. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3574. return;
  3575. }
  3576. key = add_str(varname);
  3577. if( is_string_variable(varname) )
  3578. {
  3579. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3580. {
  3581. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3582. }
  3583. }
  3584. else
  3585. {
  3586. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3587. {
  3588. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3589. }
  3590. }
  3591. }
  3592. /// sets a temporary character array variable element idx to given value
  3593. /// @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.
  3594. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3595. {
  3596. int key;
  3597. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3598. {
  3599. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3600. return;
  3601. }
  3602. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3603. {
  3604. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3605. return;
  3606. }
  3607. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3608. if( is_string_variable(varname) )
  3609. {
  3610. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3611. }
  3612. else
  3613. {
  3614. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3615. }
  3616. if( refcache )
  3617. {// save to avoid repeated add_str calls
  3618. refcache[0] = key;
  3619. }
  3620. }
  3621. #ifdef BETA_THREAD_TEST
  3622. int buildin_query_sql_sub(struct script_state* st, Sql* handle);
  3623. /* used to receive items the queryThread has already processed */
  3624. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3625. int i, cursor = 0;
  3626. bool allOk = true;
  3627. EnterSpinLock(&queryThreadLock);
  3628. for( i = 0; i < queryThreadData.count; i++ ) {
  3629. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3630. if( !entry->ok ) {
  3631. allOk = false;
  3632. continue;
  3633. }
  3634. run_script_main(entry->st);
  3635. entry->st = NULL;/* empty entries */
  3636. aFree(entry);
  3637. queryThreadData.entry[i] = NULL;
  3638. }
  3639. if( allOk ) {
  3640. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3641. delete_timer(queryThreadData.timer, queryThread_timer);
  3642. queryThreadData.timer = INVALID_TIMER;
  3643. }
  3644. /* now lets clear the mess. */
  3645. for( i = 0; i < queryThreadData.count; i++ ) {
  3646. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3647. if( entry == NULL )
  3648. continue;/* entry on hold */
  3649. /* move */
  3650. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3651. cursor++;
  3652. }
  3653. queryThreadData.count = cursor;
  3654. LeaveSpinLock(&queryThreadLock);
  3655. return 0;
  3656. }
  3657. void queryThread_add(struct script_state *st, bool type) {
  3658. int idx = 0;
  3659. struct queryThreadEntry* entry = NULL;
  3660. EnterSpinLock(&queryThreadLock);
  3661. if( queryThreadData.count++ != 0 )
  3662. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3663. idx = queryThreadData.count-1;
  3664. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3665. entry = queryThreadData.entry[idx];
  3666. entry->st = st;
  3667. entry->ok = false;
  3668. entry->type = type;
  3669. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3670. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3671. }
  3672. LeaveSpinLock(&queryThreadLock);
  3673. /* unlock the queryThread */
  3674. racond_signal(queryThreadCond);
  3675. }
  3676. /* adds a new log to the queue */
  3677. void queryThread_log(char * entry, int length) {
  3678. int idx = logThreadData.count;
  3679. EnterSpinLock(&queryThreadLock);
  3680. if( logThreadData.count++ != 0 )
  3681. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3682. CREATE(logThreadData.entry[idx], char, length + 1 );
  3683. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3684. LeaveSpinLock(&queryThreadLock);
  3685. /* unlock the queryThread */
  3686. racond_signal(queryThreadCond);
  3687. }
  3688. /* queryThread_main */
  3689. static void *queryThread_main(void *x) {
  3690. Sql *queryThread_handle = Sql_Malloc();
  3691. int i;
  3692. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) )
  3693. exit(EXIT_FAILURE);
  3694. if( strlen(default_codepage) > 0 )
  3695. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3696. Sql_ShowDebug(queryThread_handle);
  3697. if( log_config.sql_logs ) {
  3698. logmysql_handle = Sql_Malloc();
  3699. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) )
  3700. exit(EXIT_FAILURE);
  3701. if( strlen(default_codepage) > 0 )
  3702. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3703. Sql_ShowDebug(logmysql_handle);
  3704. }
  3705. while( 1 ) {
  3706. if(queryThreadTerminate > 0)
  3707. break;
  3708. EnterSpinLock(&queryThreadLock);
  3709. /* mess with queryThreadData within the lock */
  3710. for( i = 0; i < queryThreadData.count; i++ ) {
  3711. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3712. if( entry->ok )
  3713. continue;
  3714. else if ( !entry->st || !entry->st->stack ) {
  3715. entry->ok = true;/* dispose */
  3716. continue;
  3717. }
  3718. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  3719. entry->ok = true;/* we're done with this */
  3720. }
  3721. /* also check for any logs in need to be sent */
  3722. if( log_config.sql_logs ) {
  3723. for( i = 0; i < logThreadData.count; i++ ) {
  3724. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  3725. Sql_ShowDebug(logmysql_handle);
  3726. aFree(logThreadData.entry[i]);
  3727. }
  3728. logThreadData.count = 0;
  3729. }
  3730. LeaveSpinLock(&queryThreadLock);
  3731. ramutex_lock( queryThreadMutex );
  3732. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  3733. ramutex_unlock( queryThreadMutex );
  3734. }
  3735. Sql_Free(queryThread_handle);
  3736. if( log_config.sql_logs ) {
  3737. Sql_Free(logmysql_handle);
  3738. }
  3739. return NULL;
  3740. }
  3741. #endif
  3742. /*==========================================
  3743. * Destructor
  3744. *------------------------------------------*/
  3745. int do_final_script() {
  3746. int i;
  3747. #ifdef DEBUG_HASH
  3748. if (battle_config.etc_log)
  3749. {
  3750. FILE *fp = fopen("hash_dump.txt","wt");
  3751. if(fp) {
  3752. int count[SCRIPT_HASH_SIZE];
  3753. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3754. int n=0;
  3755. int min=INT_MAX,max=0,zero=0;
  3756. double mean=0.0f;
  3757. double median=0.0f;
  3758. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3759. memset(count, 0, sizeof(count));
  3760. fprintf(fp,"num : hash : data_name\n");
  3761. fprintf(fp,"---------------------------------------------------------------\n");
  3762. for(i=LABEL_START; i<str_num; i++) {
  3763. unsigned int h = calc_hash(get_str(i));
  3764. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3765. ++count[h];
  3766. }
  3767. fprintf(fp,"--------------------\n\n");
  3768. memset(count2, 0, sizeof(count2));
  3769. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3770. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3771. if(min > count[i])
  3772. min = count[i]; // minimun count of collision
  3773. if(max < count[i])
  3774. max = count[i]; // maximun count of collision
  3775. if(count[i] == 0)
  3776. zero++;
  3777. ++count2[count[i]];
  3778. }
  3779. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3780. for( i=min; i <= max; ++i ){
  3781. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3782. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3783. }
  3784. for( i=min; i <= max; ++i ){
  3785. n += count2[i];
  3786. if( n*2 >= SCRIPT_HASH_SIZE )
  3787. {
  3788. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3789. median = (i+i+1)/2.0f;
  3790. else
  3791. median = i;
  3792. break;
  3793. }
  3794. }
  3795. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3796. fclose(fp);
  3797. }
  3798. }
  3799. #endif
  3800. mapreg_final();
  3801. db_destroy(scriptlabel_db);
  3802. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3803. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3804. if(sleep_db) {
  3805. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3806. while(n) {
  3807. struct script_state *st = (struct script_state *)n->data;
  3808. script_free_state(st);
  3809. n = n->next;
  3810. }
  3811. linkdb_final(&sleep_db);
  3812. }
  3813. if (str_data)
  3814. aFree(str_data);
  3815. if (str_buf)
  3816. aFree(str_buf);
  3817. for( i = 0; i < atcmd_binding_count; i++ ) {
  3818. aFree(atcmd_binding[i]);
  3819. }
  3820. if( atcmd_binding_count != 0 )
  3821. aFree(atcmd_binding);
  3822. #ifdef BETA_THREAD_TEST
  3823. /* QueryThread */
  3824. InterlockedIncrement(&queryThreadTerminate);
  3825. racond_signal(queryThreadCond);
  3826. rathread_wait(queryThread, NULL);
  3827. // Destroy cond var and mutex.
  3828. racond_destroy( queryThreadCond );
  3829. ramutex_destroy( queryThreadMutex );
  3830. /* Clear missing vars */
  3831. for( i = 0; i < queryThreadData.count; i++ ) {
  3832. aFree(queryThreadData.entry[i]);
  3833. }
  3834. aFree(queryThreadData.entry);
  3835. for( i = 0; i < logThreadData.count; i++ ) {
  3836. aFree(logThreadData.entry[i]);
  3837. }
  3838. aFree(logThreadData.entry);
  3839. #endif
  3840. return 0;
  3841. }
  3842. /*==========================================
  3843. * Initialization
  3844. *------------------------------------------*/
  3845. int do_init_script() {
  3846. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3847. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3848. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3849. mapreg_init();
  3850. #ifdef BETA_THREAD_TEST
  3851. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  3852. queryThreadData.count = 0;
  3853. CREATE(logThreadData.entry, char *, 1);
  3854. logThreadData.count = 0;
  3855. /* QueryThread Start */
  3856. InitializeSpinLock(&queryThreadLock);
  3857. queryThreadData.timer = INVALID_TIMER;
  3858. queryThreadTerminate = 0;
  3859. queryThreadMutex = ramutex_create();
  3860. queryThreadCond = racond_create();
  3861. queryThread = rathread_create(queryThread_main, NULL);
  3862. if(queryThread == NULL){
  3863. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  3864. exit(EXIT_FAILURE);
  3865. }
  3866. add_timer_func_list(queryThread_timer, "queryThread_timer");
  3867. #endif
  3868. return 0;
  3869. }
  3870. int script_reload() {
  3871. int i;
  3872. #ifdef BETA_THREAD_TEST
  3873. /* we're reloading so any queries undergoing should be...exterminated. */
  3874. EnterSpinLock(&queryThreadLock);
  3875. for( i = 0; i < queryThreadData.count; i++ ) {
  3876. aFree(queryThreadData.entry[i]);
  3877. }
  3878. queryThreadData.count = 0;
  3879. if( queryThreadData.timer != INVALID_TIMER ) {
  3880. delete_timer(queryThreadData.timer, queryThread_timer);
  3881. queryThreadData.timer = INVALID_TIMER;
  3882. }
  3883. LeaveSpinLock(&queryThreadLock);
  3884. #endif
  3885. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  3886. db_clear(scriptlabel_db);
  3887. // @commands (script based)
  3888. // Clear bindings
  3889. for( i = 0; i < atcmd_binding_count; i++ ) {
  3890. aFree(atcmd_binding[i]);
  3891. }
  3892. if( atcmd_binding_count != 0 )
  3893. aFree(atcmd_binding);
  3894. atcmd_binding_count = 0;
  3895. if(sleep_db) {
  3896. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3897. while(n) {
  3898. struct script_state *st = (struct script_state *)n->data;
  3899. script_free_state(st);
  3900. n = n->next;
  3901. }
  3902. linkdb_final(&sleep_db);
  3903. }
  3904. mapreg_reload();
  3905. return 0;
  3906. }
  3907. //-----------------------------------------------------------------------------
  3908. // buildin functions
  3909. //
  3910. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3911. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3912. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3913. /////////////////////////////////////////////////////////////////////
  3914. // NPC interaction
  3915. //
  3916. /// Appends a message to the npc dialog.
  3917. /// If a dialog doesn't exist yet, one is created.
  3918. ///
  3919. /// mes "<message>";
  3920. BUILDIN_FUNC(mes)
  3921. {
  3922. TBL_PC* sd = script_rid2sd(st);
  3923. if( sd == NULL )
  3924. return 0;
  3925. if( !script_hasdata(st, 3) )
  3926. {// only a single line detected in the script
  3927. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3928. }
  3929. else
  3930. {// parse multiple lines as they exist
  3931. int i;
  3932. for( i = 2; script_hasdata(st, i); i++ )
  3933. {
  3934. // send the message to the client
  3935. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3936. }
  3937. }
  3938. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  3939. return SCRIPT_CMD_SUCCESS;
  3940. }
  3941. /// Displays the button 'next' in the npc dialog.
  3942. /// The dialog text is cleared and the script continues when the button is pressed.
  3943. ///
  3944. /// next;
  3945. BUILDIN_FUNC(next)
  3946. {
  3947. TBL_PC* sd;
  3948. sd = script_rid2sd(st);
  3949. if( sd == NULL )
  3950. return 0;
  3951. #ifdef SECURE_NPCTIMEOUT
  3952. sd->npc_idle_type = NPCT_WAIT;
  3953. #endif
  3954. st->state = STOP;
  3955. clif_scriptnext(sd, st->oid);
  3956. return SCRIPT_CMD_SUCCESS;
  3957. }
  3958. /// Ends the script and displays the button 'close' on the npc dialog.
  3959. /// The dialog is closed when the button is pressed.
  3960. ///
  3961. /// close;
  3962. BUILDIN_FUNC(close)
  3963. {
  3964. TBL_PC* sd;
  3965. sd = script_rid2sd(st);
  3966. if( sd == NULL )
  3967. return 0;
  3968. if( !st->mes_active ) {
  3969. TBL_NPC* nd = map_id2nd(st->oid);
  3970. st->state = END; // Keep backwards compatibility.
  3971. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  3972. } else {
  3973. st->state = CLOSE;
  3974. st->mes_active = 0;
  3975. }
  3976. clif_scriptclose(sd, st->oid);
  3977. return SCRIPT_CMD_SUCCESS;
  3978. }
  3979. /// Displays the button 'close' on the npc dialog.
  3980. /// The dialog is closed and the script continues when the button is pressed.
  3981. ///
  3982. /// close2;
  3983. BUILDIN_FUNC(close2)
  3984. {
  3985. TBL_PC* sd;
  3986. sd = script_rid2sd(st);
  3987. if( sd == NULL )
  3988. return 0;
  3989. st->state = STOP;
  3990. if( st->mes_active )
  3991. st->mes_active = 0;
  3992. clif_scriptclose(sd, st->oid);
  3993. return SCRIPT_CMD_SUCCESS;
  3994. }
  3995. /// Counts the number of valid and total number of options in 'str'
  3996. /// If max_count > 0 the counting stops when that valid option is reached
  3997. /// total is incremented for each option (NULL is supported)
  3998. static int menu_countoptions(const char* str, int max_count, int* total)
  3999. {
  4000. int count = 0;
  4001. int bogus_total;
  4002. if( total == NULL )
  4003. total = &bogus_total;
  4004. ++(*total);
  4005. // initial empty options
  4006. while( *str == ':' )
  4007. {
  4008. ++str;
  4009. ++(*total);
  4010. }
  4011. // count menu options
  4012. while( *str != '\0' )
  4013. {
  4014. ++count;
  4015. --max_count;
  4016. if( max_count == 0 )
  4017. break;
  4018. while( *str != ':' && *str != '\0' )
  4019. ++str;
  4020. while( *str == ':' )
  4021. {
  4022. ++str;
  4023. ++(*total);
  4024. }
  4025. }
  4026. return count;
  4027. }
  4028. /// Displays a menu with options and goes to the target label.
  4029. /// The script is stopped if cancel is pressed.
  4030. /// Options with no text are not displayed in the client.
  4031. ///
  4032. /// Options can be grouped together, separated by the character ':' in the text:
  4033. /// ex: menu "A:B:C",L_target;
  4034. /// All these options go to the specified target label.
  4035. ///
  4036. /// The index of the selected option is put in the variable @menu.
  4037. /// Indexes start with 1 and are consistent with grouped and empty options.
  4038. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4039. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4040. ///
  4041. /// NOTE: the client closes the npc dialog when cancel is pressed
  4042. ///
  4043. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4044. BUILDIN_FUNC(menu)
  4045. {
  4046. int i;
  4047. const char* text;
  4048. TBL_PC* sd;
  4049. sd = script_rid2sd(st);
  4050. if( sd == NULL )
  4051. return 0;
  4052. #ifdef SECURE_NPCTIMEOUT
  4053. sd->npc_idle_type = NPCT_MENU;
  4054. #endif
  4055. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4056. if( sd->state.menu_or_input == 0 )
  4057. {
  4058. struct StringBuf buf;
  4059. struct script_data* data;
  4060. if( script_lastdata(st) % 2 == 0 )
  4061. {// argument count is not even (1st argument is at index 2)
  4062. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4063. st->state = END;
  4064. return SCRIPT_CMD_FAILURE;
  4065. }
  4066. StringBuf_Init(&buf);
  4067. sd->npc_menu = 0;
  4068. for( i = 2; i < script_lastdata(st); i += 2 )
  4069. {
  4070. // menu options
  4071. text = script_getstr(st, i);
  4072. // target label
  4073. data = script_getdata(st, i+1);
  4074. if( !data_islabel(data) )
  4075. {// not a label
  4076. StringBuf_Destroy(&buf);
  4077. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4078. script_reportdata(data);
  4079. st->state = END;
  4080. return SCRIPT_CMD_FAILURE;
  4081. }
  4082. // append option(s)
  4083. if( text[0] == '\0' )
  4084. continue;// empty string, ignore
  4085. if( sd->npc_menu > 0 )
  4086. StringBuf_AppendStr(&buf, ":");
  4087. StringBuf_AppendStr(&buf, text);
  4088. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4089. }
  4090. st->state = RERUNLINE;
  4091. sd->state.menu_or_input = 1;
  4092. /**
  4093. * menus beyond this length crash the client (see bugreport:6402)
  4094. **/
  4095. if( StringBuf_Length(&buf) >= 2047 ) {
  4096. struct npc_data * nd = map_id2nd(st->oid);
  4097. char* menu;
  4098. CREATE(menu, char, 2048);
  4099. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4100. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4101. clif_scriptmenu(sd, st->oid, menu);
  4102. aFree(menu);
  4103. } else
  4104. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4105. StringBuf_Destroy(&buf);
  4106. if( sd->npc_menu >= 0xff )
  4107. {// 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
  4108. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4109. script_reportsrc(st);
  4110. }
  4111. }
  4112. else if( sd->npc_menu == 0xff )
  4113. {// Cancel was pressed
  4114. sd->state.menu_or_input = 0;
  4115. st->state = END;
  4116. }
  4117. else
  4118. {// goto target label
  4119. int menu = 0;
  4120. sd->state.menu_or_input = 0;
  4121. if( sd->npc_menu <= 0 )
  4122. {
  4123. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4124. st->state = END;
  4125. return SCRIPT_CMD_FAILURE;
  4126. }
  4127. // get target label
  4128. for( i = 2; i < script_lastdata(st); i += 2 )
  4129. {
  4130. text = script_getstr(st, i);
  4131. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4132. if( sd->npc_menu <= 0 )
  4133. break;// entry found
  4134. }
  4135. if( sd->npc_menu > 0 )
  4136. {// Invalid selection
  4137. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4138. st->state = END;
  4139. return SCRIPT_CMD_FAILURE;
  4140. }
  4141. if( !data_islabel(script_getdata(st, i + 1)) )
  4142. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4143. ShowError("script:menu: unexpected data in label argument\n");
  4144. script_reportdata(script_getdata(st, i + 1));
  4145. st->state = END;
  4146. return SCRIPT_CMD_FAILURE;
  4147. }
  4148. pc_setreg(sd, add_str("@menu"), menu);
  4149. st->pos = script_getnum(st, i + 1);
  4150. st->state = GOTO;
  4151. }
  4152. return SCRIPT_CMD_SUCCESS;
  4153. }
  4154. /// Displays a menu with options and returns the selected option.
  4155. /// Behaves like 'menu' without the target labels.
  4156. ///
  4157. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4158. ///
  4159. /// @see menu
  4160. BUILDIN_FUNC(select)
  4161. {
  4162. int i;
  4163. const char* text;
  4164. TBL_PC* sd;
  4165. sd = script_rid2sd(st);
  4166. if( sd == NULL )
  4167. return 0;
  4168. #ifdef SECURE_NPCTIMEOUT
  4169. sd->npc_idle_type = NPCT_MENU;
  4170. #endif
  4171. if( sd->state.menu_or_input == 0 ) {
  4172. struct StringBuf buf;
  4173. StringBuf_Init(&buf);
  4174. sd->npc_menu = 0;
  4175. for( i = 2; i <= script_lastdata(st); ++i ) {
  4176. text = script_getstr(st, i);
  4177. if( sd->npc_menu > 0 )
  4178. StringBuf_AppendStr(&buf, ":");
  4179. StringBuf_AppendStr(&buf, text);
  4180. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4181. }
  4182. st->state = RERUNLINE;
  4183. sd->state.menu_or_input = 1;
  4184. /**
  4185. * menus beyond this length crash the client (see bugreport:6402)
  4186. **/
  4187. if( StringBuf_Length(&buf) >= 2047 ) {
  4188. struct npc_data * nd = map_id2nd(st->oid);
  4189. char* menu;
  4190. CREATE(menu, char, 2048);
  4191. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4192. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4193. clif_scriptmenu(sd, st->oid, menu);
  4194. aFree(menu);
  4195. } else
  4196. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4197. StringBuf_Destroy(&buf);
  4198. if( sd->npc_menu >= 0xff ) {
  4199. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4200. script_reportsrc(st);
  4201. }
  4202. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4203. sd->state.menu_or_input = 0;
  4204. st->state = END;
  4205. } else {// return selected option
  4206. int menu = 0;
  4207. sd->state.menu_or_input = 0;
  4208. for( i = 2; i <= script_lastdata(st); ++i ) {
  4209. text = script_getstr(st, i);
  4210. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4211. if( sd->npc_menu <= 0 )
  4212. break;// entry found
  4213. }
  4214. pc_setreg(sd, add_str("@menu"), menu);
  4215. script_pushint(st, menu);
  4216. st->state = RUN;
  4217. }
  4218. return SCRIPT_CMD_SUCCESS;
  4219. }
  4220. /// Displays a menu with options and returns the selected option.
  4221. /// Behaves like 'menu' without the target labels, except when cancel is
  4222. /// pressed.
  4223. /// When cancel is pressed, the script continues and 255 is returned.
  4224. ///
  4225. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4226. ///
  4227. /// @see menu
  4228. BUILDIN_FUNC(prompt)
  4229. {
  4230. int i;
  4231. const char *text;
  4232. TBL_PC* sd;
  4233. sd = script_rid2sd(st);
  4234. if( sd == NULL )
  4235. return 0;
  4236. #ifdef SECURE_NPCTIMEOUT
  4237. sd->npc_idle_type = NPCT_MENU;
  4238. #endif
  4239. if( sd->state.menu_or_input == 0 )
  4240. {
  4241. struct StringBuf buf;
  4242. StringBuf_Init(&buf);
  4243. sd->npc_menu = 0;
  4244. for( i = 2; i <= script_lastdata(st); ++i )
  4245. {
  4246. text = script_getstr(st, i);
  4247. if( sd->npc_menu > 0 )
  4248. StringBuf_AppendStr(&buf, ":");
  4249. StringBuf_AppendStr(&buf, text);
  4250. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4251. }
  4252. st->state = RERUNLINE;
  4253. sd->state.menu_or_input = 1;
  4254. /**
  4255. * menus beyond this length crash the client (see bugreport:6402)
  4256. **/
  4257. if( StringBuf_Length(&buf) >= 2047 ) {
  4258. struct npc_data * nd = map_id2nd(st->oid);
  4259. char* menu;
  4260. CREATE(menu, char, 2048);
  4261. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4262. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4263. clif_scriptmenu(sd, st->oid, menu);
  4264. aFree(menu);
  4265. } else
  4266. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4267. StringBuf_Destroy(&buf);
  4268. if( sd->npc_menu >= 0xff )
  4269. {
  4270. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4271. script_reportsrc(st);
  4272. }
  4273. }
  4274. else if( sd->npc_menu == 0xff )
  4275. {// Cancel was pressed
  4276. sd->state.menu_or_input = 0;
  4277. pc_setreg(sd, add_str("@menu"), 0xff);
  4278. script_pushint(st, 0xff);
  4279. st->state = RUN;
  4280. }
  4281. else
  4282. {// return selected option
  4283. int menu = 0;
  4284. sd->state.menu_or_input = 0;
  4285. for( i = 2; i <= script_lastdata(st); ++i )
  4286. {
  4287. text = script_getstr(st, i);
  4288. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4289. if( sd->npc_menu <= 0 )
  4290. break;// entry found
  4291. }
  4292. pc_setreg(sd, add_str("@menu"), menu);
  4293. script_pushint(st, menu);
  4294. st->state = RUN;
  4295. }
  4296. return SCRIPT_CMD_SUCCESS;
  4297. }
  4298. /////////////////////////////////////////////////////////////////////
  4299. // ...
  4300. //
  4301. /// Jumps to the target script label.
  4302. ///
  4303. /// goto <label>;
  4304. BUILDIN_FUNC(goto)
  4305. {
  4306. if( !data_islabel(script_getdata(st,2)) )
  4307. {
  4308. ShowError("script:goto: not a label\n");
  4309. script_reportdata(script_getdata(st,2));
  4310. st->state = END;
  4311. return 1;
  4312. }
  4313. st->pos = script_getnum(st,2);
  4314. st->state = GOTO;
  4315. return SCRIPT_CMD_SUCCESS;
  4316. }
  4317. /*==========================================
  4318. * user-defined function call
  4319. *------------------------------------------*/
  4320. BUILDIN_FUNC(callfunc)
  4321. {
  4322. int i, j;
  4323. struct script_retinfo* ri;
  4324. struct script_code* scr;
  4325. const char* str = script_getstr(st,2);
  4326. DBMap **ref = NULL;
  4327. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4328. if( !scr )
  4329. {
  4330. ShowError("script:callfunc: function not found! [%s]\n", str);
  4331. st->state = END;
  4332. return 1;
  4333. }
  4334. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4335. {
  4336. struct script_data* data = push_copy(st->stack,i);
  4337. if( data_isreference(data) && !data->ref )
  4338. {
  4339. const char* name = reference_getname(data);
  4340. if( name[0] == '.' ) {
  4341. if(!ref){
  4342. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4343. ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars);
  4344. }
  4345. data->ref = ref;
  4346. }
  4347. }
  4348. }
  4349. CREATE(ri, struct script_retinfo, 1);
  4350. ri->script = st->script;// script code
  4351. ri->var_function = st->stack->var_function;// scope variables
  4352. ri->pos = st->pos;// script location
  4353. ri->nargs = j;// argument count
  4354. ri->defsp = st->stack->defsp;// default stack pointer
  4355. push_retinfo(st->stack, ri, ref);
  4356. st->pos = 0;
  4357. st->script = scr;
  4358. st->stack->defsp = st->stack->sp;
  4359. st->state = GOTO;
  4360. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4361. return SCRIPT_CMD_SUCCESS;
  4362. }
  4363. /*==========================================
  4364. * subroutine call
  4365. *------------------------------------------*/
  4366. BUILDIN_FUNC(callsub)
  4367. {
  4368. int i,j;
  4369. struct script_retinfo* ri;
  4370. int pos = script_getnum(st,2);
  4371. DBMap **ref = NULL;
  4372. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4373. {
  4374. ShowError("script:callsub: argument is not a label\n");
  4375. script_reportdata(script_getdata(st,2));
  4376. st->state = END;
  4377. return 1;
  4378. }
  4379. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4380. {
  4381. struct script_data* data = push_copy(st->stack,i);
  4382. if( data_isreference(data) && !data->ref )
  4383. {
  4384. const char* name = reference_getname(data);
  4385. if( name[0] == '.' && name[1] == '@' ) {
  4386. if ( !ref ) {
  4387. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4388. ref[0] = st->stack->var_function;
  4389. }
  4390. data->ref = ref;
  4391. }
  4392. }
  4393. }
  4394. CREATE(ri, struct script_retinfo, 1);
  4395. ri->script = st->script;// script code
  4396. ri->var_function = st->stack->var_function;// scope variables
  4397. ri->pos = st->pos;// script location
  4398. ri->nargs = j;// argument count
  4399. ri->defsp = st->stack->defsp;// default stack pointer
  4400. push_retinfo(st->stack, ri, ref);
  4401. st->pos = pos;
  4402. st->stack->defsp = st->stack->sp;
  4403. st->state = GOTO;
  4404. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4405. return SCRIPT_CMD_SUCCESS;
  4406. }
  4407. /// Retrieves an argument provided to callfunc/callsub.
  4408. /// If the argument doesn't exist
  4409. ///
  4410. /// getarg(<index>{,<default_value>}) -> <value>
  4411. BUILDIN_FUNC(getarg)
  4412. {
  4413. struct script_retinfo* ri;
  4414. int idx;
  4415. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4416. {
  4417. ShowError("script:getarg: no callfunc or callsub!\n");
  4418. st->state = END;
  4419. return 1;
  4420. }
  4421. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4422. idx = script_getnum(st,2);
  4423. if( idx >= 0 && idx < ri->nargs )
  4424. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4425. else if( script_hasdata(st,3) )
  4426. script_pushcopy(st, 3);
  4427. else
  4428. {
  4429. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4430. st->state = END;
  4431. return 1;
  4432. }
  4433. return SCRIPT_CMD_SUCCESS;
  4434. }
  4435. /// Returns from the current function, optionaly returning a value from the functions.
  4436. /// Don't use outside script functions.
  4437. ///
  4438. /// return;
  4439. /// return <value>;
  4440. BUILDIN_FUNC(return)
  4441. {
  4442. if( script_hasdata(st,2) )
  4443. {// return value
  4444. struct script_data* data;
  4445. script_pushcopy(st, 2);
  4446. data = script_getdatatop(st, -1);
  4447. if( data_isreference(data) )
  4448. {
  4449. const char* name = reference_getname(data);
  4450. if( name[0] == '.' && name[1] == '@' )
  4451. {// scope variable
  4452. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4453. get_val(st, data);// current scope, convert to value
  4454. }
  4455. else if( name[0] == '.' && !data->ref )
  4456. {// script variable, link to current script
  4457. data->ref = &st->script->script_vars;
  4458. }
  4459. }
  4460. }
  4461. else
  4462. {// no return value
  4463. script_pushnil(st);
  4464. }
  4465. st->state = RETFUNC;
  4466. return SCRIPT_CMD_SUCCESS;
  4467. }
  4468. /// Returns a random number from 0 to <range>-1.
  4469. /// Or returns a random number from <min> to <max>.
  4470. /// If <min> is greater than <max>, their numbers are switched.
  4471. /// rand(<range>) -> <int>
  4472. /// rand(<min>,<max>) -> <int>
  4473. BUILDIN_FUNC(rand)
  4474. {
  4475. int range;
  4476. int min;
  4477. int max;
  4478. if( script_hasdata(st,3) )
  4479. {// min,max
  4480. min = script_getnum(st,2);
  4481. max = script_getnum(st,3);
  4482. if( max < min )
  4483. swap(min, max);
  4484. range = max - min + 1;
  4485. }
  4486. else
  4487. {// range
  4488. min = 0;
  4489. range = script_getnum(st,2);
  4490. }
  4491. if( range <= 1 )
  4492. script_pushint(st, min);
  4493. else
  4494. script_pushint(st, rnd()%range + min);
  4495. return SCRIPT_CMD_SUCCESS;
  4496. }
  4497. /*==========================================
  4498. * Warp sd to str,x,y or Random or SavePoint/Save
  4499. *------------------------------------------*/
  4500. BUILDIN_FUNC(warp)
  4501. {
  4502. int ret;
  4503. int x,y;
  4504. const char* str;
  4505. TBL_PC* sd;
  4506. sd = script_rid2sd(st);
  4507. if( sd == NULL )
  4508. return 0;
  4509. str = script_getstr(st,2);
  4510. x = script_getnum(st,3);
  4511. y = script_getnum(st,4);
  4512. if(strcmp(str,"Random")==0)
  4513. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4514. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4515. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4516. else
  4517. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4518. if( ret ) {
  4519. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4520. return 1;
  4521. }
  4522. return SCRIPT_CMD_SUCCESS;
  4523. }
  4524. /*==========================================
  4525. * Warp a specified area
  4526. *------------------------------------------*/
  4527. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4528. {
  4529. int x2,y2,x3,y3;
  4530. unsigned int index;
  4531. index = va_arg(ap,unsigned int);
  4532. x2 = va_arg(ap,int);
  4533. y2 = va_arg(ap,int);
  4534. x3 = va_arg(ap,int);
  4535. y3 = va_arg(ap,int);
  4536. if(index == 0)
  4537. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4538. else if(x3 && y3) {
  4539. int max, tx, ty, j = 0;
  4540. // choose a suitable max number of attempts
  4541. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4542. max = 1000;
  4543. // find a suitable map cell
  4544. do {
  4545. tx = rnd()%(x3-x2+1)+x2;
  4546. ty = rnd()%(y3-y2+1)+y2;
  4547. j++;
  4548. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4549. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4550. }
  4551. else
  4552. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4553. return 0;
  4554. }
  4555. BUILDIN_FUNC(areawarp)
  4556. {
  4557. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4558. unsigned int index;
  4559. const char *str;
  4560. const char *mapname;
  4561. mapname = script_getstr(st,2);
  4562. x0 = script_getnum(st,3);
  4563. y0 = script_getnum(st,4);
  4564. x1 = script_getnum(st,5);
  4565. y1 = script_getnum(st,6);
  4566. str = script_getstr(st,7);
  4567. x2 = script_getnum(st,8);
  4568. y2 = script_getnum(st,9);
  4569. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4570. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4571. x3 = 0;
  4572. y3 = 0;
  4573. } else if( x3 && y3 ) {
  4574. // normalize x3/y3 coordinates
  4575. if( x3 < x2 ) swap(x3,x2);
  4576. if( y3 < y2 ) swap(y3,y2);
  4577. }
  4578. }
  4579. if( (m = map_mapname2mapid(mapname)) < 0 )
  4580. return 0;
  4581. if( strcmp(str,"Random") == 0 )
  4582. index = 0;
  4583. else if( !(index=mapindex_name2id(str)) )
  4584. return 0;
  4585. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4586. return SCRIPT_CMD_SUCCESS;
  4587. }
  4588. /*==========================================
  4589. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4590. *------------------------------------------*/
  4591. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4592. {
  4593. int hp, sp;
  4594. hp = va_arg(ap, int);
  4595. sp = va_arg(ap, int);
  4596. pc_percentheal((TBL_PC *)bl,hp,sp);
  4597. return 0;
  4598. }
  4599. BUILDIN_FUNC(areapercentheal)
  4600. {
  4601. int hp,sp,m;
  4602. const char *mapname;
  4603. int x0,y0,x1,y1;
  4604. mapname=script_getstr(st,2);
  4605. x0=script_getnum(st,3);
  4606. y0=script_getnum(st,4);
  4607. x1=script_getnum(st,5);
  4608. y1=script_getnum(st,6);
  4609. hp=script_getnum(st,7);
  4610. sp=script_getnum(st,8);
  4611. if( (m=map_mapname2mapid(mapname))< 0)
  4612. return 0;
  4613. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4614. return SCRIPT_CMD_SUCCESS;
  4615. }
  4616. /*==========================================
  4617. * warpchar [LuzZza]
  4618. * Useful for warp one player from
  4619. * another player npc-session.
  4620. * Using: warpchar "mapname",x,y,Char_ID;
  4621. *------------------------------------------*/
  4622. BUILDIN_FUNC(warpchar)
  4623. {
  4624. int x,y,a;
  4625. const char *str;
  4626. TBL_PC *sd;
  4627. str=script_getstr(st,2);
  4628. x=script_getnum(st,3);
  4629. y=script_getnum(st,4);
  4630. a=script_getnum(st,5);
  4631. sd = map_charid2sd(a);
  4632. if( sd == NULL )
  4633. return 0;
  4634. if(strcmp(str, "Random") == 0)
  4635. pc_randomwarp(sd, CLR_TELEPORT);
  4636. else
  4637. if(strcmp(str, "SavePoint") == 0)
  4638. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4639. else
  4640. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4641. return SCRIPT_CMD_SUCCESS;
  4642. }
  4643. /*==========================================
  4644. * Warpparty - [Fredzilla] [Paradox924X]
  4645. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4646. * If 'from_mapname' is specified, only the party members on that map will be warped
  4647. *------------------------------------------*/
  4648. BUILDIN_FUNC(warpparty)
  4649. {
  4650. TBL_PC *sd = NULL;
  4651. TBL_PC *pl_sd;
  4652. struct party_data* p;
  4653. int type;
  4654. int mapindex;
  4655. int i;
  4656. const char* str = script_getstr(st,2);
  4657. int x = script_getnum(st,3);
  4658. int y = script_getnum(st,4);
  4659. int p_id = script_getnum(st,5);
  4660. const char* str2 = NULL;
  4661. if ( script_hasdata(st,6) )
  4662. str2 = script_getstr(st,6);
  4663. p = party_search(p_id);
  4664. if(!p)
  4665. return 0;
  4666. type = ( strcmp(str,"Random")==0 ) ? 0
  4667. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4668. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4669. : ( strcmp(str,"Leader")==0 ) ? 3
  4670. : 4;
  4671. switch (type)
  4672. {
  4673. case 3:
  4674. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4675. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4676. return 0;
  4677. pl_sd = p->data[i].sd;
  4678. mapindex = pl_sd->mapindex;
  4679. x = pl_sd->bl.x;
  4680. y = pl_sd->bl.y;
  4681. break;
  4682. case 4:
  4683. mapindex = mapindex_name2id(str);
  4684. break;
  4685. case 2:
  4686. //"SavePoint" uses save point of the currently attached player
  4687. if (( sd = script_rid2sd(st) ) == NULL )
  4688. return 0;
  4689. default:
  4690. mapindex = 0;
  4691. break;
  4692. }
  4693. for (i = 0; i < MAX_PARTY; i++)
  4694. {
  4695. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4696. continue;
  4697. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4698. continue;
  4699. if( pc_isdead(pl_sd) )
  4700. continue;
  4701. switch( type )
  4702. {
  4703. case 0: // Random
  4704. if(!map[pl_sd->bl.m].flag.nowarp)
  4705. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4706. break;
  4707. case 1: // SavePointAll
  4708. if(!map[pl_sd->bl.m].flag.noreturn)
  4709. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4710. break;
  4711. case 2: // SavePoint
  4712. if(!map[pl_sd->bl.m].flag.noreturn)
  4713. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4714. break;
  4715. case 3: // Leader
  4716. case 4: // m,x,y
  4717. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4718. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4719. break;
  4720. }
  4721. }
  4722. return SCRIPT_CMD_SUCCESS;
  4723. }
  4724. /*==========================================
  4725. * Warpguild - [Fredzilla]
  4726. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4727. *------------------------------------------*/
  4728. BUILDIN_FUNC(warpguild)
  4729. {
  4730. TBL_PC *sd = NULL;
  4731. TBL_PC *pl_sd;
  4732. struct guild* g;
  4733. struct s_mapiterator* iter;
  4734. int type;
  4735. const char* str = script_getstr(st,2);
  4736. int x = script_getnum(st,3);
  4737. int y = script_getnum(st,4);
  4738. int gid = script_getnum(st,5);
  4739. g = guild_search(gid);
  4740. if( g == NULL )
  4741. return 0;
  4742. type = ( strcmp(str,"Random")==0 ) ? 0
  4743. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4744. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4745. : 3;
  4746. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4747. {// "SavePoint" uses save point of the currently attached player
  4748. return 0;
  4749. }
  4750. iter = mapit_getallusers();
  4751. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4752. {
  4753. if( pl_sd->status.guild_id != gid )
  4754. continue;
  4755. switch( type )
  4756. {
  4757. case 0: // Random
  4758. if(!map[pl_sd->bl.m].flag.nowarp)
  4759. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4760. break;
  4761. case 1: // SavePointAll
  4762. if(!map[pl_sd->bl.m].flag.noreturn)
  4763. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4764. break;
  4765. case 2: // SavePoint
  4766. if(!map[pl_sd->bl.m].flag.noreturn)
  4767. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4768. break;
  4769. case 3: // m,x,y
  4770. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4771. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4772. break;
  4773. }
  4774. }
  4775. mapit_free(iter);
  4776. return SCRIPT_CMD_SUCCESS;
  4777. }
  4778. /*==========================================
  4779. * Force Heal a player (hp and sp)
  4780. *------------------------------------------*/
  4781. BUILDIN_FUNC(heal)
  4782. {
  4783. TBL_PC *sd;
  4784. int hp,sp;
  4785. sd = script_rid2sd(st);
  4786. if (!sd) return 0;
  4787. hp=script_getnum(st,2);
  4788. sp=script_getnum(st,3);
  4789. status_heal(&sd->bl, hp, sp, 1);
  4790. return SCRIPT_CMD_SUCCESS;
  4791. }
  4792. /*==========================================
  4793. * Heal a player by item (get vit bonus etc)
  4794. *------------------------------------------*/
  4795. BUILDIN_FUNC(itemheal)
  4796. {
  4797. TBL_PC *sd;
  4798. int hp,sp;
  4799. hp=script_getnum(st,2);
  4800. sp=script_getnum(st,3);
  4801. if(potion_flag==1) {
  4802. potion_hp = hp;
  4803. potion_sp = sp;
  4804. return 0;
  4805. }
  4806. sd = script_rid2sd(st);
  4807. if (!sd) return 0;
  4808. pc_itemheal(sd,sd->itemid,hp,sp);
  4809. return SCRIPT_CMD_SUCCESS;
  4810. }
  4811. /*==========================================
  4812. *
  4813. *------------------------------------------*/
  4814. BUILDIN_FUNC(percentheal)
  4815. {
  4816. int hp,sp;
  4817. TBL_PC* sd;
  4818. hp=script_getnum(st,2);
  4819. sp=script_getnum(st,3);
  4820. if(potion_flag==1) {
  4821. potion_per_hp = hp;
  4822. potion_per_sp = sp;
  4823. return 0;
  4824. }
  4825. sd = script_rid2sd(st);
  4826. if( sd == NULL )
  4827. return 0;
  4828. #ifdef RENEWAL
  4829. if( sd->sc.data[SC_EXTREMITYFIST2] )
  4830. sp = 0;
  4831. #endif
  4832. pc_percentheal(sd,hp,sp);
  4833. return SCRIPT_CMD_SUCCESS;
  4834. }
  4835. /*==========================================
  4836. *
  4837. *------------------------------------------*/
  4838. BUILDIN_FUNC(jobchange)
  4839. {
  4840. int job, upper=-1;
  4841. job=script_getnum(st,2);
  4842. if( script_hasdata(st,3) )
  4843. upper=script_getnum(st,3);
  4844. if (pcdb_checkid(job))
  4845. {
  4846. TBL_PC* sd;
  4847. sd = script_rid2sd(st);
  4848. if( sd == NULL )
  4849. return 0;
  4850. pc_jobchange(sd, job, upper);
  4851. }
  4852. return SCRIPT_CMD_SUCCESS;
  4853. }
  4854. /*==========================================
  4855. *
  4856. *------------------------------------------*/
  4857. BUILDIN_FUNC(jobname)
  4858. {
  4859. int class_=script_getnum(st,2);
  4860. script_pushconststr(st, (char*)job_name(class_));
  4861. return SCRIPT_CMD_SUCCESS;
  4862. }
  4863. /// Get input from the player.
  4864. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4865. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4866. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4867. /// shorter than 'min' and 0 otherwise.
  4868. ///
  4869. /// input(<var>{,<min>{,<max>}}) -> <int>
  4870. BUILDIN_FUNC(input)
  4871. {
  4872. TBL_PC* sd;
  4873. struct script_data* data;
  4874. int uid;
  4875. const char* name;
  4876. int min;
  4877. int max;
  4878. sd = script_rid2sd(st);
  4879. if( sd == NULL )
  4880. return 0;
  4881. data = script_getdata(st,2);
  4882. if( !data_isreference(data) ){
  4883. ShowError("script:input: not a variable\n");
  4884. script_reportdata(data);
  4885. st->state = END;
  4886. return 1;
  4887. }
  4888. uid = reference_getuid(data);
  4889. name = reference_getname(data);
  4890. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4891. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4892. #ifdef SECURE_NPCTIMEOUT
  4893. sd->npc_idle_type = NPCT_WAIT;
  4894. #endif
  4895. if( !sd->state.menu_or_input )
  4896. { // first invocation, display npc input box
  4897. sd->state.menu_or_input = 1;
  4898. st->state = RERUNLINE;
  4899. if( is_string_variable(name) )
  4900. clif_scriptinputstr(sd,st->oid);
  4901. else
  4902. clif_scriptinput(sd,st->oid);
  4903. }
  4904. else
  4905. { // take received text/value and store it in the designated variable
  4906. sd->state.menu_or_input = 0;
  4907. if( is_string_variable(name) )
  4908. {
  4909. int len = (int)strlen(sd->npc_str);
  4910. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4911. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4912. }
  4913. else
  4914. {
  4915. int amount = sd->npc_amount;
  4916. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  4917. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4918. }
  4919. st->state = RUN;
  4920. }
  4921. return SCRIPT_CMD_SUCCESS;
  4922. }
  4923. // declare the copyarray method here for future reference
  4924. BUILDIN_FUNC(copyarray);
  4925. /// Sets the value of a variable.
  4926. /// The value is converted to the type of the variable.
  4927. ///
  4928. /// set(<variable>,<value>) -> <variable>
  4929. BUILDIN_FUNC(set)
  4930. {
  4931. TBL_PC* sd = NULL;
  4932. struct script_data* data;
  4933. //struct script_data* datavalue;
  4934. int num;
  4935. const char* name;
  4936. char prefix;
  4937. data = script_getdata(st,2);
  4938. //datavalue = script_getdata(st,3);
  4939. if( !data_isreference(data) )
  4940. {
  4941. ShowError("script:set: not a variable\n");
  4942. script_reportdata(script_getdata(st,2));
  4943. st->state = END;
  4944. return 1;
  4945. }
  4946. num = reference_getuid(data);
  4947. name = reference_getname(data);
  4948. prefix = *name;
  4949. if( not_server_variable(prefix) )
  4950. {
  4951. sd = script_rid2sd(st);
  4952. if( sd == NULL )
  4953. {
  4954. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4955. return 0;
  4956. }
  4957. }
  4958. #if 0
  4959. if( data_isreference(datavalue) )
  4960. {// the value being referenced is a variable
  4961. const char* namevalue = reference_getname(datavalue);
  4962. if( !not_array_variable(*namevalue) )
  4963. {// array variable being copied into another array variable
  4964. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  4965. {// player must be attached in order to copy a player variable
  4966. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  4967. return 0;
  4968. }
  4969. if( is_string_variable(namevalue) != is_string_variable(name) )
  4970. {// non-matching array value types
  4971. ShowWarning("script:set: two array variables do not match in type.\n");
  4972. return 0;
  4973. }
  4974. // push the maximum number of array values to the stack
  4975. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  4976. // call the copy array method directly
  4977. return buildin_copyarray(st);
  4978. }
  4979. }
  4980. #endif
  4981. if( is_string_variable(name) )
  4982. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4983. else
  4984. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  4985. // return a copy of the variable reference
  4986. script_pushcopy(st,2);
  4987. return SCRIPT_CMD_SUCCESS;
  4988. }
  4989. /////////////////////////////////////////////////////////////////////
  4990. /// Array variables
  4991. ///
  4992. /// Returns the size of the specified array
  4993. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  4994. {
  4995. int32 ret = idx;
  4996. if( isstring )
  4997. {
  4998. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4999. {
  5000. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  5001. if( str && *str )
  5002. ret = idx + 1;
  5003. script_removetop(st, -1, 0);
  5004. }
  5005. }
  5006. else
  5007. {
  5008. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5009. {
  5010. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  5011. if( num )
  5012. ret = idx + 1;
  5013. script_removetop(st, -1, 0);
  5014. }
  5015. }
  5016. return ret;
  5017. }
  5018. /// Sets values of an array, from the starting index.
  5019. /// ex: setarray arr[1],1,2,3;
  5020. ///
  5021. /// setarray <array variable>,<value1>{,<value2>...};
  5022. BUILDIN_FUNC(setarray)
  5023. {
  5024. struct script_data* data;
  5025. const char* name;
  5026. int32 start;
  5027. int32 end;
  5028. int32 id;
  5029. int32 i;
  5030. TBL_PC* sd = NULL;
  5031. data = script_getdata(st, 2);
  5032. if( !data_isreference(data) )
  5033. {
  5034. ShowError("script:setarray: not a variable\n");
  5035. script_reportdata(data);
  5036. st->state = END;
  5037. return 1;// not a variable
  5038. }
  5039. id = reference_getid(data);
  5040. start = reference_getindex(data);
  5041. name = reference_getname(data);
  5042. if( not_array_variable(*name) )
  5043. {
  5044. ShowError("script:setarray: illegal scope\n");
  5045. script_reportdata(data);
  5046. st->state = END;
  5047. return 1;// not supported
  5048. }
  5049. if( not_server_variable(*name) )
  5050. {
  5051. sd = script_rid2sd(st);
  5052. if( sd == NULL )
  5053. return 0;// no player attached
  5054. }
  5055. end = start + script_lastdata(st) - 2;
  5056. if( end > SCRIPT_MAX_ARRAYSIZE )
  5057. end = SCRIPT_MAX_ARRAYSIZE;
  5058. if( is_string_variable(name) )
  5059. {// string array
  5060. for( i = 3; start < end; ++start, ++i )
  5061. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5062. }
  5063. else
  5064. {// int array
  5065. for( i = 3; start < end; ++start, ++i )
  5066. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5067. }
  5068. return SCRIPT_CMD_SUCCESS;
  5069. }
  5070. /// Sets count values of an array, from the starting index.
  5071. /// ex: cleararray arr[0],0,1;
  5072. ///
  5073. /// cleararray <array variable>,<value>,<count>;
  5074. BUILDIN_FUNC(cleararray)
  5075. {
  5076. struct script_data* data;
  5077. const char* name;
  5078. int32 start;
  5079. int32 end;
  5080. int32 id;
  5081. void* v;
  5082. TBL_PC* sd = NULL;
  5083. data = script_getdata(st, 2);
  5084. if( !data_isreference(data) )
  5085. {
  5086. ShowError("script:cleararray: not a variable\n");
  5087. script_reportdata(data);
  5088. st->state = END;
  5089. return 1;// not a variable
  5090. }
  5091. id = reference_getid(data);
  5092. start = reference_getindex(data);
  5093. name = reference_getname(data);
  5094. if( not_array_variable(*name) )
  5095. {
  5096. ShowError("script:cleararray: illegal scope\n");
  5097. script_reportdata(data);
  5098. st->state = END;
  5099. return 1;// not supported
  5100. }
  5101. if( not_server_variable(*name) )
  5102. {
  5103. sd = script_rid2sd(st);
  5104. if( sd == NULL )
  5105. return 0;// no player attached
  5106. }
  5107. if( is_string_variable(name) )
  5108. v = (void*)script_getstr(st, 3);
  5109. else
  5110. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5111. end = start + script_getnum(st, 4);
  5112. if( end > SCRIPT_MAX_ARRAYSIZE )
  5113. end = SCRIPT_MAX_ARRAYSIZE;
  5114. for( ; start < end; ++start )
  5115. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5116. return SCRIPT_CMD_SUCCESS;
  5117. }
  5118. /// Copies data from one array to another.
  5119. /// ex: copyarray arr[0],arr[2],2;
  5120. ///
  5121. /// copyarray <destination array variable>,<source array variable>,<count>;
  5122. BUILDIN_FUNC(copyarray)
  5123. {
  5124. struct script_data* data1;
  5125. struct script_data* data2;
  5126. const char* name1;
  5127. const char* name2;
  5128. int32 idx1;
  5129. int32 idx2;
  5130. int32 id1;
  5131. int32 id2;
  5132. void* v;
  5133. int32 i;
  5134. int32 count;
  5135. TBL_PC* sd = NULL;
  5136. data1 = script_getdata(st, 2);
  5137. data2 = script_getdata(st, 3);
  5138. if( !data_isreference(data1) || !data_isreference(data2) )
  5139. {
  5140. ShowError("script:copyarray: not a variable\n");
  5141. script_reportdata(data1);
  5142. script_reportdata(data2);
  5143. st->state = END;
  5144. return 1;// not a variable
  5145. }
  5146. id1 = reference_getid(data1);
  5147. id2 = reference_getid(data2);
  5148. idx1 = reference_getindex(data1);
  5149. idx2 = reference_getindex(data2);
  5150. name1 = reference_getname(data1);
  5151. name2 = reference_getname(data2);
  5152. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5153. {
  5154. ShowError("script:copyarray: illegal scope\n");
  5155. script_reportdata(data1);
  5156. script_reportdata(data2);
  5157. st->state = END;
  5158. return 1;// not supported
  5159. }
  5160. if( is_string_variable(name1) != is_string_variable(name2) )
  5161. {
  5162. ShowError("script:copyarray: type mismatch\n");
  5163. script_reportdata(data1);
  5164. script_reportdata(data2);
  5165. st->state = END;
  5166. return 1;// data type mismatch
  5167. }
  5168. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5169. {
  5170. sd = script_rid2sd(st);
  5171. if( sd == NULL )
  5172. return 0;// no player attached
  5173. }
  5174. count = script_getnum(st, 4);
  5175. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5176. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5177. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5178. return 0;// nothing to copy
  5179. if( id1 == id2 && idx1 > idx2 )
  5180. {// destination might be overlapping the source - copy in reverse order
  5181. for( i = count - 1; i >= 0; --i )
  5182. {
  5183. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5184. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5185. script_removetop(st, -1, 0);
  5186. }
  5187. }
  5188. else
  5189. {// normal copy
  5190. for( i = 0; i < count; ++i )
  5191. {
  5192. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5193. {
  5194. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5195. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5196. script_removetop(st, -1, 0);
  5197. }
  5198. else// out of range - assume ""/0
  5199. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5200. }
  5201. }
  5202. return SCRIPT_CMD_SUCCESS;
  5203. }
  5204. /// Returns the size of the array.
  5205. /// Assumes that everything before the starting index exists.
  5206. /// ex: getarraysize(arr[3])
  5207. ///
  5208. /// getarraysize(<array variable>) -> <int>
  5209. BUILDIN_FUNC(getarraysize)
  5210. {
  5211. struct script_data* data;
  5212. const char* name;
  5213. data = script_getdata(st, 2);
  5214. if( !data_isreference(data) )
  5215. {
  5216. ShowError("script:getarraysize: not a variable\n");
  5217. script_reportdata(data);
  5218. script_pushnil(st);
  5219. st->state = END;
  5220. return 1;// not a variable
  5221. }
  5222. name = reference_getname(data);
  5223. if( not_array_variable(*name) )
  5224. {
  5225. ShowError("script:getarraysize: illegal scope\n");
  5226. script_reportdata(data);
  5227. script_pushnil(st);
  5228. st->state = END;
  5229. return 1;// not supported
  5230. }
  5231. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5232. return SCRIPT_CMD_SUCCESS;
  5233. }
  5234. /// Deletes count or all the elements in an array, from the starting index.
  5235. /// ex: deletearray arr[4],2;
  5236. ///
  5237. /// deletearray <array variable>;
  5238. /// deletearray <array variable>,<count>;
  5239. BUILDIN_FUNC(deletearray)
  5240. {
  5241. struct script_data* data;
  5242. const char* name;
  5243. int start;
  5244. int end;
  5245. int id;
  5246. TBL_PC *sd = NULL;
  5247. data = script_getdata(st, 2);
  5248. if( !data_isreference(data) )
  5249. {
  5250. ShowError("script:deletearray: not a variable\n");
  5251. script_reportdata(data);
  5252. st->state = END;
  5253. return 1;// not a variable
  5254. }
  5255. id = reference_getid(data);
  5256. start = reference_getindex(data);
  5257. name = reference_getname(data);
  5258. if( not_array_variable(*name) )
  5259. {
  5260. ShowError("script:deletearray: illegal scope\n");
  5261. script_reportdata(data);
  5262. st->state = END;
  5263. return 1;// not supported
  5264. }
  5265. if( not_server_variable(*name) )
  5266. {
  5267. sd = script_rid2sd(st);
  5268. if( sd == NULL )
  5269. return 0;// no player attached
  5270. }
  5271. end = SCRIPT_MAX_ARRAYSIZE;
  5272. if( start >= end )
  5273. return 0;// nothing to free
  5274. if( script_hasdata(st,3) )
  5275. {
  5276. int count = script_getnum(st, 3);
  5277. if( count > end - start )
  5278. count = end - start;
  5279. if( count <= 0 )
  5280. return 0;// nothing to free
  5281. // move rest of the elements backward
  5282. for( ; start + count < end; ++start )
  5283. {
  5284. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5285. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5286. script_removetop(st, -1, 0);
  5287. }
  5288. }
  5289. // clear the rest of the array
  5290. if( is_string_variable(name) )
  5291. {
  5292. for( ; start < end; ++start )
  5293. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5294. }
  5295. else
  5296. {
  5297. for( ; start < end; ++start )
  5298. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5299. }
  5300. return SCRIPT_CMD_SUCCESS;
  5301. }
  5302. /// Returns a reference to the target index of the array variable.
  5303. /// Equivalent to var[index].
  5304. ///
  5305. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5306. BUILDIN_FUNC(getelementofarray)
  5307. {
  5308. struct script_data* data;
  5309. const char* name;
  5310. int32 id;
  5311. int i;
  5312. data = script_getdata(st, 2);
  5313. if( !data_isreference(data) )
  5314. {
  5315. ShowError("script:getelementofarray: not a variable\n");
  5316. script_reportdata(data);
  5317. script_pushnil(st);
  5318. st->state = END;
  5319. return 1;// not a variable
  5320. }
  5321. id = reference_getid(data);
  5322. name = reference_getname(data);
  5323. if( not_array_variable(*name) )
  5324. {
  5325. ShowError("script:getelementofarray: illegal scope\n");
  5326. script_reportdata(data);
  5327. script_pushnil(st);
  5328. st->state = END;
  5329. return 1;// not supported
  5330. }
  5331. i = script_getnum(st, 3);
  5332. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  5333. {
  5334. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5335. script_reportdata(data);
  5336. script_pushnil(st);
  5337. st->state = END;
  5338. return 1;// out of range
  5339. }
  5340. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5341. return SCRIPT_CMD_SUCCESS;
  5342. }
  5343. /////////////////////////////////////////////////////////////////////
  5344. /// ...
  5345. ///
  5346. /*==========================================
  5347. *
  5348. *------------------------------------------*/
  5349. BUILDIN_FUNC(setlook)
  5350. {
  5351. int type,val;
  5352. TBL_PC* sd;
  5353. type=script_getnum(st,2);
  5354. val=script_getnum(st,3);
  5355. sd = script_rid2sd(st);
  5356. if( sd == NULL )
  5357. return 0;
  5358. pc_changelook(sd,type,val);
  5359. return SCRIPT_CMD_SUCCESS;
  5360. }
  5361. BUILDIN_FUNC(changelook)
  5362. { // As setlook but only client side
  5363. int type,val;
  5364. TBL_PC* sd;
  5365. type=script_getnum(st,2);
  5366. val=script_getnum(st,3);
  5367. sd = script_rid2sd(st);
  5368. if( sd == NULL )
  5369. return 0;
  5370. clif_changelook(&sd->bl,type,val);
  5371. return SCRIPT_CMD_SUCCESS;
  5372. }
  5373. /*==========================================
  5374. *
  5375. *------------------------------------------*/
  5376. BUILDIN_FUNC(cutin)
  5377. {
  5378. TBL_PC* sd;
  5379. sd = script_rid2sd(st);
  5380. if( sd == NULL )
  5381. return 0;
  5382. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5383. return SCRIPT_CMD_SUCCESS;
  5384. }
  5385. /*==========================================
  5386. *
  5387. *------------------------------------------*/
  5388. BUILDIN_FUNC(viewpoint)
  5389. {
  5390. int type,x,y,id,color;
  5391. TBL_PC* sd;
  5392. type=script_getnum(st,2);
  5393. x=script_getnum(st,3);
  5394. y=script_getnum(st,4);
  5395. id=script_getnum(st,5);
  5396. color=script_getnum(st,6);
  5397. sd = script_rid2sd(st);
  5398. if( sd == NULL )
  5399. return 0;
  5400. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5401. return SCRIPT_CMD_SUCCESS;
  5402. }
  5403. /*==========================================
  5404. * countitem(nameID)
  5405. * returns number of items in inventory
  5406. *==========================================
  5407. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  5408. * returns number of items that meet the conditions
  5409. *------------------------------------------*/
  5410. BUILDIN_FUNC(countitem)
  5411. {
  5412. int i, count = 0;
  5413. struct item_data* id = NULL;
  5414. struct script_data* data;
  5415. TBL_PC* sd = script_rid2sd(st);
  5416. if (!sd) {
  5417. script_pushint(st,0);
  5418. return 0;
  5419. }
  5420. data = script_getdata(st,2);
  5421. get_val(st, data); // Convert into value in case of a variable
  5422. if( data_isstring(data) ) // item name
  5423. id = itemdb_searchname(conv_str(st, data));
  5424. else // item id
  5425. id = itemdb_exists(conv_num(st, data));
  5426. if( id == NULL ) {
  5427. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5428. script_pushint(st,0);
  5429. return 1;
  5430. }
  5431. if(script_lastdata(st) == 2) { // For countitem() function
  5432. int nameid = id->nameid;
  5433. for(i = 0; i < MAX_INVENTORY; i++)
  5434. if(sd->status.inventory[i].nameid == nameid)
  5435. count += sd->status.inventory[i].amount;
  5436. } else { // For countitem2() function
  5437. int nameid, iden, ref, attr, c1, c2, c3, c4;
  5438. nameid = id->nameid;
  5439. iden = script_getnum(st,3);
  5440. ref = script_getnum(st,4);
  5441. attr = script_getnum(st,5);
  5442. c1 = (short)script_getnum(st,6);
  5443. c2 = (short)script_getnum(st,7);
  5444. c3 = (short)script_getnum(st,8);
  5445. c4 = (short)script_getnum(st,9);
  5446. for(i = 0; i < MAX_INVENTORY; i++)
  5447. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  5448. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  5449. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  5450. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  5451. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  5452. sd->status.inventory[i].card[3] == c4 )
  5453. count += sd->status.inventory[i].amount;
  5454. }
  5455. script_pushint(st,count);
  5456. return SCRIPT_CMD_SUCCESS;
  5457. }
  5458. int checkweight_sub(TBL_PC *sd,int nbargs,int32 *eitemid,int32 *eamount)
  5459. {
  5460. struct item_data* id = NULL;
  5461. int nameid,amount;
  5462. uint16 amount2=0,slots,weight=0,i;
  5463. slots = pc_inventoryblank(sd); //nb of empty slot
  5464. for(i=0; i<nbargs; i++) {
  5465. if(!eitemid[i])
  5466. continue;
  5467. id = itemdb_exists(eitemid[i]);
  5468. if( id == NULL ) {
  5469. ShowError("checkweight_sub: Invalid item '%d'.\n", eitemid[i]);
  5470. return 0;
  5471. }
  5472. nameid = id->nameid;
  5473. amount = eamount[i];
  5474. if( amount < 1 ) {
  5475. ShowError("checkweight_sub: Invalid amount '%d'.\n", eamount[i]);
  5476. return 0;
  5477. }
  5478. weight += (id->weight)*amount; //total weight for all chk
  5479. if( weight + sd->weight > sd->max_weight ) // too heavy
  5480. return 0;
  5481. switch( pc_checkadditem(sd, nameid, amount) ) {
  5482. case CHKADDITEM_EXIST: // item is already in inventory, but there is still space for the requested amount
  5483. break;
  5484. case CHKADDITEM_NEW:
  5485. if( itemdb_isstackable(nameid) )
  5486. amount2++; // stackable
  5487. else
  5488. amount2 += amount; // non-stackable
  5489. if( slots < amount2)
  5490. return 0;
  5491. break;
  5492. case CHKADDITEM_OVERAMOUNT:
  5493. return 0;
  5494. }
  5495. }
  5496. return 1;
  5497. }
  5498. /*==========================================
  5499. * Check if item with this amount can fit in inventory
  5500. * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY)
  5501. * Return
  5502. * 0 : fail
  5503. * 1 : success (npc side only)
  5504. *------------------------------------------*/
  5505. BUILDIN_FUNC(checkweight)
  5506. {
  5507. struct map_session_data* sd;
  5508. struct script_data* data;
  5509. struct item_data* id = NULL;
  5510. int32 nameid[SCRIPT_MAX_ARRAYSIZE], amount[SCRIPT_MAX_ARRAYSIZE];
  5511. uint16 nbargs,i,j=0;
  5512. if( ( sd = script_rid2sd(st) ) == NULL )
  5513. return 0;
  5514. nbargs = script_lastdata(st)+1;
  5515. if(nbargs%2) {
  5516. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5517. script_pushint(st,0);
  5518. return 1;
  5519. }
  5520. for(i=2; i<nbargs; i=i+2) {
  5521. data = script_getdata(st,i);
  5522. get_val(st, data); // Convert into value in case of a variable
  5523. if( data_isstring(data) ) // item name
  5524. id = itemdb_searchname(conv_str(st, data));
  5525. else // item id
  5526. id = itemdb_exists(conv_num(st, data));
  5527. if( id == NULL ) {
  5528. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5529. script_pushint(st,0);
  5530. return 1;
  5531. }
  5532. nameid[j] = id->nameid;
  5533. amount[j] = script_getnum(st,i+1);
  5534. j++;
  5535. }
  5536. script_pushint(st,checkweight_sub(sd,(nbargs-2)/2,nameid,amount));
  5537. return SCRIPT_CMD_SUCCESS;
  5538. }
  5539. BUILDIN_FUNC(checkweight2){
  5540. //variable sub checkweight
  5541. int32 nameid[SCRIPT_MAX_ARRAYSIZE], amount[SCRIPT_MAX_ARRAYSIZE], i;
  5542. //variable for array parsing
  5543. struct script_data* data_it;
  5544. struct script_data* data_nb;
  5545. const char* name_it;
  5546. const char* name_nb;
  5547. int32 id_it, id_nb;
  5548. int32 idx_it, idx_nb;
  5549. int nb_it, nb_nb; //array size
  5550. TBL_PC *sd = script_rid2sd(st);
  5551. nullpo_retr(1,sd);
  5552. data_it = script_getdata(st, 2);
  5553. data_nb = script_getdata(st, 3);
  5554. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5555. ShowError("buildin_checkweight2: parameter not a variable\n");
  5556. script_pushint(st,0);
  5557. return 1;// not a variable
  5558. }
  5559. id_it = reference_getid(data_it);
  5560. id_nb = reference_getid(data_nb);
  5561. idx_it = reference_getindex(data_it);
  5562. idx_nb = reference_getindex(data_nb);
  5563. name_it = reference_getname(data_it);
  5564. name_nb = reference_getname(data_nb);
  5565. if( not_array_variable(*name_it) || not_array_variable(*name_nb)) {
  5566. ShowError("buildin_checkweight2: illegal scope\n");
  5567. script_pushint(st,0);
  5568. return 1;// not supported
  5569. }
  5570. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5571. ShowError("buildin_checkweight2: illegal type, need int\n");
  5572. script_pushint(st,0);
  5573. return 1;// not supported
  5574. }
  5575. nb_it = getarraysize(st, id_it, idx_it, 0, reference_getref(data_it));
  5576. nb_nb = getarraysize(st, id_nb, idx_nb, 0, reference_getref(data_nb));
  5577. if(nb_it != nb_nb) {
  5578. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5579. script_pushint(st,0);
  5580. return 1;
  5581. }
  5582. for(i=0; i<nb_it; i++) {
  5583. nameid[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5584. script_removetop(st, -1, 0);
  5585. amount[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5586. script_removetop(st, -1, 0);
  5587. } //end loop DO NOT break it prematurly we need to depop all stack
  5588. script_pushint(st,checkweight_sub(sd,nb_it,nameid,amount)); //push result of sub to script
  5589. return SCRIPT_CMD_SUCCESS;
  5590. }
  5591. /*==========================================
  5592. * getitem <item id>,<amount>{,<account ID>};
  5593. * getitem "<item name>",<amount>{,<account ID>};
  5594. *
  5595. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5596. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5597. * Type:
  5598. * 1 - Account Bound
  5599. * 2 - Guild Bound
  5600. * 3 - Party Bound
  5601. * 4 - Character Bound
  5602. *------------------------------------------*/
  5603. BUILDIN_FUNC(getitem)
  5604. {
  5605. int nameid,amount,get_count,i,flag = 0;
  5606. struct item it;
  5607. TBL_PC *sd;
  5608. struct script_data *data;
  5609. data=script_getdata(st,2);
  5610. get_val(st,data);
  5611. if( data_isstring(data) ) {// "<item name>"
  5612. const char *name=conv_str(st,data);
  5613. struct item_data *item_data = itemdb_searchname(name);
  5614. if( item_data == NULL ){
  5615. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5616. return 1; //No item created.
  5617. }
  5618. nameid=item_data->nameid;
  5619. } else if( data_isint(data) ) {// <item id>
  5620. nameid=conv_num(st,data);
  5621. //Violet Box, Blue Box, etc - random item pick
  5622. if( nameid < 0 ) {
  5623. nameid = -nameid;
  5624. flag = 1;
  5625. }
  5626. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5627. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5628. return 1; //No item created.
  5629. }
  5630. } else {
  5631. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5632. return 1;
  5633. }
  5634. // <amount>
  5635. if( (amount=script_getnum(st,3)) <= 0)
  5636. return 0; //return if amount <=0, skip the useles iteration
  5637. memset(&it,0,sizeof(it));
  5638. it.nameid=nameid;
  5639. if(!flag)
  5640. it.identify=1;
  5641. else
  5642. it.identify=itemdb_isidentified(nameid);
  5643. if( !strcmp(script_getfuncname(st),"getitembound") ) {
  5644. char bound = script_getnum(st,4);
  5645. if( bound < 1 || bound > 4) { //Not a correct bound type
  5646. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5647. return 1;
  5648. }
  5649. it.bound = bound;
  5650. if( script_hasdata(st,5) )
  5651. sd=map_id2sd(script_getnum(st,5));
  5652. else
  5653. sd=script_rid2sd(st); // Attached player
  5654. } else if( script_hasdata(st,4) )
  5655. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5656. else
  5657. sd=script_rid2sd(st); // Attached player
  5658. if( sd == NULL ) // no target
  5659. return 0;
  5660. //Check if it's stackable.
  5661. if (!itemdb_isstackable(nameid))
  5662. get_count = 1;
  5663. else
  5664. get_count = amount;
  5665. for (i = 0; i < amount; i += get_count)
  5666. {
  5667. // if not pet egg
  5668. if (!pet_create_egg(sd, nameid))
  5669. {
  5670. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5671. {
  5672. clif_additem(sd, 0, 0, flag);
  5673. if( pc_candrop(sd,&it) )
  5674. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5675. }
  5676. }
  5677. }
  5678. return SCRIPT_CMD_SUCCESS;
  5679. }
  5680. /*==========================================
  5681. *
  5682. *------------------------------------------*/
  5683. BUILDIN_FUNC(getitem2)
  5684. {
  5685. int nameid,amount,get_count,i,flag = 0;
  5686. int iden,ref,attr,c1,c2,c3,c4;
  5687. char bound=0;
  5688. struct item_data *item_data;
  5689. struct item item_tmp;
  5690. TBL_PC *sd;
  5691. struct script_data *data;
  5692. if( !strcmp(script_getfuncname(st),"getitembound2") ) {
  5693. bound = script_getnum(st,11);
  5694. if( bound < 1 || bound > 3) { //Not a correct bound type
  5695. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  5696. return 1;
  5697. }
  5698. if( script_hasdata(st,12) )
  5699. sd=map_id2sd(script_getnum(st,12));
  5700. else
  5701. sd=script_rid2sd(st); // Attached player
  5702. } else if( script_hasdata(st,11) )
  5703. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5704. else
  5705. sd=script_rid2sd(st); // Attached player
  5706. if( sd == NULL ) // no target
  5707. return 0;
  5708. data=script_getdata(st,2);
  5709. get_val(st,data);
  5710. if( data_isstring(data) ) {
  5711. const char *name=conv_str(st,data);
  5712. struct item_data *item_data = itemdb_searchname(name);
  5713. if( item_data )
  5714. nameid=item_data->nameid;
  5715. else
  5716. nameid=UNKNOWN_ITEM_ID;
  5717. } else
  5718. nameid=conv_num(st,data);
  5719. amount=script_getnum(st,3);
  5720. iden=script_getnum(st,4);
  5721. ref=script_getnum(st,5);
  5722. attr=script_getnum(st,6);
  5723. c1=(short)script_getnum(st,7);
  5724. c2=(short)script_getnum(st,8);
  5725. c3=(short)script_getnum(st,9);
  5726. c4=(short)script_getnum(st,10);
  5727. if(nameid<0) { // Invalide nameid
  5728. nameid = -nameid;
  5729. flag = 1;
  5730. }
  5731. if(nameid > 0) {
  5732. memset(&item_tmp,0,sizeof(item_tmp));
  5733. item_data=itemdb_exists(nameid);
  5734. if (item_data == NULL)
  5735. return -1;
  5736. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR || item_data->type==IT_SHADOWGEAR ) {
  5737. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5738. }
  5739. else if(item_data->type==IT_PETEGG) {
  5740. iden = 1;
  5741. ref = 0;
  5742. }
  5743. else {
  5744. iden = 1;
  5745. ref = attr = 0;
  5746. }
  5747. item_tmp.nameid=nameid;
  5748. if(!flag)
  5749. item_tmp.identify=iden;
  5750. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR || item_data->type==IT_SHADOWGEAR )
  5751. item_tmp.identify=0;
  5752. item_tmp.refine=ref;
  5753. item_tmp.attribute=attr;
  5754. item_tmp.card[0]=(short)c1;
  5755. item_tmp.card[1]=(short)c2;
  5756. item_tmp.card[2]=(short)c3;
  5757. item_tmp.card[3]=(short)c4;
  5758. item_tmp.bound=bound;
  5759. //Check if it's stackable.
  5760. if (!itemdb_isstackable(nameid))
  5761. get_count = 1;
  5762. else
  5763. get_count = amount;
  5764. for (i = 0; i < amount; i += get_count)
  5765. {
  5766. // if not pet egg
  5767. if (!pet_create_egg(sd, nameid))
  5768. {
  5769. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5770. {
  5771. clif_additem(sd, 0, 0, flag);
  5772. if( pc_candrop(sd,&item_tmp) )
  5773. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5774. }
  5775. }
  5776. }
  5777. }
  5778. return SCRIPT_CMD_SUCCESS;
  5779. }
  5780. /*==========================================
  5781. * rentitem <item id>,<seconds>
  5782. * rentitem "<item name>",<seconds>
  5783. *------------------------------------------*/
  5784. BUILDIN_FUNC(rentitem)
  5785. {
  5786. struct map_session_data *sd;
  5787. struct script_data *data;
  5788. struct item it;
  5789. int seconds;
  5790. int nameid = 0, flag;
  5791. data = script_getdata(st,2);
  5792. get_val(st,data);
  5793. if( (sd = script_rid2sd(st)) == NULL )
  5794. return 0;
  5795. if( data_isstring(data) )
  5796. {
  5797. const char *name = conv_str(st,data);
  5798. struct item_data *itd = itemdb_searchname(name);
  5799. if( itd == NULL )
  5800. {
  5801. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5802. return 1;
  5803. }
  5804. nameid = itd->nameid;
  5805. }
  5806. else if( data_isint(data) )
  5807. {
  5808. nameid = conv_num(st,data);
  5809. if( nameid <= 0 || !itemdb_exists(nameid) )
  5810. {
  5811. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5812. return 1;
  5813. }
  5814. }
  5815. else
  5816. {
  5817. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5818. return 1;
  5819. }
  5820. seconds = script_getnum(st,3);
  5821. memset(&it, 0, sizeof(it));
  5822. it.nameid = nameid;
  5823. it.identify = 1;
  5824. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5825. it.bound = 0;
  5826. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5827. {
  5828. clif_additem(sd, 0, 0, flag);
  5829. return 1;
  5830. }
  5831. return SCRIPT_CMD_SUCCESS;
  5832. }
  5833. /*==========================================
  5834. * gets an item with someone's name inscribed [Skotlex]
  5835. * getinscribeditem item_num, character_name
  5836. * Returned Qty is always 1, only works on equip-able
  5837. * equipment
  5838. *------------------------------------------*/
  5839. BUILDIN_FUNC(getnameditem)
  5840. {
  5841. int nameid;
  5842. struct item item_tmp;
  5843. TBL_PC *sd, *tsd;
  5844. struct script_data *data;
  5845. sd = script_rid2sd(st);
  5846. if (sd == NULL)
  5847. { //Player not attached!
  5848. script_pushint(st,0);
  5849. return 0;
  5850. }
  5851. data=script_getdata(st,2);
  5852. get_val(st,data);
  5853. if( data_isstring(data) ){
  5854. const char *name=conv_str(st,data);
  5855. struct item_data *item_data = itemdb_searchname(name);
  5856. if( item_data == NULL)
  5857. { //Failed
  5858. script_pushint(st,0);
  5859. return 0;
  5860. }
  5861. nameid = item_data->nameid;
  5862. }else
  5863. nameid = conv_num(st,data);
  5864. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5865. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5866. script_pushint(st,0);
  5867. return 0;
  5868. }
  5869. data=script_getdata(st,3);
  5870. get_val(st,data);
  5871. if( data_isstring(data) ) //Char Name
  5872. tsd=map_nick2sd(conv_str(st,data));
  5873. else //Char Id was given
  5874. tsd=map_charid2sd(conv_num(st,data));
  5875. if( tsd == NULL )
  5876. { //Failed
  5877. script_pushint(st,0);
  5878. return 0;
  5879. }
  5880. memset(&item_tmp,0,sizeof(item_tmp));
  5881. item_tmp.nameid=nameid;
  5882. item_tmp.amount=1;
  5883. item_tmp.identify=1;
  5884. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5885. item_tmp.card[2]=tsd->status.char_id;
  5886. item_tmp.card[3]=tsd->status.char_id >> 16;
  5887. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  5888. script_pushint(st,0);
  5889. return 0; //Failed to add item, we will not drop if they don't fit
  5890. }
  5891. script_pushint(st,1);
  5892. return SCRIPT_CMD_SUCCESS;
  5893. }
  5894. /*==========================================
  5895. * gets a random item ID from an item group [Skotlex]
  5896. * groupranditem <group_num>{,<sub_group>};
  5897. *------------------------------------------*/
  5898. BUILDIN_FUNC(grouprandomitem)
  5899. {
  5900. script_pushint(st,itemdb_searchrandomid(script_getnum(st,2),script_getnum(st,3)));
  5901. return SCRIPT_CMD_SUCCESS;
  5902. }
  5903. /*==========================================
  5904. *
  5905. *------------------------------------------*/
  5906. BUILDIN_FUNC(makeitem)
  5907. {
  5908. int nameid,amount,flag = 0;
  5909. int x,y,m;
  5910. const char *mapname;
  5911. struct item item_tmp;
  5912. struct script_data *data;
  5913. data=script_getdata(st,2);
  5914. get_val(st,data);
  5915. if( data_isstring(data) ){
  5916. const char *name=conv_str(st,data);
  5917. struct item_data *item_data = itemdb_searchname(name);
  5918. if( item_data )
  5919. nameid=item_data->nameid;
  5920. else
  5921. nameid=UNKNOWN_ITEM_ID;
  5922. }else
  5923. nameid=conv_num(st,data);
  5924. amount=script_getnum(st,3);
  5925. mapname =script_getstr(st,4);
  5926. x =script_getnum(st,5);
  5927. y =script_getnum(st,6);
  5928. if(strcmp(mapname,"this")==0)
  5929. {
  5930. TBL_PC *sd;
  5931. sd = script_rid2sd(st);
  5932. if (!sd) return 0; //Failed...
  5933. m=sd->bl.m;
  5934. } else
  5935. m=map_mapname2mapid(mapname);
  5936. if(nameid<0) {
  5937. nameid = -nameid;
  5938. flag = 1;
  5939. }
  5940. if(nameid > 0) {
  5941. memset(&item_tmp,0,sizeof(item_tmp));
  5942. item_tmp.nameid=nameid;
  5943. if(!flag)
  5944. item_tmp.identify=1;
  5945. else
  5946. item_tmp.identify=itemdb_isidentified(nameid);
  5947. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  5948. }
  5949. return SCRIPT_CMD_SUCCESS;
  5950. }
  5951. /// Counts / deletes the current item given by idx.
  5952. /// Used by buildin_delitem_search
  5953. /// Relies on all input data being already fully valid.
  5954. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5955. {
  5956. int delamount;
  5957. struct item* inv = &sd->status.inventory[idx];
  5958. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5959. if( delete_items )
  5960. {
  5961. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5962. {// delete associated pet
  5963. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5964. }
  5965. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  5966. }
  5967. amount[0]-= delamount;
  5968. }
  5969. /// Searches for item(s) and checks, if there is enough of them.
  5970. /// Used by delitem and delitem2
  5971. /// Relies on all input data being already fully valid.
  5972. /// @param exact_match will also match item attributes and cards, not just name id
  5973. /// @return true when all items could be deleted, false when there were not enough items to delete
  5974. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5975. {
  5976. bool delete_items = false;
  5977. int i, amount, important;
  5978. struct item* inv;
  5979. // prefer always non-equipped items
  5980. it->equip = 0;
  5981. // when searching for nameid only, prefer additionally
  5982. if( !exact_match )
  5983. {
  5984. // non-refined items
  5985. it->refine = 0;
  5986. // card-less items
  5987. memset(it->card, 0, sizeof(it->card));
  5988. }
  5989. for(;;)
  5990. {
  5991. amount = it->amount;
  5992. important = 0;
  5993. // 1st pass -- less important items / exact match
  5994. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5995. {
  5996. inv = &sd->status.inventory[i];
  5997. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5998. {// wrong/invalid item
  5999. continue;
  6000. }
  6001. if( inv->equip != it->equip || inv->refine != it->refine )
  6002. {// not matching attributes
  6003. important++;
  6004. continue;
  6005. }
  6006. if( exact_match )
  6007. {
  6008. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  6009. {// not matching exact attributes
  6010. continue;
  6011. }
  6012. }
  6013. else
  6014. {
  6015. if( sd->inventory_data[i]->type == IT_PETEGG )
  6016. {
  6017. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  6018. {// pet which cannot be deleted
  6019. continue;
  6020. }
  6021. }
  6022. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  6023. {// named/carded item
  6024. important++;
  6025. continue;
  6026. }
  6027. }
  6028. // count / delete item
  6029. buildin_delitem_delete(sd, i, &amount, delete_items);
  6030. }
  6031. // 2nd pass -- any matching item
  6032. if( amount == 0 || important == 0 )
  6033. {// either everything was already consumed or no items were skipped
  6034. ;
  6035. }
  6036. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  6037. {
  6038. inv = &sd->status.inventory[i];
  6039. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  6040. {// wrong/invalid item
  6041. continue;
  6042. }
  6043. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  6044. {// pet which cannot be deleted
  6045. continue;
  6046. }
  6047. if( exact_match )
  6048. {
  6049. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  6050. {// not matching attributes
  6051. continue;
  6052. }
  6053. }
  6054. // count / delete item
  6055. buildin_delitem_delete(sd, i, &amount, delete_items);
  6056. }
  6057. if( amount )
  6058. {// not enough items
  6059. return false;
  6060. }
  6061. else if( delete_items )
  6062. {// we are done with the work
  6063. return true;
  6064. }
  6065. else
  6066. {// get rid of the items now
  6067. delete_items = true;
  6068. }
  6069. }
  6070. }
  6071. /// Deletes items from the target/attached player.
  6072. /// Prioritizes ordinary items.
  6073. ///
  6074. /// delitem <item id>,<amount>{,<account id>}
  6075. /// delitem "<item name>",<amount>{,<account id>}
  6076. BUILDIN_FUNC(delitem)
  6077. {
  6078. TBL_PC *sd;
  6079. struct item it;
  6080. struct script_data *data;
  6081. if( script_hasdata(st,4) )
  6082. {
  6083. int account_id = script_getnum(st,4);
  6084. sd = map_id2sd(account_id); // <account id>
  6085. if( sd == NULL )
  6086. {
  6087. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  6088. st->state = END;
  6089. return 1;
  6090. }
  6091. }
  6092. else
  6093. {
  6094. sd = script_rid2sd(st);// attached player
  6095. if( sd == NULL )
  6096. return 0;
  6097. }
  6098. data = script_getdata(st,2);
  6099. get_val(st,data);
  6100. if( data_isstring(data) )
  6101. {
  6102. const char* item_name = conv_str(st,data);
  6103. struct item_data* id = itemdb_searchname(item_name);
  6104. if( id == NULL )
  6105. {
  6106. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  6107. st->state = END;
  6108. return 1;
  6109. }
  6110. it.nameid = id->nameid;// "<item name>"
  6111. }
  6112. else
  6113. {
  6114. it.nameid = conv_num(st,data);// <item id>
  6115. if( !itemdb_exists( it.nameid ) )
  6116. {
  6117. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6118. st->state = END;
  6119. return 1;
  6120. }
  6121. }
  6122. it.amount=script_getnum(st,3);
  6123. if( it.amount <= 0 )
  6124. return 0;// nothing to do
  6125. if( buildin_delitem_search(sd, &it, false) )
  6126. {// success
  6127. return SCRIPT_CMD_SUCCESS;
  6128. }
  6129. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6130. st->state = END;
  6131. st->mes_active = 0;
  6132. clif_scriptclose(sd, st->oid);
  6133. return SCRIPT_CMD_FAILURE;
  6134. }
  6135. /// Deletes items from the target/attached player.
  6136. ///
  6137. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6138. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6139. BUILDIN_FUNC(delitem2)
  6140. {
  6141. TBL_PC *sd;
  6142. struct item it;
  6143. struct script_data *data;
  6144. if( script_hasdata(st,11) )
  6145. {
  6146. int account_id = script_getnum(st,11);
  6147. sd = map_id2sd(account_id); // <account id>
  6148. if( sd == NULL )
  6149. {
  6150. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  6151. st->state = END;
  6152. return 1;
  6153. }
  6154. }
  6155. else
  6156. {
  6157. sd = script_rid2sd(st);// attached player
  6158. if( sd == NULL )
  6159. return 0;
  6160. }
  6161. data = script_getdata(st,2);
  6162. get_val(st,data);
  6163. if( data_isstring(data) )
  6164. {
  6165. const char* item_name = conv_str(st,data);
  6166. struct item_data* id = itemdb_searchname(item_name);
  6167. if( id == NULL )
  6168. {
  6169. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  6170. st->state = END;
  6171. return 1;
  6172. }
  6173. it.nameid = id->nameid;// "<item name>"
  6174. }
  6175. else
  6176. {
  6177. it.nameid = conv_num(st,data);// <item id>
  6178. if( !itemdb_exists( it.nameid ) )
  6179. {
  6180. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6181. st->state = END;
  6182. return 1;
  6183. }
  6184. }
  6185. it.amount=script_getnum(st,3);
  6186. it.identify=script_getnum(st,4);
  6187. it.refine=script_getnum(st,5);
  6188. it.attribute=script_getnum(st,6);
  6189. it.card[0]=(short)script_getnum(st,7);
  6190. it.card[1]=(short)script_getnum(st,8);
  6191. it.card[2]=(short)script_getnum(st,9);
  6192. it.card[3]=(short)script_getnum(st,10);
  6193. if( it.amount <= 0 )
  6194. return 0;// nothing to do
  6195. if( buildin_delitem_search(sd, &it, true) )
  6196. {// success
  6197. return SCRIPT_CMD_SUCCESS;
  6198. }
  6199. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6200. st->state = END;
  6201. st->mes_active = 0;
  6202. clif_scriptclose(sd, st->oid);
  6203. return SCRIPT_CMD_FAILURE;
  6204. }
  6205. /*==========================================
  6206. * Enables/Disables use of items while in an NPC [Skotlex]
  6207. *------------------------------------------*/
  6208. BUILDIN_FUNC(enableitemuse)
  6209. {
  6210. TBL_PC *sd;
  6211. sd=script_rid2sd(st);
  6212. if (sd)
  6213. st->npc_item_flag = sd->npc_item_flag = 1;
  6214. return SCRIPT_CMD_SUCCESS;
  6215. }
  6216. BUILDIN_FUNC(disableitemuse)
  6217. {
  6218. TBL_PC *sd;
  6219. sd=script_rid2sd(st);
  6220. if (sd)
  6221. st->npc_item_flag = sd->npc_item_flag = 0;
  6222. return SCRIPT_CMD_SUCCESS;
  6223. }
  6224. /*==========================================
  6225. * Returns a character's specified stat.
  6226. * Check pc_readparam for available options.
  6227. *------------------------------------------*/
  6228. BUILDIN_FUNC(readparam)
  6229. {
  6230. int type;
  6231. TBL_PC *sd;
  6232. type=script_getnum(st,2);
  6233. if( script_hasdata(st,3) )
  6234. sd=map_nick2sd(script_getstr(st,3));
  6235. else
  6236. sd=script_rid2sd(st);
  6237. if(sd==NULL){
  6238. script_pushint(st,-1);
  6239. return 0;
  6240. }
  6241. script_pushint(st,pc_readparam(sd,type));
  6242. return SCRIPT_CMD_SUCCESS;
  6243. }
  6244. /*==========================================
  6245. * Return charid identification
  6246. * return by @num :
  6247. * 0 : char_id
  6248. * 1 : party_id
  6249. * 2 : guild_id
  6250. * 3 : account_id
  6251. * 4 : bg_id
  6252. *------------------------------------------*/
  6253. BUILDIN_FUNC(getcharid)
  6254. {
  6255. int num;
  6256. TBL_PC *sd;
  6257. num = script_getnum(st,2);
  6258. if( script_hasdata(st,3) )
  6259. sd=map_nick2sd(script_getstr(st,3));
  6260. else
  6261. sd=script_rid2sd(st);
  6262. if(sd==NULL){
  6263. script_pushint(st,0); //return 0, according docs
  6264. return 0;
  6265. }
  6266. switch( num ) {
  6267. case 0: script_pushint(st,sd->status.char_id); break;
  6268. case 1: script_pushint(st,sd->status.party_id); break;
  6269. case 2: script_pushint(st,sd->status.guild_id); break;
  6270. case 3: script_pushint(st,sd->status.account_id); break;
  6271. case 4: script_pushint(st,sd->bg_id); break;
  6272. default:
  6273. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6274. script_pushint(st,0);
  6275. break;
  6276. }
  6277. return SCRIPT_CMD_SUCCESS;
  6278. }
  6279. /*==========================================
  6280. * returns the GID of an NPC
  6281. *------------------------------------------*/
  6282. BUILDIN_FUNC(getnpcid)
  6283. {
  6284. int num = script_getnum(st,2);
  6285. struct npc_data* nd = NULL;
  6286. if( script_hasdata(st,3) )
  6287. {// unique npc name
  6288. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6289. {
  6290. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6291. script_pushint(st,0);
  6292. return 1;
  6293. }
  6294. }
  6295. switch (num) {
  6296. case 0:
  6297. script_pushint(st,nd ? nd->bl.id : st->oid);
  6298. break;
  6299. default:
  6300. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6301. script_pushint(st,0);
  6302. return 1;
  6303. }
  6304. return SCRIPT_CMD_SUCCESS;
  6305. }
  6306. /*==========================================
  6307. * Return the name of the party_id
  6308. * null if not found
  6309. *------------------------------------------*/
  6310. BUILDIN_FUNC(getpartyname)
  6311. {
  6312. int party_id;
  6313. struct party_data* p;
  6314. party_id = script_getnum(st,2);
  6315. if( ( p = party_search(party_id) ) != NULL )
  6316. {
  6317. script_pushstrcopy(st,p->party.name);
  6318. }
  6319. else
  6320. {
  6321. script_pushconststr(st,"null");
  6322. }
  6323. return SCRIPT_CMD_SUCCESS;
  6324. }
  6325. /*==========================================
  6326. * Get the information of the members of a party by type
  6327. * @party_id, @type
  6328. * return by @type :
  6329. * - : nom des membres
  6330. * 1 : char_id des membres
  6331. * 2 : account_id des membres
  6332. *------------------------------------------*/
  6333. BUILDIN_FUNC(getpartymember)
  6334. {
  6335. struct party_data *p;
  6336. int i,j=0,type=0;
  6337. p=party_search(script_getnum(st,2));
  6338. if( script_hasdata(st,3) )
  6339. type=script_getnum(st,3);
  6340. if(p!=NULL){
  6341. for(i=0;i<MAX_PARTY;i++){
  6342. if(p->party.member[i].account_id){
  6343. switch (type) {
  6344. case 2:
  6345. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6346. break;
  6347. case 1:
  6348. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6349. break;
  6350. default:
  6351. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6352. }
  6353. j++;
  6354. }
  6355. }
  6356. }
  6357. mapreg_setreg(add_str("$@partymembercount"),j);
  6358. return SCRIPT_CMD_SUCCESS;
  6359. }
  6360. /*==========================================
  6361. * Retrieves party leader. if flag is specified,
  6362. * return some of the leader data. Otherwise, return name.
  6363. *------------------------------------------*/
  6364. BUILDIN_FUNC(getpartyleader)
  6365. {
  6366. int party_id, type = 0, i=0;
  6367. struct party_data *p;
  6368. party_id=script_getnum(st,2);
  6369. if( script_hasdata(st,3) )
  6370. type=script_getnum(st,3);
  6371. p=party_search(party_id);
  6372. if (p) //Search leader
  6373. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6374. if (!p || i == MAX_PARTY) { //leader not found
  6375. if (type)
  6376. script_pushint(st,-1);
  6377. else
  6378. script_pushconststr(st,"null");
  6379. return 0;
  6380. }
  6381. switch (type) {
  6382. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6383. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6384. case 3: script_pushint(st,p->party.member[i].class_); break;
  6385. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6386. case 5: script_pushint(st,p->party.member[i].lv); break;
  6387. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6388. }
  6389. return SCRIPT_CMD_SUCCESS;
  6390. }
  6391. /*==========================================
  6392. * Return the name of the @guild_id
  6393. * null if not found
  6394. *------------------------------------------*/
  6395. BUILDIN_FUNC(getguildname)
  6396. {
  6397. int guild_id;
  6398. struct guild* g;
  6399. guild_id = script_getnum(st,2);
  6400. if( ( g = guild_search(guild_id) ) != NULL )
  6401. script_pushstrcopy(st,g->name);
  6402. else
  6403. script_pushconststr(st,"null");
  6404. return SCRIPT_CMD_SUCCESS;
  6405. }
  6406. /*==========================================
  6407. * Return the name of the guild master of @guild_id
  6408. * null if not found
  6409. *------------------------------------------*/
  6410. BUILDIN_FUNC(getguildmaster)
  6411. {
  6412. int guild_id;
  6413. struct guild* g;
  6414. guild_id = script_getnum(st,2);
  6415. if( ( g = guild_search(guild_id) ) != NULL )
  6416. script_pushstrcopy(st,g->member[0].name);
  6417. else
  6418. script_pushconststr(st,"null");
  6419. return SCRIPT_CMD_SUCCESS;
  6420. }
  6421. BUILDIN_FUNC(getguildmasterid)
  6422. {
  6423. int guild_id;
  6424. struct guild* g;
  6425. guild_id = script_getnum(st,2);
  6426. if( ( g = guild_search(guild_id) ) != NULL )
  6427. script_pushint(st,g->member[0].char_id);
  6428. else
  6429. script_pushint(st,0);
  6430. return SCRIPT_CMD_SUCCESS;
  6431. }
  6432. /*==========================================
  6433. * Get char string information by type :
  6434. * Return by @type :
  6435. * 0 : char_name
  6436. * 1 : party_name or ""
  6437. * 2 : guild_name or ""
  6438. * 3 : map_name
  6439. * - : ""
  6440. *------------------------------------------*/
  6441. BUILDIN_FUNC(strcharinfo)
  6442. {
  6443. TBL_PC *sd;
  6444. int num;
  6445. struct guild* g;
  6446. struct party_data* p;
  6447. sd=script_rid2sd(st);
  6448. if (!sd) { //Avoid crashing....
  6449. script_pushconststr(st,"");
  6450. return 0;
  6451. }
  6452. num=script_getnum(st,2);
  6453. switch(num){
  6454. case 0:
  6455. script_pushstrcopy(st,sd->status.name);
  6456. break;
  6457. case 1:
  6458. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  6459. script_pushstrcopy(st,p->party.name);
  6460. } else {
  6461. script_pushconststr(st,"");
  6462. }
  6463. break;
  6464. case 2:
  6465. if( ( g = sd->guild ) != NULL ) {
  6466. script_pushstrcopy(st,g->name);
  6467. } else {
  6468. script_pushconststr(st,"");
  6469. }
  6470. break;
  6471. case 3:
  6472. script_pushconststr(st,map[sd->bl.m].name);
  6473. break;
  6474. default:
  6475. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6476. script_pushconststr(st,"");
  6477. break;
  6478. }
  6479. return SCRIPT_CMD_SUCCESS;
  6480. }
  6481. /*==========================================
  6482. * Get npc string information by type
  6483. * return by @type:
  6484. * 0 : name
  6485. * 1 : str#
  6486. * 2 : #str
  6487. * 3 : ::str
  6488. * 4 : map name
  6489. *------------------------------------------*/
  6490. BUILDIN_FUNC(strnpcinfo)
  6491. {
  6492. TBL_NPC* nd;
  6493. int num;
  6494. char *buf,*name=NULL;
  6495. nd = map_id2nd(st->oid);
  6496. if (!nd) {
  6497. script_pushconststr(st, "");
  6498. return 0;
  6499. }
  6500. num = script_getnum(st,2);
  6501. switch(num){
  6502. case 0: // display name
  6503. name = aStrdup(nd->name);
  6504. break;
  6505. case 1: // visible part of display name
  6506. if((buf = strchr(nd->name,'#')) != NULL)
  6507. {
  6508. name = aStrdup(nd->name);
  6509. name[buf - nd->name] = 0;
  6510. } else // Return the name, there is no '#' present
  6511. name = aStrdup(nd->name);
  6512. break;
  6513. case 2: // # fragment
  6514. if((buf = strchr(nd->name,'#')) != NULL)
  6515. name = aStrdup(buf+1);
  6516. break;
  6517. case 3: // unique name
  6518. name = aStrdup(nd->exname);
  6519. break;
  6520. case 4: // map name
  6521. name = aStrdup(map[nd->bl.m].name);
  6522. break;
  6523. }
  6524. if(name)
  6525. script_pushstr(st, name);
  6526. else
  6527. script_pushconststr(st, "");
  6528. return SCRIPT_CMD_SUCCESS;
  6529. }
  6530. // aegis->athena slot position conversion table
  6531. static unsigned int equip[] = {EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_GARMENT,EQP_SHOES,EQP_ACC_L,EQP_ACC_R,EQP_HEAD_MID,EQP_HEAD_LOW,EQP_COSTUME_HEAD_LOW,EQP_COSTUME_HEAD_MID,EQP_COSTUME_HEAD_TOP,EQP_COSTUME_GARMENT,EQP_AMMO,EQP_SHADOW_ARMOR,EQP_SHADOW_WEAPON,EQP_SHADOW_SHIELD,EQP_SHADOW_SHOES,EQP_SHADOW_ACC_R,EQP_SHADOW_ACC_L};
  6532. /*==========================================
  6533. * GetEquipID(Pos); Pos: 1-14
  6534. *------------------------------------------*/
  6535. BUILDIN_FUNC(getequipid)
  6536. {
  6537. int i, num;
  6538. TBL_PC* sd;
  6539. struct item_data* item;
  6540. sd = script_rid2sd(st);
  6541. if( sd == NULL )
  6542. return 0;
  6543. num = script_getnum(st,2) - 1;
  6544. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6545. {
  6546. script_pushint(st,-1);
  6547. return 0;
  6548. }
  6549. // get inventory position of item
  6550. i = pc_checkequip(sd,equip[num]);
  6551. if( i < 0 )
  6552. {
  6553. script_pushint(st,-1);
  6554. return 0;
  6555. }
  6556. item = sd->inventory_data[i];
  6557. if( item != 0 )
  6558. script_pushint(st,item->nameid);
  6559. else
  6560. script_pushint(st,0);
  6561. return SCRIPT_CMD_SUCCESS;
  6562. }
  6563. /*==========================================
  6564. * Get the equipement name at pos
  6565. * return item jname or ""
  6566. *------------------------------------------*/
  6567. BUILDIN_FUNC(getequipname)
  6568. {
  6569. int i, num;
  6570. TBL_PC* sd;
  6571. struct item_data* item;
  6572. sd = script_rid2sd(st);
  6573. if( sd == NULL )
  6574. return 0;
  6575. num = script_getnum(st,2) - 1;
  6576. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6577. {
  6578. script_pushconststr(st,"");
  6579. return 0;
  6580. }
  6581. // get inventory position of item
  6582. i = pc_checkequip(sd,equip[num]);
  6583. if( i < 0 )
  6584. {
  6585. script_pushconststr(st,"");
  6586. return 0;
  6587. }
  6588. item = sd->inventory_data[i];
  6589. if( item != 0 )
  6590. script_pushstrcopy(st,item->jname);
  6591. else
  6592. script_pushconststr(st,"");
  6593. return SCRIPT_CMD_SUCCESS;
  6594. }
  6595. /*==========================================
  6596. * getbrokenid [Valaris]
  6597. *------------------------------------------*/
  6598. BUILDIN_FUNC(getbrokenid)
  6599. {
  6600. int i,num,id=0,brokencounter=0;
  6601. TBL_PC *sd;
  6602. sd = script_rid2sd(st);
  6603. if( sd == NULL )
  6604. return 0;
  6605. num=script_getnum(st,2);
  6606. for(i=0; i<MAX_INVENTORY; i++) {
  6607. if(sd->status.inventory[i].attribute){
  6608. brokencounter++;
  6609. if(num==brokencounter){
  6610. id=sd->status.inventory[i].nameid;
  6611. break;
  6612. }
  6613. }
  6614. }
  6615. script_pushint(st,id);
  6616. return SCRIPT_CMD_SUCCESS;
  6617. }
  6618. /*==========================================
  6619. * repair [Valaris]
  6620. *------------------------------------------*/
  6621. BUILDIN_FUNC(repair)
  6622. {
  6623. int i,num;
  6624. int repaircounter=0;
  6625. TBL_PC *sd;
  6626. sd = script_rid2sd(st);
  6627. if( sd == NULL )
  6628. return 0;
  6629. num=script_getnum(st,2);
  6630. for(i=0; i<MAX_INVENTORY; i++) {
  6631. if(sd->status.inventory[i].attribute){
  6632. repaircounter++;
  6633. if(num==repaircounter){
  6634. sd->status.inventory[i].attribute=0;
  6635. clif_equiplist(sd);
  6636. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6637. clif_misceffect(&sd->bl, 3);
  6638. break;
  6639. }
  6640. }
  6641. }
  6642. return SCRIPT_CMD_SUCCESS;
  6643. }
  6644. /*==========================================
  6645. * repairall
  6646. *------------------------------------------*/
  6647. BUILDIN_FUNC(repairall)
  6648. {
  6649. int i, repaircounter = 0;
  6650. TBL_PC *sd;
  6651. sd = script_rid2sd(st);
  6652. if(sd == NULL)
  6653. return 0;
  6654. for(i = 0; i < MAX_INVENTORY; i++)
  6655. {
  6656. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  6657. {
  6658. sd->status.inventory[i].attribute = 0;
  6659. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  6660. repaircounter++;
  6661. }
  6662. }
  6663. if(repaircounter)
  6664. {
  6665. clif_misceffect(&sd->bl, 3);
  6666. clif_equiplist(sd);
  6667. }
  6668. return SCRIPT_CMD_SUCCESS;
  6669. }
  6670. /*==========================================
  6671. * Chk if player have something equiped at pos
  6672. *------------------------------------------*/
  6673. BUILDIN_FUNC(getequipisequiped)
  6674. {
  6675. int i = -1,num;
  6676. TBL_PC *sd;
  6677. num = script_getnum(st,2);
  6678. sd = script_rid2sd(st);
  6679. if( sd == NULL )
  6680. return 0;
  6681. if (num > 0 && num <= ARRAYLENGTH(equip))
  6682. i=pc_checkequip(sd,equip[num-1]);
  6683. if(i >= 0)
  6684. script_pushint(st,1);
  6685. else
  6686. script_pushint(st,0);
  6687. return SCRIPT_CMD_SUCCESS;
  6688. }
  6689. /*==========================================
  6690. * Chk if the player have something equiped at pos
  6691. * if so chk if this item ain't marked not refinable or rental
  6692. * return (npc)
  6693. * 1 : true
  6694. * 0 : false
  6695. *------------------------------------------*/
  6696. BUILDIN_FUNC(getequipisenableref)
  6697. {
  6698. int i = -1,num;
  6699. TBL_PC *sd;
  6700. num = script_getnum(st,2);
  6701. sd = script_rid2sd(st);
  6702. if( sd == NULL )
  6703. return 0;
  6704. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6705. i = pc_checkequip(sd,equip[num-1]);
  6706. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6707. script_pushint(st,1);
  6708. else
  6709. script_pushint(st,0);
  6710. return SCRIPT_CMD_SUCCESS;
  6711. }
  6712. /*==========================================
  6713. * Get the item refined value at pos
  6714. * return (npc)
  6715. * x : refine amount
  6716. * 0 : false (not refined)
  6717. *------------------------------------------*/
  6718. BUILDIN_FUNC(getequiprefinerycnt)
  6719. {
  6720. int i = -1,num;
  6721. TBL_PC *sd;
  6722. num = script_getnum(st,2);
  6723. sd = script_rid2sd(st);
  6724. if( sd == NULL )
  6725. return 0;
  6726. if (num > 0 && num <= ARRAYLENGTH(equip))
  6727. i=pc_checkequip(sd,equip[num-1]);
  6728. if(i >= 0)
  6729. script_pushint(st,sd->status.inventory[i].refine);
  6730. else
  6731. script_pushint(st,0);
  6732. return SCRIPT_CMD_SUCCESS;
  6733. }
  6734. /*==========================================
  6735. * Get the weapon level value at pos
  6736. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  6737. * return (npc)
  6738. * x : weapon level
  6739. * 0 : false
  6740. *------------------------------------------*/
  6741. BUILDIN_FUNC(getequipweaponlv)
  6742. {
  6743. int i = -1,num;
  6744. TBL_PC *sd;
  6745. num = script_getnum(st,2);
  6746. sd = script_rid2sd(st);
  6747. if( sd == NULL )
  6748. return 0;
  6749. if (num > 0 && num <= ARRAYLENGTH(equip))
  6750. i=pc_checkequip(sd,equip[num-1]);
  6751. if(i >= 0 && sd->inventory_data[i])
  6752. script_pushint(st,sd->inventory_data[i]->wlv);
  6753. else
  6754. script_pushint(st,0);
  6755. return SCRIPT_CMD_SUCCESS;
  6756. }
  6757. /*==========================================
  6758. * Get the item refine chance (from refine.txt) for item at pos
  6759. * return (npc)
  6760. * x : refine chance
  6761. * 0 : false (max refine level or unequip..)
  6762. *------------------------------------------*/
  6763. BUILDIN_FUNC(getequippercentrefinery)
  6764. {
  6765. int i = -1,num;
  6766. TBL_PC *sd;
  6767. num = script_getnum(st,2);
  6768. sd = script_rid2sd(st);
  6769. if( sd == NULL )
  6770. return 0;
  6771. if (num > 0 && num <= ARRAYLENGTH(equip))
  6772. i=pc_checkequip(sd,equip[num-1]);
  6773. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6774. script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  6775. else
  6776. script_pushint(st,0);
  6777. return SCRIPT_CMD_SUCCESS;
  6778. }
  6779. /*==========================================
  6780. * Refine +1 item at pos and log and display refine
  6781. *------------------------------------------*/
  6782. BUILDIN_FUNC(successrefitem)
  6783. {
  6784. int i=-1,num,ep;
  6785. TBL_PC *sd;
  6786. num = script_getnum(st,2);
  6787. sd = script_rid2sd(st);
  6788. if( sd == NULL )
  6789. return 0;
  6790. if (num > 0 && num <= ARRAYLENGTH(equip))
  6791. i=pc_checkequip(sd,equip[num-1]);
  6792. if(i >= 0) {
  6793. ep=sd->status.inventory[i].equip;
  6794. //Logs items, got from (N)PC scripts [Lupus]
  6795. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6796. sd->status.inventory[i].refine++;
  6797. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6798. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6799. clif_delitem(sd,i,1,3);
  6800. //Logs items, got from (N)PC scripts [Lupus]
  6801. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6802. clif_additem(sd,i,1,0);
  6803. pc_equipitem(sd,i,ep);
  6804. clif_misceffect(&sd->bl,3);
  6805. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6806. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6807. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6808. ){ // Fame point system [DracoRPG]
  6809. switch (sd->inventory_data[i]->wlv){
  6810. case 1:
  6811. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6812. break;
  6813. case 2:
  6814. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6815. break;
  6816. case 3:
  6817. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6818. break;
  6819. }
  6820. }
  6821. }
  6822. return SCRIPT_CMD_SUCCESS;
  6823. }
  6824. /*==========================================
  6825. * Show a failed Refine +1 attempt
  6826. *------------------------------------------*/
  6827. BUILDIN_FUNC(failedrefitem)
  6828. {
  6829. int i=-1,num;
  6830. TBL_PC *sd;
  6831. num = script_getnum(st,2);
  6832. sd = script_rid2sd(st);
  6833. if( sd == NULL )
  6834. return 0;
  6835. if (num > 0 && num <= ARRAYLENGTH(equip))
  6836. i=pc_checkequip(sd,equip[num-1]);
  6837. if(i >= 0) {
  6838. sd->status.inventory[i].refine = 0;
  6839. pc_unequipitem(sd,i,3); //recalculate bonus
  6840. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  6841. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6842. clif_misceffect(&sd->bl,2); // display failure effect
  6843. }
  6844. return SCRIPT_CMD_SUCCESS;
  6845. }
  6846. /*==========================================
  6847. * Downgrades an Equipment Part by -1 . [Masao]
  6848. *------------------------------------------*/
  6849. BUILDIN_FUNC(downrefitem)
  6850. {
  6851. int i = -1,num,ep,down = 1;
  6852. TBL_PC *sd;
  6853. sd = script_rid2sd(st);
  6854. if( sd == NULL )
  6855. return 0;
  6856. num = script_getnum(st,2);
  6857. if( script_hasdata(st, 3) )
  6858. down = script_getnum(st, 3);
  6859. if (num > 0 && num <= ARRAYLENGTH(equip))
  6860. i = pc_checkequip(sd,equip[num-1]);
  6861. if(i >= 0) {
  6862. ep = sd->status.inventory[i].equip;
  6863. //Logs items, got from (N)PC scripts [Lupus]
  6864. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6865. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6866. sd->status.inventory[i].refine -= down;
  6867. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  6868. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine);
  6869. clif_delitem(sd,i,1,3);
  6870. //Logs items, got from (N)PC scripts [Lupus]
  6871. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6872. clif_additem(sd,i,1,0);
  6873. pc_equipitem(sd,i,ep);
  6874. clif_misceffect(&sd->bl,2);
  6875. }
  6876. return SCRIPT_CMD_SUCCESS;
  6877. }
  6878. /*==========================================
  6879. * Delete the item equipped at pos.
  6880. *------------------------------------------*/
  6881. BUILDIN_FUNC(delequip)
  6882. {
  6883. int i=-1,num,ret=0;
  6884. TBL_PC *sd;
  6885. num = script_getnum(st,2);
  6886. sd = script_rid2sd(st);
  6887. if( sd == NULL )
  6888. return 0;
  6889. if (num > 0 && num <= ARRAYLENGTH(equip))
  6890. i=pc_checkequip(sd,equip[num-1]);
  6891. if(i >= 0) {
  6892. pc_unequipitem(sd,i,3); //recalculate bonus
  6893. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  6894. }
  6895. script_pushint(st,ret);
  6896. return SCRIPT_CMD_SUCCESS;
  6897. }
  6898. /*==========================================
  6899. * Break the item equipped at pos.
  6900. *------------------------------------------*/
  6901. BUILDIN_FUNC(breakequip)
  6902. {
  6903. int i=-1,num;
  6904. TBL_PC *sd;
  6905. num = script_getnum(st,2);
  6906. sd = script_rid2sd(st);
  6907. if( sd == NULL )
  6908. return 0;
  6909. if (num > 0 && num <= ARRAYLENGTH(equip))
  6910. i = pc_checkequip(sd,equip[num-1]);
  6911. if (i >= 0) {
  6912. sd->status.inventory[i].attribute = 1;
  6913. pc_unequipitem(sd,i,3);
  6914. clif_equiplist(sd);
  6915. script_pushint(st,1);
  6916. } else
  6917. script_pushint(st,0);
  6918. return SCRIPT_CMD_SUCCESS;
  6919. }
  6920. /*==========================================
  6921. *
  6922. *------------------------------------------*/
  6923. BUILDIN_FUNC(statusup)
  6924. {
  6925. int type;
  6926. TBL_PC *sd;
  6927. type=script_getnum(st,2);
  6928. sd = script_rid2sd(st);
  6929. if( sd == NULL )
  6930. return 0;
  6931. pc_statusup(sd,type);
  6932. return SCRIPT_CMD_SUCCESS;
  6933. }
  6934. /*==========================================
  6935. *
  6936. *------------------------------------------*/
  6937. BUILDIN_FUNC(statusup2)
  6938. {
  6939. int type,val;
  6940. TBL_PC *sd;
  6941. type=script_getnum(st,2);
  6942. val=script_getnum(st,3);
  6943. sd = script_rid2sd(st);
  6944. if( sd == NULL )
  6945. return 0;
  6946. pc_statusup2(sd,type,val);
  6947. return SCRIPT_CMD_SUCCESS;
  6948. }
  6949. /// See 'doc/item_bonus.txt'
  6950. ///
  6951. /// bonus <bonus type>,<val1>;
  6952. /// bonus2 <bonus type>,<val1>,<val2>;
  6953. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6954. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6955. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6956. BUILDIN_FUNC(bonus)
  6957. {
  6958. int type;
  6959. int val1;
  6960. int val2 = 0;
  6961. int val3 = 0;
  6962. int val4 = 0;
  6963. int val5 = 0;
  6964. TBL_PC* sd;
  6965. struct script_data *data;
  6966. sd = script_rid2sd(st);
  6967. if( sd == NULL )
  6968. return 0; // no player attached
  6969. type = script_getnum(st,2);
  6970. switch( type ) {
  6971. case SP_AUTOSPELL:
  6972. case SP_AUTOSPELL_WHENHIT:
  6973. case SP_AUTOSPELL_ONSKILL:
  6974. case SP_SKILL_ATK:
  6975. case SP_SKILL_HEAL:
  6976. case SP_SKILL_HEAL2:
  6977. case SP_ADD_SKILL_BLOW:
  6978. case SP_CASTRATE:
  6979. case SP_ADDEFF_ONSKILL:
  6980. case SP_SKILL_USE_SP_RATE:
  6981. case SP_SKILL_COOLDOWN:
  6982. case SP_SKILL_FIXEDCAST:
  6983. case SP_SKILL_VARIABLECAST:
  6984. case SP_VARCASTRATE:
  6985. case SP_FIXCASTRATE:
  6986. case SP_SKILL_USE_SP:
  6987. // these bonuses support skill names
  6988. data = script_getdata(st, 3);
  6989. get_val(st, data); // Convert into value in case of a variable
  6990. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6991. break;
  6992. default:
  6993. val1 = script_getnum(st,3);
  6994. break;
  6995. }
  6996. switch( script_lastdata(st)-2 ) {
  6997. case 1:
  6998. pc_bonus(sd, type, val1);
  6999. break;
  7000. case 2:
  7001. val2 = script_getnum(st,4);
  7002. pc_bonus2(sd, type, val1, val2);
  7003. break;
  7004. case 3:
  7005. val2 = script_getnum(st,4);
  7006. val3 = script_getnum(st,5);
  7007. pc_bonus3(sd, type, val1, val2, val3);
  7008. break;
  7009. case 4:
  7010. data = script_getdata(st, 4);
  7011. get_val(st, data); // Convert into value in case of a variable
  7012. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7013. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7014. else
  7015. val2 = script_getnum(st,4);
  7016. val3 = script_getnum(st,5);
  7017. val4 = script_getnum(st,6);
  7018. pc_bonus4(sd, type, val1, val2, val3, val4);
  7019. break;
  7020. case 5:
  7021. data = script_getdata(st, 4);
  7022. get_val(st, data); // Convert into value in case of a variable
  7023. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7024. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7025. else
  7026. val2 = script_getnum(st,4);
  7027. val3 = script_getnum(st,5);
  7028. val4 = script_getnum(st,6);
  7029. val5 = script_getnum(st,7);
  7030. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7031. break;
  7032. default:
  7033. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7034. break;
  7035. }
  7036. return SCRIPT_CMD_SUCCESS;
  7037. }
  7038. BUILDIN_FUNC(autobonus)
  7039. {
  7040. unsigned int dur;
  7041. short rate;
  7042. short atk_type = 0;
  7043. TBL_PC* sd;
  7044. const char *bonus_script, *other_script = NULL;
  7045. sd = script_rid2sd(st);
  7046. if( sd == NULL )
  7047. return 0; // no player attached
  7048. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7049. return 0;
  7050. rate = script_getnum(st,3);
  7051. dur = script_getnum(st,4);
  7052. bonus_script = script_getstr(st,2);
  7053. if( !rate || !dur || !bonus_script )
  7054. return 0;
  7055. if( script_hasdata(st,5) )
  7056. atk_type = script_getnum(st,5);
  7057. if( script_hasdata(st,6) )
  7058. other_script = script_getstr(st,6);
  7059. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7060. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7061. {
  7062. script_add_autobonus(bonus_script);
  7063. if( other_script )
  7064. script_add_autobonus(other_script);
  7065. }
  7066. return SCRIPT_CMD_SUCCESS;
  7067. }
  7068. BUILDIN_FUNC(autobonus2)
  7069. {
  7070. unsigned int dur;
  7071. short rate;
  7072. short atk_type = 0;
  7073. TBL_PC* sd;
  7074. const char *bonus_script, *other_script = NULL;
  7075. sd = script_rid2sd(st);
  7076. if( sd == NULL )
  7077. return 0; // no player attached
  7078. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7079. return 0;
  7080. rate = script_getnum(st,3);
  7081. dur = script_getnum(st,4);
  7082. bonus_script = script_getstr(st,2);
  7083. if( !rate || !dur || !bonus_script )
  7084. return 0;
  7085. if( script_hasdata(st,5) )
  7086. atk_type = script_getnum(st,5);
  7087. if( script_hasdata(st,6) )
  7088. other_script = script_getstr(st,6);
  7089. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7090. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7091. {
  7092. script_add_autobonus(bonus_script);
  7093. if( other_script )
  7094. script_add_autobonus(other_script);
  7095. }
  7096. return SCRIPT_CMD_SUCCESS;
  7097. }
  7098. BUILDIN_FUNC(autobonus3)
  7099. {
  7100. unsigned int dur;
  7101. short rate,atk_type;
  7102. TBL_PC* sd;
  7103. const char *bonus_script, *other_script = NULL;
  7104. struct script_data *data;
  7105. sd = script_rid2sd(st);
  7106. if( sd == NULL )
  7107. return 0; // no player attached
  7108. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7109. return 0;
  7110. rate = script_getnum(st,3);
  7111. dur = script_getnum(st,4);
  7112. data = script_getdata(st, 5);
  7113. get_val(st, data); // Convert into value in case of a variable
  7114. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7115. bonus_script = script_getstr(st,2);
  7116. if( !rate || !dur || !atk_type || !bonus_script )
  7117. return 0;
  7118. if( script_hasdata(st,6) )
  7119. other_script = script_getstr(st,6);
  7120. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7121. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  7122. {
  7123. script_add_autobonus(bonus_script);
  7124. if( other_script )
  7125. script_add_autobonus(other_script);
  7126. }
  7127. return SCRIPT_CMD_SUCCESS;
  7128. }
  7129. /// Changes the level of a player skill.
  7130. /// <flag> defaults to 1
  7131. /// <flag>=0 : set the level of the skill
  7132. /// <flag>=1 : set the temporary level of the skill
  7133. /// <flag>=2 : add to the level of the skill
  7134. ///
  7135. /// skill <skill id>,<level>,<flag>
  7136. /// skill <skill id>,<level>
  7137. /// skill "<skill name>",<level>,<flag>
  7138. /// skill "<skill name>",<level>
  7139. BUILDIN_FUNC(skill)
  7140. {
  7141. int id;
  7142. int level;
  7143. int flag = 1;
  7144. TBL_PC* sd;
  7145. struct script_data *data;
  7146. sd = script_rid2sd(st);
  7147. if( sd == NULL )
  7148. return 0;// no player attached, report source
  7149. data = script_getdata(st, 2);
  7150. get_val(st, data); // Convert into value in case of a variable
  7151. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7152. level = script_getnum(st,3);
  7153. if( script_hasdata(st,4) )
  7154. flag = script_getnum(st,4);
  7155. pc_skill(sd, id, level, flag);
  7156. return SCRIPT_CMD_SUCCESS;
  7157. }
  7158. /// Changes the level of a player skill.
  7159. /// like skill, but <flag> defaults to 2
  7160. ///
  7161. /// addtoskill <skill id>,<amount>,<flag>
  7162. /// addtoskill <skill id>,<amount>
  7163. /// addtoskill "<skill name>",<amount>,<flag>
  7164. /// addtoskill "<skill name>",<amount>
  7165. ///
  7166. /// @see skill
  7167. BUILDIN_FUNC(addtoskill)
  7168. {
  7169. int id;
  7170. int level;
  7171. int flag = 2;
  7172. TBL_PC* sd;
  7173. struct script_data *data;
  7174. sd = script_rid2sd(st);
  7175. if( sd == NULL )
  7176. return 0;// no player attached, report source
  7177. data = script_getdata(st, 2);
  7178. get_val(st, data); // Convert into value in case of a variable
  7179. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7180. level = script_getnum(st,3);
  7181. if( script_hasdata(st,4) )
  7182. flag = script_getnum(st,4);
  7183. pc_skill(sd, id, level, flag);
  7184. return SCRIPT_CMD_SUCCESS;
  7185. }
  7186. /// Increases the level of a guild skill.
  7187. ///
  7188. /// guildskill <skill id>,<amount>;
  7189. /// guildskill "<skill name>",<amount>;
  7190. BUILDIN_FUNC(guildskill)
  7191. {
  7192. int id;
  7193. int level;
  7194. TBL_PC* sd;
  7195. int i;
  7196. struct script_data *data;
  7197. sd = script_rid2sd(st);
  7198. if( sd == NULL )
  7199. return 0;// no player attached, report source
  7200. data = script_getdata(st, 2);
  7201. get_val(st, data); // Convert into value in case of a variable
  7202. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7203. level = script_getnum(st,3);
  7204. for( i=0; i < level; i++ )
  7205. guild_skillup(sd, id);
  7206. return SCRIPT_CMD_SUCCESS;
  7207. }
  7208. /// Returns the level of the player skill.
  7209. ///
  7210. /// getskilllv(<skill id>) -> <level>
  7211. /// getskilllv("<skill name>") -> <level>
  7212. BUILDIN_FUNC(getskilllv)
  7213. {
  7214. int id;
  7215. TBL_PC* sd;
  7216. struct script_data *data;
  7217. sd = script_rid2sd(st);
  7218. if( sd == NULL )
  7219. return 0;// no player attached, report source
  7220. data = script_getdata(st, 2);
  7221. get_val(st, data); // Convert into value in case of a variable
  7222. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7223. script_pushint(st, pc_checkskill(sd,id));
  7224. return SCRIPT_CMD_SUCCESS;
  7225. }
  7226. /// Returns the level of the guild skill.
  7227. ///
  7228. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7229. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7230. BUILDIN_FUNC(getgdskilllv)
  7231. {
  7232. int guild_id;
  7233. uint16 skill_id;
  7234. struct guild* g;
  7235. struct script_data *data;
  7236. guild_id = script_getnum(st,2);
  7237. data = script_getdata(st, 3);
  7238. get_val(st, data); // Convert into value in case of a variable
  7239. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7240. g = guild_search(guild_id);
  7241. if( g == NULL )
  7242. script_pushint(st, -1);
  7243. else
  7244. script_pushint(st, guild_checkskill(g,skill_id));
  7245. return SCRIPT_CMD_SUCCESS;
  7246. }
  7247. /// Returns the 'basic_skill_check' setting.
  7248. /// This config determines if the server checks the skill level of NV_BASIC
  7249. /// before allowing the basic actions.
  7250. ///
  7251. /// basicskillcheck() -> <bool>
  7252. BUILDIN_FUNC(basicskillcheck)
  7253. {
  7254. script_pushint(st, battle_config.basic_skill_check);
  7255. return SCRIPT_CMD_SUCCESS;
  7256. }
  7257. /// Returns the GM level of the player.
  7258. ///
  7259. /// getgmlevel() -> <level>
  7260. BUILDIN_FUNC(getgmlevel)
  7261. {
  7262. TBL_PC* sd;
  7263. sd = script_rid2sd(st);
  7264. if( sd == NULL )
  7265. return 0;// no player attached, report source
  7266. script_pushint(st, pc_get_group_level(sd));
  7267. return SCRIPT_CMD_SUCCESS;
  7268. }
  7269. /// Returns the group ID of the player.
  7270. ///
  7271. /// getgroupid() -> <int>
  7272. BUILDIN_FUNC(getgroupid)
  7273. {
  7274. TBL_PC* sd;
  7275. sd = script_rid2sd(st);
  7276. if (sd == NULL)
  7277. return 1; // no player attached, report source
  7278. script_pushint(st, pc_get_group_id(sd));
  7279. return SCRIPT_CMD_SUCCESS;
  7280. }
  7281. /// Terminates the execution of this script instance.
  7282. ///
  7283. /// end
  7284. BUILDIN_FUNC(end)
  7285. {
  7286. TBL_PC* sd;
  7287. sd = map_id2sd(st->rid);
  7288. st->state = END;
  7289. if( st->mes_active )
  7290. st->mes_active = 0;
  7291. if( sd )
  7292. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  7293. return SCRIPT_CMD_SUCCESS;
  7294. }
  7295. /// Checks if the player has that effect state (option).
  7296. ///
  7297. /// checkoption(<option>) -> <bool>
  7298. BUILDIN_FUNC(checkoption)
  7299. {
  7300. int option;
  7301. TBL_PC* sd;
  7302. sd = script_rid2sd(st);
  7303. if( sd == NULL )
  7304. return 0;// no player attached, report source
  7305. option = script_getnum(st,2);
  7306. if( sd->sc.option&option )
  7307. script_pushint(st, 1);
  7308. else
  7309. script_pushint(st, 0);
  7310. return SCRIPT_CMD_SUCCESS;
  7311. }
  7312. /// Checks if the player is in that body state (opt1).
  7313. ///
  7314. /// checkoption1(<opt1>) -> <bool>
  7315. BUILDIN_FUNC(checkoption1)
  7316. {
  7317. int opt1;
  7318. TBL_PC* sd;
  7319. sd = script_rid2sd(st);
  7320. if( sd == NULL )
  7321. return 0;// no player attached, report source
  7322. opt1 = script_getnum(st,2);
  7323. if( sd->sc.opt1 == opt1 )
  7324. script_pushint(st, 1);
  7325. else
  7326. script_pushint(st, 0);
  7327. return SCRIPT_CMD_SUCCESS;
  7328. }
  7329. /// Checks if the player has that health state (opt2).
  7330. ///
  7331. /// checkoption2(<opt2>) -> <bool>
  7332. BUILDIN_FUNC(checkoption2)
  7333. {
  7334. int opt2;
  7335. TBL_PC* sd;
  7336. sd = script_rid2sd(st);
  7337. if( sd == NULL )
  7338. return 0;// no player attached, report source
  7339. opt2 = script_getnum(st,2);
  7340. if( sd->sc.opt2&opt2 )
  7341. script_pushint(st, 1);
  7342. else
  7343. script_pushint(st, 0);
  7344. return SCRIPT_CMD_SUCCESS;
  7345. }
  7346. /// Changes the effect state (option) of the player.
  7347. /// <flag> defaults to 1
  7348. /// <flag>=0 : removes the option
  7349. /// <flag>=other : adds the option
  7350. ///
  7351. /// setoption <option>,<flag>;
  7352. /// setoption <option>;
  7353. BUILDIN_FUNC(setoption)
  7354. {
  7355. int option;
  7356. int flag = 1;
  7357. TBL_PC* sd;
  7358. sd = script_rid2sd(st);
  7359. if( sd == NULL )
  7360. return 0;// no player attached, report source
  7361. option = script_getnum(st,2);
  7362. if( script_hasdata(st,3) )
  7363. flag = script_getnum(st,3);
  7364. else if( !option ){// Request to remove everything.
  7365. flag = 0;
  7366. option = OPTION_FALCON|OPTION_RIDING;
  7367. #ifndef NEW_CARTS
  7368. option |= OPTION_CART;
  7369. #endif
  7370. }
  7371. if( flag ){// Add option
  7372. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  7373. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  7374. pc_setoption(sd, sd->sc.option|option);
  7375. } else// Remove option
  7376. pc_setoption(sd, sd->sc.option&~option);
  7377. return SCRIPT_CMD_SUCCESS;
  7378. }
  7379. /// Returns if the player has a cart.
  7380. ///
  7381. /// checkcart() -> <bool>
  7382. ///
  7383. /// @author Valaris
  7384. BUILDIN_FUNC(checkcart)
  7385. {
  7386. TBL_PC* sd;
  7387. sd = script_rid2sd(st);
  7388. if( sd == NULL )
  7389. return 0;// no player attached, report source
  7390. if( pc_iscarton(sd) )
  7391. script_pushint(st, 1);
  7392. else
  7393. script_pushint(st, 0);
  7394. return SCRIPT_CMD_SUCCESS;
  7395. }
  7396. /// Sets the cart of the player.
  7397. /// <type> defaults to 1
  7398. /// <type>=0 : removes the cart
  7399. /// <type>=1 : Normal cart
  7400. /// <type>=2 : Wooden cart
  7401. /// <type>=3 : Covered cart with flowers and ferns
  7402. /// <type>=4 : Wooden cart with a Panda doll on the back
  7403. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  7404. ///
  7405. /// setcart <type>;
  7406. /// setcart;
  7407. BUILDIN_FUNC(setcart)
  7408. {
  7409. int type = 1;
  7410. TBL_PC* sd;
  7411. sd = script_rid2sd(st);
  7412. if( sd == NULL )
  7413. return 0;// no player attached, report source
  7414. if( script_hasdata(st,2) )
  7415. type = script_getnum(st,2);
  7416. pc_setcart(sd, type);
  7417. return SCRIPT_CMD_SUCCESS;
  7418. }
  7419. /// Returns if the player has a falcon.
  7420. ///
  7421. /// checkfalcon() -> <bool>
  7422. ///
  7423. /// @author Valaris
  7424. BUILDIN_FUNC(checkfalcon)
  7425. {
  7426. TBL_PC* sd;
  7427. sd = script_rid2sd(st);
  7428. if( sd == NULL )
  7429. return 0;// no player attached, report source
  7430. if( pc_isfalcon(sd) )
  7431. script_pushint(st, 1);
  7432. else
  7433. script_pushint(st, 0);
  7434. return SCRIPT_CMD_SUCCESS;
  7435. }
  7436. /// Sets if the player has a falcon or not.
  7437. /// <flag> defaults to 1
  7438. ///
  7439. /// setfalcon <flag>;
  7440. /// setfalcon;
  7441. BUILDIN_FUNC(setfalcon)
  7442. {
  7443. int flag = 1;
  7444. TBL_PC* sd;
  7445. sd = script_rid2sd(st);
  7446. if( sd == NULL )
  7447. return 0;// no player attached, report source
  7448. if( script_hasdata(st,2) )
  7449. flag = script_getnum(st,2);
  7450. pc_setfalcon(sd, flag);
  7451. return SCRIPT_CMD_SUCCESS;
  7452. }
  7453. /// Returns if the player is riding.
  7454. ///
  7455. /// checkriding() -> <bool>
  7456. ///
  7457. /// @author Valaris
  7458. BUILDIN_FUNC(checkriding)
  7459. {
  7460. TBL_PC* sd;
  7461. sd = script_rid2sd(st);
  7462. if( sd == NULL )
  7463. return 0;// no player attached, report source
  7464. if( pc_isriding(sd) || pc_isridingwug(sd) || pc_isridingdragon(sd) )
  7465. script_pushint(st, 1);
  7466. else
  7467. script_pushint(st, 0);
  7468. return SCRIPT_CMD_SUCCESS;
  7469. }
  7470. /// Sets if the player is riding.
  7471. /// <flag> defaults to 1
  7472. ///
  7473. /// setriding <flag>;
  7474. /// setriding;
  7475. BUILDIN_FUNC(setriding)
  7476. {
  7477. int flag = 1;
  7478. TBL_PC* sd;
  7479. sd = script_rid2sd(st);
  7480. if( sd == NULL )
  7481. return 0;// no player attached, report source
  7482. if( script_hasdata(st,2) )
  7483. flag = script_getnum(st,2);
  7484. pc_setriding(sd, flag);
  7485. return SCRIPT_CMD_SUCCESS;
  7486. }
  7487. /// Returns if the player has a warg.
  7488. ///
  7489. /// checkwug() -> <bool>
  7490. ///
  7491. BUILDIN_FUNC(checkwug)
  7492. {
  7493. TBL_PC* sd;
  7494. sd = script_rid2sd(st);
  7495. if( sd == NULL )
  7496. return 0;// no player attached, report source
  7497. if( pc_iswug(sd) || pc_isridingwug(sd) )
  7498. script_pushint(st, 1);
  7499. else
  7500. script_pushint(st, 0);
  7501. return SCRIPT_CMD_SUCCESS;
  7502. }
  7503. /// Returns if the player is wearing MADO Gear.
  7504. ///
  7505. /// checkmadogear() -> <bool>
  7506. ///
  7507. BUILDIN_FUNC(checkmadogear)
  7508. {
  7509. TBL_PC* sd;
  7510. sd = script_rid2sd(st);
  7511. if( sd == NULL )
  7512. return 0;// no player attached, report source
  7513. if( pc_ismadogear(sd) )
  7514. script_pushint(st, 1);
  7515. else
  7516. script_pushint(st, 0);
  7517. return SCRIPT_CMD_SUCCESS;
  7518. }
  7519. /// Sets if the player is riding MADO Gear.
  7520. /// <flag> defaults to 1
  7521. ///
  7522. /// setmadogear <flag>;
  7523. /// setmadogear;
  7524. BUILDIN_FUNC(setmadogear)
  7525. {
  7526. int flag = 1;
  7527. TBL_PC* sd;
  7528. sd = script_rid2sd(st);
  7529. if( sd == NULL )
  7530. return 0;// no player attached, report source
  7531. if( script_hasdata(st,2) )
  7532. flag = script_getnum(st,2);
  7533. pc_setmadogear(sd, flag);
  7534. return SCRIPT_CMD_SUCCESS;
  7535. }
  7536. /// Sets the save point of the player.
  7537. ///
  7538. /// save "<map name>",<x>,<y>
  7539. /// savepoint "<map name>",<x>,<y>
  7540. BUILDIN_FUNC(savepoint)
  7541. {
  7542. int x;
  7543. int y;
  7544. short map;
  7545. const char* str;
  7546. TBL_PC* sd;
  7547. sd = script_rid2sd(st);
  7548. if( sd == NULL )
  7549. return 0;// no player attached, report source
  7550. str = script_getstr(st, 2);
  7551. x = script_getnum(st,3);
  7552. y = script_getnum(st,4);
  7553. map = mapindex_name2id(str);
  7554. if( map )
  7555. pc_setsavepoint(sd, map, x, y);
  7556. return SCRIPT_CMD_SUCCESS;
  7557. }
  7558. /*==========================================
  7559. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  7560. *------------------------------------------*/
  7561. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  7562. {
  7563. int type;
  7564. time_t timer;
  7565. struct tm *t;
  7566. type=script_getnum(st,2);
  7567. switch(type){
  7568. case 2:
  7569. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  7570. // from the system clock.)
  7571. script_pushint(st,(int)time(NULL));
  7572. break;
  7573. case 1:
  7574. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  7575. time(&timer);
  7576. t=localtime(&timer);
  7577. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  7578. break;
  7579. case 0:
  7580. default:
  7581. //type 0:(System Ticks)
  7582. script_pushint(st,gettick());
  7583. break;
  7584. }
  7585. return SCRIPT_CMD_SUCCESS;
  7586. }
  7587. /*==========================================
  7588. * GetTime(Type);
  7589. * 1: Sec 2: Min 3: Hour
  7590. * 4: WeekDay 5: MonthDay 6: Month
  7591. * 7: Year
  7592. *------------------------------------------*/
  7593. BUILDIN_FUNC(gettime) /* Asgard Version */
  7594. {
  7595. int type;
  7596. time_t timer;
  7597. struct tm *t;
  7598. type=script_getnum(st,2);
  7599. time(&timer);
  7600. t=localtime(&timer);
  7601. switch(type){
  7602. case 1://Sec(0~59)
  7603. script_pushint(st,t->tm_sec);
  7604. break;
  7605. case 2://Min(0~59)
  7606. script_pushint(st,t->tm_min);
  7607. break;
  7608. case 3://Hour(0~23)
  7609. script_pushint(st,t->tm_hour);
  7610. break;
  7611. case 4://WeekDay(0~6)
  7612. script_pushint(st,t->tm_wday);
  7613. break;
  7614. case 5://MonthDay(01~31)
  7615. script_pushint(st,t->tm_mday);
  7616. break;
  7617. case 6://Month(01~12)
  7618. script_pushint(st,t->tm_mon+1);
  7619. break;
  7620. case 7://Year(20xx)
  7621. script_pushint(st,t->tm_year+1900);
  7622. break;
  7623. case 8://Year Day(01~366)
  7624. script_pushint(st,t->tm_yday+1);
  7625. break;
  7626. default://(format error)
  7627. script_pushint(st,-1);
  7628. break;
  7629. }
  7630. return SCRIPT_CMD_SUCCESS;
  7631. }
  7632. /*==========================================
  7633. * GetTimeStr("TimeFMT", Length);
  7634. *------------------------------------------*/
  7635. BUILDIN_FUNC(gettimestr)
  7636. {
  7637. char *tmpstr;
  7638. const char *fmtstr;
  7639. int maxlen;
  7640. time_t now = time(NULL);
  7641. fmtstr=script_getstr(st,2);
  7642. maxlen=script_getnum(st,3);
  7643. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  7644. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  7645. tmpstr[maxlen]='\0';
  7646. script_pushstr(st,tmpstr);
  7647. return SCRIPT_CMD_SUCCESS;
  7648. }
  7649. /*==========================================
  7650. * Open player storage
  7651. *------------------------------------------*/
  7652. BUILDIN_FUNC(openstorage)
  7653. {
  7654. TBL_PC* sd;
  7655. sd = script_rid2sd(st);
  7656. if( sd == NULL )
  7657. return 0;
  7658. storage_storageopen(sd);
  7659. return SCRIPT_CMD_SUCCESS;
  7660. }
  7661. BUILDIN_FUNC(guildopenstorage)
  7662. {
  7663. TBL_PC* sd;
  7664. int ret;
  7665. sd = script_rid2sd(st);
  7666. if( sd == NULL )
  7667. return 0;
  7668. ret = storage_guild_storageopen(sd);
  7669. script_pushint(st,ret);
  7670. return SCRIPT_CMD_SUCCESS;
  7671. }
  7672. /*==========================================
  7673. * Make player use a skill trought item usage
  7674. *------------------------------------------*/
  7675. /// itemskill <skill id>,<level>
  7676. /// itemskill "<skill name>",<level>
  7677. BUILDIN_FUNC(itemskill)
  7678. {
  7679. int id;
  7680. int lv;
  7681. TBL_PC* sd;
  7682. struct script_data *data;
  7683. sd = script_rid2sd(st);
  7684. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  7685. return 0;
  7686. data = script_getdata(st, 2);
  7687. get_val(st, data); // Convert into value in case of a variable
  7688. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7689. lv = script_getnum(st,3);
  7690. sd->skillitem=id;
  7691. sd->skillitemlv=lv;
  7692. clif_item_skill(sd,id,lv);
  7693. return SCRIPT_CMD_SUCCESS;
  7694. }
  7695. /*==========================================
  7696. * Attempt to create an item
  7697. *------------------------------------------*/
  7698. BUILDIN_FUNC(produce)
  7699. {
  7700. int trigger;
  7701. TBL_PC* sd;
  7702. sd = script_rid2sd(st);
  7703. if( sd == NULL )
  7704. return 0;
  7705. trigger=script_getnum(st,2);
  7706. clif_skill_produce_mix_list(sd, -1, trigger);
  7707. return SCRIPT_CMD_SUCCESS;
  7708. }
  7709. /*==========================================
  7710. *
  7711. *------------------------------------------*/
  7712. BUILDIN_FUNC(cooking)
  7713. {
  7714. int trigger;
  7715. TBL_PC* sd;
  7716. sd = script_rid2sd(st);
  7717. if( sd == NULL )
  7718. return 0;
  7719. trigger=script_getnum(st,2);
  7720. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  7721. return SCRIPT_CMD_SUCCESS;
  7722. }
  7723. /*==========================================
  7724. * Create a pet
  7725. *------------------------------------------*/
  7726. BUILDIN_FUNC(makepet)
  7727. {
  7728. TBL_PC* sd;
  7729. int id,pet_id;
  7730. id=script_getnum(st,2);
  7731. sd = script_rid2sd(st);
  7732. if( sd == NULL )
  7733. return 0;
  7734. pet_id = search_petDB_index(id, PET_CLASS);
  7735. if (pet_id < 0)
  7736. pet_id = search_petDB_index(id, PET_EGG);
  7737. if (pet_id >= 0 && sd) {
  7738. sd->catch_target_class = pet_db[pet_id].class_;
  7739. intif_create_pet(
  7740. sd->status.account_id, sd->status.char_id,
  7741. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  7742. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  7743. 100, 0, 1, pet_db[pet_id].jname);
  7744. }
  7745. return SCRIPT_CMD_SUCCESS;
  7746. }
  7747. /*==========================================
  7748. * Give player exp base,job * quest_exp_rate/100
  7749. *------------------------------------------*/
  7750. BUILDIN_FUNC(getexp)
  7751. {
  7752. TBL_PC* sd;
  7753. int base=0,job=0;
  7754. double bonus;
  7755. sd = script_rid2sd(st);
  7756. if( sd == NULL )
  7757. return 0;
  7758. base=script_getnum(st,2);
  7759. job =script_getnum(st,3);
  7760. if(base<0 || job<0)
  7761. return 0;
  7762. // bonus for npc-given exp
  7763. bonus = battle_config.quest_exp_rate / 100.;
  7764. base = (int) cap_value(base * bonus, 0, INT_MAX);
  7765. job = (int) cap_value(job * bonus, 0, INT_MAX);
  7766. pc_gainexp(sd, NULL, base, job, true);
  7767. return SCRIPT_CMD_SUCCESS;
  7768. }
  7769. /*==========================================
  7770. * Gain guild exp [Celest]
  7771. *------------------------------------------*/
  7772. BUILDIN_FUNC(guildgetexp)
  7773. {
  7774. TBL_PC* sd;
  7775. int exp;
  7776. sd = script_rid2sd(st);
  7777. if( sd == NULL )
  7778. return 0;
  7779. exp = script_getnum(st,2);
  7780. if(exp < 0)
  7781. return 0;
  7782. if(sd && sd->status.guild_id > 0)
  7783. guild_getexp (sd, exp);
  7784. return SCRIPT_CMD_SUCCESS;
  7785. }
  7786. /*==========================================
  7787. * Changes the guild master of a guild [Skotlex]
  7788. *------------------------------------------*/
  7789. BUILDIN_FUNC(guildchangegm)
  7790. {
  7791. TBL_PC *sd;
  7792. int guild_id;
  7793. const char *name;
  7794. guild_id = script_getnum(st,2);
  7795. name = script_getstr(st,3);
  7796. sd=map_nick2sd(name);
  7797. if (!sd)
  7798. script_pushint(st,0);
  7799. else
  7800. script_pushint(st,guild_gm_change(guild_id, sd));
  7801. return SCRIPT_CMD_SUCCESS;
  7802. }
  7803. /*==========================================
  7804. * Spawn a monster :
  7805. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  7806. @mapn,x,y : location
  7807. @str : monster name
  7808. @class_ : mob_id
  7809. @amount : nb to spawn
  7810. @event : event to attach to mob
  7811. *------------------------------------------*/
  7812. BUILDIN_FUNC(monster)
  7813. {
  7814. const char* mapn = script_getstr(st,2);
  7815. int x = script_getnum(st,3);
  7816. int y = script_getnum(st,4);
  7817. const char* str = script_getstr(st,5);
  7818. int class_ = script_getnum(st,6);
  7819. int amount = script_getnum(st,7);
  7820. const char* event = "";
  7821. unsigned int size = SZ_SMALL;
  7822. unsigned int ai = AI_NONE;
  7823. struct map_session_data* sd;
  7824. int16 m;
  7825. int i;
  7826. if (script_hasdata(st, 8)) {
  7827. event = script_getstr(st, 8);
  7828. check_event(st, event);
  7829. }
  7830. if (script_hasdata(st, 9)) {
  7831. size = script_getnum(st, 9);
  7832. if (size > 3) {
  7833. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  7834. return 1;
  7835. }
  7836. }
  7837. if (script_hasdata(st, 10)) {
  7838. ai = script_getnum(st, 10);
  7839. if (ai >= AI_MAX) {
  7840. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  7841. return 1;
  7842. }
  7843. }
  7844. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7845. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7846. return 1;
  7847. }
  7848. sd = map_id2sd(st->rid);
  7849. if (sd && strcmp(mapn, "this") == 0)
  7850. m = sd->bl.m;
  7851. else
  7852. m = map_mapname2mapid(mapn);
  7853. for(i=0; i<amount; i++){ //not optimised
  7854. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  7855. mapreg_setreg(reference_uid(add_str("$@mobid"), i),mobid);
  7856. }
  7857. return SCRIPT_CMD_SUCCESS;
  7858. }
  7859. /*==========================================
  7860. * Request List of Monster Drops
  7861. *------------------------------------------*/
  7862. BUILDIN_FUNC(getmobdrops)
  7863. {
  7864. int class_ = script_getnum(st,2);
  7865. int i, j = 0;
  7866. struct mob_db *mob;
  7867. if( !mobdb_checkid(class_) )
  7868. {
  7869. script_pushint(st, 0);
  7870. return 0;
  7871. }
  7872. mob = mob_db(class_);
  7873. for( i = 0; i < MAX_MOB_DROP; i++ )
  7874. {
  7875. if( mob->dropitem[i].nameid < 1 )
  7876. continue;
  7877. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7878. continue;
  7879. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7880. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7881. j++;
  7882. }
  7883. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7884. script_pushint(st, 1);
  7885. return SCRIPT_CMD_SUCCESS;
  7886. }
  7887. /*==========================================
  7888. * Same as monster but randomize location in x0,x1,y0,y1 area
  7889. *------------------------------------------*/
  7890. BUILDIN_FUNC(areamonster)
  7891. {
  7892. const char* mapn = script_getstr(st,2);
  7893. int x0 = script_getnum(st,3);
  7894. int y0 = script_getnum(st,4);
  7895. int x1 = script_getnum(st,5);
  7896. int y1 = script_getnum(st,6);
  7897. const char* str = script_getstr(st,7);
  7898. int class_ = script_getnum(st,8);
  7899. int amount = script_getnum(st,9);
  7900. const char* event = "";
  7901. unsigned int size = SZ_SMALL;
  7902. unsigned int ai = AI_NONE;
  7903. struct map_session_data* sd;
  7904. int16 m;
  7905. if (script_hasdata(st,10)) {
  7906. event = script_getstr(st, 10);
  7907. check_event(st, event);
  7908. }
  7909. if (script_hasdata(st, 11)) {
  7910. size = script_getnum(st, 11);
  7911. if (size > 3) {
  7912. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  7913. return 1;
  7914. }
  7915. }
  7916. if (script_hasdata(st, 12)) {
  7917. ai = script_getnum(st, 12);
  7918. if (ai >= AI_MAX) {
  7919. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  7920. return 1;
  7921. }
  7922. }
  7923. sd = map_id2sd(st->rid);
  7924. if (sd && strcmp(mapn, "this") == 0)
  7925. m = sd->bl.m;
  7926. else
  7927. m = map_mapname2mapid(mapn);
  7928. mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, amount, event, size, ai);
  7929. return SCRIPT_CMD_SUCCESS;
  7930. }
  7931. /*==========================================
  7932. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  7933. *------------------------------------------*/
  7934. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7935. { //same fix but with killmonster instead - stripping events from mobs.
  7936. TBL_MOB* md = (TBL_MOB*)bl;
  7937. char *event=va_arg(ap,char *);
  7938. int allflag=va_arg(ap,int);
  7939. md->state.npc_killmonster = 1;
  7940. if(!allflag){
  7941. if(strcmp(event,md->npc_event)==0)
  7942. status_kill(bl);
  7943. }else{
  7944. if(!md->spawn)
  7945. status_kill(bl);
  7946. }
  7947. md->state.npc_killmonster = 0;
  7948. return 0;
  7949. }
  7950. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7951. {
  7952. TBL_MOB* md = (TBL_MOB*)bl;
  7953. char *event=va_arg(ap,char *);
  7954. int allflag=va_arg(ap,int);
  7955. if(!allflag){
  7956. if(strcmp(event,md->npc_event)==0)
  7957. status_kill(bl);
  7958. }else{
  7959. if(!md->spawn)
  7960. status_kill(bl);
  7961. }
  7962. return 0;
  7963. }
  7964. BUILDIN_FUNC(killmonster)
  7965. {
  7966. const char *mapname,*event;
  7967. int16 m,allflag=0;
  7968. mapname=script_getstr(st,2);
  7969. event=script_getstr(st,3);
  7970. if(strcmp(event,"All")==0)
  7971. allflag = 1;
  7972. else
  7973. check_event(st, event);
  7974. if( (m=map_mapname2mapid(mapname))<0 )
  7975. return 0;
  7976. if( script_hasdata(st,4) ) {
  7977. if ( script_getnum(st,4) == 1 ) {
  7978. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7979. return 0;
  7980. }
  7981. }
  7982. map_freeblock_lock();
  7983. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7984. map_freeblock_unlock();
  7985. return SCRIPT_CMD_SUCCESS;
  7986. }
  7987. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7988. { //Strips the event from the mob if it's killed the old method.
  7989. struct mob_data *md;
  7990. md = BL_CAST(BL_MOB, bl);
  7991. if (md->npc_event[0])
  7992. md->npc_event[0] = 0;
  7993. status_kill(bl);
  7994. return 0;
  7995. }
  7996. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7997. {
  7998. status_kill(bl);
  7999. return 0;
  8000. }
  8001. BUILDIN_FUNC(killmonsterall)
  8002. {
  8003. const char *mapname;
  8004. int16 m;
  8005. mapname=script_getstr(st,2);
  8006. if( (m = map_mapname2mapid(mapname))<0 )
  8007. return 0;
  8008. if( script_hasdata(st,3) ) {
  8009. if ( script_getnum(st,3) == 1 ) {
  8010. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8011. return 0;
  8012. }
  8013. }
  8014. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8015. return SCRIPT_CMD_SUCCESS;
  8016. }
  8017. /*==========================================
  8018. * Creates a clone of a player.
  8019. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8020. *------------------------------------------*/
  8021. BUILDIN_FUNC(clone)
  8022. {
  8023. TBL_PC *sd, *msd=NULL;
  8024. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  8025. unsigned int duration = 0;
  8026. const char *map,*event="";
  8027. map=script_getstr(st,2);
  8028. x=script_getnum(st,3);
  8029. y=script_getnum(st,4);
  8030. event=script_getstr(st,5);
  8031. char_id=script_getnum(st,6);
  8032. if( script_hasdata(st,7) )
  8033. master_id=script_getnum(st,7);
  8034. if( script_hasdata(st,8) )
  8035. mode=script_getnum(st,8);
  8036. if( script_hasdata(st,9) )
  8037. flag=script_getnum(st,9);
  8038. if( script_hasdata(st,10) )
  8039. duration=script_getnum(st,10);
  8040. check_event(st, event);
  8041. m = map_mapname2mapid(map);
  8042. if (m < 0) return 0;
  8043. sd = map_charid2sd(char_id);
  8044. if (master_id) {
  8045. msd = map_charid2sd(master_id);
  8046. if (msd)
  8047. master_id = msd->bl.id;
  8048. else
  8049. master_id = 0;
  8050. }
  8051. if (sd) //Return ID of newly crafted clone.
  8052. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8053. else //Failed to create clone.
  8054. script_pushint(st,0);
  8055. return SCRIPT_CMD_SUCCESS;
  8056. }
  8057. /*==========================================
  8058. *------------------------------------------*/
  8059. BUILDIN_FUNC(doevent)
  8060. {
  8061. const char* event = script_getstr(st,2);
  8062. struct map_session_data* sd;
  8063. if( ( sd = script_rid2sd(st) ) == NULL )
  8064. {
  8065. return 0;
  8066. }
  8067. check_event(st, event);
  8068. npc_event(sd, event, 0);
  8069. return SCRIPT_CMD_SUCCESS;
  8070. }
  8071. /*==========================================
  8072. *------------------------------------------*/
  8073. BUILDIN_FUNC(donpcevent)
  8074. {
  8075. const char* event = script_getstr(st,2);
  8076. check_event(st, event);
  8077. if( !npc_event_do(event) ) {
  8078. struct npc_data * nd = map_id2nd(st->oid);
  8079. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8080. script_pushint(st, 0);
  8081. } else
  8082. script_pushint(st, 1);
  8083. return SCRIPT_CMD_SUCCESS;
  8084. }
  8085. /// for Aegis compatibility
  8086. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8087. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8088. {
  8089. const char* npc = script_getstr(st,2);
  8090. const char* command = script_getstr(st,3);
  8091. char event[EVENT_NAME_LENGTH];
  8092. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8093. check_event(st, event);
  8094. npc_event_do(event);
  8095. return SCRIPT_CMD_SUCCESS;
  8096. }
  8097. /*==========================================
  8098. *------------------------------------------*/
  8099. BUILDIN_FUNC(addtimer)
  8100. {
  8101. int tick = script_getnum(st,2);
  8102. const char* event = script_getstr(st, 3);
  8103. TBL_PC* sd;
  8104. check_event(st, event);
  8105. sd = script_rid2sd(st);
  8106. if( sd == NULL )
  8107. return 0;
  8108. pc_addeventtimer(sd,tick,event);
  8109. return SCRIPT_CMD_SUCCESS;
  8110. }
  8111. /*==========================================
  8112. *------------------------------------------*/
  8113. BUILDIN_FUNC(deltimer)
  8114. {
  8115. const char *event;
  8116. TBL_PC* sd;
  8117. event=script_getstr(st, 2);
  8118. sd = script_rid2sd(st);
  8119. if( sd == NULL )
  8120. return 0;
  8121. check_event(st, event);
  8122. pc_deleventtimer(sd,event);
  8123. return SCRIPT_CMD_SUCCESS;
  8124. }
  8125. /*==========================================
  8126. *------------------------------------------*/
  8127. BUILDIN_FUNC(addtimercount)
  8128. {
  8129. const char *event;
  8130. int tick;
  8131. TBL_PC* sd;
  8132. tick=script_getnum(st,2);
  8133. event=script_getstr(st,3);
  8134. sd = script_rid2sd(st);
  8135. if( sd == NULL )
  8136. return 0;
  8137. check_event(st, event);
  8138. pc_addeventtimercount(sd,event,tick);
  8139. return SCRIPT_CMD_SUCCESS;
  8140. }
  8141. /*==========================================
  8142. *------------------------------------------*/
  8143. BUILDIN_FUNC(initnpctimer)
  8144. {
  8145. struct npc_data *nd;
  8146. int flag = 0;
  8147. if( script_hasdata(st,3) )
  8148. { //Two arguments: NPC name and attach flag.
  8149. nd = npc_name2id(script_getstr(st, 2));
  8150. flag = script_getnum(st,3);
  8151. }
  8152. else if( script_hasdata(st,2) )
  8153. { //Check if argument is numeric (flag) or string (npc name)
  8154. struct script_data *data;
  8155. data = script_getdata(st,2);
  8156. get_val(st,data);
  8157. if( data_isstring(data) ) //NPC name
  8158. nd = npc_name2id(conv_str(st, data));
  8159. else if( data_isint(data) ) //Flag
  8160. {
  8161. nd = (struct npc_data *)map_id2bl(st->oid);
  8162. flag = conv_num(st,data);
  8163. }
  8164. else
  8165. {
  8166. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8167. return 1;
  8168. }
  8169. }
  8170. else
  8171. nd = (struct npc_data *)map_id2bl(st->oid);
  8172. if( !nd )
  8173. return 0;
  8174. if( flag ) //Attach
  8175. {
  8176. TBL_PC* sd = script_rid2sd(st);
  8177. if( sd == NULL )
  8178. return 0;
  8179. nd->u.scr.rid = sd->bl.id;
  8180. }
  8181. nd->u.scr.timertick = 0;
  8182. npc_settimerevent_tick(nd,0);
  8183. npc_timerevent_start(nd, st->rid);
  8184. return SCRIPT_CMD_SUCCESS;
  8185. }
  8186. /*==========================================
  8187. *------------------------------------------*/
  8188. BUILDIN_FUNC(startnpctimer)
  8189. {
  8190. struct npc_data *nd;
  8191. int flag = 0;
  8192. if( script_hasdata(st,3) )
  8193. { //Two arguments: NPC name and attach flag.
  8194. nd = npc_name2id(script_getstr(st, 2));
  8195. flag = script_getnum(st,3);
  8196. }
  8197. else if( script_hasdata(st,2) )
  8198. { //Check if argument is numeric (flag) or string (npc name)
  8199. struct script_data *data;
  8200. data = script_getdata(st,2);
  8201. get_val(st,data);
  8202. if( data_isstring(data) ) //NPC name
  8203. nd = npc_name2id(conv_str(st, data));
  8204. else if( data_isint(data) ) //Flag
  8205. {
  8206. nd = (struct npc_data *)map_id2bl(st->oid);
  8207. flag = conv_num(st,data);
  8208. }
  8209. else
  8210. {
  8211. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8212. return 1;
  8213. }
  8214. }
  8215. else
  8216. nd=(struct npc_data *)map_id2bl(st->oid);
  8217. if( !nd )
  8218. return 0;
  8219. if( flag ) //Attach
  8220. {
  8221. TBL_PC* sd = script_rid2sd(st);
  8222. if( sd == NULL )
  8223. return 0;
  8224. nd->u.scr.rid = sd->bl.id;
  8225. }
  8226. npc_timerevent_start(nd, st->rid);
  8227. return SCRIPT_CMD_SUCCESS;
  8228. }
  8229. /*==========================================
  8230. *------------------------------------------*/
  8231. BUILDIN_FUNC(stopnpctimer)
  8232. {
  8233. struct npc_data *nd;
  8234. int flag = 0;
  8235. if( script_hasdata(st,3) )
  8236. { //Two arguments: NPC name and attach flag.
  8237. nd = npc_name2id(script_getstr(st, 2));
  8238. flag = script_getnum(st,3);
  8239. }
  8240. else if( script_hasdata(st,2) )
  8241. { //Check if argument is numeric (flag) or string (npc name)
  8242. struct script_data *data;
  8243. data = script_getdata(st,2);
  8244. get_val(st,data);
  8245. if( data_isstring(data) ) //NPC name
  8246. nd = npc_name2id(conv_str(st, data));
  8247. else if( data_isint(data) ) //Flag
  8248. {
  8249. nd = (struct npc_data *)map_id2bl(st->oid);
  8250. flag = conv_num(st,data);
  8251. }
  8252. else
  8253. {
  8254. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8255. return 1;
  8256. }
  8257. }
  8258. else
  8259. nd=(struct npc_data *)map_id2bl(st->oid);
  8260. if( !nd )
  8261. return 0;
  8262. if( flag ) //Detach
  8263. nd->u.scr.rid = 0;
  8264. npc_timerevent_stop(nd);
  8265. return SCRIPT_CMD_SUCCESS;
  8266. }
  8267. /*==========================================
  8268. *------------------------------------------*/
  8269. BUILDIN_FUNC(getnpctimer)
  8270. {
  8271. struct npc_data *nd;
  8272. TBL_PC *sd;
  8273. int type = script_getnum(st,2);
  8274. int val = 0;
  8275. if( script_hasdata(st,3) )
  8276. nd = npc_name2id(script_getstr(st,3));
  8277. else
  8278. nd = (struct npc_data *)map_id2bl(st->oid);
  8279. if( !nd || nd->bl.type != BL_NPC )
  8280. {
  8281. script_pushint(st,0);
  8282. ShowError("getnpctimer: Invalid NPC.\n");
  8283. return 1;
  8284. }
  8285. switch( type )
  8286. {
  8287. case 0: val = npc_gettimerevent_tick(nd); break;
  8288. case 1:
  8289. if( nd->u.scr.rid )
  8290. {
  8291. sd = map_id2sd(nd->u.scr.rid);
  8292. if( !sd )
  8293. {
  8294. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8295. break;
  8296. }
  8297. val = (sd->npc_timer_id != INVALID_TIMER);
  8298. }
  8299. else
  8300. val = (nd->u.scr.timerid != INVALID_TIMER);
  8301. break;
  8302. case 2: val = nd->u.scr.timeramount; break;
  8303. }
  8304. script_pushint(st,val);
  8305. return SCRIPT_CMD_SUCCESS;
  8306. }
  8307. /*==========================================
  8308. *------------------------------------------*/
  8309. BUILDIN_FUNC(setnpctimer)
  8310. {
  8311. int tick;
  8312. struct npc_data *nd;
  8313. tick = script_getnum(st,2);
  8314. if( script_hasdata(st,3) )
  8315. nd = npc_name2id(script_getstr(st,3));
  8316. else
  8317. nd = (struct npc_data *)map_id2bl(st->oid);
  8318. if( !nd || nd->bl.type != BL_NPC )
  8319. {
  8320. script_pushint(st,1);
  8321. ShowError("setnpctimer: Invalid NPC.\n");
  8322. return 1;
  8323. }
  8324. npc_settimerevent_tick(nd,tick);
  8325. script_pushint(st,0);
  8326. return SCRIPT_CMD_SUCCESS;
  8327. }
  8328. /*==========================================
  8329. * attaches the player rid to the timer [Celest]
  8330. *------------------------------------------*/
  8331. BUILDIN_FUNC(attachnpctimer)
  8332. {
  8333. TBL_PC *sd;
  8334. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  8335. if( !nd || nd->bl.type != BL_NPC )
  8336. {
  8337. script_pushint(st,1);
  8338. ShowError("setnpctimer: Invalid NPC.\n");
  8339. return 1;
  8340. }
  8341. if( script_hasdata(st,2) )
  8342. sd = map_nick2sd(script_getstr(st,2));
  8343. else
  8344. sd = script_rid2sd(st);
  8345. if( !sd )
  8346. {
  8347. script_pushint(st,1);
  8348. ShowWarning("attachnpctimer: Invalid player.\n");
  8349. return 1;
  8350. }
  8351. nd->u.scr.rid = sd->bl.id;
  8352. script_pushint(st,0);
  8353. return SCRIPT_CMD_SUCCESS;
  8354. }
  8355. /*==========================================
  8356. * detaches a player rid from the timer [Celest]
  8357. *------------------------------------------*/
  8358. BUILDIN_FUNC(detachnpctimer)
  8359. {
  8360. struct npc_data *nd;
  8361. if( script_hasdata(st,2) )
  8362. nd = npc_name2id(script_getstr(st,2));
  8363. else
  8364. nd = (struct npc_data *)map_id2bl(st->oid);
  8365. if( !nd || nd->bl.type != BL_NPC )
  8366. {
  8367. script_pushint(st,1);
  8368. ShowError("detachnpctimer: Invalid NPC.\n");
  8369. return 1;
  8370. }
  8371. nd->u.scr.rid = 0;
  8372. script_pushint(st,0);
  8373. return SCRIPT_CMD_SUCCESS;
  8374. }
  8375. /*==========================================
  8376. * To avoid "player not attached" script errors, this function is provided,
  8377. * it checks if there is a player attached to the current script. [Skotlex]
  8378. * If no, returns 0, if yes, returns the account_id of the attached player.
  8379. *------------------------------------------*/
  8380. BUILDIN_FUNC(playerattached)
  8381. {
  8382. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  8383. script_pushint(st,0);
  8384. else
  8385. script_pushint(st,st->rid);
  8386. return SCRIPT_CMD_SUCCESS;
  8387. }
  8388. /*==========================================
  8389. *------------------------------------------*/
  8390. BUILDIN_FUNC(announce)
  8391. {
  8392. const char *mes = script_getstr(st,2);
  8393. int flag = script_getnum(st,3);
  8394. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  8395. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  8396. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  8397. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  8398. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  8399. if (flag&0x0f) // Broadcast source or broadcast region defined
  8400. {
  8401. send_target target;
  8402. struct block_list *bl = (flag&0x08) ? map_id2bl(st->oid) : (struct block_list *)script_rid2sd(st); // If bc_npc flag is set, use NPC as broadcast source
  8403. if (bl == NULL)
  8404. return 0;
  8405. flag &= 0x07;
  8406. target = (flag == 1) ? ALL_SAMEMAP :
  8407. (flag == 2) ? AREA :
  8408. (flag == 3) ? SELF :
  8409. ALL_CLIENT;
  8410. if (fontColor)
  8411. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  8412. else
  8413. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  8414. }
  8415. else
  8416. {
  8417. if (fontColor)
  8418. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  8419. else
  8420. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  8421. }
  8422. return SCRIPT_CMD_SUCCESS;
  8423. }
  8424. /*==========================================
  8425. *------------------------------------------*/
  8426. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  8427. {
  8428. char *mes = va_arg(ap, char *);
  8429. int len = va_arg(ap, int);
  8430. int type = va_arg(ap, int);
  8431. char *fontColor = va_arg(ap, char *);
  8432. short fontType = (short)va_arg(ap, int);
  8433. short fontSize = (short)va_arg(ap, int);
  8434. short fontAlign = (short)va_arg(ap, int);
  8435. short fontY = (short)va_arg(ap, int);
  8436. if (fontColor)
  8437. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  8438. else
  8439. clif_broadcast(bl, mes, len, type, SELF);
  8440. return 0;
  8441. }
  8442. BUILDIN_FUNC(mapannounce)
  8443. {
  8444. const char *mapname = script_getstr(st,2);
  8445. const char *mes = script_getstr(st,3);
  8446. int flag = script_getnum(st,4);
  8447. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  8448. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  8449. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  8450. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  8451. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  8452. int16 m;
  8453. if ((m = map_mapname2mapid(mapname)) < 0)
  8454. return 0;
  8455. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  8456. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8457. return SCRIPT_CMD_SUCCESS;
  8458. }
  8459. /*==========================================
  8460. *------------------------------------------*/
  8461. BUILDIN_FUNC(areaannounce)
  8462. {
  8463. const char *mapname = script_getstr(st,2);
  8464. int x0 = script_getnum(st,3);
  8465. int y0 = script_getnum(st,4);
  8466. int x1 = script_getnum(st,5);
  8467. int y1 = script_getnum(st,6);
  8468. const char *mes = script_getstr(st,7);
  8469. int flag = script_getnum(st,8);
  8470. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  8471. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  8472. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  8473. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  8474. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  8475. int16 m;
  8476. if ((m = map_mapname2mapid(mapname)) < 0)
  8477. return 0;
  8478. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  8479. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8480. return SCRIPT_CMD_SUCCESS;
  8481. }
  8482. /*==========================================
  8483. *------------------------------------------*/
  8484. BUILDIN_FUNC(getusers)
  8485. {
  8486. int flag, val = 0;
  8487. struct map_session_data* sd;
  8488. struct block_list* bl = NULL;
  8489. flag = script_getnum(st,2);
  8490. switch(flag&0x07)
  8491. {
  8492. case 0:
  8493. if(flag&0x8)
  8494. {// npc
  8495. bl = map_id2bl(st->oid);
  8496. }
  8497. else if((sd = script_rid2sd(st))!=NULL)
  8498. {// pc
  8499. bl = &sd->bl;
  8500. }
  8501. if(bl)
  8502. {
  8503. val = map[bl->m].users;
  8504. }
  8505. break;
  8506. case 1:
  8507. val = map_getusers();
  8508. break;
  8509. default:
  8510. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  8511. script_pushint(st,0);
  8512. return 1;
  8513. }
  8514. script_pushint(st,val);
  8515. return SCRIPT_CMD_SUCCESS;
  8516. }
  8517. /*==========================================
  8518. * Works like @WHO - displays all online users names in window
  8519. *------------------------------------------*/
  8520. BUILDIN_FUNC(getusersname)
  8521. {
  8522. TBL_PC *sd, *pl_sd;
  8523. int /*disp_num=1,*/ group_level = 0;
  8524. struct s_mapiterator* iter;
  8525. sd = script_rid2sd(st);
  8526. if (!sd) return 0;
  8527. group_level = pc_get_group_level(sd);
  8528. iter = mapit_getallusers();
  8529. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  8530. {
  8531. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  8532. continue; // skip hidden sessions
  8533. /* Temporary fix for bugreport:1023.
  8534. * Do not uncomment unless you want thousands of 'next' buttons.
  8535. if((disp_num++)%10==0)
  8536. clif_scriptnext(sd,st->oid);*/
  8537. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  8538. }
  8539. mapit_free(iter);
  8540. return SCRIPT_CMD_SUCCESS;
  8541. }
  8542. /*==========================================
  8543. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  8544. *------------------------------------------*/
  8545. BUILDIN_FUNC(getmapguildusers)
  8546. {
  8547. const char *str;
  8548. int16 m;
  8549. int gid;
  8550. int i=0,c=0;
  8551. struct guild *g = NULL;
  8552. str=script_getstr(st,2);
  8553. gid=script_getnum(st,3);
  8554. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  8555. script_pushint(st,-1);
  8556. return 0;
  8557. }
  8558. g = guild_search(gid);
  8559. if (g){
  8560. for(i = 0; i < g->max_member; i++)
  8561. {
  8562. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  8563. c++;
  8564. }
  8565. }
  8566. script_pushint(st,c);
  8567. return SCRIPT_CMD_SUCCESS;
  8568. }
  8569. /*==========================================
  8570. *------------------------------------------*/
  8571. BUILDIN_FUNC(getmapusers)
  8572. {
  8573. const char *str;
  8574. int16 m;
  8575. str=script_getstr(st,2);
  8576. if( (m=map_mapname2mapid(str))< 0){
  8577. script_pushint(st,-1);
  8578. return 0;
  8579. }
  8580. script_pushint(st,map[m].users);
  8581. return SCRIPT_CMD_SUCCESS;
  8582. }
  8583. /*==========================================
  8584. *------------------------------------------*/
  8585. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  8586. {
  8587. int *users=va_arg(ap,int *);
  8588. (*users)++;
  8589. return 0;
  8590. }
  8591. BUILDIN_FUNC(getareausers)
  8592. {
  8593. const char *str;
  8594. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  8595. str=script_getstr(st,2);
  8596. x0=script_getnum(st,3);
  8597. y0=script_getnum(st,4);
  8598. x1=script_getnum(st,5);
  8599. y1=script_getnum(st,6);
  8600. if( (m=map_mapname2mapid(str))< 0){
  8601. script_pushint(st,-1);
  8602. return 0;
  8603. }
  8604. map_foreachinarea(buildin_getareausers_sub,
  8605. m,x0,y0,x1,y1,BL_PC,&users);
  8606. script_pushint(st,users);
  8607. return SCRIPT_CMD_SUCCESS;
  8608. }
  8609. /*==========================================
  8610. *------------------------------------------*/
  8611. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  8612. {
  8613. int item=va_arg(ap,int);
  8614. int *amount=va_arg(ap,int *);
  8615. struct flooritem_data *drop=(struct flooritem_data *)bl;
  8616. if(drop->item_data.nameid==item)
  8617. (*amount)+=drop->item_data.amount;
  8618. return 0;
  8619. }
  8620. BUILDIN_FUNC(getareadropitem)
  8621. {
  8622. const char *str;
  8623. int16 m,x0,y0,x1,y1;
  8624. int item,amount=0;
  8625. struct script_data *data;
  8626. str=script_getstr(st,2);
  8627. x0=script_getnum(st,3);
  8628. y0=script_getnum(st,4);
  8629. x1=script_getnum(st,5);
  8630. y1=script_getnum(st,6);
  8631. data=script_getdata(st,7);
  8632. get_val(st,data);
  8633. if( data_isstring(data) ){
  8634. const char *name=conv_str(st,data);
  8635. struct item_data *item_data = itemdb_searchname(name);
  8636. item=UNKNOWN_ITEM_ID;
  8637. if( item_data )
  8638. item=item_data->nameid;
  8639. }else
  8640. item=conv_num(st,data);
  8641. if( (m=map_mapname2mapid(str))< 0){
  8642. script_pushint(st,-1);
  8643. return 0;
  8644. }
  8645. map_foreachinarea(buildin_getareadropitem_sub,
  8646. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  8647. script_pushint(st,amount);
  8648. return SCRIPT_CMD_SUCCESS;
  8649. }
  8650. /*==========================================
  8651. *------------------------------------------*/
  8652. BUILDIN_FUNC(enablenpc)
  8653. {
  8654. const char *str;
  8655. str=script_getstr(st,2);
  8656. npc_enable(str,1);
  8657. return SCRIPT_CMD_SUCCESS;
  8658. }
  8659. /*==========================================
  8660. *------------------------------------------*/
  8661. BUILDIN_FUNC(disablenpc)
  8662. {
  8663. const char *str;
  8664. str=script_getstr(st,2);
  8665. npc_enable(str,0);
  8666. return SCRIPT_CMD_SUCCESS;
  8667. }
  8668. /*==========================================
  8669. *------------------------------------------*/
  8670. BUILDIN_FUNC(hideoffnpc)
  8671. {
  8672. const char *str;
  8673. str=script_getstr(st,2);
  8674. npc_enable(str,2);
  8675. return SCRIPT_CMD_SUCCESS;
  8676. }
  8677. /*==========================================
  8678. *------------------------------------------*/
  8679. BUILDIN_FUNC(hideonnpc)
  8680. {
  8681. const char *str;
  8682. str=script_getstr(st,2);
  8683. npc_enable(str,4);
  8684. return SCRIPT_CMD_SUCCESS;
  8685. }
  8686. /* Starts a status effect on the target unit or on the attached player.
  8687. *
  8688. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  8689. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  8690. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  8691. * <flag>
  8692. * &1: Cannot be avoided (it has to start)
  8693. * &2: Tick should not be reduced (by vit, luk, lv, etc)
  8694. * &4: sc_data loaded, no value has to be altered.
  8695. * &8: rate should not be reduced
  8696. */
  8697. BUILDIN_FUNC(sc_start)
  8698. {
  8699. TBL_NPC * nd = map_id2nd(st->oid);
  8700. struct block_list* bl;
  8701. enum sc_type type;
  8702. int tick, val1, val2, val3, val4=0, rate, flag;
  8703. char start_type;
  8704. const char* command = script_getfuncname(st);
  8705. if(strstr(command, "4"))
  8706. start_type = 4;
  8707. else if(strstr(command, "2"))
  8708. start_type = 2;
  8709. else
  8710. start_type = 1;
  8711. type = (sc_type)script_getnum(st,2);
  8712. tick = script_getnum(st,3);
  8713. val1 = script_getnum(st,4);
  8714. //If from NPC we make default flag 1 to be unavoidable
  8715. if(nd && nd->bl.id == fake_nd->bl.id)
  8716. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):2;
  8717. else
  8718. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):1;
  8719. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  8720. if(script_hasdata(st,(6+start_type)))
  8721. bl = map_id2bl(script_getnum(st,(6+start_type)));
  8722. else
  8723. bl = map_id2bl(st->rid);
  8724. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  8725. {// When there isn't a duration specified, try to get it from the skill_db
  8726. tick = skill_get_time(status_sc2skill(type), val1);
  8727. }
  8728. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  8729. bl = map_id2bl(potion_target);
  8730. tick /= 2;// Thrown potions only last half.
  8731. val4 = 1;// Mark that this was a thrown sc_effect
  8732. }
  8733. if(!bl)
  8734. return 0;
  8735. switch(start_type) {
  8736. case 1:
  8737. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  8738. break;
  8739. case 2:
  8740. val2 = script_getnum(st,5);
  8741. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  8742. break;
  8743. case 4:
  8744. val2 = script_getnum(st,5);
  8745. val3 = script_getnum(st,6);
  8746. val4 = script_getnum(st,7);
  8747. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  8748. break;
  8749. }
  8750. return SCRIPT_CMD_SUCCESS;
  8751. }
  8752. /// Ends one or all status effects on the target unit or on the attached player.
  8753. ///
  8754. /// sc_end <effect_id>{,<unit_id>};
  8755. BUILDIN_FUNC(sc_end)
  8756. {
  8757. struct block_list* bl;
  8758. int type;
  8759. type = script_getnum(st, 2);
  8760. if (script_hasdata(st, 3))
  8761. bl = map_id2bl(script_getnum(st, 3));
  8762. else
  8763. bl = map_id2bl(st->rid);
  8764. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  8765. bl = map_id2bl(potion_target);
  8766. if (!bl)
  8767. return 0;
  8768. if (type >= 0 && type < SC_MAX)
  8769. {
  8770. struct status_change *sc = status_get_sc(bl);
  8771. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  8772. if (!sce)
  8773. return 0;
  8774. switch (type)
  8775. {
  8776. case SC_WEIGHT50:
  8777. case SC_WEIGHT90:
  8778. case SC_NOCHAT:
  8779. case SC_PUSH_CART:
  8780. return 0;
  8781. default:
  8782. break;
  8783. }
  8784. //This should help status_change_end force disabling the SC in case it has no limit.
  8785. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8786. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8787. }
  8788. else
  8789. status_change_clear(bl, 3); // remove all effects
  8790. return SCRIPT_CMD_SUCCESS;
  8791. }
  8792. /*==========================================
  8793. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  8794. *------------------------------------------*/
  8795. BUILDIN_FUNC(getscrate)
  8796. {
  8797. struct block_list *bl;
  8798. int type,rate;
  8799. type=script_getnum(st,2);
  8800. rate=script_getnum(st,3);
  8801. if( script_hasdata(st,4) ) //get for the bl assigned
  8802. bl = map_id2bl(script_getnum(st,4));
  8803. else
  8804. bl = map_id2bl(st->rid);
  8805. if (bl)
  8806. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, 0);
  8807. script_pushint(st,rate);
  8808. return SCRIPT_CMD_SUCCESS;
  8809. }
  8810. /*==========================================
  8811. * getstatus <type>{, <info>};
  8812. *------------------------------------------*/
  8813. BUILDIN_FUNC(getstatus)
  8814. {
  8815. int id, type;
  8816. struct map_session_data* sd = script_rid2sd(st);
  8817. if( sd == NULL )
  8818. {// no player attached
  8819. return 0;
  8820. }
  8821. id = script_getnum(st, 2);
  8822. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  8823. if( id <= SC_NONE || id >= SC_MAX )
  8824. {// invalid status type given
  8825. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  8826. return 0;
  8827. }
  8828. if( sd->sc.count == 0 || !sd->sc.data[id] )
  8829. {// no status is active
  8830. script_pushint(st, 0);
  8831. return 0;
  8832. }
  8833. switch( type )
  8834. {
  8835. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  8836. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  8837. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  8838. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  8839. case 5:
  8840. {
  8841. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  8842. if( timer )
  8843. {// return the amount of time remaining
  8844. script_pushint(st, timer->tick - gettick());
  8845. }
  8846. }
  8847. break;
  8848. default: script_pushint(st, 1); break;
  8849. }
  8850. return SCRIPT_CMD_SUCCESS;
  8851. }
  8852. /*==========================================
  8853. *
  8854. *------------------------------------------*/
  8855. BUILDIN_FUNC(debugmes)
  8856. {
  8857. const char *str;
  8858. str=script_getstr(st,2);
  8859. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8860. return SCRIPT_CMD_SUCCESS;
  8861. }
  8862. /*==========================================
  8863. *------------------------------------------*/
  8864. BUILDIN_FUNC(catchpet)
  8865. {
  8866. int pet_id;
  8867. TBL_PC *sd;
  8868. pet_id= script_getnum(st,2);
  8869. sd=script_rid2sd(st);
  8870. if( sd == NULL )
  8871. return 0;
  8872. pet_catch_process1(sd,pet_id);
  8873. return SCRIPT_CMD_SUCCESS;
  8874. }
  8875. /*==========================================
  8876. * [orn]
  8877. *------------------------------------------*/
  8878. BUILDIN_FUNC(homunculus_evolution)
  8879. {
  8880. TBL_PC *sd;
  8881. sd=script_rid2sd(st);
  8882. if( sd == NULL )
  8883. return 0;
  8884. if(merc_is_hom_active(sd->hd))
  8885. {
  8886. if (sd->hd->homunculus.intimacy > 91000)
  8887. merc_hom_evolution(sd->hd);
  8888. else
  8889. clif_emotion(&sd->hd->bl, E_SWT);
  8890. }
  8891. return SCRIPT_CMD_SUCCESS;
  8892. }
  8893. /*==========================================
  8894. * Checks for vaporized morph state
  8895. * and deletes ITEMID_STRANGE_EMBRYO.
  8896. *------------------------------------------*/
  8897. BUILDIN_FUNC(homunculus_mutate)
  8898. {
  8899. int homun_id, m_class, m_id, i;
  8900. TBL_PC *sd;
  8901. sd = script_rid2sd(st);
  8902. if( sd == NULL || sd->hd == NULL )
  8903. return 0;
  8904. if(script_hasdata(st,2))
  8905. homun_id = script_getnum(st,2);
  8906. else
  8907. homun_id = 6048 + (rnd() % 4);
  8908. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  8909. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8910. m_id = hom_class2mapid(homun_id);
  8911. i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  8912. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  8913. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  8914. merc_call_homunculus(sd); // Respawn homunculus.
  8915. hom_mutate(sd->hd, homun_id);
  8916. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  8917. script_pushint(st, 1);
  8918. return 0;
  8919. } else
  8920. clif_emotion(&sd->bl, E_SWT);
  8921. } else
  8922. clif_emotion(&sd->bl, E_SWT);
  8923. script_pushint(st, 0);
  8924. return SCRIPT_CMD_SUCCESS;
  8925. }
  8926. /*==========================================
  8927. * Puts homunculus into morph state
  8928. * and gives ITEMID_STRANGE_EMBRYO.
  8929. *------------------------------------------*/
  8930. BUILDIN_FUNC(morphembryo)
  8931. {
  8932. struct item item_tmp;
  8933. int m_class, i=0;
  8934. TBL_PC *sd;
  8935. sd = script_rid2sd(st);
  8936. if( sd == NULL || sd->hd == NULL )
  8937. return 0;
  8938. if( merc_is_hom_active(sd->hd) ) {
  8939. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8940. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  8941. memset(&item_tmp, 0, sizeof(item_tmp));
  8942. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  8943. item_tmp.identify = 1;
  8944. if( item_tmp.nameid == 0 || (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  8945. clif_additem(sd, 0, 0, i);
  8946. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  8947. } else {
  8948. merc_hom_vaporize(sd, HOM_ST_MORPH);
  8949. script_pushint(st, 1);
  8950. return 0;
  8951. }
  8952. } else
  8953. clif_emotion(&sd->hd->bl, E_SWT);
  8954. } else
  8955. clif_emotion(&sd->bl, E_SWT);
  8956. script_pushint(st, 0);
  8957. return SCRIPT_CMD_SUCCESS;
  8958. }
  8959. // [Zephyrus]
  8960. BUILDIN_FUNC(homunculus_shuffle)
  8961. {
  8962. TBL_PC *sd;
  8963. sd=script_rid2sd(st);
  8964. if( sd == NULL )
  8965. return 0;
  8966. if(merc_is_hom_active(sd->hd))
  8967. merc_hom_shuffle(sd->hd);
  8968. return SCRIPT_CMD_SUCCESS;
  8969. }
  8970. /*==========================================
  8971. * Check for homunculus state.
  8972. * Return: -1 = No homunculus
  8973. * 0 = Homunculus is active
  8974. * 1 = Homunculus is vaporized (rest)
  8975. * 2 = Homunculus is in morph state
  8976. *------------------------------------------*/
  8977. BUILDIN_FUNC(checkhomcall)
  8978. {
  8979. TBL_PC *sd = script_rid2sd(st);
  8980. TBL_HOM *hd;
  8981. if( sd == NULL )
  8982. return 0;
  8983. hd = sd->hd;
  8984. if( !hd )
  8985. script_pushint(st, -1);
  8986. else
  8987. script_pushint(st, hd->homunculus.vaporize);
  8988. return SCRIPT_CMD_SUCCESS;
  8989. }
  8990. //These two functions bring the eA MAPID_* class functionality to scripts.
  8991. BUILDIN_FUNC(eaclass)
  8992. {
  8993. int class_;
  8994. if( script_hasdata(st,2) )
  8995. class_ = script_getnum(st,2);
  8996. else {
  8997. TBL_PC *sd;
  8998. sd=script_rid2sd(st);
  8999. if (!sd) {
  9000. script_pushint(st,-1);
  9001. return 0;
  9002. }
  9003. class_ = sd->status.class_;
  9004. }
  9005. script_pushint(st,pc_jobid2mapid(class_));
  9006. return SCRIPT_CMD_SUCCESS;
  9007. }
  9008. BUILDIN_FUNC(roclass)
  9009. {
  9010. int class_ =script_getnum(st,2);
  9011. int sex;
  9012. if( script_hasdata(st,3) )
  9013. sex = script_getnum(st,3);
  9014. else {
  9015. TBL_PC *sd;
  9016. if (st->rid && (sd=script_rid2sd(st)))
  9017. sex = sd->status.sex;
  9018. else
  9019. sex = 1; //Just use male when not found.
  9020. }
  9021. script_pushint(st,pc_mapid2jobid(class_, sex));
  9022. return SCRIPT_CMD_SUCCESS;
  9023. }
  9024. /*==========================================
  9025. * Tells client to open a hatching window, used for pet incubator
  9026. *------------------------------------------*/
  9027. BUILDIN_FUNC(birthpet)
  9028. {
  9029. TBL_PC *sd;
  9030. sd=script_rid2sd(st);
  9031. if( sd == NULL )
  9032. return 0;
  9033. if( sd->status.pet_id )
  9034. {// do not send egg list, when you already have a pet
  9035. return 0;
  9036. }
  9037. clif_sendegg(sd);
  9038. return SCRIPT_CMD_SUCCESS;
  9039. }
  9040. /*==========================================
  9041. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  9042. * @type
  9043. * 1 : make like after rebirth
  9044. * 2 : blvl,jlvl=1, skillpoint=0
  9045. * 3 : don't reset skill, blvl=1
  9046. * 4 : jlvl=0
  9047. *------------------------------------------*/
  9048. BUILDIN_FUNC(resetlvl)
  9049. {
  9050. TBL_PC *sd;
  9051. int type=script_getnum(st,2);
  9052. sd=script_rid2sd(st);
  9053. if( sd == NULL )
  9054. return 0;
  9055. pc_resetlvl(sd,type);
  9056. return SCRIPT_CMD_SUCCESS;
  9057. }
  9058. /*==========================================
  9059. * Reset a player status point
  9060. *------------------------------------------*/
  9061. BUILDIN_FUNC(resetstatus)
  9062. {
  9063. TBL_PC *sd;
  9064. sd=script_rid2sd(st);
  9065. pc_resetstate(sd);
  9066. return SCRIPT_CMD_SUCCESS;
  9067. }
  9068. /*==========================================
  9069. * script command resetskill
  9070. *------------------------------------------*/
  9071. BUILDIN_FUNC(resetskill)
  9072. {
  9073. TBL_PC *sd;
  9074. sd=script_rid2sd(st);
  9075. pc_resetskill(sd,1);
  9076. return SCRIPT_CMD_SUCCESS;
  9077. }
  9078. /*==========================================
  9079. * Counts total amount of skill points.
  9080. *------------------------------------------*/
  9081. BUILDIN_FUNC(skillpointcount)
  9082. {
  9083. TBL_PC *sd;
  9084. sd=script_rid2sd(st);
  9085. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9086. return SCRIPT_CMD_SUCCESS;
  9087. }
  9088. /*==========================================
  9089. *
  9090. *------------------------------------------*/
  9091. BUILDIN_FUNC(changebase)
  9092. {
  9093. TBL_PC *sd=NULL;
  9094. int vclass;
  9095. if( script_hasdata(st,3) )
  9096. sd=map_id2sd(script_getnum(st,3));
  9097. else
  9098. sd=script_rid2sd(st);
  9099. if(sd == NULL)
  9100. return 0;
  9101. vclass = script_getnum(st,2);
  9102. if(vclass == JOB_WEDDING)
  9103. {
  9104. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9105. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9106. )
  9107. return 0;
  9108. }
  9109. if(!sd->disguise && vclass != sd->vd.class_) {
  9110. status_set_viewdata(&sd->bl, vclass);
  9111. //Updated client view. Base, Weapon and Cloth Colors.
  9112. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9113. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9114. if (sd->vd.cloth_color)
  9115. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9116. clif_skillinfoblock(sd);
  9117. }
  9118. return SCRIPT_CMD_SUCCESS;
  9119. }
  9120. /*==========================================
  9121. * Unequip all item and request for a changesex to char-serv
  9122. *------------------------------------------*/
  9123. BUILDIN_FUNC(changesex)
  9124. {
  9125. int i;
  9126. TBL_PC *sd = NULL;
  9127. sd = script_rid2sd(st);
  9128. pc_resetskill(sd,4);
  9129. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9130. for( i=0; i<EQI_MAX; i++ )
  9131. if( sd->equip_index[i] >= 0 ) pc_unequipitem(sd, sd->equip_index[i], 3);
  9132. chrif_changesex(sd);
  9133. return SCRIPT_CMD_SUCCESS;
  9134. }
  9135. /*==========================================
  9136. * Works like 'announce' but outputs in the common chat window
  9137. *------------------------------------------*/
  9138. BUILDIN_FUNC(globalmes)
  9139. {
  9140. struct block_list *bl = map_id2bl(st->oid);
  9141. struct npc_data *nd = (struct npc_data *)bl;
  9142. const char *name=NULL,*mes;
  9143. mes=script_getstr(st,2);
  9144. if(mes==NULL) return 0;
  9145. if(script_hasdata(st,3)){ // npc name to display
  9146. name=script_getstr(st,3);
  9147. } else {
  9148. name=nd->name; //use current npc name
  9149. }
  9150. npc_globalmessage(name,mes); // broadcast to all players connected
  9151. return SCRIPT_CMD_SUCCESS;
  9152. }
  9153. /////////////////////////////////////////////////////////////////////
  9154. // NPC waiting room (chat room)
  9155. //
  9156. /// Creates a waiting room (chat room) for this npc.
  9157. ///
  9158. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9159. BUILDIN_FUNC(waitingroom)
  9160. {
  9161. struct npc_data* nd;
  9162. int pub = 1;
  9163. const char* title = script_getstr(st, 2);
  9164. int limit = script_getnum(st, 3);
  9165. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9166. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9167. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9168. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9169. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9170. nd = (struct npc_data *)map_id2bl(st->oid);
  9171. if( nd != NULL )
  9172. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  9173. return SCRIPT_CMD_SUCCESS;
  9174. }
  9175. /// Removes the waiting room of the current or target npc.
  9176. ///
  9177. /// delwaitingroom "<npc_name>";
  9178. /// delwaitingroom;
  9179. BUILDIN_FUNC(delwaitingroom)
  9180. {
  9181. struct npc_data* nd;
  9182. if( script_hasdata(st,2) )
  9183. nd = npc_name2id(script_getstr(st, 2));
  9184. else
  9185. nd = (struct npc_data *)map_id2bl(st->oid);
  9186. if( nd != NULL )
  9187. chat_deletenpcchat(nd);
  9188. return SCRIPT_CMD_SUCCESS;
  9189. }
  9190. /// Kicks all the players from the waiting room of the current or target npc.
  9191. ///
  9192. /// kickwaitingroomall "<npc_name>";
  9193. /// kickwaitingroomall;
  9194. BUILDIN_FUNC(waitingroomkickall)
  9195. {
  9196. struct npc_data* nd;
  9197. struct chat_data* cd;
  9198. if( script_hasdata(st,2) )
  9199. nd = npc_name2id(script_getstr(st,2));
  9200. else
  9201. nd = (struct npc_data *)map_id2bl(st->oid);
  9202. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9203. chat_npckickall(cd);
  9204. return SCRIPT_CMD_SUCCESS;
  9205. }
  9206. /// Enables the waiting room event of the current or target npc.
  9207. ///
  9208. /// enablewaitingroomevent "<npc_name>";
  9209. /// enablewaitingroomevent;
  9210. BUILDIN_FUNC(enablewaitingroomevent)
  9211. {
  9212. struct npc_data* nd;
  9213. struct chat_data* cd;
  9214. if( script_hasdata(st,2) )
  9215. nd = npc_name2id(script_getstr(st, 2));
  9216. else
  9217. nd = (struct npc_data *)map_id2bl(st->oid);
  9218. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9219. chat_enableevent(cd);
  9220. return SCRIPT_CMD_SUCCESS;
  9221. }
  9222. /// Disables the waiting room event of the current or target npc.
  9223. ///
  9224. /// disablewaitingroomevent "<npc_name>";
  9225. /// disablewaitingroomevent;
  9226. BUILDIN_FUNC(disablewaitingroomevent)
  9227. {
  9228. struct npc_data *nd;
  9229. struct chat_data *cd;
  9230. if( script_hasdata(st,2) )
  9231. nd = npc_name2id(script_getstr(st, 2));
  9232. else
  9233. nd = (struct npc_data *)map_id2bl(st->oid);
  9234. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9235. chat_disableevent(cd);
  9236. return SCRIPT_CMD_SUCCESS;
  9237. }
  9238. /// Returns info on the waiting room of the current or target npc.
  9239. /// Returns -1 if the type unknown
  9240. /// <type>=0 : current number of users
  9241. /// <type>=1 : maximum number of users allowed
  9242. /// <type>=2 : the number of users that trigger the event
  9243. /// <type>=3 : if the trigger is disabled
  9244. /// <type>=4 : the title of the waiting room
  9245. /// <type>=5 : the password of the waiting room
  9246. /// <type>=16 : the name of the waiting room event
  9247. /// <type>=32 : if the waiting room is full
  9248. /// <type>=33 : if there are enough users to trigger the event
  9249. ///
  9250. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9251. /// getwaitingroomstate(<type>) -> <info>
  9252. BUILDIN_FUNC(getwaitingroomstate)
  9253. {
  9254. struct npc_data *nd;
  9255. struct chat_data *cd;
  9256. int type;
  9257. type = script_getnum(st,2);
  9258. if( script_hasdata(st,3) )
  9259. nd = npc_name2id(script_getstr(st, 3));
  9260. else
  9261. nd = (struct npc_data *)map_id2bl(st->oid);
  9262. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9263. {
  9264. script_pushint(st, -1);
  9265. return 0;
  9266. }
  9267. switch(type)
  9268. {
  9269. case 0: script_pushint(st, cd->users); break;
  9270. case 1: script_pushint(st, cd->limit); break;
  9271. case 2: script_pushint(st, cd->trigger&0x7f); break;
  9272. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  9273. case 4: script_pushstrcopy(st, cd->title); break;
  9274. case 5: script_pushstrcopy(st, cd->pass); break;
  9275. case 16: script_pushstrcopy(st, cd->npc_event);break;
  9276. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  9277. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  9278. default: script_pushint(st, -1); break;
  9279. }
  9280. return SCRIPT_CMD_SUCCESS;
  9281. }
  9282. /// Warps the trigger or target amount of players to the target map and position.
  9283. /// Players are automatically removed from the waiting room.
  9284. /// Those waiting the longest will get warped first.
  9285. /// The target map can be "Random" for a random position in the current map,
  9286. /// and "SavePoint" for the savepoint map+position.
  9287. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  9288. ///
  9289. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  9290. /// The total number of teleported players is put into $@warpwaitingpcnum
  9291. ///
  9292. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  9293. /// warpwaitingpc "<map name>",<x>,<y>;
  9294. BUILDIN_FUNC(warpwaitingpc)
  9295. {
  9296. int x;
  9297. int y;
  9298. int i;
  9299. int n;
  9300. const char* map_name;
  9301. struct npc_data* nd;
  9302. struct chat_data* cd;
  9303. TBL_PC* sd;
  9304. nd = (struct npc_data *)map_id2bl(st->oid);
  9305. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9306. return 0;
  9307. map_name = script_getstr(st,2);
  9308. x = script_getnum(st,3);
  9309. y = script_getnum(st,4);
  9310. n = cd->trigger&0x7f;
  9311. if( script_hasdata(st,5) )
  9312. n = script_getnum(st,5);
  9313. for( i = 0; i < n && cd->users > 0; i++ )
  9314. {
  9315. sd = cd->usersd[0];
  9316. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  9317. {// can't teleport on this map
  9318. break;
  9319. }
  9320. if( cd->zeny )
  9321. {// fee set
  9322. if( (uint32)sd->status.zeny < cd->zeny )
  9323. {// no zeny to cover set fee
  9324. break;
  9325. }
  9326. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  9327. }
  9328. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  9329. if( strcmp(map_name,"Random") == 0 )
  9330. pc_randomwarp(sd,CLR_TELEPORT);
  9331. else if( strcmp(map_name,"SavePoint") == 0 )
  9332. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  9333. else
  9334. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  9335. }
  9336. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  9337. return SCRIPT_CMD_SUCCESS;
  9338. }
  9339. /////////////////////////////////////////////////////////////////////
  9340. // ...
  9341. //
  9342. /// Detaches a character from a script.
  9343. ///
  9344. /// @param st Script state to detach the character from.
  9345. static void script_detach_rid(struct script_state* st)
  9346. {
  9347. if(st->rid)
  9348. {
  9349. script_detach_state(st, false);
  9350. st->rid = 0;
  9351. }
  9352. }
  9353. /*=========================================================================
  9354. * Attaches a set of RIDs to the current script. [digitalhamster]
  9355. * addrid(<type>{,<flag>{,<parameters>}});
  9356. * <type>:
  9357. * 0 : All players in the server.
  9358. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  9359. * 2 : Party members of a specified party ID.
  9360. * [ Parameters: <party id> ]
  9361. * 3 : Guild members of a specified guild ID.
  9362. * [ Parameters: <guild id> ]
  9363. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  9364. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  9365. * Account ID: The specified account ID.
  9366. * <flag>:
  9367. * 0 : Players are always attached. (default)
  9368. * 1 : Players currently running another script will not be attached.
  9369. *-------------------------------------------------------------------------*/
  9370. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  9371. {
  9372. int forceflag;
  9373. struct map_session_data *sd = (TBL_PC *)bl;
  9374. struct script_state* st;
  9375. st=va_arg(ap,struct script_state*);
  9376. forceflag=va_arg(ap,int);
  9377. if(!forceflag||!sd->st)
  9378. if(sd->status.account_id!=st->rid)
  9379. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9380. return 0;
  9381. }
  9382. BUILDIN_FUNC(addrid)
  9383. {
  9384. struct s_mapiterator* iter;
  9385. struct block_list *bl;
  9386. TBL_PC *sd;
  9387. if(st->rid<1){
  9388. st->state = END;
  9389. bl=map_id2bl(st->oid);
  9390. } else
  9391. bl=map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  9392. iter = mapit_getallusers();
  9393. switch(script_getnum(st,2)){
  9394. case 0:
  9395. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9396. if(!script_getnum(st,3)||!sd->st)
  9397. if(sd->status.account_id!=st->rid) //attached player already runs.
  9398. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9399. }
  9400. break;
  9401. case 1:
  9402. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9403. if(!script_getnum(st,3)||!sd->st)
  9404. if((sd->bl.m == bl->m)&&(sd->status.account_id!=st->rid))
  9405. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9406. }
  9407. break;
  9408. case 2:
  9409. if(script_getnum(st,4)==0){
  9410. script_pushint(st,0);
  9411. return 0;
  9412. }
  9413. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9414. if(!script_getnum(st,3)||!sd->st)
  9415. if((sd->status.account_id!=st->rid)&&(sd->status.party_id==script_getnum(st,4))) //attached player already runs.
  9416. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9417. }
  9418. break;
  9419. case 3:
  9420. if(script_getnum(st,4)==0){
  9421. script_pushint(st,0);
  9422. return 0;
  9423. }
  9424. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9425. if(!script_getnum(st,3)||!sd->st)
  9426. if((sd->status.account_id!=st->rid)&&(sd->status.guild_id==script_getnum(st,4))) //attached player already runs.
  9427. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9428. }
  9429. break;
  9430. case 4:
  9431. map_foreachinarea(buildin_addrid_sub,
  9432. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  9433. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  9434. break;
  9435. default:
  9436. if((map_id2sd(script_getnum(st,2)))==NULL){ // Player not found.
  9437. script_pushint(st,0);
  9438. return 0;
  9439. }
  9440. if(!script_getnum(st,3)||!map_id2sd(script_getnum(st,2))->st) {
  9441. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  9442. script_pushint(st,1);
  9443. }
  9444. return 0;
  9445. }
  9446. mapit_free(iter);
  9447. script_pushint(st,1);
  9448. return SCRIPT_CMD_SUCCESS;
  9449. }
  9450. /*==========================================
  9451. * Attach sd char id to script and detach current one if any
  9452. *------------------------------------------*/
  9453. BUILDIN_FUNC(attachrid)
  9454. {
  9455. int rid = script_getnum(st,2);
  9456. if (map_id2sd(rid) != NULL) {
  9457. script_detach_rid(st);
  9458. st->rid = rid;
  9459. script_attach_state(st);
  9460. script_pushint(st,1);
  9461. } else
  9462. script_pushint(st,0);
  9463. return SCRIPT_CMD_SUCCESS;
  9464. }
  9465. /*==========================================
  9466. * Detach script to rid
  9467. *------------------------------------------*/
  9468. BUILDIN_FUNC(detachrid)
  9469. {
  9470. script_detach_rid(st);
  9471. return SCRIPT_CMD_SUCCESS;
  9472. }
  9473. /*==========================================
  9474. * Chk if account connected, (and charid from account if specified)
  9475. *------------------------------------------*/
  9476. BUILDIN_FUNC(isloggedin)
  9477. {
  9478. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  9479. if (script_hasdata(st,3) && sd &&
  9480. sd->status.char_id != script_getnum(st,3))
  9481. sd = NULL;
  9482. push_val(st->stack,C_INT,sd!=NULL);
  9483. return SCRIPT_CMD_SUCCESS;
  9484. }
  9485. /*==========================================
  9486. *
  9487. *------------------------------------------*/
  9488. BUILDIN_FUNC(setmapflagnosave)
  9489. {
  9490. int16 m,x,y;
  9491. unsigned short mapindex;
  9492. const char *str,*str2;
  9493. str=script_getstr(st,2);
  9494. str2=script_getstr(st,3);
  9495. x=script_getnum(st,4);
  9496. y=script_getnum(st,5);
  9497. m = map_mapname2mapid(str);
  9498. mapindex = mapindex_name2id(str2);
  9499. if(m >= 0 && mapindex) {
  9500. map[m].flag.nosave=1;
  9501. map[m].save.map=mapindex;
  9502. map[m].save.x=x;
  9503. map[m].save.y=y;
  9504. }
  9505. return SCRIPT_CMD_SUCCESS;
  9506. }
  9507. BUILDIN_FUNC(getmapflag)
  9508. {
  9509. int16 m,i;
  9510. const char *str;
  9511. str=script_getstr(st,2);
  9512. i=script_getnum(st,3);
  9513. m = map_mapname2mapid(str);
  9514. if(m >= 0) {
  9515. switch(i) {
  9516. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  9517. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  9518. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  9519. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  9520. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  9521. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  9522. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  9523. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  9524. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  9525. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  9526. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  9527. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  9528. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  9529. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  9530. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  9531. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  9532. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  9533. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  9534. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  9535. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  9536. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  9537. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  9538. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  9539. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  9540. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  9541. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  9542. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  9543. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  9544. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  9545. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  9546. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  9547. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  9548. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  9549. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  9550. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  9551. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  9552. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  9553. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  9554. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  9555. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  9556. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  9557. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  9558. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  9559. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  9560. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  9561. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  9562. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  9563. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  9564. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  9565. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  9566. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  9567. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  9568. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  9569. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  9570. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  9571. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  9572. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  9573. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  9574. #ifdef ADJUST_SKILL_DAMAGE
  9575. case MF_SKILL_DAMAGE:
  9576. {
  9577. int ret_val=0, type=0;
  9578. FETCH(4,type);
  9579. switch (type) {
  9580. case 1: ret_val = map[m].adjust.damage.pc; break;
  9581. case 2: ret_val = map[m].adjust.damage.mob; break;
  9582. case 3: ret_val = map[m].adjust.damage.boss; break;
  9583. case 4: ret_val = map[m].adjust.damage.other; break;
  9584. case 5: ret_val = map[m].adjust.damage.caster; break;
  9585. default: ret_val = map[m].flag.skill_damage; break;
  9586. }
  9587. script_pushint(st,ret_val); break;
  9588. } break;
  9589. #endif
  9590. }
  9591. }
  9592. return SCRIPT_CMD_SUCCESS;
  9593. }
  9594. /* pvp timer handling */
  9595. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  9596. TBL_PC* sd = (TBL_PC*)bl;
  9597. if (sd->pvp_timer == INVALID_TIMER) {
  9598. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  9599. sd->pvp_rank = 0;
  9600. sd->pvp_lastusers = 0;
  9601. sd->pvp_point = 5;
  9602. sd->pvp_won = 0;
  9603. sd->pvp_lost = 0;
  9604. }
  9605. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  9606. clif_maptypeproperty2(&sd->bl,SELF);
  9607. return 0;
  9608. }
  9609. BUILDIN_FUNC(setmapflag)
  9610. {
  9611. int16 m,i;
  9612. const char *str;
  9613. int val=0;
  9614. str=script_getstr(st,2);
  9615. i=script_getnum(st,3);
  9616. FETCH(4,val);
  9617. m = map_mapname2mapid(str);
  9618. if(m >= 0) {
  9619. switch(i) {
  9620. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  9621. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  9622. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  9623. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  9624. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  9625. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  9626. case MF_PVP:
  9627. map[m].flag.pvp = 1;
  9628. if( !battle_config.pk_mode ) {
  9629. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  9630. }
  9631. break;
  9632. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  9633. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  9634. case MF_GVG: {
  9635. struct block_list bl;
  9636. map[m].flag.gvg = 1;
  9637. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9638. bl.type = BL_NUL;
  9639. bl.m = m;
  9640. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9641. }
  9642. break;
  9643. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  9644. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  9645. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  9646. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  9647. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  9648. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  9649. case MF_SNOW: map[m].flag.snow = 1; break;
  9650. case MF_FOG: map[m].flag.fog = 1; break;
  9651. case MF_SAKURA: map[m].flag.sakura = 1; break;
  9652. case MF_LEAVES: map[m].flag.leaves = 1; break;
  9653. case MF_NOGO: map[m].flag.nogo = 1; break;
  9654. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  9655. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  9656. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  9657. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  9658. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  9659. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  9660. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  9661. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  9662. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  9663. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  9664. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  9665. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  9666. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  9667. case MF_RESTRICTED:
  9668. map[m].zone |= 1<<(val+1);
  9669. map[m].flag.restricted=1;
  9670. break;
  9671. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  9672. case MF_NODROP: map[m].flag.nodrop = 1; break;
  9673. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  9674. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  9675. case MF_NOVENDING: map[m].flag.novending = 1; break;
  9676. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  9677. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  9678. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  9679. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  9680. case MF_TOWN: map[m].flag.town = 1; break;
  9681. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  9682. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  9683. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  9684. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  9685. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  9686. case MF_RESET: map[m].flag.reset = 1; break;
  9687. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  9688. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  9689. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  9690. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  9691. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  9692. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  9693. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  9694. #ifdef ADJUST_SKILL_DAMAGE
  9695. case MF_SKILL_DAMAGE:
  9696. {
  9697. int type=0;
  9698. FETCH(5,type);
  9699. switch (type) {
  9700. case 1: map[m].adjust.damage.pc = val; break;
  9701. case 2: map[m].adjust.damage.mob = val; break;
  9702. case 3: map[m].adjust.damage.boss = val; break;
  9703. case 4: map[m].adjust.damage.other = val; break;
  9704. case 5: map[m].adjust.damage.caster = val; break;
  9705. }
  9706. map[m].flag.skill_damage = 1;
  9707. } break;
  9708. #endif
  9709. }
  9710. }
  9711. return SCRIPT_CMD_SUCCESS;
  9712. }
  9713. BUILDIN_FUNC(removemapflag)
  9714. {
  9715. int16 m,i;
  9716. const char *str;
  9717. int val=0;
  9718. str=script_getstr(st,2);
  9719. i=script_getnum(st,3);
  9720. FETCH(4,val);
  9721. m = map_mapname2mapid(str);
  9722. if(m >= 0) {
  9723. switch(i) {
  9724. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  9725. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  9726. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  9727. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  9728. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  9729. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  9730. case MF_PVP: {
  9731. struct block_list bl;
  9732. bl.type = BL_NUL;
  9733. bl.m = m;
  9734. map[m].flag.pvp = 0;
  9735. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9736. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9737. }
  9738. break;
  9739. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  9740. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  9741. case MF_GVG: {
  9742. struct block_list bl;
  9743. bl.type = BL_NUL;
  9744. bl.m = m;
  9745. map[m].flag.gvg = 0;
  9746. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9747. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9748. }
  9749. break;
  9750. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  9751. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  9752. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  9753. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  9754. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  9755. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  9756. case MF_SNOW: map[m].flag.snow = 0; break;
  9757. case MF_FOG: map[m].flag.fog = 0; break;
  9758. case MF_SAKURA: map[m].flag.sakura = 0; break;
  9759. case MF_LEAVES: map[m].flag.leaves = 0; break;
  9760. case MF_NOGO: map[m].flag.nogo = 0; break;
  9761. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  9762. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  9763. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  9764. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  9765. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  9766. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  9767. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  9768. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  9769. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  9770. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  9771. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  9772. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  9773. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  9774. case MF_RESTRICTED:
  9775. map[m].zone ^= 1<<(val+1);
  9776. if (map[m].zone == 0){
  9777. map[m].flag.restricted=0;
  9778. }
  9779. break;
  9780. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  9781. case MF_NODROP: map[m].flag.nodrop = 0; break;
  9782. case MF_JEXP: map[m].adjust.jexp = 0; break;
  9783. case MF_BEXP: map[m].adjust.bexp = 0; break;
  9784. case MF_NOVENDING: map[m].flag.novending = 0; break;
  9785. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  9786. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  9787. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  9788. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  9789. case MF_TOWN: map[m].flag.town = 0; break;
  9790. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  9791. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  9792. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  9793. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  9794. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  9795. case MF_RESET: map[m].flag.reset = 0; break;
  9796. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  9797. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  9798. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  9799. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  9800. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  9801. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  9802. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  9803. #ifdef ADJUST_SKILL_DAMAGE
  9804. case MF_SKILL_DAMAGE:
  9805. {
  9806. map[m].flag.skill_damage = 0;
  9807. memset(&map[m].adjust.damage,0,sizeof(map[m].adjust.damage));
  9808. } break;
  9809. #endif
  9810. }
  9811. }
  9812. return SCRIPT_CMD_SUCCESS;
  9813. }
  9814. BUILDIN_FUNC(pvpon)
  9815. {
  9816. int16 m;
  9817. const char *str;
  9818. TBL_PC* sd = NULL;
  9819. struct s_mapiterator* iter;
  9820. struct block_list bl;
  9821. str = script_getstr(st,2);
  9822. m = map_mapname2mapid(str);
  9823. if( m < 0 || map[m].flag.pvp )
  9824. return 0; // nothing to do
  9825. map[m].flag.pvp = 1;
  9826. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  9827. bl.type = BL_NUL;
  9828. bl.m = m;
  9829. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9830. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  9831. return 0;
  9832. iter = mapit_getallusers();
  9833. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9834. {
  9835. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  9836. continue; // not applicable
  9837. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  9838. sd->pvp_rank = 0;
  9839. sd->pvp_lastusers = 0;
  9840. sd->pvp_point = 5;
  9841. sd->pvp_won = 0;
  9842. sd->pvp_lost = 0;
  9843. }
  9844. mapit_free(iter);
  9845. return SCRIPT_CMD_SUCCESS;
  9846. }
  9847. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  9848. {
  9849. TBL_PC* sd = (TBL_PC*)bl;
  9850. clif_pvpset(sd, 0, 0, 2);
  9851. if (sd->pvp_timer != INVALID_TIMER) {
  9852. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  9853. sd->pvp_timer = INVALID_TIMER;
  9854. }
  9855. return 0;
  9856. }
  9857. BUILDIN_FUNC(pvpoff)
  9858. {
  9859. int16 m;
  9860. const char *str;
  9861. struct block_list bl;
  9862. str=script_getstr(st,2);
  9863. m = map_mapname2mapid(str);
  9864. if(m < 0 || !map[m].flag.pvp)
  9865. return 0; //fixed Lupus
  9866. map[m].flag.pvp = 0;
  9867. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9868. bl.type = BL_NUL;
  9869. bl.m = m;
  9870. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9871. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  9872. return 0;
  9873. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  9874. return SCRIPT_CMD_SUCCESS;
  9875. }
  9876. BUILDIN_FUNC(gvgon)
  9877. {
  9878. int16 m;
  9879. const char *str;
  9880. struct block_list bl;
  9881. str=script_getstr(st,2);
  9882. m = map_mapname2mapid(str);
  9883. if(m >= 0 && !map[m].flag.gvg) {
  9884. map[m].flag.gvg = 1;
  9885. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9886. bl.type = BL_NUL;
  9887. bl.m = m;
  9888. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9889. }
  9890. return SCRIPT_CMD_SUCCESS;
  9891. }
  9892. BUILDIN_FUNC(gvgoff)
  9893. {
  9894. int16 m;
  9895. const char *str;
  9896. str=script_getstr(st,2);
  9897. m = map_mapname2mapid(str);
  9898. if(m >= 0 && map[m].flag.gvg) {
  9899. struct block_list bl;
  9900. map[m].flag.gvg = 0;
  9901. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9902. bl.type = BL_NUL;
  9903. bl.m = m;
  9904. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9905. }
  9906. return SCRIPT_CMD_SUCCESS;
  9907. }
  9908. /*==========================================
  9909. * Shows an emoticon on top of the player/npc
  9910. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  9911. *------------------------------------------*/
  9912. //Optional second parameter added by [Skotlex]
  9913. BUILDIN_FUNC(emotion)
  9914. {
  9915. int type;
  9916. int player=0;
  9917. type=script_getnum(st,2);
  9918. if(type < 0 || type > 100)
  9919. return 0;
  9920. if( script_hasdata(st,3) )
  9921. player=script_getnum(st,3);
  9922. if (player) {
  9923. TBL_PC *sd = NULL;
  9924. if( script_hasdata(st,4) )
  9925. sd = map_nick2sd(script_getstr(st,4));
  9926. else
  9927. sd = script_rid2sd(st);
  9928. if (sd)
  9929. clif_emotion(&sd->bl,type);
  9930. } else
  9931. if( script_hasdata(st,4) )
  9932. {
  9933. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  9934. if(nd)
  9935. clif_emotion(&nd->bl,type);
  9936. }
  9937. else
  9938. clif_emotion(map_id2bl(st->oid),type);
  9939. return SCRIPT_CMD_SUCCESS;
  9940. }
  9941. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  9942. {
  9943. int16 m=va_arg(ap,int);
  9944. int g_id=va_arg(ap,int);
  9945. int flag=va_arg(ap,int);
  9946. if(!sd || sd->bl.m != m)
  9947. return 0;
  9948. if(
  9949. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  9950. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  9951. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  9952. )
  9953. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  9954. return 1;
  9955. }
  9956. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  9957. {
  9958. struct mob_data *md=(struct mob_data *)bl;
  9959. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  9960. status_kill(bl);
  9961. return 0;
  9962. }
  9963. /*
  9964. * Function to kick guild members out of a map and to their save points.
  9965. * m : mapid
  9966. * g_id : owner guild id
  9967. * flag & 1 : Warp out owners
  9968. * flag & 2 : Warp out outsiders
  9969. * flag & 4 : reserved for mobs
  9970. * */
  9971. BUILDIN_FUNC(maprespawnguildid)
  9972. {
  9973. const char *mapname=script_getstr(st,2);
  9974. int g_id=script_getnum(st,3);
  9975. int flag=script_getnum(st,4);
  9976. int16 m=map_mapname2mapid(mapname);
  9977. if(m == -1)
  9978. return 0;
  9979. //Catch ALL players (in case some are 'between maps' on execution time)
  9980. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  9981. if (flag&4) //Remove script mobs.
  9982. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  9983. return SCRIPT_CMD_SUCCESS;
  9984. }
  9985. BUILDIN_FUNC(agitstart)
  9986. {
  9987. if(agit_flag==1) return SCRIPT_CMD_SUCCESS; // Agit already Start.
  9988. agit_flag=1;
  9989. guild_agit_start();
  9990. return SCRIPT_CMD_SUCCESS;
  9991. }
  9992. BUILDIN_FUNC(agitend)
  9993. {
  9994. if(agit_flag==0) return SCRIPT_CMD_SUCCESS; // Agit already End.
  9995. agit_flag=0;
  9996. guild_agit_end();
  9997. return SCRIPT_CMD_SUCCESS;
  9998. }
  9999. BUILDIN_FUNC(agitstart2)
  10000. {
  10001. if(agit2_flag==1) return SCRIPT_CMD_SUCCESS; // Agit2 already Start.
  10002. agit2_flag=1;
  10003. guild_agit2_start();
  10004. return SCRIPT_CMD_SUCCESS;
  10005. }
  10006. BUILDIN_FUNC(agitend2)
  10007. {
  10008. if(agit2_flag==0) return SCRIPT_CMD_SUCCESS; // Agit2 already End.
  10009. agit2_flag=0;
  10010. guild_agit2_end();
  10011. return SCRIPT_CMD_SUCCESS;
  10012. }
  10013. /*==========================================
  10014. * Returns whether woe is on or off. // choice script
  10015. *------------------------------------------*/
  10016. BUILDIN_FUNC(agitcheck)
  10017. {
  10018. script_pushint(st,agit_flag);
  10019. return SCRIPT_CMD_SUCCESS;
  10020. }
  10021. /*==========================================
  10022. * Returns whether woese is on or off. // choice script
  10023. *------------------------------------------*/
  10024. BUILDIN_FUNC(agitcheck2)
  10025. {
  10026. script_pushint(st,agit2_flag);
  10027. return SCRIPT_CMD_SUCCESS;
  10028. }
  10029. /// Sets the guild_id of this npc.
  10030. ///
  10031. /// flagemblem <guild_id>;
  10032. BUILDIN_FUNC(flagemblem)
  10033. {
  10034. TBL_NPC* nd;
  10035. int g_id = script_getnum(st,2);
  10036. if(g_id < 0) return 0;
  10037. nd = (TBL_NPC*)map_id2nd(st->oid);
  10038. if( nd == NULL ) {
  10039. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10040. } else if( nd->subtype != SCRIPT ) {
  10041. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10042. } else {
  10043. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10044. nd->u.scr.guild_id = g_id;
  10045. clif_guild_emblem_area(&nd->bl);
  10046. /* guild flag caching */
  10047. if( g_id ) /* adding a id */
  10048. guild_flag_add(nd);
  10049. else if( changed ) /* removing a flag */
  10050. guild_flag_remove(nd);
  10051. }
  10052. return SCRIPT_CMD_SUCCESS;
  10053. }
  10054. BUILDIN_FUNC(getcastlename)
  10055. {
  10056. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10057. struct guild_castle* gc = guild_mapname2gc(mapname);
  10058. const char* name = (gc) ? gc->castle_name : "";
  10059. script_pushstrcopy(st,name);
  10060. return SCRIPT_CMD_SUCCESS;
  10061. }
  10062. BUILDIN_FUNC(getcastledata)
  10063. {
  10064. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10065. int index = script_getnum(st,3);
  10066. struct guild_castle *gc = guild_mapname2gc(mapname);
  10067. if (gc == NULL) {
  10068. script_pushint(st,0);
  10069. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10070. return 1;
  10071. }
  10072. switch (index) {
  10073. case 1:
  10074. script_pushint(st,gc->guild_id); break;
  10075. case 2:
  10076. script_pushint(st,gc->economy); break;
  10077. case 3:
  10078. script_pushint(st,gc->defense); break;
  10079. case 4:
  10080. script_pushint(st,gc->triggerE); break;
  10081. case 5:
  10082. script_pushint(st,gc->triggerD); break;
  10083. case 6:
  10084. script_pushint(st,gc->nextTime); break;
  10085. case 7:
  10086. script_pushint(st,gc->payTime); break;
  10087. case 8:
  10088. script_pushint(st,gc->createTime); break;
  10089. case 9:
  10090. script_pushint(st,gc->visibleC); break;
  10091. default:
  10092. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10093. script_pushint(st,gc->guardian[index-10].visible);
  10094. break;
  10095. }
  10096. script_pushint(st,0);
  10097. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10098. return 1;
  10099. }
  10100. return SCRIPT_CMD_SUCCESS;
  10101. }
  10102. BUILDIN_FUNC(setcastledata)
  10103. {
  10104. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10105. int index = script_getnum(st,3);
  10106. int value = script_getnum(st,4);
  10107. struct guild_castle *gc = guild_mapname2gc(mapname);
  10108. if (gc == NULL) {
  10109. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10110. return 1;
  10111. }
  10112. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10113. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10114. return 1;
  10115. }
  10116. guild_castledatasave(gc->castle_id, index, value);
  10117. return SCRIPT_CMD_SUCCESS;
  10118. }
  10119. /* =====================================================================
  10120. * ---------------------------------------------------------------------*/
  10121. BUILDIN_FUNC(requestguildinfo)
  10122. {
  10123. int guild_id=script_getnum(st,2);
  10124. const char *event=NULL;
  10125. if( script_hasdata(st,3) ){
  10126. event=script_getstr(st,3);
  10127. check_event(st, event);
  10128. }
  10129. if(guild_id>0)
  10130. guild_npc_request_info(guild_id,event);
  10131. return SCRIPT_CMD_SUCCESS;
  10132. }
  10133. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10134. /// getequipcardcnt(<equipment slot>);
  10135. BUILDIN_FUNC(getequipcardcnt)
  10136. {
  10137. int i=-1,j,num;
  10138. TBL_PC *sd;
  10139. int count;
  10140. num=script_getnum(st,2);
  10141. sd=script_rid2sd(st);
  10142. if (num > 0 && num <= ARRAYLENGTH(equip))
  10143. i=pc_checkequip(sd,equip[num-1]);
  10144. if (i < 0 || !sd->inventory_data[i]) {
  10145. script_pushint(st,0);
  10146. return 0;
  10147. }
  10148. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10149. {
  10150. script_pushint(st,0);
  10151. return 0;
  10152. }
  10153. count = 0;
  10154. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10155. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  10156. count++;
  10157. script_pushint(st,count);
  10158. return SCRIPT_CMD_SUCCESS;
  10159. }
  10160. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10161. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10162. /// successremovecards <slot>;
  10163. BUILDIN_FUNC(successremovecards) {
  10164. int i=-1,j,c,cardflag=0;
  10165. TBL_PC* sd = script_rid2sd(st);
  10166. int num = script_getnum(st,2);
  10167. if (num > 0 && num <= ARRAYLENGTH(equip))
  10168. i=pc_checkequip(sd,equip[num-1]);
  10169. if (i < 0 || !sd->inventory_data[i]) {
  10170. return 0;
  10171. }
  10172. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10173. return 0;
  10174. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10175. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10176. int flag;
  10177. struct item item_tmp;
  10178. memset(&item_tmp,0,sizeof(item_tmp));
  10179. cardflag = 1;
  10180. item_tmp.nameid = sd->status.inventory[i].card[c];
  10181. item_tmp.identify = 1;
  10182. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10183. clif_additem(sd,0,0,flag);
  10184. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10185. }
  10186. }
  10187. }
  10188. if(cardflag == 1) {//if card was remove remplace item with no card
  10189. int flag;
  10190. struct item item_tmp;
  10191. memset(&item_tmp,0,sizeof(item_tmp));
  10192. item_tmp.nameid = sd->status.inventory[i].nameid;
  10193. item_tmp.identify = 1;
  10194. item_tmp.refine = sd->status.inventory[i].refine;
  10195. item_tmp.attribute = sd->status.inventory[i].attribute;
  10196. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10197. item_tmp.bound = sd->status.inventory[i].bound;
  10198. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10199. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10200. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10201. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10202. clif_additem(sd,0,0,flag);
  10203. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10204. }
  10205. clif_misceffect(&sd->bl,3);
  10206. }
  10207. return SCRIPT_CMD_SUCCESS;
  10208. }
  10209. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10210. /// failedremovecards <slot>, <type>;
  10211. /// <type>=0 : will destroy both the item and the cards.
  10212. /// <type>=1 : will keep the item, but destroy the cards.
  10213. /// <type>=2 : will keep the cards, but destroy the item.
  10214. /// <type>=? : will just display the failure effect.
  10215. BUILDIN_FUNC(failedremovecards) {
  10216. int i=-1,j,c,cardflag=0;
  10217. TBL_PC* sd = script_rid2sd(st);
  10218. int num = script_getnum(st,2);
  10219. int typefail = script_getnum(st,3);
  10220. if (num > 0 && num <= ARRAYLENGTH(equip))
  10221. i=pc_checkequip(sd,equip[num-1]);
  10222. if (i < 0 || !sd->inventory_data[i])
  10223. return 0;
  10224. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10225. return 0;
  10226. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10227. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  10228. cardflag = 1;
  10229. if(typefail == 2) {// add cards to inventory, clear
  10230. int flag;
  10231. struct item item_tmp;
  10232. memset(&item_tmp,0,sizeof(item_tmp));
  10233. item_tmp.nameid = sd->status.inventory[i].card[c];
  10234. item_tmp.identify = 1;
  10235. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10236. clif_additem(sd,0,0,flag);
  10237. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10238. }
  10239. }
  10240. }
  10241. }
  10242. if(cardflag == 1) {
  10243. if(typefail == 0 || typefail == 2){ // destroy the item
  10244. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10245. }
  10246. if(typefail == 1){ // destroy the card
  10247. int flag;
  10248. struct item item_tmp;
  10249. memset(&item_tmp,0,sizeof(item_tmp));
  10250. item_tmp.nameid = sd->status.inventory[i].nameid;
  10251. item_tmp.identify = 1;
  10252. item_tmp.refine = sd->status.inventory[i].refine;
  10253. item_tmp.attribute = sd->status.inventory[i].attribute;
  10254. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10255. item_tmp.bound = sd->status.inventory[i].bound;
  10256. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10257. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10258. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10259. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10260. clif_additem(sd,0,0,flag);
  10261. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10262. }
  10263. }
  10264. clif_misceffect(&sd->bl,2);
  10265. }
  10266. return SCRIPT_CMD_SUCCESS;
  10267. }
  10268. /* ================================================================
  10269. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  10270. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  10271. * improved by [Lance]
  10272. * ================================================================*/
  10273. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  10274. {
  10275. int x,y,m,check_val=0,check_ID=0,i=0;
  10276. struct guild *g = NULL;
  10277. struct party_data *p = NULL;
  10278. const char *str;
  10279. const char *mapname;
  10280. unsigned int index;
  10281. mapname=script_getstr(st,2);
  10282. str=script_getstr(st,3);
  10283. x=script_getnum(st,4);
  10284. y=script_getnum(st,5);
  10285. if(script_hasdata(st,7)){
  10286. check_val=script_getnum(st,6);
  10287. check_ID=script_getnum(st,7);
  10288. }
  10289. if((m=map_mapname2mapid(mapname))< 0)
  10290. return 0;
  10291. if(!(index=mapindex_name2id(str)))
  10292. return 0;
  10293. switch(check_val){
  10294. case 1:
  10295. g = guild_search(check_ID);
  10296. if (g){
  10297. for( i=0; i < g->max_member; i++)
  10298. {
  10299. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  10300. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  10301. }
  10302. }
  10303. }
  10304. break;
  10305. case 2:
  10306. p = party_search(check_ID);
  10307. if(p){
  10308. for(i=0;i<MAX_PARTY; i++){
  10309. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  10310. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  10311. }
  10312. }
  10313. }
  10314. break;
  10315. default:
  10316. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  10317. break;
  10318. }
  10319. return SCRIPT_CMD_SUCCESS;
  10320. }
  10321. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  10322. {
  10323. char *event=va_arg(ap,char *);
  10324. struct mob_data *md = ((struct mob_data *)bl);
  10325. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  10326. return 1;
  10327. return SCRIPT_CMD_SUCCESS;
  10328. }
  10329. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  10330. {
  10331. const char *mapname,*event;
  10332. int16 m;
  10333. mapname=script_getstr(st,2);
  10334. event=script_getstr(st,3);
  10335. if( strcmp(event, "all") == 0 )
  10336. event = NULL;
  10337. else
  10338. check_event(st, event);
  10339. if( strcmp(mapname, "this") == 0 ) {
  10340. struct map_session_data *sd = script_rid2sd(st);
  10341. if( sd )
  10342. m = sd->bl.m;
  10343. else {
  10344. script_pushint(st,-1);
  10345. return 0;
  10346. }
  10347. }
  10348. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  10349. script_pushint(st,-1);
  10350. return 0;
  10351. }
  10352. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  10353. return SCRIPT_CMD_SUCCESS;
  10354. }
  10355. BUILDIN_FUNC(marriage)
  10356. {
  10357. const char *partner=script_getstr(st,2);
  10358. TBL_PC *sd=script_rid2sd(st);
  10359. TBL_PC *p_sd=map_nick2sd(partner);
  10360. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  10361. script_pushint(st,0);
  10362. return 0;
  10363. }
  10364. script_pushint(st,1);
  10365. return SCRIPT_CMD_SUCCESS;
  10366. }
  10367. BUILDIN_FUNC(wedding_effect)
  10368. {
  10369. TBL_PC *sd=script_rid2sd(st);
  10370. struct block_list *bl;
  10371. if(sd==NULL) {
  10372. bl=map_id2bl(st->oid);
  10373. } else
  10374. bl=&sd->bl;
  10375. clif_wedding_effect(bl);
  10376. return SCRIPT_CMD_SUCCESS;
  10377. }
  10378. BUILDIN_FUNC(divorce)
  10379. {
  10380. TBL_PC *sd=script_rid2sd(st);
  10381. if(sd==NULL || pc_divorce(sd) < 0){
  10382. script_pushint(st,0);
  10383. return 0;
  10384. }
  10385. script_pushint(st,1);
  10386. return SCRIPT_CMD_SUCCESS;
  10387. }
  10388. BUILDIN_FUNC(ispartneron)
  10389. {
  10390. TBL_PC *sd=script_rid2sd(st);
  10391. if(sd==NULL || !pc_ismarried(sd) ||
  10392. map_charid2sd(sd->status.partner_id) == NULL) {
  10393. script_pushint(st,0);
  10394. return 0;
  10395. }
  10396. script_pushint(st,1);
  10397. return SCRIPT_CMD_SUCCESS;
  10398. }
  10399. BUILDIN_FUNC(getpartnerid)
  10400. {
  10401. TBL_PC *sd=script_rid2sd(st);
  10402. if (sd == NULL) {
  10403. script_pushint(st,0);
  10404. return 0;
  10405. }
  10406. script_pushint(st,sd->status.partner_id);
  10407. return SCRIPT_CMD_SUCCESS;
  10408. }
  10409. BUILDIN_FUNC(getchildid)
  10410. {
  10411. TBL_PC *sd=script_rid2sd(st);
  10412. if (sd == NULL) {
  10413. script_pushint(st,0);
  10414. return 0;
  10415. }
  10416. script_pushint(st,sd->status.child);
  10417. return SCRIPT_CMD_SUCCESS;
  10418. }
  10419. BUILDIN_FUNC(getmotherid)
  10420. {
  10421. TBL_PC *sd=script_rid2sd(st);
  10422. if (sd == NULL) {
  10423. script_pushint(st,0);
  10424. return 0;
  10425. }
  10426. script_pushint(st,sd->status.mother);
  10427. return SCRIPT_CMD_SUCCESS;
  10428. }
  10429. BUILDIN_FUNC(getfatherid)
  10430. {
  10431. TBL_PC *sd=script_rid2sd(st);
  10432. if (sd == NULL) {
  10433. script_pushint(st,0);
  10434. return 0;
  10435. }
  10436. script_pushint(st,sd->status.father);
  10437. return SCRIPT_CMD_SUCCESS;
  10438. }
  10439. BUILDIN_FUNC(warppartner)
  10440. {
  10441. int x,y;
  10442. unsigned short mapindex;
  10443. const char *str;
  10444. TBL_PC *sd=script_rid2sd(st);
  10445. TBL_PC *p_sd=NULL;
  10446. if(sd==NULL || !pc_ismarried(sd) ||
  10447. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  10448. script_pushint(st,0);
  10449. return 0;
  10450. }
  10451. str=script_getstr(st,2);
  10452. x=script_getnum(st,3);
  10453. y=script_getnum(st,4);
  10454. mapindex = mapindex_name2id(str);
  10455. if (mapindex) {
  10456. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  10457. script_pushint(st,1);
  10458. } else
  10459. script_pushint(st,0);
  10460. return SCRIPT_CMD_SUCCESS;
  10461. }
  10462. /*================================================
  10463. * Script for Displaying MOB Information [Valaris]
  10464. *------------------------------------------------*/
  10465. BUILDIN_FUNC(strmobinfo)
  10466. {
  10467. int num=script_getnum(st,2);
  10468. int class_=script_getnum(st,3);
  10469. if(!mobdb_checkid(class_))
  10470. {
  10471. if (num < 3) //requested a string
  10472. script_pushconststr(st,"");
  10473. else
  10474. script_pushint(st,0);
  10475. return 0;
  10476. }
  10477. switch (num) {
  10478. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  10479. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  10480. case 3: script_pushint(st,mob_db(class_)->lv); break;
  10481. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  10482. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  10483. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  10484. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  10485. default:
  10486. script_pushint(st,0);
  10487. break;
  10488. }
  10489. return SCRIPT_CMD_SUCCESS;
  10490. }
  10491. /*==========================================
  10492. * Summon guardians [Valaris]
  10493. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  10494. *------------------------------------------*/
  10495. BUILDIN_FUNC(guardian)
  10496. {
  10497. int class_=0,x=0,y=0,guardian=0;
  10498. const char *str,*map,*evt="";
  10499. struct script_data *data;
  10500. bool has_index = false;
  10501. map =script_getstr(st,2);
  10502. x =script_getnum(st,3);
  10503. y =script_getnum(st,4);
  10504. str =script_getstr(st,5);
  10505. class_=script_getnum(st,6);
  10506. if( script_hasdata(st,8) )
  10507. {// "<event label>",<guardian index>
  10508. evt=script_getstr(st,7);
  10509. guardian=script_getnum(st,8);
  10510. has_index = true;
  10511. } else if( script_hasdata(st,7) ){
  10512. data=script_getdata(st,7);
  10513. get_val(st,data);
  10514. if( data_isstring(data) )
  10515. {// "<event label>"
  10516. evt=script_getstr(st,7);
  10517. } else if( data_isint(data) )
  10518. {// <guardian index>
  10519. guardian=script_getnum(st,7);
  10520. has_index = true;
  10521. } else {
  10522. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  10523. script_reportdata(data);
  10524. return 1;
  10525. }
  10526. }
  10527. check_event(st, evt);
  10528. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  10529. return SCRIPT_CMD_SUCCESS;
  10530. }
  10531. /*==========================================
  10532. * Invisible Walls [Zephyrus]
  10533. *------------------------------------------*/
  10534. BUILDIN_FUNC(setwall)
  10535. {
  10536. const char *map, *name;
  10537. int x, y, m, size, dir;
  10538. bool shootable;
  10539. map = script_getstr(st,2);
  10540. x = script_getnum(st,3);
  10541. y = script_getnum(st,4);
  10542. size = script_getnum(st,5);
  10543. dir = script_getnum(st,6);
  10544. shootable = script_getnum(st,7);
  10545. name = script_getstr(st,8);
  10546. if( (m = map_mapname2mapid(map)) < 0 )
  10547. return 0; // Invalid Map
  10548. map_iwall_set(m, x, y, size, dir, shootable, name);
  10549. return SCRIPT_CMD_SUCCESS;
  10550. }
  10551. BUILDIN_FUNC(delwall)
  10552. {
  10553. const char *name = script_getstr(st,2);
  10554. map_iwall_remove(name);
  10555. return SCRIPT_CMD_SUCCESS;
  10556. }
  10557. /// Retrieves various information about the specified guardian.
  10558. ///
  10559. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  10560. /// type: 0 - whether it is deployed or not
  10561. /// 1 - maximum hp
  10562. /// 2 - current hp
  10563. ///
  10564. BUILDIN_FUNC(guardianinfo)
  10565. {
  10566. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10567. int id = script_getnum(st,3);
  10568. int type = script_getnum(st,4);
  10569. struct guild_castle* gc = guild_mapname2gc(mapname);
  10570. struct mob_data* gd;
  10571. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  10572. {
  10573. script_pushint(st,-1);
  10574. return 0;
  10575. }
  10576. if( type == 0 )
  10577. script_pushint(st, gc->guardian[id].visible);
  10578. else
  10579. if( !gc->guardian[id].visible )
  10580. script_pushint(st,-1);
  10581. else
  10582. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  10583. script_pushint(st,-1);
  10584. else
  10585. {
  10586. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  10587. else if( type == 2 ) script_pushint(st,gd->status.hp);
  10588. else
  10589. script_pushint(st,-1);
  10590. }
  10591. return SCRIPT_CMD_SUCCESS;
  10592. }
  10593. /*==========================================
  10594. * Get the item name by item_id or null
  10595. *------------------------------------------*/
  10596. BUILDIN_FUNC(getitemname)
  10597. {
  10598. int item_id=0;
  10599. struct item_data *i_data;
  10600. char *item_name;
  10601. struct script_data *data;
  10602. data=script_getdata(st,2);
  10603. get_val(st,data);
  10604. if( data_isstring(data) ){
  10605. const char *name=conv_str(st,data);
  10606. struct item_data *item_data = itemdb_searchname(name);
  10607. if( item_data )
  10608. item_id=item_data->nameid;
  10609. }else
  10610. item_id=conv_num(st,data);
  10611. i_data = itemdb_exists(item_id);
  10612. if (i_data == NULL)
  10613. {
  10614. script_pushconststr(st,"null");
  10615. return 0;
  10616. }
  10617. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  10618. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  10619. script_pushstr(st,item_name);
  10620. return SCRIPT_CMD_SUCCESS;
  10621. }
  10622. /*==========================================
  10623. * Returns number of slots an item has. [Skotlex]
  10624. *------------------------------------------*/
  10625. BUILDIN_FUNC(getitemslots)
  10626. {
  10627. int item_id;
  10628. struct item_data *i_data;
  10629. item_id=script_getnum(st,2);
  10630. i_data = itemdb_exists(item_id);
  10631. if (i_data)
  10632. script_pushint(st,i_data->slot);
  10633. else
  10634. script_pushint(st,-1);
  10635. return SCRIPT_CMD_SUCCESS;
  10636. }
  10637. // TODO: add matk here if needed/once we get rid of RENEWAL
  10638. /*==========================================
  10639. * Returns some values of an item [Lupus]
  10640. * Price, Weight, etc...
  10641. getiteminfo(itemID,n), where n
  10642. 0 value_buy;
  10643. 1 value_sell;
  10644. 2 type;
  10645. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10646. if = 0, then monsters don't drop it at all (rare or a quest item)
  10647. if = -1, then this item is sold in NPC shops only
  10648. 4 sex;
  10649. 5 equip;
  10650. 6 weight;
  10651. 7 atk;
  10652. 8 def;
  10653. 9 range;
  10654. 10 slot;
  10655. 11 look;
  10656. 12 elv;
  10657. 13 wlv;
  10658. 14 view id
  10659. *------------------------------------------*/
  10660. BUILDIN_FUNC(getiteminfo)
  10661. {
  10662. int item_id,n;
  10663. int *item_arr;
  10664. struct item_data *i_data;
  10665. item_id = script_getnum(st,2);
  10666. n = script_getnum(st,3);
  10667. i_data = itemdb_exists(item_id);
  10668. if (i_data && n>=0 && n<=14) {
  10669. item_arr = (int*)&i_data->value_buy;
  10670. script_pushint(st,item_arr[n]);
  10671. } else
  10672. script_pushint(st,-1);
  10673. return SCRIPT_CMD_SUCCESS;
  10674. }
  10675. /*==========================================
  10676. * Set some values of an item [Lupus]
  10677. * Price, Weight, etc...
  10678. setiteminfo(itemID,n,Value), where n
  10679. 0 value_buy;
  10680. 1 value_sell;
  10681. 2 type;
  10682. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10683. if = 0, then monsters don't drop it at all (rare or a quest item)
  10684. if = -1, then this item is sold in NPC shops only
  10685. 4 sex;
  10686. 5 equip;
  10687. 6 weight;
  10688. 7 atk;
  10689. 8 def;
  10690. 9 range;
  10691. 10 slot;
  10692. 11 look;
  10693. 12 elv;
  10694. 13 wlv;
  10695. 14 view id
  10696. * Returns Value or -1 if the wrong field's been set
  10697. *------------------------------------------*/
  10698. BUILDIN_FUNC(setiteminfo)
  10699. {
  10700. int item_id,n,value;
  10701. int *item_arr;
  10702. struct item_data *i_data;
  10703. item_id = script_getnum(st,2);
  10704. n = script_getnum(st,3);
  10705. value = script_getnum(st,4);
  10706. i_data = itemdb_exists(item_id);
  10707. if (i_data && n>=0 && n<=14) {
  10708. item_arr = (int*)&i_data->value_buy;
  10709. item_arr[n] = value;
  10710. script_pushint(st,value);
  10711. } else
  10712. script_pushint(st,-1);
  10713. return SCRIPT_CMD_SUCCESS;
  10714. }
  10715. /*==========================================
  10716. * Returns value from equipped item slot n [Lupus]
  10717. getequipcardid(num,slot)
  10718. where
  10719. num = eqip position slot
  10720. slot = 0,1,2,3 (Card Slot N)
  10721. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  10722. it's useful when you want to check item cards or if it's signed
  10723. Useful for such quests as "Sign this refined item with players name" etc
  10724. Hat[0] +4 -> Player's Hat[0] +4
  10725. *------------------------------------------*/
  10726. BUILDIN_FUNC(getequipcardid)
  10727. {
  10728. int i=-1,num,slot;
  10729. TBL_PC *sd;
  10730. num=script_getnum(st,2);
  10731. slot=script_getnum(st,3);
  10732. sd=script_rid2sd(st);
  10733. if (num > 0 && num <= ARRAYLENGTH(equip))
  10734. i=pc_checkequip(sd,equip[num-1]);
  10735. if(i >= 0 && slot>=0 && slot<4)
  10736. script_pushint(st,sd->status.inventory[i].card[slot]);
  10737. else
  10738. script_pushint(st,0);
  10739. return SCRIPT_CMD_SUCCESS;
  10740. }
  10741. /*==========================================
  10742. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  10743. *------------------------------------------*/
  10744. BUILDIN_FUNC(petskillbonus)
  10745. {
  10746. struct pet_data *pd;
  10747. TBL_PC *sd=script_rid2sd(st);
  10748. if(sd==NULL || sd->pd==NULL)
  10749. return 0;
  10750. pd=sd->pd;
  10751. if (pd->bonus)
  10752. { //Clear previous bonus
  10753. if (pd->bonus->timer != INVALID_TIMER)
  10754. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  10755. } else //init
  10756. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  10757. pd->bonus->type=script_getnum(st,2);
  10758. pd->bonus->val=script_getnum(st,3);
  10759. pd->bonus->duration=script_getnum(st,4);
  10760. pd->bonus->delay=script_getnum(st,5);
  10761. if (pd->state.skillbonus == 1)
  10762. pd->state.skillbonus=0; // waiting state
  10763. // wait for timer to start
  10764. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10765. pd->bonus->timer = INVALID_TIMER;
  10766. else
  10767. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  10768. return SCRIPT_CMD_SUCCESS;
  10769. }
  10770. /*==========================================
  10771. * pet looting [Valaris] //Rewritten by [Skotlex]
  10772. *------------------------------------------*/
  10773. BUILDIN_FUNC(petloot)
  10774. {
  10775. int max;
  10776. struct pet_data *pd;
  10777. TBL_PC *sd=script_rid2sd(st);
  10778. if(sd==NULL || sd->pd==NULL)
  10779. return 0;
  10780. max=script_getnum(st,2);
  10781. if(max < 1)
  10782. max = 1; //Let'em loot at least 1 item.
  10783. else if (max > MAX_PETLOOT_SIZE)
  10784. max = MAX_PETLOOT_SIZE;
  10785. pd = sd->pd;
  10786. if (pd->loot != NULL)
  10787. { //Release whatever was there already and reallocate memory
  10788. pet_lootitem_drop(pd, pd->master);
  10789. aFree(pd->loot->item);
  10790. }
  10791. else
  10792. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  10793. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  10794. pd->loot->max=max;
  10795. pd->loot->count = 0;
  10796. pd->loot->weight = 0;
  10797. return SCRIPT_CMD_SUCCESS;
  10798. }
  10799. /*==========================================
  10800. * Set arrays with info of all sd inventory :
  10801. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  10802. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  10803. * @inventorylist_card(0..3), @inventorylist_expire
  10804. * @inventorylist_count = scalar
  10805. *------------------------------------------*/
  10806. BUILDIN_FUNC(getinventorylist)
  10807. {
  10808. TBL_PC *sd=script_rid2sd(st);
  10809. char card_var[NAME_LENGTH];
  10810. int i,j=0,k;
  10811. if(!sd) return 0;
  10812. for(i=0;i<MAX_INVENTORY;i++){
  10813. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  10814. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  10815. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  10816. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  10817. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  10818. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  10819. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  10820. for (k = 0; k < MAX_SLOTS; k++)
  10821. {
  10822. sprintf(card_var, "@inventorylist_card%d",k+1);
  10823. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  10824. }
  10825. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  10826. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  10827. j++;
  10828. }
  10829. }
  10830. pc_setreg(sd,add_str("@inventorylist_count"),j);
  10831. return SCRIPT_CMD_SUCCESS;
  10832. }
  10833. BUILDIN_FUNC(getskilllist)
  10834. {
  10835. TBL_PC *sd=script_rid2sd(st);
  10836. int i,j=0;
  10837. if(!sd) return 0;
  10838. for(i=0;i<MAX_SKILL;i++){
  10839. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  10840. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  10841. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  10842. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  10843. j++;
  10844. }
  10845. }
  10846. pc_setreg(sd,add_str("@skilllist_count"),j);
  10847. return SCRIPT_CMD_SUCCESS;
  10848. }
  10849. BUILDIN_FUNC(clearitem)
  10850. {
  10851. TBL_PC *sd=script_rid2sd(st);
  10852. int i;
  10853. if(sd==NULL) return 0;
  10854. for (i=0; i<MAX_INVENTORY; i++) {
  10855. if (sd->status.inventory[i].amount) {
  10856. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  10857. }
  10858. }
  10859. return SCRIPT_CMD_SUCCESS;
  10860. }
  10861. /*==========================================
  10862. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  10863. *------------------------------------------*/
  10864. BUILDIN_FUNC(disguise)
  10865. {
  10866. int id;
  10867. TBL_PC* sd = script_rid2sd(st);
  10868. if (sd == NULL) return 0;
  10869. id = script_getnum(st,2);
  10870. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  10871. pc_disguise(sd, id);
  10872. script_pushint(st,id);
  10873. } else
  10874. script_pushint(st,0);
  10875. return SCRIPT_CMD_SUCCESS;
  10876. }
  10877. /*==========================================
  10878. * Undisguise Player (returns 1 if success, 0 on fail)
  10879. *------------------------------------------*/
  10880. BUILDIN_FUNC(undisguise)
  10881. {
  10882. TBL_PC* sd = script_rid2sd(st);
  10883. if (sd == NULL) return 0;
  10884. if (sd->disguise) {
  10885. pc_disguise(sd, 0);
  10886. script_pushint(st,0);
  10887. } else {
  10888. script_pushint(st,1);
  10889. }
  10890. return SCRIPT_CMD_SUCCESS;
  10891. }
  10892. /*==========================================
  10893. * Transform a bl to another _class,
  10894. * @type unused
  10895. *------------------------------------------*/
  10896. BUILDIN_FUNC(classchange)
  10897. {
  10898. int _class,type;
  10899. struct block_list *bl=map_id2bl(st->oid);
  10900. if(bl==NULL) return 0;
  10901. _class=script_getnum(st,2);
  10902. type=script_getnum(st,3);
  10903. clif_class_change(bl,_class,type);
  10904. return SCRIPT_CMD_SUCCESS;
  10905. }
  10906. /*==========================================
  10907. * Display an effect
  10908. *------------------------------------------*/
  10909. BUILDIN_FUNC(misceffect)
  10910. {
  10911. int type;
  10912. type=script_getnum(st,2);
  10913. if(st->oid && st->oid != fake_nd->bl.id) {
  10914. struct block_list *bl = map_id2bl(st->oid);
  10915. if (bl)
  10916. clif_specialeffect(bl,type,AREA);
  10917. } else{
  10918. TBL_PC *sd=script_rid2sd(st);
  10919. if(sd)
  10920. clif_specialeffect(&sd->bl,type,AREA);
  10921. }
  10922. return SCRIPT_CMD_SUCCESS;
  10923. }
  10924. /*==========================================
  10925. * Play a BGM on a single client [Rikter/Yommy]
  10926. *------------------------------------------*/
  10927. BUILDIN_FUNC(playBGM)
  10928. {
  10929. const char* name;
  10930. struct map_session_data* sd;
  10931. if( ( sd = script_rid2sd(st) ) != NULL ) {
  10932. name = script_getstr(st,2);
  10933. clif_playBGM(sd, name);
  10934. }
  10935. return SCRIPT_CMD_SUCCESS;
  10936. }
  10937. static int playBGM_sub(struct block_list* bl,va_list ap)
  10938. {
  10939. const char* name = va_arg(ap,const char*);
  10940. clif_playBGM(BL_CAST(BL_PC, bl), name);
  10941. return 0;
  10942. }
  10943. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  10944. {
  10945. const char* name = va_arg(args, const char*);
  10946. clif_playBGM(sd, name);
  10947. return 0;
  10948. }
  10949. /*==========================================
  10950. * Play a BGM on multiple client [Rikter/Yommy]
  10951. *------------------------------------------*/
  10952. BUILDIN_FUNC(playBGMall)
  10953. {
  10954. const char* name;
  10955. name = script_getstr(st,2);
  10956. if( script_hasdata(st,7) ) {// specified part of map
  10957. const char* map = script_getstr(st,3);
  10958. int x0 = script_getnum(st,4);
  10959. int y0 = script_getnum(st,5);
  10960. int x1 = script_getnum(st,6);
  10961. int y1 = script_getnum(st,7);
  10962. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  10963. }
  10964. else if( script_hasdata(st,3) ) {// entire map
  10965. const char* map = script_getstr(st,3);
  10966. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  10967. }
  10968. else {// entire server
  10969. map_foreachpc(&playBGM_foreachpc_sub, name);
  10970. }
  10971. return SCRIPT_CMD_SUCCESS;
  10972. }
  10973. /*==========================================
  10974. * Play a .wav sound for sd
  10975. *------------------------------------------*/
  10976. BUILDIN_FUNC(soundeffect)
  10977. {
  10978. TBL_PC* sd = script_rid2sd(st);
  10979. const char* name = script_getstr(st,2);
  10980. int type = script_getnum(st,3);
  10981. if(sd) {
  10982. clif_soundeffect(sd,&sd->bl,name,type);
  10983. }
  10984. return SCRIPT_CMD_SUCCESS;
  10985. }
  10986. int soundeffect_sub(struct block_list* bl,va_list ap)
  10987. {
  10988. char* name = va_arg(ap,char*);
  10989. int type = va_arg(ap,int);
  10990. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  10991. return 0;
  10992. }
  10993. /*==========================================
  10994. * Play a sound effect (.wav) on multiple clients
  10995. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  10996. *------------------------------------------*/
  10997. BUILDIN_FUNC(soundeffectall)
  10998. {
  10999. struct block_list* bl;
  11000. const char* name;
  11001. int type;
  11002. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11003. if (!bl)
  11004. return 0;
  11005. name = script_getstr(st,2);
  11006. type = script_getnum(st,3);
  11007. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11008. if(!script_hasdata(st,4))
  11009. { // area around
  11010. clif_soundeffectall(bl, name, type, AREA);
  11011. }
  11012. else
  11013. if(!script_hasdata(st,5))
  11014. { // entire map
  11015. const char* map = script_getstr(st,4);
  11016. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  11017. }
  11018. else
  11019. if(script_hasdata(st,8))
  11020. { // specified part of map
  11021. const char* map = script_getstr(st,4);
  11022. int x0 = script_getnum(st,5);
  11023. int y0 = script_getnum(st,6);
  11024. int x1 = script_getnum(st,7);
  11025. int y1 = script_getnum(st,8);
  11026. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  11027. }
  11028. else
  11029. {
  11030. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11031. }
  11032. return SCRIPT_CMD_SUCCESS;
  11033. }
  11034. /*==========================================
  11035. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11036. *------------------------------------------*/
  11037. BUILDIN_FUNC(petrecovery)
  11038. {
  11039. struct pet_data *pd;
  11040. TBL_PC *sd=script_rid2sd(st);
  11041. if(sd==NULL || sd->pd==NULL)
  11042. return 0;
  11043. pd=sd->pd;
  11044. if (pd->recovery)
  11045. { //Halt previous bonus
  11046. if (pd->recovery->timer != INVALID_TIMER)
  11047. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11048. } else //Init
  11049. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11050. pd->recovery->type = (sc_type)script_getnum(st,2);
  11051. pd->recovery->delay = script_getnum(st,3);
  11052. pd->recovery->timer = INVALID_TIMER;
  11053. return SCRIPT_CMD_SUCCESS;
  11054. }
  11055. /*==========================================
  11056. * pet healing [Valaris] //Rewritten by [Skotlex]
  11057. *------------------------------------------*/
  11058. BUILDIN_FUNC(petheal)
  11059. {
  11060. struct pet_data *pd;
  11061. TBL_PC *sd=script_rid2sd(st);
  11062. if(sd==NULL || sd->pd==NULL)
  11063. return 0;
  11064. pd=sd->pd;
  11065. if (pd->s_skill)
  11066. { //Clear previous skill
  11067. if (pd->s_skill->timer != INVALID_TIMER)
  11068. {
  11069. if (pd->s_skill->id)
  11070. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11071. else
  11072. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11073. }
  11074. } else //init memory
  11075. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11076. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11077. //Use the lv as the amount to heal
  11078. pd->s_skill->lv=script_getnum(st,2);
  11079. pd->s_skill->delay=script_getnum(st,3);
  11080. pd->s_skill->hp=script_getnum(st,4);
  11081. pd->s_skill->sp=script_getnum(st,5);
  11082. //Use delay as initial offset to avoid skill/heal exploits
  11083. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11084. pd->s_skill->timer = INVALID_TIMER;
  11085. else
  11086. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11087. return SCRIPT_CMD_SUCCESS;
  11088. }
  11089. /*==========================================
  11090. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11091. *------------------------------------------*/
  11092. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11093. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11094. BUILDIN_FUNC(petskillattack)
  11095. {
  11096. struct pet_data *pd;
  11097. struct script_data *data;
  11098. TBL_PC *sd=script_rid2sd(st);
  11099. if(sd==NULL || sd->pd==NULL)
  11100. return 0;
  11101. pd=sd->pd;
  11102. if (pd->a_skill == NULL)
  11103. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11104. data = script_getdata(st, 2);
  11105. get_val(st, data); // Convert into value in case of a variable
  11106. pd->a_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11107. pd->a_skill->lv=script_getnum(st,3);
  11108. pd->a_skill->div_ = 0;
  11109. pd->a_skill->rate=script_getnum(st,4);
  11110. pd->a_skill->bonusrate=script_getnum(st,5);
  11111. return SCRIPT_CMD_SUCCESS;
  11112. }
  11113. /*==========================================
  11114. * pet attack skills [Valaris]
  11115. *------------------------------------------*/
  11116. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  11117. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  11118. BUILDIN_FUNC(petskillattack2)
  11119. {
  11120. struct pet_data *pd;
  11121. struct script_data *data;
  11122. TBL_PC *sd=script_rid2sd(st);
  11123. if(sd==NULL || sd->pd==NULL)
  11124. return 0;
  11125. pd=sd->pd;
  11126. if (pd->a_skill == NULL)
  11127. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11128. data = script_getdata(st, 2);
  11129. get_val(st, data); // Convert into value in case of a variable
  11130. pd->a_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11131. pd->a_skill->lv=script_getnum(st,3);
  11132. pd->a_skill->div_ = script_getnum(st,4);
  11133. pd->a_skill->rate=script_getnum(st,5);
  11134. pd->a_skill->bonusrate=script_getnum(st,6);
  11135. return SCRIPT_CMD_SUCCESS;
  11136. }
  11137. /*==========================================
  11138. * pet support skills [Skotlex]
  11139. *------------------------------------------*/
  11140. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  11141. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  11142. BUILDIN_FUNC(petskillsupport)
  11143. {
  11144. struct pet_data *pd;
  11145. struct script_data *data;
  11146. TBL_PC *sd=script_rid2sd(st);
  11147. if(sd==NULL || sd->pd==NULL)
  11148. return 0;
  11149. pd=sd->pd;
  11150. if (pd->s_skill)
  11151. { //Clear previous skill
  11152. if (pd->s_skill->timer != INVALID_TIMER)
  11153. {
  11154. if (pd->s_skill->id)
  11155. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11156. else
  11157. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11158. }
  11159. } else //init memory
  11160. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11161. data = script_getdata(st, 2);
  11162. get_val(st, data); // Convert into value in case of a variable
  11163. pd->s_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11164. pd->s_skill->lv=script_getnum(st,3);
  11165. pd->s_skill->delay=script_getnum(st,4);
  11166. pd->s_skill->hp=script_getnum(st,5);
  11167. pd->s_skill->sp=script_getnum(st,6);
  11168. //Use delay as initial offset to avoid skill/heal exploits
  11169. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11170. pd->s_skill->timer = INVALID_TIMER;
  11171. else
  11172. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  11173. return SCRIPT_CMD_SUCCESS;
  11174. }
  11175. /*==========================================
  11176. * Scripted skill effects [Celest]
  11177. *------------------------------------------*/
  11178. /// skilleffect <skill id>,<level>
  11179. /// skilleffect "<skill name>",<level>
  11180. BUILDIN_FUNC(skilleffect)
  11181. {
  11182. TBL_PC *sd;
  11183. uint16 skill_id, skill_lv;
  11184. struct script_data *data = script_getdata(st, 2);
  11185. get_val(st, data); // Convert into value in case of a variable
  11186. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11187. skill_lv=script_getnum(st,3);
  11188. sd=script_rid2sd(st);
  11189. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  11190. return SCRIPT_CMD_SUCCESS;
  11191. }
  11192. /*==========================================
  11193. * NPC skill effects [Valaris]
  11194. *------------------------------------------*/
  11195. /// npcskilleffect <skill id>,<level>,<x>,<y>
  11196. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  11197. BUILDIN_FUNC(npcskilleffect)
  11198. {
  11199. struct block_list *bl= map_id2bl(st->oid);
  11200. uint16 skill_id, skill_lv;
  11201. int x, y;
  11202. struct script_data *data = script_getdata(st, 2);
  11203. get_val(st, data); // Convert into value in case of a variable
  11204. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11205. skill_lv=script_getnum(st,3);
  11206. x=script_getnum(st,4);
  11207. y=script_getnum(st,5);
  11208. if (bl)
  11209. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  11210. return SCRIPT_CMD_SUCCESS;
  11211. }
  11212. /*==========================================
  11213. * Special effects [Valaris]
  11214. *------------------------------------------*/
  11215. BUILDIN_FUNC(specialeffect)
  11216. {
  11217. struct block_list *bl=map_id2bl(st->oid);
  11218. int type = script_getnum(st,2);
  11219. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11220. if(bl==NULL)
  11221. return 0;
  11222. if( script_hasdata(st,4) )
  11223. {
  11224. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  11225. if(nd)
  11226. clif_specialeffect(&nd->bl, type, target);
  11227. }
  11228. else
  11229. {
  11230. if (target == SELF) {
  11231. TBL_PC *sd=script_rid2sd(st);
  11232. if (sd)
  11233. clif_specialeffect_single(bl,type,sd->fd);
  11234. } else {
  11235. clif_specialeffect(bl, type, target);
  11236. }
  11237. }
  11238. return SCRIPT_CMD_SUCCESS;
  11239. }
  11240. BUILDIN_FUNC(specialeffect2)
  11241. {
  11242. TBL_PC *sd=script_rid2sd(st);
  11243. int type = script_getnum(st,2);
  11244. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11245. if( script_hasdata(st,4) )
  11246. sd = map_nick2sd(script_getstr(st,4));
  11247. if (sd)
  11248. clif_specialeffect(&sd->bl, type, target);
  11249. return SCRIPT_CMD_SUCCESS;
  11250. }
  11251. /*==========================================
  11252. * Nude [Valaris]
  11253. *------------------------------------------*/
  11254. BUILDIN_FUNC(nude)
  11255. {
  11256. TBL_PC *sd = script_rid2sd(st);
  11257. int i, calcflag = 0;
  11258. if( sd == NULL )
  11259. return 0;
  11260. for( i = 0 ; i < EQI_MAX; i++ ) {
  11261. if( sd->equip_index[ i ] >= 0 ) {
  11262. if( !calcflag )
  11263. calcflag = 1;
  11264. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  11265. }
  11266. }
  11267. if( calcflag )
  11268. status_calc_pc(sd,0);
  11269. return SCRIPT_CMD_SUCCESS;
  11270. }
  11271. int atcommand_sub(struct script_state* st,int type){
  11272. TBL_PC dummy_sd;
  11273. TBL_PC* sd;
  11274. int fd;
  11275. const char* cmd;
  11276. cmd = script_getstr(st,2);
  11277. if (st->rid) {
  11278. sd = script_rid2sd(st);
  11279. fd = sd->fd;
  11280. } else { //Use a dummy character.
  11281. sd = &dummy_sd;
  11282. fd = 0;
  11283. memset(&dummy_sd, 0, sizeof(TBL_PC));
  11284. if (st->oid)
  11285. {
  11286. struct block_list* bl = map_id2bl(st->oid);
  11287. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  11288. if (bl->type == BL_NPC)
  11289. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  11290. }
  11291. }
  11292. if (!is_atcommand(fd, sd, cmd, type)) {
  11293. ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd);
  11294. script_reportsrc(st);
  11295. return 1;
  11296. }
  11297. return SCRIPT_CMD_SUCCESS;
  11298. }
  11299. /*==========================================
  11300. * gmcommand [MouseJstr]
  11301. *------------------------------------------*/
  11302. BUILDIN_FUNC(atcommand) {
  11303. return atcommand_sub(st,0);
  11304. }
  11305. /*==========================================
  11306. * Displays a message for the player only (like system messages like "you got an apple" )
  11307. *------------------------------------------*/
  11308. BUILDIN_FUNC(dispbottom)
  11309. {
  11310. TBL_PC *sd=script_rid2sd(st);
  11311. const char *message;
  11312. message=script_getstr(st,2);
  11313. if(sd)
  11314. clif_disp_onlyself(sd,message,(int)strlen(message));
  11315. return SCRIPT_CMD_SUCCESS;
  11316. }
  11317. /*===================================
  11318. * Heal portion of recovery command
  11319. *-----------------------------------*/
  11320. int recovery_sub(struct map_session_data* sd, int revive)
  11321. {
  11322. if(revive&(1|4) && pc_isdead(sd)) {
  11323. status_revive(&sd->bl, 100, 100);
  11324. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  11325. clif_specialeffect(&sd->bl, 77, AREA);
  11326. } else if(revive&(1|2) && !pc_isdead(sd)) {
  11327. status_percent_heal(&sd->bl, 100, 100);
  11328. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  11329. }
  11330. return SCRIPT_CMD_SUCCESS;
  11331. }
  11332. /*=========================================================================
  11333. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  11334. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  11335. * <type> determines <option>:
  11336. * 0 : char_id
  11337. * 1 : party_id
  11338. * 2 : guild_id
  11339. * 3 : map_name
  11340. * 4 : all characters
  11341. * <revive_flag>:
  11342. * 1 : Revive and heal all players (default)
  11343. * 2 : Heal living players only
  11344. * 4 : Revive dead players only
  11345. * <map name>:
  11346. * for types 1-2 : map_name (null = all maps)
  11347. *-------------------------------------------------------------------------*/
  11348. BUILDIN_FUNC(recovery)
  11349. {
  11350. TBL_PC *sd;
  11351. int map = 0, type = 0, revive = 1;
  11352. type = script_getnum(st,2);
  11353. if(script_hasdata(st,4))
  11354. revive = script_getnum(st,4);
  11355. switch(type) {
  11356. case 0:
  11357. if(script_hasdata(st,3))
  11358. sd=map_charid2sd(script_getnum(st,3));
  11359. else if((sd = script_rid2sd(st)) == NULL)
  11360. return 0; //If we don't have sd by now, bail out
  11361. recovery_sub(sd, revive);
  11362. break;
  11363. case 1:
  11364. {
  11365. struct party_data* p;
  11366. struct map_session_data* pl_sd;
  11367. //When no party given, we use invoker party
  11368. int p_id = 0, i;
  11369. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11370. map = map_mapname2mapid(script_getstr(st,5));
  11371. if(map < 1) { //But we'll check anyways
  11372. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11373. return 1;
  11374. }
  11375. }
  11376. if(script_hasdata(st,3))
  11377. p_id = script_getnum(st,3);
  11378. else if((sd = script_rid2sd(st)))
  11379. p_id = sd->status.party_id;
  11380. p = party_search(p_id);
  11381. if(p == NULL)
  11382. return 0;
  11383. for (i = 0; i < MAX_PARTY; i++) {
  11384. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  11385. || (map && pl_sd->bl.m != map))
  11386. continue;
  11387. recovery_sub(pl_sd, revive);
  11388. }
  11389. break;
  11390. }
  11391. case 2:
  11392. {
  11393. struct guild* g;
  11394. struct map_session_data* pl_sd;
  11395. //When no guild given, we use invoker guild
  11396. int g_id = 0, i;
  11397. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11398. map = map_mapname2mapid(script_getstr(st,5));
  11399. if(map < 1) { //But we'll check anyways
  11400. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11401. return 1;
  11402. }
  11403. }
  11404. if(script_hasdata(st,3))
  11405. g_id = script_getnum(st,3);
  11406. else if((sd = script_rid2sd(st)))
  11407. g_id = sd->status.guild_id;
  11408. g = guild_search(g_id);
  11409. if(g == NULL)
  11410. return 0;
  11411. for (i = 0; i < MAX_GUILD; i++) {
  11412. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  11413. || (map && pl_sd->bl.m != map))
  11414. continue;
  11415. recovery_sub(pl_sd, revive);
  11416. }
  11417. break;
  11418. }
  11419. case 3:
  11420. if(script_hasdata(st,3))
  11421. map = map_mapname2mapid(script_getstr(st,3));
  11422. else if((sd = script_rid2sd(st)))
  11423. map = sd->bl.m;
  11424. if(map < 1)
  11425. return 1; //No sd and no map given - return
  11426. case 4:
  11427. {
  11428. struct s_mapiterator *iter;
  11429. struct script_data *data = script_getdata(st, 3);
  11430. get_val(st, data); // Convert into value in case of a variable
  11431. if(script_hasdata(st,3) && !data_isstring(data))
  11432. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  11433. iter = mapit_getallusers();
  11434. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11435. if(type == 3 && sd->bl.m != map)
  11436. continue;
  11437. recovery_sub(sd, revive);
  11438. }
  11439. mapit_free(iter);
  11440. break;
  11441. }
  11442. default:
  11443. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  11444. script_pushint(st,-1);
  11445. return 1;
  11446. }
  11447. script_pushint(st,1); //Successfully executed without errors
  11448. return SCRIPT_CMD_SUCCESS;
  11449. }
  11450. /*==========================================
  11451. * Get your pet info: getpetinfo(n)
  11452. * n -> 0:pet_id 1:pet_class 2:pet_name
  11453. * 3:friendly 4:hungry, 5: rename flag.6:level
  11454. *------------------------------------------*/
  11455. BUILDIN_FUNC(getpetinfo)
  11456. {
  11457. TBL_PC *sd=script_rid2sd(st);
  11458. TBL_PET *pd;
  11459. int type=script_getnum(st,2);
  11460. if(!sd || !sd->pd) {
  11461. if (type == 2)
  11462. script_pushconststr(st,"null");
  11463. else
  11464. script_pushint(st,0);
  11465. return 0;
  11466. }
  11467. pd = sd->pd;
  11468. switch(type){
  11469. case 0: script_pushint(st,pd->pet.pet_id); break;
  11470. case 1: script_pushint(st,pd->pet.class_); break;
  11471. case 2: script_pushstrcopy(st,pd->pet.name); break;
  11472. case 3: script_pushint(st,pd->pet.intimate); break;
  11473. case 4: script_pushint(st,pd->pet.hungry); break;
  11474. case 5: script_pushint(st,pd->pet.rename_flag); break;
  11475. case 6: script_pushint(st,(int)pd->pet.level); break;
  11476. default:
  11477. script_pushint(st,0);
  11478. break;
  11479. }
  11480. return SCRIPT_CMD_SUCCESS;
  11481. }
  11482. /*==========================================
  11483. * Get your homunculus info: gethominfo(n)
  11484. * n -> 0:hom_id 1:class 2:name
  11485. * 3:friendly 4:hungry, 5: rename flag.
  11486. * 6: level
  11487. *------------------------------------------*/
  11488. BUILDIN_FUNC(gethominfo)
  11489. {
  11490. TBL_PC *sd=script_rid2sd(st);
  11491. TBL_HOM *hd;
  11492. int type=script_getnum(st,2);
  11493. hd = sd?sd->hd:NULL;
  11494. if(!hd) {
  11495. if (type == 2)
  11496. script_pushconststr(st,"null");
  11497. else
  11498. script_pushint(st,0);
  11499. return 0;
  11500. }
  11501. switch(type){
  11502. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  11503. case 1: script_pushint(st,hd->homunculus.class_); break;
  11504. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  11505. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  11506. case 4: script_pushint(st,hd->homunculus.hunger); break;
  11507. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  11508. case 6: script_pushint(st,hd->homunculus.level); break;
  11509. default:
  11510. script_pushint(st,0);
  11511. break;
  11512. }
  11513. return SCRIPT_CMD_SUCCESS;
  11514. }
  11515. /// Retrieves information about character's mercenary
  11516. /// getmercinfo <type>[,<char id>];
  11517. BUILDIN_FUNC(getmercinfo)
  11518. {
  11519. int type, char_id;
  11520. struct map_session_data* sd;
  11521. struct mercenary_data* md;
  11522. type = script_getnum(st,2);
  11523. if( script_hasdata(st,3) )
  11524. {
  11525. char_id = script_getnum(st,3);
  11526. if( ( sd = map_charid2sd(char_id) ) == NULL )
  11527. {
  11528. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  11529. script_pushnil(st);
  11530. return 1;
  11531. }
  11532. }
  11533. else
  11534. {
  11535. if( ( sd = script_rid2sd(st) ) == NULL )
  11536. {
  11537. script_pushnil(st);
  11538. return 0;
  11539. }
  11540. }
  11541. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  11542. switch( type )
  11543. {
  11544. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  11545. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  11546. case 2:
  11547. if( md )
  11548. script_pushstrcopy(st,md->db->name);
  11549. else
  11550. script_pushconststr(st,"");
  11551. break;
  11552. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  11553. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  11554. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  11555. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  11556. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  11557. default:
  11558. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  11559. script_pushnil(st);
  11560. return 1;
  11561. }
  11562. return SCRIPT_CMD_SUCCESS;
  11563. }
  11564. /*==========================================
  11565. * Shows wether your inventory(and equips) contain
  11566. selected card or not.
  11567. checkequipedcard(4001);
  11568. *------------------------------------------*/
  11569. BUILDIN_FUNC(checkequipedcard)
  11570. {
  11571. TBL_PC *sd=script_rid2sd(st);
  11572. if(sd){
  11573. int n,i,c=0;
  11574. c=script_getnum(st,2);
  11575. for(i=0;i<MAX_INVENTORY;i++){
  11576. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  11577. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  11578. continue;
  11579. for(n=0;n<sd->inventory_data[i]->slot;n++){
  11580. if(sd->status.inventory[i].card[n]==c){
  11581. script_pushint(st,1);
  11582. return 0;
  11583. }
  11584. }
  11585. }
  11586. }
  11587. }
  11588. script_pushint(st,0);
  11589. return SCRIPT_CMD_SUCCESS;
  11590. }
  11591. BUILDIN_FUNC(jump_zero)
  11592. {
  11593. int sel;
  11594. sel=script_getnum(st,2);
  11595. if(!sel) {
  11596. int pos;
  11597. if( !data_islabel(script_getdata(st,3)) ){
  11598. ShowError("script: jump_zero: not label !\n");
  11599. st->state=END;
  11600. return 1;
  11601. }
  11602. pos=script_getnum(st,3);
  11603. st->pos=pos;
  11604. st->state=GOTO;
  11605. }
  11606. return SCRIPT_CMD_SUCCESS;
  11607. }
  11608. /*==========================================
  11609. * movenpc [MouseJstr]
  11610. *------------------------------------------*/
  11611. BUILDIN_FUNC(movenpc)
  11612. {
  11613. TBL_NPC *nd = NULL;
  11614. const char *npc;
  11615. int x,y;
  11616. npc = script_getstr(st,2);
  11617. x = script_getnum(st,3);
  11618. y = script_getnum(st,4);
  11619. if ((nd = npc_name2id(npc)) == NULL)
  11620. return -1;
  11621. if (script_hasdata(st,5))
  11622. nd->ud.dir = script_getnum(st,5) % 8;
  11623. npc_movenpc(nd, x, y);
  11624. return SCRIPT_CMD_SUCCESS;
  11625. }
  11626. /*==========================================
  11627. * message [MouseJstr]
  11628. *------------------------------------------*/
  11629. BUILDIN_FUNC(message)
  11630. {
  11631. const char *msg,*player;
  11632. TBL_PC *pl_sd = NULL;
  11633. player = script_getstr(st,2);
  11634. msg = script_getstr(st,3);
  11635. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  11636. return 0;
  11637. clif_displaymessage(pl_sd->fd, msg);
  11638. return SCRIPT_CMD_SUCCESS;
  11639. }
  11640. /*==========================================
  11641. * npctalk (sends message to surrounding area)
  11642. *------------------------------------------*/
  11643. BUILDIN_FUNC(npctalk)
  11644. {
  11645. const char* str;
  11646. char name[NAME_LENGTH], message[256];
  11647. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  11648. str = script_getstr(st,2);
  11649. if(nd)
  11650. {
  11651. safestrncpy(name, nd->name, sizeof(name));
  11652. strtok(name, "#"); // discard extra name identifier if present
  11653. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  11654. clif_disp_overhead(&nd->bl, message);
  11655. }
  11656. return SCRIPT_CMD_SUCCESS;
  11657. }
  11658. // change npc walkspeed [Valaris]
  11659. BUILDIN_FUNC(npcspeed)
  11660. {
  11661. struct npc_data* nd;
  11662. int speed;
  11663. speed = script_getnum(st,2);
  11664. nd =(struct npc_data *)map_id2bl(st->oid);
  11665. if( nd ) {
  11666. nd->speed = speed;
  11667. nd->ud.state.speed_changed = 1;
  11668. }
  11669. return SCRIPT_CMD_SUCCESS;
  11670. }
  11671. // make an npc walk to a position [Valaris]
  11672. BUILDIN_FUNC(npcwalkto)
  11673. {
  11674. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11675. int x=0,y=0;
  11676. x=script_getnum(st,2);
  11677. y=script_getnum(st,3);
  11678. if(nd) {
  11679. if (!nd->status.hp) {
  11680. status_calc_npc(nd, true);
  11681. } else {
  11682. status_calc_npc(nd, false);
  11683. }
  11684. unit_walktoxy(&nd->bl,x,y,0);
  11685. }
  11686. return SCRIPT_CMD_SUCCESS;
  11687. }
  11688. // stop an npc's movement [Valaris]
  11689. BUILDIN_FUNC(npcstop)
  11690. {
  11691. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11692. if(nd) {
  11693. unit_stop_walking(&nd->bl,1|4);
  11694. }
  11695. return SCRIPT_CMD_SUCCESS;
  11696. }
  11697. /*==========================================
  11698. * getlook char info. getlook(arg)
  11699. *------------------------------------------*/
  11700. BUILDIN_FUNC(getlook)
  11701. {
  11702. int type,val;
  11703. TBL_PC *sd;
  11704. sd=script_rid2sd(st);
  11705. type=script_getnum(st,2);
  11706. val=-1;
  11707. switch(type) {
  11708. case LOOK_HAIR: val=sd->status.hair; break; //1
  11709. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  11710. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  11711. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  11712. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  11713. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  11714. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  11715. case LOOK_SHIELD: val=sd->status.shield; break; //8
  11716. case LOOK_SHOES: break; //9
  11717. case LOOK_ROBE: val=sd->status.robe; break; //12
  11718. }
  11719. script_pushint(st,val);
  11720. return SCRIPT_CMD_SUCCESS;
  11721. }
  11722. /*==========================================
  11723. * get char save point. argument: 0- map name, 1- x, 2- y
  11724. *------------------------------------------*/
  11725. BUILDIN_FUNC(getsavepoint)
  11726. {
  11727. TBL_PC* sd;
  11728. int type;
  11729. sd = script_rid2sd(st);
  11730. if (sd == NULL) {
  11731. script_pushint(st,0);
  11732. return 0;
  11733. }
  11734. type = script_getnum(st,2);
  11735. switch(type) {
  11736. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  11737. case 1: script_pushint(st,sd->status.save_point.x); break;
  11738. case 2: script_pushint(st,sd->status.save_point.y); break;
  11739. default:
  11740. script_pushint(st,0);
  11741. break;
  11742. }
  11743. return SCRIPT_CMD_SUCCESS;
  11744. }
  11745. /*==========================================
  11746. * Get position for char/npc/pet/mob objects. Added by Lorky
  11747. *
  11748. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  11749. * where type:
  11750. * MapName$ - String variable for output map name
  11751. * MapX - Integer variable for output coord X
  11752. * MapY - Integer variable for output coord Y
  11753. * type - type of object
  11754. * 0 - Character coord
  11755. * 1 - NPC coord
  11756. * 2 - Pet coord
  11757. * 3 - Mob coord (not released)
  11758. * 4 - Homun coord
  11759. * 5 - Mercenary coord
  11760. * 6 - Elemental coord
  11761. * CharName$ - Name object. If miss or "this" the current object
  11762. *
  11763. * Return:
  11764. * 0 - success
  11765. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  11766. *------------------------------------------*/
  11767. BUILDIN_FUNC(getmapxy)
  11768. {
  11769. struct block_list *bl = NULL;
  11770. TBL_PC *sd=NULL;
  11771. int num;
  11772. const char *name;
  11773. char prefix;
  11774. int x,y,type;
  11775. char mapname[MAP_NAME_LENGTH];
  11776. if( !data_isreference(script_getdata(st,2)) ){
  11777. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  11778. script_pushint(st,-1);
  11779. return 1;
  11780. }
  11781. if( !data_isreference(script_getdata(st,3)) ){
  11782. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  11783. script_pushint(st,-1);
  11784. return 1;
  11785. }
  11786. if( !data_isreference(script_getdata(st,4)) ){
  11787. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  11788. script_pushint(st,-1);
  11789. return 1;
  11790. }
  11791. // Possible needly check function parameters on C_STR,C_INT,C_INT
  11792. type=script_getnum(st,5);
  11793. switch (type){
  11794. case 0: //Get Character Position
  11795. if( script_hasdata(st,6) )
  11796. sd=map_nick2sd(script_getstr(st,6));
  11797. else
  11798. sd=script_rid2sd(st);
  11799. if (sd)
  11800. bl = &sd->bl;
  11801. break;
  11802. case 1: //Get NPC Position
  11803. if( script_hasdata(st,6) )
  11804. {
  11805. struct npc_data *nd;
  11806. nd=npc_name2id(script_getstr(st,6));
  11807. if (nd)
  11808. bl = &nd->bl;
  11809. } else //In case the origin is not an npc?
  11810. bl=map_id2bl(st->oid);
  11811. break;
  11812. case 2: //Get Pet Position
  11813. if(script_hasdata(st,6))
  11814. sd=map_nick2sd(script_getstr(st,6));
  11815. else
  11816. sd=script_rid2sd(st);
  11817. if (sd && sd->pd)
  11818. bl = &sd->pd->bl;
  11819. break;
  11820. case 3: //Get Mob Position
  11821. break; //Not supported?
  11822. case 4: //Get Homun Position
  11823. if(script_hasdata(st,6))
  11824. sd=map_nick2sd(script_getstr(st,6));
  11825. else
  11826. sd=script_rid2sd(st);
  11827. if (sd && sd->hd)
  11828. bl = &sd->hd->bl;
  11829. break;
  11830. case 5: //Get Mercenary Position
  11831. if(script_hasdata(st,6))
  11832. sd=map_nick2sd(script_getstr(st,6));
  11833. else
  11834. sd=script_rid2sd(st);
  11835. if (sd && sd->md)
  11836. bl = &sd->md->bl;
  11837. break;
  11838. case 6: //Get Elemental Position
  11839. if(script_hasdata(st,6))
  11840. sd=map_nick2sd(script_getstr(st,6));
  11841. else
  11842. sd=script_rid2sd(st);
  11843. if (sd && sd->ed)
  11844. bl = &sd->ed->bl;
  11845. break;
  11846. default:
  11847. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  11848. script_pushint(st,-1);
  11849. return 1;
  11850. }
  11851. if (!bl) { //No object found.
  11852. script_pushint(st,-1);
  11853. return 0;
  11854. }
  11855. x= bl->x;
  11856. y= bl->y;
  11857. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  11858. //Set MapName$
  11859. num=st->stack->stack_data[st->start+2].u.num;
  11860. name=get_str(num&0x00ffffff);
  11861. prefix=*name;
  11862. if(not_server_variable(prefix))
  11863. sd=script_rid2sd(st);
  11864. else
  11865. sd=NULL;
  11866. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  11867. //Set MapX
  11868. num=st->stack->stack_data[st->start+3].u.num;
  11869. name=get_str(num&0x00ffffff);
  11870. prefix=*name;
  11871. if(not_server_variable(prefix))
  11872. sd=script_rid2sd(st);
  11873. else
  11874. sd=NULL;
  11875. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  11876. //Set MapY
  11877. num=st->stack->stack_data[st->start+4].u.num;
  11878. name=get_str(num&0x00ffffff);
  11879. prefix=*name;
  11880. if(not_server_variable(prefix))
  11881. sd=script_rid2sd(st);
  11882. else
  11883. sd=NULL;
  11884. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  11885. //Return Success value
  11886. script_pushint(st,0);
  11887. return SCRIPT_CMD_SUCCESS;
  11888. }
  11889. /*==========================================
  11890. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  11891. *------------------------------------------*/
  11892. BUILDIN_FUNC(logmes)
  11893. {
  11894. const char *str;
  11895. TBL_PC* sd;
  11896. sd = script_rid2sd(st);
  11897. if( sd == NULL )
  11898. return 1;
  11899. str = script_getstr(st,2);
  11900. log_npc(sd,str);
  11901. return SCRIPT_CMD_SUCCESS;
  11902. }
  11903. BUILDIN_FUNC(summon)
  11904. {
  11905. int _class, timeout=0;
  11906. const char *str,*event="";
  11907. TBL_PC *sd;
  11908. struct mob_data *md;
  11909. int tick = gettick();
  11910. sd=script_rid2sd(st);
  11911. if (!sd) return 0;
  11912. str =script_getstr(st,2);
  11913. _class=script_getnum(st,3);
  11914. if( script_hasdata(st,4) )
  11915. timeout=script_getnum(st,4);
  11916. if( script_hasdata(st,5) ){
  11917. event=script_getstr(st,5);
  11918. check_event(st, event);
  11919. }
  11920. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  11921. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  11922. if (md) {
  11923. md->master_id=sd->bl.id;
  11924. md->special_state.ai = AI_ATTACK;
  11925. if( md->deletetimer != INVALID_TIMER )
  11926. delete_timer(md->deletetimer, mob_timer_delete);
  11927. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  11928. mob_spawn (md); //Now it is ready for spawning.
  11929. clif_specialeffect(&md->bl,344,AREA);
  11930. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  11931. }
  11932. return SCRIPT_CMD_SUCCESS;
  11933. }
  11934. /*==========================================
  11935. * Checks whether it is daytime/nighttime
  11936. *------------------------------------------*/
  11937. BUILDIN_FUNC(isnight)
  11938. {
  11939. script_pushint(st,(night_flag == 1));
  11940. return SCRIPT_CMD_SUCCESS;
  11941. }
  11942. BUILDIN_FUNC(isday)
  11943. {
  11944. script_pushint(st,(night_flag == 0));
  11945. return SCRIPT_CMD_SUCCESS;
  11946. }
  11947. /*================================================
  11948. * Check how many items/cards in the list are
  11949. * equipped - used for 2/15's cards patch [celest]
  11950. *------------------------------------------------*/
  11951. BUILDIN_FUNC(isequippedcnt)
  11952. {
  11953. TBL_PC *sd;
  11954. int i, j, k, id = 1;
  11955. int ret = 0;
  11956. sd = script_rid2sd(st);
  11957. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11958. script_pushint(st,0);
  11959. return 0;
  11960. }
  11961. for (i=0; id!=0; i++) {
  11962. FETCH (i+2, id) else id = 0;
  11963. if (id <= 0)
  11964. continue;
  11965. for (j=0; j<EQI_MAX; j++) {
  11966. int index;
  11967. index = sd->equip_index[j];
  11968. if(index < 0) continue;
  11969. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11970. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11971. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11972. if(!sd->inventory_data[index])
  11973. continue;
  11974. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  11975. if (sd->inventory_data[index]->nameid == id)
  11976. ret+= sd->status.inventory[index].amount;
  11977. } else { //Count cards.
  11978. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11979. continue; //No cards
  11980. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11981. if (sd->status.inventory[index].card[k] == id)
  11982. ret++; //[Lupus]
  11983. }
  11984. }
  11985. }
  11986. }
  11987. script_pushint(st,ret);
  11988. return SCRIPT_CMD_SUCCESS;
  11989. }
  11990. /*================================================
  11991. * Check whether another card has been
  11992. * equipped - used for 2/15's cards patch [celest]
  11993. * -- Items checked cannot be reused in another
  11994. * card set to prevent exploits
  11995. *------------------------------------------------*/
  11996. BUILDIN_FUNC(isequipped)
  11997. {
  11998. TBL_PC *sd;
  11999. int i, j, k, id = 1;
  12000. int index, flag;
  12001. int ret = -1;
  12002. //Original hash to reverse it when full check fails.
  12003. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  12004. sd = script_rid2sd(st);
  12005. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12006. script_pushint(st,0);
  12007. return 0;
  12008. }
  12009. setitem_hash = sd->bonus.setitem_hash;
  12010. setitem_hash2 = sd->bonus.setitem_hash2;
  12011. for (i=0; id!=0; i++) {
  12012. FETCH (i+2, id) else id = 0;
  12013. if (id <= 0)
  12014. continue;
  12015. flag = 0;
  12016. for (j=0; j<EQI_MAX; j++) {
  12017. index = sd->equip_index[j];
  12018. if(index < 0) continue;
  12019. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  12020. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  12021. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  12022. if(!sd->inventory_data[index])
  12023. continue;
  12024. if (itemdb_type(id) != IT_CARD) {
  12025. if (sd->inventory_data[index]->nameid != id)
  12026. continue;
  12027. flag = 1;
  12028. break;
  12029. } else { //Cards
  12030. if (sd->inventory_data[index]->slot == 0 ||
  12031. itemdb_isspecial(sd->status.inventory[index].card[0]))
  12032. continue;
  12033. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  12034. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  12035. unsigned int hash = 0;
  12036. if (sd->status.inventory[index].card[k] != id)
  12037. continue;
  12038. hash = 1<<((j<5?j:j-5)*4 + k);
  12039. // check if card is already used by another set
  12040. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  12041. continue;
  12042. // We have found a match
  12043. flag = 1;
  12044. // Set hash so this card cannot be used by another
  12045. if (j<5)
  12046. sd->bonus.setitem_hash |= hash;
  12047. else
  12048. sd->bonus.setitem_hash2 |= hash;
  12049. break;
  12050. }
  12051. }
  12052. if (flag) break; //Card found
  12053. }
  12054. if (ret == -1)
  12055. ret = flag;
  12056. else
  12057. ret &= flag;
  12058. if (!ret) break;
  12059. }
  12060. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  12061. sd->bonus.setitem_hash = setitem_hash;
  12062. sd->bonus.setitem_hash2 = setitem_hash2;
  12063. }
  12064. script_pushint(st,ret);
  12065. return SCRIPT_CMD_SUCCESS;
  12066. }
  12067. /*================================================
  12068. * Check how many given inserted cards in the CURRENT
  12069. * weapon - used for 2/15's cards patch [Lupus]
  12070. *------------------------------------------------*/
  12071. BUILDIN_FUNC(cardscnt)
  12072. {
  12073. TBL_PC *sd;
  12074. int i, k, id = 1;
  12075. int ret = 0;
  12076. int index;
  12077. sd = script_rid2sd(st);
  12078. for (i=0; id!=0; i++) {
  12079. FETCH (i+2, id) else id = 0;
  12080. if (id <= 0)
  12081. continue;
  12082. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  12083. if(index < 0) continue;
  12084. if(!sd->inventory_data[index])
  12085. continue;
  12086. if(itemdb_type(id) != IT_CARD) {
  12087. if (sd->inventory_data[index]->nameid == id)
  12088. ret+= sd->status.inventory[index].amount;
  12089. } else {
  12090. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12091. continue;
  12092. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12093. if (sd->status.inventory[index].card[k] == id)
  12094. ret++;
  12095. }
  12096. }
  12097. }
  12098. script_pushint(st,ret);
  12099. // script_pushint(st,current_equip_item_index);
  12100. return SCRIPT_CMD_SUCCESS;
  12101. }
  12102. /*=======================================================
  12103. * Returns the refined number of the current item, or an
  12104. * item with inventory index specified
  12105. *-------------------------------------------------------*/
  12106. BUILDIN_FUNC(getrefine)
  12107. {
  12108. TBL_PC *sd;
  12109. if ((sd = script_rid2sd(st))!= NULL)
  12110. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  12111. else
  12112. script_pushint(st,0);
  12113. return SCRIPT_CMD_SUCCESS;
  12114. }
  12115. /*=======================================================
  12116. * Day/Night controls
  12117. *-------------------------------------------------------*/
  12118. BUILDIN_FUNC(night)
  12119. {
  12120. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  12121. return SCRIPT_CMD_SUCCESS;
  12122. }
  12123. BUILDIN_FUNC(day)
  12124. {
  12125. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  12126. return SCRIPT_CMD_SUCCESS;
  12127. }
  12128. //=======================================================
  12129. // Unequip [Spectre]
  12130. //-------------------------------------------------------
  12131. BUILDIN_FUNC(unequip)
  12132. {
  12133. int i;
  12134. size_t num;
  12135. TBL_PC *sd;
  12136. num = script_getnum(st,2);
  12137. sd = script_rid2sd(st);
  12138. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  12139. {
  12140. i = pc_checkequip(sd,equip[num-1]);
  12141. if (i >= 0)
  12142. pc_unequipitem(sd,i,1|2);
  12143. }
  12144. return SCRIPT_CMD_SUCCESS;
  12145. }
  12146. BUILDIN_FUNC(equip)
  12147. {
  12148. int nameid=0,i;
  12149. TBL_PC *sd;
  12150. struct item_data *item_data;
  12151. sd = script_rid2sd(st);
  12152. nameid=script_getnum(st,2);
  12153. if((item_data = itemdb_exists(nameid)) == NULL)
  12154. {
  12155. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  12156. return 1;
  12157. }
  12158. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  12159. if( i < MAX_INVENTORY )
  12160. pc_equipitem(sd,i,item_data->equip);
  12161. return SCRIPT_CMD_SUCCESS;
  12162. }
  12163. BUILDIN_FUNC(autoequip)
  12164. {
  12165. int nameid, flag;
  12166. struct item_data *item_data;
  12167. nameid=script_getnum(st,2);
  12168. flag=script_getnum(st,3);
  12169. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  12170. {
  12171. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  12172. return SCRIPT_CMD_FAILURE;
  12173. }
  12174. if( !itemdb_isequip2(item_data) )
  12175. {
  12176. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  12177. return SCRIPT_CMD_FAILURE;
  12178. }
  12179. item_data->flag.autoequip = flag>0?1:0;
  12180. return SCRIPT_CMD_SUCCESS;
  12181. }
  12182. BUILDIN_FUNC(setbattleflag)
  12183. {
  12184. const char *flag, *value;
  12185. flag = script_getstr(st,2);
  12186. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  12187. if (battle_set_value(flag, value) == 0)
  12188. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  12189. else
  12190. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  12191. return SCRIPT_CMD_SUCCESS;
  12192. }
  12193. BUILDIN_FUNC(getbattleflag)
  12194. {
  12195. const char *flag;
  12196. flag = script_getstr(st,2);
  12197. script_pushint(st,battle_get_value(flag));
  12198. return SCRIPT_CMD_SUCCESS;
  12199. }
  12200. //=======================================================
  12201. // strlen [Valaris]
  12202. //-------------------------------------------------------
  12203. BUILDIN_FUNC(getstrlen)
  12204. {
  12205. const char *str = script_getstr(st,2);
  12206. int len = (str) ? (int)strlen(str) : 0;
  12207. script_pushint(st,len);
  12208. return SCRIPT_CMD_SUCCESS;
  12209. }
  12210. //=======================================================
  12211. // isalpha [Valaris]
  12212. //-------------------------------------------------------
  12213. BUILDIN_FUNC(charisalpha)
  12214. {
  12215. const char *str=script_getstr(st,2);
  12216. int pos=script_getnum(st,3);
  12217. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  12218. script_pushint(st,val);
  12219. return SCRIPT_CMD_SUCCESS;
  12220. }
  12221. //=======================================================
  12222. // charisupper <str>, <index>
  12223. //-------------------------------------------------------
  12224. BUILDIN_FUNC(charisupper)
  12225. {
  12226. const char *str = script_getstr(st,2);
  12227. int pos = script_getnum(st,3);
  12228. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  12229. script_pushint(st,val);
  12230. return SCRIPT_CMD_SUCCESS;
  12231. }
  12232. //=======================================================
  12233. // charislower <str>, <index>
  12234. //-------------------------------------------------------
  12235. BUILDIN_FUNC(charislower)
  12236. {
  12237. const char *str = script_getstr(st,2);
  12238. int pos = script_getnum(st,3);
  12239. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  12240. script_pushint(st,val);
  12241. return SCRIPT_CMD_SUCCESS;
  12242. }
  12243. //=======================================================
  12244. // charat <str>, <index>
  12245. //-------------------------------------------------------
  12246. BUILDIN_FUNC(charat) {
  12247. const char *str = script_getstr(st,2);
  12248. int pos = script_getnum(st,3);
  12249. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  12250. char output[2];
  12251. output[0] = str[pos];
  12252. output[1] = '\0';
  12253. script_pushstrcopy(st, output);
  12254. } else
  12255. script_pushconststr(st, "");
  12256. return SCRIPT_CMD_SUCCESS;
  12257. }
  12258. //=======================================================
  12259. // setchar <string>, <char>, <index>
  12260. //-------------------------------------------------------
  12261. BUILDIN_FUNC(setchar)
  12262. {
  12263. const char *str = script_getstr(st,2);
  12264. const char *c = script_getstr(st,3);
  12265. int index = script_getnum(st,4);
  12266. char *output = aStrdup(str);
  12267. if(index >= 0 && index < strlen(output))
  12268. output[index] = *c;
  12269. script_pushstr(st, output);
  12270. return SCRIPT_CMD_SUCCESS;
  12271. }
  12272. //=======================================================
  12273. // insertchar <string>, <char>, <index>
  12274. //-------------------------------------------------------
  12275. BUILDIN_FUNC(insertchar)
  12276. {
  12277. const char *str = script_getstr(st,2);
  12278. const char *c = script_getstr(st,3);
  12279. int index = script_getnum(st,4);
  12280. char *output;
  12281. size_t len = strlen(str);
  12282. if(index < 0)
  12283. index = 0;
  12284. else if(index > len)
  12285. index = len;
  12286. output = (char*)aMalloc(len + 2);
  12287. memcpy(output, str, index);
  12288. output[index] = c[0];
  12289. memcpy(&output[index+1], &str[index], len - index);
  12290. output[len+1] = '\0';
  12291. script_pushstr(st, output);
  12292. return SCRIPT_CMD_SUCCESS;
  12293. }
  12294. //=======================================================
  12295. // delchar <string>, <index>
  12296. //-------------------------------------------------------
  12297. BUILDIN_FUNC(delchar)
  12298. {
  12299. const char *str = script_getstr(st,2);
  12300. int index = script_getnum(st,3);
  12301. char *output;
  12302. size_t len = strlen(str);
  12303. if(index < 0 || index > len) {
  12304. //return original
  12305. output = aStrdup(str);
  12306. script_pushstr(st, output);
  12307. return 0;
  12308. }
  12309. output = (char*)aMalloc(len);
  12310. memcpy(output, str, index);
  12311. memcpy(&output[index], &str[index+1], len - index);
  12312. script_pushstr(st, output);
  12313. return SCRIPT_CMD_SUCCESS;
  12314. }
  12315. //=======================================================
  12316. // strtoupper <str>
  12317. //-------------------------------------------------------
  12318. BUILDIN_FUNC(strtoupper)
  12319. {
  12320. const char *str = script_getstr(st,2);
  12321. char *output = aStrdup(str);
  12322. char *cursor = output;
  12323. while (*cursor != '\0') {
  12324. *cursor = TOUPPER(*cursor);
  12325. cursor++;
  12326. }
  12327. script_pushstr(st, output);
  12328. return SCRIPT_CMD_SUCCESS;
  12329. }
  12330. //=======================================================
  12331. // strtolower <str>
  12332. //-------------------------------------------------------
  12333. BUILDIN_FUNC(strtolower)
  12334. {
  12335. const char *str = script_getstr(st,2);
  12336. char *output = aStrdup(str);
  12337. char *cursor = output;
  12338. while (*cursor != '\0') {
  12339. *cursor = TOLOWER(*cursor);
  12340. cursor++;
  12341. }
  12342. script_pushstr(st, output);
  12343. return SCRIPT_CMD_SUCCESS;
  12344. }
  12345. //=======================================================
  12346. // substr <str>, <start>, <end>
  12347. //-------------------------------------------------------
  12348. BUILDIN_FUNC(substr)
  12349. {
  12350. const char *str = script_getstr(st,2);
  12351. char *output;
  12352. int start = script_getnum(st,3);
  12353. int end = script_getnum(st,4);
  12354. int len = 0;
  12355. if(start >= 0 && end < strlen(str) && start <= end) {
  12356. len = end - start + 1;
  12357. output = (char*)aMalloc(len + 1);
  12358. memcpy(output, &str[start], len);
  12359. } else
  12360. output = (char*)aMalloc(1);
  12361. output[len] = '\0';
  12362. script_pushstr(st, output);
  12363. return SCRIPT_CMD_SUCCESS;
  12364. }
  12365. //=======================================================
  12366. // explode <dest_string_array>, <str>, <delimiter>
  12367. // Note: delimiter is limited to 1 char
  12368. //-------------------------------------------------------
  12369. BUILDIN_FUNC(explode)
  12370. {
  12371. struct script_data* data = script_getdata(st, 2);
  12372. const char *str = script_getstr(st,3);
  12373. const char delimiter = script_getstr(st, 4)[0];
  12374. int32 id;
  12375. size_t len = strlen(str);
  12376. int i = 0, j = 0;
  12377. int start;
  12378. char *temp;
  12379. const char* name;
  12380. TBL_PC* sd = NULL;
  12381. temp = (char*)aMalloc(len + 1);
  12382. if( !data_isreference(data) )
  12383. {
  12384. ShowError("script:explode: not a variable\n");
  12385. script_reportdata(data);
  12386. st->state = END;
  12387. return 1;// not a variable
  12388. }
  12389. id = reference_getid(data);
  12390. start = reference_getindex(data);
  12391. name = reference_getname(data);
  12392. if( not_array_variable(*name) )
  12393. {
  12394. ShowError("script:explode: illegal scope\n");
  12395. script_reportdata(data);
  12396. st->state = END;
  12397. return 1;// not supported
  12398. }
  12399. if( !is_string_variable(name) )
  12400. {
  12401. ShowError("script:explode: not string array\n");
  12402. script_reportdata(data);
  12403. st->state = END;
  12404. return 1;// data type mismatch
  12405. }
  12406. if( not_server_variable(*name) )
  12407. {
  12408. sd = script_rid2sd(st);
  12409. if( sd == NULL )
  12410. return 0;// no player attached
  12411. }
  12412. while(str[i] != '\0') {
  12413. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  12414. temp[j] = '\0';
  12415. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  12416. j = 0;
  12417. ++i;
  12418. } else {
  12419. temp[j++] = str[i++];
  12420. }
  12421. }
  12422. //set last string
  12423. temp[j] = '\0';
  12424. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  12425. aFree(temp);
  12426. return SCRIPT_CMD_SUCCESS;
  12427. }
  12428. //=======================================================
  12429. // implode <string_array>
  12430. // implode <string_array>, <glue>
  12431. //-------------------------------------------------------
  12432. BUILDIN_FUNC(implode)
  12433. {
  12434. struct script_data* data = script_getdata(st, 2);
  12435. const char *glue = NULL, *name, *temp;
  12436. int32 glue_len = 0, array_size, id;
  12437. size_t len = 0;
  12438. int i, k = 0;
  12439. TBL_PC* sd = NULL;
  12440. char *output;
  12441. if( !data_isreference(data) )
  12442. {
  12443. ShowError("script:implode: not a variable\n");
  12444. script_reportdata(data);
  12445. st->state = END;
  12446. return 1;// not a variable
  12447. }
  12448. id = reference_getid(data);
  12449. name = reference_getname(data);
  12450. if( not_array_variable(*name) )
  12451. {
  12452. ShowError("script:implode: illegal scope\n");
  12453. script_reportdata(data);
  12454. st->state = END;
  12455. return 1;// not supported
  12456. }
  12457. if( !is_string_variable(name) )
  12458. {
  12459. ShowError("script:implode: not string array\n");
  12460. script_reportdata(data);
  12461. st->state = END;
  12462. return 1;// data type mismatch
  12463. }
  12464. if( not_server_variable(*name) )
  12465. {
  12466. sd = script_rid2sd(st);
  12467. if( sd == NULL )
  12468. return 0;// no player attached
  12469. }
  12470. //count chars
  12471. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  12472. if(array_size == -1) //empty array check (AmsTaff)
  12473. {
  12474. ShowWarning("script:implode: array length = 0\n");
  12475. output = (char*)aMalloc(sizeof(char)*5);
  12476. sprintf(output,"%s","NULL");
  12477. } else {
  12478. for(i = 0; i <= array_size; ++i) {
  12479. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12480. len += strlen(temp);
  12481. script_removetop(st, -1, 0);
  12482. }
  12483. //allocate mem
  12484. if( script_hasdata(st,3) ) {
  12485. glue = script_getstr(st,3);
  12486. glue_len = strlen(glue);
  12487. len += glue_len * (array_size);
  12488. }
  12489. output = (char*)aMalloc(len + 1);
  12490. //build output
  12491. for(i = 0; i < array_size; ++i) {
  12492. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12493. len = strlen(temp);
  12494. memcpy(&output[k], temp, len);
  12495. k += len;
  12496. if(glue_len != 0) {
  12497. memcpy(&output[k], glue, glue_len);
  12498. k += glue_len;
  12499. }
  12500. script_removetop(st, -1, 0);
  12501. }
  12502. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  12503. len = strlen(temp);
  12504. memcpy(&output[k], temp, len);
  12505. k += len;
  12506. script_removetop(st, -1, 0);
  12507. output[k] = '\0';
  12508. }
  12509. script_pushstr(st, output);
  12510. return SCRIPT_CMD_SUCCESS;
  12511. }
  12512. //=======================================================
  12513. // sprintf(<format>, ...);
  12514. // Implements C sprintf, except format %n. The resulting string is
  12515. // returned, instead of being saved in variable by reference.
  12516. //-------------------------------------------------------
  12517. BUILDIN_FUNC(sprintf)
  12518. {
  12519. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  12520. const char* format;
  12521. char* p;
  12522. char* q;
  12523. char* buf = NULL;
  12524. char* buf2 = NULL;
  12525. struct script_data* data;
  12526. StringBuf final_buf;
  12527. // Fetch init data
  12528. format = script_getstr(st, 2);
  12529. argc = script_lastdata(st)-2;
  12530. len = strlen(format);
  12531. // Skip parsing, where no parsing is required.
  12532. if(len==0){
  12533. script_pushconststr(st,"");
  12534. return 0;
  12535. }
  12536. // Pessimistic alloc
  12537. CREATE(buf, char, len+1);
  12538. // Need not be parsed, just solve stuff like %%.
  12539. if(argc==0){
  12540. memcpy(buf,format,len+1);
  12541. script_pushstrcopy(st, buf);
  12542. aFree(buf);
  12543. return 0;
  12544. }
  12545. safestrncpy(buf, format, len+1);
  12546. // Issue sprintf for each parameter
  12547. StringBuf_Init(&final_buf);
  12548. q = buf;
  12549. while((p = strchr(q, '%'))!=NULL){
  12550. if(p!=q){
  12551. len = p-q+1;
  12552. if(buf2_len<len){
  12553. RECREATE(buf2, char, len);
  12554. buf2_len = len;
  12555. }
  12556. safestrncpy(buf2, q, len);
  12557. StringBuf_AppendStr(&final_buf, buf2);
  12558. q = p;
  12559. }
  12560. p = q+1;
  12561. if(*p=='%'){ // %%
  12562. StringBuf_AppendStr(&final_buf, "%");
  12563. q+=2;
  12564. continue;
  12565. }
  12566. if(*p=='n'){ // %n
  12567. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  12568. script_reportsrc(st);
  12569. q+=2;
  12570. continue;
  12571. }
  12572. if(arg>=argc){
  12573. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  12574. if(buf) aFree(buf);
  12575. if(buf2) aFree(buf2);
  12576. StringBuf_Destroy(&final_buf);
  12577. script_pushconststr(st,"");
  12578. return 1;
  12579. }
  12580. if((p = strchr(q+1, '%'))==NULL){
  12581. p = strchr(q, 0); // EOS
  12582. }
  12583. len = p-q+1;
  12584. if(buf2_len<len){
  12585. RECREATE(buf2, char, len);
  12586. buf2_len = len;
  12587. }
  12588. safestrncpy(buf2, q, len);
  12589. q = p;
  12590. // Note: This assumes the passed value being the correct
  12591. // type to the current format specifier. If not, the server
  12592. // probably crashes or returns anything else, than expected,
  12593. // but it would behave in normal code the same way so it's
  12594. // the scripter's responsibility.
  12595. data = script_getdata(st, arg+3);
  12596. if(data_isstring(data)){ // String
  12597. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12598. }else if(data_isint(data)){ // Number
  12599. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12600. }else if(data_isreference(data)){ // Variable
  12601. char* name = reference_getname(data);
  12602. if(name[strlen(name)-1]=='$'){ // var Str
  12603. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12604. }else{ // var Int
  12605. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12606. }
  12607. }else{ // Unsupported type
  12608. ShowError("buildin_sprintf: Unknown argument type!\n");
  12609. if(buf) aFree(buf);
  12610. if(buf2) aFree(buf2);
  12611. StringBuf_Destroy(&final_buf);
  12612. script_pushconststr(st,"");
  12613. return 1;
  12614. }
  12615. arg++;
  12616. }
  12617. // Append anything left
  12618. if(*q){
  12619. StringBuf_AppendStr(&final_buf, q);
  12620. }
  12621. // Passed more, than needed
  12622. if(arg<argc){
  12623. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  12624. script_reportsrc(st);
  12625. }
  12626. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  12627. if(buf) aFree(buf);
  12628. if(buf2) aFree(buf2);
  12629. StringBuf_Destroy(&final_buf);
  12630. return SCRIPT_CMD_SUCCESS;
  12631. }
  12632. //=======================================================
  12633. // sscanf(<str>, <format>, ...);
  12634. // Implements C sscanf.
  12635. //-------------------------------------------------------
  12636. BUILDIN_FUNC(sscanf){
  12637. unsigned int argc, arg = 0, len;
  12638. struct script_data* data;
  12639. struct map_session_data* sd = NULL;
  12640. const char* str;
  12641. const char* format;
  12642. const char* p;
  12643. const char* q;
  12644. char* buf = NULL;
  12645. char* buf_p;
  12646. char* ref_str = NULL;
  12647. int ref_int;
  12648. // Get data
  12649. str = script_getstr(st, 2);
  12650. format = script_getstr(st, 3);
  12651. argc = script_lastdata(st)-3;
  12652. len = strlen(format);
  12653. CREATE(buf, char, len*2+1);
  12654. // Issue sscanf for each parameter
  12655. *buf = 0;
  12656. q = format;
  12657. while((p = strchr(q, '%'))){
  12658. if(p!=q){
  12659. strncat(buf, q, (size_t)(p-q));
  12660. q = p;
  12661. }
  12662. p = q+1;
  12663. if(*p=='*' || *p=='%'){ // Skip
  12664. strncat(buf, q, 2);
  12665. q+=2;
  12666. continue;
  12667. }
  12668. if(arg>=argc){
  12669. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  12670. script_pushint(st, -1);
  12671. if(buf) aFree(buf);
  12672. if(ref_str) aFree(ref_str);
  12673. return 1;
  12674. }
  12675. if((p = strchr(q+1, '%'))==NULL){
  12676. p = strchr(q, 0); // EOS
  12677. }
  12678. len = p-q;
  12679. strncat(buf, q, len);
  12680. q = p;
  12681. // Validate output
  12682. data = script_getdata(st, arg+4);
  12683. if(!data_isreference(data) || !reference_tovariable(data)){
  12684. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  12685. script_pushint(st, -1);
  12686. if(buf) aFree(buf);
  12687. if(ref_str) aFree(ref_str);
  12688. return 1;
  12689. }
  12690. buf_p = reference_getname(data);
  12691. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  12692. script_pushint(st, -1);
  12693. if(buf) aFree(buf);
  12694. if(ref_str) aFree(ref_str);
  12695. return 0;
  12696. }
  12697. // Save value if any
  12698. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  12699. if(ref_str==NULL){
  12700. CREATE(ref_str, char, strlen(str)+1);
  12701. }
  12702. if(sscanf(str, buf, ref_str)==0){
  12703. break;
  12704. }
  12705. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  12706. } else { // Number
  12707. if(sscanf(str, buf, &ref_int)==0){
  12708. break;
  12709. }
  12710. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  12711. }
  12712. arg++;
  12713. // Disable used format (%... -> %*...)
  12714. buf_p = strchr(buf, 0);
  12715. memmove(buf_p-len+2, buf_p-len+1, len);
  12716. *(buf_p-len+1) = '*';
  12717. }
  12718. script_pushint(st, arg);
  12719. if(buf) aFree(buf);
  12720. if(ref_str) aFree(ref_str);
  12721. return SCRIPT_CMD_SUCCESS;
  12722. }
  12723. //=======================================================
  12724. // strpos(<haystack>, <needle>)
  12725. // strpos(<haystack>, <needle>, <offset>)
  12726. //
  12727. // Implements PHP style strpos. Adapted from code from
  12728. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  12729. //-------------------------------------------------------
  12730. BUILDIN_FUNC(strpos) {
  12731. const char *haystack = script_getstr(st,2);
  12732. const char *needle = script_getstr(st,3);
  12733. int i;
  12734. size_t len;
  12735. if( script_hasdata(st,4) )
  12736. i = script_getnum(st,4);
  12737. else
  12738. i = 0;
  12739. if (needle[0] == '\0') {
  12740. script_pushint(st, -1);
  12741. return 0;
  12742. }
  12743. len = strlen(haystack);
  12744. for ( ; i < len; ++i ) {
  12745. if ( haystack[i] == *needle ) {
  12746. // matched starting char -- loop through remaining chars
  12747. const char *h, *n;
  12748. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  12749. if ( *h != *n ) {
  12750. break;
  12751. }
  12752. }
  12753. if ( !*n ) { // matched all of 'needle' to null termination
  12754. script_pushint(st, i);
  12755. return 0;
  12756. }
  12757. }
  12758. }
  12759. script_pushint(st, -1);
  12760. return SCRIPT_CMD_SUCCESS;
  12761. }
  12762. //===============================================================
  12763. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  12764. //
  12765. // Note: Finds all instances of <search> in <input> and replaces
  12766. // with <replace>. If specified will only replace as many
  12767. // instances as specified in <count>. By default will be case
  12768. // sensitive.
  12769. //---------------------------------------------------------------
  12770. BUILDIN_FUNC(replacestr)
  12771. {
  12772. const char *input = script_getstr(st, 2);
  12773. const char *find = script_getstr(st, 3);
  12774. const char *replace = script_getstr(st, 4);
  12775. size_t inputlen = strlen(input);
  12776. size_t findlen = strlen(find);
  12777. struct StringBuf output;
  12778. bool usecase = true;
  12779. int count = 0;
  12780. int numFinds = 0;
  12781. int i = 0, f = 0;
  12782. if(findlen == 0) {
  12783. ShowError("script:replacestr: Invalid search length.\n");
  12784. st->state = END;
  12785. return 1;
  12786. }
  12787. if(script_hasdata(st, 5)) {
  12788. struct script_data *data = script_getdata(st, 5);
  12789. get_val(st, data); // Convert into value in case of a variable
  12790. if( !data_isstring(data) )
  12791. usecase = script_getnum(st, 5) != 0;
  12792. else {
  12793. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  12794. st->state = END;
  12795. return 1;
  12796. }
  12797. }
  12798. if(script_hasdata(st, 6)) {
  12799. count = script_getnum(st, 6);
  12800. if(count == 0) {
  12801. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  12802. st->state = END;
  12803. return 1;
  12804. }
  12805. }
  12806. StringBuf_Init(&output);
  12807. for(; i < inputlen; i++) {
  12808. if(count && count == numFinds) { //found enough, stop looking
  12809. break;
  12810. }
  12811. for(f = 0; f <= findlen; f++) {
  12812. if(f == findlen) { //complete match
  12813. numFinds++;
  12814. StringBuf_AppendStr(&output, replace);
  12815. i += findlen - 1;
  12816. break;
  12817. } else {
  12818. if(usecase) {
  12819. if((i + f) > inputlen || input[i + f] != find[f]) {
  12820. StringBuf_Printf(&output, "%c", input[i]);
  12821. break;
  12822. }
  12823. } else {
  12824. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12825. StringBuf_Printf(&output, "%c", input[i]);
  12826. break;
  12827. }
  12828. }
  12829. }
  12830. }
  12831. }
  12832. //append excess after enough found
  12833. if(i < inputlen)
  12834. StringBuf_AppendStr(&output, &(input[i]));
  12835. script_pushstrcopy(st, StringBuf_Value(&output));
  12836. StringBuf_Destroy(&output);
  12837. return SCRIPT_CMD_SUCCESS;
  12838. }
  12839. //========================================================
  12840. // countstr <input>, <search>{, <usecase>}
  12841. //
  12842. // Note: Counts the number of times <search> occurs in
  12843. // <input>. By default will be case sensitive.
  12844. //--------------------------------------------------------
  12845. BUILDIN_FUNC(countstr)
  12846. {
  12847. const char *input = script_getstr(st, 2);
  12848. const char *find = script_getstr(st, 3);
  12849. size_t inputlen = strlen(input);
  12850. size_t findlen = strlen(find);
  12851. bool usecase = true;
  12852. int numFinds = 0;
  12853. int i = 0, f = 0;
  12854. if(findlen == 0) {
  12855. ShowError("script:countstr: Invalid search length.\n");
  12856. st->state = END;
  12857. return 1;
  12858. }
  12859. if(script_hasdata(st, 4)) {
  12860. struct script_data *data;
  12861. data = script_getdata(st, 4);
  12862. get_val(st, data); // Convert into value in case of a variable
  12863. if( !data_isstring(data) )
  12864. usecase = script_getnum(st, 4) != 0;
  12865. else {
  12866. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  12867. st->state = END;
  12868. return 1;
  12869. }
  12870. }
  12871. for(; i < inputlen; i++) {
  12872. for(f = 0; f <= findlen; f++) {
  12873. if(f == findlen) { //complete match
  12874. numFinds++;
  12875. i += findlen - 1;
  12876. break;
  12877. } else {
  12878. if(usecase) {
  12879. if((i + f) > inputlen || input[i + f] != find[f]) {
  12880. break;
  12881. }
  12882. } else {
  12883. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12884. break;
  12885. }
  12886. }
  12887. }
  12888. }
  12889. }
  12890. script_pushint(st, numFinds);
  12891. return SCRIPT_CMD_SUCCESS;
  12892. }
  12893. /// Changes the display name and/or display class of the npc.
  12894. /// Returns 0 is successful, 1 if the npc does not exist.
  12895. ///
  12896. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  12897. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  12898. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  12899. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  12900. BUILDIN_FUNC(setnpcdisplay)
  12901. {
  12902. const char* name;
  12903. const char* newname = NULL;
  12904. int class_ = -1, size = -1;
  12905. struct script_data* data;
  12906. struct npc_data* nd;
  12907. name = script_getstr(st,2);
  12908. data = script_getdata(st,3);
  12909. if( script_hasdata(st,4) )
  12910. class_ = script_getnum(st,4);
  12911. if( script_hasdata(st,5) )
  12912. size = script_getnum(st,5);
  12913. get_val(st, data);
  12914. if( data_isstring(data) )
  12915. newname = conv_str(st,data);
  12916. else if( data_isint(data) )
  12917. class_ = conv_num(st,data);
  12918. else
  12919. {
  12920. ShowError("script:setnpcdisplay: expected string or number\n");
  12921. script_reportdata(data);
  12922. return 1;
  12923. }
  12924. nd = npc_name2id(name);
  12925. if( nd == NULL )
  12926. {// not found
  12927. script_pushint(st,1);
  12928. return 0;
  12929. }
  12930. // update npc
  12931. if( newname )
  12932. npc_setdisplayname(nd, newname);
  12933. if( size != -1 && size != (int)nd->size )
  12934. nd->size = size;
  12935. else
  12936. size = -1;
  12937. if( class_ != -1 && nd->class_ != class_ )
  12938. npc_setclass(nd, class_);
  12939. else if( size != -1 )
  12940. { // Required to update the visual size
  12941. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  12942. clif_spawn(&nd->bl);
  12943. }
  12944. script_pushint(st,0);
  12945. return SCRIPT_CMD_SUCCESS;
  12946. }
  12947. BUILDIN_FUNC(atoi)
  12948. {
  12949. const char *value;
  12950. value = script_getstr(st,2);
  12951. script_pushint(st,atoi(value));
  12952. return SCRIPT_CMD_SUCCESS;
  12953. }
  12954. // case-insensitive substring search [lordalfa]
  12955. BUILDIN_FUNC(compare)
  12956. {
  12957. const char *message;
  12958. const char *cmpstring;
  12959. message = script_getstr(st,2);
  12960. cmpstring = script_getstr(st,3);
  12961. script_pushint(st,(stristr(message,cmpstring) != NULL));
  12962. return SCRIPT_CMD_SUCCESS;
  12963. }
  12964. // [zBuffer] List of mathematics commands --->
  12965. BUILDIN_FUNC(sqrt)
  12966. {
  12967. double i, a;
  12968. i = script_getnum(st,2);
  12969. a = sqrt(i);
  12970. script_pushint(st,(int)a);
  12971. return SCRIPT_CMD_SUCCESS;
  12972. }
  12973. BUILDIN_FUNC(pow)
  12974. {
  12975. double i, a, b;
  12976. a = script_getnum(st,2);
  12977. b = script_getnum(st,3);
  12978. i = pow(a,b);
  12979. script_pushint(st,(int)i);
  12980. return SCRIPT_CMD_SUCCESS;
  12981. }
  12982. BUILDIN_FUNC(distance)
  12983. {
  12984. int x0, y0, x1, y1;
  12985. x0 = script_getnum(st,2);
  12986. y0 = script_getnum(st,3);
  12987. x1 = script_getnum(st,4);
  12988. y1 = script_getnum(st,5);
  12989. script_pushint(st,distance_xy(x0,y0,x1,y1));
  12990. return SCRIPT_CMD_SUCCESS;
  12991. }
  12992. // <--- [zBuffer] List of mathematics commands
  12993. BUILDIN_FUNC(md5)
  12994. {
  12995. const char *tmpstr;
  12996. char *md5str;
  12997. tmpstr = script_getstr(st,2);
  12998. md5str = (char *)aMalloc((32+1)*sizeof(char));
  12999. MD5_String(tmpstr, md5str);
  13000. script_pushstr(st, md5str);
  13001. return SCRIPT_CMD_SUCCESS;
  13002. }
  13003. // [zBuffer] List of dynamic var commands --->
  13004. BUILDIN_FUNC(setd)
  13005. {
  13006. TBL_PC *sd=NULL;
  13007. char varname[100];
  13008. const char *buffer;
  13009. int elem;
  13010. buffer = script_getstr(st, 2);
  13011. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  13012. elem = 0;
  13013. if( not_server_variable(*varname) )
  13014. {
  13015. sd = script_rid2sd(st);
  13016. if( sd == NULL )
  13017. {
  13018. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  13019. return 0;
  13020. }
  13021. }
  13022. if( is_string_variable(varname) ) {
  13023. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  13024. } else {
  13025. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  13026. }
  13027. return SCRIPT_CMD_SUCCESS;
  13028. }
  13029. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  13030. {
  13031. int i, j;
  13032. TBL_PC* sd = NULL;
  13033. const char* query;
  13034. struct script_data* data;
  13035. const char* name;
  13036. int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  13037. int num_vars;
  13038. int num_cols;
  13039. // check target variables
  13040. for( i = 3; script_hasdata(st,i); ++i ) {
  13041. data = script_getdata(st, i);
  13042. if( data_isreference(data) ) { // it's a variable
  13043. name = reference_getname(data);
  13044. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  13045. sd = script_rid2sd(st);
  13046. if( sd == NULL ) { // no player attached
  13047. script_reportdata(data);
  13048. st->state = END;
  13049. return 1;
  13050. }
  13051. }
  13052. if( not_array_variable(*name) )
  13053. max_rows = 1;// not an array, limit to one row
  13054. } else {
  13055. ShowError("script:query_sql: not a variable\n");
  13056. script_reportdata(data);
  13057. st->state = END;
  13058. return 1;
  13059. }
  13060. }
  13061. num_vars = i - 3;
  13062. // Execute the query
  13063. query = script_getstr(st,2);
  13064. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  13065. Sql_ShowDebug(handle);
  13066. script_pushint(st, 0);
  13067. return 1;
  13068. }
  13069. if( Sql_NumRows(handle) == 0 ) { // No data received
  13070. Sql_FreeResult(handle);
  13071. script_pushint(st, 0);
  13072. return 0;
  13073. }
  13074. // Count the number of columns to store
  13075. num_cols = Sql_NumColumns(handle);
  13076. if( num_vars < num_cols ) {
  13077. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  13078. script_reportsrc(st);
  13079. } else if( num_vars > num_cols ) {
  13080. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  13081. script_reportsrc(st);
  13082. }
  13083. // Store data
  13084. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  13085. for( j = 0; j < num_vars; ++j ) {
  13086. char* str = NULL;
  13087. if( j < num_cols )
  13088. Sql_GetData(handle, j, &str, NULL);
  13089. data = script_getdata(st, j+3);
  13090. name = reference_getname(data);
  13091. if( is_string_variable(name) )
  13092. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  13093. else
  13094. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  13095. }
  13096. }
  13097. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  13098. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  13099. script_reportsrc(st);
  13100. }
  13101. // Free data
  13102. Sql_FreeResult(handle);
  13103. script_pushint(st, i);
  13104. return SCRIPT_CMD_SUCCESS;
  13105. }
  13106. BUILDIN_FUNC(query_sql) {
  13107. #ifdef BETA_THREAD_TEST
  13108. if( st->state != RERUNLINE ) {
  13109. queryThread_add(st,false);
  13110. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13111. } else
  13112. st->state = RUN;
  13113. return 0;
  13114. #else
  13115. return buildin_query_sql_sub(st, mmysql_handle);
  13116. #endif
  13117. }
  13118. BUILDIN_FUNC(query_logsql) {
  13119. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  13120. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  13121. script_pushint(st,-1);
  13122. return 1;
  13123. }
  13124. #ifdef BETA_THREAD_TEST
  13125. if( st->state != RERUNLINE ) {
  13126. queryThread_add(st,true);
  13127. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13128. } else
  13129. st->state = RUN;
  13130. return 0;
  13131. #else
  13132. return buildin_query_sql_sub(st, logmysql_handle);
  13133. #endif
  13134. }
  13135. //Allows escaping of a given string.
  13136. BUILDIN_FUNC(escape_sql)
  13137. {
  13138. const char *str;
  13139. char *esc_str;
  13140. size_t len;
  13141. str = script_getstr(st,2);
  13142. len = strlen(str);
  13143. esc_str = (char*)aMalloc(len*2+1);
  13144. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  13145. script_pushstr(st, esc_str);
  13146. return SCRIPT_CMD_SUCCESS;
  13147. }
  13148. BUILDIN_FUNC(getd)
  13149. {
  13150. char varname[100];
  13151. const char *buffer;
  13152. int elem;
  13153. buffer = script_getstr(st, 2);
  13154. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  13155. elem = 0;
  13156. // Push the 'pointer' so it's more flexible [Lance]
  13157. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  13158. return SCRIPT_CMD_SUCCESS;
  13159. }
  13160. BUILDIN_FUNC(callshop)
  13161. {
  13162. TBL_PC *sd = NULL;
  13163. struct npc_data *nd;
  13164. const char *shopname;
  13165. int flag = 0;
  13166. sd = script_rid2sd(st);
  13167. if (!sd) {
  13168. script_pushint(st,0);
  13169. return 0;
  13170. }
  13171. shopname = script_getstr(st, 2);
  13172. if( script_hasdata(st,3) )
  13173. flag = script_getnum(st,3);
  13174. nd = npc_name2id(shopname);
  13175. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP) )
  13176. {
  13177. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  13178. script_pushint(st,0);
  13179. return 1;
  13180. }
  13181. if( nd->subtype == SHOP || nd->subtype == ITEMSHOP || nd->subtype == POINTSHOP )
  13182. {
  13183. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  13184. sd->state.callshop = 1;
  13185. switch( flag )
  13186. {
  13187. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  13188. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  13189. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  13190. }
  13191. }
  13192. else
  13193. clif_cashshop_show(sd, nd);
  13194. sd->npc_shopid = nd->bl.id;
  13195. script_pushint(st,1);
  13196. return SCRIPT_CMD_SUCCESS;
  13197. }
  13198. BUILDIN_FUNC(npcshopitem)
  13199. {
  13200. const char* npcname = script_getstr(st, 2);
  13201. struct npc_data* nd = npc_name2id(npcname);
  13202. int n, i;
  13203. int amount;
  13204. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP ) )
  13205. { //Not found.
  13206. script_pushint(st,0);
  13207. return 0;
  13208. }
  13209. // get the count of new entries
  13210. amount = (script_lastdata(st)-2)/2;
  13211. // generate new shop item list
  13212. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  13213. for( n = 0, i = 3; n < amount; n++, i+=2 )
  13214. {
  13215. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13216. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13217. }
  13218. nd->u.shop.count = n;
  13219. script_pushint(st,1);
  13220. return SCRIPT_CMD_SUCCESS;
  13221. }
  13222. BUILDIN_FUNC(npcshopadditem)
  13223. {
  13224. const char* npcname = script_getstr(st,2);
  13225. struct npc_data* nd = npc_name2id(npcname);
  13226. int n, i;
  13227. int amount;
  13228. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP ) )
  13229. { //Not found.
  13230. script_pushint(st,0);
  13231. return 0;
  13232. }
  13233. // get the count of new entries
  13234. amount = (script_lastdata(st)-2)/2;
  13235. // append new items to existing shop item list
  13236. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  13237. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  13238. {
  13239. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13240. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13241. }
  13242. nd->u.shop.count = n;
  13243. script_pushint(st,1);
  13244. return SCRIPT_CMD_SUCCESS;
  13245. }
  13246. BUILDIN_FUNC(npcshopdelitem)
  13247. {
  13248. const char* npcname = script_getstr(st,2);
  13249. struct npc_data* nd = npc_name2id(npcname);
  13250. unsigned int nameid;
  13251. int n, i;
  13252. int amount;
  13253. int size;
  13254. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP ) )
  13255. { //Not found.
  13256. script_pushint(st,0);
  13257. return 0;
  13258. }
  13259. amount = script_lastdata(st)-2;
  13260. size = nd->u.shop.count;
  13261. // remove specified items from the shop item list
  13262. for( i = 3; i < 3 + amount; i++ )
  13263. {
  13264. nameid = script_getnum(st,i);
  13265. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  13266. if( n < size )
  13267. {
  13268. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  13269. size--;
  13270. }
  13271. }
  13272. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  13273. nd->u.shop.count = size;
  13274. script_pushint(st,1);
  13275. return SCRIPT_CMD_SUCCESS;
  13276. }
  13277. //Sets a script to attach to a shop npc.
  13278. BUILDIN_FUNC(npcshopattach)
  13279. {
  13280. const char* npcname = script_getstr(st,2);
  13281. struct npc_data* nd = npc_name2id(npcname);
  13282. int flag = 1;
  13283. if( script_hasdata(st,3) )
  13284. flag = script_getnum(st,3);
  13285. if( !nd || nd->subtype != SHOP )
  13286. { //Not found.
  13287. script_pushint(st,0);
  13288. return 0;
  13289. }
  13290. if (flag)
  13291. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  13292. else
  13293. nd->master_nd = NULL;
  13294. script_pushint(st,1);
  13295. return SCRIPT_CMD_SUCCESS;
  13296. }
  13297. /*==========================================
  13298. * Returns some values of an item [Lupus]
  13299. * Price, Weight, etc...
  13300. setitemscript(itemID,"{new item bonus script}",[n]);
  13301. Where n:
  13302. 0 - script
  13303. 1 - Equip script
  13304. 2 - Unequip script
  13305. *------------------------------------------*/
  13306. BUILDIN_FUNC(setitemscript)
  13307. {
  13308. int item_id,n=0;
  13309. const char *script;
  13310. struct item_data *i_data;
  13311. struct script_code **dstscript;
  13312. item_id = script_getnum(st,2);
  13313. script = script_getstr(st,3);
  13314. if( script_hasdata(st,4) )
  13315. n=script_getnum(st,4);
  13316. i_data = itemdb_exists(item_id);
  13317. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  13318. script_pushint(st,0);
  13319. return 0;
  13320. }
  13321. switch (n) {
  13322. case 2:
  13323. dstscript = &i_data->unequip_script;
  13324. break;
  13325. case 1:
  13326. dstscript = &i_data->equip_script;
  13327. break;
  13328. default:
  13329. dstscript = &i_data->script;
  13330. break;
  13331. }
  13332. if(*dstscript)
  13333. script_free_code(*dstscript);
  13334. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  13335. script_pushint(st,1);
  13336. return SCRIPT_CMD_SUCCESS;
  13337. }
  13338. /*=======================================================
  13339. * Add or Update a mob drop temporarily [Akinari]
  13340. * Original Idea By: [Lupus]
  13341. *
  13342. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  13343. *
  13344. * If given an item the mob already drops, the rate
  13345. * is updated to the new rate. Rate cannot exceed 10000
  13346. * Returns 1 if succeeded (added/updated a mob drop)
  13347. *-------------------------------------------------------*/
  13348. BUILDIN_FUNC(addmonsterdrop)
  13349. {
  13350. struct mob_db *mob;
  13351. struct script_data *data;
  13352. int item_id,rate,i,c = 0;
  13353. data = script_getdata(st, 2);
  13354. get_val(st, data); // Convert into value in case of a variable
  13355. if(data_isstring(data))
  13356. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13357. else
  13358. mob = mob_db(script_getnum(st,2));
  13359. item_id=script_getnum(st,3);
  13360. rate=script_getnum(st,4);
  13361. if(!itemdb_exists(item_id)){
  13362. ShowError("addmonsterdrop: Nonexistant item %d requested.\n", item_id );
  13363. return 1;
  13364. }
  13365. if(mob) { //We got a valid monster, check for available drop slot
  13366. for(i = 0; i < MAX_MOB_DROP; i++) {
  13367. if(mob->dropitem[i].nameid) {
  13368. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  13369. c = i;
  13370. break;
  13371. }
  13372. continue;
  13373. }
  13374. if(!c) //Accept first available slot only
  13375. c = i;
  13376. }
  13377. if(c) { //Fill in the slot with the item and rate
  13378. mob->dropitem[c].nameid = item_id;
  13379. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  13380. script_pushint(st,1);
  13381. } else //No place to put the new drop
  13382. script_pushint(st,0);
  13383. } else {
  13384. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13385. return 1;
  13386. }
  13387. return SCRIPT_CMD_SUCCESS;
  13388. }
  13389. /*=======================================================
  13390. * Delete a mob drop temporarily [Akinari]
  13391. * Original Idea By: [Lupus]
  13392. *
  13393. * delmonsterdrop <mob_id or name>,<item_id>;
  13394. *
  13395. * Returns 1 if succeeded (deleted a mob drop)
  13396. *-------------------------------------------------------*/
  13397. BUILDIN_FUNC(delmonsterdrop)
  13398. {
  13399. struct mob_db *mob;
  13400. struct script_data *data;
  13401. int item_id,i;
  13402. data = script_getdata(st, 2);
  13403. get_val(st, data); // Convert into value in case of a variable
  13404. if(data_isstring(data))
  13405. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13406. else
  13407. mob = mob_db(script_getnum(st,2));
  13408. item_id=script_getnum(st,3);
  13409. if(!itemdb_exists(item_id)){
  13410. ShowError("delmonsterdrop: Nonexistant item %d requested.\n", item_id );
  13411. return 1;
  13412. }
  13413. if(mob) { //We got a valid monster, check for item drop on monster
  13414. for(i = 0; i < MAX_MOB_DROP; i++) {
  13415. if(mob->dropitem[i].nameid == item_id) {
  13416. mob->dropitem[i].nameid = 0;
  13417. mob->dropitem[i].p = 0;
  13418. script_pushint(st,1);
  13419. return 0;
  13420. }
  13421. }
  13422. //No drop on that monster
  13423. script_pushint(st,0);
  13424. } else {
  13425. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13426. return 1;
  13427. }
  13428. return SCRIPT_CMD_SUCCESS;
  13429. }
  13430. /*==========================================
  13431. * Returns some values of a monster [Lupus]
  13432. * Name, Level, race, size, etc...
  13433. getmonsterinfo(monsterID,queryIndex);
  13434. *------------------------------------------*/
  13435. BUILDIN_FUNC(getmonsterinfo)
  13436. {
  13437. struct mob_db *mob;
  13438. int mob_id;
  13439. mob_id = script_getnum(st,2);
  13440. if (!mobdb_checkid(mob_id)) {
  13441. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  13442. if ( !script_getnum(st,3) ) //requested a string
  13443. script_pushconststr(st,"null");
  13444. else
  13445. script_pushint(st,-1);
  13446. return -1;
  13447. }
  13448. mob = mob_db(mob_id);
  13449. switch ( script_getnum(st,3) ) {
  13450. case 0: script_pushstrcopy(st,mob->jname); break;
  13451. case 1: script_pushint(st,mob->lv); break;
  13452. case 2: script_pushint(st,mob->status.max_hp); break;
  13453. case 3: script_pushint(st,mob->base_exp); break;
  13454. case 4: script_pushint(st,mob->job_exp); break;
  13455. case 5: script_pushint(st,mob->status.rhw.atk); break;
  13456. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  13457. case 7: script_pushint(st,mob->status.def); break;
  13458. case 8: script_pushint(st,mob->status.mdef); break;
  13459. case 9: script_pushint(st,mob->status.str); break;
  13460. case 10: script_pushint(st,mob->status.agi); break;
  13461. case 11: script_pushint(st,mob->status.vit); break;
  13462. case 12: script_pushint(st,mob->status.int_); break;
  13463. case 13: script_pushint(st,mob->status.dex); break;
  13464. case 14: script_pushint(st,mob->status.luk); break;
  13465. case 15: script_pushint(st,mob->status.rhw.range); break;
  13466. case 16: script_pushint(st,mob->range2); break;
  13467. case 17: script_pushint(st,mob->range3); break;
  13468. case 18: script_pushint(st,mob->status.size); break;
  13469. case 19: script_pushint(st,mob->status.race); break;
  13470. case 20: script_pushint(st,mob->status.def_ele); break;
  13471. case 21: script_pushint(st,mob->status.mode); break;
  13472. case 22: script_pushint(st,mob->mexp); break;
  13473. default: script_pushint(st,-1); //wrong Index
  13474. }
  13475. return SCRIPT_CMD_SUCCESS;
  13476. }
  13477. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  13478. {
  13479. TBL_PC *sd = NULL;
  13480. if(script_hasdata(st,2))
  13481. sd = map_nick2sd(script_getstr(st,2));
  13482. else
  13483. sd = script_rid2sd(st);
  13484. if(sd)
  13485. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  13486. else
  13487. script_pushint(st,0);
  13488. return SCRIPT_CMD_SUCCESS;
  13489. }
  13490. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  13491. {
  13492. TBL_PC *sd = NULL;
  13493. if(script_hasdata(st,2))
  13494. sd = map_nick2sd(script_getstr(st,2));
  13495. else
  13496. sd = script_rid2sd(st);
  13497. if(sd)
  13498. script_pushint(st,(sd->chatID != 0));
  13499. else
  13500. script_pushint(st,0);
  13501. return SCRIPT_CMD_SUCCESS;
  13502. }
  13503. BUILDIN_FUNC(checkidle)
  13504. {
  13505. TBL_PC *sd = NULL;
  13506. if (script_hasdata(st, 2))
  13507. sd = map_nick2sd(script_getstr(st, 2));
  13508. else
  13509. sd = script_rid2sd(st);
  13510. if (sd)
  13511. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  13512. else
  13513. script_pushint(st, 0);
  13514. return SCRIPT_CMD_SUCCESS;
  13515. }
  13516. BUILDIN_FUNC(searchitem)
  13517. {
  13518. struct script_data* data = script_getdata(st, 2);
  13519. const char *itemname = script_getstr(st,3);
  13520. struct item_data *items[MAX_SEARCH];
  13521. int count;
  13522. char* name;
  13523. int32 start;
  13524. int32 id;
  13525. int32 i;
  13526. TBL_PC* sd = NULL;
  13527. if ((items[0] = itemdb_exists(atoi(itemname))))
  13528. count = 1;
  13529. else {
  13530. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  13531. if (count > MAX_SEARCH) count = MAX_SEARCH;
  13532. }
  13533. if (!count) {
  13534. script_pushint(st, 0);
  13535. return 0;
  13536. }
  13537. if( !data_isreference(data) )
  13538. {
  13539. ShowError("script:searchitem: not a variable\n");
  13540. script_reportdata(data);
  13541. st->state = END;
  13542. return 1;// not a variable
  13543. }
  13544. id = reference_getid(data);
  13545. start = reference_getindex(data);
  13546. name = reference_getname(data);
  13547. if( not_array_variable(*name) )
  13548. {
  13549. ShowError("script:searchitem: illegal scope\n");
  13550. script_reportdata(data);
  13551. st->state = END;
  13552. return 1;// not supported
  13553. }
  13554. if( not_server_variable(*name) )
  13555. {
  13556. sd = script_rid2sd(st);
  13557. if( sd == NULL )
  13558. return 0;// no player attached
  13559. }
  13560. if( is_string_variable(name) )
  13561. {// string array
  13562. ShowError("script:searchitem: not an integer array reference\n");
  13563. script_reportdata(data);
  13564. st->state = END;
  13565. return 1;// not supported
  13566. }
  13567. for( i = 0; i < count; ++start, ++i )
  13568. {// Set array
  13569. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  13570. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  13571. }
  13572. script_pushint(st, count);
  13573. return SCRIPT_CMD_SUCCESS;
  13574. }
  13575. int axtoi(const char *hexStg)
  13576. {
  13577. int n = 0; // position in string
  13578. int16 m = 0; // position in digit[] to shift
  13579. int count; // loop index
  13580. int intValue = 0; // integer value of hex string
  13581. int digit[11]; // hold values to convert
  13582. while (n < 10) {
  13583. if (hexStg[n]=='\0')
  13584. break;
  13585. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  13586. digit[n] = hexStg[n] & 0x0f; //convert to int
  13587. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  13588. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  13589. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  13590. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  13591. else break;
  13592. n++;
  13593. }
  13594. count = n;
  13595. m = n - 1;
  13596. n = 0;
  13597. while(n < count) {
  13598. // digit[n] is value of hex digit at position n
  13599. // (m << 2) is the number of positions to shift
  13600. // OR the bits into return value
  13601. intValue = intValue | (digit[n] << (m << 2));
  13602. m--; // adjust the position to set
  13603. n++; // next digit to process
  13604. }
  13605. return (intValue);
  13606. }
  13607. // [Lance] Hex string to integer converter
  13608. BUILDIN_FUNC(axtoi)
  13609. {
  13610. const char *hex = script_getstr(st,2);
  13611. script_pushint(st,axtoi(hex));
  13612. return SCRIPT_CMD_SUCCESS;
  13613. }
  13614. // [zBuffer] List of player cont commands --->
  13615. BUILDIN_FUNC(rid2name)
  13616. {
  13617. struct block_list *bl = NULL;
  13618. int rid = script_getnum(st,2);
  13619. if((bl = map_id2bl(rid)))
  13620. {
  13621. switch(bl->type) {
  13622. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  13623. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  13624. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  13625. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  13626. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  13627. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  13628. default:
  13629. ShowError("buildin_rid2name: BL type unknown.\n");
  13630. script_pushconststr(st,"");
  13631. break;
  13632. }
  13633. } else {
  13634. ShowError("buildin_rid2name: invalid RID\n");
  13635. script_pushconststr(st,"(null)");
  13636. }
  13637. return SCRIPT_CMD_SUCCESS;
  13638. }
  13639. BUILDIN_FUNC(pcblockmove)
  13640. {
  13641. int id, flag;
  13642. TBL_PC *sd = NULL;
  13643. id = script_getnum(st,2);
  13644. flag = script_getnum(st,3);
  13645. if(id)
  13646. sd = map_id2sd(id);
  13647. else
  13648. sd = script_rid2sd(st);
  13649. if(sd)
  13650. sd->state.blockedmove = flag > 0;
  13651. return SCRIPT_CMD_SUCCESS;
  13652. }
  13653. BUILDIN_FUNC(pcfollow)
  13654. {
  13655. int id, targetid;
  13656. TBL_PC *sd = NULL;
  13657. id = script_getnum(st,2);
  13658. targetid = script_getnum(st,3);
  13659. if(id)
  13660. sd = map_id2sd(id);
  13661. else
  13662. sd = script_rid2sd(st);
  13663. if(sd)
  13664. pc_follow(sd, targetid);
  13665. return SCRIPT_CMD_SUCCESS;
  13666. }
  13667. BUILDIN_FUNC(pcstopfollow)
  13668. {
  13669. int id;
  13670. TBL_PC *sd = NULL;
  13671. id = script_getnum(st,2);
  13672. if(id)
  13673. sd = map_id2sd(id);
  13674. else
  13675. sd = script_rid2sd(st);
  13676. if(sd)
  13677. pc_stop_following(sd);
  13678. return SCRIPT_CMD_SUCCESS;
  13679. }
  13680. // <--- [zBuffer] List of player cont commands
  13681. // [zBuffer] List of mob control commands --->
  13682. /// Makes the unit walk to target position or map
  13683. /// Returns if it was successfull
  13684. ///
  13685. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  13686. /// unitwalk(<unit_id>,<target_id>) -> <bool>
  13687. BUILDIN_FUNC(unitwalk)
  13688. {
  13689. struct block_list* bl;
  13690. bl = map_id2bl(script_getnum(st,2));
  13691. if( bl == NULL )
  13692. script_pushint(st, 0);
  13693. else if( script_hasdata(st,4) ) {
  13694. int x = script_getnum(st,3);
  13695. int y = script_getnum(st,4);
  13696. if( script_pushint(st, unit_can_reach_pos(bl,x,y,0)) )
  13697. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  13698. } else {
  13699. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  13700. if( tbl == NULL ) {
  13701. ShowError("script:unitwalk: bad target destination\n");
  13702. script_pushint(st, 0);
  13703. return 1;
  13704. }
  13705. else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  13706. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  13707. }
  13708. return SCRIPT_CMD_SUCCESS;
  13709. }
  13710. /// Kills the unit
  13711. ///
  13712. /// unitkill <unit_id>;
  13713. BUILDIN_FUNC(unitkill)
  13714. {
  13715. struct block_list* bl = map_id2bl(script_getnum(st,2));
  13716. if( bl != NULL )
  13717. status_kill(bl);
  13718. return SCRIPT_CMD_SUCCESS;
  13719. }
  13720. /// Warps the unit to the target position in the target map
  13721. /// Returns if it was successfull
  13722. ///
  13723. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  13724. BUILDIN_FUNC(unitwarp)
  13725. {
  13726. int unit_id;
  13727. int map;
  13728. short x;
  13729. short y;
  13730. struct block_list* bl;
  13731. const char *mapname;
  13732. unit_id = script_getnum(st,2);
  13733. mapname = script_getstr(st, 3);
  13734. x = (short)script_getnum(st,4);
  13735. y = (short)script_getnum(st,5);
  13736. if (!unit_id) //Warp the script's runner
  13737. bl = map_id2bl(st->rid);
  13738. else
  13739. bl = map_id2bl(unit_id);
  13740. if( strcmp(mapname,"this") == 0 )
  13741. map = bl?bl->m:-1;
  13742. else
  13743. map = map_mapname2mapid(mapname);
  13744. if( map >= 0 && bl != NULL )
  13745. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  13746. else
  13747. script_pushint(st, 0);
  13748. return SCRIPT_CMD_SUCCESS;
  13749. }
  13750. /// Makes the unit attack the target.
  13751. /// If the unit is a player and <action type> is not 0, it does a continuous
  13752. /// attack instead of a single attack.
  13753. /// Returns if the request was successful.
  13754. ///
  13755. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  13756. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  13757. BUILDIN_FUNC(unitattack)
  13758. {
  13759. struct block_list* unit_bl;
  13760. struct block_list* target_bl = NULL;
  13761. struct script_data* data;
  13762. int actiontype = 0;
  13763. // get unit
  13764. unit_bl = map_id2bl(script_getnum(st,2));
  13765. if( unit_bl == NULL ) {
  13766. script_pushint(st, 0);
  13767. return 0;
  13768. }
  13769. data = script_getdata(st, 3);
  13770. get_val(st, data);
  13771. if( data_isstring(data) )
  13772. {
  13773. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  13774. if( sd != NULL )
  13775. target_bl = &sd->bl;
  13776. } else
  13777. target_bl = map_id2bl(conv_num(st, data));
  13778. // request the attack
  13779. if( target_bl == NULL )
  13780. {
  13781. script_pushint(st, 0);
  13782. return 0;
  13783. }
  13784. // get actiontype
  13785. if( script_hasdata(st,4) )
  13786. actiontype = script_getnum(st,4);
  13787. switch( unit_bl->type )
  13788. {
  13789. case BL_PC:
  13790. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  13791. script_pushint(st, 1);
  13792. return 0;
  13793. case BL_MOB:
  13794. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  13795. break;
  13796. case BL_PET:
  13797. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  13798. break;
  13799. default:
  13800. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  13801. script_pushint(st, 0);
  13802. return 1;
  13803. }
  13804. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  13805. return SCRIPT_CMD_SUCCESS;
  13806. }
  13807. /// Makes the unit stop attacking and moving
  13808. ///
  13809. /// unitstop <unit_id>;
  13810. BUILDIN_FUNC(unitstop)
  13811. {
  13812. int unit_id;
  13813. struct block_list* bl;
  13814. unit_id = script_getnum(st,2);
  13815. bl = map_id2bl(unit_id);
  13816. if( bl != NULL )
  13817. {
  13818. unit_stop_attack(bl);
  13819. unit_stop_walking(bl,4);
  13820. if( bl->type == BL_MOB )
  13821. ((TBL_MOB*)bl)->target_id = 0;
  13822. }
  13823. return SCRIPT_CMD_SUCCESS;
  13824. }
  13825. /// Makes the unit say the message
  13826. ///
  13827. /// unittalk <unit_id>,"<message>";
  13828. BUILDIN_FUNC(unittalk)
  13829. {
  13830. int unit_id;
  13831. const char* message;
  13832. struct block_list* bl;
  13833. unit_id = script_getnum(st,2);
  13834. message = script_getstr(st, 3);
  13835. bl = map_id2bl(unit_id);
  13836. if( bl != NULL )
  13837. {
  13838. struct StringBuf sbuf;
  13839. StringBuf_Init(&sbuf);
  13840. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  13841. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  13842. StringBuf_Destroy(&sbuf);
  13843. }
  13844. return SCRIPT_CMD_SUCCESS;
  13845. }
  13846. /// Makes the unit do an emotion
  13847. ///
  13848. /// unitemote <unit_id>,<emotion>;
  13849. ///
  13850. /// @see e_* in const.txt
  13851. BUILDIN_FUNC(unitemote)
  13852. {
  13853. int unit_id;
  13854. int emotion;
  13855. struct block_list* bl;
  13856. unit_id = script_getnum(st,2);
  13857. emotion = script_getnum(st,3);
  13858. bl = map_id2bl(unit_id);
  13859. if( bl != NULL )
  13860. clif_emotion(bl, emotion);
  13861. return SCRIPT_CMD_SUCCESS;
  13862. }
  13863. /// Makes the unit cast the skill on the target or self if no target is specified
  13864. ///
  13865. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  13866. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  13867. BUILDIN_FUNC(unitskilluseid)
  13868. {
  13869. int unit_id;
  13870. uint16 skill_id;
  13871. uint16 skill_lv;
  13872. int target_id;
  13873. struct block_list* bl;
  13874. struct script_data *data;
  13875. unit_id = script_getnum(st,2);
  13876. data = script_getdata(st, 3);
  13877. get_val(st, data); // Convert into value in case of a variable
  13878. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13879. skill_lv = script_getnum(st,4);
  13880. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  13881. bl = map_id2bl(unit_id);
  13882. if( bl != NULL )
  13883. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  13884. return SCRIPT_CMD_SUCCESS;
  13885. }
  13886. /// Makes the unit cast the skill on the target position.
  13887. ///
  13888. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  13889. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  13890. BUILDIN_FUNC(unitskillusepos)
  13891. {
  13892. int unit_id;
  13893. uint16 skill_id;
  13894. uint16 skill_lv;
  13895. int skill_x;
  13896. int skill_y;
  13897. struct block_list* bl;
  13898. struct script_data *data;
  13899. unit_id = script_getnum(st,2);
  13900. data = script_getdata(st, 3);
  13901. get_val(st, data); // Convert into value in case of a variable
  13902. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13903. skill_lv = script_getnum(st,4);
  13904. skill_x = script_getnum(st,5);
  13905. skill_y = script_getnum(st,6);
  13906. bl = map_id2bl(unit_id);
  13907. if( bl != NULL )
  13908. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  13909. return SCRIPT_CMD_SUCCESS;
  13910. }
  13911. // <--- [zBuffer] List of mob control commands
  13912. /// Pauses the execution of the script, detaching the player
  13913. ///
  13914. /// sleep <mili seconds>;
  13915. BUILDIN_FUNC(sleep)
  13916. {
  13917. int ticks;
  13918. ticks = script_getnum(st,2);
  13919. // detach the player
  13920. script_detach_rid(st);
  13921. if( ticks <= 0 )
  13922. {// do nothing
  13923. }
  13924. else if( st->sleep.tick == 0 )
  13925. {// sleep for the target amount of time
  13926. st->state = RERUNLINE;
  13927. st->sleep.tick = ticks;
  13928. }
  13929. else
  13930. {// sleep time is over
  13931. st->state = RUN;
  13932. st->sleep.tick = 0;
  13933. }
  13934. return SCRIPT_CMD_SUCCESS;
  13935. }
  13936. /// Pauses the execution of the script, keeping the player attached
  13937. /// Returns if a player is still attached
  13938. ///
  13939. /// sleep2(<mili secconds>) -> <bool>
  13940. BUILDIN_FUNC(sleep2)
  13941. {
  13942. int ticks;
  13943. ticks = script_getnum(st,2);
  13944. if( ticks <= 0 )
  13945. {// do nothing
  13946. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13947. }
  13948. else if( !st->sleep.tick )
  13949. {// sleep for the target amount of time
  13950. st->state = RERUNLINE;
  13951. st->sleep.tick = ticks;
  13952. }
  13953. else
  13954. {// sleep time is over
  13955. st->state = RUN;
  13956. st->sleep.tick = 0;
  13957. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13958. }
  13959. return SCRIPT_CMD_SUCCESS;
  13960. }
  13961. /// Awakes all the sleep timers of the target npc
  13962. ///
  13963. /// awake "<npc name>";
  13964. BUILDIN_FUNC(awake)
  13965. {
  13966. struct npc_data* nd;
  13967. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  13968. nd = npc_name2id(script_getstr(st, 2));
  13969. if( nd == NULL ) {
  13970. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  13971. return 1;
  13972. }
  13973. while( node )
  13974. {
  13975. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  13976. {// sleep timer for the npc
  13977. struct script_state* tst = (struct script_state*)node->data;
  13978. TBL_PC* sd = map_id2sd(tst->rid);
  13979. if( tst->sleep.timer == INVALID_TIMER )
  13980. {// already awake ???
  13981. node = node->next;
  13982. continue;
  13983. }
  13984. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  13985. {// char not online anymore / another char of the same account is online - Cancel execution
  13986. tst->state = END;
  13987. tst->rid = 0;
  13988. }
  13989. delete_timer(tst->sleep.timer, run_script_timer);
  13990. node = script_erase_sleepdb(node);
  13991. tst->sleep.timer = INVALID_TIMER;
  13992. if(tst->state != RERUNLINE)
  13993. tst->sleep.tick = 0;
  13994. run_script_main(tst);
  13995. }
  13996. else
  13997. {
  13998. node = node->next;
  13999. }
  14000. }
  14001. return SCRIPT_CMD_SUCCESS;
  14002. }
  14003. /// Returns a reference to a variable of the target NPC.
  14004. /// Returns 0 if an error occurs.
  14005. ///
  14006. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  14007. BUILDIN_FUNC(getvariableofnpc)
  14008. {
  14009. struct script_data* data;
  14010. const char* name;
  14011. struct npc_data* nd;
  14012. data = script_getdata(st,2);
  14013. if( !data_isreference(data) )
  14014. {// Not a reference (aka varaible name)
  14015. ShowError("script:getvariableofnpc: not a variable\n");
  14016. script_reportdata(data);
  14017. script_pushnil(st);
  14018. st->state = END;
  14019. return 1;
  14020. }
  14021. name = reference_getname(data);
  14022. if( *name != '.' || name[1] == '@' )
  14023. {// not a npc variable
  14024. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  14025. script_reportdata(data);
  14026. script_pushnil(st);
  14027. st->state = END;
  14028. return 1;
  14029. }
  14030. nd = npc_name2id(script_getstr(st,3));
  14031. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  14032. {// NPC not found or has no script
  14033. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  14034. script_pushnil(st);
  14035. st->state = END;
  14036. return 1;
  14037. }
  14038. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  14039. return SCRIPT_CMD_SUCCESS;
  14040. }
  14041. /// Opens a warp portal.
  14042. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  14043. ///
  14044. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  14045. ///
  14046. /// @author blackhole89
  14047. BUILDIN_FUNC(warpportal)
  14048. {
  14049. int spx;
  14050. int spy;
  14051. unsigned short mapindex;
  14052. int tpx;
  14053. int tpy;
  14054. struct skill_unit_group* group;
  14055. struct block_list* bl;
  14056. bl = map_id2bl(st->oid);
  14057. if( bl == NULL )
  14058. {
  14059. ShowError("script:warpportal: npc is needed\n");
  14060. return 1;
  14061. }
  14062. spx = script_getnum(st,2);
  14063. spy = script_getnum(st,3);
  14064. mapindex = mapindex_name2id(script_getstr(st, 4));
  14065. tpx = script_getnum(st,5);
  14066. tpy = script_getnum(st,6);
  14067. if( mapindex == 0 )
  14068. return 0;// map not found
  14069. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  14070. if( group == NULL )
  14071. return 0;// failed
  14072. group->val1 = (group->val1<<16)|(short)0;
  14073. group->val2 = (tpx<<16) | tpy;
  14074. group->val3 = mapindex;
  14075. return SCRIPT_CMD_SUCCESS;
  14076. }
  14077. BUILDIN_FUNC(openmail)
  14078. {
  14079. TBL_PC* sd;
  14080. sd = script_rid2sd(st);
  14081. if( sd == NULL )
  14082. return 0;
  14083. mail_openmail(sd);
  14084. return SCRIPT_CMD_SUCCESS;
  14085. }
  14086. BUILDIN_FUNC(openauction)
  14087. {
  14088. TBL_PC* sd;
  14089. sd = script_rid2sd(st);
  14090. if( sd == NULL )
  14091. return 0;
  14092. clif_Auction_openwindow(sd);
  14093. return SCRIPT_CMD_SUCCESS;
  14094. }
  14095. /// Retrieves the value of the specified flag of the specified cell.
  14096. ///
  14097. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  14098. ///
  14099. /// @see cell_chk* constants in const.txt for the types
  14100. BUILDIN_FUNC(checkcell)
  14101. {
  14102. int16 m = map_mapname2mapid(script_getstr(st,2));
  14103. int16 x = script_getnum(st,3);
  14104. int16 y = script_getnum(st,4);
  14105. cell_chk type = (cell_chk)script_getnum(st,5);
  14106. script_pushint(st, map_getcell(m, x, y, type));
  14107. return SCRIPT_CMD_SUCCESS;
  14108. }
  14109. /// Modifies flags of cells in the specified area.
  14110. ///
  14111. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  14112. ///
  14113. /// @see cell_* constants in const.txt for the types
  14114. BUILDIN_FUNC(setcell)
  14115. {
  14116. int16 m = map_mapname2mapid(script_getstr(st,2));
  14117. int16 x1 = script_getnum(st,3);
  14118. int16 y1 = script_getnum(st,4);
  14119. int16 x2 = script_getnum(st,5);
  14120. int16 y2 = script_getnum(st,6);
  14121. cell_t type = (cell_t)script_getnum(st,7);
  14122. bool flag = (bool)script_getnum(st,8);
  14123. int x,y;
  14124. if( x1 > x2 ) swap(x1,x2);
  14125. if( y1 > y2 ) swap(y1,y2);
  14126. for( y = y1; y <= y2; ++y )
  14127. for( x = x1; x <= x2; ++x )
  14128. map_setcell(m, x, y, type, flag);
  14129. return SCRIPT_CMD_SUCCESS;
  14130. }
  14131. /*==========================================
  14132. * Mercenary Commands
  14133. *------------------------------------------*/
  14134. BUILDIN_FUNC(mercenary_create)
  14135. {
  14136. struct map_session_data *sd;
  14137. int class_, contract_time;
  14138. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  14139. return 0;
  14140. class_ = script_getnum(st,2);
  14141. if( !merc_class(class_) )
  14142. return 0;
  14143. contract_time = script_getnum(st,3);
  14144. merc_create(sd, class_, contract_time);
  14145. return SCRIPT_CMD_SUCCESS;
  14146. }
  14147. BUILDIN_FUNC(mercenary_heal)
  14148. {
  14149. struct map_session_data *sd = script_rid2sd(st);
  14150. int hp, sp;
  14151. if( sd == NULL || sd->md == NULL )
  14152. return 0;
  14153. hp = script_getnum(st,2);
  14154. sp = script_getnum(st,3);
  14155. status_heal(&sd->md->bl, hp, sp, 0);
  14156. return SCRIPT_CMD_SUCCESS;
  14157. }
  14158. BUILDIN_FUNC(mercenary_sc_start)
  14159. {
  14160. struct map_session_data *sd = script_rid2sd(st);
  14161. enum sc_type type;
  14162. int tick, val1;
  14163. if( sd == NULL || sd->md == NULL )
  14164. return 0;
  14165. type = (sc_type)script_getnum(st,2);
  14166. tick = script_getnum(st,3);
  14167. val1 = script_getnum(st,4);
  14168. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  14169. return SCRIPT_CMD_SUCCESS;
  14170. }
  14171. BUILDIN_FUNC(mercenary_get_calls)
  14172. {
  14173. struct map_session_data *sd = script_rid2sd(st);
  14174. int guild;
  14175. if( sd == NULL )
  14176. return 0;
  14177. guild = script_getnum(st,2);
  14178. switch( guild )
  14179. {
  14180. case ARCH_MERC_GUILD:
  14181. script_pushint(st,sd->status.arch_calls);
  14182. break;
  14183. case SPEAR_MERC_GUILD:
  14184. script_pushint(st,sd->status.spear_calls);
  14185. break;
  14186. case SWORD_MERC_GUILD:
  14187. script_pushint(st,sd->status.sword_calls);
  14188. break;
  14189. default:
  14190. script_pushint(st,0);
  14191. break;
  14192. }
  14193. return SCRIPT_CMD_SUCCESS;
  14194. }
  14195. BUILDIN_FUNC(mercenary_set_calls)
  14196. {
  14197. struct map_session_data *sd = script_rid2sd(st);
  14198. int guild, value, *calls;
  14199. if( sd == NULL )
  14200. return 0;
  14201. guild = script_getnum(st,2);
  14202. value = script_getnum(st,3);
  14203. switch( guild )
  14204. {
  14205. case ARCH_MERC_GUILD:
  14206. calls = &sd->status.arch_calls;
  14207. break;
  14208. case SPEAR_MERC_GUILD:
  14209. calls = &sd->status.spear_calls;
  14210. break;
  14211. case SWORD_MERC_GUILD:
  14212. calls = &sd->status.sword_calls;
  14213. break;
  14214. default:
  14215. return 0; // Invalid Guild
  14216. }
  14217. *calls += value;
  14218. *calls = cap_value(*calls, 0, INT_MAX);
  14219. return SCRIPT_CMD_SUCCESS;
  14220. }
  14221. BUILDIN_FUNC(mercenary_get_faith)
  14222. {
  14223. struct map_session_data *sd = script_rid2sd(st);
  14224. int guild;
  14225. if( sd == NULL )
  14226. return 0;
  14227. guild = script_getnum(st,2);
  14228. switch( guild )
  14229. {
  14230. case ARCH_MERC_GUILD:
  14231. script_pushint(st,sd->status.arch_faith);
  14232. break;
  14233. case SPEAR_MERC_GUILD:
  14234. script_pushint(st,sd->status.spear_faith);
  14235. break;
  14236. case SWORD_MERC_GUILD:
  14237. script_pushint(st,sd->status.sword_faith);
  14238. break;
  14239. default:
  14240. script_pushint(st,0);
  14241. break;
  14242. }
  14243. return SCRIPT_CMD_SUCCESS;
  14244. }
  14245. BUILDIN_FUNC(mercenary_set_faith)
  14246. {
  14247. struct map_session_data *sd = script_rid2sd(st);
  14248. int guild, value, *calls;
  14249. if( sd == NULL )
  14250. return 0;
  14251. guild = script_getnum(st,2);
  14252. value = script_getnum(st,3);
  14253. switch( guild )
  14254. {
  14255. case ARCH_MERC_GUILD:
  14256. calls = &sd->status.arch_faith;
  14257. break;
  14258. case SPEAR_MERC_GUILD:
  14259. calls = &sd->status.spear_faith;
  14260. break;
  14261. case SWORD_MERC_GUILD:
  14262. calls = &sd->status.sword_faith;
  14263. break;
  14264. default:
  14265. return 0; // Invalid Guild
  14266. }
  14267. *calls += value;
  14268. *calls = cap_value(*calls, 0, INT_MAX);
  14269. if( mercenary_get_guild(sd->md) == guild )
  14270. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  14271. return SCRIPT_CMD_SUCCESS;
  14272. }
  14273. /*------------------------------------------
  14274. * Book Reading
  14275. *------------------------------------------*/
  14276. BUILDIN_FUNC(readbook)
  14277. {
  14278. struct map_session_data *sd;
  14279. int book_id, page;
  14280. if( (sd = script_rid2sd(st)) == NULL )
  14281. return 0;
  14282. book_id = script_getnum(st,2);
  14283. page = script_getnum(st,3);
  14284. clif_readbook(sd->fd, book_id, page);
  14285. return SCRIPT_CMD_SUCCESS;
  14286. }
  14287. /******************
  14288. Questlog script commands
  14289. *******************/
  14290. BUILDIN_FUNC(setquest)
  14291. {
  14292. struct map_session_data *sd = script_rid2sd(st);
  14293. nullpo_ret(sd);
  14294. quest_add(sd, script_getnum(st, 2));
  14295. return SCRIPT_CMD_SUCCESS;
  14296. }
  14297. BUILDIN_FUNC(erasequest)
  14298. {
  14299. struct map_session_data *sd = script_rid2sd(st);
  14300. nullpo_ret(sd);
  14301. quest_delete(sd, script_getnum(st, 2));
  14302. return SCRIPT_CMD_SUCCESS;
  14303. }
  14304. BUILDIN_FUNC(completequest)
  14305. {
  14306. struct map_session_data *sd = script_rid2sd(st);
  14307. nullpo_ret(sd);
  14308. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  14309. return SCRIPT_CMD_SUCCESS;
  14310. }
  14311. BUILDIN_FUNC(changequest)
  14312. {
  14313. struct map_session_data *sd = script_rid2sd(st);
  14314. nullpo_ret(sd);
  14315. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  14316. return SCRIPT_CMD_SUCCESS;
  14317. }
  14318. BUILDIN_FUNC(checkquest)
  14319. {
  14320. struct map_session_data *sd = script_rid2sd(st);
  14321. quest_check_type type = HAVEQUEST;
  14322. nullpo_ret(sd);
  14323. if( script_hasdata(st, 3) )
  14324. type = (quest_check_type)script_getnum(st, 3);
  14325. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  14326. return SCRIPT_CMD_SUCCESS;
  14327. }
  14328. BUILDIN_FUNC(showevent)
  14329. {
  14330. TBL_PC *sd = script_rid2sd(st);
  14331. struct npc_data *nd = map_id2nd(st->oid);
  14332. int state, color;
  14333. if( sd == NULL || nd == NULL )
  14334. return 0;
  14335. state = script_getnum(st, 2);
  14336. color = script_getnum(st, 3);
  14337. if( color < 0 || color > 3 )
  14338. color = 0; // set default color
  14339. clif_quest_show_event(sd, &nd->bl, state, color);
  14340. return SCRIPT_CMD_SUCCESS;
  14341. }
  14342. /*==========================================
  14343. * BattleGround System
  14344. *------------------------------------------*/
  14345. BUILDIN_FUNC(waitingroom2bg)
  14346. {
  14347. struct npc_data *nd;
  14348. struct chat_data *cd;
  14349. const char *map_name, *ev = "", *dev = "";
  14350. int x, y, i, mapindex = 0, bg_id, n;
  14351. struct map_session_data *sd;
  14352. if( script_hasdata(st,7) )
  14353. nd = npc_name2id(script_getstr(st,7));
  14354. else
  14355. nd = (struct npc_data *)map_id2bl(st->oid);
  14356. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  14357. {
  14358. script_pushint(st,0);
  14359. return 0;
  14360. }
  14361. map_name = script_getstr(st,2);
  14362. if( strcmp(map_name,"-") != 0 )
  14363. {
  14364. mapindex = mapindex_name2id(map_name);
  14365. if( mapindex == 0 )
  14366. { // Invalid Map
  14367. script_pushint(st,0);
  14368. return 0;
  14369. }
  14370. }
  14371. x = script_getnum(st,3);
  14372. y = script_getnum(st,4);
  14373. ev = script_getstr(st,5); // Logout Event
  14374. dev = script_getstr(st,6); // Die Event
  14375. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  14376. { // Creation failed
  14377. script_pushint(st,0);
  14378. return 0;
  14379. }
  14380. n = cd->users;
  14381. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  14382. {
  14383. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  14384. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  14385. else
  14386. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  14387. }
  14388. mapreg_setreg(add_str("$@arenamembersnum"), i);
  14389. script_pushint(st,bg_id);
  14390. return SCRIPT_CMD_SUCCESS;
  14391. }
  14392. BUILDIN_FUNC(waitingroom2bg_single)
  14393. {
  14394. const char* map_name;
  14395. struct npc_data *nd;
  14396. struct chat_data *cd;
  14397. struct map_session_data *sd;
  14398. int x, y, mapindex, bg_id;
  14399. bg_id = script_getnum(st,2);
  14400. map_name = script_getstr(st,3);
  14401. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14402. return 0; // Invalid Map
  14403. x = script_getnum(st,4);
  14404. y = script_getnum(st,5);
  14405. nd = npc_name2id(script_getstr(st,6));
  14406. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  14407. return 0;
  14408. if( (sd = cd->usersd[0]) == NULL )
  14409. return 0;
  14410. if( bg_team_join(bg_id, sd) )
  14411. {
  14412. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  14413. script_pushint(st,1);
  14414. }
  14415. else
  14416. script_pushint(st,0);
  14417. return SCRIPT_CMD_SUCCESS;
  14418. }
  14419. BUILDIN_FUNC(bg_team_setxy)
  14420. {
  14421. struct battleground_data *bg;
  14422. int bg_id;
  14423. bg_id = script_getnum(st,2);
  14424. if( (bg = bg_team_search(bg_id)) == NULL )
  14425. return 0;
  14426. bg->x = script_getnum(st,3);
  14427. bg->y = script_getnum(st,4);
  14428. return SCRIPT_CMD_SUCCESS;
  14429. }
  14430. BUILDIN_FUNC(bg_warp)
  14431. {
  14432. int x, y, mapindex, bg_id;
  14433. const char* map_name;
  14434. bg_id = script_getnum(st,2);
  14435. map_name = script_getstr(st,3);
  14436. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14437. return 0; // Invalid Map
  14438. x = script_getnum(st,4);
  14439. y = script_getnum(st,5);
  14440. bg_team_warp(bg_id, mapindex, x, y);
  14441. return SCRIPT_CMD_SUCCESS;
  14442. }
  14443. BUILDIN_FUNC(bg_monster)
  14444. {
  14445. int class_ = 0, x = 0, y = 0, bg_id = 0;
  14446. const char *str,*map, *evt="";
  14447. bg_id = script_getnum(st,2);
  14448. map = script_getstr(st,3);
  14449. x = script_getnum(st,4);
  14450. y = script_getnum(st,5);
  14451. str = script_getstr(st,6);
  14452. class_ = script_getnum(st,7);
  14453. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  14454. check_event(st, evt);
  14455. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  14456. return SCRIPT_CMD_SUCCESS;
  14457. }
  14458. BUILDIN_FUNC(bg_monster_set_team)
  14459. {
  14460. struct mob_data *md;
  14461. struct block_list *mbl;
  14462. int id = script_getnum(st,2),
  14463. bg_id = script_getnum(st,3);
  14464. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  14465. return 0;
  14466. md = (TBL_MOB *)mbl;
  14467. md->bg_id = bg_id;
  14468. mob_stop_attack(md);
  14469. mob_stop_walking(md, 0);
  14470. md->target_id = md->attacked_id = 0;
  14471. clif_charnameack(0, &md->bl);
  14472. return SCRIPT_CMD_SUCCESS;
  14473. }
  14474. BUILDIN_FUNC(bg_leave)
  14475. {
  14476. struct map_session_data *sd = script_rid2sd(st);
  14477. if( sd == NULL || !sd->bg_id )
  14478. return 0;
  14479. bg_team_leave(sd,0);
  14480. return SCRIPT_CMD_SUCCESS;
  14481. }
  14482. BUILDIN_FUNC(bg_destroy)
  14483. {
  14484. int bg_id = script_getnum(st,2);
  14485. bg_team_delete(bg_id);
  14486. return SCRIPT_CMD_SUCCESS;
  14487. }
  14488. BUILDIN_FUNC(bg_getareausers)
  14489. {
  14490. const char *str;
  14491. int16 m, x0, y0, x1, y1;
  14492. int bg_id;
  14493. int i = 0, c = 0;
  14494. struct battleground_data *bg = NULL;
  14495. struct map_session_data *sd;
  14496. bg_id = script_getnum(st,2);
  14497. str = script_getstr(st,3);
  14498. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  14499. {
  14500. script_pushint(st,0);
  14501. return 0;
  14502. }
  14503. x0 = script_getnum(st,4);
  14504. y0 = script_getnum(st,5);
  14505. x1 = script_getnum(st,6);
  14506. y1 = script_getnum(st,7);
  14507. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  14508. {
  14509. if( (sd = bg->members[i].sd) == NULL )
  14510. continue;
  14511. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  14512. continue;
  14513. c++;
  14514. }
  14515. script_pushint(st,c);
  14516. return SCRIPT_CMD_SUCCESS;
  14517. }
  14518. BUILDIN_FUNC(bg_updatescore)
  14519. {
  14520. const char *str;
  14521. int16 m;
  14522. str = script_getstr(st,2);
  14523. if( (m = map_mapname2mapid(str)) < 0 )
  14524. return 0;
  14525. map[m].bgscore_lion = script_getnum(st,3);
  14526. map[m].bgscore_eagle = script_getnum(st,4);
  14527. clif_bg_updatescore(m);
  14528. return SCRIPT_CMD_SUCCESS;
  14529. }
  14530. BUILDIN_FUNC(bg_get_data)
  14531. {
  14532. struct battleground_data *bg;
  14533. int bg_id = script_getnum(st,2),
  14534. type = script_getnum(st,3);
  14535. if( (bg = bg_team_search(bg_id)) == NULL )
  14536. {
  14537. script_pushint(st,0);
  14538. return 0;
  14539. }
  14540. switch( type )
  14541. {
  14542. case 0: script_pushint(st, bg->count); break;
  14543. default:
  14544. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  14545. break;
  14546. }
  14547. return SCRIPT_CMD_SUCCESS;
  14548. }
  14549. /*==========================================
  14550. * Instancing System
  14551. *------------------------------------------*/
  14552. //Returns an Instance ID
  14553. //Checks NPC first, then if player is attached we check party
  14554. int script_instancegetid(struct script_state* st)
  14555. {
  14556. short instance_id = 0;
  14557. struct npc_data *nd;
  14558. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  14559. instance_id = nd->instance_id;
  14560. else {
  14561. struct map_session_data *sd;
  14562. struct party_data *p;
  14563. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14564. instance_id = p->instance_id;
  14565. }
  14566. return instance_id;
  14567. }
  14568. /*==========================================
  14569. * Creates the instance
  14570. * Returns Instance ID if created successfully
  14571. *------------------------------------------*/
  14572. BUILDIN_FUNC(instance_create)
  14573. {
  14574. struct map_session_data *sd;
  14575. if((sd = script_rid2sd(st)) == NULL)
  14576. return -1;
  14577. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  14578. return SCRIPT_CMD_SUCCESS;
  14579. }
  14580. /*==========================================
  14581. * Destroys an instance (unofficial)
  14582. * Officially instances are only destroyed by timeout
  14583. *
  14584. * instance_destroy {<instance_id>};
  14585. *------------------------------------------*/
  14586. BUILDIN_FUNC(instance_destroy)
  14587. {
  14588. short instance_id;
  14589. if( script_hasdata(st,2) )
  14590. instance_id = script_getnum(st,2);
  14591. else
  14592. instance_id = script_instancegetid(st);
  14593. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  14594. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  14595. return 0;
  14596. }
  14597. instance_destroy(instance_id);
  14598. return SCRIPT_CMD_SUCCESS;
  14599. }
  14600. /*==========================================
  14601. * Warps player to instance
  14602. * Results:
  14603. * 0: Success
  14604. * 1: Character not in party
  14605. * 2: Party doesn't have instance
  14606. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  14607. *------------------------------------------*/
  14608. BUILDIN_FUNC(instance_enter)
  14609. {
  14610. struct map_session_data *sd;
  14611. if((sd = script_rid2sd(st)) != NULL)
  14612. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  14613. else
  14614. return 1;
  14615. return SCRIPT_CMD_SUCCESS;
  14616. }
  14617. /*==========================================
  14618. * Returns the name of a duplicated NPC
  14619. *
  14620. * instance_npcname <npc_name>{,<instance_id>};
  14621. * <npc_name> is the full name of an NPC.
  14622. *------------------------------------------*/
  14623. BUILDIN_FUNC(instance_npcname)
  14624. {
  14625. const char *str;
  14626. short instance_id = 0;
  14627. struct npc_data *nd;
  14628. str = script_getstr(st,2);
  14629. if( script_hasdata(st,3) )
  14630. instance_id = script_getnum(st,3);
  14631. else
  14632. instance_id = script_instancegetid(st);
  14633. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  14634. static char npcname[NAME_LENGTH];
  14635. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  14636. script_pushconststr(st,npcname);
  14637. } else {
  14638. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  14639. st->state = END;
  14640. return 1;
  14641. }
  14642. return SCRIPT_CMD_SUCCESS;
  14643. }
  14644. /*==========================================
  14645. * Returns the name of a duplicated map
  14646. *
  14647. * instance_mapname <map_name>{,<instance_id>};
  14648. *------------------------------------------*/
  14649. BUILDIN_FUNC(instance_mapname)
  14650. {
  14651. const char *str;
  14652. int16 m;
  14653. short instance_id = 0;
  14654. str = script_getstr(st,2);
  14655. if( script_hasdata(st,3) )
  14656. instance_id = script_getnum(st,3);
  14657. else
  14658. instance_id = script_instancegetid(st);
  14659. // Check that instance mapname is a valid map
  14660. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  14661. script_pushconststr(st, "");
  14662. else
  14663. script_pushconststr(st, map[m].name);
  14664. return SCRIPT_CMD_SUCCESS;
  14665. }
  14666. /*==========================================
  14667. * Returns an Instance ID
  14668. *------------------------------------------*/
  14669. BUILDIN_FUNC(instance_id)
  14670. {
  14671. short instance_id;
  14672. instance_id = script_instancegetid(st);
  14673. if(!instance_id) {
  14674. //ShowError("script:instance_id: No instance attached to NPC or player");
  14675. script_pushint(st, 0);
  14676. return 1;
  14677. }
  14678. script_pushint(st, instance_id);
  14679. return SCRIPT_CMD_SUCCESS;
  14680. }
  14681. /*==========================================
  14682. * Warps all players inside an instance
  14683. *
  14684. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  14685. *------------------------------------------*/
  14686. BUILDIN_FUNC(instance_warpall)
  14687. {
  14688. struct party_data *p;
  14689. struct map_session_data *pl_sd;
  14690. int16 m, i;
  14691. short instance_id;
  14692. const char *mapn;
  14693. int x, y;
  14694. mapn = script_getstr(st,2);
  14695. x = script_getnum(st,3);
  14696. y = script_getnum(st,4);
  14697. if( script_hasdata(st,5) )
  14698. instance_id = script_getnum(st,5);
  14699. else
  14700. instance_id = script_instancegetid(st);
  14701. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  14702. return 0;
  14703. if( !(p = party_search(instance_data[instance_id].party_id)) )
  14704. return 0;
  14705. for( i = 0; i < MAX_PARTY; i++ )
  14706. 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);
  14707. return SCRIPT_CMD_SUCCESS;
  14708. }
  14709. /*==========================================
  14710. * Broadcasts to all maps inside an instance
  14711. *
  14712. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  14713. * Using -1 for <instance id> will auto-detect the id.
  14714. *------------------------------------------*/
  14715. BUILDIN_FUNC(instance_announce) {
  14716. int instance_id = script_getnum(st,2);
  14717. const char *mes = script_getstr(st,3);
  14718. int flag = script_getnum(st,4);
  14719. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  14720. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  14721. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  14722. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  14723. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  14724. int i;
  14725. if( instance_id == -1 ) {
  14726. instance_id = script_instancegetid(st);
  14727. }
  14728. if( !instance_id && &instance_data[instance_id] != NULL)
  14729. return true;
  14730. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  14731. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  14732. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  14733. return SCRIPT_CMD_SUCCESS;
  14734. }
  14735. /*==========================================
  14736. * instance_check_party [malufett]
  14737. * Values:
  14738. * party_id : Party ID of the invoking character. [Required Parameter]
  14739. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  14740. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  14741. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  14742. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  14743. * Example 2: instance_check_party (getcharid(1),1,1,99);
  14744. *------------------------------------------*/
  14745. BUILDIN_FUNC(instance_check_party)
  14746. {
  14747. struct map_session_data *pl_sd;
  14748. int amount, min, max, i, party_id, c = 0;
  14749. struct party_data *p = NULL;
  14750. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  14751. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  14752. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  14753. if( min < 1 || min > MAX_LEVEL) {
  14754. ShowError("script:check_party: Invalid min level, %d\n", min);
  14755. return 1;
  14756. } else if( max < 1 || max > MAX_LEVEL) {
  14757. ShowError("script:check_party: Invalid max level, %d\n", max);
  14758. return 1;
  14759. }
  14760. if( script_hasdata(st,2) )
  14761. party_id = script_getnum(st,2);
  14762. else return 0;
  14763. if( !(p = party_search(party_id)) ) {
  14764. script_pushint(st, 0); // Returns false if party does not exist.
  14765. return 0;
  14766. }
  14767. for( i = 0; i < MAX_PARTY; i++ )
  14768. if( (pl_sd = p->data[i].sd) )
  14769. if(map_id2bl(pl_sd->bl.id)) {
  14770. if(pl_sd->status.base_level < min) {
  14771. script_pushint(st, 0);
  14772. return 0;
  14773. } else if(pl_sd->status.base_level > max) {
  14774. script_pushint(st, 0);
  14775. return 0;
  14776. }
  14777. c++;
  14778. }
  14779. if(c < amount)
  14780. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  14781. else
  14782. script_pushint(st, 1);
  14783. return SCRIPT_CMD_SUCCESS;
  14784. }
  14785. /*==========================================
  14786. * Custom Fonts
  14787. *------------------------------------------*/
  14788. BUILDIN_FUNC(setfont)
  14789. {
  14790. struct map_session_data *sd = script_rid2sd(st);
  14791. int font = script_getnum(st,2);
  14792. if( sd == NULL )
  14793. return 0;
  14794. if( sd->user_font != font )
  14795. sd->user_font = font;
  14796. else
  14797. sd->user_font = 0;
  14798. clif_font(sd);
  14799. return SCRIPT_CMD_SUCCESS;
  14800. }
  14801. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  14802. {
  14803. TBL_MOB* md = (TBL_MOB*)bl;
  14804. struct block_list *tbl;
  14805. int mobid = va_arg(ap,int);
  14806. uint16 skill_id = va_arg(ap,int);
  14807. uint16 skill_lv = va_arg(ap,int);
  14808. int casttime = va_arg(ap,int);
  14809. int cancel = va_arg(ap,int);
  14810. int emotion = va_arg(ap,int);
  14811. int target = va_arg(ap,int);
  14812. if( md->class_ != mobid )
  14813. return 0;
  14814. // 0:self, 1:target, 2:master, default:random
  14815. switch( target )
  14816. {
  14817. case 0: tbl = map_id2bl(md->bl.id); break;
  14818. case 1: tbl = map_id2bl(md->target_id); break;
  14819. case 2: tbl = map_id2bl(md->master_id); break;
  14820. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  14821. }
  14822. if( !tbl )
  14823. return 0;
  14824. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  14825. unit_skillcastcancel(bl,0);
  14826. if( skill_get_casttype(skill_id) == CAST_GROUND )
  14827. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  14828. else
  14829. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  14830. clif_emotion(&md->bl, emotion);
  14831. return SCRIPT_CMD_SUCCESS;
  14832. }
  14833. /*==========================================
  14834. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  14835. *------------------------------------------*/
  14836. BUILDIN_FUNC(areamobuseskill)
  14837. {
  14838. struct block_list center;
  14839. struct script_data *data;
  14840. int16 m;
  14841. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  14842. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  14843. ShowError("areamobuseskill: invalid map name.\n");
  14844. return 0;
  14845. }
  14846. center.m = m;
  14847. center.x = script_getnum(st,3);
  14848. center.y = script_getnum(st,4);
  14849. range = script_getnum(st,5);
  14850. mobid = script_getnum(st,6);
  14851. data = script_getdata(st, 7);
  14852. get_val(st, data); // Convert into value in case of a variable
  14853. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  14854. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  14855. skill_lv = battle_config.mob_max_skilllvl;
  14856. casttime = script_getnum(st,9);
  14857. cancel = script_getnum(st,10);
  14858. emotion = script_getnum(st,11);
  14859. target = script_getnum(st,12);
  14860. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  14861. return SCRIPT_CMD_SUCCESS;
  14862. }
  14863. BUILDIN_FUNC(progressbar)
  14864. {
  14865. struct map_session_data * sd = script_rid2sd(st);
  14866. const char * color;
  14867. unsigned int second;
  14868. if( !st || !sd )
  14869. return 0;
  14870. st->state = STOP;
  14871. color = script_getstr(st,2);
  14872. second = script_getnum(st,3);
  14873. sd->progressbar.npc_id = st->oid;
  14874. sd->progressbar.timeout = gettick() + second*1000;
  14875. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  14876. return SCRIPT_CMD_SUCCESS;
  14877. }
  14878. BUILDIN_FUNC(pushpc)
  14879. {
  14880. uint8 dir;
  14881. int cells, dx, dy;
  14882. struct map_session_data* sd;
  14883. if((sd = script_rid2sd(st))==NULL)
  14884. {
  14885. return 0;
  14886. }
  14887. dir = script_getnum(st,2);
  14888. cells = script_getnum(st,3);
  14889. if(dir>7)
  14890. {
  14891. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  14892. script_reportsrc(st);
  14893. dir%= 8; // trim spin-over
  14894. }
  14895. if(!cells)
  14896. {// zero distance
  14897. return 0;
  14898. }
  14899. else if(cells<0)
  14900. {// pushing backwards
  14901. dir = (dir+4)%8; // turn around
  14902. cells = -cells;
  14903. }
  14904. dx = dirx[dir];
  14905. dy = diry[dir];
  14906. unit_blown(&sd->bl, dx, dy, cells, 0);
  14907. return SCRIPT_CMD_SUCCESS;
  14908. }
  14909. /// Invokes buying store preparation window
  14910. /// buyingstore <slots>;
  14911. BUILDIN_FUNC(buyingstore)
  14912. {
  14913. struct map_session_data* sd;
  14914. if( ( sd = script_rid2sd(st) ) == NULL )
  14915. {
  14916. return 0;
  14917. }
  14918. if( npc_isnear(&sd->bl) ) {
  14919. char output[150];
  14920. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  14921. clif_displaymessage(sd->fd, output);
  14922. return 0;
  14923. }
  14924. buyingstore_setup(sd, script_getnum(st,2));
  14925. return SCRIPT_CMD_SUCCESS;
  14926. }
  14927. /// Invokes search store info window
  14928. /// searchstores <uses>,<effect>;
  14929. BUILDIN_FUNC(searchstores)
  14930. {
  14931. unsigned short effect;
  14932. unsigned int uses;
  14933. struct map_session_data* sd;
  14934. if( ( sd = script_rid2sd(st) ) == NULL )
  14935. {
  14936. return 0;
  14937. }
  14938. uses = script_getnum(st,2);
  14939. effect = script_getnum(st,3);
  14940. if( !uses )
  14941. {
  14942. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  14943. return 1;
  14944. }
  14945. if( effect > 1 )
  14946. {
  14947. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  14948. return 1;
  14949. }
  14950. searchstore_open(sd, uses, effect);
  14951. return SCRIPT_CMD_SUCCESS;
  14952. }
  14953. /// Displays a number as large digital clock.
  14954. /// showdigit <value>[,<type>];
  14955. BUILDIN_FUNC(showdigit)
  14956. {
  14957. unsigned int type = 0;
  14958. int value;
  14959. struct map_session_data* sd;
  14960. if( ( sd = script_rid2sd(st) ) == NULL )
  14961. {
  14962. return 0;
  14963. }
  14964. value = script_getnum(st,2);
  14965. if( script_hasdata(st,3) )
  14966. {
  14967. type = script_getnum(st,3);
  14968. if( type > 3 )
  14969. {
  14970. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  14971. return 1;
  14972. }
  14973. }
  14974. clif_showdigit(sd, (unsigned char)type, value);
  14975. return SCRIPT_CMD_SUCCESS;
  14976. }
  14977. /**
  14978. * Rune Knight
  14979. **/
  14980. BUILDIN_FUNC(makerune) {
  14981. TBL_PC* sd;
  14982. if( (sd = script_rid2sd(st)) == NULL )
  14983. return 0;
  14984. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  14985. sd->itemid = script_getnum(st,2);
  14986. return SCRIPT_CMD_SUCCESS;
  14987. }
  14988. /**
  14989. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  14990. **/
  14991. BUILDIN_FUNC(checkdragon) {
  14992. TBL_PC* sd;
  14993. if( (sd = script_rid2sd(st)) == NULL )
  14994. return 0;
  14995. if( pc_isridingdragon(sd) )
  14996. script_pushint(st,1);
  14997. else
  14998. script_pushint(st,0);
  14999. return SCRIPT_CMD_SUCCESS;
  15000. }
  15001. /**
  15002. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  15003. * - Toggles the dragon on a RK if he can mount;
  15004. * @param Color - when not provided uses the green dragon;
  15005. * - 1 : Green Dragon
  15006. * - 2 : Brown Dragon
  15007. * - 3 : Gray Dragon
  15008. * - 4 : Blue Dragon
  15009. * - 5 : Red Dragon
  15010. **/
  15011. BUILDIN_FUNC(setdragon) {
  15012. TBL_PC* sd;
  15013. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  15014. if( (sd = script_rid2sd(st)) == NULL )
  15015. return 0;
  15016. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  15017. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  15018. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  15019. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  15020. script_pushint(st,1);
  15021. } else {//Not mounted; Mount now.
  15022. unsigned int option = OPTION_DRAGON1;
  15023. if( color ) {
  15024. option = ( color == 1 ? OPTION_DRAGON1 :
  15025. color == 2 ? OPTION_DRAGON2 :
  15026. color == 3 ? OPTION_DRAGON3 :
  15027. color == 4 ? OPTION_DRAGON4 :
  15028. color == 5 ? OPTION_DRAGON5 : 0);
  15029. if( !option ) {
  15030. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  15031. option = OPTION_DRAGON1;
  15032. }
  15033. }
  15034. pc_setoption(sd, sd->sc.option|option);
  15035. script_pushint(st,1);
  15036. }
  15037. return SCRIPT_CMD_SUCCESS;
  15038. }
  15039. /**
  15040. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  15041. **/
  15042. BUILDIN_FUNC(ismounting) {
  15043. TBL_PC* sd;
  15044. if( (sd = script_rid2sd(st)) == NULL )
  15045. return 0;
  15046. if( sd->sc.option&OPTION_MOUNTING )
  15047. script_pushint(st,1);
  15048. else
  15049. script_pushint(st,0);
  15050. return SCRIPT_CMD_SUCCESS;
  15051. }
  15052. /**
  15053. * setmounting() returns 1 on success or 0 otherwise
  15054. * - Toggles new mounts on a player when he can mount
  15055. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  15056. * - Will unmount the player is he is already mounting
  15057. **/
  15058. BUILDIN_FUNC(setmounting) {
  15059. TBL_PC* sd;
  15060. if( (sd = script_rid2sd(st)) == NULL )
  15061. return 0;
  15062. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) )
  15063. script_pushint(st,0);//can't mount with one of these
  15064. else {
  15065. if( sd->sc.option&OPTION_MOUNTING )
  15066. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  15067. else
  15068. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  15069. script_pushint(st,1);//in both cases, return 1.
  15070. }
  15071. return SCRIPT_CMD_SUCCESS;
  15072. }
  15073. /**
  15074. * Retrieves quantity of arguments provided to callfunc/callsub.
  15075. * getargcount() -> amount of arguments received in a function
  15076. **/
  15077. BUILDIN_FUNC(getargcount) {
  15078. struct script_retinfo* ri;
  15079. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  15080. ShowError("script:getargcount: used out of function or callsub label!\n");
  15081. st->state = END;
  15082. return 1;
  15083. }
  15084. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  15085. script_pushint(st, ri->nargs);
  15086. return SCRIPT_CMD_SUCCESS;
  15087. }
  15088. /**
  15089. * getcharip(<account ID>/<character ID>/<character name>)
  15090. **/
  15091. BUILDIN_FUNC(getcharip)
  15092. {
  15093. struct map_session_data* sd = NULL;
  15094. /* check if a character name is specified */
  15095. if( script_hasdata(st, 2) )
  15096. {
  15097. struct script_data *data;
  15098. data = script_getdata(st, 2);
  15099. get_val(st, data); // Convert into value in case of a variable
  15100. if (data_isstring(data))
  15101. sd = map_nick2sd(script_getstr(st, 2));
  15102. else if (data_isint(data) || script_getnum(st, 2))
  15103. {
  15104. int id = 0;
  15105. id = script_getnum(st, 2);
  15106. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  15107. }
  15108. }
  15109. else
  15110. sd = script_rid2sd(st);
  15111. /* check for sd and IP */
  15112. if (!sd || !session[sd->fd]->client_addr)
  15113. {
  15114. script_pushconststr(st, "");
  15115. return 0;
  15116. }
  15117. /* return the client ip_addr converted for output */
  15118. if (sd && sd->fd && session[sd->fd])
  15119. {
  15120. /* initiliaze */
  15121. const char *ip_addr = NULL;
  15122. uint32 ip;
  15123. /* set ip, ip_addr and convert to ip and push str */
  15124. ip = session[sd->fd]->client_addr;
  15125. ip_addr = ip2str(ip, NULL);
  15126. script_pushstrcopy(st, ip_addr);
  15127. }
  15128. return SCRIPT_CMD_SUCCESS;
  15129. }
  15130. /**
  15131. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  15132. **/
  15133. BUILDIN_FUNC(is_function) {
  15134. const char* str = script_getstr(st,2);
  15135. if( strdb_exists(userfunc_db, str) )
  15136. script_pushint(st,1);
  15137. else
  15138. script_pushint(st,0);
  15139. return SCRIPT_CMD_SUCCESS;
  15140. }
  15141. /**
  15142. * get_revision() -> retrieves the current svn revision (if available)
  15143. **/
  15144. BUILDIN_FUNC(get_revision) {
  15145. const char *svn = get_svn_revision();
  15146. if ( svn[0] != UNKNOWN_VERSION )
  15147. script_pushint(st,atoi(svn));
  15148. else
  15149. script_pushint(st,-1); //unknown
  15150. return SCRIPT_CMD_SUCCESS;
  15151. }
  15152. /**
  15153. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  15154. **/
  15155. BUILDIN_FUNC(freeloop) {
  15156. if( script_getnum(st,2) )
  15157. st->freeloop = 1;
  15158. else
  15159. st->freeloop = 0;
  15160. script_pushint(st, st->freeloop);
  15161. return SCRIPT_CMD_SUCCESS;
  15162. }
  15163. /**
  15164. * @commands (script based)
  15165. **/
  15166. BUILDIN_FUNC(bindatcmd) {
  15167. const char* atcmd;
  15168. const char* eventName;
  15169. int i, level = 0, level2 = 0;
  15170. bool create = false;
  15171. atcmd = script_getstr(st,2);
  15172. eventName = script_getstr(st,3);
  15173. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15174. atcmd++;
  15175. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  15176. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  15177. if( atcmd_binding_count == 0 ) {
  15178. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  15179. create = true;
  15180. } else {
  15181. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  15182. if( i < atcmd_binding_count ) {/* update existent entry */
  15183. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15184. atcmd_binding[i]->level = level;
  15185. atcmd_binding[i]->level2 = level2;
  15186. } else
  15187. create = true;
  15188. }
  15189. if( create ) {
  15190. i = atcmd_binding_count;
  15191. if( atcmd_binding_count++ != 0 )
  15192. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  15193. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  15194. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  15195. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15196. atcmd_binding[i]->level = level;
  15197. atcmd_binding[i]->level2 = level2;
  15198. }
  15199. return SCRIPT_CMD_SUCCESS;
  15200. }
  15201. BUILDIN_FUNC(unbindatcmd) {
  15202. const char* atcmd;
  15203. int i = 0;
  15204. atcmd = script_getstr(st, 2);
  15205. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15206. atcmd++;
  15207. if( atcmd_binding_count == 0 ) {
  15208. script_pushint(st, 0);
  15209. return 0;
  15210. }
  15211. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  15212. if( i < atcmd_binding_count ) {
  15213. int cursor = 0;
  15214. aFree(atcmd_binding[i]);
  15215. atcmd_binding[i] = NULL;
  15216. /* compact the list now that we freed a slot somewhere */
  15217. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  15218. if( atcmd_binding[i] == NULL )
  15219. continue;
  15220. if( cursor != i ) {
  15221. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  15222. }
  15223. cursor++;
  15224. }
  15225. if( (atcmd_binding_count = cursor) == 0 )
  15226. aFree(atcmd_binding);
  15227. script_pushint(st, 1);
  15228. } else
  15229. script_pushint(st, 0);/* not found */
  15230. return SCRIPT_CMD_SUCCESS;
  15231. }
  15232. BUILDIN_FUNC(useatcmd) {
  15233. return atcommand_sub(st,3);
  15234. }
  15235. BUILDIN_FUNC(checkre)
  15236. {
  15237. int num;
  15238. num=script_getnum(st,2);
  15239. switch(num){
  15240. case 0:
  15241. #ifdef RENEWAL
  15242. script_pushint(st, 1);
  15243. #else
  15244. script_pushint(st, 0);
  15245. #endif
  15246. break;
  15247. case 1:
  15248. #ifdef RENEWAL_CAST
  15249. script_pushint(st, 1);
  15250. #else
  15251. script_pushint(st, 0);
  15252. #endif
  15253. break;
  15254. case 2:
  15255. #ifdef RENEWAL_DROP
  15256. script_pushint(st, 1);
  15257. #else
  15258. script_pushint(st, 0);
  15259. #endif
  15260. break;
  15261. case 3:
  15262. #ifdef RENEWAL_EXP
  15263. script_pushint(st, 1);
  15264. #else
  15265. script_pushint(st, 0);
  15266. #endif
  15267. break;
  15268. case 4:
  15269. #ifdef RENEWAL_LVDMG
  15270. script_pushint(st, 1);
  15271. #else
  15272. script_pushint(st, 0);
  15273. #endif
  15274. break;
  15275. case 5:
  15276. #ifdef RENEWAL_ASPD
  15277. script_pushint(st, 1);
  15278. #else
  15279. script_pushint(st, 0);
  15280. #endif
  15281. break;
  15282. default:
  15283. ShowWarning("buildin_checkre: unknown parameter.\n");
  15284. break;
  15285. }
  15286. return SCRIPT_CMD_SUCCESS;
  15287. }
  15288. /* getrandgroupitem <group_id>,<quantity>{,<sub_group>} */
  15289. BUILDIN_FUNC(getrandgroupitem) {
  15290. TBL_PC* sd;
  15291. int i, get_count = 0, flag;
  15292. uint16 nameid, group = script_getnum(st,2), qty = script_getnum(st,3);
  15293. uint8 sub_group = script_getnum(st,4);
  15294. struct item item_tmp;
  15295. if (!( sd = script_rid2sd(st)))
  15296. return SCRIPT_CMD_SUCCESS;
  15297. if (!group) {
  15298. ShowError("getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  15299. return SCRIPT_CMD_FAILURE;
  15300. }
  15301. if ((nameid = itemdb_searchrandomid(group,sub_group)) == UNKNOWN_ITEM_ID) {
  15302. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  15303. }
  15304. memset(&item_tmp,0,sizeof(item_tmp));
  15305. item_tmp.nameid = nameid;
  15306. item_tmp.identify = itemdb_isidentified(nameid);
  15307. if (!qty)
  15308. qty = itemdb_get_randgroupitem_count(group,sub_group,nameid);
  15309. //Check if it's stackable.
  15310. if (!itemdb_isstackable(nameid)) {
  15311. item_tmp.amount = 1;
  15312. get_count = qty;
  15313. }
  15314. else {
  15315. item_tmp.amount = qty;
  15316. get_count = 1;
  15317. }
  15318. for (i = 0; i < get_count; i++) {
  15319. // if not pet egg
  15320. if (!pet_create_egg(sd, nameid)) {
  15321. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  15322. clif_additem(sd,0,0,flag);
  15323. if (pc_candrop(sd,&item_tmp))
  15324. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  15325. }
  15326. }
  15327. }
  15328. return SCRIPT_CMD_SUCCESS;
  15329. }
  15330. /* getgroupitem <group_id>;
  15331. * Gives item(s) to the attached player based on item group contents
  15332. */
  15333. BUILDIN_FUNC(getgroupitem) {
  15334. TBL_PC *sd;
  15335. int group_id = script_getnum(st,2);
  15336. if (!(sd = script_rid2sd(st)))
  15337. return SCRIPT_CMD_SUCCESS;
  15338. if (itemdb_pc_get_itemgroup(group_id,sd->itemid,sd)) {
  15339. ShowError("getgroupitem: Invalid group id '%d' specified.",group_id);
  15340. return SCRIPT_CMD_FAILURE;
  15341. }
  15342. return SCRIPT_CMD_SUCCESS;
  15343. }
  15344. /* cleanmap <map_name>;
  15345. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  15346. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  15347. {
  15348. nullpo_ret(bl);
  15349. map_clearflooritem(bl);
  15350. return SCRIPT_CMD_SUCCESS;
  15351. }
  15352. BUILDIN_FUNC(cleanmap)
  15353. {
  15354. const char *map;
  15355. int16 m = -1;
  15356. int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  15357. map = script_getstr(st, 2);
  15358. m = map_mapname2mapid(map);
  15359. if (!m)
  15360. return 1;
  15361. if ((script_lastdata(st) - 2) < 4) {
  15362. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  15363. } else {
  15364. x0 = script_getnum(st, 3);
  15365. y0 = script_getnum(st, 4);
  15366. x1 = script_getnum(st, 5);
  15367. y1 = script_getnum(st, 6);
  15368. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  15369. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  15370. } else {
  15371. ShowError("cleanarea: invalid coordinate defined!\n");
  15372. return 1;
  15373. }
  15374. }
  15375. return SCRIPT_CMD_SUCCESS;
  15376. }
  15377. /* Cast a skill on the attached player.
  15378. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  15379. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  15380. BUILDIN_FUNC(npcskill)
  15381. {
  15382. uint16 skill_id;
  15383. unsigned short skill_level;
  15384. unsigned int stat_point;
  15385. unsigned int npc_level;
  15386. struct npc_data *nd;
  15387. struct map_session_data *sd;
  15388. struct script_data *data;
  15389. data = script_getdata(st, 2);
  15390. get_val(st, data); // Convert into value in case of a variable
  15391. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  15392. skill_level = script_getnum(st, 3);
  15393. stat_point = script_getnum(st, 4);
  15394. npc_level = script_getnum(st, 5);
  15395. sd = script_rid2sd(st);
  15396. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  15397. if (stat_point > battle_config.max_third_parameter) {
  15398. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  15399. return 1;
  15400. }
  15401. if (npc_level > MAX_LEVEL) {
  15402. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  15403. return 1;
  15404. }
  15405. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  15406. return 1;
  15407. }
  15408. nd->level = npc_level;
  15409. nd->stat_point = stat_point;
  15410. if (!nd->status.hp) {
  15411. status_calc_npc(nd, true);
  15412. } else {
  15413. status_calc_npc(nd, false);
  15414. }
  15415. if (skill_get_inf(skill_id)&INF_GROUND_SKILL) {
  15416. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  15417. } else {
  15418. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  15419. }
  15420. return SCRIPT_CMD_SUCCESS;
  15421. }
  15422. /* Consumes an item.
  15423. * consumeitem <item id>;
  15424. * consumeitem "<item name>"; */
  15425. BUILDIN_FUNC(consumeitem)
  15426. {
  15427. TBL_NPC *nd;
  15428. TBL_PC *sd;
  15429. struct script_data *data;
  15430. struct item_data *item_data;
  15431. nullpo_retr( 1, ( sd = script_rid2sd( st ) ) );
  15432. nullpo_retr( 1, ( nd = (TBL_NPC *)map_id2bl( sd->npc_id ) ) );
  15433. data = script_getdata( st, 2 );
  15434. get_val( st, data );
  15435. if( data_isstring( data ) ){
  15436. const char *name = conv_str( st, data );
  15437. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  15438. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  15439. return 1;
  15440. }
  15441. }else if( data_isint( data ) ){
  15442. int nameid = conv_num( st, data );
  15443. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  15444. ShowError("buildin_consumeitem: Nonexistant item %d requested.\n", nameid );
  15445. return 1;
  15446. }
  15447. }else{
  15448. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).", data->type );
  15449. return 1;
  15450. }
  15451. run_script( item_data->script, 0, sd->bl.id, nd->bl.id );
  15452. return SCRIPT_CMD_SUCCESS;
  15453. }
  15454. /*=======================================================
  15455. * Make a player sit/stand.
  15456. * sit {"<character name>"};
  15457. * stand {"<character name>"};
  15458. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  15459. *-------------------------------------------------------*/
  15460. BUILDIN_FUNC(sit)
  15461. {
  15462. TBL_PC *sd;
  15463. if( script_hasdata(st, 2) )
  15464. sd = map_nick2sd(script_getstr(st, 2));
  15465. else
  15466. sd = script_rid2sd(st);
  15467. if( sd == NULL)
  15468. return 0;
  15469. if( !pc_issit(sd) ) {
  15470. unit_stop_walking(&sd->bl, 1|4);
  15471. pc_setsit(sd);
  15472. skill_sit(sd, 1);
  15473. clif_sitting(&sd->bl);
  15474. }
  15475. return SCRIPT_CMD_SUCCESS;
  15476. }
  15477. BUILDIN_FUNC(stand)
  15478. {
  15479. TBL_PC *sd;
  15480. if( script_hasdata(st, 2) )
  15481. sd = map_nick2sd(script_getstr(st, 2));
  15482. else
  15483. sd = script_rid2sd(st);
  15484. if( sd == NULL)
  15485. return 0;
  15486. if( pc_issit(sd) ) {
  15487. pc_setstand(sd);
  15488. skill_sit(sd, 0);
  15489. clif_standing(&sd->bl);
  15490. }
  15491. return SCRIPT_CMD_SUCCESS;
  15492. }
  15493. /*==========================================
  15494. * countbound {<type>};
  15495. * Creates an array of bounded item IDs
  15496. * Returns amount of items found
  15497. * Type:
  15498. * 1 - Account Bound
  15499. * 2 - Guild Bound
  15500. * 3 - Party Bound
  15501. *------------------------------------------*/
  15502. BUILDIN_FUNC(countbound)
  15503. {
  15504. int i, type, j=0, k=0;
  15505. TBL_PC *sd;
  15506. if( (sd = script_rid2sd(st)) == NULL )
  15507. return 0;
  15508. type = script_hasdata(st,2)?script_getnum(st,2):0;
  15509. for(i=0;i<MAX_INVENTORY;i++){
  15510. if(sd->status.inventory[i].nameid > 0 && (
  15511. (!type && sd->status.inventory[i].bound > 0) ||
  15512. (type && sd->status.inventory[i].bound == type)
  15513. )) {
  15514. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  15515. k++;
  15516. j += sd->status.inventory[i].amount;
  15517. }
  15518. }
  15519. script_pushint(st,j);
  15520. return SCRIPT_CMD_SUCCESS;
  15521. }
  15522. /*==========================================
  15523. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  15524. * <item share>: 0-Each Take. 1-Party Share
  15525. * <item share type>: 0-Each Take. 1-Even Share
  15526. * Return values:
  15527. * -3 - party name is exist
  15528. * -2 - player is in party already
  15529. * -1 - player is not found
  15530. * 0 - unknown error
  15531. * 1 - success, will return party id $@party_create_id
  15532. *------------------------------------------*/
  15533. BUILDIN_FUNC(party_create)
  15534. {
  15535. char party_name[NAME_LENGTH];
  15536. int item1 = 0, item2 = 0;
  15537. TBL_PC *sd;
  15538. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  15539. script_pushint(st,-1);
  15540. return 0;
  15541. }
  15542. if( sd->status.party_id ) {
  15543. script_pushint(st,-2);
  15544. return 0;
  15545. }
  15546. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  15547. trim(party_name);
  15548. if( party_searchname(party_name) ) {
  15549. script_pushint(st,-3);
  15550. return 0;
  15551. }
  15552. if( script_getnum(st,4) )
  15553. item1 = 1;
  15554. if( script_getnum(st,5) )
  15555. item2 = 1;
  15556. party_create_byscript = 1;
  15557. script_pushint(st,party_create(sd,party_name,item1,item2));
  15558. return SCRIPT_CMD_SUCCESS;
  15559. }
  15560. /*==========================================
  15561. * party_addmember <party id>,<char id>;
  15562. * Adds player to specified party
  15563. * Return values:
  15564. * -4 - party is full
  15565. * -3 - party is not found
  15566. * -2 - player is in party already
  15567. * -1 - player is not found
  15568. * 0 - unknown error
  15569. * 1 - success
  15570. *------------------------------------------*/
  15571. BUILDIN_FUNC(party_addmember)
  15572. {
  15573. int party_id = script_getnum(st,2);
  15574. TBL_PC *sd;
  15575. struct party_data *party;
  15576. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  15577. script_pushint(st,-1);
  15578. return 0;
  15579. }
  15580. if( sd->status.party_id ) {
  15581. script_pushint(st,-2);
  15582. return 0;
  15583. }
  15584. if( !(party = party_search(party_id)) ) {
  15585. script_pushint(st,-3);
  15586. return 0;
  15587. }
  15588. if( party->party.count >= MAX_PARTY ) {
  15589. script_pushint(st,-4);
  15590. return 0;
  15591. }
  15592. sd->party_invite = party_id;
  15593. script_pushint(st,party_add_member(party_id,sd));
  15594. return SCRIPT_CMD_SUCCESS;
  15595. }
  15596. /*==========================================
  15597. * party_delmember {<char id>,<party_id>};
  15598. * Removes player from his/her party. If party_id and char_id is empty
  15599. * remove the invoker from his/her party
  15600. * Return values:
  15601. * -3 - player is not in party
  15602. * -2 - party is not found
  15603. * -1 - player is not found
  15604. * 0 - unknown error
  15605. * 1 - success
  15606. *------------------------------------------*/
  15607. BUILDIN_FUNC(party_delmember)
  15608. {
  15609. TBL_PC *sd = NULL;
  15610. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  15611. script_pushint(st,-1);
  15612. return 0;
  15613. }
  15614. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  15615. script_pushint(st,party_removemember2(sd,0,0));
  15616. else
  15617. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  15618. return SCRIPT_CMD_SUCCESS;
  15619. }
  15620. /*==========================================
  15621. * party_changeleader <party id>,<char id>;
  15622. * Can change party leader even the leader is not online
  15623. * Return values:
  15624. * -4 - player is party leader already
  15625. * -3 - player is not in this party
  15626. * -2 - player is not found
  15627. * -1 - party is not found
  15628. * 0 - unknown error
  15629. * 1 - success
  15630. *------------------------------------------*/
  15631. BUILDIN_FUNC(party_changeleader)
  15632. {
  15633. int i, party_id = script_getnum(st,2);
  15634. TBL_PC *sd = NULL;
  15635. TBL_PC *tsd = NULL;
  15636. struct party_data *party = NULL;
  15637. if( !(party = party_search(party_id)) ) {
  15638. script_pushint(st,-1);
  15639. return 0;
  15640. }
  15641. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  15642. script_pushint(st,-2);
  15643. return 0;
  15644. }
  15645. if( tsd->status.party_id != party_id ) {
  15646. script_pushint(st,-3);
  15647. return 0;
  15648. }
  15649. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  15650. if( i >= MAX_PARTY ) { //this is should impossible!
  15651. script_pushint(st,0);
  15652. return 0;
  15653. }
  15654. if( party->data[i].sd == tsd ) {
  15655. script_pushint(st,-4);
  15656. return 0;
  15657. }
  15658. script_pushint(st,party_changeleader(sd,tsd,party));
  15659. return SCRIPT_CMD_SUCCESS;
  15660. }
  15661. /*==========================================
  15662. * party_changeoption <party id>,<option>,<flag>;
  15663. * Return values:
  15664. * -1 - party is not found
  15665. * 0 - invalid option
  15666. * 1 - success
  15667. *------------------------------------------*/
  15668. BUILDIN_FUNC(party_changeoption)
  15669. {
  15670. struct party_data *party;
  15671. if( !(party = party_search(script_getnum(st,2))) ) {
  15672. script_pushint(st,-1);
  15673. return 0;
  15674. }
  15675. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  15676. return SCRIPT_CMD_SUCCESS;
  15677. }
  15678. /*==========================================
  15679. * party_destroy <party id>;
  15680. * Destroys party with party id.
  15681. * Return values:
  15682. * 0 - failed
  15683. * 1 - success
  15684. *------------------------------------------*/
  15685. BUILDIN_FUNC(party_destroy)
  15686. {
  15687. int i;
  15688. struct party_data *party;
  15689. if( !(party = party_search(script_getnum(st,2))) ) {
  15690. script_pushint(st,0);
  15691. return 0;
  15692. }
  15693. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  15694. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  15695. int j;
  15696. for( j = 0; j < MAX_PARTY; j++ ) {
  15697. TBL_PC *sd = party->data[j].sd;
  15698. if(sd)
  15699. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  15700. else if( party->party.member[j].char_id )
  15701. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  15702. }
  15703. party_broken(party->party.party_id);
  15704. script_pushint(st,1);
  15705. }
  15706. else //leader leave = party broken
  15707. script_pushint(st,party_leave(party->data[i].sd));
  15708. return SCRIPT_CMD_SUCCESS;
  15709. }
  15710. /*==========================================
  15711. * Checks if a player's client version meets a required version or date.
  15712. * @type :
  15713. * 0 - check by version number
  15714. * 1 - check by date
  15715. * @return true/false
  15716. *------------------------------------------*/
  15717. BUILDIN_FUNC(is_clientver) {
  15718. TBL_PC *sd = NULL;
  15719. int type = script_getnum(st,2);
  15720. int data = script_getnum(st,3);
  15721. int ret = 0;
  15722. if (script_hasdata(st,4))
  15723. sd = map_charid2sd(script_getnum(st,4));
  15724. else
  15725. sd = script_rid2sd(st);
  15726. if (sd == NULL) {
  15727. script_pushint(st,0);
  15728. return 0;
  15729. }
  15730. switch(type){
  15731. case 0:
  15732. ret = (sd->packet_ver >= data)?1:0;
  15733. break;
  15734. case 1:
  15735. ret = (sd->packet_ver >= date2version(data))?1:0;
  15736. break;
  15737. }
  15738. script_pushint(st,ret);
  15739. return SCRIPT_CMD_SUCCESS;
  15740. }
  15741. /*==========================================
  15742. * Retrieves server definitions.
  15743. * (see @type in const.txt)
  15744. *------------------------------------------*/
  15745. BUILDIN_FUNC(getserverdef) {
  15746. int type = script_getnum(st,2);
  15747. switch(type){
  15748. case 0: script_pushint(st,PACKETVER); break;
  15749. case 1: script_pushint(st,MAX_LEVEL); break;
  15750. case 2: script_pushint(st,MAX_STORAGE); break;
  15751. case 3: script_pushint(st,MAX_INVENTORY); break;
  15752. case 4: script_pushint(st,MAX_ZENY); break;
  15753. case 5: script_pushint(st,MAX_PARTY); break;
  15754. case 6: script_pushint(st,MAX_GUILD); break;
  15755. case 7: script_pushint(st,MAX_GUILDLEVEL); break;
  15756. case 8: script_pushint(st,MAX_GUILD_STORAGE); break;
  15757. case 9: script_pushint(st,MAX_BG_MEMBERS); break;
  15758. case 10: script_pushint(st,VIP_SCRIPT); break;
  15759. case 11: script_pushint(st,MIN_STORAGE); break;
  15760. default:
  15761. ShowWarning("buildin_getserverdef: unknown type %d.\n", type);
  15762. script_pushint(st,0);
  15763. break;
  15764. }
  15765. return SCRIPT_CMD_SUCCESS;
  15766. }
  15767. /* Returns various information about a player's VIP status.
  15768. * vip_status <type>,{"<character name>"};
  15769. * Note: VIP System needs to be enabled.
  15770. */
  15771. BUILDIN_FUNC(vip_status) {
  15772. #ifdef VIP_ENABLE
  15773. TBL_PC *sd;
  15774. char vip_str[26];
  15775. time_t now = time(NULL);
  15776. int type = script_getnum(st, 2);
  15777. if (script_hasdata(st, 3))
  15778. sd = map_nick2sd(script_getstr(st, 3));
  15779. else
  15780. sd = script_rid2sd(st);
  15781. if (sd == NULL)
  15782. return 0;
  15783. switch(type) {
  15784. case 1: // Get VIP status.
  15785. script_pushint(st, pc_isvip(sd));
  15786. break;
  15787. case 2: // Get VIP expire date.
  15788. if (pc_isvip(sd)) {
  15789. time_t viptime = sd->vip.time;
  15790. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  15791. vip_str[25] = '\0';
  15792. script_pushstrcopy(st, vip_str);
  15793. } else
  15794. script_pushint(st, 0);
  15795. break;
  15796. case 3: // Get remaining time.
  15797. if (pc_isvip(sd)) {
  15798. time_t viptime_remain = sd->vip.time - now;
  15799. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  15800. split_time(viptime_remain,&year,&month,&day,&hour,&min,&sec);
  15801. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  15802. script_pushstrcopy(st, vip_str);
  15803. } else
  15804. script_pushint(st, 0);
  15805. break;
  15806. }
  15807. #else
  15808. script_pushint(st, 0);
  15809. #endif
  15810. return SCRIPT_CMD_SUCCESS;
  15811. }
  15812. /* Adds or removes VIP time in minutes.
  15813. * vip_time <time in mn>,{"<character name>"};
  15814. * If time < 0 remove time, else add time.
  15815. * Note: VIP System needs to be enabled.
  15816. */
  15817. BUILDIN_FUNC(vip_time) {
  15818. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  15819. TBL_PC *sd;
  15820. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  15821. if (script_hasdata(st, 3))
  15822. sd = map_nick2sd(script_getstr(st, 3));
  15823. else
  15824. sd = script_rid2sd(st);
  15825. if (sd == NULL)
  15826. return 0;
  15827. chrif_req_login_operation(sd->status.account_id, sd->status.name, 6, viptime, 7, 0);
  15828. #endif
  15829. return SCRIPT_CMD_SUCCESS;
  15830. }
  15831. /*==========================================
  15832. * Turns a player into a monster and grants SC attribute effect. [malufett/Hercules]
  15833. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  15834. *------------------------------------------*/
  15835. BUILDIN_FUNC(montransform) {
  15836. TBL_PC *sd;
  15837. enum sc_type type;
  15838. char msg[CHAT_SIZE_MAX];
  15839. int tick, mob_id, val1, val2, val3, val4;
  15840. struct script_data *data;
  15841. if( (sd = script_rid2sd(st)) == NULL )
  15842. return 1;
  15843. data = script_getdata(st, 2);
  15844. get_val(st, data); // Convert into value in case of a variable
  15845. if( data_isstring(data) )
  15846. mob_id = mobdb_searchname(script_getstr(st, 2));
  15847. else
  15848. mob_id = mobdb_checkid(script_getnum(st, 2));
  15849. tick = script_getnum(st, 3);
  15850. type = (sc_type)script_getnum(st, 4);
  15851. val1 = val2 = val3 = val4 = 0;
  15852. if (mob_id == 0) {
  15853. if( data_isstring(data) )
  15854. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  15855. else
  15856. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  15857. return 0;
  15858. }
  15859. if (mob_id == MOBID_EMPERIUM) {
  15860. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  15861. return 0;
  15862. }
  15863. if (!(type > SC_NONE && type < SC_MAX)) {
  15864. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  15865. return 0;
  15866. }
  15867. if (script_hasdata(st, 5))
  15868. val1 = script_getnum(st, 5);
  15869. if (script_hasdata(st, 6))
  15870. val2 = script_getnum(st, 6);
  15871. if (script_hasdata(st, 7))
  15872. val3 = script_getnum(st, 7);
  15873. if (script_hasdata(st, 8))
  15874. val4 = script_getnum(st, 8);
  15875. if (tick != 0) {
  15876. struct mob_db *monster = mob_db(mob_id);
  15877. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  15878. clif_displaymessage(sd->fd, msg_txt(sd,1500)); // Transforming into monster is not allowed in Guild Wars.
  15879. return 0;
  15880. }
  15881. if (sd->disguise){
  15882. clif_displaymessage(sd->fd, msg_txt(sd,1498)); // Cannot transform into monster while in disguise.
  15883. return 0;
  15884. }
  15885. sprintf(msg, msg_txt(sd,1497), monster->name); // Traaaansformation-!! %s form!!
  15886. clif_disp_overhead(&sd->bl, msg);
  15887. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  15888. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  15889. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  15890. }
  15891. return SCRIPT_CMD_SUCCESS;
  15892. }
  15893. /** [Cydh]
  15894. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  15895. * @param "script code"
  15896. * @param duration
  15897. * @param flag
  15898. * @param char_id
  15899. **/
  15900. BUILDIN_FUNC(bonus_script) {
  15901. uint8 i, flag = 0;
  15902. int16 icon = SI_BLANK;
  15903. uint32 dur;
  15904. char type = 0;
  15905. TBL_PC* sd;
  15906. const char *script_str = NULL;
  15907. struct script_code *script = NULL;
  15908. if (script_hasdata(st,7))
  15909. sd = map_charid2sd(script_getnum(st,7));
  15910. else
  15911. sd = script_rid2sd(st);
  15912. if (sd == NULL)
  15913. return 0;
  15914. script_str = script_getstr(st,2);
  15915. dur = 1000 * abs(script_getnum(st,3));
  15916. FETCH(4,flag);
  15917. FETCH(5,type);
  15918. FETCH(6,icon);
  15919. if (!strlen(script_str) || !dur) {
  15920. //ShowWarning("buildin_bonus_script: Invalid value(s). Skipping...\n");
  15921. return 0;
  15922. }
  15923. //Skip duplicate entry
  15924. 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);
  15925. if (i < MAX_PC_BONUS_SCRIPT) {
  15926. //ShowWarning("buildin_bonus_script: Duplicate entry with bonus '%d'. Skipping...\n",i);
  15927. return 0;
  15928. }
  15929. if (!(script = parse_script(script_str,"bonus_script",0,1))) {
  15930. //ShowWarning("buildin_bonus_script: Failed to parse script '%s'. Skipping...\n",script_str);
  15931. return 0;
  15932. }
  15933. //Find the empty slot
  15934. ARR_FIND(0,MAX_PC_BONUS_SCRIPT,i,!sd->bonus_script[i].script);
  15935. if (i >= MAX_PC_BONUS_SCRIPT) {
  15936. ShowWarning("buildin_itemscript: Maximum script_bonus is reached (max: %d). Skipping...\n",MAX_PC_BONUS_SCRIPT);
  15937. return 0;
  15938. }
  15939. //Add the script data
  15940. memcpy(sd->bonus_script[i].script_str,script_str,strlen(script_str)+1);
  15941. sd->bonus_script[i].script = script;
  15942. sd->bonus_script[i].tick = gettick() + dur;
  15943. sd->bonus_script[i].flag = flag;
  15944. sd->bonus_script[i].type = type;
  15945. sd->bonus_script[i].icon = icon;
  15946. if (sd->bonus_script[i].icon != SI_BLANK) //Gives status icon if exist
  15947. clif_status_change(&sd->bl,sd->bonus_script[i].icon,1,dur,1,0,0);
  15948. status_calc_pc(sd,false);
  15949. return SCRIPT_CMD_SUCCESS;
  15950. }
  15951. #include "../custom/script.inc"
  15952. // declarations that were supposed to be exported from npc_chat.c
  15953. #ifdef PCRE_SUPPORT
  15954. BUILDIN_FUNC(defpattern);
  15955. BUILDIN_FUNC(activatepset);
  15956. BUILDIN_FUNC(deactivatepset);
  15957. BUILDIN_FUNC(deletepset);
  15958. #endif
  15959. /// script command definitions
  15960. /// for an explanation on args, see add_buildin_func
  15961. struct script_function buildin_func[] = {
  15962. // NPC interaction
  15963. BUILDIN_DEF(mes,"s*"),
  15964. BUILDIN_DEF(next,""),
  15965. BUILDIN_DEF(close,""),
  15966. BUILDIN_DEF(close2,""),
  15967. BUILDIN_DEF(menu,"sl*"),
  15968. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  15969. BUILDIN_DEF(prompt,"s*"),
  15970. //
  15971. BUILDIN_DEF(goto,"l"),
  15972. BUILDIN_DEF(callsub,"l*"),
  15973. BUILDIN_DEF(callfunc,"s*"),
  15974. BUILDIN_DEF(return,"?"),
  15975. BUILDIN_DEF(getarg,"i?"),
  15976. BUILDIN_DEF(jobchange,"i?"),
  15977. BUILDIN_DEF(jobname,"i"),
  15978. BUILDIN_DEF(input,"r??"),
  15979. BUILDIN_DEF(warp,"sii"),
  15980. BUILDIN_DEF(areawarp,"siiiisii??"),
  15981. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  15982. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  15983. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  15984. BUILDIN_DEF(setlook,"ii"),
  15985. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  15986. BUILDIN_DEF(set,"rv"),
  15987. BUILDIN_DEF(setarray,"rv*"),
  15988. BUILDIN_DEF(cleararray,"rvi"),
  15989. BUILDIN_DEF(copyarray,"rri"),
  15990. BUILDIN_DEF(getarraysize,"r"),
  15991. BUILDIN_DEF(deletearray,"r?"),
  15992. BUILDIN_DEF(getelementofarray,"ri"),
  15993. BUILDIN_DEF(getitem,"vi?"),
  15994. BUILDIN_DEF(rentitem,"vi"),
  15995. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  15996. BUILDIN_DEF(getnameditem,"vv"),
  15997. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  15998. BUILDIN_DEF(makeitem,"visii"),
  15999. BUILDIN_DEF(delitem,"vi?"),
  16000. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  16001. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  16002. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  16003. BUILDIN_DEF(cutin,"si"),
  16004. BUILDIN_DEF(viewpoint,"iiiii"),
  16005. BUILDIN_DEF(heal,"ii"),
  16006. BUILDIN_DEF(itemheal,"ii"),
  16007. BUILDIN_DEF(percentheal,"ii"),
  16008. BUILDIN_DEF(rand,"i?"),
  16009. BUILDIN_DEF(countitem,"v"),
  16010. BUILDIN_DEF2(countitem,"countitem2","viiiiiii"),
  16011. BUILDIN_DEF(checkweight,"vi*"),
  16012. BUILDIN_DEF(checkweight2,"rr"),
  16013. BUILDIN_DEF(readparam,"i?"),
  16014. BUILDIN_DEF(getcharid,"i?"),
  16015. BUILDIN_DEF(getnpcid,"i?"),
  16016. BUILDIN_DEF(getpartyname,"i"),
  16017. BUILDIN_DEF(getpartymember,"i?"),
  16018. BUILDIN_DEF(getpartyleader,"i?"),
  16019. BUILDIN_DEF(getguildname,"i"),
  16020. BUILDIN_DEF(getguildmaster,"i"),
  16021. BUILDIN_DEF(getguildmasterid,"i"),
  16022. BUILDIN_DEF(strcharinfo,"i"),
  16023. BUILDIN_DEF(strnpcinfo,"i"),
  16024. BUILDIN_DEF(getequipid,"i"),
  16025. BUILDIN_DEF(getequipname,"i"),
  16026. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  16027. BUILDIN_DEF(repair,"i"), // [Valaris]
  16028. BUILDIN_DEF(repairall,""),
  16029. BUILDIN_DEF(getequipisequiped,"i"),
  16030. BUILDIN_DEF(getequipisenableref,"i"),
  16031. BUILDIN_DEF(getequiprefinerycnt,"i"),
  16032. BUILDIN_DEF(getequipweaponlv,"i"),
  16033. BUILDIN_DEF(getequippercentrefinery,"i"),
  16034. BUILDIN_DEF(successrefitem,"i"),
  16035. BUILDIN_DEF(failedrefitem,"i"),
  16036. BUILDIN_DEF(downrefitem,"i?"),
  16037. BUILDIN_DEF(statusup,"i"),
  16038. BUILDIN_DEF(statusup2,"ii"),
  16039. BUILDIN_DEF(bonus,"iv"),
  16040. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  16041. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  16042. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  16043. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  16044. BUILDIN_DEF(autobonus,"sii??"),
  16045. BUILDIN_DEF(autobonus2,"sii??"),
  16046. BUILDIN_DEF(autobonus3,"siiv?"),
  16047. BUILDIN_DEF(skill,"vi?"),
  16048. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  16049. BUILDIN_DEF(guildskill,"vi"),
  16050. BUILDIN_DEF(getskilllv,"v"),
  16051. BUILDIN_DEF(getgdskilllv,"iv"),
  16052. BUILDIN_DEF(basicskillcheck,""),
  16053. BUILDIN_DEF(getgmlevel,""),
  16054. BUILDIN_DEF(getgroupid,""),
  16055. BUILDIN_DEF(end,""),
  16056. BUILDIN_DEF(checkoption,"i"),
  16057. BUILDIN_DEF(setoption,"i?"),
  16058. BUILDIN_DEF(setcart,"?"),
  16059. BUILDIN_DEF(checkcart,""),
  16060. BUILDIN_DEF(setfalcon,"?"),
  16061. BUILDIN_DEF(checkfalcon,""),
  16062. BUILDIN_DEF(setriding,"?"),
  16063. BUILDIN_DEF(checkriding,""),
  16064. BUILDIN_DEF(checkwug,""),
  16065. BUILDIN_DEF(checkmadogear,""),
  16066. BUILDIN_DEF(setmadogear,"?"),
  16067. BUILDIN_DEF2(savepoint,"save","sii"),
  16068. BUILDIN_DEF(savepoint,"sii"),
  16069. BUILDIN_DEF(gettimetick,"i"),
  16070. BUILDIN_DEF(gettime,"i"),
  16071. BUILDIN_DEF(gettimestr,"si"),
  16072. BUILDIN_DEF(openstorage,""),
  16073. BUILDIN_DEF(guildopenstorage,""),
  16074. BUILDIN_DEF(itemskill,"vi"),
  16075. BUILDIN_DEF(produce,"i"),
  16076. BUILDIN_DEF(cooking,"i"),
  16077. BUILDIN_DEF(monster,"siisii???"),
  16078. BUILDIN_DEF(getmobdrops,"i"),
  16079. BUILDIN_DEF(areamonster,"siiiisii???"),
  16080. BUILDIN_DEF(killmonster,"ss?"),
  16081. BUILDIN_DEF(killmonsterall,"s?"),
  16082. BUILDIN_DEF(clone,"siisi????"),
  16083. BUILDIN_DEF(doevent,"s"),
  16084. BUILDIN_DEF(donpcevent,"s"),
  16085. BUILDIN_DEF(cmdothernpc,"ss"),
  16086. BUILDIN_DEF(addtimer,"is"),
  16087. BUILDIN_DEF(deltimer,"s"),
  16088. BUILDIN_DEF(addtimercount,"is"),
  16089. BUILDIN_DEF(initnpctimer,"??"),
  16090. BUILDIN_DEF(stopnpctimer,"??"),
  16091. BUILDIN_DEF(startnpctimer,"??"),
  16092. BUILDIN_DEF(setnpctimer,"i?"),
  16093. BUILDIN_DEF(getnpctimer,"i?"),
  16094. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  16095. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  16096. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  16097. BUILDIN_DEF(announce,"si?????"),
  16098. BUILDIN_DEF(mapannounce,"ssi?????"),
  16099. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  16100. BUILDIN_DEF(getusers,"i"),
  16101. BUILDIN_DEF(getmapguildusers,"si"),
  16102. BUILDIN_DEF(getmapusers,"s"),
  16103. BUILDIN_DEF(getareausers,"siiii"),
  16104. BUILDIN_DEF(getareadropitem,"siiiiv"),
  16105. BUILDIN_DEF(enablenpc,"s"),
  16106. BUILDIN_DEF(disablenpc,"s"),
  16107. BUILDIN_DEF(hideoffnpc,"s"),
  16108. BUILDIN_DEF(hideonnpc,"s"),
  16109. BUILDIN_DEF(sc_start,"iii???"),
  16110. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  16111. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  16112. BUILDIN_DEF(sc_end,"i?"),
  16113. BUILDIN_DEF(getstatus, "i?"),
  16114. BUILDIN_DEF(getscrate,"ii?"),
  16115. BUILDIN_DEF(debugmes,"s"),
  16116. BUILDIN_DEF2(catchpet,"pet","i"),
  16117. BUILDIN_DEF2(birthpet,"bpet",""),
  16118. BUILDIN_DEF(resetlvl,"i"),
  16119. BUILDIN_DEF(resetstatus,""),
  16120. BUILDIN_DEF(resetskill,""),
  16121. BUILDIN_DEF(skillpointcount,""),
  16122. BUILDIN_DEF(changebase,"i?"),
  16123. BUILDIN_DEF(changesex,""),
  16124. BUILDIN_DEF(waitingroom,"si?????"),
  16125. BUILDIN_DEF(delwaitingroom,"?"),
  16126. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  16127. BUILDIN_DEF(enablewaitingroomevent,"?"),
  16128. BUILDIN_DEF(disablewaitingroomevent,"?"),
  16129. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  16130. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  16131. BUILDIN_DEF(getwaitingroomstate,"i?"),
  16132. BUILDIN_DEF(warpwaitingpc,"sii?"),
  16133. BUILDIN_DEF(attachrid,"i"),
  16134. BUILDIN_DEF(addrid,"i?????"),
  16135. BUILDIN_DEF(detachrid,""),
  16136. BUILDIN_DEF(isloggedin,"i?"),
  16137. BUILDIN_DEF(setmapflagnosave,"ssii"),
  16138. BUILDIN_DEF(getmapflag,"si?"),
  16139. BUILDIN_DEF(setmapflag,"si??"),
  16140. BUILDIN_DEF(removemapflag,"si?"),
  16141. BUILDIN_DEF(pvpon,"s"),
  16142. BUILDIN_DEF(pvpoff,"s"),
  16143. BUILDIN_DEF(gvgon,"s"),
  16144. BUILDIN_DEF(gvgoff,"s"),
  16145. BUILDIN_DEF(emotion,"i??"),
  16146. BUILDIN_DEF(maprespawnguildid,"sii"),
  16147. BUILDIN_DEF(agitstart,""), // <Agit>
  16148. BUILDIN_DEF(agitend,""),
  16149. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  16150. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  16151. BUILDIN_DEF(getcastlename,"s"),
  16152. BUILDIN_DEF(getcastledata,"si"),
  16153. BUILDIN_DEF(setcastledata,"sii"),
  16154. BUILDIN_DEF(requestguildinfo,"i?"),
  16155. BUILDIN_DEF(getequipcardcnt,"i"),
  16156. BUILDIN_DEF(successremovecards,"i"),
  16157. BUILDIN_DEF(failedremovecards,"ii"),
  16158. BUILDIN_DEF(marriage,"s"),
  16159. BUILDIN_DEF2(wedding_effect,"wedding",""),
  16160. BUILDIN_DEF(divorce,""),
  16161. BUILDIN_DEF(ispartneron,""),
  16162. BUILDIN_DEF(getpartnerid,""),
  16163. BUILDIN_DEF(getchildid,""),
  16164. BUILDIN_DEF(getmotherid,""),
  16165. BUILDIN_DEF(getfatherid,""),
  16166. BUILDIN_DEF(warppartner,"sii"),
  16167. BUILDIN_DEF(getitemname,"v"),
  16168. BUILDIN_DEF(getitemslots,"i"),
  16169. BUILDIN_DEF(makepet,"i"),
  16170. BUILDIN_DEF(getexp,"ii"),
  16171. BUILDIN_DEF(getinventorylist,""),
  16172. BUILDIN_DEF(getskilllist,""),
  16173. BUILDIN_DEF(clearitem,""),
  16174. BUILDIN_DEF(classchange,"ii"),
  16175. BUILDIN_DEF(misceffect,"i"),
  16176. BUILDIN_DEF(playBGM,"s"),
  16177. BUILDIN_DEF(playBGMall,"s?????"),
  16178. BUILDIN_DEF(soundeffect,"si"),
  16179. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  16180. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  16181. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  16182. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  16183. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  16184. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  16185. BUILDIN_DEF(petloot,"i"), // [Valaris]
  16186. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  16187. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  16188. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  16189. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  16190. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  16191. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  16192. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  16193. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  16194. BUILDIN_DEF(nude,""), // nude command [Valaris]
  16195. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  16196. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  16197. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  16198. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  16199. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  16200. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  16201. BUILDIN_DEF(mobcount,"ss"),
  16202. BUILDIN_DEF(getlook,"i"),
  16203. BUILDIN_DEF(getsavepoint,"i"),
  16204. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  16205. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  16206. BUILDIN_DEF(npcstop,""), // [Valaris]
  16207. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  16208. BUILDIN_DEF(checkoption1,"i"),
  16209. BUILDIN_DEF(checkoption2,"i"),
  16210. BUILDIN_DEF(guildgetexp,"i"),
  16211. BUILDIN_DEF(guildchangegm,"is"),
  16212. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  16213. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  16214. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  16215. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  16216. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  16217. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  16218. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  16219. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  16220. BUILDIN_DEF(night,""), // sets the server to night time
  16221. BUILDIN_DEF(day,""), // sets the server to day time
  16222. #ifdef PCRE_SUPPORT
  16223. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  16224. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  16225. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  16226. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  16227. #endif
  16228. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  16229. BUILDIN_DEF(getusersname,""),
  16230. BUILDIN_DEF(recovery,"i???"),
  16231. BUILDIN_DEF(getpetinfo,"i"),
  16232. BUILDIN_DEF(gethominfo,"i"),
  16233. BUILDIN_DEF(getmercinfo,"i?"),
  16234. BUILDIN_DEF(checkequipedcard,"i"),
  16235. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  16236. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  16237. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  16238. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  16239. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  16240. BUILDIN_DEF(charat,"si"),
  16241. BUILDIN_DEF(setchar,"ssi"),
  16242. BUILDIN_DEF(insertchar,"ssi"),
  16243. BUILDIN_DEF(delchar,"si"),
  16244. BUILDIN_DEF(strtoupper,"s"),
  16245. BUILDIN_DEF(strtolower,"s"),
  16246. BUILDIN_DEF(charisupper, "si"),
  16247. BUILDIN_DEF(charislower, "si"),
  16248. BUILDIN_DEF(substr,"sii"),
  16249. BUILDIN_DEF(explode, "rss"),
  16250. BUILDIN_DEF(implode, "r?"),
  16251. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  16252. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  16253. BUILDIN_DEF(strpos,"ss?"),
  16254. BUILDIN_DEF(replacestr,"sss??"),
  16255. BUILDIN_DEF(countstr,"ss?"),
  16256. BUILDIN_DEF(setnpcdisplay,"sv??"),
  16257. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  16258. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  16259. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  16260. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  16261. // [zBuffer] List of mathematics commands --->
  16262. BUILDIN_DEF(sqrt,"i"),
  16263. BUILDIN_DEF(pow,"ii"),
  16264. BUILDIN_DEF(distance,"iiii"),
  16265. // <--- [zBuffer] List of mathematics commands
  16266. BUILDIN_DEF(md5,"s"),
  16267. // [zBuffer] List of dynamic var commands --->
  16268. BUILDIN_DEF(getd,"s"),
  16269. BUILDIN_DEF(setd,"sv"),
  16270. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  16271. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  16272. BUILDIN_DEF(npcshopadditem,"sii*"),
  16273. BUILDIN_DEF(npcshopdelitem,"si*"),
  16274. BUILDIN_DEF(npcshopattach,"s?"),
  16275. BUILDIN_DEF(equip,"i"),
  16276. BUILDIN_DEF(autoequip,"ii"),
  16277. BUILDIN_DEF(setbattleflag,"si"),
  16278. BUILDIN_DEF(getbattleflag,"s"),
  16279. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  16280. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  16281. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  16282. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  16283. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  16284. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  16285. BUILDIN_DEF(axtoi,"s"),
  16286. BUILDIN_DEF(query_sql,"s*"),
  16287. BUILDIN_DEF(query_logsql,"s*"),
  16288. BUILDIN_DEF(escape_sql,"v"),
  16289. BUILDIN_DEF(atoi,"s"),
  16290. // [zBuffer] List of player cont commands --->
  16291. BUILDIN_DEF(rid2name,"i"),
  16292. BUILDIN_DEF(pcfollow,"ii"),
  16293. BUILDIN_DEF(pcstopfollow,"i"),
  16294. BUILDIN_DEF(pcblockmove,"ii"),
  16295. // <--- [zBuffer] List of player cont commands
  16296. // [zBuffer] List of mob control commands --->
  16297. BUILDIN_DEF(unitwalk,"ii?"),
  16298. BUILDIN_DEF(unitkill,"i"),
  16299. BUILDIN_DEF(unitwarp,"isii"),
  16300. BUILDIN_DEF(unitattack,"iv?"),
  16301. BUILDIN_DEF(unitstop,"i"),
  16302. BUILDIN_DEF(unittalk,"is"),
  16303. BUILDIN_DEF(unitemote,"ii"),
  16304. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  16305. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  16306. // <--- [zBuffer] List of mob control commands
  16307. BUILDIN_DEF(sleep,"i"),
  16308. BUILDIN_DEF(sleep2,"i"),
  16309. BUILDIN_DEF(awake,"s"),
  16310. BUILDIN_DEF(getvariableofnpc,"rs"),
  16311. BUILDIN_DEF(warpportal,"iisii"),
  16312. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  16313. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  16314. BUILDIN_DEF(morphembryo,""),
  16315. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  16316. BUILDIN_DEF(checkhomcall,""),
  16317. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  16318. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  16319. BUILDIN_DEF(checkvending,"?"),
  16320. BUILDIN_DEF(checkchatting,"?"),
  16321. BUILDIN_DEF(checkidle,"?"),
  16322. BUILDIN_DEF(openmail,""),
  16323. BUILDIN_DEF(openauction,""),
  16324. BUILDIN_DEF(checkcell,"siii"),
  16325. BUILDIN_DEF(setcell,"siiiiii"),
  16326. BUILDIN_DEF(setwall,"siiiiis"),
  16327. BUILDIN_DEF(delwall,"s"),
  16328. BUILDIN_DEF(searchitem,"rs"),
  16329. BUILDIN_DEF(mercenary_create,"ii"),
  16330. BUILDIN_DEF(mercenary_heal,"ii"),
  16331. BUILDIN_DEF(mercenary_sc_start,"iii"),
  16332. BUILDIN_DEF(mercenary_get_calls,"i"),
  16333. BUILDIN_DEF(mercenary_get_faith,"i"),
  16334. BUILDIN_DEF(mercenary_set_calls,"ii"),
  16335. BUILDIN_DEF(mercenary_set_faith,"ii"),
  16336. BUILDIN_DEF(readbook,"ii"),
  16337. BUILDIN_DEF(setfont,"i"),
  16338. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  16339. BUILDIN_DEF(progressbar,"si"),
  16340. BUILDIN_DEF(pushpc,"ii"),
  16341. BUILDIN_DEF(buyingstore,"i"),
  16342. BUILDIN_DEF(searchstores,"ii"),
  16343. BUILDIN_DEF(showdigit,"i?"),
  16344. // WoE SE
  16345. BUILDIN_DEF(agitstart2,""),
  16346. BUILDIN_DEF(agitend2,""),
  16347. BUILDIN_DEF(agitcheck2,""),
  16348. // BattleGround
  16349. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  16350. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  16351. BUILDIN_DEF(bg_team_setxy,"iii"),
  16352. BUILDIN_DEF(bg_warp,"isii"),
  16353. BUILDIN_DEF(bg_monster,"isiisi?"),
  16354. BUILDIN_DEF(bg_monster_set_team,"ii"),
  16355. BUILDIN_DEF(bg_leave,""),
  16356. BUILDIN_DEF(bg_destroy,"i"),
  16357. BUILDIN_DEF(areapercentheal,"siiiiii"),
  16358. BUILDIN_DEF(bg_get_data,"ii"),
  16359. BUILDIN_DEF(bg_getareausers,"isiiii"),
  16360. BUILDIN_DEF(bg_updatescore,"sii"),
  16361. // Instancing
  16362. BUILDIN_DEF(instance_create,"s"),
  16363. BUILDIN_DEF(instance_destroy,"?"),
  16364. BUILDIN_DEF(instance_id,""),
  16365. BUILDIN_DEF(instance_enter,"s"),
  16366. BUILDIN_DEF(instance_npcname,"s?"),
  16367. BUILDIN_DEF(instance_mapname,"s?"),
  16368. BUILDIN_DEF(instance_warpall,"sii?"),
  16369. BUILDIN_DEF(instance_announce,"isi?????"),
  16370. BUILDIN_DEF(instance_check_party,"i???"),
  16371. /**
  16372. * 3rd-related
  16373. **/
  16374. BUILDIN_DEF(makerune,"i"),
  16375. BUILDIN_DEF(checkdragon,""),//[Ind]
  16376. BUILDIN_DEF(setdragon,"?"),//[Ind]
  16377. BUILDIN_DEF(ismounting,""),//[Ind]
  16378. BUILDIN_DEF(setmounting,""),//[Ind]
  16379. BUILDIN_DEF(checkre,"i"),
  16380. /**
  16381. * rAthena and beyond!
  16382. **/
  16383. BUILDIN_DEF(getargcount,""),
  16384. BUILDIN_DEF(getcharip,"?"),
  16385. BUILDIN_DEF(is_function,"s"),
  16386. BUILDIN_DEF(get_revision,""),
  16387. BUILDIN_DEF(freeloop,"i"),
  16388. BUILDIN_DEF(getrandgroupitem,"ii?"),
  16389. BUILDIN_DEF(cleanmap,"s"),
  16390. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  16391. BUILDIN_DEF(npcskill,"viii"),
  16392. BUILDIN_DEF(consumeitem,"v"),
  16393. BUILDIN_DEF(delequip,"i"),
  16394. BUILDIN_DEF(breakequip,"i"),
  16395. BUILDIN_DEF(sit,"?"),
  16396. BUILDIN_DEF(stand,"?"),
  16397. //@commands (script based)
  16398. BUILDIN_DEF(bindatcmd, "ss??"),
  16399. BUILDIN_DEF(unbindatcmd, "s"),
  16400. BUILDIN_DEF(useatcmd, "s"),
  16401. //Quest Log System [Inkfish]
  16402. BUILDIN_DEF(setquest, "i"),
  16403. BUILDIN_DEF(erasequest, "i"),
  16404. BUILDIN_DEF(completequest, "i"),
  16405. BUILDIN_DEF(checkquest, "i?"),
  16406. BUILDIN_DEF(changequest, "ii"),
  16407. BUILDIN_DEF(showevent, "ii"),
  16408. //Bound items [Xantara] & [Akinari]
  16409. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  16410. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  16411. BUILDIN_DEF(countbound, "?"),
  16412. // Party related
  16413. BUILDIN_DEF(party_create,"s???"),
  16414. BUILDIN_DEF(party_addmember,"ii"),
  16415. BUILDIN_DEF(party_delmember,"??"),
  16416. BUILDIN_DEF(party_changeleader,"ii"),
  16417. BUILDIN_DEF(party_changeoption,"iii"),
  16418. BUILDIN_DEF(party_destroy,"i"),
  16419. BUILDIN_DEF(is_clientver,"ii?"),
  16420. BUILDIN_DEF(getserverdef,"i"),
  16421. BUILDIN_DEF2(montransform, "transform", "vii????"), // Monster Transform [malufett/Hercules]
  16422. BUILDIN_DEF(vip_status,"i?"),
  16423. BUILDIN_DEF(vip_time,"i?"),
  16424. BUILDIN_DEF(bonus_script,"si????"),
  16425. BUILDIN_DEF(getgroupitem,"i"),
  16426. #include "../custom/script_def.inc"
  16427. {NULL,NULL,NULL},
  16428. };