script.c 457 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470
  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_BLOCK_SIZE 512
  158. enum { LABEL_NEXTLINE=1,LABEL_START };
  159. /// temporary buffer for passing around compiled bytecode
  160. /// @see add_scriptb, set_label, parse_script
  161. static unsigned char* script_buf = NULL;
  162. static int script_pos = 0, script_size = 0;
  163. static inline int GETVALUE(const unsigned char* buf, int i)
  164. {
  165. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  166. }
  167. static inline void SETVALUE(unsigned char* buf, int i, int n)
  168. {
  169. buf[i] = GetByte(n, 0);
  170. buf[i+1] = GetByte(n, 1);
  171. buf[i+2] = GetByte(n, 2);
  172. }
  173. // String buffer structures.
  174. // str_data stores string information
  175. static struct str_data_struct {
  176. enum c_op type;
  177. int str;
  178. int backpatch;
  179. int label;
  180. int (*func)(struct script_state *st);
  181. int val;
  182. int next;
  183. } *str_data = NULL;
  184. static int str_data_size = 0; // size of the data
  185. static int str_num = LABEL_START; // next id to be assigned
  186. // str_buf holds the strings themselves
  187. static char *str_buf;
  188. static int str_size = 0; // size of the buffer
  189. static int str_pos = 0; // next position to be assigned
  190. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  191. #define SCRIPT_HASH_SIZE 1021
  192. int str_hash[SCRIPT_HASH_SIZE];
  193. // Specifies which string hashing method to use
  194. //#define SCRIPT_HASH_DJB2
  195. //#define SCRIPT_HASH_SDBM
  196. #define SCRIPT_HASH_ELF
  197. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  198. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  199. static int parse_options=0;
  200. DBMap* script_get_label_db(void){ return scriptlabel_db; }
  201. DBMap* script_get_userfunc_db(void){ return userfunc_db; }
  202. // important buildin function references for usage in scripts
  203. static int buildin_set_ref = 0;
  204. static int buildin_callsub_ref = 0;
  205. static int buildin_callfunc_ref = 0;
  206. static int buildin_getelementofarray_ref = 0;
  207. // Caches compiled autoscript item code.
  208. // Note: This is not cleared when reloading itemdb.
  209. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  210. struct Script_Config script_config = {
  211. 1, // warn_func_mismatch_argtypes
  212. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  213. 0, INT_MAX, // input_min_value/input_max_value
  214. "OnPCDieEvent", //die_event_name
  215. "OnPCKillEvent", //kill_pc_event_name
  216. "OnNPCKillEvent", //kill_mob_event_name
  217. "OnPCLoginEvent", //login_event_name
  218. "OnPCLogoutEvent", //logout_event_name
  219. "OnPCLoadMapEvent", //loadmap_event_name
  220. "OnPCBaseLvUpEvent", //baselvup_event_name
  221. "OnPCJobLvUpEvent", //joblvup_event_name
  222. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  223. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  224. };
  225. static jmp_buf error_jump;
  226. static char* error_msg;
  227. static const char* error_pos;
  228. static int error_report; // if the error should produce output
  229. // Used by disp_warning_message
  230. static const char* parser_current_src;
  231. static const char* parser_current_file;
  232. static int parser_current_line;
  233. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  234. // [Eoe / jA 1080, 1081, 1094, 1164]
  235. enum curly_type {
  236. TYPE_NULL = 0,
  237. TYPE_IF,
  238. TYPE_SWITCH,
  239. TYPE_WHILE,
  240. TYPE_FOR,
  241. TYPE_DO,
  242. TYPE_USERFUNC,
  243. TYPE_ARGLIST // function argument list
  244. };
  245. enum e_arglist
  246. {
  247. ARGLIST_UNDEFINED = 0,
  248. ARGLIST_NO_PAREN = 1,
  249. ARGLIST_PAREN = 2,
  250. };
  251. static struct {
  252. struct {
  253. enum curly_type type;
  254. int index;
  255. int count;
  256. int flag;
  257. struct linkdb_node *case_label;
  258. } curly[256]; // Information right parenthesis
  259. int curly_count; // The number of right brackets
  260. int index; // Number of the syntax used in the script
  261. } syntax;
  262. const char* parse_curly_close(const char* p);
  263. const char* parse_syntax_close(const char* p);
  264. const char* parse_syntax_close_sub(const char* p,int* flag);
  265. const char* parse_syntax(const char* p);
  266. static int parse_syntax_for_flag = 0;
  267. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  268. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  269. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  270. int potion_target=0;
  271. c_op get_com(unsigned char *script,int *pos);
  272. int get_num(unsigned char *script,int *pos);
  273. typedef struct script_function {
  274. int (*func)(struct script_state *st);
  275. const char *name;
  276. const char *arg;
  277. } script_function;
  278. extern script_function buildin_func[];
  279. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  280. #ifdef BETA_THREAD_TEST
  281. /**
  282. * MySQL Query Slave
  283. **/
  284. static SPIN_LOCK queryThreadLock;
  285. static rAthread queryThread = NULL;
  286. static ramutex queryThreadMutex = NULL;
  287. static racond queryThreadCond = NULL;
  288. static volatile int32 queryThreadTerminate = 0;
  289. struct queryThreadEntry {
  290. bool ok;
  291. bool type; /* main db or log db? */
  292. struct script_state *st;
  293. };
  294. /* Ladies and Gentleman the Manager! */
  295. struct {
  296. struct queryThreadEntry **entry;/* array of structs */
  297. int count;
  298. int timer;/* used to receive processed entries */
  299. } queryThreadData;
  300. #endif
  301. /*==========================================
  302. * (Only those needed) local declaration prototype
  303. *------------------------------------------*/
  304. const char* parse_subexpr(const char* p,int limit);
  305. int run_func(struct script_state *st);
  306. int script_instancegetid(struct script_state *st);
  307. enum {
  308. MF_NOMEMO, //0
  309. MF_NOTELEPORT,
  310. MF_NOSAVE,
  311. MF_NOBRANCH,
  312. MF_NOPENALTY,
  313. MF_NOZENYPENALTY,
  314. MF_PVP,
  315. MF_PVP_NOPARTY,
  316. MF_PVP_NOGUILD,
  317. MF_GVG,
  318. MF_GVG_NOPARTY, //10
  319. MF_NOTRADE,
  320. MF_NOSKILL,
  321. MF_NOWARP,
  322. MF_PARTYLOCK,
  323. MF_NOICEWALL,
  324. MF_SNOW,
  325. MF_FOG,
  326. MF_SAKURA,
  327. MF_LEAVES,
  328. /**
  329. * No longer available, keeping here just in case it's back someday. [Ind]
  330. **/
  331. //MF_RAIN, //20
  332. // 21 free
  333. MF_NOGO = 22,
  334. MF_CLOUDS,
  335. MF_CLOUDS2,
  336. MF_FIREWORKS,
  337. MF_GVG_CASTLE,
  338. MF_GVG_DUNGEON,
  339. MF_NIGHTENABLED,
  340. MF_NOBASEEXP,
  341. MF_NOJOBEXP, //30
  342. MF_NOMOBLOOT,
  343. MF_NOMVPLOOT,
  344. MF_NORETURN,
  345. MF_NOWARPTO,
  346. MF_NIGHTMAREDROP,
  347. MF_RESTRICTED,
  348. MF_NOCOMMAND,
  349. MF_NODROP,
  350. MF_JEXP,
  351. MF_BEXP, //40
  352. MF_NOVENDING,
  353. MF_LOADEVENT,
  354. MF_NOCHAT,
  355. MF_NOEXPPENALTY,
  356. MF_GUILDLOCK,
  357. MF_TOWN,
  358. MF_AUTOTRADE,
  359. MF_ALLOWKS,
  360. MF_MONSTER_NOTELEPORT,
  361. MF_PVP_NOCALCRANK, //50
  362. MF_BATTLEGROUND,
  363. MF_RESET,
  364. MF_CHANNELAUTOJOIN,
  365. MF_NOUSECART,
  366. MF_NOITEMCONSUMPTION,
  367. MF_SUMSTARTMIRACLE,
  368. MF_NOMINEEFFECT,
  369. MF_NOLOCKON,
  370. MF_NOTOMB,
  371. MF_SKILL_DAMAGE //60
  372. };
  373. const char* script_op2name(int op)
  374. {
  375. #define RETURN_OP_NAME(type) case type: return #type
  376. switch( op )
  377. {
  378. RETURN_OP_NAME(C_NOP);
  379. RETURN_OP_NAME(C_POS);
  380. RETURN_OP_NAME(C_INT);
  381. RETURN_OP_NAME(C_PARAM);
  382. RETURN_OP_NAME(C_FUNC);
  383. RETURN_OP_NAME(C_STR);
  384. RETURN_OP_NAME(C_CONSTSTR);
  385. RETURN_OP_NAME(C_ARG);
  386. RETURN_OP_NAME(C_NAME);
  387. RETURN_OP_NAME(C_EOL);
  388. RETURN_OP_NAME(C_RETINFO);
  389. RETURN_OP_NAME(C_USERFUNC);
  390. RETURN_OP_NAME(C_USERFUNC_POS);
  391. // operators
  392. RETURN_OP_NAME(C_OP3);
  393. RETURN_OP_NAME(C_LOR);
  394. RETURN_OP_NAME(C_LAND);
  395. RETURN_OP_NAME(C_LE);
  396. RETURN_OP_NAME(C_LT);
  397. RETURN_OP_NAME(C_GE);
  398. RETURN_OP_NAME(C_GT);
  399. RETURN_OP_NAME(C_EQ);
  400. RETURN_OP_NAME(C_NE);
  401. RETURN_OP_NAME(C_XOR);
  402. RETURN_OP_NAME(C_OR);
  403. RETURN_OP_NAME(C_AND);
  404. RETURN_OP_NAME(C_ADD);
  405. RETURN_OP_NAME(C_SUB);
  406. RETURN_OP_NAME(C_MUL);
  407. RETURN_OP_NAME(C_DIV);
  408. RETURN_OP_NAME(C_MOD);
  409. RETURN_OP_NAME(C_NEG);
  410. RETURN_OP_NAME(C_LNOT);
  411. RETURN_OP_NAME(C_NOT);
  412. RETURN_OP_NAME(C_R_SHIFT);
  413. RETURN_OP_NAME(C_L_SHIFT);
  414. default:
  415. ShowDebug("script_op2name: unexpected op=%d\n", op);
  416. return "???";
  417. }
  418. #undef RETURN_OP_NAME
  419. }
  420. #ifdef DEBUG_DUMP_STACK
  421. static void script_dump_stack(struct script_state* st)
  422. {
  423. int i;
  424. ShowMessage("\tstart = %d\n", st->start);
  425. ShowMessage("\tend = %d\n", st->end);
  426. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  427. ShowMessage("\tsp = %d\n", st->stack->sp);
  428. for( i = 0; i < st->stack->sp; ++i )
  429. {
  430. struct script_data* data = &st->stack->stack_data[i];
  431. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  432. switch( data->type )
  433. {
  434. case C_INT:
  435. case C_POS:
  436. ShowMessage(" %d\n", data->u.num);
  437. break;
  438. case C_STR:
  439. case C_CONSTSTR:
  440. ShowMessage(" \"%s\"\n", data->u.str);
  441. break;
  442. case C_NAME:
  443. 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));
  444. break;
  445. case C_RETINFO:
  446. {
  447. struct script_retinfo* ri = data->u.ri;
  448. 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);
  449. }
  450. break;
  451. default:
  452. ShowMessage("\n");
  453. break;
  454. }
  455. }
  456. }
  457. #endif
  458. /// Reports on the console the src of a script error.
  459. static void script_reportsrc(struct script_state *st)
  460. {
  461. struct block_list* bl;
  462. if( st->oid == 0 )
  463. return; //Can't report source.
  464. bl = map_id2bl(st->oid);
  465. if( bl == NULL )
  466. return;
  467. switch( bl->type )
  468. {
  469. case BL_NPC:
  470. if( bl->m >= 0 )
  471. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  472. else
  473. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  474. break;
  475. default:
  476. if( bl->m >= 0 )
  477. 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);
  478. else
  479. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  480. break;
  481. }
  482. }
  483. /// Reports on the console information about the script data.
  484. static void script_reportdata(struct script_data* data)
  485. {
  486. if( data == NULL )
  487. return;
  488. switch( data->type )
  489. {
  490. case C_NOP:// no value
  491. ShowDebug("Data: nothing (nil)\n");
  492. break;
  493. case C_INT:// number
  494. ShowDebug("Data: number value=%d\n", data->u.num);
  495. break;
  496. case C_STR:
  497. case C_CONSTSTR:// string
  498. if( data->u.str )
  499. {
  500. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  501. }
  502. else
  503. {
  504. ShowDebug("Data: string value=NULL\n");
  505. }
  506. break;
  507. case C_NAME:// reference
  508. if( reference_tovariable(data) )
  509. {// variable
  510. const char* name = reference_getname(data);
  511. if( not_array_variable(*name) )
  512. ShowDebug("Data: variable name='%s'\n", name);
  513. else
  514. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  515. }
  516. else if( reference_toconstant(data) )
  517. {// constant
  518. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  519. }
  520. else if( reference_toparam(data) )
  521. {// param
  522. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  523. }
  524. else
  525. {// ???
  526. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  527. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  528. }
  529. break;
  530. case C_POS:// label
  531. ShowDebug("Data: label pos=%d\n", data->u.num);
  532. break;
  533. default:
  534. ShowDebug("Data: %s\n", script_op2name(data->type));
  535. break;
  536. }
  537. }
  538. /// Reports on the console information about the current built-in function.
  539. static void script_reportfunc(struct script_state* st)
  540. {
  541. int i, params, id;
  542. struct script_data* data;
  543. if( !script_hasdata(st,0) )
  544. {// no stack
  545. return;
  546. }
  547. data = script_getdata(st,0);
  548. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  549. {// script currently not executing a built-in function or corrupt stack
  550. return;
  551. }
  552. id = reference_getid(data);
  553. params = script_lastdata(st)-1;
  554. if( params > 0 )
  555. {
  556. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  557. for( i = 2; i <= script_lastdata(st); i++ )
  558. {
  559. script_reportdata(script_getdata(st,i));
  560. }
  561. }
  562. else
  563. {
  564. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  565. }
  566. }
  567. /*==========================================
  568. * Output error message
  569. *------------------------------------------*/
  570. static void disp_error_message2(const char *mes,const char *pos,int report)
  571. {
  572. error_msg = aStrdup(mes);
  573. error_pos = pos;
  574. error_report = report;
  575. longjmp( error_jump, 1 );
  576. }
  577. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  578. static void disp_warning_message(const char *mes, const char *pos) {
  579. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  580. }
  581. /// Checks event parameter validity
  582. static void check_event(struct script_state *st, const char *evt)
  583. {
  584. if( evt && evt[0] && !stristr(evt, "::On") )
  585. {
  586. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  587. script_reportsrc(st);
  588. }
  589. }
  590. /*==========================================
  591. * Hashes the input string
  592. *------------------------------------------*/
  593. static unsigned int calc_hash(const char* p)
  594. {
  595. unsigned int h;
  596. #if defined(SCRIPT_HASH_DJB2)
  597. h = 5381;
  598. while( *p ) // hash*33 + c
  599. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  600. #elif defined(SCRIPT_HASH_SDBM)
  601. h = 0;
  602. while( *p ) // hash*65599 + c
  603. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  604. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  605. h = 0;
  606. while( *p ){
  607. unsigned int g;
  608. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  609. g = h & 0xF0000000;
  610. if( g )
  611. {
  612. h ^= g >> 24;
  613. h &= ~g;
  614. }
  615. }
  616. #else // athena hash
  617. h = 0;
  618. while( *p )
  619. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  620. #endif
  621. return h % SCRIPT_HASH_SIZE;
  622. }
  623. /*==========================================
  624. * str_data manipulation functions
  625. *------------------------------------------*/
  626. /// Looks up string using the provided id.
  627. const char* get_str(int id)
  628. {
  629. Assert( id >= LABEL_START && id < str_size );
  630. return str_buf+str_data[id].str;
  631. }
  632. /// Returns the uid of the string, or -1.
  633. static int search_str(const char* p)
  634. {
  635. int i;
  636. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  637. if( strcasecmp(get_str(i),p) == 0 )
  638. return i;
  639. return -1;
  640. }
  641. /// Stores a copy of the string and returns its id.
  642. /// If an identical string is already present, returns its id instead.
  643. int add_str(const char* p)
  644. {
  645. int i, h;
  646. int len;
  647. h = calc_hash(p);
  648. if( str_hash[h] == 0 )
  649. {// empty bucket, add new node here
  650. str_hash[h] = str_num;
  651. }
  652. else
  653. {// scan for end of list, or occurence of identical string
  654. for( i = str_hash[h]; ; i = str_data[i].next )
  655. {
  656. if( strcasecmp(get_str(i),p) == 0 )
  657. return i; // string already in list
  658. if( str_data[i].next == 0 )
  659. break; // reached the end
  660. }
  661. // append node to end of list
  662. str_data[i].next = str_num;
  663. }
  664. // grow list if neccessary
  665. if( str_num >= str_data_size )
  666. {
  667. str_data_size += 128;
  668. RECREATE(str_data,struct str_data_struct,str_data_size);
  669. memset(str_data + (str_data_size - 128), '\0', 128);
  670. }
  671. len=(int)strlen(p);
  672. // grow string buffer if neccessary
  673. while( str_pos+len+1 >= str_size )
  674. {
  675. str_size += 256;
  676. RECREATE(str_buf,char,str_size);
  677. memset(str_buf + (str_size - 256), '\0', 256);
  678. }
  679. safestrncpy(str_buf+str_pos, p, len+1);
  680. str_data[str_num].type = C_NOP;
  681. str_data[str_num].str = str_pos;
  682. str_data[str_num].next = 0;
  683. str_data[str_num].func = NULL;
  684. str_data[str_num].backpatch = -1;
  685. str_data[str_num].label = -1;
  686. str_pos += len+1;
  687. return str_num++;
  688. }
  689. /// Appends 1 byte to the script buffer.
  690. static void add_scriptb(int a)
  691. {
  692. if( script_pos+1 >= script_size )
  693. {
  694. script_size += SCRIPT_BLOCK_SIZE;
  695. RECREATE(script_buf,unsigned char,script_size);
  696. }
  697. script_buf[script_pos++] = (uint8)(a);
  698. }
  699. /// Appends a c_op value to the script buffer.
  700. /// The value is variable-length encoded into 8-bit blocks.
  701. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  702. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  703. static void add_scriptc(int a)
  704. {
  705. while( a >= 0x40 )
  706. {
  707. add_scriptb((a&0x3f)|0x40);
  708. a = (a - 0x40) >> 6;
  709. }
  710. add_scriptb(a);
  711. }
  712. /// Appends an integer value to the script buffer.
  713. /// The value is variable-length encoded into 8-bit blocks.
  714. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  715. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  716. static void add_scripti(int a)
  717. {
  718. while( a >= 0x40 )
  719. {
  720. add_scriptb((a&0x3f)|0xc0);
  721. a = (a - 0x40) >> 6;
  722. }
  723. add_scriptb(a|0x80);
  724. }
  725. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  726. ///
  727. /// @param l The id of the str_data entry
  728. // Maximum up to 16M
  729. static void add_scriptl(int l)
  730. {
  731. int backpatch = str_data[l].backpatch;
  732. switch(str_data[l].type){
  733. case C_POS:
  734. case C_USERFUNC_POS:
  735. add_scriptc(C_POS);
  736. add_scriptb(str_data[l].label);
  737. add_scriptb(str_data[l].label>>8);
  738. add_scriptb(str_data[l].label>>16);
  739. break;
  740. case C_NOP:
  741. case C_USERFUNC:
  742. // Embedded data backpatch there is a possibility of label
  743. add_scriptc(C_NAME);
  744. str_data[l].backpatch = script_pos;
  745. add_scriptb(backpatch);
  746. add_scriptb(backpatch>>8);
  747. add_scriptb(backpatch>>16);
  748. break;
  749. case C_INT:
  750. add_scripti(abs(str_data[l].val));
  751. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  752. add_scriptc(C_NEG);
  753. break;
  754. default: // assume C_NAME
  755. add_scriptc(C_NAME);
  756. add_scriptb(l);
  757. add_scriptb(l>>8);
  758. add_scriptb(l>>16);
  759. break;
  760. }
  761. }
  762. /*==========================================
  763. * Resolve the label
  764. *------------------------------------------*/
  765. void set_label(int l,int pos, const char* script_pos)
  766. {
  767. int i,next;
  768. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  769. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  770. disp_error_message("set_label: invalid label name",script_pos);
  771. return;
  772. }
  773. if(str_data[l].label!=-1){
  774. disp_error_message("set_label: dup label ",script_pos);
  775. return;
  776. }
  777. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  778. str_data[l].label=pos;
  779. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  780. next=GETVALUE(script_buf,i);
  781. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  782. SETVALUE(script_buf,i,pos);
  783. i=next;
  784. }
  785. }
  786. /// Skips spaces and/or comments.
  787. const char* skip_space(const char* p)
  788. {
  789. if( p == NULL )
  790. return NULL;
  791. for(;;)
  792. {
  793. while( ISSPACE(*p) )
  794. ++p;
  795. if( *p == '/' && p[1] == '/' )
  796. {// line comment
  797. while(*p && *p!='\n')
  798. ++p;
  799. }
  800. else if( *p == '/' && p[1] == '*' )
  801. {// block comment
  802. p += 2;
  803. for(;;)
  804. {
  805. if( *p == '\0' ) {
  806. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  807. return p;
  808. }
  809. if( *p == '*' && p[1] == '/' )
  810. {// end of block comment
  811. p += 2;
  812. break;
  813. }
  814. ++p;
  815. }
  816. }
  817. else
  818. break;
  819. }
  820. return p;
  821. }
  822. /// Skips a word.
  823. /// A word consists of undercores and/or alphanumeric characters,
  824. /// and valid variable prefixes/postfixes.
  825. static
  826. const char* skip_word(const char* p)
  827. {
  828. // prefix
  829. switch( *p )
  830. {
  831. case '@':// temporary char variable
  832. ++p; break;
  833. case '#':// account variable
  834. p += ( p[1] == '#' ? 2 : 1 ); break;
  835. case '\'':// instance variable
  836. ++p; break;
  837. case '.':// npc variable
  838. p += ( p[1] == '@' ? 2 : 1 ); break;
  839. case '$':// global variable
  840. p += ( p[1] == '@' ? 2 : 1 ); break;
  841. }
  842. while( ISALNUM(*p) || *p == '_' )
  843. ++p;
  844. // postfix
  845. if( *p == '$' )// string
  846. p++;
  847. return p;
  848. }
  849. /// Adds a word to str_data.
  850. /// @see skip_word
  851. /// @see add_str
  852. static
  853. int add_word(const char* p)
  854. {
  855. char* word;
  856. int len;
  857. int i;
  858. // Check for a word
  859. len = skip_word(p) - p;
  860. if( len == 0 )
  861. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  862. // Duplicate the word
  863. word = (char*)aMalloc(len+1);
  864. memcpy(word, p, len);
  865. word[len] = 0;
  866. // add the word
  867. i = add_str(word);
  868. aFree(word);
  869. return i;
  870. }
  871. /// Parses a function call.
  872. /// The argument list can have parenthesis or not.
  873. /// The number of arguments is checked.
  874. static
  875. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  876. {
  877. const char* p2;
  878. const char* arg=NULL;
  879. int func;
  880. func = add_word(p);
  881. if( str_data[func].type == C_FUNC ){
  882. // buildin function
  883. add_scriptl(func);
  884. add_scriptc(C_ARG);
  885. arg = buildin_func[str_data[func].val].arg;
  886. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  887. // script defined function
  888. add_scriptl(buildin_callsub_ref);
  889. add_scriptc(C_ARG);
  890. add_scriptl(func);
  891. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  892. if( *arg == 0 )
  893. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  894. if( *arg != '*' )
  895. ++arg; // count func as argument
  896. } else {
  897. #ifdef SCRIPT_CALLFUNC_CHECK
  898. const char* name = get_str(func);
  899. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  900. #endif
  901. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  902. #ifdef SCRIPT_CALLFUNC_CHECK
  903. } else {;
  904. add_scriptl(buildin_callfunc_ref);
  905. add_scriptc(C_ARG);
  906. add_scriptc(C_STR);
  907. while( *name ) add_scriptb(*name ++);
  908. add_scriptb(0);
  909. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  910. if( *arg != '*' ) ++ arg;
  911. }
  912. #endif
  913. }
  914. p = skip_word(p);
  915. p = skip_space(p);
  916. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  917. syntax.curly[syntax.curly_count].count = 0;
  918. if( *p == ';' )
  919. {// <func name> ';'
  920. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  921. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  922. {// <func name> '(' ')'
  923. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  924. p = p2;
  925. /*
  926. } else if( 0 && require_paren && *p != '(' )
  927. {// <func name>
  928. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  929. */
  930. } else
  931. {// <func name> <arg list>
  932. if( require_paren ){
  933. if( *p != '(' )
  934. disp_error_message("need '('",p);
  935. ++p; // skip '('
  936. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  937. } else if( *p == '(' ){
  938. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  939. } else {
  940. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  941. }
  942. ++syntax.curly_count;
  943. while( *arg ) {
  944. p2=parse_subexpr(p,-1);
  945. if( p == p2 )
  946. break; // not an argument
  947. if( *arg != '*' )
  948. ++arg; // next argument
  949. p=skip_space(p2);
  950. if( *arg == 0 || *p != ',' )
  951. break; // no more arguments
  952. ++p; // skip comma
  953. }
  954. --syntax.curly_count;
  955. }
  956. if( *arg && *arg != '?' && *arg != '*' )
  957. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  958. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  959. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  960. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  961. if( *p != ')' )
  962. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  963. ++p;
  964. }
  965. add_scriptc(C_FUNC);
  966. return p;
  967. }
  968. /// Processes end of logical script line.
  969. /// @param first When true, only fix up scheduling data is initialized
  970. /// @param p Script position for error reporting in set_label
  971. static void parse_nextline(bool first, const char* p)
  972. {
  973. if( !first )
  974. {
  975. add_scriptc(C_EOL); // mark end of line for stack cleanup
  976. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  977. }
  978. // initialize data for new '-' label fix up scheduling
  979. str_data[LABEL_NEXTLINE].type = C_NOP;
  980. str_data[LABEL_NEXTLINE].backpatch = -1;
  981. str_data[LABEL_NEXTLINE].label = -1;
  982. }
  983. /// Parse a variable assignment using the direct equals operator
  984. /// @param p script position where the function should run from
  985. /// @return NULL if not a variable assignment, the new position otherwise
  986. const char* parse_variable(const char* p) {
  987. int i, j, word;
  988. c_op type = C_NOP;
  989. const char *p2 = NULL;
  990. const char *var = p;
  991. // skip the variable where applicable
  992. p = skip_word(p);
  993. p = skip_space(p);
  994. if( p == NULL ) {// end of the line or invalid buffer
  995. return NULL;
  996. }
  997. if( *p == '[' ) {// array variable so process the array as appropriate
  998. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  999. if( *p ++ == ']' && --(j) == 0 ) break;
  1000. if( *p == '[' ) ++ j;
  1001. }
  1002. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1003. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1004. }
  1005. }
  1006. if( type == C_NOP &&
  1007. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1008. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1009. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1010. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1011. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1012. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1013. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1014. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1015. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1016. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1017. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  1018. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  1019. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1020. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1021. ) )
  1022. {// failed to find a matching operator combination so invalid
  1023. return NULL;
  1024. }
  1025. switch( type ) {
  1026. case C_EQ: {// incremental modifier
  1027. p = skip_space( &p[1] );
  1028. }
  1029. break;
  1030. case C_L_SHIFT:
  1031. case C_R_SHIFT: {// left or right shift modifier
  1032. p = skip_space( &p[3] );
  1033. }
  1034. break;
  1035. default: {// normal incremental command
  1036. p = skip_space( &p[2] );
  1037. }
  1038. }
  1039. if( p == NULL ) {// end of line or invalid buffer
  1040. return NULL;
  1041. }
  1042. // push the set function onto the stack
  1043. add_scriptl(buildin_set_ref);
  1044. add_scriptc(C_ARG);
  1045. // always append parenthesis to avoid errors
  1046. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1047. syntax.curly[syntax.curly_count].count = 0;
  1048. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1049. // increment the total curly count for the position in the script
  1050. ++ syntax.curly_count;
  1051. // parse the variable currently being modified
  1052. word = add_word(var);
  1053. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  1054. {// cannot assign a variable which exists as a function or label
  1055. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1056. }
  1057. if( p2 ) {// process the variable index
  1058. const char* p3 = NULL;
  1059. // push the getelementofarray method into the stack
  1060. add_scriptl(buildin_getelementofarray_ref);
  1061. add_scriptc(C_ARG);
  1062. add_scriptl(word);
  1063. // process the sub-expression for this assignment
  1064. p3 = parse_subexpr(p2 + 1, 1);
  1065. p3 = skip_space(p3);
  1066. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1067. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1068. }
  1069. // push the closing function stack operator onto the stack
  1070. add_scriptc(C_FUNC);
  1071. p3 ++;
  1072. } else {// simply push the variable or value onto the stack
  1073. add_scriptl(word);
  1074. }
  1075. if( type != C_EQ )
  1076. add_scriptc(C_REF);
  1077. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1078. add_scripti(1);
  1079. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1080. } else {// process the value as an expression
  1081. p = parse_subexpr(p, -1);
  1082. if( type != C_EQ )
  1083. {// push the type of modifier onto the stack
  1084. add_scriptc(type);
  1085. }
  1086. }
  1087. // decrement the curly count for the position within the script
  1088. -- syntax.curly_count;
  1089. // close the script by appending the function operator
  1090. add_scriptc(C_FUNC);
  1091. // push the buffer from the method
  1092. return p;
  1093. }
  1094. /*==========================================
  1095. * Analysis section
  1096. *------------------------------------------*/
  1097. const char* parse_simpleexpr(const char *p)
  1098. {
  1099. long long i;
  1100. p=skip_space(p);
  1101. if(*p==';' || *p==',')
  1102. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1103. if(*p=='('){
  1104. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1105. ++syntax.curly[i].count;
  1106. p=parse_subexpr(p+1,-1);
  1107. p=skip_space(p);
  1108. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1109. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1110. ){
  1111. if( *p == ',' ){
  1112. syntax.curly[i].flag = ARGLIST_PAREN;
  1113. return p;
  1114. } else
  1115. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1116. }
  1117. if( *p != ')' )
  1118. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1119. ++p;
  1120. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  1121. char *np;
  1122. while(*p == '0' && ISDIGIT(p[1])) p++;
  1123. i=strtoll(p,&np,0);
  1124. if( i < INT_MIN ) {
  1125. i = INT_MIN;
  1126. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1127. } else if( i > INT_MAX ) {
  1128. i = INT_MAX;
  1129. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1130. }
  1131. add_scripti((int)i);
  1132. p=np;
  1133. } else if(*p=='"'){
  1134. add_scriptc(C_STR);
  1135. p++;
  1136. while( *p && *p != '"' ){
  1137. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1138. {
  1139. char buf[8];
  1140. size_t len = skip_escaped_c(p) - p;
  1141. size_t n = sv_unescape_c(buf, p, len);
  1142. if( n != 1 )
  1143. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1144. p += len;
  1145. add_scriptb(*buf);
  1146. continue;
  1147. }
  1148. else if( *p == '\n' )
  1149. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1150. add_scriptb(*p++);
  1151. }
  1152. if(!*p)
  1153. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1154. add_scriptb(0);
  1155. p++; //'"'
  1156. } else {
  1157. int l;
  1158. const char* pv;
  1159. // label , register , function etc
  1160. if(skip_word(p)==p)
  1161. disp_error_message("parse_simpleexpr: unexpected character",p);
  1162. l=add_word(p);
  1163. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1164. return parse_callfunc(p,1,0);
  1165. #ifdef SCRIPT_CALLFUNC_CHECK
  1166. else {
  1167. const char* name = get_str(l);
  1168. if( strdb_get(userfunc_db,name) != NULL ) {
  1169. return parse_callfunc(p,1,1);
  1170. }
  1171. }
  1172. #endif
  1173. if( (pv = parse_variable(p)) )
  1174. {// successfully processed a variable assignment
  1175. return pv;
  1176. }
  1177. p=skip_word(p);
  1178. if( *p == '[' ){
  1179. // array(name[i] => getelementofarray(name,i) )
  1180. add_scriptl(buildin_getelementofarray_ref);
  1181. add_scriptc(C_ARG);
  1182. add_scriptl(l);
  1183. p=parse_subexpr(p+1,-1);
  1184. p=skip_space(p);
  1185. if( *p != ']' )
  1186. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1187. ++p;
  1188. add_scriptc(C_FUNC);
  1189. }else
  1190. add_scriptl(l);
  1191. }
  1192. return p;
  1193. }
  1194. /*==========================================
  1195. * Analysis of the expression
  1196. *------------------------------------------*/
  1197. const char* parse_subexpr(const char* p,int limit)
  1198. {
  1199. int op,opl,len;
  1200. const char* tmpp;
  1201. p=skip_space(p);
  1202. if( *p == '-' ){
  1203. tmpp = skip_space(p+1);
  1204. if( *tmpp == ';' || *tmpp == ',' ){
  1205. add_scriptl(LABEL_NEXTLINE);
  1206. p++;
  1207. return p;
  1208. }
  1209. }
  1210. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1211. p=parse_subexpr(p+1,10);
  1212. add_scriptc(op);
  1213. } else
  1214. p=parse_simpleexpr(p);
  1215. p=skip_space(p);
  1216. while((
  1217. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1218. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1219. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1220. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1221. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1222. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1223. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1224. (op=C_AND,opl=6,len=1,*p=='&') ||
  1225. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1226. (op=C_OR,opl=5,len=1,*p=='|') ||
  1227. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1228. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1229. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1230. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1231. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1232. (op=C_GT,opl=3,len=1,*p=='>') ||
  1233. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1234. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1235. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1236. p+=len;
  1237. if(op == C_OP3) {
  1238. p=parse_subexpr(p,-1);
  1239. p=skip_space(p);
  1240. if( *(p++) != ':')
  1241. disp_error_message("parse_subexpr: expected ':'", p-1);
  1242. p=parse_subexpr(p,-1);
  1243. } else {
  1244. p=parse_subexpr(p,opl);
  1245. }
  1246. add_scriptc(op);
  1247. p=skip_space(p);
  1248. }
  1249. return p; /* return first untreated operator */
  1250. }
  1251. /*==========================================
  1252. * Evaluation of the expression
  1253. *------------------------------------------*/
  1254. const char* parse_expr(const char *p)
  1255. {
  1256. switch(*p){
  1257. case ')': case ';': case ':': case '[': case ']':
  1258. case '}':
  1259. disp_error_message("parse_expr: unexpected character",p);
  1260. }
  1261. p=parse_subexpr(p,-1);
  1262. return p;
  1263. }
  1264. /*==========================================
  1265. * Analysis of the line
  1266. *------------------------------------------*/
  1267. const char* parse_line(const char* p)
  1268. {
  1269. const char* p2;
  1270. p=skip_space(p);
  1271. if(*p==';') {
  1272. //Close decision for if(); for(); while();
  1273. p = parse_syntax_close(p + 1);
  1274. return p;
  1275. }
  1276. if(*p==')' && parse_syntax_for_flag)
  1277. return p+1;
  1278. p = skip_space(p);
  1279. if(p[0] == '{') {
  1280. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1281. syntax.curly[syntax.curly_count].count = -1;
  1282. syntax.curly[syntax.curly_count].index = -1;
  1283. syntax.curly_count++;
  1284. return p + 1;
  1285. } else if(p[0] == '}') {
  1286. return parse_curly_close(p);
  1287. }
  1288. // Syntax-related processing
  1289. p2 = parse_syntax(p);
  1290. if(p2 != NULL)
  1291. return p2;
  1292. // attempt to process a variable assignment
  1293. p2 = parse_variable(p);
  1294. if( p2 != NULL )
  1295. {// variable assignment processed so leave the method
  1296. return parse_syntax_close(p2 + 1);
  1297. }
  1298. p = parse_callfunc(p,0,0);
  1299. p = skip_space(p);
  1300. if(parse_syntax_for_flag) {
  1301. if( *p != ')' )
  1302. disp_error_message("parse_line: expected ')'",p);
  1303. } else {
  1304. if( *p != ';' )
  1305. disp_error_message("parse_line: expected ';'",p);
  1306. }
  1307. //Binding decision for if(), for(), while()
  1308. p = parse_syntax_close(p+1);
  1309. return p;
  1310. }
  1311. // { ... } Closing process
  1312. const char* parse_curly_close(const char* p)
  1313. {
  1314. if(syntax.curly_count <= 0) {
  1315. disp_error_message("parse_curly_close: unexpected string",p);
  1316. return p + 1;
  1317. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1318. syntax.curly_count--;
  1319. //Close decision if, for , while
  1320. p = parse_syntax_close(p + 1);
  1321. return p;
  1322. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1323. //Closing switch()
  1324. int pos = syntax.curly_count-1;
  1325. char label[256];
  1326. int l;
  1327. // Remove temporary variables
  1328. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1329. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1330. parse_line(label);
  1331. syntax.curly_count--;
  1332. // Go to the end pointer unconditionally
  1333. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1334. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1335. parse_line(label);
  1336. syntax.curly_count--;
  1337. // You are here labeled
  1338. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1339. l=add_str(label);
  1340. set_label(l,script_pos, p);
  1341. if(syntax.curly[pos].flag) {
  1342. //Exists default
  1343. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1344. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1345. parse_line(label);
  1346. syntax.curly_count--;
  1347. }
  1348. // Label end
  1349. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1350. l=add_str(label);
  1351. set_label(l,script_pos, p);
  1352. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1353. syntax.curly_count--;
  1354. //Closing decision if, for , while
  1355. p = parse_syntax_close(p + 1);
  1356. return p;
  1357. } else {
  1358. disp_error_message("parse_curly_close: unexpected string",p);
  1359. return p + 1;
  1360. }
  1361. }
  1362. // Syntax-related processing
  1363. // break, case, continue, default, do, for, function,
  1364. // if, switch, while ? will handle this internally.
  1365. const char* parse_syntax(const char* p)
  1366. {
  1367. const char *p2 = skip_word(p);
  1368. switch(*p) {
  1369. case 'B':
  1370. case 'b':
  1371. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1372. // break Processing
  1373. char label[256];
  1374. int pos = syntax.curly_count - 1;
  1375. while(pos >= 0) {
  1376. if(syntax.curly[pos].type == TYPE_DO) {
  1377. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1378. break;
  1379. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1380. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1381. break;
  1382. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1383. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1384. break;
  1385. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1386. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1387. break;
  1388. }
  1389. pos--;
  1390. }
  1391. if(pos < 0) {
  1392. disp_error_message("parse_syntax: unexpected 'break'",p);
  1393. } else {
  1394. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1395. parse_line(label);
  1396. syntax.curly_count--;
  1397. }
  1398. p = skip_space(p2);
  1399. if(*p != ';')
  1400. disp_error_message("parse_syntax: expected ';'",p);
  1401. // Closing decision if, for , while
  1402. p = parse_syntax_close(p + 1);
  1403. return p;
  1404. }
  1405. break;
  1406. case 'c':
  1407. case 'C':
  1408. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1409. //Processing case
  1410. int pos = syntax.curly_count-1;
  1411. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1412. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1413. return p+1;
  1414. } else {
  1415. char label[256];
  1416. int l,v;
  1417. char *np;
  1418. if(syntax.curly[pos].count != 1) {
  1419. //Jump for FALLTHRU
  1420. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1421. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1422. parse_line(label);
  1423. syntax.curly_count--;
  1424. // You are here labeled
  1425. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1426. l=add_str(label);
  1427. set_label(l,script_pos, p);
  1428. }
  1429. //Decision statement switch
  1430. p = skip_space(p2);
  1431. if(p == p2) {
  1432. disp_error_message("parse_syntax: expected a space ' '",p);
  1433. }
  1434. // check whether case label is integer or not
  1435. v = strtol(p,&np,0);
  1436. if(np == p) { //Check for constants
  1437. p2 = skip_word(p);
  1438. v = p2-p; // length of word at p2
  1439. memcpy(label,p,v);
  1440. label[v]='\0';
  1441. if( !script_get_constant(label, &v) )
  1442. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1443. p = skip_word(p);
  1444. } else { //Numeric value
  1445. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1446. p++;
  1447. p = skip_word(p);
  1448. if(np != p)
  1449. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1450. }
  1451. p = skip_space(p);
  1452. if(*p != ':')
  1453. disp_error_message("parse_syntax: expect ':'",p);
  1454. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1455. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1456. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1457. // Bad I do not parse twice
  1458. p2 = parse_line(label);
  1459. parse_line(p2);
  1460. syntax.curly_count--;
  1461. if(syntax.curly[pos].count != 1) {
  1462. // Label after the completion of FALLTHRU
  1463. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1464. l=add_str(label);
  1465. set_label(l,script_pos,p);
  1466. }
  1467. // check duplication of case label [Rayce]
  1468. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1469. disp_error_message("parse_syntax: dup 'case'",p);
  1470. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1471. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1472. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1473. parse_line(label);
  1474. syntax.curly_count--;
  1475. syntax.curly[pos].count++;
  1476. }
  1477. return p + 1;
  1478. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1479. // Processing continue
  1480. char label[256];
  1481. int pos = syntax.curly_count - 1;
  1482. while(pos >= 0) {
  1483. if(syntax.curly[pos].type == TYPE_DO) {
  1484. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1485. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1486. break;
  1487. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1488. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1489. break;
  1490. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1491. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1492. break;
  1493. }
  1494. pos--;
  1495. }
  1496. if(pos < 0) {
  1497. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1498. } else {
  1499. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1500. parse_line(label);
  1501. syntax.curly_count--;
  1502. }
  1503. p = skip_space(p2);
  1504. if(*p != ';')
  1505. disp_error_message("parse_syntax: expected ';'",p);
  1506. //Closing decision if, for , while
  1507. p = parse_syntax_close(p + 1);
  1508. return p;
  1509. }
  1510. break;
  1511. case 'd':
  1512. case 'D':
  1513. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1514. // Switch - default processing
  1515. int pos = syntax.curly_count-1;
  1516. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1517. disp_error_message("parse_syntax: unexpected 'default'",p);
  1518. } else if(syntax.curly[pos].flag) {
  1519. disp_error_message("parse_syntax: dup 'default'",p);
  1520. } else {
  1521. char label[256];
  1522. int l;
  1523. // Put the label location
  1524. p = skip_space(p2);
  1525. if(*p != ':') {
  1526. disp_error_message("parse_syntax: expected ':'",p);
  1527. }
  1528. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1529. l=add_str(label);
  1530. set_label(l,script_pos,p);
  1531. // Skip to the next link w/o condition
  1532. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1533. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1534. parse_line(label);
  1535. syntax.curly_count--;
  1536. // The default label
  1537. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1538. l=add_str(label);
  1539. set_label(l,script_pos,p);
  1540. syntax.curly[syntax.curly_count - 1].flag = 1;
  1541. syntax.curly[pos].count++;
  1542. }
  1543. return p + 1;
  1544. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1545. int l;
  1546. char label[256];
  1547. p=skip_space(p2);
  1548. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1549. syntax.curly[syntax.curly_count].count = 1;
  1550. syntax.curly[syntax.curly_count].index = syntax.index++;
  1551. syntax.curly[syntax.curly_count].flag = 0;
  1552. // Label of the (do) form here
  1553. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1554. l=add_str(label);
  1555. set_label(l,script_pos,p);
  1556. syntax.curly_count++;
  1557. return p;
  1558. }
  1559. break;
  1560. case 'f':
  1561. case 'F':
  1562. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1563. int l;
  1564. char label[256];
  1565. int pos = syntax.curly_count;
  1566. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1567. syntax.curly[syntax.curly_count].count = 1;
  1568. syntax.curly[syntax.curly_count].index = syntax.index++;
  1569. syntax.curly[syntax.curly_count].flag = 0;
  1570. syntax.curly_count++;
  1571. p=skip_space(p2);
  1572. if(*p != '(')
  1573. disp_error_message("parse_syntax: expected '('",p);
  1574. p++;
  1575. // Execute the initialization statement
  1576. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1577. p=parse_line(p);
  1578. syntax.curly_count--;
  1579. // Form the start of label decision
  1580. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1581. l=add_str(label);
  1582. set_label(l,script_pos,p);
  1583. p=skip_space(p);
  1584. if(*p == ';') {
  1585. // For (; Because the pattern of always true ;)
  1586. ;
  1587. } else {
  1588. // Skip to the end point if the condition is false
  1589. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1590. add_scriptl(add_str("jump_zero"));
  1591. add_scriptc(C_ARG);
  1592. p=parse_expr(p);
  1593. p=skip_space(p);
  1594. add_scriptl(add_str(label));
  1595. add_scriptc(C_FUNC);
  1596. }
  1597. if(*p != ';')
  1598. disp_error_message("parse_syntax: expected ';'",p);
  1599. p++;
  1600. // Skip to the beginning of the loop
  1601. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1602. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1603. parse_line(label);
  1604. syntax.curly_count--;
  1605. // Labels to form the next loop
  1606. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1607. l=add_str(label);
  1608. set_label(l,script_pos,p);
  1609. // Process the next time you enter the loop
  1610. // A ')' last for; flag to be treated as'
  1611. parse_syntax_for_flag = 1;
  1612. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1613. p=parse_line(p);
  1614. syntax.curly_count--;
  1615. parse_syntax_for_flag = 0;
  1616. // Skip to the determination process conditions
  1617. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1618. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1619. parse_line(label);
  1620. syntax.curly_count--;
  1621. // Loop start labeling
  1622. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1623. l=add_str(label);
  1624. set_label(l,script_pos,p);
  1625. return p;
  1626. }
  1627. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1628. {// internal script function
  1629. const char *func_name;
  1630. func_name = skip_space(p2);
  1631. p = skip_word(func_name);
  1632. if( p == func_name )
  1633. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1634. p2 = skip_space(p);
  1635. if( *p2 == ';' )
  1636. {// function <name> ;
  1637. // function declaration - just register the name
  1638. int l;
  1639. l = add_word(func_name);
  1640. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1641. str_data[l].type = C_USERFUNC;
  1642. else if( str_data[l].type == C_USERFUNC )
  1643. ; // already registered
  1644. else
  1645. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1646. // Close condition of if, for, while
  1647. p = parse_syntax_close(p2 + 1);
  1648. return p;
  1649. }
  1650. else if(*p2 == '{')
  1651. {// function <name> <line/block of code>
  1652. char label[256];
  1653. int l;
  1654. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1655. syntax.curly[syntax.curly_count].count = 1;
  1656. syntax.curly[syntax.curly_count].index = syntax.index++;
  1657. syntax.curly[syntax.curly_count].flag = 0;
  1658. ++syntax.curly_count;
  1659. // Jump over the function code
  1660. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1661. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1662. ++syntax.curly_count;
  1663. parse_line(label);
  1664. --syntax.curly_count;
  1665. // Set the position of the function (label)
  1666. l=add_word(func_name);
  1667. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1668. {
  1669. str_data[l].type = C_USERFUNC;
  1670. set_label(l, script_pos, p);
  1671. if( parse_options&SCRIPT_USE_LABEL_DB )
  1672. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1673. }
  1674. else
  1675. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1676. return skip_space(p);
  1677. }
  1678. else
  1679. {
  1680. disp_error_message("expect ';' or '{' at function syntax",p);
  1681. }
  1682. }
  1683. break;
  1684. case 'i':
  1685. case 'I':
  1686. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1687. // If process
  1688. char label[256];
  1689. p=skip_space(p2);
  1690. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1691. disp_error_message("need '('",p);
  1692. }
  1693. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1694. syntax.curly[syntax.curly_count].count = 1;
  1695. syntax.curly[syntax.curly_count].index = syntax.index++;
  1696. syntax.curly[syntax.curly_count].flag = 0;
  1697. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1698. syntax.curly_count++;
  1699. add_scriptl(add_str("jump_zero"));
  1700. add_scriptc(C_ARG);
  1701. p=parse_expr(p);
  1702. p=skip_space(p);
  1703. add_scriptl(add_str(label));
  1704. add_scriptc(C_FUNC);
  1705. return p;
  1706. }
  1707. break;
  1708. case 's':
  1709. case 'S':
  1710. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1711. // Processing of switch ()
  1712. char label[256];
  1713. p=skip_space(p2);
  1714. if(*p != '(') {
  1715. disp_error_message("need '('",p);
  1716. }
  1717. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1718. syntax.curly[syntax.curly_count].count = 1;
  1719. syntax.curly[syntax.curly_count].index = syntax.index++;
  1720. syntax.curly[syntax.curly_count].flag = 0;
  1721. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1722. syntax.curly_count++;
  1723. add_scriptl(add_str("set"));
  1724. add_scriptc(C_ARG);
  1725. add_scriptl(add_str(label));
  1726. p=parse_expr(p);
  1727. p=skip_space(p);
  1728. if(*p != '{') {
  1729. disp_error_message("parse_syntax: expected '{'",p);
  1730. }
  1731. add_scriptc(C_FUNC);
  1732. return p + 1;
  1733. }
  1734. break;
  1735. case 'w':
  1736. case 'W':
  1737. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1738. int l;
  1739. char label[256];
  1740. p=skip_space(p2);
  1741. if(*p != '(') {
  1742. disp_error_message("need '('",p);
  1743. }
  1744. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1745. syntax.curly[syntax.curly_count].count = 1;
  1746. syntax.curly[syntax.curly_count].index = syntax.index++;
  1747. syntax.curly[syntax.curly_count].flag = 0;
  1748. // Form the start of label decision
  1749. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1750. l=add_str(label);
  1751. set_label(l,script_pos,p);
  1752. // Skip to the end point if the condition is false
  1753. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1754. syntax.curly_count++;
  1755. add_scriptl(add_str("jump_zero"));
  1756. add_scriptc(C_ARG);
  1757. p=parse_expr(p);
  1758. p=skip_space(p);
  1759. add_scriptl(add_str(label));
  1760. add_scriptc(C_FUNC);
  1761. return p;
  1762. }
  1763. break;
  1764. }
  1765. return NULL;
  1766. }
  1767. const char* parse_syntax_close(const char *p) {
  1768. // If (...) for (...) hoge (); as to make sure closed closed once again
  1769. int flag;
  1770. do {
  1771. p = parse_syntax_close_sub(p,&flag);
  1772. } while(flag);
  1773. return p;
  1774. }
  1775. // Close judgment if, for, while, of do
  1776. // flag == 1 : closed
  1777. // flag == 0 : not closed
  1778. const char* parse_syntax_close_sub(const char* p,int* flag)
  1779. {
  1780. char label[256];
  1781. int pos = syntax.curly_count - 1;
  1782. int l;
  1783. *flag = 1;
  1784. if(syntax.curly_count <= 0) {
  1785. *flag = 0;
  1786. return p;
  1787. } else if(syntax.curly[pos].type == TYPE_IF) {
  1788. const char *bp = p;
  1789. const char *p2;
  1790. // if-block and else-block end is a new line
  1791. parse_nextline(false, p);
  1792. // Skip to the last location if
  1793. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1794. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1795. parse_line(label);
  1796. syntax.curly_count--;
  1797. // Put the label of the location
  1798. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1799. l=add_str(label);
  1800. set_label(l,script_pos,p);
  1801. syntax.curly[pos].count++;
  1802. p = skip_space(p);
  1803. p2 = skip_word(p);
  1804. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1805. // else or else - if
  1806. p = skip_space(p2);
  1807. p2 = skip_word(p);
  1808. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1809. // else - if
  1810. p=skip_space(p2);
  1811. if(*p != '(') {
  1812. disp_error_message("need '('",p);
  1813. }
  1814. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1815. add_scriptl(add_str("jump_zero"));
  1816. add_scriptc(C_ARG);
  1817. p=parse_expr(p);
  1818. p=skip_space(p);
  1819. add_scriptl(add_str(label));
  1820. add_scriptc(C_FUNC);
  1821. *flag = 0;
  1822. return p;
  1823. } else {
  1824. // else
  1825. if(!syntax.curly[pos].flag) {
  1826. syntax.curly[pos].flag = 1;
  1827. *flag = 0;
  1828. return p;
  1829. }
  1830. }
  1831. }
  1832. // Close if
  1833. syntax.curly_count--;
  1834. // Put the label of the final location
  1835. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1836. l=add_str(label);
  1837. set_label(l,script_pos,p);
  1838. if(syntax.curly[pos].flag == 1) {
  1839. // Because the position of the pointer is the same if not else for this
  1840. return bp;
  1841. }
  1842. return p;
  1843. } else if(syntax.curly[pos].type == TYPE_DO) {
  1844. int l;
  1845. char label[256];
  1846. const char *p2;
  1847. if(syntax.curly[pos].flag) {
  1848. // (Come here continue) to form the label here
  1849. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1850. l=add_str(label);
  1851. set_label(l,script_pos,p);
  1852. }
  1853. // Skip to the end point if the condition is false
  1854. p = skip_space(p);
  1855. p2 = skip_word(p);
  1856. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1857. disp_error_message("parse_syntax: expected 'while'",p);
  1858. p = skip_space(p2);
  1859. if(*p != '(') {
  1860. disp_error_message("need '('",p);
  1861. }
  1862. // do-block end is a new line
  1863. parse_nextline(false, p);
  1864. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1865. add_scriptl(add_str("jump_zero"));
  1866. add_scriptc(C_ARG);
  1867. p=parse_expr(p);
  1868. p=skip_space(p);
  1869. add_scriptl(add_str(label));
  1870. add_scriptc(C_FUNC);
  1871. // Skip to the starting point
  1872. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1873. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1874. parse_line(label);
  1875. syntax.curly_count--;
  1876. // Form label of the end point conditions
  1877. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1878. l=add_str(label);
  1879. set_label(l,script_pos,p);
  1880. p = skip_space(p);
  1881. if(*p != ';') {
  1882. disp_error_message("parse_syntax: expected ';'",p);
  1883. return p+1;
  1884. }
  1885. p++;
  1886. syntax.curly_count--;
  1887. return p;
  1888. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1889. // for-block end is a new line
  1890. parse_nextline(false, p);
  1891. // Skip to the next loop
  1892. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1893. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1894. parse_line(label);
  1895. syntax.curly_count--;
  1896. // End for labeling
  1897. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1898. l=add_str(label);
  1899. set_label(l,script_pos,p);
  1900. syntax.curly_count--;
  1901. return p;
  1902. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1903. // while-block end is a new line
  1904. parse_nextline(false, p);
  1905. // Skip to the decision while
  1906. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1907. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1908. parse_line(label);
  1909. syntax.curly_count--;
  1910. // End while labeling
  1911. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1912. l=add_str(label);
  1913. set_label(l,script_pos,p);
  1914. syntax.curly_count--;
  1915. return p;
  1916. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1917. int pos = syntax.curly_count-1;
  1918. char label[256];
  1919. int l;
  1920. // Back
  1921. sprintf(label,"return;");
  1922. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1923. parse_line(label);
  1924. syntax.curly_count--;
  1925. // Put the label of the location
  1926. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1927. l=add_str(label);
  1928. set_label(l,script_pos,p);
  1929. syntax.curly_count--;
  1930. return p;
  1931. } else {
  1932. *flag = 0;
  1933. return p;
  1934. }
  1935. }
  1936. /*==========================================
  1937. * Added built-in functions
  1938. *------------------------------------------*/
  1939. static void add_buildin_func(void)
  1940. {
  1941. int i,n;
  1942. const char* p;
  1943. for( i = 0; buildin_func[i].func; i++ )
  1944. {
  1945. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1946. // 'v' - value (either string or int or reference)
  1947. // 's' - string
  1948. // 'i' - int
  1949. // 'r' - reference (of a variable)
  1950. // 'l' - label
  1951. // '?' - one optional parameter
  1952. // '*' - unknown number of optional parameters
  1953. p = buildin_func[i].arg;
  1954. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1955. while( *p == '?' ) ++p;
  1956. if( *p == '*' ) ++p;
  1957. if( *p != 0){
  1958. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1959. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1960. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1961. } else {
  1962. n = add_str(buildin_func[i].name);
  1963. str_data[n].type = C_FUNC;
  1964. str_data[n].val = i;
  1965. str_data[n].func = buildin_func[i].func;
  1966. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  1967. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1968. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1969. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1970. }
  1971. }
  1972. }
  1973. /// Retrieves the value of a constant.
  1974. bool script_get_constant(const char* name, int* value)
  1975. {
  1976. int n = search_str(name);
  1977. if( n == -1 || str_data[n].type != C_INT )
  1978. {// not found or not a constant
  1979. return false;
  1980. }
  1981. value[0] = str_data[n].val;
  1982. return true;
  1983. }
  1984. /// Creates new constant or parameter with given value.
  1985. void script_set_constant(const char* name, int value, bool isparameter)
  1986. {
  1987. int n = add_str(name);
  1988. if( str_data[n].type == C_NOP )
  1989. {// new
  1990. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1991. str_data[n].val = value;
  1992. }
  1993. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1994. {// existing parameter or constant
  1995. 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);
  1996. }
  1997. else
  1998. {// existing name
  1999. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2000. }
  2001. }
  2002. /*==========================================
  2003. * Reading constant databases
  2004. * const.txt
  2005. *------------------------------------------*/
  2006. static void read_constdb(void)
  2007. {
  2008. FILE *fp;
  2009. char line[1024],name[1024],val[1024];
  2010. int type;
  2011. sprintf(line, "%s/const.txt", db_path);
  2012. fp=fopen(line, "r");
  2013. if(fp==NULL){
  2014. ShowError("can't read %s\n", line);
  2015. return ;
  2016. }
  2017. while(fgets(line, sizeof(line), fp))
  2018. {
  2019. if(line[0]=='/' && line[1]=='/')
  2020. continue;
  2021. type=0;
  2022. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  2023. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  2024. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2025. }
  2026. }
  2027. fclose(fp);
  2028. }
  2029. /*==========================================
  2030. * Display emplacement line of script
  2031. *------------------------------------------*/
  2032. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2033. {
  2034. int i;
  2035. if( p == NULL || !p[0] ) return NULL;
  2036. if( line < 0 )
  2037. StringBuf_Printf(buf, "*% 5d : ", -line);
  2038. else
  2039. StringBuf_Printf(buf, " % 5d : ", line);
  2040. for(i=0;p[i] && p[i] != '\n';i++){
  2041. if(p + i != mark)
  2042. StringBuf_Printf(buf, "%c", p[i]);
  2043. else
  2044. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2045. }
  2046. StringBuf_AppendStr(buf, "\n");
  2047. return p+i+(p[i] == '\n' ? 1 : 0);
  2048. }
  2049. void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) {
  2050. // Find the line where the error occurred
  2051. int j;
  2052. int line = start_line;
  2053. const char *p;
  2054. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2055. for(p=src;p && *p;line++){
  2056. const char *lineend=strchr(p,'\n');
  2057. if(lineend==NULL || error_pos<lineend){
  2058. break;
  2059. }
  2060. for( j = 0; j < 4; j++ ) {
  2061. linestart[j] = linestart[j+1];
  2062. }
  2063. linestart[4] = p;
  2064. p=lineend+1;
  2065. }
  2066. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2067. StringBuf_Printf(buf, " %s\n", error_msg);
  2068. for(j = 0; j < 5; j++ ) {
  2069. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2070. }
  2071. p = script_print_line(buf, p, error_pos, -line);
  2072. for(j = 0; j < 5; j++) {
  2073. p = script_print_line(buf, p, NULL, line + j + 1 );
  2074. }
  2075. }
  2076. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) {
  2077. StringBuf buf;
  2078. StringBuf_Init(&buf);
  2079. StringBuf_AppendStr(&buf, "\a\n");
  2080. script_errorwarning_sub(&buf, src, file, start_line, error_msg, error_pos);
  2081. ShowError("%s", StringBuf_Value(&buf));
  2082. StringBuf_Destroy(&buf);
  2083. }
  2084. void script_warning(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) {
  2085. StringBuf buf;
  2086. StringBuf_Init(&buf);
  2087. script_errorwarning_sub(&buf, src, file, start_line, error_msg, error_pos);
  2088. ShowWarning("%s", StringBuf_Value(&buf));
  2089. StringBuf_Destroy(&buf);
  2090. }
  2091. /*==========================================
  2092. * Analysis of the script
  2093. *------------------------------------------*/
  2094. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2095. {
  2096. const char *p,*tmpp;
  2097. int i;
  2098. struct script_code* code = NULL;
  2099. static int first=1;
  2100. char end;
  2101. bool unresolved_names = false;
  2102. parser_current_src = src;
  2103. parser_current_file = file;
  2104. parser_current_line = line;
  2105. if( src == NULL )
  2106. return NULL;// empty script
  2107. memset(&syntax,0,sizeof(syntax));
  2108. if(first){
  2109. add_buildin_func();
  2110. read_constdb();
  2111. first=0;
  2112. }
  2113. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2114. script_pos=0;
  2115. script_size=SCRIPT_BLOCK_SIZE;
  2116. parse_nextline(true, NULL);
  2117. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2118. if( options&SCRIPT_USE_LABEL_DB )
  2119. db_clear(scriptlabel_db);
  2120. parse_options = options;
  2121. if( setjmp( error_jump ) != 0 ) {
  2122. //Restore program state when script has problems. [from jA]
  2123. int i;
  2124. const int size = ARRAYLENGTH(syntax.curly);
  2125. if( error_report )
  2126. script_error(src,file,line,error_msg,error_pos);
  2127. aFree( error_msg );
  2128. aFree( script_buf );
  2129. script_pos = 0;
  2130. script_size = 0;
  2131. script_buf = NULL;
  2132. for(i=LABEL_START;i<str_num;i++)
  2133. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  2134. for(i=0; i<size; i++)
  2135. linkdb_final(&syntax.curly[i].case_label);
  2136. return NULL;
  2137. }
  2138. parse_syntax_for_flag=0;
  2139. p=src;
  2140. p=skip_space(p);
  2141. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2142. {// does not require brackets around the script
  2143. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2144. {// empty script and can return NULL
  2145. aFree( script_buf );
  2146. script_pos = 0;
  2147. script_size = 0;
  2148. script_buf = NULL;
  2149. return NULL;
  2150. }
  2151. end = '\0';
  2152. }
  2153. else
  2154. {// requires brackets around the script
  2155. if( *p != '{' )
  2156. disp_error_message("not found '{'",p);
  2157. p = skip_space(p+1);
  2158. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2159. {// empty script and can return NULL
  2160. aFree( script_buf );
  2161. script_pos = 0;
  2162. script_size = 0;
  2163. script_buf = NULL;
  2164. return NULL;
  2165. }
  2166. end = '}';
  2167. }
  2168. // clear references of labels, variables and internal functions
  2169. for(i=LABEL_START;i<str_num;i++){
  2170. if(
  2171. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2172. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2173. ){
  2174. str_data[i].type=C_NOP;
  2175. str_data[i].backpatch=-1;
  2176. str_data[i].label=-1;
  2177. }
  2178. }
  2179. while( syntax.curly_count != 0 || *p != end )
  2180. {
  2181. if( *p == '\0' )
  2182. disp_error_message("unexpected end of script",p);
  2183. // Special handling only label
  2184. tmpp=skip_space(skip_word(p));
  2185. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2186. i=add_word(p);
  2187. set_label(i,script_pos,p);
  2188. if( parse_options&SCRIPT_USE_LABEL_DB )
  2189. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2190. p=tmpp+1;
  2191. p=skip_space(p);
  2192. continue;
  2193. }
  2194. // All other lumped
  2195. p=parse_line(p);
  2196. p=skip_space(p);
  2197. parse_nextline(false, p);
  2198. }
  2199. add_scriptc(C_NOP);
  2200. // trim code to size
  2201. script_size = script_pos;
  2202. RECREATE(script_buf,unsigned char,script_pos);
  2203. // default unknown references to variables
  2204. for(i=LABEL_START;i<str_num;i++){
  2205. if(str_data[i].type==C_NOP){
  2206. int j,next;
  2207. str_data[i].type=C_NAME;
  2208. str_data[i].label=i;
  2209. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2210. next=GETVALUE(script_buf,j);
  2211. SETVALUE(script_buf,j,i);
  2212. j=next;
  2213. }
  2214. }
  2215. else if( str_data[i].type == C_USERFUNC )
  2216. {// 'function name;' without follow-up code
  2217. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2218. unresolved_names = true;
  2219. }
  2220. }
  2221. if( unresolved_names )
  2222. {
  2223. disp_error_message("parse_script: unresolved function references", p);
  2224. }
  2225. #ifdef DEBUG_DISP
  2226. for(i=0;i<script_pos;i++){
  2227. if((i&15)==0) ShowMessage("%04x : ",i);
  2228. ShowMessage("%02x ",script_buf[i]);
  2229. if((i&15)==15) ShowMessage("\n");
  2230. }
  2231. ShowMessage("\n");
  2232. #endif
  2233. #ifdef DEBUG_DISASM
  2234. {
  2235. int i = 0,j;
  2236. while(i < script_pos) {
  2237. c_op op = get_com(script_buf,&i);
  2238. ShowMessage("%06x %s", i, script_op2name(op));
  2239. j = i;
  2240. switch(op) {
  2241. case C_INT:
  2242. ShowMessage(" %d", get_num(script_buf,&i));
  2243. break;
  2244. case C_POS:
  2245. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2246. i += 3;
  2247. break;
  2248. case C_NAME:
  2249. j = (*(int*)(script_buf+i)&0xffffff);
  2250. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2251. i += 3;
  2252. break;
  2253. case C_STR:
  2254. j = strlen(script_buf + i);
  2255. ShowMessage(" %s", script_buf + i);
  2256. i += j+1;
  2257. break;
  2258. }
  2259. ShowMessage(CL_CLL"\n");
  2260. }
  2261. }
  2262. #endif
  2263. CREATE(code,struct script_code,1);
  2264. code->script_buf = script_buf;
  2265. code->script_size = script_size;
  2266. code->script_vars = idb_alloc(DB_OPT_RELEASE_DATA);
  2267. return code;
  2268. }
  2269. /// Returns the player attached to this script, identified by the rid.
  2270. /// If there is no player attached, the script is terminated.
  2271. TBL_PC *script_rid2sd(struct script_state *st)
  2272. {
  2273. TBL_PC *sd=map_id2sd(st->rid);
  2274. if(!sd){
  2275. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2276. script_reportfunc(st);
  2277. script_reportsrc(st);
  2278. st->state = END;
  2279. }
  2280. return sd;
  2281. }
  2282. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2283. ///
  2284. /// @param st Script state
  2285. /// @param data Variable/constant
  2286. void get_val(struct script_state* st, struct script_data* data)
  2287. {
  2288. const char* name;
  2289. char prefix;
  2290. char postfix;
  2291. TBL_PC* sd = NULL;
  2292. if( !data_isreference(data) )
  2293. return;// not a variable/constant
  2294. name = reference_getname(data);
  2295. prefix = name[0];
  2296. postfix = name[strlen(name) - 1];
  2297. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2298. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2299. {
  2300. sd = script_rid2sd(st);
  2301. if( sd == NULL )
  2302. {// needs player attached
  2303. if( postfix == '$' )
  2304. {// string variable
  2305. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2306. data->type = C_CONSTSTR;
  2307. data->u.str = "";
  2308. }
  2309. else
  2310. {// integer variable
  2311. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2312. data->type = C_INT;
  2313. data->u.num = 0;
  2314. }
  2315. return;
  2316. }
  2317. }
  2318. if( postfix == '$' )
  2319. {// string variable
  2320. switch( prefix )
  2321. {
  2322. case '@':
  2323. data->u.str = pc_readregstr(sd, data->u.num);
  2324. break;
  2325. case '$':
  2326. data->u.str = mapreg_readregstr(data->u.num);
  2327. break;
  2328. case '#':
  2329. if( name[1] == '#' )
  2330. data->u.str = pc_readaccountreg2str(sd, name);// global
  2331. else
  2332. data->u.str = pc_readaccountregstr(sd, name);// local
  2333. break;
  2334. case '.':
  2335. {
  2336. struct DBMap* n =
  2337. data->ref ? *data->ref:
  2338. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2339. st->script->script_vars;// npc variable
  2340. if( n )
  2341. data->u.str = (char*)idb_get(n,reference_getuid(data));
  2342. else
  2343. data->u.str = NULL;
  2344. }
  2345. break;
  2346. case '\'':
  2347. {
  2348. int instance_id = script_instancegetid(st);
  2349. if( instance_id )
  2350. data->u.str = (char*)idb_get(instance_data[instance_id].vars,reference_getuid(data));
  2351. else {
  2352. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2353. data->u.str = NULL;
  2354. }
  2355. }
  2356. break;
  2357. default:
  2358. data->u.str = pc_readglobalreg_str(sd, name);
  2359. break;
  2360. }
  2361. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2362. {// empty string
  2363. data->type = C_CONSTSTR;
  2364. data->u.str = "";
  2365. }
  2366. else
  2367. {// duplicate string
  2368. data->type = C_STR;
  2369. data->u.str = aStrdup(data->u.str);
  2370. }
  2371. }
  2372. else
  2373. {// integer variable
  2374. data->type = C_INT;
  2375. if( reference_toconstant(data) )
  2376. {
  2377. data->u.num = reference_getconstant(data);
  2378. }
  2379. else if( reference_toparam(data) )
  2380. {
  2381. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2382. }
  2383. else
  2384. switch( prefix )
  2385. {
  2386. case '@':
  2387. data->u.num = pc_readreg(sd, data->u.num);
  2388. break;
  2389. case '$':
  2390. data->u.num = mapreg_readreg(data->u.num);
  2391. break;
  2392. case '#':
  2393. if( name[1] == '#' )
  2394. data->u.num = pc_readaccountreg2(sd, name);// global
  2395. else
  2396. data->u.num = pc_readaccountreg(sd, name);// local
  2397. break;
  2398. case '.':
  2399. {
  2400. struct DBMap* n =
  2401. data->ref ? *data->ref:
  2402. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2403. st->script->script_vars;// npc variable
  2404. if( n )
  2405. data->u.num = (int)idb_iget(n,reference_getuid(data));
  2406. else
  2407. data->u.num = 0;
  2408. }
  2409. break;
  2410. case '\'':
  2411. {
  2412. int instance_id = script_instancegetid(st);
  2413. if( instance_id )
  2414. data->u.num = (int)idb_iget(instance_data[instance_id].vars,reference_getuid(data));
  2415. else {
  2416. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2417. data->u.num = 0;
  2418. }
  2419. }
  2420. break;
  2421. default:
  2422. data->u.num = pc_readglobalreg(sd, name);
  2423. break;
  2424. }
  2425. }
  2426. return;
  2427. }
  2428. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
  2429. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2430. /// The value is left in the top of the stack and needs to be removed manually.
  2431. void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
  2432. {
  2433. struct script_data* data;
  2434. push_val2(st->stack, C_NAME, uid, ref);
  2435. data = script_getdatatop(st, -1);
  2436. get_val(st, data);
  2437. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2438. }
  2439. /*==========================================
  2440. * Stores the value of a script variable
  2441. * Return value is 0 on fail, 1 on success.
  2442. *------------------------------------------*/
  2443. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
  2444. {
  2445. char prefix = name[0];
  2446. if( is_string_variable(name) ) {// string variable
  2447. const char* str = (const char*)value;
  2448. switch (prefix) {
  2449. case '@':
  2450. return pc_setregstr(sd, num, str);
  2451. case '$':
  2452. return mapreg_setregstr(num, str);
  2453. case '#':
  2454. return (name[1] == '#') ?
  2455. pc_setaccountreg2str(sd, name, str) :
  2456. pc_setaccountregstr(sd, name, str);
  2457. case '.':
  2458. {
  2459. struct DBMap* n;
  2460. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2461. if( n ) {
  2462. idb_remove(n, num);
  2463. if (str[0]) idb_put(n, num, aStrdup(str));
  2464. }
  2465. }
  2466. return 1;
  2467. case '\'':
  2468. {
  2469. int instance_id = script_instancegetid(st);
  2470. if( instance_id ) {
  2471. idb_remove(instance_data[instance_id].vars, num);
  2472. if( str[0] ) idb_put(instance_data[instance_id].vars, num, aStrdup(str));
  2473. }
  2474. return 1;
  2475. }
  2476. default:
  2477. return pc_setglobalreg_str(sd, name, str);
  2478. }
  2479. } else {// integer variable
  2480. int val = (int)__64BPRTSIZE(value);
  2481. if(str_data[num&0x00ffffff].type == C_PARAM)
  2482. {
  2483. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2484. {
  2485. if( st != NULL )
  2486. {
  2487. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2488. script_reportsrc(st);
  2489. st->state = END;
  2490. }
  2491. return 0;
  2492. }
  2493. return 1;
  2494. }
  2495. switch (prefix) {
  2496. case '@':
  2497. return pc_setreg(sd, num, val);
  2498. case '$':
  2499. return mapreg_setreg(num, val);
  2500. case '#':
  2501. return (name[1] == '#') ?
  2502. pc_setaccountreg2(sd, name, val) :
  2503. pc_setaccountreg(sd, name, val);
  2504. case '.':
  2505. {
  2506. struct DBMap* n;
  2507. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2508. if( n ) {
  2509. idb_remove(n, num);
  2510. if( val != 0 )
  2511. idb_iput(n, num, val);
  2512. }
  2513. }
  2514. return 1;
  2515. case '\'':
  2516. {
  2517. int instance_id = script_instancegetid(st);
  2518. if( instance_id ) {
  2519. idb_remove(instance_data[instance_id].vars, num);
  2520. if( val != 0 )
  2521. idb_iput(instance_data[instance_id].vars, num, val);
  2522. }
  2523. return 1;
  2524. }
  2525. default:
  2526. return pc_setglobalreg(sd, name, val);
  2527. }
  2528. }
  2529. }
  2530. int set_var(TBL_PC* sd, char* name, void* val)
  2531. {
  2532. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2533. }
  2534. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
  2535. {
  2536. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2537. }
  2538. /// Converts the data to a string
  2539. const char* conv_str(struct script_state* st, struct script_data* data)
  2540. {
  2541. char* p;
  2542. get_val(st, data);
  2543. if( data_isstring(data) )
  2544. {// nothing to convert
  2545. }
  2546. else if( data_isint(data) )
  2547. {// int -> string
  2548. CREATE(p, char, ITEM_NAME_LENGTH);
  2549. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2550. p[ITEM_NAME_LENGTH-1] = '\0';
  2551. data->type = C_STR;
  2552. data->u.str = p;
  2553. }
  2554. else if( data_isreference(data) )
  2555. {// reference -> string
  2556. //##TODO when does this happen (check get_val) [FlavioJS]
  2557. data->type = C_CONSTSTR;
  2558. data->u.str = reference_getname(data);
  2559. }
  2560. else
  2561. {// unsupported data type
  2562. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2563. script_reportdata(data);
  2564. script_reportsrc(st);
  2565. data->type = C_CONSTSTR;
  2566. data->u.str = "";
  2567. }
  2568. return data->u.str;
  2569. }
  2570. /// Converts the data to an int
  2571. int conv_num(struct script_state* st, struct script_data* data)
  2572. {
  2573. char* p;
  2574. long num;
  2575. get_val(st, data);
  2576. if( data_isint(data) )
  2577. {// nothing to convert
  2578. }
  2579. else if( data_isstring(data) )
  2580. {// string -> int
  2581. // the result does not overflow or underflow, it is capped instead
  2582. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2583. p = data->u.str;
  2584. errno = 0;
  2585. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2586. if( errno == ERANGE
  2587. #if LONG_MAX > INT_MAX
  2588. || num < INT_MIN || num > INT_MAX
  2589. #endif
  2590. )
  2591. {
  2592. if( num <= INT_MIN )
  2593. {
  2594. num = INT_MIN;
  2595. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2596. }
  2597. else//if( num >= INT_MAX )
  2598. {
  2599. num = INT_MAX;
  2600. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2601. }
  2602. script_reportdata(data);
  2603. script_reportsrc(st);
  2604. }
  2605. if( data->type == C_STR )
  2606. aFree(p);
  2607. data->type = C_INT;
  2608. data->u.num = (int)num;
  2609. }
  2610. #if 0
  2611. // FIXME this function is being used to retrieve the position of labels and
  2612. // probably other stuff [FlavioJS]
  2613. else
  2614. {// unsupported data type
  2615. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2616. script_reportdata(data);
  2617. script_reportsrc(st);
  2618. data->type = C_INT;
  2619. data->u.num = 0;
  2620. }
  2621. #endif
  2622. return data->u.num;
  2623. }
  2624. //
  2625. // Stack operations
  2626. //
  2627. /// Increases the size of the stack
  2628. void stack_expand(struct script_stack* stack)
  2629. {
  2630. stack->sp_max += 64;
  2631. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2632. stack->sp_max * sizeof(stack->stack_data[0]) );
  2633. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2634. 64 * sizeof(stack->stack_data[0]) );
  2635. }
  2636. /// Pushes a value into the stack
  2637. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2638. /// Pushes a value into the stack (with reference)
  2639. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
  2640. {
  2641. if( stack->sp >= stack->sp_max )
  2642. stack_expand(stack);
  2643. stack->stack_data[stack->sp].type = type;
  2644. stack->stack_data[stack->sp].u.num = val;
  2645. stack->stack_data[stack->sp].ref = ref;
  2646. stack->sp++;
  2647. return &stack->stack_data[stack->sp-1];
  2648. }
  2649. /// Pushes a string into the stack
  2650. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2651. {
  2652. if( stack->sp >= stack->sp_max )
  2653. stack_expand(stack);
  2654. stack->stack_data[stack->sp].type = type;
  2655. stack->stack_data[stack->sp].u.str = str;
  2656. stack->stack_data[stack->sp].ref = NULL;
  2657. stack->sp++;
  2658. return &stack->stack_data[stack->sp-1];
  2659. }
  2660. /// Pushes a retinfo into the stack
  2661. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, DBMap **ref)
  2662. {
  2663. if( stack->sp >= stack->sp_max )
  2664. stack_expand(stack);
  2665. stack->stack_data[stack->sp].type = C_RETINFO;
  2666. stack->stack_data[stack->sp].u.ri = ri;
  2667. stack->stack_data[stack->sp].ref = ref;
  2668. stack->sp++;
  2669. return &stack->stack_data[stack->sp-1];
  2670. }
  2671. /// Pushes a copy of the target position into the stack
  2672. struct script_data* push_copy(struct script_stack* stack, int pos)
  2673. {
  2674. switch( stack->stack_data[pos].type )
  2675. {
  2676. case C_CONSTSTR:
  2677. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2678. break;
  2679. case C_STR:
  2680. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2681. break;
  2682. case C_RETINFO:
  2683. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2684. exit(1);
  2685. break;
  2686. default:
  2687. return push_val2(
  2688. stack,stack->stack_data[pos].type,
  2689. stack->stack_data[pos].u.num,
  2690. stack->stack_data[pos].ref
  2691. );
  2692. break;
  2693. }
  2694. }
  2695. /// Removes the values in indexes [start,end[ from the stack.
  2696. /// Adjusts all stack pointers.
  2697. void pop_stack(struct script_state* st, int start, int end)
  2698. {
  2699. struct script_stack* stack = st->stack;
  2700. struct script_data* data;
  2701. int i;
  2702. if( start < 0 )
  2703. start = 0;
  2704. if( end > stack->sp )
  2705. end = stack->sp;
  2706. if( start >= end )
  2707. return;// nothing to pop
  2708. // free stack elements
  2709. for( i = start; i < end; i++ )
  2710. {
  2711. data = &stack->stack_data[i];
  2712. if( data->type == C_STR )
  2713. aFree(data->u.str);
  2714. if( data->type == C_RETINFO )
  2715. {
  2716. struct script_retinfo* ri = data->u.ri;
  2717. if( ri->var_function )
  2718. script_free_vars(ri->var_function);
  2719. if( data->ref )
  2720. aFree(data->ref);
  2721. aFree(ri);
  2722. }
  2723. data->type = C_NOP;
  2724. }
  2725. // move the rest of the elements
  2726. if( stack->sp > end )
  2727. {
  2728. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2729. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2730. stack->stack_data[i].type = C_NOP;
  2731. }
  2732. // adjust stack pointers
  2733. if( st->start > end ) st->start -= end - start;
  2734. else if( st->start > start ) st->start = start;
  2735. if( st->end > end ) st->end -= end - start;
  2736. else if( st->end > start ) st->end = start;
  2737. if( stack->defsp > end ) stack->defsp -= end - start;
  2738. else if( stack->defsp > start ) stack->defsp = start;
  2739. stack->sp -= end - start;
  2740. }
  2741. ///
  2742. ///
  2743. ///
  2744. /*==========================================
  2745. * Release script dependent variable, dependent variable of function
  2746. *------------------------------------------*/
  2747. void script_free_vars(struct DBMap* storage)
  2748. {
  2749. if( storage )
  2750. {// destroy the storage construct containing the variables
  2751. db_destroy(storage);
  2752. }
  2753. }
  2754. void script_free_code(struct script_code* code)
  2755. {
  2756. script_free_vars( code->script_vars );
  2757. aFree( code->script_buf );
  2758. aFree( code );
  2759. }
  2760. /// Creates a new script state.
  2761. ///
  2762. /// @param script Script code
  2763. /// @param pos Position in the code
  2764. /// @param rid Who is running the script (attached player)
  2765. /// @param oid Where the code is being run (npc 'object')
  2766. /// @return Script state
  2767. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2768. {
  2769. struct script_state* st;
  2770. CREATE(st, struct script_state, 1);
  2771. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2772. st->stack->sp = 0;
  2773. st->stack->sp_max = 64;
  2774. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2775. st->stack->defsp = st->stack->sp;
  2776. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  2777. st->state = RUN;
  2778. st->script = script;
  2779. //st->scriptroot = script;
  2780. st->pos = pos;
  2781. st->rid = rid;
  2782. st->oid = oid;
  2783. st->sleep.timer = INVALID_TIMER;
  2784. st->npc_item_flag = battle_config.item_enabled_npc;
  2785. return st;
  2786. }
  2787. /// Frees a script state.
  2788. ///
  2789. /// @param st Script state
  2790. void script_free_state(struct script_state* st)
  2791. {
  2792. if(st->bk_st)
  2793. {// backup was not restored
  2794. 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);
  2795. }
  2796. if( st->sleep.timer != INVALID_TIMER )
  2797. delete_timer(st->sleep.timer, run_script_timer);
  2798. script_free_vars(st->stack->var_function);
  2799. pop_stack(st, 0, st->stack->sp);
  2800. aFree(st->stack->stack_data);
  2801. aFree(st->stack);
  2802. st->stack = NULL;
  2803. st->pos = -1;
  2804. aFree(st);
  2805. }
  2806. //
  2807. // Main execution unit
  2808. //
  2809. /*==========================================
  2810. * Read command
  2811. *------------------------------------------*/
  2812. c_op get_com(unsigned char *script,int *pos)
  2813. {
  2814. int i = 0, j = 0;
  2815. if(script[*pos]>=0x80){
  2816. return C_INT;
  2817. }
  2818. while(script[*pos]>=0x40){
  2819. i=script[(*pos)++]<<j;
  2820. j+=6;
  2821. }
  2822. return (c_op)(i+(script[(*pos)++]<<j));
  2823. }
  2824. /*==========================================
  2825. * Income figures
  2826. *------------------------------------------*/
  2827. int get_num(unsigned char *script,int *pos)
  2828. {
  2829. int i,j;
  2830. i=0; j=0;
  2831. while(script[*pos]>=0xc0){
  2832. i+=(script[(*pos)++]&0x7f)<<j;
  2833. j+=6;
  2834. }
  2835. return i+((script[(*pos)++]&0x7f)<<j);
  2836. }
  2837. /*==========================================
  2838. * Remove the value from the stack
  2839. *------------------------------------------*/
  2840. int pop_val(struct script_state* st)
  2841. {
  2842. if(st->stack->sp<=0)
  2843. return 0;
  2844. st->stack->sp--;
  2845. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2846. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2847. return st->stack->stack_data[st->stack->sp].u.num;
  2848. return 0;
  2849. }
  2850. /// Ternary operators
  2851. /// test ? if_true : if_false
  2852. void op_3(struct script_state* st, int op)
  2853. {
  2854. struct script_data* data;
  2855. int flag = 0;
  2856. data = script_getdatatop(st, -3);
  2857. get_val(st, data);
  2858. if( data_isstring(data) )
  2859. flag = data->u.str[0];// "" -> false
  2860. else if( data_isint(data) )
  2861. flag = data->u.num;// 0 -> false
  2862. else
  2863. {
  2864. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2865. script_reportdata(data);
  2866. script_reportsrc(st);
  2867. script_removetop(st, -3, 0);
  2868. script_pushnil(st);
  2869. return;
  2870. }
  2871. if( flag )
  2872. script_pushcopytop(st, -2);
  2873. else
  2874. script_pushcopytop(st, -1);
  2875. script_removetop(st, -4, -1);
  2876. }
  2877. /// Binary string operators
  2878. /// s1 EQ s2 -> i
  2879. /// s1 NE s2 -> i
  2880. /// s1 GT s2 -> i
  2881. /// s1 GE s2 -> i
  2882. /// s1 LT s2 -> i
  2883. /// s1 LE s2 -> i
  2884. /// s1 ADD s2 -> s
  2885. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2886. {
  2887. int a = 0;
  2888. switch(op){
  2889. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2890. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2891. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2892. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2893. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2894. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2895. case C_ADD:
  2896. {
  2897. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2898. strcpy(buf, s1);
  2899. strcat(buf, s2);
  2900. script_pushstr(st, buf);
  2901. return;
  2902. }
  2903. default:
  2904. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2905. script_reportsrc(st);
  2906. script_pushnil(st);
  2907. st->state = END;
  2908. return;
  2909. }
  2910. script_pushint(st,a);
  2911. }
  2912. /// Binary number operators
  2913. /// i OP i -> i
  2914. void op_2num(struct script_state* st, int op, int i1, int i2)
  2915. {
  2916. int ret;
  2917. double ret_double;
  2918. switch( op )
  2919. {
  2920. case C_AND: ret = i1 & i2; break;
  2921. case C_OR: ret = i1 | i2; break;
  2922. case C_XOR: ret = i1 ^ i2; break;
  2923. case C_LAND: ret = (i1 && i2); break;
  2924. case C_LOR: ret = (i1 || i2); break;
  2925. case C_EQ: ret = (i1 == i2); break;
  2926. case C_NE: ret = (i1 != i2); break;
  2927. case C_GT: ret = (i1 > i2); break;
  2928. case C_GE: ret = (i1 >= i2); break;
  2929. case C_LT: ret = (i1 < i2); break;
  2930. case C_LE: ret = (i1 <= i2); break;
  2931. case C_R_SHIFT: ret = i1>>i2; break;
  2932. case C_L_SHIFT: ret = i1<<i2; break;
  2933. case C_DIV:
  2934. case C_MOD:
  2935. if( i2 == 0 )
  2936. {
  2937. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2938. script_reportsrc(st);
  2939. script_pushnil(st);
  2940. st->state = END;
  2941. return;
  2942. }
  2943. else if( op == C_DIV )
  2944. ret = i1 / i2;
  2945. else//if( op == C_MOD )
  2946. ret = i1 % i2;
  2947. break;
  2948. default:
  2949. switch( op )
  2950. {// operators that can overflow/underflow
  2951. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2952. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2953. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2954. default:
  2955. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2956. script_reportsrc(st);
  2957. script_pushnil(st);
  2958. return;
  2959. }
  2960. if( ret_double < (double)INT_MIN )
  2961. {
  2962. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2963. script_reportsrc(st);
  2964. ret = INT_MIN;
  2965. }
  2966. else if( ret_double > (double)INT_MAX )
  2967. {
  2968. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2969. script_reportsrc(st);
  2970. ret = INT_MAX;
  2971. }
  2972. }
  2973. script_pushint(st, ret);
  2974. }
  2975. /// Binary operators
  2976. void op_2(struct script_state *st, int op)
  2977. {
  2978. struct script_data* left, leftref;
  2979. struct script_data* right;
  2980. leftref.type = C_NOP;
  2981. left = script_getdatatop(st, -2);
  2982. right = script_getdatatop(st, -1);
  2983. if (st->op2ref) {
  2984. if (data_isreference(left)) {
  2985. leftref = *left;
  2986. }
  2987. st->op2ref = 0;
  2988. }
  2989. get_val(st, left);
  2990. get_val(st, right);
  2991. // automatic conversions
  2992. switch( op )
  2993. {
  2994. case C_ADD:
  2995. if( data_isint(left) && data_isstring(right) )
  2996. {// convert int-string to string-string
  2997. conv_str(st, left);
  2998. }
  2999. else if( data_isstring(left) && data_isint(right) )
  3000. {// convert string-int to string-string
  3001. conv_str(st, right);
  3002. }
  3003. break;
  3004. }
  3005. if( data_isstring(left) && data_isstring(right) )
  3006. {// ss => op_2str
  3007. op_2str(st, op, left->u.str, right->u.str);
  3008. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3009. if (leftref.type != C_NOP)
  3010. {
  3011. if (left->type == C_STR) // don't free C_CONSTSTR
  3012. aFree(left->u.str);
  3013. *left = leftref;
  3014. }
  3015. }
  3016. else if( data_isint(left) && data_isint(right) )
  3017. {// ii => op_2num
  3018. int i1 = left->u.num;
  3019. int i2 = right->u.num;
  3020. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3021. op_2num(st, op, i1, i2);
  3022. if (leftref.type != C_NOP)
  3023. *left = leftref;
  3024. }
  3025. else
  3026. {// invalid argument
  3027. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3028. script_reportdata(left);
  3029. script_reportdata(right);
  3030. script_reportsrc(st);
  3031. script_removetop(st, -2, 0);
  3032. script_pushnil(st);
  3033. st->state = END;
  3034. }
  3035. }
  3036. /// Unary operators
  3037. /// NEG i -> i
  3038. /// NOT i -> i
  3039. /// LNOT i -> i
  3040. void op_1(struct script_state* st, int op)
  3041. {
  3042. struct script_data* data;
  3043. int i1;
  3044. data = script_getdatatop(st, -1);
  3045. get_val(st, data);
  3046. if( !data_isint(data) )
  3047. {// not a number
  3048. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3049. script_reportdata(data);
  3050. script_reportsrc(st);
  3051. script_pushnil(st);
  3052. st->state = END;
  3053. return;
  3054. }
  3055. i1 = data->u.num;
  3056. script_removetop(st, -1, 0);
  3057. switch( op )
  3058. {
  3059. case C_NEG: i1 = -i1; break;
  3060. case C_NOT: i1 = ~i1; break;
  3061. case C_LNOT: i1 = !i1; break;
  3062. default:
  3063. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3064. script_reportsrc(st);
  3065. script_pushnil(st);
  3066. st->state = END;
  3067. return;
  3068. }
  3069. script_pushint(st, i1);
  3070. }
  3071. /// Checks the type of all arguments passed to a built-in function.
  3072. ///
  3073. /// @param st Script state whose stack arguments should be inspected.
  3074. /// @param func Built-in function for which the arguments are intended.
  3075. static void script_check_buildin_argtype(struct script_state* st, int func)
  3076. {
  3077. char type;
  3078. int idx, invalid = 0;
  3079. script_function* sf = &buildin_func[str_data[func].val];
  3080. for( idx = 2; script_hasdata(st, idx); idx++ )
  3081. {
  3082. struct script_data* data = script_getdata(st, idx);
  3083. type = sf->arg[idx-2];
  3084. if( type == '?' || type == '*' )
  3085. {// optional argument or unknown number of optional parameters ( no types are after this )
  3086. break;
  3087. }
  3088. else if( type == 0 )
  3089. {// more arguments than necessary ( should not happen, as it is checked before )
  3090. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3091. invalid++;
  3092. break;
  3093. }
  3094. else
  3095. {
  3096. const char* name = NULL;
  3097. if( data_isreference(data) )
  3098. {// get name for variables to determine the type they refer to
  3099. name = reference_getname(data);
  3100. }
  3101. switch( type )
  3102. {
  3103. case 'v':
  3104. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3105. {// variant
  3106. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3107. script_reportdata(data);
  3108. invalid++;
  3109. }
  3110. break;
  3111. case 's':
  3112. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3113. {// string
  3114. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3115. script_reportdata(data);
  3116. invalid++;
  3117. }
  3118. break;
  3119. case 'i':
  3120. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3121. {// int ( params and constants are always int )
  3122. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3123. script_reportdata(data);
  3124. invalid++;
  3125. }
  3126. break;
  3127. case 'r':
  3128. if( !data_isreference(data) )
  3129. {// variables
  3130. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3131. script_reportdata(data);
  3132. invalid++;
  3133. }
  3134. break;
  3135. case 'l':
  3136. if( !data_islabel(data) && !data_isfunclabel(data) )
  3137. {// label
  3138. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3139. script_reportdata(data);
  3140. invalid++;
  3141. }
  3142. break;
  3143. }
  3144. }
  3145. }
  3146. if(invalid)
  3147. {
  3148. ShowDebug("Function: %s\n", get_str(func));
  3149. script_reportsrc(st);
  3150. }
  3151. }
  3152. /// Executes a buildin command.
  3153. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3154. int run_func(struct script_state *st)
  3155. {
  3156. struct script_data* data;
  3157. int i,start_sp,end_sp,func;
  3158. end_sp = st->stack->sp;// position after the last argument
  3159. for( i = end_sp-1; i > 0 ; --i )
  3160. if( st->stack->stack_data[i].type == C_ARG )
  3161. break;
  3162. if( i == 0 ) {
  3163. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3164. st->state = END;
  3165. script_reportsrc(st);
  3166. return 1;
  3167. }
  3168. start_sp = i-1;// C_NAME of the command
  3169. st->start = start_sp;
  3170. st->end = end_sp;
  3171. data = &st->stack->stack_data[st->start];
  3172. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3173. func = data->u.num;
  3174. st->funcname = reference_getname(data);
  3175. } else {
  3176. ShowError("script:run_func: not a buildin command.\n");
  3177. script_reportdata(data);
  3178. script_reportsrc(st);
  3179. st->state = END;
  3180. return 1;
  3181. }
  3182. if( script_config.warn_func_mismatch_argtypes ) {
  3183. script_check_buildin_argtype(st, func);
  3184. }
  3185. if(str_data[func].func) {
  3186. if (str_data[func].func(st)) //Report error
  3187. script_reportsrc(st);
  3188. } else {
  3189. 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));
  3190. script_reportsrc(st);
  3191. st->state = END;
  3192. }
  3193. // Stack's datum are used when re-running functions [Eoe]
  3194. if( st->state == RERUNLINE )
  3195. return 0;
  3196. pop_stack(st, st->start, st->end);
  3197. if( st->state == RETFUNC ) {// return from a user-defined function
  3198. struct script_retinfo* ri;
  3199. int olddefsp = st->stack->defsp;
  3200. int nargs;
  3201. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3202. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3203. {
  3204. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3205. script_reportsrc(st);
  3206. st->state = END;
  3207. return 1;
  3208. }
  3209. script_free_vars( st->stack->var_function );
  3210. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3211. nargs = ri->nargs;
  3212. st->pos = ri->pos;
  3213. st->script = ri->script;
  3214. st->stack->var_function = ri->var_function;
  3215. st->stack->defsp = ri->defsp;
  3216. memset(ri, 0, sizeof(struct script_retinfo));
  3217. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3218. st->state = GOTO;
  3219. }
  3220. return 0;
  3221. }
  3222. /*==========================================
  3223. * script execution
  3224. *------------------------------------------*/
  3225. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3226. {
  3227. struct script_state *st;
  3228. if( rootscript == NULL || pos < 0 )
  3229. return;
  3230. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3231. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3232. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3233. st = script_alloc_state(rootscript, pos, rid, oid);
  3234. run_script_main(st);
  3235. }
  3236. void script_stop_sleeptimers(int id)
  3237. {
  3238. struct script_state* st;
  3239. for(;;)
  3240. {
  3241. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3242. if( st == NULL )
  3243. break; // no more sleep timers
  3244. script_free_state(st);
  3245. }
  3246. }
  3247. /*==========================================
  3248. * Delete the specified node from sleep_db
  3249. *------------------------------------------*/
  3250. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3251. {
  3252. struct linkdb_node *retnode;
  3253. if( n == NULL)
  3254. return NULL;
  3255. if( n->prev == NULL )
  3256. sleep_db = n->next;
  3257. else
  3258. n->prev->next = n->next;
  3259. if( n->next )
  3260. n->next->prev = n->prev;
  3261. retnode = n->next;
  3262. aFree( n );
  3263. return retnode; // The following; return retnode
  3264. }
  3265. /*==========================================
  3266. * Timer function for sleep
  3267. *------------------------------------------*/
  3268. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3269. {
  3270. struct script_state *st = (struct script_state *)data;
  3271. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3272. TBL_PC *sd = map_id2sd(st->rid);
  3273. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3274. { //Character mismatch. Cancel execution.
  3275. st->rid = 0;
  3276. st->state = END;
  3277. }
  3278. while( node && st->sleep.timer != INVALID_TIMER ) {
  3279. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3280. script_erase_sleepdb(node);
  3281. st->sleep.timer = INVALID_TIMER;
  3282. break;
  3283. }
  3284. node = node->next;
  3285. }
  3286. if(st->state != RERUNLINE)
  3287. st->sleep.tick = 0;
  3288. run_script_main(st);
  3289. return 0;
  3290. }
  3291. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3292. ///
  3293. /// @param st Script state to detach.
  3294. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3295. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3296. {
  3297. struct map_session_data* sd;
  3298. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3299. sd->st = st->bk_st;
  3300. sd->npc_id = st->bk_npcid;
  3301. if(st->bk_st) {
  3302. //Remove tag for removal.
  3303. st->bk_st = NULL;
  3304. st->bk_npcid = 0;
  3305. } else if(dequeue_event) {
  3306. #ifdef SECURE_NPCTIMEOUT
  3307. /**
  3308. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3309. **/
  3310. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3311. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3312. sd->npc_idle_timer = INVALID_TIMER;
  3313. }
  3314. #endif
  3315. npc_event_dequeue(sd);
  3316. }
  3317. }
  3318. else if(st->bk_st)
  3319. {// rid was set to 0, before detaching the script state
  3320. 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);
  3321. script_reportsrc(st->bk_st);
  3322. script_free_state(st->bk_st);
  3323. st->bk_st = NULL;
  3324. }
  3325. }
  3326. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3327. ///
  3328. /// @param st Script state to attach.
  3329. static void script_attach_state(struct script_state* st)
  3330. {
  3331. struct map_session_data* sd;
  3332. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3333. {
  3334. if(st!=sd->st)
  3335. {
  3336. if(st->bk_st)
  3337. {// there is already a backup
  3338. 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);
  3339. }
  3340. st->bk_st = sd->st;
  3341. st->bk_npcid = sd->npc_id;
  3342. }
  3343. sd->st = st;
  3344. sd->npc_id = st->oid;
  3345. sd->npc_item_flag = st->npc_item_flag; // load default.
  3346. #ifdef SECURE_NPCTIMEOUT
  3347. if( sd->npc_idle_timer == INVALID_TIMER )
  3348. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3349. sd->npc_idle_tick = gettick();
  3350. #endif
  3351. }
  3352. }
  3353. /*==========================================
  3354. * The main part of the script execution
  3355. *------------------------------------------*/
  3356. void run_script_main(struct script_state *st)
  3357. {
  3358. int cmdcount = script_config.check_cmdcount;
  3359. int gotocount = script_config.check_gotocount;
  3360. TBL_PC *sd;
  3361. struct script_stack *stack=st->stack;
  3362. script_attach_state(st);
  3363. if(st->state == RERUNLINE) {
  3364. run_func(st);
  3365. if(st->state == GOTO)
  3366. st->state = RUN;
  3367. } else if(st->state != END)
  3368. st->state = RUN;
  3369. while(st->state == RUN)
  3370. {
  3371. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3372. switch(c){
  3373. case C_EOL:
  3374. if( stack->defsp > stack->sp )
  3375. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3376. else
  3377. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3378. break;
  3379. case C_INT:
  3380. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3381. break;
  3382. case C_POS:
  3383. case C_NAME:
  3384. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3385. st->pos+=3;
  3386. break;
  3387. case C_ARG:
  3388. push_val(stack,c,0);
  3389. break;
  3390. case C_STR:
  3391. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3392. while(st->script->script_buf[st->pos++]);
  3393. break;
  3394. case C_FUNC:
  3395. run_func(st);
  3396. if(st->state==GOTO){
  3397. st->state = RUN;
  3398. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3399. ShowError("run_script: infinity loop !\n");
  3400. script_reportsrc(st);
  3401. st->state=END;
  3402. }
  3403. }
  3404. break;
  3405. case C_REF:
  3406. st->op2ref = 1;
  3407. break;
  3408. case C_NEG:
  3409. case C_NOT:
  3410. case C_LNOT:
  3411. op_1(st ,c);
  3412. break;
  3413. case C_ADD:
  3414. case C_SUB:
  3415. case C_MUL:
  3416. case C_DIV:
  3417. case C_MOD:
  3418. case C_EQ:
  3419. case C_NE:
  3420. case C_GT:
  3421. case C_GE:
  3422. case C_LT:
  3423. case C_LE:
  3424. case C_AND:
  3425. case C_OR:
  3426. case C_XOR:
  3427. case C_LAND:
  3428. case C_LOR:
  3429. case C_R_SHIFT:
  3430. case C_L_SHIFT:
  3431. op_2(st, c);
  3432. break;
  3433. case C_OP3:
  3434. op_3(st, c);
  3435. break;
  3436. case C_NOP:
  3437. st->state=END;
  3438. break;
  3439. default:
  3440. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3441. st->state=END;
  3442. break;
  3443. }
  3444. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3445. ShowError("run_script: infinity loop !\n");
  3446. script_reportsrc(st);
  3447. st->state=END;
  3448. }
  3449. }
  3450. if(st->sleep.tick > 0) {
  3451. //Restore previous script
  3452. script_detach_state(st, false);
  3453. //Delay execution
  3454. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3455. st->sleep.charid = sd?sd->status.char_id:0;
  3456. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3457. run_script_timer, st->sleep.charid, (intptr_t)st);
  3458. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3459. }
  3460. else if(st->state != END && st->rid){
  3461. //Resume later (st is already attached to player).
  3462. if(st->bk_st) {
  3463. ShowWarning("Unable to restore stack! Double continuation!\n");
  3464. //Report BOTH scripts to see if that can help somehow.
  3465. ShowDebug("Previous script (lost):\n");
  3466. script_reportsrc(st->bk_st);
  3467. ShowDebug("Current script:\n");
  3468. script_reportsrc(st);
  3469. script_free_state(st->bk_st);
  3470. st->bk_st = NULL;
  3471. }
  3472. } else {
  3473. //Dispose of script.
  3474. if ((sd = map_id2sd(st->rid))!=NULL)
  3475. { //Restore previous stack and save char.
  3476. if(sd->state.using_fake_npc){
  3477. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3478. sd->state.using_fake_npc = 0;
  3479. }
  3480. //Restore previous script if any.
  3481. script_detach_state(st, true);
  3482. if (sd->state.reg_dirty&2)
  3483. intif_saveregistry(sd,2);
  3484. if (sd->state.reg_dirty&1)
  3485. intif_saveregistry(sd,1);
  3486. }
  3487. script_free_state(st);
  3488. st = NULL;
  3489. }
  3490. }
  3491. int script_config_read(char *cfgName)
  3492. {
  3493. int i;
  3494. char line[1024],w1[1024],w2[1024];
  3495. FILE *fp;
  3496. fp=fopen(cfgName,"r");
  3497. if(fp==NULL){
  3498. ShowError("File not found: %s\n", cfgName);
  3499. return 1;
  3500. }
  3501. while(fgets(line, sizeof(line), fp))
  3502. {
  3503. if(line[0] == '/' && line[1] == '/')
  3504. continue;
  3505. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3506. if(i!=2)
  3507. continue;
  3508. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3509. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3510. }
  3511. else if(strcmpi(w1,"check_cmdcount")==0) {
  3512. script_config.check_cmdcount = config_switch(w2);
  3513. }
  3514. else if(strcmpi(w1,"check_gotocount")==0) {
  3515. script_config.check_gotocount = config_switch(w2);
  3516. }
  3517. else if(strcmpi(w1,"input_min_value")==0) {
  3518. script_config.input_min_value = config_switch(w2);
  3519. }
  3520. else if(strcmpi(w1,"input_max_value")==0) {
  3521. script_config.input_max_value = config_switch(w2);
  3522. }
  3523. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3524. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3525. }
  3526. else if(strcmpi(w1,"import")==0){
  3527. script_config_read(w2);
  3528. }
  3529. else {
  3530. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3531. }
  3532. }
  3533. fclose(fp);
  3534. return 0;
  3535. }
  3536. /**
  3537. * @see DBApply
  3538. */
  3539. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3540. {
  3541. struct script_code *code = db_data2ptr(data);
  3542. if (code)
  3543. script_free_code(code);
  3544. return 0;
  3545. }
  3546. void script_run_autobonus(const char *autobonus, int id, int pos)
  3547. {
  3548. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3549. if( script )
  3550. {
  3551. current_equip_item_index = pos;
  3552. run_script(script,0,id,0);
  3553. }
  3554. }
  3555. void script_add_autobonus(const char *autobonus)
  3556. {
  3557. if( strdb_get(autobonus_db, autobonus) == NULL )
  3558. {
  3559. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3560. if( script )
  3561. strdb_put(autobonus_db, autobonus, script);
  3562. }
  3563. }
  3564. /// resets a temporary character array variable to given value
  3565. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3566. {
  3567. int key;
  3568. uint8 idx;
  3569. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3570. {
  3571. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3572. return;
  3573. }
  3574. key = add_str(varname);
  3575. if( is_string_variable(varname) )
  3576. {
  3577. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3578. {
  3579. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3580. }
  3581. }
  3582. else
  3583. {
  3584. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3585. {
  3586. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3587. }
  3588. }
  3589. }
  3590. /// sets a temporary character array variable element idx to given value
  3591. /// @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.
  3592. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3593. {
  3594. int key;
  3595. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3596. {
  3597. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3598. return;
  3599. }
  3600. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3601. {
  3602. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3603. return;
  3604. }
  3605. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3606. if( is_string_variable(varname) )
  3607. {
  3608. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3609. }
  3610. else
  3611. {
  3612. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3613. }
  3614. if( refcache )
  3615. {// save to avoid repeated add_str calls
  3616. refcache[0] = key;
  3617. }
  3618. }
  3619. #ifdef BETA_THREAD_TEST
  3620. int buildin_query_sql_sub(struct script_state* st, Sql* handle);
  3621. /* used to receive items the queryThread has already processed */
  3622. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3623. int i, cursor = 0;
  3624. bool allOk = true;
  3625. EnterSpinLock(&queryThreadLock);
  3626. for( i = 0; i < queryThreadData.count; i++ ) {
  3627. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3628. if( !entry->ok ) {
  3629. allOk = false;
  3630. continue;
  3631. }
  3632. run_script_main(entry->st);
  3633. entry->st = NULL;/* empty entries */
  3634. aFree(entry);
  3635. queryThreadData.entry[i] = NULL;
  3636. }
  3637. if( allOk ) {
  3638. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3639. delete_timer(queryThreadData.timer, queryThread_timer);
  3640. queryThreadData.timer = INVALID_TIMER;
  3641. }
  3642. /* now lets clear the mess. */
  3643. for( i = 0; i < queryThreadData.count; i++ ) {
  3644. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3645. if( entry == NULL )
  3646. continue;/* entry on hold */
  3647. /* move */
  3648. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3649. cursor++;
  3650. }
  3651. queryThreadData.count = cursor;
  3652. LeaveSpinLock(&queryThreadLock);
  3653. return 0;
  3654. }
  3655. void queryThread_add(struct script_state *st, bool type) {
  3656. int idx = 0;
  3657. struct queryThreadEntry* entry = NULL;
  3658. EnterSpinLock(&queryThreadLock);
  3659. if( queryThreadData.count++ != 0 )
  3660. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3661. idx = queryThreadData.count-1;
  3662. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3663. entry = queryThreadData.entry[idx];
  3664. entry->st = st;
  3665. entry->ok = false;
  3666. entry->type = type;
  3667. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3668. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3669. }
  3670. LeaveSpinLock(&queryThreadLock);
  3671. /* unlock the queryThread */
  3672. racond_signal(queryThreadCond);
  3673. }
  3674. /* adds a new log to the queue */
  3675. void queryThread_log(char * entry, int length) {
  3676. int idx = logThreadData.count;
  3677. EnterSpinLock(&queryThreadLock);
  3678. if( logThreadData.count++ != 0 )
  3679. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3680. CREATE(logThreadData.entry[idx], char, length + 1 );
  3681. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3682. LeaveSpinLock(&queryThreadLock);
  3683. /* unlock the queryThread */
  3684. racond_signal(queryThreadCond);
  3685. }
  3686. /* queryThread_main */
  3687. static void *queryThread_main(void *x) {
  3688. Sql *queryThread_handle = Sql_Malloc();
  3689. int i;
  3690. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) )
  3691. exit(EXIT_FAILURE);
  3692. if( strlen(default_codepage) > 0 )
  3693. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3694. Sql_ShowDebug(queryThread_handle);
  3695. if( log_config.sql_logs ) {
  3696. logmysql_handle = Sql_Malloc();
  3697. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) )
  3698. exit(EXIT_FAILURE);
  3699. if( strlen(default_codepage) > 0 )
  3700. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3701. Sql_ShowDebug(logmysql_handle);
  3702. }
  3703. while( 1 ) {
  3704. if(queryThreadTerminate > 0)
  3705. break;
  3706. EnterSpinLock(&queryThreadLock);
  3707. /* mess with queryThreadData within the lock */
  3708. for( i = 0; i < queryThreadData.count; i++ ) {
  3709. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3710. if( entry->ok )
  3711. continue;
  3712. else if ( !entry->st || !entry->st->stack ) {
  3713. entry->ok = true;/* dispose */
  3714. continue;
  3715. }
  3716. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  3717. entry->ok = true;/* we're done with this */
  3718. }
  3719. /* also check for any logs in need to be sent */
  3720. if( log_config.sql_logs ) {
  3721. for( i = 0; i < logThreadData.count; i++ ) {
  3722. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  3723. Sql_ShowDebug(logmysql_handle);
  3724. aFree(logThreadData.entry[i]);
  3725. }
  3726. logThreadData.count = 0;
  3727. }
  3728. LeaveSpinLock(&queryThreadLock);
  3729. ramutex_lock( queryThreadMutex );
  3730. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  3731. ramutex_unlock( queryThreadMutex );
  3732. }
  3733. Sql_Free(queryThread_handle);
  3734. if( log_config.sql_logs ) {
  3735. Sql_Free(logmysql_handle);
  3736. }
  3737. return NULL;
  3738. }
  3739. #endif
  3740. /*==========================================
  3741. * Destructor
  3742. *------------------------------------------*/
  3743. int do_final_script() {
  3744. int i;
  3745. #ifdef DEBUG_HASH
  3746. if (battle_config.etc_log)
  3747. {
  3748. FILE *fp = fopen("hash_dump.txt","wt");
  3749. if(fp) {
  3750. int count[SCRIPT_HASH_SIZE];
  3751. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3752. int n=0;
  3753. int min=INT_MAX,max=0,zero=0;
  3754. double mean=0.0f;
  3755. double median=0.0f;
  3756. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3757. memset(count, 0, sizeof(count));
  3758. fprintf(fp,"num : hash : data_name\n");
  3759. fprintf(fp,"---------------------------------------------------------------\n");
  3760. for(i=LABEL_START; i<str_num; i++) {
  3761. unsigned int h = calc_hash(get_str(i));
  3762. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3763. ++count[h];
  3764. }
  3765. fprintf(fp,"--------------------\n\n");
  3766. memset(count2, 0, sizeof(count2));
  3767. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3768. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3769. if(min > count[i])
  3770. min = count[i]; // minimun count of collision
  3771. if(max < count[i])
  3772. max = count[i]; // maximun count of collision
  3773. if(count[i] == 0)
  3774. zero++;
  3775. ++count2[count[i]];
  3776. }
  3777. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3778. for( i=min; i <= max; ++i ){
  3779. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3780. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3781. }
  3782. for( i=min; i <= max; ++i ){
  3783. n += count2[i];
  3784. if( n*2 >= SCRIPT_HASH_SIZE )
  3785. {
  3786. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3787. median = (i+i+1)/2.0f;
  3788. else
  3789. median = i;
  3790. break;
  3791. }
  3792. }
  3793. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3794. fclose(fp);
  3795. }
  3796. }
  3797. #endif
  3798. mapreg_final();
  3799. db_destroy(scriptlabel_db);
  3800. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3801. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3802. if(sleep_db) {
  3803. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3804. while(n) {
  3805. struct script_state *st = (struct script_state *)n->data;
  3806. script_free_state(st);
  3807. n = n->next;
  3808. }
  3809. linkdb_final(&sleep_db);
  3810. }
  3811. if (str_data)
  3812. aFree(str_data);
  3813. if (str_buf)
  3814. aFree(str_buf);
  3815. for( i = 0; i < atcmd_binding_count; i++ ) {
  3816. aFree(atcmd_binding[i]);
  3817. }
  3818. if( atcmd_binding_count != 0 )
  3819. aFree(atcmd_binding);
  3820. #ifdef BETA_THREAD_TEST
  3821. /* QueryThread */
  3822. InterlockedIncrement(&queryThreadTerminate);
  3823. racond_signal(queryThreadCond);
  3824. rathread_wait(queryThread, NULL);
  3825. // Destroy cond var and mutex.
  3826. racond_destroy( queryThreadCond );
  3827. ramutex_destroy( queryThreadMutex );
  3828. /* Clear missing vars */
  3829. for( i = 0; i < queryThreadData.count; i++ ) {
  3830. aFree(queryThreadData.entry[i]);
  3831. }
  3832. aFree(queryThreadData.entry);
  3833. for( i = 0; i < logThreadData.count; i++ ) {
  3834. aFree(logThreadData.entry[i]);
  3835. }
  3836. aFree(logThreadData.entry);
  3837. #endif
  3838. return 0;
  3839. }
  3840. /*==========================================
  3841. * Initialization
  3842. *------------------------------------------*/
  3843. int do_init_script() {
  3844. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3845. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3846. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3847. mapreg_init();
  3848. #ifdef BETA_THREAD_TEST
  3849. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  3850. queryThreadData.count = 0;
  3851. CREATE(logThreadData.entry, char *, 1);
  3852. logThreadData.count = 0;
  3853. /* QueryThread Start */
  3854. InitializeSpinLock(&queryThreadLock);
  3855. queryThreadData.timer = INVALID_TIMER;
  3856. queryThreadTerminate = 0;
  3857. queryThreadMutex = ramutex_create();
  3858. queryThreadCond = racond_create();
  3859. queryThread = rathread_create(queryThread_main, NULL);
  3860. if(queryThread == NULL){
  3861. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  3862. exit(EXIT_FAILURE);
  3863. }
  3864. add_timer_func_list(queryThread_timer, "queryThread_timer");
  3865. #endif
  3866. return 0;
  3867. }
  3868. int script_reload() {
  3869. int i;
  3870. #ifdef BETA_THREAD_TEST
  3871. /* we're reloading so any queries undergoing should be...exterminated. */
  3872. EnterSpinLock(&queryThreadLock);
  3873. for( i = 0; i < queryThreadData.count; i++ ) {
  3874. aFree(queryThreadData.entry[i]);
  3875. }
  3876. queryThreadData.count = 0;
  3877. if( queryThreadData.timer != INVALID_TIMER ) {
  3878. delete_timer(queryThreadData.timer, queryThread_timer);
  3879. queryThreadData.timer = INVALID_TIMER;
  3880. }
  3881. LeaveSpinLock(&queryThreadLock);
  3882. #endif
  3883. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  3884. db_clear(scriptlabel_db);
  3885. // @commands (script based)
  3886. // Clear bindings
  3887. for( i = 0; i < atcmd_binding_count; i++ ) {
  3888. aFree(atcmd_binding[i]);
  3889. }
  3890. if( atcmd_binding_count != 0 )
  3891. aFree(atcmd_binding);
  3892. atcmd_binding_count = 0;
  3893. if(sleep_db) {
  3894. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3895. while(n) {
  3896. struct script_state *st = (struct script_state *)n->data;
  3897. script_free_state(st);
  3898. n = n->next;
  3899. }
  3900. linkdb_final(&sleep_db);
  3901. }
  3902. mapreg_reload();
  3903. return 0;
  3904. }
  3905. //-----------------------------------------------------------------------------
  3906. // buildin functions
  3907. //
  3908. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3909. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3910. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3911. /////////////////////////////////////////////////////////////////////
  3912. // NPC interaction
  3913. //
  3914. /// Appends a message to the npc dialog.
  3915. /// If a dialog doesn't exist yet, one is created.
  3916. ///
  3917. /// mes "<message>";
  3918. BUILDIN_FUNC(mes)
  3919. {
  3920. TBL_PC* sd = script_rid2sd(st);
  3921. if( sd == NULL )
  3922. return 0;
  3923. if( !script_hasdata(st, 3) )
  3924. {// only a single line detected in the script
  3925. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3926. }
  3927. else
  3928. {// parse multiple lines as they exist
  3929. int i;
  3930. for( i = 2; script_hasdata(st, i); i++ )
  3931. {
  3932. // send the message to the client
  3933. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3934. }
  3935. }
  3936. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  3937. return 0;
  3938. }
  3939. /// Displays the button 'next' in the npc dialog.
  3940. /// The dialog text is cleared and the script continues when the button is pressed.
  3941. ///
  3942. /// next;
  3943. BUILDIN_FUNC(next)
  3944. {
  3945. TBL_PC* sd;
  3946. sd = script_rid2sd(st);
  3947. if( sd == NULL )
  3948. return 0;
  3949. #ifdef SECURE_NPCTIMEOUT
  3950. sd->npc_idle_type = NPCT_WAIT;
  3951. #endif
  3952. st->state = STOP;
  3953. clif_scriptnext(sd, st->oid);
  3954. return 0;
  3955. }
  3956. /// Ends the script and displays the button 'close' on the npc dialog.
  3957. /// The dialog is closed when the button is pressed.
  3958. ///
  3959. /// close;
  3960. BUILDIN_FUNC(close)
  3961. {
  3962. TBL_PC* sd;
  3963. sd = script_rid2sd(st);
  3964. if( sd == NULL )
  3965. return 0;
  3966. if( !st->mes_active ) {
  3967. TBL_NPC* nd = map_id2nd(st->oid);
  3968. st->state = END; // Keep backwards compatibility.
  3969. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  3970. } else {
  3971. st->state = CLOSE;
  3972. st->mes_active = 0;
  3973. }
  3974. clif_scriptclose(sd, st->oid);
  3975. return 0;
  3976. }
  3977. /// Displays the button 'close' on the npc dialog.
  3978. /// The dialog is closed and the script continues when the button is pressed.
  3979. ///
  3980. /// close2;
  3981. BUILDIN_FUNC(close2)
  3982. {
  3983. TBL_PC* sd;
  3984. sd = script_rid2sd(st);
  3985. if( sd == NULL )
  3986. return 0;
  3987. st->state = STOP;
  3988. if( st->mes_active )
  3989. st->mes_active = 0;
  3990. clif_scriptclose(sd, st->oid);
  3991. return 0;
  3992. }
  3993. /// Counts the number of valid and total number of options in 'str'
  3994. /// If max_count > 0 the counting stops when that valid option is reached
  3995. /// total is incremented for each option (NULL is supported)
  3996. static int menu_countoptions(const char* str, int max_count, int* total)
  3997. {
  3998. int count = 0;
  3999. int bogus_total;
  4000. if( total == NULL )
  4001. total = &bogus_total;
  4002. ++(*total);
  4003. // initial empty options
  4004. while( *str == ':' )
  4005. {
  4006. ++str;
  4007. ++(*total);
  4008. }
  4009. // count menu options
  4010. while( *str != '\0' )
  4011. {
  4012. ++count;
  4013. --max_count;
  4014. if( max_count == 0 )
  4015. break;
  4016. while( *str != ':' && *str != '\0' )
  4017. ++str;
  4018. while( *str == ':' )
  4019. {
  4020. ++str;
  4021. ++(*total);
  4022. }
  4023. }
  4024. return count;
  4025. }
  4026. /// Displays a menu with options and goes to the target label.
  4027. /// The script is stopped if cancel is pressed.
  4028. /// Options with no text are not displayed in the client.
  4029. ///
  4030. /// Options can be grouped together, separated by the character ':' in the text:
  4031. /// ex: menu "A:B:C",L_target;
  4032. /// All these options go to the specified target label.
  4033. ///
  4034. /// The index of the selected option is put in the variable @menu.
  4035. /// Indexes start with 1 and are consistent with grouped and empty options.
  4036. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4037. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4038. ///
  4039. /// NOTE: the client closes the npc dialog when cancel is pressed
  4040. ///
  4041. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4042. BUILDIN_FUNC(menu)
  4043. {
  4044. int i;
  4045. const char* text;
  4046. TBL_PC* sd;
  4047. sd = script_rid2sd(st);
  4048. if( sd == NULL )
  4049. return 0;
  4050. #ifdef SECURE_NPCTIMEOUT
  4051. sd->npc_idle_type = NPCT_MENU;
  4052. #endif
  4053. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4054. if( sd->state.menu_or_input == 0 )
  4055. {
  4056. struct StringBuf buf;
  4057. struct script_data* data;
  4058. if( script_lastdata(st) % 2 == 0 )
  4059. {// argument count is not even (1st argument is at index 2)
  4060. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4061. st->state = END;
  4062. return 1;
  4063. }
  4064. StringBuf_Init(&buf);
  4065. sd->npc_menu = 0;
  4066. for( i = 2; i < script_lastdata(st); i += 2 )
  4067. {
  4068. // menu options
  4069. text = script_getstr(st, i);
  4070. // target label
  4071. data = script_getdata(st, i+1);
  4072. if( !data_islabel(data) )
  4073. {// not a label
  4074. StringBuf_Destroy(&buf);
  4075. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4076. script_reportdata(data);
  4077. st->state = END;
  4078. return 1;
  4079. }
  4080. // append option(s)
  4081. if( text[0] == '\0' )
  4082. continue;// empty string, ignore
  4083. if( sd->npc_menu > 0 )
  4084. StringBuf_AppendStr(&buf, ":");
  4085. StringBuf_AppendStr(&buf, text);
  4086. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4087. }
  4088. st->state = RERUNLINE;
  4089. sd->state.menu_or_input = 1;
  4090. /**
  4091. * menus beyond this length crash the client (see bugreport:6402)
  4092. **/
  4093. if( StringBuf_Length(&buf) >= 2047 ) {
  4094. struct npc_data * nd = map_id2nd(st->oid);
  4095. char* menu;
  4096. CREATE(menu, char, 2048);
  4097. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4098. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4099. clif_scriptmenu(sd, st->oid, menu);
  4100. aFree(menu);
  4101. } else
  4102. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4103. StringBuf_Destroy(&buf);
  4104. if( sd->npc_menu >= 0xff )
  4105. {// 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
  4106. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4107. script_reportsrc(st);
  4108. }
  4109. }
  4110. else if( sd->npc_menu == 0xff )
  4111. {// Cancel was pressed
  4112. sd->state.menu_or_input = 0;
  4113. st->state = END;
  4114. }
  4115. else
  4116. {// goto target label
  4117. int menu = 0;
  4118. sd->state.menu_or_input = 0;
  4119. if( sd->npc_menu <= 0 )
  4120. {
  4121. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4122. st->state = END;
  4123. return 1;
  4124. }
  4125. // get target label
  4126. for( i = 2; i < script_lastdata(st); i += 2 )
  4127. {
  4128. text = script_getstr(st, i);
  4129. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4130. if( sd->npc_menu <= 0 )
  4131. break;// entry found
  4132. }
  4133. if( sd->npc_menu > 0 )
  4134. {// Invalid selection
  4135. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4136. st->state = END;
  4137. return 1;
  4138. }
  4139. if( !data_islabel(script_getdata(st, i + 1)) )
  4140. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4141. ShowError("script:menu: unexpected data in label argument\n");
  4142. script_reportdata(script_getdata(st, i + 1));
  4143. st->state = END;
  4144. return 1;
  4145. }
  4146. pc_setreg(sd, add_str("@menu"), menu);
  4147. st->pos = script_getnum(st, i + 1);
  4148. st->state = GOTO;
  4149. }
  4150. return 0;
  4151. }
  4152. /// Displays a menu with options and returns the selected option.
  4153. /// Behaves like 'menu' without the target labels.
  4154. ///
  4155. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4156. ///
  4157. /// @see menu
  4158. BUILDIN_FUNC(select)
  4159. {
  4160. int i;
  4161. const char* text;
  4162. TBL_PC* sd;
  4163. sd = script_rid2sd(st);
  4164. if( sd == NULL )
  4165. return 0;
  4166. #ifdef SECURE_NPCTIMEOUT
  4167. sd->npc_idle_type = NPCT_MENU;
  4168. #endif
  4169. if( sd->state.menu_or_input == 0 ) {
  4170. struct StringBuf buf;
  4171. StringBuf_Init(&buf);
  4172. sd->npc_menu = 0;
  4173. for( i = 2; i <= script_lastdata(st); ++i ) {
  4174. text = script_getstr(st, i);
  4175. if( sd->npc_menu > 0 )
  4176. StringBuf_AppendStr(&buf, ":");
  4177. StringBuf_AppendStr(&buf, text);
  4178. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4179. }
  4180. st->state = RERUNLINE;
  4181. sd->state.menu_or_input = 1;
  4182. /**
  4183. * menus beyond this length crash the client (see bugreport:6402)
  4184. **/
  4185. if( StringBuf_Length(&buf) >= 2047 ) {
  4186. struct npc_data * nd = map_id2nd(st->oid);
  4187. char* menu;
  4188. CREATE(menu, char, 2048);
  4189. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4190. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4191. clif_scriptmenu(sd, st->oid, menu);
  4192. aFree(menu);
  4193. } else
  4194. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4195. StringBuf_Destroy(&buf);
  4196. if( sd->npc_menu >= 0xff ) {
  4197. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4198. script_reportsrc(st);
  4199. }
  4200. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4201. sd->state.menu_or_input = 0;
  4202. st->state = END;
  4203. } else {// return selected option
  4204. int menu = 0;
  4205. sd->state.menu_or_input = 0;
  4206. for( i = 2; i <= script_lastdata(st); ++i ) {
  4207. text = script_getstr(st, i);
  4208. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4209. if( sd->npc_menu <= 0 )
  4210. break;// entry found
  4211. }
  4212. pc_setreg(sd, add_str("@menu"), menu);
  4213. script_pushint(st, menu);
  4214. st->state = RUN;
  4215. }
  4216. return 0;
  4217. }
  4218. /// Displays a menu with options and returns the selected option.
  4219. /// Behaves like 'menu' without the target labels, except when cancel is
  4220. /// pressed.
  4221. /// When cancel is pressed, the script continues and 255 is returned.
  4222. ///
  4223. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4224. ///
  4225. /// @see menu
  4226. BUILDIN_FUNC(prompt)
  4227. {
  4228. int i;
  4229. const char *text;
  4230. TBL_PC* sd;
  4231. sd = script_rid2sd(st);
  4232. if( sd == NULL )
  4233. return 0;
  4234. #ifdef SECURE_NPCTIMEOUT
  4235. sd->npc_idle_type = NPCT_MENU;
  4236. #endif
  4237. if( sd->state.menu_or_input == 0 )
  4238. {
  4239. struct StringBuf buf;
  4240. StringBuf_Init(&buf);
  4241. sd->npc_menu = 0;
  4242. for( i = 2; i <= script_lastdata(st); ++i )
  4243. {
  4244. text = script_getstr(st, i);
  4245. if( sd->npc_menu > 0 )
  4246. StringBuf_AppendStr(&buf, ":");
  4247. StringBuf_AppendStr(&buf, text);
  4248. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4249. }
  4250. st->state = RERUNLINE;
  4251. sd->state.menu_or_input = 1;
  4252. /**
  4253. * menus beyond this length crash the client (see bugreport:6402)
  4254. **/
  4255. if( StringBuf_Length(&buf) >= 2047 ) {
  4256. struct npc_data * nd = map_id2nd(st->oid);
  4257. char* menu;
  4258. CREATE(menu, char, 2048);
  4259. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4260. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4261. clif_scriptmenu(sd, st->oid, menu);
  4262. aFree(menu);
  4263. } else
  4264. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4265. StringBuf_Destroy(&buf);
  4266. if( sd->npc_menu >= 0xff )
  4267. {
  4268. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4269. script_reportsrc(st);
  4270. }
  4271. }
  4272. else if( sd->npc_menu == 0xff )
  4273. {// Cancel was pressed
  4274. sd->state.menu_or_input = 0;
  4275. pc_setreg(sd, add_str("@menu"), 0xff);
  4276. script_pushint(st, 0xff);
  4277. st->state = RUN;
  4278. }
  4279. else
  4280. {// return selected option
  4281. int menu = 0;
  4282. sd->state.menu_or_input = 0;
  4283. for( i = 2; i <= script_lastdata(st); ++i )
  4284. {
  4285. text = script_getstr(st, i);
  4286. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4287. if( sd->npc_menu <= 0 )
  4288. break;// entry found
  4289. }
  4290. pc_setreg(sd, add_str("@menu"), menu);
  4291. script_pushint(st, menu);
  4292. st->state = RUN;
  4293. }
  4294. return 0;
  4295. }
  4296. /////////////////////////////////////////////////////////////////////
  4297. // ...
  4298. //
  4299. /// Jumps to the target script label.
  4300. ///
  4301. /// goto <label>;
  4302. BUILDIN_FUNC(goto)
  4303. {
  4304. if( !data_islabel(script_getdata(st,2)) )
  4305. {
  4306. ShowError("script:goto: not a label\n");
  4307. script_reportdata(script_getdata(st,2));
  4308. st->state = END;
  4309. return 1;
  4310. }
  4311. st->pos = script_getnum(st,2);
  4312. st->state = GOTO;
  4313. return 0;
  4314. }
  4315. /*==========================================
  4316. * user-defined function call
  4317. *------------------------------------------*/
  4318. BUILDIN_FUNC(callfunc)
  4319. {
  4320. int i, j;
  4321. struct script_retinfo* ri;
  4322. struct script_code* scr;
  4323. const char* str = script_getstr(st,2);
  4324. DBMap **ref = NULL;
  4325. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4326. if( !scr )
  4327. {
  4328. ShowError("script:callfunc: function not found! [%s]\n", str);
  4329. st->state = END;
  4330. return 1;
  4331. }
  4332. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4333. {
  4334. struct script_data* data = push_copy(st->stack,i);
  4335. if( data_isreference(data) && !data->ref )
  4336. {
  4337. const char* name = reference_getname(data);
  4338. if( name[0] == '.' ) {
  4339. if(!ref){
  4340. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4341. ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars);
  4342. }
  4343. data->ref = ref;
  4344. }
  4345. }
  4346. }
  4347. CREATE(ri, struct script_retinfo, 1);
  4348. ri->script = st->script;// script code
  4349. ri->var_function = st->stack->var_function;// scope variables
  4350. ri->pos = st->pos;// script location
  4351. ri->nargs = j;// argument count
  4352. ri->defsp = st->stack->defsp;// default stack pointer
  4353. push_retinfo(st->stack, ri, ref);
  4354. st->pos = 0;
  4355. st->script = scr;
  4356. st->stack->defsp = st->stack->sp;
  4357. st->state = GOTO;
  4358. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4359. return 0;
  4360. }
  4361. /*==========================================
  4362. * subroutine call
  4363. *------------------------------------------*/
  4364. BUILDIN_FUNC(callsub)
  4365. {
  4366. int i,j;
  4367. struct script_retinfo* ri;
  4368. int pos = script_getnum(st,2);
  4369. DBMap **ref = NULL;
  4370. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4371. {
  4372. ShowError("script:callsub: argument is not a label\n");
  4373. script_reportdata(script_getdata(st,2));
  4374. st->state = END;
  4375. return 1;
  4376. }
  4377. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4378. {
  4379. struct script_data* data = push_copy(st->stack,i);
  4380. if( data_isreference(data) && !data->ref )
  4381. {
  4382. const char* name = reference_getname(data);
  4383. if( name[0] == '.' && name[1] == '@' ) {
  4384. if ( !ref ) {
  4385. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4386. ref[0] = st->stack->var_function;
  4387. }
  4388. data->ref = ref;
  4389. }
  4390. }
  4391. }
  4392. CREATE(ri, struct script_retinfo, 1);
  4393. ri->script = st->script;// script code
  4394. ri->var_function = st->stack->var_function;// scope variables
  4395. ri->pos = st->pos;// script location
  4396. ri->nargs = j;// argument count
  4397. ri->defsp = st->stack->defsp;// default stack pointer
  4398. push_retinfo(st->stack, ri, ref);
  4399. st->pos = pos;
  4400. st->stack->defsp = st->stack->sp;
  4401. st->state = GOTO;
  4402. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4403. return 0;
  4404. }
  4405. /// Retrieves an argument provided to callfunc/callsub.
  4406. /// If the argument doesn't exist
  4407. ///
  4408. /// getarg(<index>{,<default_value>}) -> <value>
  4409. BUILDIN_FUNC(getarg)
  4410. {
  4411. struct script_retinfo* ri;
  4412. int idx;
  4413. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4414. {
  4415. ShowError("script:getarg: no callfunc or callsub!\n");
  4416. st->state = END;
  4417. return 1;
  4418. }
  4419. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4420. idx = script_getnum(st,2);
  4421. if( idx >= 0 && idx < ri->nargs )
  4422. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4423. else if( script_hasdata(st,3) )
  4424. script_pushcopy(st, 3);
  4425. else
  4426. {
  4427. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4428. st->state = END;
  4429. return 1;
  4430. }
  4431. return 0;
  4432. }
  4433. /// Returns from the current function, optionaly returning a value from the functions.
  4434. /// Don't use outside script functions.
  4435. ///
  4436. /// return;
  4437. /// return <value>;
  4438. BUILDIN_FUNC(return)
  4439. {
  4440. if( script_hasdata(st,2) )
  4441. {// return value
  4442. struct script_data* data;
  4443. script_pushcopy(st, 2);
  4444. data = script_getdatatop(st, -1);
  4445. if( data_isreference(data) )
  4446. {
  4447. const char* name = reference_getname(data);
  4448. if( name[0] == '.' && name[1] == '@' )
  4449. {// scope variable
  4450. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4451. get_val(st, data);// current scope, convert to value
  4452. }
  4453. else if( name[0] == '.' && !data->ref )
  4454. {// script variable, link to current script
  4455. data->ref = &st->script->script_vars;
  4456. }
  4457. }
  4458. }
  4459. else
  4460. {// no return value
  4461. script_pushnil(st);
  4462. }
  4463. st->state = RETFUNC;
  4464. return 0;
  4465. }
  4466. /// Returns a random number from 0 to <range>-1.
  4467. /// Or returns a random number from <min> to <max>.
  4468. /// If <min> is greater than <max>, their numbers are switched.
  4469. /// rand(<range>) -> <int>
  4470. /// rand(<min>,<max>) -> <int>
  4471. BUILDIN_FUNC(rand)
  4472. {
  4473. int range;
  4474. int min;
  4475. int max;
  4476. if( script_hasdata(st,3) )
  4477. {// min,max
  4478. min = script_getnum(st,2);
  4479. max = script_getnum(st,3);
  4480. if( max < min )
  4481. swap(min, max);
  4482. range = max - min + 1;
  4483. }
  4484. else
  4485. {// range
  4486. min = 0;
  4487. range = script_getnum(st,2);
  4488. }
  4489. if( range <= 1 )
  4490. script_pushint(st, min);
  4491. else
  4492. script_pushint(st, rnd()%range + min);
  4493. return 0;
  4494. }
  4495. /*==========================================
  4496. * Warp sd to str,x,y or Random or SavePoint/Save
  4497. *------------------------------------------*/
  4498. BUILDIN_FUNC(warp)
  4499. {
  4500. int ret;
  4501. int x,y;
  4502. const char* str;
  4503. TBL_PC* sd;
  4504. sd = script_rid2sd(st);
  4505. if( sd == NULL )
  4506. return 0;
  4507. str = script_getstr(st,2);
  4508. x = script_getnum(st,3);
  4509. y = script_getnum(st,4);
  4510. if(strcmp(str,"Random")==0)
  4511. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4512. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4513. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4514. else
  4515. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4516. if( ret ) {
  4517. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4518. script_reportsrc(st);
  4519. }
  4520. return 0;
  4521. }
  4522. /*==========================================
  4523. * Warp a specified area
  4524. *------------------------------------------*/
  4525. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4526. {
  4527. int x2,y2,x3,y3;
  4528. unsigned int index;
  4529. index = va_arg(ap,unsigned int);
  4530. x2 = va_arg(ap,int);
  4531. y2 = va_arg(ap,int);
  4532. x3 = va_arg(ap,int);
  4533. y3 = va_arg(ap,int);
  4534. if(index == 0)
  4535. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4536. else if(x3 && y3) {
  4537. int max, tx, ty, j = 0;
  4538. // choose a suitable max number of attempts
  4539. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4540. max = 1000;
  4541. // find a suitable map cell
  4542. do {
  4543. tx = rnd()%(x3-x2+1)+x2;
  4544. ty = rnd()%(y3-y2+1)+y2;
  4545. j++;
  4546. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4547. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4548. }
  4549. else
  4550. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4551. return 0;
  4552. }
  4553. BUILDIN_FUNC(areawarp)
  4554. {
  4555. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4556. unsigned int index;
  4557. const char *str;
  4558. const char *mapname;
  4559. mapname = script_getstr(st,2);
  4560. x0 = script_getnum(st,3);
  4561. y0 = script_getnum(st,4);
  4562. x1 = script_getnum(st,5);
  4563. y1 = script_getnum(st,6);
  4564. str = script_getstr(st,7);
  4565. x2 = script_getnum(st,8);
  4566. y2 = script_getnum(st,9);
  4567. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4568. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4569. x3 = 0;
  4570. y3 = 0;
  4571. } else if( x3 && y3 ) {
  4572. // normalize x3/y3 coordinates
  4573. if( x3 < x2 ) swap(x3,x2);
  4574. if( y3 < y2 ) swap(y3,y2);
  4575. }
  4576. }
  4577. if( (m = map_mapname2mapid(mapname)) < 0 )
  4578. return 0;
  4579. if( strcmp(str,"Random") == 0 )
  4580. index = 0;
  4581. else if( !(index=mapindex_name2id(str)) )
  4582. return 0;
  4583. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4584. return 0;
  4585. }
  4586. /*==========================================
  4587. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4588. *------------------------------------------*/
  4589. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4590. {
  4591. int hp, sp;
  4592. hp = va_arg(ap, int);
  4593. sp = va_arg(ap, int);
  4594. pc_percentheal((TBL_PC *)bl,hp,sp);
  4595. return 0;
  4596. }
  4597. BUILDIN_FUNC(areapercentheal)
  4598. {
  4599. int hp,sp,m;
  4600. const char *mapname;
  4601. int x0,y0,x1,y1;
  4602. mapname=script_getstr(st,2);
  4603. x0=script_getnum(st,3);
  4604. y0=script_getnum(st,4);
  4605. x1=script_getnum(st,5);
  4606. y1=script_getnum(st,6);
  4607. hp=script_getnum(st,7);
  4608. sp=script_getnum(st,8);
  4609. if( (m=map_mapname2mapid(mapname))< 0)
  4610. return 0;
  4611. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4612. return 0;
  4613. }
  4614. /*==========================================
  4615. * warpchar [LuzZza]
  4616. * Useful for warp one player from
  4617. * another player npc-session.
  4618. * Using: warpchar "mapname",x,y,Char_ID;
  4619. *------------------------------------------*/
  4620. BUILDIN_FUNC(warpchar)
  4621. {
  4622. int x,y,a;
  4623. const char *str;
  4624. TBL_PC *sd;
  4625. str=script_getstr(st,2);
  4626. x=script_getnum(st,3);
  4627. y=script_getnum(st,4);
  4628. a=script_getnum(st,5);
  4629. sd = map_charid2sd(a);
  4630. if( sd == NULL )
  4631. return 0;
  4632. if(strcmp(str, "Random") == 0)
  4633. pc_randomwarp(sd, CLR_TELEPORT);
  4634. else
  4635. if(strcmp(str, "SavePoint") == 0)
  4636. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4637. else
  4638. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4639. return 0;
  4640. }
  4641. /*==========================================
  4642. * Warpparty - [Fredzilla] [Paradox924X]
  4643. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4644. * If 'from_mapname' is specified, only the party members on that map will be warped
  4645. *------------------------------------------*/
  4646. BUILDIN_FUNC(warpparty)
  4647. {
  4648. TBL_PC *sd = NULL;
  4649. TBL_PC *pl_sd;
  4650. struct party_data* p;
  4651. int type;
  4652. int mapindex;
  4653. int i;
  4654. const char* str = script_getstr(st,2);
  4655. int x = script_getnum(st,3);
  4656. int y = script_getnum(st,4);
  4657. int p_id = script_getnum(st,5);
  4658. const char* str2 = NULL;
  4659. if ( script_hasdata(st,6) )
  4660. str2 = script_getstr(st,6);
  4661. p = party_search(p_id);
  4662. if(!p)
  4663. return 0;
  4664. type = ( strcmp(str,"Random")==0 ) ? 0
  4665. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4666. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4667. : ( strcmp(str,"Leader")==0 ) ? 3
  4668. : 4;
  4669. switch (type)
  4670. {
  4671. case 3:
  4672. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4673. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4674. return 0;
  4675. pl_sd = p->data[i].sd;
  4676. mapindex = pl_sd->mapindex;
  4677. x = pl_sd->bl.x;
  4678. y = pl_sd->bl.y;
  4679. break;
  4680. case 4:
  4681. mapindex = mapindex_name2id(str);
  4682. break;
  4683. case 2:
  4684. //"SavePoint" uses save point of the currently attached player
  4685. if (( sd = script_rid2sd(st) ) == NULL )
  4686. return 0;
  4687. default:
  4688. mapindex = 0;
  4689. break;
  4690. }
  4691. for (i = 0; i < MAX_PARTY; i++)
  4692. {
  4693. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4694. continue;
  4695. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4696. continue;
  4697. if( pc_isdead(pl_sd) )
  4698. continue;
  4699. switch( type )
  4700. {
  4701. case 0: // Random
  4702. if(!map[pl_sd->bl.m].flag.nowarp)
  4703. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4704. break;
  4705. case 1: // SavePointAll
  4706. if(!map[pl_sd->bl.m].flag.noreturn)
  4707. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4708. break;
  4709. case 2: // SavePoint
  4710. if(!map[pl_sd->bl.m].flag.noreturn)
  4711. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4712. break;
  4713. case 3: // Leader
  4714. case 4: // m,x,y
  4715. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4716. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4717. break;
  4718. }
  4719. }
  4720. return 0;
  4721. }
  4722. /*==========================================
  4723. * Warpguild - [Fredzilla]
  4724. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4725. *------------------------------------------*/
  4726. BUILDIN_FUNC(warpguild)
  4727. {
  4728. TBL_PC *sd = NULL;
  4729. TBL_PC *pl_sd;
  4730. struct guild* g;
  4731. struct s_mapiterator* iter;
  4732. int type;
  4733. const char* str = script_getstr(st,2);
  4734. int x = script_getnum(st,3);
  4735. int y = script_getnum(st,4);
  4736. int gid = script_getnum(st,5);
  4737. g = guild_search(gid);
  4738. if( g == NULL )
  4739. return 0;
  4740. type = ( strcmp(str,"Random")==0 ) ? 0
  4741. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4742. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4743. : 3;
  4744. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4745. {// "SavePoint" uses save point of the currently attached player
  4746. return 0;
  4747. }
  4748. iter = mapit_getallusers();
  4749. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4750. {
  4751. if( pl_sd->status.guild_id != gid )
  4752. continue;
  4753. switch( type )
  4754. {
  4755. case 0: // Random
  4756. if(!map[pl_sd->bl.m].flag.nowarp)
  4757. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4758. break;
  4759. case 1: // SavePointAll
  4760. if(!map[pl_sd->bl.m].flag.noreturn)
  4761. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4762. break;
  4763. case 2: // SavePoint
  4764. if(!map[pl_sd->bl.m].flag.noreturn)
  4765. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4766. break;
  4767. case 3: // m,x,y
  4768. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4769. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4770. break;
  4771. }
  4772. }
  4773. mapit_free(iter);
  4774. return 0;
  4775. }
  4776. /*==========================================
  4777. * Force Heal a player (hp and sp)
  4778. *------------------------------------------*/
  4779. BUILDIN_FUNC(heal)
  4780. {
  4781. TBL_PC *sd;
  4782. int hp,sp;
  4783. sd = script_rid2sd(st);
  4784. if (!sd) return 0;
  4785. hp=script_getnum(st,2);
  4786. sp=script_getnum(st,3);
  4787. status_heal(&sd->bl, hp, sp, 1);
  4788. return 0;
  4789. }
  4790. /*==========================================
  4791. * Heal a player by item (get vit bonus etc)
  4792. *------------------------------------------*/
  4793. BUILDIN_FUNC(itemheal)
  4794. {
  4795. TBL_PC *sd;
  4796. int hp,sp;
  4797. hp=script_getnum(st,2);
  4798. sp=script_getnum(st,3);
  4799. if(potion_flag==1) {
  4800. potion_hp = hp;
  4801. potion_sp = sp;
  4802. return 0;
  4803. }
  4804. sd = script_rid2sd(st);
  4805. if (!sd) return 0;
  4806. pc_itemheal(sd,sd->itemid,hp,sp);
  4807. return 0;
  4808. }
  4809. /*==========================================
  4810. *
  4811. *------------------------------------------*/
  4812. BUILDIN_FUNC(percentheal)
  4813. {
  4814. int hp,sp;
  4815. TBL_PC* sd;
  4816. hp=script_getnum(st,2);
  4817. sp=script_getnum(st,3);
  4818. if(potion_flag==1) {
  4819. potion_per_hp = hp;
  4820. potion_per_sp = sp;
  4821. return 0;
  4822. }
  4823. sd = script_rid2sd(st);
  4824. if( sd == NULL )
  4825. return 0;
  4826. #ifdef RENEWAL
  4827. if( sd->sc.data[SC_EXTREMITYFIST2] )
  4828. sp = 0;
  4829. #endif
  4830. pc_percentheal(sd,hp,sp);
  4831. return 0;
  4832. }
  4833. /*==========================================
  4834. *
  4835. *------------------------------------------*/
  4836. BUILDIN_FUNC(jobchange)
  4837. {
  4838. int job, upper=-1;
  4839. job=script_getnum(st,2);
  4840. if( script_hasdata(st,3) )
  4841. upper=script_getnum(st,3);
  4842. if (pcdb_checkid(job))
  4843. {
  4844. TBL_PC* sd;
  4845. sd = script_rid2sd(st);
  4846. if( sd == NULL )
  4847. return 0;
  4848. pc_jobchange(sd, job, upper);
  4849. }
  4850. return 0;
  4851. }
  4852. /*==========================================
  4853. *
  4854. *------------------------------------------*/
  4855. BUILDIN_FUNC(jobname)
  4856. {
  4857. int class_=script_getnum(st,2);
  4858. script_pushconststr(st, (char*)job_name(class_));
  4859. return 0;
  4860. }
  4861. /// Get input from the player.
  4862. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4863. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4864. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4865. /// shorter than 'min' and 0 otherwise.
  4866. ///
  4867. /// input(<var>{,<min>{,<max>}}) -> <int>
  4868. BUILDIN_FUNC(input)
  4869. {
  4870. TBL_PC* sd;
  4871. struct script_data* data;
  4872. int uid;
  4873. const char* name;
  4874. int min;
  4875. int max;
  4876. sd = script_rid2sd(st);
  4877. if( sd == NULL )
  4878. return 0;
  4879. data = script_getdata(st,2);
  4880. if( !data_isreference(data) ){
  4881. ShowError("script:input: not a variable\n");
  4882. script_reportdata(data);
  4883. st->state = END;
  4884. return 1;
  4885. }
  4886. uid = reference_getuid(data);
  4887. name = reference_getname(data);
  4888. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4889. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4890. #ifdef SECURE_NPCTIMEOUT
  4891. sd->npc_idle_type = NPCT_WAIT;
  4892. #endif
  4893. if( !sd->state.menu_or_input )
  4894. { // first invocation, display npc input box
  4895. sd->state.menu_or_input = 1;
  4896. st->state = RERUNLINE;
  4897. if( is_string_variable(name) )
  4898. clif_scriptinputstr(sd,st->oid);
  4899. else
  4900. clif_scriptinput(sd,st->oid);
  4901. }
  4902. else
  4903. { // take received text/value and store it in the designated variable
  4904. sd->state.menu_or_input = 0;
  4905. if( is_string_variable(name) )
  4906. {
  4907. int len = (int)strlen(sd->npc_str);
  4908. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4909. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4910. }
  4911. else
  4912. {
  4913. int amount = sd->npc_amount;
  4914. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  4915. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4916. }
  4917. st->state = RUN;
  4918. }
  4919. return 0;
  4920. }
  4921. // declare the copyarray method here for future reference
  4922. BUILDIN_FUNC(copyarray);
  4923. /// Sets the value of a variable.
  4924. /// The value is converted to the type of the variable.
  4925. ///
  4926. /// set(<variable>,<value>) -> <variable>
  4927. BUILDIN_FUNC(set)
  4928. {
  4929. TBL_PC* sd = NULL;
  4930. struct script_data* data;
  4931. //struct script_data* datavalue;
  4932. int num;
  4933. const char* name;
  4934. char prefix;
  4935. data = script_getdata(st,2);
  4936. //datavalue = script_getdata(st,3);
  4937. if( !data_isreference(data) )
  4938. {
  4939. ShowError("script:set: not a variable\n");
  4940. script_reportdata(script_getdata(st,2));
  4941. st->state = END;
  4942. return 1;
  4943. }
  4944. num = reference_getuid(data);
  4945. name = reference_getname(data);
  4946. prefix = *name;
  4947. if( not_server_variable(prefix) )
  4948. {
  4949. sd = script_rid2sd(st);
  4950. if( sd == NULL )
  4951. {
  4952. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4953. return 0;
  4954. }
  4955. }
  4956. #if 0
  4957. if( data_isreference(datavalue) )
  4958. {// the value being referenced is a variable
  4959. const char* namevalue = reference_getname(datavalue);
  4960. if( !not_array_variable(*namevalue) )
  4961. {// array variable being copied into another array variable
  4962. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  4963. {// player must be attached in order to copy a player variable
  4964. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  4965. return 0;
  4966. }
  4967. if( is_string_variable(namevalue) != is_string_variable(name) )
  4968. {// non-matching array value types
  4969. ShowWarning("script:set: two array variables do not match in type.\n");
  4970. return 0;
  4971. }
  4972. // push the maximum number of array values to the stack
  4973. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  4974. // call the copy array method directly
  4975. return buildin_copyarray(st);
  4976. }
  4977. }
  4978. #endif
  4979. if( is_string_variable(name) )
  4980. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4981. else
  4982. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  4983. // return a copy of the variable reference
  4984. script_pushcopy(st,2);
  4985. return 0;
  4986. }
  4987. /////////////////////////////////////////////////////////////////////
  4988. /// Array variables
  4989. ///
  4990. /// Returns the size of the specified array
  4991. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  4992. {
  4993. int32 ret = idx;
  4994. if( isstring )
  4995. {
  4996. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4997. {
  4998. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4999. if( str && *str )
  5000. ret = idx + 1;
  5001. script_removetop(st, -1, 0);
  5002. }
  5003. }
  5004. else
  5005. {
  5006. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5007. {
  5008. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  5009. if( num )
  5010. ret = idx + 1;
  5011. script_removetop(st, -1, 0);
  5012. }
  5013. }
  5014. return ret;
  5015. }
  5016. /// Sets values of an array, from the starting index.
  5017. /// ex: setarray arr[1],1,2,3;
  5018. ///
  5019. /// setarray <array variable>,<value1>{,<value2>...};
  5020. BUILDIN_FUNC(setarray)
  5021. {
  5022. struct script_data* data;
  5023. const char* name;
  5024. int32 start;
  5025. int32 end;
  5026. int32 id;
  5027. int32 i;
  5028. TBL_PC* sd = NULL;
  5029. data = script_getdata(st, 2);
  5030. if( !data_isreference(data) )
  5031. {
  5032. ShowError("script:setarray: not a variable\n");
  5033. script_reportdata(data);
  5034. st->state = END;
  5035. return 1;// not a variable
  5036. }
  5037. id = reference_getid(data);
  5038. start = reference_getindex(data);
  5039. name = reference_getname(data);
  5040. if( not_array_variable(*name) )
  5041. {
  5042. ShowError("script:setarray: illegal scope\n");
  5043. script_reportdata(data);
  5044. st->state = END;
  5045. return 1;// not supported
  5046. }
  5047. if( not_server_variable(*name) )
  5048. {
  5049. sd = script_rid2sd(st);
  5050. if( sd == NULL )
  5051. return 0;// no player attached
  5052. }
  5053. end = start + script_lastdata(st) - 2;
  5054. if( end > SCRIPT_MAX_ARRAYSIZE )
  5055. end = SCRIPT_MAX_ARRAYSIZE;
  5056. if( is_string_variable(name) )
  5057. {// string array
  5058. for( i = 3; start < end; ++start, ++i )
  5059. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5060. }
  5061. else
  5062. {// int array
  5063. for( i = 3; start < end; ++start, ++i )
  5064. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5065. }
  5066. return 0;
  5067. }
  5068. /// Sets count values of an array, from the starting index.
  5069. /// ex: cleararray arr[0],0,1;
  5070. ///
  5071. /// cleararray <array variable>,<value>,<count>;
  5072. BUILDIN_FUNC(cleararray)
  5073. {
  5074. struct script_data* data;
  5075. const char* name;
  5076. int32 start;
  5077. int32 end;
  5078. int32 id;
  5079. void* v;
  5080. TBL_PC* sd = NULL;
  5081. data = script_getdata(st, 2);
  5082. if( !data_isreference(data) )
  5083. {
  5084. ShowError("script:cleararray: not a variable\n");
  5085. script_reportdata(data);
  5086. st->state = END;
  5087. return 1;// not a variable
  5088. }
  5089. id = reference_getid(data);
  5090. start = reference_getindex(data);
  5091. name = reference_getname(data);
  5092. if( not_array_variable(*name) )
  5093. {
  5094. ShowError("script:cleararray: illegal scope\n");
  5095. script_reportdata(data);
  5096. st->state = END;
  5097. return 1;// not supported
  5098. }
  5099. if( not_server_variable(*name) )
  5100. {
  5101. sd = script_rid2sd(st);
  5102. if( sd == NULL )
  5103. return 0;// no player attached
  5104. }
  5105. if( is_string_variable(name) )
  5106. v = (void*)script_getstr(st, 3);
  5107. else
  5108. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5109. end = start + script_getnum(st, 4);
  5110. if( end > SCRIPT_MAX_ARRAYSIZE )
  5111. end = SCRIPT_MAX_ARRAYSIZE;
  5112. for( ; start < end; ++start )
  5113. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5114. return 0;
  5115. }
  5116. /// Copies data from one array to another.
  5117. /// ex: copyarray arr[0],arr[2],2;
  5118. ///
  5119. /// copyarray <destination array variable>,<source array variable>,<count>;
  5120. BUILDIN_FUNC(copyarray)
  5121. {
  5122. struct script_data* data1;
  5123. struct script_data* data2;
  5124. const char* name1;
  5125. const char* name2;
  5126. int32 idx1;
  5127. int32 idx2;
  5128. int32 id1;
  5129. int32 id2;
  5130. void* v;
  5131. int32 i;
  5132. int32 count;
  5133. TBL_PC* sd = NULL;
  5134. data1 = script_getdata(st, 2);
  5135. data2 = script_getdata(st, 3);
  5136. if( !data_isreference(data1) || !data_isreference(data2) )
  5137. {
  5138. ShowError("script:copyarray: not a variable\n");
  5139. script_reportdata(data1);
  5140. script_reportdata(data2);
  5141. st->state = END;
  5142. return 1;// not a variable
  5143. }
  5144. id1 = reference_getid(data1);
  5145. id2 = reference_getid(data2);
  5146. idx1 = reference_getindex(data1);
  5147. idx2 = reference_getindex(data2);
  5148. name1 = reference_getname(data1);
  5149. name2 = reference_getname(data2);
  5150. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5151. {
  5152. ShowError("script:copyarray: illegal scope\n");
  5153. script_reportdata(data1);
  5154. script_reportdata(data2);
  5155. st->state = END;
  5156. return 1;// not supported
  5157. }
  5158. if( is_string_variable(name1) != is_string_variable(name2) )
  5159. {
  5160. ShowError("script:copyarray: type mismatch\n");
  5161. script_reportdata(data1);
  5162. script_reportdata(data2);
  5163. st->state = END;
  5164. return 1;// data type mismatch
  5165. }
  5166. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5167. {
  5168. sd = script_rid2sd(st);
  5169. if( sd == NULL )
  5170. return 0;// no player attached
  5171. }
  5172. count = script_getnum(st, 4);
  5173. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5174. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5175. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5176. return 0;// nothing to copy
  5177. if( id1 == id2 && idx1 > idx2 )
  5178. {// destination might be overlapping the source - copy in reverse order
  5179. for( i = count - 1; i >= 0; --i )
  5180. {
  5181. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5182. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5183. script_removetop(st, -1, 0);
  5184. }
  5185. }
  5186. else
  5187. {// normal copy
  5188. for( i = 0; i < count; ++i )
  5189. {
  5190. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5191. {
  5192. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5193. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5194. script_removetop(st, -1, 0);
  5195. }
  5196. else// out of range - assume ""/0
  5197. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5198. }
  5199. }
  5200. return 0;
  5201. }
  5202. /// Returns the size of the array.
  5203. /// Assumes that everything before the starting index exists.
  5204. /// ex: getarraysize(arr[3])
  5205. ///
  5206. /// getarraysize(<array variable>) -> <int>
  5207. BUILDIN_FUNC(getarraysize)
  5208. {
  5209. struct script_data* data;
  5210. const char* name;
  5211. data = script_getdata(st, 2);
  5212. if( !data_isreference(data) )
  5213. {
  5214. ShowError("script:getarraysize: not a variable\n");
  5215. script_reportdata(data);
  5216. script_pushnil(st);
  5217. st->state = END;
  5218. return 1;// not a variable
  5219. }
  5220. name = reference_getname(data);
  5221. if( not_array_variable(*name) )
  5222. {
  5223. ShowError("script:getarraysize: illegal scope\n");
  5224. script_reportdata(data);
  5225. script_pushnil(st);
  5226. st->state = END;
  5227. return 1;// not supported
  5228. }
  5229. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5230. return 0;
  5231. }
  5232. /// Deletes count or all the elements in an array, from the starting index.
  5233. /// ex: deletearray arr[4],2;
  5234. ///
  5235. /// deletearray <array variable>;
  5236. /// deletearray <array variable>,<count>;
  5237. BUILDIN_FUNC(deletearray)
  5238. {
  5239. struct script_data* data;
  5240. const char* name;
  5241. int start;
  5242. int end;
  5243. int id;
  5244. TBL_PC *sd = NULL;
  5245. data = script_getdata(st, 2);
  5246. if( !data_isreference(data) )
  5247. {
  5248. ShowError("script:deletearray: not a variable\n");
  5249. script_reportdata(data);
  5250. st->state = END;
  5251. return 1;// not a variable
  5252. }
  5253. id = reference_getid(data);
  5254. start = reference_getindex(data);
  5255. name = reference_getname(data);
  5256. if( not_array_variable(*name) )
  5257. {
  5258. ShowError("script:deletearray: illegal scope\n");
  5259. script_reportdata(data);
  5260. st->state = END;
  5261. return 1;// not supported
  5262. }
  5263. if( not_server_variable(*name) )
  5264. {
  5265. sd = script_rid2sd(st);
  5266. if( sd == NULL )
  5267. return 0;// no player attached
  5268. }
  5269. end = SCRIPT_MAX_ARRAYSIZE;
  5270. if( start >= end )
  5271. return 0;// nothing to free
  5272. if( script_hasdata(st,3) )
  5273. {
  5274. int count = script_getnum(st, 3);
  5275. if( count > end - start )
  5276. count = end - start;
  5277. if( count <= 0 )
  5278. return 0;// nothing to free
  5279. // move rest of the elements backward
  5280. for( ; start + count < end; ++start )
  5281. {
  5282. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5283. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5284. script_removetop(st, -1, 0);
  5285. }
  5286. }
  5287. // clear the rest of the array
  5288. if( is_string_variable(name) )
  5289. {
  5290. for( ; start < end; ++start )
  5291. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5292. }
  5293. else
  5294. {
  5295. for( ; start < end; ++start )
  5296. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5297. }
  5298. return 0;
  5299. }
  5300. /// Returns a reference to the target index of the array variable.
  5301. /// Equivalent to var[index].
  5302. ///
  5303. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5304. BUILDIN_FUNC(getelementofarray)
  5305. {
  5306. struct script_data* data;
  5307. const char* name;
  5308. int32 id;
  5309. int i;
  5310. data = script_getdata(st, 2);
  5311. if( !data_isreference(data) )
  5312. {
  5313. ShowError("script:getelementofarray: not a variable\n");
  5314. script_reportdata(data);
  5315. script_pushnil(st);
  5316. st->state = END;
  5317. return 1;// not a variable
  5318. }
  5319. id = reference_getid(data);
  5320. name = reference_getname(data);
  5321. if( not_array_variable(*name) )
  5322. {
  5323. ShowError("script:getelementofarray: illegal scope\n");
  5324. script_reportdata(data);
  5325. script_pushnil(st);
  5326. st->state = END;
  5327. return 1;// not supported
  5328. }
  5329. i = script_getnum(st, 3);
  5330. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  5331. {
  5332. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5333. script_reportdata(data);
  5334. script_pushnil(st);
  5335. st->state = END;
  5336. return 1;// out of range
  5337. }
  5338. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5339. return 0;
  5340. }
  5341. /////////////////////////////////////////////////////////////////////
  5342. /// ...
  5343. ///
  5344. /*==========================================
  5345. *
  5346. *------------------------------------------*/
  5347. BUILDIN_FUNC(setlook)
  5348. {
  5349. int type,val;
  5350. TBL_PC* sd;
  5351. type=script_getnum(st,2);
  5352. val=script_getnum(st,3);
  5353. sd = script_rid2sd(st);
  5354. if( sd == NULL )
  5355. return 0;
  5356. pc_changelook(sd,type,val);
  5357. return 0;
  5358. }
  5359. BUILDIN_FUNC(changelook)
  5360. { // As setlook but only client side
  5361. int type,val;
  5362. TBL_PC* sd;
  5363. type=script_getnum(st,2);
  5364. val=script_getnum(st,3);
  5365. sd = script_rid2sd(st);
  5366. if( sd == NULL )
  5367. return 0;
  5368. clif_changelook(&sd->bl,type,val);
  5369. return 0;
  5370. }
  5371. /*==========================================
  5372. *
  5373. *------------------------------------------*/
  5374. BUILDIN_FUNC(cutin)
  5375. {
  5376. TBL_PC* sd;
  5377. sd = script_rid2sd(st);
  5378. if( sd == NULL )
  5379. return 0;
  5380. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5381. return 0;
  5382. }
  5383. /*==========================================
  5384. *
  5385. *------------------------------------------*/
  5386. BUILDIN_FUNC(viewpoint)
  5387. {
  5388. int type,x,y,id,color;
  5389. TBL_PC* sd;
  5390. type=script_getnum(st,2);
  5391. x=script_getnum(st,3);
  5392. y=script_getnum(st,4);
  5393. id=script_getnum(st,5);
  5394. color=script_getnum(st,6);
  5395. sd = script_rid2sd(st);
  5396. if( sd == NULL )
  5397. return 0;
  5398. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5399. return 0;
  5400. }
  5401. /*==========================================
  5402. * countitem(nameID)
  5403. * returns number of items in inventory
  5404. *==========================================
  5405. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  5406. * returns number of items that meet the conditions
  5407. *------------------------------------------*/
  5408. BUILDIN_FUNC(countitem)
  5409. {
  5410. int i, count = 0;
  5411. struct item_data* id = NULL;
  5412. struct script_data* data;
  5413. TBL_PC* sd = script_rid2sd(st);
  5414. if (!sd) {
  5415. script_pushint(st,0);
  5416. return 0;
  5417. }
  5418. data = script_getdata(st,2);
  5419. get_val(st, data); // convert into value in case of a variable
  5420. if( data_isstring(data) ) // item name
  5421. id = itemdb_searchname(conv_str(st, data));
  5422. else // item id
  5423. id = itemdb_exists(conv_num(st, data));
  5424. if( id == NULL ) {
  5425. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5426. script_pushint(st,0);
  5427. return 1;
  5428. }
  5429. if(script_lastdata(st) == 2) { // For countitem() function
  5430. int nameid = id->nameid;
  5431. for(i = 0; i < MAX_INVENTORY; i++)
  5432. if(sd->status.inventory[i].nameid == nameid)
  5433. count += sd->status.inventory[i].amount;
  5434. } else { // For countitem2() function
  5435. int nameid, iden, ref, attr, c1, c2, c3, c4;
  5436. nameid = id->nameid;
  5437. iden = script_getnum(st,3);
  5438. ref = script_getnum(st,4);
  5439. attr = script_getnum(st,5);
  5440. c1 = (short)script_getnum(st,6);
  5441. c2 = (short)script_getnum(st,7);
  5442. c3 = (short)script_getnum(st,8);
  5443. c4 = (short)script_getnum(st,9);
  5444. for(i = 0; i < MAX_INVENTORY; i++)
  5445. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  5446. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  5447. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  5448. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  5449. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  5450. sd->status.inventory[i].card[3] == c4 )
  5451. count += sd->status.inventory[i].amount;
  5452. }
  5453. script_pushint(st,count);
  5454. return 0;
  5455. }
  5456. int checkweight_sub(TBL_PC *sd,int nbargs,int32 *eitemid,int32 *eamount)
  5457. {
  5458. struct item_data* id = NULL;
  5459. int nameid,amount;
  5460. uint16 amount2=0,slots,weight=0,i;
  5461. slots = pc_inventoryblank(sd); //nb of empty slot
  5462. for(i=0; i<nbargs; i++) {
  5463. if(!eitemid[i])
  5464. continue;
  5465. id = itemdb_exists(eitemid[i]);
  5466. if( id == NULL ) {
  5467. ShowError("checkweight_sub: Invalid item '%d'.\n", eitemid[i]);
  5468. return 0;
  5469. }
  5470. nameid = id->nameid;
  5471. amount = eamount[i];
  5472. if( amount < 1 ) {
  5473. ShowError("checkweight_sub: Invalid amount '%d'.\n", eamount[i]);
  5474. return 0;
  5475. }
  5476. weight += (id->weight)*amount; //total weight for all chk
  5477. if( weight + sd->weight > sd->max_weight ) // too heavy
  5478. return 0;
  5479. switch( pc_checkadditem(sd, nameid, amount) ) {
  5480. case CHKADDITEM_EXIST: // item is already in inventory, but there is still space for the requested amount
  5481. break;
  5482. case CHKADDITEM_NEW:
  5483. if( itemdb_isstackable(nameid) )
  5484. amount2++; // stackable
  5485. else
  5486. amount2 += amount; // non-stackable
  5487. if( slots < amount2)
  5488. return 0;
  5489. break;
  5490. case CHKADDITEM_OVERAMOUNT:
  5491. return 0;
  5492. }
  5493. }
  5494. return 1;
  5495. }
  5496. /*==========================================
  5497. * Check if item with this amount can fit in inventory
  5498. * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY)
  5499. * Return
  5500. * 0 : fail
  5501. * 1 : success (npc side only)
  5502. *------------------------------------------*/
  5503. BUILDIN_FUNC(checkweight)
  5504. {
  5505. struct map_session_data* sd;
  5506. struct script_data* data;
  5507. struct item_data* id = NULL;
  5508. int32 nameid[SCRIPT_MAX_ARRAYSIZE], amount[SCRIPT_MAX_ARRAYSIZE];
  5509. uint16 nbargs,i,j=0;
  5510. if( ( sd = script_rid2sd(st) ) == NULL )
  5511. return 0;
  5512. nbargs = script_lastdata(st)+1;
  5513. if(nbargs%2) {
  5514. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5515. script_pushint(st,0);
  5516. return 1;
  5517. }
  5518. for(i=2; i<nbargs; i=i+2) {
  5519. data = script_getdata(st,i);
  5520. get_val(st, data); // convert into value in case of a variable
  5521. if( data_isstring(data) ) // item name
  5522. id = itemdb_searchname(conv_str(st, data));
  5523. else // item id
  5524. id = itemdb_exists(conv_num(st, data));
  5525. if( id == NULL ) {
  5526. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5527. script_pushint(st,0);
  5528. return 1;
  5529. }
  5530. nameid[j] = id->nameid;
  5531. amount[j] = script_getnum(st,i+1);
  5532. j++;
  5533. }
  5534. script_pushint(st,checkweight_sub(sd,(nbargs-2)/2,nameid,amount));
  5535. return 0;
  5536. }
  5537. BUILDIN_FUNC(checkweight2){
  5538. //variable sub checkweight
  5539. int32 nameid[SCRIPT_MAX_ARRAYSIZE], amount[SCRIPT_MAX_ARRAYSIZE], i;
  5540. //variable for array parsing
  5541. struct script_data* data_it;
  5542. struct script_data* data_nb;
  5543. const char* name_it;
  5544. const char* name_nb;
  5545. int32 id_it, id_nb;
  5546. int32 idx_it, idx_nb;
  5547. int nb_it, nb_nb; //array size
  5548. TBL_PC *sd = script_rid2sd(st);
  5549. nullpo_retr(1,sd);
  5550. data_it = script_getdata(st, 2);
  5551. data_nb = script_getdata(st, 3);
  5552. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5553. ShowError("buildin_checkweight2: parameter not a variable\n");
  5554. script_pushint(st,0);
  5555. return 1;// not a variable
  5556. }
  5557. id_it = reference_getid(data_it);
  5558. id_nb = reference_getid(data_nb);
  5559. idx_it = reference_getindex(data_it);
  5560. idx_nb = reference_getindex(data_nb);
  5561. name_it = reference_getname(data_it);
  5562. name_nb = reference_getname(data_nb);
  5563. if( not_array_variable(*name_it) || not_array_variable(*name_nb)) {
  5564. ShowError("buildin_checkweight2: illegal scope\n");
  5565. script_pushint(st,0);
  5566. return 1;// not supported
  5567. }
  5568. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5569. ShowError("buildin_checkweight2: illegal type, need int\n");
  5570. script_pushint(st,0);
  5571. return 1;// not supported
  5572. }
  5573. nb_it = getarraysize(st, id_it, idx_it, 0, reference_getref(data_it));
  5574. nb_nb = getarraysize(st, id_nb, idx_nb, 0, reference_getref(data_nb));
  5575. if(nb_it != nb_nb) {
  5576. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5577. script_pushint(st,0);
  5578. return 1;
  5579. }
  5580. for(i=0; i<nb_it; i++) {
  5581. nameid[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5582. script_removetop(st, -1, 0);
  5583. amount[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5584. script_removetop(st, -1, 0);
  5585. } //end loop DO NOT break it prematurly we need to depop all stack
  5586. script_pushint(st,checkweight_sub(sd,nb_it,nameid,amount)); //push result of sub to script
  5587. return 0;
  5588. }
  5589. /*==========================================
  5590. * getitem <item id>,<amount>{,<account ID>};
  5591. * getitem "<item name>",<amount>{,<account ID>};
  5592. *
  5593. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5594. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5595. * Type:
  5596. * 1 - Account Bound
  5597. * 2 - Guild Bound
  5598. * 3 - Party Bound
  5599. * 4 - Character Bound
  5600. *------------------------------------------*/
  5601. BUILDIN_FUNC(getitem)
  5602. {
  5603. int nameid,amount,get_count,i,flag = 0;
  5604. struct item it;
  5605. TBL_PC *sd;
  5606. struct script_data *data;
  5607. data=script_getdata(st,2);
  5608. get_val(st,data);
  5609. if( data_isstring(data) ) {// "<item name>"
  5610. const char *name=conv_str(st,data);
  5611. struct item_data *item_data = itemdb_searchname(name);
  5612. if( item_data == NULL ){
  5613. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5614. return 1; //No item created.
  5615. }
  5616. nameid=item_data->nameid;
  5617. } else if( data_isint(data) ) {// <item id>
  5618. nameid=conv_num(st,data);
  5619. //Violet Box, Blue Box, etc - random item pick
  5620. if( nameid < 0 ) {
  5621. nameid = -nameid;
  5622. flag = 1;
  5623. }
  5624. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5625. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5626. return 1; //No item created.
  5627. }
  5628. } else {
  5629. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5630. return 1;
  5631. }
  5632. // <amount>
  5633. if( (amount=script_getnum(st,3)) <= 0)
  5634. return 0; //return if amount <=0, skip the useles iteration
  5635. memset(&it,0,sizeof(it));
  5636. it.nameid=nameid;
  5637. if(!flag)
  5638. it.identify=1;
  5639. else
  5640. it.identify=itemdb_isidentified(nameid);
  5641. if( !strcmp(script_getfuncname(st),"getitembound") ) {
  5642. char bound = script_getnum(st,4);
  5643. if( bound < 1 || bound > 4) { //Not a correct bound type
  5644. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5645. return 1;
  5646. }
  5647. it.bound = bound;
  5648. if( script_hasdata(st,5) )
  5649. sd=map_id2sd(script_getnum(st,5));
  5650. else
  5651. sd=script_rid2sd(st); // Attached player
  5652. } else if( script_hasdata(st,4) )
  5653. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5654. else
  5655. sd=script_rid2sd(st); // Attached player
  5656. if( sd == NULL ) // no target
  5657. return 0;
  5658. //Check if it's stackable.
  5659. if (!itemdb_isstackable(nameid))
  5660. get_count = 1;
  5661. else
  5662. get_count = amount;
  5663. for (i = 0; i < amount; i += get_count)
  5664. {
  5665. // if not pet egg
  5666. if (!pet_create_egg(sd, nameid))
  5667. {
  5668. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5669. {
  5670. clif_additem(sd, 0, 0, flag);
  5671. if( pc_candrop(sd,&it) )
  5672. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5673. }
  5674. }
  5675. }
  5676. return 0;
  5677. }
  5678. /*==========================================
  5679. *
  5680. *------------------------------------------*/
  5681. BUILDIN_FUNC(getitem2)
  5682. {
  5683. int nameid,amount,get_count,i,flag = 0;
  5684. int iden,ref,attr,c1,c2,c3,c4;
  5685. char bound=0;
  5686. struct item_data *item_data;
  5687. struct item item_tmp;
  5688. TBL_PC *sd;
  5689. struct script_data *data;
  5690. if( !strcmp(script_getfuncname(st),"getitembound2") ) {
  5691. bound = script_getnum(st,11);
  5692. if( bound < 1 || bound > 3) { //Not a correct bound type
  5693. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  5694. return 1;
  5695. }
  5696. if( script_hasdata(st,12) )
  5697. sd=map_id2sd(script_getnum(st,12));
  5698. else
  5699. sd=script_rid2sd(st); // Attached player
  5700. } else if( script_hasdata(st,11) )
  5701. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5702. else
  5703. sd=script_rid2sd(st); // Attached player
  5704. if( sd == NULL ) // no target
  5705. return 0;
  5706. data=script_getdata(st,2);
  5707. get_val(st,data);
  5708. if( data_isstring(data) ) {
  5709. const char *name=conv_str(st,data);
  5710. struct item_data *item_data = itemdb_searchname(name);
  5711. if( item_data )
  5712. nameid=item_data->nameid;
  5713. else
  5714. nameid=UNKNOWN_ITEM_ID;
  5715. } else
  5716. nameid=conv_num(st,data);
  5717. amount=script_getnum(st,3);
  5718. iden=script_getnum(st,4);
  5719. ref=script_getnum(st,5);
  5720. attr=script_getnum(st,6);
  5721. c1=(short)script_getnum(st,7);
  5722. c2=(short)script_getnum(st,8);
  5723. c3=(short)script_getnum(st,9);
  5724. c4=(short)script_getnum(st,10);
  5725. if(nameid<0) { // Invalide nameid
  5726. nameid = -nameid;
  5727. flag = 1;
  5728. }
  5729. if(nameid > 0) {
  5730. memset(&item_tmp,0,sizeof(item_tmp));
  5731. item_data=itemdb_exists(nameid);
  5732. if (item_data == NULL)
  5733. return -1;
  5734. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5735. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5736. }
  5737. else if(item_data->type==IT_PETEGG) {
  5738. iden = 1;
  5739. ref = 0;
  5740. }
  5741. else {
  5742. iden = 1;
  5743. ref = attr = 0;
  5744. }
  5745. item_tmp.nameid=nameid;
  5746. if(!flag)
  5747. item_tmp.identify=iden;
  5748. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5749. item_tmp.identify=0;
  5750. item_tmp.refine=ref;
  5751. item_tmp.attribute=attr;
  5752. item_tmp.card[0]=(short)c1;
  5753. item_tmp.card[1]=(short)c2;
  5754. item_tmp.card[2]=(short)c3;
  5755. item_tmp.card[3]=(short)c4;
  5756. item_tmp.bound=bound;
  5757. //Check if it's stackable.
  5758. if (!itemdb_isstackable(nameid))
  5759. get_count = 1;
  5760. else
  5761. get_count = amount;
  5762. for (i = 0; i < amount; i += get_count)
  5763. {
  5764. // if not pet egg
  5765. if (!pet_create_egg(sd, nameid))
  5766. {
  5767. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5768. {
  5769. clif_additem(sd, 0, 0, flag);
  5770. if( pc_candrop(sd,&item_tmp) )
  5771. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5772. }
  5773. }
  5774. }
  5775. }
  5776. return 0;
  5777. }
  5778. /*==========================================
  5779. * rentitem <item id>,<seconds>
  5780. * rentitem "<item name>",<seconds>
  5781. *------------------------------------------*/
  5782. BUILDIN_FUNC(rentitem)
  5783. {
  5784. struct map_session_data *sd;
  5785. struct script_data *data;
  5786. struct item it;
  5787. int seconds;
  5788. int nameid = 0, flag;
  5789. data = script_getdata(st,2);
  5790. get_val(st,data);
  5791. if( (sd = script_rid2sd(st)) == NULL )
  5792. return 0;
  5793. if( data_isstring(data) )
  5794. {
  5795. const char *name = conv_str(st,data);
  5796. struct item_data *itd = itemdb_searchname(name);
  5797. if( itd == NULL )
  5798. {
  5799. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5800. return 1;
  5801. }
  5802. nameid = itd->nameid;
  5803. }
  5804. else if( data_isint(data) )
  5805. {
  5806. nameid = conv_num(st,data);
  5807. if( nameid <= 0 || !itemdb_exists(nameid) )
  5808. {
  5809. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5810. return 1;
  5811. }
  5812. }
  5813. else
  5814. {
  5815. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5816. return 1;
  5817. }
  5818. seconds = script_getnum(st,3);
  5819. memset(&it, 0, sizeof(it));
  5820. it.nameid = nameid;
  5821. it.identify = 1;
  5822. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5823. it.bound = 0;
  5824. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5825. {
  5826. clif_additem(sd, 0, 0, flag);
  5827. return 1;
  5828. }
  5829. return 0;
  5830. }
  5831. /*==========================================
  5832. * gets an item with someone's name inscribed [Skotlex]
  5833. * getinscribeditem item_num, character_name
  5834. * Returned Qty is always 1, only works on equip-able
  5835. * equipment
  5836. *------------------------------------------*/
  5837. BUILDIN_FUNC(getnameditem)
  5838. {
  5839. int nameid;
  5840. struct item item_tmp;
  5841. TBL_PC *sd, *tsd;
  5842. struct script_data *data;
  5843. sd = script_rid2sd(st);
  5844. if (sd == NULL)
  5845. { //Player not attached!
  5846. script_pushint(st,0);
  5847. return 0;
  5848. }
  5849. data=script_getdata(st,2);
  5850. get_val(st,data);
  5851. if( data_isstring(data) ){
  5852. const char *name=conv_str(st,data);
  5853. struct item_data *item_data = itemdb_searchname(name);
  5854. if( item_data == NULL)
  5855. { //Failed
  5856. script_pushint(st,0);
  5857. return 0;
  5858. }
  5859. nameid = item_data->nameid;
  5860. }else
  5861. nameid = conv_num(st,data);
  5862. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5863. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5864. script_pushint(st,0);
  5865. return 0;
  5866. }
  5867. data=script_getdata(st,3);
  5868. get_val(st,data);
  5869. if( data_isstring(data) ) //Char Name
  5870. tsd=map_nick2sd(conv_str(st,data));
  5871. else //Char Id was given
  5872. tsd=map_charid2sd(conv_num(st,data));
  5873. if( tsd == NULL )
  5874. { //Failed
  5875. script_pushint(st,0);
  5876. return 0;
  5877. }
  5878. memset(&item_tmp,0,sizeof(item_tmp));
  5879. item_tmp.nameid=nameid;
  5880. item_tmp.amount=1;
  5881. item_tmp.identify=1;
  5882. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5883. item_tmp.card[2]=tsd->status.char_id;
  5884. item_tmp.card[3]=tsd->status.char_id >> 16;
  5885. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  5886. script_pushint(st,0);
  5887. return 0; //Failed to add item, we will not drop if they don't fit
  5888. }
  5889. script_pushint(st,1);
  5890. return 0;
  5891. }
  5892. /*==========================================
  5893. * gets a random item ID from an item group [Skotlex]
  5894. * groupranditem group_num
  5895. *------------------------------------------*/
  5896. BUILDIN_FUNC(grouprandomitem)
  5897. {
  5898. int group;
  5899. group = script_getnum(st,2);
  5900. script_pushint(st,itemdb_searchrandomid(group));
  5901. return 0;
  5902. }
  5903. /*==========================================
  5904. *
  5905. *------------------------------------------*/
  5906. BUILDIN_FUNC(makeitem)
  5907. {
  5908. int nameid,amount,flag = 0;
  5909. int x,y,m;
  5910. const char *mapname;
  5911. struct item item_tmp;
  5912. struct script_data *data;
  5913. data=script_getdata(st,2);
  5914. get_val(st,data);
  5915. if( data_isstring(data) ){
  5916. const char *name=conv_str(st,data);
  5917. struct item_data *item_data = itemdb_searchname(name);
  5918. if( item_data )
  5919. nameid=item_data->nameid;
  5920. else
  5921. nameid=UNKNOWN_ITEM_ID;
  5922. }else
  5923. nameid=conv_num(st,data);
  5924. amount=script_getnum(st,3);
  5925. mapname =script_getstr(st,4);
  5926. x =script_getnum(st,5);
  5927. y =script_getnum(st,6);
  5928. if(strcmp(mapname,"this")==0)
  5929. {
  5930. TBL_PC *sd;
  5931. sd = script_rid2sd(st);
  5932. if (!sd) return 0; //Failed...
  5933. m=sd->bl.m;
  5934. } else
  5935. m=map_mapname2mapid(mapname);
  5936. if(nameid<0) {
  5937. nameid = -nameid;
  5938. flag = 1;
  5939. }
  5940. if(nameid > 0) {
  5941. memset(&item_tmp,0,sizeof(item_tmp));
  5942. item_tmp.nameid=nameid;
  5943. if(!flag)
  5944. item_tmp.identify=1;
  5945. else
  5946. item_tmp.identify=itemdb_isidentified(nameid);
  5947. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  5948. }
  5949. return 0;
  5950. }
  5951. /// Counts / deletes the current item given by idx.
  5952. /// Used by buildin_delitem_search
  5953. /// Relies on all input data being already fully valid.
  5954. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5955. {
  5956. int delamount;
  5957. struct item* inv = &sd->status.inventory[idx];
  5958. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5959. if( delete_items )
  5960. {
  5961. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5962. {// delete associated pet
  5963. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5964. }
  5965. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  5966. }
  5967. amount[0]-= delamount;
  5968. }
  5969. /// Searches for item(s) and checks, if there is enough of them.
  5970. /// Used by delitem and delitem2
  5971. /// Relies on all input data being already fully valid.
  5972. /// @param exact_match will also match item attributes and cards, not just name id
  5973. /// @return true when all items could be deleted, false when there were not enough items to delete
  5974. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5975. {
  5976. bool delete_items = false;
  5977. int i, amount, important;
  5978. struct item* inv;
  5979. // prefer always non-equipped items
  5980. it->equip = 0;
  5981. // when searching for nameid only, prefer additionally
  5982. if( !exact_match )
  5983. {
  5984. // non-refined items
  5985. it->refine = 0;
  5986. // card-less items
  5987. memset(it->card, 0, sizeof(it->card));
  5988. }
  5989. for(;;)
  5990. {
  5991. amount = it->amount;
  5992. important = 0;
  5993. // 1st pass -- less important items / exact match
  5994. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5995. {
  5996. inv = &sd->status.inventory[i];
  5997. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5998. {// wrong/invalid item
  5999. continue;
  6000. }
  6001. if( inv->equip != it->equip || inv->refine != it->refine )
  6002. {// not matching attributes
  6003. important++;
  6004. continue;
  6005. }
  6006. if( exact_match )
  6007. {
  6008. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  6009. {// not matching exact attributes
  6010. continue;
  6011. }
  6012. }
  6013. else
  6014. {
  6015. if( sd->inventory_data[i]->type == IT_PETEGG )
  6016. {
  6017. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  6018. {// pet which cannot be deleted
  6019. continue;
  6020. }
  6021. }
  6022. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  6023. {// named/carded item
  6024. important++;
  6025. continue;
  6026. }
  6027. }
  6028. // count / delete item
  6029. buildin_delitem_delete(sd, i, &amount, delete_items);
  6030. }
  6031. // 2nd pass -- any matching item
  6032. if( amount == 0 || important == 0 )
  6033. {// either everything was already consumed or no items were skipped
  6034. ;
  6035. }
  6036. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  6037. {
  6038. inv = &sd->status.inventory[i];
  6039. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  6040. {// wrong/invalid item
  6041. continue;
  6042. }
  6043. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  6044. {// pet which cannot be deleted
  6045. continue;
  6046. }
  6047. if( exact_match )
  6048. {
  6049. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  6050. {// not matching attributes
  6051. continue;
  6052. }
  6053. }
  6054. // count / delete item
  6055. buildin_delitem_delete(sd, i, &amount, delete_items);
  6056. }
  6057. if( amount )
  6058. {// not enough items
  6059. return false;
  6060. }
  6061. else if( delete_items )
  6062. {// we are done with the work
  6063. return true;
  6064. }
  6065. else
  6066. {// get rid of the items now
  6067. delete_items = true;
  6068. }
  6069. }
  6070. }
  6071. /// Deletes items from the target/attached player.
  6072. /// Prioritizes ordinary items.
  6073. ///
  6074. /// delitem <item id>,<amount>{,<account id>}
  6075. /// delitem "<item name>",<amount>{,<account id>}
  6076. BUILDIN_FUNC(delitem)
  6077. {
  6078. TBL_PC *sd;
  6079. struct item it;
  6080. struct script_data *data;
  6081. if( script_hasdata(st,4) )
  6082. {
  6083. int account_id = script_getnum(st,4);
  6084. sd = map_id2sd(account_id); // <account id>
  6085. if( sd == NULL )
  6086. {
  6087. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  6088. st->state = END;
  6089. return 1;
  6090. }
  6091. }
  6092. else
  6093. {
  6094. sd = script_rid2sd(st);// attached player
  6095. if( sd == NULL )
  6096. return 0;
  6097. }
  6098. data = script_getdata(st,2);
  6099. get_val(st,data);
  6100. if( data_isstring(data) )
  6101. {
  6102. const char* item_name = conv_str(st,data);
  6103. struct item_data* id = itemdb_searchname(item_name);
  6104. if( id == NULL )
  6105. {
  6106. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  6107. st->state = END;
  6108. return 1;
  6109. }
  6110. it.nameid = id->nameid;// "<item name>"
  6111. }
  6112. else
  6113. {
  6114. it.nameid = conv_num(st,data);// <item id>
  6115. if( !itemdb_exists( it.nameid ) )
  6116. {
  6117. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6118. st->state = END;
  6119. return 1;
  6120. }
  6121. }
  6122. it.amount=script_getnum(st,3);
  6123. if( it.amount <= 0 )
  6124. return 0;// nothing to do
  6125. if( buildin_delitem_search(sd, &it, false) )
  6126. {// success
  6127. return 0;
  6128. }
  6129. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6130. st->state = END;
  6131. st->mes_active = 0;
  6132. clif_scriptclose(sd, st->oid);
  6133. return 1;
  6134. }
  6135. /// Deletes items from the target/attached player.
  6136. ///
  6137. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6138. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6139. BUILDIN_FUNC(delitem2)
  6140. {
  6141. TBL_PC *sd;
  6142. struct item it;
  6143. struct script_data *data;
  6144. if( script_hasdata(st,11) )
  6145. {
  6146. int account_id = script_getnum(st,11);
  6147. sd = map_id2sd(account_id); // <account id>
  6148. if( sd == NULL )
  6149. {
  6150. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  6151. st->state = END;
  6152. return 1;
  6153. }
  6154. }
  6155. else
  6156. {
  6157. sd = script_rid2sd(st);// attached player
  6158. if( sd == NULL )
  6159. return 0;
  6160. }
  6161. data = script_getdata(st,2);
  6162. get_val(st,data);
  6163. if( data_isstring(data) )
  6164. {
  6165. const char* item_name = conv_str(st,data);
  6166. struct item_data* id = itemdb_searchname(item_name);
  6167. if( id == NULL )
  6168. {
  6169. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  6170. st->state = END;
  6171. return 1;
  6172. }
  6173. it.nameid = id->nameid;// "<item name>"
  6174. }
  6175. else
  6176. {
  6177. it.nameid = conv_num(st,data);// <item id>
  6178. if( !itemdb_exists( it.nameid ) )
  6179. {
  6180. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6181. st->state = END;
  6182. return 1;
  6183. }
  6184. }
  6185. it.amount=script_getnum(st,3);
  6186. it.identify=script_getnum(st,4);
  6187. it.refine=script_getnum(st,5);
  6188. it.attribute=script_getnum(st,6);
  6189. it.card[0]=(short)script_getnum(st,7);
  6190. it.card[1]=(short)script_getnum(st,8);
  6191. it.card[2]=(short)script_getnum(st,9);
  6192. it.card[3]=(short)script_getnum(st,10);
  6193. if( it.amount <= 0 )
  6194. return 0;// nothing to do
  6195. if( buildin_delitem_search(sd, &it, true) )
  6196. {// success
  6197. return 0;
  6198. }
  6199. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6200. st->state = END;
  6201. st->mes_active = 0;
  6202. clif_scriptclose(sd, st->oid);
  6203. return 1;
  6204. }
  6205. /*==========================================
  6206. * Enables/Disables use of items while in an NPC [Skotlex]
  6207. *------------------------------------------*/
  6208. BUILDIN_FUNC(enableitemuse)
  6209. {
  6210. TBL_PC *sd;
  6211. sd=script_rid2sd(st);
  6212. if (sd)
  6213. st->npc_item_flag = sd->npc_item_flag = 1;
  6214. return 0;
  6215. }
  6216. BUILDIN_FUNC(disableitemuse)
  6217. {
  6218. TBL_PC *sd;
  6219. sd=script_rid2sd(st);
  6220. if (sd)
  6221. st->npc_item_flag = sd->npc_item_flag = 0;
  6222. return 0;
  6223. }
  6224. /*==========================================
  6225. * Returns a character's specified stat.
  6226. * Check pc_readparam for available options.
  6227. *------------------------------------------*/
  6228. BUILDIN_FUNC(readparam)
  6229. {
  6230. int type;
  6231. TBL_PC *sd;
  6232. type=script_getnum(st,2);
  6233. if( script_hasdata(st,3) )
  6234. sd=map_nick2sd(script_getstr(st,3));
  6235. else
  6236. sd=script_rid2sd(st);
  6237. if(sd==NULL){
  6238. script_pushint(st,-1);
  6239. return 0;
  6240. }
  6241. script_pushint(st,pc_readparam(sd,type));
  6242. return 0;
  6243. }
  6244. /*==========================================
  6245. * Return charid identification
  6246. * return by @num :
  6247. * 0 : char_id
  6248. * 1 : party_id
  6249. * 2 : guild_id
  6250. * 3 : account_id
  6251. * 4 : bg_id
  6252. *------------------------------------------*/
  6253. BUILDIN_FUNC(getcharid)
  6254. {
  6255. int num;
  6256. TBL_PC *sd;
  6257. num = script_getnum(st,2);
  6258. if( script_hasdata(st,3) )
  6259. sd=map_nick2sd(script_getstr(st,3));
  6260. else
  6261. sd=script_rid2sd(st);
  6262. if(sd==NULL){
  6263. script_pushint(st,0); //return 0, according docs
  6264. return 0;
  6265. }
  6266. switch( num ) {
  6267. case 0: script_pushint(st,sd->status.char_id); break;
  6268. case 1: script_pushint(st,sd->status.party_id); break;
  6269. case 2: script_pushint(st,sd->status.guild_id); break;
  6270. case 3: script_pushint(st,sd->status.account_id); break;
  6271. case 4: script_pushint(st,sd->bg_id); break;
  6272. default:
  6273. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6274. script_pushint(st,0);
  6275. break;
  6276. }
  6277. return 0;
  6278. }
  6279. /*==========================================
  6280. * returns the GID of an NPC
  6281. *------------------------------------------*/
  6282. BUILDIN_FUNC(getnpcid)
  6283. {
  6284. int num = script_getnum(st,2);
  6285. struct npc_data* nd = NULL;
  6286. if( script_hasdata(st,3) )
  6287. {// unique npc name
  6288. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6289. {
  6290. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6291. script_pushint(st,0);
  6292. return 1;
  6293. }
  6294. }
  6295. switch (num) {
  6296. case 0:
  6297. script_pushint(st,nd ? nd->bl.id : st->oid);
  6298. break;
  6299. default:
  6300. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6301. script_pushint(st,0);
  6302. return 1;
  6303. }
  6304. return 0;
  6305. }
  6306. /*==========================================
  6307. * Return the name of the party_id
  6308. * null if not found
  6309. *------------------------------------------*/
  6310. BUILDIN_FUNC(getpartyname)
  6311. {
  6312. int party_id;
  6313. struct party_data* p;
  6314. party_id = script_getnum(st,2);
  6315. if( ( p = party_search(party_id) ) != NULL )
  6316. {
  6317. script_pushstrcopy(st,p->party.name);
  6318. }
  6319. else
  6320. {
  6321. script_pushconststr(st,"null");
  6322. }
  6323. return 0;
  6324. }
  6325. /*==========================================
  6326. * Get the information of the members of a party by type
  6327. * @party_id, @type
  6328. * return by @type :
  6329. * - : nom des membres
  6330. * 1 : char_id des membres
  6331. * 2 : account_id des membres
  6332. *------------------------------------------*/
  6333. BUILDIN_FUNC(getpartymember)
  6334. {
  6335. struct party_data *p;
  6336. int i,j=0,type=0;
  6337. p=party_search(script_getnum(st,2));
  6338. if( script_hasdata(st,3) )
  6339. type=script_getnum(st,3);
  6340. if(p!=NULL){
  6341. for(i=0;i<MAX_PARTY;i++){
  6342. if(p->party.member[i].account_id){
  6343. switch (type) {
  6344. case 2:
  6345. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6346. break;
  6347. case 1:
  6348. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6349. break;
  6350. default:
  6351. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6352. }
  6353. j++;
  6354. }
  6355. }
  6356. }
  6357. mapreg_setreg(add_str("$@partymembercount"),j);
  6358. return 0;
  6359. }
  6360. /*==========================================
  6361. * Retrieves party leader. if flag is specified,
  6362. * return some of the leader data. Otherwise, return name.
  6363. *------------------------------------------*/
  6364. BUILDIN_FUNC(getpartyleader)
  6365. {
  6366. int party_id, type = 0, i=0;
  6367. struct party_data *p;
  6368. party_id=script_getnum(st,2);
  6369. if( script_hasdata(st,3) )
  6370. type=script_getnum(st,3);
  6371. p=party_search(party_id);
  6372. if (p) //Search leader
  6373. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6374. if (!p || i == MAX_PARTY) { //leader not found
  6375. if (type)
  6376. script_pushint(st,-1);
  6377. else
  6378. script_pushconststr(st,"null");
  6379. return 0;
  6380. }
  6381. switch (type) {
  6382. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6383. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6384. case 3: script_pushint(st,p->party.member[i].class_); break;
  6385. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6386. case 5: script_pushint(st,p->party.member[i].lv); break;
  6387. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6388. }
  6389. return 0;
  6390. }
  6391. /*==========================================
  6392. * Return the name of the @guild_id
  6393. * null if not found
  6394. *------------------------------------------*/
  6395. BUILDIN_FUNC(getguildname)
  6396. {
  6397. int guild_id;
  6398. struct guild* g;
  6399. guild_id = script_getnum(st,2);
  6400. if( ( g = guild_search(guild_id) ) != NULL )
  6401. {
  6402. script_pushstrcopy(st,g->name);
  6403. }
  6404. else
  6405. {
  6406. script_pushconststr(st,"null");
  6407. }
  6408. return 0;
  6409. }
  6410. /*==========================================
  6411. * Return the name of the guild master of @guild_id
  6412. * null if not found
  6413. *------------------------------------------*/
  6414. BUILDIN_FUNC(getguildmaster)
  6415. {
  6416. int guild_id;
  6417. struct guild* g;
  6418. guild_id = script_getnum(st,2);
  6419. if( ( g = guild_search(guild_id) ) != NULL )
  6420. {
  6421. script_pushstrcopy(st,g->member[0].name);
  6422. }
  6423. else
  6424. {
  6425. script_pushconststr(st,"null");
  6426. }
  6427. return 0;
  6428. }
  6429. BUILDIN_FUNC(getguildmasterid)
  6430. {
  6431. int guild_id;
  6432. struct guild* g;
  6433. guild_id = script_getnum(st,2);
  6434. if( ( g = guild_search(guild_id) ) != NULL )
  6435. {
  6436. script_pushint(st,g->member[0].char_id);
  6437. }
  6438. else
  6439. {
  6440. script_pushint(st,0);
  6441. }
  6442. return 0;
  6443. }
  6444. /*==========================================
  6445. * Get char string information by type :
  6446. * Return by @type :
  6447. * 0 : char_name
  6448. * 1 : party_name or ""
  6449. * 2 : guild_name or ""
  6450. * 3 : map_name
  6451. * - : ""
  6452. *------------------------------------------*/
  6453. BUILDIN_FUNC(strcharinfo)
  6454. {
  6455. TBL_PC *sd;
  6456. int num;
  6457. struct guild* g;
  6458. struct party_data* p;
  6459. sd=script_rid2sd(st);
  6460. if (!sd) { //Avoid crashing....
  6461. script_pushconststr(st,"");
  6462. return 0;
  6463. }
  6464. num=script_getnum(st,2);
  6465. switch(num){
  6466. case 0:
  6467. script_pushstrcopy(st,sd->status.name);
  6468. break;
  6469. case 1:
  6470. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  6471. script_pushstrcopy(st,p->party.name);
  6472. } else {
  6473. script_pushconststr(st,"");
  6474. }
  6475. break;
  6476. case 2:
  6477. if( ( g = sd->guild ) != NULL ) {
  6478. script_pushstrcopy(st,g->name);
  6479. } else {
  6480. script_pushconststr(st,"");
  6481. }
  6482. break;
  6483. case 3:
  6484. script_pushconststr(st,map[sd->bl.m].name);
  6485. break;
  6486. default:
  6487. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6488. script_pushconststr(st,"");
  6489. break;
  6490. }
  6491. return 0;
  6492. }
  6493. /*==========================================
  6494. * Get npc string information by type
  6495. * return by @type:
  6496. * 0 : name
  6497. * 1 : str#
  6498. * 2 : #str
  6499. * 3 : ::str
  6500. * 4 : map name
  6501. *------------------------------------------*/
  6502. BUILDIN_FUNC(strnpcinfo)
  6503. {
  6504. TBL_NPC* nd;
  6505. int num;
  6506. char *buf,*name=NULL;
  6507. nd = map_id2nd(st->oid);
  6508. if (!nd) {
  6509. script_pushconststr(st, "");
  6510. return 0;
  6511. }
  6512. num = script_getnum(st,2);
  6513. switch(num){
  6514. case 0: // display name
  6515. name = aStrdup(nd->name);
  6516. break;
  6517. case 1: // visible part of display name
  6518. if((buf = strchr(nd->name,'#')) != NULL)
  6519. {
  6520. name = aStrdup(nd->name);
  6521. name[buf - nd->name] = 0;
  6522. } else // Return the name, there is no '#' present
  6523. name = aStrdup(nd->name);
  6524. break;
  6525. case 2: // # fragment
  6526. if((buf = strchr(nd->name,'#')) != NULL)
  6527. name = aStrdup(buf+1);
  6528. break;
  6529. case 3: // unique name
  6530. name = aStrdup(nd->exname);
  6531. break;
  6532. case 4: // map name
  6533. name = aStrdup(map[nd->bl.m].name);
  6534. break;
  6535. }
  6536. if(name)
  6537. script_pushstr(st, name);
  6538. else
  6539. script_pushconststr(st, "");
  6540. return 0;
  6541. }
  6542. // aegis->athena slot position conversion table
  6543. 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};
  6544. /*==========================================
  6545. * GetEquipID(Pos); Pos: 1-14
  6546. *------------------------------------------*/
  6547. BUILDIN_FUNC(getequipid)
  6548. {
  6549. int i, num;
  6550. TBL_PC* sd;
  6551. struct item_data* item;
  6552. sd = script_rid2sd(st);
  6553. if( sd == NULL )
  6554. return 0;
  6555. num = script_getnum(st,2) - 1;
  6556. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6557. {
  6558. script_pushint(st,-1);
  6559. return 0;
  6560. }
  6561. // get inventory position of item
  6562. i = pc_checkequip(sd,equip[num]);
  6563. if( i < 0 )
  6564. {
  6565. script_pushint(st,-1);
  6566. return 0;
  6567. }
  6568. item = sd->inventory_data[i];
  6569. if( item != 0 )
  6570. script_pushint(st,item->nameid);
  6571. else
  6572. script_pushint(st,0);
  6573. return 0;
  6574. }
  6575. /*==========================================
  6576. * Get the equipement name at pos
  6577. * return item jname or ""
  6578. *------------------------------------------*/
  6579. BUILDIN_FUNC(getequipname)
  6580. {
  6581. int i, num;
  6582. TBL_PC* sd;
  6583. struct item_data* item;
  6584. sd = script_rid2sd(st);
  6585. if( sd == NULL )
  6586. return 0;
  6587. num = script_getnum(st,2) - 1;
  6588. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6589. {
  6590. script_pushconststr(st,"");
  6591. return 0;
  6592. }
  6593. // get inventory position of item
  6594. i = pc_checkequip(sd,equip[num]);
  6595. if( i < 0 )
  6596. {
  6597. script_pushconststr(st,"");
  6598. return 0;
  6599. }
  6600. item = sd->inventory_data[i];
  6601. if( item != 0 )
  6602. script_pushstrcopy(st,item->jname);
  6603. else
  6604. script_pushconststr(st,"");
  6605. return 0;
  6606. }
  6607. /*==========================================
  6608. * getbrokenid [Valaris]
  6609. *------------------------------------------*/
  6610. BUILDIN_FUNC(getbrokenid)
  6611. {
  6612. int i,num,id=0,brokencounter=0;
  6613. TBL_PC *sd;
  6614. sd = script_rid2sd(st);
  6615. if( sd == NULL )
  6616. return 0;
  6617. num=script_getnum(st,2);
  6618. for(i=0; i<MAX_INVENTORY; i++) {
  6619. if(sd->status.inventory[i].attribute){
  6620. brokencounter++;
  6621. if(num==brokencounter){
  6622. id=sd->status.inventory[i].nameid;
  6623. break;
  6624. }
  6625. }
  6626. }
  6627. script_pushint(st,id);
  6628. return 0;
  6629. }
  6630. /*==========================================
  6631. * repair [Valaris]
  6632. *------------------------------------------*/
  6633. BUILDIN_FUNC(repair)
  6634. {
  6635. int i,num;
  6636. int repaircounter=0;
  6637. TBL_PC *sd;
  6638. sd = script_rid2sd(st);
  6639. if( sd == NULL )
  6640. return 0;
  6641. num=script_getnum(st,2);
  6642. for(i=0; i<MAX_INVENTORY; i++) {
  6643. if(sd->status.inventory[i].attribute){
  6644. repaircounter++;
  6645. if(num==repaircounter){
  6646. sd->status.inventory[i].attribute=0;
  6647. clif_equiplist(sd);
  6648. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6649. clif_misceffect(&sd->bl, 3);
  6650. break;
  6651. }
  6652. }
  6653. }
  6654. return 0;
  6655. }
  6656. /*==========================================
  6657. * repairall
  6658. *------------------------------------------*/
  6659. BUILDIN_FUNC(repairall)
  6660. {
  6661. int i, repaircounter = 0;
  6662. TBL_PC *sd;
  6663. sd = script_rid2sd(st);
  6664. if(sd == NULL)
  6665. return 0;
  6666. for(i = 0; i < MAX_INVENTORY; i++)
  6667. {
  6668. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  6669. {
  6670. sd->status.inventory[i].attribute = 0;
  6671. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  6672. repaircounter++;
  6673. }
  6674. }
  6675. if(repaircounter)
  6676. {
  6677. clif_misceffect(&sd->bl, 3);
  6678. clif_equiplist(sd);
  6679. }
  6680. return 0;
  6681. }
  6682. /*==========================================
  6683. * Chk if player have something equiped at pos
  6684. *------------------------------------------*/
  6685. BUILDIN_FUNC(getequipisequiped)
  6686. {
  6687. int i = -1,num;
  6688. TBL_PC *sd;
  6689. num = script_getnum(st,2);
  6690. sd = script_rid2sd(st);
  6691. if( sd == NULL )
  6692. return 0;
  6693. if (num > 0 && num <= ARRAYLENGTH(equip))
  6694. i=pc_checkequip(sd,equip[num-1]);
  6695. if(i >= 0)
  6696. script_pushint(st,1);
  6697. else
  6698. script_pushint(st,0);
  6699. return 0;
  6700. }
  6701. /*==========================================
  6702. * Chk if the player have something equiped at pos
  6703. * if so chk if this item ain't marked not refinable or rental
  6704. * return (npc)
  6705. * 1 : true
  6706. * 0 : false
  6707. *------------------------------------------*/
  6708. BUILDIN_FUNC(getequipisenableref)
  6709. {
  6710. int i = -1,num;
  6711. TBL_PC *sd;
  6712. num = script_getnum(st,2);
  6713. sd = script_rid2sd(st);
  6714. if( sd == NULL )
  6715. return 0;
  6716. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6717. i = pc_checkequip(sd,equip[num-1]);
  6718. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6719. script_pushint(st,1);
  6720. else
  6721. script_pushint(st,0);
  6722. return 0;
  6723. }
  6724. /*==========================================
  6725. * Get the item refined value at pos
  6726. * return (npc)
  6727. * x : refine amount
  6728. * 0 : false (not refined)
  6729. *------------------------------------------*/
  6730. BUILDIN_FUNC(getequiprefinerycnt)
  6731. {
  6732. int i = -1,num;
  6733. TBL_PC *sd;
  6734. num = script_getnum(st,2);
  6735. sd = script_rid2sd(st);
  6736. if( sd == NULL )
  6737. return 0;
  6738. if (num > 0 && num <= ARRAYLENGTH(equip))
  6739. i=pc_checkequip(sd,equip[num-1]);
  6740. if(i >= 0)
  6741. script_pushint(st,sd->status.inventory[i].refine);
  6742. else
  6743. script_pushint(st,0);
  6744. return 0;
  6745. }
  6746. /*==========================================
  6747. * Get the weapon level value at pos
  6748. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  6749. * return (npc)
  6750. * x : weapon level
  6751. * 0 : false
  6752. *------------------------------------------*/
  6753. BUILDIN_FUNC(getequipweaponlv)
  6754. {
  6755. int i = -1,num;
  6756. TBL_PC *sd;
  6757. num = script_getnum(st,2);
  6758. sd = script_rid2sd(st);
  6759. if( sd == NULL )
  6760. return 0;
  6761. if (num > 0 && num <= ARRAYLENGTH(equip))
  6762. i=pc_checkequip(sd,equip[num-1]);
  6763. if(i >= 0 && sd->inventory_data[i])
  6764. script_pushint(st,sd->inventory_data[i]->wlv);
  6765. else
  6766. script_pushint(st,0);
  6767. return 0;
  6768. }
  6769. /*==========================================
  6770. * Get the item refine chance (from refine.txt) for item at pos
  6771. * return (npc)
  6772. * x : refine chance
  6773. * 0 : false (max refine level or unequip..)
  6774. *------------------------------------------*/
  6775. BUILDIN_FUNC(getequippercentrefinery)
  6776. {
  6777. int i = -1,num;
  6778. TBL_PC *sd;
  6779. num = script_getnum(st,2);
  6780. sd = script_rid2sd(st);
  6781. if( sd == NULL )
  6782. return 0;
  6783. if (num > 0 && num <= ARRAYLENGTH(equip))
  6784. i=pc_checkequip(sd,equip[num-1]);
  6785. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6786. script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  6787. else
  6788. script_pushint(st,0);
  6789. return 0;
  6790. }
  6791. /*==========================================
  6792. * Refine +1 item at pos and log and display refine
  6793. *------------------------------------------*/
  6794. BUILDIN_FUNC(successrefitem)
  6795. {
  6796. int i=-1,num,ep;
  6797. TBL_PC *sd;
  6798. num = script_getnum(st,2);
  6799. sd = script_rid2sd(st);
  6800. if( sd == NULL )
  6801. return 0;
  6802. if (num > 0 && num <= ARRAYLENGTH(equip))
  6803. i=pc_checkequip(sd,equip[num-1]);
  6804. if(i >= 0) {
  6805. ep=sd->status.inventory[i].equip;
  6806. //Logs items, got from (N)PC scripts [Lupus]
  6807. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6808. sd->status.inventory[i].refine++;
  6809. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6810. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6811. clif_delitem(sd,i,1,3);
  6812. //Logs items, got from (N)PC scripts [Lupus]
  6813. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6814. clif_additem(sd,i,1,0);
  6815. pc_equipitem(sd,i,ep);
  6816. clif_misceffect(&sd->bl,3);
  6817. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6818. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6819. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6820. ){ // Fame point system [DracoRPG]
  6821. switch (sd->inventory_data[i]->wlv){
  6822. case 1:
  6823. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6824. break;
  6825. case 2:
  6826. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6827. break;
  6828. case 3:
  6829. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6830. break;
  6831. }
  6832. }
  6833. }
  6834. return 0;
  6835. }
  6836. /*==========================================
  6837. * Show a failed Refine +1 attempt
  6838. *------------------------------------------*/
  6839. BUILDIN_FUNC(failedrefitem)
  6840. {
  6841. int i=-1,num;
  6842. TBL_PC *sd;
  6843. num = script_getnum(st,2);
  6844. sd = script_rid2sd(st);
  6845. if( sd == NULL )
  6846. return 0;
  6847. if (num > 0 && num <= ARRAYLENGTH(equip))
  6848. i=pc_checkequip(sd,equip[num-1]);
  6849. if(i >= 0) {
  6850. sd->status.inventory[i].refine = 0;
  6851. pc_unequipitem(sd,i,3); //recalculate bonus
  6852. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  6853. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6854. clif_misceffect(&sd->bl,2); // display failure effect
  6855. }
  6856. return 0;
  6857. }
  6858. /*==========================================
  6859. * Downgrades an Equipment Part by -1 . [Masao]
  6860. *------------------------------------------*/
  6861. BUILDIN_FUNC(downrefitem)
  6862. {
  6863. int i = -1,num,ep;
  6864. TBL_PC *sd;
  6865. num = script_getnum(st,2);
  6866. sd = script_rid2sd(st);
  6867. if( sd == NULL )
  6868. return 0;
  6869. if (num > 0 && num <= ARRAYLENGTH(equip))
  6870. i = pc_checkequip(sd,equip[num-1]);
  6871. if(i >= 0) {
  6872. ep = sd->status.inventory[i].equip;
  6873. //Logs items, got from (N)PC scripts [Lupus]
  6874. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6875. sd->status.inventory[i].refine++;
  6876. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6877. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine = sd->status.inventory[i].refine - 2);
  6878. clif_delitem(sd,i,1,3);
  6879. //Logs items, got from (N)PC scripts [Lupus]
  6880. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6881. clif_additem(sd,i,1,0);
  6882. pc_equipitem(sd,i,ep);
  6883. clif_misceffect(&sd->bl,2);
  6884. }
  6885. return 0;
  6886. }
  6887. /*==========================================
  6888. * Delete the item equipped at pos.
  6889. *------------------------------------------*/
  6890. BUILDIN_FUNC(delequip)
  6891. {
  6892. int i=-1,num;
  6893. TBL_PC *sd;
  6894. num = script_getnum(st,2);
  6895. sd = script_rid2sd(st);
  6896. if( sd == NULL )
  6897. return 0;
  6898. if (num > 0 && num <= ARRAYLENGTH(equip))
  6899. i=pc_checkequip(sd,equip[num-1]);
  6900. if(i >= 0) {
  6901. int ret;
  6902. pc_unequipitem(sd,i,3); //recalculate bonus
  6903. ret=pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6904. script_pushint(st,ret==0);
  6905. }
  6906. return 0;
  6907. }
  6908. /*==========================================
  6909. *
  6910. *------------------------------------------*/
  6911. BUILDIN_FUNC(statusup)
  6912. {
  6913. int type;
  6914. TBL_PC *sd;
  6915. type=script_getnum(st,2);
  6916. sd = script_rid2sd(st);
  6917. if( sd == NULL )
  6918. return 0;
  6919. pc_statusup(sd,type);
  6920. return 0;
  6921. }
  6922. /*==========================================
  6923. *
  6924. *------------------------------------------*/
  6925. BUILDIN_FUNC(statusup2)
  6926. {
  6927. int type,val;
  6928. TBL_PC *sd;
  6929. type=script_getnum(st,2);
  6930. val=script_getnum(st,3);
  6931. sd = script_rid2sd(st);
  6932. if( sd == NULL )
  6933. return 0;
  6934. pc_statusup2(sd,type,val);
  6935. return 0;
  6936. }
  6937. /// See 'doc/item_bonus.txt'
  6938. ///
  6939. /// bonus <bonus type>,<val1>;
  6940. /// bonus2 <bonus type>,<val1>,<val2>;
  6941. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6942. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6943. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6944. BUILDIN_FUNC(bonus)
  6945. {
  6946. int type;
  6947. int val1;
  6948. int val2 = 0;
  6949. int val3 = 0;
  6950. int val4 = 0;
  6951. int val5 = 0;
  6952. TBL_PC* sd;
  6953. sd = script_rid2sd(st);
  6954. if( sd == NULL )
  6955. return 0; // no player attached
  6956. type = script_getnum(st,2);
  6957. switch( type ) {
  6958. case SP_AUTOSPELL:
  6959. case SP_AUTOSPELL_WHENHIT:
  6960. case SP_AUTOSPELL_ONSKILL:
  6961. case SP_SKILL_ATK:
  6962. case SP_SKILL_HEAL:
  6963. case SP_SKILL_HEAL2:
  6964. case SP_ADD_SKILL_BLOW:
  6965. case SP_CASTRATE:
  6966. case SP_ADDEFF_ONSKILL:
  6967. case SP_SKILL_USE_SP_RATE:
  6968. case SP_SKILL_COOLDOWN:
  6969. case SP_SKILL_FIXEDCAST:
  6970. case SP_SKILL_VARIABLECAST:
  6971. case SP_VARCASTRATE:
  6972. case SP_FIXCASTRATE:
  6973. case SP_SKILL_USE_SP:
  6974. // these bonuses support skill names
  6975. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6976. break;
  6977. default:
  6978. val1 = script_getnum(st,3);
  6979. break;
  6980. }
  6981. switch( script_lastdata(st)-2 ) {
  6982. case 1:
  6983. pc_bonus(sd, type, val1);
  6984. break;
  6985. case 2:
  6986. val2 = script_getnum(st,4);
  6987. pc_bonus2(sd, type, val1, val2);
  6988. break;
  6989. case 3:
  6990. val2 = script_getnum(st,4);
  6991. val3 = script_getnum(st,5);
  6992. pc_bonus3(sd, type, val1, val2, val3);
  6993. break;
  6994. case 4:
  6995. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6996. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6997. else
  6998. val2 = script_getnum(st,4);
  6999. val3 = script_getnum(st,5);
  7000. val4 = script_getnum(st,6);
  7001. pc_bonus4(sd, type, val1, val2, val3, val4);
  7002. break;
  7003. case 5:
  7004. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  7005. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7006. else
  7007. val2 = script_getnum(st,4);
  7008. val3 = script_getnum(st,5);
  7009. val4 = script_getnum(st,6);
  7010. val5 = script_getnum(st,7);
  7011. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7012. break;
  7013. default:
  7014. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7015. break;
  7016. }
  7017. return 0;
  7018. }
  7019. BUILDIN_FUNC(autobonus)
  7020. {
  7021. unsigned int dur;
  7022. short rate;
  7023. short atk_type = 0;
  7024. TBL_PC* sd;
  7025. const char *bonus_script, *other_script = NULL;
  7026. sd = script_rid2sd(st);
  7027. if( sd == NULL )
  7028. return 0; // no player attached
  7029. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7030. return 0;
  7031. rate = script_getnum(st,3);
  7032. dur = script_getnum(st,4);
  7033. bonus_script = script_getstr(st,2);
  7034. if( !rate || !dur || !bonus_script )
  7035. return 0;
  7036. if( script_hasdata(st,5) )
  7037. atk_type = script_getnum(st,5);
  7038. if( script_hasdata(st,6) )
  7039. other_script = script_getstr(st,6);
  7040. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7041. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7042. {
  7043. script_add_autobonus(bonus_script);
  7044. if( other_script )
  7045. script_add_autobonus(other_script);
  7046. }
  7047. return 0;
  7048. }
  7049. BUILDIN_FUNC(autobonus2)
  7050. {
  7051. unsigned int dur;
  7052. short rate;
  7053. short atk_type = 0;
  7054. TBL_PC* sd;
  7055. const char *bonus_script, *other_script = NULL;
  7056. sd = script_rid2sd(st);
  7057. if( sd == NULL )
  7058. return 0; // no player attached
  7059. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7060. return 0;
  7061. rate = script_getnum(st,3);
  7062. dur = script_getnum(st,4);
  7063. bonus_script = script_getstr(st,2);
  7064. if( !rate || !dur || !bonus_script )
  7065. return 0;
  7066. if( script_hasdata(st,5) )
  7067. atk_type = script_getnum(st,5);
  7068. if( script_hasdata(st,6) )
  7069. other_script = script_getstr(st,6);
  7070. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7071. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7072. {
  7073. script_add_autobonus(bonus_script);
  7074. if( other_script )
  7075. script_add_autobonus(other_script);
  7076. }
  7077. return 0;
  7078. }
  7079. BUILDIN_FUNC(autobonus3)
  7080. {
  7081. unsigned int dur;
  7082. short rate,atk_type;
  7083. TBL_PC* sd;
  7084. const char *bonus_script, *other_script = NULL;
  7085. sd = script_rid2sd(st);
  7086. if( sd == NULL )
  7087. return 0; // no player attached
  7088. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7089. return 0;
  7090. rate = script_getnum(st,3);
  7091. dur = script_getnum(st,4);
  7092. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7093. bonus_script = script_getstr(st,2);
  7094. if( !rate || !dur || !atk_type || !bonus_script )
  7095. return 0;
  7096. if( script_hasdata(st,6) )
  7097. other_script = script_getstr(st,6);
  7098. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7099. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  7100. {
  7101. script_add_autobonus(bonus_script);
  7102. if( other_script )
  7103. script_add_autobonus(other_script);
  7104. }
  7105. return 0;
  7106. }
  7107. /// Changes the level of a player skill.
  7108. /// <flag> defaults to 1
  7109. /// <flag>=0 : set the level of the skill
  7110. /// <flag>=1 : set the temporary level of the skill
  7111. /// <flag>=2 : add to the level of the skill
  7112. ///
  7113. /// skill <skill id>,<level>,<flag>
  7114. /// skill <skill id>,<level>
  7115. /// skill "<skill name>",<level>,<flag>
  7116. /// skill "<skill name>",<level>
  7117. BUILDIN_FUNC(skill)
  7118. {
  7119. int id;
  7120. int level;
  7121. int flag = 1;
  7122. TBL_PC* sd;
  7123. sd = script_rid2sd(st);
  7124. if( sd == NULL )
  7125. return 0;// no player attached, report source
  7126. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7127. level = script_getnum(st,3);
  7128. if( script_hasdata(st,4) )
  7129. flag = script_getnum(st,4);
  7130. pc_skill(sd, id, level, flag);
  7131. return 0;
  7132. }
  7133. /// Changes the level of a player skill.
  7134. /// like skill, but <flag> defaults to 2
  7135. ///
  7136. /// addtoskill <skill id>,<amount>,<flag>
  7137. /// addtoskill <skill id>,<amount>
  7138. /// addtoskill "<skill name>",<amount>,<flag>
  7139. /// addtoskill "<skill name>",<amount>
  7140. ///
  7141. /// @see skill
  7142. BUILDIN_FUNC(addtoskill)
  7143. {
  7144. int id;
  7145. int level;
  7146. int flag = 2;
  7147. TBL_PC* sd;
  7148. sd = script_rid2sd(st);
  7149. if( sd == NULL )
  7150. return 0;// no player attached, report source
  7151. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7152. level = script_getnum(st,3);
  7153. if( script_hasdata(st,4) )
  7154. flag = script_getnum(st,4);
  7155. pc_skill(sd, id, level, flag);
  7156. return 0;
  7157. }
  7158. /// Increases the level of a guild skill.
  7159. ///
  7160. /// guildskill <skill id>,<amount>;
  7161. /// guildskill "<skill name>",<amount>;
  7162. BUILDIN_FUNC(guildskill)
  7163. {
  7164. int id;
  7165. int level;
  7166. TBL_PC* sd;
  7167. int i;
  7168. sd = script_rid2sd(st);
  7169. if( sd == NULL )
  7170. return 0;// no player attached, report source
  7171. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7172. level = script_getnum(st,3);
  7173. for( i=0; i < level; i++ )
  7174. guild_skillup(sd, id);
  7175. return 0;
  7176. }
  7177. /// Returns the level of the player skill.
  7178. ///
  7179. /// getskilllv(<skill id>) -> <level>
  7180. /// getskilllv("<skill name>") -> <level>
  7181. BUILDIN_FUNC(getskilllv)
  7182. {
  7183. int id;
  7184. TBL_PC* sd;
  7185. sd = script_rid2sd(st);
  7186. if( sd == NULL )
  7187. return 0;// no player attached, report source
  7188. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7189. script_pushint(st, pc_checkskill(sd,id));
  7190. return 0;
  7191. }
  7192. /// Returns the level of the guild skill.
  7193. ///
  7194. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7195. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7196. BUILDIN_FUNC(getgdskilllv)
  7197. {
  7198. int guild_id;
  7199. uint16 skill_id;
  7200. struct guild* g;
  7201. guild_id = script_getnum(st,2);
  7202. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7203. g = guild_search(guild_id);
  7204. if( g == NULL )
  7205. script_pushint(st, -1);
  7206. else
  7207. script_pushint(st, guild_checkskill(g,skill_id));
  7208. return 0;
  7209. }
  7210. /// Returns the 'basic_skill_check' setting.
  7211. /// This config determines if the server checks the skill level of NV_BASIC
  7212. /// before allowing the basic actions.
  7213. ///
  7214. /// basicskillcheck() -> <bool>
  7215. BUILDIN_FUNC(basicskillcheck)
  7216. {
  7217. script_pushint(st, battle_config.basic_skill_check);
  7218. return 0;
  7219. }
  7220. /// Returns the GM level of the player.
  7221. ///
  7222. /// getgmlevel() -> <level>
  7223. BUILDIN_FUNC(getgmlevel)
  7224. {
  7225. TBL_PC* sd;
  7226. sd = script_rid2sd(st);
  7227. if( sd == NULL )
  7228. return 0;// no player attached, report source
  7229. script_pushint(st, pc_get_group_level(sd));
  7230. return 0;
  7231. }
  7232. /// Returns the group ID of the player.
  7233. ///
  7234. /// getgroupid() -> <int>
  7235. BUILDIN_FUNC(getgroupid)
  7236. {
  7237. TBL_PC* sd;
  7238. sd = script_rid2sd(st);
  7239. if (sd == NULL)
  7240. return 1; // no player attached, report source
  7241. script_pushint(st, pc_get_group_id(sd));
  7242. return 0;
  7243. }
  7244. /// Terminates the execution of this script instance.
  7245. ///
  7246. /// end
  7247. BUILDIN_FUNC(end)
  7248. {
  7249. TBL_PC* sd;
  7250. sd = map_id2sd(st->rid);
  7251. st->state = END;
  7252. if( st->mes_active )
  7253. st->mes_active = 0;
  7254. if( sd )
  7255. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  7256. return 0;
  7257. }
  7258. /// Checks if the player has that effect state (option).
  7259. ///
  7260. /// checkoption(<option>) -> <bool>
  7261. BUILDIN_FUNC(checkoption)
  7262. {
  7263. int option;
  7264. TBL_PC* sd;
  7265. sd = script_rid2sd(st);
  7266. if( sd == NULL )
  7267. return 0;// no player attached, report source
  7268. option = script_getnum(st,2);
  7269. if( sd->sc.option&option )
  7270. script_pushint(st, 1);
  7271. else
  7272. script_pushint(st, 0);
  7273. return 0;
  7274. }
  7275. /// Checks if the player is in that body state (opt1).
  7276. ///
  7277. /// checkoption1(<opt1>) -> <bool>
  7278. BUILDIN_FUNC(checkoption1)
  7279. {
  7280. int opt1;
  7281. TBL_PC* sd;
  7282. sd = script_rid2sd(st);
  7283. if( sd == NULL )
  7284. return 0;// no player attached, report source
  7285. opt1 = script_getnum(st,2);
  7286. if( sd->sc.opt1 == opt1 )
  7287. script_pushint(st, 1);
  7288. else
  7289. script_pushint(st, 0);
  7290. return 0;
  7291. }
  7292. /// Checks if the player has that health state (opt2).
  7293. ///
  7294. /// checkoption2(<opt2>) -> <bool>
  7295. BUILDIN_FUNC(checkoption2)
  7296. {
  7297. int opt2;
  7298. TBL_PC* sd;
  7299. sd = script_rid2sd(st);
  7300. if( sd == NULL )
  7301. return 0;// no player attached, report source
  7302. opt2 = script_getnum(st,2);
  7303. if( sd->sc.opt2&opt2 )
  7304. script_pushint(st, 1);
  7305. else
  7306. script_pushint(st, 0);
  7307. return 0;
  7308. }
  7309. /// Changes the effect state (option) of the player.
  7310. /// <flag> defaults to 1
  7311. /// <flag>=0 : removes the option
  7312. /// <flag>=other : adds the option
  7313. ///
  7314. /// setoption <option>,<flag>;
  7315. /// setoption <option>;
  7316. BUILDIN_FUNC(setoption)
  7317. {
  7318. int option;
  7319. int flag = 1;
  7320. TBL_PC* sd;
  7321. sd = script_rid2sd(st);
  7322. if( sd == NULL )
  7323. return 0;// no player attached, report source
  7324. option = script_getnum(st,2);
  7325. if( script_hasdata(st,3) )
  7326. flag = script_getnum(st,3);
  7327. else if( !option ){// Request to remove everything.
  7328. flag = 0;
  7329. option = OPTION_FALCON|OPTION_RIDING;
  7330. #ifndef NEW_CARTS
  7331. option |= OPTION_CART;
  7332. #endif
  7333. }
  7334. if( flag ){// Add option
  7335. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  7336. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  7337. pc_setoption(sd, sd->sc.option|option);
  7338. } else// Remove option
  7339. pc_setoption(sd, sd->sc.option&~option);
  7340. return 0;
  7341. }
  7342. /// Returns if the player has a cart.
  7343. ///
  7344. /// checkcart() -> <bool>
  7345. ///
  7346. /// @author Valaris
  7347. BUILDIN_FUNC(checkcart)
  7348. {
  7349. TBL_PC* sd;
  7350. sd = script_rid2sd(st);
  7351. if( sd == NULL )
  7352. return 0;// no player attached, report source
  7353. if( pc_iscarton(sd) )
  7354. script_pushint(st, 1);
  7355. else
  7356. script_pushint(st, 0);
  7357. return 0;
  7358. }
  7359. /// Sets the cart of the player.
  7360. /// <type> defaults to 1
  7361. /// <type>=0 : removes the cart
  7362. /// <type>=1 : Normal cart
  7363. /// <type>=2 : Wooden cart
  7364. /// <type>=3 : Covered cart with flowers and ferns
  7365. /// <type>=4 : Wooden cart with a Panda doll on the back
  7366. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  7367. ///
  7368. /// setcart <type>;
  7369. /// setcart;
  7370. BUILDIN_FUNC(setcart)
  7371. {
  7372. int type = 1;
  7373. TBL_PC* sd;
  7374. sd = script_rid2sd(st);
  7375. if( sd == NULL )
  7376. return 0;// no player attached, report source
  7377. if( script_hasdata(st,2) )
  7378. type = script_getnum(st,2);
  7379. pc_setcart(sd, type);
  7380. return 0;
  7381. }
  7382. /// Returns if the player has a falcon.
  7383. ///
  7384. /// checkfalcon() -> <bool>
  7385. ///
  7386. /// @author Valaris
  7387. BUILDIN_FUNC(checkfalcon)
  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_isfalcon(sd) )
  7394. script_pushint(st, 1);
  7395. else
  7396. script_pushint(st, 0);
  7397. return 0;
  7398. }
  7399. /// Sets if the player has a falcon or not.
  7400. /// <flag> defaults to 1
  7401. ///
  7402. /// setfalcon <flag>;
  7403. /// setfalcon;
  7404. BUILDIN_FUNC(setfalcon)
  7405. {
  7406. int flag = 1;
  7407. TBL_PC* sd;
  7408. sd = script_rid2sd(st);
  7409. if( sd == NULL )
  7410. return 0;// no player attached, report source
  7411. if( script_hasdata(st,2) )
  7412. flag = script_getnum(st,2);
  7413. pc_setfalcon(sd, flag);
  7414. return 0;
  7415. }
  7416. /// Returns if the player is riding.
  7417. ///
  7418. /// checkriding() -> <bool>
  7419. ///
  7420. /// @author Valaris
  7421. BUILDIN_FUNC(checkriding)
  7422. {
  7423. TBL_PC* sd;
  7424. sd = script_rid2sd(st);
  7425. if( sd == NULL )
  7426. return 0;// no player attached, report source
  7427. if( pc_isriding(sd) || pc_isridingwug(sd) || pc_isridingdragon(sd) )
  7428. script_pushint(st, 1);
  7429. else
  7430. script_pushint(st, 0);
  7431. return 0;
  7432. }
  7433. /// Sets if the player is riding.
  7434. /// <flag> defaults to 1
  7435. ///
  7436. /// setriding <flag>;
  7437. /// setriding;
  7438. BUILDIN_FUNC(setriding)
  7439. {
  7440. int flag = 1;
  7441. TBL_PC* sd;
  7442. sd = script_rid2sd(st);
  7443. if( sd == NULL )
  7444. return 0;// no player attached, report source
  7445. if( script_hasdata(st,2) )
  7446. flag = script_getnum(st,2);
  7447. pc_setriding(sd, flag);
  7448. return 0;
  7449. }
  7450. /// Returns if the player has a warg.
  7451. ///
  7452. /// checkwug() -> <bool>
  7453. ///
  7454. BUILDIN_FUNC(checkwug)
  7455. {
  7456. TBL_PC* sd;
  7457. sd = script_rid2sd(st);
  7458. if( sd == NULL )
  7459. return 0;// no player attached, report source
  7460. if( pc_iswug(sd) || pc_isridingwug(sd) )
  7461. script_pushint(st, 1);
  7462. else
  7463. script_pushint(st, 0);
  7464. return 0;
  7465. }
  7466. /// Returns if the player is wearing MADO Gear.
  7467. ///
  7468. /// checkmadogear() -> <bool>
  7469. ///
  7470. BUILDIN_FUNC(checkmadogear)
  7471. {
  7472. TBL_PC* sd;
  7473. sd = script_rid2sd(st);
  7474. if( sd == NULL )
  7475. return 0;// no player attached, report source
  7476. if( pc_ismadogear(sd) )
  7477. script_pushint(st, 1);
  7478. else
  7479. script_pushint(st, 0);
  7480. return 0;
  7481. }
  7482. /// Sets if the player is riding MADO Gear.
  7483. /// <flag> defaults to 1
  7484. ///
  7485. /// setmadogear <flag>;
  7486. /// setmadogear;
  7487. BUILDIN_FUNC(setmadogear)
  7488. {
  7489. int flag = 1;
  7490. TBL_PC* sd;
  7491. sd = script_rid2sd(st);
  7492. if( sd == NULL )
  7493. return 0;// no player attached, report source
  7494. if( script_hasdata(st,2) )
  7495. flag = script_getnum(st,2);
  7496. pc_setmadogear(sd, flag);
  7497. return 0;
  7498. }
  7499. /// Sets the save point of the player.
  7500. ///
  7501. /// save "<map name>",<x>,<y>
  7502. /// savepoint "<map name>",<x>,<y>
  7503. BUILDIN_FUNC(savepoint)
  7504. {
  7505. int x;
  7506. int y;
  7507. short map;
  7508. const char* str;
  7509. TBL_PC* sd;
  7510. sd = script_rid2sd(st);
  7511. if( sd == NULL )
  7512. return 0;// no player attached, report source
  7513. str = script_getstr(st, 2);
  7514. x = script_getnum(st,3);
  7515. y = script_getnum(st,4);
  7516. map = mapindex_name2id(str);
  7517. if( map )
  7518. pc_setsavepoint(sd, map, x, y);
  7519. return 0;
  7520. }
  7521. /*==========================================
  7522. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  7523. *------------------------------------------*/
  7524. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  7525. {
  7526. int type;
  7527. time_t timer;
  7528. struct tm *t;
  7529. type=script_getnum(st,2);
  7530. switch(type){
  7531. case 2:
  7532. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  7533. // from the system clock.)
  7534. script_pushint(st,(int)time(NULL));
  7535. break;
  7536. case 1:
  7537. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  7538. time(&timer);
  7539. t=localtime(&timer);
  7540. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  7541. break;
  7542. case 0:
  7543. default:
  7544. //type 0:(System Ticks)
  7545. script_pushint(st,gettick());
  7546. break;
  7547. }
  7548. return 0;
  7549. }
  7550. /*==========================================
  7551. * GetTime(Type);
  7552. * 1: Sec 2: Min 3: Hour
  7553. * 4: WeekDay 5: MonthDay 6: Month
  7554. * 7: Year
  7555. *------------------------------------------*/
  7556. BUILDIN_FUNC(gettime) /* Asgard Version */
  7557. {
  7558. int type;
  7559. time_t timer;
  7560. struct tm *t;
  7561. type=script_getnum(st,2);
  7562. time(&timer);
  7563. t=localtime(&timer);
  7564. switch(type){
  7565. case 1://Sec(0~59)
  7566. script_pushint(st,t->tm_sec);
  7567. break;
  7568. case 2://Min(0~59)
  7569. script_pushint(st,t->tm_min);
  7570. break;
  7571. case 3://Hour(0~23)
  7572. script_pushint(st,t->tm_hour);
  7573. break;
  7574. case 4://WeekDay(0~6)
  7575. script_pushint(st,t->tm_wday);
  7576. break;
  7577. case 5://MonthDay(01~31)
  7578. script_pushint(st,t->tm_mday);
  7579. break;
  7580. case 6://Month(01~12)
  7581. script_pushint(st,t->tm_mon+1);
  7582. break;
  7583. case 7://Year(20xx)
  7584. script_pushint(st,t->tm_year+1900);
  7585. break;
  7586. case 8://Year Day(01~366)
  7587. script_pushint(st,t->tm_yday+1);
  7588. break;
  7589. default://(format error)
  7590. script_pushint(st,-1);
  7591. break;
  7592. }
  7593. return 0;
  7594. }
  7595. /*==========================================
  7596. * GetTimeStr("TimeFMT", Length);
  7597. *------------------------------------------*/
  7598. BUILDIN_FUNC(gettimestr)
  7599. {
  7600. char *tmpstr;
  7601. const char *fmtstr;
  7602. int maxlen;
  7603. time_t now = time(NULL);
  7604. fmtstr=script_getstr(st,2);
  7605. maxlen=script_getnum(st,3);
  7606. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  7607. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  7608. tmpstr[maxlen]='\0';
  7609. script_pushstr(st,tmpstr);
  7610. return 0;
  7611. }
  7612. /*==========================================
  7613. * Open player storage
  7614. *------------------------------------------*/
  7615. BUILDIN_FUNC(openstorage)
  7616. {
  7617. TBL_PC* sd;
  7618. sd = script_rid2sd(st);
  7619. if( sd == NULL )
  7620. return 0;
  7621. storage_storageopen(sd);
  7622. return 0;
  7623. }
  7624. BUILDIN_FUNC(guildopenstorage)
  7625. {
  7626. TBL_PC* sd;
  7627. int ret;
  7628. sd = script_rid2sd(st);
  7629. if( sd == NULL )
  7630. return 0;
  7631. ret = storage_guild_storageopen(sd);
  7632. script_pushint(st,ret);
  7633. return 0;
  7634. }
  7635. /*==========================================
  7636. * Make player use a skill trought item usage
  7637. *------------------------------------------*/
  7638. /// itemskill <skill id>,<level>
  7639. /// itemskill "<skill name>",<level>
  7640. BUILDIN_FUNC(itemskill)
  7641. {
  7642. int id;
  7643. int lv;
  7644. TBL_PC* sd;
  7645. sd = script_rid2sd(st);
  7646. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  7647. return 0;
  7648. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7649. lv = script_getnum(st,3);
  7650. sd->skillitem=id;
  7651. sd->skillitemlv=lv;
  7652. clif_item_skill(sd,id,lv);
  7653. return 0;
  7654. }
  7655. /*==========================================
  7656. * Attempt to create an item
  7657. *------------------------------------------*/
  7658. BUILDIN_FUNC(produce)
  7659. {
  7660. int trigger;
  7661. TBL_PC* sd;
  7662. sd = script_rid2sd(st);
  7663. if( sd == NULL )
  7664. return 0;
  7665. trigger=script_getnum(st,2);
  7666. clif_skill_produce_mix_list(sd, -1, trigger);
  7667. return 0;
  7668. }
  7669. /*==========================================
  7670. *
  7671. *------------------------------------------*/
  7672. BUILDIN_FUNC(cooking)
  7673. {
  7674. int trigger;
  7675. TBL_PC* sd;
  7676. sd = script_rid2sd(st);
  7677. if( sd == NULL )
  7678. return 0;
  7679. trigger=script_getnum(st,2);
  7680. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  7681. return 0;
  7682. }
  7683. /*==========================================
  7684. * Create a pet
  7685. *------------------------------------------*/
  7686. BUILDIN_FUNC(makepet)
  7687. {
  7688. TBL_PC* sd;
  7689. int id,pet_id;
  7690. id=script_getnum(st,2);
  7691. sd = script_rid2sd(st);
  7692. if( sd == NULL )
  7693. return 0;
  7694. pet_id = search_petDB_index(id, PET_CLASS);
  7695. if (pet_id < 0)
  7696. pet_id = search_petDB_index(id, PET_EGG);
  7697. if (pet_id >= 0 && sd) {
  7698. sd->catch_target_class = pet_db[pet_id].class_;
  7699. intif_create_pet(
  7700. sd->status.account_id, sd->status.char_id,
  7701. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  7702. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  7703. 100, 0, 1, pet_db[pet_id].jname);
  7704. }
  7705. return 0;
  7706. }
  7707. /*==========================================
  7708. * Give player exp base,job * quest_exp_rate/100
  7709. *------------------------------------------*/
  7710. BUILDIN_FUNC(getexp)
  7711. {
  7712. TBL_PC* sd;
  7713. int base=0,job=0;
  7714. double bonus;
  7715. sd = script_rid2sd(st);
  7716. if( sd == NULL )
  7717. return 0;
  7718. base=script_getnum(st,2);
  7719. job =script_getnum(st,3);
  7720. if(base<0 || job<0)
  7721. return 0;
  7722. // bonus for npc-given exp
  7723. bonus = battle_config.quest_exp_rate / 100.;
  7724. base = (int) cap_value(base * bonus, 0, INT_MAX);
  7725. job = (int) cap_value(job * bonus, 0, INT_MAX);
  7726. pc_gainexp(sd, NULL, base, job, true);
  7727. return 0;
  7728. }
  7729. /*==========================================
  7730. * Gain guild exp [Celest]
  7731. *------------------------------------------*/
  7732. BUILDIN_FUNC(guildgetexp)
  7733. {
  7734. TBL_PC* sd;
  7735. int exp;
  7736. sd = script_rid2sd(st);
  7737. if( sd == NULL )
  7738. return 0;
  7739. exp = script_getnum(st,2);
  7740. if(exp < 0)
  7741. return 0;
  7742. if(sd && sd->status.guild_id > 0)
  7743. guild_getexp (sd, exp);
  7744. return 0;
  7745. }
  7746. /*==========================================
  7747. * Changes the guild master of a guild [Skotlex]
  7748. *------------------------------------------*/
  7749. BUILDIN_FUNC(guildchangegm)
  7750. {
  7751. TBL_PC *sd;
  7752. int guild_id;
  7753. const char *name;
  7754. guild_id = script_getnum(st,2);
  7755. name = script_getstr(st,3);
  7756. sd=map_nick2sd(name);
  7757. if (!sd)
  7758. script_pushint(st,0);
  7759. else
  7760. script_pushint(st,guild_gm_change(guild_id, sd));
  7761. return 0;
  7762. }
  7763. /*==========================================
  7764. * Spawn a monster :
  7765. @mapn,x,y : location
  7766. @str : monster name
  7767. @class_ : mob_id
  7768. @amount : nb to spawn
  7769. @event : event to attach to mob
  7770. *------------------------------------------*/
  7771. BUILDIN_FUNC(monster)
  7772. {
  7773. const char* mapn = script_getstr(st,2);
  7774. int x = script_getnum(st,3);
  7775. int y = script_getnum(st,4);
  7776. const char* str = script_getstr(st,5);
  7777. int class_ = script_getnum(st,6);
  7778. int amount = script_getnum(st,7);
  7779. const char* event = "";
  7780. unsigned int size = SZ_SMALL;
  7781. unsigned int ai = AI_NONE;
  7782. struct map_session_data* sd;
  7783. int16 m;
  7784. if (script_hasdata(st, 8)) {
  7785. event = script_getstr(st, 8);
  7786. check_event(st, event);
  7787. }
  7788. if (script_hasdata(st, 9)) {
  7789. size = script_getnum(st, 9);
  7790. if (size > 3) {
  7791. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  7792. return 1;
  7793. }
  7794. }
  7795. if (script_hasdata(st, 10)) {
  7796. ai = script_getnum(st, 10);
  7797. if (ai >= AI_MAX) {
  7798. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  7799. return 1;
  7800. }
  7801. }
  7802. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7803. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7804. return 1;
  7805. }
  7806. sd = map_id2sd(st->rid);
  7807. if (sd && strcmp(mapn, "this") == 0)
  7808. m = sd->bl.m;
  7809. else
  7810. m = map_mapname2mapid(mapn);
  7811. mob_once_spawn(sd, m, x, y, str, class_, amount, event, size, ai);
  7812. return 0;
  7813. }
  7814. /*==========================================
  7815. * Request List of Monster Drops
  7816. *------------------------------------------*/
  7817. BUILDIN_FUNC(getmobdrops)
  7818. {
  7819. int class_ = script_getnum(st,2);
  7820. int i, j = 0;
  7821. struct mob_db *mob;
  7822. if( !mobdb_checkid(class_) )
  7823. {
  7824. script_pushint(st, 0);
  7825. return 0;
  7826. }
  7827. mob = mob_db(class_);
  7828. for( i = 0; i < MAX_MOB_DROP; i++ )
  7829. {
  7830. if( mob->dropitem[i].nameid < 1 )
  7831. continue;
  7832. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7833. continue;
  7834. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7835. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7836. j++;
  7837. }
  7838. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7839. script_pushint(st, 1);
  7840. return 0;
  7841. }
  7842. /*==========================================
  7843. * Same as monster but randomize location in x0,x1,y0,y1 area
  7844. *------------------------------------------*/
  7845. BUILDIN_FUNC(areamonster)
  7846. {
  7847. const char* mapn = script_getstr(st,2);
  7848. int x0 = script_getnum(st,3);
  7849. int y0 = script_getnum(st,4);
  7850. int x1 = script_getnum(st,5);
  7851. int y1 = script_getnum(st,6);
  7852. const char* str = script_getstr(st,7);
  7853. int class_ = script_getnum(st,8);
  7854. int amount = script_getnum(st,9);
  7855. const char* event = "";
  7856. unsigned int size = SZ_SMALL;
  7857. unsigned int ai = AI_NONE;
  7858. struct map_session_data* sd;
  7859. int16 m;
  7860. if (script_hasdata(st,10)) {
  7861. event = script_getstr(st, 10);
  7862. check_event(st, event);
  7863. }
  7864. if (script_hasdata(st, 11)) {
  7865. size = script_getnum(st, 11);
  7866. if (size > 3) {
  7867. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  7868. return 1;
  7869. }
  7870. }
  7871. if (script_hasdata(st, 12)) {
  7872. ai = script_getnum(st, 12);
  7873. if (ai >= AI_MAX) {
  7874. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  7875. return 1;
  7876. }
  7877. }
  7878. sd = map_id2sd(st->rid);
  7879. if (sd && strcmp(mapn, "this") == 0)
  7880. m = sd->bl.m;
  7881. else
  7882. m = map_mapname2mapid(mapn);
  7883. mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, amount, event, size, ai);
  7884. return 0;
  7885. }
  7886. /*==========================================
  7887. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  7888. *------------------------------------------*/
  7889. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7890. { //same fix but with killmonster instead - stripping events from mobs.
  7891. TBL_MOB* md = (TBL_MOB*)bl;
  7892. char *event=va_arg(ap,char *);
  7893. int allflag=va_arg(ap,int);
  7894. md->state.npc_killmonster = 1;
  7895. if(!allflag){
  7896. if(strcmp(event,md->npc_event)==0)
  7897. status_kill(bl);
  7898. }else{
  7899. if(!md->spawn)
  7900. status_kill(bl);
  7901. }
  7902. md->state.npc_killmonster = 0;
  7903. return 0;
  7904. }
  7905. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7906. {
  7907. TBL_MOB* md = (TBL_MOB*)bl;
  7908. char *event=va_arg(ap,char *);
  7909. int allflag=va_arg(ap,int);
  7910. if(!allflag){
  7911. if(strcmp(event,md->npc_event)==0)
  7912. status_kill(bl);
  7913. }else{
  7914. if(!md->spawn)
  7915. status_kill(bl);
  7916. }
  7917. return 0;
  7918. }
  7919. BUILDIN_FUNC(killmonster)
  7920. {
  7921. const char *mapname,*event;
  7922. int16 m,allflag=0;
  7923. mapname=script_getstr(st,2);
  7924. event=script_getstr(st,3);
  7925. if(strcmp(event,"All")==0)
  7926. allflag = 1;
  7927. else
  7928. check_event(st, event);
  7929. if( (m=map_mapname2mapid(mapname))<0 )
  7930. return 0;
  7931. if( script_hasdata(st,4) ) {
  7932. if ( script_getnum(st,4) == 1 ) {
  7933. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7934. return 0;
  7935. }
  7936. }
  7937. map_freeblock_lock();
  7938. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7939. map_freeblock_unlock();
  7940. return 0;
  7941. }
  7942. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7943. { //Strips the event from the mob if it's killed the old method.
  7944. struct mob_data *md;
  7945. md = BL_CAST(BL_MOB, bl);
  7946. if (md->npc_event[0])
  7947. md->npc_event[0] = 0;
  7948. status_kill(bl);
  7949. return 0;
  7950. }
  7951. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7952. {
  7953. status_kill(bl);
  7954. return 0;
  7955. }
  7956. BUILDIN_FUNC(killmonsterall)
  7957. {
  7958. const char *mapname;
  7959. int16 m;
  7960. mapname=script_getstr(st,2);
  7961. if( (m = map_mapname2mapid(mapname))<0 )
  7962. return 0;
  7963. if( script_hasdata(st,3) ) {
  7964. if ( script_getnum(st,3) == 1 ) {
  7965. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  7966. return 0;
  7967. }
  7968. }
  7969. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  7970. return 0;
  7971. }
  7972. /*==========================================
  7973. * Creates a clone of a player.
  7974. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7975. *------------------------------------------*/
  7976. BUILDIN_FUNC(clone)
  7977. {
  7978. TBL_PC *sd, *msd=NULL;
  7979. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7980. unsigned int duration = 0;
  7981. const char *map,*event="";
  7982. map=script_getstr(st,2);
  7983. x=script_getnum(st,3);
  7984. y=script_getnum(st,4);
  7985. event=script_getstr(st,5);
  7986. char_id=script_getnum(st,6);
  7987. if( script_hasdata(st,7) )
  7988. master_id=script_getnum(st,7);
  7989. if( script_hasdata(st,8) )
  7990. mode=script_getnum(st,8);
  7991. if( script_hasdata(st,9) )
  7992. flag=script_getnum(st,9);
  7993. if( script_hasdata(st,10) )
  7994. duration=script_getnum(st,10);
  7995. check_event(st, event);
  7996. m = map_mapname2mapid(map);
  7997. if (m < 0) return 0;
  7998. sd = map_charid2sd(char_id);
  7999. if (master_id) {
  8000. msd = map_charid2sd(master_id);
  8001. if (msd)
  8002. master_id = msd->bl.id;
  8003. else
  8004. master_id = 0;
  8005. }
  8006. if (sd) //Return ID of newly crafted clone.
  8007. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8008. else //Failed to create clone.
  8009. script_pushint(st,0);
  8010. return 0;
  8011. }
  8012. /*==========================================
  8013. *------------------------------------------*/
  8014. BUILDIN_FUNC(doevent)
  8015. {
  8016. const char* event = script_getstr(st,2);
  8017. struct map_session_data* sd;
  8018. if( ( sd = script_rid2sd(st) ) == NULL )
  8019. {
  8020. return 0;
  8021. }
  8022. check_event(st, event);
  8023. npc_event(sd, event, 0);
  8024. return 0;
  8025. }
  8026. /*==========================================
  8027. *------------------------------------------*/
  8028. BUILDIN_FUNC(donpcevent)
  8029. {
  8030. const char* event = script_getstr(st,2);
  8031. check_event(st, event);
  8032. if( !npc_event_do(event) ) {
  8033. struct npc_data * nd = map_id2nd(st->oid);
  8034. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8035. script_pushint(st, 0);
  8036. } else
  8037. script_pushint(st, 1);
  8038. return 0;
  8039. }
  8040. /// for Aegis compatibility
  8041. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8042. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8043. {
  8044. const char* npc = script_getstr(st,2);
  8045. const char* command = script_getstr(st,3);
  8046. char event[EVENT_NAME_LENGTH];
  8047. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8048. check_event(st, event);
  8049. npc_event_do(event);
  8050. return 0;
  8051. }
  8052. /*==========================================
  8053. *------------------------------------------*/
  8054. BUILDIN_FUNC(addtimer)
  8055. {
  8056. int tick = script_getnum(st,2);
  8057. const char* event = script_getstr(st, 3);
  8058. TBL_PC* sd;
  8059. check_event(st, event);
  8060. sd = script_rid2sd(st);
  8061. if( sd == NULL )
  8062. return 0;
  8063. pc_addeventtimer(sd,tick,event);
  8064. return 0;
  8065. }
  8066. /*==========================================
  8067. *------------------------------------------*/
  8068. BUILDIN_FUNC(deltimer)
  8069. {
  8070. const char *event;
  8071. TBL_PC* sd;
  8072. event=script_getstr(st, 2);
  8073. sd = script_rid2sd(st);
  8074. if( sd == NULL )
  8075. return 0;
  8076. check_event(st, event);
  8077. pc_deleventtimer(sd,event);
  8078. return 0;
  8079. }
  8080. /*==========================================
  8081. *------------------------------------------*/
  8082. BUILDIN_FUNC(addtimercount)
  8083. {
  8084. const char *event;
  8085. int tick;
  8086. TBL_PC* sd;
  8087. event=script_getstr(st, 2);
  8088. tick=script_getnum(st,3);
  8089. sd = script_rid2sd(st);
  8090. if( sd == NULL )
  8091. return 0;
  8092. check_event(st, event);
  8093. pc_addeventtimercount(sd,event,tick);
  8094. return 0;
  8095. }
  8096. /*==========================================
  8097. *------------------------------------------*/
  8098. BUILDIN_FUNC(initnpctimer)
  8099. {
  8100. struct npc_data *nd;
  8101. int flag = 0;
  8102. if( script_hasdata(st,3) )
  8103. { //Two arguments: NPC name and attach flag.
  8104. nd = npc_name2id(script_getstr(st, 2));
  8105. flag = script_getnum(st,3);
  8106. }
  8107. else if( script_hasdata(st,2) )
  8108. { //Check if argument is numeric (flag) or string (npc name)
  8109. struct script_data *data;
  8110. data = script_getdata(st,2);
  8111. get_val(st,data);
  8112. if( data_isstring(data) ) //NPC name
  8113. nd = npc_name2id(conv_str(st, data));
  8114. else if( data_isint(data) ) //Flag
  8115. {
  8116. nd = (struct npc_data *)map_id2bl(st->oid);
  8117. flag = conv_num(st,data);
  8118. }
  8119. else
  8120. {
  8121. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8122. return 1;
  8123. }
  8124. }
  8125. else
  8126. nd = (struct npc_data *)map_id2bl(st->oid);
  8127. if( !nd )
  8128. return 0;
  8129. if( flag ) //Attach
  8130. {
  8131. TBL_PC* sd = script_rid2sd(st);
  8132. if( sd == NULL )
  8133. return 0;
  8134. nd->u.scr.rid = sd->bl.id;
  8135. }
  8136. nd->u.scr.timertick = 0;
  8137. npc_settimerevent_tick(nd,0);
  8138. npc_timerevent_start(nd, st->rid);
  8139. return 0;
  8140. }
  8141. /*==========================================
  8142. *------------------------------------------*/
  8143. BUILDIN_FUNC(startnpctimer)
  8144. {
  8145. struct npc_data *nd;
  8146. int flag = 0;
  8147. if( script_hasdata(st,3) )
  8148. { //Two arguments: NPC name and attach flag.
  8149. nd = npc_name2id(script_getstr(st, 2));
  8150. flag = script_getnum(st,3);
  8151. }
  8152. else if( script_hasdata(st,2) )
  8153. { //Check if argument is numeric (flag) or string (npc name)
  8154. struct script_data *data;
  8155. data = script_getdata(st,2);
  8156. get_val(st,data);
  8157. if( data_isstring(data) ) //NPC name
  8158. nd = npc_name2id(conv_str(st, data));
  8159. else if( data_isint(data) ) //Flag
  8160. {
  8161. nd = (struct npc_data *)map_id2bl(st->oid);
  8162. flag = conv_num(st,data);
  8163. }
  8164. else
  8165. {
  8166. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8167. return 1;
  8168. }
  8169. }
  8170. else
  8171. nd=(struct npc_data *)map_id2bl(st->oid);
  8172. if( !nd )
  8173. return 0;
  8174. if( flag ) //Attach
  8175. {
  8176. TBL_PC* sd = script_rid2sd(st);
  8177. if( sd == NULL )
  8178. return 0;
  8179. nd->u.scr.rid = sd->bl.id;
  8180. }
  8181. npc_timerevent_start(nd, st->rid);
  8182. return 0;
  8183. }
  8184. /*==========================================
  8185. *------------------------------------------*/
  8186. BUILDIN_FUNC(stopnpctimer)
  8187. {
  8188. struct npc_data *nd;
  8189. int flag = 0;
  8190. if( script_hasdata(st,3) )
  8191. { //Two arguments: NPC name and attach flag.
  8192. nd = npc_name2id(script_getstr(st, 2));
  8193. flag = script_getnum(st,3);
  8194. }
  8195. else if( script_hasdata(st,2) )
  8196. { //Check if argument is numeric (flag) or string (npc name)
  8197. struct script_data *data;
  8198. data = script_getdata(st,2);
  8199. get_val(st,data);
  8200. if( data_isstring(data) ) //NPC name
  8201. nd = npc_name2id(conv_str(st, data));
  8202. else if( data_isint(data) ) //Flag
  8203. {
  8204. nd = (struct npc_data *)map_id2bl(st->oid);
  8205. flag = conv_num(st,data);
  8206. }
  8207. else
  8208. {
  8209. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8210. return 1;
  8211. }
  8212. }
  8213. else
  8214. nd=(struct npc_data *)map_id2bl(st->oid);
  8215. if( !nd )
  8216. return 0;
  8217. if( flag ) //Detach
  8218. nd->u.scr.rid = 0;
  8219. npc_timerevent_stop(nd);
  8220. return 0;
  8221. }
  8222. /*==========================================
  8223. *------------------------------------------*/
  8224. BUILDIN_FUNC(getnpctimer)
  8225. {
  8226. struct npc_data *nd;
  8227. TBL_PC *sd;
  8228. int type = script_getnum(st,2);
  8229. int val = 0;
  8230. if( script_hasdata(st,3) )
  8231. nd = npc_name2id(script_getstr(st,3));
  8232. else
  8233. nd = (struct npc_data *)map_id2bl(st->oid);
  8234. if( !nd || nd->bl.type != BL_NPC )
  8235. {
  8236. script_pushint(st,0);
  8237. ShowError("getnpctimer: Invalid NPC.\n");
  8238. return 1;
  8239. }
  8240. switch( type )
  8241. {
  8242. case 0: val = npc_gettimerevent_tick(nd); break;
  8243. case 1:
  8244. if( nd->u.scr.rid )
  8245. {
  8246. sd = map_id2sd(nd->u.scr.rid);
  8247. if( !sd )
  8248. {
  8249. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8250. break;
  8251. }
  8252. val = (sd->npc_timer_id != INVALID_TIMER);
  8253. }
  8254. else
  8255. val = (nd->u.scr.timerid != INVALID_TIMER);
  8256. break;
  8257. case 2: val = nd->u.scr.timeramount; break;
  8258. }
  8259. script_pushint(st,val);
  8260. return 0;
  8261. }
  8262. /*==========================================
  8263. *------------------------------------------*/
  8264. BUILDIN_FUNC(setnpctimer)
  8265. {
  8266. int tick;
  8267. struct npc_data *nd;
  8268. tick = script_getnum(st,2);
  8269. if( script_hasdata(st,3) )
  8270. nd = npc_name2id(script_getstr(st,3));
  8271. else
  8272. nd = (struct npc_data *)map_id2bl(st->oid);
  8273. if( !nd || nd->bl.type != BL_NPC )
  8274. {
  8275. script_pushint(st,1);
  8276. ShowError("setnpctimer: Invalid NPC.\n");
  8277. return 1;
  8278. }
  8279. npc_settimerevent_tick(nd,tick);
  8280. script_pushint(st,0);
  8281. return 0;
  8282. }
  8283. /*==========================================
  8284. * attaches the player rid to the timer [Celest]
  8285. *------------------------------------------*/
  8286. BUILDIN_FUNC(attachnpctimer)
  8287. {
  8288. TBL_PC *sd;
  8289. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  8290. if( !nd || nd->bl.type != BL_NPC )
  8291. {
  8292. script_pushint(st,1);
  8293. ShowError("setnpctimer: Invalid NPC.\n");
  8294. return 1;
  8295. }
  8296. if( script_hasdata(st,2) )
  8297. sd = map_nick2sd(script_getstr(st,2));
  8298. else
  8299. sd = script_rid2sd(st);
  8300. if( !sd )
  8301. {
  8302. script_pushint(st,1);
  8303. ShowWarning("attachnpctimer: Invalid player.\n");
  8304. return 1;
  8305. }
  8306. nd->u.scr.rid = sd->bl.id;
  8307. script_pushint(st,0);
  8308. return 0;
  8309. }
  8310. /*==========================================
  8311. * detaches a player rid from the timer [Celest]
  8312. *------------------------------------------*/
  8313. BUILDIN_FUNC(detachnpctimer)
  8314. {
  8315. struct npc_data *nd;
  8316. if( script_hasdata(st,2) )
  8317. nd = npc_name2id(script_getstr(st,2));
  8318. else
  8319. nd = (struct npc_data *)map_id2bl(st->oid);
  8320. if( !nd || nd->bl.type != BL_NPC )
  8321. {
  8322. script_pushint(st,1);
  8323. ShowError("detachnpctimer: Invalid NPC.\n");
  8324. return 1;
  8325. }
  8326. nd->u.scr.rid = 0;
  8327. script_pushint(st,0);
  8328. return 0;
  8329. }
  8330. /*==========================================
  8331. * To avoid "player not attached" script errors, this function is provided,
  8332. * it checks if there is a player attached to the current script. [Skotlex]
  8333. * If no, returns 0, if yes, returns the account_id of the attached player.
  8334. *------------------------------------------*/
  8335. BUILDIN_FUNC(playerattached)
  8336. {
  8337. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  8338. script_pushint(st,0);
  8339. else
  8340. script_pushint(st,st->rid);
  8341. return 0;
  8342. }
  8343. /*==========================================
  8344. *------------------------------------------*/
  8345. BUILDIN_FUNC(announce)
  8346. {
  8347. const char *mes = script_getstr(st,2);
  8348. int flag = script_getnum(st,3);
  8349. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  8350. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  8351. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  8352. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  8353. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  8354. if (flag&0x0f) // Broadcast source or broadcast region defined
  8355. {
  8356. send_target target;
  8357. 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
  8358. if (bl == NULL)
  8359. return 0;
  8360. flag &= 0x07;
  8361. target = (flag == 1) ? ALL_SAMEMAP :
  8362. (flag == 2) ? AREA :
  8363. (flag == 3) ? SELF :
  8364. ALL_CLIENT;
  8365. if (fontColor)
  8366. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  8367. else
  8368. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  8369. }
  8370. else
  8371. {
  8372. if (fontColor)
  8373. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  8374. else
  8375. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  8376. }
  8377. return 0;
  8378. }
  8379. /*==========================================
  8380. *------------------------------------------*/
  8381. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  8382. {
  8383. char *mes = va_arg(ap, char *);
  8384. int len = va_arg(ap, int);
  8385. int type = va_arg(ap, int);
  8386. char *fontColor = va_arg(ap, char *);
  8387. short fontType = (short)va_arg(ap, int);
  8388. short fontSize = (short)va_arg(ap, int);
  8389. short fontAlign = (short)va_arg(ap, int);
  8390. short fontY = (short)va_arg(ap, int);
  8391. if (fontColor)
  8392. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  8393. else
  8394. clif_broadcast(bl, mes, len, type, SELF);
  8395. return 0;
  8396. }
  8397. BUILDIN_FUNC(mapannounce)
  8398. {
  8399. const char *mapname = script_getstr(st,2);
  8400. const char *mes = script_getstr(st,3);
  8401. int flag = script_getnum(st,4);
  8402. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  8403. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  8404. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  8405. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  8406. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  8407. int16 m;
  8408. if ((m = map_mapname2mapid(mapname)) < 0)
  8409. return 0;
  8410. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  8411. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8412. return 0;
  8413. }
  8414. /*==========================================
  8415. *------------------------------------------*/
  8416. BUILDIN_FUNC(areaannounce)
  8417. {
  8418. const char *mapname = script_getstr(st,2);
  8419. int x0 = script_getnum(st,3);
  8420. int y0 = script_getnum(st,4);
  8421. int x1 = script_getnum(st,5);
  8422. int y1 = script_getnum(st,6);
  8423. const char *mes = script_getstr(st,7);
  8424. int flag = script_getnum(st,8);
  8425. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  8426. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  8427. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  8428. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  8429. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  8430. int16 m;
  8431. if ((m = map_mapname2mapid(mapname)) < 0)
  8432. return 0;
  8433. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  8434. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8435. return 0;
  8436. }
  8437. /*==========================================
  8438. *------------------------------------------*/
  8439. BUILDIN_FUNC(getusers)
  8440. {
  8441. int flag, val = 0;
  8442. struct map_session_data* sd;
  8443. struct block_list* bl = NULL;
  8444. flag = script_getnum(st,2);
  8445. switch(flag&0x07)
  8446. {
  8447. case 0:
  8448. if(flag&0x8)
  8449. {// npc
  8450. bl = map_id2bl(st->oid);
  8451. }
  8452. else if((sd = script_rid2sd(st))!=NULL)
  8453. {// pc
  8454. bl = &sd->bl;
  8455. }
  8456. if(bl)
  8457. {
  8458. val = map[bl->m].users;
  8459. }
  8460. break;
  8461. case 1:
  8462. val = map_getusers();
  8463. break;
  8464. default:
  8465. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  8466. script_pushint(st,0);
  8467. return 1;
  8468. }
  8469. script_pushint(st,val);
  8470. return 0;
  8471. }
  8472. /*==========================================
  8473. * Works like @WHO - displays all online users names in window
  8474. *------------------------------------------*/
  8475. BUILDIN_FUNC(getusersname)
  8476. {
  8477. TBL_PC *sd, *pl_sd;
  8478. int /*disp_num=1,*/ group_level = 0;
  8479. struct s_mapiterator* iter;
  8480. sd = script_rid2sd(st);
  8481. if (!sd) return 0;
  8482. group_level = pc_get_group_level(sd);
  8483. iter = mapit_getallusers();
  8484. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  8485. {
  8486. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  8487. continue; // skip hidden sessions
  8488. /* Temporary fix for bugreport:1023.
  8489. * Do not uncomment unless you want thousands of 'next' buttons.
  8490. if((disp_num++)%10==0)
  8491. clif_scriptnext(sd,st->oid);*/
  8492. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  8493. }
  8494. mapit_free(iter);
  8495. return 0;
  8496. }
  8497. /*==========================================
  8498. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  8499. *------------------------------------------*/
  8500. BUILDIN_FUNC(getmapguildusers)
  8501. {
  8502. const char *str;
  8503. int16 m;
  8504. int gid;
  8505. int i=0,c=0;
  8506. struct guild *g = NULL;
  8507. str=script_getstr(st,2);
  8508. gid=script_getnum(st,3);
  8509. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  8510. script_pushint(st,-1);
  8511. return 0;
  8512. }
  8513. g = guild_search(gid);
  8514. if (g){
  8515. for(i = 0; i < g->max_member; i++)
  8516. {
  8517. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  8518. c++;
  8519. }
  8520. }
  8521. script_pushint(st,c);
  8522. return 0;
  8523. }
  8524. /*==========================================
  8525. *------------------------------------------*/
  8526. BUILDIN_FUNC(getmapusers)
  8527. {
  8528. const char *str;
  8529. int16 m;
  8530. str=script_getstr(st,2);
  8531. if( (m=map_mapname2mapid(str))< 0){
  8532. script_pushint(st,-1);
  8533. return 0;
  8534. }
  8535. script_pushint(st,map[m].users);
  8536. return 0;
  8537. }
  8538. /*==========================================
  8539. *------------------------------------------*/
  8540. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  8541. {
  8542. int *users=va_arg(ap,int *);
  8543. (*users)++;
  8544. return 0;
  8545. }
  8546. BUILDIN_FUNC(getareausers)
  8547. {
  8548. const char *str;
  8549. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  8550. str=script_getstr(st,2);
  8551. x0=script_getnum(st,3);
  8552. y0=script_getnum(st,4);
  8553. x1=script_getnum(st,5);
  8554. y1=script_getnum(st,6);
  8555. if( (m=map_mapname2mapid(str))< 0){
  8556. script_pushint(st,-1);
  8557. return 0;
  8558. }
  8559. map_foreachinarea(buildin_getareausers_sub,
  8560. m,x0,y0,x1,y1,BL_PC,&users);
  8561. script_pushint(st,users);
  8562. return 0;
  8563. }
  8564. /*==========================================
  8565. *------------------------------------------*/
  8566. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  8567. {
  8568. int item=va_arg(ap,int);
  8569. int *amount=va_arg(ap,int *);
  8570. struct flooritem_data *drop=(struct flooritem_data *)bl;
  8571. if(drop->item_data.nameid==item)
  8572. (*amount)+=drop->item_data.amount;
  8573. return 0;
  8574. }
  8575. BUILDIN_FUNC(getareadropitem)
  8576. {
  8577. const char *str;
  8578. int16 m,x0,y0,x1,y1;
  8579. int item,amount=0;
  8580. struct script_data *data;
  8581. str=script_getstr(st,2);
  8582. x0=script_getnum(st,3);
  8583. y0=script_getnum(st,4);
  8584. x1=script_getnum(st,5);
  8585. y1=script_getnum(st,6);
  8586. data=script_getdata(st,7);
  8587. get_val(st,data);
  8588. if( data_isstring(data) ){
  8589. const char *name=conv_str(st,data);
  8590. struct item_data *item_data = itemdb_searchname(name);
  8591. item=UNKNOWN_ITEM_ID;
  8592. if( item_data )
  8593. item=item_data->nameid;
  8594. }else
  8595. item=conv_num(st,data);
  8596. if( (m=map_mapname2mapid(str))< 0){
  8597. script_pushint(st,-1);
  8598. return 0;
  8599. }
  8600. map_foreachinarea(buildin_getareadropitem_sub,
  8601. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  8602. script_pushint(st,amount);
  8603. return 0;
  8604. }
  8605. /*==========================================
  8606. *------------------------------------------*/
  8607. BUILDIN_FUNC(enablenpc)
  8608. {
  8609. const char *str;
  8610. str=script_getstr(st,2);
  8611. npc_enable(str,1);
  8612. return 0;
  8613. }
  8614. /*==========================================
  8615. *------------------------------------------*/
  8616. BUILDIN_FUNC(disablenpc)
  8617. {
  8618. const char *str;
  8619. str=script_getstr(st,2);
  8620. npc_enable(str,0);
  8621. return 0;
  8622. }
  8623. /*==========================================
  8624. *------------------------------------------*/
  8625. BUILDIN_FUNC(hideoffnpc)
  8626. {
  8627. const char *str;
  8628. str=script_getstr(st,2);
  8629. npc_enable(str,2);
  8630. return 0;
  8631. }
  8632. /*==========================================
  8633. *------------------------------------------*/
  8634. BUILDIN_FUNC(hideonnpc)
  8635. {
  8636. const char *str;
  8637. str=script_getstr(st,2);
  8638. npc_enable(str,4);
  8639. return 0;
  8640. }
  8641. /* Starts a status effect on the target unit or on the attached player.
  8642. *
  8643. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  8644. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  8645. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  8646. * <flag>
  8647. * &1: Cannot be avoided (it has to start)
  8648. * &2: Tick should not be reduced (by vit, luk, lv, etc)
  8649. * &4: sc_data loaded, no value has to be altered.
  8650. * &8: rate should not be reduced
  8651. */
  8652. BUILDIN_FUNC(sc_start)
  8653. {
  8654. TBL_NPC * nd = map_id2nd(st->oid);
  8655. struct block_list* bl;
  8656. enum sc_type type;
  8657. int tick, val1, val2, val3, val4=0, rate, flag;
  8658. char start_type;
  8659. const char* command = script_getfuncname(st);
  8660. if(strstr(command, "4"))
  8661. start_type = 4;
  8662. else if(strstr(command, "2"))
  8663. start_type = 2;
  8664. else
  8665. start_type = 1;
  8666. type = (sc_type)script_getnum(st,2);
  8667. tick = script_getnum(st,3);
  8668. val1 = script_getnum(st,4);
  8669. //If from NPC we make default flag 1 to be unavoidable
  8670. if(nd && nd->bl.id == fake_nd->bl.id)
  8671. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):2;
  8672. else
  8673. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):1;
  8674. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  8675. if(script_hasdata(st,(6+start_type)))
  8676. bl = map_id2bl(script_getnum(st,(6+start_type)));
  8677. else
  8678. bl = map_id2bl(st->rid);
  8679. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  8680. {// When there isn't a duration specified, try to get it from the skill_db
  8681. tick = skill_get_time(status_sc2skill(type), val1);
  8682. }
  8683. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  8684. bl = map_id2bl(potion_target);
  8685. tick /= 2;// Thrown potions only last half.
  8686. val4 = 1;// Mark that this was a thrown sc_effect
  8687. }
  8688. if(!bl)
  8689. return 0;
  8690. switch(start_type) {
  8691. case 1:
  8692. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  8693. break;
  8694. case 2:
  8695. val2 = script_getnum(st,5);
  8696. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  8697. break;
  8698. case 4:
  8699. val2 = script_getnum(st,5);
  8700. val3 = script_getnum(st,6);
  8701. val4 = script_getnum(st,7);
  8702. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  8703. break;
  8704. }
  8705. return 0;
  8706. }
  8707. /// Ends one or all status effects on the target unit or on the attached player.
  8708. ///
  8709. /// sc_end <effect_id>{,<unit_id>};
  8710. BUILDIN_FUNC(sc_end)
  8711. {
  8712. struct block_list* bl;
  8713. int type;
  8714. type = script_getnum(st, 2);
  8715. if (script_hasdata(st, 3))
  8716. bl = map_id2bl(script_getnum(st, 3));
  8717. else
  8718. bl = map_id2bl(st->rid);
  8719. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  8720. bl = map_id2bl(potion_target);
  8721. if (!bl)
  8722. return 0;
  8723. if (type >= 0 && type < SC_MAX)
  8724. {
  8725. struct status_change *sc = status_get_sc(bl);
  8726. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  8727. if (!sce)
  8728. return 0;
  8729. switch (type)
  8730. {
  8731. case SC_WEIGHT50:
  8732. case SC_WEIGHT90:
  8733. case SC_NOCHAT:
  8734. case SC_PUSH_CART:
  8735. return 0;
  8736. default:
  8737. break;
  8738. }
  8739. //This should help status_change_end force disabling the SC in case it has no limit.
  8740. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8741. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8742. }
  8743. else
  8744. status_change_clear(bl, 3); // remove all effects
  8745. return 0;
  8746. }
  8747. /*==========================================
  8748. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  8749. *------------------------------------------*/
  8750. BUILDIN_FUNC(getscrate)
  8751. {
  8752. struct block_list *bl;
  8753. int type,rate;
  8754. type=script_getnum(st,2);
  8755. rate=script_getnum(st,3);
  8756. if( script_hasdata(st,4) ) //get for the bl assigned
  8757. bl = map_id2bl(script_getnum(st,4));
  8758. else
  8759. bl = map_id2bl(st->rid);
  8760. if (bl)
  8761. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, 0);
  8762. script_pushint(st,rate);
  8763. return 0;
  8764. }
  8765. /*==========================================
  8766. * getstatus <type>{, <info>};
  8767. *------------------------------------------*/
  8768. BUILDIN_FUNC(getstatus)
  8769. {
  8770. int id, type;
  8771. struct map_session_data* sd = script_rid2sd(st);
  8772. if( sd == NULL )
  8773. {// no player attached
  8774. return 0;
  8775. }
  8776. id = script_getnum(st, 2);
  8777. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  8778. if( id <= SC_NONE || id >= SC_MAX )
  8779. {// invalid status type given
  8780. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  8781. return 0;
  8782. }
  8783. if( sd->sc.count == 0 || !sd->sc.data[id] )
  8784. {// no status is active
  8785. script_pushint(st, 0);
  8786. return 0;
  8787. }
  8788. switch( type )
  8789. {
  8790. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  8791. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  8792. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  8793. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  8794. case 5:
  8795. {
  8796. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  8797. if( timer )
  8798. {// return the amount of time remaining
  8799. script_pushint(st, timer->tick - gettick());
  8800. }
  8801. }
  8802. break;
  8803. default: script_pushint(st, 1); break;
  8804. }
  8805. return 0;
  8806. }
  8807. /*==========================================
  8808. *
  8809. *------------------------------------------*/
  8810. BUILDIN_FUNC(debugmes)
  8811. {
  8812. const char *str;
  8813. str=script_getstr(st,2);
  8814. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8815. return 0;
  8816. }
  8817. /*==========================================
  8818. *------------------------------------------*/
  8819. BUILDIN_FUNC(catchpet)
  8820. {
  8821. int pet_id;
  8822. TBL_PC *sd;
  8823. pet_id= script_getnum(st,2);
  8824. sd=script_rid2sd(st);
  8825. if( sd == NULL )
  8826. return 0;
  8827. pet_catch_process1(sd,pet_id);
  8828. return 0;
  8829. }
  8830. /*==========================================
  8831. * [orn]
  8832. *------------------------------------------*/
  8833. BUILDIN_FUNC(homunculus_evolution)
  8834. {
  8835. TBL_PC *sd;
  8836. sd=script_rid2sd(st);
  8837. if( sd == NULL )
  8838. return 0;
  8839. if(merc_is_hom_active(sd->hd))
  8840. {
  8841. if (sd->hd->homunculus.intimacy > 91000)
  8842. merc_hom_evolution(sd->hd);
  8843. else
  8844. clif_emotion(&sd->hd->bl, E_SWT);
  8845. }
  8846. return 0;
  8847. }
  8848. /*==========================================
  8849. * Checks for vaporized morph state
  8850. * and deletes ITEMID_STRANGE_EMBRYO.
  8851. *------------------------------------------*/
  8852. BUILDIN_FUNC(homunculus_mutate)
  8853. {
  8854. int homun_id, m_class, m_id, i;
  8855. TBL_PC *sd;
  8856. sd = script_rid2sd(st);
  8857. if( sd == NULL || sd->hd == NULL )
  8858. return 0;
  8859. if(script_hasdata(st,2))
  8860. homun_id = script_getnum(st,2);
  8861. else
  8862. homun_id = 6048 + (rnd() % 4);
  8863. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  8864. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8865. m_id = hom_class2mapid(homun_id);
  8866. i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  8867. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  8868. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  8869. merc_call_homunculus(sd); // Respawn homunculus.
  8870. hom_mutate(sd->hd, homun_id);
  8871. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  8872. script_pushint(st, 1);
  8873. return 0;
  8874. } else
  8875. clif_emotion(&sd->bl, E_SWT);
  8876. } else
  8877. clif_emotion(&sd->bl, E_SWT);
  8878. script_pushint(st, 0);
  8879. return 0;
  8880. }
  8881. /*==========================================
  8882. * Puts homunculus into morph state
  8883. * and gives ITEMID_STRANGE_EMBRYO.
  8884. *------------------------------------------*/
  8885. BUILDIN_FUNC(morphembryo)
  8886. {
  8887. struct item item_tmp;
  8888. int m_class, i=0;
  8889. TBL_PC *sd;
  8890. sd = script_rid2sd(st);
  8891. if( sd == NULL || sd->hd == NULL )
  8892. return 0;
  8893. if( merc_is_hom_active(sd->hd) ) {
  8894. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8895. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  8896. memset(&item_tmp, 0, sizeof(item_tmp));
  8897. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  8898. item_tmp.identify = 1;
  8899. if( item_tmp.nameid == 0 || (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  8900. clif_additem(sd, 0, 0, i);
  8901. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  8902. } else {
  8903. merc_hom_vaporize(sd, HOM_ST_MORPH);
  8904. script_pushint(st, 1);
  8905. return 0;
  8906. }
  8907. } else
  8908. clif_emotion(&sd->hd->bl, E_SWT);
  8909. } else
  8910. clif_emotion(&sd->bl, E_SWT);
  8911. script_pushint(st, 0);
  8912. return 0;
  8913. }
  8914. // [Zephyrus]
  8915. BUILDIN_FUNC(homunculus_shuffle)
  8916. {
  8917. TBL_PC *sd;
  8918. sd=script_rid2sd(st);
  8919. if( sd == NULL )
  8920. return 0;
  8921. if(merc_is_hom_active(sd->hd))
  8922. merc_hom_shuffle(sd->hd);
  8923. return 0;
  8924. }
  8925. /*==========================================
  8926. * Check for homunculus state.
  8927. * Return: -1 = No homunculus
  8928. * 0 = Homunculus is active
  8929. * 1 = Homunculus is vaporized (rest)
  8930. * 2 = Homunculus is in morph state
  8931. *------------------------------------------*/
  8932. BUILDIN_FUNC(checkhomcall)
  8933. {
  8934. TBL_PC *sd = script_rid2sd(st);
  8935. TBL_HOM *hd;
  8936. if( sd == NULL )
  8937. return 0;
  8938. hd = sd->hd;
  8939. if( !hd )
  8940. script_pushint(st, -1);
  8941. else
  8942. script_pushint(st, hd->homunculus.vaporize);
  8943. return 0;
  8944. }
  8945. //These two functions bring the eA MAPID_* class functionality to scripts.
  8946. BUILDIN_FUNC(eaclass)
  8947. {
  8948. int class_;
  8949. if( script_hasdata(st,2) )
  8950. class_ = script_getnum(st,2);
  8951. else {
  8952. TBL_PC *sd;
  8953. sd=script_rid2sd(st);
  8954. if (!sd) {
  8955. script_pushint(st,-1);
  8956. return 0;
  8957. }
  8958. class_ = sd->status.class_;
  8959. }
  8960. script_pushint(st,pc_jobid2mapid(class_));
  8961. return 0;
  8962. }
  8963. BUILDIN_FUNC(roclass)
  8964. {
  8965. int class_ =script_getnum(st,2);
  8966. int sex;
  8967. if( script_hasdata(st,3) )
  8968. sex = script_getnum(st,3);
  8969. else {
  8970. TBL_PC *sd;
  8971. if (st->rid && (sd=script_rid2sd(st)))
  8972. sex = sd->status.sex;
  8973. else
  8974. sex = 1; //Just use male when not found.
  8975. }
  8976. script_pushint(st,pc_mapid2jobid(class_, sex));
  8977. return 0;
  8978. }
  8979. /*==========================================
  8980. * Tells client to open a hatching window, used for pet incubator
  8981. *------------------------------------------*/
  8982. BUILDIN_FUNC(birthpet)
  8983. {
  8984. TBL_PC *sd;
  8985. sd=script_rid2sd(st);
  8986. if( sd == NULL )
  8987. return 0;
  8988. if( sd->status.pet_id )
  8989. {// do not send egg list, when you already have a pet
  8990. return 0;
  8991. }
  8992. clif_sendegg(sd);
  8993. return 0;
  8994. }
  8995. /*==========================================
  8996. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  8997. * @type
  8998. * 1 : make like after rebirth
  8999. * 2 : blvl,jlvl=1, skillpoint=0
  9000. * 3 : don't reset skill, blvl=1
  9001. * 4 : jlvl=0
  9002. *------------------------------------------*/
  9003. BUILDIN_FUNC(resetlvl)
  9004. {
  9005. TBL_PC *sd;
  9006. int type=script_getnum(st,2);
  9007. sd=script_rid2sd(st);
  9008. if( sd == NULL )
  9009. return 0;
  9010. pc_resetlvl(sd,type);
  9011. return 0;
  9012. }
  9013. /*==========================================
  9014. * Reset a player status point
  9015. *------------------------------------------*/
  9016. BUILDIN_FUNC(resetstatus)
  9017. {
  9018. TBL_PC *sd;
  9019. sd=script_rid2sd(st);
  9020. pc_resetstate(sd);
  9021. return 0;
  9022. }
  9023. /*==========================================
  9024. * script command resetskill
  9025. *------------------------------------------*/
  9026. BUILDIN_FUNC(resetskill)
  9027. {
  9028. TBL_PC *sd;
  9029. sd=script_rid2sd(st);
  9030. pc_resetskill(sd,1);
  9031. return 0;
  9032. }
  9033. /*==========================================
  9034. * Counts total amount of skill points.
  9035. *------------------------------------------*/
  9036. BUILDIN_FUNC(skillpointcount)
  9037. {
  9038. TBL_PC *sd;
  9039. sd=script_rid2sd(st);
  9040. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9041. return 0;
  9042. }
  9043. /*==========================================
  9044. *
  9045. *------------------------------------------*/
  9046. BUILDIN_FUNC(changebase)
  9047. {
  9048. TBL_PC *sd=NULL;
  9049. int vclass;
  9050. if( script_hasdata(st,3) )
  9051. sd=map_id2sd(script_getnum(st,3));
  9052. else
  9053. sd=script_rid2sd(st);
  9054. if(sd == NULL)
  9055. return 0;
  9056. vclass = script_getnum(st,2);
  9057. if(vclass == JOB_WEDDING)
  9058. {
  9059. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9060. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9061. )
  9062. return 0;
  9063. }
  9064. if(!sd->disguise && vclass != sd->vd.class_) {
  9065. status_set_viewdata(&sd->bl, vclass);
  9066. //Updated client view. Base, Weapon and Cloth Colors.
  9067. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9068. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9069. if (sd->vd.cloth_color)
  9070. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9071. clif_skillinfoblock(sd);
  9072. }
  9073. return 0;
  9074. }
  9075. /*==========================================
  9076. * Unequip all item and request for a changesex to char-serv
  9077. *------------------------------------------*/
  9078. BUILDIN_FUNC(changesex)
  9079. {
  9080. int i;
  9081. TBL_PC *sd = NULL;
  9082. sd = script_rid2sd(st);
  9083. pc_resetskill(sd,4);
  9084. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9085. for( i=0; i<EQI_MAX; i++ )
  9086. if( sd->equip_index[i] >= 0 ) pc_unequipitem(sd, sd->equip_index[i], 3);
  9087. chrif_changesex(sd);
  9088. return 0;
  9089. }
  9090. /*==========================================
  9091. * Works like 'announce' but outputs in the common chat window
  9092. *------------------------------------------*/
  9093. BUILDIN_FUNC(globalmes)
  9094. {
  9095. struct block_list *bl = map_id2bl(st->oid);
  9096. struct npc_data *nd = (struct npc_data *)bl;
  9097. const char *name=NULL,*mes;
  9098. mes=script_getstr(st,2);
  9099. if(mes==NULL) return 0;
  9100. if(script_hasdata(st,3)){ // npc name to display
  9101. name=script_getstr(st,3);
  9102. } else {
  9103. name=nd->name; //use current npc name
  9104. }
  9105. npc_globalmessage(name,mes); // broadcast to all players connected
  9106. return 0;
  9107. }
  9108. /////////////////////////////////////////////////////////////////////
  9109. // NPC waiting room (chat room)
  9110. //
  9111. /// Creates a waiting room (chat room) for this npc.
  9112. ///
  9113. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9114. BUILDIN_FUNC(waitingroom)
  9115. {
  9116. struct npc_data* nd;
  9117. int pub = 1;
  9118. const char* title = script_getstr(st, 2);
  9119. int limit = script_getnum(st, 3);
  9120. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9121. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9122. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9123. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9124. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9125. nd = (struct npc_data *)map_id2bl(st->oid);
  9126. if( nd != NULL )
  9127. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  9128. return 0;
  9129. }
  9130. /// Removes the waiting room of the current or target npc.
  9131. ///
  9132. /// delwaitingroom "<npc_name>";
  9133. /// delwaitingroom;
  9134. BUILDIN_FUNC(delwaitingroom)
  9135. {
  9136. struct npc_data* nd;
  9137. if( script_hasdata(st,2) )
  9138. nd = npc_name2id(script_getstr(st, 2));
  9139. else
  9140. nd = (struct npc_data *)map_id2bl(st->oid);
  9141. if( nd != NULL )
  9142. chat_deletenpcchat(nd);
  9143. return 0;
  9144. }
  9145. /// Kicks all the players from the waiting room of the current or target npc.
  9146. ///
  9147. /// kickwaitingroomall "<npc_name>";
  9148. /// kickwaitingroomall;
  9149. BUILDIN_FUNC(waitingroomkickall)
  9150. {
  9151. struct npc_data* nd;
  9152. struct chat_data* cd;
  9153. if( script_hasdata(st,2) )
  9154. nd = npc_name2id(script_getstr(st,2));
  9155. else
  9156. nd = (struct npc_data *)map_id2bl(st->oid);
  9157. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9158. chat_npckickall(cd);
  9159. return 0;
  9160. }
  9161. /// Enables the waiting room event of the current or target npc.
  9162. ///
  9163. /// enablewaitingroomevent "<npc_name>";
  9164. /// enablewaitingroomevent;
  9165. BUILDIN_FUNC(enablewaitingroomevent)
  9166. {
  9167. struct npc_data* nd;
  9168. struct chat_data* cd;
  9169. if( script_hasdata(st,2) )
  9170. nd = npc_name2id(script_getstr(st, 2));
  9171. else
  9172. nd = (struct npc_data *)map_id2bl(st->oid);
  9173. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9174. chat_enableevent(cd);
  9175. return 0;
  9176. }
  9177. /// Disables the waiting room event of the current or target npc.
  9178. ///
  9179. /// disablewaitingroomevent "<npc_name>";
  9180. /// disablewaitingroomevent;
  9181. BUILDIN_FUNC(disablewaitingroomevent)
  9182. {
  9183. struct npc_data *nd;
  9184. struct chat_data *cd;
  9185. if( script_hasdata(st,2) )
  9186. nd = npc_name2id(script_getstr(st, 2));
  9187. else
  9188. nd = (struct npc_data *)map_id2bl(st->oid);
  9189. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9190. chat_disableevent(cd);
  9191. return 0;
  9192. }
  9193. /// Returns info on the waiting room of the current or target npc.
  9194. /// Returns -1 if the type unknown
  9195. /// <type>=0 : current number of users
  9196. /// <type>=1 : maximum number of users allowed
  9197. /// <type>=2 : the number of users that trigger the event
  9198. /// <type>=3 : if the trigger is disabled
  9199. /// <type>=4 : the title of the waiting room
  9200. /// <type>=5 : the password of the waiting room
  9201. /// <type>=16 : the name of the waiting room event
  9202. /// <type>=32 : if the waiting room is full
  9203. /// <type>=33 : if there are enough users to trigger the event
  9204. ///
  9205. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9206. /// getwaitingroomstate(<type>) -> <info>
  9207. BUILDIN_FUNC(getwaitingroomstate)
  9208. {
  9209. struct npc_data *nd;
  9210. struct chat_data *cd;
  9211. int type;
  9212. type = script_getnum(st,2);
  9213. if( script_hasdata(st,3) )
  9214. nd = npc_name2id(script_getstr(st, 3));
  9215. else
  9216. nd = (struct npc_data *)map_id2bl(st->oid);
  9217. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9218. {
  9219. script_pushint(st, -1);
  9220. return 0;
  9221. }
  9222. switch(type)
  9223. {
  9224. case 0: script_pushint(st, cd->users); break;
  9225. case 1: script_pushint(st, cd->limit); break;
  9226. case 2: script_pushint(st, cd->trigger&0x7f); break;
  9227. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  9228. case 4: script_pushstrcopy(st, cd->title); break;
  9229. case 5: script_pushstrcopy(st, cd->pass); break;
  9230. case 16: script_pushstrcopy(st, cd->npc_event);break;
  9231. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  9232. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  9233. default: script_pushint(st, -1); break;
  9234. }
  9235. return 0;
  9236. }
  9237. /// Warps the trigger or target amount of players to the target map and position.
  9238. /// Players are automatically removed from the waiting room.
  9239. /// Those waiting the longest will get warped first.
  9240. /// The target map can be "Random" for a random position in the current map,
  9241. /// and "SavePoint" for the savepoint map+position.
  9242. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  9243. ///
  9244. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  9245. /// The total number of teleported players is put into $@warpwaitingpcnum
  9246. ///
  9247. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  9248. /// warpwaitingpc "<map name>",<x>,<y>;
  9249. BUILDIN_FUNC(warpwaitingpc)
  9250. {
  9251. int x;
  9252. int y;
  9253. int i;
  9254. int n;
  9255. const char* map_name;
  9256. struct npc_data* nd;
  9257. struct chat_data* cd;
  9258. TBL_PC* sd;
  9259. nd = (struct npc_data *)map_id2bl(st->oid);
  9260. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9261. return 0;
  9262. map_name = script_getstr(st,2);
  9263. x = script_getnum(st,3);
  9264. y = script_getnum(st,4);
  9265. n = cd->trigger&0x7f;
  9266. if( script_hasdata(st,5) )
  9267. n = script_getnum(st,5);
  9268. for( i = 0; i < n && cd->users > 0; i++ )
  9269. {
  9270. sd = cd->usersd[0];
  9271. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  9272. {// can't teleport on this map
  9273. break;
  9274. }
  9275. if( cd->zeny )
  9276. {// fee set
  9277. if( (uint32)sd->status.zeny < cd->zeny )
  9278. {// no zeny to cover set fee
  9279. break;
  9280. }
  9281. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  9282. }
  9283. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  9284. if( strcmp(map_name,"Random") == 0 )
  9285. pc_randomwarp(sd,CLR_TELEPORT);
  9286. else if( strcmp(map_name,"SavePoint") == 0 )
  9287. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  9288. else
  9289. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  9290. }
  9291. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  9292. return 0;
  9293. }
  9294. /////////////////////////////////////////////////////////////////////
  9295. // ...
  9296. //
  9297. /// Detaches a character from a script.
  9298. ///
  9299. /// @param st Script state to detach the character from.
  9300. static void script_detach_rid(struct script_state* st)
  9301. {
  9302. if(st->rid)
  9303. {
  9304. script_detach_state(st, false);
  9305. st->rid = 0;
  9306. }
  9307. }
  9308. /*=========================================================================
  9309. * Attaches a set of RIDs to the current script. [digitalhamster]
  9310. * addrid(<type>{,<flag>{,<parameters>}});
  9311. * <type>:
  9312. * 0 : All players in the server.
  9313. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  9314. * 2 : Party members of a specified party ID.
  9315. * [ Parameters: <party id> ]
  9316. * 3 : Guild members of a specified guild ID.
  9317. * [ Parameters: <guild id> ]
  9318. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  9319. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  9320. * Account ID: The specified account ID.
  9321. * <flag>:
  9322. * 0 : Players are always attached. (default)
  9323. * 1 : Players currently running another script will not be attached.
  9324. *-------------------------------------------------------------------------*/
  9325. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  9326. {
  9327. int forceflag;
  9328. struct map_session_data *sd = (TBL_PC *)bl;
  9329. struct script_state* st;
  9330. st=va_arg(ap,struct script_state*);
  9331. forceflag=va_arg(ap,int);
  9332. if(!forceflag||!sd->st)
  9333. if(sd->status.account_id!=st->rid)
  9334. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9335. return 0;
  9336. }
  9337. BUILDIN_FUNC(addrid)
  9338. {
  9339. struct s_mapiterator* iter;
  9340. struct block_list *bl;
  9341. TBL_PC *sd;
  9342. if(st->rid<1){
  9343. st->state = END;
  9344. bl=map_id2bl(st->oid);
  9345. } else
  9346. bl=map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  9347. iter = mapit_getallusers();
  9348. switch(script_getnum(st,2)){
  9349. case 0:
  9350. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9351. if(!script_getnum(st,3)||!sd->st)
  9352. if(sd->status.account_id!=st->rid) //attached player already runs.
  9353. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9354. }
  9355. break;
  9356. case 1:
  9357. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9358. if(!script_getnum(st,3)||!sd->st)
  9359. if((sd->bl.m == bl->m)&&(sd->status.account_id!=st->rid))
  9360. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9361. }
  9362. break;
  9363. case 2:
  9364. if(script_getnum(st,4)==0){
  9365. script_pushint(st,0);
  9366. return 0;
  9367. }
  9368. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9369. if(!script_getnum(st,3)||!sd->st)
  9370. if((sd->status.account_id!=st->rid)&&(sd->status.party_id==script_getnum(st,4))) //attached player already runs.
  9371. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9372. }
  9373. break;
  9374. case 3:
  9375. if(script_getnum(st,4)==0){
  9376. script_pushint(st,0);
  9377. return 0;
  9378. }
  9379. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9380. if(!script_getnum(st,3)||!sd->st)
  9381. if((sd->status.account_id!=st->rid)&&(sd->status.guild_id==script_getnum(st,4))) //attached player already runs.
  9382. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9383. }
  9384. break;
  9385. case 4:
  9386. map_foreachinarea(buildin_addrid_sub,
  9387. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  9388. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  9389. break;
  9390. default:
  9391. if((map_id2sd(script_getnum(st,2)))==NULL){ // Player not found.
  9392. script_pushint(st,0);
  9393. return 0;
  9394. }
  9395. if(!script_getnum(st,3)||!map_id2sd(script_getnum(st,2))->st) {
  9396. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  9397. script_pushint(st,1);
  9398. }
  9399. return 0;
  9400. }
  9401. mapit_free(iter);
  9402. script_pushint(st,1);
  9403. return 0;
  9404. }
  9405. /*==========================================
  9406. * Attach sd char id to script and detach current one if any
  9407. *------------------------------------------*/
  9408. BUILDIN_FUNC(attachrid)
  9409. {
  9410. int rid = script_getnum(st,2);
  9411. if (map_id2sd(rid) != NULL) {
  9412. script_detach_rid(st);
  9413. st->rid = rid;
  9414. script_attach_state(st);
  9415. script_pushint(st,1);
  9416. } else
  9417. script_pushint(st,0);
  9418. return 0;
  9419. }
  9420. /*==========================================
  9421. * Detach script to rid
  9422. *------------------------------------------*/
  9423. BUILDIN_FUNC(detachrid)
  9424. {
  9425. script_detach_rid(st);
  9426. return 0;
  9427. }
  9428. /*==========================================
  9429. * Chk if account connected, (and charid from account if specified)
  9430. *------------------------------------------*/
  9431. BUILDIN_FUNC(isloggedin)
  9432. {
  9433. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  9434. if (script_hasdata(st,3) && sd &&
  9435. sd->status.char_id != script_getnum(st,3))
  9436. sd = NULL;
  9437. push_val(st->stack,C_INT,sd!=NULL);
  9438. return 0;
  9439. }
  9440. /*==========================================
  9441. *
  9442. *------------------------------------------*/
  9443. BUILDIN_FUNC(setmapflagnosave)
  9444. {
  9445. int16 m,x,y;
  9446. unsigned short mapindex;
  9447. const char *str,*str2;
  9448. str=script_getstr(st,2);
  9449. str2=script_getstr(st,3);
  9450. x=script_getnum(st,4);
  9451. y=script_getnum(st,5);
  9452. m = map_mapname2mapid(str);
  9453. mapindex = mapindex_name2id(str2);
  9454. if(m >= 0 && mapindex) {
  9455. map[m].flag.nosave=1;
  9456. map[m].save.map=mapindex;
  9457. map[m].save.x=x;
  9458. map[m].save.y=y;
  9459. }
  9460. return 0;
  9461. }
  9462. BUILDIN_FUNC(getmapflag)
  9463. {
  9464. int16 m,i,type=0;
  9465. const char *str;
  9466. str=script_getstr(st,2);
  9467. i=script_getnum(st,3);
  9468. FETCH(4,type);
  9469. m = map_mapname2mapid(str);
  9470. if(m >= 0) {
  9471. switch(i) {
  9472. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  9473. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  9474. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  9475. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  9476. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  9477. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  9478. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  9479. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  9480. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  9481. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  9482. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  9483. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  9484. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  9485. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  9486. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  9487. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  9488. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  9489. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  9490. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  9491. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  9492. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  9493. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  9494. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  9495. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  9496. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  9497. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  9498. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  9499. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  9500. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  9501. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  9502. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  9503. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  9504. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  9505. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  9506. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  9507. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  9508. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  9509. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  9510. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  9511. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  9512. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  9513. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  9514. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  9515. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  9516. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  9517. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  9518. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  9519. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  9520. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  9521. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  9522. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  9523. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  9524. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  9525. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  9526. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  9527. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  9528. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  9529. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  9530. #ifdef ADJUST_SKILL_DAMAGE
  9531. case MF_SKILL_DAMAGE:
  9532. {
  9533. int ret_val = 0;
  9534. switch (type) {
  9535. case 1: ret_val = map[m].adjust.damage.pc; break;
  9536. case 2: ret_val = map[m].adjust.damage.mob; break;
  9537. case 3: ret_val = map[m].adjust.damage.boss; break;
  9538. case 4: ret_val = map[m].adjust.damage.other; break;
  9539. case 5: ret_val = map[m].adjust.damage.caster; break;
  9540. default: ret_val = map[m].flag.skill_damage; break;
  9541. }
  9542. script_pushint(st,ret_val); break;
  9543. } break;
  9544. #endif
  9545. }
  9546. }
  9547. return 0;
  9548. }
  9549. /* pvp timer handling */
  9550. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  9551. TBL_PC* sd = (TBL_PC*)bl;
  9552. if (sd->pvp_timer == INVALID_TIMER) {
  9553. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  9554. sd->pvp_rank = 0;
  9555. sd->pvp_lastusers = 0;
  9556. sd->pvp_point = 5;
  9557. sd->pvp_won = 0;
  9558. sd->pvp_lost = 0;
  9559. }
  9560. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  9561. clif_maptypeproperty2(&sd->bl,SELF);
  9562. return 0;
  9563. }
  9564. BUILDIN_FUNC(setmapflag)
  9565. {
  9566. int16 m,i,type=0;
  9567. const char *str;
  9568. int val=0;
  9569. str=script_getstr(st,2);
  9570. i=script_getnum(st,3);
  9571. FETCH(4,val);
  9572. FETCH(5,type);
  9573. m = map_mapname2mapid(str);
  9574. if(m >= 0) {
  9575. switch(i) {
  9576. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  9577. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  9578. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  9579. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  9580. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  9581. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  9582. case MF_PVP:
  9583. map[m].flag.pvp = 1;
  9584. if( !battle_config.pk_mode ) {
  9585. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  9586. }
  9587. break;
  9588. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  9589. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  9590. case MF_GVG: {
  9591. struct block_list bl;
  9592. map[m].flag.gvg = 1;
  9593. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9594. bl.type = BL_NUL;
  9595. bl.m = m;
  9596. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9597. }
  9598. break;
  9599. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  9600. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  9601. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  9602. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  9603. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  9604. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  9605. case MF_SNOW: map[m].flag.snow = 1; break;
  9606. case MF_FOG: map[m].flag.fog = 1; break;
  9607. case MF_SAKURA: map[m].flag.sakura = 1; break;
  9608. case MF_LEAVES: map[m].flag.leaves = 1; break;
  9609. case MF_NOGO: map[m].flag.nogo = 1; break;
  9610. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  9611. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  9612. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  9613. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  9614. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  9615. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  9616. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  9617. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  9618. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  9619. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  9620. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  9621. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  9622. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  9623. case MF_RESTRICTED:
  9624. map[m].zone |= 1<<(val+1);
  9625. map[m].flag.restricted=1;
  9626. break;
  9627. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  9628. case MF_NODROP: map[m].flag.nodrop = 1; break;
  9629. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  9630. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  9631. case MF_NOVENDING: map[m].flag.novending = 1; break;
  9632. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  9633. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  9634. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  9635. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  9636. case MF_TOWN: map[m].flag.town = 1; break;
  9637. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  9638. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  9639. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  9640. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  9641. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  9642. case MF_RESET: map[m].flag.reset = 1; break;
  9643. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  9644. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  9645. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  9646. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  9647. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  9648. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  9649. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  9650. #ifdef ADJUST_SKILL_DAMAGE
  9651. case MF_SKILL_DAMAGE:
  9652. {
  9653. switch (type) {
  9654. case 1: map[m].adjust.damage.pc = val; break;
  9655. case 2: map[m].adjust.damage.mob = val; break;
  9656. case 3: map[m].adjust.damage.boss = val; break;
  9657. case 4: map[m].adjust.damage.other = val; break;
  9658. case 5: map[m].adjust.damage.caster = val; break;
  9659. }
  9660. map[m].flag.skill_damage = 1;
  9661. } break;
  9662. #endif
  9663. }
  9664. }
  9665. return 0;
  9666. }
  9667. BUILDIN_FUNC(removemapflag)
  9668. {
  9669. int16 m,i;
  9670. const char *str;
  9671. int val=0;
  9672. str=script_getstr(st,2);
  9673. i=script_getnum(st,3);
  9674. FETCH(4,val);
  9675. m = map_mapname2mapid(str);
  9676. if(m >= 0) {
  9677. switch(i) {
  9678. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  9679. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  9680. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  9681. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  9682. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  9683. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  9684. case MF_PVP: {
  9685. struct block_list bl;
  9686. bl.type = BL_NUL;
  9687. bl.m = m;
  9688. map[m].flag.pvp = 0;
  9689. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9690. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9691. }
  9692. break;
  9693. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  9694. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  9695. case MF_GVG: {
  9696. struct block_list bl;
  9697. bl.type = BL_NUL;
  9698. bl.m = m;
  9699. map[m].flag.gvg = 0;
  9700. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9701. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9702. }
  9703. break;
  9704. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  9705. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  9706. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  9707. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  9708. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  9709. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  9710. case MF_SNOW: map[m].flag.snow = 0; break;
  9711. case MF_FOG: map[m].flag.fog = 0; break;
  9712. case MF_SAKURA: map[m].flag.sakura = 0; break;
  9713. case MF_LEAVES: map[m].flag.leaves = 0; break;
  9714. case MF_NOGO: map[m].flag.nogo = 0; break;
  9715. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  9716. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  9717. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  9718. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  9719. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  9720. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  9721. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  9722. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  9723. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  9724. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  9725. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  9726. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  9727. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  9728. case MF_RESTRICTED:
  9729. map[m].zone ^= 1<<(val+1);
  9730. if (map[m].zone == 0){
  9731. map[m].flag.restricted=0;
  9732. }
  9733. break;
  9734. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  9735. case MF_NODROP: map[m].flag.nodrop = 0; break;
  9736. case MF_JEXP: map[m].adjust.jexp = 0; break;
  9737. case MF_BEXP: map[m].adjust.bexp = 0; break;
  9738. case MF_NOVENDING: map[m].flag.novending = 0; break;
  9739. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  9740. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  9741. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  9742. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  9743. case MF_TOWN: map[m].flag.town = 0; break;
  9744. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  9745. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  9746. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  9747. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  9748. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  9749. case MF_RESET: map[m].flag.reset = 0; break;
  9750. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  9751. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  9752. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  9753. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  9754. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  9755. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  9756. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  9757. #ifdef ADJUST_SKILL_DAMAGE
  9758. case MF_SKILL_DAMAGE:
  9759. {
  9760. map[m].flag.skill_damage = 0;
  9761. memset(&map[m].adjust.damage,0,sizeof(map[m].adjust.damage));
  9762. } break;
  9763. #endif
  9764. }
  9765. }
  9766. return 0;
  9767. }
  9768. BUILDIN_FUNC(pvpon)
  9769. {
  9770. int16 m;
  9771. const char *str;
  9772. TBL_PC* sd = NULL;
  9773. struct s_mapiterator* iter;
  9774. struct block_list bl;
  9775. str = script_getstr(st,2);
  9776. m = map_mapname2mapid(str);
  9777. if( m < 0 || map[m].flag.pvp )
  9778. return 0; // nothing to do
  9779. map[m].flag.pvp = 1;
  9780. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  9781. bl.type = BL_NUL;
  9782. bl.m = m;
  9783. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9784. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  9785. return 0;
  9786. iter = mapit_getallusers();
  9787. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9788. {
  9789. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  9790. continue; // not applicable
  9791. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  9792. sd->pvp_rank = 0;
  9793. sd->pvp_lastusers = 0;
  9794. sd->pvp_point = 5;
  9795. sd->pvp_won = 0;
  9796. sd->pvp_lost = 0;
  9797. }
  9798. mapit_free(iter);
  9799. return 0;
  9800. }
  9801. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  9802. {
  9803. TBL_PC* sd = (TBL_PC*)bl;
  9804. clif_pvpset(sd, 0, 0, 2);
  9805. if (sd->pvp_timer != INVALID_TIMER) {
  9806. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  9807. sd->pvp_timer = INVALID_TIMER;
  9808. }
  9809. return 0;
  9810. }
  9811. BUILDIN_FUNC(pvpoff)
  9812. {
  9813. int16 m;
  9814. const char *str;
  9815. struct block_list bl;
  9816. str=script_getstr(st,2);
  9817. m = map_mapname2mapid(str);
  9818. if(m < 0 || !map[m].flag.pvp)
  9819. return 0; //fixed Lupus
  9820. map[m].flag.pvp = 0;
  9821. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9822. bl.type = BL_NUL;
  9823. bl.m = m;
  9824. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9825. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  9826. return 0;
  9827. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  9828. return 0;
  9829. }
  9830. BUILDIN_FUNC(gvgon)
  9831. {
  9832. int16 m;
  9833. const char *str;
  9834. struct block_list bl;
  9835. str=script_getstr(st,2);
  9836. m = map_mapname2mapid(str);
  9837. if(m >= 0 && !map[m].flag.gvg) {
  9838. map[m].flag.gvg = 1;
  9839. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9840. bl.type = BL_NUL;
  9841. bl.m = m;
  9842. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9843. }
  9844. return 0;
  9845. }
  9846. BUILDIN_FUNC(gvgoff)
  9847. {
  9848. int16 m;
  9849. const char *str;
  9850. str=script_getstr(st,2);
  9851. m = map_mapname2mapid(str);
  9852. if(m >= 0 && map[m].flag.gvg) {
  9853. struct block_list bl;
  9854. map[m].flag.gvg = 0;
  9855. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9856. bl.type = BL_NUL;
  9857. bl.m = m;
  9858. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9859. }
  9860. return 0;
  9861. }
  9862. /*==========================================
  9863. * Shows an emoticon on top of the player/npc
  9864. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  9865. *------------------------------------------*/
  9866. //Optional second parameter added by [Skotlex]
  9867. BUILDIN_FUNC(emotion)
  9868. {
  9869. int type;
  9870. int player=0;
  9871. type=script_getnum(st,2);
  9872. if(type < 0 || type > 100)
  9873. return 0;
  9874. if( script_hasdata(st,3) )
  9875. player=script_getnum(st,3);
  9876. if (player) {
  9877. TBL_PC *sd = NULL;
  9878. if( script_hasdata(st,4) )
  9879. sd = map_nick2sd(script_getstr(st,4));
  9880. else
  9881. sd = script_rid2sd(st);
  9882. if (sd)
  9883. clif_emotion(&sd->bl,type);
  9884. } else
  9885. if( script_hasdata(st,4) )
  9886. {
  9887. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  9888. if(nd)
  9889. clif_emotion(&nd->bl,type);
  9890. }
  9891. else
  9892. clif_emotion(map_id2bl(st->oid),type);
  9893. return 0;
  9894. }
  9895. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  9896. {
  9897. int16 m=va_arg(ap,int);
  9898. int g_id=va_arg(ap,int);
  9899. int flag=va_arg(ap,int);
  9900. if(!sd || sd->bl.m != m)
  9901. return 0;
  9902. if(
  9903. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  9904. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  9905. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  9906. )
  9907. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  9908. return 1;
  9909. }
  9910. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  9911. {
  9912. struct mob_data *md=(struct mob_data *)bl;
  9913. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  9914. status_kill(bl);
  9915. return 0;
  9916. }
  9917. /*
  9918. * Function to kick guild members out of a map and to their save points.
  9919. * m : mapid
  9920. * g_id : owner guild id
  9921. * flag & 1 : Warp out owners
  9922. * flag & 2 : Warp out outsiders
  9923. * flag & 4 : reserved for mobs
  9924. * */
  9925. BUILDIN_FUNC(maprespawnguildid)
  9926. {
  9927. const char *mapname=script_getstr(st,2);
  9928. int g_id=script_getnum(st,3);
  9929. int flag=script_getnum(st,4);
  9930. int16 m=map_mapname2mapid(mapname);
  9931. if(m == -1)
  9932. return 0;
  9933. //Catch ALL players (in case some are 'between maps' on execution time)
  9934. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  9935. if (flag&4) //Remove script mobs.
  9936. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  9937. return 0;
  9938. }
  9939. BUILDIN_FUNC(agitstart)
  9940. {
  9941. if(agit_flag==1) return 0; // Agit already Start.
  9942. agit_flag=1;
  9943. guild_agit_start();
  9944. return 0;
  9945. }
  9946. BUILDIN_FUNC(agitend)
  9947. {
  9948. if(agit_flag==0) return 0; // Agit already End.
  9949. agit_flag=0;
  9950. guild_agit_end();
  9951. return 0;
  9952. }
  9953. BUILDIN_FUNC(agitstart2)
  9954. {
  9955. if(agit2_flag==1) return 0; // Agit2 already Start.
  9956. agit2_flag=1;
  9957. guild_agit2_start();
  9958. return 0;
  9959. }
  9960. BUILDIN_FUNC(agitend2)
  9961. {
  9962. if(agit2_flag==0) return 0; // Agit2 already End.
  9963. agit2_flag=0;
  9964. guild_agit2_end();
  9965. return 0;
  9966. }
  9967. /*==========================================
  9968. * Returns whether woe is on or off. // choice script
  9969. *------------------------------------------*/
  9970. BUILDIN_FUNC(agitcheck)
  9971. {
  9972. script_pushint(st,agit_flag);
  9973. return 0;
  9974. }
  9975. /*==========================================
  9976. * Returns whether woese is on or off. // choice script
  9977. *------------------------------------------*/
  9978. BUILDIN_FUNC(agitcheck2)
  9979. {
  9980. script_pushint(st,agit2_flag);
  9981. return 0;
  9982. }
  9983. /// Sets the guild_id of this npc.
  9984. ///
  9985. /// flagemblem <guild_id>;
  9986. BUILDIN_FUNC(flagemblem)
  9987. {
  9988. TBL_NPC* nd;
  9989. int g_id = script_getnum(st,2);
  9990. if(g_id < 0) return 0;
  9991. nd = (TBL_NPC*)map_id2nd(st->oid);
  9992. if( nd == NULL ) {
  9993. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  9994. } else if( nd->subtype != SCRIPT ) {
  9995. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  9996. } else {
  9997. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  9998. nd->u.scr.guild_id = g_id;
  9999. clif_guild_emblem_area(&nd->bl);
  10000. /* guild flag caching */
  10001. if( g_id ) /* adding a id */
  10002. guild_flag_add(nd);
  10003. else if( changed ) /* removing a flag */
  10004. guild_flag_remove(nd);
  10005. }
  10006. return 0;
  10007. }
  10008. BUILDIN_FUNC(getcastlename)
  10009. {
  10010. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10011. struct guild_castle* gc = guild_mapname2gc(mapname);
  10012. const char* name = (gc) ? gc->castle_name : "";
  10013. script_pushstrcopy(st,name);
  10014. return 0;
  10015. }
  10016. BUILDIN_FUNC(getcastledata)
  10017. {
  10018. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10019. int index = script_getnum(st,3);
  10020. struct guild_castle *gc = guild_mapname2gc(mapname);
  10021. if (gc == NULL) {
  10022. script_pushint(st,0);
  10023. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10024. return 1;
  10025. }
  10026. switch (index) {
  10027. case 1:
  10028. script_pushint(st,gc->guild_id); break;
  10029. case 2:
  10030. script_pushint(st,gc->economy); break;
  10031. case 3:
  10032. script_pushint(st,gc->defense); break;
  10033. case 4:
  10034. script_pushint(st,gc->triggerE); break;
  10035. case 5:
  10036. script_pushint(st,gc->triggerD); break;
  10037. case 6:
  10038. script_pushint(st,gc->nextTime); break;
  10039. case 7:
  10040. script_pushint(st,gc->payTime); break;
  10041. case 8:
  10042. script_pushint(st,gc->createTime); break;
  10043. case 9:
  10044. script_pushint(st,gc->visibleC); break;
  10045. default:
  10046. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10047. script_pushint(st,gc->guardian[index-10].visible);
  10048. break;
  10049. }
  10050. script_pushint(st,0);
  10051. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10052. return 1;
  10053. }
  10054. return 0;
  10055. }
  10056. BUILDIN_FUNC(setcastledata)
  10057. {
  10058. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10059. int index = script_getnum(st,3);
  10060. int value = script_getnum(st,4);
  10061. struct guild_castle *gc = guild_mapname2gc(mapname);
  10062. if (gc == NULL) {
  10063. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10064. return 1;
  10065. }
  10066. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10067. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10068. return 1;
  10069. }
  10070. guild_castledatasave(gc->castle_id, index, value);
  10071. return 0;
  10072. }
  10073. /* =====================================================================
  10074. * ---------------------------------------------------------------------*/
  10075. BUILDIN_FUNC(requestguildinfo)
  10076. {
  10077. int guild_id=script_getnum(st,2);
  10078. const char *event=NULL;
  10079. if( script_hasdata(st,3) ){
  10080. event=script_getstr(st,3);
  10081. check_event(st, event);
  10082. }
  10083. if(guild_id>0)
  10084. guild_npc_request_info(guild_id,event);
  10085. return 0;
  10086. }
  10087. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10088. /// getequipcardcnt(<equipment slot>);
  10089. BUILDIN_FUNC(getequipcardcnt)
  10090. {
  10091. int i=-1,j,num;
  10092. TBL_PC *sd;
  10093. int count;
  10094. num=script_getnum(st,2);
  10095. sd=script_rid2sd(st);
  10096. if (num > 0 && num <= ARRAYLENGTH(equip))
  10097. i=pc_checkequip(sd,equip[num-1]);
  10098. if (i < 0 || !sd->inventory_data[i]) {
  10099. script_pushint(st,0);
  10100. return 0;
  10101. }
  10102. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10103. {
  10104. script_pushint(st,0);
  10105. return 0;
  10106. }
  10107. count = 0;
  10108. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10109. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  10110. count++;
  10111. script_pushint(st,count);
  10112. return 0;
  10113. }
  10114. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10115. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10116. /// successremovecards <slot>;
  10117. BUILDIN_FUNC(successremovecards) {
  10118. int i=-1,j,c,cardflag=0;
  10119. TBL_PC* sd = script_rid2sd(st);
  10120. int num = script_getnum(st,2);
  10121. if (num > 0 && num <= ARRAYLENGTH(equip))
  10122. i=pc_checkequip(sd,equip[num-1]);
  10123. if (i < 0 || !sd->inventory_data[i]) {
  10124. return 0;
  10125. }
  10126. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10127. return 0;
  10128. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10129. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10130. int flag;
  10131. struct item item_tmp;
  10132. memset(&item_tmp,0,sizeof(item_tmp));
  10133. cardflag = 1;
  10134. item_tmp.nameid = sd->status.inventory[i].card[c];
  10135. item_tmp.identify = 1;
  10136. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10137. clif_additem(sd,0,0,flag);
  10138. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10139. }
  10140. }
  10141. }
  10142. if(cardflag == 1) {//if card was remove remplace item with no card
  10143. int flag;
  10144. struct item item_tmp;
  10145. memset(&item_tmp,0,sizeof(item_tmp));
  10146. item_tmp.nameid = sd->status.inventory[i].nameid;
  10147. item_tmp.identify = 1;
  10148. item_tmp.refine = sd->status.inventory[i].refine;
  10149. item_tmp.attribute = sd->status.inventory[i].attribute;
  10150. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10151. item_tmp.bound = sd->status.inventory[i].bound;
  10152. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10153. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10154. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10155. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10156. clif_additem(sd,0,0,flag);
  10157. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10158. }
  10159. clif_misceffect(&sd->bl,3);
  10160. }
  10161. return 0;
  10162. }
  10163. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10164. /// failedremovecards <slot>, <type>;
  10165. /// <type>=0 : will destroy both the item and the cards.
  10166. /// <type>=1 : will keep the item, but destroy the cards.
  10167. /// <type>=2 : will keep the cards, but destroy the item.
  10168. /// <type>=? : will just display the failure effect.
  10169. BUILDIN_FUNC(failedremovecards) {
  10170. int i=-1,j,c,cardflag=0;
  10171. TBL_PC* sd = script_rid2sd(st);
  10172. int num = script_getnum(st,2);
  10173. int typefail = script_getnum(st,3);
  10174. if (num > 0 && num <= ARRAYLENGTH(equip))
  10175. i=pc_checkequip(sd,equip[num-1]);
  10176. if (i < 0 || !sd->inventory_data[i])
  10177. return 0;
  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 ) {
  10182. cardflag = 1;
  10183. if(typefail == 2) {// add cards to inventory, clear
  10184. int flag;
  10185. struct item item_tmp;
  10186. memset(&item_tmp,0,sizeof(item_tmp));
  10187. item_tmp.nameid = sd->status.inventory[i].card[c];
  10188. item_tmp.identify = 1;
  10189. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10190. clif_additem(sd,0,0,flag);
  10191. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10192. }
  10193. }
  10194. }
  10195. }
  10196. if(cardflag == 1) {
  10197. if(typefail == 0 || typefail == 2){ // destroy the item
  10198. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10199. }
  10200. if(typefail == 1){ // destroy the card
  10201. int flag;
  10202. struct item item_tmp;
  10203. memset(&item_tmp,0,sizeof(item_tmp));
  10204. item_tmp.nameid = sd->status.inventory[i].nameid;
  10205. item_tmp.identify = 1;
  10206. item_tmp.refine = sd->status.inventory[i].refine;
  10207. item_tmp.attribute = sd->status.inventory[i].attribute;
  10208. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10209. item_tmp.bound = sd->status.inventory[i].bound;
  10210. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10211. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10212. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10213. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10214. clif_additem(sd,0,0,flag);
  10215. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10216. }
  10217. }
  10218. clif_misceffect(&sd->bl,2);
  10219. }
  10220. return 0;
  10221. }
  10222. /* ================================================================
  10223. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  10224. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  10225. * improved by [Lance]
  10226. * ================================================================*/
  10227. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  10228. {
  10229. int x,y,m,check_val=0,check_ID=0,i=0;
  10230. struct guild *g = NULL;
  10231. struct party_data *p = NULL;
  10232. const char *str;
  10233. const char *mapname;
  10234. unsigned int index;
  10235. mapname=script_getstr(st,2);
  10236. str=script_getstr(st,3);
  10237. x=script_getnum(st,4);
  10238. y=script_getnum(st,5);
  10239. if(script_hasdata(st,7)){
  10240. check_val=script_getnum(st,6);
  10241. check_ID=script_getnum(st,7);
  10242. }
  10243. if((m=map_mapname2mapid(mapname))< 0)
  10244. return 0;
  10245. if(!(index=mapindex_name2id(str)))
  10246. return 0;
  10247. switch(check_val){
  10248. case 1:
  10249. g = guild_search(check_ID);
  10250. if (g){
  10251. for( i=0; i < g->max_member; i++)
  10252. {
  10253. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  10254. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  10255. }
  10256. }
  10257. }
  10258. break;
  10259. case 2:
  10260. p = party_search(check_ID);
  10261. if(p){
  10262. for(i=0;i<MAX_PARTY; i++){
  10263. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  10264. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  10265. }
  10266. }
  10267. }
  10268. break;
  10269. default:
  10270. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  10271. break;
  10272. }
  10273. return 0;
  10274. }
  10275. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  10276. {
  10277. char *event=va_arg(ap,char *);
  10278. struct mob_data *md = ((struct mob_data *)bl);
  10279. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  10280. return 1;
  10281. return 0;
  10282. }
  10283. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  10284. {
  10285. const char *mapname,*event;
  10286. int16 m;
  10287. mapname=script_getstr(st,2);
  10288. event=script_getstr(st,3);
  10289. if( strcmp(event, "all") == 0 )
  10290. event = NULL;
  10291. else
  10292. check_event(st, event);
  10293. if( strcmp(mapname, "this") == 0 ) {
  10294. struct map_session_data *sd = script_rid2sd(st);
  10295. if( sd )
  10296. m = sd->bl.m;
  10297. else {
  10298. script_pushint(st,-1);
  10299. return 0;
  10300. }
  10301. }
  10302. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  10303. script_pushint(st,-1);
  10304. return 0;
  10305. }
  10306. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  10307. return 0;
  10308. }
  10309. BUILDIN_FUNC(marriage)
  10310. {
  10311. const char *partner=script_getstr(st,2);
  10312. TBL_PC *sd=script_rid2sd(st);
  10313. TBL_PC *p_sd=map_nick2sd(partner);
  10314. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  10315. script_pushint(st,0);
  10316. return 0;
  10317. }
  10318. script_pushint(st,1);
  10319. return 0;
  10320. }
  10321. BUILDIN_FUNC(wedding_effect)
  10322. {
  10323. TBL_PC *sd=script_rid2sd(st);
  10324. struct block_list *bl;
  10325. if(sd==NULL) {
  10326. bl=map_id2bl(st->oid);
  10327. } else
  10328. bl=&sd->bl;
  10329. clif_wedding_effect(bl);
  10330. return 0;
  10331. }
  10332. BUILDIN_FUNC(divorce)
  10333. {
  10334. TBL_PC *sd=script_rid2sd(st);
  10335. if(sd==NULL || pc_divorce(sd) < 0){
  10336. script_pushint(st,0);
  10337. return 0;
  10338. }
  10339. script_pushint(st,1);
  10340. return 0;
  10341. }
  10342. BUILDIN_FUNC(ispartneron)
  10343. {
  10344. TBL_PC *sd=script_rid2sd(st);
  10345. if(sd==NULL || !pc_ismarried(sd) ||
  10346. map_charid2sd(sd->status.partner_id) == NULL) {
  10347. script_pushint(st,0);
  10348. return 0;
  10349. }
  10350. script_pushint(st,1);
  10351. return 0;
  10352. }
  10353. BUILDIN_FUNC(getpartnerid)
  10354. {
  10355. TBL_PC *sd=script_rid2sd(st);
  10356. if (sd == NULL) {
  10357. script_pushint(st,0);
  10358. return 0;
  10359. }
  10360. script_pushint(st,sd->status.partner_id);
  10361. return 0;
  10362. }
  10363. BUILDIN_FUNC(getchildid)
  10364. {
  10365. TBL_PC *sd=script_rid2sd(st);
  10366. if (sd == NULL) {
  10367. script_pushint(st,0);
  10368. return 0;
  10369. }
  10370. script_pushint(st,sd->status.child);
  10371. return 0;
  10372. }
  10373. BUILDIN_FUNC(getmotherid)
  10374. {
  10375. TBL_PC *sd=script_rid2sd(st);
  10376. if (sd == NULL) {
  10377. script_pushint(st,0);
  10378. return 0;
  10379. }
  10380. script_pushint(st,sd->status.mother);
  10381. return 0;
  10382. }
  10383. BUILDIN_FUNC(getfatherid)
  10384. {
  10385. TBL_PC *sd=script_rid2sd(st);
  10386. if (sd == NULL) {
  10387. script_pushint(st,0);
  10388. return 0;
  10389. }
  10390. script_pushint(st,sd->status.father);
  10391. return 0;
  10392. }
  10393. BUILDIN_FUNC(warppartner)
  10394. {
  10395. int x,y;
  10396. unsigned short mapindex;
  10397. const char *str;
  10398. TBL_PC *sd=script_rid2sd(st);
  10399. TBL_PC *p_sd=NULL;
  10400. if(sd==NULL || !pc_ismarried(sd) ||
  10401. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  10402. script_pushint(st,0);
  10403. return 0;
  10404. }
  10405. str=script_getstr(st,2);
  10406. x=script_getnum(st,3);
  10407. y=script_getnum(st,4);
  10408. mapindex = mapindex_name2id(str);
  10409. if (mapindex) {
  10410. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  10411. script_pushint(st,1);
  10412. } else
  10413. script_pushint(st,0);
  10414. return 0;
  10415. }
  10416. /*================================================
  10417. * Script for Displaying MOB Information [Valaris]
  10418. *------------------------------------------------*/
  10419. BUILDIN_FUNC(strmobinfo)
  10420. {
  10421. int num=script_getnum(st,2);
  10422. int class_=script_getnum(st,3);
  10423. if(!mobdb_checkid(class_))
  10424. {
  10425. if (num < 3) //requested a string
  10426. script_pushconststr(st,"");
  10427. else
  10428. script_pushint(st,0);
  10429. return 0;
  10430. }
  10431. switch (num) {
  10432. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  10433. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  10434. case 3: script_pushint(st,mob_db(class_)->lv); break;
  10435. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  10436. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  10437. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  10438. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  10439. default:
  10440. script_pushint(st,0);
  10441. break;
  10442. }
  10443. return 0;
  10444. }
  10445. /*==========================================
  10446. * Summon guardians [Valaris]
  10447. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  10448. *------------------------------------------*/
  10449. BUILDIN_FUNC(guardian)
  10450. {
  10451. int class_=0,x=0,y=0,guardian=0;
  10452. const char *str,*map,*evt="";
  10453. struct script_data *data;
  10454. bool has_index = false;
  10455. map =script_getstr(st,2);
  10456. x =script_getnum(st,3);
  10457. y =script_getnum(st,4);
  10458. str =script_getstr(st,5);
  10459. class_=script_getnum(st,6);
  10460. if( script_hasdata(st,8) )
  10461. {// "<event label>",<guardian index>
  10462. evt=script_getstr(st,7);
  10463. guardian=script_getnum(st,8);
  10464. has_index = true;
  10465. } else if( script_hasdata(st,7) ){
  10466. data=script_getdata(st,7);
  10467. get_val(st,data);
  10468. if( data_isstring(data) )
  10469. {// "<event label>"
  10470. evt=script_getstr(st,7);
  10471. } else if( data_isint(data) )
  10472. {// <guardian index>
  10473. guardian=script_getnum(st,7);
  10474. has_index = true;
  10475. } else {
  10476. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  10477. script_reportdata(data);
  10478. return 1;
  10479. }
  10480. }
  10481. check_event(st, evt);
  10482. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  10483. return 0;
  10484. }
  10485. /*==========================================
  10486. * Invisible Walls [Zephyrus]
  10487. *------------------------------------------*/
  10488. BUILDIN_FUNC(setwall)
  10489. {
  10490. const char *map, *name;
  10491. int x, y, m, size, dir;
  10492. bool shootable;
  10493. map = script_getstr(st,2);
  10494. x = script_getnum(st,3);
  10495. y = script_getnum(st,4);
  10496. size = script_getnum(st,5);
  10497. dir = script_getnum(st,6);
  10498. shootable = script_getnum(st,7);
  10499. name = script_getstr(st,8);
  10500. if( (m = map_mapname2mapid(map)) < 0 )
  10501. return 0; // Invalid Map
  10502. map_iwall_set(m, x, y, size, dir, shootable, name);
  10503. return 0;
  10504. }
  10505. BUILDIN_FUNC(delwall)
  10506. {
  10507. const char *name = script_getstr(st,2);
  10508. map_iwall_remove(name);
  10509. return 0;
  10510. }
  10511. /// Retrieves various information about the specified guardian.
  10512. ///
  10513. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  10514. /// type: 0 - whether it is deployed or not
  10515. /// 1 - maximum hp
  10516. /// 2 - current hp
  10517. ///
  10518. BUILDIN_FUNC(guardianinfo)
  10519. {
  10520. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10521. int id = script_getnum(st,3);
  10522. int type = script_getnum(st,4);
  10523. struct guild_castle* gc = guild_mapname2gc(mapname);
  10524. struct mob_data* gd;
  10525. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  10526. {
  10527. script_pushint(st,-1);
  10528. return 0;
  10529. }
  10530. if( type == 0 )
  10531. script_pushint(st, gc->guardian[id].visible);
  10532. else
  10533. if( !gc->guardian[id].visible )
  10534. script_pushint(st,-1);
  10535. else
  10536. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  10537. script_pushint(st,-1);
  10538. else
  10539. {
  10540. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  10541. else if( type == 2 ) script_pushint(st,gd->status.hp);
  10542. else
  10543. script_pushint(st,-1);
  10544. }
  10545. return 0;
  10546. }
  10547. /*==========================================
  10548. * Get the item name by item_id or null
  10549. *------------------------------------------*/
  10550. BUILDIN_FUNC(getitemname)
  10551. {
  10552. int item_id=0;
  10553. struct item_data *i_data;
  10554. char *item_name;
  10555. struct script_data *data;
  10556. data=script_getdata(st,2);
  10557. get_val(st,data);
  10558. if( data_isstring(data) ){
  10559. const char *name=conv_str(st,data);
  10560. struct item_data *item_data = itemdb_searchname(name);
  10561. if( item_data )
  10562. item_id=item_data->nameid;
  10563. }else
  10564. item_id=conv_num(st,data);
  10565. i_data = itemdb_exists(item_id);
  10566. if (i_data == NULL)
  10567. {
  10568. script_pushconststr(st,"null");
  10569. return 0;
  10570. }
  10571. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  10572. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  10573. script_pushstr(st,item_name);
  10574. return 0;
  10575. }
  10576. /*==========================================
  10577. * Returns number of slots an item has. [Skotlex]
  10578. *------------------------------------------*/
  10579. BUILDIN_FUNC(getitemslots)
  10580. {
  10581. int item_id;
  10582. struct item_data *i_data;
  10583. item_id=script_getnum(st,2);
  10584. i_data = itemdb_exists(item_id);
  10585. if (i_data)
  10586. script_pushint(st,i_data->slot);
  10587. else
  10588. script_pushint(st,-1);
  10589. return 0;
  10590. }
  10591. // TODO: add matk here if needed/once we get rid of RENEWAL
  10592. /*==========================================
  10593. * Returns some values of an item [Lupus]
  10594. * Price, Weight, etc...
  10595. getiteminfo(itemID,n), where n
  10596. 0 value_buy;
  10597. 1 value_sell;
  10598. 2 type;
  10599. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10600. if = 0, then monsters don't drop it at all (rare or a quest item)
  10601. if = -1, then this item is sold in NPC shops only
  10602. 4 sex;
  10603. 5 equip;
  10604. 6 weight;
  10605. 7 atk;
  10606. 8 def;
  10607. 9 range;
  10608. 10 slot;
  10609. 11 look;
  10610. 12 elv;
  10611. 13 wlv;
  10612. 14 view id
  10613. *------------------------------------------*/
  10614. BUILDIN_FUNC(getiteminfo)
  10615. {
  10616. int item_id,n;
  10617. int *item_arr;
  10618. struct item_data *i_data;
  10619. item_id = script_getnum(st,2);
  10620. n = script_getnum(st,3);
  10621. i_data = itemdb_exists(item_id);
  10622. if (i_data && n>=0 && n<=14) {
  10623. item_arr = (int*)&i_data->value_buy;
  10624. script_pushint(st,item_arr[n]);
  10625. } else
  10626. script_pushint(st,-1);
  10627. return 0;
  10628. }
  10629. /*==========================================
  10630. * Set some values of an item [Lupus]
  10631. * Price, Weight, etc...
  10632. setiteminfo(itemID,n,Value), where n
  10633. 0 value_buy;
  10634. 1 value_sell;
  10635. 2 type;
  10636. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10637. if = 0, then monsters don't drop it at all (rare or a quest item)
  10638. if = -1, then this item is sold in NPC shops only
  10639. 4 sex;
  10640. 5 equip;
  10641. 6 weight;
  10642. 7 atk;
  10643. 8 def;
  10644. 9 range;
  10645. 10 slot;
  10646. 11 look;
  10647. 12 elv;
  10648. 13 wlv;
  10649. 14 view id
  10650. * Returns Value or -1 if the wrong field's been set
  10651. *------------------------------------------*/
  10652. BUILDIN_FUNC(setiteminfo)
  10653. {
  10654. int item_id,n,value;
  10655. int *item_arr;
  10656. struct item_data *i_data;
  10657. item_id = script_getnum(st,2);
  10658. n = script_getnum(st,3);
  10659. value = script_getnum(st,4);
  10660. i_data = itemdb_exists(item_id);
  10661. if (i_data && n>=0 && n<=14) {
  10662. item_arr = (int*)&i_data->value_buy;
  10663. item_arr[n] = value;
  10664. script_pushint(st,value);
  10665. } else
  10666. script_pushint(st,-1);
  10667. return 0;
  10668. }
  10669. /*==========================================
  10670. * Returns value from equipped item slot n [Lupus]
  10671. getequipcardid(num,slot)
  10672. where
  10673. num = eqip position slot
  10674. slot = 0,1,2,3 (Card Slot N)
  10675. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  10676. it's useful when you want to check item cards or if it's signed
  10677. Useful for such quests as "Sign this refined item with players name" etc
  10678. Hat[0] +4 -> Player's Hat[0] +4
  10679. *------------------------------------------*/
  10680. BUILDIN_FUNC(getequipcardid)
  10681. {
  10682. int i=-1,num,slot;
  10683. TBL_PC *sd;
  10684. num=script_getnum(st,2);
  10685. slot=script_getnum(st,3);
  10686. sd=script_rid2sd(st);
  10687. if (num > 0 && num <= ARRAYLENGTH(equip))
  10688. i=pc_checkequip(sd,equip[num-1]);
  10689. if(i >= 0 && slot>=0 && slot<4)
  10690. script_pushint(st,sd->status.inventory[i].card[slot]);
  10691. else
  10692. script_pushint(st,0);
  10693. return 0;
  10694. }
  10695. /*==========================================
  10696. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  10697. *------------------------------------------*/
  10698. BUILDIN_FUNC(petskillbonus)
  10699. {
  10700. struct pet_data *pd;
  10701. TBL_PC *sd=script_rid2sd(st);
  10702. if(sd==NULL || sd->pd==NULL)
  10703. return 0;
  10704. pd=sd->pd;
  10705. if (pd->bonus)
  10706. { //Clear previous bonus
  10707. if (pd->bonus->timer != INVALID_TIMER)
  10708. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  10709. } else //init
  10710. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  10711. pd->bonus->type=script_getnum(st,2);
  10712. pd->bonus->val=script_getnum(st,3);
  10713. pd->bonus->duration=script_getnum(st,4);
  10714. pd->bonus->delay=script_getnum(st,5);
  10715. if (pd->state.skillbonus == 1)
  10716. pd->state.skillbonus=0; // waiting state
  10717. // wait for timer to start
  10718. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10719. pd->bonus->timer = INVALID_TIMER;
  10720. else
  10721. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  10722. return 0;
  10723. }
  10724. /*==========================================
  10725. * pet looting [Valaris] //Rewritten by [Skotlex]
  10726. *------------------------------------------*/
  10727. BUILDIN_FUNC(petloot)
  10728. {
  10729. int max;
  10730. struct pet_data *pd;
  10731. TBL_PC *sd=script_rid2sd(st);
  10732. if(sd==NULL || sd->pd==NULL)
  10733. return 0;
  10734. max=script_getnum(st,2);
  10735. if(max < 1)
  10736. max = 1; //Let'em loot at least 1 item.
  10737. else if (max > MAX_PETLOOT_SIZE)
  10738. max = MAX_PETLOOT_SIZE;
  10739. pd = sd->pd;
  10740. if (pd->loot != NULL)
  10741. { //Release whatever was there already and reallocate memory
  10742. pet_lootitem_drop(pd, pd->master);
  10743. aFree(pd->loot->item);
  10744. }
  10745. else
  10746. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  10747. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  10748. pd->loot->max=max;
  10749. pd->loot->count = 0;
  10750. pd->loot->weight = 0;
  10751. return 0;
  10752. }
  10753. /*==========================================
  10754. * Set arrays with info of all sd inventory :
  10755. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  10756. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  10757. * @inventorylist_card(0..3), @inventorylist_expire
  10758. * @inventorylist_count = scalar
  10759. *------------------------------------------*/
  10760. BUILDIN_FUNC(getinventorylist)
  10761. {
  10762. TBL_PC *sd=script_rid2sd(st);
  10763. char card_var[NAME_LENGTH];
  10764. int i,j=0,k;
  10765. if(!sd) return 0;
  10766. for(i=0;i<MAX_INVENTORY;i++){
  10767. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  10768. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  10769. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  10770. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  10771. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  10772. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  10773. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  10774. for (k = 0; k < MAX_SLOTS; k++)
  10775. {
  10776. sprintf(card_var, "@inventorylist_card%d",k+1);
  10777. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  10778. }
  10779. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  10780. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  10781. j++;
  10782. }
  10783. }
  10784. pc_setreg(sd,add_str("@inventorylist_count"),j);
  10785. return 0;
  10786. }
  10787. BUILDIN_FUNC(getskilllist)
  10788. {
  10789. TBL_PC *sd=script_rid2sd(st);
  10790. int i,j=0;
  10791. if(!sd) return 0;
  10792. for(i=0;i<MAX_SKILL;i++){
  10793. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  10794. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  10795. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  10796. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  10797. j++;
  10798. }
  10799. }
  10800. pc_setreg(sd,add_str("@skilllist_count"),j);
  10801. return 0;
  10802. }
  10803. BUILDIN_FUNC(clearitem)
  10804. {
  10805. TBL_PC *sd=script_rid2sd(st);
  10806. int i;
  10807. if(sd==NULL) return 0;
  10808. for (i=0; i<MAX_INVENTORY; i++) {
  10809. if (sd->status.inventory[i].amount) {
  10810. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  10811. }
  10812. }
  10813. return 0;
  10814. }
  10815. /*==========================================
  10816. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  10817. *------------------------------------------*/
  10818. BUILDIN_FUNC(disguise)
  10819. {
  10820. int id;
  10821. TBL_PC* sd = script_rid2sd(st);
  10822. if (sd == NULL) return 0;
  10823. id = script_getnum(st,2);
  10824. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  10825. pc_disguise(sd, id);
  10826. script_pushint(st,id);
  10827. } else
  10828. script_pushint(st,0);
  10829. return 0;
  10830. }
  10831. /*==========================================
  10832. * Undisguise Player (returns 1 if success, 0 on fail)
  10833. *------------------------------------------*/
  10834. BUILDIN_FUNC(undisguise)
  10835. {
  10836. TBL_PC* sd = script_rid2sd(st);
  10837. if (sd == NULL) return 0;
  10838. if (sd->disguise) {
  10839. pc_disguise(sd, 0);
  10840. script_pushint(st,0);
  10841. } else {
  10842. script_pushint(st,1);
  10843. }
  10844. return 0;
  10845. }
  10846. /*==========================================
  10847. * Transform a bl to another _class,
  10848. * @type unused
  10849. *------------------------------------------*/
  10850. BUILDIN_FUNC(classchange)
  10851. {
  10852. int _class,type;
  10853. struct block_list *bl=map_id2bl(st->oid);
  10854. if(bl==NULL) return 0;
  10855. _class=script_getnum(st,2);
  10856. type=script_getnum(st,3);
  10857. clif_class_change(bl,_class,type);
  10858. return 0;
  10859. }
  10860. /*==========================================
  10861. * Display an effect
  10862. *------------------------------------------*/
  10863. BUILDIN_FUNC(misceffect)
  10864. {
  10865. int type;
  10866. type=script_getnum(st,2);
  10867. if(st->oid && st->oid != fake_nd->bl.id) {
  10868. struct block_list *bl = map_id2bl(st->oid);
  10869. if (bl)
  10870. clif_specialeffect(bl,type,AREA);
  10871. } else{
  10872. TBL_PC *sd=script_rid2sd(st);
  10873. if(sd)
  10874. clif_specialeffect(&sd->bl,type,AREA);
  10875. }
  10876. return 0;
  10877. }
  10878. /*==========================================
  10879. * Play a BGM on a single client [Rikter/Yommy]
  10880. *------------------------------------------*/
  10881. BUILDIN_FUNC(playBGM)
  10882. {
  10883. const char* name;
  10884. struct map_session_data* sd;
  10885. if( ( sd = script_rid2sd(st) ) != NULL )
  10886. {
  10887. name = script_getstr(st,2);
  10888. clif_playBGM(sd, name);
  10889. }
  10890. return 0;
  10891. }
  10892. static int playBGM_sub(struct block_list* bl,va_list ap)
  10893. {
  10894. const char* name = va_arg(ap,const char*);
  10895. clif_playBGM(BL_CAST(BL_PC, bl), name);
  10896. return 0;
  10897. }
  10898. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  10899. {
  10900. const char* name = va_arg(args, const char*);
  10901. clif_playBGM(sd, name);
  10902. return 0;
  10903. }
  10904. /*==========================================
  10905. * Play a BGM on multiple client [Rikter/Yommy]
  10906. *------------------------------------------*/
  10907. BUILDIN_FUNC(playBGMall)
  10908. {
  10909. const char* name;
  10910. name = script_getstr(st,2);
  10911. if( script_hasdata(st,7) )
  10912. {// specified part of map
  10913. const char* map = script_getstr(st,3);
  10914. int x0 = script_getnum(st,4);
  10915. int y0 = script_getnum(st,5);
  10916. int x1 = script_getnum(st,6);
  10917. int y1 = script_getnum(st,7);
  10918. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  10919. }
  10920. else if( script_hasdata(st,3) )
  10921. {// entire map
  10922. const char* map = script_getstr(st,3);
  10923. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  10924. }
  10925. else
  10926. {// entire server
  10927. map_foreachpc(&playBGM_foreachpc_sub, name);
  10928. }
  10929. return 0;
  10930. }
  10931. /*==========================================
  10932. * Play a .wav sound for sd
  10933. *------------------------------------------*/
  10934. BUILDIN_FUNC(soundeffect)
  10935. {
  10936. TBL_PC* sd = script_rid2sd(st);
  10937. const char* name = script_getstr(st,2);
  10938. int type = script_getnum(st,3);
  10939. if(sd)
  10940. {
  10941. clif_soundeffect(sd,&sd->bl,name,type);
  10942. }
  10943. return 0;
  10944. }
  10945. int soundeffect_sub(struct block_list* bl,va_list ap)
  10946. {
  10947. char* name = va_arg(ap,char*);
  10948. int type = va_arg(ap,int);
  10949. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  10950. return 0;
  10951. }
  10952. /*==========================================
  10953. * Play a sound effect (.wav) on multiple clients
  10954. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  10955. *------------------------------------------*/
  10956. BUILDIN_FUNC(soundeffectall)
  10957. {
  10958. struct block_list* bl;
  10959. const char* name;
  10960. int type;
  10961. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  10962. if (!bl)
  10963. return 0;
  10964. name = script_getstr(st,2);
  10965. type = script_getnum(st,3);
  10966. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  10967. if(!script_hasdata(st,4))
  10968. { // area around
  10969. clif_soundeffectall(bl, name, type, AREA);
  10970. }
  10971. else
  10972. if(!script_hasdata(st,5))
  10973. { // entire map
  10974. const char* map = script_getstr(st,4);
  10975. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  10976. }
  10977. else
  10978. if(script_hasdata(st,8))
  10979. { // specified part of map
  10980. const char* map = script_getstr(st,4);
  10981. int x0 = script_getnum(st,5);
  10982. int y0 = script_getnum(st,6);
  10983. int x1 = script_getnum(st,7);
  10984. int y1 = script_getnum(st,8);
  10985. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  10986. }
  10987. else
  10988. {
  10989. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  10990. }
  10991. return 0;
  10992. }
  10993. /*==========================================
  10994. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  10995. *------------------------------------------*/
  10996. BUILDIN_FUNC(petrecovery)
  10997. {
  10998. struct pet_data *pd;
  10999. TBL_PC *sd=script_rid2sd(st);
  11000. if(sd==NULL || sd->pd==NULL)
  11001. return 0;
  11002. pd=sd->pd;
  11003. if (pd->recovery)
  11004. { //Halt previous bonus
  11005. if (pd->recovery->timer != INVALID_TIMER)
  11006. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11007. } else //Init
  11008. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11009. pd->recovery->type = (sc_type)script_getnum(st,2);
  11010. pd->recovery->delay = script_getnum(st,3);
  11011. pd->recovery->timer = INVALID_TIMER;
  11012. return 0;
  11013. }
  11014. /*==========================================
  11015. * pet healing [Valaris] //Rewritten by [Skotlex]
  11016. *------------------------------------------*/
  11017. BUILDIN_FUNC(petheal)
  11018. {
  11019. struct pet_data *pd;
  11020. TBL_PC *sd=script_rid2sd(st);
  11021. if(sd==NULL || sd->pd==NULL)
  11022. return 0;
  11023. pd=sd->pd;
  11024. if (pd->s_skill)
  11025. { //Clear previous skill
  11026. if (pd->s_skill->timer != INVALID_TIMER)
  11027. {
  11028. if (pd->s_skill->id)
  11029. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11030. else
  11031. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11032. }
  11033. } else //init memory
  11034. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11035. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11036. //Use the lv as the amount to heal
  11037. pd->s_skill->lv=script_getnum(st,2);
  11038. pd->s_skill->delay=script_getnum(st,3);
  11039. pd->s_skill->hp=script_getnum(st,4);
  11040. pd->s_skill->sp=script_getnum(st,5);
  11041. //Use delay as initial offset to avoid skill/heal exploits
  11042. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11043. pd->s_skill->timer = INVALID_TIMER;
  11044. else
  11045. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11046. return 0;
  11047. }
  11048. /*==========================================
  11049. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11050. *------------------------------------------*/
  11051. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11052. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11053. BUILDIN_FUNC(petskillattack)
  11054. {
  11055. struct pet_data *pd;
  11056. TBL_PC *sd=script_rid2sd(st);
  11057. if(sd==NULL || sd->pd==NULL)
  11058. return 0;
  11059. pd=sd->pd;
  11060. if (pd->a_skill == NULL)
  11061. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11062. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11063. pd->a_skill->lv=script_getnum(st,3);
  11064. pd->a_skill->div_ = 0;
  11065. pd->a_skill->rate=script_getnum(st,4);
  11066. pd->a_skill->bonusrate=script_getnum(st,5);
  11067. return 0;
  11068. }
  11069. /*==========================================
  11070. * pet attack skills [Valaris]
  11071. *------------------------------------------*/
  11072. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  11073. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  11074. BUILDIN_FUNC(petskillattack2)
  11075. {
  11076. struct pet_data *pd;
  11077. TBL_PC *sd=script_rid2sd(st);
  11078. if(sd==NULL || sd->pd==NULL)
  11079. return 0;
  11080. pd=sd->pd;
  11081. if (pd->a_skill == NULL)
  11082. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11083. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11084. pd->a_skill->lv=script_getnum(st,3);
  11085. pd->a_skill->div_ = script_getnum(st,4);
  11086. pd->a_skill->rate=script_getnum(st,5);
  11087. pd->a_skill->bonusrate=script_getnum(st,6);
  11088. return 0;
  11089. }
  11090. /*==========================================
  11091. * pet support skills [Skotlex]
  11092. *------------------------------------------*/
  11093. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  11094. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  11095. BUILDIN_FUNC(petskillsupport)
  11096. {
  11097. struct pet_data *pd;
  11098. TBL_PC *sd=script_rid2sd(st);
  11099. if(sd==NULL || sd->pd==NULL)
  11100. return 0;
  11101. pd=sd->pd;
  11102. if (pd->s_skill)
  11103. { //Clear previous skill
  11104. if (pd->s_skill->timer != INVALID_TIMER)
  11105. {
  11106. if (pd->s_skill->id)
  11107. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11108. else
  11109. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11110. }
  11111. } else //init memory
  11112. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11113. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11114. pd->s_skill->lv=script_getnum(st,3);
  11115. pd->s_skill->delay=script_getnum(st,4);
  11116. pd->s_skill->hp=script_getnum(st,5);
  11117. pd->s_skill->sp=script_getnum(st,6);
  11118. //Use delay as initial offset to avoid skill/heal exploits
  11119. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11120. pd->s_skill->timer = INVALID_TIMER;
  11121. else
  11122. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  11123. return 0;
  11124. }
  11125. /*==========================================
  11126. * Scripted skill effects [Celest]
  11127. *------------------------------------------*/
  11128. /// skilleffect <skill id>,<level>
  11129. /// skilleffect "<skill name>",<level>
  11130. BUILDIN_FUNC(skilleffect)
  11131. {
  11132. TBL_PC *sd;
  11133. uint16 skill_id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11134. uint16 skill_lv=script_getnum(st,3);
  11135. sd=script_rid2sd(st);
  11136. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  11137. return 0;
  11138. }
  11139. /*==========================================
  11140. * NPC skill effects [Valaris]
  11141. *------------------------------------------*/
  11142. /// npcskilleffect <skill id>,<level>,<x>,<y>
  11143. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  11144. BUILDIN_FUNC(npcskilleffect)
  11145. {
  11146. struct block_list *bl= map_id2bl(st->oid);
  11147. uint16 skill_id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11148. uint16 skill_lv=script_getnum(st,3);
  11149. int x=script_getnum(st,4);
  11150. int y=script_getnum(st,5);
  11151. if (bl)
  11152. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  11153. return 0;
  11154. }
  11155. /*==========================================
  11156. * Special effects [Valaris]
  11157. *------------------------------------------*/
  11158. BUILDIN_FUNC(specialeffect)
  11159. {
  11160. struct block_list *bl=map_id2bl(st->oid);
  11161. int type = script_getnum(st,2);
  11162. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11163. if(bl==NULL)
  11164. return 0;
  11165. if( script_hasdata(st,4) )
  11166. {
  11167. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  11168. if(nd)
  11169. clif_specialeffect(&nd->bl, type, target);
  11170. }
  11171. else
  11172. {
  11173. if (target == SELF) {
  11174. TBL_PC *sd=script_rid2sd(st);
  11175. if (sd)
  11176. clif_specialeffect_single(bl,type,sd->fd);
  11177. } else {
  11178. clif_specialeffect(bl, type, target);
  11179. }
  11180. }
  11181. return 0;
  11182. }
  11183. BUILDIN_FUNC(specialeffect2)
  11184. {
  11185. TBL_PC *sd=script_rid2sd(st);
  11186. int type = script_getnum(st,2);
  11187. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11188. if( script_hasdata(st,4) )
  11189. sd = map_nick2sd(script_getstr(st,4));
  11190. if (sd)
  11191. clif_specialeffect(&sd->bl, type, target);
  11192. return 0;
  11193. }
  11194. /*==========================================
  11195. * Nude [Valaris]
  11196. *------------------------------------------*/
  11197. BUILDIN_FUNC(nude)
  11198. {
  11199. TBL_PC *sd = script_rid2sd(st);
  11200. int i, calcflag = 0;
  11201. if( sd == NULL )
  11202. return 0;
  11203. for( i = 0 ; i < EQI_MAX; i++ ) {
  11204. if( sd->equip_index[ i ] >= 0 ) {
  11205. if( !calcflag )
  11206. calcflag = 1;
  11207. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  11208. }
  11209. }
  11210. if( calcflag )
  11211. status_calc_pc(sd,0);
  11212. return 0;
  11213. }
  11214. int atcommand_sub(struct script_state* st,int type){
  11215. TBL_PC dummy_sd;
  11216. TBL_PC* sd;
  11217. int fd;
  11218. const char* cmd;
  11219. cmd = script_getstr(st,2);
  11220. if (st->rid) {
  11221. sd = script_rid2sd(st);
  11222. fd = sd->fd;
  11223. } else { //Use a dummy character.
  11224. sd = &dummy_sd;
  11225. fd = 0;
  11226. memset(&dummy_sd, 0, sizeof(TBL_PC));
  11227. if (st->oid)
  11228. {
  11229. struct block_list* bl = map_id2bl(st->oid);
  11230. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  11231. if (bl->type == BL_NPC)
  11232. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  11233. }
  11234. }
  11235. if (!is_atcommand(fd, sd, cmd, type)) {
  11236. ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd);
  11237. script_reportsrc(st);
  11238. return 1;
  11239. }
  11240. return 0;
  11241. }
  11242. /*==========================================
  11243. * gmcommand [MouseJstr]
  11244. *------------------------------------------*/
  11245. BUILDIN_FUNC(atcommand) {
  11246. return atcommand_sub(st,0);
  11247. }
  11248. /*==========================================
  11249. * Displays a message for the player only (like system messages like "you got an apple" )
  11250. *------------------------------------------*/
  11251. BUILDIN_FUNC(dispbottom)
  11252. {
  11253. TBL_PC *sd=script_rid2sd(st);
  11254. const char *message;
  11255. message=script_getstr(st,2);
  11256. if(sd)
  11257. clif_disp_onlyself(sd,message,(int)strlen(message));
  11258. return 0;
  11259. }
  11260. /*===================================
  11261. * Heal portion of recovery command
  11262. *-----------------------------------*/
  11263. int recovery_sub(struct map_session_data* sd, int revive)
  11264. {
  11265. if(revive&(1|4) && pc_isdead(sd)) {
  11266. status_revive(&sd->bl, 100, 100);
  11267. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  11268. clif_specialeffect(&sd->bl, 77, AREA);
  11269. } else if(revive&(1|2) && !pc_isdead(sd)) {
  11270. status_percent_heal(&sd->bl, 100, 100);
  11271. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  11272. }
  11273. return 0;
  11274. }
  11275. /*=========================================================================
  11276. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  11277. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  11278. * <type> determines <option>:
  11279. * 0 : char_id
  11280. * 1 : party_id
  11281. * 2 : guild_id
  11282. * 3 : map_name
  11283. * 4 : all characters
  11284. * <revive_flag>:
  11285. * 1 : Revive and heal all players (default)
  11286. * 2 : Heal living players only
  11287. * 4 : Revive dead players only
  11288. * <map name>:
  11289. * for types 1-2 : map_name (null = all maps)
  11290. *-------------------------------------------------------------------------*/
  11291. BUILDIN_FUNC(recovery)
  11292. {
  11293. TBL_PC *sd = script_rid2sd(st);
  11294. int map = 0, type = 0, revive = 1;
  11295. type = script_getnum(st,2);
  11296. if(script_hasdata(st,4))
  11297. revive = script_getnum(st,4);
  11298. switch(type) {
  11299. case 0:
  11300. if(script_hasdata(st,3))
  11301. sd=map_charid2sd(script_getnum(st,3));
  11302. if(sd == NULL) //If we don't have sd by now, bail out
  11303. return 0;
  11304. recovery_sub(sd, revive);
  11305. break;
  11306. case 1:
  11307. {
  11308. struct party_data* p;
  11309. struct map_session_data* pl_sd;
  11310. //When no party given, we use invoker party
  11311. int p_id, i;
  11312. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11313. map = map_mapname2mapid(script_getstr(st,5));
  11314. if(map < 1) { //But we'll check anyways
  11315. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11316. return 1;
  11317. }
  11318. }
  11319. if(script_hasdata(st,3))
  11320. p_id = script_getnum(st,3);
  11321. else
  11322. p_id = (sd)?sd->status.party_id:0;
  11323. p = party_search(p_id);
  11324. if(p == NULL)
  11325. return 0;
  11326. for (i = 0; i < MAX_PARTY; i++) {
  11327. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  11328. || (map && pl_sd->bl.m != map))
  11329. continue;
  11330. recovery_sub(pl_sd, revive);
  11331. }
  11332. break;
  11333. }
  11334. case 2:
  11335. {
  11336. struct guild* g;
  11337. struct map_session_data* pl_sd;
  11338. //When no guild given, we use invoker guild
  11339. int g_id, i;
  11340. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11341. map = map_mapname2mapid(script_getstr(st,5));
  11342. if(map < 1) { //But we'll check anyways
  11343. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11344. return 1;
  11345. }
  11346. }
  11347. if(script_hasdata(st,3))
  11348. g_id = script_getnum(st,3);
  11349. else
  11350. g_id = (sd)?sd->status.guild_id:0;
  11351. g = guild_search(g_id);
  11352. if(g == NULL)
  11353. return 0;
  11354. for (i = 0; i < MAX_GUILD; i++) {
  11355. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  11356. || (map && pl_sd->bl.m != map))
  11357. continue;
  11358. recovery_sub(pl_sd, revive);
  11359. }
  11360. break;
  11361. }
  11362. case 3:
  11363. if(script_hasdata(st,3))
  11364. map = map_mapname2mapid(script_getstr(st,3));
  11365. else
  11366. map = (sd)?sd->bl.m:0; //No sd and no map given - return
  11367. if(map < 1)
  11368. return 1;
  11369. case 4:
  11370. {
  11371. struct s_mapiterator *iter;
  11372. if(script_hasdata(st,3) && !script_isstring(st,3))
  11373. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  11374. iter = mapit_getallusers();
  11375. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11376. if(type == 3 && sd->bl.m != map)
  11377. continue;
  11378. recovery_sub(sd, revive);
  11379. }
  11380. mapit_free(iter);
  11381. break;
  11382. }
  11383. default:
  11384. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  11385. script_pushint(st,-1);
  11386. return 1;
  11387. }
  11388. script_pushint(st,1); //Successfully executed without errors
  11389. return 0;
  11390. }
  11391. /*==========================================
  11392. * Get your pet info: getpetinfo(n)
  11393. * n -> 0:pet_id 1:pet_class 2:pet_name
  11394. * 3:friendly 4:hungry, 5: rename flag.6:level
  11395. *------------------------------------------*/
  11396. BUILDIN_FUNC(getpetinfo)
  11397. {
  11398. TBL_PC *sd=script_rid2sd(st);
  11399. TBL_PET *pd;
  11400. int type=script_getnum(st,2);
  11401. if(!sd || !sd->pd) {
  11402. if (type == 2)
  11403. script_pushconststr(st,"null");
  11404. else
  11405. script_pushint(st,0);
  11406. return 0;
  11407. }
  11408. pd = sd->pd;
  11409. switch(type){
  11410. case 0: script_pushint(st,pd->pet.pet_id); break;
  11411. case 1: script_pushint(st,pd->pet.class_); break;
  11412. case 2: script_pushstrcopy(st,pd->pet.name); break;
  11413. case 3: script_pushint(st,pd->pet.intimate); break;
  11414. case 4: script_pushint(st,pd->pet.hungry); break;
  11415. case 5: script_pushint(st,pd->pet.rename_flag); break;
  11416. case 6: script_pushint(st,(int)pd->pet.level); break;
  11417. default:
  11418. script_pushint(st,0);
  11419. break;
  11420. }
  11421. return 0;
  11422. }
  11423. /*==========================================
  11424. * Get your homunculus info: gethominfo(n)
  11425. * n -> 0:hom_id 1:class 2:name
  11426. * 3:friendly 4:hungry, 5: rename flag.
  11427. * 6: level
  11428. *------------------------------------------*/
  11429. BUILDIN_FUNC(gethominfo)
  11430. {
  11431. TBL_PC *sd=script_rid2sd(st);
  11432. TBL_HOM *hd;
  11433. int type=script_getnum(st,2);
  11434. hd = sd?sd->hd:NULL;
  11435. if(!hd) {
  11436. if (type == 2)
  11437. script_pushconststr(st,"null");
  11438. else
  11439. script_pushint(st,0);
  11440. return 0;
  11441. }
  11442. switch(type){
  11443. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  11444. case 1: script_pushint(st,hd->homunculus.class_); break;
  11445. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  11446. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  11447. case 4: script_pushint(st,hd->homunculus.hunger); break;
  11448. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  11449. case 6: script_pushint(st,hd->homunculus.level); break;
  11450. default:
  11451. script_pushint(st,0);
  11452. break;
  11453. }
  11454. return 0;
  11455. }
  11456. /// Retrieves information about character's mercenary
  11457. /// getmercinfo <type>[,<char id>];
  11458. BUILDIN_FUNC(getmercinfo)
  11459. {
  11460. int type, char_id;
  11461. struct map_session_data* sd;
  11462. struct mercenary_data* md;
  11463. type = script_getnum(st,2);
  11464. if( script_hasdata(st,3) )
  11465. {
  11466. char_id = script_getnum(st,3);
  11467. if( ( sd = map_charid2sd(char_id) ) == NULL )
  11468. {
  11469. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  11470. script_pushnil(st);
  11471. return 1;
  11472. }
  11473. }
  11474. else
  11475. {
  11476. if( ( sd = script_rid2sd(st) ) == NULL )
  11477. {
  11478. script_pushnil(st);
  11479. return 0;
  11480. }
  11481. }
  11482. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  11483. switch( type )
  11484. {
  11485. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  11486. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  11487. case 2:
  11488. if( md )
  11489. script_pushstrcopy(st,md->db->name);
  11490. else
  11491. script_pushconststr(st,"");
  11492. break;
  11493. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  11494. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  11495. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  11496. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  11497. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  11498. default:
  11499. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  11500. script_pushnil(st);
  11501. return 1;
  11502. }
  11503. return 0;
  11504. }
  11505. /*==========================================
  11506. * Shows wether your inventory(and equips) contain
  11507. selected card or not.
  11508. checkequipedcard(4001);
  11509. *------------------------------------------*/
  11510. BUILDIN_FUNC(checkequipedcard)
  11511. {
  11512. TBL_PC *sd=script_rid2sd(st);
  11513. if(sd){
  11514. int n,i,c=0;
  11515. c=script_getnum(st,2);
  11516. for(i=0;i<MAX_INVENTORY;i++){
  11517. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  11518. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  11519. continue;
  11520. for(n=0;n<sd->inventory_data[i]->slot;n++){
  11521. if(sd->status.inventory[i].card[n]==c){
  11522. script_pushint(st,1);
  11523. return 0;
  11524. }
  11525. }
  11526. }
  11527. }
  11528. }
  11529. script_pushint(st,0);
  11530. return 0;
  11531. }
  11532. BUILDIN_FUNC(jump_zero)
  11533. {
  11534. int sel;
  11535. sel=script_getnum(st,2);
  11536. if(!sel) {
  11537. int pos;
  11538. if( !data_islabel(script_getdata(st,3)) ){
  11539. ShowError("script: jump_zero: not label !\n");
  11540. st->state=END;
  11541. return 1;
  11542. }
  11543. pos=script_getnum(st,3);
  11544. st->pos=pos;
  11545. st->state=GOTO;
  11546. }
  11547. return 0;
  11548. }
  11549. /*==========================================
  11550. * movenpc [MouseJstr]
  11551. *------------------------------------------*/
  11552. BUILDIN_FUNC(movenpc)
  11553. {
  11554. TBL_NPC *nd = NULL;
  11555. const char *npc;
  11556. int x,y;
  11557. npc = script_getstr(st,2);
  11558. x = script_getnum(st,3);
  11559. y = script_getnum(st,4);
  11560. if ((nd = npc_name2id(npc)) == NULL)
  11561. return -1;
  11562. if (script_hasdata(st,5))
  11563. nd->ud.dir = script_getnum(st,5) % 8;
  11564. npc_movenpc(nd, x, y);
  11565. return 0;
  11566. }
  11567. /*==========================================
  11568. * message [MouseJstr]
  11569. *------------------------------------------*/
  11570. BUILDIN_FUNC(message)
  11571. {
  11572. const char *msg,*player;
  11573. TBL_PC *pl_sd = NULL;
  11574. player = script_getstr(st,2);
  11575. msg = script_getstr(st,3);
  11576. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  11577. return 0;
  11578. clif_displaymessage(pl_sd->fd, msg);
  11579. return 0;
  11580. }
  11581. /*==========================================
  11582. * npctalk (sends message to surrounding area)
  11583. *------------------------------------------*/
  11584. BUILDIN_FUNC(npctalk)
  11585. {
  11586. const char* str;
  11587. char name[NAME_LENGTH], message[256];
  11588. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  11589. str = script_getstr(st,2);
  11590. if(nd)
  11591. {
  11592. safestrncpy(name, nd->name, sizeof(name));
  11593. strtok(name, "#"); // discard extra name identifier if present
  11594. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  11595. clif_disp_overhead(&nd->bl, message);
  11596. }
  11597. return 0;
  11598. }
  11599. // change npc walkspeed [Valaris]
  11600. BUILDIN_FUNC(npcspeed)
  11601. {
  11602. struct npc_data* nd;
  11603. int speed;
  11604. speed = script_getnum(st,2);
  11605. nd =(struct npc_data *)map_id2bl(st->oid);
  11606. if( nd )
  11607. {
  11608. nd->speed = speed;
  11609. nd->ud.state.speed_changed = 1;
  11610. }
  11611. return 0;
  11612. }
  11613. // make an npc walk to a position [Valaris]
  11614. BUILDIN_FUNC(npcwalkto)
  11615. {
  11616. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11617. int x=0,y=0;
  11618. x=script_getnum(st,2);
  11619. y=script_getnum(st,3);
  11620. if(nd) {
  11621. if (!nd->status.hp) {
  11622. status_calc_npc(nd, true);
  11623. } else {
  11624. status_calc_npc(nd, false);
  11625. }
  11626. unit_walktoxy(&nd->bl,x,y,0);
  11627. }
  11628. return 0;
  11629. }
  11630. // stop an npc's movement [Valaris]
  11631. BUILDIN_FUNC(npcstop)
  11632. {
  11633. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11634. if(nd) {
  11635. unit_stop_walking(&nd->bl,1|4);
  11636. }
  11637. return 0;
  11638. }
  11639. /*==========================================
  11640. * getlook char info. getlook(arg)
  11641. *------------------------------------------*/
  11642. BUILDIN_FUNC(getlook)
  11643. {
  11644. int type,val;
  11645. TBL_PC *sd;
  11646. sd=script_rid2sd(st);
  11647. type=script_getnum(st,2);
  11648. val=-1;
  11649. switch(type) {
  11650. case LOOK_HAIR: val=sd->status.hair; break; //1
  11651. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  11652. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  11653. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  11654. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  11655. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  11656. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  11657. case LOOK_SHIELD: val=sd->status.shield; break; //8
  11658. case LOOK_SHOES: break; //9
  11659. case LOOK_ROBE: val=sd->status.robe; break; //12
  11660. }
  11661. script_pushint(st,val);
  11662. return 0;
  11663. }
  11664. /*==========================================
  11665. * get char save point. argument: 0- map name, 1- x, 2- y
  11666. *------------------------------------------*/
  11667. BUILDIN_FUNC(getsavepoint)
  11668. {
  11669. TBL_PC* sd;
  11670. int type;
  11671. sd = script_rid2sd(st);
  11672. if (sd == NULL) {
  11673. script_pushint(st,0);
  11674. return 0;
  11675. }
  11676. type = script_getnum(st,2);
  11677. switch(type) {
  11678. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  11679. case 1: script_pushint(st,sd->status.save_point.x); break;
  11680. case 2: script_pushint(st,sd->status.save_point.y); break;
  11681. default:
  11682. script_pushint(st,0);
  11683. break;
  11684. }
  11685. return 0;
  11686. }
  11687. /*==========================================
  11688. * Get position for char/npc/pet/mob objects. Added by Lorky
  11689. *
  11690. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  11691. * where type:
  11692. * MapName$ - String variable for output map name
  11693. * MapX - Integer variable for output coord X
  11694. * MapY - Integer variable for output coord Y
  11695. * type - type of object
  11696. * 0 - Character coord
  11697. * 1 - NPC coord
  11698. * 2 - Pet coord
  11699. * 3 - Mob coord (not released)
  11700. * 4 - Homun coord
  11701. * 5 - Mercenary coord
  11702. * 6 - Elemental coord
  11703. * CharName$ - Name object. If miss or "this" the current object
  11704. *
  11705. * Return:
  11706. * 0 - success
  11707. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  11708. *------------------------------------------*/
  11709. BUILDIN_FUNC(getmapxy)
  11710. {
  11711. struct block_list *bl = NULL;
  11712. TBL_PC *sd=NULL;
  11713. int num;
  11714. const char *name;
  11715. char prefix;
  11716. int x,y,type;
  11717. char mapname[MAP_NAME_LENGTH];
  11718. if( !data_isreference(script_getdata(st,2)) ){
  11719. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  11720. script_pushint(st,-1);
  11721. return 1;
  11722. }
  11723. if( !data_isreference(script_getdata(st,3)) ){
  11724. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  11725. script_pushint(st,-1);
  11726. return 1;
  11727. }
  11728. if( !data_isreference(script_getdata(st,4)) ){
  11729. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  11730. script_pushint(st,-1);
  11731. return 1;
  11732. }
  11733. // Possible needly check function parameters on C_STR,C_INT,C_INT
  11734. type=script_getnum(st,5);
  11735. switch (type){
  11736. case 0: //Get Character Position
  11737. if( script_hasdata(st,6) )
  11738. sd=map_nick2sd(script_getstr(st,6));
  11739. else
  11740. sd=script_rid2sd(st);
  11741. if (sd)
  11742. bl = &sd->bl;
  11743. break;
  11744. case 1: //Get NPC Position
  11745. if( script_hasdata(st,6) )
  11746. {
  11747. struct npc_data *nd;
  11748. nd=npc_name2id(script_getstr(st,6));
  11749. if (nd)
  11750. bl = &nd->bl;
  11751. } else //In case the origin is not an npc?
  11752. bl=map_id2bl(st->oid);
  11753. break;
  11754. case 2: //Get Pet Position
  11755. if(script_hasdata(st,6))
  11756. sd=map_nick2sd(script_getstr(st,6));
  11757. else
  11758. sd=script_rid2sd(st);
  11759. if (sd && sd->pd)
  11760. bl = &sd->pd->bl;
  11761. break;
  11762. case 3: //Get Mob Position
  11763. break; //Not supported?
  11764. case 4: //Get Homun Position
  11765. if(script_hasdata(st,6))
  11766. sd=map_nick2sd(script_getstr(st,6));
  11767. else
  11768. sd=script_rid2sd(st);
  11769. if (sd && sd->hd)
  11770. bl = &sd->hd->bl;
  11771. break;
  11772. case 5: //Get Mercenary Position
  11773. if(script_hasdata(st,6))
  11774. sd=map_nick2sd(script_getstr(st,6));
  11775. else
  11776. sd=script_rid2sd(st);
  11777. if (sd && sd->md)
  11778. bl = &sd->md->bl;
  11779. break;
  11780. case 6: //Get Elemental Position
  11781. if(script_hasdata(st,6))
  11782. sd=map_nick2sd(script_getstr(st,6));
  11783. else
  11784. sd=script_rid2sd(st);
  11785. if (sd && sd->ed)
  11786. bl = &sd->ed->bl;
  11787. break;
  11788. default:
  11789. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  11790. script_pushint(st,-1);
  11791. return 1;
  11792. }
  11793. if (!bl) { //No object found.
  11794. script_pushint(st,-1);
  11795. return 0;
  11796. }
  11797. x= bl->x;
  11798. y= bl->y;
  11799. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  11800. //Set MapName$
  11801. num=st->stack->stack_data[st->start+2].u.num;
  11802. name=get_str(num&0x00ffffff);
  11803. prefix=*name;
  11804. if(not_server_variable(prefix))
  11805. sd=script_rid2sd(st);
  11806. else
  11807. sd=NULL;
  11808. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  11809. //Set MapX
  11810. num=st->stack->stack_data[st->start+3].u.num;
  11811. name=get_str(num&0x00ffffff);
  11812. prefix=*name;
  11813. if(not_server_variable(prefix))
  11814. sd=script_rid2sd(st);
  11815. else
  11816. sd=NULL;
  11817. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  11818. //Set MapY
  11819. num=st->stack->stack_data[st->start+4].u.num;
  11820. name=get_str(num&0x00ffffff);
  11821. prefix=*name;
  11822. if(not_server_variable(prefix))
  11823. sd=script_rid2sd(st);
  11824. else
  11825. sd=NULL;
  11826. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  11827. //Return Success value
  11828. script_pushint(st,0);
  11829. return 0;
  11830. }
  11831. /*==========================================
  11832. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  11833. *------------------------------------------*/
  11834. BUILDIN_FUNC(logmes)
  11835. {
  11836. const char *str;
  11837. TBL_PC* sd;
  11838. sd = script_rid2sd(st);
  11839. if( sd == NULL )
  11840. return 1;
  11841. str = script_getstr(st,2);
  11842. log_npc(sd,str);
  11843. return 0;
  11844. }
  11845. BUILDIN_FUNC(summon)
  11846. {
  11847. int _class, timeout=0;
  11848. const char *str,*event="";
  11849. TBL_PC *sd;
  11850. struct mob_data *md;
  11851. int tick = gettick();
  11852. sd=script_rid2sd(st);
  11853. if (!sd) return 0;
  11854. str =script_getstr(st,2);
  11855. _class=script_getnum(st,3);
  11856. if( script_hasdata(st,4) )
  11857. timeout=script_getnum(st,4);
  11858. if( script_hasdata(st,5) ){
  11859. event=script_getstr(st,5);
  11860. check_event(st, event);
  11861. }
  11862. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  11863. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  11864. if (md) {
  11865. md->master_id=sd->bl.id;
  11866. md->special_state.ai = AI_ATTACK;
  11867. if( md->deletetimer != INVALID_TIMER )
  11868. delete_timer(md->deletetimer, mob_timer_delete);
  11869. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  11870. mob_spawn (md); //Now it is ready for spawning.
  11871. clif_specialeffect(&md->bl,344,AREA);
  11872. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  11873. }
  11874. return 0;
  11875. }
  11876. /*==========================================
  11877. * Checks whether it is daytime/nighttime
  11878. *------------------------------------------*/
  11879. BUILDIN_FUNC(isnight)
  11880. {
  11881. script_pushint(st,(night_flag == 1));
  11882. return 0;
  11883. }
  11884. BUILDIN_FUNC(isday)
  11885. {
  11886. script_pushint(st,(night_flag == 0));
  11887. return 0;
  11888. }
  11889. /*================================================
  11890. * Check how many items/cards in the list are
  11891. * equipped - used for 2/15's cards patch [celest]
  11892. *------------------------------------------------*/
  11893. BUILDIN_FUNC(isequippedcnt)
  11894. {
  11895. TBL_PC *sd;
  11896. int i, j, k, id = 1;
  11897. int ret = 0;
  11898. sd = script_rid2sd(st);
  11899. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11900. script_pushint(st,0);
  11901. return 0;
  11902. }
  11903. for (i=0; id!=0; i++) {
  11904. FETCH (i+2, id) else id = 0;
  11905. if (id <= 0)
  11906. continue;
  11907. for (j=0; j<EQI_MAX; j++) {
  11908. int index;
  11909. index = sd->equip_index[j];
  11910. if(index < 0) continue;
  11911. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11912. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11913. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11914. if(!sd->inventory_data[index])
  11915. continue;
  11916. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  11917. if (sd->inventory_data[index]->nameid == id)
  11918. ret+= sd->status.inventory[index].amount;
  11919. } else { //Count cards.
  11920. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11921. continue; //No cards
  11922. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11923. if (sd->status.inventory[index].card[k] == id)
  11924. ret++; //[Lupus]
  11925. }
  11926. }
  11927. }
  11928. }
  11929. script_pushint(st,ret);
  11930. return 0;
  11931. }
  11932. /*================================================
  11933. * Check whether another card has been
  11934. * equipped - used for 2/15's cards patch [celest]
  11935. * -- Items checked cannot be reused in another
  11936. * card set to prevent exploits
  11937. *------------------------------------------------*/
  11938. BUILDIN_FUNC(isequipped)
  11939. {
  11940. TBL_PC *sd;
  11941. int i, j, k, id = 1;
  11942. int index, flag;
  11943. int ret = -1;
  11944. //Original hash to reverse it when full check fails.
  11945. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  11946. sd = script_rid2sd(st);
  11947. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11948. script_pushint(st,0);
  11949. return 0;
  11950. }
  11951. setitem_hash = sd->bonus.setitem_hash;
  11952. setitem_hash2 = sd->bonus.setitem_hash2;
  11953. for (i=0; id!=0; i++) {
  11954. FETCH (i+2, id) else id = 0;
  11955. if (id <= 0)
  11956. continue;
  11957. flag = 0;
  11958. for (j=0; j<EQI_MAX; j++) {
  11959. index = sd->equip_index[j];
  11960. if(index < 0) continue;
  11961. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11962. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11963. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11964. if(!sd->inventory_data[index])
  11965. continue;
  11966. if (itemdb_type(id) != IT_CARD) {
  11967. if (sd->inventory_data[index]->nameid != id)
  11968. continue;
  11969. flag = 1;
  11970. break;
  11971. } else { //Cards
  11972. if (sd->inventory_data[index]->slot == 0 ||
  11973. itemdb_isspecial(sd->status.inventory[index].card[0]))
  11974. continue;
  11975. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  11976. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  11977. unsigned int hash = 0;
  11978. if (sd->status.inventory[index].card[k] != id)
  11979. continue;
  11980. hash = 1<<((j<5?j:j-5)*4 + k);
  11981. // check if card is already used by another set
  11982. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  11983. continue;
  11984. // We have found a match
  11985. flag = 1;
  11986. // Set hash so this card cannot be used by another
  11987. if (j<5)
  11988. sd->bonus.setitem_hash |= hash;
  11989. else
  11990. sd->bonus.setitem_hash2 |= hash;
  11991. break;
  11992. }
  11993. }
  11994. if (flag) break; //Card found
  11995. }
  11996. if (ret == -1)
  11997. ret = flag;
  11998. else
  11999. ret &= flag;
  12000. if (!ret) break;
  12001. }
  12002. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  12003. sd->bonus.setitem_hash = setitem_hash;
  12004. sd->bonus.setitem_hash2 = setitem_hash2;
  12005. }
  12006. script_pushint(st,ret);
  12007. return 0;
  12008. }
  12009. /*================================================
  12010. * Check how many given inserted cards in the CURRENT
  12011. * weapon - used for 2/15's cards patch [Lupus]
  12012. *------------------------------------------------*/
  12013. BUILDIN_FUNC(cardscnt)
  12014. {
  12015. TBL_PC *sd;
  12016. int i, k, id = 1;
  12017. int ret = 0;
  12018. int index;
  12019. sd = script_rid2sd(st);
  12020. for (i=0; id!=0; i++) {
  12021. FETCH (i+2, id) else id = 0;
  12022. if (id <= 0)
  12023. continue;
  12024. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  12025. if(index < 0) continue;
  12026. if(!sd->inventory_data[index])
  12027. continue;
  12028. if(itemdb_type(id) != IT_CARD) {
  12029. if (sd->inventory_data[index]->nameid == id)
  12030. ret+= sd->status.inventory[index].amount;
  12031. } else {
  12032. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12033. continue;
  12034. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12035. if (sd->status.inventory[index].card[k] == id)
  12036. ret++;
  12037. }
  12038. }
  12039. }
  12040. script_pushint(st,ret);
  12041. // script_pushint(st,current_equip_item_index);
  12042. return 0;
  12043. }
  12044. /*=======================================================
  12045. * Returns the refined number of the current item, or an
  12046. * item with inventory index specified
  12047. *-------------------------------------------------------*/
  12048. BUILDIN_FUNC(getrefine)
  12049. {
  12050. TBL_PC *sd;
  12051. if ((sd = script_rid2sd(st))!= NULL)
  12052. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  12053. else
  12054. script_pushint(st,0);
  12055. return 0;
  12056. }
  12057. /*=======================================================
  12058. * Day/Night controls
  12059. *-------------------------------------------------------*/
  12060. BUILDIN_FUNC(night)
  12061. {
  12062. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  12063. return 0;
  12064. }
  12065. BUILDIN_FUNC(day)
  12066. {
  12067. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  12068. return 0;
  12069. }
  12070. //=======================================================
  12071. // Unequip [Spectre]
  12072. //-------------------------------------------------------
  12073. BUILDIN_FUNC(unequip)
  12074. {
  12075. int i;
  12076. size_t num;
  12077. TBL_PC *sd;
  12078. num = script_getnum(st,2);
  12079. sd = script_rid2sd(st);
  12080. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  12081. {
  12082. i = pc_checkequip(sd,equip[num-1]);
  12083. if (i >= 0)
  12084. pc_unequipitem(sd,i,1|2);
  12085. }
  12086. return 0;
  12087. }
  12088. BUILDIN_FUNC(equip)
  12089. {
  12090. int nameid=0,i;
  12091. TBL_PC *sd;
  12092. struct item_data *item_data;
  12093. sd = script_rid2sd(st);
  12094. nameid=script_getnum(st,2);
  12095. if((item_data = itemdb_exists(nameid)) == NULL)
  12096. {
  12097. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  12098. return 1;
  12099. }
  12100. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  12101. if( i < MAX_INVENTORY )
  12102. pc_equipitem(sd,i,item_data->equip);
  12103. return 0;
  12104. }
  12105. BUILDIN_FUNC(autoequip)
  12106. {
  12107. int nameid, flag;
  12108. struct item_data *item_data;
  12109. nameid=script_getnum(st,2);
  12110. flag=script_getnum(st,3);
  12111. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  12112. {
  12113. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  12114. return 1;
  12115. }
  12116. if( !itemdb_isequip2(item_data) )
  12117. {
  12118. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  12119. return 1;
  12120. }
  12121. item_data->flag.autoequip = flag>0?1:0;
  12122. return 0;
  12123. }
  12124. BUILDIN_FUNC(setbattleflag)
  12125. {
  12126. const char *flag, *value;
  12127. flag = script_getstr(st,2);
  12128. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  12129. if (battle_set_value(flag, value) == 0)
  12130. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  12131. else
  12132. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  12133. return 0;
  12134. }
  12135. BUILDIN_FUNC(getbattleflag)
  12136. {
  12137. const char *flag;
  12138. flag = script_getstr(st,2);
  12139. script_pushint(st,battle_get_value(flag));
  12140. return 0;
  12141. }
  12142. //=======================================================
  12143. // strlen [Valaris]
  12144. //-------------------------------------------------------
  12145. BUILDIN_FUNC(getstrlen)
  12146. {
  12147. const char *str = script_getstr(st,2);
  12148. int len = (str) ? (int)strlen(str) : 0;
  12149. script_pushint(st,len);
  12150. return 0;
  12151. }
  12152. //=======================================================
  12153. // isalpha [Valaris]
  12154. //-------------------------------------------------------
  12155. BUILDIN_FUNC(charisalpha)
  12156. {
  12157. const char *str=script_getstr(st,2);
  12158. int pos=script_getnum(st,3);
  12159. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  12160. script_pushint(st,val);
  12161. return 0;
  12162. }
  12163. //=======================================================
  12164. // charisupper <str>, <index>
  12165. //-------------------------------------------------------
  12166. BUILDIN_FUNC(charisupper)
  12167. {
  12168. const char *str = script_getstr(st,2);
  12169. int pos = script_getnum(st,3);
  12170. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  12171. script_pushint(st,val);
  12172. return 0;
  12173. }
  12174. //=======================================================
  12175. // charislower <str>, <index>
  12176. //-------------------------------------------------------
  12177. BUILDIN_FUNC(charislower)
  12178. {
  12179. const char *str = script_getstr(st,2);
  12180. int pos = script_getnum(st,3);
  12181. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  12182. script_pushint(st,val);
  12183. return 0;
  12184. }
  12185. //=======================================================
  12186. // charat <str>, <index>
  12187. //-------------------------------------------------------
  12188. BUILDIN_FUNC(charat) {
  12189. const char *str = script_getstr(st,2);
  12190. int pos = script_getnum(st,3);
  12191. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  12192. char output[2];
  12193. output[0] = str[pos];
  12194. output[1] = '\0';
  12195. script_pushstrcopy(st, output);
  12196. } else
  12197. script_pushconststr(st, "");
  12198. return 0;
  12199. }
  12200. //=======================================================
  12201. // setchar <string>, <char>, <index>
  12202. //-------------------------------------------------------
  12203. BUILDIN_FUNC(setchar)
  12204. {
  12205. const char *str = script_getstr(st,2);
  12206. const char *c = script_getstr(st,3);
  12207. int index = script_getnum(st,4);
  12208. char *output = aStrdup(str);
  12209. if(index >= 0 && index < strlen(output))
  12210. output[index] = *c;
  12211. script_pushstr(st, output);
  12212. return 0;
  12213. }
  12214. //=======================================================
  12215. // insertchar <string>, <char>, <index>
  12216. //-------------------------------------------------------
  12217. BUILDIN_FUNC(insertchar)
  12218. {
  12219. const char *str = script_getstr(st,2);
  12220. const char *c = script_getstr(st,3);
  12221. int index = script_getnum(st,4);
  12222. char *output;
  12223. size_t len = strlen(str);
  12224. if(index < 0)
  12225. index = 0;
  12226. else if(index > len)
  12227. index = len;
  12228. output = (char*)aMalloc(len + 2);
  12229. memcpy(output, str, index);
  12230. output[index] = c[0];
  12231. memcpy(&output[index+1], &str[index], len - index);
  12232. output[len+1] = '\0';
  12233. script_pushstr(st, output);
  12234. return 0;
  12235. }
  12236. //=======================================================
  12237. // delchar <string>, <index>
  12238. //-------------------------------------------------------
  12239. BUILDIN_FUNC(delchar)
  12240. {
  12241. const char *str = script_getstr(st,2);
  12242. int index = script_getnum(st,3);
  12243. char *output;
  12244. size_t len = strlen(str);
  12245. if(index < 0 || index > len) {
  12246. //return original
  12247. output = aStrdup(str);
  12248. script_pushstr(st, output);
  12249. return 0;
  12250. }
  12251. output = (char*)aMalloc(len);
  12252. memcpy(output, str, index);
  12253. memcpy(&output[index], &str[index+1], len - index);
  12254. script_pushstr(st, output);
  12255. return 0;
  12256. }
  12257. //=======================================================
  12258. // strtoupper <str>
  12259. //-------------------------------------------------------
  12260. BUILDIN_FUNC(strtoupper)
  12261. {
  12262. const char *str = script_getstr(st,2);
  12263. char *output = aStrdup(str);
  12264. char *cursor = output;
  12265. while (*cursor != '\0') {
  12266. *cursor = TOUPPER(*cursor);
  12267. cursor++;
  12268. }
  12269. script_pushstr(st, output);
  12270. return 0;
  12271. }
  12272. //=======================================================
  12273. // strtolower <str>
  12274. //-------------------------------------------------------
  12275. BUILDIN_FUNC(strtolower)
  12276. {
  12277. const char *str = script_getstr(st,2);
  12278. char *output = aStrdup(str);
  12279. char *cursor = output;
  12280. while (*cursor != '\0') {
  12281. *cursor = TOLOWER(*cursor);
  12282. cursor++;
  12283. }
  12284. script_pushstr(st, output);
  12285. return 0;
  12286. }
  12287. //=======================================================
  12288. // substr <str>, <start>, <end>
  12289. //-------------------------------------------------------
  12290. BUILDIN_FUNC(substr)
  12291. {
  12292. const char *str = script_getstr(st,2);
  12293. char *output;
  12294. int start = script_getnum(st,3);
  12295. int end = script_getnum(st,4);
  12296. int len = 0;
  12297. if(start >= 0 && end < strlen(str) && start <= end) {
  12298. len = end - start + 1;
  12299. output = (char*)aMalloc(len + 1);
  12300. memcpy(output, &str[start], len);
  12301. } else
  12302. output = (char*)aMalloc(1);
  12303. output[len] = '\0';
  12304. script_pushstr(st, output);
  12305. return 0;
  12306. }
  12307. //=======================================================
  12308. // explode <dest_string_array>, <str>, <delimiter>
  12309. // Note: delimiter is limited to 1 char
  12310. //-------------------------------------------------------
  12311. BUILDIN_FUNC(explode)
  12312. {
  12313. struct script_data* data = script_getdata(st, 2);
  12314. const char *str = script_getstr(st,3);
  12315. const char delimiter = script_getstr(st, 4)[0];
  12316. int32 id;
  12317. size_t len = strlen(str);
  12318. int i = 0, j = 0;
  12319. int start;
  12320. char *temp;
  12321. const char* name;
  12322. TBL_PC* sd = NULL;
  12323. temp = (char*)aMalloc(len + 1);
  12324. if( !data_isreference(data) )
  12325. {
  12326. ShowError("script:explode: not a variable\n");
  12327. script_reportdata(data);
  12328. st->state = END;
  12329. return 1;// not a variable
  12330. }
  12331. id = reference_getid(data);
  12332. start = reference_getindex(data);
  12333. name = reference_getname(data);
  12334. if( not_array_variable(*name) )
  12335. {
  12336. ShowError("script:explode: illegal scope\n");
  12337. script_reportdata(data);
  12338. st->state = END;
  12339. return 1;// not supported
  12340. }
  12341. if( !is_string_variable(name) )
  12342. {
  12343. ShowError("script:explode: not string array\n");
  12344. script_reportdata(data);
  12345. st->state = END;
  12346. return 1;// data type mismatch
  12347. }
  12348. if( not_server_variable(*name) )
  12349. {
  12350. sd = script_rid2sd(st);
  12351. if( sd == NULL )
  12352. return 0;// no player attached
  12353. }
  12354. while(str[i] != '\0') {
  12355. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  12356. temp[j] = '\0';
  12357. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  12358. j = 0;
  12359. ++i;
  12360. } else {
  12361. temp[j++] = str[i++];
  12362. }
  12363. }
  12364. //set last string
  12365. temp[j] = '\0';
  12366. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  12367. aFree(temp);
  12368. return 0;
  12369. }
  12370. //=======================================================
  12371. // implode <string_array>
  12372. // implode <string_array>, <glue>
  12373. //-------------------------------------------------------
  12374. BUILDIN_FUNC(implode)
  12375. {
  12376. struct script_data* data = script_getdata(st, 2);
  12377. const char *glue = NULL, *name, *temp;
  12378. int32 glue_len = 0, array_size, id;
  12379. size_t len = 0;
  12380. int i, k = 0;
  12381. TBL_PC* sd = NULL;
  12382. char *output;
  12383. if( !data_isreference(data) )
  12384. {
  12385. ShowError("script:implode: not a variable\n");
  12386. script_reportdata(data);
  12387. st->state = END;
  12388. return 1;// not a variable
  12389. }
  12390. id = reference_getid(data);
  12391. name = reference_getname(data);
  12392. if( not_array_variable(*name) )
  12393. {
  12394. ShowError("script:implode: illegal scope\n");
  12395. script_reportdata(data);
  12396. st->state = END;
  12397. return 1;// not supported
  12398. }
  12399. if( !is_string_variable(name) )
  12400. {
  12401. ShowError("script:implode: not string array\n");
  12402. script_reportdata(data);
  12403. st->state = END;
  12404. return 1;// data type mismatch
  12405. }
  12406. if( not_server_variable(*name) )
  12407. {
  12408. sd = script_rid2sd(st);
  12409. if( sd == NULL )
  12410. return 0;// no player attached
  12411. }
  12412. //count chars
  12413. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  12414. if(array_size == -1) //empty array check (AmsTaff)
  12415. {
  12416. ShowWarning("script:implode: array length = 0\n");
  12417. output = (char*)aMalloc(sizeof(char)*5);
  12418. sprintf(output,"%s","NULL");
  12419. } else {
  12420. for(i = 0; i <= array_size; ++i) {
  12421. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12422. len += strlen(temp);
  12423. script_removetop(st, -1, 0);
  12424. }
  12425. //allocate mem
  12426. if( script_hasdata(st,3) ) {
  12427. glue = script_getstr(st,3);
  12428. glue_len = strlen(glue);
  12429. len += glue_len * (array_size);
  12430. }
  12431. output = (char*)aMalloc(len + 1);
  12432. //build output
  12433. for(i = 0; i < array_size; ++i) {
  12434. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12435. len = strlen(temp);
  12436. memcpy(&output[k], temp, len);
  12437. k += len;
  12438. if(glue_len != 0) {
  12439. memcpy(&output[k], glue, glue_len);
  12440. k += glue_len;
  12441. }
  12442. script_removetop(st, -1, 0);
  12443. }
  12444. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  12445. len = strlen(temp);
  12446. memcpy(&output[k], temp, len);
  12447. k += len;
  12448. script_removetop(st, -1, 0);
  12449. output[k] = '\0';
  12450. }
  12451. script_pushstr(st, output);
  12452. return 0;
  12453. }
  12454. //=======================================================
  12455. // sprintf(<format>, ...);
  12456. // Implements C sprintf, except format %n. The resulting string is
  12457. // returned, instead of being saved in variable by reference.
  12458. //-------------------------------------------------------
  12459. BUILDIN_FUNC(sprintf)
  12460. {
  12461. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  12462. const char* format;
  12463. char* p;
  12464. char* q;
  12465. char* buf = NULL;
  12466. char* buf2 = NULL;
  12467. struct script_data* data;
  12468. StringBuf final_buf;
  12469. // Fetch init data
  12470. format = script_getstr(st, 2);
  12471. argc = script_lastdata(st)-2;
  12472. len = strlen(format);
  12473. // Skip parsing, where no parsing is required.
  12474. if(len==0){
  12475. script_pushconststr(st,"");
  12476. return 0;
  12477. }
  12478. // Pessimistic alloc
  12479. CREATE(buf, char, len+1);
  12480. // Need not be parsed, just solve stuff like %%.
  12481. if(argc==0){
  12482. memcpy(buf,format,len+1);
  12483. script_pushstrcopy(st, buf);
  12484. aFree(buf);
  12485. return 0;
  12486. }
  12487. safestrncpy(buf, format, len+1);
  12488. // Issue sprintf for each parameter
  12489. StringBuf_Init(&final_buf);
  12490. q = buf;
  12491. while((p = strchr(q, '%'))!=NULL){
  12492. if(p!=q){
  12493. len = p-q+1;
  12494. if(buf2_len<len){
  12495. RECREATE(buf2, char, len);
  12496. buf2_len = len;
  12497. }
  12498. safestrncpy(buf2, q, len);
  12499. StringBuf_AppendStr(&final_buf, buf2);
  12500. q = p;
  12501. }
  12502. p = q+1;
  12503. if(*p=='%'){ // %%
  12504. StringBuf_AppendStr(&final_buf, "%");
  12505. q+=2;
  12506. continue;
  12507. }
  12508. if(*p=='n'){ // %n
  12509. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  12510. script_reportsrc(st);
  12511. q+=2;
  12512. continue;
  12513. }
  12514. if(arg>=argc){
  12515. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  12516. if(buf) aFree(buf);
  12517. if(buf2) aFree(buf2);
  12518. StringBuf_Destroy(&final_buf);
  12519. script_pushconststr(st,"");
  12520. return 1;
  12521. }
  12522. if((p = strchr(q+1, '%'))==NULL){
  12523. p = strchr(q, 0); // EOS
  12524. }
  12525. len = p-q+1;
  12526. if(buf2_len<len){
  12527. RECREATE(buf2, char, len);
  12528. buf2_len = len;
  12529. }
  12530. safestrncpy(buf2, q, len);
  12531. q = p;
  12532. // Note: This assumes the passed value being the correct
  12533. // type to the current format specifier. If not, the server
  12534. // probably crashes or returns anything else, than expected,
  12535. // but it would behave in normal code the same way so it's
  12536. // the scripter's responsibility.
  12537. data = script_getdata(st, arg+3);
  12538. if(data_isstring(data)){ // String
  12539. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12540. }else if(data_isint(data)){ // Number
  12541. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12542. }else if(data_isreference(data)){ // Variable
  12543. char* name = reference_getname(data);
  12544. if(name[strlen(name)-1]=='$'){ // var Str
  12545. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12546. }else{ // var Int
  12547. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12548. }
  12549. }else{ // Unsupported type
  12550. ShowError("buildin_sprintf: Unknown argument type!\n");
  12551. if(buf) aFree(buf);
  12552. if(buf2) aFree(buf2);
  12553. StringBuf_Destroy(&final_buf);
  12554. script_pushconststr(st,"");
  12555. return 1;
  12556. }
  12557. arg++;
  12558. }
  12559. // Append anything left
  12560. if(*q){
  12561. StringBuf_AppendStr(&final_buf, q);
  12562. }
  12563. // Passed more, than needed
  12564. if(arg<argc){
  12565. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  12566. script_reportsrc(st);
  12567. }
  12568. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  12569. if(buf) aFree(buf);
  12570. if(buf2) aFree(buf2);
  12571. StringBuf_Destroy(&final_buf);
  12572. return 0;
  12573. }
  12574. //=======================================================
  12575. // sscanf(<str>, <format>, ...);
  12576. // Implements C sscanf.
  12577. //-------------------------------------------------------
  12578. BUILDIN_FUNC(sscanf){
  12579. unsigned int argc, arg = 0, len;
  12580. struct script_data* data;
  12581. struct map_session_data* sd = NULL;
  12582. const char* str;
  12583. const char* format;
  12584. const char* p;
  12585. const char* q;
  12586. char* buf = NULL;
  12587. char* buf_p;
  12588. char* ref_str = NULL;
  12589. int ref_int;
  12590. // Get data
  12591. str = script_getstr(st, 2);
  12592. format = script_getstr(st, 3);
  12593. argc = script_lastdata(st)-3;
  12594. len = strlen(format);
  12595. CREATE(buf, char, len*2+1);
  12596. // Issue sscanf for each parameter
  12597. *buf = 0;
  12598. q = format;
  12599. while((p = strchr(q, '%'))){
  12600. if(p!=q){
  12601. strncat(buf, q, (size_t)(p-q));
  12602. q = p;
  12603. }
  12604. p = q+1;
  12605. if(*p=='*' || *p=='%'){ // Skip
  12606. strncat(buf, q, 2);
  12607. q+=2;
  12608. continue;
  12609. }
  12610. if(arg>=argc){
  12611. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  12612. script_pushint(st, -1);
  12613. if(buf) aFree(buf);
  12614. if(ref_str) aFree(ref_str);
  12615. return 1;
  12616. }
  12617. if((p = strchr(q+1, '%'))==NULL){
  12618. p = strchr(q, 0); // EOS
  12619. }
  12620. len = p-q;
  12621. strncat(buf, q, len);
  12622. q = p;
  12623. // Validate output
  12624. data = script_getdata(st, arg+4);
  12625. if(!data_isreference(data) || !reference_tovariable(data)){
  12626. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  12627. script_pushint(st, -1);
  12628. if(buf) aFree(buf);
  12629. if(ref_str) aFree(ref_str);
  12630. return 1;
  12631. }
  12632. buf_p = reference_getname(data);
  12633. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  12634. script_pushint(st, -1);
  12635. if(buf) aFree(buf);
  12636. if(ref_str) aFree(ref_str);
  12637. return 0;
  12638. }
  12639. // Save value if any
  12640. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  12641. if(ref_str==NULL){
  12642. CREATE(ref_str, char, strlen(str)+1);
  12643. }
  12644. if(sscanf(str, buf, ref_str)==0){
  12645. break;
  12646. }
  12647. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  12648. } else { // Number
  12649. if(sscanf(str, buf, &ref_int)==0){
  12650. break;
  12651. }
  12652. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  12653. }
  12654. arg++;
  12655. // Disable used format (%... -> %*...)
  12656. buf_p = strchr(buf, 0);
  12657. memmove(buf_p-len+2, buf_p-len+1, len);
  12658. *(buf_p-len+1) = '*';
  12659. }
  12660. script_pushint(st, arg);
  12661. if(buf) aFree(buf);
  12662. if(ref_str) aFree(ref_str);
  12663. return 0;
  12664. }
  12665. //=======================================================
  12666. // strpos(<haystack>, <needle>)
  12667. // strpos(<haystack>, <needle>, <offset>)
  12668. //
  12669. // Implements PHP style strpos. Adapted from code from
  12670. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  12671. //-------------------------------------------------------
  12672. BUILDIN_FUNC(strpos) {
  12673. const char *haystack = script_getstr(st,2);
  12674. const char *needle = script_getstr(st,3);
  12675. int i;
  12676. size_t len;
  12677. if( script_hasdata(st,4) )
  12678. i = script_getnum(st,4);
  12679. else
  12680. i = 0;
  12681. if (needle[0] == '\0') {
  12682. script_pushint(st, -1);
  12683. return 0;
  12684. }
  12685. len = strlen(haystack);
  12686. for ( ; i < len; ++i ) {
  12687. if ( haystack[i] == *needle ) {
  12688. // matched starting char -- loop through remaining chars
  12689. const char *h, *n;
  12690. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  12691. if ( *h != *n ) {
  12692. break;
  12693. }
  12694. }
  12695. if ( !*n ) { // matched all of 'needle' to null termination
  12696. script_pushint(st, i);
  12697. return 0;
  12698. }
  12699. }
  12700. }
  12701. script_pushint(st, -1);
  12702. return 0;
  12703. }
  12704. //===============================================================
  12705. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  12706. //
  12707. // Note: Finds all instances of <search> in <input> and replaces
  12708. // with <replace>. If specified will only replace as many
  12709. // instances as specified in <count>. By default will be case
  12710. // sensitive.
  12711. //---------------------------------------------------------------
  12712. BUILDIN_FUNC(replacestr)
  12713. {
  12714. const char *input = script_getstr(st, 2);
  12715. const char *find = script_getstr(st, 3);
  12716. const char *replace = script_getstr(st, 4);
  12717. size_t inputlen = strlen(input);
  12718. size_t findlen = strlen(find);
  12719. struct StringBuf output;
  12720. bool usecase = true;
  12721. int count = 0;
  12722. int numFinds = 0;
  12723. int i = 0, f = 0;
  12724. if(findlen == 0) {
  12725. ShowError("script:replacestr: Invalid search length.\n");
  12726. st->state = END;
  12727. return 1;
  12728. }
  12729. if(script_hasdata(st, 5)) {
  12730. if( !script_isstring(st,5) )
  12731. usecase = script_getnum(st, 5) != 0;
  12732. else {
  12733. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  12734. st->state = END;
  12735. return 1;
  12736. }
  12737. }
  12738. if(script_hasdata(st, 6)) {
  12739. count = script_getnum(st, 6);
  12740. if(count == 0) {
  12741. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  12742. st->state = END;
  12743. return 1;
  12744. }
  12745. }
  12746. StringBuf_Init(&output);
  12747. for(; i < inputlen; i++) {
  12748. if(count && count == numFinds) { //found enough, stop looking
  12749. break;
  12750. }
  12751. for(f = 0; f <= findlen; f++) {
  12752. if(f == findlen) { //complete match
  12753. numFinds++;
  12754. StringBuf_AppendStr(&output, replace);
  12755. i += findlen - 1;
  12756. break;
  12757. } else {
  12758. if(usecase) {
  12759. if((i + f) > inputlen || input[i + f] != find[f]) {
  12760. StringBuf_Printf(&output, "%c", input[i]);
  12761. break;
  12762. }
  12763. } else {
  12764. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12765. StringBuf_Printf(&output, "%c", input[i]);
  12766. break;
  12767. }
  12768. }
  12769. }
  12770. }
  12771. }
  12772. //append excess after enough found
  12773. if(i < inputlen)
  12774. StringBuf_AppendStr(&output, &(input[i]));
  12775. script_pushstrcopy(st, StringBuf_Value(&output));
  12776. StringBuf_Destroy(&output);
  12777. return 0;
  12778. }
  12779. //========================================================
  12780. // countstr <input>, <search>{, <usecase>}
  12781. //
  12782. // Note: Counts the number of times <search> occurs in
  12783. // <input>. By default will be case sensitive.
  12784. //--------------------------------------------------------
  12785. BUILDIN_FUNC(countstr)
  12786. {
  12787. const char *input = script_getstr(st, 2);
  12788. const char *find = script_getstr(st, 3);
  12789. size_t inputlen = strlen(input);
  12790. size_t findlen = strlen(find);
  12791. bool usecase = true;
  12792. int numFinds = 0;
  12793. int i = 0, f = 0;
  12794. if(findlen == 0) {
  12795. ShowError("script:countstr: Invalid search length.\n");
  12796. st->state = END;
  12797. return 1;
  12798. }
  12799. if(script_hasdata(st, 4)) {
  12800. if( !script_isstring(st,4) )
  12801. usecase = script_getnum(st, 4) != 0;
  12802. else {
  12803. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  12804. st->state = END;
  12805. return 1;
  12806. }
  12807. }
  12808. for(; i < inputlen; i++) {
  12809. for(f = 0; f <= findlen; f++) {
  12810. if(f == findlen) { //complete match
  12811. numFinds++;
  12812. i += findlen - 1;
  12813. break;
  12814. } else {
  12815. if(usecase) {
  12816. if((i + f) > inputlen || input[i + f] != find[f]) {
  12817. break;
  12818. }
  12819. } else {
  12820. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12821. break;
  12822. }
  12823. }
  12824. }
  12825. }
  12826. }
  12827. script_pushint(st, numFinds);
  12828. return 0;
  12829. }
  12830. /// Changes the display name and/or display class of the npc.
  12831. /// Returns 0 is successful, 1 if the npc does not exist.
  12832. ///
  12833. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  12834. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  12835. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  12836. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  12837. BUILDIN_FUNC(setnpcdisplay)
  12838. {
  12839. const char* name;
  12840. const char* newname = NULL;
  12841. int class_ = -1, size = -1;
  12842. struct script_data* data;
  12843. struct npc_data* nd;
  12844. name = script_getstr(st,2);
  12845. data = script_getdata(st,3);
  12846. if( script_hasdata(st,4) )
  12847. class_ = script_getnum(st,4);
  12848. if( script_hasdata(st,5) )
  12849. size = script_getnum(st,5);
  12850. get_val(st, data);
  12851. if( data_isstring(data) )
  12852. newname = conv_str(st,data);
  12853. else if( data_isint(data) )
  12854. class_ = conv_num(st,data);
  12855. else
  12856. {
  12857. ShowError("script:setnpcdisplay: expected string or number\n");
  12858. script_reportdata(data);
  12859. return 1;
  12860. }
  12861. nd = npc_name2id(name);
  12862. if( nd == NULL )
  12863. {// not found
  12864. script_pushint(st,1);
  12865. return 0;
  12866. }
  12867. // update npc
  12868. if( newname )
  12869. npc_setdisplayname(nd, newname);
  12870. if( size != -1 && size != (int)nd->size )
  12871. nd->size = size;
  12872. else
  12873. size = -1;
  12874. if( class_ != -1 && nd->class_ != class_ )
  12875. npc_setclass(nd, class_);
  12876. else if( size != -1 )
  12877. { // Required to update the visual size
  12878. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  12879. clif_spawn(&nd->bl);
  12880. }
  12881. script_pushint(st,0);
  12882. return 0;
  12883. }
  12884. BUILDIN_FUNC(atoi)
  12885. {
  12886. const char *value;
  12887. value = script_getstr(st,2);
  12888. script_pushint(st,atoi(value));
  12889. return 0;
  12890. }
  12891. // case-insensitive substring search [lordalfa]
  12892. BUILDIN_FUNC(compare)
  12893. {
  12894. const char *message;
  12895. const char *cmpstring;
  12896. message = script_getstr(st,2);
  12897. cmpstring = script_getstr(st,3);
  12898. script_pushint(st,(stristr(message,cmpstring) != NULL));
  12899. return 0;
  12900. }
  12901. // [zBuffer] List of mathematics commands --->
  12902. BUILDIN_FUNC(sqrt)
  12903. {
  12904. double i, a;
  12905. i = script_getnum(st,2);
  12906. a = sqrt(i);
  12907. script_pushint(st,(int)a);
  12908. return 0;
  12909. }
  12910. BUILDIN_FUNC(pow)
  12911. {
  12912. double i, a, b;
  12913. a = script_getnum(st,2);
  12914. b = script_getnum(st,3);
  12915. i = pow(a,b);
  12916. script_pushint(st,(int)i);
  12917. return 0;
  12918. }
  12919. BUILDIN_FUNC(distance)
  12920. {
  12921. int x0, y0, x1, y1;
  12922. x0 = script_getnum(st,2);
  12923. y0 = script_getnum(st,3);
  12924. x1 = script_getnum(st,4);
  12925. y1 = script_getnum(st,5);
  12926. script_pushint(st,distance_xy(x0,y0,x1,y1));
  12927. return 0;
  12928. }
  12929. // <--- [zBuffer] List of mathematics commands
  12930. BUILDIN_FUNC(md5)
  12931. {
  12932. const char *tmpstr;
  12933. char *md5str;
  12934. tmpstr = script_getstr(st,2);
  12935. md5str = (char *)aMalloc((32+1)*sizeof(char));
  12936. MD5_String(tmpstr, md5str);
  12937. script_pushstr(st, md5str);
  12938. return 0;
  12939. }
  12940. // [zBuffer] List of dynamic var commands --->
  12941. BUILDIN_FUNC(setd)
  12942. {
  12943. TBL_PC *sd=NULL;
  12944. char varname[100];
  12945. const char *buffer;
  12946. int elem;
  12947. buffer = script_getstr(st, 2);
  12948. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  12949. elem = 0;
  12950. if( not_server_variable(*varname) )
  12951. {
  12952. sd = script_rid2sd(st);
  12953. if( sd == NULL )
  12954. {
  12955. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  12956. return 0;
  12957. }
  12958. }
  12959. if( is_string_variable(varname) ) {
  12960. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  12961. } else {
  12962. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  12963. }
  12964. return 0;
  12965. }
  12966. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  12967. {
  12968. int i, j;
  12969. TBL_PC* sd = NULL;
  12970. const char* query;
  12971. struct script_data* data;
  12972. const char* name;
  12973. int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  12974. int num_vars;
  12975. int num_cols;
  12976. // check target variables
  12977. for( i = 3; script_hasdata(st,i); ++i ) {
  12978. data = script_getdata(st, i);
  12979. if( data_isreference(data) ) { // it's a variable
  12980. name = reference_getname(data);
  12981. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  12982. sd = script_rid2sd(st);
  12983. if( sd == NULL ) { // no player attached
  12984. script_reportdata(data);
  12985. st->state = END;
  12986. return 1;
  12987. }
  12988. }
  12989. if( not_array_variable(*name) )
  12990. max_rows = 1;// not an array, limit to one row
  12991. } else {
  12992. ShowError("script:query_sql: not a variable\n");
  12993. script_reportdata(data);
  12994. st->state = END;
  12995. return 1;
  12996. }
  12997. }
  12998. num_vars = i - 3;
  12999. // Execute the query
  13000. query = script_getstr(st,2);
  13001. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  13002. Sql_ShowDebug(handle);
  13003. script_pushint(st, 0);
  13004. return 1;
  13005. }
  13006. if( Sql_NumRows(handle) == 0 ) { // No data received
  13007. Sql_FreeResult(handle);
  13008. script_pushint(st, 0);
  13009. return 0;
  13010. }
  13011. // Count the number of columns to store
  13012. num_cols = Sql_NumColumns(handle);
  13013. if( num_vars < num_cols ) {
  13014. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  13015. script_reportsrc(st);
  13016. } else if( num_vars > num_cols ) {
  13017. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  13018. script_reportsrc(st);
  13019. }
  13020. // Store data
  13021. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  13022. for( j = 0; j < num_vars; ++j ) {
  13023. char* str = NULL;
  13024. if( j < num_cols )
  13025. Sql_GetData(handle, j, &str, NULL);
  13026. data = script_getdata(st, j+3);
  13027. name = reference_getname(data);
  13028. if( is_string_variable(name) )
  13029. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  13030. else
  13031. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  13032. }
  13033. }
  13034. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  13035. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  13036. script_reportsrc(st);
  13037. }
  13038. // Free data
  13039. Sql_FreeResult(handle);
  13040. script_pushint(st, i);
  13041. return 0;
  13042. }
  13043. BUILDIN_FUNC(query_sql) {
  13044. #ifdef BETA_THREAD_TEST
  13045. if( st->state != RERUNLINE ) {
  13046. queryThread_add(st,false);
  13047. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13048. } else
  13049. st->state = RUN;
  13050. return 0;
  13051. #else
  13052. return buildin_query_sql_sub(st, mmysql_handle);
  13053. #endif
  13054. }
  13055. BUILDIN_FUNC(query_logsql) {
  13056. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  13057. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  13058. script_pushint(st,-1);
  13059. return 1;
  13060. }
  13061. #ifdef BETA_THREAD_TEST
  13062. if( st->state != RERUNLINE ) {
  13063. queryThread_add(st,true);
  13064. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13065. } else
  13066. st->state = RUN;
  13067. return 0;
  13068. #else
  13069. return buildin_query_sql_sub(st, logmysql_handle);
  13070. #endif
  13071. }
  13072. //Allows escaping of a given string.
  13073. BUILDIN_FUNC(escape_sql)
  13074. {
  13075. const char *str;
  13076. char *esc_str;
  13077. size_t len;
  13078. str = script_getstr(st,2);
  13079. len = strlen(str);
  13080. esc_str = (char*)aMalloc(len*2+1);
  13081. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  13082. script_pushstr(st, esc_str);
  13083. return 0;
  13084. }
  13085. BUILDIN_FUNC(getd)
  13086. {
  13087. char varname[100];
  13088. const char *buffer;
  13089. int elem;
  13090. buffer = script_getstr(st, 2);
  13091. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  13092. elem = 0;
  13093. // Push the 'pointer' so it's more flexible [Lance]
  13094. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  13095. return 0;
  13096. }
  13097. BUILDIN_FUNC(callshop)
  13098. {
  13099. TBL_PC *sd = NULL;
  13100. struct npc_data *nd;
  13101. const char *shopname;
  13102. int flag = 0;
  13103. sd = script_rid2sd(st);
  13104. if (!sd) {
  13105. script_pushint(st,0);
  13106. return 0;
  13107. }
  13108. shopname = script_getstr(st, 2);
  13109. if( script_hasdata(st,3) )
  13110. flag = script_getnum(st,3);
  13111. nd = npc_name2id(shopname);
  13112. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  13113. {
  13114. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  13115. script_pushint(st,0);
  13116. return 1;
  13117. }
  13118. if( nd->subtype == SHOP )
  13119. {
  13120. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  13121. sd->state.callshop = 1;
  13122. switch( flag )
  13123. {
  13124. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  13125. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  13126. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  13127. }
  13128. }
  13129. else
  13130. clif_cashshop_show(sd, nd);
  13131. sd->npc_shopid = nd->bl.id;
  13132. script_pushint(st,1);
  13133. return 0;
  13134. }
  13135. BUILDIN_FUNC(npcshopitem)
  13136. {
  13137. const char* npcname = script_getstr(st, 2);
  13138. struct npc_data* nd = npc_name2id(npcname);
  13139. int n, i;
  13140. int amount;
  13141. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  13142. { //Not found.
  13143. script_pushint(st,0);
  13144. return 0;
  13145. }
  13146. // get the count of new entries
  13147. amount = (script_lastdata(st)-2)/2;
  13148. // generate new shop item list
  13149. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  13150. for( n = 0, i = 3; n < amount; n++, i+=2 )
  13151. {
  13152. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13153. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13154. }
  13155. nd->u.shop.count = n;
  13156. script_pushint(st,1);
  13157. return 0;
  13158. }
  13159. BUILDIN_FUNC(npcshopadditem)
  13160. {
  13161. const char* npcname = script_getstr(st,2);
  13162. struct npc_data* nd = npc_name2id(npcname);
  13163. int n, i;
  13164. int amount;
  13165. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  13166. { //Not found.
  13167. script_pushint(st,0);
  13168. return 0;
  13169. }
  13170. // get the count of new entries
  13171. amount = (script_lastdata(st)-2)/2;
  13172. // append new items to existing shop item list
  13173. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  13174. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  13175. {
  13176. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13177. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13178. }
  13179. nd->u.shop.count = n;
  13180. script_pushint(st,1);
  13181. return 0;
  13182. }
  13183. BUILDIN_FUNC(npcshopdelitem)
  13184. {
  13185. const char* npcname = script_getstr(st,2);
  13186. struct npc_data* nd = npc_name2id(npcname);
  13187. unsigned int nameid;
  13188. int n, i;
  13189. int amount;
  13190. int size;
  13191. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  13192. { //Not found.
  13193. script_pushint(st,0);
  13194. return 0;
  13195. }
  13196. amount = script_lastdata(st)-2;
  13197. size = nd->u.shop.count;
  13198. // remove specified items from the shop item list
  13199. for( i = 3; i < 3 + amount; i++ )
  13200. {
  13201. nameid = script_getnum(st,i);
  13202. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  13203. if( n < size )
  13204. {
  13205. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  13206. size--;
  13207. }
  13208. }
  13209. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  13210. nd->u.shop.count = size;
  13211. script_pushint(st,1);
  13212. return 0;
  13213. }
  13214. //Sets a script to attach to a shop npc.
  13215. BUILDIN_FUNC(npcshopattach)
  13216. {
  13217. const char* npcname = script_getstr(st,2);
  13218. struct npc_data* nd = npc_name2id(npcname);
  13219. int flag = 1;
  13220. if( script_hasdata(st,3) )
  13221. flag = script_getnum(st,3);
  13222. if( !nd || nd->subtype != SHOP )
  13223. { //Not found.
  13224. script_pushint(st,0);
  13225. return 0;
  13226. }
  13227. if (flag)
  13228. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  13229. else
  13230. nd->master_nd = NULL;
  13231. script_pushint(st,1);
  13232. return 0;
  13233. }
  13234. /*==========================================
  13235. * Returns some values of an item [Lupus]
  13236. * Price, Weight, etc...
  13237. setitemscript(itemID,"{new item bonus script}",[n]);
  13238. Where n:
  13239. 0 - script
  13240. 1 - Equip script
  13241. 2 - Unequip script
  13242. *------------------------------------------*/
  13243. BUILDIN_FUNC(setitemscript)
  13244. {
  13245. int item_id,n=0;
  13246. const char *script;
  13247. struct item_data *i_data;
  13248. struct script_code **dstscript;
  13249. item_id = script_getnum(st,2);
  13250. script = script_getstr(st,3);
  13251. if( script_hasdata(st,4) )
  13252. n=script_getnum(st,4);
  13253. i_data = itemdb_exists(item_id);
  13254. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  13255. script_pushint(st,0);
  13256. return 0;
  13257. }
  13258. switch (n) {
  13259. case 2:
  13260. dstscript = &i_data->unequip_script;
  13261. break;
  13262. case 1:
  13263. dstscript = &i_data->equip_script;
  13264. break;
  13265. default:
  13266. dstscript = &i_data->script;
  13267. break;
  13268. }
  13269. if(*dstscript)
  13270. script_free_code(*dstscript);
  13271. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  13272. script_pushint(st,1);
  13273. return 0;
  13274. }
  13275. /*=======================================================
  13276. * Add or Update a mob drop temporarily [Akinari]
  13277. * Original Idea By: [Lupus]
  13278. *
  13279. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  13280. *
  13281. * If given an item the mob already drops, the rate
  13282. * is updated to the new rate. Rate cannot exceed 10000
  13283. * Returns 1 if succeeded (added/updated a mob drop)
  13284. *-------------------------------------------------------*/
  13285. BUILDIN_FUNC(addmonsterdrop)
  13286. {
  13287. struct mob_db *mob;
  13288. int item_id,rate,i,c = 0;
  13289. if(script_isstring(st,2))
  13290. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13291. else
  13292. mob = mob_db(script_getnum(st,2));
  13293. item_id=script_getnum(st,3);
  13294. rate=script_getnum(st,4);
  13295. if(!itemdb_exists(item_id)){
  13296. ShowError("addmonsterdrop: Nonexistant item %d requested.\n", item_id );
  13297. return 1;
  13298. }
  13299. if(mob) { //We got a valid monster, check for available drop slot
  13300. for(i = 0; i < MAX_MOB_DROP; i++) {
  13301. if(mob->dropitem[i].nameid) {
  13302. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  13303. c = i;
  13304. break;
  13305. }
  13306. continue;
  13307. }
  13308. if(!c) //Accept first available slot only
  13309. c = i;
  13310. }
  13311. if(c) { //Fill in the slot with the item and rate
  13312. mob->dropitem[c].nameid = item_id;
  13313. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  13314. script_pushint(st,1);
  13315. } else //No place to put the new drop
  13316. script_pushint(st,0);
  13317. } else {
  13318. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13319. return 1;
  13320. }
  13321. return 0;
  13322. }
  13323. /*=======================================================
  13324. * Delete a mob drop temporarily [Akinari]
  13325. * Original Idea By: [Lupus]
  13326. *
  13327. * delmonsterdrop <mob_id or name>,<item_id>;
  13328. *
  13329. * Returns 1 if succeeded (deleted a mob drop)
  13330. *-------------------------------------------------------*/
  13331. BUILDIN_FUNC(delmonsterdrop)
  13332. {
  13333. struct mob_db *mob;
  13334. int item_id,i;
  13335. if(script_isstring(st,2))
  13336. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13337. else
  13338. mob = mob_db(script_getnum(st,2));
  13339. item_id=script_getnum(st,3);
  13340. if(!itemdb_exists(item_id)){
  13341. ShowError("delmonsterdrop: Nonexistant item %d requested.\n", item_id );
  13342. return 1;
  13343. }
  13344. if(mob) { //We got a valid monster, check for item drop on monster
  13345. for(i = 0; i < MAX_MOB_DROP; i++) {
  13346. if(mob->dropitem[i].nameid == item_id) {
  13347. mob->dropitem[i].nameid = 0;
  13348. mob->dropitem[i].p = 0;
  13349. script_pushint(st,1);
  13350. return 0;
  13351. }
  13352. }
  13353. //No drop on that monster
  13354. script_pushint(st,0);
  13355. } else {
  13356. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13357. return 1;
  13358. }
  13359. return 0;
  13360. }
  13361. /*==========================================
  13362. * Returns some values of a monster [Lupus]
  13363. * Name, Level, race, size, etc...
  13364. getmonsterinfo(monsterID,queryIndex);
  13365. *------------------------------------------*/
  13366. BUILDIN_FUNC(getmonsterinfo)
  13367. {
  13368. struct mob_db *mob;
  13369. int mob_id;
  13370. mob_id = script_getnum(st,2);
  13371. if (!mobdb_checkid(mob_id)) {
  13372. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  13373. if ( !script_getnum(st,3) ) //requested a string
  13374. script_pushconststr(st,"null");
  13375. else
  13376. script_pushint(st,-1);
  13377. return -1;
  13378. }
  13379. mob = mob_db(mob_id);
  13380. switch ( script_getnum(st,3) ) {
  13381. case 0: script_pushstrcopy(st,mob->jname); break;
  13382. case 1: script_pushint(st,mob->lv); break;
  13383. case 2: script_pushint(st,mob->status.max_hp); break;
  13384. case 3: script_pushint(st,mob->base_exp); break;
  13385. case 4: script_pushint(st,mob->job_exp); break;
  13386. case 5: script_pushint(st,mob->status.rhw.atk); break;
  13387. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  13388. case 7: script_pushint(st,mob->status.def); break;
  13389. case 8: script_pushint(st,mob->status.mdef); break;
  13390. case 9: script_pushint(st,mob->status.str); break;
  13391. case 10: script_pushint(st,mob->status.agi); break;
  13392. case 11: script_pushint(st,mob->status.vit); break;
  13393. case 12: script_pushint(st,mob->status.int_); break;
  13394. case 13: script_pushint(st,mob->status.dex); break;
  13395. case 14: script_pushint(st,mob->status.luk); break;
  13396. case 15: script_pushint(st,mob->status.rhw.range); break;
  13397. case 16: script_pushint(st,mob->range2); break;
  13398. case 17: script_pushint(st,mob->range3); break;
  13399. case 18: script_pushint(st,mob->status.size); break;
  13400. case 19: script_pushint(st,mob->status.race); break;
  13401. case 20: script_pushint(st,mob->status.def_ele); break;
  13402. case 21: script_pushint(st,mob->status.mode); break;
  13403. case 22: script_pushint(st,mob->mexp); break;
  13404. default: script_pushint(st,-1); //wrong Index
  13405. }
  13406. return 0;
  13407. }
  13408. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  13409. {
  13410. TBL_PC *sd = NULL;
  13411. if(script_hasdata(st,2))
  13412. sd = map_nick2sd(script_getstr(st,2));
  13413. else
  13414. sd = script_rid2sd(st);
  13415. if(sd)
  13416. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  13417. else
  13418. script_pushint(st,0);
  13419. return 0;
  13420. }
  13421. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  13422. {
  13423. TBL_PC *sd = NULL;
  13424. if(script_hasdata(st,2))
  13425. sd = map_nick2sd(script_getstr(st,2));
  13426. else
  13427. sd = script_rid2sd(st);
  13428. if(sd)
  13429. script_pushint(st,(sd->chatID != 0));
  13430. else
  13431. script_pushint(st,0);
  13432. return 0;
  13433. }
  13434. BUILDIN_FUNC(checkidle)
  13435. {
  13436. TBL_PC *sd = NULL;
  13437. if (script_hasdata(st, 2))
  13438. sd = map_nick2sd(script_getstr(st, 2));
  13439. else
  13440. sd = script_rid2sd(st);
  13441. if (sd)
  13442. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  13443. else
  13444. script_pushint(st, 0);
  13445. return 0;
  13446. }
  13447. BUILDIN_FUNC(searchitem)
  13448. {
  13449. struct script_data* data = script_getdata(st, 2);
  13450. const char *itemname = script_getstr(st,3);
  13451. struct item_data *items[MAX_SEARCH];
  13452. int count;
  13453. char* name;
  13454. int32 start;
  13455. int32 id;
  13456. int32 i;
  13457. TBL_PC* sd = NULL;
  13458. if ((items[0] = itemdb_exists(atoi(itemname))))
  13459. count = 1;
  13460. else {
  13461. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  13462. if (count > MAX_SEARCH) count = MAX_SEARCH;
  13463. }
  13464. if (!count) {
  13465. script_pushint(st, 0);
  13466. return 0;
  13467. }
  13468. if( !data_isreference(data) )
  13469. {
  13470. ShowError("script:searchitem: not a variable\n");
  13471. script_reportdata(data);
  13472. st->state = END;
  13473. return 1;// not a variable
  13474. }
  13475. id = reference_getid(data);
  13476. start = reference_getindex(data);
  13477. name = reference_getname(data);
  13478. if( not_array_variable(*name) )
  13479. {
  13480. ShowError("script:searchitem: illegal scope\n");
  13481. script_reportdata(data);
  13482. st->state = END;
  13483. return 1;// not supported
  13484. }
  13485. if( not_server_variable(*name) )
  13486. {
  13487. sd = script_rid2sd(st);
  13488. if( sd == NULL )
  13489. return 0;// no player attached
  13490. }
  13491. if( is_string_variable(name) )
  13492. {// string array
  13493. ShowError("script:searchitem: not an integer array reference\n");
  13494. script_reportdata(data);
  13495. st->state = END;
  13496. return 1;// not supported
  13497. }
  13498. for( i = 0; i < count; ++start, ++i )
  13499. {// Set array
  13500. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  13501. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  13502. }
  13503. script_pushint(st, count);
  13504. return 0;
  13505. }
  13506. int axtoi(const char *hexStg)
  13507. {
  13508. int n = 0; // position in string
  13509. int16 m = 0; // position in digit[] to shift
  13510. int count; // loop index
  13511. int intValue = 0; // integer value of hex string
  13512. int digit[11]; // hold values to convert
  13513. while (n < 10) {
  13514. if (hexStg[n]=='\0')
  13515. break;
  13516. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  13517. digit[n] = hexStg[n] & 0x0f; //convert to int
  13518. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  13519. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  13520. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  13521. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  13522. else break;
  13523. n++;
  13524. }
  13525. count = n;
  13526. m = n - 1;
  13527. n = 0;
  13528. while(n < count) {
  13529. // digit[n] is value of hex digit at position n
  13530. // (m << 2) is the number of positions to shift
  13531. // OR the bits into return value
  13532. intValue = intValue | (digit[n] << (m << 2));
  13533. m--; // adjust the position to set
  13534. n++; // next digit to process
  13535. }
  13536. return (intValue);
  13537. }
  13538. // [Lance] Hex string to integer converter
  13539. BUILDIN_FUNC(axtoi)
  13540. {
  13541. const char *hex = script_getstr(st,2);
  13542. script_pushint(st,axtoi(hex));
  13543. return 0;
  13544. }
  13545. // [zBuffer] List of player cont commands --->
  13546. BUILDIN_FUNC(rid2name)
  13547. {
  13548. struct block_list *bl = NULL;
  13549. int rid = script_getnum(st,2);
  13550. if((bl = map_id2bl(rid)))
  13551. {
  13552. switch(bl->type) {
  13553. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  13554. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  13555. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  13556. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  13557. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  13558. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  13559. default:
  13560. ShowError("buildin_rid2name: BL type unknown.\n");
  13561. script_pushconststr(st,"");
  13562. break;
  13563. }
  13564. } else {
  13565. ShowError("buildin_rid2name: invalid RID\n");
  13566. script_pushconststr(st,"(null)");
  13567. }
  13568. return 0;
  13569. }
  13570. BUILDIN_FUNC(pcblockmove)
  13571. {
  13572. int id, flag;
  13573. TBL_PC *sd = NULL;
  13574. id = script_getnum(st,2);
  13575. flag = script_getnum(st,3);
  13576. if(id)
  13577. sd = map_id2sd(id);
  13578. else
  13579. sd = script_rid2sd(st);
  13580. if(sd)
  13581. sd->state.blockedmove = flag > 0;
  13582. return 0;
  13583. }
  13584. BUILDIN_FUNC(pcfollow)
  13585. {
  13586. int id, targetid;
  13587. TBL_PC *sd = NULL;
  13588. id = script_getnum(st,2);
  13589. targetid = script_getnum(st,3);
  13590. if(id)
  13591. sd = map_id2sd(id);
  13592. else
  13593. sd = script_rid2sd(st);
  13594. if(sd)
  13595. pc_follow(sd, targetid);
  13596. return 0;
  13597. }
  13598. BUILDIN_FUNC(pcstopfollow)
  13599. {
  13600. int id;
  13601. TBL_PC *sd = NULL;
  13602. id = script_getnum(st,2);
  13603. if(id)
  13604. sd = map_id2sd(id);
  13605. else
  13606. sd = script_rid2sd(st);
  13607. if(sd)
  13608. pc_stop_following(sd);
  13609. return 0;
  13610. }
  13611. // <--- [zBuffer] List of player cont commands
  13612. // [zBuffer] List of mob control commands --->
  13613. /// Makes the unit walk to target position or map
  13614. /// Returns if it was successfull
  13615. ///
  13616. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  13617. /// unitwalk(<unit_id>,<target_id>) -> <bool>
  13618. BUILDIN_FUNC(unitwalk)
  13619. {
  13620. struct block_list* bl;
  13621. bl = map_id2bl(script_getnum(st,2));
  13622. if( bl == NULL )
  13623. script_pushint(st, 0);
  13624. else if( script_hasdata(st,4) ) {
  13625. int x = script_getnum(st,3);
  13626. int y = script_getnum(st,4);
  13627. if( script_pushint(st, unit_can_reach_pos(bl,x,y,0)) )
  13628. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  13629. } else {
  13630. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  13631. if( tbl == NULL ) {
  13632. ShowError("script:unitwalk: bad target destination\n");
  13633. script_pushint(st, 0);
  13634. return 1;
  13635. }
  13636. else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  13637. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  13638. }
  13639. return 0;
  13640. }
  13641. /// Kills the unit
  13642. ///
  13643. /// unitkill <unit_id>;
  13644. BUILDIN_FUNC(unitkill)
  13645. {
  13646. struct block_list* bl = map_id2bl(script_getnum(st,2));
  13647. if( bl != NULL )
  13648. status_kill(bl);
  13649. return 0;
  13650. }
  13651. /// Warps the unit to the target position in the target map
  13652. /// Returns if it was successfull
  13653. ///
  13654. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  13655. BUILDIN_FUNC(unitwarp)
  13656. {
  13657. int unit_id;
  13658. int map;
  13659. short x;
  13660. short y;
  13661. struct block_list* bl;
  13662. const char *mapname;
  13663. unit_id = script_getnum(st,2);
  13664. mapname = script_getstr(st, 3);
  13665. x = (short)script_getnum(st,4);
  13666. y = (short)script_getnum(st,5);
  13667. if (!unit_id) //Warp the script's runner
  13668. bl = map_id2bl(st->rid);
  13669. else
  13670. bl = map_id2bl(unit_id);
  13671. if( strcmp(mapname,"this") == 0 )
  13672. map = bl?bl->m:-1;
  13673. else
  13674. map = map_mapname2mapid(mapname);
  13675. if( map >= 0 && bl != NULL )
  13676. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  13677. else
  13678. script_pushint(st, 0);
  13679. return 0;
  13680. }
  13681. /// Makes the unit attack the target.
  13682. /// If the unit is a player and <action type> is not 0, it does a continuous
  13683. /// attack instead of a single attack.
  13684. /// Returns if the request was successful.
  13685. ///
  13686. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  13687. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  13688. BUILDIN_FUNC(unitattack)
  13689. {
  13690. struct block_list* unit_bl;
  13691. struct block_list* target_bl = NULL;
  13692. struct script_data* data;
  13693. int actiontype = 0;
  13694. // get unit
  13695. unit_bl = map_id2bl(script_getnum(st,2));
  13696. if( unit_bl == NULL ) {
  13697. script_pushint(st, 0);
  13698. return 0;
  13699. }
  13700. data = script_getdata(st, 3);
  13701. get_val(st, data);
  13702. if( data_isstring(data) )
  13703. {
  13704. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  13705. if( sd != NULL )
  13706. target_bl = &sd->bl;
  13707. } else
  13708. target_bl = map_id2bl(conv_num(st, data));
  13709. // request the attack
  13710. if( target_bl == NULL )
  13711. {
  13712. script_pushint(st, 0);
  13713. return 0;
  13714. }
  13715. // get actiontype
  13716. if( script_hasdata(st,4) )
  13717. actiontype = script_getnum(st,4);
  13718. switch( unit_bl->type )
  13719. {
  13720. case BL_PC:
  13721. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  13722. script_pushint(st, 1);
  13723. return 0;
  13724. case BL_MOB:
  13725. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  13726. break;
  13727. case BL_PET:
  13728. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  13729. break;
  13730. default:
  13731. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  13732. script_pushint(st, 0);
  13733. return 1;
  13734. }
  13735. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  13736. return 0;
  13737. }
  13738. /// Makes the unit stop attacking and moving
  13739. ///
  13740. /// unitstop <unit_id>;
  13741. BUILDIN_FUNC(unitstop)
  13742. {
  13743. int unit_id;
  13744. struct block_list* bl;
  13745. unit_id = script_getnum(st,2);
  13746. bl = map_id2bl(unit_id);
  13747. if( bl != NULL )
  13748. {
  13749. unit_stop_attack(bl);
  13750. unit_stop_walking(bl,4);
  13751. if( bl->type == BL_MOB )
  13752. ((TBL_MOB*)bl)->target_id = 0;
  13753. }
  13754. return 0;
  13755. }
  13756. /// Makes the unit say the message
  13757. ///
  13758. /// unittalk <unit_id>,"<message>";
  13759. BUILDIN_FUNC(unittalk)
  13760. {
  13761. int unit_id;
  13762. const char* message;
  13763. struct block_list* bl;
  13764. unit_id = script_getnum(st,2);
  13765. message = script_getstr(st, 3);
  13766. bl = map_id2bl(unit_id);
  13767. if( bl != NULL )
  13768. {
  13769. struct StringBuf sbuf;
  13770. StringBuf_Init(&sbuf);
  13771. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  13772. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  13773. StringBuf_Destroy(&sbuf);
  13774. }
  13775. return 0;
  13776. }
  13777. /// Makes the unit do an emotion
  13778. ///
  13779. /// unitemote <unit_id>,<emotion>;
  13780. ///
  13781. /// @see e_* in const.txt
  13782. BUILDIN_FUNC(unitemote)
  13783. {
  13784. int unit_id;
  13785. int emotion;
  13786. struct block_list* bl;
  13787. unit_id = script_getnum(st,2);
  13788. emotion = script_getnum(st,3);
  13789. bl = map_id2bl(unit_id);
  13790. if( bl != NULL )
  13791. clif_emotion(bl, emotion);
  13792. return 0;
  13793. }
  13794. /// Makes the unit cast the skill on the target or self if no target is specified
  13795. ///
  13796. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  13797. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  13798. BUILDIN_FUNC(unitskilluseid)
  13799. {
  13800. int unit_id;
  13801. uint16 skill_id;
  13802. uint16 skill_lv;
  13803. int target_id;
  13804. struct block_list* bl;
  13805. unit_id = script_getnum(st,2);
  13806. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13807. skill_lv = script_getnum(st,4);
  13808. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  13809. bl = map_id2bl(unit_id);
  13810. if( bl != NULL )
  13811. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  13812. return 0;
  13813. }
  13814. /// Makes the unit cast the skill on the target position.
  13815. ///
  13816. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  13817. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  13818. BUILDIN_FUNC(unitskillusepos)
  13819. {
  13820. int unit_id;
  13821. uint16 skill_id;
  13822. uint16 skill_lv;
  13823. int skill_x;
  13824. int skill_y;
  13825. struct block_list* bl;
  13826. unit_id = script_getnum(st,2);
  13827. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13828. skill_lv = script_getnum(st,4);
  13829. skill_x = script_getnum(st,5);
  13830. skill_y = script_getnum(st,6);
  13831. bl = map_id2bl(unit_id);
  13832. if( bl != NULL )
  13833. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  13834. return 0;
  13835. }
  13836. // <--- [zBuffer] List of mob control commands
  13837. /// Pauses the execution of the script, detaching the player
  13838. ///
  13839. /// sleep <mili seconds>;
  13840. BUILDIN_FUNC(sleep)
  13841. {
  13842. int ticks;
  13843. ticks = script_getnum(st,2);
  13844. // detach the player
  13845. script_detach_rid(st);
  13846. if( ticks <= 0 )
  13847. {// do nothing
  13848. }
  13849. else if( st->sleep.tick == 0 )
  13850. {// sleep for the target amount of time
  13851. st->state = RERUNLINE;
  13852. st->sleep.tick = ticks;
  13853. }
  13854. else
  13855. {// sleep time is over
  13856. st->state = RUN;
  13857. st->sleep.tick = 0;
  13858. }
  13859. return 0;
  13860. }
  13861. /// Pauses the execution of the script, keeping the player attached
  13862. /// Returns if a player is still attached
  13863. ///
  13864. /// sleep2(<mili secconds>) -> <bool>
  13865. BUILDIN_FUNC(sleep2)
  13866. {
  13867. int ticks;
  13868. ticks = script_getnum(st,2);
  13869. if( ticks <= 0 )
  13870. {// do nothing
  13871. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13872. }
  13873. else if( !st->sleep.tick )
  13874. {// sleep for the target amount of time
  13875. st->state = RERUNLINE;
  13876. st->sleep.tick = ticks;
  13877. }
  13878. else
  13879. {// sleep time is over
  13880. st->state = RUN;
  13881. st->sleep.tick = 0;
  13882. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13883. }
  13884. return 0;
  13885. }
  13886. /// Awakes all the sleep timers of the target npc
  13887. ///
  13888. /// awake "<npc name>";
  13889. BUILDIN_FUNC(awake)
  13890. {
  13891. struct npc_data* nd;
  13892. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  13893. nd = npc_name2id(script_getstr(st, 2));
  13894. if( nd == NULL ) {
  13895. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  13896. return 1;
  13897. }
  13898. while( node )
  13899. {
  13900. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  13901. {// sleep timer for the npc
  13902. struct script_state* tst = (struct script_state*)node->data;
  13903. TBL_PC* sd = map_id2sd(tst->rid);
  13904. if( tst->sleep.timer == INVALID_TIMER )
  13905. {// already awake ???
  13906. node = node->next;
  13907. continue;
  13908. }
  13909. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  13910. {// char not online anymore / another char of the same account is online - Cancel execution
  13911. tst->state = END;
  13912. tst->rid = 0;
  13913. }
  13914. delete_timer(tst->sleep.timer, run_script_timer);
  13915. node = script_erase_sleepdb(node);
  13916. tst->sleep.timer = INVALID_TIMER;
  13917. if(tst->state != RERUNLINE)
  13918. tst->sleep.tick = 0;
  13919. run_script_main(tst);
  13920. }
  13921. else
  13922. {
  13923. node = node->next;
  13924. }
  13925. }
  13926. return 0;
  13927. }
  13928. /// Returns a reference to a variable of the target NPC.
  13929. /// Returns 0 if an error occurs.
  13930. ///
  13931. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  13932. BUILDIN_FUNC(getvariableofnpc)
  13933. {
  13934. struct script_data* data;
  13935. const char* name;
  13936. struct npc_data* nd;
  13937. data = script_getdata(st,2);
  13938. if( !data_isreference(data) )
  13939. {// Not a reference (aka varaible name)
  13940. ShowError("script:getvariableofnpc: not a variable\n");
  13941. script_reportdata(data);
  13942. script_pushnil(st);
  13943. st->state = END;
  13944. return 1;
  13945. }
  13946. name = reference_getname(data);
  13947. if( *name != '.' || name[1] == '@' )
  13948. {// not a npc variable
  13949. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  13950. script_reportdata(data);
  13951. script_pushnil(st);
  13952. st->state = END;
  13953. return 1;
  13954. }
  13955. nd = npc_name2id(script_getstr(st,3));
  13956. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  13957. {// NPC not found or has no script
  13958. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  13959. script_pushnil(st);
  13960. st->state = END;
  13961. return 1;
  13962. }
  13963. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  13964. return 0;
  13965. }
  13966. /// Opens a warp portal.
  13967. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  13968. ///
  13969. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  13970. ///
  13971. /// @author blackhole89
  13972. BUILDIN_FUNC(warpportal)
  13973. {
  13974. int spx;
  13975. int spy;
  13976. unsigned short mapindex;
  13977. int tpx;
  13978. int tpy;
  13979. struct skill_unit_group* group;
  13980. struct block_list* bl;
  13981. bl = map_id2bl(st->oid);
  13982. if( bl == NULL )
  13983. {
  13984. ShowError("script:warpportal: npc is needed\n");
  13985. return 1;
  13986. }
  13987. spx = script_getnum(st,2);
  13988. spy = script_getnum(st,3);
  13989. mapindex = mapindex_name2id(script_getstr(st, 4));
  13990. tpx = script_getnum(st,5);
  13991. tpy = script_getnum(st,6);
  13992. if( mapindex == 0 )
  13993. return 0;// map not found
  13994. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  13995. if( group == NULL )
  13996. return 0;// failed
  13997. group->val1 = (group->val1<<16)|(short)0;
  13998. group->val2 = (tpx<<16) | tpy;
  13999. group->val3 = mapindex;
  14000. return 0;
  14001. }
  14002. BUILDIN_FUNC(openmail)
  14003. {
  14004. TBL_PC* sd;
  14005. sd = script_rid2sd(st);
  14006. if( sd == NULL )
  14007. return 0;
  14008. mail_openmail(sd);
  14009. return 0;
  14010. }
  14011. BUILDIN_FUNC(openauction)
  14012. {
  14013. TBL_PC* sd;
  14014. sd = script_rid2sd(st);
  14015. if( sd == NULL )
  14016. return 0;
  14017. clif_Auction_openwindow(sd);
  14018. return 0;
  14019. }
  14020. /// Retrieves the value of the specified flag of the specified cell.
  14021. ///
  14022. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  14023. ///
  14024. /// @see cell_chk* constants in const.txt for the types
  14025. BUILDIN_FUNC(checkcell)
  14026. {
  14027. int16 m = map_mapname2mapid(script_getstr(st,2));
  14028. int16 x = script_getnum(st,3);
  14029. int16 y = script_getnum(st,4);
  14030. cell_chk type = (cell_chk)script_getnum(st,5);
  14031. script_pushint(st, map_getcell(m, x, y, type));
  14032. return 0;
  14033. }
  14034. /// Modifies flags of cells in the specified area.
  14035. ///
  14036. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  14037. ///
  14038. /// @see cell_* constants in const.txt for the types
  14039. BUILDIN_FUNC(setcell)
  14040. {
  14041. int16 m = map_mapname2mapid(script_getstr(st,2));
  14042. int16 x1 = script_getnum(st,3);
  14043. int16 y1 = script_getnum(st,4);
  14044. int16 x2 = script_getnum(st,5);
  14045. int16 y2 = script_getnum(st,6);
  14046. cell_t type = (cell_t)script_getnum(st,7);
  14047. bool flag = (bool)script_getnum(st,8);
  14048. int x,y;
  14049. if( x1 > x2 ) swap(x1,x2);
  14050. if( y1 > y2 ) swap(y1,y2);
  14051. for( y = y1; y <= y2; ++y )
  14052. for( x = x1; x <= x2; ++x )
  14053. map_setcell(m, x, y, type, flag);
  14054. return 0;
  14055. }
  14056. /*==========================================
  14057. * Mercenary Commands
  14058. *------------------------------------------*/
  14059. BUILDIN_FUNC(mercenary_create)
  14060. {
  14061. struct map_session_data *sd;
  14062. int class_, contract_time;
  14063. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  14064. return 0;
  14065. class_ = script_getnum(st,2);
  14066. if( !merc_class(class_) )
  14067. return 0;
  14068. contract_time = script_getnum(st,3);
  14069. merc_create(sd, class_, contract_time);
  14070. return 0;
  14071. }
  14072. BUILDIN_FUNC(mercenary_heal)
  14073. {
  14074. struct map_session_data *sd = script_rid2sd(st);
  14075. int hp, sp;
  14076. if( sd == NULL || sd->md == NULL )
  14077. return 0;
  14078. hp = script_getnum(st,2);
  14079. sp = script_getnum(st,3);
  14080. status_heal(&sd->md->bl, hp, sp, 0);
  14081. return 0;
  14082. }
  14083. BUILDIN_FUNC(mercenary_sc_start)
  14084. {
  14085. struct map_session_data *sd = script_rid2sd(st);
  14086. enum sc_type type;
  14087. int tick, val1;
  14088. if( sd == NULL || sd->md == NULL )
  14089. return 0;
  14090. type = (sc_type)script_getnum(st,2);
  14091. tick = script_getnum(st,3);
  14092. val1 = script_getnum(st,4);
  14093. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  14094. return 0;
  14095. }
  14096. BUILDIN_FUNC(mercenary_get_calls)
  14097. {
  14098. struct map_session_data *sd = script_rid2sd(st);
  14099. int guild;
  14100. if( sd == NULL )
  14101. return 0;
  14102. guild = script_getnum(st,2);
  14103. switch( guild )
  14104. {
  14105. case ARCH_MERC_GUILD:
  14106. script_pushint(st,sd->status.arch_calls);
  14107. break;
  14108. case SPEAR_MERC_GUILD:
  14109. script_pushint(st,sd->status.spear_calls);
  14110. break;
  14111. case SWORD_MERC_GUILD:
  14112. script_pushint(st,sd->status.sword_calls);
  14113. break;
  14114. default:
  14115. script_pushint(st,0);
  14116. break;
  14117. }
  14118. return 0;
  14119. }
  14120. BUILDIN_FUNC(mercenary_set_calls)
  14121. {
  14122. struct map_session_data *sd = script_rid2sd(st);
  14123. int guild, value, *calls;
  14124. if( sd == NULL )
  14125. return 0;
  14126. guild = script_getnum(st,2);
  14127. value = script_getnum(st,3);
  14128. switch( guild )
  14129. {
  14130. case ARCH_MERC_GUILD:
  14131. calls = &sd->status.arch_calls;
  14132. break;
  14133. case SPEAR_MERC_GUILD:
  14134. calls = &sd->status.spear_calls;
  14135. break;
  14136. case SWORD_MERC_GUILD:
  14137. calls = &sd->status.sword_calls;
  14138. break;
  14139. default:
  14140. return 0; // Invalid Guild
  14141. }
  14142. *calls += value;
  14143. *calls = cap_value(*calls, 0, INT_MAX);
  14144. return 0;
  14145. }
  14146. BUILDIN_FUNC(mercenary_get_faith)
  14147. {
  14148. struct map_session_data *sd = script_rid2sd(st);
  14149. int guild;
  14150. if( sd == NULL )
  14151. return 0;
  14152. guild = script_getnum(st,2);
  14153. switch( guild )
  14154. {
  14155. case ARCH_MERC_GUILD:
  14156. script_pushint(st,sd->status.arch_faith);
  14157. break;
  14158. case SPEAR_MERC_GUILD:
  14159. script_pushint(st,sd->status.spear_faith);
  14160. break;
  14161. case SWORD_MERC_GUILD:
  14162. script_pushint(st,sd->status.sword_faith);
  14163. break;
  14164. default:
  14165. script_pushint(st,0);
  14166. break;
  14167. }
  14168. return 0;
  14169. }
  14170. BUILDIN_FUNC(mercenary_set_faith)
  14171. {
  14172. struct map_session_data *sd = script_rid2sd(st);
  14173. int guild, value, *calls;
  14174. if( sd == NULL )
  14175. return 0;
  14176. guild = script_getnum(st,2);
  14177. value = script_getnum(st,3);
  14178. switch( guild )
  14179. {
  14180. case ARCH_MERC_GUILD:
  14181. calls = &sd->status.arch_faith;
  14182. break;
  14183. case SPEAR_MERC_GUILD:
  14184. calls = &sd->status.spear_faith;
  14185. break;
  14186. case SWORD_MERC_GUILD:
  14187. calls = &sd->status.sword_faith;
  14188. break;
  14189. default:
  14190. return 0; // Invalid Guild
  14191. }
  14192. *calls += value;
  14193. *calls = cap_value(*calls, 0, INT_MAX);
  14194. if( mercenary_get_guild(sd->md) == guild )
  14195. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  14196. return 0;
  14197. }
  14198. /*------------------------------------------
  14199. * Book Reading
  14200. *------------------------------------------*/
  14201. BUILDIN_FUNC(readbook)
  14202. {
  14203. struct map_session_data *sd;
  14204. int book_id, page;
  14205. if( (sd = script_rid2sd(st)) == NULL )
  14206. return 0;
  14207. book_id = script_getnum(st,2);
  14208. page = script_getnum(st,3);
  14209. clif_readbook(sd->fd, book_id, page);
  14210. return 0;
  14211. }
  14212. /******************
  14213. Questlog script commands
  14214. *******************/
  14215. BUILDIN_FUNC(setquest)
  14216. {
  14217. struct map_session_data *sd = script_rid2sd(st);
  14218. nullpo_ret(sd);
  14219. quest_add(sd, script_getnum(st, 2));
  14220. return 0;
  14221. }
  14222. BUILDIN_FUNC(erasequest)
  14223. {
  14224. struct map_session_data *sd = script_rid2sd(st);
  14225. nullpo_ret(sd);
  14226. quest_delete(sd, script_getnum(st, 2));
  14227. return 0;
  14228. }
  14229. BUILDIN_FUNC(completequest)
  14230. {
  14231. struct map_session_data *sd = script_rid2sd(st);
  14232. nullpo_ret(sd);
  14233. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  14234. return 0;
  14235. }
  14236. BUILDIN_FUNC(changequest)
  14237. {
  14238. struct map_session_data *sd = script_rid2sd(st);
  14239. nullpo_ret(sd);
  14240. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  14241. return 0;
  14242. }
  14243. BUILDIN_FUNC(checkquest)
  14244. {
  14245. struct map_session_data *sd = script_rid2sd(st);
  14246. quest_check_type type = HAVEQUEST;
  14247. nullpo_ret(sd);
  14248. if( script_hasdata(st, 3) )
  14249. type = (quest_check_type)script_getnum(st, 3);
  14250. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  14251. return 0;
  14252. }
  14253. BUILDIN_FUNC(showevent)
  14254. {
  14255. TBL_PC *sd = script_rid2sd(st);
  14256. struct npc_data *nd = map_id2nd(st->oid);
  14257. int state, color;
  14258. if( sd == NULL || nd == NULL )
  14259. return 0;
  14260. state = script_getnum(st, 2);
  14261. color = script_getnum(st, 3);
  14262. if( color < 0 || color > 3 )
  14263. color = 0; // set default color
  14264. clif_quest_show_event(sd, &nd->bl, state, color);
  14265. return 0;
  14266. }
  14267. /*==========================================
  14268. * BattleGround System
  14269. *------------------------------------------*/
  14270. BUILDIN_FUNC(waitingroom2bg)
  14271. {
  14272. struct npc_data *nd;
  14273. struct chat_data *cd;
  14274. const char *map_name, *ev = "", *dev = "";
  14275. int x, y, i, mapindex = 0, bg_id, n;
  14276. struct map_session_data *sd;
  14277. if( script_hasdata(st,7) )
  14278. nd = npc_name2id(script_getstr(st,7));
  14279. else
  14280. nd = (struct npc_data *)map_id2bl(st->oid);
  14281. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  14282. {
  14283. script_pushint(st,0);
  14284. return 0;
  14285. }
  14286. map_name = script_getstr(st,2);
  14287. if( strcmp(map_name,"-") != 0 )
  14288. {
  14289. mapindex = mapindex_name2id(map_name);
  14290. if( mapindex == 0 )
  14291. { // Invalid Map
  14292. script_pushint(st,0);
  14293. return 0;
  14294. }
  14295. }
  14296. x = script_getnum(st,3);
  14297. y = script_getnum(st,4);
  14298. ev = script_getstr(st,5); // Logout Event
  14299. dev = script_getstr(st,6); // Die Event
  14300. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  14301. { // Creation failed
  14302. script_pushint(st,0);
  14303. return 0;
  14304. }
  14305. n = cd->users;
  14306. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  14307. {
  14308. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  14309. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  14310. else
  14311. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  14312. }
  14313. mapreg_setreg(add_str("$@arenamembersnum"), i);
  14314. script_pushint(st,bg_id);
  14315. return 0;
  14316. }
  14317. BUILDIN_FUNC(waitingroom2bg_single)
  14318. {
  14319. const char* map_name;
  14320. struct npc_data *nd;
  14321. struct chat_data *cd;
  14322. struct map_session_data *sd;
  14323. int x, y, mapindex, bg_id;
  14324. bg_id = script_getnum(st,2);
  14325. map_name = script_getstr(st,3);
  14326. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14327. return 0; // Invalid Map
  14328. x = script_getnum(st,4);
  14329. y = script_getnum(st,5);
  14330. nd = npc_name2id(script_getstr(st,6));
  14331. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  14332. return 0;
  14333. if( (sd = cd->usersd[0]) == NULL )
  14334. return 0;
  14335. if( bg_team_join(bg_id, sd) )
  14336. {
  14337. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  14338. script_pushint(st,1);
  14339. }
  14340. else
  14341. script_pushint(st,0);
  14342. return 0;
  14343. }
  14344. BUILDIN_FUNC(bg_team_setxy)
  14345. {
  14346. struct battleground_data *bg;
  14347. int bg_id;
  14348. bg_id = script_getnum(st,2);
  14349. if( (bg = bg_team_search(bg_id)) == NULL )
  14350. return 0;
  14351. bg->x = script_getnum(st,3);
  14352. bg->y = script_getnum(st,4);
  14353. return 0;
  14354. }
  14355. BUILDIN_FUNC(bg_warp)
  14356. {
  14357. int x, y, mapindex, bg_id;
  14358. const char* map_name;
  14359. bg_id = script_getnum(st,2);
  14360. map_name = script_getstr(st,3);
  14361. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14362. return 0; // Invalid Map
  14363. x = script_getnum(st,4);
  14364. y = script_getnum(st,5);
  14365. bg_team_warp(bg_id, mapindex, x, y);
  14366. return 0;
  14367. }
  14368. BUILDIN_FUNC(bg_monster)
  14369. {
  14370. int class_ = 0, x = 0, y = 0, bg_id = 0;
  14371. const char *str,*map, *evt="";
  14372. bg_id = script_getnum(st,2);
  14373. map = script_getstr(st,3);
  14374. x = script_getnum(st,4);
  14375. y = script_getnum(st,5);
  14376. str = script_getstr(st,6);
  14377. class_ = script_getnum(st,7);
  14378. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  14379. check_event(st, evt);
  14380. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  14381. return 0;
  14382. }
  14383. BUILDIN_FUNC(bg_monster_set_team)
  14384. {
  14385. struct mob_data *md;
  14386. struct block_list *mbl;
  14387. int id = script_getnum(st,2),
  14388. bg_id = script_getnum(st,3);
  14389. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  14390. return 0;
  14391. md = (TBL_MOB *)mbl;
  14392. md->bg_id = bg_id;
  14393. mob_stop_attack(md);
  14394. mob_stop_walking(md, 0);
  14395. md->target_id = md->attacked_id = 0;
  14396. clif_charnameack(0, &md->bl);
  14397. return 0;
  14398. }
  14399. BUILDIN_FUNC(bg_leave)
  14400. {
  14401. struct map_session_data *sd = script_rid2sd(st);
  14402. if( sd == NULL || !sd->bg_id )
  14403. return 0;
  14404. bg_team_leave(sd,0);
  14405. return 0;
  14406. }
  14407. BUILDIN_FUNC(bg_destroy)
  14408. {
  14409. int bg_id = script_getnum(st,2);
  14410. bg_team_delete(bg_id);
  14411. return 0;
  14412. }
  14413. BUILDIN_FUNC(bg_getareausers)
  14414. {
  14415. const char *str;
  14416. int16 m, x0, y0, x1, y1;
  14417. int bg_id;
  14418. int i = 0, c = 0;
  14419. struct battleground_data *bg = NULL;
  14420. struct map_session_data *sd;
  14421. bg_id = script_getnum(st,2);
  14422. str = script_getstr(st,3);
  14423. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  14424. {
  14425. script_pushint(st,0);
  14426. return 0;
  14427. }
  14428. x0 = script_getnum(st,4);
  14429. y0 = script_getnum(st,5);
  14430. x1 = script_getnum(st,6);
  14431. y1 = script_getnum(st,7);
  14432. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  14433. {
  14434. if( (sd = bg->members[i].sd) == NULL )
  14435. continue;
  14436. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  14437. continue;
  14438. c++;
  14439. }
  14440. script_pushint(st,c);
  14441. return 0;
  14442. }
  14443. BUILDIN_FUNC(bg_updatescore)
  14444. {
  14445. const char *str;
  14446. int16 m;
  14447. str = script_getstr(st,2);
  14448. if( (m = map_mapname2mapid(str)) < 0 )
  14449. return 0;
  14450. map[m].bgscore_lion = script_getnum(st,3);
  14451. map[m].bgscore_eagle = script_getnum(st,4);
  14452. clif_bg_updatescore(m);
  14453. return 0;
  14454. }
  14455. BUILDIN_FUNC(bg_get_data)
  14456. {
  14457. struct battleground_data *bg;
  14458. int bg_id = script_getnum(st,2),
  14459. type = script_getnum(st,3);
  14460. if( (bg = bg_team_search(bg_id)) == NULL )
  14461. {
  14462. script_pushint(st,0);
  14463. return 0;
  14464. }
  14465. switch( type )
  14466. {
  14467. case 0: script_pushint(st, bg->count); break;
  14468. default:
  14469. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  14470. break;
  14471. }
  14472. return 0;
  14473. }
  14474. /*==========================================
  14475. * Instancing System
  14476. *------------------------------------------*/
  14477. //Returns an Instance ID
  14478. //Checks NPC first, then if player is attached we check party
  14479. int script_instancegetid(struct script_state* st)
  14480. {
  14481. short instance_id = 0;
  14482. struct npc_data *nd;
  14483. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  14484. instance_id = nd->instance_id;
  14485. else {
  14486. struct map_session_data *sd;
  14487. struct party_data *p;
  14488. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14489. instance_id = p->instance_id;
  14490. }
  14491. return instance_id;
  14492. }
  14493. /*==========================================
  14494. * Creates the instance
  14495. * Returns Instance ID if created successfully
  14496. *------------------------------------------*/
  14497. BUILDIN_FUNC(instance_create)
  14498. {
  14499. struct map_session_data *sd;
  14500. if((sd = script_rid2sd(st)) == NULL)
  14501. return -1;
  14502. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  14503. return 0;
  14504. }
  14505. /*==========================================
  14506. * Destroys an instance (unofficial)
  14507. * Officially instances are only destroyed by timeout
  14508. *
  14509. * instance_destroy {<instance_id>};
  14510. *------------------------------------------*/
  14511. BUILDIN_FUNC(instance_destroy)
  14512. {
  14513. short instance_id;
  14514. if( script_hasdata(st,2) )
  14515. instance_id = script_getnum(st,2);
  14516. else
  14517. instance_id = script_instancegetid(st);
  14518. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  14519. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  14520. return 0;
  14521. }
  14522. instance_destroy(instance_id);
  14523. return 0;
  14524. }
  14525. /*==========================================
  14526. * Warps player to instance
  14527. * Results:
  14528. * 0: Success
  14529. * 1: Character not in party
  14530. * 2: Party doesn't have instance
  14531. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  14532. *------------------------------------------*/
  14533. BUILDIN_FUNC(instance_enter)
  14534. {
  14535. struct map_session_data *sd;
  14536. if((sd = script_rid2sd(st)) != NULL)
  14537. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  14538. else
  14539. return 1;
  14540. return 0;
  14541. }
  14542. /*==========================================
  14543. * Returns the name of a duplicated NPC
  14544. *
  14545. * instance_npcname <npc_name>{,<instance_id>};
  14546. * <npc_name> is the full name of an NPC.
  14547. *------------------------------------------*/
  14548. BUILDIN_FUNC(instance_npcname)
  14549. {
  14550. const char *str;
  14551. short instance_id = 0;
  14552. struct npc_data *nd;
  14553. str = script_getstr(st,2);
  14554. if( script_hasdata(st,3) )
  14555. instance_id = script_getnum(st,3);
  14556. else
  14557. instance_id = script_instancegetid(st);
  14558. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  14559. static char npcname[NAME_LENGTH];
  14560. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  14561. script_pushconststr(st,npcname);
  14562. } else {
  14563. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  14564. st->state = END;
  14565. return 1;
  14566. }
  14567. return 0;
  14568. }
  14569. /*==========================================
  14570. * Returns the name of a duplicated map
  14571. *
  14572. * instance_mapname <map_name>{,<instance_id>};
  14573. *------------------------------------------*/
  14574. BUILDIN_FUNC(instance_mapname)
  14575. {
  14576. const char *str;
  14577. int16 m;
  14578. short instance_id = 0;
  14579. str = script_getstr(st,2);
  14580. if( script_hasdata(st,3) )
  14581. instance_id = script_getnum(st,3);
  14582. else
  14583. instance_id = script_instancegetid(st);
  14584. // Check that instance mapname is a valid map
  14585. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  14586. script_pushconststr(st, "");
  14587. else
  14588. script_pushconststr(st, map[m].name);
  14589. return 0;
  14590. }
  14591. /*==========================================
  14592. * Returns an Instance ID
  14593. *------------------------------------------*/
  14594. BUILDIN_FUNC(instance_id)
  14595. {
  14596. short instance_id;
  14597. instance_id = script_instancegetid(st);
  14598. if(!instance_id) {
  14599. //ShowError("script:instance_id: No instance attached to NPC or player");
  14600. script_pushint(st, 0);
  14601. return 1;
  14602. }
  14603. script_pushint(st, instance_id);
  14604. return 0;
  14605. }
  14606. /*==========================================
  14607. * Warps all players inside an instance
  14608. *
  14609. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  14610. *------------------------------------------*/
  14611. BUILDIN_FUNC(instance_warpall)
  14612. {
  14613. struct party_data *p;
  14614. struct map_session_data *pl_sd;
  14615. int16 m, i;
  14616. short instance_id;
  14617. const char *mapn;
  14618. int x, y;
  14619. mapn = script_getstr(st,2);
  14620. x = script_getnum(st,3);
  14621. y = script_getnum(st,4);
  14622. if( script_hasdata(st,5) )
  14623. instance_id = script_getnum(st,5);
  14624. else
  14625. instance_id = script_instancegetid(st);
  14626. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  14627. return 0;
  14628. if( !(p = party_search(instance_data[instance_id].party_id)) )
  14629. return 0;
  14630. for( i = 0; i < MAX_PARTY; i++ )
  14631. 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);
  14632. return 0;
  14633. }
  14634. /*==========================================
  14635. * Broadcasts to all maps inside an instance
  14636. *
  14637. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  14638. * Using -1 for <instance id> will auto-detect the id.
  14639. *------------------------------------------*/
  14640. BUILDIN_FUNC(instance_announce) {
  14641. int instance_id = script_getnum(st,2);
  14642. const char *mes = script_getstr(st,3);
  14643. int flag = script_getnum(st,4);
  14644. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  14645. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  14646. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  14647. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  14648. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  14649. int i;
  14650. if( instance_id == -1 ) {
  14651. instance_id = script_instancegetid(st);
  14652. }
  14653. if( !instance_id && &instance_data[instance_id] != NULL)
  14654. return true;
  14655. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  14656. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  14657. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  14658. return true;
  14659. }
  14660. /*==========================================
  14661. * instance_check_party [malufett]
  14662. * Values:
  14663. * party_id : Party ID of the invoking character. [Required Parameter]
  14664. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  14665. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  14666. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  14667. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  14668. * Example 2: instance_check_party (getcharid(1),1,1,99);
  14669. *------------------------------------------*/
  14670. BUILDIN_FUNC(instance_check_party)
  14671. {
  14672. struct map_session_data *pl_sd;
  14673. int amount, min, max, i, party_id, c = 0;
  14674. struct party_data *p = NULL;
  14675. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  14676. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  14677. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  14678. if( min < 1 || min > MAX_LEVEL) {
  14679. ShowError("script:check_party: Invalid min level, %d\n", min);
  14680. return 1;
  14681. } else if( max < 1 || max > MAX_LEVEL) {
  14682. ShowError("script:check_party: Invalid max level, %d\n", max);
  14683. return 1;
  14684. }
  14685. if( script_hasdata(st,2) )
  14686. party_id = script_getnum(st,2);
  14687. else return 0;
  14688. if( !(p = party_search(party_id)) ) {
  14689. script_pushint(st, 0); // Returns false if party does not exist.
  14690. return 0;
  14691. }
  14692. for( i = 0; i < MAX_PARTY; i++ )
  14693. if( (pl_sd = p->data[i].sd) )
  14694. if(map_id2bl(pl_sd->bl.id)) {
  14695. if(pl_sd->status.base_level < min) {
  14696. script_pushint(st, 0);
  14697. return 0;
  14698. } else if(pl_sd->status.base_level > max) {
  14699. script_pushint(st, 0);
  14700. return 0;
  14701. }
  14702. c++;
  14703. }
  14704. if(c < amount)
  14705. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  14706. else
  14707. script_pushint(st, 1);
  14708. return 0;
  14709. }
  14710. /*==========================================
  14711. * Custom Fonts
  14712. *------------------------------------------*/
  14713. BUILDIN_FUNC(setfont)
  14714. {
  14715. struct map_session_data *sd = script_rid2sd(st);
  14716. int font = script_getnum(st,2);
  14717. if( sd == NULL )
  14718. return 0;
  14719. if( sd->user_font != font )
  14720. sd->user_font = font;
  14721. else
  14722. sd->user_font = 0;
  14723. clif_font(sd);
  14724. return 0;
  14725. }
  14726. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  14727. {
  14728. TBL_MOB* md = (TBL_MOB*)bl;
  14729. struct block_list *tbl;
  14730. int mobid = va_arg(ap,int);
  14731. uint16 skill_id = va_arg(ap,int);
  14732. uint16 skill_lv = va_arg(ap,int);
  14733. int casttime = va_arg(ap,int);
  14734. int cancel = va_arg(ap,int);
  14735. int emotion = va_arg(ap,int);
  14736. int target = va_arg(ap,int);
  14737. if( md->class_ != mobid )
  14738. return 0;
  14739. // 0:self, 1:target, 2:master, default:random
  14740. switch( target )
  14741. {
  14742. case 0: tbl = map_id2bl(md->bl.id); break;
  14743. case 1: tbl = map_id2bl(md->target_id); break;
  14744. case 2: tbl = map_id2bl(md->master_id); break;
  14745. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  14746. }
  14747. if( !tbl )
  14748. return 0;
  14749. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  14750. unit_skillcastcancel(bl,0);
  14751. if( skill_get_casttype(skill_id) == CAST_GROUND )
  14752. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  14753. else
  14754. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  14755. clif_emotion(&md->bl, emotion);
  14756. return 0;
  14757. }
  14758. /*==========================================
  14759. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  14760. *------------------------------------------*/
  14761. BUILDIN_FUNC(areamobuseskill)
  14762. {
  14763. struct block_list center;
  14764. int16 m;
  14765. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  14766. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  14767. ShowError("areamobuseskill: invalid map name.\n");
  14768. return 0;
  14769. }
  14770. center.m = m;
  14771. center.x = script_getnum(st,3);
  14772. center.y = script_getnum(st,4);
  14773. range = script_getnum(st,5);
  14774. mobid = script_getnum(st,6);
  14775. skill_id = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  14776. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  14777. skill_lv = battle_config.mob_max_skilllvl;
  14778. casttime = script_getnum(st,9);
  14779. cancel = script_getnum(st,10);
  14780. emotion = script_getnum(st,11);
  14781. target = script_getnum(st,12);
  14782. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  14783. return 0;
  14784. }
  14785. BUILDIN_FUNC(progressbar)
  14786. {
  14787. struct map_session_data * sd = script_rid2sd(st);
  14788. const char * color;
  14789. unsigned int second;
  14790. if( !st || !sd )
  14791. return 0;
  14792. st->state = STOP;
  14793. color = script_getstr(st,2);
  14794. second = script_getnum(st,3);
  14795. sd->progressbar.npc_id = st->oid;
  14796. sd->progressbar.timeout = gettick() + second*1000;
  14797. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  14798. return 0;
  14799. }
  14800. BUILDIN_FUNC(pushpc)
  14801. {
  14802. uint8 dir;
  14803. int cells, dx, dy;
  14804. struct map_session_data* sd;
  14805. if((sd = script_rid2sd(st))==NULL)
  14806. {
  14807. return 0;
  14808. }
  14809. dir = script_getnum(st,2);
  14810. cells = script_getnum(st,3);
  14811. if(dir>7)
  14812. {
  14813. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  14814. script_reportsrc(st);
  14815. dir%= 8; // trim spin-over
  14816. }
  14817. if(!cells)
  14818. {// zero distance
  14819. return 0;
  14820. }
  14821. else if(cells<0)
  14822. {// pushing backwards
  14823. dir = (dir+4)%8; // turn around
  14824. cells = -cells;
  14825. }
  14826. dx = dirx[dir];
  14827. dy = diry[dir];
  14828. unit_blown(&sd->bl, dx, dy, cells, 0);
  14829. return 0;
  14830. }
  14831. /// Invokes buying store preparation window
  14832. /// buyingstore <slots>;
  14833. BUILDIN_FUNC(buyingstore)
  14834. {
  14835. struct map_session_data* sd;
  14836. if( ( sd = script_rid2sd(st) ) == NULL )
  14837. {
  14838. return 0;
  14839. }
  14840. if( npc_isnear(&sd->bl) ) {
  14841. char output[150];
  14842. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  14843. clif_displaymessage(sd->fd, output);
  14844. return 0;
  14845. }
  14846. buyingstore_setup(sd, script_getnum(st,2));
  14847. return 0;
  14848. }
  14849. /// Invokes search store info window
  14850. /// searchstores <uses>,<effect>;
  14851. BUILDIN_FUNC(searchstores)
  14852. {
  14853. unsigned short effect;
  14854. unsigned int uses;
  14855. struct map_session_data* sd;
  14856. if( ( sd = script_rid2sd(st) ) == NULL )
  14857. {
  14858. return 0;
  14859. }
  14860. uses = script_getnum(st,2);
  14861. effect = script_getnum(st,3);
  14862. if( !uses )
  14863. {
  14864. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  14865. return 1;
  14866. }
  14867. if( effect > 1 )
  14868. {
  14869. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  14870. return 1;
  14871. }
  14872. searchstore_open(sd, uses, effect);
  14873. return 0;
  14874. }
  14875. /// Displays a number as large digital clock.
  14876. /// showdigit <value>[,<type>];
  14877. BUILDIN_FUNC(showdigit)
  14878. {
  14879. unsigned int type = 0;
  14880. int value;
  14881. struct map_session_data* sd;
  14882. if( ( sd = script_rid2sd(st) ) == NULL )
  14883. {
  14884. return 0;
  14885. }
  14886. value = script_getnum(st,2);
  14887. if( script_hasdata(st,3) )
  14888. {
  14889. type = script_getnum(st,3);
  14890. if( type > 3 )
  14891. {
  14892. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  14893. return 1;
  14894. }
  14895. }
  14896. clif_showdigit(sd, (unsigned char)type, value);
  14897. return 0;
  14898. }
  14899. /**
  14900. * Rune Knight
  14901. **/
  14902. BUILDIN_FUNC(makerune) {
  14903. TBL_PC* sd;
  14904. if( (sd = script_rid2sd(st)) == NULL )
  14905. return 0;
  14906. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  14907. sd->itemid = script_getnum(st,2);
  14908. return 0;
  14909. }
  14910. /**
  14911. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  14912. **/
  14913. BUILDIN_FUNC(checkdragon) {
  14914. TBL_PC* sd;
  14915. if( (sd = script_rid2sd(st)) == NULL )
  14916. return 0;
  14917. if( pc_isridingdragon(sd) )
  14918. script_pushint(st,1);
  14919. else
  14920. script_pushint(st,0);
  14921. return 0;
  14922. }
  14923. /**
  14924. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  14925. * - Toggles the dragon on a RK if he can mount;
  14926. * @param Color - when not provided uses the green dragon;
  14927. * - 1 : Green Dragon
  14928. * - 2 : Brown Dragon
  14929. * - 3 : Gray Dragon
  14930. * - 4 : Blue Dragon
  14931. * - 5 : Red Dragon
  14932. **/
  14933. BUILDIN_FUNC(setdragon) {
  14934. TBL_PC* sd;
  14935. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  14936. if( (sd = script_rid2sd(st)) == NULL )
  14937. return 0;
  14938. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  14939. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  14940. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  14941. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  14942. script_pushint(st,1);
  14943. } else {//Not mounted; Mount now.
  14944. unsigned int option = OPTION_DRAGON1;
  14945. if( color ) {
  14946. option = ( color == 1 ? OPTION_DRAGON1 :
  14947. color == 2 ? OPTION_DRAGON2 :
  14948. color == 3 ? OPTION_DRAGON3 :
  14949. color == 4 ? OPTION_DRAGON4 :
  14950. color == 5 ? OPTION_DRAGON5 : 0);
  14951. if( !option ) {
  14952. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  14953. option = OPTION_DRAGON1;
  14954. }
  14955. }
  14956. pc_setoption(sd, sd->sc.option|option);
  14957. script_pushint(st,1);
  14958. }
  14959. return 0;
  14960. }
  14961. /**
  14962. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  14963. **/
  14964. BUILDIN_FUNC(ismounting) {
  14965. TBL_PC* sd;
  14966. if( (sd = script_rid2sd(st)) == NULL )
  14967. return 0;
  14968. if( sd->sc.option&OPTION_MOUNTING )
  14969. script_pushint(st,1);
  14970. else
  14971. script_pushint(st,0);
  14972. return 0;
  14973. }
  14974. /**
  14975. * setmounting() returns 1 on success or 0 otherwise
  14976. * - Toggles new mounts on a player when he can mount
  14977. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  14978. * - Will unmount the player is he is already mounting
  14979. **/
  14980. BUILDIN_FUNC(setmounting) {
  14981. TBL_PC* sd;
  14982. if( (sd = script_rid2sd(st)) == NULL )
  14983. return 0;
  14984. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) )
  14985. script_pushint(st,0);//can't mount with one of these
  14986. else {
  14987. if( sd->sc.option&OPTION_MOUNTING )
  14988. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  14989. else
  14990. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  14991. script_pushint(st,1);//in both cases, return 1.
  14992. }
  14993. return 0;
  14994. }
  14995. /**
  14996. * Retrieves quantity of arguments provided to callfunc/callsub.
  14997. * getargcount() -> amount of arguments received in a function
  14998. **/
  14999. BUILDIN_FUNC(getargcount) {
  15000. struct script_retinfo* ri;
  15001. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  15002. ShowError("script:getargcount: used out of function or callsub label!\n");
  15003. st->state = END;
  15004. return 1;
  15005. }
  15006. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  15007. script_pushint(st, ri->nargs);
  15008. return 0;
  15009. }
  15010. /**
  15011. * getcharip(<account ID>/<character ID>/<character name>)
  15012. **/
  15013. BUILDIN_FUNC(getcharip)
  15014. {
  15015. struct map_session_data* sd = NULL;
  15016. /* check if a character name is specified */
  15017. if( script_hasdata(st, 2) )
  15018. {
  15019. if (script_isstring(st, 2))
  15020. sd = map_nick2sd(script_getstr(st, 2));
  15021. else if (script_isint(st, 2) || script_getnum(st, 2))
  15022. {
  15023. int id = 0;
  15024. id = script_getnum(st, 2);
  15025. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  15026. }
  15027. }
  15028. else
  15029. sd = script_rid2sd(st);
  15030. /* check for sd and IP */
  15031. if (!sd || !session[sd->fd]->client_addr)
  15032. {
  15033. script_pushconststr(st, "");
  15034. return 0;
  15035. }
  15036. /* return the client ip_addr converted for output */
  15037. if (sd && sd->fd && session[sd->fd])
  15038. {
  15039. /* initiliaze */
  15040. const char *ip_addr = NULL;
  15041. uint32 ip;
  15042. /* set ip, ip_addr and convert to ip and push str */
  15043. ip = session[sd->fd]->client_addr;
  15044. ip_addr = ip2str(ip, NULL);
  15045. script_pushstrcopy(st, ip_addr);
  15046. }
  15047. return 0;
  15048. }
  15049. /**
  15050. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  15051. **/
  15052. BUILDIN_FUNC(is_function) {
  15053. const char* str = script_getstr(st,2);
  15054. if( strdb_exists(userfunc_db, str) )
  15055. script_pushint(st,1);
  15056. else
  15057. script_pushint(st,0);
  15058. return 0;
  15059. }
  15060. /**
  15061. * get_revision() -> retrieves the current svn revision (if available)
  15062. **/
  15063. BUILDIN_FUNC(get_revision) {
  15064. const char *svn = get_svn_revision();
  15065. if ( svn[0] != UNKNOWN_VERSION )
  15066. script_pushint(st,atoi(svn));
  15067. else
  15068. script_pushint(st,-1); //unknown
  15069. return 0;
  15070. }
  15071. /**
  15072. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  15073. **/
  15074. BUILDIN_FUNC(freeloop) {
  15075. if( script_getnum(st,2) )
  15076. st->freeloop = 1;
  15077. else
  15078. st->freeloop = 0;
  15079. script_pushint(st, st->freeloop);
  15080. return 0;
  15081. }
  15082. /**
  15083. * @commands (script based)
  15084. **/
  15085. BUILDIN_FUNC(bindatcmd) {
  15086. const char* atcmd;
  15087. const char* eventName;
  15088. int i, level = 0, level2 = 0;
  15089. bool create = false;
  15090. atcmd = script_getstr(st,2);
  15091. eventName = script_getstr(st,3);
  15092. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15093. atcmd++;
  15094. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  15095. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  15096. if( atcmd_binding_count == 0 ) {
  15097. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  15098. create = true;
  15099. } else {
  15100. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  15101. if( i < atcmd_binding_count ) {/* update existent entry */
  15102. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15103. atcmd_binding[i]->level = level;
  15104. atcmd_binding[i]->level2 = level2;
  15105. } else
  15106. create = true;
  15107. }
  15108. if( create ) {
  15109. i = atcmd_binding_count;
  15110. if( atcmd_binding_count++ != 0 )
  15111. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  15112. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  15113. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  15114. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15115. atcmd_binding[i]->level = level;
  15116. atcmd_binding[i]->level2 = level2;
  15117. }
  15118. return 0;
  15119. }
  15120. BUILDIN_FUNC(unbindatcmd) {
  15121. const char* atcmd;
  15122. int i = 0;
  15123. atcmd = script_getstr(st, 2);
  15124. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15125. atcmd++;
  15126. if( atcmd_binding_count == 0 ) {
  15127. script_pushint(st, 0);
  15128. return 0;
  15129. }
  15130. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  15131. if( i < atcmd_binding_count ) {
  15132. int cursor = 0;
  15133. aFree(atcmd_binding[i]);
  15134. atcmd_binding[i] = NULL;
  15135. /* compact the list now that we freed a slot somewhere */
  15136. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  15137. if( atcmd_binding[i] == NULL )
  15138. continue;
  15139. if( cursor != i ) {
  15140. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  15141. }
  15142. cursor++;
  15143. }
  15144. if( (atcmd_binding_count = cursor) == 0 )
  15145. aFree(atcmd_binding);
  15146. script_pushint(st, 1);
  15147. } else
  15148. script_pushint(st, 0);/* not found */
  15149. return 0;
  15150. }
  15151. BUILDIN_FUNC(useatcmd) {
  15152. return atcommand_sub(st,3);
  15153. }
  15154. BUILDIN_FUNC(checkre)
  15155. {
  15156. int num;
  15157. num=script_getnum(st,2);
  15158. switch(num){
  15159. case 0:
  15160. #ifdef RENEWAL
  15161. script_pushint(st, 1);
  15162. #else
  15163. script_pushint(st, 0);
  15164. #endif
  15165. break;
  15166. case 1:
  15167. #ifdef RENEWAL_CAST
  15168. script_pushint(st, 1);
  15169. #else
  15170. script_pushint(st, 0);
  15171. #endif
  15172. break;
  15173. case 2:
  15174. #ifdef RENEWAL_DROP
  15175. script_pushint(st, 1);
  15176. #else
  15177. script_pushint(st, 0);
  15178. #endif
  15179. break;
  15180. case 3:
  15181. #ifdef RENEWAL_EXP
  15182. script_pushint(st, 1);
  15183. #else
  15184. script_pushint(st, 0);
  15185. #endif
  15186. break;
  15187. case 4:
  15188. #ifdef RENEWAL_LVDMG
  15189. script_pushint(st, 1);
  15190. #else
  15191. script_pushint(st, 0);
  15192. #endif
  15193. break;
  15194. case 5:
  15195. #ifdef RENEWAL_ASPD
  15196. script_pushint(st, 1);
  15197. #else
  15198. script_pushint(st, 0);
  15199. #endif
  15200. break;
  15201. default:
  15202. ShowWarning("buildin_checkre: unknown parameter.\n");
  15203. break;
  15204. }
  15205. return 0;
  15206. }
  15207. /* getrandgroupitem <group_id>,<quantity> */
  15208. BUILDIN_FUNC(getrandgroupitem) {
  15209. TBL_PC* sd;
  15210. int i, get_count = 0, flag, nameid, group = script_getnum(st, 2), qty = script_getnum(st,3);
  15211. struct item item_tmp;
  15212. if( !( sd = script_rid2sd(st) ) )
  15213. return 0;
  15214. if( qty <= 0 ) {
  15215. ShowError("getrandgroupitem: qty is <= 0!\n");
  15216. return 1;
  15217. }
  15218. if( (nameid = itemdb_searchrandomid(group)) == UNKNOWN_ITEM_ID ) {
  15219. return 1; //ensure valid itemid
  15220. }
  15221. memset(&item_tmp,0,sizeof(item_tmp));
  15222. item_tmp.nameid = nameid;
  15223. item_tmp.identify = itemdb_isidentified(nameid);
  15224. //Check if it's stackable.
  15225. if (!itemdb_isstackable(nameid))
  15226. get_count = 1;
  15227. else
  15228. get_count = qty;
  15229. for (i = 0; i < qty; i += get_count) {
  15230. // if not pet egg
  15231. if (!pet_create_egg(sd, nameid)) {
  15232. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT))) {
  15233. clif_additem(sd, 0, 0, flag);
  15234. if( pc_candrop(sd,&item_tmp) )
  15235. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  15236. }
  15237. }
  15238. }
  15239. return 0;
  15240. }
  15241. /* cleanmap <map_name>;
  15242. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  15243. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  15244. {
  15245. nullpo_ret(bl);
  15246. map_clearflooritem(bl);
  15247. return 0;
  15248. }
  15249. BUILDIN_FUNC(cleanmap)
  15250. {
  15251. const char *map;
  15252. int16 m = -1;
  15253. int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  15254. map = script_getstr(st, 2);
  15255. m = map_mapname2mapid(map);
  15256. if (!m)
  15257. return 1;
  15258. if ((script_lastdata(st) - 2) < 4) {
  15259. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  15260. } else {
  15261. x0 = script_getnum(st, 3);
  15262. y0 = script_getnum(st, 4);
  15263. x1 = script_getnum(st, 5);
  15264. y1 = script_getnum(st, 6);
  15265. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  15266. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  15267. } else {
  15268. ShowError("cleanarea: invalid coordinate defined!\n");
  15269. return 1;
  15270. }
  15271. }
  15272. return 0;
  15273. }
  15274. /* Cast a skill on the attached player.
  15275. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  15276. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  15277. BUILDIN_FUNC(npcskill)
  15278. {
  15279. uint16 skill_id;
  15280. unsigned short skill_level;
  15281. unsigned int stat_point;
  15282. unsigned int npc_level;
  15283. struct npc_data *nd;
  15284. struct map_session_data *sd;
  15285. skill_id = script_isstring(st, 2) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  15286. skill_level = script_getnum(st, 3);
  15287. stat_point = script_getnum(st, 4);
  15288. npc_level = script_getnum(st, 5);
  15289. sd = script_rid2sd(st);
  15290. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  15291. if (stat_point > battle_config.max_third_parameter) {
  15292. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  15293. return 1;
  15294. }
  15295. if (npc_level > MAX_LEVEL) {
  15296. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  15297. return 1;
  15298. }
  15299. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  15300. return 1;
  15301. }
  15302. nd->level = npc_level;
  15303. nd->stat_point = stat_point;
  15304. if (!nd->status.hp) {
  15305. status_calc_npc(nd, true);
  15306. } else {
  15307. status_calc_npc(nd, false);
  15308. }
  15309. if (skill_get_inf(skill_id)&INF_GROUND_SKILL) {
  15310. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  15311. } else {
  15312. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  15313. }
  15314. return 0;
  15315. }
  15316. /* Consumes an item.
  15317. * consumeitem <item id>;
  15318. * consumeitem "<item name>"; */
  15319. BUILDIN_FUNC(consumeitem)
  15320. {
  15321. TBL_NPC *nd;
  15322. TBL_PC *sd;
  15323. struct script_data *data;
  15324. struct item_data *item_data;
  15325. nullpo_retr( 1, ( sd = script_rid2sd( st ) ) );
  15326. nullpo_retr( 1, ( nd = (TBL_NPC *)map_id2bl( sd->npc_id ) ) );
  15327. data = script_getdata( st, 2 );
  15328. get_val( st, data );
  15329. if( data_isstring( data ) ){
  15330. const char *name = conv_str( st, data );
  15331. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  15332. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  15333. return 1;
  15334. }
  15335. }else if( data_isint( data ) ){
  15336. int nameid = conv_num( st, data );
  15337. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  15338. ShowError("buildin_consumeitem: Nonexistant item %d requested.\n", nameid );
  15339. return 1;
  15340. }
  15341. }else{
  15342. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).", data->type );
  15343. return 1;
  15344. }
  15345. run_script( item_data->script, 0, sd->bl.id, nd->bl.id );
  15346. return 0;
  15347. }
  15348. /*=======================================================
  15349. * Make a player sit/stand.
  15350. * sit {"<character name>"};
  15351. * stand {"<character name>"};
  15352. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  15353. *-------------------------------------------------------*/
  15354. BUILDIN_FUNC(sit)
  15355. {
  15356. TBL_PC *sd;
  15357. if( script_hasdata(st, 2) )
  15358. sd = map_nick2sd(script_getstr(st, 2));
  15359. else
  15360. sd = script_rid2sd(st);
  15361. if( sd == NULL)
  15362. return 0;
  15363. if( !pc_issit(sd) ) {
  15364. unit_stop_walking(&sd->bl, 1|4);
  15365. pc_setsit(sd);
  15366. skill_sit(sd, 1);
  15367. clif_sitting(&sd->bl);
  15368. }
  15369. return 0;
  15370. }
  15371. BUILDIN_FUNC(stand)
  15372. {
  15373. TBL_PC *sd;
  15374. if( script_hasdata(st, 2) )
  15375. sd = map_nick2sd(script_getstr(st, 2));
  15376. else
  15377. sd = script_rid2sd(st);
  15378. if( sd == NULL)
  15379. return 0;
  15380. if( pc_issit(sd) ) {
  15381. pc_setstand(sd);
  15382. skill_sit(sd, 0);
  15383. clif_standing(&sd->bl);
  15384. }
  15385. return 0;
  15386. }
  15387. /*==========================================
  15388. * countbound {<type>};
  15389. * Creates an array of bounded item IDs
  15390. * Returns amount of items found
  15391. * Type:
  15392. * 1 - Account Bound
  15393. * 2 - Guild Bound
  15394. * 3 - Party Bound
  15395. *------------------------------------------*/
  15396. BUILDIN_FUNC(countbound)
  15397. {
  15398. int i, type, j=0, k=0;
  15399. TBL_PC *sd;
  15400. if( (sd = script_rid2sd(st)) == NULL )
  15401. return 0;
  15402. type = script_hasdata(st,2)?script_getnum(st,2):0;
  15403. for(i=0;i<MAX_INVENTORY;i++){
  15404. if(sd->status.inventory[i].nameid > 0 && (
  15405. (!type && sd->status.inventory[i].bound > 0) ||
  15406. (type && sd->status.inventory[i].bound == type)
  15407. )) {
  15408. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  15409. k++;
  15410. j += sd->status.inventory[i].amount;
  15411. }
  15412. }
  15413. script_pushint(st,j);
  15414. return 0;
  15415. }
  15416. /*==========================================
  15417. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  15418. * <item share>: 0-Each Take. 1-Party Share
  15419. * <item share type>: 0-Each Take. 1-Even Share
  15420. * Return values:
  15421. * -3 - party name is exist
  15422. * -2 - player is in party already
  15423. * -1 - player is not found
  15424. * 0 - unknown error
  15425. * 1 - success, will return party id $@party_create_id
  15426. *------------------------------------------*/
  15427. BUILDIN_FUNC(party_create)
  15428. {
  15429. char party_name[NAME_LENGTH];
  15430. int item1 = 0, item2 = 0;
  15431. TBL_PC *sd;
  15432. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  15433. script_pushint(st,-1);
  15434. return 0;
  15435. }
  15436. if( sd->status.party_id ) {
  15437. script_pushint(st,-2);
  15438. return 0;
  15439. }
  15440. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  15441. trim(party_name);
  15442. if( party_searchname(party_name) ) {
  15443. script_pushint(st,-3);
  15444. return 0;
  15445. }
  15446. if( script_getnum(st,4) )
  15447. item1 = 1;
  15448. if( script_getnum(st,5) )
  15449. item2 = 1;
  15450. party_create_byscript = 1;
  15451. script_pushint(st,party_create(sd,party_name,item1,item2));
  15452. return 0;
  15453. }
  15454. /*==========================================
  15455. * party_addmember <party id>,<char id>;
  15456. * Adds player to specified party
  15457. * Return values:
  15458. * -4 - party is full
  15459. * -3 - party is not found
  15460. * -2 - player is in party already
  15461. * -1 - player is not found
  15462. * 0 - unknown error
  15463. * 1 - success
  15464. *------------------------------------------*/
  15465. BUILDIN_FUNC(party_addmember)
  15466. {
  15467. int party_id = script_getnum(st,2);
  15468. TBL_PC *sd;
  15469. struct party_data *party;
  15470. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  15471. script_pushint(st,-1);
  15472. return 0;
  15473. }
  15474. if( sd->status.party_id ) {
  15475. script_pushint(st,-2);
  15476. return 0;
  15477. }
  15478. if( !(party = party_search(party_id)) ) {
  15479. script_pushint(st,-3);
  15480. return 0;
  15481. }
  15482. if( party->party.count >= MAX_PARTY ) {
  15483. script_pushint(st,-4);
  15484. return 0;
  15485. }
  15486. sd->party_invite = party_id;
  15487. script_pushint(st,party_add_member(party_id,sd));
  15488. return 0;
  15489. }
  15490. /*==========================================
  15491. * party_delmember {<char id>,<party_id>};
  15492. * Removes player from his/her party. If party_id and char_id is empty
  15493. * remove the invoker from his/her party
  15494. * Return values:
  15495. * -3 - player is not in party
  15496. * -2 - party is not found
  15497. * -1 - player is not found
  15498. * 0 - unknown error
  15499. * 1 - success
  15500. *------------------------------------------*/
  15501. BUILDIN_FUNC(party_delmember)
  15502. {
  15503. TBL_PC *sd = NULL;
  15504. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  15505. script_pushint(st,-1);
  15506. return 0;
  15507. }
  15508. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  15509. script_pushint(st,party_removemember2(sd,0,0));
  15510. else
  15511. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  15512. return 0;
  15513. }
  15514. /*==========================================
  15515. * party_changeleader <party id>,<char id>;
  15516. * Can change party leader even the leader is not online
  15517. * Return values:
  15518. * -4 - player is party leader already
  15519. * -3 - player is not in this party
  15520. * -2 - player is not found
  15521. * -1 - party is not found
  15522. * 0 - unknown error
  15523. * 1 - success
  15524. *------------------------------------------*/
  15525. BUILDIN_FUNC(party_changeleader)
  15526. {
  15527. int i, party_id = script_getnum(st,2);
  15528. TBL_PC *sd = NULL;
  15529. TBL_PC *tsd = NULL;
  15530. struct party_data *party = NULL;
  15531. if( !(party = party_search(party_id)) ) {
  15532. script_pushint(st,-1);
  15533. return 0;
  15534. }
  15535. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  15536. script_pushint(st,-2);
  15537. return 0;
  15538. }
  15539. if( tsd->status.party_id != party_id ) {
  15540. script_pushint(st,-3);
  15541. return 0;
  15542. }
  15543. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  15544. if( i >= MAX_PARTY ) { //this is should impossible!
  15545. script_pushint(st,0);
  15546. return 0;
  15547. }
  15548. if( party->data[i].sd == tsd ) {
  15549. script_pushint(st,-4);
  15550. return 0;
  15551. }
  15552. script_pushint(st,party_changeleader(sd,tsd,party));
  15553. return 0;
  15554. }
  15555. /*==========================================
  15556. * party_changeoption <party id>,<option>,<flag>;
  15557. * Return values:
  15558. * -1 - party is not found
  15559. * 0 - invalid option
  15560. * 1 - success
  15561. *------------------------------------------*/
  15562. BUILDIN_FUNC(party_changeoption)
  15563. {
  15564. struct party_data *party;
  15565. if( !(party = party_search(script_getnum(st,2))) ) {
  15566. script_pushint(st,-1);
  15567. return 0;
  15568. }
  15569. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  15570. return 0;
  15571. }
  15572. /*==========================================
  15573. * party_destroy <party id>;
  15574. * Destroys party with party id.
  15575. * Return values:
  15576. * 0 - failed
  15577. * 1 - success
  15578. *------------------------------------------*/
  15579. BUILDIN_FUNC(party_destroy)
  15580. {
  15581. int i;
  15582. struct party_data *party;
  15583. if( !(party = party_search(script_getnum(st,2))) ) {
  15584. script_pushint(st,0);
  15585. return 0;
  15586. }
  15587. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  15588. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  15589. int j;
  15590. for( j = 0; j < MAX_PARTY; j++ ) {
  15591. TBL_PC *sd = party->data[j].sd;
  15592. if(sd)
  15593. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  15594. else if( party->party.member[j].char_id )
  15595. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  15596. }
  15597. party_broken(party->party.party_id);
  15598. script_pushint(st,1);
  15599. }
  15600. else //leader leave = party broken
  15601. script_pushint(st,party_leave(party->data[i].sd));
  15602. return 0;
  15603. }
  15604. /*==========================================
  15605. * Checks if a player's client version meets a required version or date.
  15606. * @type :
  15607. * 0 - check by version number
  15608. * 1 - check by date
  15609. * @return true/false
  15610. *------------------------------------------*/
  15611. BUILDIN_FUNC(is_clientver){
  15612. TBL_PC *sd = NULL;
  15613. int type = script_getnum(st,2);
  15614. int data = script_getnum(st,3);
  15615. int ret = 0;
  15616. if (script_hasdata(st,4))
  15617. sd = map_charid2sd(script_getnum(st,4));
  15618. else
  15619. sd = script_rid2sd(st);
  15620. if (sd == NULL) {
  15621. script_pushint(st,0);
  15622. return 0;
  15623. }
  15624. switch(type){
  15625. case 0:
  15626. ret = (sd->packet_ver >= data)?1:0;
  15627. break;
  15628. case 1:
  15629. ret = (sd->packet_ver >= date2version(data))?1:0;
  15630. break;
  15631. }
  15632. script_pushint(st,ret);
  15633. return 0;
  15634. }
  15635. /*==========================================
  15636. * Retrieves server definitions.
  15637. * (see @type in const.txt)
  15638. *------------------------------------------*/
  15639. BUILDIN_FUNC(getserverdef){
  15640. int type = script_getnum(st,2);
  15641. switch(type){
  15642. case 0: script_pushint(st,PACKETVER); break;
  15643. case 1: script_pushint(st,MAX_LEVEL); break;
  15644. case 2: script_pushint(st,MAX_STORAGE); break;
  15645. case 3: script_pushint(st,MAX_INVENTORY); break;
  15646. case 4: script_pushint(st,MAX_ZENY); break;
  15647. case 5: script_pushint(st,MAX_PARTY); break;
  15648. case 6: script_pushint(st,MAX_GUILD); break;
  15649. case 7: script_pushint(st,MAX_GUILDLEVEL); break;
  15650. case 8: script_pushint(st,MAX_GUILD_STORAGE); break;
  15651. case 9: script_pushint(st,MAX_BG_MEMBERS); break;
  15652. default:
  15653. ShowWarning("buildin_getserverdef: unknown type %d.\n", type);
  15654. script_pushint(st,0);
  15655. break;
  15656. }
  15657. return 0;
  15658. }
  15659. // declarations that were supposed to be exported from npc_chat.c
  15660. #ifdef PCRE_SUPPORT
  15661. BUILDIN_FUNC(defpattern);
  15662. BUILDIN_FUNC(activatepset);
  15663. BUILDIN_FUNC(deactivatepset);
  15664. BUILDIN_FUNC(deletepset);
  15665. #endif
  15666. /// script command definitions
  15667. /// for an explanation on args, see add_buildin_func
  15668. struct script_function buildin_func[] = {
  15669. // NPC interaction
  15670. BUILDIN_DEF(mes,"s*"),
  15671. BUILDIN_DEF(next,""),
  15672. BUILDIN_DEF(close,""),
  15673. BUILDIN_DEF(close2,""),
  15674. BUILDIN_DEF(menu,"sl*"),
  15675. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  15676. BUILDIN_DEF(prompt,"s*"),
  15677. //
  15678. BUILDIN_DEF(goto,"l"),
  15679. BUILDIN_DEF(callsub,"l*"),
  15680. BUILDIN_DEF(callfunc,"s*"),
  15681. BUILDIN_DEF(return,"?"),
  15682. BUILDIN_DEF(getarg,"i?"),
  15683. BUILDIN_DEF(jobchange,"i?"),
  15684. BUILDIN_DEF(jobname,"i"),
  15685. BUILDIN_DEF(input,"r??"),
  15686. BUILDIN_DEF(warp,"sii"),
  15687. BUILDIN_DEF(areawarp,"siiiisii??"),
  15688. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  15689. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  15690. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  15691. BUILDIN_DEF(setlook,"ii"),
  15692. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  15693. BUILDIN_DEF(set,"rv"),
  15694. BUILDIN_DEF(setarray,"rv*"),
  15695. BUILDIN_DEF(cleararray,"rvi"),
  15696. BUILDIN_DEF(copyarray,"rri"),
  15697. BUILDIN_DEF(getarraysize,"r"),
  15698. BUILDIN_DEF(deletearray,"r?"),
  15699. BUILDIN_DEF(getelementofarray,"ri"),
  15700. BUILDIN_DEF(getitem,"vi?"),
  15701. BUILDIN_DEF(rentitem,"vi"),
  15702. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  15703. BUILDIN_DEF(getnameditem,"vv"),
  15704. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  15705. BUILDIN_DEF(makeitem,"visii"),
  15706. BUILDIN_DEF(delitem,"vi?"),
  15707. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  15708. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  15709. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  15710. BUILDIN_DEF(cutin,"si"),
  15711. BUILDIN_DEF(viewpoint,"iiiii"),
  15712. BUILDIN_DEF(heal,"ii"),
  15713. BUILDIN_DEF(itemheal,"ii"),
  15714. BUILDIN_DEF(percentheal,"ii"),
  15715. BUILDIN_DEF(rand,"i?"),
  15716. BUILDIN_DEF(countitem,"v"),
  15717. BUILDIN_DEF2(countitem,"countitem2","viiiiiii"),
  15718. BUILDIN_DEF(checkweight,"vi*"),
  15719. BUILDIN_DEF(checkweight2,"rr"),
  15720. BUILDIN_DEF(readparam,"i?"),
  15721. BUILDIN_DEF(getcharid,"i?"),
  15722. BUILDIN_DEF(getnpcid,"i?"),
  15723. BUILDIN_DEF(getpartyname,"i"),
  15724. BUILDIN_DEF(getpartymember,"i?"),
  15725. BUILDIN_DEF(getpartyleader,"i?"),
  15726. BUILDIN_DEF(getguildname,"i"),
  15727. BUILDIN_DEF(getguildmaster,"i"),
  15728. BUILDIN_DEF(getguildmasterid,"i"),
  15729. BUILDIN_DEF(strcharinfo,"i"),
  15730. BUILDIN_DEF(strnpcinfo,"i"),
  15731. BUILDIN_DEF(getequipid,"i"),
  15732. BUILDIN_DEF(getequipname,"i"),
  15733. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  15734. BUILDIN_DEF(repair,"i"), // [Valaris]
  15735. BUILDIN_DEF(repairall,""),
  15736. BUILDIN_DEF(getequipisequiped,"i"),
  15737. BUILDIN_DEF(getequipisenableref,"i"),
  15738. BUILDIN_DEF(getequiprefinerycnt,"i"),
  15739. BUILDIN_DEF(getequipweaponlv,"i"),
  15740. BUILDIN_DEF(getequippercentrefinery,"i"),
  15741. BUILDIN_DEF(successrefitem,"i"),
  15742. BUILDIN_DEF(failedrefitem,"i"),
  15743. BUILDIN_DEF(downrefitem,"i"),
  15744. BUILDIN_DEF(statusup,"i"),
  15745. BUILDIN_DEF(statusup2,"ii"),
  15746. BUILDIN_DEF(bonus,"iv"),
  15747. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  15748. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  15749. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  15750. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  15751. BUILDIN_DEF(autobonus,"sii??"),
  15752. BUILDIN_DEF(autobonus2,"sii??"),
  15753. BUILDIN_DEF(autobonus3,"siiv?"),
  15754. BUILDIN_DEF(skill,"vi?"),
  15755. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  15756. BUILDIN_DEF(guildskill,"vi"),
  15757. BUILDIN_DEF(getskilllv,"v"),
  15758. BUILDIN_DEF(getgdskilllv,"iv"),
  15759. BUILDIN_DEF(basicskillcheck,""),
  15760. BUILDIN_DEF(getgmlevel,""),
  15761. BUILDIN_DEF(getgroupid,""),
  15762. BUILDIN_DEF(end,""),
  15763. BUILDIN_DEF(checkoption,"i"),
  15764. BUILDIN_DEF(setoption,"i?"),
  15765. BUILDIN_DEF(setcart,"?"),
  15766. BUILDIN_DEF(checkcart,""),
  15767. BUILDIN_DEF(setfalcon,"?"),
  15768. BUILDIN_DEF(checkfalcon,""),
  15769. BUILDIN_DEF(setriding,"?"),
  15770. BUILDIN_DEF(checkriding,""),
  15771. BUILDIN_DEF(checkwug,""),
  15772. BUILDIN_DEF(checkmadogear,""),
  15773. BUILDIN_DEF(setmadogear,"?"),
  15774. BUILDIN_DEF2(savepoint,"save","sii"),
  15775. BUILDIN_DEF(savepoint,"sii"),
  15776. BUILDIN_DEF(gettimetick,"i"),
  15777. BUILDIN_DEF(gettime,"i"),
  15778. BUILDIN_DEF(gettimestr,"si"),
  15779. BUILDIN_DEF(openstorage,""),
  15780. BUILDIN_DEF(guildopenstorage,""),
  15781. BUILDIN_DEF(itemskill,"vi"),
  15782. BUILDIN_DEF(produce,"i"),
  15783. BUILDIN_DEF(cooking,"i"),
  15784. BUILDIN_DEF(monster,"siisii???"),
  15785. BUILDIN_DEF(getmobdrops,"i"),
  15786. BUILDIN_DEF(areamonster,"siiiisii???"),
  15787. BUILDIN_DEF(killmonster,"ss?"),
  15788. BUILDIN_DEF(killmonsterall,"s?"),
  15789. BUILDIN_DEF(clone,"siisi????"),
  15790. BUILDIN_DEF(doevent,"s"),
  15791. BUILDIN_DEF(donpcevent,"s"),
  15792. BUILDIN_DEF(cmdothernpc,"ss"),
  15793. BUILDIN_DEF(addtimer,"is"),
  15794. BUILDIN_DEF(deltimer,"s"),
  15795. BUILDIN_DEF(addtimercount,"si"),
  15796. BUILDIN_DEF(initnpctimer,"??"),
  15797. BUILDIN_DEF(stopnpctimer,"??"),
  15798. BUILDIN_DEF(startnpctimer,"??"),
  15799. BUILDIN_DEF(setnpctimer,"i?"),
  15800. BUILDIN_DEF(getnpctimer,"i?"),
  15801. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  15802. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  15803. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  15804. BUILDIN_DEF(announce,"si?????"),
  15805. BUILDIN_DEF(mapannounce,"ssi?????"),
  15806. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  15807. BUILDIN_DEF(getusers,"i"),
  15808. BUILDIN_DEF(getmapguildusers,"si"),
  15809. BUILDIN_DEF(getmapusers,"s"),
  15810. BUILDIN_DEF(getareausers,"siiii"),
  15811. BUILDIN_DEF(getareadropitem,"siiiiv"),
  15812. BUILDIN_DEF(enablenpc,"s"),
  15813. BUILDIN_DEF(disablenpc,"s"),
  15814. BUILDIN_DEF(hideoffnpc,"s"),
  15815. BUILDIN_DEF(hideonnpc,"s"),
  15816. BUILDIN_DEF(sc_start,"iii???"),
  15817. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  15818. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  15819. BUILDIN_DEF(sc_end,"i?"),
  15820. BUILDIN_DEF(getstatus, "i?"),
  15821. BUILDIN_DEF(getscrate,"ii?"),
  15822. BUILDIN_DEF(debugmes,"s"),
  15823. BUILDIN_DEF2(catchpet,"pet","i"),
  15824. BUILDIN_DEF2(birthpet,"bpet",""),
  15825. BUILDIN_DEF(resetlvl,"i"),
  15826. BUILDIN_DEF(resetstatus,""),
  15827. BUILDIN_DEF(resetskill,""),
  15828. BUILDIN_DEF(skillpointcount,""),
  15829. BUILDIN_DEF(changebase,"i?"),
  15830. BUILDIN_DEF(changesex,""),
  15831. BUILDIN_DEF(waitingroom,"si?????"),
  15832. BUILDIN_DEF(delwaitingroom,"?"),
  15833. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  15834. BUILDIN_DEF(enablewaitingroomevent,"?"),
  15835. BUILDIN_DEF(disablewaitingroomevent,"?"),
  15836. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  15837. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  15838. BUILDIN_DEF(getwaitingroomstate,"i?"),
  15839. BUILDIN_DEF(warpwaitingpc,"sii?"),
  15840. BUILDIN_DEF(attachrid,"i"),
  15841. BUILDIN_DEF(addrid,"i?????"),
  15842. BUILDIN_DEF(detachrid,""),
  15843. BUILDIN_DEF(isloggedin,"i?"),
  15844. BUILDIN_DEF(setmapflagnosave,"ssii"),
  15845. BUILDIN_DEF(getmapflag,"si?"),
  15846. BUILDIN_DEF(setmapflag,"si??"),
  15847. BUILDIN_DEF(removemapflag,"si?"),
  15848. BUILDIN_DEF(pvpon,"s"),
  15849. BUILDIN_DEF(pvpoff,"s"),
  15850. BUILDIN_DEF(gvgon,"s"),
  15851. BUILDIN_DEF(gvgoff,"s"),
  15852. BUILDIN_DEF(emotion,"i??"),
  15853. BUILDIN_DEF(maprespawnguildid,"sii"),
  15854. BUILDIN_DEF(agitstart,""), // <Agit>
  15855. BUILDIN_DEF(agitend,""),
  15856. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  15857. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  15858. BUILDIN_DEF(getcastlename,"s"),
  15859. BUILDIN_DEF(getcastledata,"si"),
  15860. BUILDIN_DEF(setcastledata,"sii"),
  15861. BUILDIN_DEF(requestguildinfo,"i?"),
  15862. BUILDIN_DEF(getequipcardcnt,"i"),
  15863. BUILDIN_DEF(successremovecards,"i"),
  15864. BUILDIN_DEF(failedremovecards,"ii"),
  15865. BUILDIN_DEF(marriage,"s"),
  15866. BUILDIN_DEF2(wedding_effect,"wedding",""),
  15867. BUILDIN_DEF(divorce,""),
  15868. BUILDIN_DEF(ispartneron,""),
  15869. BUILDIN_DEF(getpartnerid,""),
  15870. BUILDIN_DEF(getchildid,""),
  15871. BUILDIN_DEF(getmotherid,""),
  15872. BUILDIN_DEF(getfatherid,""),
  15873. BUILDIN_DEF(warppartner,"sii"),
  15874. BUILDIN_DEF(getitemname,"v"),
  15875. BUILDIN_DEF(getitemslots,"i"),
  15876. BUILDIN_DEF(makepet,"i"),
  15877. BUILDIN_DEF(getexp,"ii"),
  15878. BUILDIN_DEF(getinventorylist,""),
  15879. BUILDIN_DEF(getskilllist,""),
  15880. BUILDIN_DEF(clearitem,""),
  15881. BUILDIN_DEF(classchange,"ii"),
  15882. BUILDIN_DEF(misceffect,"i"),
  15883. BUILDIN_DEF(playBGM,"s"),
  15884. BUILDIN_DEF(playBGMall,"s?????"),
  15885. BUILDIN_DEF(soundeffect,"si"),
  15886. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  15887. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  15888. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  15889. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  15890. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  15891. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  15892. BUILDIN_DEF(petloot,"i"), // [Valaris]
  15893. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  15894. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  15895. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  15896. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  15897. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  15898. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  15899. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  15900. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  15901. BUILDIN_DEF(nude,""), // nude command [Valaris]
  15902. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  15903. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  15904. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  15905. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  15906. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  15907. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  15908. BUILDIN_DEF(mobcount,"ss"),
  15909. BUILDIN_DEF(getlook,"i"),
  15910. BUILDIN_DEF(getsavepoint,"i"),
  15911. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  15912. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  15913. BUILDIN_DEF(npcstop,""), // [Valaris]
  15914. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  15915. BUILDIN_DEF(checkoption1,"i"),
  15916. BUILDIN_DEF(checkoption2,"i"),
  15917. BUILDIN_DEF(guildgetexp,"i"),
  15918. BUILDIN_DEF(guildchangegm,"is"),
  15919. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  15920. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  15921. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  15922. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  15923. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  15924. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  15925. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  15926. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  15927. BUILDIN_DEF(night,""), // sets the server to night time
  15928. BUILDIN_DEF(day,""), // sets the server to day time
  15929. #ifdef PCRE_SUPPORT
  15930. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  15931. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  15932. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  15933. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  15934. #endif
  15935. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  15936. BUILDIN_DEF(getusersname,""),
  15937. BUILDIN_DEF(recovery,"i???"),
  15938. BUILDIN_DEF(getpetinfo,"i"),
  15939. BUILDIN_DEF(gethominfo,"i"),
  15940. BUILDIN_DEF(getmercinfo,"i?"),
  15941. BUILDIN_DEF(checkequipedcard,"i"),
  15942. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  15943. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  15944. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  15945. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  15946. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  15947. BUILDIN_DEF(charat,"si"),
  15948. BUILDIN_DEF(setchar,"ssi"),
  15949. BUILDIN_DEF(insertchar,"ssi"),
  15950. BUILDIN_DEF(delchar,"si"),
  15951. BUILDIN_DEF(strtoupper,"s"),
  15952. BUILDIN_DEF(strtolower,"s"),
  15953. BUILDIN_DEF(charisupper, "si"),
  15954. BUILDIN_DEF(charislower, "si"),
  15955. BUILDIN_DEF(substr,"sii"),
  15956. BUILDIN_DEF(explode, "rss"),
  15957. BUILDIN_DEF(implode, "r?"),
  15958. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  15959. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  15960. BUILDIN_DEF(strpos,"ss?"),
  15961. BUILDIN_DEF(replacestr,"sss??"),
  15962. BUILDIN_DEF(countstr,"ss?"),
  15963. BUILDIN_DEF(setnpcdisplay,"sv??"),
  15964. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  15965. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  15966. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  15967. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  15968. // [zBuffer] List of mathematics commands --->
  15969. BUILDIN_DEF(sqrt,"i"),
  15970. BUILDIN_DEF(pow,"ii"),
  15971. BUILDIN_DEF(distance,"iiii"),
  15972. // <--- [zBuffer] List of mathematics commands
  15973. BUILDIN_DEF(md5,"s"),
  15974. // [zBuffer] List of dynamic var commands --->
  15975. BUILDIN_DEF(getd,"s"),
  15976. BUILDIN_DEF(setd,"sv"),
  15977. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  15978. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  15979. BUILDIN_DEF(npcshopadditem,"sii*"),
  15980. BUILDIN_DEF(npcshopdelitem,"si*"),
  15981. BUILDIN_DEF(npcshopattach,"s?"),
  15982. BUILDIN_DEF(equip,"i"),
  15983. BUILDIN_DEF(autoequip,"ii"),
  15984. BUILDIN_DEF(setbattleflag,"si"),
  15985. BUILDIN_DEF(getbattleflag,"s"),
  15986. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  15987. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  15988. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  15989. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  15990. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  15991. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  15992. BUILDIN_DEF(axtoi,"s"),
  15993. BUILDIN_DEF(query_sql,"s*"),
  15994. BUILDIN_DEF(query_logsql,"s*"),
  15995. BUILDIN_DEF(escape_sql,"v"),
  15996. BUILDIN_DEF(atoi,"s"),
  15997. // [zBuffer] List of player cont commands --->
  15998. BUILDIN_DEF(rid2name,"i"),
  15999. BUILDIN_DEF(pcfollow,"ii"),
  16000. BUILDIN_DEF(pcstopfollow,"i"),
  16001. BUILDIN_DEF(pcblockmove,"ii"),
  16002. // <--- [zBuffer] List of player cont commands
  16003. // [zBuffer] List of mob control commands --->
  16004. BUILDIN_DEF(unitwalk,"ii?"),
  16005. BUILDIN_DEF(unitkill,"i"),
  16006. BUILDIN_DEF(unitwarp,"isii"),
  16007. BUILDIN_DEF(unitattack,"iv?"),
  16008. BUILDIN_DEF(unitstop,"i"),
  16009. BUILDIN_DEF(unittalk,"is"),
  16010. BUILDIN_DEF(unitemote,"ii"),
  16011. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  16012. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  16013. // <--- [zBuffer] List of mob control commands
  16014. BUILDIN_DEF(sleep,"i"),
  16015. BUILDIN_DEF(sleep2,"i"),
  16016. BUILDIN_DEF(awake,"s"),
  16017. BUILDIN_DEF(getvariableofnpc,"rs"),
  16018. BUILDIN_DEF(warpportal,"iisii"),
  16019. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  16020. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  16021. BUILDIN_DEF(morphembryo,""),
  16022. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  16023. BUILDIN_DEF(checkhomcall,""),
  16024. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  16025. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  16026. BUILDIN_DEF(checkvending,"?"),
  16027. BUILDIN_DEF(checkchatting,"?"),
  16028. BUILDIN_DEF(checkidle,"?"),
  16029. BUILDIN_DEF(openmail,""),
  16030. BUILDIN_DEF(openauction,""),
  16031. BUILDIN_DEF(checkcell,"siii"),
  16032. BUILDIN_DEF(setcell,"siiiiii"),
  16033. BUILDIN_DEF(setwall,"siiiiis"),
  16034. BUILDIN_DEF(delwall,"s"),
  16035. BUILDIN_DEF(searchitem,"rs"),
  16036. BUILDIN_DEF(mercenary_create,"ii"),
  16037. BUILDIN_DEF(mercenary_heal,"ii"),
  16038. BUILDIN_DEF(mercenary_sc_start,"iii"),
  16039. BUILDIN_DEF(mercenary_get_calls,"i"),
  16040. BUILDIN_DEF(mercenary_get_faith,"i"),
  16041. BUILDIN_DEF(mercenary_set_calls,"ii"),
  16042. BUILDIN_DEF(mercenary_set_faith,"ii"),
  16043. BUILDIN_DEF(readbook,"ii"),
  16044. BUILDIN_DEF(setfont,"i"),
  16045. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  16046. BUILDIN_DEF(progressbar,"si"),
  16047. BUILDIN_DEF(pushpc,"ii"),
  16048. BUILDIN_DEF(buyingstore,"i"),
  16049. BUILDIN_DEF(searchstores,"ii"),
  16050. BUILDIN_DEF(showdigit,"i?"),
  16051. // WoE SE
  16052. BUILDIN_DEF(agitstart2,""),
  16053. BUILDIN_DEF(agitend2,""),
  16054. BUILDIN_DEF(agitcheck2,""),
  16055. // BattleGround
  16056. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  16057. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  16058. BUILDIN_DEF(bg_team_setxy,"iii"),
  16059. BUILDIN_DEF(bg_warp,"isii"),
  16060. BUILDIN_DEF(bg_monster,"isiisi?"),
  16061. BUILDIN_DEF(bg_monster_set_team,"ii"),
  16062. BUILDIN_DEF(bg_leave,""),
  16063. BUILDIN_DEF(bg_destroy,"i"),
  16064. BUILDIN_DEF(areapercentheal,"siiiiii"),
  16065. BUILDIN_DEF(bg_get_data,"ii"),
  16066. BUILDIN_DEF(bg_getareausers,"isiiii"),
  16067. BUILDIN_DEF(bg_updatescore,"sii"),
  16068. // Instancing
  16069. BUILDIN_DEF(instance_create,"s"),
  16070. BUILDIN_DEF(instance_destroy,"?"),
  16071. BUILDIN_DEF(instance_id,""),
  16072. BUILDIN_DEF(instance_enter,"s"),
  16073. BUILDIN_DEF(instance_npcname,"s?"),
  16074. BUILDIN_DEF(instance_mapname,"s?"),
  16075. BUILDIN_DEF(instance_warpall,"sii?"),
  16076. BUILDIN_DEF(instance_announce,"isi?????"),
  16077. BUILDIN_DEF(instance_check_party,"i???"),
  16078. /**
  16079. * 3rd-related
  16080. **/
  16081. BUILDIN_DEF(makerune,"i"),
  16082. BUILDIN_DEF(checkdragon,""),//[Ind]
  16083. BUILDIN_DEF(setdragon,"?"),//[Ind]
  16084. BUILDIN_DEF(ismounting,""),//[Ind]
  16085. BUILDIN_DEF(setmounting,""),//[Ind]
  16086. BUILDIN_DEF(checkre,"i"),
  16087. /**
  16088. * rAthena and beyond!
  16089. **/
  16090. BUILDIN_DEF(getargcount,""),
  16091. BUILDIN_DEF(getcharip,"?"),
  16092. BUILDIN_DEF(is_function,"s"),
  16093. BUILDIN_DEF(get_revision,""),
  16094. BUILDIN_DEF(freeloop,"i"),
  16095. BUILDIN_DEF(getrandgroupitem,"ii"),
  16096. BUILDIN_DEF(cleanmap,"s"),
  16097. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  16098. BUILDIN_DEF(npcskill,"viii"),
  16099. BUILDIN_DEF(consumeitem,"v"),
  16100. BUILDIN_DEF(delequip,"i"),
  16101. BUILDIN_DEF(sit,"?"),
  16102. BUILDIN_DEF(stand,"?"),
  16103. /**
  16104. * @commands (script based)
  16105. **/
  16106. BUILDIN_DEF(bindatcmd, "ss??"),
  16107. BUILDIN_DEF(unbindatcmd, "s"),
  16108. BUILDIN_DEF(useatcmd, "s"),
  16109. //Quest Log System [Inkfish]
  16110. BUILDIN_DEF(setquest, "i"),
  16111. BUILDIN_DEF(erasequest, "i"),
  16112. BUILDIN_DEF(completequest, "i"),
  16113. BUILDIN_DEF(checkquest, "i?"),
  16114. BUILDIN_DEF(changequest, "ii"),
  16115. BUILDIN_DEF(showevent, "ii"),
  16116. //Bound items [Xantara] & [Akinari]
  16117. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  16118. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  16119. BUILDIN_DEF(countbound, "?"),
  16120. // Party related
  16121. BUILDIN_DEF(party_create,"s???"),
  16122. BUILDIN_DEF(party_addmember,"ii"),
  16123. BUILDIN_DEF(party_delmember,"??"),
  16124. BUILDIN_DEF(party_changeleader,"ii"),
  16125. BUILDIN_DEF(party_changeoption,"iii"),
  16126. BUILDIN_DEF(party_destroy,"i"),
  16127. BUILDIN_DEF(is_clientver,"ii?"),
  16128. BUILDIN_DEF(getserverdef,"i"),
  16129. {NULL,NULL,NULL},
  16130. };