script.c 477 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843
  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. st->atcommand_enable_npc = battle_config.atcommand_enable_npc;
  2788. return st;
  2789. }
  2790. /// Frees a script state.
  2791. ///
  2792. /// @param st Script state
  2793. void script_free_state(struct script_state* st)
  2794. {
  2795. if(st->bk_st)
  2796. {// backup was not restored
  2797. 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);
  2798. }
  2799. if( st->sleep.timer != INVALID_TIMER )
  2800. delete_timer(st->sleep.timer, run_script_timer);
  2801. script_free_vars(st->stack->var_function);
  2802. pop_stack(st, 0, st->stack->sp);
  2803. aFree(st->stack->stack_data);
  2804. aFree(st->stack);
  2805. st->stack = NULL;
  2806. st->pos = -1;
  2807. aFree(st);
  2808. }
  2809. //
  2810. // Main execution unit
  2811. //
  2812. /*==========================================
  2813. * Read command
  2814. *------------------------------------------*/
  2815. c_op get_com(unsigned char *script,int *pos)
  2816. {
  2817. int i = 0, j = 0;
  2818. if(script[*pos]>=0x80){
  2819. return C_INT;
  2820. }
  2821. while(script[*pos]>=0x40){
  2822. i=script[(*pos)++]<<j;
  2823. j+=6;
  2824. }
  2825. return (c_op)(i+(script[(*pos)++]<<j));
  2826. }
  2827. /*==========================================
  2828. * Income figures
  2829. *------------------------------------------*/
  2830. int get_num(unsigned char *script,int *pos)
  2831. {
  2832. int i,j;
  2833. i=0; j=0;
  2834. while(script[*pos]>=0xc0){
  2835. i+=(script[(*pos)++]&0x7f)<<j;
  2836. j+=6;
  2837. }
  2838. return i+((script[(*pos)++]&0x7f)<<j);
  2839. }
  2840. /*==========================================
  2841. * Remove the value from the stack
  2842. *------------------------------------------*/
  2843. int pop_val(struct script_state* st)
  2844. {
  2845. if(st->stack->sp<=0)
  2846. return 0;
  2847. st->stack->sp--;
  2848. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2849. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2850. return st->stack->stack_data[st->stack->sp].u.num;
  2851. return 0;
  2852. }
  2853. /// Ternary operators
  2854. /// test ? if_true : if_false
  2855. void op_3(struct script_state* st, int op)
  2856. {
  2857. struct script_data* data;
  2858. int flag = 0;
  2859. data = script_getdatatop(st, -3);
  2860. get_val(st, data);
  2861. if( data_isstring(data) )
  2862. flag = data->u.str[0];// "" -> false
  2863. else if( data_isint(data) )
  2864. flag = data->u.num;// 0 -> false
  2865. else
  2866. {
  2867. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2868. script_reportdata(data);
  2869. script_reportsrc(st);
  2870. script_removetop(st, -3, 0);
  2871. script_pushnil(st);
  2872. return;
  2873. }
  2874. if( flag )
  2875. script_pushcopytop(st, -2);
  2876. else
  2877. script_pushcopytop(st, -1);
  2878. script_removetop(st, -4, -1);
  2879. }
  2880. /// Binary string operators
  2881. /// s1 EQ s2 -> i
  2882. /// s1 NE s2 -> i
  2883. /// s1 GT s2 -> i
  2884. /// s1 GE s2 -> i
  2885. /// s1 LT s2 -> i
  2886. /// s1 LE s2 -> i
  2887. /// s1 ADD s2 -> s
  2888. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2889. {
  2890. int a = 0;
  2891. switch(op){
  2892. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2893. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2894. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2895. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2896. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2897. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2898. case C_ADD:
  2899. {
  2900. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2901. strcpy(buf, s1);
  2902. strcat(buf, s2);
  2903. script_pushstr(st, buf);
  2904. return;
  2905. }
  2906. default:
  2907. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2908. script_reportsrc(st);
  2909. script_pushnil(st);
  2910. st->state = END;
  2911. return;
  2912. }
  2913. script_pushint(st,a);
  2914. }
  2915. /// Binary number operators
  2916. /// i OP i -> i
  2917. void op_2num(struct script_state* st, int op, int i1, int i2)
  2918. {
  2919. int ret;
  2920. double ret_double;
  2921. switch( op )
  2922. {
  2923. case C_AND: ret = i1 & i2; break;
  2924. case C_OR: ret = i1 | i2; break;
  2925. case C_XOR: ret = i1 ^ i2; break;
  2926. case C_LAND: ret = (i1 && i2); break;
  2927. case C_LOR: ret = (i1 || i2); break;
  2928. case C_EQ: ret = (i1 == i2); break;
  2929. case C_NE: ret = (i1 != i2); break;
  2930. case C_GT: ret = (i1 > i2); break;
  2931. case C_GE: ret = (i1 >= i2); break;
  2932. case C_LT: ret = (i1 < i2); break;
  2933. case C_LE: ret = (i1 <= i2); break;
  2934. case C_R_SHIFT: ret = i1>>i2; break;
  2935. case C_L_SHIFT: ret = i1<<i2; break;
  2936. case C_DIV:
  2937. case C_MOD:
  2938. if( i2 == 0 )
  2939. {
  2940. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2941. script_reportsrc(st);
  2942. script_pushnil(st);
  2943. st->state = END;
  2944. return;
  2945. }
  2946. else if( op == C_DIV )
  2947. ret = i1 / i2;
  2948. else//if( op == C_MOD )
  2949. ret = i1 % i2;
  2950. break;
  2951. default:
  2952. switch( op )
  2953. {// operators that can overflow/underflow
  2954. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2955. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2956. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2957. default:
  2958. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2959. script_reportsrc(st);
  2960. script_pushnil(st);
  2961. return;
  2962. }
  2963. if( ret_double < (double)INT_MIN )
  2964. {
  2965. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2966. script_reportsrc(st);
  2967. ret = INT_MIN;
  2968. }
  2969. else if( ret_double > (double)INT_MAX )
  2970. {
  2971. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2972. script_reportsrc(st);
  2973. ret = INT_MAX;
  2974. }
  2975. }
  2976. script_pushint(st, ret);
  2977. }
  2978. /// Binary operators
  2979. void op_2(struct script_state *st, int op)
  2980. {
  2981. struct script_data* left, leftref;
  2982. struct script_data* right;
  2983. leftref.type = C_NOP;
  2984. left = script_getdatatop(st, -2);
  2985. right = script_getdatatop(st, -1);
  2986. if (st->op2ref) {
  2987. if (data_isreference(left)) {
  2988. leftref = *left;
  2989. }
  2990. st->op2ref = 0;
  2991. }
  2992. get_val(st, left);
  2993. get_val(st, right);
  2994. // automatic conversions
  2995. switch( op )
  2996. {
  2997. case C_ADD:
  2998. if( data_isint(left) && data_isstring(right) )
  2999. {// convert int-string to string-string
  3000. conv_str(st, left);
  3001. }
  3002. else if( data_isstring(left) && data_isint(right) )
  3003. {// convert string-int to string-string
  3004. conv_str(st, right);
  3005. }
  3006. break;
  3007. }
  3008. if( data_isstring(left) && data_isstring(right) )
  3009. {// ss => op_2str
  3010. op_2str(st, op, left->u.str, right->u.str);
  3011. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3012. if (leftref.type != C_NOP)
  3013. {
  3014. if (left->type == C_STR) // don't free C_CONSTSTR
  3015. aFree(left->u.str);
  3016. *left = leftref;
  3017. }
  3018. }
  3019. else if( data_isint(left) && data_isint(right) )
  3020. {// ii => op_2num
  3021. int i1 = left->u.num;
  3022. int i2 = right->u.num;
  3023. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3024. op_2num(st, op, i1, i2);
  3025. if (leftref.type != C_NOP)
  3026. *left = leftref;
  3027. }
  3028. else
  3029. {// invalid argument
  3030. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3031. script_reportdata(left);
  3032. script_reportdata(right);
  3033. script_reportsrc(st);
  3034. script_removetop(st, -2, 0);
  3035. script_pushnil(st);
  3036. st->state = END;
  3037. }
  3038. }
  3039. /// Unary operators
  3040. /// NEG i -> i
  3041. /// NOT i -> i
  3042. /// LNOT i -> i
  3043. void op_1(struct script_state* st, int op)
  3044. {
  3045. struct script_data* data;
  3046. int i1;
  3047. data = script_getdatatop(st, -1);
  3048. get_val(st, data);
  3049. if( !data_isint(data) )
  3050. {// not a number
  3051. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3052. script_reportdata(data);
  3053. script_reportsrc(st);
  3054. script_pushnil(st);
  3055. st->state = END;
  3056. return;
  3057. }
  3058. i1 = data->u.num;
  3059. script_removetop(st, -1, 0);
  3060. switch( op )
  3061. {
  3062. case C_NEG: i1 = -i1; break;
  3063. case C_NOT: i1 = ~i1; break;
  3064. case C_LNOT: i1 = !i1; break;
  3065. default:
  3066. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3067. script_reportsrc(st);
  3068. script_pushnil(st);
  3069. st->state = END;
  3070. return;
  3071. }
  3072. script_pushint(st, i1);
  3073. }
  3074. /// Checks the type of all arguments passed to a built-in function.
  3075. ///
  3076. /// @param st Script state whose stack arguments should be inspected.
  3077. /// @param func Built-in function for which the arguments are intended.
  3078. static void script_check_buildin_argtype(struct script_state* st, int func)
  3079. {
  3080. char type;
  3081. int idx, invalid = 0;
  3082. script_function* sf = &buildin_func[str_data[func].val];
  3083. for( idx = 2; script_hasdata(st, idx); idx++ )
  3084. {
  3085. struct script_data* data = script_getdata(st, idx);
  3086. type = sf->arg[idx-2];
  3087. if( type == '?' || type == '*' )
  3088. {// optional argument or unknown number of optional parameters ( no types are after this )
  3089. break;
  3090. }
  3091. else if( type == 0 )
  3092. {// more arguments than necessary ( should not happen, as it is checked before )
  3093. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3094. invalid++;
  3095. break;
  3096. }
  3097. else
  3098. {
  3099. const char* name = NULL;
  3100. if( data_isreference(data) )
  3101. {// get name for variables to determine the type they refer to
  3102. name = reference_getname(data);
  3103. }
  3104. switch( type )
  3105. {
  3106. case 'v':
  3107. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3108. {// variant
  3109. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3110. script_reportdata(data);
  3111. invalid++;
  3112. }
  3113. break;
  3114. case 's':
  3115. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3116. {// string
  3117. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3118. script_reportdata(data);
  3119. invalid++;
  3120. }
  3121. break;
  3122. case 'i':
  3123. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3124. {// int ( params and constants are always int )
  3125. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3126. script_reportdata(data);
  3127. invalid++;
  3128. }
  3129. break;
  3130. case 'r':
  3131. if( !data_isreference(data) )
  3132. {// variables
  3133. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3134. script_reportdata(data);
  3135. invalid++;
  3136. }
  3137. break;
  3138. case 'l':
  3139. if( !data_islabel(data) && !data_isfunclabel(data) )
  3140. {// label
  3141. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3142. script_reportdata(data);
  3143. invalid++;
  3144. }
  3145. break;
  3146. }
  3147. }
  3148. }
  3149. if(invalid)
  3150. {
  3151. ShowDebug("Function: %s\n", get_str(func));
  3152. script_reportsrc(st);
  3153. }
  3154. }
  3155. /// Executes a buildin command.
  3156. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3157. int run_func(struct script_state *st)
  3158. {
  3159. struct script_data* data;
  3160. int i,start_sp,end_sp,func;
  3161. end_sp = st->stack->sp;// position after the last argument
  3162. for( i = end_sp-1; i > 0 ; --i )
  3163. if( st->stack->stack_data[i].type == C_ARG )
  3164. break;
  3165. if( i == 0 ) {
  3166. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3167. st->state = END;
  3168. script_reportsrc(st);
  3169. return 1;
  3170. }
  3171. start_sp = i-1;// C_NAME of the command
  3172. st->start = start_sp;
  3173. st->end = end_sp;
  3174. data = &st->stack->stack_data[st->start];
  3175. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3176. func = data->u.num;
  3177. st->funcname = reference_getname(data);
  3178. } else {
  3179. ShowError("script:run_func: not a buildin command.\n");
  3180. script_reportdata(data);
  3181. script_reportsrc(st);
  3182. st->state = END;
  3183. return 1;
  3184. }
  3185. if( script_config.warn_func_mismatch_argtypes ) {
  3186. script_check_buildin_argtype(st, func);
  3187. }
  3188. if(str_data[func].func) {
  3189. if (str_data[func].func(st)) //Report error
  3190. script_reportsrc(st);
  3191. } else {
  3192. 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));
  3193. script_reportsrc(st);
  3194. st->state = END;
  3195. }
  3196. // Stack's datum are used when re-running functions [Eoe]
  3197. if( st->state == RERUNLINE )
  3198. return 0;
  3199. pop_stack(st, st->start, st->end);
  3200. if( st->state == RETFUNC ) {// return from a user-defined function
  3201. struct script_retinfo* ri;
  3202. int olddefsp = st->stack->defsp;
  3203. int nargs;
  3204. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3205. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3206. {
  3207. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3208. script_reportsrc(st);
  3209. st->state = END;
  3210. return 1;
  3211. }
  3212. script_free_vars( st->stack->var_function );
  3213. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3214. nargs = ri->nargs;
  3215. st->pos = ri->pos;
  3216. st->script = ri->script;
  3217. st->stack->var_function = ri->var_function;
  3218. st->stack->defsp = ri->defsp;
  3219. memset(ri, 0, sizeof(struct script_retinfo));
  3220. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3221. st->state = GOTO;
  3222. }
  3223. return 0;
  3224. }
  3225. /*==========================================
  3226. * script execution
  3227. *------------------------------------------*/
  3228. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3229. {
  3230. struct script_state *st;
  3231. if( rootscript == NULL || pos < 0 )
  3232. return;
  3233. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3234. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3235. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3236. st = script_alloc_state(rootscript, pos, rid, oid);
  3237. run_script_main(st);
  3238. }
  3239. void script_stop_sleeptimers(int id)
  3240. {
  3241. struct script_state* st;
  3242. for(;;)
  3243. {
  3244. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3245. if( st == NULL )
  3246. break; // no more sleep timers
  3247. script_free_state(st);
  3248. }
  3249. }
  3250. /*==========================================
  3251. * Delete the specified node from sleep_db
  3252. *------------------------------------------*/
  3253. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3254. {
  3255. struct linkdb_node *retnode;
  3256. if( n == NULL)
  3257. return NULL;
  3258. if( n->prev == NULL )
  3259. sleep_db = n->next;
  3260. else
  3261. n->prev->next = n->next;
  3262. if( n->next )
  3263. n->next->prev = n->prev;
  3264. retnode = n->next;
  3265. aFree( n );
  3266. return retnode; // The following; return retnode
  3267. }
  3268. /*==========================================
  3269. * Timer function for sleep
  3270. *------------------------------------------*/
  3271. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3272. {
  3273. struct script_state *st = (struct script_state *)data;
  3274. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3275. TBL_PC *sd = map_id2sd(st->rid);
  3276. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3277. { //Character mismatch. Cancel execution.
  3278. st->rid = 0;
  3279. st->state = END;
  3280. }
  3281. while( node && st->sleep.timer != INVALID_TIMER ) {
  3282. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3283. script_erase_sleepdb(node);
  3284. st->sleep.timer = INVALID_TIMER;
  3285. break;
  3286. }
  3287. node = node->next;
  3288. }
  3289. if(st->state != RERUNLINE)
  3290. st->sleep.tick = 0;
  3291. run_script_main(st);
  3292. return 0;
  3293. }
  3294. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3295. ///
  3296. /// @param st Script state to detach.
  3297. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3298. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3299. {
  3300. struct map_session_data* sd;
  3301. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3302. sd->st = st->bk_st;
  3303. sd->npc_id = st->bk_npcid;
  3304. if(st->bk_st) {
  3305. //Remove tag for removal.
  3306. st->bk_st = NULL;
  3307. st->bk_npcid = 0;
  3308. } else if(dequeue_event) {
  3309. #ifdef SECURE_NPCTIMEOUT
  3310. /**
  3311. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3312. **/
  3313. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3314. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3315. sd->npc_idle_timer = INVALID_TIMER;
  3316. }
  3317. #endif
  3318. npc_event_dequeue(sd);
  3319. }
  3320. }
  3321. else if(st->bk_st)
  3322. {// rid was set to 0, before detaching the script state
  3323. 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);
  3324. script_reportsrc(st->bk_st);
  3325. script_free_state(st->bk_st);
  3326. st->bk_st = NULL;
  3327. }
  3328. }
  3329. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3330. ///
  3331. /// @param st Script state to attach.
  3332. static void script_attach_state(struct script_state* st)
  3333. {
  3334. struct map_session_data* sd;
  3335. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3336. {
  3337. if(st!=sd->st)
  3338. {
  3339. if(st->bk_st)
  3340. {// there is already a backup
  3341. 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);
  3342. }
  3343. st->bk_st = sd->st;
  3344. st->bk_npcid = sd->npc_id;
  3345. }
  3346. sd->st = st;
  3347. sd->npc_id = st->oid;
  3348. sd->npc_item_flag = st->npc_item_flag; // load default.
  3349. sd->state.disable_atcommand_on_npc = (pc_get_group_level(sd) >= st->atcommand_enable_npc) ? false : true;
  3350. #ifdef SECURE_NPCTIMEOUT
  3351. if( sd->npc_idle_timer == INVALID_TIMER )
  3352. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3353. sd->npc_idle_tick = gettick();
  3354. #endif
  3355. }
  3356. }
  3357. /*==========================================
  3358. * The main part of the script execution
  3359. *------------------------------------------*/
  3360. void run_script_main(struct script_state *st)
  3361. {
  3362. int cmdcount = script_config.check_cmdcount;
  3363. int gotocount = script_config.check_gotocount;
  3364. TBL_PC *sd;
  3365. struct script_stack *stack=st->stack;
  3366. script_attach_state(st);
  3367. if(st->state == RERUNLINE) {
  3368. run_func(st);
  3369. if(st->state == GOTO)
  3370. st->state = RUN;
  3371. } else if(st->state != END)
  3372. st->state = RUN;
  3373. while(st->state == RUN)
  3374. {
  3375. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3376. switch(c){
  3377. case C_EOL:
  3378. if( stack->defsp > stack->sp )
  3379. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3380. else
  3381. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3382. break;
  3383. case C_INT:
  3384. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3385. break;
  3386. case C_POS:
  3387. case C_NAME:
  3388. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3389. st->pos+=3;
  3390. break;
  3391. case C_ARG:
  3392. push_val(stack,c,0);
  3393. break;
  3394. case C_STR:
  3395. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3396. while(st->script->script_buf[st->pos++]);
  3397. break;
  3398. case C_FUNC:
  3399. run_func(st);
  3400. if(st->state==GOTO){
  3401. st->state = RUN;
  3402. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3403. ShowError("run_script: infinity loop !\n");
  3404. script_reportsrc(st);
  3405. st->state=END;
  3406. }
  3407. }
  3408. break;
  3409. case C_REF:
  3410. st->op2ref = 1;
  3411. break;
  3412. case C_NEG:
  3413. case C_NOT:
  3414. case C_LNOT:
  3415. op_1(st ,c);
  3416. break;
  3417. case C_ADD:
  3418. case C_SUB:
  3419. case C_MUL:
  3420. case C_DIV:
  3421. case C_MOD:
  3422. case C_EQ:
  3423. case C_NE:
  3424. case C_GT:
  3425. case C_GE:
  3426. case C_LT:
  3427. case C_LE:
  3428. case C_AND:
  3429. case C_OR:
  3430. case C_XOR:
  3431. case C_LAND:
  3432. case C_LOR:
  3433. case C_R_SHIFT:
  3434. case C_L_SHIFT:
  3435. op_2(st, c);
  3436. break;
  3437. case C_OP3:
  3438. op_3(st, c);
  3439. break;
  3440. case C_NOP:
  3441. st->state=END;
  3442. break;
  3443. default:
  3444. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3445. st->state=END;
  3446. break;
  3447. }
  3448. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3449. ShowError("run_script: infinity loop !\n");
  3450. script_reportsrc(st);
  3451. st->state=END;
  3452. }
  3453. }
  3454. if(st->sleep.tick > 0) {
  3455. //Restore previous script
  3456. script_detach_state(st, false);
  3457. //Delay execution
  3458. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3459. st->sleep.charid = sd?sd->status.char_id:0;
  3460. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3461. run_script_timer, st->sleep.charid, (intptr_t)st);
  3462. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3463. }
  3464. else if(st->state != END && st->rid){
  3465. //Resume later (st is already attached to player).
  3466. if(st->bk_st) {
  3467. ShowWarning("Unable to restore stack! Double continuation!\n");
  3468. //Report BOTH scripts to see if that can help somehow.
  3469. ShowDebug("Previous script (lost):\n");
  3470. script_reportsrc(st->bk_st);
  3471. ShowDebug("Current script:\n");
  3472. script_reportsrc(st);
  3473. script_free_state(st->bk_st);
  3474. st->bk_st = NULL;
  3475. }
  3476. } else {
  3477. //Dispose of script.
  3478. if ((sd = map_id2sd(st->rid))!=NULL)
  3479. { //Restore previous stack and save char.
  3480. if(sd->state.using_fake_npc){
  3481. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3482. sd->state.using_fake_npc = 0;
  3483. }
  3484. //Restore previous script if any.
  3485. script_detach_state(st, true);
  3486. if (sd->state.reg_dirty&2)
  3487. intif_saveregistry(sd,2);
  3488. if (sd->state.reg_dirty&1)
  3489. intif_saveregistry(sd,1);
  3490. }
  3491. script_free_state(st);
  3492. st = NULL;
  3493. }
  3494. }
  3495. int script_config_read(char *cfgName)
  3496. {
  3497. int i;
  3498. char line[1024],w1[1024],w2[1024];
  3499. FILE *fp;
  3500. fp=fopen(cfgName,"r");
  3501. if(fp==NULL){
  3502. ShowError("File not found: %s\n", cfgName);
  3503. return 1;
  3504. }
  3505. while(fgets(line, sizeof(line), fp))
  3506. {
  3507. if(line[0] == '/' && line[1] == '/')
  3508. continue;
  3509. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3510. if(i!=2)
  3511. continue;
  3512. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3513. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3514. }
  3515. else if(strcmpi(w1,"check_cmdcount")==0) {
  3516. script_config.check_cmdcount = config_switch(w2);
  3517. }
  3518. else if(strcmpi(w1,"check_gotocount")==0) {
  3519. script_config.check_gotocount = config_switch(w2);
  3520. }
  3521. else if(strcmpi(w1,"input_min_value")==0) {
  3522. script_config.input_min_value = config_switch(w2);
  3523. }
  3524. else if(strcmpi(w1,"input_max_value")==0) {
  3525. script_config.input_max_value = config_switch(w2);
  3526. }
  3527. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3528. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3529. }
  3530. else if(strcmpi(w1,"import")==0){
  3531. script_config_read(w2);
  3532. }
  3533. else {
  3534. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3535. }
  3536. }
  3537. fclose(fp);
  3538. return 0;
  3539. }
  3540. /**
  3541. * @see DBApply
  3542. */
  3543. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3544. {
  3545. struct script_code *code = db_data2ptr(data);
  3546. if (code)
  3547. script_free_code(code);
  3548. return 0;
  3549. }
  3550. void script_run_autobonus(const char *autobonus, int id, int pos)
  3551. {
  3552. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3553. if( script )
  3554. {
  3555. current_equip_item_index = pos;
  3556. run_script(script,0,id,0);
  3557. }
  3558. }
  3559. void script_add_autobonus(const char *autobonus)
  3560. {
  3561. if( strdb_get(autobonus_db, autobonus) == NULL )
  3562. {
  3563. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3564. if( script )
  3565. strdb_put(autobonus_db, autobonus, script);
  3566. }
  3567. }
  3568. /// resets a temporary character array variable to given value
  3569. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3570. {
  3571. int key;
  3572. uint8 idx;
  3573. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3574. {
  3575. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3576. return;
  3577. }
  3578. key = add_str(varname);
  3579. if( is_string_variable(varname) )
  3580. {
  3581. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3582. {
  3583. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3584. }
  3585. }
  3586. else
  3587. {
  3588. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3589. {
  3590. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3591. }
  3592. }
  3593. }
  3594. /// sets a temporary character array variable element idx to given value
  3595. /// @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.
  3596. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3597. {
  3598. int key;
  3599. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3600. {
  3601. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3602. return;
  3603. }
  3604. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3605. {
  3606. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3607. return;
  3608. }
  3609. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3610. if( is_string_variable(varname) )
  3611. {
  3612. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3613. }
  3614. else
  3615. {
  3616. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3617. }
  3618. if( refcache )
  3619. {// save to avoid repeated add_str calls
  3620. refcache[0] = key;
  3621. }
  3622. }
  3623. #ifdef BETA_THREAD_TEST
  3624. int buildin_query_sql_sub(struct script_state* st, Sql* handle);
  3625. /* used to receive items the queryThread has already processed */
  3626. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3627. int i, cursor = 0;
  3628. bool allOk = true;
  3629. EnterSpinLock(&queryThreadLock);
  3630. for( i = 0; i < queryThreadData.count; i++ ) {
  3631. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3632. if( !entry->ok ) {
  3633. allOk = false;
  3634. continue;
  3635. }
  3636. run_script_main(entry->st);
  3637. entry->st = NULL;/* empty entries */
  3638. aFree(entry);
  3639. queryThreadData.entry[i] = NULL;
  3640. }
  3641. if( allOk ) {
  3642. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3643. delete_timer(queryThreadData.timer, queryThread_timer);
  3644. queryThreadData.timer = INVALID_TIMER;
  3645. }
  3646. /* now lets clear the mess. */
  3647. for( i = 0; i < queryThreadData.count; i++ ) {
  3648. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3649. if( entry == NULL )
  3650. continue;/* entry on hold */
  3651. /* move */
  3652. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3653. cursor++;
  3654. }
  3655. queryThreadData.count = cursor;
  3656. LeaveSpinLock(&queryThreadLock);
  3657. return 0;
  3658. }
  3659. void queryThread_add(struct script_state *st, bool type) {
  3660. int idx = 0;
  3661. struct queryThreadEntry* entry = NULL;
  3662. EnterSpinLock(&queryThreadLock);
  3663. if( queryThreadData.count++ != 0 )
  3664. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3665. idx = queryThreadData.count-1;
  3666. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3667. entry = queryThreadData.entry[idx];
  3668. entry->st = st;
  3669. entry->ok = false;
  3670. entry->type = type;
  3671. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3672. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3673. }
  3674. LeaveSpinLock(&queryThreadLock);
  3675. /* unlock the queryThread */
  3676. racond_signal(queryThreadCond);
  3677. }
  3678. /* adds a new log to the queue */
  3679. void queryThread_log(char * entry, int length) {
  3680. int idx = logThreadData.count;
  3681. EnterSpinLock(&queryThreadLock);
  3682. if( logThreadData.count++ != 0 )
  3683. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3684. CREATE(logThreadData.entry[idx], char, length + 1 );
  3685. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3686. LeaveSpinLock(&queryThreadLock);
  3687. /* unlock the queryThread */
  3688. racond_signal(queryThreadCond);
  3689. }
  3690. /* queryThread_main */
  3691. static void *queryThread_main(void *x) {
  3692. Sql *queryThread_handle = Sql_Malloc();
  3693. int i;
  3694. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) )
  3695. exit(EXIT_FAILURE);
  3696. if( strlen(default_codepage) > 0 )
  3697. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3698. Sql_ShowDebug(queryThread_handle);
  3699. if( log_config.sql_logs ) {
  3700. logmysql_handle = Sql_Malloc();
  3701. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) )
  3702. exit(EXIT_FAILURE);
  3703. if( strlen(default_codepage) > 0 )
  3704. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3705. Sql_ShowDebug(logmysql_handle);
  3706. }
  3707. while( 1 ) {
  3708. if(queryThreadTerminate > 0)
  3709. break;
  3710. EnterSpinLock(&queryThreadLock);
  3711. /* mess with queryThreadData within the lock */
  3712. for( i = 0; i < queryThreadData.count; i++ ) {
  3713. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3714. if( entry->ok )
  3715. continue;
  3716. else if ( !entry->st || !entry->st->stack ) {
  3717. entry->ok = true;/* dispose */
  3718. continue;
  3719. }
  3720. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  3721. entry->ok = true;/* we're done with this */
  3722. }
  3723. /* also check for any logs in need to be sent */
  3724. if( log_config.sql_logs ) {
  3725. for( i = 0; i < logThreadData.count; i++ ) {
  3726. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  3727. Sql_ShowDebug(logmysql_handle);
  3728. aFree(logThreadData.entry[i]);
  3729. }
  3730. logThreadData.count = 0;
  3731. }
  3732. LeaveSpinLock(&queryThreadLock);
  3733. ramutex_lock( queryThreadMutex );
  3734. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  3735. ramutex_unlock( queryThreadMutex );
  3736. }
  3737. Sql_Free(queryThread_handle);
  3738. if( log_config.sql_logs ) {
  3739. Sql_Free(logmysql_handle);
  3740. }
  3741. return NULL;
  3742. }
  3743. #endif
  3744. /*==========================================
  3745. * Destructor
  3746. *------------------------------------------*/
  3747. int do_final_script() {
  3748. int i;
  3749. #ifdef DEBUG_HASH
  3750. if (battle_config.etc_log)
  3751. {
  3752. FILE *fp = fopen("hash_dump.txt","wt");
  3753. if(fp) {
  3754. int count[SCRIPT_HASH_SIZE];
  3755. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3756. int n=0;
  3757. int min=INT_MAX,max=0,zero=0;
  3758. double mean=0.0f;
  3759. double median=0.0f;
  3760. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3761. memset(count, 0, sizeof(count));
  3762. fprintf(fp,"num : hash : data_name\n");
  3763. fprintf(fp,"---------------------------------------------------------------\n");
  3764. for(i=LABEL_START; i<str_num; i++) {
  3765. unsigned int h = calc_hash(get_str(i));
  3766. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3767. ++count[h];
  3768. }
  3769. fprintf(fp,"--------------------\n\n");
  3770. memset(count2, 0, sizeof(count2));
  3771. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3772. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3773. if(min > count[i])
  3774. min = count[i]; // minimun count of collision
  3775. if(max < count[i])
  3776. max = count[i]; // maximun count of collision
  3777. if(count[i] == 0)
  3778. zero++;
  3779. ++count2[count[i]];
  3780. }
  3781. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3782. for( i=min; i <= max; ++i ){
  3783. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3784. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3785. }
  3786. for( i=min; i <= max; ++i ){
  3787. n += count2[i];
  3788. if( n*2 >= SCRIPT_HASH_SIZE )
  3789. {
  3790. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3791. median = (i+i+1)/2.0f;
  3792. else
  3793. median = i;
  3794. break;
  3795. }
  3796. }
  3797. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3798. fclose(fp);
  3799. }
  3800. }
  3801. #endif
  3802. mapreg_final();
  3803. db_destroy(scriptlabel_db);
  3804. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3805. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3806. if(sleep_db) {
  3807. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3808. while(n) {
  3809. struct script_state *st = (struct script_state *)n->data;
  3810. script_free_state(st);
  3811. n = n->next;
  3812. }
  3813. linkdb_final(&sleep_db);
  3814. }
  3815. if (str_data)
  3816. aFree(str_data);
  3817. if (str_buf)
  3818. aFree(str_buf);
  3819. for( i = 0; i < atcmd_binding_count; i++ ) {
  3820. aFree(atcmd_binding[i]);
  3821. }
  3822. if( atcmd_binding_count != 0 )
  3823. aFree(atcmd_binding);
  3824. #ifdef BETA_THREAD_TEST
  3825. /* QueryThread */
  3826. InterlockedIncrement(&queryThreadTerminate);
  3827. racond_signal(queryThreadCond);
  3828. rathread_wait(queryThread, NULL);
  3829. // Destroy cond var and mutex.
  3830. racond_destroy( queryThreadCond );
  3831. ramutex_destroy( queryThreadMutex );
  3832. /* Clear missing vars */
  3833. for( i = 0; i < queryThreadData.count; i++ ) {
  3834. aFree(queryThreadData.entry[i]);
  3835. }
  3836. aFree(queryThreadData.entry);
  3837. for( i = 0; i < logThreadData.count; i++ ) {
  3838. aFree(logThreadData.entry[i]);
  3839. }
  3840. aFree(logThreadData.entry);
  3841. #endif
  3842. return 0;
  3843. }
  3844. /*==========================================
  3845. * Initialization
  3846. *------------------------------------------*/
  3847. int do_init_script() {
  3848. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3849. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3850. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3851. mapreg_init();
  3852. #ifdef BETA_THREAD_TEST
  3853. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  3854. queryThreadData.count = 0;
  3855. CREATE(logThreadData.entry, char *, 1);
  3856. logThreadData.count = 0;
  3857. /* QueryThread Start */
  3858. InitializeSpinLock(&queryThreadLock);
  3859. queryThreadData.timer = INVALID_TIMER;
  3860. queryThreadTerminate = 0;
  3861. queryThreadMutex = ramutex_create();
  3862. queryThreadCond = racond_create();
  3863. queryThread = rathread_create(queryThread_main, NULL);
  3864. if(queryThread == NULL){
  3865. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  3866. exit(EXIT_FAILURE);
  3867. }
  3868. add_timer_func_list(queryThread_timer, "queryThread_timer");
  3869. #endif
  3870. return 0;
  3871. }
  3872. int script_reload() {
  3873. int i;
  3874. #ifdef BETA_THREAD_TEST
  3875. /* we're reloading so any queries undergoing should be...exterminated. */
  3876. EnterSpinLock(&queryThreadLock);
  3877. for( i = 0; i < queryThreadData.count; i++ ) {
  3878. aFree(queryThreadData.entry[i]);
  3879. }
  3880. queryThreadData.count = 0;
  3881. if( queryThreadData.timer != INVALID_TIMER ) {
  3882. delete_timer(queryThreadData.timer, queryThread_timer);
  3883. queryThreadData.timer = INVALID_TIMER;
  3884. }
  3885. LeaveSpinLock(&queryThreadLock);
  3886. #endif
  3887. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  3888. db_clear(scriptlabel_db);
  3889. // @commands (script based)
  3890. // Clear bindings
  3891. for( i = 0; i < atcmd_binding_count; i++ ) {
  3892. aFree(atcmd_binding[i]);
  3893. }
  3894. if( atcmd_binding_count != 0 )
  3895. aFree(atcmd_binding);
  3896. atcmd_binding_count = 0;
  3897. if(sleep_db) {
  3898. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3899. while(n) {
  3900. struct script_state *st = (struct script_state *)n->data;
  3901. script_free_state(st);
  3902. n = n->next;
  3903. }
  3904. linkdb_final(&sleep_db);
  3905. }
  3906. mapreg_reload();
  3907. return 0;
  3908. }
  3909. //-----------------------------------------------------------------------------
  3910. // buildin functions
  3911. //
  3912. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3913. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3914. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3915. /////////////////////////////////////////////////////////////////////
  3916. // NPC interaction
  3917. //
  3918. /// Appends a message to the npc dialog.
  3919. /// If a dialog doesn't exist yet, one is created.
  3920. ///
  3921. /// mes "<message>";
  3922. BUILDIN_FUNC(mes)
  3923. {
  3924. TBL_PC* sd = script_rid2sd(st);
  3925. if( sd == NULL )
  3926. return 0;
  3927. if( !script_hasdata(st, 3) )
  3928. {// only a single line detected in the script
  3929. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3930. }
  3931. else
  3932. {// parse multiple lines as they exist
  3933. int i;
  3934. for( i = 2; script_hasdata(st, i); i++ )
  3935. {
  3936. // send the message to the client
  3937. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3938. }
  3939. }
  3940. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  3941. return SCRIPT_CMD_SUCCESS;
  3942. }
  3943. /// Displays the button 'next' in the npc dialog.
  3944. /// The dialog text is cleared and the script continues when the button is pressed.
  3945. ///
  3946. /// next;
  3947. BUILDIN_FUNC(next)
  3948. {
  3949. TBL_PC* sd;
  3950. sd = script_rid2sd(st);
  3951. if( sd == NULL )
  3952. return 0;
  3953. #ifdef SECURE_NPCTIMEOUT
  3954. sd->npc_idle_type = NPCT_WAIT;
  3955. #endif
  3956. st->state = STOP;
  3957. clif_scriptnext(sd, st->oid);
  3958. return SCRIPT_CMD_SUCCESS;
  3959. }
  3960. /// Ends the script and displays the button 'close' on the npc dialog.
  3961. /// The dialog is closed when the button is pressed.
  3962. ///
  3963. /// close;
  3964. BUILDIN_FUNC(close)
  3965. {
  3966. TBL_PC* sd;
  3967. sd = script_rid2sd(st);
  3968. if( sd == NULL )
  3969. return 0;
  3970. if( !st->mes_active ) {
  3971. TBL_NPC* nd = map_id2nd(st->oid);
  3972. st->state = END; // Keep backwards compatibility.
  3973. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  3974. } else {
  3975. st->state = CLOSE;
  3976. st->mes_active = 0;
  3977. }
  3978. clif_scriptclose(sd, st->oid);
  3979. return SCRIPT_CMD_SUCCESS;
  3980. }
  3981. /// Displays the button 'close' on the npc dialog.
  3982. /// The dialog is closed and the script continues when the button is pressed.
  3983. ///
  3984. /// close2;
  3985. BUILDIN_FUNC(close2)
  3986. {
  3987. TBL_PC* sd;
  3988. sd = script_rid2sd(st);
  3989. if( sd == NULL )
  3990. return 0;
  3991. st->state = STOP;
  3992. if( st->mes_active )
  3993. st->mes_active = 0;
  3994. clif_scriptclose(sd, st->oid);
  3995. return SCRIPT_CMD_SUCCESS;
  3996. }
  3997. /// Counts the number of valid and total number of options in 'str'
  3998. /// If max_count > 0 the counting stops when that valid option is reached
  3999. /// total is incremented for each option (NULL is supported)
  4000. static int menu_countoptions(const char* str, int max_count, int* total)
  4001. {
  4002. int count = 0;
  4003. int bogus_total;
  4004. if( total == NULL )
  4005. total = &bogus_total;
  4006. ++(*total);
  4007. // initial empty options
  4008. while( *str == ':' )
  4009. {
  4010. ++str;
  4011. ++(*total);
  4012. }
  4013. // count menu options
  4014. while( *str != '\0' )
  4015. {
  4016. ++count;
  4017. --max_count;
  4018. if( max_count == 0 )
  4019. break;
  4020. while( *str != ':' && *str != '\0' )
  4021. ++str;
  4022. while( *str == ':' )
  4023. {
  4024. ++str;
  4025. ++(*total);
  4026. }
  4027. }
  4028. return count;
  4029. }
  4030. /// Displays a menu with options and goes to the target label.
  4031. /// The script is stopped if cancel is pressed.
  4032. /// Options with no text are not displayed in the client.
  4033. ///
  4034. /// Options can be grouped together, separated by the character ':' in the text:
  4035. /// ex: menu "A:B:C",L_target;
  4036. /// All these options go to the specified target label.
  4037. ///
  4038. /// The index of the selected option is put in the variable @menu.
  4039. /// Indexes start with 1 and are consistent with grouped and empty options.
  4040. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4041. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4042. ///
  4043. /// NOTE: the client closes the npc dialog when cancel is pressed
  4044. ///
  4045. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4046. BUILDIN_FUNC(menu)
  4047. {
  4048. int i;
  4049. const char* text;
  4050. TBL_PC* sd;
  4051. sd = script_rid2sd(st);
  4052. if( sd == NULL )
  4053. return 0;
  4054. #ifdef SECURE_NPCTIMEOUT
  4055. sd->npc_idle_type = NPCT_MENU;
  4056. #endif
  4057. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4058. if( sd->state.menu_or_input == 0 )
  4059. {
  4060. struct StringBuf buf;
  4061. struct script_data* data;
  4062. if( script_lastdata(st) % 2 == 0 )
  4063. {// argument count is not even (1st argument is at index 2)
  4064. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4065. st->state = END;
  4066. return SCRIPT_CMD_FAILURE;
  4067. }
  4068. StringBuf_Init(&buf);
  4069. sd->npc_menu = 0;
  4070. for( i = 2; i < script_lastdata(st); i += 2 )
  4071. {
  4072. // menu options
  4073. text = script_getstr(st, i);
  4074. // target label
  4075. data = script_getdata(st, i+1);
  4076. if( !data_islabel(data) )
  4077. {// not a label
  4078. StringBuf_Destroy(&buf);
  4079. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4080. script_reportdata(data);
  4081. st->state = END;
  4082. return SCRIPT_CMD_FAILURE;
  4083. }
  4084. // append option(s)
  4085. if( text[0] == '\0' )
  4086. continue;// empty string, ignore
  4087. if( sd->npc_menu > 0 )
  4088. StringBuf_AppendStr(&buf, ":");
  4089. StringBuf_AppendStr(&buf, text);
  4090. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4091. }
  4092. st->state = RERUNLINE;
  4093. sd->state.menu_or_input = 1;
  4094. /**
  4095. * menus beyond this length crash the client (see bugreport:6402)
  4096. **/
  4097. if( StringBuf_Length(&buf) >= 2047 ) {
  4098. struct npc_data * nd = map_id2nd(st->oid);
  4099. char* menu;
  4100. CREATE(menu, char, 2048);
  4101. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4102. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4103. clif_scriptmenu(sd, st->oid, menu);
  4104. aFree(menu);
  4105. } else
  4106. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4107. StringBuf_Destroy(&buf);
  4108. if( sd->npc_menu >= 0xff )
  4109. {// 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
  4110. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4111. script_reportsrc(st);
  4112. }
  4113. }
  4114. else if( sd->npc_menu == 0xff )
  4115. {// Cancel was pressed
  4116. sd->state.menu_or_input = 0;
  4117. st->state = END;
  4118. }
  4119. else
  4120. {// goto target label
  4121. int menu = 0;
  4122. sd->state.menu_or_input = 0;
  4123. if( sd->npc_menu <= 0 )
  4124. {
  4125. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4126. st->state = END;
  4127. return SCRIPT_CMD_FAILURE;
  4128. }
  4129. // get target label
  4130. for( i = 2; i < script_lastdata(st); i += 2 )
  4131. {
  4132. text = script_getstr(st, i);
  4133. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4134. if( sd->npc_menu <= 0 )
  4135. break;// entry found
  4136. }
  4137. if( sd->npc_menu > 0 )
  4138. {// Invalid selection
  4139. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4140. st->state = END;
  4141. return SCRIPT_CMD_FAILURE;
  4142. }
  4143. if( !data_islabel(script_getdata(st, i + 1)) )
  4144. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4145. ShowError("script:menu: unexpected data in label argument\n");
  4146. script_reportdata(script_getdata(st, i + 1));
  4147. st->state = END;
  4148. return SCRIPT_CMD_FAILURE;
  4149. }
  4150. pc_setreg(sd, add_str("@menu"), menu);
  4151. st->pos = script_getnum(st, i + 1);
  4152. st->state = GOTO;
  4153. }
  4154. return SCRIPT_CMD_SUCCESS;
  4155. }
  4156. /// Displays a menu with options and returns the selected option.
  4157. /// Behaves like 'menu' without the target labels.
  4158. ///
  4159. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4160. ///
  4161. /// @see menu
  4162. BUILDIN_FUNC(select)
  4163. {
  4164. int i;
  4165. const char* text;
  4166. TBL_PC* sd;
  4167. sd = script_rid2sd(st);
  4168. if( sd == NULL )
  4169. return 0;
  4170. #ifdef SECURE_NPCTIMEOUT
  4171. sd->npc_idle_type = NPCT_MENU;
  4172. #endif
  4173. if( sd->state.menu_or_input == 0 ) {
  4174. struct StringBuf buf;
  4175. StringBuf_Init(&buf);
  4176. sd->npc_menu = 0;
  4177. for( i = 2; i <= script_lastdata(st); ++i ) {
  4178. text = script_getstr(st, i);
  4179. if( sd->npc_menu > 0 )
  4180. StringBuf_AppendStr(&buf, ":");
  4181. StringBuf_AppendStr(&buf, text);
  4182. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4183. }
  4184. st->state = RERUNLINE;
  4185. sd->state.menu_or_input = 1;
  4186. /**
  4187. * menus beyond this length crash the client (see bugreport:6402)
  4188. **/
  4189. if( StringBuf_Length(&buf) >= 2047 ) {
  4190. struct npc_data * nd = map_id2nd(st->oid);
  4191. char* menu;
  4192. CREATE(menu, char, 2048);
  4193. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4194. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4195. clif_scriptmenu(sd, st->oid, menu);
  4196. aFree(menu);
  4197. } else
  4198. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4199. StringBuf_Destroy(&buf);
  4200. if( sd->npc_menu >= 0xff ) {
  4201. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4202. script_reportsrc(st);
  4203. }
  4204. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4205. sd->state.menu_or_input = 0;
  4206. st->state = END;
  4207. } else {// return selected option
  4208. int menu = 0;
  4209. sd->state.menu_or_input = 0;
  4210. for( i = 2; i <= script_lastdata(st); ++i ) {
  4211. text = script_getstr(st, i);
  4212. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4213. if( sd->npc_menu <= 0 )
  4214. break;// entry found
  4215. }
  4216. pc_setreg(sd, add_str("@menu"), menu);
  4217. script_pushint(st, menu);
  4218. st->state = RUN;
  4219. }
  4220. return SCRIPT_CMD_SUCCESS;
  4221. }
  4222. /// Displays a menu with options and returns the selected option.
  4223. /// Behaves like 'menu' without the target labels, except when cancel is
  4224. /// pressed.
  4225. /// When cancel is pressed, the script continues and 255 is returned.
  4226. ///
  4227. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4228. ///
  4229. /// @see menu
  4230. BUILDIN_FUNC(prompt)
  4231. {
  4232. int i;
  4233. const char *text;
  4234. TBL_PC* sd;
  4235. sd = script_rid2sd(st);
  4236. if( sd == NULL )
  4237. return 0;
  4238. #ifdef SECURE_NPCTIMEOUT
  4239. sd->npc_idle_type = NPCT_MENU;
  4240. #endif
  4241. if( sd->state.menu_or_input == 0 )
  4242. {
  4243. struct StringBuf buf;
  4244. StringBuf_Init(&buf);
  4245. sd->npc_menu = 0;
  4246. for( i = 2; i <= script_lastdata(st); ++i )
  4247. {
  4248. text = script_getstr(st, i);
  4249. if( sd->npc_menu > 0 )
  4250. StringBuf_AppendStr(&buf, ":");
  4251. StringBuf_AppendStr(&buf, text);
  4252. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4253. }
  4254. st->state = RERUNLINE;
  4255. sd->state.menu_or_input = 1;
  4256. /**
  4257. * menus beyond this length crash the client (see bugreport:6402)
  4258. **/
  4259. if( StringBuf_Length(&buf) >= 2047 ) {
  4260. struct npc_data * nd = map_id2nd(st->oid);
  4261. char* menu;
  4262. CREATE(menu, char, 2048);
  4263. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4264. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4265. clif_scriptmenu(sd, st->oid, menu);
  4266. aFree(menu);
  4267. } else
  4268. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4269. StringBuf_Destroy(&buf);
  4270. if( sd->npc_menu >= 0xff )
  4271. {
  4272. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4273. script_reportsrc(st);
  4274. }
  4275. }
  4276. else if( sd->npc_menu == 0xff )
  4277. {// Cancel was pressed
  4278. sd->state.menu_or_input = 0;
  4279. pc_setreg(sd, add_str("@menu"), 0xff);
  4280. script_pushint(st, 0xff);
  4281. st->state = END;
  4282. }
  4283. else
  4284. {// return selected option
  4285. int menu = 0;
  4286. sd->state.menu_or_input = 0;
  4287. for( i = 2; i <= script_lastdata(st); ++i )
  4288. {
  4289. text = script_getstr(st, i);
  4290. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4291. if( sd->npc_menu <= 0 )
  4292. break;// entry found
  4293. }
  4294. pc_setreg(sd, add_str("@menu"), menu);
  4295. script_pushint(st, menu);
  4296. st->state = RUN;
  4297. }
  4298. return SCRIPT_CMD_SUCCESS;
  4299. }
  4300. /////////////////////////////////////////////////////////////////////
  4301. // ...
  4302. //
  4303. /// Jumps to the target script label.
  4304. ///
  4305. /// goto <label>;
  4306. BUILDIN_FUNC(goto)
  4307. {
  4308. if( !data_islabel(script_getdata(st,2)) )
  4309. {
  4310. ShowError("script:goto: not a label\n");
  4311. script_reportdata(script_getdata(st,2));
  4312. st->state = END;
  4313. return 1;
  4314. }
  4315. st->pos = script_getnum(st,2);
  4316. st->state = GOTO;
  4317. return SCRIPT_CMD_SUCCESS;
  4318. }
  4319. /*==========================================
  4320. * user-defined function call
  4321. *------------------------------------------*/
  4322. BUILDIN_FUNC(callfunc)
  4323. {
  4324. int i, j;
  4325. struct script_retinfo* ri;
  4326. struct script_code* scr;
  4327. const char* str = script_getstr(st,2);
  4328. DBMap **ref = NULL;
  4329. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4330. if( !scr )
  4331. {
  4332. ShowError("script:callfunc: function not found! [%s]\n", str);
  4333. st->state = END;
  4334. return 1;
  4335. }
  4336. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4337. {
  4338. struct script_data* data = push_copy(st->stack,i);
  4339. if( data_isreference(data) && !data->ref )
  4340. {
  4341. const char* name = reference_getname(data);
  4342. if( name[0] == '.' ) {
  4343. if(!ref){
  4344. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4345. ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars);
  4346. }
  4347. data->ref = ref;
  4348. }
  4349. }
  4350. }
  4351. CREATE(ri, struct script_retinfo, 1);
  4352. ri->script = st->script;// script code
  4353. ri->var_function = st->stack->var_function;// scope variables
  4354. ri->pos = st->pos;// script location
  4355. ri->nargs = j;// argument count
  4356. ri->defsp = st->stack->defsp;// default stack pointer
  4357. push_retinfo(st->stack, ri, ref);
  4358. st->pos = 0;
  4359. st->script = scr;
  4360. st->stack->defsp = st->stack->sp;
  4361. st->state = GOTO;
  4362. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4363. return SCRIPT_CMD_SUCCESS;
  4364. }
  4365. /*==========================================
  4366. * subroutine call
  4367. *------------------------------------------*/
  4368. BUILDIN_FUNC(callsub)
  4369. {
  4370. int i,j;
  4371. struct script_retinfo* ri;
  4372. int pos = script_getnum(st,2);
  4373. DBMap **ref = NULL;
  4374. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4375. {
  4376. ShowError("script:callsub: argument is not a label\n");
  4377. script_reportdata(script_getdata(st,2));
  4378. st->state = END;
  4379. return 1;
  4380. }
  4381. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4382. {
  4383. struct script_data* data = push_copy(st->stack,i);
  4384. if( data_isreference(data) && !data->ref )
  4385. {
  4386. const char* name = reference_getname(data);
  4387. if( name[0] == '.' && name[1] == '@' ) {
  4388. if ( !ref ) {
  4389. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4390. ref[0] = st->stack->var_function;
  4391. }
  4392. data->ref = ref;
  4393. }
  4394. }
  4395. }
  4396. CREATE(ri, struct script_retinfo, 1);
  4397. ri->script = st->script;// script code
  4398. ri->var_function = st->stack->var_function;// scope variables
  4399. ri->pos = st->pos;// script location
  4400. ri->nargs = j;// argument count
  4401. ri->defsp = st->stack->defsp;// default stack pointer
  4402. push_retinfo(st->stack, ri, ref);
  4403. st->pos = pos;
  4404. st->stack->defsp = st->stack->sp;
  4405. st->state = GOTO;
  4406. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4407. return SCRIPT_CMD_SUCCESS;
  4408. }
  4409. /// Retrieves an argument provided to callfunc/callsub.
  4410. /// If the argument doesn't exist
  4411. ///
  4412. /// getarg(<index>{,<default_value>}) -> <value>
  4413. BUILDIN_FUNC(getarg)
  4414. {
  4415. struct script_retinfo* ri;
  4416. int idx;
  4417. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4418. {
  4419. ShowError("script:getarg: no callfunc or callsub!\n");
  4420. st->state = END;
  4421. return 1;
  4422. }
  4423. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4424. idx = script_getnum(st,2);
  4425. if( idx >= 0 && idx < ri->nargs )
  4426. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4427. else if( script_hasdata(st,3) )
  4428. script_pushcopy(st, 3);
  4429. else
  4430. {
  4431. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4432. st->state = END;
  4433. return 1;
  4434. }
  4435. return SCRIPT_CMD_SUCCESS;
  4436. }
  4437. /// Returns from the current function, optionaly returning a value from the functions.
  4438. /// Don't use outside script functions.
  4439. ///
  4440. /// return;
  4441. /// return <value>;
  4442. BUILDIN_FUNC(return)
  4443. {
  4444. if( script_hasdata(st,2) )
  4445. {// return value
  4446. struct script_data* data;
  4447. script_pushcopy(st, 2);
  4448. data = script_getdatatop(st, -1);
  4449. if( data_isreference(data) )
  4450. {
  4451. const char* name = reference_getname(data);
  4452. if( name[0] == '.' && name[1] == '@' )
  4453. {// scope variable
  4454. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4455. get_val(st, data);// current scope, convert to value
  4456. }
  4457. else if( name[0] == '.' && !data->ref )
  4458. {// script variable, link to current script
  4459. data->ref = &st->script->script_vars;
  4460. }
  4461. }
  4462. }
  4463. else
  4464. {// no return value
  4465. script_pushnil(st);
  4466. }
  4467. st->state = RETFUNC;
  4468. return SCRIPT_CMD_SUCCESS;
  4469. }
  4470. /// Returns a random number from 0 to <range>-1.
  4471. /// Or returns a random number from <min> to <max>.
  4472. /// If <min> is greater than <max>, their numbers are switched.
  4473. /// rand(<range>) -> <int>
  4474. /// rand(<min>,<max>) -> <int>
  4475. BUILDIN_FUNC(rand)
  4476. {
  4477. int range;
  4478. int min;
  4479. int max;
  4480. if( script_hasdata(st,3) )
  4481. {// min,max
  4482. min = script_getnum(st,2);
  4483. max = script_getnum(st,3);
  4484. if( max < min )
  4485. swap(min, max);
  4486. range = max - min + 1;
  4487. }
  4488. else
  4489. {// range
  4490. min = 0;
  4491. range = script_getnum(st,2);
  4492. }
  4493. if( range <= 1 )
  4494. script_pushint(st, min);
  4495. else
  4496. script_pushint(st, rnd()%range + min);
  4497. return SCRIPT_CMD_SUCCESS;
  4498. }
  4499. /*==========================================
  4500. * Warp sd to str,x,y or Random or SavePoint/Save
  4501. *------------------------------------------*/
  4502. BUILDIN_FUNC(warp)
  4503. {
  4504. int ret;
  4505. int x,y;
  4506. const char* str;
  4507. TBL_PC* sd;
  4508. sd = script_rid2sd(st);
  4509. if( sd == NULL )
  4510. return 0;
  4511. str = script_getstr(st,2);
  4512. x = script_getnum(st,3);
  4513. y = script_getnum(st,4);
  4514. if(strcmp(str,"Random")==0)
  4515. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4516. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4517. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4518. else
  4519. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4520. if( ret ) {
  4521. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4522. return 1;
  4523. }
  4524. return SCRIPT_CMD_SUCCESS;
  4525. }
  4526. /*==========================================
  4527. * Warp a specified area
  4528. *------------------------------------------*/
  4529. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4530. {
  4531. int x2,y2,x3,y3;
  4532. unsigned int index;
  4533. index = va_arg(ap,unsigned int);
  4534. x2 = va_arg(ap,int);
  4535. y2 = va_arg(ap,int);
  4536. x3 = va_arg(ap,int);
  4537. y3 = va_arg(ap,int);
  4538. if(index == 0)
  4539. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4540. else if(x3 && y3) {
  4541. int max, tx, ty, j = 0;
  4542. // choose a suitable max number of attempts
  4543. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4544. max = 1000;
  4545. // find a suitable map cell
  4546. do {
  4547. tx = rnd()%(x3-x2+1)+x2;
  4548. ty = rnd()%(y3-y2+1)+y2;
  4549. j++;
  4550. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4551. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4552. }
  4553. else
  4554. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4555. return 0;
  4556. }
  4557. BUILDIN_FUNC(areawarp)
  4558. {
  4559. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4560. unsigned int index;
  4561. const char *str;
  4562. const char *mapname;
  4563. mapname = script_getstr(st,2);
  4564. x0 = script_getnum(st,3);
  4565. y0 = script_getnum(st,4);
  4566. x1 = script_getnum(st,5);
  4567. y1 = script_getnum(st,6);
  4568. str = script_getstr(st,7);
  4569. x2 = script_getnum(st,8);
  4570. y2 = script_getnum(st,9);
  4571. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4572. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4573. x3 = 0;
  4574. y3 = 0;
  4575. } else if( x3 && y3 ) {
  4576. // normalize x3/y3 coordinates
  4577. if( x3 < x2 ) swap(x3,x2);
  4578. if( y3 < y2 ) swap(y3,y2);
  4579. }
  4580. }
  4581. if( (m = map_mapname2mapid(mapname)) < 0 )
  4582. return 0;
  4583. if( strcmp(str,"Random") == 0 )
  4584. index = 0;
  4585. else if( !(index=mapindex_name2id(str)) )
  4586. return 0;
  4587. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4588. return SCRIPT_CMD_SUCCESS;
  4589. }
  4590. /*==========================================
  4591. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4592. *------------------------------------------*/
  4593. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4594. {
  4595. int hp, sp;
  4596. hp = va_arg(ap, int);
  4597. sp = va_arg(ap, int);
  4598. pc_percentheal((TBL_PC *)bl,hp,sp);
  4599. return 0;
  4600. }
  4601. BUILDIN_FUNC(areapercentheal)
  4602. {
  4603. int hp,sp,m;
  4604. const char *mapname;
  4605. int x0,y0,x1,y1;
  4606. mapname=script_getstr(st,2);
  4607. x0=script_getnum(st,3);
  4608. y0=script_getnum(st,4);
  4609. x1=script_getnum(st,5);
  4610. y1=script_getnum(st,6);
  4611. hp=script_getnum(st,7);
  4612. sp=script_getnum(st,8);
  4613. if( (m=map_mapname2mapid(mapname))< 0)
  4614. return 0;
  4615. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4616. return SCRIPT_CMD_SUCCESS;
  4617. }
  4618. /*==========================================
  4619. * warpchar [LuzZza]
  4620. * Useful for warp one player from
  4621. * another player npc-session.
  4622. * Using: warpchar "mapname",x,y,Char_ID;
  4623. *------------------------------------------*/
  4624. BUILDIN_FUNC(warpchar)
  4625. {
  4626. int x,y,a;
  4627. const char *str;
  4628. TBL_PC *sd;
  4629. str=script_getstr(st,2);
  4630. x=script_getnum(st,3);
  4631. y=script_getnum(st,4);
  4632. a=script_getnum(st,5);
  4633. sd = map_charid2sd(a);
  4634. if( sd == NULL )
  4635. return 0;
  4636. if(strcmp(str, "Random") == 0)
  4637. pc_randomwarp(sd, CLR_TELEPORT);
  4638. else
  4639. if(strcmp(str, "SavePoint") == 0)
  4640. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4641. else
  4642. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4643. return SCRIPT_CMD_SUCCESS;
  4644. }
  4645. /*==========================================
  4646. * Warpparty - [Fredzilla] [Paradox924X]
  4647. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4648. * If 'from_mapname' is specified, only the party members on that map will be warped
  4649. *------------------------------------------*/
  4650. BUILDIN_FUNC(warpparty)
  4651. {
  4652. TBL_PC *sd = NULL;
  4653. TBL_PC *pl_sd;
  4654. struct party_data* p;
  4655. int type;
  4656. int mapindex;
  4657. int i;
  4658. const char* str = script_getstr(st,2);
  4659. int x = script_getnum(st,3);
  4660. int y = script_getnum(st,4);
  4661. int p_id = script_getnum(st,5);
  4662. const char* str2 = NULL;
  4663. if ( script_hasdata(st,6) )
  4664. str2 = script_getstr(st,6);
  4665. p = party_search(p_id);
  4666. if(!p)
  4667. return 0;
  4668. type = ( strcmp(str,"Random")==0 ) ? 0
  4669. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4670. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4671. : ( strcmp(str,"Leader")==0 ) ? 3
  4672. : 4;
  4673. switch (type)
  4674. {
  4675. case 3:
  4676. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4677. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4678. return 0;
  4679. pl_sd = p->data[i].sd;
  4680. mapindex = pl_sd->mapindex;
  4681. x = pl_sd->bl.x;
  4682. y = pl_sd->bl.y;
  4683. break;
  4684. case 4:
  4685. mapindex = mapindex_name2id(str);
  4686. break;
  4687. case 2:
  4688. //"SavePoint" uses save point of the currently attached player
  4689. if (( sd = script_rid2sd(st) ) == NULL )
  4690. return 0;
  4691. default:
  4692. mapindex = 0;
  4693. break;
  4694. }
  4695. for (i = 0; i < MAX_PARTY; i++)
  4696. {
  4697. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4698. continue;
  4699. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4700. continue;
  4701. if( pc_isdead(pl_sd) )
  4702. continue;
  4703. switch( type )
  4704. {
  4705. case 0: // Random
  4706. if(!map[pl_sd->bl.m].flag.nowarp)
  4707. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4708. break;
  4709. case 1: // SavePointAll
  4710. if(!map[pl_sd->bl.m].flag.noreturn)
  4711. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4712. break;
  4713. case 2: // SavePoint
  4714. if(!map[pl_sd->bl.m].flag.noreturn)
  4715. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4716. break;
  4717. case 3: // Leader
  4718. case 4: // m,x,y
  4719. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4720. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4721. break;
  4722. }
  4723. }
  4724. return SCRIPT_CMD_SUCCESS;
  4725. }
  4726. /*==========================================
  4727. * Warpguild - [Fredzilla]
  4728. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4729. *------------------------------------------*/
  4730. BUILDIN_FUNC(warpguild)
  4731. {
  4732. TBL_PC *sd = NULL;
  4733. TBL_PC *pl_sd;
  4734. struct guild* g;
  4735. struct s_mapiterator* iter;
  4736. int type;
  4737. const char* str = script_getstr(st,2);
  4738. int x = script_getnum(st,3);
  4739. int y = script_getnum(st,4);
  4740. int gid = script_getnum(st,5);
  4741. g = guild_search(gid);
  4742. if( g == NULL )
  4743. return 0;
  4744. type = ( strcmp(str,"Random")==0 ) ? 0
  4745. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4746. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4747. : 3;
  4748. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4749. {// "SavePoint" uses save point of the currently attached player
  4750. return 0;
  4751. }
  4752. iter = mapit_getallusers();
  4753. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4754. {
  4755. if( pl_sd->status.guild_id != gid )
  4756. continue;
  4757. switch( type )
  4758. {
  4759. case 0: // Random
  4760. if(!map[pl_sd->bl.m].flag.nowarp)
  4761. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4762. break;
  4763. case 1: // SavePointAll
  4764. if(!map[pl_sd->bl.m].flag.noreturn)
  4765. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4766. break;
  4767. case 2: // SavePoint
  4768. if(!map[pl_sd->bl.m].flag.noreturn)
  4769. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4770. break;
  4771. case 3: // m,x,y
  4772. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4773. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4774. break;
  4775. }
  4776. }
  4777. mapit_free(iter);
  4778. return SCRIPT_CMD_SUCCESS;
  4779. }
  4780. /*==========================================
  4781. * Force Heal a player (hp and sp)
  4782. *------------------------------------------*/
  4783. BUILDIN_FUNC(heal)
  4784. {
  4785. TBL_PC *sd;
  4786. int hp,sp;
  4787. sd = script_rid2sd(st);
  4788. if (!sd) return 0;
  4789. hp=script_getnum(st,2);
  4790. sp=script_getnum(st,3);
  4791. status_heal(&sd->bl, hp, sp, 1);
  4792. return SCRIPT_CMD_SUCCESS;
  4793. }
  4794. /*==========================================
  4795. * Heal a player by item (get vit bonus etc)
  4796. *------------------------------------------*/
  4797. BUILDIN_FUNC(itemheal)
  4798. {
  4799. TBL_PC *sd;
  4800. int hp,sp;
  4801. hp=script_getnum(st,2);
  4802. sp=script_getnum(st,3);
  4803. if(potion_flag==1) {
  4804. potion_hp = hp;
  4805. potion_sp = sp;
  4806. return 0;
  4807. }
  4808. sd = script_rid2sd(st);
  4809. if (!sd) return 0;
  4810. pc_itemheal(sd,sd->itemid,hp,sp);
  4811. return SCRIPT_CMD_SUCCESS;
  4812. }
  4813. /*==========================================
  4814. *
  4815. *------------------------------------------*/
  4816. BUILDIN_FUNC(percentheal)
  4817. {
  4818. int hp,sp;
  4819. TBL_PC* sd;
  4820. hp=script_getnum(st,2);
  4821. sp=script_getnum(st,3);
  4822. if(potion_flag==1) {
  4823. potion_per_hp = hp;
  4824. potion_per_sp = sp;
  4825. return 0;
  4826. }
  4827. sd = script_rid2sd(st);
  4828. if( sd == NULL )
  4829. return 0;
  4830. #ifdef RENEWAL
  4831. if( sd->sc.data[SC_EXTREMITYFIST2] )
  4832. sp = 0;
  4833. #endif
  4834. pc_percentheal(sd,hp,sp);
  4835. return SCRIPT_CMD_SUCCESS;
  4836. }
  4837. /*==========================================
  4838. *
  4839. *------------------------------------------*/
  4840. BUILDIN_FUNC(jobchange)
  4841. {
  4842. int job, upper=-1;
  4843. job=script_getnum(st,2);
  4844. if( script_hasdata(st,3) )
  4845. upper=script_getnum(st,3);
  4846. if (pcdb_checkid(job))
  4847. {
  4848. TBL_PC* sd;
  4849. sd = script_rid2sd(st);
  4850. if( sd == NULL )
  4851. return 0;
  4852. pc_jobchange(sd, job, upper);
  4853. }
  4854. return SCRIPT_CMD_SUCCESS;
  4855. }
  4856. /*==========================================
  4857. *
  4858. *------------------------------------------*/
  4859. BUILDIN_FUNC(jobname)
  4860. {
  4861. int class_=script_getnum(st,2);
  4862. script_pushconststr(st, (char*)job_name(class_));
  4863. return SCRIPT_CMD_SUCCESS;
  4864. }
  4865. /// Get input from the player.
  4866. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4867. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4868. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4869. /// shorter than 'min' and 0 otherwise.
  4870. ///
  4871. /// input(<var>{,<min>{,<max>}}) -> <int>
  4872. BUILDIN_FUNC(input)
  4873. {
  4874. TBL_PC* sd;
  4875. struct script_data* data;
  4876. int uid;
  4877. const char* name;
  4878. int min;
  4879. int max;
  4880. sd = script_rid2sd(st);
  4881. if( sd == NULL )
  4882. return 0;
  4883. data = script_getdata(st,2);
  4884. if( !data_isreference(data) ){
  4885. ShowError("script:input: not a variable\n");
  4886. script_reportdata(data);
  4887. st->state = END;
  4888. return 1;
  4889. }
  4890. uid = reference_getuid(data);
  4891. name = reference_getname(data);
  4892. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4893. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4894. #ifdef SECURE_NPCTIMEOUT
  4895. sd->npc_idle_type = NPCT_WAIT;
  4896. #endif
  4897. if( !sd->state.menu_or_input )
  4898. { // first invocation, display npc input box
  4899. sd->state.menu_or_input = 1;
  4900. st->state = RERUNLINE;
  4901. if( is_string_variable(name) )
  4902. clif_scriptinputstr(sd,st->oid);
  4903. else
  4904. clif_scriptinput(sd,st->oid);
  4905. }
  4906. else
  4907. { // take received text/value and store it in the designated variable
  4908. sd->state.menu_or_input = 0;
  4909. if( is_string_variable(name) )
  4910. {
  4911. int len = (int)strlen(sd->npc_str);
  4912. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4913. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4914. }
  4915. else
  4916. {
  4917. int amount = sd->npc_amount;
  4918. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  4919. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4920. }
  4921. st->state = RUN;
  4922. }
  4923. return SCRIPT_CMD_SUCCESS;
  4924. }
  4925. // declare the copyarray method here for future reference
  4926. BUILDIN_FUNC(copyarray);
  4927. /// Sets the value of a variable.
  4928. /// The value is converted to the type of the variable.
  4929. ///
  4930. /// set(<variable>,<value>) -> <variable>
  4931. BUILDIN_FUNC(set)
  4932. {
  4933. TBL_PC* sd = NULL;
  4934. struct script_data* data;
  4935. //struct script_data* datavalue;
  4936. int num;
  4937. const char* name;
  4938. char prefix;
  4939. data = script_getdata(st,2);
  4940. //datavalue = script_getdata(st,3);
  4941. if( !data_isreference(data) )
  4942. {
  4943. ShowError("script:set: not a variable\n");
  4944. script_reportdata(script_getdata(st,2));
  4945. st->state = END;
  4946. return 1;
  4947. }
  4948. num = reference_getuid(data);
  4949. name = reference_getname(data);
  4950. prefix = *name;
  4951. if( not_server_variable(prefix) )
  4952. {
  4953. sd = script_rid2sd(st);
  4954. if( sd == NULL )
  4955. {
  4956. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4957. return 0;
  4958. }
  4959. }
  4960. #if 0
  4961. if( data_isreference(datavalue) )
  4962. {// the value being referenced is a variable
  4963. const char* namevalue = reference_getname(datavalue);
  4964. if( !not_array_variable(*namevalue) )
  4965. {// array variable being copied into another array variable
  4966. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  4967. {// player must be attached in order to copy a player variable
  4968. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  4969. return 0;
  4970. }
  4971. if( is_string_variable(namevalue) != is_string_variable(name) )
  4972. {// non-matching array value types
  4973. ShowWarning("script:set: two array variables do not match in type.\n");
  4974. return 0;
  4975. }
  4976. // push the maximum number of array values to the stack
  4977. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  4978. // call the copy array method directly
  4979. return buildin_copyarray(st);
  4980. }
  4981. }
  4982. #endif
  4983. if( is_string_variable(name) )
  4984. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4985. else
  4986. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  4987. // return a copy of the variable reference
  4988. script_pushcopy(st,2);
  4989. return SCRIPT_CMD_SUCCESS;
  4990. }
  4991. /////////////////////////////////////////////////////////////////////
  4992. /// Array variables
  4993. ///
  4994. /// Returns the size of the specified array
  4995. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  4996. {
  4997. int32 ret = idx;
  4998. if( isstring )
  4999. {
  5000. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5001. {
  5002. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  5003. if( str && *str )
  5004. ret = idx + 1;
  5005. script_removetop(st, -1, 0);
  5006. }
  5007. }
  5008. else
  5009. {
  5010. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5011. {
  5012. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  5013. if( num )
  5014. ret = idx + 1;
  5015. script_removetop(st, -1, 0);
  5016. }
  5017. }
  5018. return ret;
  5019. }
  5020. /// Sets values of an array, from the starting index.
  5021. /// ex: setarray arr[1],1,2,3;
  5022. ///
  5023. /// setarray <array variable>,<value1>{,<value2>...};
  5024. BUILDIN_FUNC(setarray)
  5025. {
  5026. struct script_data* data;
  5027. const char* name;
  5028. int32 start;
  5029. int32 end;
  5030. int32 id;
  5031. int32 i;
  5032. TBL_PC* sd = NULL;
  5033. data = script_getdata(st, 2);
  5034. if( !data_isreference(data) )
  5035. {
  5036. ShowError("script:setarray: not a variable\n");
  5037. script_reportdata(data);
  5038. st->state = END;
  5039. return 1;// not a variable
  5040. }
  5041. id = reference_getid(data);
  5042. start = reference_getindex(data);
  5043. name = reference_getname(data);
  5044. if( not_array_variable(*name) )
  5045. {
  5046. ShowError("script:setarray: illegal scope\n");
  5047. script_reportdata(data);
  5048. st->state = END;
  5049. return 1;// not supported
  5050. }
  5051. if( not_server_variable(*name) )
  5052. {
  5053. sd = script_rid2sd(st);
  5054. if( sd == NULL )
  5055. return 0;// no player attached
  5056. }
  5057. end = start + script_lastdata(st) - 2;
  5058. if( end > SCRIPT_MAX_ARRAYSIZE )
  5059. end = SCRIPT_MAX_ARRAYSIZE;
  5060. if( is_string_variable(name) )
  5061. {// string array
  5062. for( i = 3; start < end; ++start, ++i )
  5063. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5064. }
  5065. else
  5066. {// int array
  5067. for( i = 3; start < end; ++start, ++i )
  5068. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5069. }
  5070. return SCRIPT_CMD_SUCCESS;
  5071. }
  5072. /// Sets count values of an array, from the starting index.
  5073. /// ex: cleararray arr[0],0,1;
  5074. ///
  5075. /// cleararray <array variable>,<value>,<count>;
  5076. BUILDIN_FUNC(cleararray)
  5077. {
  5078. struct script_data* data;
  5079. const char* name;
  5080. int32 start;
  5081. int32 end;
  5082. int32 id;
  5083. void* v;
  5084. TBL_PC* sd = NULL;
  5085. data = script_getdata(st, 2);
  5086. if( !data_isreference(data) )
  5087. {
  5088. ShowError("script:cleararray: not a variable\n");
  5089. script_reportdata(data);
  5090. st->state = END;
  5091. return 1;// not a variable
  5092. }
  5093. id = reference_getid(data);
  5094. start = reference_getindex(data);
  5095. name = reference_getname(data);
  5096. if( not_array_variable(*name) )
  5097. {
  5098. ShowError("script:cleararray: illegal scope\n");
  5099. script_reportdata(data);
  5100. st->state = END;
  5101. return 1;// not supported
  5102. }
  5103. if( not_server_variable(*name) )
  5104. {
  5105. sd = script_rid2sd(st);
  5106. if( sd == NULL )
  5107. return 0;// no player attached
  5108. }
  5109. if( is_string_variable(name) )
  5110. v = (void*)script_getstr(st, 3);
  5111. else
  5112. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5113. end = start + script_getnum(st, 4);
  5114. if( end > SCRIPT_MAX_ARRAYSIZE )
  5115. end = SCRIPT_MAX_ARRAYSIZE;
  5116. for( ; start < end; ++start )
  5117. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5118. return SCRIPT_CMD_SUCCESS;
  5119. }
  5120. /// Copies data from one array to another.
  5121. /// ex: copyarray arr[0],arr[2],2;
  5122. ///
  5123. /// copyarray <destination array variable>,<source array variable>,<count>;
  5124. BUILDIN_FUNC(copyarray)
  5125. {
  5126. struct script_data* data1;
  5127. struct script_data* data2;
  5128. const char* name1;
  5129. const char* name2;
  5130. int32 idx1;
  5131. int32 idx2;
  5132. int32 id1;
  5133. int32 id2;
  5134. void* v;
  5135. int32 i;
  5136. int32 count;
  5137. TBL_PC* sd = NULL;
  5138. data1 = script_getdata(st, 2);
  5139. data2 = script_getdata(st, 3);
  5140. if( !data_isreference(data1) || !data_isreference(data2) )
  5141. {
  5142. ShowError("script:copyarray: not a variable\n");
  5143. script_reportdata(data1);
  5144. script_reportdata(data2);
  5145. st->state = END;
  5146. return 1;// not a variable
  5147. }
  5148. id1 = reference_getid(data1);
  5149. id2 = reference_getid(data2);
  5150. idx1 = reference_getindex(data1);
  5151. idx2 = reference_getindex(data2);
  5152. name1 = reference_getname(data1);
  5153. name2 = reference_getname(data2);
  5154. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5155. {
  5156. ShowError("script:copyarray: illegal scope\n");
  5157. script_reportdata(data1);
  5158. script_reportdata(data2);
  5159. st->state = END;
  5160. return 1;// not supported
  5161. }
  5162. if( is_string_variable(name1) != is_string_variable(name2) )
  5163. {
  5164. ShowError("script:copyarray: type mismatch\n");
  5165. script_reportdata(data1);
  5166. script_reportdata(data2);
  5167. st->state = END;
  5168. return 1;// data type mismatch
  5169. }
  5170. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5171. {
  5172. sd = script_rid2sd(st);
  5173. if( sd == NULL )
  5174. return 0;// no player attached
  5175. }
  5176. count = script_getnum(st, 4);
  5177. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5178. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5179. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5180. return 0;// nothing to copy
  5181. if( id1 == id2 && idx1 > idx2 )
  5182. {// destination might be overlapping the source - copy in reverse order
  5183. for( i = count - 1; i >= 0; --i )
  5184. {
  5185. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5186. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5187. script_removetop(st, -1, 0);
  5188. }
  5189. }
  5190. else
  5191. {// normal copy
  5192. for( i = 0; i < count; ++i )
  5193. {
  5194. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5195. {
  5196. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5197. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5198. script_removetop(st, -1, 0);
  5199. }
  5200. else// out of range - assume ""/0
  5201. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5202. }
  5203. }
  5204. return SCRIPT_CMD_SUCCESS;
  5205. }
  5206. /// Returns the size of the array.
  5207. /// Assumes that everything before the starting index exists.
  5208. /// ex: getarraysize(arr[3])
  5209. ///
  5210. /// getarraysize(<array variable>) -> <int>
  5211. BUILDIN_FUNC(getarraysize)
  5212. {
  5213. struct script_data* data;
  5214. const char* name;
  5215. data = script_getdata(st, 2);
  5216. if( !data_isreference(data) )
  5217. {
  5218. ShowError("script:getarraysize: not a variable\n");
  5219. script_reportdata(data);
  5220. script_pushnil(st);
  5221. st->state = END;
  5222. return 1;// not a variable
  5223. }
  5224. name = reference_getname(data);
  5225. if( not_array_variable(*name) )
  5226. {
  5227. ShowError("script:getarraysize: illegal scope\n");
  5228. script_reportdata(data);
  5229. script_pushnil(st);
  5230. st->state = END;
  5231. return 1;// not supported
  5232. }
  5233. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5234. return SCRIPT_CMD_SUCCESS;
  5235. }
  5236. /// Deletes count or all the elements in an array, from the starting index.
  5237. /// ex: deletearray arr[4],2;
  5238. ///
  5239. /// deletearray <array variable>;
  5240. /// deletearray <array variable>,<count>;
  5241. BUILDIN_FUNC(deletearray)
  5242. {
  5243. struct script_data* data;
  5244. const char* name;
  5245. int start;
  5246. int end;
  5247. int id;
  5248. TBL_PC *sd = NULL;
  5249. data = script_getdata(st, 2);
  5250. if( !data_isreference(data) )
  5251. {
  5252. ShowError("script:deletearray: not a variable\n");
  5253. script_reportdata(data);
  5254. st->state = END;
  5255. return 1;// not a variable
  5256. }
  5257. id = reference_getid(data);
  5258. start = reference_getindex(data);
  5259. name = reference_getname(data);
  5260. if( not_array_variable(*name) )
  5261. {
  5262. ShowError("script:deletearray: illegal scope\n");
  5263. script_reportdata(data);
  5264. st->state = END;
  5265. return 1;// not supported
  5266. }
  5267. if( not_server_variable(*name) )
  5268. {
  5269. sd = script_rid2sd(st);
  5270. if( sd == NULL )
  5271. return 0;// no player attached
  5272. }
  5273. end = SCRIPT_MAX_ARRAYSIZE;
  5274. if( start >= end )
  5275. return 0;// nothing to free
  5276. if( script_hasdata(st,3) )
  5277. {
  5278. int count = script_getnum(st, 3);
  5279. if( count > end - start )
  5280. count = end - start;
  5281. if( count <= 0 )
  5282. return 0;// nothing to free
  5283. // move rest of the elements backward
  5284. for( ; start + count < end; ++start )
  5285. {
  5286. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5287. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5288. script_removetop(st, -1, 0);
  5289. }
  5290. }
  5291. // clear the rest of the array
  5292. if( is_string_variable(name) )
  5293. {
  5294. for( ; start < end; ++start )
  5295. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5296. }
  5297. else
  5298. {
  5299. for( ; start < end; ++start )
  5300. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5301. }
  5302. return SCRIPT_CMD_SUCCESS;
  5303. }
  5304. /// Returns a reference to the target index of the array variable.
  5305. /// Equivalent to var[index].
  5306. ///
  5307. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5308. BUILDIN_FUNC(getelementofarray)
  5309. {
  5310. struct script_data* data;
  5311. const char* name;
  5312. int32 id;
  5313. int i;
  5314. data = script_getdata(st, 2);
  5315. if( !data_isreference(data) )
  5316. {
  5317. ShowError("script:getelementofarray: not a variable\n");
  5318. script_reportdata(data);
  5319. script_pushnil(st);
  5320. st->state = END;
  5321. return 1;// not a variable
  5322. }
  5323. id = reference_getid(data);
  5324. name = reference_getname(data);
  5325. if( not_array_variable(*name) )
  5326. {
  5327. ShowError("script:getelementofarray: illegal scope\n");
  5328. script_reportdata(data);
  5329. script_pushnil(st);
  5330. st->state = END;
  5331. return 1;// not supported
  5332. }
  5333. i = script_getnum(st, 3);
  5334. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  5335. {
  5336. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5337. script_reportdata(data);
  5338. script_pushnil(st);
  5339. st->state = END;
  5340. return 1;// out of range
  5341. }
  5342. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5343. return SCRIPT_CMD_SUCCESS;
  5344. }
  5345. /////////////////////////////////////////////////////////////////////
  5346. /// ...
  5347. ///
  5348. /*==========================================
  5349. *
  5350. *------------------------------------------*/
  5351. BUILDIN_FUNC(setlook)
  5352. {
  5353. int type,val;
  5354. TBL_PC* sd;
  5355. type=script_getnum(st,2);
  5356. val=script_getnum(st,3);
  5357. sd = script_rid2sd(st);
  5358. if( sd == NULL )
  5359. return 0;
  5360. pc_changelook(sd,type,val);
  5361. return SCRIPT_CMD_SUCCESS;
  5362. }
  5363. BUILDIN_FUNC(changelook)
  5364. { // As setlook but only client side
  5365. int type,val;
  5366. TBL_PC* sd;
  5367. type=script_getnum(st,2);
  5368. val=script_getnum(st,3);
  5369. sd = script_rid2sd(st);
  5370. if( sd == NULL )
  5371. return 0;
  5372. clif_changelook(&sd->bl,type,val);
  5373. return SCRIPT_CMD_SUCCESS;
  5374. }
  5375. /*==========================================
  5376. *
  5377. *------------------------------------------*/
  5378. BUILDIN_FUNC(cutin)
  5379. {
  5380. TBL_PC* sd;
  5381. sd = script_rid2sd(st);
  5382. if( sd == NULL )
  5383. return 0;
  5384. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5385. return SCRIPT_CMD_SUCCESS;
  5386. }
  5387. /*==========================================
  5388. *
  5389. *------------------------------------------*/
  5390. BUILDIN_FUNC(viewpoint)
  5391. {
  5392. int type,x,y,id,color;
  5393. TBL_PC* sd;
  5394. type=script_getnum(st,2);
  5395. x=script_getnum(st,3);
  5396. y=script_getnum(st,4);
  5397. id=script_getnum(st,5);
  5398. color=script_getnum(st,6);
  5399. sd = script_rid2sd(st);
  5400. if( sd == NULL )
  5401. return 0;
  5402. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5403. return SCRIPT_CMD_SUCCESS;
  5404. }
  5405. /*==========================================
  5406. * countitem(nameID)
  5407. * returns number of items in inventory
  5408. *==========================================
  5409. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  5410. * returns number of items that meet the conditions
  5411. *------------------------------------------*/
  5412. BUILDIN_FUNC(countitem)
  5413. {
  5414. int i, count = 0;
  5415. struct item_data* id = NULL;
  5416. struct script_data* data;
  5417. TBL_PC* sd = script_rid2sd(st);
  5418. if (!sd) {
  5419. script_pushint(st,0);
  5420. return 0;
  5421. }
  5422. data = script_getdata(st,2);
  5423. get_val(st, data); // Convert into value in case of a variable
  5424. if( data_isstring(data) ) // item name
  5425. id = itemdb_searchname(conv_str(st, data));
  5426. else // item id
  5427. id = itemdb_exists(conv_num(st, data));
  5428. if( id == NULL ) {
  5429. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5430. script_pushint(st,0);
  5431. return 1;
  5432. }
  5433. if(script_lastdata(st) == 2) { // For countitem() function
  5434. int nameid = id->nameid;
  5435. for(i = 0; i < MAX_INVENTORY; i++)
  5436. if(sd->status.inventory[i].nameid == nameid)
  5437. count += sd->status.inventory[i].amount;
  5438. } else { // For countitem2() function
  5439. int nameid, iden, ref, attr, c1, c2, c3, c4;
  5440. nameid = id->nameid;
  5441. iden = script_getnum(st,3);
  5442. ref = script_getnum(st,4);
  5443. attr = script_getnum(st,5);
  5444. c1 = (short)script_getnum(st,6);
  5445. c2 = (short)script_getnum(st,7);
  5446. c3 = (short)script_getnum(st,8);
  5447. c4 = (short)script_getnum(st,9);
  5448. for(i = 0; i < MAX_INVENTORY; i++)
  5449. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  5450. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  5451. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  5452. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  5453. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  5454. sd->status.inventory[i].card[3] == c4 )
  5455. count += sd->status.inventory[i].amount;
  5456. }
  5457. script_pushint(st,count);
  5458. return SCRIPT_CMD_SUCCESS;
  5459. }
  5460. int checkweight_sub(TBL_PC *sd,int nbargs,int32 *eitemid,int32 *eamount)
  5461. {
  5462. struct item_data* id = NULL;
  5463. int nameid,amount;
  5464. uint32 weight=0;
  5465. uint16 amount2=0,slots,i;
  5466. slots = pc_inventoryblank(sd); //nb of empty slot
  5467. for(i=0; i<nbargs; i++) {
  5468. if(!eitemid[i])
  5469. continue;
  5470. id = itemdb_exists(eitemid[i]);
  5471. if( id == NULL ) {
  5472. ShowError("checkweight_sub: Invalid item '%d'.\n", eitemid[i]);
  5473. return 0;
  5474. }
  5475. nameid = id->nameid;
  5476. amount = eamount[i];
  5477. if( amount < 1 ) {
  5478. ShowError("checkweight_sub: Invalid amount '%d'.\n", eamount[i]);
  5479. return 0;
  5480. }
  5481. weight += (id->weight)*amount; //total weight for all chk
  5482. if( weight + sd->weight > sd->max_weight ) // too heavy
  5483. return 0;
  5484. switch( pc_checkadditem(sd, nameid, amount) ) {
  5485. case CHKADDITEM_EXIST: // item is already in inventory, but there is still space for the requested amount
  5486. break;
  5487. case CHKADDITEM_NEW:
  5488. if( itemdb_isstackable(nameid) )
  5489. amount2++; // stackable
  5490. else
  5491. amount2 += amount; // non-stackable
  5492. if( slots < amount2)
  5493. return 0;
  5494. break;
  5495. case CHKADDITEM_OVERAMOUNT:
  5496. return 0;
  5497. }
  5498. }
  5499. return 1;
  5500. }
  5501. /*==========================================
  5502. * Check if item with this amount can fit in inventory
  5503. * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY)
  5504. * Return
  5505. * 0 : fail
  5506. * 1 : success (npc side only)
  5507. *------------------------------------------*/
  5508. BUILDIN_FUNC(checkweight)
  5509. {
  5510. struct map_session_data* sd;
  5511. struct script_data* data;
  5512. struct item_data* id = NULL;
  5513. int32 nameid[SCRIPT_MAX_ARRAYSIZE], amount[SCRIPT_MAX_ARRAYSIZE];
  5514. uint16 nbargs,i,j=0;
  5515. if( ( sd = script_rid2sd(st) ) == NULL )
  5516. return 0;
  5517. nbargs = script_lastdata(st)+1;
  5518. if(nbargs%2) {
  5519. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5520. script_pushint(st,0);
  5521. return 1;
  5522. }
  5523. for(i=2; i<nbargs; i=i+2) {
  5524. data = script_getdata(st,i);
  5525. get_val(st, data); // Convert into value in case of a variable
  5526. if( data_isstring(data) ) // item name
  5527. id = itemdb_searchname(conv_str(st, data));
  5528. else // item id
  5529. id = itemdb_exists(conv_num(st, data));
  5530. if( id == NULL ) {
  5531. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5532. script_pushint(st,0);
  5533. return 1;
  5534. }
  5535. nameid[j] = id->nameid;
  5536. amount[j] = script_getnum(st,i+1);
  5537. j++;
  5538. }
  5539. script_pushint(st,checkweight_sub(sd,(nbargs-2)/2,nameid,amount));
  5540. return SCRIPT_CMD_SUCCESS;
  5541. }
  5542. BUILDIN_FUNC(checkweight2){
  5543. //variable sub checkweight
  5544. int32 nameid[SCRIPT_MAX_ARRAYSIZE], amount[SCRIPT_MAX_ARRAYSIZE], i;
  5545. //variable for array parsing
  5546. struct script_data* data_it;
  5547. struct script_data* data_nb;
  5548. const char* name_it;
  5549. const char* name_nb;
  5550. int32 id_it, id_nb;
  5551. int32 idx_it, idx_nb;
  5552. int nb_it, nb_nb; //array size
  5553. TBL_PC *sd = script_rid2sd(st);
  5554. nullpo_retr(1,sd);
  5555. data_it = script_getdata(st, 2);
  5556. data_nb = script_getdata(st, 3);
  5557. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5558. ShowError("buildin_checkweight2: parameter not a variable\n");
  5559. script_pushint(st,0);
  5560. return 1;// not a variable
  5561. }
  5562. id_it = reference_getid(data_it);
  5563. id_nb = reference_getid(data_nb);
  5564. idx_it = reference_getindex(data_it);
  5565. idx_nb = reference_getindex(data_nb);
  5566. name_it = reference_getname(data_it);
  5567. name_nb = reference_getname(data_nb);
  5568. if( not_array_variable(*name_it) || not_array_variable(*name_nb)) {
  5569. ShowError("buildin_checkweight2: illegal scope\n");
  5570. script_pushint(st,0);
  5571. return 1;// not supported
  5572. }
  5573. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5574. ShowError("buildin_checkweight2: illegal type, need int\n");
  5575. script_pushint(st,0);
  5576. return 1;// not supported
  5577. }
  5578. nb_it = getarraysize(st, id_it, idx_it, 0, reference_getref(data_it));
  5579. nb_nb = getarraysize(st, id_nb, idx_nb, 0, reference_getref(data_nb));
  5580. if(nb_it != nb_nb) {
  5581. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5582. script_pushint(st,0);
  5583. return 1;
  5584. }
  5585. for(i=0; i<nb_it; i++) {
  5586. nameid[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5587. script_removetop(st, -1, 0);
  5588. amount[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5589. script_removetop(st, -1, 0);
  5590. } //end loop DO NOT break it prematurly we need to depop all stack
  5591. script_pushint(st,checkweight_sub(sd,nb_it,nameid,amount)); //push result of sub to script
  5592. return SCRIPT_CMD_SUCCESS;
  5593. }
  5594. /*==========================================
  5595. * getitem <item id>,<amount>{,<account ID>};
  5596. * getitem "<item name>",<amount>{,<account ID>};
  5597. *
  5598. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5599. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5600. * Type:
  5601. * 1 - Account Bound
  5602. * 2 - Guild Bound
  5603. * 3 - Party Bound
  5604. * 4 - Character Bound
  5605. *------------------------------------------*/
  5606. BUILDIN_FUNC(getitem)
  5607. {
  5608. int nameid,amount,get_count,i,flag = 0;
  5609. struct item it;
  5610. TBL_PC *sd;
  5611. struct script_data *data;
  5612. data=script_getdata(st,2);
  5613. get_val(st,data);
  5614. if( data_isstring(data) ) {// "<item name>"
  5615. const char *name=conv_str(st,data);
  5616. struct item_data *item_data = itemdb_searchname(name);
  5617. if( item_data == NULL ){
  5618. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5619. return 1; //No item created.
  5620. }
  5621. nameid=item_data->nameid;
  5622. } else if( data_isint(data) ) {// <item id>
  5623. nameid=conv_num(st,data);
  5624. //Violet Box, Blue Box, etc - random item pick
  5625. if( nameid < 0 ) {
  5626. nameid = -nameid;
  5627. flag = 1;
  5628. }
  5629. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5630. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5631. return 1; //No item created.
  5632. }
  5633. } else {
  5634. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5635. return 1;
  5636. }
  5637. // <amount>
  5638. if( (amount=script_getnum(st,3)) <= 0)
  5639. return 0; //return if amount <=0, skip the useles iteration
  5640. memset(&it,0,sizeof(it));
  5641. it.nameid=nameid;
  5642. if(!flag)
  5643. it.identify=1;
  5644. else
  5645. it.identify=itemdb_isidentified(nameid);
  5646. if( !strcmp(script_getfuncname(st),"getitembound") ) {
  5647. char bound = script_getnum(st,4);
  5648. if( bound < 1 || bound > 4) { //Not a correct bound type
  5649. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5650. return 1;
  5651. }
  5652. it.bound = bound;
  5653. if( script_hasdata(st,5) )
  5654. sd=map_id2sd(script_getnum(st,5));
  5655. else
  5656. sd=script_rid2sd(st); // Attached player
  5657. } else if( script_hasdata(st,4) )
  5658. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5659. else
  5660. sd=script_rid2sd(st); // Attached player
  5661. if( sd == NULL ) // no target
  5662. return 0;
  5663. //Check if it's stackable.
  5664. if (!itemdb_isstackable(nameid))
  5665. get_count = 1;
  5666. else
  5667. get_count = amount;
  5668. for (i = 0; i < amount; i += get_count)
  5669. {
  5670. // if not pet egg
  5671. if (!pet_create_egg(sd, nameid))
  5672. {
  5673. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5674. {
  5675. clif_additem(sd, 0, 0, flag);
  5676. if( pc_candrop(sd,&it) )
  5677. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5678. }
  5679. }
  5680. }
  5681. return SCRIPT_CMD_SUCCESS;
  5682. }
  5683. /*==========================================
  5684. *
  5685. *------------------------------------------*/
  5686. BUILDIN_FUNC(getitem2)
  5687. {
  5688. int nameid,amount,get_count,i,flag = 0;
  5689. int iden,ref,attr,c1,c2,c3,c4;
  5690. char bound=0;
  5691. struct item_data *item_data;
  5692. struct item item_tmp;
  5693. TBL_PC *sd;
  5694. struct script_data *data;
  5695. if( !strcmp(script_getfuncname(st),"getitembound2") ) {
  5696. bound = script_getnum(st,11);
  5697. if( bound < 1 || bound > 3) { //Not a correct bound type
  5698. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  5699. return 1;
  5700. }
  5701. if( script_hasdata(st,12) )
  5702. sd=map_id2sd(script_getnum(st,12));
  5703. else
  5704. sd=script_rid2sd(st); // Attached player
  5705. } else if( script_hasdata(st,11) )
  5706. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5707. else
  5708. sd=script_rid2sd(st); // Attached player
  5709. if( sd == NULL ) // no target
  5710. return 0;
  5711. data=script_getdata(st,2);
  5712. get_val(st,data);
  5713. if( data_isstring(data) ) {
  5714. const char *name=conv_str(st,data);
  5715. struct item_data *item_data = itemdb_searchname(name);
  5716. if( item_data )
  5717. nameid=item_data->nameid;
  5718. else
  5719. nameid=UNKNOWN_ITEM_ID;
  5720. } else
  5721. nameid=conv_num(st,data);
  5722. amount=script_getnum(st,3);
  5723. iden=script_getnum(st,4);
  5724. ref=script_getnum(st,5);
  5725. attr=script_getnum(st,6);
  5726. c1=(short)script_getnum(st,7);
  5727. c2=(short)script_getnum(st,8);
  5728. c3=(short)script_getnum(st,9);
  5729. c4=(short)script_getnum(st,10);
  5730. if(nameid<0) { // Invalide nameid
  5731. nameid = -nameid;
  5732. flag = 1;
  5733. }
  5734. if(nameid > 0) {
  5735. memset(&item_tmp,0,sizeof(item_tmp));
  5736. item_data=itemdb_exists(nameid);
  5737. if (item_data == NULL)
  5738. return -1;
  5739. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR || item_data->type==IT_SHADOWGEAR ) {
  5740. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5741. }
  5742. else if(item_data->type==IT_PETEGG) {
  5743. iden = 1;
  5744. ref = 0;
  5745. }
  5746. else {
  5747. iden = 1;
  5748. ref = attr = 0;
  5749. }
  5750. item_tmp.nameid=nameid;
  5751. if(!flag)
  5752. item_tmp.identify=iden;
  5753. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR || item_data->type==IT_SHADOWGEAR )
  5754. item_tmp.identify=0;
  5755. item_tmp.refine=ref;
  5756. item_tmp.attribute=attr;
  5757. item_tmp.card[0]=(short)c1;
  5758. item_tmp.card[1]=(short)c2;
  5759. item_tmp.card[2]=(short)c3;
  5760. item_tmp.card[3]=(short)c4;
  5761. item_tmp.bound=bound;
  5762. //Check if it's stackable.
  5763. if (!itemdb_isstackable(nameid))
  5764. get_count = 1;
  5765. else
  5766. get_count = amount;
  5767. for (i = 0; i < amount; i += get_count)
  5768. {
  5769. // if not pet egg
  5770. if (!pet_create_egg(sd, nameid))
  5771. {
  5772. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5773. {
  5774. clif_additem(sd, 0, 0, flag);
  5775. if( pc_candrop(sd,&item_tmp) )
  5776. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5777. }
  5778. }
  5779. }
  5780. }
  5781. return SCRIPT_CMD_SUCCESS;
  5782. }
  5783. /*==========================================
  5784. * rentitem <item id>,<seconds>
  5785. * rentitem "<item name>",<seconds>
  5786. *------------------------------------------*/
  5787. BUILDIN_FUNC(rentitem)
  5788. {
  5789. struct map_session_data *sd;
  5790. struct script_data *data;
  5791. struct item it;
  5792. int seconds;
  5793. int nameid = 0, flag;
  5794. data = script_getdata(st,2);
  5795. get_val(st,data);
  5796. if( (sd = script_rid2sd(st)) == NULL )
  5797. return 0;
  5798. if( data_isstring(data) )
  5799. {
  5800. const char *name = conv_str(st,data);
  5801. struct item_data *itd = itemdb_searchname(name);
  5802. if( itd == NULL )
  5803. {
  5804. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5805. return 1;
  5806. }
  5807. nameid = itd->nameid;
  5808. }
  5809. else if( data_isint(data) )
  5810. {
  5811. nameid = conv_num(st,data);
  5812. if( nameid <= 0 || !itemdb_exists(nameid) )
  5813. {
  5814. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5815. return 1;
  5816. }
  5817. }
  5818. else
  5819. {
  5820. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5821. return 1;
  5822. }
  5823. seconds = script_getnum(st,3);
  5824. memset(&it, 0, sizeof(it));
  5825. it.nameid = nameid;
  5826. it.identify = 1;
  5827. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5828. it.bound = 0;
  5829. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5830. {
  5831. clif_additem(sd, 0, 0, flag);
  5832. return 1;
  5833. }
  5834. return SCRIPT_CMD_SUCCESS;
  5835. }
  5836. /*==========================================
  5837. * gets an item with someone's name inscribed [Skotlex]
  5838. * getinscribeditem item_num, character_name
  5839. * Returned Qty is always 1, only works on equip-able
  5840. * equipment
  5841. *------------------------------------------*/
  5842. BUILDIN_FUNC(getnameditem)
  5843. {
  5844. int nameid;
  5845. struct item item_tmp;
  5846. TBL_PC *sd, *tsd;
  5847. struct script_data *data;
  5848. sd = script_rid2sd(st);
  5849. if (sd == NULL)
  5850. { //Player not attached!
  5851. script_pushint(st,0);
  5852. return 0;
  5853. }
  5854. data=script_getdata(st,2);
  5855. get_val(st,data);
  5856. if( data_isstring(data) ){
  5857. const char *name=conv_str(st,data);
  5858. struct item_data *item_data = itemdb_searchname(name);
  5859. if( item_data == NULL)
  5860. { //Failed
  5861. script_pushint(st,0);
  5862. return 0;
  5863. }
  5864. nameid = item_data->nameid;
  5865. }else
  5866. nameid = conv_num(st,data);
  5867. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5868. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5869. script_pushint(st,0);
  5870. return 0;
  5871. }
  5872. data=script_getdata(st,3);
  5873. get_val(st,data);
  5874. if( data_isstring(data) ) //Char Name
  5875. tsd=map_nick2sd(conv_str(st,data));
  5876. else //Char Id was given
  5877. tsd=map_charid2sd(conv_num(st,data));
  5878. if( tsd == NULL )
  5879. { //Failed
  5880. script_pushint(st,0);
  5881. return 0;
  5882. }
  5883. memset(&item_tmp,0,sizeof(item_tmp));
  5884. item_tmp.nameid=nameid;
  5885. item_tmp.amount=1;
  5886. item_tmp.identify=1;
  5887. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5888. item_tmp.card[2]=tsd->status.char_id;
  5889. item_tmp.card[3]=tsd->status.char_id >> 16;
  5890. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  5891. script_pushint(st,0);
  5892. return 0; //Failed to add item, we will not drop if they don't fit
  5893. }
  5894. script_pushint(st,1);
  5895. return SCRIPT_CMD_SUCCESS;
  5896. }
  5897. /*==========================================
  5898. * gets a random item ID from an item group [Skotlex]
  5899. * groupranditem <group_num>{,<sub_group>};
  5900. *------------------------------------------*/
  5901. BUILDIN_FUNC(grouprandomitem)
  5902. {
  5903. script_pushint(st,itemdb_searchrandomid(script_getnum(st,2),script_getnum(st,3)));
  5904. return SCRIPT_CMD_SUCCESS;
  5905. }
  5906. /*==========================================
  5907. *
  5908. *------------------------------------------*/
  5909. BUILDIN_FUNC(makeitem)
  5910. {
  5911. int nameid,amount,flag = 0;
  5912. int x,y,m;
  5913. const char *mapname;
  5914. struct item item_tmp;
  5915. struct script_data *data;
  5916. data=script_getdata(st,2);
  5917. get_val(st,data);
  5918. if( data_isstring(data) ){
  5919. const char *name=conv_str(st,data);
  5920. struct item_data *item_data = itemdb_searchname(name);
  5921. if( item_data )
  5922. nameid=item_data->nameid;
  5923. else
  5924. nameid=UNKNOWN_ITEM_ID;
  5925. }else
  5926. nameid=conv_num(st,data);
  5927. amount=script_getnum(st,3);
  5928. mapname =script_getstr(st,4);
  5929. x =script_getnum(st,5);
  5930. y =script_getnum(st,6);
  5931. if(strcmp(mapname,"this")==0)
  5932. {
  5933. TBL_PC *sd;
  5934. sd = script_rid2sd(st);
  5935. if (!sd) return 0; //Failed...
  5936. m=sd->bl.m;
  5937. } else
  5938. m=map_mapname2mapid(mapname);
  5939. if(nameid<0) {
  5940. nameid = -nameid;
  5941. flag = 1;
  5942. }
  5943. if(nameid > 0) {
  5944. memset(&item_tmp,0,sizeof(item_tmp));
  5945. item_tmp.nameid=nameid;
  5946. if(!flag)
  5947. item_tmp.identify=1;
  5948. else
  5949. item_tmp.identify=itemdb_isidentified(nameid);
  5950. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  5951. }
  5952. return SCRIPT_CMD_SUCCESS;
  5953. }
  5954. /// Counts / deletes the current item given by idx.
  5955. /// Used by buildin_delitem_search
  5956. /// Relies on all input data being already fully valid.
  5957. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5958. {
  5959. int delamount;
  5960. struct item* inv = &sd->status.inventory[idx];
  5961. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5962. if( delete_items )
  5963. {
  5964. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5965. {// delete associated pet
  5966. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5967. }
  5968. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  5969. }
  5970. amount[0]-= delamount;
  5971. }
  5972. /// Searches for item(s) and checks, if there is enough of them.
  5973. /// Used by delitem and delitem2
  5974. /// Relies on all input data being already fully valid.
  5975. /// @param exact_match will also match item attributes and cards, not just name id
  5976. /// @return true when all items could be deleted, false when there were not enough items to delete
  5977. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5978. {
  5979. bool delete_items = false;
  5980. int i, amount, important;
  5981. struct item* inv;
  5982. // prefer always non-equipped items
  5983. it->equip = 0;
  5984. // when searching for nameid only, prefer additionally
  5985. if( !exact_match )
  5986. {
  5987. // non-refined items
  5988. it->refine = 0;
  5989. // card-less items
  5990. memset(it->card, 0, sizeof(it->card));
  5991. }
  5992. for(;;)
  5993. {
  5994. amount = it->amount;
  5995. important = 0;
  5996. // 1st pass -- less important items / exact match
  5997. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5998. {
  5999. inv = &sd->status.inventory[i];
  6000. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  6001. {// wrong/invalid item
  6002. continue;
  6003. }
  6004. if( inv->equip != it->equip || inv->refine != it->refine )
  6005. {// not matching attributes
  6006. important++;
  6007. continue;
  6008. }
  6009. if( exact_match )
  6010. {
  6011. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  6012. {// not matching exact attributes
  6013. continue;
  6014. }
  6015. }
  6016. else
  6017. {
  6018. if( sd->inventory_data[i]->type == IT_PETEGG )
  6019. {
  6020. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  6021. {// pet which cannot be deleted
  6022. continue;
  6023. }
  6024. }
  6025. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  6026. {// named/carded item
  6027. important++;
  6028. continue;
  6029. }
  6030. }
  6031. // count / delete item
  6032. buildin_delitem_delete(sd, i, &amount, delete_items);
  6033. }
  6034. // 2nd pass -- any matching item
  6035. if( amount == 0 || important == 0 )
  6036. {// either everything was already consumed or no items were skipped
  6037. ;
  6038. }
  6039. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  6040. {
  6041. inv = &sd->status.inventory[i];
  6042. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  6043. {// wrong/invalid item
  6044. continue;
  6045. }
  6046. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  6047. {// pet which cannot be deleted
  6048. continue;
  6049. }
  6050. if( exact_match )
  6051. {
  6052. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  6053. {// not matching attributes
  6054. continue;
  6055. }
  6056. }
  6057. // count / delete item
  6058. buildin_delitem_delete(sd, i, &amount, delete_items);
  6059. }
  6060. if( amount )
  6061. {// not enough items
  6062. return false;
  6063. }
  6064. else if( delete_items )
  6065. {// we are done with the work
  6066. return true;
  6067. }
  6068. else
  6069. {// get rid of the items now
  6070. delete_items = true;
  6071. }
  6072. }
  6073. }
  6074. /// Deletes items from the target/attached player.
  6075. /// Prioritizes ordinary items.
  6076. ///
  6077. /// delitem <item id>,<amount>{,<account id>}
  6078. /// delitem "<item name>",<amount>{,<account id>}
  6079. BUILDIN_FUNC(delitem)
  6080. {
  6081. TBL_PC *sd;
  6082. struct item it;
  6083. struct script_data *data;
  6084. if( script_hasdata(st,4) )
  6085. {
  6086. int account_id = script_getnum(st,4);
  6087. sd = map_id2sd(account_id); // <account id>
  6088. if( sd == NULL )
  6089. {
  6090. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  6091. st->state = END;
  6092. return 1;
  6093. }
  6094. }
  6095. else
  6096. {
  6097. sd = script_rid2sd(st);// attached player
  6098. if( sd == NULL )
  6099. return 0;
  6100. }
  6101. data = script_getdata(st,2);
  6102. get_val(st,data);
  6103. if( data_isstring(data) )
  6104. {
  6105. const char* item_name = conv_str(st,data);
  6106. struct item_data* id = itemdb_searchname(item_name);
  6107. if( id == NULL )
  6108. {
  6109. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  6110. st->state = END;
  6111. return 1;
  6112. }
  6113. it.nameid = id->nameid;// "<item name>"
  6114. }
  6115. else
  6116. {
  6117. it.nameid = conv_num(st,data);// <item id>
  6118. if( !itemdb_exists( it.nameid ) )
  6119. {
  6120. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6121. st->state = END;
  6122. return 1;
  6123. }
  6124. }
  6125. it.amount=script_getnum(st,3);
  6126. if( it.amount <= 0 )
  6127. return 0;// nothing to do
  6128. if( buildin_delitem_search(sd, &it, false) )
  6129. {// success
  6130. return SCRIPT_CMD_SUCCESS;
  6131. }
  6132. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6133. st->state = END;
  6134. st->mes_active = 0;
  6135. clif_scriptclose(sd, st->oid);
  6136. return SCRIPT_CMD_FAILURE;
  6137. }
  6138. /// Deletes items from the target/attached player.
  6139. ///
  6140. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6141. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6142. BUILDIN_FUNC(delitem2)
  6143. {
  6144. TBL_PC *sd;
  6145. struct item it;
  6146. struct script_data *data;
  6147. if( script_hasdata(st,11) )
  6148. {
  6149. int account_id = script_getnum(st,11);
  6150. sd = map_id2sd(account_id); // <account id>
  6151. if( sd == NULL )
  6152. {
  6153. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  6154. st->state = END;
  6155. return 1;
  6156. }
  6157. }
  6158. else
  6159. {
  6160. sd = script_rid2sd(st);// attached player
  6161. if( sd == NULL )
  6162. return 0;
  6163. }
  6164. data = script_getdata(st,2);
  6165. get_val(st,data);
  6166. if( data_isstring(data) )
  6167. {
  6168. const char* item_name = conv_str(st,data);
  6169. struct item_data* id = itemdb_searchname(item_name);
  6170. if( id == NULL )
  6171. {
  6172. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  6173. st->state = END;
  6174. return 1;
  6175. }
  6176. it.nameid = id->nameid;// "<item name>"
  6177. }
  6178. else
  6179. {
  6180. it.nameid = conv_num(st,data);// <item id>
  6181. if( !itemdb_exists( it.nameid ) )
  6182. {
  6183. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6184. st->state = END;
  6185. return 1;
  6186. }
  6187. }
  6188. it.amount=script_getnum(st,3);
  6189. it.identify=script_getnum(st,4);
  6190. it.refine=script_getnum(st,5);
  6191. it.attribute=script_getnum(st,6);
  6192. it.card[0]=(short)script_getnum(st,7);
  6193. it.card[1]=(short)script_getnum(st,8);
  6194. it.card[2]=(short)script_getnum(st,9);
  6195. it.card[3]=(short)script_getnum(st,10);
  6196. if( it.amount <= 0 )
  6197. return 0;// nothing to do
  6198. if( buildin_delitem_search(sd, &it, true) )
  6199. {// success
  6200. return SCRIPT_CMD_SUCCESS;
  6201. }
  6202. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6203. st->state = END;
  6204. st->mes_active = 0;
  6205. clif_scriptclose(sd, st->oid);
  6206. return SCRIPT_CMD_FAILURE;
  6207. }
  6208. /*==========================================
  6209. * Enables/Disables use of items while in an NPC [Skotlex]
  6210. *------------------------------------------*/
  6211. BUILDIN_FUNC(enableitemuse)
  6212. {
  6213. TBL_PC *sd;
  6214. sd=script_rid2sd(st);
  6215. if (sd)
  6216. st->npc_item_flag = sd->npc_item_flag = 1;
  6217. return SCRIPT_CMD_SUCCESS;
  6218. }
  6219. BUILDIN_FUNC(disableitemuse)
  6220. {
  6221. TBL_PC *sd;
  6222. sd=script_rid2sd(st);
  6223. if (sd)
  6224. st->npc_item_flag = sd->npc_item_flag = 0;
  6225. return SCRIPT_CMD_SUCCESS;
  6226. }
  6227. /*==========================================
  6228. * Returns a character's specified stat.
  6229. * Check pc_readparam for available options.
  6230. *------------------------------------------*/
  6231. BUILDIN_FUNC(readparam)
  6232. {
  6233. int type;
  6234. TBL_PC *sd;
  6235. type=script_getnum(st,2);
  6236. if( script_hasdata(st,3) )
  6237. sd=map_nick2sd(script_getstr(st,3));
  6238. else
  6239. sd=script_rid2sd(st);
  6240. if(sd==NULL){
  6241. script_pushint(st,-1);
  6242. return 0;
  6243. }
  6244. script_pushint(st,pc_readparam(sd,type));
  6245. return SCRIPT_CMD_SUCCESS;
  6246. }
  6247. /*==========================================
  6248. * Return charid identification
  6249. * return by @num :
  6250. * 0 : char_id
  6251. * 1 : party_id
  6252. * 2 : guild_id
  6253. * 3 : account_id
  6254. * 4 : bg_id
  6255. *------------------------------------------*/
  6256. BUILDIN_FUNC(getcharid)
  6257. {
  6258. int num;
  6259. TBL_PC *sd;
  6260. num = script_getnum(st,2);
  6261. if( script_hasdata(st,3) )
  6262. sd=map_nick2sd(script_getstr(st,3));
  6263. else
  6264. sd=script_rid2sd(st);
  6265. if(sd==NULL){
  6266. script_pushint(st,0); //return 0, according docs
  6267. return 0;
  6268. }
  6269. switch( num ) {
  6270. case 0: script_pushint(st,sd->status.char_id); break;
  6271. case 1: script_pushint(st,sd->status.party_id); break;
  6272. case 2: script_pushint(st,sd->status.guild_id); break;
  6273. case 3: script_pushint(st,sd->status.account_id); break;
  6274. case 4: script_pushint(st,sd->bg_id); break;
  6275. default:
  6276. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6277. script_pushint(st,0);
  6278. break;
  6279. }
  6280. return SCRIPT_CMD_SUCCESS;
  6281. }
  6282. /*==========================================
  6283. * returns the GID of an NPC
  6284. *------------------------------------------*/
  6285. BUILDIN_FUNC(getnpcid)
  6286. {
  6287. int num = script_getnum(st,2);
  6288. struct npc_data* nd = NULL;
  6289. if( script_hasdata(st,3) )
  6290. {// unique npc name
  6291. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6292. {
  6293. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6294. script_pushint(st,0);
  6295. return 1;
  6296. }
  6297. }
  6298. switch (num) {
  6299. case 0:
  6300. script_pushint(st,nd ? nd->bl.id : st->oid);
  6301. break;
  6302. default:
  6303. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6304. script_pushint(st,0);
  6305. return 1;
  6306. }
  6307. return SCRIPT_CMD_SUCCESS;
  6308. }
  6309. /*==========================================
  6310. * Return the name of the party_id
  6311. * null if not found
  6312. *------------------------------------------*/
  6313. BUILDIN_FUNC(getpartyname)
  6314. {
  6315. int party_id;
  6316. struct party_data* p;
  6317. party_id = script_getnum(st,2);
  6318. if( ( p = party_search(party_id) ) != NULL )
  6319. {
  6320. script_pushstrcopy(st,p->party.name);
  6321. }
  6322. else
  6323. {
  6324. script_pushconststr(st,"null");
  6325. }
  6326. return SCRIPT_CMD_SUCCESS;
  6327. }
  6328. /*==========================================
  6329. * Get the information of the members of a party by type
  6330. * @party_id, @type
  6331. * return by @type :
  6332. * - : nom des membres
  6333. * 1 : char_id des membres
  6334. * 2 : account_id des membres
  6335. *------------------------------------------*/
  6336. BUILDIN_FUNC(getpartymember)
  6337. {
  6338. struct party_data *p;
  6339. int i,j=0,type=0;
  6340. p=party_search(script_getnum(st,2));
  6341. if( script_hasdata(st,3) )
  6342. type=script_getnum(st,3);
  6343. if(p!=NULL){
  6344. for(i=0;i<MAX_PARTY;i++){
  6345. if(p->party.member[i].account_id){
  6346. switch (type) {
  6347. case 2:
  6348. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6349. break;
  6350. case 1:
  6351. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6352. break;
  6353. default:
  6354. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6355. }
  6356. j++;
  6357. }
  6358. }
  6359. }
  6360. mapreg_setreg(add_str("$@partymembercount"),j);
  6361. return SCRIPT_CMD_SUCCESS;
  6362. }
  6363. /*==========================================
  6364. * Retrieves party leader. if flag is specified,
  6365. * return some of the leader data. Otherwise, return name.
  6366. *------------------------------------------*/
  6367. BUILDIN_FUNC(getpartyleader)
  6368. {
  6369. int party_id, type = 0, i=0;
  6370. struct party_data *p;
  6371. party_id=script_getnum(st,2);
  6372. if( script_hasdata(st,3) )
  6373. type=script_getnum(st,3);
  6374. p=party_search(party_id);
  6375. if (p) //Search leader
  6376. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6377. if (!p || i == MAX_PARTY) { //leader not found
  6378. if (type)
  6379. script_pushint(st,-1);
  6380. else
  6381. script_pushconststr(st,"null");
  6382. return 0;
  6383. }
  6384. switch (type) {
  6385. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6386. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6387. case 3: script_pushint(st,p->party.member[i].class_); break;
  6388. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6389. case 5: script_pushint(st,p->party.member[i].lv); break;
  6390. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6391. }
  6392. return SCRIPT_CMD_SUCCESS;
  6393. }
  6394. /*==========================================
  6395. * Return the name of the @guild_id
  6396. * null if not found
  6397. *------------------------------------------*/
  6398. BUILDIN_FUNC(getguildname)
  6399. {
  6400. int guild_id;
  6401. struct guild* g;
  6402. guild_id = script_getnum(st,2);
  6403. if( ( g = guild_search(guild_id) ) != NULL )
  6404. script_pushstrcopy(st,g->name);
  6405. else
  6406. script_pushconststr(st,"null");
  6407. return SCRIPT_CMD_SUCCESS;
  6408. }
  6409. /*==========================================
  6410. * Return the name of the guild master of @guild_id
  6411. * null if not found
  6412. *------------------------------------------*/
  6413. BUILDIN_FUNC(getguildmaster)
  6414. {
  6415. int guild_id;
  6416. struct guild* g;
  6417. guild_id = script_getnum(st,2);
  6418. if( ( g = guild_search(guild_id) ) != NULL )
  6419. script_pushstrcopy(st,g->member[0].name);
  6420. else
  6421. script_pushconststr(st,"null");
  6422. return SCRIPT_CMD_SUCCESS;
  6423. }
  6424. BUILDIN_FUNC(getguildmasterid)
  6425. {
  6426. int guild_id;
  6427. struct guild* g;
  6428. guild_id = script_getnum(st,2);
  6429. if( ( g = guild_search(guild_id) ) != NULL )
  6430. script_pushint(st,g->member[0].char_id);
  6431. else
  6432. script_pushint(st,0);
  6433. return SCRIPT_CMD_SUCCESS;
  6434. }
  6435. /*==========================================
  6436. * Get char string information by type :
  6437. * Return by @type :
  6438. * 0 : char_name
  6439. * 1 : party_name or ""
  6440. * 2 : guild_name or ""
  6441. * 3 : map_name
  6442. * - : ""
  6443. *------------------------------------------*/
  6444. BUILDIN_FUNC(strcharinfo)
  6445. {
  6446. TBL_PC *sd;
  6447. int num;
  6448. struct guild* g;
  6449. struct party_data* p;
  6450. sd=script_rid2sd(st);
  6451. if (!sd) { //Avoid crashing....
  6452. script_pushconststr(st,"");
  6453. return 0;
  6454. }
  6455. num=script_getnum(st,2);
  6456. switch(num){
  6457. case 0:
  6458. script_pushstrcopy(st,sd->status.name);
  6459. break;
  6460. case 1:
  6461. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  6462. script_pushstrcopy(st,p->party.name);
  6463. } else {
  6464. script_pushconststr(st,"");
  6465. }
  6466. break;
  6467. case 2:
  6468. if( ( g = sd->guild ) != NULL ) {
  6469. script_pushstrcopy(st,g->name);
  6470. } else {
  6471. script_pushconststr(st,"");
  6472. }
  6473. break;
  6474. case 3:
  6475. script_pushconststr(st,map[sd->bl.m].name);
  6476. break;
  6477. default:
  6478. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6479. script_pushconststr(st,"");
  6480. break;
  6481. }
  6482. return SCRIPT_CMD_SUCCESS;
  6483. }
  6484. /*==========================================
  6485. * Get npc string information by type
  6486. * return by @type:
  6487. * 0 : name
  6488. * 1 : str#
  6489. * 2 : #str
  6490. * 3 : ::str
  6491. * 4 : map name
  6492. *------------------------------------------*/
  6493. BUILDIN_FUNC(strnpcinfo)
  6494. {
  6495. TBL_NPC* nd;
  6496. int num;
  6497. char *buf,*name=NULL;
  6498. nd = map_id2nd(st->oid);
  6499. if (!nd) {
  6500. script_pushconststr(st, "");
  6501. return 0;
  6502. }
  6503. num = script_getnum(st,2);
  6504. switch(num){
  6505. case 0: // display name
  6506. name = aStrdup(nd->name);
  6507. break;
  6508. case 1: // visible part of display name
  6509. if((buf = strchr(nd->name,'#')) != NULL)
  6510. {
  6511. name = aStrdup(nd->name);
  6512. name[buf - nd->name] = 0;
  6513. } else // Return the name, there is no '#' present
  6514. name = aStrdup(nd->name);
  6515. break;
  6516. case 2: // # fragment
  6517. if((buf = strchr(nd->name,'#')) != NULL)
  6518. name = aStrdup(buf+1);
  6519. break;
  6520. case 3: // unique name
  6521. name = aStrdup(nd->exname);
  6522. break;
  6523. case 4: // map name
  6524. name = aStrdup(map[nd->bl.m].name);
  6525. break;
  6526. }
  6527. if(name)
  6528. script_pushstr(st, name);
  6529. else
  6530. script_pushconststr(st, "");
  6531. return SCRIPT_CMD_SUCCESS;
  6532. }
  6533. // aegis->athena slot position conversion table
  6534. 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};
  6535. /*==========================================
  6536. * GetEquipID(Pos); Pos: 1-14
  6537. *------------------------------------------*/
  6538. BUILDIN_FUNC(getequipid)
  6539. {
  6540. int i, num;
  6541. TBL_PC* sd;
  6542. struct item_data* item;
  6543. sd = script_rid2sd(st);
  6544. if( sd == NULL )
  6545. return 0;
  6546. num = script_getnum(st,2) - 1;
  6547. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6548. {
  6549. script_pushint(st,-1);
  6550. return 0;
  6551. }
  6552. // get inventory position of item
  6553. i = pc_checkequip(sd,equip[num]);
  6554. if( i < 0 )
  6555. {
  6556. script_pushint(st,-1);
  6557. return 0;
  6558. }
  6559. item = sd->inventory_data[i];
  6560. if( item != 0 )
  6561. script_pushint(st,item->nameid);
  6562. else
  6563. script_pushint(st,0);
  6564. return SCRIPT_CMD_SUCCESS;
  6565. }
  6566. /*==========================================
  6567. * Get the equipement name at pos
  6568. * return item jname or ""
  6569. *------------------------------------------*/
  6570. BUILDIN_FUNC(getequipname)
  6571. {
  6572. int i, num;
  6573. TBL_PC* sd;
  6574. struct item_data* item;
  6575. sd = script_rid2sd(st);
  6576. if( sd == NULL )
  6577. return 0;
  6578. num = script_getnum(st,2) - 1;
  6579. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6580. {
  6581. script_pushconststr(st,"");
  6582. return 0;
  6583. }
  6584. // get inventory position of item
  6585. i = pc_checkequip(sd,equip[num]);
  6586. if( i < 0 )
  6587. {
  6588. script_pushconststr(st,"");
  6589. return 0;
  6590. }
  6591. item = sd->inventory_data[i];
  6592. if( item != 0 )
  6593. script_pushstrcopy(st,item->jname);
  6594. else
  6595. script_pushconststr(st,"");
  6596. return SCRIPT_CMD_SUCCESS;
  6597. }
  6598. /*==========================================
  6599. * getbrokenid [Valaris]
  6600. *------------------------------------------*/
  6601. BUILDIN_FUNC(getbrokenid)
  6602. {
  6603. int i,num,id=0,brokencounter=0;
  6604. TBL_PC *sd;
  6605. sd = script_rid2sd(st);
  6606. if( sd == NULL )
  6607. return 0;
  6608. num=script_getnum(st,2);
  6609. for(i=0; i<MAX_INVENTORY; i++) {
  6610. if(sd->status.inventory[i].attribute){
  6611. brokencounter++;
  6612. if(num==brokencounter){
  6613. id=sd->status.inventory[i].nameid;
  6614. break;
  6615. }
  6616. }
  6617. }
  6618. script_pushint(st,id);
  6619. return SCRIPT_CMD_SUCCESS;
  6620. }
  6621. /*==========================================
  6622. * repair [Valaris]
  6623. *------------------------------------------*/
  6624. BUILDIN_FUNC(repair)
  6625. {
  6626. int i,num;
  6627. int repaircounter=0;
  6628. TBL_PC *sd;
  6629. sd = script_rid2sd(st);
  6630. if( sd == NULL )
  6631. return 0;
  6632. num=script_getnum(st,2);
  6633. for(i=0; i<MAX_INVENTORY; i++) {
  6634. if(sd->status.inventory[i].attribute){
  6635. repaircounter++;
  6636. if(num==repaircounter){
  6637. sd->status.inventory[i].attribute=0;
  6638. clif_equiplist(sd);
  6639. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6640. clif_misceffect(&sd->bl, 3);
  6641. break;
  6642. }
  6643. }
  6644. }
  6645. return SCRIPT_CMD_SUCCESS;
  6646. }
  6647. /*==========================================
  6648. * repairall
  6649. *------------------------------------------*/
  6650. BUILDIN_FUNC(repairall)
  6651. {
  6652. int i, repaircounter = 0;
  6653. TBL_PC *sd;
  6654. sd = script_rid2sd(st);
  6655. if(sd == NULL)
  6656. return 0;
  6657. for(i = 0; i < MAX_INVENTORY; i++)
  6658. {
  6659. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  6660. {
  6661. sd->status.inventory[i].attribute = 0;
  6662. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  6663. repaircounter++;
  6664. }
  6665. }
  6666. if(repaircounter)
  6667. {
  6668. clif_misceffect(&sd->bl, 3);
  6669. clif_equiplist(sd);
  6670. }
  6671. return SCRIPT_CMD_SUCCESS;
  6672. }
  6673. /*==========================================
  6674. * Chk if player have something equiped at pos
  6675. *------------------------------------------*/
  6676. BUILDIN_FUNC(getequipisequiped)
  6677. {
  6678. int i = -1,num;
  6679. TBL_PC *sd;
  6680. num = script_getnum(st,2);
  6681. sd = script_rid2sd(st);
  6682. if( sd == NULL )
  6683. return 0;
  6684. if (num > 0 && num <= ARRAYLENGTH(equip))
  6685. i=pc_checkequip(sd,equip[num-1]);
  6686. if(i >= 0)
  6687. script_pushint(st,1);
  6688. else
  6689. script_pushint(st,0);
  6690. return SCRIPT_CMD_SUCCESS;
  6691. }
  6692. /*==========================================
  6693. * Chk if the player have something equiped at pos
  6694. * if so chk if this item ain't marked not refinable or rental
  6695. * return (npc)
  6696. * 1 : true
  6697. * 0 : false
  6698. *------------------------------------------*/
  6699. BUILDIN_FUNC(getequipisenableref)
  6700. {
  6701. int i = -1,num;
  6702. TBL_PC *sd;
  6703. num = script_getnum(st,2);
  6704. sd = script_rid2sd(st);
  6705. if( sd == NULL )
  6706. return 0;
  6707. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6708. i = pc_checkequip(sd,equip[num-1]);
  6709. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6710. script_pushint(st,1);
  6711. else
  6712. script_pushint(st,0);
  6713. return SCRIPT_CMD_SUCCESS;
  6714. }
  6715. /*==========================================
  6716. * Get the item refined value at pos
  6717. * return (npc)
  6718. * x : refine amount
  6719. * 0 : false (not refined)
  6720. *------------------------------------------*/
  6721. BUILDIN_FUNC(getequiprefinerycnt)
  6722. {
  6723. int i = -1,num;
  6724. TBL_PC *sd;
  6725. num = script_getnum(st,2);
  6726. sd = script_rid2sd(st);
  6727. if( sd == NULL )
  6728. return 0;
  6729. if (num > 0 && num <= ARRAYLENGTH(equip))
  6730. i=pc_checkequip(sd,equip[num-1]);
  6731. if(i >= 0)
  6732. script_pushint(st,sd->status.inventory[i].refine);
  6733. else
  6734. script_pushint(st,0);
  6735. return SCRIPT_CMD_SUCCESS;
  6736. }
  6737. /*==========================================
  6738. * Get the weapon level value at pos
  6739. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  6740. * return (npc)
  6741. * x : weapon level
  6742. * 0 : false
  6743. *------------------------------------------*/
  6744. BUILDIN_FUNC(getequipweaponlv)
  6745. {
  6746. int i = -1,num;
  6747. TBL_PC *sd;
  6748. num = script_getnum(st,2);
  6749. sd = script_rid2sd(st);
  6750. if( sd == NULL )
  6751. return 0;
  6752. if (num > 0 && num <= ARRAYLENGTH(equip))
  6753. i=pc_checkequip(sd,equip[num-1]);
  6754. if(i >= 0 && sd->inventory_data[i])
  6755. script_pushint(st,sd->inventory_data[i]->wlv);
  6756. else
  6757. script_pushint(st,0);
  6758. return SCRIPT_CMD_SUCCESS;
  6759. }
  6760. /*==========================================
  6761. * Get the item refine chance (from refine.txt) for item at pos
  6762. * return (npc)
  6763. * x : refine chance
  6764. * 0 : false (max refine level or unequip..)
  6765. *------------------------------------------*/
  6766. BUILDIN_FUNC(getequippercentrefinery)
  6767. {
  6768. int i = -1,num;
  6769. TBL_PC *sd;
  6770. num = script_getnum(st,2);
  6771. sd = script_rid2sd(st);
  6772. if( sd == NULL )
  6773. return 0;
  6774. if (num > 0 && num <= ARRAYLENGTH(equip))
  6775. i=pc_checkequip(sd,equip[num-1]);
  6776. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6777. script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  6778. else
  6779. script_pushint(st,0);
  6780. return SCRIPT_CMD_SUCCESS;
  6781. }
  6782. /*==========================================
  6783. * Refine +1 item at pos and log and display refine
  6784. *------------------------------------------*/
  6785. BUILDIN_FUNC(successrefitem)
  6786. {
  6787. int i=-1,num,ep;
  6788. TBL_PC *sd;
  6789. num = script_getnum(st,2);
  6790. sd = script_rid2sd(st);
  6791. if( sd == NULL )
  6792. return 0;
  6793. if (num > 0 && num <= ARRAYLENGTH(equip))
  6794. i=pc_checkequip(sd,equip[num-1]);
  6795. if(i >= 0) {
  6796. ep=sd->status.inventory[i].equip;
  6797. //Logs items, got from (N)PC scripts [Lupus]
  6798. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6799. sd->status.inventory[i].refine++;
  6800. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6801. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6802. clif_delitem(sd,i,1,3);
  6803. //Logs items, got from (N)PC scripts [Lupus]
  6804. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6805. clif_additem(sd,i,1,0);
  6806. pc_equipitem(sd,i,ep);
  6807. clif_misceffect(&sd->bl,3);
  6808. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6809. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6810. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6811. ){ // Fame point system [DracoRPG]
  6812. switch (sd->inventory_data[i]->wlv){
  6813. case 1:
  6814. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6815. break;
  6816. case 2:
  6817. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6818. break;
  6819. case 3:
  6820. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6821. break;
  6822. }
  6823. }
  6824. }
  6825. return SCRIPT_CMD_SUCCESS;
  6826. }
  6827. /*==========================================
  6828. * Show a failed Refine +1 attempt
  6829. *------------------------------------------*/
  6830. BUILDIN_FUNC(failedrefitem)
  6831. {
  6832. int i=-1,num;
  6833. TBL_PC *sd;
  6834. num = script_getnum(st,2);
  6835. sd = script_rid2sd(st);
  6836. if( sd == NULL )
  6837. return 0;
  6838. if (num > 0 && num <= ARRAYLENGTH(equip))
  6839. i=pc_checkequip(sd,equip[num-1]);
  6840. if(i >= 0) {
  6841. sd->status.inventory[i].refine = 0;
  6842. pc_unequipitem(sd,i,3); //recalculate bonus
  6843. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  6844. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6845. clif_misceffect(&sd->bl,2); // display failure effect
  6846. }
  6847. return SCRIPT_CMD_SUCCESS;
  6848. }
  6849. /*==========================================
  6850. * Downgrades an Equipment Part by -1 . [Masao]
  6851. *------------------------------------------*/
  6852. BUILDIN_FUNC(downrefitem)
  6853. {
  6854. int i = -1,num,ep,down = 1;
  6855. TBL_PC *sd;
  6856. sd = script_rid2sd(st);
  6857. if( sd == NULL )
  6858. return 0;
  6859. num = script_getnum(st,2);
  6860. if( script_hasdata(st, 3) )
  6861. down = script_getnum(st, 3);
  6862. if (num > 0 && num <= ARRAYLENGTH(equip))
  6863. i = pc_checkequip(sd,equip[num-1]);
  6864. if(i >= 0) {
  6865. ep = sd->status.inventory[i].equip;
  6866. //Logs items, got from (N)PC scripts [Lupus]
  6867. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6868. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6869. sd->status.inventory[i].refine -= down;
  6870. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  6871. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine);
  6872. clif_delitem(sd,i,1,3);
  6873. //Logs items, got from (N)PC scripts [Lupus]
  6874. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6875. clif_additem(sd,i,1,0);
  6876. pc_equipitem(sd,i,ep);
  6877. clif_misceffect(&sd->bl,2);
  6878. }
  6879. return SCRIPT_CMD_SUCCESS;
  6880. }
  6881. /*==========================================
  6882. * Delete the item equipped at pos.
  6883. *------------------------------------------*/
  6884. BUILDIN_FUNC(delequip)
  6885. {
  6886. int i=-1,num,ret=0;
  6887. TBL_PC *sd;
  6888. num = script_getnum(st,2);
  6889. sd = script_rid2sd(st);
  6890. if( sd == NULL )
  6891. return 0;
  6892. if (num > 0 && num <= ARRAYLENGTH(equip))
  6893. i=pc_checkequip(sd,equip[num-1]);
  6894. if(i >= 0) {
  6895. pc_unequipitem(sd,i,3); //recalculate bonus
  6896. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  6897. }
  6898. script_pushint(st,ret);
  6899. return SCRIPT_CMD_SUCCESS;
  6900. }
  6901. /*==========================================
  6902. * Break the item equipped at pos.
  6903. *------------------------------------------*/
  6904. BUILDIN_FUNC(breakequip)
  6905. {
  6906. int i=-1,num;
  6907. TBL_PC *sd;
  6908. num = script_getnum(st,2);
  6909. sd = script_rid2sd(st);
  6910. if( sd == NULL )
  6911. return 0;
  6912. if (num > 0 && num <= ARRAYLENGTH(equip))
  6913. i = pc_checkequip(sd,equip[num-1]);
  6914. if (i >= 0) {
  6915. sd->status.inventory[i].attribute = 1;
  6916. pc_unequipitem(sd,i,3);
  6917. clif_equiplist(sd);
  6918. script_pushint(st,1);
  6919. } else
  6920. script_pushint(st,0);
  6921. return SCRIPT_CMD_SUCCESS;
  6922. }
  6923. /*==========================================
  6924. *
  6925. *------------------------------------------*/
  6926. BUILDIN_FUNC(statusup)
  6927. {
  6928. int type;
  6929. TBL_PC *sd;
  6930. type=script_getnum(st,2);
  6931. sd = script_rid2sd(st);
  6932. if( sd == NULL )
  6933. return 0;
  6934. pc_statusup(sd,type);
  6935. return SCRIPT_CMD_SUCCESS;
  6936. }
  6937. /*==========================================
  6938. *
  6939. *------------------------------------------*/
  6940. BUILDIN_FUNC(statusup2)
  6941. {
  6942. int type,val;
  6943. TBL_PC *sd;
  6944. type=script_getnum(st,2);
  6945. val=script_getnum(st,3);
  6946. sd = script_rid2sd(st);
  6947. if( sd == NULL )
  6948. return 0;
  6949. pc_statusup2(sd,type,val);
  6950. return SCRIPT_CMD_SUCCESS;
  6951. }
  6952. /// See 'doc/item_bonus.txt'
  6953. ///
  6954. /// bonus <bonus type>,<val1>;
  6955. /// bonus2 <bonus type>,<val1>,<val2>;
  6956. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6957. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6958. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6959. BUILDIN_FUNC(bonus)
  6960. {
  6961. int type;
  6962. int val1;
  6963. int val2 = 0;
  6964. int val3 = 0;
  6965. int val4 = 0;
  6966. int val5 = 0;
  6967. TBL_PC* sd;
  6968. struct script_data *data;
  6969. sd = script_rid2sd(st);
  6970. if( sd == NULL )
  6971. return 0; // no player attached
  6972. type = script_getnum(st,2);
  6973. switch( type ) {
  6974. case SP_AUTOSPELL:
  6975. case SP_AUTOSPELL_WHENHIT:
  6976. case SP_AUTOSPELL_ONSKILL:
  6977. case SP_SKILL_ATK:
  6978. case SP_SKILL_HEAL:
  6979. case SP_SKILL_HEAL2:
  6980. case SP_ADD_SKILL_BLOW:
  6981. case SP_CASTRATE:
  6982. case SP_ADDEFF_ONSKILL:
  6983. case SP_SKILL_USE_SP_RATE:
  6984. case SP_SKILL_COOLDOWN:
  6985. case SP_SKILL_FIXEDCAST:
  6986. case SP_SKILL_VARIABLECAST:
  6987. case SP_VARCASTRATE:
  6988. case SP_FIXCASTRATE:
  6989. case SP_SKILL_USE_SP:
  6990. // these bonuses support skill names
  6991. data = script_getdata(st, 3);
  6992. get_val(st, data); // Convert into value in case of a variable
  6993. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6994. break;
  6995. default:
  6996. val1 = script_getnum(st,3);
  6997. break;
  6998. }
  6999. switch( script_lastdata(st)-2 ) {
  7000. case 1:
  7001. pc_bonus(sd, type, val1);
  7002. break;
  7003. case 2:
  7004. val2 = script_getnum(st,4);
  7005. pc_bonus2(sd, type, val1, val2);
  7006. break;
  7007. case 3:
  7008. val2 = script_getnum(st,4);
  7009. val3 = script_getnum(st,5);
  7010. pc_bonus3(sd, type, val1, val2, val3);
  7011. break;
  7012. case 4:
  7013. data = script_getdata(st, 4);
  7014. get_val(st, data); // Convert into value in case of a variable
  7015. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7016. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7017. else
  7018. val2 = script_getnum(st,4);
  7019. val3 = script_getnum(st,5);
  7020. val4 = script_getnum(st,6);
  7021. pc_bonus4(sd, type, val1, val2, val3, val4);
  7022. break;
  7023. case 5:
  7024. data = script_getdata(st, 4);
  7025. get_val(st, data); // Convert into value in case of a variable
  7026. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7027. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7028. else
  7029. val2 = script_getnum(st,4);
  7030. val3 = script_getnum(st,5);
  7031. val4 = script_getnum(st,6);
  7032. val5 = script_getnum(st,7);
  7033. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7034. break;
  7035. default:
  7036. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7037. break;
  7038. }
  7039. return SCRIPT_CMD_SUCCESS;
  7040. }
  7041. BUILDIN_FUNC(autobonus)
  7042. {
  7043. unsigned int dur;
  7044. short rate;
  7045. short atk_type = 0;
  7046. TBL_PC* sd;
  7047. const char *bonus_script, *other_script = NULL;
  7048. sd = script_rid2sd(st);
  7049. if( sd == NULL )
  7050. return 0; // no player attached
  7051. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7052. return 0;
  7053. rate = script_getnum(st,3);
  7054. dur = script_getnum(st,4);
  7055. bonus_script = script_getstr(st,2);
  7056. if( !rate || !dur || !bonus_script )
  7057. return 0;
  7058. if( script_hasdata(st,5) )
  7059. atk_type = script_getnum(st,5);
  7060. if( script_hasdata(st,6) )
  7061. other_script = script_getstr(st,6);
  7062. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7063. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7064. {
  7065. script_add_autobonus(bonus_script);
  7066. if( other_script )
  7067. script_add_autobonus(other_script);
  7068. }
  7069. return SCRIPT_CMD_SUCCESS;
  7070. }
  7071. BUILDIN_FUNC(autobonus2)
  7072. {
  7073. unsigned int dur;
  7074. short rate;
  7075. short atk_type = 0;
  7076. TBL_PC* sd;
  7077. const char *bonus_script, *other_script = NULL;
  7078. sd = script_rid2sd(st);
  7079. if( sd == NULL )
  7080. return 0; // no player attached
  7081. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7082. return 0;
  7083. rate = script_getnum(st,3);
  7084. dur = script_getnum(st,4);
  7085. bonus_script = script_getstr(st,2);
  7086. if( !rate || !dur || !bonus_script )
  7087. return 0;
  7088. if( script_hasdata(st,5) )
  7089. atk_type = script_getnum(st,5);
  7090. if( script_hasdata(st,6) )
  7091. other_script = script_getstr(st,6);
  7092. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7093. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7094. {
  7095. script_add_autobonus(bonus_script);
  7096. if( other_script )
  7097. script_add_autobonus(other_script);
  7098. }
  7099. return SCRIPT_CMD_SUCCESS;
  7100. }
  7101. BUILDIN_FUNC(autobonus3)
  7102. {
  7103. unsigned int dur;
  7104. short rate,atk_type;
  7105. TBL_PC* sd;
  7106. const char *bonus_script, *other_script = NULL;
  7107. struct script_data *data;
  7108. sd = script_rid2sd(st);
  7109. if( sd == NULL )
  7110. return 0; // no player attached
  7111. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7112. return 0;
  7113. rate = script_getnum(st,3);
  7114. dur = script_getnum(st,4);
  7115. data = script_getdata(st, 5);
  7116. get_val(st, data); // Convert into value in case of a variable
  7117. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7118. bonus_script = script_getstr(st,2);
  7119. if( !rate || !dur || !atk_type || !bonus_script )
  7120. return 0;
  7121. if( script_hasdata(st,6) )
  7122. other_script = script_getstr(st,6);
  7123. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7124. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  7125. {
  7126. script_add_autobonus(bonus_script);
  7127. if( other_script )
  7128. script_add_autobonus(other_script);
  7129. }
  7130. return SCRIPT_CMD_SUCCESS;
  7131. }
  7132. /// Changes the level of a player skill.
  7133. /// <flag> defaults to 1
  7134. /// <flag>=0 : set the level of the skill
  7135. /// <flag>=1 : set the temporary level of the skill
  7136. /// <flag>=2 : add to the level of the skill
  7137. ///
  7138. /// skill <skill id>,<level>,<flag>
  7139. /// skill <skill id>,<level>
  7140. /// skill "<skill name>",<level>,<flag>
  7141. /// skill "<skill name>",<level>
  7142. BUILDIN_FUNC(skill)
  7143. {
  7144. int id;
  7145. int level;
  7146. int flag = 1;
  7147. TBL_PC* sd;
  7148. struct script_data *data;
  7149. sd = script_rid2sd(st);
  7150. if( sd == NULL )
  7151. return 0;// no player attached, report source
  7152. data = script_getdata(st, 2);
  7153. get_val(st, data); // Convert into value in case of a variable
  7154. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7155. level = script_getnum(st,3);
  7156. if( script_hasdata(st,4) )
  7157. flag = script_getnum(st,4);
  7158. pc_skill(sd, id, level, flag);
  7159. return SCRIPT_CMD_SUCCESS;
  7160. }
  7161. /// Changes the level of a player skill.
  7162. /// like skill, but <flag> defaults to 2
  7163. ///
  7164. /// addtoskill <skill id>,<amount>,<flag>
  7165. /// addtoskill <skill id>,<amount>
  7166. /// addtoskill "<skill name>",<amount>,<flag>
  7167. /// addtoskill "<skill name>",<amount>
  7168. ///
  7169. /// @see skill
  7170. BUILDIN_FUNC(addtoskill)
  7171. {
  7172. int id;
  7173. int level;
  7174. int flag = 2;
  7175. TBL_PC* sd;
  7176. struct script_data *data;
  7177. sd = script_rid2sd(st);
  7178. if( sd == NULL )
  7179. return 0;// no player attached, report source
  7180. data = script_getdata(st, 2);
  7181. get_val(st, data); // Convert into value in case of a variable
  7182. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7183. level = script_getnum(st,3);
  7184. if( script_hasdata(st,4) )
  7185. flag = script_getnum(st,4);
  7186. pc_skill(sd, id, level, flag);
  7187. return SCRIPT_CMD_SUCCESS;
  7188. }
  7189. /// Increases the level of a guild skill.
  7190. ///
  7191. /// guildskill <skill id>,<amount>;
  7192. /// guildskill "<skill name>",<amount>;
  7193. BUILDIN_FUNC(guildskill)
  7194. {
  7195. int id;
  7196. int level;
  7197. TBL_PC* sd;
  7198. int i;
  7199. struct script_data *data;
  7200. sd = script_rid2sd(st);
  7201. if( sd == NULL )
  7202. return 0;// no player attached, report source
  7203. data = script_getdata(st, 2);
  7204. get_val(st, data); // Convert into value in case of a variable
  7205. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7206. level = script_getnum(st,3);
  7207. for( i=0; i < level; i++ )
  7208. guild_skillup(sd, id);
  7209. return SCRIPT_CMD_SUCCESS;
  7210. }
  7211. /// Returns the level of the player skill.
  7212. ///
  7213. /// getskilllv(<skill id>) -> <level>
  7214. /// getskilllv("<skill name>") -> <level>
  7215. BUILDIN_FUNC(getskilllv)
  7216. {
  7217. int id;
  7218. TBL_PC* sd;
  7219. struct script_data *data;
  7220. sd = script_rid2sd(st);
  7221. if( sd == NULL )
  7222. return 0;// no player attached, report source
  7223. data = script_getdata(st, 2);
  7224. get_val(st, data); // Convert into value in case of a variable
  7225. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7226. script_pushint(st, pc_checkskill(sd,id));
  7227. return SCRIPT_CMD_SUCCESS;
  7228. }
  7229. /// Returns the level of the guild skill.
  7230. ///
  7231. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7232. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7233. BUILDIN_FUNC(getgdskilllv)
  7234. {
  7235. int guild_id;
  7236. uint16 skill_id;
  7237. struct guild* g;
  7238. struct script_data *data;
  7239. guild_id = script_getnum(st,2);
  7240. data = script_getdata(st, 3);
  7241. get_val(st, data); // Convert into value in case of a variable
  7242. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7243. g = guild_search(guild_id);
  7244. if( g == NULL )
  7245. script_pushint(st, -1);
  7246. else
  7247. script_pushint(st, guild_checkskill(g,skill_id));
  7248. return SCRIPT_CMD_SUCCESS;
  7249. }
  7250. /// Returns the 'basic_skill_check' setting.
  7251. /// This config determines if the server checks the skill level of NV_BASIC
  7252. /// before allowing the basic actions.
  7253. ///
  7254. /// basicskillcheck() -> <bool>
  7255. BUILDIN_FUNC(basicskillcheck)
  7256. {
  7257. script_pushint(st, battle_config.basic_skill_check);
  7258. return SCRIPT_CMD_SUCCESS;
  7259. }
  7260. /// Returns the GM level of the player.
  7261. ///
  7262. /// getgmlevel() -> <level>
  7263. BUILDIN_FUNC(getgmlevel)
  7264. {
  7265. TBL_PC* sd;
  7266. sd = script_rid2sd(st);
  7267. if( sd == NULL )
  7268. return 0;// no player attached, report source
  7269. script_pushint(st, pc_get_group_level(sd));
  7270. return SCRIPT_CMD_SUCCESS;
  7271. }
  7272. /// Returns the group ID of the player.
  7273. ///
  7274. /// getgroupid() -> <int>
  7275. BUILDIN_FUNC(getgroupid)
  7276. {
  7277. TBL_PC* sd;
  7278. sd = script_rid2sd(st);
  7279. if (sd == NULL)
  7280. return 1; // no player attached, report source
  7281. script_pushint(st, pc_get_group_id(sd));
  7282. return SCRIPT_CMD_SUCCESS;
  7283. }
  7284. /// Terminates the execution of this script instance.
  7285. ///
  7286. /// end
  7287. BUILDIN_FUNC(end)
  7288. {
  7289. TBL_PC* sd;
  7290. sd = map_id2sd(st->rid);
  7291. st->state = END;
  7292. if( st->mes_active )
  7293. st->mes_active = 0;
  7294. if( sd )
  7295. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  7296. return SCRIPT_CMD_SUCCESS;
  7297. }
  7298. /// Checks if the player has that effect state (option).
  7299. ///
  7300. /// checkoption(<option>) -> <bool>
  7301. BUILDIN_FUNC(checkoption)
  7302. {
  7303. int option;
  7304. TBL_PC* sd;
  7305. sd = script_rid2sd(st);
  7306. if( sd == NULL )
  7307. return 0;// no player attached, report source
  7308. option = script_getnum(st,2);
  7309. if( sd->sc.option&option )
  7310. script_pushint(st, 1);
  7311. else
  7312. script_pushint(st, 0);
  7313. return SCRIPT_CMD_SUCCESS;
  7314. }
  7315. /// Checks if the player is in that body state (opt1).
  7316. ///
  7317. /// checkoption1(<opt1>) -> <bool>
  7318. BUILDIN_FUNC(checkoption1)
  7319. {
  7320. int opt1;
  7321. TBL_PC* sd;
  7322. sd = script_rid2sd(st);
  7323. if( sd == NULL )
  7324. return 0;// no player attached, report source
  7325. opt1 = script_getnum(st,2);
  7326. if( sd->sc.opt1 == opt1 )
  7327. script_pushint(st, 1);
  7328. else
  7329. script_pushint(st, 0);
  7330. return SCRIPT_CMD_SUCCESS;
  7331. }
  7332. /// Checks if the player has that health state (opt2).
  7333. ///
  7334. /// checkoption2(<opt2>) -> <bool>
  7335. BUILDIN_FUNC(checkoption2)
  7336. {
  7337. int opt2;
  7338. TBL_PC* sd;
  7339. sd = script_rid2sd(st);
  7340. if( sd == NULL )
  7341. return 0;// no player attached, report source
  7342. opt2 = script_getnum(st,2);
  7343. if( sd->sc.opt2&opt2 )
  7344. script_pushint(st, 1);
  7345. else
  7346. script_pushint(st, 0);
  7347. return SCRIPT_CMD_SUCCESS;
  7348. }
  7349. /// Changes the effect state (option) of the player.
  7350. /// <flag> defaults to 1
  7351. /// <flag>=0 : removes the option
  7352. /// <flag>=other : adds the option
  7353. ///
  7354. /// setoption <option>,<flag>;
  7355. /// setoption <option>;
  7356. BUILDIN_FUNC(setoption)
  7357. {
  7358. int option;
  7359. int flag = 1;
  7360. TBL_PC* sd;
  7361. sd = script_rid2sd(st);
  7362. if( sd == NULL )
  7363. return 0;// no player attached, report source
  7364. option = script_getnum(st,2);
  7365. if( script_hasdata(st,3) )
  7366. flag = script_getnum(st,3);
  7367. else if( !option ){// Request to remove everything.
  7368. flag = 0;
  7369. option = OPTION_FALCON|OPTION_RIDING;
  7370. #ifndef NEW_CARTS
  7371. option |= OPTION_CART;
  7372. #endif
  7373. }
  7374. if( flag ){// Add option
  7375. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  7376. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  7377. pc_setoption(sd, sd->sc.option|option);
  7378. } else// Remove option
  7379. pc_setoption(sd, sd->sc.option&~option);
  7380. return SCRIPT_CMD_SUCCESS;
  7381. }
  7382. /// Returns if the player has a cart.
  7383. ///
  7384. /// checkcart() -> <bool>
  7385. ///
  7386. /// @author Valaris
  7387. BUILDIN_FUNC(checkcart)
  7388. {
  7389. TBL_PC* sd;
  7390. sd = script_rid2sd(st);
  7391. if( sd == NULL )
  7392. return 0;// no player attached, report source
  7393. if( pc_iscarton(sd) )
  7394. script_pushint(st, 1);
  7395. else
  7396. script_pushint(st, 0);
  7397. return SCRIPT_CMD_SUCCESS;
  7398. }
  7399. /// Sets the cart of the player.
  7400. /// <type> defaults to 1
  7401. /// <type>=0 : removes the cart
  7402. /// <type>=1 : Normal cart
  7403. /// <type>=2 : Wooden cart
  7404. /// <type>=3 : Covered cart with flowers and ferns
  7405. /// <type>=4 : Wooden cart with a Panda doll on the back
  7406. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  7407. ///
  7408. /// setcart <type>;
  7409. /// setcart;
  7410. BUILDIN_FUNC(setcart)
  7411. {
  7412. int type = 1;
  7413. TBL_PC* sd;
  7414. sd = script_rid2sd(st);
  7415. if( sd == NULL )
  7416. return 0;// no player attached, report source
  7417. if( script_hasdata(st,2) )
  7418. type = script_getnum(st,2);
  7419. pc_setcart(sd, type);
  7420. return SCRIPT_CMD_SUCCESS;
  7421. }
  7422. /// Returns if the player has a falcon.
  7423. ///
  7424. /// checkfalcon() -> <bool>
  7425. ///
  7426. /// @author Valaris
  7427. BUILDIN_FUNC(checkfalcon)
  7428. {
  7429. TBL_PC* sd;
  7430. sd = script_rid2sd(st);
  7431. if( sd == NULL )
  7432. return 0;// no player attached, report source
  7433. if( pc_isfalcon(sd) )
  7434. script_pushint(st, 1);
  7435. else
  7436. script_pushint(st, 0);
  7437. return SCRIPT_CMD_SUCCESS;
  7438. }
  7439. /// Sets if the player has a falcon or not.
  7440. /// <flag> defaults to 1
  7441. ///
  7442. /// setfalcon <flag>;
  7443. /// setfalcon;
  7444. BUILDIN_FUNC(setfalcon)
  7445. {
  7446. int flag = 1;
  7447. TBL_PC* sd;
  7448. sd = script_rid2sd(st);
  7449. if( sd == NULL )
  7450. return 0;// no player attached, report source
  7451. if( script_hasdata(st,2) )
  7452. flag = script_getnum(st,2);
  7453. pc_setfalcon(sd, flag);
  7454. return SCRIPT_CMD_SUCCESS;
  7455. }
  7456. /// Returns if the player is riding.
  7457. ///
  7458. /// checkriding() -> <bool>
  7459. ///
  7460. /// @author Valaris
  7461. BUILDIN_FUNC(checkriding)
  7462. {
  7463. TBL_PC* sd;
  7464. sd = script_rid2sd(st);
  7465. if( sd == NULL )
  7466. return 0;// no player attached, report source
  7467. if( pc_isriding(sd) || pc_isridingwug(sd) || pc_isridingdragon(sd) )
  7468. script_pushint(st, 1);
  7469. else
  7470. script_pushint(st, 0);
  7471. return SCRIPT_CMD_SUCCESS;
  7472. }
  7473. /// Sets if the player is riding.
  7474. /// <flag> defaults to 1
  7475. ///
  7476. /// setriding <flag>;
  7477. /// setriding;
  7478. BUILDIN_FUNC(setriding)
  7479. {
  7480. int flag = 1;
  7481. TBL_PC* sd;
  7482. sd = script_rid2sd(st);
  7483. if( sd == NULL )
  7484. return 0;// no player attached, report source
  7485. if( script_hasdata(st,2) )
  7486. flag = script_getnum(st,2);
  7487. pc_setriding(sd, flag);
  7488. return SCRIPT_CMD_SUCCESS;
  7489. }
  7490. /// Returns if the player has a warg.
  7491. ///
  7492. /// checkwug() -> <bool>
  7493. ///
  7494. BUILDIN_FUNC(checkwug)
  7495. {
  7496. TBL_PC* sd;
  7497. sd = script_rid2sd(st);
  7498. if( sd == NULL )
  7499. return 0;// no player attached, report source
  7500. if( pc_iswug(sd) || pc_isridingwug(sd) )
  7501. script_pushint(st, 1);
  7502. else
  7503. script_pushint(st, 0);
  7504. return SCRIPT_CMD_SUCCESS;
  7505. }
  7506. /// Returns if the player is wearing MADO Gear.
  7507. ///
  7508. /// checkmadogear() -> <bool>
  7509. ///
  7510. BUILDIN_FUNC(checkmadogear)
  7511. {
  7512. TBL_PC* sd;
  7513. sd = script_rid2sd(st);
  7514. if( sd == NULL )
  7515. return 0;// no player attached, report source
  7516. if( pc_ismadogear(sd) )
  7517. script_pushint(st, 1);
  7518. else
  7519. script_pushint(st, 0);
  7520. return SCRIPT_CMD_SUCCESS;
  7521. }
  7522. /// Sets if the player is riding MADO Gear.
  7523. /// <flag> defaults to 1
  7524. ///
  7525. /// setmadogear <flag>;
  7526. /// setmadogear;
  7527. BUILDIN_FUNC(setmadogear)
  7528. {
  7529. int flag = 1;
  7530. TBL_PC* sd;
  7531. sd = script_rid2sd(st);
  7532. if( sd == NULL )
  7533. return 0;// no player attached, report source
  7534. if( script_hasdata(st,2) )
  7535. flag = script_getnum(st,2);
  7536. pc_setmadogear(sd, flag);
  7537. return SCRIPT_CMD_SUCCESS;
  7538. }
  7539. /// Sets the save point of the player.
  7540. ///
  7541. /// save "<map name>",<x>,<y>
  7542. /// savepoint "<map name>",<x>,<y>
  7543. BUILDIN_FUNC(savepoint)
  7544. {
  7545. int x;
  7546. int y;
  7547. short map;
  7548. const char* str;
  7549. TBL_PC* sd;
  7550. sd = script_rid2sd(st);
  7551. if( sd == NULL )
  7552. return 0;// no player attached, report source
  7553. str = script_getstr(st, 2);
  7554. x = script_getnum(st,3);
  7555. y = script_getnum(st,4);
  7556. map = mapindex_name2id(str);
  7557. if( map )
  7558. pc_setsavepoint(sd, map, x, y);
  7559. return SCRIPT_CMD_SUCCESS;
  7560. }
  7561. /*==========================================
  7562. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  7563. *------------------------------------------*/
  7564. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  7565. {
  7566. int type;
  7567. time_t timer;
  7568. struct tm *t;
  7569. type=script_getnum(st,2);
  7570. switch(type){
  7571. case 2:
  7572. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  7573. // from the system clock.)
  7574. script_pushint(st,(int)time(NULL));
  7575. break;
  7576. case 1:
  7577. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  7578. time(&timer);
  7579. t=localtime(&timer);
  7580. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  7581. break;
  7582. case 0:
  7583. default:
  7584. //type 0:(System Ticks)
  7585. script_pushint(st,gettick());
  7586. break;
  7587. }
  7588. return SCRIPT_CMD_SUCCESS;
  7589. }
  7590. /*==========================================
  7591. * GetTime(Type);
  7592. * 1: Sec 2: Min 3: Hour
  7593. * 4: WeekDay 5: MonthDay 6: Month
  7594. * 7: Year
  7595. *------------------------------------------*/
  7596. BUILDIN_FUNC(gettime) /* Asgard Version */
  7597. {
  7598. int type;
  7599. time_t timer;
  7600. struct tm *t;
  7601. type=script_getnum(st,2);
  7602. time(&timer);
  7603. t=localtime(&timer);
  7604. switch(type){
  7605. case 1://Sec(0~59)
  7606. script_pushint(st,t->tm_sec);
  7607. break;
  7608. case 2://Min(0~59)
  7609. script_pushint(st,t->tm_min);
  7610. break;
  7611. case 3://Hour(0~23)
  7612. script_pushint(st,t->tm_hour);
  7613. break;
  7614. case 4://WeekDay(0~6)
  7615. script_pushint(st,t->tm_wday);
  7616. break;
  7617. case 5://MonthDay(01~31)
  7618. script_pushint(st,t->tm_mday);
  7619. break;
  7620. case 6://Month(01~12)
  7621. script_pushint(st,t->tm_mon+1);
  7622. break;
  7623. case 7://Year(20xx)
  7624. script_pushint(st,t->tm_year+1900);
  7625. break;
  7626. case 8://Year Day(01~366)
  7627. script_pushint(st,t->tm_yday+1);
  7628. break;
  7629. default://(format error)
  7630. script_pushint(st,-1);
  7631. break;
  7632. }
  7633. return SCRIPT_CMD_SUCCESS;
  7634. }
  7635. /*==========================================
  7636. * GetTimeStr("TimeFMT", Length);
  7637. *------------------------------------------*/
  7638. BUILDIN_FUNC(gettimestr)
  7639. {
  7640. char *tmpstr;
  7641. const char *fmtstr;
  7642. int maxlen;
  7643. time_t now = time(NULL);
  7644. fmtstr=script_getstr(st,2);
  7645. maxlen=script_getnum(st,3);
  7646. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  7647. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  7648. tmpstr[maxlen]='\0';
  7649. script_pushstr(st,tmpstr);
  7650. return SCRIPT_CMD_SUCCESS;
  7651. }
  7652. /*==========================================
  7653. * Open player storage
  7654. *------------------------------------------*/
  7655. BUILDIN_FUNC(openstorage)
  7656. {
  7657. TBL_PC* sd;
  7658. sd = script_rid2sd(st);
  7659. if( sd == NULL )
  7660. return 0;
  7661. storage_storageopen(sd);
  7662. return SCRIPT_CMD_SUCCESS;
  7663. }
  7664. BUILDIN_FUNC(guildopenstorage)
  7665. {
  7666. TBL_PC* sd;
  7667. int ret;
  7668. sd = script_rid2sd(st);
  7669. if( sd == NULL )
  7670. return 0;
  7671. ret = storage_guild_storageopen(sd);
  7672. script_pushint(st,ret);
  7673. return SCRIPT_CMD_SUCCESS;
  7674. }
  7675. /*==========================================
  7676. * Make player use a skill trought item usage
  7677. *------------------------------------------*/
  7678. /// itemskill <skill id>,<level>
  7679. /// itemskill "<skill name>",<level>
  7680. BUILDIN_FUNC(itemskill)
  7681. {
  7682. int id;
  7683. int lv;
  7684. TBL_PC* sd;
  7685. struct script_data *data;
  7686. sd = script_rid2sd(st);
  7687. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  7688. return 0;
  7689. data = script_getdata(st, 2);
  7690. get_val(st, data); // Convert into value in case of a variable
  7691. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7692. lv = script_getnum(st,3);
  7693. sd->skillitem=id;
  7694. sd->skillitemlv=lv;
  7695. clif_item_skill(sd,id,lv);
  7696. return SCRIPT_CMD_SUCCESS;
  7697. }
  7698. /*==========================================
  7699. * Attempt to create an item
  7700. *------------------------------------------*/
  7701. BUILDIN_FUNC(produce)
  7702. {
  7703. int trigger;
  7704. TBL_PC* sd;
  7705. sd = script_rid2sd(st);
  7706. if( sd == NULL )
  7707. return 0;
  7708. trigger=script_getnum(st,2);
  7709. clif_skill_produce_mix_list(sd, -1, trigger);
  7710. return SCRIPT_CMD_SUCCESS;
  7711. }
  7712. /*==========================================
  7713. *
  7714. *------------------------------------------*/
  7715. BUILDIN_FUNC(cooking)
  7716. {
  7717. int trigger;
  7718. TBL_PC* sd;
  7719. sd = script_rid2sd(st);
  7720. if( sd == NULL )
  7721. return 0;
  7722. trigger=script_getnum(st,2);
  7723. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  7724. return SCRIPT_CMD_SUCCESS;
  7725. }
  7726. /*==========================================
  7727. * Create a pet
  7728. *------------------------------------------*/
  7729. BUILDIN_FUNC(makepet)
  7730. {
  7731. TBL_PC* sd;
  7732. int id,pet_id;
  7733. id=script_getnum(st,2);
  7734. sd = script_rid2sd(st);
  7735. if( sd == NULL )
  7736. return 0;
  7737. pet_id = search_petDB_index(id, PET_CLASS);
  7738. if (pet_id < 0)
  7739. pet_id = search_petDB_index(id, PET_EGG);
  7740. if (pet_id >= 0 && sd) {
  7741. sd->catch_target_class = pet_db[pet_id].class_;
  7742. intif_create_pet(
  7743. sd->status.account_id, sd->status.char_id,
  7744. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  7745. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  7746. 100, 0, 1, pet_db[pet_id].jname);
  7747. }
  7748. return SCRIPT_CMD_SUCCESS;
  7749. }
  7750. /*==========================================
  7751. * Give player exp base,job * quest_exp_rate/100
  7752. *------------------------------------------*/
  7753. BUILDIN_FUNC(getexp)
  7754. {
  7755. TBL_PC* sd;
  7756. int base=0,job=0;
  7757. double bonus;
  7758. sd = script_rid2sd(st);
  7759. if( sd == NULL )
  7760. return 0;
  7761. base=script_getnum(st,2);
  7762. job =script_getnum(st,3);
  7763. if(base<0 || job<0)
  7764. return 0;
  7765. // bonus for npc-given exp
  7766. bonus = battle_config.quest_exp_rate / 100.;
  7767. base = (int) cap_value(base * bonus, 0, INT_MAX);
  7768. job = (int) cap_value(job * bonus, 0, INT_MAX);
  7769. pc_gainexp(sd, NULL, base, job, true);
  7770. return SCRIPT_CMD_SUCCESS;
  7771. }
  7772. /*==========================================
  7773. * Gain guild exp [Celest]
  7774. *------------------------------------------*/
  7775. BUILDIN_FUNC(guildgetexp)
  7776. {
  7777. TBL_PC* sd;
  7778. int exp;
  7779. sd = script_rid2sd(st);
  7780. if( sd == NULL )
  7781. return 0;
  7782. exp = script_getnum(st,2);
  7783. if(exp < 0)
  7784. return 0;
  7785. if(sd && sd->status.guild_id > 0)
  7786. guild_getexp (sd, exp);
  7787. return SCRIPT_CMD_SUCCESS;
  7788. }
  7789. /*==========================================
  7790. * Changes the guild master of a guild [Skotlex]
  7791. *------------------------------------------*/
  7792. BUILDIN_FUNC(guildchangegm)
  7793. {
  7794. TBL_PC *sd;
  7795. int guild_id;
  7796. const char *name;
  7797. guild_id = script_getnum(st,2);
  7798. name = script_getstr(st,3);
  7799. sd=map_nick2sd(name);
  7800. if (!sd)
  7801. script_pushint(st,0);
  7802. else
  7803. script_pushint(st,guild_gm_change(guild_id, sd));
  7804. return SCRIPT_CMD_SUCCESS;
  7805. }
  7806. /*==========================================
  7807. * Spawn a monster :
  7808. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  7809. @mapn,x,y : location
  7810. @str : monster name
  7811. @class_ : mob_id
  7812. @amount : nb to spawn
  7813. @event : event to attach to mob
  7814. *------------------------------------------*/
  7815. BUILDIN_FUNC(monster)
  7816. {
  7817. const char* mapn = script_getstr(st,2);
  7818. int x = script_getnum(st,3);
  7819. int y = script_getnum(st,4);
  7820. const char* str = script_getstr(st,5);
  7821. int class_ = script_getnum(st,6);
  7822. int amount = script_getnum(st,7);
  7823. const char* event = "";
  7824. unsigned int size = SZ_SMALL;
  7825. unsigned int ai = AI_NONE;
  7826. struct map_session_data* sd;
  7827. int16 m;
  7828. int i;
  7829. if (script_hasdata(st, 8)) {
  7830. event = script_getstr(st, 8);
  7831. check_event(st, event);
  7832. }
  7833. if (script_hasdata(st, 9)) {
  7834. size = script_getnum(st, 9);
  7835. if (size > 3) {
  7836. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  7837. return 1;
  7838. }
  7839. }
  7840. if (script_hasdata(st, 10)) {
  7841. ai = script_getnum(st, 10);
  7842. if (ai >= AI_MAX) {
  7843. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  7844. return 1;
  7845. }
  7846. }
  7847. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7848. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7849. return 1;
  7850. }
  7851. sd = map_id2sd(st->rid);
  7852. if (sd && strcmp(mapn, "this") == 0)
  7853. m = sd->bl.m;
  7854. else
  7855. m = map_mapname2mapid(mapn);
  7856. for(i=0; i<amount; i++){ //not optimised
  7857. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  7858. mapreg_setreg(reference_uid(add_str("$@mobid"), i),mobid);
  7859. }
  7860. return SCRIPT_CMD_SUCCESS;
  7861. }
  7862. /*==========================================
  7863. * Request List of Monster Drops
  7864. *------------------------------------------*/
  7865. BUILDIN_FUNC(getmobdrops)
  7866. {
  7867. int class_ = script_getnum(st,2);
  7868. int i, j = 0;
  7869. struct mob_db *mob;
  7870. if( !mobdb_checkid(class_) )
  7871. {
  7872. script_pushint(st, 0);
  7873. return 0;
  7874. }
  7875. mob = mob_db(class_);
  7876. for( i = 0; i < MAX_MOB_DROP; i++ )
  7877. {
  7878. if( mob->dropitem[i].nameid < 1 )
  7879. continue;
  7880. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7881. continue;
  7882. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7883. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7884. j++;
  7885. }
  7886. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7887. script_pushint(st, 1);
  7888. return SCRIPT_CMD_SUCCESS;
  7889. }
  7890. /*==========================================
  7891. * Same as monster but randomize location in x0,x1,y0,y1 area
  7892. *------------------------------------------*/
  7893. BUILDIN_FUNC(areamonster)
  7894. {
  7895. const char* mapn = script_getstr(st,2);
  7896. int x0 = script_getnum(st,3);
  7897. int y0 = script_getnum(st,4);
  7898. int x1 = script_getnum(st,5);
  7899. int y1 = script_getnum(st,6);
  7900. const char* str = script_getstr(st,7);
  7901. int class_ = script_getnum(st,8);
  7902. int amount = script_getnum(st,9);
  7903. const char* event = "";
  7904. unsigned int size = SZ_SMALL;
  7905. unsigned int ai = AI_NONE;
  7906. struct map_session_data* sd;
  7907. int16 m;
  7908. if (script_hasdata(st,10)) {
  7909. event = script_getstr(st, 10);
  7910. check_event(st, event);
  7911. }
  7912. if (script_hasdata(st, 11)) {
  7913. size = script_getnum(st, 11);
  7914. if (size > 3) {
  7915. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  7916. return 1;
  7917. }
  7918. }
  7919. if (script_hasdata(st, 12)) {
  7920. ai = script_getnum(st, 12);
  7921. if (ai >= AI_MAX) {
  7922. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  7923. return 1;
  7924. }
  7925. }
  7926. sd = map_id2sd(st->rid);
  7927. if (sd && strcmp(mapn, "this") == 0)
  7928. m = sd->bl.m;
  7929. else
  7930. m = map_mapname2mapid(mapn);
  7931. mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, amount, event, size, ai);
  7932. return SCRIPT_CMD_SUCCESS;
  7933. }
  7934. /*==========================================
  7935. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  7936. *------------------------------------------*/
  7937. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7938. { //same fix but with killmonster instead - stripping events from mobs.
  7939. TBL_MOB* md = (TBL_MOB*)bl;
  7940. char *event=va_arg(ap,char *);
  7941. int allflag=va_arg(ap,int);
  7942. md->state.npc_killmonster = 1;
  7943. if(!allflag){
  7944. if(strcmp(event,md->npc_event)==0)
  7945. status_kill(bl);
  7946. }else{
  7947. if(!md->spawn)
  7948. status_kill(bl);
  7949. }
  7950. md->state.npc_killmonster = 0;
  7951. return 0;
  7952. }
  7953. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7954. {
  7955. TBL_MOB* md = (TBL_MOB*)bl;
  7956. char *event=va_arg(ap,char *);
  7957. int allflag=va_arg(ap,int);
  7958. if(!allflag){
  7959. if(strcmp(event,md->npc_event)==0)
  7960. status_kill(bl);
  7961. }else{
  7962. if(!md->spawn)
  7963. status_kill(bl);
  7964. }
  7965. return 0;
  7966. }
  7967. BUILDIN_FUNC(killmonster)
  7968. {
  7969. const char *mapname,*event;
  7970. int16 m,allflag=0;
  7971. mapname=script_getstr(st,2);
  7972. event=script_getstr(st,3);
  7973. if(strcmp(event,"All")==0)
  7974. allflag = 1;
  7975. else
  7976. check_event(st, event);
  7977. if( (m=map_mapname2mapid(mapname))<0 )
  7978. return 0;
  7979. if( script_hasdata(st,4) ) {
  7980. if ( script_getnum(st,4) == 1 ) {
  7981. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7982. return 0;
  7983. }
  7984. }
  7985. map_freeblock_lock();
  7986. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7987. map_freeblock_unlock();
  7988. return SCRIPT_CMD_SUCCESS;
  7989. }
  7990. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7991. { //Strips the event from the mob if it's killed the old method.
  7992. struct mob_data *md;
  7993. md = BL_CAST(BL_MOB, bl);
  7994. if (md->npc_event[0])
  7995. md->npc_event[0] = 0;
  7996. status_kill(bl);
  7997. return 0;
  7998. }
  7999. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8000. {
  8001. status_kill(bl);
  8002. return 0;
  8003. }
  8004. BUILDIN_FUNC(killmonsterall)
  8005. {
  8006. const char *mapname;
  8007. int16 m;
  8008. mapname=script_getstr(st,2);
  8009. if( (m = map_mapname2mapid(mapname))<0 )
  8010. return 0;
  8011. if( script_hasdata(st,3) ) {
  8012. if ( script_getnum(st,3) == 1 ) {
  8013. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8014. return 0;
  8015. }
  8016. }
  8017. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8018. return SCRIPT_CMD_SUCCESS;
  8019. }
  8020. /*==========================================
  8021. * Creates a clone of a player.
  8022. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8023. *------------------------------------------*/
  8024. BUILDIN_FUNC(clone)
  8025. {
  8026. TBL_PC *sd, *msd=NULL;
  8027. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  8028. unsigned int duration = 0;
  8029. const char *map,*event="";
  8030. map=script_getstr(st,2);
  8031. x=script_getnum(st,3);
  8032. y=script_getnum(st,4);
  8033. event=script_getstr(st,5);
  8034. char_id=script_getnum(st,6);
  8035. if( script_hasdata(st,7) )
  8036. master_id=script_getnum(st,7);
  8037. if( script_hasdata(st,8) )
  8038. mode=script_getnum(st,8);
  8039. if( script_hasdata(st,9) )
  8040. flag=script_getnum(st,9);
  8041. if( script_hasdata(st,10) )
  8042. duration=script_getnum(st,10);
  8043. check_event(st, event);
  8044. m = map_mapname2mapid(map);
  8045. if (m < 0) return 0;
  8046. sd = map_charid2sd(char_id);
  8047. if (master_id) {
  8048. msd = map_charid2sd(master_id);
  8049. if (msd)
  8050. master_id = msd->bl.id;
  8051. else
  8052. master_id = 0;
  8053. }
  8054. if (sd) //Return ID of newly crafted clone.
  8055. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8056. else //Failed to create clone.
  8057. script_pushint(st,0);
  8058. return SCRIPT_CMD_SUCCESS;
  8059. }
  8060. /*==========================================
  8061. *------------------------------------------*/
  8062. BUILDIN_FUNC(doevent)
  8063. {
  8064. const char* event = script_getstr(st,2);
  8065. struct map_session_data* sd;
  8066. if( ( sd = script_rid2sd(st) ) == NULL )
  8067. {
  8068. return 0;
  8069. }
  8070. check_event(st, event);
  8071. npc_event(sd, event, 0);
  8072. return SCRIPT_CMD_SUCCESS;
  8073. }
  8074. /*==========================================
  8075. *------------------------------------------*/
  8076. BUILDIN_FUNC(donpcevent)
  8077. {
  8078. const char* event = script_getstr(st,2);
  8079. check_event(st, event);
  8080. if( !npc_event_do(event) ) {
  8081. struct npc_data * nd = map_id2nd(st->oid);
  8082. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8083. script_pushint(st, 0);
  8084. } else
  8085. script_pushint(st, 1);
  8086. return SCRIPT_CMD_SUCCESS;
  8087. }
  8088. /// for Aegis compatibility
  8089. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8090. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8091. {
  8092. const char* npc = script_getstr(st,2);
  8093. const char* command = script_getstr(st,3);
  8094. char event[EVENT_NAME_LENGTH];
  8095. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8096. check_event(st, event);
  8097. npc_event_do(event);
  8098. return SCRIPT_CMD_SUCCESS;
  8099. }
  8100. /*==========================================
  8101. *------------------------------------------*/
  8102. BUILDIN_FUNC(addtimer)
  8103. {
  8104. int tick = script_getnum(st,2);
  8105. const char* event = script_getstr(st, 3);
  8106. TBL_PC* sd;
  8107. check_event(st, event);
  8108. sd = script_rid2sd(st);
  8109. if( sd == NULL )
  8110. return 0;
  8111. if (!pc_addeventtimer(sd,tick,event)) {
  8112. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8113. return SCRIPT_CMD_FAILURE;
  8114. }
  8115. return SCRIPT_CMD_SUCCESS;
  8116. }
  8117. /*==========================================
  8118. *------------------------------------------*/
  8119. BUILDIN_FUNC(deltimer)
  8120. {
  8121. const char *event;
  8122. TBL_PC* sd;
  8123. event=script_getstr(st, 2);
  8124. sd = script_rid2sd(st);
  8125. if( sd == NULL )
  8126. return 0;
  8127. check_event(st, event);
  8128. pc_deleventtimer(sd,event);
  8129. return SCRIPT_CMD_SUCCESS;
  8130. }
  8131. /*==========================================
  8132. *------------------------------------------*/
  8133. BUILDIN_FUNC(addtimercount)
  8134. {
  8135. const char *event;
  8136. int tick;
  8137. TBL_PC* sd;
  8138. tick=script_getnum(st,2);
  8139. event=script_getstr(st,3);
  8140. sd = script_rid2sd(st);
  8141. if( sd == NULL )
  8142. return 0;
  8143. check_event(st, event);
  8144. pc_addeventtimercount(sd,event,tick);
  8145. return SCRIPT_CMD_SUCCESS;
  8146. }
  8147. /*==========================================
  8148. *------------------------------------------*/
  8149. BUILDIN_FUNC(initnpctimer)
  8150. {
  8151. struct npc_data *nd;
  8152. int flag = 0;
  8153. if( script_hasdata(st,3) )
  8154. { //Two arguments: NPC name and attach flag.
  8155. nd = npc_name2id(script_getstr(st, 2));
  8156. flag = script_getnum(st,3);
  8157. }
  8158. else if( script_hasdata(st,2) )
  8159. { //Check if argument is numeric (flag) or string (npc name)
  8160. struct script_data *data;
  8161. data = script_getdata(st,2);
  8162. get_val(st,data);
  8163. if( data_isstring(data) ) //NPC name
  8164. nd = npc_name2id(conv_str(st, data));
  8165. else if( data_isint(data) ) //Flag
  8166. {
  8167. nd = (struct npc_data *)map_id2bl(st->oid);
  8168. flag = conv_num(st,data);
  8169. }
  8170. else
  8171. {
  8172. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8173. return 1;
  8174. }
  8175. }
  8176. else
  8177. nd = (struct npc_data *)map_id2bl(st->oid);
  8178. if( !nd )
  8179. return 0;
  8180. if( flag ) //Attach
  8181. {
  8182. TBL_PC* sd = script_rid2sd(st);
  8183. if( sd == NULL )
  8184. return 0;
  8185. nd->u.scr.rid = sd->bl.id;
  8186. }
  8187. nd->u.scr.timertick = 0;
  8188. npc_settimerevent_tick(nd,0);
  8189. npc_timerevent_start(nd, st->rid);
  8190. return SCRIPT_CMD_SUCCESS;
  8191. }
  8192. /*==========================================
  8193. *------------------------------------------*/
  8194. BUILDIN_FUNC(startnpctimer)
  8195. {
  8196. struct npc_data *nd;
  8197. int flag = 0;
  8198. if( script_hasdata(st,3) )
  8199. { //Two arguments: NPC name and attach flag.
  8200. nd = npc_name2id(script_getstr(st, 2));
  8201. flag = script_getnum(st,3);
  8202. }
  8203. else if( script_hasdata(st,2) )
  8204. { //Check if argument is numeric (flag) or string (npc name)
  8205. struct script_data *data;
  8206. data = script_getdata(st,2);
  8207. get_val(st,data);
  8208. if( data_isstring(data) ) //NPC name
  8209. nd = npc_name2id(conv_str(st, data));
  8210. else if( data_isint(data) ) //Flag
  8211. {
  8212. nd = (struct npc_data *)map_id2bl(st->oid);
  8213. flag = conv_num(st,data);
  8214. }
  8215. else
  8216. {
  8217. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8218. return 1;
  8219. }
  8220. }
  8221. else
  8222. nd=(struct npc_data *)map_id2bl(st->oid);
  8223. if( !nd )
  8224. return 0;
  8225. if( flag ) //Attach
  8226. {
  8227. TBL_PC* sd = script_rid2sd(st);
  8228. if( sd == NULL )
  8229. return 0;
  8230. nd->u.scr.rid = sd->bl.id;
  8231. }
  8232. npc_timerevent_start(nd, st->rid);
  8233. return SCRIPT_CMD_SUCCESS;
  8234. }
  8235. /*==========================================
  8236. *------------------------------------------*/
  8237. BUILDIN_FUNC(stopnpctimer)
  8238. {
  8239. struct npc_data *nd;
  8240. int flag = 0;
  8241. if( script_hasdata(st,3) )
  8242. { //Two arguments: NPC name and attach flag.
  8243. nd = npc_name2id(script_getstr(st, 2));
  8244. flag = script_getnum(st,3);
  8245. }
  8246. else if( script_hasdata(st,2) )
  8247. { //Check if argument is numeric (flag) or string (npc name)
  8248. struct script_data *data;
  8249. data = script_getdata(st,2);
  8250. get_val(st,data);
  8251. if( data_isstring(data) ) //NPC name
  8252. nd = npc_name2id(conv_str(st, data));
  8253. else if( data_isint(data) ) //Flag
  8254. {
  8255. nd = (struct npc_data *)map_id2bl(st->oid);
  8256. flag = conv_num(st,data);
  8257. }
  8258. else
  8259. {
  8260. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8261. return 1;
  8262. }
  8263. }
  8264. else
  8265. nd=(struct npc_data *)map_id2bl(st->oid);
  8266. if( !nd )
  8267. return 0;
  8268. if( flag ) //Detach
  8269. nd->u.scr.rid = 0;
  8270. npc_timerevent_stop(nd);
  8271. return SCRIPT_CMD_SUCCESS;
  8272. }
  8273. /*==========================================
  8274. *------------------------------------------*/
  8275. BUILDIN_FUNC(getnpctimer)
  8276. {
  8277. struct npc_data *nd;
  8278. TBL_PC *sd;
  8279. int type = script_getnum(st,2);
  8280. int val = 0;
  8281. if( script_hasdata(st,3) )
  8282. nd = npc_name2id(script_getstr(st,3));
  8283. else
  8284. nd = (struct npc_data *)map_id2bl(st->oid);
  8285. if( !nd || nd->bl.type != BL_NPC )
  8286. {
  8287. script_pushint(st,0);
  8288. ShowError("getnpctimer: Invalid NPC.\n");
  8289. return 1;
  8290. }
  8291. switch( type )
  8292. {
  8293. case 0: val = npc_gettimerevent_tick(nd); break;
  8294. case 1:
  8295. if( nd->u.scr.rid )
  8296. {
  8297. sd = map_id2sd(nd->u.scr.rid);
  8298. if( !sd )
  8299. {
  8300. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8301. break;
  8302. }
  8303. val = (sd->npc_timer_id != INVALID_TIMER);
  8304. }
  8305. else
  8306. val = (nd->u.scr.timerid != INVALID_TIMER);
  8307. break;
  8308. case 2: val = nd->u.scr.timeramount; break;
  8309. }
  8310. script_pushint(st,val);
  8311. return SCRIPT_CMD_SUCCESS;
  8312. }
  8313. /*==========================================
  8314. *------------------------------------------*/
  8315. BUILDIN_FUNC(setnpctimer)
  8316. {
  8317. int tick;
  8318. struct npc_data *nd;
  8319. tick = script_getnum(st,2);
  8320. if( script_hasdata(st,3) )
  8321. nd = npc_name2id(script_getstr(st,3));
  8322. else
  8323. nd = (struct npc_data *)map_id2bl(st->oid);
  8324. if( !nd || nd->bl.type != BL_NPC )
  8325. {
  8326. script_pushint(st,1);
  8327. ShowError("setnpctimer: Invalid NPC.\n");
  8328. return 1;
  8329. }
  8330. npc_settimerevent_tick(nd,tick);
  8331. script_pushint(st,0);
  8332. return SCRIPT_CMD_SUCCESS;
  8333. }
  8334. /*==========================================
  8335. * attaches the player rid to the timer [Celest]
  8336. *------------------------------------------*/
  8337. BUILDIN_FUNC(attachnpctimer)
  8338. {
  8339. TBL_PC *sd;
  8340. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  8341. if( !nd || nd->bl.type != BL_NPC )
  8342. {
  8343. script_pushint(st,1);
  8344. ShowError("setnpctimer: Invalid NPC.\n");
  8345. return 1;
  8346. }
  8347. if( script_hasdata(st,2) )
  8348. sd = map_nick2sd(script_getstr(st,2));
  8349. else
  8350. sd = script_rid2sd(st);
  8351. if( !sd )
  8352. {
  8353. script_pushint(st,1);
  8354. ShowWarning("attachnpctimer: Invalid player.\n");
  8355. return 1;
  8356. }
  8357. nd->u.scr.rid = sd->bl.id;
  8358. script_pushint(st,0);
  8359. return SCRIPT_CMD_SUCCESS;
  8360. }
  8361. /*==========================================
  8362. * detaches a player rid from the timer [Celest]
  8363. *------------------------------------------*/
  8364. BUILDIN_FUNC(detachnpctimer)
  8365. {
  8366. struct npc_data *nd;
  8367. if( script_hasdata(st,2) )
  8368. nd = npc_name2id(script_getstr(st,2));
  8369. else
  8370. nd = (struct npc_data *)map_id2bl(st->oid);
  8371. if( !nd || nd->bl.type != BL_NPC )
  8372. {
  8373. script_pushint(st,1);
  8374. ShowError("detachnpctimer: Invalid NPC.\n");
  8375. return 1;
  8376. }
  8377. nd->u.scr.rid = 0;
  8378. script_pushint(st,0);
  8379. return SCRIPT_CMD_SUCCESS;
  8380. }
  8381. /*==========================================
  8382. * To avoid "player not attached" script errors, this function is provided,
  8383. * it checks if there is a player attached to the current script. [Skotlex]
  8384. * If no, returns 0, if yes, returns the account_id of the attached player.
  8385. *------------------------------------------*/
  8386. BUILDIN_FUNC(playerattached)
  8387. {
  8388. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  8389. script_pushint(st,0);
  8390. else
  8391. script_pushint(st,st->rid);
  8392. return SCRIPT_CMD_SUCCESS;
  8393. }
  8394. /*==========================================
  8395. *------------------------------------------*/
  8396. BUILDIN_FUNC(announce)
  8397. {
  8398. const char *mes = script_getstr(st,2);
  8399. int flag = script_getnum(st,3);
  8400. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  8401. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  8402. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  8403. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  8404. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  8405. if (flag&0x0f) // Broadcast source or broadcast region defined
  8406. {
  8407. send_target target;
  8408. 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
  8409. if (bl == NULL)
  8410. return 0;
  8411. flag &= 0x07;
  8412. target = (flag == 1) ? ALL_SAMEMAP :
  8413. (flag == 2) ? AREA :
  8414. (flag == 3) ? SELF :
  8415. ALL_CLIENT;
  8416. if (fontColor)
  8417. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  8418. else
  8419. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  8420. }
  8421. else
  8422. {
  8423. if (fontColor)
  8424. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  8425. else
  8426. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  8427. }
  8428. return SCRIPT_CMD_SUCCESS;
  8429. }
  8430. /*==========================================
  8431. *------------------------------------------*/
  8432. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  8433. {
  8434. char *mes = va_arg(ap, char *);
  8435. int len = va_arg(ap, int);
  8436. int type = va_arg(ap, int);
  8437. char *fontColor = va_arg(ap, char *);
  8438. short fontType = (short)va_arg(ap, int);
  8439. short fontSize = (short)va_arg(ap, int);
  8440. short fontAlign = (short)va_arg(ap, int);
  8441. short fontY = (short)va_arg(ap, int);
  8442. if (fontColor)
  8443. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  8444. else
  8445. clif_broadcast(bl, mes, len, type, SELF);
  8446. return 0;
  8447. }
  8448. BUILDIN_FUNC(mapannounce)
  8449. {
  8450. const char *mapname = script_getstr(st,2);
  8451. const char *mes = script_getstr(st,3);
  8452. int flag = script_getnum(st,4);
  8453. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  8454. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  8455. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  8456. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  8457. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  8458. int16 m;
  8459. if ((m = map_mapname2mapid(mapname)) < 0)
  8460. return 0;
  8461. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  8462. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8463. return SCRIPT_CMD_SUCCESS;
  8464. }
  8465. /*==========================================
  8466. *------------------------------------------*/
  8467. BUILDIN_FUNC(areaannounce)
  8468. {
  8469. const char *mapname = script_getstr(st,2);
  8470. int x0 = script_getnum(st,3);
  8471. int y0 = script_getnum(st,4);
  8472. int x1 = script_getnum(st,5);
  8473. int y1 = script_getnum(st,6);
  8474. const char *mes = script_getstr(st,7);
  8475. int flag = script_getnum(st,8);
  8476. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  8477. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  8478. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  8479. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  8480. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  8481. int16 m;
  8482. if ((m = map_mapname2mapid(mapname)) < 0)
  8483. return 0;
  8484. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  8485. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8486. return SCRIPT_CMD_SUCCESS;
  8487. }
  8488. /*==========================================
  8489. *------------------------------------------*/
  8490. BUILDIN_FUNC(getusers)
  8491. {
  8492. int flag, val = 0;
  8493. struct map_session_data* sd;
  8494. struct block_list* bl = NULL;
  8495. flag = script_getnum(st,2);
  8496. switch(flag&0x07)
  8497. {
  8498. case 0:
  8499. if(flag&0x8)
  8500. {// npc
  8501. bl = map_id2bl(st->oid);
  8502. }
  8503. else if((sd = script_rid2sd(st))!=NULL)
  8504. {// pc
  8505. bl = &sd->bl;
  8506. }
  8507. if(bl)
  8508. {
  8509. val = map[bl->m].users;
  8510. }
  8511. break;
  8512. case 1:
  8513. val = map_getusers();
  8514. break;
  8515. default:
  8516. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  8517. script_pushint(st,0);
  8518. return 1;
  8519. }
  8520. script_pushint(st,val);
  8521. return SCRIPT_CMD_SUCCESS;
  8522. }
  8523. /*==========================================
  8524. * Works like @WHO - displays all online users names in window
  8525. *------------------------------------------*/
  8526. BUILDIN_FUNC(getusersname)
  8527. {
  8528. TBL_PC *sd, *pl_sd;
  8529. int /*disp_num=1,*/ group_level = 0;
  8530. struct s_mapiterator* iter;
  8531. sd = script_rid2sd(st);
  8532. if (!sd) return 0;
  8533. group_level = pc_get_group_level(sd);
  8534. iter = mapit_getallusers();
  8535. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  8536. {
  8537. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  8538. continue; // skip hidden sessions
  8539. /* Temporary fix for bugreport:1023.
  8540. * Do not uncomment unless you want thousands of 'next' buttons.
  8541. if((disp_num++)%10==0)
  8542. clif_scriptnext(sd,st->oid);*/
  8543. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  8544. }
  8545. mapit_free(iter);
  8546. return SCRIPT_CMD_SUCCESS;
  8547. }
  8548. /*==========================================
  8549. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  8550. *------------------------------------------*/
  8551. BUILDIN_FUNC(getmapguildusers)
  8552. {
  8553. const char *str;
  8554. int16 m;
  8555. int gid;
  8556. int i=0,c=0;
  8557. struct guild *g = NULL;
  8558. str=script_getstr(st,2);
  8559. gid=script_getnum(st,3);
  8560. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  8561. script_pushint(st,-1);
  8562. return 0;
  8563. }
  8564. g = guild_search(gid);
  8565. if (g){
  8566. for(i = 0; i < g->max_member; i++)
  8567. {
  8568. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  8569. c++;
  8570. }
  8571. }
  8572. script_pushint(st,c);
  8573. return SCRIPT_CMD_SUCCESS;
  8574. }
  8575. /*==========================================
  8576. *------------------------------------------*/
  8577. BUILDIN_FUNC(getmapusers)
  8578. {
  8579. const char *str;
  8580. int16 m;
  8581. str=script_getstr(st,2);
  8582. if( (m=map_mapname2mapid(str))< 0){
  8583. script_pushint(st,-1);
  8584. return 0;
  8585. }
  8586. script_pushint(st,map[m].users);
  8587. return SCRIPT_CMD_SUCCESS;
  8588. }
  8589. /*==========================================
  8590. *------------------------------------------*/
  8591. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  8592. {
  8593. int *users=va_arg(ap,int *);
  8594. (*users)++;
  8595. return 0;
  8596. }
  8597. BUILDIN_FUNC(getareausers)
  8598. {
  8599. const char *str;
  8600. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  8601. str=script_getstr(st,2);
  8602. x0=script_getnum(st,3);
  8603. y0=script_getnum(st,4);
  8604. x1=script_getnum(st,5);
  8605. y1=script_getnum(st,6);
  8606. if( (m=map_mapname2mapid(str))< 0){
  8607. script_pushint(st,-1);
  8608. return 0;
  8609. }
  8610. map_foreachinarea(buildin_getareausers_sub,
  8611. m,x0,y0,x1,y1,BL_PC,&users);
  8612. script_pushint(st,users);
  8613. return SCRIPT_CMD_SUCCESS;
  8614. }
  8615. /*==========================================
  8616. *------------------------------------------*/
  8617. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  8618. {
  8619. int item=va_arg(ap,int);
  8620. int *amount=va_arg(ap,int *);
  8621. struct flooritem_data *drop=(struct flooritem_data *)bl;
  8622. if(drop->item_data.nameid==item)
  8623. (*amount)+=drop->item_data.amount;
  8624. return 0;
  8625. }
  8626. BUILDIN_FUNC(getareadropitem)
  8627. {
  8628. const char *str;
  8629. int16 m,x0,y0,x1,y1;
  8630. int item,amount=0;
  8631. struct script_data *data;
  8632. str=script_getstr(st,2);
  8633. x0=script_getnum(st,3);
  8634. y0=script_getnum(st,4);
  8635. x1=script_getnum(st,5);
  8636. y1=script_getnum(st,6);
  8637. data=script_getdata(st,7);
  8638. get_val(st,data);
  8639. if( data_isstring(data) ){
  8640. const char *name=conv_str(st,data);
  8641. struct item_data *item_data = itemdb_searchname(name);
  8642. item=UNKNOWN_ITEM_ID;
  8643. if( item_data )
  8644. item=item_data->nameid;
  8645. }else
  8646. item=conv_num(st,data);
  8647. if( (m=map_mapname2mapid(str))< 0){
  8648. script_pushint(st,-1);
  8649. return 0;
  8650. }
  8651. map_foreachinarea(buildin_getareadropitem_sub,
  8652. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  8653. script_pushint(st,amount);
  8654. return SCRIPT_CMD_SUCCESS;
  8655. }
  8656. /*==========================================
  8657. *------------------------------------------*/
  8658. BUILDIN_FUNC(enablenpc)
  8659. {
  8660. const char *str;
  8661. str=script_getstr(st,2);
  8662. npc_enable(str,1);
  8663. return SCRIPT_CMD_SUCCESS;
  8664. }
  8665. /*==========================================
  8666. *------------------------------------------*/
  8667. BUILDIN_FUNC(disablenpc)
  8668. {
  8669. const char *str;
  8670. str=script_getstr(st,2);
  8671. npc_enable(str,0);
  8672. return SCRIPT_CMD_SUCCESS;
  8673. }
  8674. /*==========================================
  8675. *------------------------------------------*/
  8676. BUILDIN_FUNC(hideoffnpc)
  8677. {
  8678. const char *str;
  8679. str=script_getstr(st,2);
  8680. npc_enable(str,2);
  8681. return SCRIPT_CMD_SUCCESS;
  8682. }
  8683. /*==========================================
  8684. *------------------------------------------*/
  8685. BUILDIN_FUNC(hideonnpc)
  8686. {
  8687. const char *str;
  8688. str=script_getstr(st,2);
  8689. npc_enable(str,4);
  8690. return SCRIPT_CMD_SUCCESS;
  8691. }
  8692. /* Starts a status effect on the target unit or on the attached player.
  8693. *
  8694. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  8695. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  8696. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  8697. * <flag>
  8698. * &1: Cannot be avoided (it has to start)
  8699. * &2: Tick should not be reduced (by vit, luk, lv, etc)
  8700. * &4: sc_data loaded, no value has to be altered.
  8701. * &8: rate should not be reduced
  8702. */
  8703. BUILDIN_FUNC(sc_start)
  8704. {
  8705. TBL_NPC * nd = map_id2nd(st->oid);
  8706. struct block_list* bl;
  8707. enum sc_type type;
  8708. int tick, val1, val2, val3, val4=0, rate, flag;
  8709. char start_type;
  8710. const char* command = script_getfuncname(st);
  8711. if(strstr(command, "4"))
  8712. start_type = 4;
  8713. else if(strstr(command, "2"))
  8714. start_type = 2;
  8715. else
  8716. start_type = 1;
  8717. type = (sc_type)script_getnum(st,2);
  8718. tick = script_getnum(st,3);
  8719. val1 = script_getnum(st,4);
  8720. //If from NPC we make default flag 1 to be unavoidable
  8721. if(nd && nd->bl.id == fake_nd->bl.id)
  8722. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):2;
  8723. else
  8724. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):1;
  8725. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  8726. if(script_hasdata(st,(6+start_type)))
  8727. bl = map_id2bl(script_getnum(st,(6+start_type)));
  8728. else
  8729. bl = map_id2bl(st->rid);
  8730. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  8731. {// When there isn't a duration specified, try to get it from the skill_db
  8732. tick = skill_get_time(status_sc2skill(type), val1);
  8733. }
  8734. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  8735. bl = map_id2bl(potion_target);
  8736. tick /= 2;// Thrown potions only last half.
  8737. val4 = 1;// Mark that this was a thrown sc_effect
  8738. }
  8739. if(!bl)
  8740. return 0;
  8741. switch(start_type) {
  8742. case 1:
  8743. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  8744. break;
  8745. case 2:
  8746. val2 = script_getnum(st,5);
  8747. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  8748. break;
  8749. case 4:
  8750. val2 = script_getnum(st,5);
  8751. val3 = script_getnum(st,6);
  8752. val4 = script_getnum(st,7);
  8753. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  8754. break;
  8755. }
  8756. return SCRIPT_CMD_SUCCESS;
  8757. }
  8758. /// Ends one or all status effects on the target unit or on the attached player.
  8759. ///
  8760. /// sc_end <effect_id>{,<unit_id>};
  8761. BUILDIN_FUNC(sc_end)
  8762. {
  8763. struct block_list* bl;
  8764. int type;
  8765. type = script_getnum(st, 2);
  8766. if (script_hasdata(st, 3))
  8767. bl = map_id2bl(script_getnum(st, 3));
  8768. else
  8769. bl = map_id2bl(st->rid);
  8770. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  8771. bl = map_id2bl(potion_target);
  8772. if (!bl)
  8773. return 0;
  8774. if (type >= 0 && type < SC_MAX)
  8775. {
  8776. struct status_change *sc = status_get_sc(bl);
  8777. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  8778. if (!sce)
  8779. return 0;
  8780. switch (type)
  8781. {
  8782. case SC_WEIGHT50:
  8783. case SC_WEIGHT90:
  8784. case SC_NOCHAT:
  8785. case SC_PUSH_CART:
  8786. return 0;
  8787. default:
  8788. break;
  8789. }
  8790. //This should help status_change_end force disabling the SC in case it has no limit.
  8791. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8792. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8793. }
  8794. else
  8795. status_change_clear(bl, 3); // remove all effects
  8796. return SCRIPT_CMD_SUCCESS;
  8797. }
  8798. /*==========================================
  8799. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  8800. *------------------------------------------*/
  8801. BUILDIN_FUNC(getscrate)
  8802. {
  8803. struct block_list *bl;
  8804. int type,rate;
  8805. type=script_getnum(st,2);
  8806. rate=script_getnum(st,3);
  8807. if( script_hasdata(st,4) ) //get for the bl assigned
  8808. bl = map_id2bl(script_getnum(st,4));
  8809. else
  8810. bl = map_id2bl(st->rid);
  8811. if (bl)
  8812. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, 0);
  8813. script_pushint(st,rate);
  8814. return SCRIPT_CMD_SUCCESS;
  8815. }
  8816. /*==========================================
  8817. * getstatus <type>{, <info>};
  8818. *------------------------------------------*/
  8819. BUILDIN_FUNC(getstatus)
  8820. {
  8821. int id, type;
  8822. struct map_session_data* sd = script_rid2sd(st);
  8823. if( sd == NULL )
  8824. {// no player attached
  8825. return 0;
  8826. }
  8827. id = script_getnum(st, 2);
  8828. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  8829. if( id <= SC_NONE || id >= SC_MAX )
  8830. {// invalid status type given
  8831. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  8832. return 0;
  8833. }
  8834. if( sd->sc.count == 0 || !sd->sc.data[id] )
  8835. {// no status is active
  8836. script_pushint(st, 0);
  8837. return 0;
  8838. }
  8839. switch( type )
  8840. {
  8841. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  8842. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  8843. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  8844. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  8845. case 5:
  8846. {
  8847. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  8848. if( timer )
  8849. {// return the amount of time remaining
  8850. script_pushint(st, timer->tick - gettick());
  8851. }
  8852. }
  8853. break;
  8854. default: script_pushint(st, 1); break;
  8855. }
  8856. return SCRIPT_CMD_SUCCESS;
  8857. }
  8858. /*==========================================
  8859. *
  8860. *------------------------------------------*/
  8861. BUILDIN_FUNC(debugmes)
  8862. {
  8863. const char *str;
  8864. str=script_getstr(st,2);
  8865. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8866. return SCRIPT_CMD_SUCCESS;
  8867. }
  8868. /*==========================================
  8869. *------------------------------------------*/
  8870. BUILDIN_FUNC(catchpet)
  8871. {
  8872. int pet_id;
  8873. TBL_PC *sd;
  8874. pet_id= script_getnum(st,2);
  8875. sd=script_rid2sd(st);
  8876. if( sd == NULL )
  8877. return 0;
  8878. pet_catch_process1(sd,pet_id);
  8879. return SCRIPT_CMD_SUCCESS;
  8880. }
  8881. /*==========================================
  8882. * [orn]
  8883. *------------------------------------------*/
  8884. BUILDIN_FUNC(homunculus_evolution)
  8885. {
  8886. TBL_PC *sd;
  8887. sd=script_rid2sd(st);
  8888. if( sd == NULL )
  8889. return 0;
  8890. if(merc_is_hom_active(sd->hd))
  8891. {
  8892. if (sd->hd->homunculus.intimacy > 91000)
  8893. merc_hom_evolution(sd->hd);
  8894. else
  8895. clif_emotion(&sd->hd->bl, E_SWT);
  8896. }
  8897. return SCRIPT_CMD_SUCCESS;
  8898. }
  8899. /*==========================================
  8900. * Checks for vaporized morph state
  8901. * and deletes ITEMID_STRANGE_EMBRYO.
  8902. *------------------------------------------*/
  8903. BUILDIN_FUNC(homunculus_mutate)
  8904. {
  8905. int homun_id, m_class, m_id, i;
  8906. TBL_PC *sd;
  8907. sd = script_rid2sd(st);
  8908. if( sd == NULL || sd->hd == NULL )
  8909. return 0;
  8910. if(script_hasdata(st,2))
  8911. homun_id = script_getnum(st,2);
  8912. else
  8913. homun_id = 6048 + (rnd() % 4);
  8914. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  8915. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8916. m_id = hom_class2mapid(homun_id);
  8917. i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  8918. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  8919. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  8920. merc_call_homunculus(sd); // Respawn homunculus.
  8921. hom_mutate(sd->hd, homun_id);
  8922. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  8923. script_pushint(st, 1);
  8924. return 0;
  8925. } else
  8926. clif_emotion(&sd->bl, E_SWT);
  8927. } else
  8928. clif_emotion(&sd->bl, E_SWT);
  8929. script_pushint(st, 0);
  8930. return SCRIPT_CMD_SUCCESS;
  8931. }
  8932. /*==========================================
  8933. * Puts homunculus into morph state
  8934. * and gives ITEMID_STRANGE_EMBRYO.
  8935. *------------------------------------------*/
  8936. BUILDIN_FUNC(morphembryo)
  8937. {
  8938. struct item item_tmp;
  8939. int m_class, i=0;
  8940. TBL_PC *sd;
  8941. sd = script_rid2sd(st);
  8942. if( sd == NULL || sd->hd == NULL )
  8943. return 0;
  8944. if( merc_is_hom_active(sd->hd) ) {
  8945. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8946. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  8947. memset(&item_tmp, 0, sizeof(item_tmp));
  8948. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  8949. item_tmp.identify = 1;
  8950. if( item_tmp.nameid == 0 || (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  8951. clif_additem(sd, 0, 0, i);
  8952. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  8953. } else {
  8954. merc_hom_vaporize(sd, HOM_ST_MORPH);
  8955. script_pushint(st, 1);
  8956. return 0;
  8957. }
  8958. } else
  8959. clif_emotion(&sd->hd->bl, E_SWT);
  8960. } else
  8961. clif_emotion(&sd->bl, E_SWT);
  8962. script_pushint(st, 0);
  8963. return SCRIPT_CMD_SUCCESS;
  8964. }
  8965. // [Zephyrus]
  8966. BUILDIN_FUNC(homunculus_shuffle)
  8967. {
  8968. TBL_PC *sd;
  8969. sd=script_rid2sd(st);
  8970. if( sd == NULL )
  8971. return 0;
  8972. if(merc_is_hom_active(sd->hd))
  8973. merc_hom_shuffle(sd->hd);
  8974. return SCRIPT_CMD_SUCCESS;
  8975. }
  8976. /*==========================================
  8977. * Check for homunculus state.
  8978. * Return: -1 = No homunculus
  8979. * 0 = Homunculus is active
  8980. * 1 = Homunculus is vaporized (rest)
  8981. * 2 = Homunculus is in morph state
  8982. *------------------------------------------*/
  8983. BUILDIN_FUNC(checkhomcall)
  8984. {
  8985. TBL_PC *sd = script_rid2sd(st);
  8986. TBL_HOM *hd;
  8987. if( sd == NULL )
  8988. return 0;
  8989. hd = sd->hd;
  8990. if( !hd )
  8991. script_pushint(st, -1);
  8992. else
  8993. script_pushint(st, hd->homunculus.vaporize);
  8994. return SCRIPT_CMD_SUCCESS;
  8995. }
  8996. //These two functions bring the eA MAPID_* class functionality to scripts.
  8997. BUILDIN_FUNC(eaclass)
  8998. {
  8999. int class_;
  9000. if( script_hasdata(st,2) )
  9001. class_ = script_getnum(st,2);
  9002. else {
  9003. TBL_PC *sd;
  9004. sd=script_rid2sd(st);
  9005. if (!sd) {
  9006. script_pushint(st,-1);
  9007. return 0;
  9008. }
  9009. class_ = sd->status.class_;
  9010. }
  9011. script_pushint(st,pc_jobid2mapid(class_));
  9012. return SCRIPT_CMD_SUCCESS;
  9013. }
  9014. BUILDIN_FUNC(roclass)
  9015. {
  9016. int class_ =script_getnum(st,2);
  9017. int sex;
  9018. if( script_hasdata(st,3) )
  9019. sex = script_getnum(st,3);
  9020. else {
  9021. TBL_PC *sd;
  9022. if (st->rid && (sd=script_rid2sd(st)))
  9023. sex = sd->status.sex;
  9024. else
  9025. sex = 1; //Just use male when not found.
  9026. }
  9027. script_pushint(st,pc_mapid2jobid(class_, sex));
  9028. return SCRIPT_CMD_SUCCESS;
  9029. }
  9030. /*==========================================
  9031. * Tells client to open a hatching window, used for pet incubator
  9032. *------------------------------------------*/
  9033. BUILDIN_FUNC(birthpet)
  9034. {
  9035. TBL_PC *sd;
  9036. sd=script_rid2sd(st);
  9037. if( sd == NULL )
  9038. return 0;
  9039. if( sd->status.pet_id )
  9040. {// do not send egg list, when you already have a pet
  9041. return 0;
  9042. }
  9043. clif_sendegg(sd);
  9044. return SCRIPT_CMD_SUCCESS;
  9045. }
  9046. /*==========================================
  9047. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  9048. * @type
  9049. * 1 : make like after rebirth
  9050. * 2 : blvl,jlvl=1, skillpoint=0
  9051. * 3 : don't reset skill, blvl=1
  9052. * 4 : jlvl=0
  9053. *------------------------------------------*/
  9054. BUILDIN_FUNC(resetlvl)
  9055. {
  9056. TBL_PC *sd;
  9057. int type=script_getnum(st,2);
  9058. sd=script_rid2sd(st);
  9059. if( sd == NULL )
  9060. return 0;
  9061. pc_resetlvl(sd,type);
  9062. return SCRIPT_CMD_SUCCESS;
  9063. }
  9064. /*==========================================
  9065. * Reset a player status point
  9066. *------------------------------------------*/
  9067. BUILDIN_FUNC(resetstatus)
  9068. {
  9069. TBL_PC *sd;
  9070. sd=script_rid2sd(st);
  9071. pc_resetstate(sd);
  9072. return SCRIPT_CMD_SUCCESS;
  9073. }
  9074. /*==========================================
  9075. * script command resetskill
  9076. *------------------------------------------*/
  9077. BUILDIN_FUNC(resetskill)
  9078. {
  9079. TBL_PC *sd;
  9080. sd=script_rid2sd(st);
  9081. pc_resetskill(sd,1);
  9082. return SCRIPT_CMD_SUCCESS;
  9083. }
  9084. /*==========================================
  9085. * Counts total amount of skill points.
  9086. *------------------------------------------*/
  9087. BUILDIN_FUNC(skillpointcount)
  9088. {
  9089. TBL_PC *sd;
  9090. sd=script_rid2sd(st);
  9091. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9092. return SCRIPT_CMD_SUCCESS;
  9093. }
  9094. /*==========================================
  9095. *
  9096. *------------------------------------------*/
  9097. BUILDIN_FUNC(changebase)
  9098. {
  9099. TBL_PC *sd=NULL;
  9100. int vclass;
  9101. if( script_hasdata(st,3) )
  9102. sd=map_id2sd(script_getnum(st,3));
  9103. else
  9104. sd=script_rid2sd(st);
  9105. if(sd == NULL)
  9106. return 0;
  9107. vclass = script_getnum(st,2);
  9108. if(vclass == JOB_WEDDING)
  9109. {
  9110. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9111. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9112. )
  9113. return 0;
  9114. }
  9115. if(!sd->disguise && vclass != sd->vd.class_) {
  9116. status_set_viewdata(&sd->bl, vclass);
  9117. //Updated client view. Base, Weapon and Cloth Colors.
  9118. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9119. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9120. if (sd->vd.cloth_color)
  9121. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9122. clif_skillinfoblock(sd);
  9123. }
  9124. return SCRIPT_CMD_SUCCESS;
  9125. }
  9126. /*==========================================
  9127. * Unequip all item and request for a changesex to char-serv
  9128. *------------------------------------------*/
  9129. BUILDIN_FUNC(changesex)
  9130. {
  9131. int i;
  9132. TBL_PC *sd = NULL;
  9133. sd = script_rid2sd(st);
  9134. pc_resetskill(sd,4);
  9135. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9136. for( i=0; i<EQI_MAX; i++ )
  9137. if( sd->equip_index[i] >= 0 ) pc_unequipitem(sd, sd->equip_index[i], 3);
  9138. chrif_changesex(sd);
  9139. return SCRIPT_CMD_SUCCESS;
  9140. }
  9141. /*==========================================
  9142. * Works like 'announce' but outputs in the common chat window
  9143. *------------------------------------------*/
  9144. BUILDIN_FUNC(globalmes)
  9145. {
  9146. struct block_list *bl = map_id2bl(st->oid);
  9147. struct npc_data *nd = (struct npc_data *)bl;
  9148. const char *name=NULL,*mes;
  9149. mes=script_getstr(st,2);
  9150. if(mes==NULL) return 0;
  9151. if(script_hasdata(st,3)){ // npc name to display
  9152. name=script_getstr(st,3);
  9153. } else {
  9154. name=nd->name; //use current npc name
  9155. }
  9156. npc_globalmessage(name,mes); // broadcast to all players connected
  9157. return SCRIPT_CMD_SUCCESS;
  9158. }
  9159. /////////////////////////////////////////////////////////////////////
  9160. // NPC waiting room (chat room)
  9161. //
  9162. /// Creates a waiting room (chat room) for this npc.
  9163. ///
  9164. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9165. BUILDIN_FUNC(waitingroom)
  9166. {
  9167. struct npc_data* nd;
  9168. int pub = 1;
  9169. const char* title = script_getstr(st, 2);
  9170. int limit = script_getnum(st, 3);
  9171. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9172. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9173. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9174. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9175. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9176. nd = (struct npc_data *)map_id2bl(st->oid);
  9177. if( nd != NULL )
  9178. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  9179. return SCRIPT_CMD_SUCCESS;
  9180. }
  9181. /// Removes the waiting room of the current or target npc.
  9182. ///
  9183. /// delwaitingroom "<npc_name>";
  9184. /// delwaitingroom;
  9185. BUILDIN_FUNC(delwaitingroom)
  9186. {
  9187. struct npc_data* nd;
  9188. if( script_hasdata(st,2) )
  9189. nd = npc_name2id(script_getstr(st, 2));
  9190. else
  9191. nd = (struct npc_data *)map_id2bl(st->oid);
  9192. if( nd != NULL )
  9193. chat_deletenpcchat(nd);
  9194. return SCRIPT_CMD_SUCCESS;
  9195. }
  9196. /// Kicks all the players from the waiting room of the current or target npc.
  9197. ///
  9198. /// kickwaitingroomall "<npc_name>";
  9199. /// kickwaitingroomall;
  9200. BUILDIN_FUNC(waitingroomkickall)
  9201. {
  9202. struct npc_data* nd;
  9203. struct chat_data* cd;
  9204. if( script_hasdata(st,2) )
  9205. nd = npc_name2id(script_getstr(st,2));
  9206. else
  9207. nd = (struct npc_data *)map_id2bl(st->oid);
  9208. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9209. chat_npckickall(cd);
  9210. return SCRIPT_CMD_SUCCESS;
  9211. }
  9212. /// Enables the waiting room event of the current or target npc.
  9213. ///
  9214. /// enablewaitingroomevent "<npc_name>";
  9215. /// enablewaitingroomevent;
  9216. BUILDIN_FUNC(enablewaitingroomevent)
  9217. {
  9218. struct npc_data* nd;
  9219. struct chat_data* cd;
  9220. if( script_hasdata(st,2) )
  9221. nd = npc_name2id(script_getstr(st, 2));
  9222. else
  9223. nd = (struct npc_data *)map_id2bl(st->oid);
  9224. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9225. chat_enableevent(cd);
  9226. return SCRIPT_CMD_SUCCESS;
  9227. }
  9228. /// Disables the waiting room event of the current or target npc.
  9229. ///
  9230. /// disablewaitingroomevent "<npc_name>";
  9231. /// disablewaitingroomevent;
  9232. BUILDIN_FUNC(disablewaitingroomevent)
  9233. {
  9234. struct npc_data *nd;
  9235. struct chat_data *cd;
  9236. if( script_hasdata(st,2) )
  9237. nd = npc_name2id(script_getstr(st, 2));
  9238. else
  9239. nd = (struct npc_data *)map_id2bl(st->oid);
  9240. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9241. chat_disableevent(cd);
  9242. return SCRIPT_CMD_SUCCESS;
  9243. }
  9244. /// Returns info on the waiting room of the current or target npc.
  9245. /// Returns -1 if the type unknown
  9246. /// <type>=0 : current number of users
  9247. /// <type>=1 : maximum number of users allowed
  9248. /// <type>=2 : the number of users that trigger the event
  9249. /// <type>=3 : if the trigger is disabled
  9250. /// <type>=4 : the title of the waiting room
  9251. /// <type>=5 : the password of the waiting room
  9252. /// <type>=16 : the name of the waiting room event
  9253. /// <type>=32 : if the waiting room is full
  9254. /// <type>=33 : if there are enough users to trigger the event
  9255. ///
  9256. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9257. /// getwaitingroomstate(<type>) -> <info>
  9258. BUILDIN_FUNC(getwaitingroomstate)
  9259. {
  9260. struct npc_data *nd;
  9261. struct chat_data *cd;
  9262. int type;
  9263. type = script_getnum(st,2);
  9264. if( script_hasdata(st,3) )
  9265. nd = npc_name2id(script_getstr(st, 3));
  9266. else
  9267. nd = (struct npc_data *)map_id2bl(st->oid);
  9268. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9269. {
  9270. script_pushint(st, -1);
  9271. return 0;
  9272. }
  9273. switch(type)
  9274. {
  9275. case 0: script_pushint(st, cd->users); break;
  9276. case 1: script_pushint(st, cd->limit); break;
  9277. case 2: script_pushint(st, cd->trigger&0x7f); break;
  9278. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  9279. case 4: script_pushstrcopy(st, cd->title); break;
  9280. case 5: script_pushstrcopy(st, cd->pass); break;
  9281. case 16: script_pushstrcopy(st, cd->npc_event);break;
  9282. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  9283. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  9284. default: script_pushint(st, -1); break;
  9285. }
  9286. return SCRIPT_CMD_SUCCESS;
  9287. }
  9288. /// Warps the trigger or target amount of players to the target map and position.
  9289. /// Players are automatically removed from the waiting room.
  9290. /// Those waiting the longest will get warped first.
  9291. /// The target map can be "Random" for a random position in the current map,
  9292. /// and "SavePoint" for the savepoint map+position.
  9293. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  9294. ///
  9295. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  9296. /// The total number of teleported players is put into $@warpwaitingpcnum
  9297. ///
  9298. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  9299. /// warpwaitingpc "<map name>",<x>,<y>;
  9300. BUILDIN_FUNC(warpwaitingpc)
  9301. {
  9302. int x;
  9303. int y;
  9304. int i;
  9305. int n;
  9306. const char* map_name;
  9307. struct npc_data* nd;
  9308. struct chat_data* cd;
  9309. TBL_PC* sd;
  9310. nd = (struct npc_data *)map_id2bl(st->oid);
  9311. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9312. return 0;
  9313. map_name = script_getstr(st,2);
  9314. x = script_getnum(st,3);
  9315. y = script_getnum(st,4);
  9316. n = cd->trigger&0x7f;
  9317. if( script_hasdata(st,5) )
  9318. n = script_getnum(st,5);
  9319. for( i = 0; i < n && cd->users > 0; i++ )
  9320. {
  9321. sd = cd->usersd[0];
  9322. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  9323. {// can't teleport on this map
  9324. break;
  9325. }
  9326. if( cd->zeny )
  9327. {// fee set
  9328. if( (uint32)sd->status.zeny < cd->zeny )
  9329. {// no zeny to cover set fee
  9330. break;
  9331. }
  9332. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  9333. }
  9334. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  9335. if( strcmp(map_name,"Random") == 0 )
  9336. pc_randomwarp(sd,CLR_TELEPORT);
  9337. else if( strcmp(map_name,"SavePoint") == 0 )
  9338. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  9339. else
  9340. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  9341. }
  9342. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  9343. return SCRIPT_CMD_SUCCESS;
  9344. }
  9345. /////////////////////////////////////////////////////////////////////
  9346. // ...
  9347. //
  9348. /// Detaches a character from a script.
  9349. ///
  9350. /// @param st Script state to detach the character from.
  9351. static void script_detach_rid(struct script_state* st)
  9352. {
  9353. if(st->rid)
  9354. {
  9355. script_detach_state(st, false);
  9356. st->rid = 0;
  9357. }
  9358. }
  9359. /*=========================================================================
  9360. * Attaches a set of RIDs to the current script. [digitalhamster]
  9361. * addrid(<type>{,<flag>{,<parameters>}});
  9362. * <type>:
  9363. * 0 : All players in the server.
  9364. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  9365. * 2 : Party members of a specified party ID.
  9366. * [ Parameters: <party id> ]
  9367. * 3 : Guild members of a specified guild ID.
  9368. * [ Parameters: <guild id> ]
  9369. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  9370. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  9371. * Account ID: The specified account ID.
  9372. * <flag>:
  9373. * 0 : Players are always attached. (default)
  9374. * 1 : Players currently running another script will not be attached.
  9375. *-------------------------------------------------------------------------*/
  9376. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  9377. {
  9378. int forceflag;
  9379. struct map_session_data *sd = (TBL_PC *)bl;
  9380. struct script_state* st;
  9381. st=va_arg(ap,struct script_state*);
  9382. forceflag=va_arg(ap,int);
  9383. if(!forceflag||!sd->st)
  9384. if(sd->status.account_id!=st->rid)
  9385. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9386. return 0;
  9387. }
  9388. BUILDIN_FUNC(addrid)
  9389. {
  9390. struct s_mapiterator* iter;
  9391. struct block_list *bl;
  9392. TBL_PC *sd;
  9393. if(st->rid<1){
  9394. st->state = END;
  9395. bl=map_id2bl(st->oid);
  9396. } else
  9397. bl=map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  9398. iter = mapit_getallusers();
  9399. switch(script_getnum(st,2)){
  9400. case 0:
  9401. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9402. if(!script_getnum(st,3)||!sd->st)
  9403. if(sd->status.account_id!=st->rid) //attached player already runs.
  9404. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9405. }
  9406. break;
  9407. case 1:
  9408. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9409. if(!script_getnum(st,3)||!sd->st)
  9410. if((sd->bl.m == bl->m)&&(sd->status.account_id!=st->rid))
  9411. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9412. }
  9413. break;
  9414. case 2:
  9415. if(script_getnum(st,4)==0){
  9416. script_pushint(st,0);
  9417. return 0;
  9418. }
  9419. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9420. if(!script_getnum(st,3)||!sd->st)
  9421. if((sd->status.account_id!=st->rid)&&(sd->status.party_id==script_getnum(st,4))) //attached player already runs.
  9422. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9423. }
  9424. break;
  9425. case 3:
  9426. if(script_getnum(st,4)==0){
  9427. script_pushint(st,0);
  9428. return 0;
  9429. }
  9430. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9431. if(!script_getnum(st,3)||!sd->st)
  9432. if((sd->status.account_id!=st->rid)&&(sd->status.guild_id==script_getnum(st,4))) //attached player already runs.
  9433. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9434. }
  9435. break;
  9436. case 4:
  9437. map_foreachinarea(buildin_addrid_sub,
  9438. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  9439. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  9440. break;
  9441. default:
  9442. if((map_id2sd(script_getnum(st,2)))==NULL){ // Player not found.
  9443. script_pushint(st,0);
  9444. return 0;
  9445. }
  9446. if(!script_getnum(st,3)||!map_id2sd(script_getnum(st,2))->st) {
  9447. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  9448. script_pushint(st,1);
  9449. }
  9450. return 0;
  9451. }
  9452. mapit_free(iter);
  9453. script_pushint(st,1);
  9454. return SCRIPT_CMD_SUCCESS;
  9455. }
  9456. /*==========================================
  9457. * Attach sd char id to script and detach current one if any
  9458. *------------------------------------------*/
  9459. BUILDIN_FUNC(attachrid)
  9460. {
  9461. int rid = script_getnum(st,2);
  9462. if (map_id2sd(rid) != NULL) {
  9463. script_detach_rid(st);
  9464. st->rid = rid;
  9465. script_attach_state(st);
  9466. script_pushint(st,1);
  9467. } else
  9468. script_pushint(st,0);
  9469. return SCRIPT_CMD_SUCCESS;
  9470. }
  9471. /*==========================================
  9472. * Detach script to rid
  9473. *------------------------------------------*/
  9474. BUILDIN_FUNC(detachrid)
  9475. {
  9476. script_detach_rid(st);
  9477. return SCRIPT_CMD_SUCCESS;
  9478. }
  9479. /*==========================================
  9480. * Chk if account connected, (and charid from account if specified)
  9481. *------------------------------------------*/
  9482. BUILDIN_FUNC(isloggedin)
  9483. {
  9484. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  9485. if (script_hasdata(st,3) && sd &&
  9486. sd->status.char_id != script_getnum(st,3))
  9487. sd = NULL;
  9488. push_val(st->stack,C_INT,sd!=NULL);
  9489. return SCRIPT_CMD_SUCCESS;
  9490. }
  9491. /*==========================================
  9492. *
  9493. *------------------------------------------*/
  9494. BUILDIN_FUNC(setmapflagnosave)
  9495. {
  9496. int16 m,x,y;
  9497. unsigned short mapindex;
  9498. const char *str,*str2;
  9499. str=script_getstr(st,2);
  9500. str2=script_getstr(st,3);
  9501. x=script_getnum(st,4);
  9502. y=script_getnum(st,5);
  9503. m = map_mapname2mapid(str);
  9504. mapindex = mapindex_name2id(str2);
  9505. if(m >= 0 && mapindex) {
  9506. map[m].flag.nosave=1;
  9507. map[m].save.map=mapindex;
  9508. map[m].save.x=x;
  9509. map[m].save.y=y;
  9510. }
  9511. return SCRIPT_CMD_SUCCESS;
  9512. }
  9513. BUILDIN_FUNC(getmapflag)
  9514. {
  9515. int16 m,i;
  9516. const char *str;
  9517. str=script_getstr(st,2);
  9518. i=script_getnum(st,3);
  9519. m = map_mapname2mapid(str);
  9520. if(m >= 0) {
  9521. switch(i) {
  9522. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  9523. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  9524. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  9525. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  9526. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  9527. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  9528. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  9529. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  9530. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  9531. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  9532. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  9533. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  9534. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  9535. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  9536. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  9537. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  9538. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  9539. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  9540. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  9541. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  9542. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  9543. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  9544. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  9545. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  9546. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  9547. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  9548. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  9549. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  9550. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  9551. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  9552. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  9553. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  9554. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  9555. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  9556. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  9557. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  9558. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  9559. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  9560. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  9561. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  9562. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  9563. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  9564. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  9565. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  9566. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  9567. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  9568. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  9569. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  9570. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  9571. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  9572. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  9573. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  9574. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  9575. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  9576. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  9577. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  9578. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  9579. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  9580. #ifdef ADJUST_SKILL_DAMAGE
  9581. case MF_SKILL_DAMAGE:
  9582. {
  9583. int ret_val=0, type=0;
  9584. FETCH(4,type);
  9585. switch (type) {
  9586. case 1: ret_val = map[m].adjust.damage.pc; break;
  9587. case 2: ret_val = map[m].adjust.damage.mob; break;
  9588. case 3: ret_val = map[m].adjust.damage.boss; break;
  9589. case 4: ret_val = map[m].adjust.damage.other; break;
  9590. case 5: ret_val = map[m].adjust.damage.caster; break;
  9591. default: ret_val = map[m].flag.skill_damage; break;
  9592. }
  9593. script_pushint(st,ret_val); break;
  9594. } break;
  9595. #endif
  9596. }
  9597. }
  9598. return SCRIPT_CMD_SUCCESS;
  9599. }
  9600. /* pvp timer handling */
  9601. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  9602. TBL_PC* sd = (TBL_PC*)bl;
  9603. if (sd->pvp_timer == INVALID_TIMER) {
  9604. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  9605. sd->pvp_rank = 0;
  9606. sd->pvp_lastusers = 0;
  9607. sd->pvp_point = 5;
  9608. sd->pvp_won = 0;
  9609. sd->pvp_lost = 0;
  9610. }
  9611. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  9612. clif_maptypeproperty2(&sd->bl,SELF);
  9613. return 0;
  9614. }
  9615. BUILDIN_FUNC(setmapflag)
  9616. {
  9617. int16 m,i;
  9618. const char *str;
  9619. int val=0;
  9620. str=script_getstr(st,2);
  9621. i=script_getnum(st,3);
  9622. FETCH(4,val);
  9623. m = map_mapname2mapid(str);
  9624. if(m >= 0) {
  9625. switch(i) {
  9626. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  9627. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  9628. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  9629. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  9630. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  9631. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  9632. case MF_PVP:
  9633. map[m].flag.pvp = 1;
  9634. if( !battle_config.pk_mode ) {
  9635. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  9636. }
  9637. break;
  9638. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  9639. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  9640. case MF_GVG: {
  9641. struct block_list bl;
  9642. map[m].flag.gvg = 1;
  9643. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9644. bl.type = BL_NUL;
  9645. bl.m = m;
  9646. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9647. }
  9648. break;
  9649. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  9650. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  9651. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  9652. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  9653. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  9654. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  9655. case MF_SNOW: map[m].flag.snow = 1; break;
  9656. case MF_FOG: map[m].flag.fog = 1; break;
  9657. case MF_SAKURA: map[m].flag.sakura = 1; break;
  9658. case MF_LEAVES: map[m].flag.leaves = 1; break;
  9659. case MF_NOGO: map[m].flag.nogo = 1; break;
  9660. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  9661. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  9662. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  9663. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  9664. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  9665. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  9666. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  9667. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  9668. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  9669. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  9670. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  9671. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  9672. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  9673. case MF_RESTRICTED:
  9674. map[m].zone |= 1<<(val+1);
  9675. map[m].flag.restricted=1;
  9676. break;
  9677. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  9678. case MF_NODROP: map[m].flag.nodrop = 1; break;
  9679. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  9680. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  9681. case MF_NOVENDING: map[m].flag.novending = 1; break;
  9682. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  9683. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  9684. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  9685. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  9686. case MF_TOWN: map[m].flag.town = 1; break;
  9687. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  9688. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  9689. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  9690. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  9691. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  9692. case MF_RESET: map[m].flag.reset = 1; break;
  9693. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  9694. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  9695. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  9696. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  9697. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  9698. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  9699. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  9700. #ifdef ADJUST_SKILL_DAMAGE
  9701. case MF_SKILL_DAMAGE:
  9702. {
  9703. int type=0;
  9704. FETCH(5,type);
  9705. switch (type) {
  9706. case 1: map[m].adjust.damage.pc = val; break;
  9707. case 2: map[m].adjust.damage.mob = val; break;
  9708. case 3: map[m].adjust.damage.boss = val; break;
  9709. case 4: map[m].adjust.damage.other = val; break;
  9710. case 5: map[m].adjust.damage.caster = val; break;
  9711. }
  9712. map[m].flag.skill_damage = 1;
  9713. } break;
  9714. #endif
  9715. }
  9716. }
  9717. return SCRIPT_CMD_SUCCESS;
  9718. }
  9719. BUILDIN_FUNC(removemapflag)
  9720. {
  9721. int16 m,i;
  9722. const char *str;
  9723. int val=0;
  9724. str=script_getstr(st,2);
  9725. i=script_getnum(st,3);
  9726. FETCH(4,val);
  9727. m = map_mapname2mapid(str);
  9728. if(m >= 0) {
  9729. switch(i) {
  9730. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  9731. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  9732. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  9733. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  9734. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  9735. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  9736. case MF_PVP: {
  9737. struct block_list bl;
  9738. bl.type = BL_NUL;
  9739. bl.m = m;
  9740. map[m].flag.pvp = 0;
  9741. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9742. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9743. }
  9744. break;
  9745. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  9746. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  9747. case MF_GVG: {
  9748. struct block_list bl;
  9749. bl.type = BL_NUL;
  9750. bl.m = m;
  9751. map[m].flag.gvg = 0;
  9752. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9753. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9754. }
  9755. break;
  9756. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  9757. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  9758. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  9759. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  9760. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  9761. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  9762. case MF_SNOW: map[m].flag.snow = 0; break;
  9763. case MF_FOG: map[m].flag.fog = 0; break;
  9764. case MF_SAKURA: map[m].flag.sakura = 0; break;
  9765. case MF_LEAVES: map[m].flag.leaves = 0; break;
  9766. case MF_NOGO: map[m].flag.nogo = 0; break;
  9767. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  9768. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  9769. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  9770. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  9771. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  9772. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  9773. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  9774. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  9775. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  9776. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  9777. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  9778. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  9779. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  9780. case MF_RESTRICTED:
  9781. map[m].zone ^= 1<<(val+1);
  9782. if (map[m].zone == 0){
  9783. map[m].flag.restricted=0;
  9784. }
  9785. break;
  9786. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  9787. case MF_NODROP: map[m].flag.nodrop = 0; break;
  9788. case MF_JEXP: map[m].adjust.jexp = 0; break;
  9789. case MF_BEXP: map[m].adjust.bexp = 0; break;
  9790. case MF_NOVENDING: map[m].flag.novending = 0; break;
  9791. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  9792. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  9793. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  9794. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  9795. case MF_TOWN: map[m].flag.town = 0; break;
  9796. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  9797. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  9798. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  9799. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  9800. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  9801. case MF_RESET: map[m].flag.reset = 0; break;
  9802. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  9803. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  9804. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  9805. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  9806. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  9807. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  9808. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  9809. #ifdef ADJUST_SKILL_DAMAGE
  9810. case MF_SKILL_DAMAGE:
  9811. {
  9812. map[m].flag.skill_damage = 0;
  9813. memset(&map[m].adjust.damage,0,sizeof(map[m].adjust.damage));
  9814. } break;
  9815. #endif
  9816. }
  9817. }
  9818. return SCRIPT_CMD_SUCCESS;
  9819. }
  9820. BUILDIN_FUNC(pvpon)
  9821. {
  9822. int16 m;
  9823. const char *str;
  9824. TBL_PC* sd = NULL;
  9825. struct s_mapiterator* iter;
  9826. struct block_list bl;
  9827. str = script_getstr(st,2);
  9828. m = map_mapname2mapid(str);
  9829. if( m < 0 || map[m].flag.pvp )
  9830. return 0; // nothing to do
  9831. map[m].flag.pvp = 1;
  9832. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  9833. bl.type = BL_NUL;
  9834. bl.m = m;
  9835. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9836. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  9837. return 0;
  9838. iter = mapit_getallusers();
  9839. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9840. {
  9841. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  9842. continue; // not applicable
  9843. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  9844. sd->pvp_rank = 0;
  9845. sd->pvp_lastusers = 0;
  9846. sd->pvp_point = 5;
  9847. sd->pvp_won = 0;
  9848. sd->pvp_lost = 0;
  9849. }
  9850. mapit_free(iter);
  9851. return SCRIPT_CMD_SUCCESS;
  9852. }
  9853. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  9854. {
  9855. TBL_PC* sd = (TBL_PC*)bl;
  9856. clif_pvpset(sd, 0, 0, 2);
  9857. if (sd->pvp_timer != INVALID_TIMER) {
  9858. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  9859. sd->pvp_timer = INVALID_TIMER;
  9860. }
  9861. return 0;
  9862. }
  9863. BUILDIN_FUNC(pvpoff)
  9864. {
  9865. int16 m;
  9866. const char *str;
  9867. struct block_list bl;
  9868. str=script_getstr(st,2);
  9869. m = map_mapname2mapid(str);
  9870. if(m < 0 || !map[m].flag.pvp)
  9871. return 0; //fixed Lupus
  9872. map[m].flag.pvp = 0;
  9873. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9874. bl.type = BL_NUL;
  9875. bl.m = m;
  9876. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9877. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  9878. return 0;
  9879. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  9880. return SCRIPT_CMD_SUCCESS;
  9881. }
  9882. BUILDIN_FUNC(gvgon)
  9883. {
  9884. int16 m;
  9885. const char *str;
  9886. struct block_list bl;
  9887. str=script_getstr(st,2);
  9888. m = map_mapname2mapid(str);
  9889. if(m >= 0 && !map[m].flag.gvg) {
  9890. map[m].flag.gvg = 1;
  9891. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9892. bl.type = BL_NUL;
  9893. bl.m = m;
  9894. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9895. }
  9896. return SCRIPT_CMD_SUCCESS;
  9897. }
  9898. BUILDIN_FUNC(gvgoff)
  9899. {
  9900. int16 m;
  9901. const char *str;
  9902. str=script_getstr(st,2);
  9903. m = map_mapname2mapid(str);
  9904. if(m >= 0 && map[m].flag.gvg) {
  9905. struct block_list bl;
  9906. map[m].flag.gvg = 0;
  9907. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9908. bl.type = BL_NUL;
  9909. bl.m = m;
  9910. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9911. }
  9912. return SCRIPT_CMD_SUCCESS;
  9913. }
  9914. /*==========================================
  9915. * Shows an emoticon on top of the player/npc
  9916. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  9917. *------------------------------------------*/
  9918. //Optional second parameter added by [Skotlex]
  9919. BUILDIN_FUNC(emotion)
  9920. {
  9921. int type;
  9922. int player=0;
  9923. type=script_getnum(st,2);
  9924. if(type < 0 || type > 100)
  9925. return 0;
  9926. if( script_hasdata(st,3) )
  9927. player=script_getnum(st,3);
  9928. if (player) {
  9929. TBL_PC *sd = NULL;
  9930. if( script_hasdata(st,4) )
  9931. sd = map_nick2sd(script_getstr(st,4));
  9932. else
  9933. sd = script_rid2sd(st);
  9934. if (sd)
  9935. clif_emotion(&sd->bl,type);
  9936. } else
  9937. if( script_hasdata(st,4) )
  9938. {
  9939. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  9940. if(nd)
  9941. clif_emotion(&nd->bl,type);
  9942. }
  9943. else
  9944. clif_emotion(map_id2bl(st->oid),type);
  9945. return SCRIPT_CMD_SUCCESS;
  9946. }
  9947. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  9948. {
  9949. int16 m=va_arg(ap,int);
  9950. int g_id=va_arg(ap,int);
  9951. int flag=va_arg(ap,int);
  9952. if(!sd || sd->bl.m != m)
  9953. return 0;
  9954. if(
  9955. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  9956. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  9957. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  9958. )
  9959. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  9960. return 1;
  9961. }
  9962. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  9963. {
  9964. struct mob_data *md=(struct mob_data *)bl;
  9965. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM)
  9966. status_kill(bl);
  9967. return 0;
  9968. }
  9969. /*
  9970. * Function to kick guild members out of a map and to their save points.
  9971. * m : mapid
  9972. * g_id : owner guild id
  9973. * flag & 1 : Warp out owners
  9974. * flag & 2 : Warp out outsiders
  9975. * flag & 4 : reserved for mobs
  9976. * */
  9977. BUILDIN_FUNC(maprespawnguildid)
  9978. {
  9979. const char *mapname=script_getstr(st,2);
  9980. int g_id=script_getnum(st,3);
  9981. int flag=script_getnum(st,4);
  9982. int16 m=map_mapname2mapid(mapname);
  9983. if(m == -1)
  9984. return 0;
  9985. //Catch ALL players (in case some are 'between maps' on execution time)
  9986. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  9987. if (flag&4) //Remove script mobs.
  9988. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  9989. return SCRIPT_CMD_SUCCESS;
  9990. }
  9991. BUILDIN_FUNC(agitstart)
  9992. {
  9993. if(agit_flag==1) return SCRIPT_CMD_SUCCESS; // Agit already Start.
  9994. agit_flag=1;
  9995. guild_agit_start();
  9996. return SCRIPT_CMD_SUCCESS;
  9997. }
  9998. BUILDIN_FUNC(agitend)
  9999. {
  10000. if(agit_flag==0) return SCRIPT_CMD_SUCCESS; // Agit already End.
  10001. agit_flag=0;
  10002. guild_agit_end();
  10003. return SCRIPT_CMD_SUCCESS;
  10004. }
  10005. BUILDIN_FUNC(agitstart2)
  10006. {
  10007. if(agit2_flag==1) return SCRIPT_CMD_SUCCESS; // Agit2 already Start.
  10008. agit2_flag=1;
  10009. guild_agit2_start();
  10010. return SCRIPT_CMD_SUCCESS;
  10011. }
  10012. BUILDIN_FUNC(agitend2)
  10013. {
  10014. if(agit2_flag==0) return SCRIPT_CMD_SUCCESS; // Agit2 already End.
  10015. agit2_flag=0;
  10016. guild_agit2_end();
  10017. return SCRIPT_CMD_SUCCESS;
  10018. }
  10019. /*==========================================
  10020. * Returns whether woe is on or off. // choice script
  10021. *------------------------------------------*/
  10022. BUILDIN_FUNC(agitcheck)
  10023. {
  10024. script_pushint(st,agit_flag);
  10025. return SCRIPT_CMD_SUCCESS;
  10026. }
  10027. /*==========================================
  10028. * Returns whether woese is on or off. // choice script
  10029. *------------------------------------------*/
  10030. BUILDIN_FUNC(agitcheck2)
  10031. {
  10032. script_pushint(st,agit2_flag);
  10033. return SCRIPT_CMD_SUCCESS;
  10034. }
  10035. /// Sets the guild_id of this npc.
  10036. ///
  10037. /// flagemblem <guild_id>;
  10038. BUILDIN_FUNC(flagemblem)
  10039. {
  10040. TBL_NPC* nd;
  10041. int g_id = script_getnum(st,2);
  10042. if(g_id < 0) return 0;
  10043. nd = (TBL_NPC*)map_id2nd(st->oid);
  10044. if( nd == NULL ) {
  10045. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10046. } else if( nd->subtype != SCRIPT ) {
  10047. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10048. } else {
  10049. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10050. nd->u.scr.guild_id = g_id;
  10051. clif_guild_emblem_area(&nd->bl);
  10052. /* guild flag caching */
  10053. if( g_id ) /* adding a id */
  10054. guild_flag_add(nd);
  10055. else if( changed ) /* removing a flag */
  10056. guild_flag_remove(nd);
  10057. }
  10058. return SCRIPT_CMD_SUCCESS;
  10059. }
  10060. BUILDIN_FUNC(getcastlename)
  10061. {
  10062. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10063. struct guild_castle* gc = guild_mapname2gc(mapname);
  10064. const char* name = (gc) ? gc->castle_name : "";
  10065. script_pushstrcopy(st,name);
  10066. return SCRIPT_CMD_SUCCESS;
  10067. }
  10068. BUILDIN_FUNC(getcastledata)
  10069. {
  10070. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10071. int index = script_getnum(st,3);
  10072. struct guild_castle *gc = guild_mapname2gc(mapname);
  10073. if (gc == NULL) {
  10074. script_pushint(st,0);
  10075. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10076. return 1;
  10077. }
  10078. switch (index) {
  10079. case 1:
  10080. script_pushint(st,gc->guild_id); break;
  10081. case 2:
  10082. script_pushint(st,gc->economy); break;
  10083. case 3:
  10084. script_pushint(st,gc->defense); break;
  10085. case 4:
  10086. script_pushint(st,gc->triggerE); break;
  10087. case 5:
  10088. script_pushint(st,gc->triggerD); break;
  10089. case 6:
  10090. script_pushint(st,gc->nextTime); break;
  10091. case 7:
  10092. script_pushint(st,gc->payTime); break;
  10093. case 8:
  10094. script_pushint(st,gc->createTime); break;
  10095. case 9:
  10096. script_pushint(st,gc->visibleC); break;
  10097. default:
  10098. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10099. script_pushint(st,gc->guardian[index-10].visible);
  10100. break;
  10101. }
  10102. script_pushint(st,0);
  10103. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10104. return 1;
  10105. }
  10106. return SCRIPT_CMD_SUCCESS;
  10107. }
  10108. BUILDIN_FUNC(setcastledata)
  10109. {
  10110. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10111. int index = script_getnum(st,3);
  10112. int value = script_getnum(st,4);
  10113. struct guild_castle *gc = guild_mapname2gc(mapname);
  10114. if (gc == NULL) {
  10115. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10116. return 1;
  10117. }
  10118. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10119. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10120. return 1;
  10121. }
  10122. guild_castledatasave(gc->castle_id, index, value);
  10123. return SCRIPT_CMD_SUCCESS;
  10124. }
  10125. /* =====================================================================
  10126. * ---------------------------------------------------------------------*/
  10127. BUILDIN_FUNC(requestguildinfo)
  10128. {
  10129. int guild_id=script_getnum(st,2);
  10130. const char *event=NULL;
  10131. if( script_hasdata(st,3) ){
  10132. event=script_getstr(st,3);
  10133. check_event(st, event);
  10134. }
  10135. if(guild_id>0)
  10136. guild_npc_request_info(guild_id,event);
  10137. return SCRIPT_CMD_SUCCESS;
  10138. }
  10139. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10140. /// getequipcardcnt(<equipment slot>);
  10141. BUILDIN_FUNC(getequipcardcnt)
  10142. {
  10143. int i=-1,j,num;
  10144. TBL_PC *sd;
  10145. int count;
  10146. num=script_getnum(st,2);
  10147. sd=script_rid2sd(st);
  10148. if (num > 0 && num <= ARRAYLENGTH(equip))
  10149. i=pc_checkequip(sd,equip[num-1]);
  10150. if (i < 0 || !sd->inventory_data[i]) {
  10151. script_pushint(st,0);
  10152. return 0;
  10153. }
  10154. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10155. {
  10156. script_pushint(st,0);
  10157. return 0;
  10158. }
  10159. count = 0;
  10160. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10161. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  10162. count++;
  10163. script_pushint(st,count);
  10164. return SCRIPT_CMD_SUCCESS;
  10165. }
  10166. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10167. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10168. /// successremovecards <slot>;
  10169. BUILDIN_FUNC(successremovecards) {
  10170. int i=-1,j,c,cardflag=0;
  10171. TBL_PC* sd = script_rid2sd(st);
  10172. int num = script_getnum(st,2);
  10173. if (num > 0 && num <= ARRAYLENGTH(equip))
  10174. i=pc_checkequip(sd,equip[num-1]);
  10175. if (i < 0 || !sd->inventory_data[i]) {
  10176. return 0;
  10177. }
  10178. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10179. return 0;
  10180. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10181. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10182. int flag;
  10183. struct item item_tmp;
  10184. memset(&item_tmp,0,sizeof(item_tmp));
  10185. cardflag = 1;
  10186. item_tmp.nameid = sd->status.inventory[i].card[c];
  10187. item_tmp.identify = 1;
  10188. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10189. clif_additem(sd,0,0,flag);
  10190. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10191. }
  10192. }
  10193. }
  10194. if(cardflag == 1) {//if card was remove remplace item with no card
  10195. int flag;
  10196. struct item item_tmp;
  10197. memset(&item_tmp,0,sizeof(item_tmp));
  10198. item_tmp.nameid = sd->status.inventory[i].nameid;
  10199. item_tmp.identify = 1;
  10200. item_tmp.refine = sd->status.inventory[i].refine;
  10201. item_tmp.attribute = sd->status.inventory[i].attribute;
  10202. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10203. item_tmp.bound = sd->status.inventory[i].bound;
  10204. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10205. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10206. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10207. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10208. clif_additem(sd,0,0,flag);
  10209. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10210. }
  10211. clif_misceffect(&sd->bl,3);
  10212. }
  10213. return SCRIPT_CMD_SUCCESS;
  10214. }
  10215. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10216. /// failedremovecards <slot>, <type>;
  10217. /// <type>=0 : will destroy both the item and the cards.
  10218. /// <type>=1 : will keep the item, but destroy the cards.
  10219. /// <type>=2 : will keep the cards, but destroy the item.
  10220. /// <type>=? : will just display the failure effect.
  10221. BUILDIN_FUNC(failedremovecards) {
  10222. int i=-1,j,c,cardflag=0;
  10223. TBL_PC* sd = script_rid2sd(st);
  10224. int num = script_getnum(st,2);
  10225. int typefail = script_getnum(st,3);
  10226. if (num > 0 && num <= ARRAYLENGTH(equip))
  10227. i=pc_checkequip(sd,equip[num-1]);
  10228. if (i < 0 || !sd->inventory_data[i])
  10229. return 0;
  10230. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10231. return 0;
  10232. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10233. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  10234. cardflag = 1;
  10235. if(typefail == 2) {// add cards to inventory, clear
  10236. int flag;
  10237. struct item item_tmp;
  10238. memset(&item_tmp,0,sizeof(item_tmp));
  10239. item_tmp.nameid = sd->status.inventory[i].card[c];
  10240. item_tmp.identify = 1;
  10241. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10242. clif_additem(sd,0,0,flag);
  10243. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10244. }
  10245. }
  10246. }
  10247. }
  10248. if(cardflag == 1) {
  10249. if(typefail == 0 || typefail == 2){ // destroy the item
  10250. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10251. }
  10252. if(typefail == 1){ // destroy the card
  10253. int flag;
  10254. struct item item_tmp;
  10255. memset(&item_tmp,0,sizeof(item_tmp));
  10256. item_tmp.nameid = sd->status.inventory[i].nameid;
  10257. item_tmp.identify = 1;
  10258. item_tmp.refine = sd->status.inventory[i].refine;
  10259. item_tmp.attribute = sd->status.inventory[i].attribute;
  10260. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10261. item_tmp.bound = sd->status.inventory[i].bound;
  10262. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10263. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10264. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10265. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10266. clif_additem(sd,0,0,flag);
  10267. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10268. }
  10269. }
  10270. clif_misceffect(&sd->bl,2);
  10271. }
  10272. return SCRIPT_CMD_SUCCESS;
  10273. }
  10274. /* ================================================================
  10275. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  10276. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  10277. * improved by [Lance]
  10278. * ================================================================*/
  10279. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  10280. {
  10281. int x,y,m,check_val=0,check_ID=0,i=0;
  10282. struct guild *g = NULL;
  10283. struct party_data *p = NULL;
  10284. const char *str;
  10285. const char *mapname;
  10286. unsigned int index;
  10287. mapname=script_getstr(st,2);
  10288. str=script_getstr(st,3);
  10289. x=script_getnum(st,4);
  10290. y=script_getnum(st,5);
  10291. if(script_hasdata(st,7)){
  10292. check_val=script_getnum(st,6);
  10293. check_ID=script_getnum(st,7);
  10294. }
  10295. if((m=map_mapname2mapid(mapname))< 0)
  10296. return 0;
  10297. if(!(index=mapindex_name2id(str)))
  10298. return 0;
  10299. switch(check_val){
  10300. case 1:
  10301. g = guild_search(check_ID);
  10302. if (g){
  10303. for( i=0; i < g->max_member; i++)
  10304. {
  10305. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  10306. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  10307. }
  10308. }
  10309. }
  10310. break;
  10311. case 2:
  10312. p = party_search(check_ID);
  10313. if(p){
  10314. for(i=0;i<MAX_PARTY; i++){
  10315. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  10316. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  10317. }
  10318. }
  10319. }
  10320. break;
  10321. default:
  10322. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  10323. break;
  10324. }
  10325. return SCRIPT_CMD_SUCCESS;
  10326. }
  10327. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  10328. {
  10329. char *event=va_arg(ap,char *);
  10330. struct mob_data *md = ((struct mob_data *)bl);
  10331. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  10332. return 1;
  10333. return SCRIPT_CMD_SUCCESS;
  10334. }
  10335. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  10336. {
  10337. const char *mapname,*event;
  10338. int16 m;
  10339. mapname=script_getstr(st,2);
  10340. event=script_getstr(st,3);
  10341. if( strcmp(event, "all") == 0 )
  10342. event = NULL;
  10343. else
  10344. check_event(st, event);
  10345. if( strcmp(mapname, "this") == 0 ) {
  10346. struct map_session_data *sd = script_rid2sd(st);
  10347. if( sd )
  10348. m = sd->bl.m;
  10349. else {
  10350. script_pushint(st,-1);
  10351. return 0;
  10352. }
  10353. }
  10354. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  10355. script_pushint(st,-1);
  10356. return 0;
  10357. }
  10358. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  10359. return SCRIPT_CMD_SUCCESS;
  10360. }
  10361. BUILDIN_FUNC(marriage)
  10362. {
  10363. const char *partner=script_getstr(st,2);
  10364. TBL_PC *sd=script_rid2sd(st);
  10365. TBL_PC *p_sd=map_nick2sd(partner);
  10366. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  10367. script_pushint(st,0);
  10368. return 0;
  10369. }
  10370. script_pushint(st,1);
  10371. return SCRIPT_CMD_SUCCESS;
  10372. }
  10373. BUILDIN_FUNC(wedding_effect)
  10374. {
  10375. TBL_PC *sd=script_rid2sd(st);
  10376. struct block_list *bl;
  10377. if(sd==NULL) {
  10378. bl=map_id2bl(st->oid);
  10379. } else
  10380. bl=&sd->bl;
  10381. clif_wedding_effect(bl);
  10382. return SCRIPT_CMD_SUCCESS;
  10383. }
  10384. BUILDIN_FUNC(divorce)
  10385. {
  10386. TBL_PC *sd=script_rid2sd(st);
  10387. if(!sd || !pc_divorce(sd)){
  10388. script_pushint(st,0);
  10389. return 0;
  10390. }
  10391. script_pushint(st,1);
  10392. return SCRIPT_CMD_SUCCESS;
  10393. }
  10394. BUILDIN_FUNC(ispartneron)
  10395. {
  10396. TBL_PC *sd=script_rid2sd(st);
  10397. if(sd==NULL || !pc_ismarried(sd) ||
  10398. map_charid2sd(sd->status.partner_id) == NULL) {
  10399. script_pushint(st,0);
  10400. return 0;
  10401. }
  10402. script_pushint(st,1);
  10403. return SCRIPT_CMD_SUCCESS;
  10404. }
  10405. BUILDIN_FUNC(getpartnerid)
  10406. {
  10407. TBL_PC *sd=script_rid2sd(st);
  10408. if (sd == NULL) {
  10409. script_pushint(st,0);
  10410. return 0;
  10411. }
  10412. script_pushint(st,sd->status.partner_id);
  10413. return SCRIPT_CMD_SUCCESS;
  10414. }
  10415. BUILDIN_FUNC(getchildid)
  10416. {
  10417. TBL_PC *sd=script_rid2sd(st);
  10418. if (sd == NULL) {
  10419. script_pushint(st,0);
  10420. return 0;
  10421. }
  10422. script_pushint(st,sd->status.child);
  10423. return SCRIPT_CMD_SUCCESS;
  10424. }
  10425. BUILDIN_FUNC(getmotherid)
  10426. {
  10427. TBL_PC *sd=script_rid2sd(st);
  10428. if (sd == NULL) {
  10429. script_pushint(st,0);
  10430. return 0;
  10431. }
  10432. script_pushint(st,sd->status.mother);
  10433. return SCRIPT_CMD_SUCCESS;
  10434. }
  10435. BUILDIN_FUNC(getfatherid)
  10436. {
  10437. TBL_PC *sd=script_rid2sd(st);
  10438. if (sd == NULL) {
  10439. script_pushint(st,0);
  10440. return 0;
  10441. }
  10442. script_pushint(st,sd->status.father);
  10443. return SCRIPT_CMD_SUCCESS;
  10444. }
  10445. BUILDIN_FUNC(warppartner)
  10446. {
  10447. int x,y;
  10448. unsigned short mapindex;
  10449. const char *str;
  10450. TBL_PC *sd=script_rid2sd(st);
  10451. TBL_PC *p_sd=NULL;
  10452. if(sd==NULL || !pc_ismarried(sd) ||
  10453. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  10454. script_pushint(st,0);
  10455. return 0;
  10456. }
  10457. str=script_getstr(st,2);
  10458. x=script_getnum(st,3);
  10459. y=script_getnum(st,4);
  10460. mapindex = mapindex_name2id(str);
  10461. if (mapindex) {
  10462. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  10463. script_pushint(st,1);
  10464. } else
  10465. script_pushint(st,0);
  10466. return SCRIPT_CMD_SUCCESS;
  10467. }
  10468. /*================================================
  10469. * Script for Displaying MOB Information [Valaris]
  10470. *------------------------------------------------*/
  10471. BUILDIN_FUNC(strmobinfo)
  10472. {
  10473. int num=script_getnum(st,2);
  10474. int class_=script_getnum(st,3);
  10475. if(!mobdb_checkid(class_))
  10476. {
  10477. if (num < 3) //requested a string
  10478. script_pushconststr(st,"");
  10479. else
  10480. script_pushint(st,0);
  10481. return 0;
  10482. }
  10483. switch (num) {
  10484. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  10485. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  10486. case 3: script_pushint(st,mob_db(class_)->lv); break;
  10487. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  10488. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  10489. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  10490. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  10491. default:
  10492. script_pushint(st,0);
  10493. break;
  10494. }
  10495. return SCRIPT_CMD_SUCCESS;
  10496. }
  10497. /*==========================================
  10498. * Summon guardians [Valaris]
  10499. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  10500. *------------------------------------------*/
  10501. BUILDIN_FUNC(guardian)
  10502. {
  10503. int class_=0,x=0,y=0,guardian=0;
  10504. const char *str,*map,*evt="";
  10505. struct script_data *data;
  10506. bool has_index = false;
  10507. map =script_getstr(st,2);
  10508. x =script_getnum(st,3);
  10509. y =script_getnum(st,4);
  10510. str =script_getstr(st,5);
  10511. class_=script_getnum(st,6);
  10512. if( script_hasdata(st,8) )
  10513. {// "<event label>",<guardian index>
  10514. evt=script_getstr(st,7);
  10515. guardian=script_getnum(st,8);
  10516. has_index = true;
  10517. } else if( script_hasdata(st,7) ){
  10518. data=script_getdata(st,7);
  10519. get_val(st,data);
  10520. if( data_isstring(data) )
  10521. {// "<event label>"
  10522. evt=script_getstr(st,7);
  10523. } else if( data_isint(data) )
  10524. {// <guardian index>
  10525. guardian=script_getnum(st,7);
  10526. has_index = true;
  10527. } else {
  10528. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  10529. script_reportdata(data);
  10530. return 1;
  10531. }
  10532. }
  10533. check_event(st, evt);
  10534. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  10535. return SCRIPT_CMD_SUCCESS;
  10536. }
  10537. /*==========================================
  10538. * Invisible Walls [Zephyrus]
  10539. *------------------------------------------*/
  10540. BUILDIN_FUNC(setwall)
  10541. {
  10542. const char *map, *name;
  10543. int x, y, m, size, dir;
  10544. bool shootable;
  10545. map = script_getstr(st,2);
  10546. x = script_getnum(st,3);
  10547. y = script_getnum(st,4);
  10548. size = script_getnum(st,5);
  10549. dir = script_getnum(st,6);
  10550. shootable = script_getnum(st,7);
  10551. name = script_getstr(st,8);
  10552. if( (m = map_mapname2mapid(map)) < 0 )
  10553. return 0; // Invalid Map
  10554. map_iwall_set(m, x, y, size, dir, shootable, name);
  10555. return SCRIPT_CMD_SUCCESS;
  10556. }
  10557. BUILDIN_FUNC(delwall)
  10558. {
  10559. const char *name = script_getstr(st,2);
  10560. map_iwall_remove(name);
  10561. return SCRIPT_CMD_SUCCESS;
  10562. }
  10563. /// Retrieves various information about the specified guardian.
  10564. ///
  10565. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  10566. /// type: 0 - whether it is deployed or not
  10567. /// 1 - maximum hp
  10568. /// 2 - current hp
  10569. ///
  10570. BUILDIN_FUNC(guardianinfo)
  10571. {
  10572. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10573. int id = script_getnum(st,3);
  10574. int type = script_getnum(st,4);
  10575. struct guild_castle* gc = guild_mapname2gc(mapname);
  10576. struct mob_data* gd;
  10577. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  10578. {
  10579. script_pushint(st,-1);
  10580. return 0;
  10581. }
  10582. if( type == 0 )
  10583. script_pushint(st, gc->guardian[id].visible);
  10584. else
  10585. if( !gc->guardian[id].visible )
  10586. script_pushint(st,-1);
  10587. else
  10588. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  10589. script_pushint(st,-1);
  10590. else
  10591. {
  10592. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  10593. else if( type == 2 ) script_pushint(st,gd->status.hp);
  10594. else
  10595. script_pushint(st,-1);
  10596. }
  10597. return SCRIPT_CMD_SUCCESS;
  10598. }
  10599. /*==========================================
  10600. * Get the item name by item_id or null
  10601. *------------------------------------------*/
  10602. BUILDIN_FUNC(getitemname)
  10603. {
  10604. int item_id=0;
  10605. struct item_data *i_data;
  10606. char *item_name;
  10607. struct script_data *data;
  10608. data=script_getdata(st,2);
  10609. get_val(st,data);
  10610. if( data_isstring(data) ){
  10611. const char *name=conv_str(st,data);
  10612. struct item_data *item_data = itemdb_searchname(name);
  10613. if( item_data )
  10614. item_id=item_data->nameid;
  10615. }else
  10616. item_id=conv_num(st,data);
  10617. i_data = itemdb_exists(item_id);
  10618. if (i_data == NULL)
  10619. {
  10620. script_pushconststr(st,"null");
  10621. return 0;
  10622. }
  10623. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  10624. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  10625. script_pushstr(st,item_name);
  10626. return SCRIPT_CMD_SUCCESS;
  10627. }
  10628. /*==========================================
  10629. * Returns number of slots an item has. [Skotlex]
  10630. *------------------------------------------*/
  10631. BUILDIN_FUNC(getitemslots)
  10632. {
  10633. int item_id;
  10634. struct item_data *i_data;
  10635. item_id=script_getnum(st,2);
  10636. i_data = itemdb_exists(item_id);
  10637. if (i_data)
  10638. script_pushint(st,i_data->slot);
  10639. else
  10640. script_pushint(st,-1);
  10641. return SCRIPT_CMD_SUCCESS;
  10642. }
  10643. // TODO: add matk here if needed/once we get rid of RENEWAL
  10644. /*==========================================
  10645. * Returns some values of an item [Lupus]
  10646. * Price, Weight, etc...
  10647. getiteminfo(itemID,n), where n
  10648. 0 value_buy;
  10649. 1 value_sell;
  10650. 2 type;
  10651. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10652. if = 0, then monsters don't drop it at all (rare or a quest item)
  10653. if = -1, then this item is sold in NPC shops only
  10654. 4 sex;
  10655. 5 equip;
  10656. 6 weight;
  10657. 7 atk;
  10658. 8 def;
  10659. 9 range;
  10660. 10 slot;
  10661. 11 look;
  10662. 12 elv;
  10663. 13 wlv;
  10664. 14 view id
  10665. *------------------------------------------*/
  10666. BUILDIN_FUNC(getiteminfo)
  10667. {
  10668. int item_id,n;
  10669. int *item_arr;
  10670. struct item_data *i_data;
  10671. item_id = script_getnum(st,2);
  10672. n = script_getnum(st,3);
  10673. i_data = itemdb_exists(item_id);
  10674. if (i_data && n>=0 && n<=14) {
  10675. item_arr = (int*)&i_data->value_buy;
  10676. script_pushint(st,item_arr[n]);
  10677. } else
  10678. script_pushint(st,-1);
  10679. return SCRIPT_CMD_SUCCESS;
  10680. }
  10681. /*==========================================
  10682. * Set some values of an item [Lupus]
  10683. * Price, Weight, etc...
  10684. setiteminfo(itemID,n,Value), where n
  10685. 0 value_buy;
  10686. 1 value_sell;
  10687. 2 type;
  10688. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10689. if = 0, then monsters don't drop it at all (rare or a quest item)
  10690. if = -1, then this item is sold in NPC shops only
  10691. 4 sex;
  10692. 5 equip;
  10693. 6 weight;
  10694. 7 atk;
  10695. 8 def;
  10696. 9 range;
  10697. 10 slot;
  10698. 11 look;
  10699. 12 elv;
  10700. 13 wlv;
  10701. 14 view id
  10702. * Returns Value or -1 if the wrong field's been set
  10703. *------------------------------------------*/
  10704. BUILDIN_FUNC(setiteminfo)
  10705. {
  10706. int item_id,n,value;
  10707. int *item_arr;
  10708. struct item_data *i_data;
  10709. item_id = script_getnum(st,2);
  10710. n = script_getnum(st,3);
  10711. value = script_getnum(st,4);
  10712. i_data = itemdb_exists(item_id);
  10713. if (i_data && n>=0 && n<=14) {
  10714. item_arr = (int*)&i_data->value_buy;
  10715. item_arr[n] = value;
  10716. script_pushint(st,value);
  10717. } else
  10718. script_pushint(st,-1);
  10719. return SCRIPT_CMD_SUCCESS;
  10720. }
  10721. /*==========================================
  10722. * Returns value from equipped item slot n [Lupus]
  10723. getequipcardid(num,slot)
  10724. where
  10725. num = eqip position slot
  10726. slot = 0,1,2,3 (Card Slot N)
  10727. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  10728. it's useful when you want to check item cards or if it's signed
  10729. Useful for such quests as "Sign this refined item with players name" etc
  10730. Hat[0] +4 -> Player's Hat[0] +4
  10731. *------------------------------------------*/
  10732. BUILDIN_FUNC(getequipcardid)
  10733. {
  10734. int i=-1,num,slot;
  10735. TBL_PC *sd;
  10736. num=script_getnum(st,2);
  10737. slot=script_getnum(st,3);
  10738. sd=script_rid2sd(st);
  10739. if (num > 0 && num <= ARRAYLENGTH(equip))
  10740. i=pc_checkequip(sd,equip[num-1]);
  10741. if(i >= 0 && slot>=0 && slot<4)
  10742. script_pushint(st,sd->status.inventory[i].card[slot]);
  10743. else
  10744. script_pushint(st,0);
  10745. return SCRIPT_CMD_SUCCESS;
  10746. }
  10747. /*==========================================
  10748. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  10749. *------------------------------------------*/
  10750. BUILDIN_FUNC(petskillbonus)
  10751. {
  10752. struct pet_data *pd;
  10753. TBL_PC *sd=script_rid2sd(st);
  10754. if(sd==NULL || sd->pd==NULL)
  10755. return 0;
  10756. pd=sd->pd;
  10757. if (pd->bonus)
  10758. { //Clear previous bonus
  10759. if (pd->bonus->timer != INVALID_TIMER)
  10760. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  10761. } else //init
  10762. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  10763. pd->bonus->type=script_getnum(st,2);
  10764. pd->bonus->val=script_getnum(st,3);
  10765. pd->bonus->duration=script_getnum(st,4);
  10766. pd->bonus->delay=script_getnum(st,5);
  10767. if (pd->state.skillbonus == 1)
  10768. pd->state.skillbonus=0; // waiting state
  10769. // wait for timer to start
  10770. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10771. pd->bonus->timer = INVALID_TIMER;
  10772. else
  10773. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  10774. return SCRIPT_CMD_SUCCESS;
  10775. }
  10776. /*==========================================
  10777. * pet looting [Valaris] //Rewritten by [Skotlex]
  10778. *------------------------------------------*/
  10779. BUILDIN_FUNC(petloot)
  10780. {
  10781. int max;
  10782. struct pet_data *pd;
  10783. TBL_PC *sd=script_rid2sd(st);
  10784. if(sd==NULL || sd->pd==NULL)
  10785. return 0;
  10786. max=script_getnum(st,2);
  10787. if(max < 1)
  10788. max = 1; //Let'em loot at least 1 item.
  10789. else if (max > MAX_PETLOOT_SIZE)
  10790. max = MAX_PETLOOT_SIZE;
  10791. pd = sd->pd;
  10792. if (pd->loot != NULL)
  10793. { //Release whatever was there already and reallocate memory
  10794. pet_lootitem_drop(pd, pd->master);
  10795. aFree(pd->loot->item);
  10796. }
  10797. else
  10798. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  10799. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  10800. pd->loot->max=max;
  10801. pd->loot->count = 0;
  10802. pd->loot->weight = 0;
  10803. return SCRIPT_CMD_SUCCESS;
  10804. }
  10805. /*==========================================
  10806. * Set arrays with info of all sd inventory :
  10807. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  10808. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  10809. * @inventorylist_card(0..3), @inventorylist_expire
  10810. * @inventorylist_count = scalar
  10811. *------------------------------------------*/
  10812. BUILDIN_FUNC(getinventorylist)
  10813. {
  10814. TBL_PC *sd=script_rid2sd(st);
  10815. char card_var[NAME_LENGTH];
  10816. int i,j=0,k;
  10817. if(!sd) return 0;
  10818. for(i=0;i<MAX_INVENTORY;i++){
  10819. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  10820. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  10821. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  10822. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  10823. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  10824. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  10825. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  10826. for (k = 0; k < MAX_SLOTS; k++)
  10827. {
  10828. sprintf(card_var, "@inventorylist_card%d",k+1);
  10829. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  10830. }
  10831. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  10832. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  10833. j++;
  10834. }
  10835. }
  10836. pc_setreg(sd,add_str("@inventorylist_count"),j);
  10837. return SCRIPT_CMD_SUCCESS;
  10838. }
  10839. BUILDIN_FUNC(getskilllist)
  10840. {
  10841. TBL_PC *sd=script_rid2sd(st);
  10842. int i,j=0;
  10843. if(!sd) return 0;
  10844. for(i=0;i<MAX_SKILL;i++){
  10845. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  10846. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  10847. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  10848. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  10849. j++;
  10850. }
  10851. }
  10852. pc_setreg(sd,add_str("@skilllist_count"),j);
  10853. return SCRIPT_CMD_SUCCESS;
  10854. }
  10855. BUILDIN_FUNC(clearitem)
  10856. {
  10857. TBL_PC *sd=script_rid2sd(st);
  10858. int i;
  10859. if(sd==NULL) return 0;
  10860. for (i=0; i<MAX_INVENTORY; i++) {
  10861. if (sd->status.inventory[i].amount) {
  10862. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  10863. }
  10864. }
  10865. return SCRIPT_CMD_SUCCESS;
  10866. }
  10867. /*==========================================
  10868. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  10869. *------------------------------------------*/
  10870. BUILDIN_FUNC(disguise)
  10871. {
  10872. int id;
  10873. TBL_PC* sd = script_rid2sd(st);
  10874. if (sd == NULL) return 0;
  10875. id = script_getnum(st,2);
  10876. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  10877. pc_disguise(sd, id);
  10878. script_pushint(st,id);
  10879. } else
  10880. script_pushint(st,0);
  10881. return SCRIPT_CMD_SUCCESS;
  10882. }
  10883. /*==========================================
  10884. * Undisguise Player (returns 1 if success, 0 on fail)
  10885. *------------------------------------------*/
  10886. BUILDIN_FUNC(undisguise)
  10887. {
  10888. TBL_PC* sd = script_rid2sd(st);
  10889. if (sd == NULL) return 0;
  10890. if (sd->disguise) {
  10891. pc_disguise(sd, 0);
  10892. script_pushint(st,0);
  10893. } else {
  10894. script_pushint(st,1);
  10895. }
  10896. return SCRIPT_CMD_SUCCESS;
  10897. }
  10898. /*==========================================
  10899. * Transform a bl to another _class,
  10900. * @type unused
  10901. *------------------------------------------*/
  10902. BUILDIN_FUNC(classchange)
  10903. {
  10904. int _class,type;
  10905. struct block_list *bl=map_id2bl(st->oid);
  10906. if(bl==NULL) return 0;
  10907. _class=script_getnum(st,2);
  10908. type=script_getnum(st,3);
  10909. clif_class_change(bl,_class,type);
  10910. return SCRIPT_CMD_SUCCESS;
  10911. }
  10912. /*==========================================
  10913. * Display an effect
  10914. *------------------------------------------*/
  10915. BUILDIN_FUNC(misceffect)
  10916. {
  10917. int type;
  10918. type=script_getnum(st,2);
  10919. if(st->oid && st->oid != fake_nd->bl.id) {
  10920. struct block_list *bl = map_id2bl(st->oid);
  10921. if (bl)
  10922. clif_specialeffect(bl,type,AREA);
  10923. } else{
  10924. TBL_PC *sd=script_rid2sd(st);
  10925. if(sd)
  10926. clif_specialeffect(&sd->bl,type,AREA);
  10927. }
  10928. return SCRIPT_CMD_SUCCESS;
  10929. }
  10930. /*==========================================
  10931. * Play a BGM on a single client [Rikter/Yommy]
  10932. *------------------------------------------*/
  10933. BUILDIN_FUNC(playBGM)
  10934. {
  10935. const char* name;
  10936. struct map_session_data* sd;
  10937. if( ( sd = script_rid2sd(st) ) != NULL ) {
  10938. name = script_getstr(st,2);
  10939. clif_playBGM(sd, name);
  10940. }
  10941. return SCRIPT_CMD_SUCCESS;
  10942. }
  10943. static int playBGM_sub(struct block_list* bl,va_list ap)
  10944. {
  10945. const char* name = va_arg(ap,const char*);
  10946. clif_playBGM(BL_CAST(BL_PC, bl), name);
  10947. return 0;
  10948. }
  10949. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  10950. {
  10951. const char* name = va_arg(args, const char*);
  10952. clif_playBGM(sd, name);
  10953. return 0;
  10954. }
  10955. /*==========================================
  10956. * Play a BGM on multiple client [Rikter/Yommy]
  10957. *------------------------------------------*/
  10958. BUILDIN_FUNC(playBGMall)
  10959. {
  10960. const char* name;
  10961. name = script_getstr(st,2);
  10962. if( script_hasdata(st,7) ) {// specified part of map
  10963. const char* map = script_getstr(st,3);
  10964. int x0 = script_getnum(st,4);
  10965. int y0 = script_getnum(st,5);
  10966. int x1 = script_getnum(st,6);
  10967. int y1 = script_getnum(st,7);
  10968. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  10969. }
  10970. else if( script_hasdata(st,3) ) {// entire map
  10971. const char* map = script_getstr(st,3);
  10972. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  10973. }
  10974. else {// entire server
  10975. map_foreachpc(&playBGM_foreachpc_sub, name);
  10976. }
  10977. return SCRIPT_CMD_SUCCESS;
  10978. }
  10979. /*==========================================
  10980. * Play a .wav sound for sd
  10981. *------------------------------------------*/
  10982. BUILDIN_FUNC(soundeffect)
  10983. {
  10984. TBL_PC* sd = script_rid2sd(st);
  10985. const char* name = script_getstr(st,2);
  10986. int type = script_getnum(st,3);
  10987. if(sd) {
  10988. clif_soundeffect(sd,&sd->bl,name,type);
  10989. }
  10990. return SCRIPT_CMD_SUCCESS;
  10991. }
  10992. int soundeffect_sub(struct block_list* bl,va_list ap)
  10993. {
  10994. char* name = va_arg(ap,char*);
  10995. int type = va_arg(ap,int);
  10996. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  10997. return 0;
  10998. }
  10999. /*==========================================
  11000. * Play a sound effect (.wav) on multiple clients
  11001. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11002. *------------------------------------------*/
  11003. BUILDIN_FUNC(soundeffectall)
  11004. {
  11005. struct block_list* bl;
  11006. const char* name;
  11007. int type;
  11008. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11009. if (!bl)
  11010. return 0;
  11011. name = script_getstr(st,2);
  11012. type = script_getnum(st,3);
  11013. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11014. if(!script_hasdata(st,4))
  11015. { // area around
  11016. clif_soundeffectall(bl, name, type, AREA);
  11017. }
  11018. else
  11019. if(!script_hasdata(st,5))
  11020. { // entire map
  11021. const char* map = script_getstr(st,4);
  11022. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  11023. }
  11024. else
  11025. if(script_hasdata(st,8))
  11026. { // specified part of map
  11027. const char* map = script_getstr(st,4);
  11028. int x0 = script_getnum(st,5);
  11029. int y0 = script_getnum(st,6);
  11030. int x1 = script_getnum(st,7);
  11031. int y1 = script_getnum(st,8);
  11032. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  11033. }
  11034. else
  11035. {
  11036. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11037. }
  11038. return SCRIPT_CMD_SUCCESS;
  11039. }
  11040. /*==========================================
  11041. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11042. *------------------------------------------*/
  11043. BUILDIN_FUNC(petrecovery)
  11044. {
  11045. struct pet_data *pd;
  11046. TBL_PC *sd=script_rid2sd(st);
  11047. if(sd==NULL || sd->pd==NULL)
  11048. return 0;
  11049. pd=sd->pd;
  11050. if (pd->recovery)
  11051. { //Halt previous bonus
  11052. if (pd->recovery->timer != INVALID_TIMER)
  11053. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11054. } else //Init
  11055. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11056. pd->recovery->type = (sc_type)script_getnum(st,2);
  11057. pd->recovery->delay = script_getnum(st,3);
  11058. pd->recovery->timer = INVALID_TIMER;
  11059. return SCRIPT_CMD_SUCCESS;
  11060. }
  11061. /*==========================================
  11062. * pet healing [Valaris] //Rewritten by [Skotlex]
  11063. *------------------------------------------*/
  11064. BUILDIN_FUNC(petheal)
  11065. {
  11066. struct pet_data *pd;
  11067. TBL_PC *sd=script_rid2sd(st);
  11068. if(sd==NULL || sd->pd==NULL)
  11069. return 0;
  11070. pd=sd->pd;
  11071. if (pd->s_skill)
  11072. { //Clear previous skill
  11073. if (pd->s_skill->timer != INVALID_TIMER)
  11074. {
  11075. if (pd->s_skill->id)
  11076. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11077. else
  11078. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11079. }
  11080. } else //init memory
  11081. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11082. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11083. //Use the lv as the amount to heal
  11084. pd->s_skill->lv=script_getnum(st,2);
  11085. pd->s_skill->delay=script_getnum(st,3);
  11086. pd->s_skill->hp=script_getnum(st,4);
  11087. pd->s_skill->sp=script_getnum(st,5);
  11088. //Use delay as initial offset to avoid skill/heal exploits
  11089. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11090. pd->s_skill->timer = INVALID_TIMER;
  11091. else
  11092. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11093. return SCRIPT_CMD_SUCCESS;
  11094. }
  11095. /*==========================================
  11096. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11097. *------------------------------------------*/
  11098. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11099. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11100. BUILDIN_FUNC(petskillattack)
  11101. {
  11102. struct pet_data *pd;
  11103. struct script_data *data;
  11104. TBL_PC *sd=script_rid2sd(st);
  11105. if(sd==NULL || sd->pd==NULL)
  11106. return 0;
  11107. pd=sd->pd;
  11108. if (pd->a_skill == NULL)
  11109. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11110. data = script_getdata(st, 2);
  11111. get_val(st, data); // Convert into value in case of a variable
  11112. pd->a_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11113. pd->a_skill->lv=script_getnum(st,3);
  11114. pd->a_skill->div_ = 0;
  11115. pd->a_skill->rate=script_getnum(st,4);
  11116. pd->a_skill->bonusrate=script_getnum(st,5);
  11117. return SCRIPT_CMD_SUCCESS;
  11118. }
  11119. /*==========================================
  11120. * pet attack skills [Valaris]
  11121. *------------------------------------------*/
  11122. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  11123. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  11124. BUILDIN_FUNC(petskillattack2)
  11125. {
  11126. struct pet_data *pd;
  11127. struct script_data *data;
  11128. TBL_PC *sd=script_rid2sd(st);
  11129. if(sd==NULL || sd->pd==NULL)
  11130. return 0;
  11131. pd=sd->pd;
  11132. if (pd->a_skill == NULL)
  11133. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11134. data = script_getdata(st, 2);
  11135. get_val(st, data); // Convert into value in case of a variable
  11136. pd->a_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11137. pd->a_skill->lv=script_getnum(st,3);
  11138. pd->a_skill->div_ = script_getnum(st,4);
  11139. pd->a_skill->rate=script_getnum(st,5);
  11140. pd->a_skill->bonusrate=script_getnum(st,6);
  11141. return SCRIPT_CMD_SUCCESS;
  11142. }
  11143. /*==========================================
  11144. * pet support skills [Skotlex]
  11145. *------------------------------------------*/
  11146. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  11147. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  11148. BUILDIN_FUNC(petskillsupport)
  11149. {
  11150. struct pet_data *pd;
  11151. struct script_data *data;
  11152. TBL_PC *sd=script_rid2sd(st);
  11153. if(sd==NULL || sd->pd==NULL)
  11154. return 0;
  11155. pd=sd->pd;
  11156. if (pd->s_skill)
  11157. { //Clear previous skill
  11158. if (pd->s_skill->timer != INVALID_TIMER)
  11159. {
  11160. if (pd->s_skill->id)
  11161. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11162. else
  11163. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11164. }
  11165. } else //init memory
  11166. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11167. data = script_getdata(st, 2);
  11168. get_val(st, data); // Convert into value in case of a variable
  11169. pd->s_skill->id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11170. pd->s_skill->lv=script_getnum(st,3);
  11171. pd->s_skill->delay=script_getnum(st,4);
  11172. pd->s_skill->hp=script_getnum(st,5);
  11173. pd->s_skill->sp=script_getnum(st,6);
  11174. //Use delay as initial offset to avoid skill/heal exploits
  11175. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11176. pd->s_skill->timer = INVALID_TIMER;
  11177. else
  11178. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  11179. return SCRIPT_CMD_SUCCESS;
  11180. }
  11181. /*==========================================
  11182. * Scripted skill effects [Celest]
  11183. *------------------------------------------*/
  11184. /// skilleffect <skill id>,<level>
  11185. /// skilleffect "<skill name>",<level>
  11186. BUILDIN_FUNC(skilleffect)
  11187. {
  11188. TBL_PC *sd;
  11189. uint16 skill_id, skill_lv;
  11190. struct script_data *data = script_getdata(st, 2);
  11191. get_val(st, data); // Convert into value in case of a variable
  11192. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11193. skill_lv=script_getnum(st,3);
  11194. sd=script_rid2sd(st);
  11195. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  11196. return SCRIPT_CMD_SUCCESS;
  11197. }
  11198. /*==========================================
  11199. * NPC skill effects [Valaris]
  11200. *------------------------------------------*/
  11201. /// npcskilleffect <skill id>,<level>,<x>,<y>
  11202. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  11203. BUILDIN_FUNC(npcskilleffect)
  11204. {
  11205. struct block_list *bl= map_id2bl(st->oid);
  11206. uint16 skill_id, skill_lv;
  11207. int x, y;
  11208. struct script_data *data = script_getdata(st, 2);
  11209. get_val(st, data); // Convert into value in case of a variable
  11210. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11211. skill_lv=script_getnum(st,3);
  11212. x=script_getnum(st,4);
  11213. y=script_getnum(st,5);
  11214. if (bl)
  11215. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  11216. return SCRIPT_CMD_SUCCESS;
  11217. }
  11218. /*==========================================
  11219. * Special effects [Valaris]
  11220. *------------------------------------------*/
  11221. BUILDIN_FUNC(specialeffect)
  11222. {
  11223. struct block_list *bl=map_id2bl(st->oid);
  11224. int type = script_getnum(st,2);
  11225. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11226. if(bl==NULL)
  11227. return 0;
  11228. if( script_hasdata(st,4) )
  11229. {
  11230. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  11231. if(nd)
  11232. clif_specialeffect(&nd->bl, type, target);
  11233. }
  11234. else
  11235. {
  11236. if (target == SELF) {
  11237. TBL_PC *sd=script_rid2sd(st);
  11238. if (sd)
  11239. clif_specialeffect_single(bl,type,sd->fd);
  11240. } else {
  11241. clif_specialeffect(bl, type, target);
  11242. }
  11243. }
  11244. return SCRIPT_CMD_SUCCESS;
  11245. }
  11246. BUILDIN_FUNC(specialeffect2)
  11247. {
  11248. TBL_PC *sd=script_rid2sd(st);
  11249. int type = script_getnum(st,2);
  11250. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11251. if( script_hasdata(st,4) )
  11252. sd = map_nick2sd(script_getstr(st,4));
  11253. if (sd)
  11254. clif_specialeffect(&sd->bl, type, target);
  11255. return SCRIPT_CMD_SUCCESS;
  11256. }
  11257. /*==========================================
  11258. * Nude [Valaris]
  11259. *------------------------------------------*/
  11260. BUILDIN_FUNC(nude)
  11261. {
  11262. TBL_PC *sd = script_rid2sd(st);
  11263. int i, calcflag = 0;
  11264. if( sd == NULL )
  11265. return 0;
  11266. for( i = 0 ; i < EQI_MAX; i++ ) {
  11267. if( sd->equip_index[ i ] >= 0 ) {
  11268. if( !calcflag )
  11269. calcflag = 1;
  11270. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  11271. }
  11272. }
  11273. if( calcflag )
  11274. status_calc_pc(sd,0);
  11275. return SCRIPT_CMD_SUCCESS;
  11276. }
  11277. int atcommand_sub(struct script_state* st,int type){
  11278. TBL_PC dummy_sd;
  11279. TBL_PC* sd;
  11280. int fd;
  11281. const char* cmd;
  11282. cmd = script_getstr(st,2);
  11283. if (st->rid) {
  11284. sd = script_rid2sd(st);
  11285. fd = sd->fd;
  11286. } else { //Use a dummy character.
  11287. sd = &dummy_sd;
  11288. fd = 0;
  11289. memset(&dummy_sd, 0, sizeof(TBL_PC));
  11290. if (st->oid)
  11291. {
  11292. struct block_list* bl = map_id2bl(st->oid);
  11293. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  11294. if (bl->type == BL_NPC)
  11295. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  11296. }
  11297. }
  11298. if (!is_atcommand(fd, sd, cmd, type)) {
  11299. ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd);
  11300. script_reportsrc(st);
  11301. return 1;
  11302. }
  11303. return SCRIPT_CMD_SUCCESS;
  11304. }
  11305. /*==========================================
  11306. * gmcommand [MouseJstr]
  11307. *------------------------------------------*/
  11308. BUILDIN_FUNC(atcommand) {
  11309. return atcommand_sub(st,0);
  11310. }
  11311. /*==========================================
  11312. * Displays a message for the player only (like system messages like "you got an apple" )
  11313. *------------------------------------------*/
  11314. BUILDIN_FUNC(dispbottom)
  11315. {
  11316. TBL_PC *sd=script_rid2sd(st);
  11317. const char *message;
  11318. message=script_getstr(st,2);
  11319. if(sd)
  11320. clif_disp_onlyself(sd,message,(int)strlen(message));
  11321. return SCRIPT_CMD_SUCCESS;
  11322. }
  11323. /*===================================
  11324. * Heal portion of recovery command
  11325. *-----------------------------------*/
  11326. int recovery_sub(struct map_session_data* sd, int revive)
  11327. {
  11328. if(revive&(1|4) && pc_isdead(sd)) {
  11329. status_revive(&sd->bl, 100, 100);
  11330. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  11331. clif_specialeffect(&sd->bl, 77, AREA);
  11332. } else if(revive&(1|2) && !pc_isdead(sd)) {
  11333. status_percent_heal(&sd->bl, 100, 100);
  11334. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  11335. }
  11336. return SCRIPT_CMD_SUCCESS;
  11337. }
  11338. /*=========================================================================
  11339. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  11340. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  11341. * <type> determines <option>:
  11342. * 0 : char_id
  11343. * 1 : party_id
  11344. * 2 : guild_id
  11345. * 3 : map_name
  11346. * 4 : all characters
  11347. * <revive_flag>:
  11348. * 1 : Revive and heal all players (default)
  11349. * 2 : Heal living players only
  11350. * 4 : Revive dead players only
  11351. * <map name>:
  11352. * for types 1-2 : map_name (null = all maps)
  11353. *-------------------------------------------------------------------------*/
  11354. BUILDIN_FUNC(recovery)
  11355. {
  11356. TBL_PC *sd;
  11357. int map = 0, type = 0, revive = 1;
  11358. type = script_getnum(st,2);
  11359. if(script_hasdata(st,4))
  11360. revive = script_getnum(st,4);
  11361. switch(type) {
  11362. case 0:
  11363. if(script_hasdata(st,3))
  11364. sd=map_charid2sd(script_getnum(st,3));
  11365. else if((sd = script_rid2sd(st)) == NULL)
  11366. return 0; //If we don't have sd by now, bail out
  11367. recovery_sub(sd, revive);
  11368. break;
  11369. case 1:
  11370. {
  11371. struct party_data* p;
  11372. struct map_session_data* pl_sd;
  11373. //When no party given, we use invoker party
  11374. int p_id = 0, i;
  11375. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11376. map = map_mapname2mapid(script_getstr(st,5));
  11377. if(map < 1) { //But we'll check anyways
  11378. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11379. return 1;
  11380. }
  11381. }
  11382. if(script_hasdata(st,3))
  11383. p_id = script_getnum(st,3);
  11384. else if((sd = script_rid2sd(st)))
  11385. p_id = sd->status.party_id;
  11386. p = party_search(p_id);
  11387. if(p == NULL)
  11388. return 0;
  11389. for (i = 0; i < MAX_PARTY; i++) {
  11390. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  11391. || (map && pl_sd->bl.m != map))
  11392. continue;
  11393. recovery_sub(pl_sd, revive);
  11394. }
  11395. break;
  11396. }
  11397. case 2:
  11398. {
  11399. struct guild* g;
  11400. struct map_session_data* pl_sd;
  11401. //When no guild given, we use invoker guild
  11402. int g_id = 0, i;
  11403. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11404. map = map_mapname2mapid(script_getstr(st,5));
  11405. if(map < 1) { //But we'll check anyways
  11406. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11407. return 1;
  11408. }
  11409. }
  11410. if(script_hasdata(st,3))
  11411. g_id = script_getnum(st,3);
  11412. else if((sd = script_rid2sd(st)))
  11413. g_id = sd->status.guild_id;
  11414. g = guild_search(g_id);
  11415. if(g == NULL)
  11416. return 0;
  11417. for (i = 0; i < MAX_GUILD; i++) {
  11418. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  11419. || (map && pl_sd->bl.m != map))
  11420. continue;
  11421. recovery_sub(pl_sd, revive);
  11422. }
  11423. break;
  11424. }
  11425. case 3:
  11426. if(script_hasdata(st,3))
  11427. map = map_mapname2mapid(script_getstr(st,3));
  11428. else if((sd = script_rid2sd(st)))
  11429. map = sd->bl.m;
  11430. if(map < 1)
  11431. return 1; //No sd and no map given - return
  11432. case 4:
  11433. {
  11434. struct s_mapiterator *iter;
  11435. struct script_data *data = script_getdata(st, 3);
  11436. get_val(st, data); // Convert into value in case of a variable
  11437. if(script_hasdata(st,3) && !data_isstring(data))
  11438. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  11439. iter = mapit_getallusers();
  11440. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11441. if(type == 3 && sd->bl.m != map)
  11442. continue;
  11443. recovery_sub(sd, revive);
  11444. }
  11445. mapit_free(iter);
  11446. break;
  11447. }
  11448. default:
  11449. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  11450. script_pushint(st,-1);
  11451. return 1;
  11452. }
  11453. script_pushint(st,1); //Successfully executed without errors
  11454. return SCRIPT_CMD_SUCCESS;
  11455. }
  11456. /*==========================================
  11457. * Get your pet info: getpetinfo(n)
  11458. * n -> 0:pet_id 1:pet_class 2:pet_name
  11459. * 3:friendly 4:hungry, 5: rename flag.6:level
  11460. *------------------------------------------*/
  11461. BUILDIN_FUNC(getpetinfo)
  11462. {
  11463. TBL_PC *sd=script_rid2sd(st);
  11464. TBL_PET *pd;
  11465. int type=script_getnum(st,2);
  11466. if(!sd || !sd->pd) {
  11467. if (type == 2)
  11468. script_pushconststr(st,"null");
  11469. else
  11470. script_pushint(st,0);
  11471. return 0;
  11472. }
  11473. pd = sd->pd;
  11474. switch(type){
  11475. case 0: script_pushint(st,pd->pet.pet_id); break;
  11476. case 1: script_pushint(st,pd->pet.class_); break;
  11477. case 2: script_pushstrcopy(st,pd->pet.name); break;
  11478. case 3: script_pushint(st,pd->pet.intimate); break;
  11479. case 4: script_pushint(st,pd->pet.hungry); break;
  11480. case 5: script_pushint(st,pd->pet.rename_flag); break;
  11481. case 6: script_pushint(st,(int)pd->pet.level); break;
  11482. default:
  11483. script_pushint(st,0);
  11484. break;
  11485. }
  11486. return SCRIPT_CMD_SUCCESS;
  11487. }
  11488. /*==========================================
  11489. * Get your homunculus info: gethominfo(n)
  11490. * n -> 0:hom_id 1:class 2:name
  11491. * 3:friendly 4:hungry, 5: rename flag.
  11492. * 6: level
  11493. *------------------------------------------*/
  11494. BUILDIN_FUNC(gethominfo)
  11495. {
  11496. TBL_PC *sd=script_rid2sd(st);
  11497. TBL_HOM *hd;
  11498. int type=script_getnum(st,2);
  11499. hd = sd?sd->hd:NULL;
  11500. if(!hd) {
  11501. if (type == 2)
  11502. script_pushconststr(st,"null");
  11503. else
  11504. script_pushint(st,0);
  11505. return 0;
  11506. }
  11507. switch(type){
  11508. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  11509. case 1: script_pushint(st,hd->homunculus.class_); break;
  11510. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  11511. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  11512. case 4: script_pushint(st,hd->homunculus.hunger); break;
  11513. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  11514. case 6: script_pushint(st,hd->homunculus.level); break;
  11515. default:
  11516. script_pushint(st,0);
  11517. break;
  11518. }
  11519. return SCRIPT_CMD_SUCCESS;
  11520. }
  11521. /// Retrieves information about character's mercenary
  11522. /// getmercinfo <type>[,<char id>];
  11523. BUILDIN_FUNC(getmercinfo)
  11524. {
  11525. int type, char_id;
  11526. struct map_session_data* sd;
  11527. struct mercenary_data* md;
  11528. type = script_getnum(st,2);
  11529. if( script_hasdata(st,3) )
  11530. {
  11531. char_id = script_getnum(st,3);
  11532. if( ( sd = map_charid2sd(char_id) ) == NULL )
  11533. {
  11534. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  11535. script_pushnil(st);
  11536. return 1;
  11537. }
  11538. }
  11539. else
  11540. {
  11541. if( ( sd = script_rid2sd(st) ) == NULL )
  11542. {
  11543. script_pushnil(st);
  11544. return 0;
  11545. }
  11546. }
  11547. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  11548. switch( type )
  11549. {
  11550. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  11551. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  11552. case 2:
  11553. if( md )
  11554. script_pushstrcopy(st,md->db->name);
  11555. else
  11556. script_pushconststr(st,"");
  11557. break;
  11558. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  11559. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  11560. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  11561. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  11562. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  11563. default:
  11564. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  11565. script_pushnil(st);
  11566. return 1;
  11567. }
  11568. return SCRIPT_CMD_SUCCESS;
  11569. }
  11570. /*==========================================
  11571. * Shows wether your inventory(and equips) contain
  11572. selected card or not.
  11573. checkequipedcard(4001);
  11574. *------------------------------------------*/
  11575. BUILDIN_FUNC(checkequipedcard)
  11576. {
  11577. TBL_PC *sd=script_rid2sd(st);
  11578. if(sd){
  11579. int n,i,c=0;
  11580. c=script_getnum(st,2);
  11581. for(i=0;i<MAX_INVENTORY;i++){
  11582. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  11583. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  11584. continue;
  11585. for(n=0;n<sd->inventory_data[i]->slot;n++){
  11586. if(sd->status.inventory[i].card[n]==c){
  11587. script_pushint(st,1);
  11588. return 0;
  11589. }
  11590. }
  11591. }
  11592. }
  11593. }
  11594. script_pushint(st,0);
  11595. return SCRIPT_CMD_SUCCESS;
  11596. }
  11597. BUILDIN_FUNC(jump_zero)
  11598. {
  11599. int sel;
  11600. sel=script_getnum(st,2);
  11601. if(!sel) {
  11602. int pos;
  11603. if( !data_islabel(script_getdata(st,3)) ){
  11604. ShowError("script: jump_zero: not label !\n");
  11605. st->state=END;
  11606. return 1;
  11607. }
  11608. pos=script_getnum(st,3);
  11609. st->pos=pos;
  11610. st->state=GOTO;
  11611. }
  11612. return SCRIPT_CMD_SUCCESS;
  11613. }
  11614. /*==========================================
  11615. * movenpc [MouseJstr]
  11616. *------------------------------------------*/
  11617. BUILDIN_FUNC(movenpc)
  11618. {
  11619. TBL_NPC *nd = NULL;
  11620. const char *npc;
  11621. int x,y;
  11622. npc = script_getstr(st,2);
  11623. x = script_getnum(st,3);
  11624. y = script_getnum(st,4);
  11625. if ((nd = npc_name2id(npc)) == NULL)
  11626. return -1;
  11627. if (script_hasdata(st,5))
  11628. nd->ud.dir = script_getnum(st,5) % 8;
  11629. npc_movenpc(nd, x, y);
  11630. return SCRIPT_CMD_SUCCESS;
  11631. }
  11632. /*==========================================
  11633. * message [MouseJstr]
  11634. *------------------------------------------*/
  11635. BUILDIN_FUNC(message)
  11636. {
  11637. const char *msg,*player;
  11638. TBL_PC *pl_sd = NULL;
  11639. player = script_getstr(st,2);
  11640. msg = script_getstr(st,3);
  11641. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  11642. return 0;
  11643. clif_displaymessage(pl_sd->fd, msg);
  11644. return SCRIPT_CMD_SUCCESS;
  11645. }
  11646. /*==========================================
  11647. * npctalk (sends message to surrounding area)
  11648. *------------------------------------------*/
  11649. BUILDIN_FUNC(npctalk)
  11650. {
  11651. const char* str;
  11652. char name[NAME_LENGTH], message[256];
  11653. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  11654. str = script_getstr(st,2);
  11655. if(nd)
  11656. {
  11657. safestrncpy(name, nd->name, sizeof(name));
  11658. strtok(name, "#"); // discard extra name identifier if present
  11659. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  11660. clif_disp_overhead(&nd->bl, message);
  11661. }
  11662. return SCRIPT_CMD_SUCCESS;
  11663. }
  11664. // change npc walkspeed [Valaris]
  11665. BUILDIN_FUNC(npcspeed)
  11666. {
  11667. struct npc_data* nd;
  11668. int speed;
  11669. speed = script_getnum(st,2);
  11670. nd =(struct npc_data *)map_id2bl(st->oid);
  11671. if( nd ) {
  11672. nd->speed = speed;
  11673. nd->ud.state.speed_changed = 1;
  11674. }
  11675. return SCRIPT_CMD_SUCCESS;
  11676. }
  11677. // make an npc walk to a position [Valaris]
  11678. BUILDIN_FUNC(npcwalkto)
  11679. {
  11680. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11681. int x=0,y=0;
  11682. x=script_getnum(st,2);
  11683. y=script_getnum(st,3);
  11684. if(nd) {
  11685. if (!nd->status.hp) {
  11686. status_calc_npc(nd, true);
  11687. } else {
  11688. status_calc_npc(nd, false);
  11689. }
  11690. unit_walktoxy(&nd->bl,x,y,0);
  11691. }
  11692. return SCRIPT_CMD_SUCCESS;
  11693. }
  11694. // stop an npc's movement [Valaris]
  11695. BUILDIN_FUNC(npcstop)
  11696. {
  11697. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11698. if(nd) {
  11699. unit_stop_walking(&nd->bl,1|4);
  11700. }
  11701. return SCRIPT_CMD_SUCCESS;
  11702. }
  11703. /*==========================================
  11704. * getlook char info. getlook(arg)
  11705. *------------------------------------------*/
  11706. BUILDIN_FUNC(getlook)
  11707. {
  11708. int type,val;
  11709. TBL_PC *sd;
  11710. sd=script_rid2sd(st);
  11711. type=script_getnum(st,2);
  11712. val=-1;
  11713. switch(type) {
  11714. case LOOK_HAIR: val=sd->status.hair; break; //1
  11715. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  11716. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  11717. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  11718. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  11719. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  11720. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  11721. case LOOK_SHIELD: val=sd->status.shield; break; //8
  11722. case LOOK_SHOES: break; //9
  11723. case LOOK_ROBE: val=sd->status.robe; break; //12
  11724. }
  11725. script_pushint(st,val);
  11726. return SCRIPT_CMD_SUCCESS;
  11727. }
  11728. /*==========================================
  11729. * get char save point. argument: 0- map name, 1- x, 2- y
  11730. *------------------------------------------*/
  11731. BUILDIN_FUNC(getsavepoint)
  11732. {
  11733. TBL_PC* sd;
  11734. int type;
  11735. sd = script_rid2sd(st);
  11736. if (sd == NULL) {
  11737. script_pushint(st,0);
  11738. return 0;
  11739. }
  11740. type = script_getnum(st,2);
  11741. switch(type) {
  11742. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  11743. case 1: script_pushint(st,sd->status.save_point.x); break;
  11744. case 2: script_pushint(st,sd->status.save_point.y); break;
  11745. default:
  11746. script_pushint(st,0);
  11747. break;
  11748. }
  11749. return SCRIPT_CMD_SUCCESS;
  11750. }
  11751. /*==========================================
  11752. * Get position for char/npc/pet/mob objects. Added by Lorky
  11753. *
  11754. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  11755. * where type:
  11756. * MapName$ - String variable for output map name
  11757. * MapX - Integer variable for output coord X
  11758. * MapY - Integer variable for output coord Y
  11759. * type - type of object
  11760. * 0 - Character coord
  11761. * 1 - NPC coord
  11762. * 2 - Pet coord
  11763. * 3 - Mob coord (not released)
  11764. * 4 - Homun coord
  11765. * 5 - Mercenary coord
  11766. * 6 - Elemental coord
  11767. * CharName$ - Name object. If miss or "this" the current object
  11768. *
  11769. * Return:
  11770. * 0 - success
  11771. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  11772. *------------------------------------------*/
  11773. BUILDIN_FUNC(getmapxy)
  11774. {
  11775. struct block_list *bl = NULL;
  11776. TBL_PC *sd=NULL;
  11777. int num;
  11778. const char *name;
  11779. char prefix;
  11780. int x,y,type;
  11781. char mapname[MAP_NAME_LENGTH];
  11782. if( !data_isreference(script_getdata(st,2)) ){
  11783. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  11784. script_pushint(st,-1);
  11785. return 1;
  11786. }
  11787. if( !data_isreference(script_getdata(st,3)) ){
  11788. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  11789. script_pushint(st,-1);
  11790. return 1;
  11791. }
  11792. if( !data_isreference(script_getdata(st,4)) ){
  11793. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  11794. script_pushint(st,-1);
  11795. return 1;
  11796. }
  11797. // Possible needly check function parameters on C_STR,C_INT,C_INT
  11798. type=script_getnum(st,5);
  11799. switch (type){
  11800. case 0: //Get Character Position
  11801. if( script_hasdata(st,6) )
  11802. sd=map_nick2sd(script_getstr(st,6));
  11803. else
  11804. sd=script_rid2sd(st);
  11805. if (sd)
  11806. bl = &sd->bl;
  11807. break;
  11808. case 1: //Get NPC Position
  11809. if( script_hasdata(st,6) )
  11810. {
  11811. struct npc_data *nd;
  11812. nd=npc_name2id(script_getstr(st,6));
  11813. if (nd)
  11814. bl = &nd->bl;
  11815. } else //In case the origin is not an npc?
  11816. bl=map_id2bl(st->oid);
  11817. break;
  11818. case 2: //Get Pet Position
  11819. if(script_hasdata(st,6))
  11820. sd=map_nick2sd(script_getstr(st,6));
  11821. else
  11822. sd=script_rid2sd(st);
  11823. if (sd && sd->pd)
  11824. bl = &sd->pd->bl;
  11825. break;
  11826. case 3: //Get Mob Position
  11827. break; //Not supported?
  11828. case 4: //Get Homun Position
  11829. if(script_hasdata(st,6))
  11830. sd=map_nick2sd(script_getstr(st,6));
  11831. else
  11832. sd=script_rid2sd(st);
  11833. if (sd && sd->hd)
  11834. bl = &sd->hd->bl;
  11835. break;
  11836. case 5: //Get Mercenary Position
  11837. if(script_hasdata(st,6))
  11838. sd=map_nick2sd(script_getstr(st,6));
  11839. else
  11840. sd=script_rid2sd(st);
  11841. if (sd && sd->md)
  11842. bl = &sd->md->bl;
  11843. break;
  11844. case 6: //Get Elemental Position
  11845. if(script_hasdata(st,6))
  11846. sd=map_nick2sd(script_getstr(st,6));
  11847. else
  11848. sd=script_rid2sd(st);
  11849. if (sd && sd->ed)
  11850. bl = &sd->ed->bl;
  11851. break;
  11852. default:
  11853. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  11854. script_pushint(st,-1);
  11855. return 1;
  11856. }
  11857. if (!bl) { //No object found.
  11858. script_pushint(st,-1);
  11859. return 0;
  11860. }
  11861. x= bl->x;
  11862. y= bl->y;
  11863. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  11864. //Set MapName$
  11865. num=st->stack->stack_data[st->start+2].u.num;
  11866. name=get_str(num&0x00ffffff);
  11867. prefix=*name;
  11868. if(not_server_variable(prefix))
  11869. sd=script_rid2sd(st);
  11870. else
  11871. sd=NULL;
  11872. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  11873. //Set MapX
  11874. num=st->stack->stack_data[st->start+3].u.num;
  11875. name=get_str(num&0x00ffffff);
  11876. prefix=*name;
  11877. if(not_server_variable(prefix))
  11878. sd=script_rid2sd(st);
  11879. else
  11880. sd=NULL;
  11881. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  11882. //Set MapY
  11883. num=st->stack->stack_data[st->start+4].u.num;
  11884. name=get_str(num&0x00ffffff);
  11885. prefix=*name;
  11886. if(not_server_variable(prefix))
  11887. sd=script_rid2sd(st);
  11888. else
  11889. sd=NULL;
  11890. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  11891. //Return Success value
  11892. script_pushint(st,0);
  11893. return SCRIPT_CMD_SUCCESS;
  11894. }
  11895. /*==========================================
  11896. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  11897. *------------------------------------------*/
  11898. BUILDIN_FUNC(logmes)
  11899. {
  11900. const char *str;
  11901. TBL_PC* sd;
  11902. sd = script_rid2sd(st);
  11903. if( sd == NULL )
  11904. return 1;
  11905. str = script_getstr(st,2);
  11906. log_npc(sd,str);
  11907. return SCRIPT_CMD_SUCCESS;
  11908. }
  11909. BUILDIN_FUNC(summon)
  11910. {
  11911. int _class, timeout=0;
  11912. const char *str,*event="";
  11913. TBL_PC *sd;
  11914. struct mob_data *md;
  11915. int tick = gettick();
  11916. sd=script_rid2sd(st);
  11917. if (!sd) return 0;
  11918. str =script_getstr(st,2);
  11919. _class=script_getnum(st,3);
  11920. if( script_hasdata(st,4) )
  11921. timeout=script_getnum(st,4);
  11922. if( script_hasdata(st,5) ){
  11923. event=script_getstr(st,5);
  11924. check_event(st, event);
  11925. }
  11926. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  11927. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  11928. if (md) {
  11929. md->master_id=sd->bl.id;
  11930. md->special_state.ai = AI_ATTACK;
  11931. if( md->deletetimer != INVALID_TIMER )
  11932. delete_timer(md->deletetimer, mob_timer_delete);
  11933. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  11934. mob_spawn (md); //Now it is ready for spawning.
  11935. clif_specialeffect(&md->bl,344,AREA);
  11936. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  11937. }
  11938. return SCRIPT_CMD_SUCCESS;
  11939. }
  11940. /*==========================================
  11941. * Checks whether it is daytime/nighttime
  11942. *------------------------------------------*/
  11943. BUILDIN_FUNC(isnight)
  11944. {
  11945. script_pushint(st,(night_flag == 1));
  11946. return SCRIPT_CMD_SUCCESS;
  11947. }
  11948. BUILDIN_FUNC(isday)
  11949. {
  11950. script_pushint(st,(night_flag == 0));
  11951. return SCRIPT_CMD_SUCCESS;
  11952. }
  11953. /*================================================
  11954. * Check how many items/cards in the list are
  11955. * equipped - used for 2/15's cards patch [celest]
  11956. *------------------------------------------------*/
  11957. BUILDIN_FUNC(isequippedcnt)
  11958. {
  11959. TBL_PC *sd;
  11960. int i, j, k, id = 1;
  11961. int ret = 0;
  11962. sd = script_rid2sd(st);
  11963. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11964. script_pushint(st,0);
  11965. return 0;
  11966. }
  11967. for (i=0; id!=0; i++) {
  11968. FETCH (i+2, id) else id = 0;
  11969. if (id <= 0)
  11970. continue;
  11971. for (j=0; j<EQI_MAX; j++) {
  11972. int index;
  11973. index = sd->equip_index[j];
  11974. if(index < 0) continue;
  11975. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11976. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11977. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11978. if(!sd->inventory_data[index])
  11979. continue;
  11980. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  11981. if (sd->inventory_data[index]->nameid == id)
  11982. ret+= sd->status.inventory[index].amount;
  11983. } else { //Count cards.
  11984. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11985. continue; //No cards
  11986. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11987. if (sd->status.inventory[index].card[k] == id)
  11988. ret++; //[Lupus]
  11989. }
  11990. }
  11991. }
  11992. }
  11993. script_pushint(st,ret);
  11994. return SCRIPT_CMD_SUCCESS;
  11995. }
  11996. /*================================================
  11997. * Check whether another card has been
  11998. * equipped - used for 2/15's cards patch [celest]
  11999. * -- Items checked cannot be reused in another
  12000. * card set to prevent exploits
  12001. *------------------------------------------------*/
  12002. BUILDIN_FUNC(isequipped)
  12003. {
  12004. TBL_PC *sd;
  12005. int i, j, k, id = 1;
  12006. int index, flag;
  12007. int ret = -1;
  12008. //Original hash to reverse it when full check fails.
  12009. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  12010. sd = script_rid2sd(st);
  12011. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12012. script_pushint(st,0);
  12013. return 0;
  12014. }
  12015. setitem_hash = sd->bonus.setitem_hash;
  12016. setitem_hash2 = sd->bonus.setitem_hash2;
  12017. for (i=0; id!=0; i++) {
  12018. FETCH (i+2, id) else id = 0;
  12019. if (id <= 0)
  12020. continue;
  12021. flag = 0;
  12022. for (j=0; j<EQI_MAX; j++) {
  12023. index = sd->equip_index[j];
  12024. if(index < 0) continue;
  12025. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  12026. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  12027. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  12028. if(!sd->inventory_data[index])
  12029. continue;
  12030. if (itemdb_type(id) != IT_CARD) {
  12031. if (sd->inventory_data[index]->nameid != id)
  12032. continue;
  12033. flag = 1;
  12034. break;
  12035. } else { //Cards
  12036. if (sd->inventory_data[index]->slot == 0 ||
  12037. itemdb_isspecial(sd->status.inventory[index].card[0]))
  12038. continue;
  12039. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  12040. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  12041. unsigned int hash = 0;
  12042. if (sd->status.inventory[index].card[k] != id)
  12043. continue;
  12044. hash = 1<<((j<5?j:j-5)*4 + k);
  12045. // check if card is already used by another set
  12046. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  12047. continue;
  12048. // We have found a match
  12049. flag = 1;
  12050. // Set hash so this card cannot be used by another
  12051. if (j<5)
  12052. sd->bonus.setitem_hash |= hash;
  12053. else
  12054. sd->bonus.setitem_hash2 |= hash;
  12055. break;
  12056. }
  12057. }
  12058. if (flag) break; //Card found
  12059. }
  12060. if (ret == -1)
  12061. ret = flag;
  12062. else
  12063. ret &= flag;
  12064. if (!ret) break;
  12065. }
  12066. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  12067. sd->bonus.setitem_hash = setitem_hash;
  12068. sd->bonus.setitem_hash2 = setitem_hash2;
  12069. }
  12070. script_pushint(st,ret);
  12071. return SCRIPT_CMD_SUCCESS;
  12072. }
  12073. /*================================================
  12074. * Check how many given inserted cards in the CURRENT
  12075. * weapon - used for 2/15's cards patch [Lupus]
  12076. *------------------------------------------------*/
  12077. BUILDIN_FUNC(cardscnt)
  12078. {
  12079. TBL_PC *sd;
  12080. int i, k, id = 1;
  12081. int ret = 0;
  12082. int index;
  12083. sd = script_rid2sd(st);
  12084. for (i=0; id!=0; i++) {
  12085. FETCH (i+2, id) else id = 0;
  12086. if (id <= 0)
  12087. continue;
  12088. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  12089. if(index < 0) continue;
  12090. if(!sd->inventory_data[index])
  12091. continue;
  12092. if(itemdb_type(id) != IT_CARD) {
  12093. if (sd->inventory_data[index]->nameid == id)
  12094. ret+= sd->status.inventory[index].amount;
  12095. } else {
  12096. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12097. continue;
  12098. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12099. if (sd->status.inventory[index].card[k] == id)
  12100. ret++;
  12101. }
  12102. }
  12103. }
  12104. script_pushint(st,ret);
  12105. // script_pushint(st,current_equip_item_index);
  12106. return SCRIPT_CMD_SUCCESS;
  12107. }
  12108. /*=======================================================
  12109. * Returns the refined number of the current item, or an
  12110. * item with inventory index specified
  12111. *-------------------------------------------------------*/
  12112. BUILDIN_FUNC(getrefine)
  12113. {
  12114. TBL_PC *sd;
  12115. if ((sd = script_rid2sd(st))!= NULL)
  12116. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  12117. else
  12118. script_pushint(st,0);
  12119. return SCRIPT_CMD_SUCCESS;
  12120. }
  12121. /*=======================================================
  12122. * Day/Night controls
  12123. *-------------------------------------------------------*/
  12124. BUILDIN_FUNC(night)
  12125. {
  12126. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  12127. return SCRIPT_CMD_SUCCESS;
  12128. }
  12129. BUILDIN_FUNC(day)
  12130. {
  12131. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  12132. return SCRIPT_CMD_SUCCESS;
  12133. }
  12134. //=======================================================
  12135. // Unequip [Spectre]
  12136. //-------------------------------------------------------
  12137. BUILDIN_FUNC(unequip)
  12138. {
  12139. int i;
  12140. size_t num;
  12141. TBL_PC *sd;
  12142. num = script_getnum(st,2);
  12143. sd = script_rid2sd(st);
  12144. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  12145. {
  12146. i = pc_checkequip(sd,equip[num-1]);
  12147. if (i >= 0)
  12148. pc_unequipitem(sd,i,1|2);
  12149. }
  12150. return SCRIPT_CMD_SUCCESS;
  12151. }
  12152. BUILDIN_FUNC(equip)
  12153. {
  12154. int nameid=0,i;
  12155. TBL_PC *sd;
  12156. struct item_data *item_data;
  12157. sd = script_rid2sd(st);
  12158. nameid=script_getnum(st,2);
  12159. if((item_data = itemdb_exists(nameid)) == NULL)
  12160. {
  12161. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  12162. return 1;
  12163. }
  12164. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  12165. if( i < MAX_INVENTORY )
  12166. pc_equipitem(sd,i,item_data->equip);
  12167. return SCRIPT_CMD_SUCCESS;
  12168. }
  12169. BUILDIN_FUNC(autoequip)
  12170. {
  12171. int nameid, flag;
  12172. struct item_data *item_data;
  12173. nameid=script_getnum(st,2);
  12174. flag=script_getnum(st,3);
  12175. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  12176. {
  12177. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  12178. return SCRIPT_CMD_FAILURE;
  12179. }
  12180. if( !itemdb_isequip2(item_data) )
  12181. {
  12182. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  12183. return SCRIPT_CMD_FAILURE;
  12184. }
  12185. item_data->flag.autoequip = flag>0?1:0;
  12186. return SCRIPT_CMD_SUCCESS;
  12187. }
  12188. BUILDIN_FUNC(setbattleflag)
  12189. {
  12190. const char *flag, *value;
  12191. flag = script_getstr(st,2);
  12192. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  12193. if (battle_set_value(flag, value) == 0)
  12194. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  12195. else
  12196. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  12197. return SCRIPT_CMD_SUCCESS;
  12198. }
  12199. BUILDIN_FUNC(getbattleflag)
  12200. {
  12201. const char *flag;
  12202. flag = script_getstr(st,2);
  12203. script_pushint(st,battle_get_value(flag));
  12204. return SCRIPT_CMD_SUCCESS;
  12205. }
  12206. //=======================================================
  12207. // strlen [Valaris]
  12208. //-------------------------------------------------------
  12209. BUILDIN_FUNC(getstrlen)
  12210. {
  12211. const char *str = script_getstr(st,2);
  12212. int len = (str) ? (int)strlen(str) : 0;
  12213. script_pushint(st,len);
  12214. return SCRIPT_CMD_SUCCESS;
  12215. }
  12216. //=======================================================
  12217. // isalpha [Valaris]
  12218. //-------------------------------------------------------
  12219. BUILDIN_FUNC(charisalpha)
  12220. {
  12221. const char *str=script_getstr(st,2);
  12222. int pos=script_getnum(st,3);
  12223. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  12224. script_pushint(st,val);
  12225. return SCRIPT_CMD_SUCCESS;
  12226. }
  12227. //=======================================================
  12228. // charisupper <str>, <index>
  12229. //-------------------------------------------------------
  12230. BUILDIN_FUNC(charisupper)
  12231. {
  12232. const char *str = script_getstr(st,2);
  12233. int pos = script_getnum(st,3);
  12234. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  12235. script_pushint(st,val);
  12236. return SCRIPT_CMD_SUCCESS;
  12237. }
  12238. //=======================================================
  12239. // charislower <str>, <index>
  12240. //-------------------------------------------------------
  12241. BUILDIN_FUNC(charislower)
  12242. {
  12243. const char *str = script_getstr(st,2);
  12244. int pos = script_getnum(st,3);
  12245. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  12246. script_pushint(st,val);
  12247. return SCRIPT_CMD_SUCCESS;
  12248. }
  12249. //=======================================================
  12250. // charat <str>, <index>
  12251. //-------------------------------------------------------
  12252. BUILDIN_FUNC(charat) {
  12253. const char *str = script_getstr(st,2);
  12254. int pos = script_getnum(st,3);
  12255. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  12256. char output[2];
  12257. output[0] = str[pos];
  12258. output[1] = '\0';
  12259. script_pushstrcopy(st, output);
  12260. } else
  12261. script_pushconststr(st, "");
  12262. return SCRIPT_CMD_SUCCESS;
  12263. }
  12264. //=======================================================
  12265. // setchar <string>, <char>, <index>
  12266. //-------------------------------------------------------
  12267. BUILDIN_FUNC(setchar)
  12268. {
  12269. const char *str = script_getstr(st,2);
  12270. const char *c = script_getstr(st,3);
  12271. int index = script_getnum(st,4);
  12272. char *output = aStrdup(str);
  12273. if(index >= 0 && index < strlen(output))
  12274. output[index] = *c;
  12275. script_pushstr(st, output);
  12276. return SCRIPT_CMD_SUCCESS;
  12277. }
  12278. //=======================================================
  12279. // insertchar <string>, <char>, <index>
  12280. //-------------------------------------------------------
  12281. BUILDIN_FUNC(insertchar)
  12282. {
  12283. const char *str = script_getstr(st,2);
  12284. const char *c = script_getstr(st,3);
  12285. int index = script_getnum(st,4);
  12286. char *output;
  12287. size_t len = strlen(str);
  12288. if(index < 0)
  12289. index = 0;
  12290. else if(index > len)
  12291. index = len;
  12292. output = (char*)aMalloc(len + 2);
  12293. memcpy(output, str, index);
  12294. output[index] = c[0];
  12295. memcpy(&output[index+1], &str[index], len - index);
  12296. output[len+1] = '\0';
  12297. script_pushstr(st, output);
  12298. return SCRIPT_CMD_SUCCESS;
  12299. }
  12300. //=======================================================
  12301. // delchar <string>, <index>
  12302. //-------------------------------------------------------
  12303. BUILDIN_FUNC(delchar)
  12304. {
  12305. const char *str = script_getstr(st,2);
  12306. int index = script_getnum(st,3);
  12307. char *output;
  12308. size_t len = strlen(str);
  12309. if(index < 0 || index > len) {
  12310. //return original
  12311. output = aStrdup(str);
  12312. script_pushstr(st, output);
  12313. return 0;
  12314. }
  12315. output = (char*)aMalloc(len);
  12316. memcpy(output, str, index);
  12317. memcpy(&output[index], &str[index+1], len - index);
  12318. script_pushstr(st, output);
  12319. return SCRIPT_CMD_SUCCESS;
  12320. }
  12321. //=======================================================
  12322. // strtoupper <str>
  12323. //-------------------------------------------------------
  12324. BUILDIN_FUNC(strtoupper)
  12325. {
  12326. const char *str = script_getstr(st,2);
  12327. char *output = aStrdup(str);
  12328. char *cursor = output;
  12329. while (*cursor != '\0') {
  12330. *cursor = TOUPPER(*cursor);
  12331. cursor++;
  12332. }
  12333. script_pushstr(st, output);
  12334. return SCRIPT_CMD_SUCCESS;
  12335. }
  12336. //=======================================================
  12337. // strtolower <str>
  12338. //-------------------------------------------------------
  12339. BUILDIN_FUNC(strtolower)
  12340. {
  12341. const char *str = script_getstr(st,2);
  12342. char *output = aStrdup(str);
  12343. char *cursor = output;
  12344. while (*cursor != '\0') {
  12345. *cursor = TOLOWER(*cursor);
  12346. cursor++;
  12347. }
  12348. script_pushstr(st, output);
  12349. return SCRIPT_CMD_SUCCESS;
  12350. }
  12351. //=======================================================
  12352. // substr <str>, <start>, <end>
  12353. //-------------------------------------------------------
  12354. BUILDIN_FUNC(substr)
  12355. {
  12356. const char *str = script_getstr(st,2);
  12357. char *output;
  12358. int start = script_getnum(st,3);
  12359. int end = script_getnum(st,4);
  12360. int len = 0;
  12361. if(start >= 0 && end < strlen(str) && start <= end) {
  12362. len = end - start + 1;
  12363. output = (char*)aMalloc(len + 1);
  12364. memcpy(output, &str[start], len);
  12365. } else
  12366. output = (char*)aMalloc(1);
  12367. output[len] = '\0';
  12368. script_pushstr(st, output);
  12369. return SCRIPT_CMD_SUCCESS;
  12370. }
  12371. //=======================================================
  12372. // explode <dest_string_array>, <str>, <delimiter>
  12373. // Note: delimiter is limited to 1 char
  12374. //-------------------------------------------------------
  12375. BUILDIN_FUNC(explode)
  12376. {
  12377. struct script_data* data = script_getdata(st, 2);
  12378. const char *str = script_getstr(st,3);
  12379. const char delimiter = script_getstr(st, 4)[0];
  12380. int32 id;
  12381. size_t len = strlen(str);
  12382. int i = 0, j = 0;
  12383. int start;
  12384. char *temp;
  12385. const char* name;
  12386. TBL_PC* sd = NULL;
  12387. temp = (char*)aMalloc(len + 1);
  12388. if( !data_isreference(data) )
  12389. {
  12390. ShowError("script:explode: not a variable\n");
  12391. script_reportdata(data);
  12392. st->state = END;
  12393. return 1;// not a variable
  12394. }
  12395. id = reference_getid(data);
  12396. start = reference_getindex(data);
  12397. name = reference_getname(data);
  12398. if( not_array_variable(*name) )
  12399. {
  12400. ShowError("script:explode: illegal scope\n");
  12401. script_reportdata(data);
  12402. st->state = END;
  12403. return 1;// not supported
  12404. }
  12405. if( !is_string_variable(name) )
  12406. {
  12407. ShowError("script:explode: not string array\n");
  12408. script_reportdata(data);
  12409. st->state = END;
  12410. return 1;// data type mismatch
  12411. }
  12412. if( not_server_variable(*name) )
  12413. {
  12414. sd = script_rid2sd(st);
  12415. if( sd == NULL )
  12416. return 0;// no player attached
  12417. }
  12418. while(str[i] != '\0') {
  12419. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  12420. temp[j] = '\0';
  12421. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  12422. j = 0;
  12423. ++i;
  12424. } else {
  12425. temp[j++] = str[i++];
  12426. }
  12427. }
  12428. //set last string
  12429. temp[j] = '\0';
  12430. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  12431. aFree(temp);
  12432. return SCRIPT_CMD_SUCCESS;
  12433. }
  12434. //=======================================================
  12435. // implode <string_array>
  12436. // implode <string_array>, <glue>
  12437. //-------------------------------------------------------
  12438. BUILDIN_FUNC(implode)
  12439. {
  12440. struct script_data* data = script_getdata(st, 2);
  12441. const char *glue = NULL, *name, *temp;
  12442. int32 glue_len = 0, array_size, id;
  12443. size_t len = 0;
  12444. int i, k = 0;
  12445. TBL_PC* sd = NULL;
  12446. char *output;
  12447. if( !data_isreference(data) )
  12448. {
  12449. ShowError("script:implode: not a variable\n");
  12450. script_reportdata(data);
  12451. st->state = END;
  12452. return 1;// not a variable
  12453. }
  12454. id = reference_getid(data);
  12455. name = reference_getname(data);
  12456. if( not_array_variable(*name) )
  12457. {
  12458. ShowError("script:implode: illegal scope\n");
  12459. script_reportdata(data);
  12460. st->state = END;
  12461. return 1;// not supported
  12462. }
  12463. if( !is_string_variable(name) )
  12464. {
  12465. ShowError("script:implode: not string array\n");
  12466. script_reportdata(data);
  12467. st->state = END;
  12468. return 1;// data type mismatch
  12469. }
  12470. if( not_server_variable(*name) )
  12471. {
  12472. sd = script_rid2sd(st);
  12473. if( sd == NULL )
  12474. return 0;// no player attached
  12475. }
  12476. //count chars
  12477. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  12478. if(array_size == -1) //empty array check (AmsTaff)
  12479. {
  12480. ShowWarning("script:implode: array length = 0\n");
  12481. output = (char*)aMalloc(sizeof(char)*5);
  12482. sprintf(output,"%s","NULL");
  12483. } else {
  12484. for(i = 0; i <= array_size; ++i) {
  12485. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12486. len += strlen(temp);
  12487. script_removetop(st, -1, 0);
  12488. }
  12489. //allocate mem
  12490. if( script_hasdata(st,3) ) {
  12491. glue = script_getstr(st,3);
  12492. glue_len = strlen(glue);
  12493. len += glue_len * (array_size);
  12494. }
  12495. output = (char*)aMalloc(len + 1);
  12496. //build output
  12497. for(i = 0; i < array_size; ++i) {
  12498. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12499. len = strlen(temp);
  12500. memcpy(&output[k], temp, len);
  12501. k += len;
  12502. if(glue_len != 0) {
  12503. memcpy(&output[k], glue, glue_len);
  12504. k += glue_len;
  12505. }
  12506. script_removetop(st, -1, 0);
  12507. }
  12508. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  12509. len = strlen(temp);
  12510. memcpy(&output[k], temp, len);
  12511. k += len;
  12512. script_removetop(st, -1, 0);
  12513. output[k] = '\0';
  12514. }
  12515. script_pushstr(st, output);
  12516. return SCRIPT_CMD_SUCCESS;
  12517. }
  12518. //=======================================================
  12519. // sprintf(<format>, ...);
  12520. // Implements C sprintf, except format %n. The resulting string is
  12521. // returned, instead of being saved in variable by reference.
  12522. //-------------------------------------------------------
  12523. BUILDIN_FUNC(sprintf)
  12524. {
  12525. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  12526. const char* format;
  12527. char* p;
  12528. char* q;
  12529. char* buf = NULL;
  12530. char* buf2 = NULL;
  12531. struct script_data* data;
  12532. StringBuf final_buf;
  12533. // Fetch init data
  12534. format = script_getstr(st, 2);
  12535. argc = script_lastdata(st)-2;
  12536. len = strlen(format);
  12537. // Skip parsing, where no parsing is required.
  12538. if(len==0){
  12539. script_pushconststr(st,"");
  12540. return 0;
  12541. }
  12542. // Pessimistic alloc
  12543. CREATE(buf, char, len+1);
  12544. // Need not be parsed, just solve stuff like %%.
  12545. if(argc==0){
  12546. memcpy(buf,format,len+1);
  12547. script_pushstrcopy(st, buf);
  12548. aFree(buf);
  12549. return 0;
  12550. }
  12551. safestrncpy(buf, format, len+1);
  12552. // Issue sprintf for each parameter
  12553. StringBuf_Init(&final_buf);
  12554. q = buf;
  12555. while((p = strchr(q, '%'))!=NULL){
  12556. if(p!=q){
  12557. len = p-q+1;
  12558. if(buf2_len<len){
  12559. RECREATE(buf2, char, len);
  12560. buf2_len = len;
  12561. }
  12562. safestrncpy(buf2, q, len);
  12563. StringBuf_AppendStr(&final_buf, buf2);
  12564. q = p;
  12565. }
  12566. p = q+1;
  12567. if(*p=='%'){ // %%
  12568. StringBuf_AppendStr(&final_buf, "%");
  12569. q+=2;
  12570. continue;
  12571. }
  12572. if(*p=='n'){ // %n
  12573. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  12574. script_reportsrc(st);
  12575. q+=2;
  12576. continue;
  12577. }
  12578. if(arg>=argc){
  12579. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  12580. if(buf) aFree(buf);
  12581. if(buf2) aFree(buf2);
  12582. StringBuf_Destroy(&final_buf);
  12583. script_pushconststr(st,"");
  12584. return 1;
  12585. }
  12586. if((p = strchr(q+1, '%'))==NULL){
  12587. p = strchr(q, 0); // EOS
  12588. }
  12589. len = p-q+1;
  12590. if(buf2_len<len){
  12591. RECREATE(buf2, char, len);
  12592. buf2_len = len;
  12593. }
  12594. safestrncpy(buf2, q, len);
  12595. q = p;
  12596. // Note: This assumes the passed value being the correct
  12597. // type to the current format specifier. If not, the server
  12598. // probably crashes or returns anything else, than expected,
  12599. // but it would behave in normal code the same way so it's
  12600. // the scripter's responsibility.
  12601. data = script_getdata(st, arg+3);
  12602. if(data_isstring(data)){ // String
  12603. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12604. }else if(data_isint(data)){ // Number
  12605. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12606. }else if(data_isreference(data)){ // Variable
  12607. char* name = reference_getname(data);
  12608. if(name[strlen(name)-1]=='$'){ // var Str
  12609. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12610. }else{ // var Int
  12611. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12612. }
  12613. }else{ // Unsupported type
  12614. ShowError("buildin_sprintf: Unknown argument type!\n");
  12615. if(buf) aFree(buf);
  12616. if(buf2) aFree(buf2);
  12617. StringBuf_Destroy(&final_buf);
  12618. script_pushconststr(st,"");
  12619. return 1;
  12620. }
  12621. arg++;
  12622. }
  12623. // Append anything left
  12624. if(*q){
  12625. StringBuf_AppendStr(&final_buf, q);
  12626. }
  12627. // Passed more, than needed
  12628. if(arg<argc){
  12629. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  12630. script_reportsrc(st);
  12631. }
  12632. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  12633. if(buf) aFree(buf);
  12634. if(buf2) aFree(buf2);
  12635. StringBuf_Destroy(&final_buf);
  12636. return SCRIPT_CMD_SUCCESS;
  12637. }
  12638. //=======================================================
  12639. // sscanf(<str>, <format>, ...);
  12640. // Implements C sscanf.
  12641. //-------------------------------------------------------
  12642. BUILDIN_FUNC(sscanf){
  12643. unsigned int argc, arg = 0, len;
  12644. struct script_data* data;
  12645. struct map_session_data* sd = NULL;
  12646. const char* str;
  12647. const char* format;
  12648. const char* p;
  12649. const char* q;
  12650. char* buf = NULL;
  12651. char* buf_p;
  12652. char* ref_str = NULL;
  12653. int ref_int;
  12654. // Get data
  12655. str = script_getstr(st, 2);
  12656. format = script_getstr(st, 3);
  12657. argc = script_lastdata(st)-3;
  12658. len = strlen(format);
  12659. CREATE(buf, char, len*2+1);
  12660. // Issue sscanf for each parameter
  12661. *buf = 0;
  12662. q = format;
  12663. while((p = strchr(q, '%'))){
  12664. if(p!=q){
  12665. strncat(buf, q, (size_t)(p-q));
  12666. q = p;
  12667. }
  12668. p = q+1;
  12669. if(*p=='*' || *p=='%'){ // Skip
  12670. strncat(buf, q, 2);
  12671. q+=2;
  12672. continue;
  12673. }
  12674. if(arg>=argc){
  12675. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  12676. script_pushint(st, -1);
  12677. if(buf) aFree(buf);
  12678. if(ref_str) aFree(ref_str);
  12679. return 1;
  12680. }
  12681. if((p = strchr(q+1, '%'))==NULL){
  12682. p = strchr(q, 0); // EOS
  12683. }
  12684. len = p-q;
  12685. strncat(buf, q, len);
  12686. q = p;
  12687. // Validate output
  12688. data = script_getdata(st, arg+4);
  12689. if(!data_isreference(data) || !reference_tovariable(data)){
  12690. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  12691. script_pushint(st, -1);
  12692. if(buf) aFree(buf);
  12693. if(ref_str) aFree(ref_str);
  12694. return 1;
  12695. }
  12696. buf_p = reference_getname(data);
  12697. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  12698. script_pushint(st, -1);
  12699. if(buf) aFree(buf);
  12700. if(ref_str) aFree(ref_str);
  12701. return 0;
  12702. }
  12703. // Save value if any
  12704. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  12705. if(ref_str==NULL){
  12706. CREATE(ref_str, char, strlen(str)+1);
  12707. }
  12708. if(sscanf(str, buf, ref_str)==0){
  12709. break;
  12710. }
  12711. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  12712. } else { // Number
  12713. if(sscanf(str, buf, &ref_int)==0){
  12714. break;
  12715. }
  12716. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  12717. }
  12718. arg++;
  12719. // Disable used format (%... -> %*...)
  12720. buf_p = strchr(buf, 0);
  12721. memmove(buf_p-len+2, buf_p-len+1, len);
  12722. *(buf_p-len+1) = '*';
  12723. }
  12724. if( !strcmp(str, "") )
  12725. script_pushint(st, -1);
  12726. else
  12727. script_pushint(st, arg);
  12728. if(buf) aFree(buf);
  12729. if(ref_str) aFree(ref_str);
  12730. return SCRIPT_CMD_SUCCESS;
  12731. }
  12732. //=======================================================
  12733. // strpos(<haystack>, <needle>)
  12734. // strpos(<haystack>, <needle>, <offset>)
  12735. //
  12736. // Implements PHP style strpos. Adapted from code from
  12737. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  12738. //-------------------------------------------------------
  12739. BUILDIN_FUNC(strpos) {
  12740. const char *haystack = script_getstr(st,2);
  12741. const char *needle = script_getstr(st,3);
  12742. int i;
  12743. size_t len;
  12744. if( script_hasdata(st,4) )
  12745. i = script_getnum(st,4);
  12746. else
  12747. i = 0;
  12748. if (needle[0] == '\0') {
  12749. script_pushint(st, -1);
  12750. return 0;
  12751. }
  12752. len = strlen(haystack);
  12753. for ( ; i < len; ++i ) {
  12754. if ( haystack[i] == *needle ) {
  12755. // matched starting char -- loop through remaining chars
  12756. const char *h, *n;
  12757. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  12758. if ( *h != *n ) {
  12759. break;
  12760. }
  12761. }
  12762. if ( !*n ) { // matched all of 'needle' to null termination
  12763. script_pushint(st, i);
  12764. return 0;
  12765. }
  12766. }
  12767. }
  12768. script_pushint(st, -1);
  12769. return SCRIPT_CMD_SUCCESS;
  12770. }
  12771. //===============================================================
  12772. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  12773. //
  12774. // Note: Finds all instances of <search> in <input> and replaces
  12775. // with <replace>. If specified will only replace as many
  12776. // instances as specified in <count>. By default will be case
  12777. // sensitive.
  12778. //---------------------------------------------------------------
  12779. BUILDIN_FUNC(replacestr)
  12780. {
  12781. const char *input = script_getstr(st, 2);
  12782. const char *find = script_getstr(st, 3);
  12783. const char *replace = script_getstr(st, 4);
  12784. size_t inputlen = strlen(input);
  12785. size_t findlen = strlen(find);
  12786. struct StringBuf output;
  12787. bool usecase = true;
  12788. int count = 0;
  12789. int numFinds = 0;
  12790. int i = 0, f = 0;
  12791. if(findlen == 0) {
  12792. ShowError("script:replacestr: Invalid search length.\n");
  12793. st->state = END;
  12794. return 1;
  12795. }
  12796. if(script_hasdata(st, 5)) {
  12797. struct script_data *data = script_getdata(st, 5);
  12798. get_val(st, data); // Convert into value in case of a variable
  12799. if( !data_isstring(data) )
  12800. usecase = script_getnum(st, 5) != 0;
  12801. else {
  12802. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  12803. st->state = END;
  12804. return 1;
  12805. }
  12806. }
  12807. if(script_hasdata(st, 6)) {
  12808. count = script_getnum(st, 6);
  12809. if(count == 0) {
  12810. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  12811. st->state = END;
  12812. return 1;
  12813. }
  12814. }
  12815. StringBuf_Init(&output);
  12816. for(; i < inputlen; i++) {
  12817. if(count && count == numFinds) { //found enough, stop looking
  12818. break;
  12819. }
  12820. for(f = 0; f <= findlen; f++) {
  12821. if(f == findlen) { //complete match
  12822. numFinds++;
  12823. StringBuf_AppendStr(&output, replace);
  12824. i += findlen - 1;
  12825. break;
  12826. } else {
  12827. if(usecase) {
  12828. if((i + f) > inputlen || input[i + f] != find[f]) {
  12829. StringBuf_Printf(&output, "%c", input[i]);
  12830. break;
  12831. }
  12832. } else {
  12833. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12834. StringBuf_Printf(&output, "%c", input[i]);
  12835. break;
  12836. }
  12837. }
  12838. }
  12839. }
  12840. }
  12841. //append excess after enough found
  12842. if(i < inputlen)
  12843. StringBuf_AppendStr(&output, &(input[i]));
  12844. script_pushstrcopy(st, StringBuf_Value(&output));
  12845. StringBuf_Destroy(&output);
  12846. return SCRIPT_CMD_SUCCESS;
  12847. }
  12848. //========================================================
  12849. // countstr <input>, <search>{, <usecase>}
  12850. //
  12851. // Note: Counts the number of times <search> occurs in
  12852. // <input>. By default will be case sensitive.
  12853. //--------------------------------------------------------
  12854. BUILDIN_FUNC(countstr)
  12855. {
  12856. const char *input = script_getstr(st, 2);
  12857. const char *find = script_getstr(st, 3);
  12858. size_t inputlen = strlen(input);
  12859. size_t findlen = strlen(find);
  12860. bool usecase = true;
  12861. int numFinds = 0;
  12862. int i = 0, f = 0;
  12863. if(findlen == 0) {
  12864. ShowError("script:countstr: Invalid search length.\n");
  12865. st->state = END;
  12866. return 1;
  12867. }
  12868. if(script_hasdata(st, 4)) {
  12869. struct script_data *data;
  12870. data = script_getdata(st, 4);
  12871. get_val(st, data); // Convert into value in case of a variable
  12872. if( !data_isstring(data) )
  12873. usecase = script_getnum(st, 4) != 0;
  12874. else {
  12875. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  12876. st->state = END;
  12877. return 1;
  12878. }
  12879. }
  12880. for(; i < inputlen; i++) {
  12881. for(f = 0; f <= findlen; f++) {
  12882. if(f == findlen) { //complete match
  12883. numFinds++;
  12884. i += findlen - 1;
  12885. break;
  12886. } else {
  12887. if(usecase) {
  12888. if((i + f) > inputlen || input[i + f] != find[f]) {
  12889. break;
  12890. }
  12891. } else {
  12892. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12893. break;
  12894. }
  12895. }
  12896. }
  12897. }
  12898. }
  12899. script_pushint(st, numFinds);
  12900. return SCRIPT_CMD_SUCCESS;
  12901. }
  12902. /// Changes the display name and/or display class of the npc.
  12903. /// Returns 0 is successful, 1 if the npc does not exist.
  12904. ///
  12905. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  12906. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  12907. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  12908. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  12909. BUILDIN_FUNC(setnpcdisplay)
  12910. {
  12911. const char* name;
  12912. const char* newname = NULL;
  12913. int class_ = -1, size = -1;
  12914. struct script_data* data;
  12915. struct npc_data* nd;
  12916. name = script_getstr(st,2);
  12917. data = script_getdata(st,3);
  12918. if( script_hasdata(st,4) )
  12919. class_ = script_getnum(st,4);
  12920. if( script_hasdata(st,5) )
  12921. size = script_getnum(st,5);
  12922. get_val(st, data);
  12923. if( data_isstring(data) )
  12924. newname = conv_str(st,data);
  12925. else if( data_isint(data) )
  12926. class_ = conv_num(st,data);
  12927. else
  12928. {
  12929. ShowError("script:setnpcdisplay: expected string or number\n");
  12930. script_reportdata(data);
  12931. return 1;
  12932. }
  12933. nd = npc_name2id(name);
  12934. if( nd == NULL )
  12935. {// not found
  12936. script_pushint(st,1);
  12937. return 0;
  12938. }
  12939. // update npc
  12940. if( newname )
  12941. npc_setdisplayname(nd, newname);
  12942. if( size != -1 && size != (int)nd->size )
  12943. nd->size = size;
  12944. else
  12945. size = -1;
  12946. if( class_ != -1 && nd->class_ != class_ )
  12947. npc_setclass(nd, class_);
  12948. else if( size != -1 )
  12949. { // Required to update the visual size
  12950. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  12951. clif_spawn(&nd->bl);
  12952. }
  12953. script_pushint(st,0);
  12954. return SCRIPT_CMD_SUCCESS;
  12955. }
  12956. BUILDIN_FUNC(atoi)
  12957. {
  12958. const char *value;
  12959. value = script_getstr(st,2);
  12960. script_pushint(st,atoi(value));
  12961. return SCRIPT_CMD_SUCCESS;
  12962. }
  12963. // case-insensitive substring search [lordalfa]
  12964. BUILDIN_FUNC(compare)
  12965. {
  12966. const char *message;
  12967. const char *cmpstring;
  12968. message = script_getstr(st,2);
  12969. cmpstring = script_getstr(st,3);
  12970. script_pushint(st,(stristr(message,cmpstring) != NULL));
  12971. return SCRIPT_CMD_SUCCESS;
  12972. }
  12973. BUILDIN_FUNC(strcmp)
  12974. {
  12975. const char *str1;
  12976. const char *str2;
  12977. str1 = script_getstr(st,2);
  12978. str2 = script_getstr(st,3);
  12979. script_pushint(st,strcmp(str1, str2));
  12980. return SCRIPT_CMD_SUCCESS;
  12981. }
  12982. // [zBuffer] List of mathematics commands --->
  12983. BUILDIN_FUNC(sqrt)
  12984. {
  12985. double i, a;
  12986. i = script_getnum(st,2);
  12987. a = sqrt(i);
  12988. script_pushint(st,(int)a);
  12989. return SCRIPT_CMD_SUCCESS;
  12990. }
  12991. BUILDIN_FUNC(pow)
  12992. {
  12993. double i, a, b;
  12994. a = script_getnum(st,2);
  12995. b = script_getnum(st,3);
  12996. i = pow(a,b);
  12997. script_pushint(st,(int)i);
  12998. return SCRIPT_CMD_SUCCESS;
  12999. }
  13000. BUILDIN_FUNC(distance)
  13001. {
  13002. int x0, y0, x1, y1;
  13003. x0 = script_getnum(st,2);
  13004. y0 = script_getnum(st,3);
  13005. x1 = script_getnum(st,4);
  13006. y1 = script_getnum(st,5);
  13007. script_pushint(st,distance_xy(x0,y0,x1,y1));
  13008. return SCRIPT_CMD_SUCCESS;
  13009. }
  13010. // <--- [zBuffer] List of mathematics commands
  13011. BUILDIN_FUNC(md5)
  13012. {
  13013. const char *tmpstr;
  13014. char *md5str;
  13015. tmpstr = script_getstr(st,2);
  13016. md5str = (char *)aMalloc((32+1)*sizeof(char));
  13017. MD5_String(tmpstr, md5str);
  13018. script_pushstr(st, md5str);
  13019. return SCRIPT_CMD_SUCCESS;
  13020. }
  13021. // [zBuffer] List of dynamic var commands --->
  13022. BUILDIN_FUNC(setd)
  13023. {
  13024. TBL_PC *sd=NULL;
  13025. char varname[100];
  13026. const char *buffer;
  13027. int elem;
  13028. buffer = script_getstr(st, 2);
  13029. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  13030. elem = 0;
  13031. if( not_server_variable(*varname) )
  13032. {
  13033. sd = script_rid2sd(st);
  13034. if( sd == NULL )
  13035. {
  13036. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  13037. return 0;
  13038. }
  13039. }
  13040. if( is_string_variable(varname) ) {
  13041. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  13042. } else {
  13043. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  13044. }
  13045. return SCRIPT_CMD_SUCCESS;
  13046. }
  13047. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  13048. {
  13049. int i, j;
  13050. TBL_PC* sd = NULL;
  13051. const char* query;
  13052. struct script_data* data;
  13053. const char* name;
  13054. int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  13055. int num_vars;
  13056. int num_cols;
  13057. // check target variables
  13058. for( i = 3; script_hasdata(st,i); ++i ) {
  13059. data = script_getdata(st, i);
  13060. if( data_isreference(data) ) { // it's a variable
  13061. name = reference_getname(data);
  13062. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  13063. sd = script_rid2sd(st);
  13064. if( sd == NULL ) { // no player attached
  13065. script_reportdata(data);
  13066. st->state = END;
  13067. return 1;
  13068. }
  13069. }
  13070. if( not_array_variable(*name) )
  13071. max_rows = 1;// not an array, limit to one row
  13072. } else {
  13073. ShowError("script:query_sql: not a variable\n");
  13074. script_reportdata(data);
  13075. st->state = END;
  13076. return 1;
  13077. }
  13078. }
  13079. num_vars = i - 3;
  13080. // Execute the query
  13081. query = script_getstr(st,2);
  13082. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  13083. Sql_ShowDebug(handle);
  13084. script_pushint(st, 0);
  13085. return 1;
  13086. }
  13087. if( Sql_NumRows(handle) == 0 ) { // No data received
  13088. Sql_FreeResult(handle);
  13089. script_pushint(st, 0);
  13090. return 0;
  13091. }
  13092. // Count the number of columns to store
  13093. num_cols = Sql_NumColumns(handle);
  13094. if( num_vars < num_cols ) {
  13095. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  13096. script_reportsrc(st);
  13097. } else if( num_vars > num_cols ) {
  13098. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  13099. script_reportsrc(st);
  13100. }
  13101. // Store data
  13102. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  13103. for( j = 0; j < num_vars; ++j ) {
  13104. char* str = NULL;
  13105. if( j < num_cols )
  13106. Sql_GetData(handle, j, &str, NULL);
  13107. data = script_getdata(st, j+3);
  13108. name = reference_getname(data);
  13109. if( is_string_variable(name) )
  13110. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  13111. else
  13112. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  13113. }
  13114. }
  13115. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  13116. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  13117. script_reportsrc(st);
  13118. }
  13119. // Free data
  13120. Sql_FreeResult(handle);
  13121. script_pushint(st, i);
  13122. return SCRIPT_CMD_SUCCESS;
  13123. }
  13124. BUILDIN_FUNC(query_sql) {
  13125. #ifdef BETA_THREAD_TEST
  13126. if( st->state != RERUNLINE ) {
  13127. queryThread_add(st,false);
  13128. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13129. } else
  13130. st->state = RUN;
  13131. return 0;
  13132. #else
  13133. return buildin_query_sql_sub(st, mmysql_handle);
  13134. #endif
  13135. }
  13136. BUILDIN_FUNC(query_logsql) {
  13137. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  13138. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  13139. script_pushint(st,-1);
  13140. return 1;
  13141. }
  13142. #ifdef BETA_THREAD_TEST
  13143. if( st->state != RERUNLINE ) {
  13144. queryThread_add(st,true);
  13145. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13146. } else
  13147. st->state = RUN;
  13148. return 0;
  13149. #else
  13150. return buildin_query_sql_sub(st, logmysql_handle);
  13151. #endif
  13152. }
  13153. //Allows escaping of a given string.
  13154. BUILDIN_FUNC(escape_sql)
  13155. {
  13156. const char *str;
  13157. char *esc_str;
  13158. size_t len;
  13159. str = script_getstr(st,2);
  13160. len = strlen(str);
  13161. esc_str = (char*)aMalloc(len*2+1);
  13162. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  13163. script_pushstr(st, esc_str);
  13164. return SCRIPT_CMD_SUCCESS;
  13165. }
  13166. BUILDIN_FUNC(getd)
  13167. {
  13168. char varname[100];
  13169. const char *buffer;
  13170. int elem;
  13171. buffer = script_getstr(st, 2);
  13172. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  13173. elem = 0;
  13174. // Push the 'pointer' so it's more flexible [Lance]
  13175. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  13176. return SCRIPT_CMD_SUCCESS;
  13177. }
  13178. BUILDIN_FUNC(callshop)
  13179. {
  13180. TBL_PC *sd = NULL;
  13181. struct npc_data *nd;
  13182. const char *shopname;
  13183. int flag = 0;
  13184. sd = script_rid2sd(st);
  13185. if (!sd) {
  13186. script_pushint(st,0);
  13187. return 0;
  13188. }
  13189. shopname = script_getstr(st, 2);
  13190. if( script_hasdata(st,3) )
  13191. flag = script_getnum(st,3);
  13192. nd = npc_name2id(shopname);
  13193. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP) )
  13194. {
  13195. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  13196. script_pushint(st,0);
  13197. return 1;
  13198. }
  13199. if( nd->subtype == SHOP || nd->subtype == ITEMSHOP || nd->subtype == POINTSHOP )
  13200. {
  13201. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  13202. sd->state.callshop = 1;
  13203. switch( flag )
  13204. {
  13205. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  13206. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  13207. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  13208. }
  13209. }
  13210. else
  13211. clif_cashshop_show(sd, nd);
  13212. sd->npc_shopid = nd->bl.id;
  13213. script_pushint(st,1);
  13214. return SCRIPT_CMD_SUCCESS;
  13215. }
  13216. BUILDIN_FUNC(npcshopitem)
  13217. {
  13218. const char* npcname = script_getstr(st, 2);
  13219. struct npc_data* nd = npc_name2id(npcname);
  13220. int n, i;
  13221. int amount;
  13222. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP ) )
  13223. { //Not found.
  13224. script_pushint(st,0);
  13225. return 0;
  13226. }
  13227. // get the count of new entries
  13228. amount = (script_lastdata(st)-2)/2;
  13229. // generate new shop item list
  13230. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  13231. for( n = 0, i = 3; n < amount; n++, i+=2 )
  13232. {
  13233. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13234. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13235. }
  13236. nd->u.shop.count = n;
  13237. script_pushint(st,1);
  13238. return SCRIPT_CMD_SUCCESS;
  13239. }
  13240. BUILDIN_FUNC(npcshopadditem)
  13241. {
  13242. const char* npcname = script_getstr(st,2);
  13243. struct npc_data* nd = npc_name2id(npcname);
  13244. int n, i;
  13245. int amount;
  13246. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP ) )
  13247. { //Not found.
  13248. script_pushint(st,0);
  13249. return 0;
  13250. }
  13251. // get the count of new entries
  13252. amount = (script_lastdata(st)-2)/2;
  13253. // append new items to existing shop item list
  13254. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  13255. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  13256. {
  13257. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13258. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13259. }
  13260. nd->u.shop.count = n;
  13261. script_pushint(st,1);
  13262. return SCRIPT_CMD_SUCCESS;
  13263. }
  13264. BUILDIN_FUNC(npcshopdelitem)
  13265. {
  13266. const char* npcname = script_getstr(st,2);
  13267. struct npc_data* nd = npc_name2id(npcname);
  13268. unsigned int nameid;
  13269. int n, i;
  13270. int amount;
  13271. int size;
  13272. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP ) )
  13273. { //Not found.
  13274. script_pushint(st,0);
  13275. return 0;
  13276. }
  13277. amount = script_lastdata(st)-2;
  13278. size = nd->u.shop.count;
  13279. // remove specified items from the shop item list
  13280. for( i = 3; i < 3 + amount; i++ )
  13281. {
  13282. nameid = script_getnum(st,i);
  13283. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  13284. if( n < size )
  13285. {
  13286. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  13287. size--;
  13288. }
  13289. }
  13290. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  13291. nd->u.shop.count = size;
  13292. script_pushint(st,1);
  13293. return SCRIPT_CMD_SUCCESS;
  13294. }
  13295. //Sets a script to attach to a shop npc.
  13296. BUILDIN_FUNC(npcshopattach)
  13297. {
  13298. const char* npcname = script_getstr(st,2);
  13299. struct npc_data* nd = npc_name2id(npcname);
  13300. int flag = 1;
  13301. if( script_hasdata(st,3) )
  13302. flag = script_getnum(st,3);
  13303. if( !nd || nd->subtype != SHOP )
  13304. { //Not found.
  13305. script_pushint(st,0);
  13306. return 0;
  13307. }
  13308. if (flag)
  13309. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  13310. else
  13311. nd->master_nd = NULL;
  13312. script_pushint(st,1);
  13313. return SCRIPT_CMD_SUCCESS;
  13314. }
  13315. /*==========================================
  13316. * Returns some values of an item [Lupus]
  13317. * Price, Weight, etc...
  13318. setitemscript(itemID,"{new item bonus script}",[n]);
  13319. Where n:
  13320. 0 - script
  13321. 1 - Equip script
  13322. 2 - Unequip script
  13323. *------------------------------------------*/
  13324. BUILDIN_FUNC(setitemscript)
  13325. {
  13326. int item_id,n=0;
  13327. const char *script;
  13328. struct item_data *i_data;
  13329. struct script_code **dstscript;
  13330. item_id = script_getnum(st,2);
  13331. script = script_getstr(st,3);
  13332. if( script_hasdata(st,4) )
  13333. n=script_getnum(st,4);
  13334. i_data = itemdb_exists(item_id);
  13335. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  13336. script_pushint(st,0);
  13337. return 0;
  13338. }
  13339. switch (n) {
  13340. case 2:
  13341. dstscript = &i_data->unequip_script;
  13342. break;
  13343. case 1:
  13344. dstscript = &i_data->equip_script;
  13345. break;
  13346. default:
  13347. dstscript = &i_data->script;
  13348. break;
  13349. }
  13350. if(*dstscript)
  13351. script_free_code(*dstscript);
  13352. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  13353. script_pushint(st,1);
  13354. return SCRIPT_CMD_SUCCESS;
  13355. }
  13356. /*=======================================================
  13357. * Add or Update a mob drop temporarily [Akinari]
  13358. * Original Idea By: [Lupus]
  13359. *
  13360. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  13361. *
  13362. * If given an item the mob already drops, the rate
  13363. * is updated to the new rate. Rate cannot exceed 10000
  13364. * Returns 1 if succeeded (added/updated a mob drop)
  13365. *-------------------------------------------------------*/
  13366. BUILDIN_FUNC(addmonsterdrop)
  13367. {
  13368. struct mob_db *mob;
  13369. struct script_data *data;
  13370. int item_id,rate,i,c = 0;
  13371. data = script_getdata(st, 2);
  13372. get_val(st, data); // Convert into value in case of a variable
  13373. if(data_isstring(data))
  13374. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13375. else
  13376. mob = mob_db(script_getnum(st,2));
  13377. item_id=script_getnum(st,3);
  13378. rate=script_getnum(st,4);
  13379. if(!itemdb_exists(item_id)){
  13380. ShowError("addmonsterdrop: Nonexistant item %d requested.\n", item_id );
  13381. return 1;
  13382. }
  13383. if(mob) { //We got a valid monster, check for available drop slot
  13384. for(i = 0; i < MAX_MOB_DROP; i++) {
  13385. if(mob->dropitem[i].nameid) {
  13386. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  13387. c = i;
  13388. break;
  13389. }
  13390. continue;
  13391. }
  13392. if(!c) //Accept first available slot only
  13393. c = i;
  13394. }
  13395. if(c) { //Fill in the slot with the item and rate
  13396. mob->dropitem[c].nameid = item_id;
  13397. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  13398. script_pushint(st,1);
  13399. } else //No place to put the new drop
  13400. script_pushint(st,0);
  13401. } else {
  13402. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13403. return 1;
  13404. }
  13405. return SCRIPT_CMD_SUCCESS;
  13406. }
  13407. /*=======================================================
  13408. * Delete a mob drop temporarily [Akinari]
  13409. * Original Idea By: [Lupus]
  13410. *
  13411. * delmonsterdrop <mob_id or name>,<item_id>;
  13412. *
  13413. * Returns 1 if succeeded (deleted a mob drop)
  13414. *-------------------------------------------------------*/
  13415. BUILDIN_FUNC(delmonsterdrop)
  13416. {
  13417. struct mob_db *mob;
  13418. struct script_data *data;
  13419. int item_id,i;
  13420. data = script_getdata(st, 2);
  13421. get_val(st, data); // Convert into value in case of a variable
  13422. if(data_isstring(data))
  13423. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13424. else
  13425. mob = mob_db(script_getnum(st,2));
  13426. item_id=script_getnum(st,3);
  13427. if(!itemdb_exists(item_id)){
  13428. ShowError("delmonsterdrop: Nonexistant item %d requested.\n", item_id );
  13429. return 1;
  13430. }
  13431. if(mob) { //We got a valid monster, check for item drop on monster
  13432. for(i = 0; i < MAX_MOB_DROP; i++) {
  13433. if(mob->dropitem[i].nameid == item_id) {
  13434. mob->dropitem[i].nameid = 0;
  13435. mob->dropitem[i].p = 0;
  13436. script_pushint(st,1);
  13437. return 0;
  13438. }
  13439. }
  13440. //No drop on that monster
  13441. script_pushint(st,0);
  13442. } else {
  13443. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13444. return 1;
  13445. }
  13446. return SCRIPT_CMD_SUCCESS;
  13447. }
  13448. /*==========================================
  13449. * Returns some values of a monster [Lupus]
  13450. * Name, Level, race, size, etc...
  13451. getmonsterinfo(monsterID,queryIndex);
  13452. *------------------------------------------*/
  13453. BUILDIN_FUNC(getmonsterinfo)
  13454. {
  13455. struct mob_db *mob;
  13456. int mob_id;
  13457. mob_id = script_getnum(st,2);
  13458. if (!mobdb_checkid(mob_id)) {
  13459. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  13460. if ( !script_getnum(st,3) ) //requested a string
  13461. script_pushconststr(st,"null");
  13462. else
  13463. script_pushint(st,-1);
  13464. return -1;
  13465. }
  13466. mob = mob_db(mob_id);
  13467. switch ( script_getnum(st,3) ) {
  13468. case 0: script_pushstrcopy(st,mob->jname); break;
  13469. case 1: script_pushint(st,mob->lv); break;
  13470. case 2: script_pushint(st,mob->status.max_hp); break;
  13471. case 3: script_pushint(st,mob->base_exp); break;
  13472. case 4: script_pushint(st,mob->job_exp); break;
  13473. case 5: script_pushint(st,mob->status.rhw.atk); break;
  13474. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  13475. case 7: script_pushint(st,mob->status.def); break;
  13476. case 8: script_pushint(st,mob->status.mdef); break;
  13477. case 9: script_pushint(st,mob->status.str); break;
  13478. case 10: script_pushint(st,mob->status.agi); break;
  13479. case 11: script_pushint(st,mob->status.vit); break;
  13480. case 12: script_pushint(st,mob->status.int_); break;
  13481. case 13: script_pushint(st,mob->status.dex); break;
  13482. case 14: script_pushint(st,mob->status.luk); break;
  13483. case 15: script_pushint(st,mob->status.rhw.range); break;
  13484. case 16: script_pushint(st,mob->range2); break;
  13485. case 17: script_pushint(st,mob->range3); break;
  13486. case 18: script_pushint(st,mob->status.size); break;
  13487. case 19: script_pushint(st,mob->status.race); break;
  13488. case 20: script_pushint(st,mob->status.def_ele); break;
  13489. case 21: script_pushint(st,mob->status.mode); break;
  13490. case 22: script_pushint(st,mob->mexp); break;
  13491. default: script_pushint(st,-1); //wrong Index
  13492. }
  13493. return SCRIPT_CMD_SUCCESS;
  13494. }
  13495. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  13496. {
  13497. TBL_PC *sd = NULL;
  13498. if(script_hasdata(st,2))
  13499. sd = map_nick2sd(script_getstr(st,2));
  13500. else
  13501. sd = script_rid2sd(st);
  13502. if(sd)
  13503. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  13504. else
  13505. script_pushint(st,0);
  13506. return SCRIPT_CMD_SUCCESS;
  13507. }
  13508. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  13509. {
  13510. TBL_PC *sd = NULL;
  13511. if(script_hasdata(st,2))
  13512. sd = map_nick2sd(script_getstr(st,2));
  13513. else
  13514. sd = script_rid2sd(st);
  13515. if(sd)
  13516. script_pushint(st,(sd->chatID != 0));
  13517. else
  13518. script_pushint(st,0);
  13519. return SCRIPT_CMD_SUCCESS;
  13520. }
  13521. BUILDIN_FUNC(checkidle)
  13522. {
  13523. TBL_PC *sd = NULL;
  13524. if (script_hasdata(st, 2))
  13525. sd = map_nick2sd(script_getstr(st, 2));
  13526. else
  13527. sd = script_rid2sd(st);
  13528. if (sd)
  13529. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  13530. else
  13531. script_pushint(st, 0);
  13532. return SCRIPT_CMD_SUCCESS;
  13533. }
  13534. BUILDIN_FUNC(searchitem)
  13535. {
  13536. struct script_data* data = script_getdata(st, 2);
  13537. const char *itemname = script_getstr(st,3);
  13538. struct item_data *items[MAX_SEARCH];
  13539. int count;
  13540. char* name;
  13541. int32 start;
  13542. int32 id;
  13543. int32 i;
  13544. TBL_PC* sd = NULL;
  13545. if ((items[0] = itemdb_exists(atoi(itemname))))
  13546. count = 1;
  13547. else {
  13548. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  13549. if (count > MAX_SEARCH) count = MAX_SEARCH;
  13550. }
  13551. if (!count) {
  13552. script_pushint(st, 0);
  13553. return 0;
  13554. }
  13555. if( !data_isreference(data) )
  13556. {
  13557. ShowError("script:searchitem: not a variable\n");
  13558. script_reportdata(data);
  13559. st->state = END;
  13560. return 1;// not a variable
  13561. }
  13562. id = reference_getid(data);
  13563. start = reference_getindex(data);
  13564. name = reference_getname(data);
  13565. if( not_array_variable(*name) )
  13566. {
  13567. ShowError("script:searchitem: illegal scope\n");
  13568. script_reportdata(data);
  13569. st->state = END;
  13570. return 1;// not supported
  13571. }
  13572. if( not_server_variable(*name) )
  13573. {
  13574. sd = script_rid2sd(st);
  13575. if( sd == NULL )
  13576. return 0;// no player attached
  13577. }
  13578. if( is_string_variable(name) )
  13579. {// string array
  13580. ShowError("script:searchitem: not an integer array reference\n");
  13581. script_reportdata(data);
  13582. st->state = END;
  13583. return 1;// not supported
  13584. }
  13585. for( i = 0; i < count; ++start, ++i )
  13586. {// Set array
  13587. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  13588. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  13589. }
  13590. script_pushint(st, count);
  13591. return SCRIPT_CMD_SUCCESS;
  13592. }
  13593. int axtoi(const char *hexStg)
  13594. {
  13595. int n = 0; // position in string
  13596. int16 m = 0; // position in digit[] to shift
  13597. int count; // loop index
  13598. int intValue = 0; // integer value of hex string
  13599. int digit[11]; // hold values to convert
  13600. while (n < 10) {
  13601. if (hexStg[n]=='\0')
  13602. break;
  13603. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  13604. digit[n] = hexStg[n] & 0x0f; //convert to int
  13605. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  13606. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  13607. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  13608. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  13609. else break;
  13610. n++;
  13611. }
  13612. count = n;
  13613. m = n - 1;
  13614. n = 0;
  13615. while(n < count) {
  13616. // digit[n] is value of hex digit at position n
  13617. // (m << 2) is the number of positions to shift
  13618. // OR the bits into return value
  13619. intValue = intValue | (digit[n] << (m << 2));
  13620. m--; // adjust the position to set
  13621. n++; // next digit to process
  13622. }
  13623. return (intValue);
  13624. }
  13625. // [Lance] Hex string to integer converter
  13626. BUILDIN_FUNC(axtoi)
  13627. {
  13628. const char *hex = script_getstr(st,2);
  13629. script_pushint(st,axtoi(hex));
  13630. return SCRIPT_CMD_SUCCESS;
  13631. }
  13632. // [zBuffer] List of player cont commands --->
  13633. BUILDIN_FUNC(rid2name)
  13634. {
  13635. struct block_list *bl = NULL;
  13636. int rid = script_getnum(st,2);
  13637. if((bl = map_id2bl(rid)))
  13638. {
  13639. switch(bl->type) {
  13640. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  13641. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  13642. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  13643. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  13644. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  13645. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  13646. default:
  13647. ShowError("buildin_rid2name: BL type unknown.\n");
  13648. script_pushconststr(st,"");
  13649. break;
  13650. }
  13651. } else {
  13652. ShowError("buildin_rid2name: invalid RID\n");
  13653. script_pushconststr(st,"(null)");
  13654. }
  13655. return SCRIPT_CMD_SUCCESS;
  13656. }
  13657. BUILDIN_FUNC(pcblockmove)
  13658. {
  13659. int id, flag;
  13660. TBL_PC *sd = NULL;
  13661. id = script_getnum(st,2);
  13662. flag = script_getnum(st,3);
  13663. if(id)
  13664. sd = map_id2sd(id);
  13665. else
  13666. sd = script_rid2sd(st);
  13667. if(sd)
  13668. sd->state.blockedmove = flag > 0;
  13669. return SCRIPT_CMD_SUCCESS;
  13670. }
  13671. BUILDIN_FUNC(pcfollow)
  13672. {
  13673. int id, targetid;
  13674. TBL_PC *sd = NULL;
  13675. id = script_getnum(st,2);
  13676. targetid = script_getnum(st,3);
  13677. if(id)
  13678. sd = map_id2sd(id);
  13679. else
  13680. sd = script_rid2sd(st);
  13681. if(sd)
  13682. pc_follow(sd, targetid);
  13683. return SCRIPT_CMD_SUCCESS;
  13684. }
  13685. BUILDIN_FUNC(pcstopfollow)
  13686. {
  13687. int id;
  13688. TBL_PC *sd = NULL;
  13689. id = script_getnum(st,2);
  13690. if(id)
  13691. sd = map_id2sd(id);
  13692. else
  13693. sd = script_rid2sd(st);
  13694. if(sd)
  13695. pc_stop_following(sd);
  13696. return SCRIPT_CMD_SUCCESS;
  13697. }
  13698. // <--- [zBuffer] List of player cont commands
  13699. // [zBuffer] List of mob control commands --->
  13700. /// Makes the unit walk to target position or map
  13701. /// Returns if it was successfull
  13702. ///
  13703. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  13704. /// unitwalk(<unit_id>,<target_id>) -> <bool>
  13705. BUILDIN_FUNC(unitwalk)
  13706. {
  13707. struct block_list* bl;
  13708. bl = map_id2bl(script_getnum(st,2));
  13709. if( bl == NULL )
  13710. script_pushint(st, 0);
  13711. else if( script_hasdata(st,4) ) {
  13712. int x = script_getnum(st,3);
  13713. int y = script_getnum(st,4);
  13714. if( script_pushint(st, unit_can_reach_pos(bl,x,y,0)) )
  13715. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  13716. } else {
  13717. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  13718. if( tbl == NULL ) {
  13719. ShowError("script:unitwalk: bad target destination\n");
  13720. script_pushint(st, 0);
  13721. return 1;
  13722. }
  13723. else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  13724. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  13725. }
  13726. return SCRIPT_CMD_SUCCESS;
  13727. }
  13728. /// Kills the unit
  13729. ///
  13730. /// unitkill <unit_id>;
  13731. BUILDIN_FUNC(unitkill)
  13732. {
  13733. struct block_list* bl = map_id2bl(script_getnum(st,2));
  13734. if( bl != NULL )
  13735. status_kill(bl);
  13736. return SCRIPT_CMD_SUCCESS;
  13737. }
  13738. /// Warps the unit to the target position in the target map
  13739. /// Returns if it was successfull
  13740. ///
  13741. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  13742. BUILDIN_FUNC(unitwarp)
  13743. {
  13744. int unit_id;
  13745. int map;
  13746. short x;
  13747. short y;
  13748. struct block_list* bl;
  13749. const char *mapname;
  13750. unit_id = script_getnum(st,2);
  13751. mapname = script_getstr(st, 3);
  13752. x = (short)script_getnum(st,4);
  13753. y = (short)script_getnum(st,5);
  13754. if (!unit_id) //Warp the script's runner
  13755. bl = map_id2bl(st->rid);
  13756. else
  13757. bl = map_id2bl(unit_id);
  13758. if( strcmp(mapname,"this") == 0 )
  13759. map = bl?bl->m:-1;
  13760. else
  13761. map = map_mapname2mapid(mapname);
  13762. if( map >= 0 && bl != NULL )
  13763. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  13764. else
  13765. script_pushint(st, 0);
  13766. return SCRIPT_CMD_SUCCESS;
  13767. }
  13768. /// Makes the unit attack the target.
  13769. /// If the unit is a player and <action type> is not 0, it does a continuous
  13770. /// attack instead of a single attack.
  13771. /// Returns if the request was successful.
  13772. ///
  13773. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  13774. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  13775. BUILDIN_FUNC(unitattack)
  13776. {
  13777. struct block_list* unit_bl;
  13778. struct block_list* target_bl = NULL;
  13779. struct script_data* data;
  13780. int actiontype = 0;
  13781. // get unit
  13782. unit_bl = map_id2bl(script_getnum(st,2));
  13783. if( unit_bl == NULL ) {
  13784. script_pushint(st, 0);
  13785. return 0;
  13786. }
  13787. data = script_getdata(st, 3);
  13788. get_val(st, data);
  13789. if( data_isstring(data) )
  13790. {
  13791. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  13792. if( sd != NULL )
  13793. target_bl = &sd->bl;
  13794. } else
  13795. target_bl = map_id2bl(conv_num(st, data));
  13796. // request the attack
  13797. if( target_bl == NULL )
  13798. {
  13799. script_pushint(st, 0);
  13800. return 0;
  13801. }
  13802. // get actiontype
  13803. if( script_hasdata(st,4) )
  13804. actiontype = script_getnum(st,4);
  13805. switch( unit_bl->type )
  13806. {
  13807. case BL_PC:
  13808. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  13809. script_pushint(st, 1);
  13810. return 0;
  13811. case BL_MOB:
  13812. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  13813. break;
  13814. case BL_PET:
  13815. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  13816. break;
  13817. default:
  13818. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  13819. script_pushint(st, 0);
  13820. return 1;
  13821. }
  13822. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  13823. return SCRIPT_CMD_SUCCESS;
  13824. }
  13825. /// Makes the unit stop attacking and moving
  13826. ///
  13827. /// unitstop <unit_id>;
  13828. BUILDIN_FUNC(unitstop)
  13829. {
  13830. int unit_id;
  13831. struct block_list* bl;
  13832. unit_id = script_getnum(st,2);
  13833. bl = map_id2bl(unit_id);
  13834. if( bl != NULL )
  13835. {
  13836. unit_stop_attack(bl);
  13837. unit_stop_walking(bl,4);
  13838. if( bl->type == BL_MOB )
  13839. ((TBL_MOB*)bl)->target_id = 0;
  13840. }
  13841. return SCRIPT_CMD_SUCCESS;
  13842. }
  13843. /// Makes the unit say the message
  13844. ///
  13845. /// unittalk <unit_id>,"<message>";
  13846. BUILDIN_FUNC(unittalk)
  13847. {
  13848. int unit_id;
  13849. const char* message;
  13850. struct block_list* bl;
  13851. unit_id = script_getnum(st,2);
  13852. message = script_getstr(st, 3);
  13853. bl = map_id2bl(unit_id);
  13854. if( bl != NULL )
  13855. {
  13856. struct StringBuf sbuf;
  13857. StringBuf_Init(&sbuf);
  13858. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  13859. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  13860. StringBuf_Destroy(&sbuf);
  13861. }
  13862. return SCRIPT_CMD_SUCCESS;
  13863. }
  13864. /// Makes the unit do an emotion
  13865. ///
  13866. /// unitemote <unit_id>,<emotion>;
  13867. ///
  13868. /// @see e_* in const.txt
  13869. BUILDIN_FUNC(unitemote)
  13870. {
  13871. int unit_id;
  13872. int emotion;
  13873. struct block_list* bl;
  13874. unit_id = script_getnum(st,2);
  13875. emotion = script_getnum(st,3);
  13876. bl = map_id2bl(unit_id);
  13877. if( bl != NULL )
  13878. clif_emotion(bl, emotion);
  13879. return SCRIPT_CMD_SUCCESS;
  13880. }
  13881. /// Makes the unit cast the skill on the target or self if no target is specified
  13882. ///
  13883. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  13884. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  13885. BUILDIN_FUNC(unitskilluseid)
  13886. {
  13887. int unit_id;
  13888. uint16 skill_id;
  13889. uint16 skill_lv;
  13890. int target_id;
  13891. struct block_list* bl;
  13892. struct script_data *data;
  13893. unit_id = script_getnum(st,2);
  13894. data = script_getdata(st, 3);
  13895. get_val(st, data); // Convert into value in case of a variable
  13896. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13897. skill_lv = script_getnum(st,4);
  13898. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  13899. bl = map_id2bl(unit_id);
  13900. if( bl != NULL )
  13901. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  13902. return SCRIPT_CMD_SUCCESS;
  13903. }
  13904. /// Makes the unit cast the skill on the target position.
  13905. ///
  13906. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  13907. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  13908. BUILDIN_FUNC(unitskillusepos)
  13909. {
  13910. int unit_id;
  13911. uint16 skill_id;
  13912. uint16 skill_lv;
  13913. int skill_x;
  13914. int skill_y;
  13915. struct block_list* bl;
  13916. struct script_data *data;
  13917. unit_id = script_getnum(st,2);
  13918. data = script_getdata(st, 3);
  13919. get_val(st, data); // Convert into value in case of a variable
  13920. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13921. skill_lv = script_getnum(st,4);
  13922. skill_x = script_getnum(st,5);
  13923. skill_y = script_getnum(st,6);
  13924. bl = map_id2bl(unit_id);
  13925. if( bl != NULL )
  13926. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  13927. return SCRIPT_CMD_SUCCESS;
  13928. }
  13929. // <--- [zBuffer] List of mob control commands
  13930. /// Pauses the execution of the script, detaching the player
  13931. ///
  13932. /// sleep <mili seconds>;
  13933. BUILDIN_FUNC(sleep)
  13934. {
  13935. int ticks;
  13936. ticks = script_getnum(st,2);
  13937. // detach the player
  13938. script_detach_rid(st);
  13939. if( ticks <= 0 )
  13940. {// do nothing
  13941. }
  13942. else if( st->sleep.tick == 0 )
  13943. {// sleep for the target amount of time
  13944. st->state = RERUNLINE;
  13945. st->sleep.tick = ticks;
  13946. }
  13947. else
  13948. {// sleep time is over
  13949. st->state = RUN;
  13950. st->sleep.tick = 0;
  13951. }
  13952. return SCRIPT_CMD_SUCCESS;
  13953. }
  13954. /// Pauses the execution of the script, keeping the player attached
  13955. /// Returns if a player is still attached
  13956. ///
  13957. /// sleep2(<mili secconds>) -> <bool>
  13958. BUILDIN_FUNC(sleep2)
  13959. {
  13960. int ticks;
  13961. ticks = script_getnum(st,2);
  13962. if( ticks <= 0 )
  13963. {// do nothing
  13964. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13965. }
  13966. else if( !st->sleep.tick )
  13967. {// sleep for the target amount of time
  13968. st->state = RERUNLINE;
  13969. st->sleep.tick = ticks;
  13970. }
  13971. else
  13972. {// sleep time is over
  13973. st->state = RUN;
  13974. st->sleep.tick = 0;
  13975. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13976. }
  13977. return SCRIPT_CMD_SUCCESS;
  13978. }
  13979. /// Awakes all the sleep timers of the target npc
  13980. ///
  13981. /// awake "<npc name>";
  13982. BUILDIN_FUNC(awake)
  13983. {
  13984. struct npc_data* nd;
  13985. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  13986. nd = npc_name2id(script_getstr(st, 2));
  13987. if( nd == NULL ) {
  13988. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  13989. return 1;
  13990. }
  13991. while( node )
  13992. {
  13993. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  13994. {// sleep timer for the npc
  13995. struct script_state* tst = (struct script_state*)node->data;
  13996. TBL_PC* sd = map_id2sd(tst->rid);
  13997. if( tst->sleep.timer == INVALID_TIMER )
  13998. {// already awake ???
  13999. node = node->next;
  14000. continue;
  14001. }
  14002. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  14003. {// char not online anymore / another char of the same account is online - Cancel execution
  14004. tst->state = END;
  14005. tst->rid = 0;
  14006. }
  14007. delete_timer(tst->sleep.timer, run_script_timer);
  14008. node = script_erase_sleepdb(node);
  14009. tst->sleep.timer = INVALID_TIMER;
  14010. if(tst->state != RERUNLINE)
  14011. tst->sleep.tick = 0;
  14012. run_script_main(tst);
  14013. }
  14014. else
  14015. {
  14016. node = node->next;
  14017. }
  14018. }
  14019. return SCRIPT_CMD_SUCCESS;
  14020. }
  14021. /// Returns a reference to a variable of the target NPC.
  14022. /// Returns 0 if an error occurs.
  14023. ///
  14024. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  14025. BUILDIN_FUNC(getvariableofnpc)
  14026. {
  14027. struct script_data* data;
  14028. const char* name;
  14029. struct npc_data* nd;
  14030. data = script_getdata(st,2);
  14031. if( !data_isreference(data) )
  14032. {// Not a reference (aka varaible name)
  14033. ShowError("script:getvariableofnpc: not a variable\n");
  14034. script_reportdata(data);
  14035. script_pushnil(st);
  14036. st->state = END;
  14037. return 1;
  14038. }
  14039. name = reference_getname(data);
  14040. if( *name != '.' || name[1] == '@' )
  14041. {// not a npc variable
  14042. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  14043. script_reportdata(data);
  14044. script_pushnil(st);
  14045. st->state = END;
  14046. return 1;
  14047. }
  14048. nd = npc_name2id(script_getstr(st,3));
  14049. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  14050. {// NPC not found or has no script
  14051. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  14052. script_pushnil(st);
  14053. st->state = END;
  14054. return 1;
  14055. }
  14056. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  14057. return SCRIPT_CMD_SUCCESS;
  14058. }
  14059. /// Opens a warp portal.
  14060. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  14061. ///
  14062. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  14063. ///
  14064. /// @author blackhole89
  14065. BUILDIN_FUNC(warpportal)
  14066. {
  14067. int spx;
  14068. int spy;
  14069. unsigned short mapindex;
  14070. int tpx;
  14071. int tpy;
  14072. struct skill_unit_group* group;
  14073. struct block_list* bl;
  14074. bl = map_id2bl(st->oid);
  14075. if( bl == NULL )
  14076. {
  14077. ShowError("script:warpportal: npc is needed\n");
  14078. return 1;
  14079. }
  14080. spx = script_getnum(st,2);
  14081. spy = script_getnum(st,3);
  14082. mapindex = mapindex_name2id(script_getstr(st, 4));
  14083. tpx = script_getnum(st,5);
  14084. tpy = script_getnum(st,6);
  14085. if( mapindex == 0 )
  14086. return 0;// map not found
  14087. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  14088. if( group == NULL )
  14089. return 0;// failed
  14090. group->val1 = (group->val1<<16)|(short)0;
  14091. group->val2 = (tpx<<16) | tpy;
  14092. group->val3 = mapindex;
  14093. return SCRIPT_CMD_SUCCESS;
  14094. }
  14095. BUILDIN_FUNC(openmail)
  14096. {
  14097. TBL_PC* sd;
  14098. sd = script_rid2sd(st);
  14099. if( sd == NULL )
  14100. return 0;
  14101. mail_openmail(sd);
  14102. return SCRIPT_CMD_SUCCESS;
  14103. }
  14104. BUILDIN_FUNC(openauction)
  14105. {
  14106. TBL_PC* sd;
  14107. sd = script_rid2sd(st);
  14108. if( sd == NULL )
  14109. return 0;
  14110. clif_Auction_openwindow(sd);
  14111. return SCRIPT_CMD_SUCCESS;
  14112. }
  14113. /// Retrieves the value of the specified flag of the specified cell.
  14114. ///
  14115. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  14116. ///
  14117. /// @see cell_chk* constants in const.txt for the types
  14118. BUILDIN_FUNC(checkcell)
  14119. {
  14120. int16 m = map_mapname2mapid(script_getstr(st,2));
  14121. int16 x = script_getnum(st,3);
  14122. int16 y = script_getnum(st,4);
  14123. cell_chk type = (cell_chk)script_getnum(st,5);
  14124. script_pushint(st, map_getcell(m, x, y, type));
  14125. return SCRIPT_CMD_SUCCESS;
  14126. }
  14127. /// Modifies flags of cells in the specified area.
  14128. ///
  14129. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  14130. ///
  14131. /// @see cell_* constants in const.txt for the types
  14132. BUILDIN_FUNC(setcell)
  14133. {
  14134. int16 m = map_mapname2mapid(script_getstr(st,2));
  14135. int16 x1 = script_getnum(st,3);
  14136. int16 y1 = script_getnum(st,4);
  14137. int16 x2 = script_getnum(st,5);
  14138. int16 y2 = script_getnum(st,6);
  14139. cell_t type = (cell_t)script_getnum(st,7);
  14140. bool flag = (bool)script_getnum(st,8);
  14141. int x,y;
  14142. if( x1 > x2 ) swap(x1,x2);
  14143. if( y1 > y2 ) swap(y1,y2);
  14144. for( y = y1; y <= y2; ++y )
  14145. for( x = x1; x <= x2; ++x )
  14146. map_setcell(m, x, y, type, flag);
  14147. return SCRIPT_CMD_SUCCESS;
  14148. }
  14149. /*==========================================
  14150. * Mercenary Commands
  14151. *------------------------------------------*/
  14152. BUILDIN_FUNC(mercenary_create)
  14153. {
  14154. struct map_session_data *sd;
  14155. int class_, contract_time;
  14156. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  14157. return 0;
  14158. class_ = script_getnum(st,2);
  14159. if( !merc_class(class_) )
  14160. return 0;
  14161. contract_time = script_getnum(st,3);
  14162. merc_create(sd, class_, contract_time);
  14163. return SCRIPT_CMD_SUCCESS;
  14164. }
  14165. BUILDIN_FUNC(mercenary_heal)
  14166. {
  14167. struct map_session_data *sd = script_rid2sd(st);
  14168. int hp, sp;
  14169. if( sd == NULL || sd->md == NULL )
  14170. return 0;
  14171. hp = script_getnum(st,2);
  14172. sp = script_getnum(st,3);
  14173. status_heal(&sd->md->bl, hp, sp, 0);
  14174. return SCRIPT_CMD_SUCCESS;
  14175. }
  14176. BUILDIN_FUNC(mercenary_sc_start)
  14177. {
  14178. struct map_session_data *sd = script_rid2sd(st);
  14179. enum sc_type type;
  14180. int tick, val1;
  14181. if( sd == NULL || sd->md == NULL )
  14182. return 0;
  14183. type = (sc_type)script_getnum(st,2);
  14184. tick = script_getnum(st,3);
  14185. val1 = script_getnum(st,4);
  14186. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  14187. return SCRIPT_CMD_SUCCESS;
  14188. }
  14189. BUILDIN_FUNC(mercenary_get_calls)
  14190. {
  14191. struct map_session_data *sd = script_rid2sd(st);
  14192. int guild;
  14193. if( sd == NULL )
  14194. return 0;
  14195. guild = script_getnum(st,2);
  14196. switch( guild )
  14197. {
  14198. case ARCH_MERC_GUILD:
  14199. script_pushint(st,sd->status.arch_calls);
  14200. break;
  14201. case SPEAR_MERC_GUILD:
  14202. script_pushint(st,sd->status.spear_calls);
  14203. break;
  14204. case SWORD_MERC_GUILD:
  14205. script_pushint(st,sd->status.sword_calls);
  14206. break;
  14207. default:
  14208. script_pushint(st,0);
  14209. break;
  14210. }
  14211. return SCRIPT_CMD_SUCCESS;
  14212. }
  14213. BUILDIN_FUNC(mercenary_set_calls)
  14214. {
  14215. struct map_session_data *sd = script_rid2sd(st);
  14216. int guild, value, *calls;
  14217. if( sd == NULL )
  14218. return 0;
  14219. guild = script_getnum(st,2);
  14220. value = script_getnum(st,3);
  14221. switch( guild )
  14222. {
  14223. case ARCH_MERC_GUILD:
  14224. calls = &sd->status.arch_calls;
  14225. break;
  14226. case SPEAR_MERC_GUILD:
  14227. calls = &sd->status.spear_calls;
  14228. break;
  14229. case SWORD_MERC_GUILD:
  14230. calls = &sd->status.sword_calls;
  14231. break;
  14232. default:
  14233. return 0; // Invalid Guild
  14234. }
  14235. *calls += value;
  14236. *calls = cap_value(*calls, 0, INT_MAX);
  14237. return SCRIPT_CMD_SUCCESS;
  14238. }
  14239. BUILDIN_FUNC(mercenary_get_faith)
  14240. {
  14241. struct map_session_data *sd = script_rid2sd(st);
  14242. int guild;
  14243. if( sd == NULL )
  14244. return 0;
  14245. guild = script_getnum(st,2);
  14246. switch( guild )
  14247. {
  14248. case ARCH_MERC_GUILD:
  14249. script_pushint(st,sd->status.arch_faith);
  14250. break;
  14251. case SPEAR_MERC_GUILD:
  14252. script_pushint(st,sd->status.spear_faith);
  14253. break;
  14254. case SWORD_MERC_GUILD:
  14255. script_pushint(st,sd->status.sword_faith);
  14256. break;
  14257. default:
  14258. script_pushint(st,0);
  14259. break;
  14260. }
  14261. return SCRIPT_CMD_SUCCESS;
  14262. }
  14263. BUILDIN_FUNC(mercenary_set_faith)
  14264. {
  14265. struct map_session_data *sd = script_rid2sd(st);
  14266. int guild, value, *calls;
  14267. if( sd == NULL )
  14268. return 0;
  14269. guild = script_getnum(st,2);
  14270. value = script_getnum(st,3);
  14271. switch( guild )
  14272. {
  14273. case ARCH_MERC_GUILD:
  14274. calls = &sd->status.arch_faith;
  14275. break;
  14276. case SPEAR_MERC_GUILD:
  14277. calls = &sd->status.spear_faith;
  14278. break;
  14279. case SWORD_MERC_GUILD:
  14280. calls = &sd->status.sword_faith;
  14281. break;
  14282. default:
  14283. return 0; // Invalid Guild
  14284. }
  14285. *calls += value;
  14286. *calls = cap_value(*calls, 0, INT_MAX);
  14287. if( mercenary_get_guild(sd->md) == guild )
  14288. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  14289. return SCRIPT_CMD_SUCCESS;
  14290. }
  14291. /*------------------------------------------
  14292. * Book Reading
  14293. *------------------------------------------*/
  14294. BUILDIN_FUNC(readbook)
  14295. {
  14296. struct map_session_data *sd;
  14297. int book_id, page;
  14298. if( (sd = script_rid2sd(st)) == NULL )
  14299. return 0;
  14300. book_id = script_getnum(st,2);
  14301. page = script_getnum(st,3);
  14302. clif_readbook(sd->fd, book_id, page);
  14303. return SCRIPT_CMD_SUCCESS;
  14304. }
  14305. /******************
  14306. Questlog script commands
  14307. *******************/
  14308. BUILDIN_FUNC(setquest)
  14309. {
  14310. struct map_session_data *sd = script_rid2sd(st);
  14311. nullpo_ret(sd);
  14312. quest_add(sd, script_getnum(st, 2));
  14313. return SCRIPT_CMD_SUCCESS;
  14314. }
  14315. BUILDIN_FUNC(erasequest)
  14316. {
  14317. struct map_session_data *sd = script_rid2sd(st);
  14318. nullpo_ret(sd);
  14319. quest_delete(sd, script_getnum(st, 2));
  14320. return SCRIPT_CMD_SUCCESS;
  14321. }
  14322. BUILDIN_FUNC(completequest)
  14323. {
  14324. struct map_session_data *sd = script_rid2sd(st);
  14325. nullpo_ret(sd);
  14326. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  14327. return SCRIPT_CMD_SUCCESS;
  14328. }
  14329. BUILDIN_FUNC(changequest)
  14330. {
  14331. struct map_session_data *sd = script_rid2sd(st);
  14332. nullpo_ret(sd);
  14333. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  14334. return SCRIPT_CMD_SUCCESS;
  14335. }
  14336. BUILDIN_FUNC(checkquest)
  14337. {
  14338. struct map_session_data *sd = script_rid2sd(st);
  14339. quest_check_type type = HAVEQUEST;
  14340. nullpo_ret(sd);
  14341. if( script_hasdata(st, 3) )
  14342. type = (quest_check_type)script_getnum(st, 3);
  14343. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  14344. return SCRIPT_CMD_SUCCESS;
  14345. }
  14346. BUILDIN_FUNC(isbegin_quest)
  14347. {
  14348. struct map_session_data *sd = script_rid2sd(st);
  14349. int i;
  14350. nullpo_ret(sd);
  14351. i = quest_check(sd, script_getnum(st, 2), (quest_check_type) HAVEQUEST);
  14352. script_pushint(st, i + (i < 1));
  14353. return SCRIPT_CMD_SUCCESS;
  14354. }
  14355. BUILDIN_FUNC(showevent)
  14356. {
  14357. TBL_PC *sd = script_rid2sd(st);
  14358. struct npc_data *nd = map_id2nd(st->oid);
  14359. int state, color;
  14360. if( sd == NULL || nd == NULL )
  14361. return 0;
  14362. state = script_getnum(st, 2);
  14363. color = script_getnum(st, 3);
  14364. if( color < 0 || color > 3 )
  14365. color = 0; // set default color
  14366. clif_quest_show_event(sd, &nd->bl, state, color);
  14367. return SCRIPT_CMD_SUCCESS;
  14368. }
  14369. /*==========================================
  14370. * BattleGround System
  14371. *------------------------------------------*/
  14372. BUILDIN_FUNC(waitingroom2bg)
  14373. {
  14374. struct npc_data *nd;
  14375. struct chat_data *cd;
  14376. const char *map_name, *ev = "", *dev = "";
  14377. int x, y, i, mapindex = 0, bg_id, n;
  14378. struct map_session_data *sd;
  14379. if( script_hasdata(st,7) )
  14380. nd = npc_name2id(script_getstr(st,7));
  14381. else
  14382. nd = (struct npc_data *)map_id2bl(st->oid);
  14383. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  14384. {
  14385. script_pushint(st,0);
  14386. return 0;
  14387. }
  14388. map_name = script_getstr(st,2);
  14389. if( strcmp(map_name,"-") != 0 )
  14390. {
  14391. mapindex = mapindex_name2id(map_name);
  14392. if( mapindex == 0 )
  14393. { // Invalid Map
  14394. script_pushint(st,0);
  14395. return 0;
  14396. }
  14397. }
  14398. x = script_getnum(st,3);
  14399. y = script_getnum(st,4);
  14400. ev = script_getstr(st,5); // Logout Event
  14401. dev = script_getstr(st,6); // Die Event
  14402. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  14403. { // Creation failed
  14404. script_pushint(st,0);
  14405. return 0;
  14406. }
  14407. n = cd->users;
  14408. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  14409. {
  14410. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  14411. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  14412. else
  14413. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  14414. }
  14415. mapreg_setreg(add_str("$@arenamembersnum"), i);
  14416. script_pushint(st,bg_id);
  14417. return SCRIPT_CMD_SUCCESS;
  14418. }
  14419. BUILDIN_FUNC(waitingroom2bg_single)
  14420. {
  14421. const char* map_name;
  14422. struct npc_data *nd;
  14423. struct chat_data *cd;
  14424. struct map_session_data *sd;
  14425. int x, y, mapindex, bg_id;
  14426. bg_id = script_getnum(st,2);
  14427. map_name = script_getstr(st,3);
  14428. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14429. return 0; // Invalid Map
  14430. x = script_getnum(st,4);
  14431. y = script_getnum(st,5);
  14432. nd = npc_name2id(script_getstr(st,6));
  14433. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  14434. return 0;
  14435. if( (sd = cd->usersd[0]) == NULL )
  14436. return 0;
  14437. if( bg_team_join(bg_id, sd) )
  14438. {
  14439. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  14440. script_pushint(st,1);
  14441. }
  14442. else
  14443. script_pushint(st,0);
  14444. return SCRIPT_CMD_SUCCESS;
  14445. }
  14446. BUILDIN_FUNC(bg_team_setxy)
  14447. {
  14448. struct battleground_data *bg;
  14449. int bg_id;
  14450. bg_id = script_getnum(st,2);
  14451. if( (bg = bg_team_search(bg_id)) == NULL )
  14452. return 0;
  14453. bg->x = script_getnum(st,3);
  14454. bg->y = script_getnum(st,4);
  14455. return SCRIPT_CMD_SUCCESS;
  14456. }
  14457. BUILDIN_FUNC(bg_warp)
  14458. {
  14459. int x, y, mapindex, bg_id;
  14460. const char* map_name;
  14461. bg_id = script_getnum(st,2);
  14462. map_name = script_getstr(st,3);
  14463. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14464. return 0; // Invalid Map
  14465. x = script_getnum(st,4);
  14466. y = script_getnum(st,5);
  14467. bg_team_warp(bg_id, mapindex, x, y);
  14468. return SCRIPT_CMD_SUCCESS;
  14469. }
  14470. BUILDIN_FUNC(bg_monster)
  14471. {
  14472. int class_ = 0, x = 0, y = 0, bg_id = 0;
  14473. const char *str,*map, *evt="";
  14474. bg_id = script_getnum(st,2);
  14475. map = script_getstr(st,3);
  14476. x = script_getnum(st,4);
  14477. y = script_getnum(st,5);
  14478. str = script_getstr(st,6);
  14479. class_ = script_getnum(st,7);
  14480. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  14481. check_event(st, evt);
  14482. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  14483. return SCRIPT_CMD_SUCCESS;
  14484. }
  14485. BUILDIN_FUNC(bg_monster_set_team)
  14486. {
  14487. struct mob_data *md;
  14488. struct block_list *mbl;
  14489. int id = script_getnum(st,2),
  14490. bg_id = script_getnum(st,3);
  14491. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  14492. return 0;
  14493. md = (TBL_MOB *)mbl;
  14494. md->bg_id = bg_id;
  14495. mob_stop_attack(md);
  14496. mob_stop_walking(md, 0);
  14497. md->target_id = md->attacked_id = 0;
  14498. clif_charnameack(0, &md->bl);
  14499. return SCRIPT_CMD_SUCCESS;
  14500. }
  14501. BUILDIN_FUNC(bg_leave)
  14502. {
  14503. struct map_session_data *sd = script_rid2sd(st);
  14504. if( sd == NULL || !sd->bg_id )
  14505. return 0;
  14506. bg_team_leave(sd,0);
  14507. return SCRIPT_CMD_SUCCESS;
  14508. }
  14509. BUILDIN_FUNC(bg_destroy)
  14510. {
  14511. int bg_id = script_getnum(st,2);
  14512. bg_team_delete(bg_id);
  14513. return SCRIPT_CMD_SUCCESS;
  14514. }
  14515. BUILDIN_FUNC(bg_getareausers)
  14516. {
  14517. const char *str;
  14518. int16 m, x0, y0, x1, y1;
  14519. int bg_id;
  14520. int i = 0, c = 0;
  14521. struct battleground_data *bg = NULL;
  14522. struct map_session_data *sd;
  14523. bg_id = script_getnum(st,2);
  14524. str = script_getstr(st,3);
  14525. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  14526. {
  14527. script_pushint(st,0);
  14528. return 0;
  14529. }
  14530. x0 = script_getnum(st,4);
  14531. y0 = script_getnum(st,5);
  14532. x1 = script_getnum(st,6);
  14533. y1 = script_getnum(st,7);
  14534. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  14535. {
  14536. if( (sd = bg->members[i].sd) == NULL )
  14537. continue;
  14538. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  14539. continue;
  14540. c++;
  14541. }
  14542. script_pushint(st,c);
  14543. return SCRIPT_CMD_SUCCESS;
  14544. }
  14545. BUILDIN_FUNC(bg_updatescore)
  14546. {
  14547. const char *str;
  14548. int16 m;
  14549. str = script_getstr(st,2);
  14550. if( (m = map_mapname2mapid(str)) < 0 )
  14551. return 0;
  14552. map[m].bgscore_lion = script_getnum(st,3);
  14553. map[m].bgscore_eagle = script_getnum(st,4);
  14554. clif_bg_updatescore(m);
  14555. return SCRIPT_CMD_SUCCESS;
  14556. }
  14557. BUILDIN_FUNC(bg_get_data)
  14558. {
  14559. struct battleground_data *bg;
  14560. int bg_id = script_getnum(st,2),
  14561. type = script_getnum(st,3);
  14562. if( (bg = bg_team_search(bg_id)) == NULL )
  14563. {
  14564. script_pushint(st,0);
  14565. return 0;
  14566. }
  14567. switch( type )
  14568. {
  14569. case 0: script_pushint(st, bg->count); break;
  14570. default:
  14571. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  14572. break;
  14573. }
  14574. return SCRIPT_CMD_SUCCESS;
  14575. }
  14576. /*==========================================
  14577. * Instancing System
  14578. *------------------------------------------*/
  14579. //Returns an Instance ID
  14580. //Checks NPC first, then if player is attached we check party
  14581. int script_instancegetid(struct script_state* st)
  14582. {
  14583. short instance_id = 0;
  14584. struct npc_data *nd;
  14585. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  14586. instance_id = nd->instance_id;
  14587. else {
  14588. struct map_session_data *sd;
  14589. struct party_data *p;
  14590. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14591. instance_id = p->instance_id;
  14592. }
  14593. return instance_id;
  14594. }
  14595. /*==========================================
  14596. * Creates the instance
  14597. * Returns Instance ID if created successfully
  14598. *------------------------------------------*/
  14599. BUILDIN_FUNC(instance_create)
  14600. {
  14601. struct map_session_data *sd;
  14602. if((sd = script_rid2sd(st)) == NULL)
  14603. return -1;
  14604. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  14605. return SCRIPT_CMD_SUCCESS;
  14606. }
  14607. /*==========================================
  14608. * Destroys an instance (unofficial)
  14609. * Officially instances are only destroyed by timeout
  14610. *
  14611. * instance_destroy {<instance_id>};
  14612. *------------------------------------------*/
  14613. BUILDIN_FUNC(instance_destroy)
  14614. {
  14615. short instance_id;
  14616. if( script_hasdata(st,2) )
  14617. instance_id = script_getnum(st,2);
  14618. else
  14619. instance_id = script_instancegetid(st);
  14620. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  14621. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  14622. return 0;
  14623. }
  14624. instance_destroy(instance_id);
  14625. return SCRIPT_CMD_SUCCESS;
  14626. }
  14627. /*==========================================
  14628. * Warps player to instance
  14629. * Results:
  14630. * 0: Success
  14631. * 1: Character not in party
  14632. * 2: Party doesn't have instance
  14633. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  14634. *------------------------------------------*/
  14635. BUILDIN_FUNC(instance_enter)
  14636. {
  14637. struct map_session_data *sd;
  14638. if((sd = script_rid2sd(st)) != NULL)
  14639. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  14640. else
  14641. return 1;
  14642. return SCRIPT_CMD_SUCCESS;
  14643. }
  14644. /*==========================================
  14645. * Returns the name of a duplicated NPC
  14646. *
  14647. * instance_npcname <npc_name>{,<instance_id>};
  14648. * <npc_name> is the full name of an NPC.
  14649. *------------------------------------------*/
  14650. BUILDIN_FUNC(instance_npcname)
  14651. {
  14652. const char *str;
  14653. short instance_id = 0;
  14654. struct npc_data *nd;
  14655. str = script_getstr(st,2);
  14656. if( script_hasdata(st,3) )
  14657. instance_id = script_getnum(st,3);
  14658. else
  14659. instance_id = script_instancegetid(st);
  14660. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  14661. static char npcname[NAME_LENGTH];
  14662. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  14663. script_pushconststr(st,npcname);
  14664. } else {
  14665. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  14666. st->state = END;
  14667. return 1;
  14668. }
  14669. return SCRIPT_CMD_SUCCESS;
  14670. }
  14671. /*==========================================
  14672. * Returns the name of a duplicated map
  14673. *
  14674. * instance_mapname <map_name>{,<instance_id>};
  14675. *------------------------------------------*/
  14676. BUILDIN_FUNC(instance_mapname)
  14677. {
  14678. const char *str;
  14679. int16 m;
  14680. short instance_id = 0;
  14681. str = script_getstr(st,2);
  14682. if( script_hasdata(st,3) )
  14683. instance_id = script_getnum(st,3);
  14684. else
  14685. instance_id = script_instancegetid(st);
  14686. // Check that instance mapname is a valid map
  14687. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  14688. script_pushconststr(st, "");
  14689. else
  14690. script_pushconststr(st, map[m].name);
  14691. return SCRIPT_CMD_SUCCESS;
  14692. }
  14693. /*==========================================
  14694. * Returns an Instance ID
  14695. *------------------------------------------*/
  14696. BUILDIN_FUNC(instance_id)
  14697. {
  14698. short instance_id;
  14699. instance_id = script_instancegetid(st);
  14700. if(!instance_id) {
  14701. //ShowError("script:instance_id: No instance attached to NPC or player");
  14702. script_pushint(st, 0);
  14703. return 1;
  14704. }
  14705. script_pushint(st, instance_id);
  14706. return SCRIPT_CMD_SUCCESS;
  14707. }
  14708. /*==========================================
  14709. * Warps all players inside an instance
  14710. *
  14711. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  14712. *------------------------------------------*/
  14713. BUILDIN_FUNC(instance_warpall)
  14714. {
  14715. struct party_data *p;
  14716. struct map_session_data *pl_sd;
  14717. int16 m, i;
  14718. short instance_id;
  14719. const char *mapn;
  14720. int x, y;
  14721. mapn = script_getstr(st,2);
  14722. x = script_getnum(st,3);
  14723. y = script_getnum(st,4);
  14724. if( script_hasdata(st,5) )
  14725. instance_id = script_getnum(st,5);
  14726. else
  14727. instance_id = script_instancegetid(st);
  14728. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  14729. return 0;
  14730. if( !(p = party_search(instance_data[instance_id].party_id)) )
  14731. return 0;
  14732. for( i = 0; i < MAX_PARTY; i++ )
  14733. 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);
  14734. return SCRIPT_CMD_SUCCESS;
  14735. }
  14736. /*==========================================
  14737. * Broadcasts to all maps inside an instance
  14738. *
  14739. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  14740. * Using -1 for <instance id> will auto-detect the id.
  14741. *------------------------------------------*/
  14742. BUILDIN_FUNC(instance_announce) {
  14743. int instance_id = script_getnum(st,2);
  14744. const char *mes = script_getstr(st,3);
  14745. int flag = script_getnum(st,4);
  14746. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  14747. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  14748. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  14749. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  14750. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  14751. int i;
  14752. if( instance_id == -1 ) {
  14753. instance_id = script_instancegetid(st);
  14754. }
  14755. if( !instance_id && &instance_data[instance_id] != NULL)
  14756. return true;
  14757. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  14758. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  14759. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  14760. return SCRIPT_CMD_SUCCESS;
  14761. }
  14762. /*==========================================
  14763. * instance_check_party [malufett]
  14764. * Values:
  14765. * party_id : Party ID of the invoking character. [Required Parameter]
  14766. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  14767. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  14768. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  14769. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  14770. * Example 2: instance_check_party (getcharid(1),1,1,99);
  14771. *------------------------------------------*/
  14772. BUILDIN_FUNC(instance_check_party)
  14773. {
  14774. struct map_session_data *pl_sd;
  14775. int amount, min, max, i, party_id, c = 0;
  14776. struct party_data *p = NULL;
  14777. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  14778. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  14779. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  14780. if( min < 1 || min > MAX_LEVEL) {
  14781. ShowError("script:check_party: Invalid min level, %d\n", min);
  14782. return 1;
  14783. } else if( max < 1 || max > MAX_LEVEL) {
  14784. ShowError("script:check_party: Invalid max level, %d\n", max);
  14785. return 1;
  14786. }
  14787. if( script_hasdata(st,2) )
  14788. party_id = script_getnum(st,2);
  14789. else return 0;
  14790. if( !(p = party_search(party_id)) ) {
  14791. script_pushint(st, 0); // Returns false if party does not exist.
  14792. return 0;
  14793. }
  14794. for( i = 0; i < MAX_PARTY; i++ )
  14795. if( (pl_sd = p->data[i].sd) )
  14796. if(map_id2bl(pl_sd->bl.id)) {
  14797. if(pl_sd->status.base_level < min) {
  14798. script_pushint(st, 0);
  14799. return 0;
  14800. } else if(pl_sd->status.base_level > max) {
  14801. script_pushint(st, 0);
  14802. return 0;
  14803. }
  14804. c++;
  14805. }
  14806. if(c < amount)
  14807. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  14808. else
  14809. script_pushint(st, 1);
  14810. return SCRIPT_CMD_SUCCESS;
  14811. }
  14812. /*==========================================
  14813. * Custom Fonts
  14814. *------------------------------------------*/
  14815. BUILDIN_FUNC(setfont)
  14816. {
  14817. struct map_session_data *sd = script_rid2sd(st);
  14818. int font = script_getnum(st,2);
  14819. if( sd == NULL )
  14820. return 0;
  14821. if( sd->user_font != font )
  14822. sd->user_font = font;
  14823. else
  14824. sd->user_font = 0;
  14825. clif_font(sd);
  14826. return SCRIPT_CMD_SUCCESS;
  14827. }
  14828. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  14829. {
  14830. TBL_MOB* md = (TBL_MOB*)bl;
  14831. struct block_list *tbl;
  14832. int mobid = va_arg(ap,int);
  14833. uint16 skill_id = va_arg(ap,int);
  14834. uint16 skill_lv = va_arg(ap,int);
  14835. int casttime = va_arg(ap,int);
  14836. int cancel = va_arg(ap,int);
  14837. int emotion = va_arg(ap,int);
  14838. int target = va_arg(ap,int);
  14839. if( md->mob_id != mobid )
  14840. return 0;
  14841. // 0:self, 1:target, 2:master, default:random
  14842. switch( target )
  14843. {
  14844. case 0: tbl = map_id2bl(md->bl.id); break;
  14845. case 1: tbl = map_id2bl(md->target_id); break;
  14846. case 2: tbl = map_id2bl(md->master_id); break;
  14847. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  14848. }
  14849. if( !tbl )
  14850. return 0;
  14851. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  14852. unit_skillcastcancel(bl,0);
  14853. if( skill_get_casttype(skill_id) == CAST_GROUND )
  14854. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  14855. else
  14856. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  14857. clif_emotion(&md->bl, emotion);
  14858. return SCRIPT_CMD_SUCCESS;
  14859. }
  14860. /*==========================================
  14861. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  14862. *------------------------------------------*/
  14863. BUILDIN_FUNC(areamobuseskill)
  14864. {
  14865. struct block_list center;
  14866. struct script_data *data;
  14867. int16 m;
  14868. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  14869. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  14870. ShowError("areamobuseskill: invalid map name.\n");
  14871. return 0;
  14872. }
  14873. center.m = m;
  14874. center.x = script_getnum(st,3);
  14875. center.y = script_getnum(st,4);
  14876. range = script_getnum(st,5);
  14877. mobid = script_getnum(st,6);
  14878. data = script_getdata(st, 7);
  14879. get_val(st, data); // Convert into value in case of a variable
  14880. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  14881. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  14882. skill_lv = battle_config.mob_max_skilllvl;
  14883. casttime = script_getnum(st,9);
  14884. cancel = script_getnum(st,10);
  14885. emotion = script_getnum(st,11);
  14886. target = script_getnum(st,12);
  14887. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  14888. return SCRIPT_CMD_SUCCESS;
  14889. }
  14890. BUILDIN_FUNC(progressbar)
  14891. {
  14892. struct map_session_data * sd = script_rid2sd(st);
  14893. const char * color;
  14894. unsigned int second;
  14895. if( !st || !sd )
  14896. return 0;
  14897. st->state = STOP;
  14898. color = script_getstr(st,2);
  14899. second = script_getnum(st,3);
  14900. sd->progressbar.npc_id = st->oid;
  14901. sd->progressbar.timeout = gettick() + second*1000;
  14902. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  14903. return SCRIPT_CMD_SUCCESS;
  14904. }
  14905. BUILDIN_FUNC(pushpc)
  14906. {
  14907. uint8 dir;
  14908. int cells, dx, dy;
  14909. struct map_session_data* sd;
  14910. if((sd = script_rid2sd(st))==NULL)
  14911. {
  14912. return 0;
  14913. }
  14914. dir = script_getnum(st,2);
  14915. cells = script_getnum(st,3);
  14916. if(dir>7)
  14917. {
  14918. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  14919. script_reportsrc(st);
  14920. dir%= 8; // trim spin-over
  14921. }
  14922. if(!cells)
  14923. {// zero distance
  14924. return 0;
  14925. }
  14926. else if(cells<0)
  14927. {// pushing backwards
  14928. dir = (dir+4)%8; // turn around
  14929. cells = -cells;
  14930. }
  14931. dx = dirx[dir];
  14932. dy = diry[dir];
  14933. unit_blown(&sd->bl, dx, dy, cells, 0);
  14934. return SCRIPT_CMD_SUCCESS;
  14935. }
  14936. /// Invokes buying store preparation window
  14937. /// buyingstore <slots>;
  14938. BUILDIN_FUNC(buyingstore)
  14939. {
  14940. struct map_session_data* sd;
  14941. if( ( sd = script_rid2sd(st) ) == NULL )
  14942. {
  14943. return 0;
  14944. }
  14945. if( npc_isnear(&sd->bl) ) {
  14946. char output[150];
  14947. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  14948. clif_displaymessage(sd->fd, output);
  14949. return 0;
  14950. }
  14951. buyingstore_setup(sd, script_getnum(st,2));
  14952. return SCRIPT_CMD_SUCCESS;
  14953. }
  14954. /// Invokes search store info window
  14955. /// searchstores <uses>,<effect>;
  14956. BUILDIN_FUNC(searchstores)
  14957. {
  14958. unsigned short effect;
  14959. unsigned int uses;
  14960. struct map_session_data* sd;
  14961. if( ( sd = script_rid2sd(st) ) == NULL )
  14962. {
  14963. return 0;
  14964. }
  14965. uses = script_getnum(st,2);
  14966. effect = script_getnum(st,3);
  14967. if( !uses )
  14968. {
  14969. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  14970. return 1;
  14971. }
  14972. if( effect > 1 )
  14973. {
  14974. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  14975. return 1;
  14976. }
  14977. searchstore_open(sd, uses, effect);
  14978. return SCRIPT_CMD_SUCCESS;
  14979. }
  14980. /// Displays a number as large digital clock.
  14981. /// showdigit <value>[,<type>];
  14982. BUILDIN_FUNC(showdigit)
  14983. {
  14984. unsigned int type = 0;
  14985. int value;
  14986. struct map_session_data* sd;
  14987. if( ( sd = script_rid2sd(st) ) == NULL )
  14988. {
  14989. return 0;
  14990. }
  14991. value = script_getnum(st,2);
  14992. if( script_hasdata(st,3) )
  14993. {
  14994. type = script_getnum(st,3);
  14995. if( type > 3 )
  14996. {
  14997. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  14998. return 1;
  14999. }
  15000. }
  15001. clif_showdigit(sd, (unsigned char)type, value);
  15002. return SCRIPT_CMD_SUCCESS;
  15003. }
  15004. /**
  15005. * Rune Knight
  15006. **/
  15007. BUILDIN_FUNC(makerune) {
  15008. TBL_PC* sd;
  15009. if( (sd = script_rid2sd(st)) == NULL )
  15010. return 0;
  15011. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  15012. sd->itemid = script_getnum(st,2);
  15013. return SCRIPT_CMD_SUCCESS;
  15014. }
  15015. /**
  15016. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  15017. **/
  15018. BUILDIN_FUNC(checkdragon) {
  15019. TBL_PC* sd;
  15020. if( (sd = script_rid2sd(st)) == NULL )
  15021. return 0;
  15022. if( pc_isridingdragon(sd) )
  15023. script_pushint(st,1);
  15024. else
  15025. script_pushint(st,0);
  15026. return SCRIPT_CMD_SUCCESS;
  15027. }
  15028. /**
  15029. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  15030. * - Toggles the dragon on a RK if he can mount;
  15031. * @param Color - when not provided uses the green dragon;
  15032. * - 1 : Green Dragon
  15033. * - 2 : Brown Dragon
  15034. * - 3 : Gray Dragon
  15035. * - 4 : Blue Dragon
  15036. * - 5 : Red Dragon
  15037. **/
  15038. BUILDIN_FUNC(setdragon) {
  15039. TBL_PC* sd;
  15040. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  15041. if( (sd = script_rid2sd(st)) == NULL )
  15042. return 0;
  15043. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  15044. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  15045. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  15046. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  15047. script_pushint(st,1);
  15048. } else {//Not mounted; Mount now.
  15049. unsigned int option = OPTION_DRAGON1;
  15050. if( color ) {
  15051. option = ( color == 1 ? OPTION_DRAGON1 :
  15052. color == 2 ? OPTION_DRAGON2 :
  15053. color == 3 ? OPTION_DRAGON3 :
  15054. color == 4 ? OPTION_DRAGON4 :
  15055. color == 5 ? OPTION_DRAGON5 : 0);
  15056. if( !option ) {
  15057. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  15058. option = OPTION_DRAGON1;
  15059. }
  15060. }
  15061. pc_setoption(sd, sd->sc.option|option);
  15062. script_pushint(st,1);
  15063. }
  15064. return SCRIPT_CMD_SUCCESS;
  15065. }
  15066. /**
  15067. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  15068. **/
  15069. BUILDIN_FUNC(ismounting) {
  15070. TBL_PC* sd;
  15071. if( (sd = script_rid2sd(st)) == NULL )
  15072. return 0;
  15073. if( sd->sc.option&OPTION_MOUNTING )
  15074. script_pushint(st,1);
  15075. else
  15076. script_pushint(st,0);
  15077. return SCRIPT_CMD_SUCCESS;
  15078. }
  15079. /**
  15080. * setmounting() returns 1 on success or 0 otherwise
  15081. * - Toggles new mounts on a player when he can mount
  15082. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  15083. * - Will unmount the player is he is already mounting
  15084. **/
  15085. BUILDIN_FUNC(setmounting) {
  15086. TBL_PC* sd;
  15087. if( (sd = script_rid2sd(st)) == NULL )
  15088. return 0;
  15089. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  15090. clif_msgtable(sd->fd, 0x78b);
  15091. script_pushint(st,0);//can't mount with one of these
  15092. } else {
  15093. if( sd->sc.option&OPTION_MOUNTING )
  15094. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  15095. else
  15096. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  15097. script_pushint(st,1);//in both cases, return 1.
  15098. }
  15099. return SCRIPT_CMD_SUCCESS;
  15100. }
  15101. /**
  15102. * Retrieves quantity of arguments provided to callfunc/callsub.
  15103. * getargcount() -> amount of arguments received in a function
  15104. **/
  15105. BUILDIN_FUNC(getargcount) {
  15106. struct script_retinfo* ri;
  15107. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  15108. ShowError("script:getargcount: used out of function or callsub label!\n");
  15109. st->state = END;
  15110. return 1;
  15111. }
  15112. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  15113. script_pushint(st, ri->nargs);
  15114. return SCRIPT_CMD_SUCCESS;
  15115. }
  15116. /**
  15117. * getcharip(<account ID>/<character ID>/<character name>)
  15118. **/
  15119. BUILDIN_FUNC(getcharip)
  15120. {
  15121. struct map_session_data* sd = NULL;
  15122. /* check if a character name is specified */
  15123. if( script_hasdata(st, 2) )
  15124. {
  15125. struct script_data *data;
  15126. data = script_getdata(st, 2);
  15127. get_val(st, data); // Convert into value in case of a variable
  15128. if (data_isstring(data))
  15129. sd = map_nick2sd(script_getstr(st, 2));
  15130. else if (data_isint(data) || script_getnum(st, 2))
  15131. {
  15132. int id = 0;
  15133. id = script_getnum(st, 2);
  15134. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  15135. }
  15136. }
  15137. else
  15138. sd = script_rid2sd(st);
  15139. /* check for sd and IP */
  15140. if (!sd || !session[sd->fd]->client_addr)
  15141. {
  15142. script_pushconststr(st, "");
  15143. return 0;
  15144. }
  15145. /* return the client ip_addr converted for output */
  15146. if (sd && sd->fd && session[sd->fd])
  15147. {
  15148. /* initiliaze */
  15149. const char *ip_addr = NULL;
  15150. uint32 ip;
  15151. /* set ip, ip_addr and convert to ip and push str */
  15152. ip = session[sd->fd]->client_addr;
  15153. ip_addr = ip2str(ip, NULL);
  15154. script_pushstrcopy(st, ip_addr);
  15155. }
  15156. return SCRIPT_CMD_SUCCESS;
  15157. }
  15158. /**
  15159. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  15160. **/
  15161. BUILDIN_FUNC(is_function) {
  15162. const char* str = script_getstr(st,2);
  15163. if( strdb_exists(userfunc_db, str) )
  15164. script_pushint(st,1);
  15165. else
  15166. script_pushint(st,0);
  15167. return SCRIPT_CMD_SUCCESS;
  15168. }
  15169. /**
  15170. * get_revision() -> retrieves the current svn revision (if available)
  15171. **/
  15172. BUILDIN_FUNC(get_revision) {
  15173. const char *svn = get_svn_revision();
  15174. if ( svn[0] != UNKNOWN_VERSION )
  15175. script_pushint(st,atoi(svn));
  15176. else
  15177. script_pushint(st,-1); //unknown
  15178. return SCRIPT_CMD_SUCCESS;
  15179. }
  15180. /* get_hash() -> retrieves the current git hash (if available)*/
  15181. BUILDIN_FUNC(get_githash) {
  15182. const char* git = get_git_hash();
  15183. char buf[CHAT_SIZE_MAX];
  15184. safestrncpy(buf,git,strlen(git)+1);
  15185. if ( git[0] != UNKNOWN_VERSION )
  15186. script_pushstr(st,buf);
  15187. else
  15188. script_pushstr(st,"Unknown"); //unknown
  15189. return SCRIPT_CMD_SUCCESS;
  15190. }
  15191. /**
  15192. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  15193. **/
  15194. BUILDIN_FUNC(freeloop) {
  15195. if( script_getnum(st,2) )
  15196. st->freeloop = 1;
  15197. else
  15198. st->freeloop = 0;
  15199. script_pushint(st, st->freeloop);
  15200. return SCRIPT_CMD_SUCCESS;
  15201. }
  15202. /**
  15203. * @commands (script based)
  15204. **/
  15205. BUILDIN_FUNC(bindatcmd) {
  15206. const char* atcmd;
  15207. const char* eventName;
  15208. int i, level = 0, level2 = 0;
  15209. bool create = false;
  15210. atcmd = script_getstr(st,2);
  15211. eventName = script_getstr(st,3);
  15212. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15213. atcmd++;
  15214. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  15215. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  15216. if( atcmd_binding_count == 0 ) {
  15217. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  15218. create = true;
  15219. } else {
  15220. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  15221. if( i < atcmd_binding_count ) {/* update existent entry */
  15222. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15223. atcmd_binding[i]->level = level;
  15224. atcmd_binding[i]->level2 = level2;
  15225. } else
  15226. create = true;
  15227. }
  15228. if( create ) {
  15229. i = atcmd_binding_count;
  15230. if( atcmd_binding_count++ != 0 )
  15231. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  15232. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  15233. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  15234. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15235. atcmd_binding[i]->level = level;
  15236. atcmd_binding[i]->level2 = level2;
  15237. }
  15238. return SCRIPT_CMD_SUCCESS;
  15239. }
  15240. BUILDIN_FUNC(unbindatcmd) {
  15241. const char* atcmd;
  15242. int i = 0;
  15243. atcmd = script_getstr(st, 2);
  15244. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15245. atcmd++;
  15246. if( atcmd_binding_count == 0 ) {
  15247. script_pushint(st, 0);
  15248. return 0;
  15249. }
  15250. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  15251. if( i < atcmd_binding_count ) {
  15252. int cursor = 0;
  15253. aFree(atcmd_binding[i]);
  15254. atcmd_binding[i] = NULL;
  15255. /* compact the list now that we freed a slot somewhere */
  15256. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  15257. if( atcmd_binding[i] == NULL )
  15258. continue;
  15259. if( cursor != i ) {
  15260. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  15261. }
  15262. cursor++;
  15263. }
  15264. if( (atcmd_binding_count = cursor) == 0 )
  15265. aFree(atcmd_binding);
  15266. script_pushint(st, 1);
  15267. } else
  15268. script_pushint(st, 0);/* not found */
  15269. return SCRIPT_CMD_SUCCESS;
  15270. }
  15271. BUILDIN_FUNC(useatcmd) {
  15272. return atcommand_sub(st,3);
  15273. }
  15274. BUILDIN_FUNC(checkre)
  15275. {
  15276. int num;
  15277. num=script_getnum(st,2);
  15278. switch(num){
  15279. case 0:
  15280. #ifdef RENEWAL
  15281. script_pushint(st, 1);
  15282. #else
  15283. script_pushint(st, 0);
  15284. #endif
  15285. break;
  15286. case 1:
  15287. #ifdef RENEWAL_CAST
  15288. script_pushint(st, 1);
  15289. #else
  15290. script_pushint(st, 0);
  15291. #endif
  15292. break;
  15293. case 2:
  15294. #ifdef RENEWAL_DROP
  15295. script_pushint(st, 1);
  15296. #else
  15297. script_pushint(st, 0);
  15298. #endif
  15299. break;
  15300. case 3:
  15301. #ifdef RENEWAL_EXP
  15302. script_pushint(st, 1);
  15303. #else
  15304. script_pushint(st, 0);
  15305. #endif
  15306. break;
  15307. case 4:
  15308. #ifdef RENEWAL_LVDMG
  15309. script_pushint(st, 1);
  15310. #else
  15311. script_pushint(st, 0);
  15312. #endif
  15313. break;
  15314. case 5:
  15315. #ifdef RENEWAL_ASPD
  15316. script_pushint(st, 1);
  15317. #else
  15318. script_pushint(st, 0);
  15319. #endif
  15320. break;
  15321. default:
  15322. ShowWarning("buildin_checkre: unknown parameter.\n");
  15323. break;
  15324. }
  15325. return SCRIPT_CMD_SUCCESS;
  15326. }
  15327. /* getrandgroupitem <group_id>,<quantity>{,<sub_group>} */
  15328. BUILDIN_FUNC(getrandgroupitem) {
  15329. TBL_PC* sd;
  15330. int i, get_count = 0, flag;
  15331. uint16 nameid, group = script_getnum(st,2), qty = script_getnum(st,3);
  15332. uint8 sub_group = script_getnum(st,4);
  15333. struct item item_tmp;
  15334. if (!( sd = script_rid2sd(st)))
  15335. return SCRIPT_CMD_SUCCESS;
  15336. if (!group) {
  15337. ShowError("getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  15338. return SCRIPT_CMD_FAILURE;
  15339. }
  15340. if ((nameid = itemdb_searchrandomid(group,sub_group)) == UNKNOWN_ITEM_ID) {
  15341. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  15342. }
  15343. memset(&item_tmp,0,sizeof(item_tmp));
  15344. item_tmp.nameid = nameid;
  15345. item_tmp.identify = itemdb_isidentified(nameid);
  15346. if (!qty)
  15347. qty = itemdb_get_randgroupitem_count(group,sub_group,nameid);
  15348. //Check if it's stackable.
  15349. if (!itemdb_isstackable(nameid)) {
  15350. item_tmp.amount = 1;
  15351. get_count = qty;
  15352. }
  15353. else {
  15354. item_tmp.amount = qty;
  15355. get_count = 1;
  15356. }
  15357. for (i = 0; i < get_count; i++) {
  15358. // if not pet egg
  15359. if (!pet_create_egg(sd, nameid)) {
  15360. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  15361. clif_additem(sd,0,0,flag);
  15362. if (pc_candrop(sd,&item_tmp))
  15363. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  15364. }
  15365. }
  15366. }
  15367. return SCRIPT_CMD_SUCCESS;
  15368. }
  15369. /* getgroupitem <group_id>;
  15370. * Gives item(s) to the attached player based on item group contents
  15371. */
  15372. BUILDIN_FUNC(getgroupitem) {
  15373. TBL_PC *sd;
  15374. int group_id = script_getnum(st,2);
  15375. if (!(sd = script_rid2sd(st)))
  15376. return SCRIPT_CMD_SUCCESS;
  15377. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  15378. ShowError("getgroupitem: Invalid group id '%d' specified.",group_id);
  15379. return SCRIPT_CMD_FAILURE;
  15380. }
  15381. return SCRIPT_CMD_SUCCESS;
  15382. }
  15383. /* cleanmap <map_name>;
  15384. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  15385. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  15386. {
  15387. nullpo_ret(bl);
  15388. map_clearflooritem(bl);
  15389. return SCRIPT_CMD_SUCCESS;
  15390. }
  15391. BUILDIN_FUNC(cleanmap)
  15392. {
  15393. const char *map;
  15394. int16 m = -1;
  15395. int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  15396. map = script_getstr(st, 2);
  15397. m = map_mapname2mapid(map);
  15398. if (!m)
  15399. return 1;
  15400. if ((script_lastdata(st) - 2) < 4) {
  15401. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  15402. } else {
  15403. x0 = script_getnum(st, 3);
  15404. y0 = script_getnum(st, 4);
  15405. x1 = script_getnum(st, 5);
  15406. y1 = script_getnum(st, 6);
  15407. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  15408. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  15409. } else {
  15410. ShowError("cleanarea: invalid coordinate defined!\n");
  15411. return 1;
  15412. }
  15413. }
  15414. return SCRIPT_CMD_SUCCESS;
  15415. }
  15416. /* Cast a skill on the attached player.
  15417. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  15418. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  15419. BUILDIN_FUNC(npcskill)
  15420. {
  15421. uint16 skill_id;
  15422. unsigned short skill_level;
  15423. unsigned int stat_point;
  15424. unsigned int npc_level;
  15425. struct npc_data *nd;
  15426. struct map_session_data *sd;
  15427. struct script_data *data;
  15428. data = script_getdata(st, 2);
  15429. get_val(st, data); // Convert into value in case of a variable
  15430. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  15431. skill_level = script_getnum(st, 3);
  15432. stat_point = script_getnum(st, 4);
  15433. npc_level = script_getnum(st, 5);
  15434. sd = script_rid2sd(st);
  15435. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  15436. if (stat_point > battle_config.max_third_parameter) {
  15437. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  15438. return 1;
  15439. }
  15440. if (npc_level > MAX_LEVEL) {
  15441. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  15442. return 1;
  15443. }
  15444. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  15445. return 1;
  15446. }
  15447. nd->level = npc_level;
  15448. nd->stat_point = stat_point;
  15449. if (!nd->status.hp) {
  15450. status_calc_npc(nd, true);
  15451. } else {
  15452. status_calc_npc(nd, false);
  15453. }
  15454. if (skill_get_inf(skill_id)&INF_GROUND_SKILL) {
  15455. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  15456. } else {
  15457. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  15458. }
  15459. return SCRIPT_CMD_SUCCESS;
  15460. }
  15461. /* Consumes an item.
  15462. * consumeitem <item id>;
  15463. * consumeitem "<item name>";
  15464. * @param item: Item ID or name
  15465. */
  15466. BUILDIN_FUNC(consumeitem)
  15467. {
  15468. TBL_PC *sd;
  15469. struct script_data *data;
  15470. struct item_data *item_data;
  15471. nullpo_retr( 1, ( sd = script_rid2sd( st ) ) );
  15472. data = script_getdata( st, 2 );
  15473. get_val( st, data );
  15474. if( data_isstring( data ) ){
  15475. const char *name = conv_str( st, data );
  15476. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  15477. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  15478. return SCRIPT_CMD_FAILURE;
  15479. }
  15480. }else if( data_isint( data ) ){
  15481. int nameid = conv_num( st, data );
  15482. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  15483. ShowError("buildin_consumeitem: Nonexistant item %d requested.\n", nameid );
  15484. return SCRIPT_CMD_FAILURE;
  15485. }
  15486. }else{
  15487. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).", data->type );
  15488. return SCRIPT_CMD_FAILURE;
  15489. }
  15490. run_script( item_data->script, 0, sd->bl.id, 0 );
  15491. return SCRIPT_CMD_SUCCESS;
  15492. }
  15493. /** Makes a player sit/stand.
  15494. * sit {"<character name>"};
  15495. * stand {"<character name>"};
  15496. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  15497. * @param name: Player name that will be invoked
  15498. */
  15499. BUILDIN_FUNC(sit)
  15500. {
  15501. TBL_PC *sd;
  15502. if( script_hasdata(st, 2) )
  15503. sd = map_nick2sd(script_getstr(st, 2));
  15504. else
  15505. sd = script_rid2sd(st);
  15506. if( sd == NULL)
  15507. return SCRIPT_CMD_FAILURE;
  15508. if( !pc_issit(sd) ) {
  15509. unit_stop_walking(&sd->bl, 1|4);
  15510. pc_setsit(sd);
  15511. skill_sit(sd, 1);
  15512. clif_sitting(&sd->bl);
  15513. }
  15514. return SCRIPT_CMD_SUCCESS;
  15515. }
  15516. /** Makes player to stand
  15517. * @param name: Player name that will be set
  15518. */
  15519. BUILDIN_FUNC(stand)
  15520. {
  15521. TBL_PC *sd;
  15522. if( script_hasdata(st, 2) )
  15523. sd = map_nick2sd(script_getstr(st, 2));
  15524. else
  15525. sd = script_rid2sd(st);
  15526. if( sd == NULL)
  15527. return SCRIPT_CMD_FAILURE;
  15528. if( pc_issit(sd) ) {
  15529. pc_setstand(sd);
  15530. skill_sit(sd, 0);
  15531. clif_standing(&sd->bl);
  15532. }
  15533. return SCRIPT_CMD_SUCCESS;
  15534. }
  15535. /** Creates an array of bounded item IDs
  15536. * countbound {<type>};
  15537. * @param type: 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  15538. * @return amt: Amount of items found
  15539. */
  15540. BUILDIN_FUNC(countbound)
  15541. {
  15542. int i, type, j=0, k=0;
  15543. TBL_PC *sd;
  15544. if( (sd = script_rid2sd(st)) == NULL )
  15545. return SCRIPT_CMD_FAILURE;
  15546. type = script_hasdata(st,2)?script_getnum(st,2):0;
  15547. for(i=0;i<MAX_INVENTORY;i++){
  15548. if(sd->status.inventory[i].nameid > 0 && (
  15549. (!type && sd->status.inventory[i].bound > 0) ||
  15550. (type && sd->status.inventory[i].bound == type)
  15551. )) {
  15552. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  15553. k++;
  15554. j += sd->status.inventory[i].amount;
  15555. }
  15556. }
  15557. script_pushint(st,j);
  15558. return SCRIPT_CMD_SUCCESS;
  15559. }
  15560. /** Creates new party
  15561. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  15562. * @param party_name: String of party name that will be created
  15563. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  15564. * @param item_share: 0-Each Take. 1-Party Share
  15565. * @param item_share_type: 0-Each Take. 1-Even Share
  15566. * @return val: Result value
  15567. * -3 - party name is exist
  15568. * -2 - player is in party already
  15569. * -1 - player is not found
  15570. * 0 - unknown error
  15571. * 1 - success, will return party id $@party_create_id
  15572. */
  15573. BUILDIN_FUNC(party_create)
  15574. {
  15575. char party_name[NAME_LENGTH];
  15576. int item1 = 0, item2 = 0;
  15577. TBL_PC *sd;
  15578. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  15579. script_pushint(st,-1);
  15580. return SCRIPT_CMD_FAILURE;
  15581. }
  15582. if( sd->status.party_id ) {
  15583. script_pushint(st,-2);
  15584. return SCRIPT_CMD_FAILURE;
  15585. }
  15586. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  15587. trim(party_name);
  15588. if( party_searchname(party_name) ) {
  15589. script_pushint(st,-3);
  15590. return SCRIPT_CMD_FAILURE;
  15591. }
  15592. if( script_getnum(st,4) )
  15593. item1 = 1;
  15594. if( script_getnum(st,5) )
  15595. item2 = 1;
  15596. party_create_byscript = 1;
  15597. script_pushint(st,party_create(sd,party_name,item1,item2));
  15598. return SCRIPT_CMD_SUCCESS;
  15599. }
  15600. /** Adds player to specified party
  15601. * party_addmember <party id>,<char id>;
  15602. * @param party_id: The party that will be entered by player
  15603. * @param char_id: Char id of player that will be joined to the party
  15604. * @return val: Result value
  15605. * -4 - party is full
  15606. * -3 - party is not found
  15607. * -2 - player is in party already
  15608. * -1 - player is not found
  15609. * 0 - unknown error
  15610. * 1 - success
  15611. */
  15612. BUILDIN_FUNC(party_addmember)
  15613. {
  15614. int party_id = script_getnum(st,2);
  15615. TBL_PC *sd;
  15616. struct party_data *party;
  15617. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  15618. script_pushint(st,-1);
  15619. return SCRIPT_CMD_FAILURE;
  15620. }
  15621. if( sd->status.party_id ) {
  15622. script_pushint(st,-2);
  15623. return SCRIPT_CMD_FAILURE;
  15624. }
  15625. if( !(party = party_search(party_id)) ) {
  15626. script_pushint(st,-3);
  15627. return SCRIPT_CMD_FAILURE;
  15628. }
  15629. if( party->party.count >= MAX_PARTY ) {
  15630. script_pushint(st,-4);
  15631. return SCRIPT_CMD_FAILURE;
  15632. }
  15633. sd->party_invite = party_id;
  15634. script_pushint(st,party_add_member(party_id,sd));
  15635. return SCRIPT_CMD_SUCCESS;
  15636. }
  15637. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  15638. * party_delmember {<char id>,<party_id>};
  15639. * @param: char_id
  15640. * @param: party_id
  15641. * @return val: Result value
  15642. * -3 - player is not in party
  15643. * -2 - party is not found
  15644. * -1 - player is not found
  15645. * 0 - unknown error
  15646. * 1 - success
  15647. */
  15648. BUILDIN_FUNC(party_delmember)
  15649. {
  15650. TBL_PC *sd = NULL;
  15651. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  15652. script_pushint(st,-1);
  15653. return SCRIPT_CMD_FAILURE;
  15654. }
  15655. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  15656. script_pushint(st,party_removemember2(sd,0,0));
  15657. else
  15658. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  15659. return SCRIPT_CMD_SUCCESS;
  15660. }
  15661. /** Changes party leader of specified party (even the leader is offline)
  15662. * party_changeleader <party id>,<char id>;
  15663. * @param party_id: ID of party
  15664. * @param char_id: Char ID of new leader
  15665. * @return val: Result value
  15666. * -4 - player is party leader already
  15667. * -3 - player is not in this party
  15668. * -2 - player is not found
  15669. * -1 - party is not found
  15670. * 0 - unknown error
  15671. * 1 - success */
  15672. BUILDIN_FUNC(party_changeleader)
  15673. {
  15674. int i, party_id = script_getnum(st,2);
  15675. TBL_PC *sd = NULL;
  15676. TBL_PC *tsd = NULL;
  15677. struct party_data *party = NULL;
  15678. if( !(party = party_search(party_id)) ) {
  15679. script_pushint(st,-1);
  15680. return SCRIPT_CMD_FAILURE;
  15681. }
  15682. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  15683. script_pushint(st,-2);
  15684. return SCRIPT_CMD_FAILURE;
  15685. }
  15686. if( tsd->status.party_id != party_id ) {
  15687. script_pushint(st,-3);
  15688. return SCRIPT_CMD_FAILURE;
  15689. }
  15690. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  15691. if( i >= MAX_PARTY ) { //this is should impossible!
  15692. script_pushint(st,0);
  15693. return SCRIPT_CMD_FAILURE;
  15694. }
  15695. if( party->data[i].sd == tsd ) {
  15696. script_pushint(st,-4);
  15697. return SCRIPT_CMD_FAILURE;
  15698. }
  15699. script_pushint(st,party_changeleader(sd,tsd,party));
  15700. return SCRIPT_CMD_SUCCESS;
  15701. }
  15702. /** Changes party option
  15703. * party_changeoption <party id>,<option>,<flag>;
  15704. * @param party_id: ID of party that will be changed
  15705. * @param option: Type of option
  15706. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  15707. */
  15708. BUILDIN_FUNC(party_changeoption)
  15709. {
  15710. struct party_data *party;
  15711. if( !(party = party_search(script_getnum(st,2))) ) {
  15712. script_pushint(st,-1);
  15713. return SCRIPT_CMD_FAILURE;
  15714. }
  15715. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  15716. return SCRIPT_CMD_SUCCESS;
  15717. }
  15718. /** Destroys party with party id.
  15719. * party_destroy <party id>;
  15720. * @param party_id: ID of party that will be destroyed
  15721. */
  15722. BUILDIN_FUNC(party_destroy)
  15723. {
  15724. int i;
  15725. struct party_data *party;
  15726. if( !(party = party_search(script_getnum(st,2))) ) {
  15727. script_pushint(st,0);
  15728. return SCRIPT_CMD_FAILURE;
  15729. }
  15730. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  15731. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  15732. int j;
  15733. for( j = 0; j < MAX_PARTY; j++ ) {
  15734. TBL_PC *sd = party->data[j].sd;
  15735. if(sd)
  15736. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  15737. else if( party->party.member[j].char_id )
  15738. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  15739. }
  15740. party_broken(party->party.party_id);
  15741. script_pushint(st,1);
  15742. }
  15743. else //leader leave = party broken
  15744. script_pushint(st,party_leave(party->data[i].sd));
  15745. return SCRIPT_CMD_SUCCESS;
  15746. }
  15747. /** Checks if a player's client version meets a required version or date.
  15748. * @param type: 0 - check by version number; 1 - check by date
  15749. * @param data: Input
  15750. */
  15751. BUILDIN_FUNC(is_clientver) {
  15752. TBL_PC *sd = NULL;
  15753. int type = script_getnum(st,2);
  15754. int data = script_getnum(st,3);
  15755. int ret = 0;
  15756. if (script_hasdata(st,4))
  15757. sd = map_charid2sd(script_getnum(st,4));
  15758. else
  15759. sd = script_rid2sd(st);
  15760. if (sd == NULL) {
  15761. script_pushint(st,0);
  15762. return SCRIPT_CMD_FAILURE;
  15763. }
  15764. switch(type){
  15765. case 0:
  15766. ret = (sd->packet_ver >= data)?1:0;
  15767. break;
  15768. case 1:
  15769. ret = (sd->packet_ver >= date2version(data))?1:0;
  15770. break;
  15771. }
  15772. script_pushint(st,ret);
  15773. return SCRIPT_CMD_SUCCESS;
  15774. }
  15775. /** Retrieves server definitions
  15776. * @param type: See in const.txt
  15777. */
  15778. BUILDIN_FUNC(getserverdef) {
  15779. int type = script_getnum(st,2);
  15780. switch(type){
  15781. case 0: script_pushint(st,PACKETVER); break;
  15782. case 1: script_pushint(st,MAX_LEVEL); break;
  15783. case 2: script_pushint(st,MAX_STORAGE); break;
  15784. case 3: script_pushint(st,MAX_INVENTORY); break;
  15785. case 4: script_pushint(st,MAX_ZENY); break;
  15786. case 5: script_pushint(st,MAX_PARTY); break;
  15787. case 6: script_pushint(st,MAX_GUILD); break;
  15788. case 7: script_pushint(st,MAX_GUILDLEVEL); break;
  15789. case 8: script_pushint(st,MAX_GUILD_STORAGE); break;
  15790. case 9: script_pushint(st,MAX_BG_MEMBERS); break;
  15791. case 10: script_pushint(st,VIP_SCRIPT); break;
  15792. case 11: script_pushint(st,MIN_STORAGE); break;
  15793. default:
  15794. ShowWarning("buildin_getserverdef: unknown type %d.\n", type);
  15795. script_pushint(st,0);
  15796. break;
  15797. }
  15798. return SCRIPT_CMD_SUCCESS;
  15799. }
  15800. /** Returns various information about a player's VIP status. Need to enable VIP system
  15801. * vip_status <type>,{"<character name>"};
  15802. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  15803. * @param name: Character name (Optional)
  15804. */
  15805. BUILDIN_FUNC(vip_status) {
  15806. #ifdef VIP_ENABLE
  15807. TBL_PC *sd;
  15808. char vip_str[26];
  15809. time_t now = time(NULL);
  15810. int type = script_getnum(st, 2);
  15811. if (script_hasdata(st, 3))
  15812. sd = map_nick2sd(script_getstr(st, 3));
  15813. else
  15814. sd = script_rid2sd(st);
  15815. if (sd == NULL)
  15816. return SCRIPT_CMD_FAILURE;
  15817. switch(type) {
  15818. case 1: // Get VIP status.
  15819. script_pushint(st, pc_isvip(sd));
  15820. break;
  15821. case 2: // Get VIP expire date.
  15822. if (pc_isvip(sd)) {
  15823. time_t viptime = sd->vip.time;
  15824. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  15825. vip_str[25] = '\0';
  15826. script_pushstrcopy(st, vip_str);
  15827. } else
  15828. script_pushint(st, 0);
  15829. break;
  15830. case 3: // Get remaining time.
  15831. if (pc_isvip(sd)) {
  15832. time_t viptime_remain = sd->vip.time - now;
  15833. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  15834. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  15835. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  15836. script_pushstrcopy(st, vip_str);
  15837. } else
  15838. script_pushint(st, 0);
  15839. break;
  15840. }
  15841. #else
  15842. script_pushint(st, 0);
  15843. #endif
  15844. return SCRIPT_CMD_SUCCESS;
  15845. }
  15846. /** Adds or removes VIP time in minutes. Need to enable VIP system
  15847. * vip_time <time in mn>,{"<character name>"};
  15848. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  15849. * @param name: Character name (optional)
  15850. */
  15851. BUILDIN_FUNC(vip_time) {
  15852. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  15853. TBL_PC *sd;
  15854. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  15855. if (script_hasdata(st, 3))
  15856. sd = map_nick2sd(script_getstr(st, 3));
  15857. else
  15858. sd = script_rid2sd(st);
  15859. if (sd == NULL)
  15860. return SCRIPT_CMD_FAILURE;
  15861. chrif_req_login_operation(sd->status.account_id, sd->status.name, 6, viptime, 7, 0);
  15862. #endif
  15863. return SCRIPT_CMD_SUCCESS;
  15864. }
  15865. /** Turns a player into a monster and grants SC attribute effect. [malufett/Hercules]
  15866. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  15867. * @param monster: Monster ID or name
  15868. * @param duration: Transform duration in millisecond (ms)
  15869. * @param sc_type: Type of SC that will be affected during the transformation
  15870. * @param val1: Value for SC
  15871. * @param val2: Value for SC
  15872. * @param val3: Value for SC
  15873. * @param val4: Value for SC
  15874. */
  15875. BUILDIN_FUNC(montransform) {
  15876. TBL_PC *sd;
  15877. enum sc_type type;
  15878. char msg[CHAT_SIZE_MAX];
  15879. int tick, mob_id, val1, val2, val3, val4;
  15880. struct script_data *data;
  15881. if( (sd = script_rid2sd(st)) == NULL )
  15882. return 1;
  15883. data = script_getdata(st, 2);
  15884. get_val(st, data); // Convert into value in case of a variable
  15885. if( data_isstring(data) )
  15886. mob_id = mobdb_searchname(script_getstr(st, 2));
  15887. else
  15888. mob_id = mobdb_checkid(script_getnum(st, 2));
  15889. tick = script_getnum(st, 3);
  15890. type = (sc_type)script_getnum(st, 4);
  15891. val1 = val2 = val3 = val4 = 0;
  15892. if (mob_id == 0) {
  15893. if( data_isstring(data) )
  15894. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  15895. else
  15896. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  15897. return SCRIPT_CMD_FAILURE;
  15898. }
  15899. if (mob_id == MOBID_EMPERIUM) {
  15900. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  15901. return SCRIPT_CMD_FAILURE;
  15902. }
  15903. if (!(type > SC_NONE && type < SC_MAX)) {
  15904. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  15905. return SCRIPT_CMD_FAILURE;
  15906. }
  15907. if (script_hasdata(st, 5))
  15908. val1 = script_getnum(st, 5);
  15909. if (script_hasdata(st, 6))
  15910. val2 = script_getnum(st, 6);
  15911. if (script_hasdata(st, 7))
  15912. val3 = script_getnum(st, 7);
  15913. if (script_hasdata(st, 8))
  15914. val4 = script_getnum(st, 8);
  15915. if (tick != 0) {
  15916. struct mob_db *monster = mob_db(mob_id);
  15917. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  15918. clif_displaymessage(sd->fd, msg_txt(sd,1500)); // Transforming into monster is not allowed in Guild Wars.
  15919. return SCRIPT_CMD_FAILURE;
  15920. }
  15921. if (sd->disguise){
  15922. clif_displaymessage(sd->fd, msg_txt(sd,1498)); // Cannot transform into monster while in disguise.
  15923. return SCRIPT_CMD_FAILURE;
  15924. }
  15925. sprintf(msg, msg_txt(sd,1497), monster->name); // Traaaansformation-!! %s form!!
  15926. clif_disp_overhead(&sd->bl, msg);
  15927. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  15928. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  15929. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  15930. }
  15931. return SCRIPT_CMD_SUCCESS;
  15932. }
  15933. /** [Cydh]
  15934. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  15935. * @param "script code"
  15936. * @param duration
  15937. * @param flag
  15938. * @param icon
  15939. * @param char_id
  15940. **/
  15941. BUILDIN_FUNC(bonus_script) {
  15942. uint8 i, flag = 0;
  15943. int16 icon = SI_BLANK;
  15944. uint32 dur;
  15945. char type = 0;
  15946. TBL_PC* sd;
  15947. const char *script_str = NULL;
  15948. struct script_code *script = NULL;
  15949. if (script_hasdata(st,7))
  15950. sd = map_charid2sd(script_getnum(st,7));
  15951. else
  15952. sd = script_rid2sd(st);
  15953. if (sd == NULL)
  15954. return SCRIPT_CMD_FAILURE;
  15955. script_str = script_getstr(st,2);
  15956. dur = 1000 * abs(script_getnum(st,3));
  15957. FETCH(4,flag);
  15958. FETCH(5,type);
  15959. FETCH(6,icon);
  15960. if (script_str[0] == '\0' || !dur) {
  15961. //ShowWarning("buildin_bonus_script: Invalid script. Skipping...\n");
  15962. return SCRIPT_CMD_FAILURE;
  15963. }
  15964. //Skip duplicate entry
  15965. 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);
  15966. if (i < MAX_PC_BONUS_SCRIPT) {
  15967. //ShowWarning("buildin_bonus_script: Duplicate entry with bonus '%d'. Skipping...\n",i);
  15968. return SCRIPT_CMD_SUCCESS;
  15969. }
  15970. if (!(script = parse_script(script_str,"bonus_script",0,1))) {
  15971. ShowWarning("buildin_bonus_script: Failed to parse script '%s' (cid:%d). Skipping...\n",script_str,sd->status.char_id);
  15972. return SCRIPT_CMD_FAILURE;
  15973. }
  15974. //Find the empty slot
  15975. ARR_FIND(0,MAX_PC_BONUS_SCRIPT,i,!sd->bonus_script[i].script);
  15976. if (i >= MAX_PC_BONUS_SCRIPT) {
  15977. ShowWarning("buildin_itemscript: Maximum script_bonus is reached (cid:%d max: %d). Skipping...\n",sd->status.char_id,MAX_PC_BONUS_SCRIPT);
  15978. return SCRIPT_CMD_SUCCESS;
  15979. }
  15980. //Add the script data
  15981. memcpy(sd->bonus_script[i].script_str,script_str,strlen(script_str)+1);
  15982. sd->bonus_script[i].script = script;
  15983. sd->bonus_script[i].tick = gettick() + dur;
  15984. sd->bonus_script[i].flag = flag;
  15985. sd->bonus_script[i].type = type;
  15986. sd->bonus_script[i].icon = icon;
  15987. if (sd->bonus_script[i].icon != SI_BLANK) //Gives status icon if exist
  15988. clif_status_change(&sd->bl,sd->bonus_script[i].icon,1,dur,1,0,0);
  15989. status_calc_pc(sd,false);
  15990. return SCRIPT_CMD_SUCCESS;
  15991. }
  15992. /** Allows player to use atcommand while talking with NPC
  15993. * @author [Cydh], [Kichi] */
  15994. BUILDIN_FUNC(enable_command) {
  15995. TBL_PC* sd = script_rid2sd(st);
  15996. if (!sd)
  15997. return SCRIPT_CMD_FAILURE;
  15998. sd->state.disable_atcommand_on_npc = false;
  15999. return SCRIPT_CMD_SUCCESS;
  16000. }
  16001. /** Prevents player to use atcommand while talking with NPC
  16002. * @author [Cydh], [Kichi] */
  16003. BUILDIN_FUNC(disable_command) {
  16004. TBL_PC* sd = script_rid2sd(st);
  16005. if (!sd)
  16006. return SCRIPT_CMD_FAILURE;
  16007. sd->state.disable_atcommand_on_npc = true;
  16008. return SCRIPT_CMD_SUCCESS;
  16009. }
  16010. #include "../custom/script.inc"
  16011. // declarations that were supposed to be exported from npc_chat.c
  16012. #ifdef PCRE_SUPPORT
  16013. BUILDIN_FUNC(defpattern);
  16014. BUILDIN_FUNC(activatepset);
  16015. BUILDIN_FUNC(deactivatepset);
  16016. BUILDIN_FUNC(deletepset);
  16017. #endif
  16018. /// script command definitions
  16019. /// for an explanation on args, see add_buildin_func
  16020. struct script_function buildin_func[] = {
  16021. // NPC interaction
  16022. BUILDIN_DEF(mes,"s*"),
  16023. BUILDIN_DEF(next,""),
  16024. BUILDIN_DEF(close,""),
  16025. BUILDIN_DEF(close2,""),
  16026. BUILDIN_DEF(menu,"sl*"),
  16027. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  16028. BUILDIN_DEF(prompt,"s*"),
  16029. //
  16030. BUILDIN_DEF(goto,"l"),
  16031. BUILDIN_DEF(callsub,"l*"),
  16032. BUILDIN_DEF(callfunc,"s*"),
  16033. BUILDIN_DEF(return,"?"),
  16034. BUILDIN_DEF(getarg,"i?"),
  16035. BUILDIN_DEF(jobchange,"i?"),
  16036. BUILDIN_DEF(jobname,"i"),
  16037. BUILDIN_DEF(input,"r??"),
  16038. BUILDIN_DEF(warp,"sii"),
  16039. BUILDIN_DEF(areawarp,"siiiisii??"),
  16040. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  16041. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  16042. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  16043. BUILDIN_DEF(setlook,"ii"),
  16044. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  16045. BUILDIN_DEF(set,"rv"),
  16046. BUILDIN_DEF(setarray,"rv*"),
  16047. BUILDIN_DEF(cleararray,"rvi"),
  16048. BUILDIN_DEF(copyarray,"rri"),
  16049. BUILDIN_DEF(getarraysize,"r"),
  16050. BUILDIN_DEF(deletearray,"r?"),
  16051. BUILDIN_DEF(getelementofarray,"ri"),
  16052. BUILDIN_DEF(getitem,"vi?"),
  16053. BUILDIN_DEF(rentitem,"vi"),
  16054. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  16055. BUILDIN_DEF(getnameditem,"vv"),
  16056. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  16057. BUILDIN_DEF(makeitem,"visii"),
  16058. BUILDIN_DEF(delitem,"vi?"),
  16059. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  16060. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  16061. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  16062. BUILDIN_DEF(cutin,"si"),
  16063. BUILDIN_DEF(viewpoint,"iiiii"),
  16064. BUILDIN_DEF(heal,"ii"),
  16065. BUILDIN_DEF(itemheal,"ii"),
  16066. BUILDIN_DEF(percentheal,"ii"),
  16067. BUILDIN_DEF(rand,"i?"),
  16068. BUILDIN_DEF(countitem,"v"),
  16069. BUILDIN_DEF2(countitem,"countitem2","viiiiiii"),
  16070. BUILDIN_DEF(checkweight,"vi*"),
  16071. BUILDIN_DEF(checkweight2,"rr"),
  16072. BUILDIN_DEF(readparam,"i?"),
  16073. BUILDIN_DEF(getcharid,"i?"),
  16074. BUILDIN_DEF(getnpcid,"i?"),
  16075. BUILDIN_DEF(getpartyname,"i"),
  16076. BUILDIN_DEF(getpartymember,"i?"),
  16077. BUILDIN_DEF(getpartyleader,"i?"),
  16078. BUILDIN_DEF(getguildname,"i"),
  16079. BUILDIN_DEF(getguildmaster,"i"),
  16080. BUILDIN_DEF(getguildmasterid,"i"),
  16081. BUILDIN_DEF(strcharinfo,"i"),
  16082. BUILDIN_DEF(strnpcinfo,"i"),
  16083. BUILDIN_DEF(getequipid,"i"),
  16084. BUILDIN_DEF(getequipname,"i"),
  16085. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  16086. BUILDIN_DEF(repair,"i"), // [Valaris]
  16087. BUILDIN_DEF(repairall,""),
  16088. BUILDIN_DEF(getequipisequiped,"i"),
  16089. BUILDIN_DEF(getequipisenableref,"i"),
  16090. BUILDIN_DEF(getequiprefinerycnt,"i"),
  16091. BUILDIN_DEF(getequipweaponlv,"i"),
  16092. BUILDIN_DEF(getequippercentrefinery,"i"),
  16093. BUILDIN_DEF(successrefitem,"i"),
  16094. BUILDIN_DEF(failedrefitem,"i"),
  16095. BUILDIN_DEF(downrefitem,"i?"),
  16096. BUILDIN_DEF(statusup,"i"),
  16097. BUILDIN_DEF(statusup2,"ii"),
  16098. BUILDIN_DEF(bonus,"iv"),
  16099. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  16100. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  16101. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  16102. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  16103. BUILDIN_DEF(autobonus,"sii??"),
  16104. BUILDIN_DEF(autobonus2,"sii??"),
  16105. BUILDIN_DEF(autobonus3,"siiv?"),
  16106. BUILDIN_DEF(skill,"vi?"),
  16107. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  16108. BUILDIN_DEF(guildskill,"vi"),
  16109. BUILDIN_DEF(getskilllv,"v"),
  16110. BUILDIN_DEF(getgdskilllv,"iv"),
  16111. BUILDIN_DEF(basicskillcheck,""),
  16112. BUILDIN_DEF(getgmlevel,""),
  16113. BUILDIN_DEF(getgroupid,""),
  16114. BUILDIN_DEF(end,""),
  16115. BUILDIN_DEF(checkoption,"i"),
  16116. BUILDIN_DEF(setoption,"i?"),
  16117. BUILDIN_DEF(setcart,"?"),
  16118. BUILDIN_DEF(checkcart,""),
  16119. BUILDIN_DEF(setfalcon,"?"),
  16120. BUILDIN_DEF(checkfalcon,""),
  16121. BUILDIN_DEF(setriding,"?"),
  16122. BUILDIN_DEF(checkriding,""),
  16123. BUILDIN_DEF(checkwug,""),
  16124. BUILDIN_DEF(checkmadogear,""),
  16125. BUILDIN_DEF(setmadogear,"?"),
  16126. BUILDIN_DEF2(savepoint,"save","sii"),
  16127. BUILDIN_DEF(savepoint,"sii"),
  16128. BUILDIN_DEF(gettimetick,"i"),
  16129. BUILDIN_DEF(gettime,"i"),
  16130. BUILDIN_DEF(gettimestr,"si"),
  16131. BUILDIN_DEF(openstorage,""),
  16132. BUILDIN_DEF(guildopenstorage,""),
  16133. BUILDIN_DEF(itemskill,"vi"),
  16134. BUILDIN_DEF(produce,"i"),
  16135. BUILDIN_DEF(cooking,"i"),
  16136. BUILDIN_DEF(monster,"siisii???"),
  16137. BUILDIN_DEF(getmobdrops,"i"),
  16138. BUILDIN_DEF(areamonster,"siiiisii???"),
  16139. BUILDIN_DEF(killmonster,"ss?"),
  16140. BUILDIN_DEF(killmonsterall,"s?"),
  16141. BUILDIN_DEF(clone,"siisi????"),
  16142. BUILDIN_DEF(doevent,"s"),
  16143. BUILDIN_DEF(donpcevent,"s"),
  16144. BUILDIN_DEF(cmdothernpc,"ss"),
  16145. BUILDIN_DEF(addtimer,"is"),
  16146. BUILDIN_DEF(deltimer,"s"),
  16147. BUILDIN_DEF(addtimercount,"is"),
  16148. BUILDIN_DEF(initnpctimer,"??"),
  16149. BUILDIN_DEF(stopnpctimer,"??"),
  16150. BUILDIN_DEF(startnpctimer,"??"),
  16151. BUILDIN_DEF(setnpctimer,"i?"),
  16152. BUILDIN_DEF(getnpctimer,"i?"),
  16153. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  16154. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  16155. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  16156. BUILDIN_DEF(announce,"si?????"),
  16157. BUILDIN_DEF(mapannounce,"ssi?????"),
  16158. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  16159. BUILDIN_DEF(getusers,"i"),
  16160. BUILDIN_DEF(getmapguildusers,"si"),
  16161. BUILDIN_DEF(getmapusers,"s"),
  16162. BUILDIN_DEF(getareausers,"siiii"),
  16163. BUILDIN_DEF(getareadropitem,"siiiiv"),
  16164. BUILDIN_DEF(enablenpc,"s"),
  16165. BUILDIN_DEF(disablenpc,"s"),
  16166. BUILDIN_DEF(hideoffnpc,"s"),
  16167. BUILDIN_DEF(hideonnpc,"s"),
  16168. BUILDIN_DEF(sc_start,"iii???"),
  16169. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  16170. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  16171. BUILDIN_DEF(sc_end,"i?"),
  16172. BUILDIN_DEF(getstatus, "i?"),
  16173. BUILDIN_DEF(getscrate,"ii?"),
  16174. BUILDIN_DEF(debugmes,"s"),
  16175. BUILDIN_DEF2(catchpet,"pet","i"),
  16176. BUILDIN_DEF2(birthpet,"bpet",""),
  16177. BUILDIN_DEF(resetlvl,"i"),
  16178. BUILDIN_DEF(resetstatus,""),
  16179. BUILDIN_DEF(resetskill,""),
  16180. BUILDIN_DEF(skillpointcount,""),
  16181. BUILDIN_DEF(changebase,"i?"),
  16182. BUILDIN_DEF(changesex,""),
  16183. BUILDIN_DEF(waitingroom,"si?????"),
  16184. BUILDIN_DEF(delwaitingroom,"?"),
  16185. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  16186. BUILDIN_DEF(enablewaitingroomevent,"?"),
  16187. BUILDIN_DEF(disablewaitingroomevent,"?"),
  16188. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  16189. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  16190. BUILDIN_DEF(getwaitingroomstate,"i?"),
  16191. BUILDIN_DEF(warpwaitingpc,"sii?"),
  16192. BUILDIN_DEF(attachrid,"i"),
  16193. BUILDIN_DEF(addrid,"i?????"),
  16194. BUILDIN_DEF(detachrid,""),
  16195. BUILDIN_DEF(isloggedin,"i?"),
  16196. BUILDIN_DEF(setmapflagnosave,"ssii"),
  16197. BUILDIN_DEF(getmapflag,"si?"),
  16198. BUILDIN_DEF(setmapflag,"si??"),
  16199. BUILDIN_DEF(removemapflag,"si?"),
  16200. BUILDIN_DEF(pvpon,"s"),
  16201. BUILDIN_DEF(pvpoff,"s"),
  16202. BUILDIN_DEF(gvgon,"s"),
  16203. BUILDIN_DEF(gvgoff,"s"),
  16204. BUILDIN_DEF(emotion,"i??"),
  16205. BUILDIN_DEF(maprespawnguildid,"sii"),
  16206. BUILDIN_DEF(agitstart,""), // <Agit>
  16207. BUILDIN_DEF(agitend,""),
  16208. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  16209. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  16210. BUILDIN_DEF(getcastlename,"s"),
  16211. BUILDIN_DEF(getcastledata,"si"),
  16212. BUILDIN_DEF(setcastledata,"sii"),
  16213. BUILDIN_DEF(requestguildinfo,"i?"),
  16214. BUILDIN_DEF(getequipcardcnt,"i"),
  16215. BUILDIN_DEF(successremovecards,"i"),
  16216. BUILDIN_DEF(failedremovecards,"ii"),
  16217. BUILDIN_DEF(marriage,"s"),
  16218. BUILDIN_DEF2(wedding_effect,"wedding",""),
  16219. BUILDIN_DEF(divorce,""),
  16220. BUILDIN_DEF(ispartneron,""),
  16221. BUILDIN_DEF(getpartnerid,""),
  16222. BUILDIN_DEF(getchildid,""),
  16223. BUILDIN_DEF(getmotherid,""),
  16224. BUILDIN_DEF(getfatherid,""),
  16225. BUILDIN_DEF(warppartner,"sii"),
  16226. BUILDIN_DEF(getitemname,"v"),
  16227. BUILDIN_DEF(getitemslots,"i"),
  16228. BUILDIN_DEF(makepet,"i"),
  16229. BUILDIN_DEF(getexp,"ii"),
  16230. BUILDIN_DEF(getinventorylist,""),
  16231. BUILDIN_DEF(getskilllist,""),
  16232. BUILDIN_DEF(clearitem,""),
  16233. BUILDIN_DEF(classchange,"ii"),
  16234. BUILDIN_DEF(misceffect,"i"),
  16235. BUILDIN_DEF(playBGM,"s"),
  16236. BUILDIN_DEF(playBGMall,"s?????"),
  16237. BUILDIN_DEF(soundeffect,"si"),
  16238. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  16239. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  16240. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  16241. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  16242. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  16243. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  16244. BUILDIN_DEF(petloot,"i"), // [Valaris]
  16245. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  16246. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  16247. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  16248. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  16249. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  16250. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  16251. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  16252. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  16253. BUILDIN_DEF(nude,""), // nude command [Valaris]
  16254. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  16255. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  16256. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  16257. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  16258. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  16259. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  16260. BUILDIN_DEF(mobcount,"ss"),
  16261. BUILDIN_DEF(getlook,"i"),
  16262. BUILDIN_DEF(getsavepoint,"i"),
  16263. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  16264. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  16265. BUILDIN_DEF(npcstop,""), // [Valaris]
  16266. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  16267. BUILDIN_DEF(checkoption1,"i"),
  16268. BUILDIN_DEF(checkoption2,"i"),
  16269. BUILDIN_DEF(guildgetexp,"i"),
  16270. BUILDIN_DEF(guildchangegm,"is"),
  16271. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  16272. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  16273. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  16274. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  16275. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  16276. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  16277. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  16278. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  16279. BUILDIN_DEF(night,""), // sets the server to night time
  16280. BUILDIN_DEF(day,""), // sets the server to day time
  16281. #ifdef PCRE_SUPPORT
  16282. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  16283. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  16284. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  16285. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  16286. #endif
  16287. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  16288. BUILDIN_DEF(getusersname,""),
  16289. BUILDIN_DEF(recovery,"i???"),
  16290. BUILDIN_DEF(getpetinfo,"i"),
  16291. BUILDIN_DEF(gethominfo,"i"),
  16292. BUILDIN_DEF(getmercinfo,"i?"),
  16293. BUILDIN_DEF(checkequipedcard,"i"),
  16294. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  16295. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  16296. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  16297. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  16298. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  16299. BUILDIN_DEF(charat,"si"),
  16300. BUILDIN_DEF(setchar,"ssi"),
  16301. BUILDIN_DEF(insertchar,"ssi"),
  16302. BUILDIN_DEF(delchar,"si"),
  16303. BUILDIN_DEF(strtoupper,"s"),
  16304. BUILDIN_DEF(strtolower,"s"),
  16305. BUILDIN_DEF(charisupper, "si"),
  16306. BUILDIN_DEF(charislower, "si"),
  16307. BUILDIN_DEF(substr,"sii"),
  16308. BUILDIN_DEF(explode, "rss"),
  16309. BUILDIN_DEF(implode, "r?"),
  16310. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  16311. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  16312. BUILDIN_DEF(strpos,"ss?"),
  16313. BUILDIN_DEF(replacestr,"sss??"),
  16314. BUILDIN_DEF(countstr,"ss?"),
  16315. BUILDIN_DEF(setnpcdisplay,"sv??"),
  16316. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  16317. BUILDIN_DEF(strcmp,"ss"),
  16318. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  16319. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  16320. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  16321. // [zBuffer] List of mathematics commands --->
  16322. BUILDIN_DEF(sqrt,"i"),
  16323. BUILDIN_DEF(pow,"ii"),
  16324. BUILDIN_DEF(distance,"iiii"),
  16325. // <--- [zBuffer] List of mathematics commands
  16326. BUILDIN_DEF(md5,"s"),
  16327. // [zBuffer] List of dynamic var commands --->
  16328. BUILDIN_DEF(getd,"s"),
  16329. BUILDIN_DEF(setd,"sv"),
  16330. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  16331. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  16332. BUILDIN_DEF(npcshopadditem,"sii*"),
  16333. BUILDIN_DEF(npcshopdelitem,"si*"),
  16334. BUILDIN_DEF(npcshopattach,"s?"),
  16335. BUILDIN_DEF(equip,"i"),
  16336. BUILDIN_DEF(autoequip,"ii"),
  16337. BUILDIN_DEF(setbattleflag,"si"),
  16338. BUILDIN_DEF(getbattleflag,"s"),
  16339. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  16340. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  16341. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  16342. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  16343. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  16344. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  16345. BUILDIN_DEF(axtoi,"s"),
  16346. BUILDIN_DEF(query_sql,"s*"),
  16347. BUILDIN_DEF(query_logsql,"s*"),
  16348. BUILDIN_DEF(escape_sql,"v"),
  16349. BUILDIN_DEF(atoi,"s"),
  16350. // [zBuffer] List of player cont commands --->
  16351. BUILDIN_DEF(rid2name,"i"),
  16352. BUILDIN_DEF(pcfollow,"ii"),
  16353. BUILDIN_DEF(pcstopfollow,"i"),
  16354. BUILDIN_DEF(pcblockmove,"ii"),
  16355. // <--- [zBuffer] List of player cont commands
  16356. // [zBuffer] List of mob control commands --->
  16357. BUILDIN_DEF(unitwalk,"ii?"),
  16358. BUILDIN_DEF(unitkill,"i"),
  16359. BUILDIN_DEF(unitwarp,"isii"),
  16360. BUILDIN_DEF(unitattack,"iv?"),
  16361. BUILDIN_DEF(unitstop,"i"),
  16362. BUILDIN_DEF(unittalk,"is"),
  16363. BUILDIN_DEF(unitemote,"ii"),
  16364. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  16365. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  16366. // <--- [zBuffer] List of mob control commands
  16367. BUILDIN_DEF(sleep,"i"),
  16368. BUILDIN_DEF(sleep2,"i"),
  16369. BUILDIN_DEF(awake,"s"),
  16370. BUILDIN_DEF(getvariableofnpc,"rs"),
  16371. BUILDIN_DEF(warpportal,"iisii"),
  16372. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  16373. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  16374. BUILDIN_DEF(morphembryo,""),
  16375. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  16376. BUILDIN_DEF(checkhomcall,""),
  16377. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  16378. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  16379. BUILDIN_DEF(checkvending,"?"),
  16380. BUILDIN_DEF(checkchatting,"?"),
  16381. BUILDIN_DEF(checkidle,"?"),
  16382. BUILDIN_DEF(openmail,""),
  16383. BUILDIN_DEF(openauction,""),
  16384. BUILDIN_DEF(checkcell,"siii"),
  16385. BUILDIN_DEF(setcell,"siiiiii"),
  16386. BUILDIN_DEF(setwall,"siiiiis"),
  16387. BUILDIN_DEF(delwall,"s"),
  16388. BUILDIN_DEF(searchitem,"rs"),
  16389. BUILDIN_DEF(mercenary_create,"ii"),
  16390. BUILDIN_DEF(mercenary_heal,"ii"),
  16391. BUILDIN_DEF(mercenary_sc_start,"iii"),
  16392. BUILDIN_DEF(mercenary_get_calls,"i"),
  16393. BUILDIN_DEF(mercenary_get_faith,"i"),
  16394. BUILDIN_DEF(mercenary_set_calls,"ii"),
  16395. BUILDIN_DEF(mercenary_set_faith,"ii"),
  16396. BUILDIN_DEF(readbook,"ii"),
  16397. BUILDIN_DEF(setfont,"i"),
  16398. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  16399. BUILDIN_DEF(progressbar,"si"),
  16400. BUILDIN_DEF(pushpc,"ii"),
  16401. BUILDIN_DEF(buyingstore,"i"),
  16402. BUILDIN_DEF(searchstores,"ii"),
  16403. BUILDIN_DEF(showdigit,"i?"),
  16404. // WoE SE
  16405. BUILDIN_DEF(agitstart2,""),
  16406. BUILDIN_DEF(agitend2,""),
  16407. BUILDIN_DEF(agitcheck2,""),
  16408. // BattleGround
  16409. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  16410. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  16411. BUILDIN_DEF(bg_team_setxy,"iii"),
  16412. BUILDIN_DEF(bg_warp,"isii"),
  16413. BUILDIN_DEF(bg_monster,"isiisi?"),
  16414. BUILDIN_DEF(bg_monster_set_team,"ii"),
  16415. BUILDIN_DEF(bg_leave,""),
  16416. BUILDIN_DEF(bg_destroy,"i"),
  16417. BUILDIN_DEF(areapercentheal,"siiiiii"),
  16418. BUILDIN_DEF(bg_get_data,"ii"),
  16419. BUILDIN_DEF(bg_getareausers,"isiiii"),
  16420. BUILDIN_DEF(bg_updatescore,"sii"),
  16421. // Instancing
  16422. BUILDIN_DEF(instance_create,"s"),
  16423. BUILDIN_DEF(instance_destroy,"?"),
  16424. BUILDIN_DEF(instance_id,""),
  16425. BUILDIN_DEF(instance_enter,"s"),
  16426. BUILDIN_DEF(instance_npcname,"s?"),
  16427. BUILDIN_DEF(instance_mapname,"s?"),
  16428. BUILDIN_DEF(instance_warpall,"sii?"),
  16429. BUILDIN_DEF(instance_announce,"isi?????"),
  16430. BUILDIN_DEF(instance_check_party,"i???"),
  16431. /**
  16432. * 3rd-related
  16433. **/
  16434. BUILDIN_DEF(makerune,"i"),
  16435. BUILDIN_DEF(checkdragon,""),//[Ind]
  16436. BUILDIN_DEF(setdragon,"?"),//[Ind]
  16437. BUILDIN_DEF(ismounting,""),//[Ind]
  16438. BUILDIN_DEF(setmounting,""),//[Ind]
  16439. BUILDIN_DEF(checkre,"i"),
  16440. /**
  16441. * rAthena and beyond!
  16442. **/
  16443. BUILDIN_DEF(getargcount,""),
  16444. BUILDIN_DEF(getcharip,"?"),
  16445. BUILDIN_DEF(is_function,"s"),
  16446. BUILDIN_DEF(get_revision,""),
  16447. BUILDIN_DEF(get_githash,""),
  16448. BUILDIN_DEF(freeloop,"i"),
  16449. BUILDIN_DEF(getrandgroupitem,"ii?"),
  16450. BUILDIN_DEF(cleanmap,"s"),
  16451. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  16452. BUILDIN_DEF(npcskill,"viii"),
  16453. BUILDIN_DEF(consumeitem,"v"),
  16454. BUILDIN_DEF(delequip,"i"),
  16455. BUILDIN_DEF(breakequip,"i"),
  16456. BUILDIN_DEF(sit,"?"),
  16457. BUILDIN_DEF(stand,"?"),
  16458. //@commands (script based)
  16459. BUILDIN_DEF(bindatcmd, "ss??"),
  16460. BUILDIN_DEF(unbindatcmd, "s"),
  16461. BUILDIN_DEF(useatcmd, "s"),
  16462. //Quest Log System [Inkfish]
  16463. BUILDIN_DEF(setquest, "i"),
  16464. BUILDIN_DEF(erasequest, "i"),
  16465. BUILDIN_DEF(completequest, "i"),
  16466. BUILDIN_DEF(checkquest, "i?"),
  16467. BUILDIN_DEF(isbegin_quest,"i"),
  16468. BUILDIN_DEF(changequest, "ii"),
  16469. BUILDIN_DEF(showevent, "ii"),
  16470. //Bound items [Xantara] & [Akinari]
  16471. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  16472. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  16473. BUILDIN_DEF(countbound, "?"),
  16474. // Party related
  16475. BUILDIN_DEF(party_create,"s???"),
  16476. BUILDIN_DEF(party_addmember,"ii"),
  16477. BUILDIN_DEF(party_delmember,"??"),
  16478. BUILDIN_DEF(party_changeleader,"ii"),
  16479. BUILDIN_DEF(party_changeoption,"iii"),
  16480. BUILDIN_DEF(party_destroy,"i"),
  16481. BUILDIN_DEF(is_clientver,"ii?"),
  16482. BUILDIN_DEF(getserverdef,"i"),
  16483. BUILDIN_DEF2(montransform, "transform", "vii????"), // Monster Transform [malufett/Hercules]
  16484. BUILDIN_DEF(vip_status,"i?"),
  16485. BUILDIN_DEF(vip_time,"i?"),
  16486. BUILDIN_DEF(bonus_script,"si????"),
  16487. BUILDIN_DEF(getgroupitem,"i"),
  16488. BUILDIN_DEF(enable_command,""),
  16489. BUILDIN_DEF(disable_command,""),
  16490. #include "../custom/script_def.inc"
  16491. {NULL,NULL,NULL},
  16492. };