script.c 424 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415
  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/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/random.h"
  14. #include "../common/showmsg.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 <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <math.h>
  53. #ifndef WIN32
  54. #include <sys/time.h>
  55. #endif
  56. #include <time.h>
  57. #include <setjmp.h>
  58. #include <errno.h>
  59. #ifdef BETA_THREAD_TEST
  60. #include "../common/atomic.h"
  61. #include "../common/spinlock.h"
  62. #include "../common/thread.h"
  63. #include "../common/mutex.h"
  64. #endif
  65. ///////////////////////////////////////////////////////////////////////////////
  66. //## TODO possible enhancements: [FlavioJS]
  67. // - 'callfunc' supporting labels in the current npc "::LabelName"
  68. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  69. // - 'function FuncName;' function declarations reverting to global functions
  70. // if local label isn't found
  71. // - join callfunc and callsub's functionality
  72. // - remove dynamic allocation in add_word()
  73. // - remove GETVALUE / SETVALUE
  74. // - clean up the set_reg / set_val / setd_sub mess
  75. // - detect invalid label references at parse-time
  76. //
  77. // struct script_state* st;
  78. //
  79. /// Returns the script_data at the target index
  80. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  81. /// Returns if the stack contains data at the target index
  82. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  83. /// Returns the index of the last data in the stack
  84. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  85. /// Pushes an int into the stack
  86. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  87. /// Pushes a string into the stack (script engine frees it automatically)
  88. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  89. /// Pushes a copy of a string into the stack
  90. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  91. /// Pushes a constant string into the stack (must never change or be freed)
  92. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  93. /// Pushes a nil into the stack
  94. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  95. /// Pushes a copy of the data in the target index
  96. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  97. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  98. #define script_isint(st,i) data_isint(script_getdata(st,i))
  99. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  100. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  101. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  102. // Note: "top" functions/defines use indexes relative to the top of the stack
  103. // -1 is the index of the data at the top
  104. /// Returns the script_data at the target index relative to the top of the stack
  105. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  106. /// Pushes a copy of the data in the target index relative to the top of the stack
  107. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  108. /// Removes the range of values [start,end[ relative to the top of the stack
  109. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  110. //
  111. // struct script_data* data;
  112. //
  113. /// Returns if the script data is a string
  114. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  115. /// Returns if the script data is an int
  116. #define data_isint(data) ( (data)->type == C_INT )
  117. /// Returns if the script data is a reference
  118. #define data_isreference(data) ( (data)->type == C_NAME )
  119. /// Returns if the script data is a label
  120. #define data_islabel(data) ( (data)->type == C_POS )
  121. /// Returns if the script data is an internal script function label
  122. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  123. /// Returns if this is a reference to a constant
  124. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  125. /// Returns if this a reference to a param
  126. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  127. /// Returns if this a reference to a variable
  128. //##TODO confirm it's C_NAME [FlavioJS]
  129. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  130. /// Returns the unique id of the reference (id and index)
  131. #define reference_getuid(data) ( (data)->u.num )
  132. /// Returns the id of the reference
  133. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  134. /// Returns the array index of the reference
  135. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  136. /// Returns the name of the reference
  137. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  138. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  139. #define reference_getref(data) ( (data)->ref )
  140. /// Returns the value of the constant
  141. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  142. /// Returns the type of param
  143. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  144. /// Composes the uid of a reference from the id and the index
  145. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  146. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  147. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  148. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  149. #define FETCH(n, t) \
  150. if( script_hasdata(st,n) ) \
  151. (t)=script_getnum(st,n);
  152. /// Maximum amount of elements in script arrays
  153. #define SCRIPT_MAX_ARRAYSIZE 128
  154. #define SCRIPT_BLOCK_SIZE 512
  155. enum { LABEL_NEXTLINE=1,LABEL_START };
  156. /// temporary buffer for passing around compiled bytecode
  157. /// @see add_scriptb, set_label, parse_script
  158. static unsigned char* script_buf = NULL;
  159. static int script_pos = 0, script_size = 0;
  160. static inline int GETVALUE(const unsigned char* buf, int i)
  161. {
  162. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  163. }
  164. static inline void SETVALUE(unsigned char* buf, int i, int n)
  165. {
  166. buf[i] = GetByte(n, 0);
  167. buf[i+1] = GetByte(n, 1);
  168. buf[i+2] = GetByte(n, 2);
  169. }
  170. // String buffer structures.
  171. // str_data stores string information
  172. static struct str_data_struct {
  173. enum c_op type;
  174. int str;
  175. int backpatch;
  176. int label;
  177. int (*func)(struct script_state *st);
  178. int val;
  179. int next;
  180. } *str_data = NULL;
  181. static int str_data_size = 0; // size of the data
  182. static int str_num = LABEL_START; // next id to be assigned
  183. // str_buf holds the strings themselves
  184. static char *str_buf;
  185. static int str_size = 0; // size of the buffer
  186. static int str_pos = 0; // next position to be assigned
  187. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  188. #define SCRIPT_HASH_SIZE 1021
  189. int str_hash[SCRIPT_HASH_SIZE];
  190. // Specifies which string hashing method to use
  191. //#define SCRIPT_HASH_DJB2
  192. //#define SCRIPT_HASH_SDBM
  193. #define SCRIPT_HASH_ELF
  194. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  195. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  196. static int parse_options=0;
  197. DBMap* script_get_label_db(void){ return scriptlabel_db; }
  198. DBMap* script_get_userfunc_db(void){ return userfunc_db; }
  199. // important buildin function references for usage in scripts
  200. static int buildin_set_ref = 0;
  201. static int buildin_callsub_ref = 0;
  202. static int buildin_callfunc_ref = 0;
  203. static int buildin_getelementofarray_ref = 0;
  204. // Caches compiled autoscript item code.
  205. // Note: This is not cleared when reloading itemdb.
  206. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  207. struct Script_Config script_config = {
  208. 1, // warn_func_mismatch_argtypes
  209. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  210. 0, INT_MAX, // input_min_value/input_max_value
  211. "OnPCDieEvent", //die_event_name
  212. "OnPCKillEvent", //kill_pc_event_name
  213. "OnNPCKillEvent", //kill_mob_event_name
  214. "OnPCLoginEvent", //login_event_name
  215. "OnPCLogoutEvent", //logout_event_name
  216. "OnPCLoadMapEvent", //loadmap_event_name
  217. "OnPCBaseLvUpEvent", //baselvup_event_name
  218. "OnPCJobLvUpEvent", //joblvup_event_name
  219. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  220. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  221. };
  222. static jmp_buf error_jump;
  223. static char* error_msg;
  224. static const char* error_pos;
  225. static int error_report; // if the error should produce output
  226. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  227. // [Eoe / jA 1080, 1081, 1094, 1164]
  228. enum curly_type {
  229. TYPE_NULL = 0,
  230. TYPE_IF,
  231. TYPE_SWITCH,
  232. TYPE_WHILE,
  233. TYPE_FOR,
  234. TYPE_DO,
  235. TYPE_USERFUNC,
  236. TYPE_ARGLIST // function argument list
  237. };
  238. enum e_arglist
  239. {
  240. ARGLIST_UNDEFINED = 0,
  241. ARGLIST_NO_PAREN = 1,
  242. ARGLIST_PAREN = 2,
  243. };
  244. static struct {
  245. struct {
  246. enum curly_type type;
  247. int index;
  248. int count;
  249. int flag;
  250. struct linkdb_node *case_label;
  251. } curly[256]; // 右カッコの情報
  252. int curly_count; // 右カッコの数
  253. int index; // スクリプト内で使用した構文の数
  254. } syntax;
  255. const char* parse_curly_close(const char* p);
  256. const char* parse_syntax_close(const char* p);
  257. const char* parse_syntax_close_sub(const char* p,int* flag);
  258. const char* parse_syntax(const char* p);
  259. static int parse_syntax_for_flag = 0;
  260. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  261. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  262. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  263. int potion_target=0;
  264. c_op get_com(unsigned char *script,int *pos);
  265. int get_num(unsigned char *script,int *pos);
  266. typedef struct script_function {
  267. int (*func)(struct script_state *st);
  268. const char *name;
  269. const char *arg;
  270. } script_function;
  271. extern script_function buildin_func[];
  272. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  273. #ifdef BETA_THREAD_TEST
  274. /**
  275. * MySQL Query Slave
  276. **/
  277. static SPIN_LOCK queryThreadLock;
  278. static rAthread queryThread = NULL;
  279. static ramutex queryThreadMutex = NULL;
  280. static racond queryThreadCond = NULL;
  281. static volatile int32 queryThreadTerminate = 0;
  282. struct queryThreadEntry {
  283. bool ok;
  284. bool type; /* main db or log db? */
  285. struct script_state *st;
  286. };
  287. /* Ladies and Gentleman the Manager! */
  288. struct {
  289. struct queryThreadEntry **entry;/* array of structs */
  290. int count;
  291. int timer;/* used to receive processed entries */
  292. } queryThreadData;
  293. #endif
  294. /*==========================================
  295. * ローカルプロトタイプ宣言 (必要な物のみ)
  296. *------------------------------------------*/
  297. const char* parse_subexpr(const char* p,int limit);
  298. int run_func(struct script_state *st);
  299. enum {
  300. MF_NOMEMO, //0
  301. MF_NOTELEPORT,
  302. MF_NOSAVE,
  303. MF_NOBRANCH,
  304. MF_NOPENALTY,
  305. MF_NOZENYPENALTY,
  306. MF_PVP,
  307. MF_PVP_NOPARTY,
  308. MF_PVP_NOGUILD,
  309. MF_GVG,
  310. MF_GVG_NOPARTY, //10
  311. MF_NOTRADE,
  312. MF_NOSKILL,
  313. MF_NOWARP,
  314. MF_PARTYLOCK,
  315. MF_NOICEWALL,
  316. MF_SNOW,
  317. MF_FOG,
  318. MF_SAKURA,
  319. MF_LEAVES,
  320. /**
  321. * No longer available, keeping here just in case it's back someday. [Ind]
  322. **/
  323. //MF_RAIN, //20
  324. // 21 free
  325. MF_NOGO = 22,
  326. MF_CLOUDS,
  327. MF_CLOUDS2,
  328. MF_FIREWORKS,
  329. MF_GVG_CASTLE,
  330. MF_GVG_DUNGEON,
  331. MF_NIGHTENABLED,
  332. MF_NOBASEEXP,
  333. MF_NOJOBEXP, //30
  334. MF_NOMOBLOOT,
  335. MF_NOMVPLOOT,
  336. MF_NORETURN,
  337. MF_NOWARPTO,
  338. MF_NIGHTMAREDROP,
  339. MF_RESTRICTED,
  340. MF_NOCOMMAND,
  341. MF_NODROP,
  342. MF_JEXP,
  343. MF_BEXP, //40
  344. MF_NOVENDING,
  345. MF_LOADEVENT,
  346. MF_NOCHAT,
  347. MF_NOEXPPENALTY,
  348. MF_GUILDLOCK,
  349. MF_TOWN,
  350. MF_AUTOTRADE,
  351. MF_ALLOWKS,
  352. MF_MONSTER_NOTELEPORT,
  353. MF_PVP_NOCALCRANK, //50
  354. MF_BATTLEGROUND,
  355. MF_RESET
  356. };
  357. const char* script_op2name(int op)
  358. {
  359. #define RETURN_OP_NAME(type) case type: return #type
  360. switch( op )
  361. {
  362. RETURN_OP_NAME(C_NOP);
  363. RETURN_OP_NAME(C_POS);
  364. RETURN_OP_NAME(C_INT);
  365. RETURN_OP_NAME(C_PARAM);
  366. RETURN_OP_NAME(C_FUNC);
  367. RETURN_OP_NAME(C_STR);
  368. RETURN_OP_NAME(C_CONSTSTR);
  369. RETURN_OP_NAME(C_ARG);
  370. RETURN_OP_NAME(C_NAME);
  371. RETURN_OP_NAME(C_EOL);
  372. RETURN_OP_NAME(C_RETINFO);
  373. RETURN_OP_NAME(C_USERFUNC);
  374. RETURN_OP_NAME(C_USERFUNC_POS);
  375. // operators
  376. RETURN_OP_NAME(C_OP3);
  377. RETURN_OP_NAME(C_LOR);
  378. RETURN_OP_NAME(C_LAND);
  379. RETURN_OP_NAME(C_LE);
  380. RETURN_OP_NAME(C_LT);
  381. RETURN_OP_NAME(C_GE);
  382. RETURN_OP_NAME(C_GT);
  383. RETURN_OP_NAME(C_EQ);
  384. RETURN_OP_NAME(C_NE);
  385. RETURN_OP_NAME(C_XOR);
  386. RETURN_OP_NAME(C_OR);
  387. RETURN_OP_NAME(C_AND);
  388. RETURN_OP_NAME(C_ADD);
  389. RETURN_OP_NAME(C_SUB);
  390. RETURN_OP_NAME(C_MUL);
  391. RETURN_OP_NAME(C_DIV);
  392. RETURN_OP_NAME(C_MOD);
  393. RETURN_OP_NAME(C_NEG);
  394. RETURN_OP_NAME(C_LNOT);
  395. RETURN_OP_NAME(C_NOT);
  396. RETURN_OP_NAME(C_R_SHIFT);
  397. RETURN_OP_NAME(C_L_SHIFT);
  398. default:
  399. ShowDebug("script_op2name: unexpected op=%d\n", op);
  400. return "???";
  401. }
  402. #undef RETURN_OP_NAME
  403. }
  404. #ifdef DEBUG_DUMP_STACK
  405. static void script_dump_stack(struct script_state* st)
  406. {
  407. int i;
  408. ShowMessage("\tstart = %d\n", st->start);
  409. ShowMessage("\tend = %d\n", st->end);
  410. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  411. ShowMessage("\tsp = %d\n", st->stack->sp);
  412. for( i = 0; i < st->stack->sp; ++i )
  413. {
  414. struct script_data* data = &st->stack->stack_data[i];
  415. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  416. switch( data->type )
  417. {
  418. case C_INT:
  419. case C_POS:
  420. ShowMessage(" %d\n", data->u.num);
  421. break;
  422. case C_STR:
  423. case C_CONSTSTR:
  424. ShowMessage(" \"%s\"\n", data->u.str);
  425. break;
  426. case C_NAME:
  427. 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));
  428. break;
  429. case C_RETINFO:
  430. {
  431. struct script_retinfo* ri = data->u.ri;
  432. 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);
  433. }
  434. break;
  435. default:
  436. ShowMessage("\n");
  437. break;
  438. }
  439. }
  440. }
  441. #endif
  442. /// Reports on the console the src of a script error.
  443. static void script_reportsrc(struct script_state *st)
  444. {
  445. struct block_list* bl;
  446. if( st->oid == 0 )
  447. return; //Can't report source.
  448. bl = map_id2bl(st->oid);
  449. if( bl == NULL )
  450. return;
  451. switch( bl->type )
  452. {
  453. case BL_NPC:
  454. if( bl->m >= 0 )
  455. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  456. else
  457. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  458. break;
  459. default:
  460. if( bl->m >= 0 )
  461. 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);
  462. else
  463. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  464. break;
  465. }
  466. }
  467. /// Reports on the console information about the script data.
  468. static void script_reportdata(struct script_data* data)
  469. {
  470. if( data == NULL )
  471. return;
  472. switch( data->type )
  473. {
  474. case C_NOP:// no value
  475. ShowDebug("Data: nothing (nil)\n");
  476. break;
  477. case C_INT:// number
  478. ShowDebug("Data: number value=%d\n", data->u.num);
  479. break;
  480. case C_STR:
  481. case C_CONSTSTR:// string
  482. if( data->u.str )
  483. {
  484. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  485. }
  486. else
  487. {
  488. ShowDebug("Data: string value=NULL\n");
  489. }
  490. break;
  491. case C_NAME:// reference
  492. if( reference_tovariable(data) )
  493. {// variable
  494. const char* name = reference_getname(data);
  495. if( not_array_variable(*name) )
  496. ShowDebug("Data: variable name='%s'\n", name);
  497. else
  498. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  499. }
  500. else if( reference_toconstant(data) )
  501. {// constant
  502. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  503. }
  504. else if( reference_toparam(data) )
  505. {// param
  506. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  507. }
  508. else
  509. {// ???
  510. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  511. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  512. }
  513. break;
  514. case C_POS:// label
  515. ShowDebug("Data: label pos=%d\n", data->u.num);
  516. break;
  517. default:
  518. ShowDebug("Data: %s\n", script_op2name(data->type));
  519. break;
  520. }
  521. }
  522. /// Reports on the console information about the current built-in function.
  523. static void script_reportfunc(struct script_state* st)
  524. {
  525. int i, params, id;
  526. struct script_data* data;
  527. if( !script_hasdata(st,0) )
  528. {// no stack
  529. return;
  530. }
  531. data = script_getdata(st,0);
  532. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  533. {// script currently not executing a built-in function or corrupt stack
  534. return;
  535. }
  536. id = reference_getid(data);
  537. params = script_lastdata(st)-1;
  538. if( params > 0 )
  539. {
  540. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  541. for( i = 2; i <= script_lastdata(st); i++ )
  542. {
  543. script_reportdata(script_getdata(st,i));
  544. }
  545. }
  546. else
  547. {
  548. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  549. }
  550. }
  551. /*==========================================
  552. * エラーメッセージ出力
  553. *------------------------------------------*/
  554. static void disp_error_message2(const char *mes,const char *pos,int report)
  555. {
  556. error_msg = aStrdup(mes);
  557. error_pos = pos;
  558. error_report = report;
  559. longjmp( error_jump, 1 );
  560. }
  561. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  562. /// Checks event parameter validity
  563. static void check_event(struct script_state *st, const char *evt)
  564. {
  565. if( evt && evt[0] && !stristr(evt, "::On") )
  566. {
  567. if( npc_event_isspecial(evt) )
  568. {
  569. ; // portable small/large monsters or other attributes
  570. }
  571. else
  572. {
  573. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  574. script_reportsrc(st);
  575. }
  576. }
  577. }
  578. /*==========================================
  579. * Hashes the input string
  580. *------------------------------------------*/
  581. static unsigned int calc_hash(const char* p)
  582. {
  583. unsigned int h;
  584. #if defined(SCRIPT_HASH_DJB2)
  585. h = 5381;
  586. while( *p ) // hash*33 + c
  587. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  588. #elif defined(SCRIPT_HASH_SDBM)
  589. h = 0;
  590. while( *p ) // hash*65599 + c
  591. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  592. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  593. h = 0;
  594. while( *p ){
  595. unsigned int g;
  596. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  597. g = h & 0xF0000000;
  598. if( g )
  599. {
  600. h ^= g >> 24;
  601. h &= ~g;
  602. }
  603. }
  604. #else // athena hash
  605. h = 0;
  606. while( *p )
  607. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  608. #endif
  609. return h % SCRIPT_HASH_SIZE;
  610. }
  611. /*==========================================
  612. * str_data manipulation functions
  613. *------------------------------------------*/
  614. /// Looks up string using the provided id.
  615. const char* get_str(int id)
  616. {
  617. Assert( id >= LABEL_START && id < str_size );
  618. return str_buf+str_data[id].str;
  619. }
  620. /// Returns the uid of the string, or -1.
  621. static int search_str(const char* p)
  622. {
  623. int i;
  624. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  625. if( strcasecmp(get_str(i),p) == 0 )
  626. return i;
  627. return -1;
  628. }
  629. /// Stores a copy of the string and returns its id.
  630. /// If an identical string is already present, returns its id instead.
  631. int add_str(const char* p)
  632. {
  633. int i, h;
  634. int len;
  635. h = calc_hash(p);
  636. if( str_hash[h] == 0 )
  637. {// empty bucket, add new node here
  638. str_hash[h] = str_num;
  639. }
  640. else
  641. {// scan for end of list, or occurence of identical string
  642. for( i = str_hash[h]; ; i = str_data[i].next )
  643. {
  644. if( strcasecmp(get_str(i),p) == 0 )
  645. return i; // string already in list
  646. if( str_data[i].next == 0 )
  647. break; // reached the end
  648. }
  649. // append node to end of list
  650. str_data[i].next = str_num;
  651. }
  652. // grow list if neccessary
  653. if( str_num >= str_data_size )
  654. {
  655. str_data_size += 128;
  656. RECREATE(str_data,struct str_data_struct,str_data_size);
  657. memset(str_data + (str_data_size - 128), '\0', 128);
  658. }
  659. len=(int)strlen(p);
  660. // grow string buffer if neccessary
  661. while( str_pos+len+1 >= str_size )
  662. {
  663. str_size += 256;
  664. RECREATE(str_buf,char,str_size);
  665. memset(str_buf + (str_size - 256), '\0', 256);
  666. }
  667. safestrncpy(str_buf+str_pos, p, len+1);
  668. str_data[str_num].type = C_NOP;
  669. str_data[str_num].str = str_pos;
  670. str_data[str_num].next = 0;
  671. str_data[str_num].func = NULL;
  672. str_data[str_num].backpatch = -1;
  673. str_data[str_num].label = -1;
  674. str_pos += len+1;
  675. return str_num++;
  676. }
  677. /// Appends 1 byte to the script buffer.
  678. static void add_scriptb(int a)
  679. {
  680. if( script_pos+1 >= script_size )
  681. {
  682. script_size += SCRIPT_BLOCK_SIZE;
  683. RECREATE(script_buf,unsigned char,script_size);
  684. }
  685. script_buf[script_pos++] = (uint8)(a);
  686. }
  687. /// Appends a c_op value to the script buffer.
  688. /// The value is variable-length encoded into 8-bit blocks.
  689. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  690. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  691. static void add_scriptc(int a)
  692. {
  693. while( a >= 0x40 )
  694. {
  695. add_scriptb((a&0x3f)|0x40);
  696. a = (a - 0x40) >> 6;
  697. }
  698. add_scriptb(a);
  699. }
  700. /// Appends an integer value to the script buffer.
  701. /// The value is variable-length encoded into 8-bit blocks.
  702. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  703. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  704. static void add_scripti(int a)
  705. {
  706. while( a >= 0x40 )
  707. {
  708. add_scriptb((a&0x3f)|0xc0);
  709. a = (a - 0x40) >> 6;
  710. }
  711. add_scriptb(a|0x80);
  712. }
  713. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  714. ///
  715. /// @param l The id of the str_data entry
  716. // 最大16Mまで
  717. static void add_scriptl(int l)
  718. {
  719. int backpatch = str_data[l].backpatch;
  720. switch(str_data[l].type){
  721. case C_POS:
  722. case C_USERFUNC_POS:
  723. add_scriptc(C_POS);
  724. add_scriptb(str_data[l].label);
  725. add_scriptb(str_data[l].label>>8);
  726. add_scriptb(str_data[l].label>>16);
  727. break;
  728. case C_NOP:
  729. case C_USERFUNC:
  730. // ラベルの可能性があるのでbackpatch用データ埋め込み
  731. add_scriptc(C_NAME);
  732. str_data[l].backpatch = script_pos;
  733. add_scriptb(backpatch);
  734. add_scriptb(backpatch>>8);
  735. add_scriptb(backpatch>>16);
  736. break;
  737. case C_INT:
  738. add_scripti(abs(str_data[l].val));
  739. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  740. add_scriptc(C_NEG);
  741. break;
  742. default: // assume C_NAME
  743. add_scriptc(C_NAME);
  744. add_scriptb(l);
  745. add_scriptb(l>>8);
  746. add_scriptb(l>>16);
  747. break;
  748. }
  749. }
  750. /*==========================================
  751. * ラベルを解決する
  752. *------------------------------------------*/
  753. void set_label(int l,int pos, const char* script_pos)
  754. {
  755. int i,next;
  756. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  757. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  758. disp_error_message("set_label: invalid label name",script_pos);
  759. return;
  760. }
  761. if(str_data[l].label!=-1){
  762. disp_error_message("set_label: dup label ",script_pos);
  763. return;
  764. }
  765. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  766. str_data[l].label=pos;
  767. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  768. next=GETVALUE(script_buf,i);
  769. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  770. SETVALUE(script_buf,i,pos);
  771. i=next;
  772. }
  773. }
  774. /// Skips spaces and/or comments.
  775. const char* skip_space(const char* p)
  776. {
  777. if( p == NULL )
  778. return NULL;
  779. for(;;)
  780. {
  781. while( ISSPACE(*p) )
  782. ++p;
  783. if( *p == '/' && p[1] == '/' )
  784. {// line comment
  785. while(*p && *p!='\n')
  786. ++p;
  787. }
  788. else if( *p == '/' && p[1] == '*' )
  789. {// block comment
  790. p += 2;
  791. for(;;)
  792. {
  793. if( *p == '\0' )
  794. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  795. if( *p == '*' && p[1] == '/' )
  796. {// end of block comment
  797. p += 2;
  798. break;
  799. }
  800. ++p;
  801. }
  802. }
  803. else
  804. break;
  805. }
  806. return p;
  807. }
  808. /// Skips a word.
  809. /// A word consists of undercores and/or alfanumeric characters,
  810. /// and valid variable prefixes/postfixes.
  811. static
  812. const char* skip_word(const char* p)
  813. {
  814. // prefix
  815. switch( *p )
  816. {
  817. case '@':// temporary char variable
  818. ++p; break;
  819. case '#':// account variable
  820. p += ( p[1] == '#' ? 2 : 1 ); break;
  821. case '\'':// instance variable
  822. ++p; break;
  823. case '.':// npc variable
  824. p += ( p[1] == '@' ? 2 : 1 ); break;
  825. case '$':// global variable
  826. p += ( p[1] == '@' ? 2 : 1 ); break;
  827. }
  828. while( ISALNUM(*p) || *p == '_' )
  829. ++p;
  830. // postfix
  831. if( *p == '$' )// string
  832. p++;
  833. return p;
  834. }
  835. /// Adds a word to str_data.
  836. /// @see skip_word
  837. /// @see add_str
  838. static
  839. int add_word(const char* p)
  840. {
  841. char* word;
  842. int len;
  843. int i;
  844. // Check for a word
  845. len = skip_word(p) - p;
  846. if( len == 0 )
  847. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  848. // Duplicate the word
  849. word = (char*)aMalloc(len+1);
  850. memcpy(word, p, len);
  851. word[len] = 0;
  852. // add the word
  853. i = add_str(word);
  854. aFree(word);
  855. return i;
  856. }
  857. /// Parses a function call.
  858. /// The argument list can have parenthesis or not.
  859. /// The number of arguments is checked.
  860. static
  861. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  862. {
  863. const char* p2;
  864. const char* arg=NULL;
  865. int func;
  866. func = add_word(p);
  867. if( str_data[func].type == C_FUNC ){
  868. // buildin function
  869. add_scriptl(func);
  870. add_scriptc(C_ARG);
  871. arg = buildin_func[str_data[func].val].arg;
  872. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  873. // script defined function
  874. add_scriptl(buildin_callsub_ref);
  875. add_scriptc(C_ARG);
  876. add_scriptl(func);
  877. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  878. if( *arg == 0 )
  879. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  880. if( *arg != '*' )
  881. ++arg; // count func as argument
  882. } else {
  883. #ifdef SCRIPT_CALLFUNC_CHECK
  884. const char* name = get_str(func);
  885. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  886. #endif
  887. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  888. #ifdef SCRIPT_CALLFUNC_CHECK
  889. } else {;
  890. add_scriptl(buildin_callfunc_ref);
  891. add_scriptc(C_ARG);
  892. add_scriptc(C_STR);
  893. while( *name ) add_scriptb(*name ++);
  894. add_scriptb(0);
  895. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  896. if( *arg != '*' ) ++ arg;
  897. }
  898. #endif
  899. }
  900. p = skip_word(p);
  901. p = skip_space(p);
  902. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  903. syntax.curly[syntax.curly_count].count = 0;
  904. if( *p == ';' )
  905. {// <func name> ';'
  906. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  907. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  908. {// <func name> '(' ')'
  909. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  910. p = p2;
  911. /*
  912. } else if( 0 && require_paren && *p != '(' )
  913. {// <func name>
  914. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  915. */
  916. } else
  917. {// <func name> <arg list>
  918. if( require_paren ){
  919. if( *p != '(' )
  920. disp_error_message("need '('",p);
  921. ++p; // skip '('
  922. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  923. } else if( *p == '(' ){
  924. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  925. } else {
  926. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  927. }
  928. ++syntax.curly_count;
  929. while( *arg ) {
  930. p2=parse_subexpr(p,-1);
  931. if( p == p2 )
  932. break; // not an argument
  933. if( *arg != '*' )
  934. ++arg; // next argument
  935. p=skip_space(p2);
  936. if( *arg == 0 || *p != ',' )
  937. break; // no more arguments
  938. ++p; // skip comma
  939. }
  940. --syntax.curly_count;
  941. }
  942. if( *arg && *arg != '?' && *arg != '*' )
  943. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  944. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  945. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  946. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  947. if( *p != ')' )
  948. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  949. ++p;
  950. }
  951. add_scriptc(C_FUNC);
  952. return p;
  953. }
  954. /// Processes end of logical script line.
  955. /// @param first When true, only fix up scheduling data is initialized
  956. /// @param p Script position for error reporting in set_label
  957. static void parse_nextline(bool first, const char* p)
  958. {
  959. if( !first )
  960. {
  961. add_scriptc(C_EOL); // mark end of line for stack cleanup
  962. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  963. }
  964. // initialize data for new '-' label fix up scheduling
  965. str_data[LABEL_NEXTLINE].type = C_NOP;
  966. str_data[LABEL_NEXTLINE].backpatch = -1;
  967. str_data[LABEL_NEXTLINE].label = -1;
  968. }
  969. /// Parse a variable assignment using the direct equals operator
  970. /// @param p script position where the function should run from
  971. /// @return NULL if not a variable assignment, the new position otherwise
  972. const char* parse_variable(const char* p) {
  973. int i, j, word;
  974. c_op type = C_NOP;
  975. const char *p2 = NULL;
  976. const char *var = p;
  977. // skip the variable where applicable
  978. p = skip_word(p);
  979. p = skip_space(p);
  980. if( p == NULL ) {// end of the line or invalid buffer
  981. return NULL;
  982. }
  983. if( *p == '[' ) {// array variable so process the array as appropriate
  984. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  985. if( *p ++ == ']' && --(j) == 0 ) break;
  986. if( *p == '[' ) ++ j;
  987. }
  988. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  989. disp_error_message("Missing right expression or closing bracket for variable.", p);
  990. }
  991. }
  992. if( type == C_NOP &&
  993. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  994. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  995. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  996. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  997. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  998. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  999. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1000. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1001. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1002. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1003. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  1004. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  1005. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1006. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1007. ) )
  1008. {// failed to find a matching operator combination so invalid
  1009. return NULL;
  1010. }
  1011. switch( type ) {
  1012. case C_EQ: {// incremental modifier
  1013. p = skip_space( &p[1] );
  1014. }
  1015. break;
  1016. case C_L_SHIFT:
  1017. case C_R_SHIFT: {// left or right shift modifier
  1018. p = skip_space( &p[3] );
  1019. }
  1020. break;
  1021. default: {// normal incremental command
  1022. p = skip_space( &p[2] );
  1023. }
  1024. }
  1025. if( p == NULL ) {// end of line or invalid buffer
  1026. return NULL;
  1027. }
  1028. // push the set function onto the stack
  1029. add_scriptl(buildin_set_ref);
  1030. add_scriptc(C_ARG);
  1031. // always append parenthesis to avoid errors
  1032. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1033. syntax.curly[syntax.curly_count].count = 0;
  1034. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1035. // increment the total curly count for the position in the script
  1036. ++ syntax.curly_count;
  1037. // parse the variable currently being modified
  1038. word = add_word(var);
  1039. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  1040. {// cannot assign a variable which exists as a function or label
  1041. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1042. }
  1043. if( p2 ) {// process the variable index
  1044. const char* p3 = NULL;
  1045. // push the getelementofarray method into the stack
  1046. add_scriptl(buildin_getelementofarray_ref);
  1047. add_scriptc(C_ARG);
  1048. add_scriptl(word);
  1049. // process the sub-expression for this assignment
  1050. p3 = parse_subexpr(p2 + 1, 1);
  1051. p3 = skip_space(p3);
  1052. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1053. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1054. }
  1055. // push the closing function stack operator onto the stack
  1056. add_scriptc(C_FUNC);
  1057. p3 ++;
  1058. } else {// simply push the variable or value onto the stack
  1059. add_scriptl(word);
  1060. }
  1061. if( type != C_EQ )
  1062. add_scriptc(C_REF);
  1063. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1064. add_scripti(1);
  1065. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1066. } else {// process the value as an expression
  1067. p = parse_subexpr(p, -1);
  1068. if( type != C_EQ )
  1069. {// push the type of modifier onto the stack
  1070. add_scriptc(type);
  1071. }
  1072. }
  1073. // decrement the curly count for the position within the script
  1074. -- syntax.curly_count;
  1075. // close the script by appending the function operator
  1076. add_scriptc(C_FUNC);
  1077. // push the buffer from the method
  1078. return p;
  1079. }
  1080. /*==========================================
  1081. * 項の解析
  1082. *------------------------------------------*/
  1083. const char* parse_simpleexpr(const char *p)
  1084. {
  1085. int i;
  1086. p=skip_space(p);
  1087. if(*p==';' || *p==',')
  1088. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  1089. if(*p=='('){
  1090. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1091. ++syntax.curly[i].count;
  1092. p=parse_subexpr(p+1,-1);
  1093. p=skip_space(p);
  1094. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1095. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1096. ){
  1097. if( *p == ',' ){
  1098. syntax.curly[i].flag = ARGLIST_PAREN;
  1099. return p;
  1100. } else
  1101. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1102. }
  1103. if( *p != ')' )
  1104. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  1105. ++p;
  1106. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  1107. char *np;
  1108. i=strtoul(p,&np,0);
  1109. add_scripti(i);
  1110. p=np;
  1111. } else if(*p=='"'){
  1112. add_scriptc(C_STR);
  1113. p++;
  1114. while( *p && *p != '"' ){
  1115. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1116. {
  1117. char buf[8];
  1118. size_t len = skip_escaped_c(p) - p;
  1119. size_t n = sv_unescape_c(buf, p, len);
  1120. if( n != 1 )
  1121. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1122. p += len;
  1123. add_scriptb(*buf);
  1124. continue;
  1125. }
  1126. else if( *p == '\n' )
  1127. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1128. add_scriptb(*p++);
  1129. }
  1130. if(!*p)
  1131. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1132. add_scriptb(0);
  1133. p++; //'"'
  1134. } else {
  1135. int l;
  1136. const char* pv;
  1137. // label , register , function etc
  1138. if(skip_word(p)==p)
  1139. disp_error_message("parse_simpleexpr: unexpected character",p);
  1140. l=add_word(p);
  1141. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1142. return parse_callfunc(p,1,0);
  1143. #ifdef SCRIPT_CALLFUNC_CHECK
  1144. else {
  1145. const char* name = get_str(l);
  1146. if( strdb_get(userfunc_db,name) != NULL ) {
  1147. return parse_callfunc(p,1,1);
  1148. }
  1149. }
  1150. #endif
  1151. if( (pv = parse_variable(p)) )
  1152. {// successfully processed a variable assignment
  1153. return pv;
  1154. }
  1155. p=skip_word(p);
  1156. if( *p == '[' ){
  1157. // array(name[i] => getelementofarray(name,i) )
  1158. add_scriptl(buildin_getelementofarray_ref);
  1159. add_scriptc(C_ARG);
  1160. add_scriptl(l);
  1161. p=parse_subexpr(p+1,-1);
  1162. p=skip_space(p);
  1163. if( *p != ']' )
  1164. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  1165. ++p;
  1166. add_scriptc(C_FUNC);
  1167. }else
  1168. add_scriptl(l);
  1169. }
  1170. return p;
  1171. }
  1172. /*==========================================
  1173. * 式の解析
  1174. *------------------------------------------*/
  1175. const char* parse_subexpr(const char* p,int limit)
  1176. {
  1177. int op,opl,len;
  1178. const char* tmpp;
  1179. p=skip_space(p);
  1180. if( *p == '-' ){
  1181. tmpp = skip_space(p+1);
  1182. if( *tmpp == ';' || *tmpp == ',' ){
  1183. add_scriptl(LABEL_NEXTLINE);
  1184. p++;
  1185. return p;
  1186. }
  1187. }
  1188. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1189. p=parse_subexpr(p+1,10);
  1190. add_scriptc(op);
  1191. } else
  1192. p=parse_simpleexpr(p);
  1193. p=skip_space(p);
  1194. while((
  1195. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1196. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1197. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1198. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1199. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1200. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1201. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1202. (op=C_AND,opl=6,len=1,*p=='&') ||
  1203. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1204. (op=C_OR,opl=5,len=1,*p=='|') ||
  1205. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1206. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1207. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1208. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1209. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1210. (op=C_GT,opl=3,len=1,*p=='>') ||
  1211. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1212. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1213. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1214. p+=len;
  1215. if(op == C_OP3) {
  1216. p=parse_subexpr(p,-1);
  1217. p=skip_space(p);
  1218. if( *(p++) != ':')
  1219. disp_error_message("parse_subexpr: need ':'", p-1);
  1220. p=parse_subexpr(p,-1);
  1221. } else {
  1222. p=parse_subexpr(p,opl);
  1223. }
  1224. add_scriptc(op);
  1225. p=skip_space(p);
  1226. }
  1227. return p; /* return first untreated operator */
  1228. }
  1229. /*==========================================
  1230. * 式の評価
  1231. *------------------------------------------*/
  1232. const char* parse_expr(const char *p)
  1233. {
  1234. switch(*p){
  1235. case ')': case ';': case ':': case '[': case ']':
  1236. case '}':
  1237. disp_error_message("parse_expr: unexpected char",p);
  1238. }
  1239. p=parse_subexpr(p,-1);
  1240. return p;
  1241. }
  1242. /*==========================================
  1243. * 行の解析
  1244. *------------------------------------------*/
  1245. const char* parse_line(const char* p)
  1246. {
  1247. const char* p2;
  1248. p=skip_space(p);
  1249. if(*p==';') {
  1250. // if(); for(); while(); のために閉じ判定
  1251. p = parse_syntax_close(p + 1);
  1252. return p;
  1253. }
  1254. if(*p==')' && parse_syntax_for_flag)
  1255. return p+1;
  1256. p = skip_space(p);
  1257. if(p[0] == '{') {
  1258. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1259. syntax.curly[syntax.curly_count].count = -1;
  1260. syntax.curly[syntax.curly_count].index = -1;
  1261. syntax.curly_count++;
  1262. return p + 1;
  1263. } else if(p[0] == '}') {
  1264. return parse_curly_close(p);
  1265. }
  1266. // 構文関連の処理
  1267. p2 = parse_syntax(p);
  1268. if(p2 != NULL)
  1269. return p2;
  1270. // attempt to process a variable assignment
  1271. p2 = parse_variable(p);
  1272. if( p2 != NULL )
  1273. {// variable assignment processed so leave the method
  1274. return parse_syntax_close(p2 + 1);
  1275. }
  1276. p = parse_callfunc(p,0,0);
  1277. p = skip_space(p);
  1278. if(parse_syntax_for_flag) {
  1279. if( *p != ')' )
  1280. disp_error_message("parse_line: need ')'",p);
  1281. } else {
  1282. if( *p != ';' )
  1283. disp_error_message("parse_line: need ';'",p);
  1284. }
  1285. // if, for , while の閉じ判定
  1286. p = parse_syntax_close(p+1);
  1287. return p;
  1288. }
  1289. // { ... } の閉じ処理
  1290. const char* parse_curly_close(const char* p)
  1291. {
  1292. if(syntax.curly_count <= 0) {
  1293. disp_error_message("parse_curly_close: unexpected string",p);
  1294. return p + 1;
  1295. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1296. syntax.curly_count--;
  1297. // if, for , while の閉じ判定
  1298. p = parse_syntax_close(p + 1);
  1299. return p;
  1300. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1301. // switch() 閉じ判定
  1302. int pos = syntax.curly_count-1;
  1303. char label[256];
  1304. int l;
  1305. // 一時変数を消す
  1306. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1307. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1308. parse_line(label);
  1309. syntax.curly_count--;
  1310. // 無条件で終了ポインタに移動
  1311. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1312. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1313. parse_line(label);
  1314. syntax.curly_count--;
  1315. // 現在地のラベルを付ける
  1316. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1317. l=add_str(label);
  1318. set_label(l,script_pos, p);
  1319. if(syntax.curly[pos].flag) {
  1320. // default が存在する
  1321. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1322. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1323. parse_line(label);
  1324. syntax.curly_count--;
  1325. }
  1326. // 終了ラベルを付ける
  1327. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1328. l=add_str(label);
  1329. set_label(l,script_pos, p);
  1330. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1331. syntax.curly_count--;
  1332. // if, for , while の閉じ判定
  1333. p = parse_syntax_close(p + 1);
  1334. return p;
  1335. } else {
  1336. disp_error_message("parse_curly_close: unexpected string",p);
  1337. return p + 1;
  1338. }
  1339. }
  1340. // 構文関連の処理
  1341. // break, case, continue, default, do, for, function,
  1342. // if, switch, while をこの内部で処理します。
  1343. const char* parse_syntax(const char* p)
  1344. {
  1345. const char *p2 = skip_word(p);
  1346. switch(*p) {
  1347. case 'B':
  1348. case 'b':
  1349. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1350. // break の処理
  1351. char label[256];
  1352. int pos = syntax.curly_count - 1;
  1353. while(pos >= 0) {
  1354. if(syntax.curly[pos].type == TYPE_DO) {
  1355. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1356. break;
  1357. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1358. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1359. break;
  1360. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1361. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1362. break;
  1363. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1364. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1365. break;
  1366. }
  1367. pos--;
  1368. }
  1369. if(pos < 0) {
  1370. disp_error_message("parse_syntax: unexpected 'break'",p);
  1371. } else {
  1372. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1373. parse_line(label);
  1374. syntax.curly_count--;
  1375. }
  1376. p = skip_space(p2);
  1377. if(*p != ';')
  1378. disp_error_message("parse_syntax: need ';'",p);
  1379. // if, for , while の閉じ判定
  1380. p = parse_syntax_close(p + 1);
  1381. return p;
  1382. }
  1383. break;
  1384. case 'c':
  1385. case 'C':
  1386. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1387. // case の処理
  1388. int pos = syntax.curly_count-1;
  1389. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1390. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1391. return p+1;
  1392. } else {
  1393. char label[256];
  1394. int l,v;
  1395. char *np;
  1396. if(syntax.curly[pos].count != 1) {
  1397. // FALLTHRU 用のジャンプ
  1398. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1399. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1400. parse_line(label);
  1401. syntax.curly_count--;
  1402. // 現在地のラベルを付ける
  1403. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1404. l=add_str(label);
  1405. set_label(l,script_pos, p);
  1406. }
  1407. // switch 判定文
  1408. p = skip_space(p2);
  1409. if(p == p2) {
  1410. disp_error_message("parse_syntax: expect space ' '",p);
  1411. }
  1412. // check whether case label is integer or not
  1413. v = strtol(p,&np,0);
  1414. if(np == p) { //Check for constants
  1415. p2 = skip_word(p);
  1416. v = p2-p; // length of word at p2
  1417. memcpy(label,p,v);
  1418. label[v]='\0';
  1419. if( !script_get_constant(label, &v) )
  1420. disp_error_message("parse_syntax: 'case' label not integer",p);
  1421. p = skip_word(p);
  1422. } else { //Numeric value
  1423. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1424. p++;
  1425. p = skip_word(p);
  1426. if(np != p)
  1427. disp_error_message("parse_syntax: 'case' label not integer",np);
  1428. }
  1429. p = skip_space(p);
  1430. if(*p != ':')
  1431. disp_error_message("parse_syntax: expect ':'",p);
  1432. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1433. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1434. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1435. // 2回parse しないとダメ
  1436. p2 = parse_line(label);
  1437. parse_line(p2);
  1438. syntax.curly_count--;
  1439. if(syntax.curly[pos].count != 1) {
  1440. // FALLTHRU 終了後のラベル
  1441. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1442. l=add_str(label);
  1443. set_label(l,script_pos,p);
  1444. }
  1445. // check duplication of case label [Rayce]
  1446. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1447. disp_error_message("parse_syntax: dup 'case'",p);
  1448. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1449. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1450. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1451. parse_line(label);
  1452. syntax.curly_count--;
  1453. syntax.curly[pos].count++;
  1454. }
  1455. return p + 1;
  1456. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1457. // continue の処理
  1458. char label[256];
  1459. int pos = syntax.curly_count - 1;
  1460. while(pos >= 0) {
  1461. if(syntax.curly[pos].type == TYPE_DO) {
  1462. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1463. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1464. break;
  1465. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1466. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1467. break;
  1468. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1469. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1470. break;
  1471. }
  1472. pos--;
  1473. }
  1474. if(pos < 0) {
  1475. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1476. } else {
  1477. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1478. parse_line(label);
  1479. syntax.curly_count--;
  1480. }
  1481. p = skip_space(p2);
  1482. if(*p != ';')
  1483. disp_error_message("parse_syntax: need ';'",p);
  1484. // if, for , while の閉じ判定
  1485. p = parse_syntax_close(p + 1);
  1486. return p;
  1487. }
  1488. break;
  1489. case 'd':
  1490. case 'D':
  1491. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1492. // switch - default の処理
  1493. int pos = syntax.curly_count-1;
  1494. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1495. disp_error_message("parse_syntax: unexpected 'default'",p);
  1496. } else if(syntax.curly[pos].flag) {
  1497. disp_error_message("parse_syntax: dup 'default'",p);
  1498. } else {
  1499. char label[256];
  1500. int l;
  1501. // 現在地のラベルを付ける
  1502. p = skip_space(p2);
  1503. if(*p != ':') {
  1504. disp_error_message("parse_syntax: need ':'",p);
  1505. }
  1506. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1507. l=add_str(label);
  1508. set_label(l,script_pos,p);
  1509. // 無条件で次のリンクに飛ばす
  1510. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1511. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1512. parse_line(label);
  1513. syntax.curly_count--;
  1514. // default のラベルを付ける
  1515. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1516. l=add_str(label);
  1517. set_label(l,script_pos,p);
  1518. syntax.curly[syntax.curly_count - 1].flag = 1;
  1519. syntax.curly[pos].count++;
  1520. }
  1521. return p + 1;
  1522. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1523. int l;
  1524. char label[256];
  1525. p=skip_space(p2);
  1526. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1527. syntax.curly[syntax.curly_count].count = 1;
  1528. syntax.curly[syntax.curly_count].index = syntax.index++;
  1529. syntax.curly[syntax.curly_count].flag = 0;
  1530. // 現在地のラベル形成する
  1531. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1532. l=add_str(label);
  1533. set_label(l,script_pos,p);
  1534. syntax.curly_count++;
  1535. return p;
  1536. }
  1537. break;
  1538. case 'f':
  1539. case 'F':
  1540. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1541. int l;
  1542. char label[256];
  1543. int pos = syntax.curly_count;
  1544. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1545. syntax.curly[syntax.curly_count].count = 1;
  1546. syntax.curly[syntax.curly_count].index = syntax.index++;
  1547. syntax.curly[syntax.curly_count].flag = 0;
  1548. syntax.curly_count++;
  1549. p=skip_space(p2);
  1550. if(*p != '(')
  1551. disp_error_message("parse_syntax: need '('",p);
  1552. p++;
  1553. // 初期化文を実行する
  1554. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1555. p=parse_line(p);
  1556. syntax.curly_count--;
  1557. // 条件判断開始のラベル形成する
  1558. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1559. l=add_str(label);
  1560. set_label(l,script_pos,p);
  1561. p=skip_space(p);
  1562. if(*p == ';') {
  1563. // for(;;) のパターンなので必ず真
  1564. ;
  1565. } else {
  1566. // 条件が偽なら終了地点に飛ばす
  1567. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1568. add_scriptl(add_str("jump_zero"));
  1569. add_scriptc(C_ARG);
  1570. p=parse_expr(p);
  1571. p=skip_space(p);
  1572. add_scriptl(add_str(label));
  1573. add_scriptc(C_FUNC);
  1574. }
  1575. if(*p != ';')
  1576. disp_error_message("parse_syntax: need ';'",p);
  1577. p++;
  1578. // ループ開始に飛ばす
  1579. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1580. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1581. parse_line(label);
  1582. syntax.curly_count--;
  1583. // 次のループへのラベル形成する
  1584. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1585. l=add_str(label);
  1586. set_label(l,script_pos,p);
  1587. // 次のループに入る時の処理
  1588. // for 最後の ')' を ';' として扱うフラグ
  1589. parse_syntax_for_flag = 1;
  1590. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1591. p=parse_line(p);
  1592. syntax.curly_count--;
  1593. parse_syntax_for_flag = 0;
  1594. // 条件判定処理に飛ばす
  1595. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1596. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1597. parse_line(label);
  1598. syntax.curly_count--;
  1599. // ループ開始のラベル付け
  1600. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1601. l=add_str(label);
  1602. set_label(l,script_pos,p);
  1603. return p;
  1604. }
  1605. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1606. {// internal script function
  1607. const char *func_name;
  1608. func_name = skip_space(p2);
  1609. p = skip_word(func_name);
  1610. if( p == func_name )
  1611. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1612. p2 = skip_space(p);
  1613. if( *p2 == ';' )
  1614. {// function <name> ;
  1615. // function declaration - just register the name
  1616. int l;
  1617. l = add_word(func_name);
  1618. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1619. str_data[l].type = C_USERFUNC;
  1620. else if( str_data[l].type == C_USERFUNC )
  1621. ; // already registered
  1622. else
  1623. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1624. // if, for , while の閉じ判定
  1625. p = parse_syntax_close(p2 + 1);
  1626. return p;
  1627. }
  1628. else if(*p2 == '{')
  1629. {// function <name> <line/block of code>
  1630. char label[256];
  1631. int l;
  1632. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1633. syntax.curly[syntax.curly_count].count = 1;
  1634. syntax.curly[syntax.curly_count].index = syntax.index++;
  1635. syntax.curly[syntax.curly_count].flag = 0;
  1636. ++syntax.curly_count;
  1637. // Jump over the function code
  1638. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1639. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1640. ++syntax.curly_count;
  1641. parse_line(label);
  1642. --syntax.curly_count;
  1643. // Set the position of the function (label)
  1644. l=add_word(func_name);
  1645. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1646. {
  1647. str_data[l].type = C_USERFUNC;
  1648. set_label(l, script_pos, p);
  1649. if( parse_options&SCRIPT_USE_LABEL_DB )
  1650. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1651. }
  1652. else
  1653. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1654. return skip_space(p);
  1655. }
  1656. else
  1657. {
  1658. disp_error_message("expect ';' or '{' at function syntax",p);
  1659. }
  1660. }
  1661. break;
  1662. case 'i':
  1663. case 'I':
  1664. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1665. // if() の処理
  1666. char label[256];
  1667. p=skip_space(p2);
  1668. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1669. disp_error_message("need '('",p);
  1670. }
  1671. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1672. syntax.curly[syntax.curly_count].count = 1;
  1673. syntax.curly[syntax.curly_count].index = syntax.index++;
  1674. syntax.curly[syntax.curly_count].flag = 0;
  1675. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1676. syntax.curly_count++;
  1677. add_scriptl(add_str("jump_zero"));
  1678. add_scriptc(C_ARG);
  1679. p=parse_expr(p);
  1680. p=skip_space(p);
  1681. add_scriptl(add_str(label));
  1682. add_scriptc(C_FUNC);
  1683. return p;
  1684. }
  1685. break;
  1686. case 's':
  1687. case 'S':
  1688. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1689. // switch() の処理
  1690. char label[256];
  1691. p=skip_space(p2);
  1692. if(*p != '(') {
  1693. disp_error_message("need '('",p);
  1694. }
  1695. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1696. syntax.curly[syntax.curly_count].count = 1;
  1697. syntax.curly[syntax.curly_count].index = syntax.index++;
  1698. syntax.curly[syntax.curly_count].flag = 0;
  1699. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1700. syntax.curly_count++;
  1701. add_scriptl(add_str("set"));
  1702. add_scriptc(C_ARG);
  1703. add_scriptl(add_str(label));
  1704. p=parse_expr(p);
  1705. p=skip_space(p);
  1706. if(*p != '{') {
  1707. disp_error_message("parse_syntax: need '{'",p);
  1708. }
  1709. add_scriptc(C_FUNC);
  1710. return p + 1;
  1711. }
  1712. break;
  1713. case 'w':
  1714. case 'W':
  1715. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1716. int l;
  1717. char label[256];
  1718. p=skip_space(p2);
  1719. if(*p != '(') {
  1720. disp_error_message("need '('",p);
  1721. }
  1722. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1723. syntax.curly[syntax.curly_count].count = 1;
  1724. syntax.curly[syntax.curly_count].index = syntax.index++;
  1725. syntax.curly[syntax.curly_count].flag = 0;
  1726. // 条件判断開始のラベル形成する
  1727. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1728. l=add_str(label);
  1729. set_label(l,script_pos,p);
  1730. // 条件が偽なら終了地点に飛ばす
  1731. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1732. syntax.curly_count++;
  1733. add_scriptl(add_str("jump_zero"));
  1734. add_scriptc(C_ARG);
  1735. p=parse_expr(p);
  1736. p=skip_space(p);
  1737. add_scriptl(add_str(label));
  1738. add_scriptc(C_FUNC);
  1739. return p;
  1740. }
  1741. break;
  1742. }
  1743. return NULL;
  1744. }
  1745. const char* parse_syntax_close(const char *p) {
  1746. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1747. int flag;
  1748. do {
  1749. p = parse_syntax_close_sub(p,&flag);
  1750. } while(flag);
  1751. return p;
  1752. }
  1753. // if, for , while , do の閉じ判定
  1754. // flag == 1 : 閉じられた
  1755. // flag == 0 : 閉じられない
  1756. const char* parse_syntax_close_sub(const char* p,int* flag)
  1757. {
  1758. char label[256];
  1759. int pos = syntax.curly_count - 1;
  1760. int l;
  1761. *flag = 1;
  1762. if(syntax.curly_count <= 0) {
  1763. *flag = 0;
  1764. return p;
  1765. } else if(syntax.curly[pos].type == TYPE_IF) {
  1766. const char *bp = p;
  1767. const char *p2;
  1768. // if-block and else-block end is a new line
  1769. parse_nextline(false, p);
  1770. // if 最終場所へ飛ばす
  1771. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1772. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1773. parse_line(label);
  1774. syntax.curly_count--;
  1775. // 現在地のラベルを付ける
  1776. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1777. l=add_str(label);
  1778. set_label(l,script_pos,p);
  1779. syntax.curly[pos].count++;
  1780. p = skip_space(p);
  1781. p2 = skip_word(p);
  1782. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1783. // else or else - if
  1784. p = skip_space(p2);
  1785. p2 = skip_word(p);
  1786. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1787. // else - if
  1788. p=skip_space(p2);
  1789. if(*p != '(') {
  1790. disp_error_message("need '('",p);
  1791. }
  1792. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1793. add_scriptl(add_str("jump_zero"));
  1794. add_scriptc(C_ARG);
  1795. p=parse_expr(p);
  1796. p=skip_space(p);
  1797. add_scriptl(add_str(label));
  1798. add_scriptc(C_FUNC);
  1799. *flag = 0;
  1800. return p;
  1801. } else {
  1802. // else
  1803. if(!syntax.curly[pos].flag) {
  1804. syntax.curly[pos].flag = 1;
  1805. *flag = 0;
  1806. return p;
  1807. }
  1808. }
  1809. }
  1810. // if 閉じ
  1811. syntax.curly_count--;
  1812. // 最終地のラベルを付ける
  1813. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1814. l=add_str(label);
  1815. set_label(l,script_pos,p);
  1816. if(syntax.curly[pos].flag == 1) {
  1817. // このifに対するelseじゃないのでポインタの位置は同じ
  1818. return bp;
  1819. }
  1820. return p;
  1821. } else if(syntax.curly[pos].type == TYPE_DO) {
  1822. int l;
  1823. char label[256];
  1824. const char *p2;
  1825. if(syntax.curly[pos].flag) {
  1826. // 現在地のラベル形成する(continue でここに来る)
  1827. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1828. l=add_str(label);
  1829. set_label(l,script_pos,p);
  1830. }
  1831. // 条件が偽なら終了地点に飛ばす
  1832. p = skip_space(p);
  1833. p2 = skip_word(p);
  1834. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1835. disp_error_message("parse_syntax: need 'while'",p);
  1836. p = skip_space(p2);
  1837. if(*p != '(') {
  1838. disp_error_message("need '('",p);
  1839. }
  1840. // do-block end is a new line
  1841. parse_nextline(false, p);
  1842. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1843. add_scriptl(add_str("jump_zero"));
  1844. add_scriptc(C_ARG);
  1845. p=parse_expr(p);
  1846. p=skip_space(p);
  1847. add_scriptl(add_str(label));
  1848. add_scriptc(C_FUNC);
  1849. // 開始地点に飛ばす
  1850. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1851. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1852. parse_line(label);
  1853. syntax.curly_count--;
  1854. // 条件終了地点のラベル形成する
  1855. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1856. l=add_str(label);
  1857. set_label(l,script_pos,p);
  1858. p = skip_space(p);
  1859. if(*p != ';') {
  1860. disp_error_message("parse_syntax: need ';'",p);
  1861. return p+1;
  1862. }
  1863. p++;
  1864. syntax.curly_count--;
  1865. return p;
  1866. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1867. // for-block end is a new line
  1868. parse_nextline(false, p);
  1869. // 次のループに飛ばす
  1870. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1871. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1872. parse_line(label);
  1873. syntax.curly_count--;
  1874. // for 終了のラベル付け
  1875. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1876. l=add_str(label);
  1877. set_label(l,script_pos,p);
  1878. syntax.curly_count--;
  1879. return p;
  1880. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1881. // while-block end is a new line
  1882. parse_nextline(false, p);
  1883. // while 条件判断へ飛ばす
  1884. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1885. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1886. parse_line(label);
  1887. syntax.curly_count--;
  1888. // while 終了のラベル付け
  1889. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1890. l=add_str(label);
  1891. set_label(l,script_pos,p);
  1892. syntax.curly_count--;
  1893. return p;
  1894. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1895. int pos = syntax.curly_count-1;
  1896. char label[256];
  1897. int l;
  1898. // 戻す
  1899. sprintf(label,"return;");
  1900. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1901. parse_line(label);
  1902. syntax.curly_count--;
  1903. // 現在地のラベルを付ける
  1904. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1905. l=add_str(label);
  1906. set_label(l,script_pos,p);
  1907. syntax.curly_count--;
  1908. return p;
  1909. } else {
  1910. *flag = 0;
  1911. return p;
  1912. }
  1913. }
  1914. /*==========================================
  1915. * 組み込み関数の追加
  1916. *------------------------------------------*/
  1917. static void add_buildin_func(void)
  1918. {
  1919. int i,n;
  1920. const char* p;
  1921. for( i = 0; buildin_func[i].func; i++ )
  1922. {
  1923. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1924. // 'v' - value (either string or int or reference)
  1925. // 's' - string
  1926. // 'i' - int
  1927. // 'r' - reference (of a variable)
  1928. // 'l' - label
  1929. // '?' - one optional parameter
  1930. // '*' - unknown number of optional parameters
  1931. p = buildin_func[i].arg;
  1932. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1933. while( *p == '?' ) ++p;
  1934. if( *p == '*' ) ++p;
  1935. if( *p != 0){
  1936. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1937. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1938. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1939. } else {
  1940. n = add_str(buildin_func[i].name);
  1941. str_data[n].type = C_FUNC;
  1942. str_data[n].val = i;
  1943. str_data[n].func = buildin_func[i].func;
  1944. if( !strcmp(buildin_func[i].name, "set") ) buildin_set_ref = n; else
  1945. if( !strcmp(buildin_func[i].name, "callsub") ) buildin_callsub_ref = n; else
  1946. if( !strcmp(buildin_func[i].name, "callfunc") ) buildin_callfunc_ref = n; else
  1947. if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1948. }
  1949. }
  1950. }
  1951. /// Retrieves the value of a constant.
  1952. bool script_get_constant(const char* name, int* value)
  1953. {
  1954. int n = search_str(name);
  1955. if( n == -1 || str_data[n].type != C_INT )
  1956. {// not found or not a constant
  1957. return false;
  1958. }
  1959. value[0] = str_data[n].val;
  1960. return true;
  1961. }
  1962. /// Creates new constant or parameter with given value.
  1963. void script_set_constant(const char* name, int value, bool isparameter)
  1964. {
  1965. int n = add_str(name);
  1966. if( str_data[n].type == C_NOP )
  1967. {// new
  1968. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1969. str_data[n].val = value;
  1970. }
  1971. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1972. {// existing parameter or constant
  1973. 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);
  1974. }
  1975. else
  1976. {// existing name
  1977. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1978. }
  1979. }
  1980. /*==========================================
  1981. * 定数データベースの読み込み
  1982. *------------------------------------------*/
  1983. static void read_constdb(void)
  1984. {
  1985. FILE *fp;
  1986. char line[1024],name[1024],val[1024];
  1987. int type;
  1988. sprintf(line, "%s/const.txt", db_path);
  1989. fp=fopen(line, "r");
  1990. if(fp==NULL){
  1991. ShowError("can't read %s\n", line);
  1992. return ;
  1993. }
  1994. while(fgets(line, sizeof(line), fp))
  1995. {
  1996. if(line[0]=='/' && line[1]=='/')
  1997. continue;
  1998. type=0;
  1999. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  2000. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  2001. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2002. }
  2003. }
  2004. fclose(fp);
  2005. }
  2006. /*==========================================
  2007. * エラー表示
  2008. *------------------------------------------*/
  2009. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2010. {
  2011. int i;
  2012. if( p == NULL || !p[0] ) return NULL;
  2013. if( line < 0 )
  2014. StringBuf_Printf(buf, "*% 5d : ", -line);
  2015. else
  2016. StringBuf_Printf(buf, " % 5d : ", line);
  2017. for(i=0;p[i] && p[i] != '\n';i++){
  2018. if(p + i != mark)
  2019. StringBuf_Printf(buf, "%c", p[i]);
  2020. else
  2021. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2022. }
  2023. StringBuf_AppendStr(buf, "\n");
  2024. return p+i+(p[i] == '\n' ? 1 : 0);
  2025. }
  2026. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  2027. {
  2028. // エラーが発生した行を求める
  2029. int j;
  2030. int line = start_line;
  2031. const char *p;
  2032. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2033. StringBuf buf;
  2034. for(p=src;p && *p;line++){
  2035. const char *lineend=strchr(p,'\n');
  2036. if(lineend==NULL || error_pos<lineend){
  2037. break;
  2038. }
  2039. for( j = 0; j < 4; j++ ) {
  2040. linestart[j] = linestart[j+1];
  2041. }
  2042. linestart[4] = p;
  2043. p=lineend+1;
  2044. }
  2045. StringBuf_Init(&buf);
  2046. StringBuf_AppendStr(&buf, "\a\n");
  2047. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  2048. StringBuf_Printf(&buf, " %s\n", error_msg);
  2049. for(j = 0; j < 5; j++ ) {
  2050. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  2051. }
  2052. p = script_print_line(&buf, p, error_pos, -line);
  2053. for(j = 0; j < 5; j++) {
  2054. p = script_print_line(&buf, p, NULL, line + j + 1 );
  2055. }
  2056. ShowError("%s", StringBuf_Value(&buf));
  2057. StringBuf_Destroy(&buf);
  2058. }
  2059. /*==========================================
  2060. * スクリプトの解析
  2061. *------------------------------------------*/
  2062. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2063. {
  2064. const char *p,*tmpp;
  2065. int i;
  2066. struct script_code* code = NULL;
  2067. static int first=1;
  2068. char end;
  2069. bool unresolved_names = false;
  2070. if( src == NULL )
  2071. return NULL;// empty script
  2072. memset(&syntax,0,sizeof(syntax));
  2073. if(first){
  2074. add_buildin_func();
  2075. read_constdb();
  2076. first=0;
  2077. }
  2078. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2079. script_pos=0;
  2080. script_size=SCRIPT_BLOCK_SIZE;
  2081. parse_nextline(true, NULL);
  2082. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2083. if( options&SCRIPT_USE_LABEL_DB )
  2084. db_clear(scriptlabel_db);
  2085. parse_options = options;
  2086. if( setjmp( error_jump ) != 0 ) {
  2087. //Restore program state when script has problems. [from jA]
  2088. int i;
  2089. const int size = ARRAYLENGTH(syntax.curly);
  2090. if( error_report )
  2091. script_error(src,file,line,error_msg,error_pos);
  2092. aFree( error_msg );
  2093. aFree( script_buf );
  2094. script_pos = 0;
  2095. script_size = 0;
  2096. script_buf = NULL;
  2097. for(i=LABEL_START;i<str_num;i++)
  2098. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  2099. for(i=0; i<size; i++)
  2100. linkdb_final(&syntax.curly[i].case_label);
  2101. return NULL;
  2102. }
  2103. parse_syntax_for_flag=0;
  2104. p=src;
  2105. p=skip_space(p);
  2106. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2107. {// does not require brackets around the script
  2108. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2109. {// empty script and can return NULL
  2110. aFree( script_buf );
  2111. script_pos = 0;
  2112. script_size = 0;
  2113. script_buf = NULL;
  2114. return NULL;
  2115. }
  2116. end = '\0';
  2117. }
  2118. else
  2119. {// requires brackets around the script
  2120. if( *p != '{' )
  2121. disp_error_message("not found '{'",p);
  2122. p = skip_space(p+1);
  2123. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2124. {// empty script and can return NULL
  2125. aFree( script_buf );
  2126. script_pos = 0;
  2127. script_size = 0;
  2128. script_buf = NULL;
  2129. return NULL;
  2130. }
  2131. end = '}';
  2132. }
  2133. // clear references of labels, variables and internal functions
  2134. for(i=LABEL_START;i<str_num;i++){
  2135. if(
  2136. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2137. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2138. ){
  2139. str_data[i].type=C_NOP;
  2140. str_data[i].backpatch=-1;
  2141. str_data[i].label=-1;
  2142. }
  2143. }
  2144. while( syntax.curly_count != 0 || *p != end )
  2145. {
  2146. if( *p == '\0' )
  2147. disp_error_message("unexpected end of script",p);
  2148. // labelだけ特殊処理
  2149. tmpp=skip_space(skip_word(p));
  2150. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2151. i=add_word(p);
  2152. set_label(i,script_pos,p);
  2153. if( parse_options&SCRIPT_USE_LABEL_DB )
  2154. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2155. p=tmpp+1;
  2156. p=skip_space(p);
  2157. continue;
  2158. }
  2159. // 他は全部一緒くた
  2160. p=parse_line(p);
  2161. p=skip_space(p);
  2162. parse_nextline(false, p);
  2163. }
  2164. add_scriptc(C_NOP);
  2165. // trim code to size
  2166. script_size = script_pos;
  2167. RECREATE(script_buf,unsigned char,script_pos);
  2168. // default unknown references to variables
  2169. for(i=LABEL_START;i<str_num;i++){
  2170. if(str_data[i].type==C_NOP){
  2171. int j,next;
  2172. str_data[i].type=C_NAME;
  2173. str_data[i].label=i;
  2174. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2175. next=GETVALUE(script_buf,j);
  2176. SETVALUE(script_buf,j,i);
  2177. j=next;
  2178. }
  2179. }
  2180. else if( str_data[i].type == C_USERFUNC )
  2181. {// 'function name;' without follow-up code
  2182. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2183. unresolved_names = true;
  2184. }
  2185. }
  2186. if( unresolved_names )
  2187. {
  2188. disp_error_message("parse_script: unresolved function references", p);
  2189. }
  2190. #ifdef DEBUG_DISP
  2191. for(i=0;i<script_pos;i++){
  2192. if((i&15)==0) ShowMessage("%04x : ",i);
  2193. ShowMessage("%02x ",script_buf[i]);
  2194. if((i&15)==15) ShowMessage("\n");
  2195. }
  2196. ShowMessage("\n");
  2197. #endif
  2198. #ifdef DEBUG_DISASM
  2199. {
  2200. int i = 0,j;
  2201. while(i < script_pos) {
  2202. c_op op = get_com(script_buf,&i);
  2203. ShowMessage("%06x %s", i, script_op2name(op));
  2204. j = i;
  2205. switch(op) {
  2206. case C_INT:
  2207. ShowMessage(" %d", get_num(script_buf,&i));
  2208. break;
  2209. case C_POS:
  2210. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2211. i += 3;
  2212. break;
  2213. case C_NAME:
  2214. j = (*(int*)(script_buf+i)&0xffffff);
  2215. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2216. i += 3;
  2217. break;
  2218. case C_STR:
  2219. j = strlen(script_buf + i);
  2220. ShowMessage(" %s", script_buf + i);
  2221. i += j+1;
  2222. break;
  2223. }
  2224. ShowMessage(CL_CLL"\n");
  2225. }
  2226. }
  2227. #endif
  2228. CREATE(code,struct script_code,1);
  2229. code->script_buf = script_buf;
  2230. code->script_size = script_size;
  2231. code->script_vars = idb_alloc(DB_OPT_RELEASE_DATA);
  2232. return code;
  2233. }
  2234. /// Returns the player attached to this script, identified by the rid.
  2235. /// If there is no player attached, the script is terminated.
  2236. TBL_PC *script_rid2sd(struct script_state *st)
  2237. {
  2238. TBL_PC *sd=map_id2sd(st->rid);
  2239. if(!sd){
  2240. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2241. script_reportfunc(st);
  2242. script_reportsrc(st);
  2243. st->state = END;
  2244. }
  2245. return sd;
  2246. }
  2247. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2248. ///
  2249. /// @param st Script state
  2250. /// @param data Variable/constant
  2251. void get_val(struct script_state* st, struct script_data* data)
  2252. {
  2253. const char* name;
  2254. char prefix;
  2255. char postfix;
  2256. TBL_PC* sd = NULL;
  2257. if( !data_isreference(data) )
  2258. return;// not a variable/constant
  2259. name = reference_getname(data);
  2260. prefix = name[0];
  2261. postfix = name[strlen(name) - 1];
  2262. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2263. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2264. {
  2265. sd = script_rid2sd(st);
  2266. if( sd == NULL )
  2267. {// needs player attached
  2268. if( postfix == '$' )
  2269. {// string variable
  2270. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2271. data->type = C_CONSTSTR;
  2272. data->u.str = "";
  2273. }
  2274. else
  2275. {// integer variable
  2276. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2277. data->type = C_INT;
  2278. data->u.num = 0;
  2279. }
  2280. return;
  2281. }
  2282. }
  2283. if( postfix == '$' )
  2284. {// string variable
  2285. switch( prefix )
  2286. {
  2287. case '@':
  2288. data->u.str = pc_readregstr(sd, data->u.num);
  2289. break;
  2290. case '$':
  2291. data->u.str = mapreg_readregstr(data->u.num);
  2292. break;
  2293. case '#':
  2294. if( name[1] == '#' )
  2295. data->u.str = pc_readaccountreg2str(sd, name);// global
  2296. else
  2297. data->u.str = pc_readaccountregstr(sd, name);// local
  2298. break;
  2299. case '.':
  2300. {
  2301. struct DBMap* n =
  2302. data->ref ? *data->ref:
  2303. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2304. st->script->script_vars;// npc variable
  2305. if( n )
  2306. data->u.str = (char*)idb_get(n,reference_getuid(data));
  2307. else
  2308. data->u.str = NULL;
  2309. }
  2310. break;
  2311. case '\'':
  2312. if (st->instance_id) {
  2313. data->u.str = (char*)idb_get(instance[st->instance_id].vars,reference_getuid(data));
  2314. } else {
  2315. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2316. data->u.str = NULL;
  2317. }
  2318. break;
  2319. default:
  2320. data->u.str = pc_readglobalreg_str(sd, name);
  2321. break;
  2322. }
  2323. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2324. {// empty string
  2325. data->type = C_CONSTSTR;
  2326. data->u.str = "";
  2327. }
  2328. else
  2329. {// duplicate string
  2330. data->type = C_STR;
  2331. data->u.str = aStrdup(data->u.str);
  2332. }
  2333. }
  2334. else
  2335. {// integer variable
  2336. data->type = C_INT;
  2337. if( reference_toconstant(data) )
  2338. {
  2339. data->u.num = reference_getconstant(data);
  2340. }
  2341. else if( reference_toparam(data) )
  2342. {
  2343. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2344. }
  2345. else
  2346. switch( prefix )
  2347. {
  2348. case '@':
  2349. data->u.num = pc_readreg(sd, data->u.num);
  2350. break;
  2351. case '$':
  2352. data->u.num = mapreg_readreg(data->u.num);
  2353. break;
  2354. case '#':
  2355. if( name[1] == '#' )
  2356. data->u.num = pc_readaccountreg2(sd, name);// global
  2357. else
  2358. data->u.num = pc_readaccountreg(sd, name);// local
  2359. break;
  2360. case '.':
  2361. {
  2362. struct DBMap* n =
  2363. data->ref ? *data->ref:
  2364. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2365. st->script->script_vars;// npc variable
  2366. if( n )
  2367. data->u.num = (int)idb_iget(n,reference_getuid(data));
  2368. else
  2369. data->u.num = 0;
  2370. }
  2371. break;
  2372. case '\'':
  2373. if( st->instance_id )
  2374. data->u.num = (int)idb_iget(instance[st->instance_id].vars,reference_getuid(data));
  2375. else {
  2376. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2377. data->u.num = 0;
  2378. }
  2379. break;
  2380. default:
  2381. data->u.num = pc_readglobalreg(sd, name);
  2382. break;
  2383. }
  2384. }
  2385. return;
  2386. }
  2387. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
  2388. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2389. /// The value is left in the top of the stack and needs to be removed manually.
  2390. void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
  2391. {
  2392. struct script_data* data;
  2393. push_val2(st->stack, C_NAME, uid, ref);
  2394. data = script_getdatatop(st, -1);
  2395. get_val(st, data);
  2396. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2397. }
  2398. /*==========================================
  2399. * Stores the value of a script variable
  2400. * Return value is 0 on fail, 1 on success.
  2401. *------------------------------------------*/
  2402. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
  2403. {
  2404. char prefix = name[0];
  2405. if( is_string_variable(name) )
  2406. {// string variable
  2407. const char* str = (const char*)value;
  2408. switch (prefix) {
  2409. case '@':
  2410. return pc_setregstr(sd, num, str);
  2411. case '$':
  2412. return mapreg_setregstr(num, str);
  2413. case '#':
  2414. return (name[1] == '#') ?
  2415. pc_setaccountreg2str(sd, name, str) :
  2416. pc_setaccountregstr(sd, name, str);
  2417. case '.':
  2418. {
  2419. struct DBMap* n;
  2420. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2421. if( n ) {
  2422. idb_remove(n, num);
  2423. if (str[0]) idb_put(n, num, aStrdup(str));
  2424. }
  2425. }
  2426. return 1;
  2427. case '\'':
  2428. if( st->instance_id ) {
  2429. idb_remove(instance[st->instance_id].vars, num);
  2430. if( str[0] ) idb_put(instance[st->instance_id].vars, num, aStrdup(str));
  2431. }
  2432. return 1;
  2433. default:
  2434. return pc_setglobalreg_str(sd, name, str);
  2435. }
  2436. }
  2437. else
  2438. {// integer variable
  2439. int val = (int)__64BPRTSIZE(value);
  2440. if(str_data[num&0x00ffffff].type == C_PARAM)
  2441. {
  2442. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2443. {
  2444. if( st != NULL )
  2445. {
  2446. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2447. script_reportsrc(st);
  2448. st->state = END;
  2449. }
  2450. return 0;
  2451. }
  2452. return 1;
  2453. }
  2454. switch (prefix) {
  2455. case '@':
  2456. return pc_setreg(sd, num, val);
  2457. case '$':
  2458. return mapreg_setreg(num, val);
  2459. case '#':
  2460. return (name[1] == '#') ?
  2461. pc_setaccountreg2(sd, name, val) :
  2462. pc_setaccountreg(sd, name, val);
  2463. case '.':
  2464. {
  2465. struct DBMap* n;
  2466. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2467. if( n ) {
  2468. idb_remove(n, num);
  2469. if( val != 0 )
  2470. idb_iput(n, num, val);
  2471. }
  2472. }
  2473. return 1;
  2474. case '\'':
  2475. if( st->instance_id ) {
  2476. idb_remove(instance[st->instance_id].vars, num);
  2477. if( val != 0 )
  2478. idb_iput(instance[st->instance_id].vars, num, val);
  2479. }
  2480. return 1;
  2481. default:
  2482. return pc_setglobalreg(sd, name, val);
  2483. }
  2484. }
  2485. }
  2486. int set_var(TBL_PC* sd, char* name, void* val)
  2487. {
  2488. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2489. }
  2490. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
  2491. {
  2492. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2493. }
  2494. /// Converts the data to a string
  2495. const char* conv_str(struct script_state* st, struct script_data* data)
  2496. {
  2497. char* p;
  2498. get_val(st, data);
  2499. if( data_isstring(data) )
  2500. {// nothing to convert
  2501. }
  2502. else if( data_isint(data) )
  2503. {// int -> string
  2504. CREATE(p, char, ITEM_NAME_LENGTH);
  2505. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2506. p[ITEM_NAME_LENGTH-1] = '\0';
  2507. data->type = C_STR;
  2508. data->u.str = p;
  2509. }
  2510. else if( data_isreference(data) )
  2511. {// reference -> string
  2512. //##TODO when does this happen (check get_val) [FlavioJS]
  2513. data->type = C_CONSTSTR;
  2514. data->u.str = reference_getname(data);
  2515. }
  2516. else
  2517. {// unsupported data type
  2518. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2519. script_reportdata(data);
  2520. script_reportsrc(st);
  2521. data->type = C_CONSTSTR;
  2522. data->u.str = "";
  2523. }
  2524. return data->u.str;
  2525. }
  2526. /// Converts the data to an int
  2527. int conv_num(struct script_state* st, struct script_data* data)
  2528. {
  2529. char* p;
  2530. long num;
  2531. get_val(st, data);
  2532. if( data_isint(data) )
  2533. {// nothing to convert
  2534. }
  2535. else if( data_isstring(data) )
  2536. {// string -> int
  2537. // the result does not overflow or underflow, it is capped instead
  2538. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2539. p = data->u.str;
  2540. errno = 0;
  2541. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2542. if( errno == ERANGE
  2543. #if LONG_MAX > INT_MAX
  2544. || num < INT_MIN || num > INT_MAX
  2545. #endif
  2546. )
  2547. {
  2548. if( num <= INT_MIN )
  2549. {
  2550. num = INT_MIN;
  2551. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2552. }
  2553. else//if( num >= INT_MAX )
  2554. {
  2555. num = INT_MAX;
  2556. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2557. }
  2558. script_reportdata(data);
  2559. script_reportsrc(st);
  2560. }
  2561. if( data->type == C_STR )
  2562. aFree(p);
  2563. data->type = C_INT;
  2564. data->u.num = (int)num;
  2565. }
  2566. #if 0
  2567. // FIXME this function is being used to retrieve the position of labels and
  2568. // probably other stuff [FlavioJS]
  2569. else
  2570. {// unsupported data type
  2571. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2572. script_reportdata(data);
  2573. script_reportsrc(st);
  2574. data->type = C_INT;
  2575. data->u.num = 0;
  2576. }
  2577. #endif
  2578. return data->u.num;
  2579. }
  2580. //
  2581. // Stack operations
  2582. //
  2583. /// Increases the size of the stack
  2584. void stack_expand(struct script_stack* stack)
  2585. {
  2586. stack->sp_max += 64;
  2587. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2588. stack->sp_max * sizeof(stack->stack_data[0]) );
  2589. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2590. 64 * sizeof(stack->stack_data[0]) );
  2591. }
  2592. /// Pushes a value into the stack
  2593. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2594. /// Pushes a value into the stack (with reference)
  2595. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
  2596. {
  2597. if( stack->sp >= stack->sp_max )
  2598. stack_expand(stack);
  2599. stack->stack_data[stack->sp].type = type;
  2600. stack->stack_data[stack->sp].u.num = val;
  2601. stack->stack_data[stack->sp].ref = ref;
  2602. stack->sp++;
  2603. return &stack->stack_data[stack->sp-1];
  2604. }
  2605. /// Pushes a string into the stack
  2606. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2607. {
  2608. if( stack->sp >= stack->sp_max )
  2609. stack_expand(stack);
  2610. stack->stack_data[stack->sp].type = type;
  2611. stack->stack_data[stack->sp].u.str = str;
  2612. stack->stack_data[stack->sp].ref = NULL;
  2613. stack->sp++;
  2614. return &stack->stack_data[stack->sp-1];
  2615. }
  2616. /// Pushes a retinfo into the stack
  2617. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, DBMap **ref)
  2618. {
  2619. if( stack->sp >= stack->sp_max )
  2620. stack_expand(stack);
  2621. stack->stack_data[stack->sp].type = C_RETINFO;
  2622. stack->stack_data[stack->sp].u.ri = ri;
  2623. stack->stack_data[stack->sp].ref = ref;
  2624. stack->sp++;
  2625. return &stack->stack_data[stack->sp-1];
  2626. }
  2627. /// Pushes a copy of the target position into the stack
  2628. struct script_data* push_copy(struct script_stack* stack, int pos)
  2629. {
  2630. switch( stack->stack_data[pos].type )
  2631. {
  2632. case C_CONSTSTR:
  2633. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2634. break;
  2635. case C_STR:
  2636. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2637. break;
  2638. case C_RETINFO:
  2639. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2640. exit(1);
  2641. break;
  2642. default:
  2643. return push_val2(
  2644. stack,stack->stack_data[pos].type,
  2645. stack->stack_data[pos].u.num,
  2646. stack->stack_data[pos].ref
  2647. );
  2648. break;
  2649. }
  2650. }
  2651. /// Removes the values in indexes [start,end[ from the stack.
  2652. /// Adjusts all stack pointers.
  2653. void pop_stack(struct script_state* st, int start, int end)
  2654. {
  2655. struct script_stack* stack = st->stack;
  2656. struct script_data* data;
  2657. int i;
  2658. if( start < 0 )
  2659. start = 0;
  2660. if( end > stack->sp )
  2661. end = stack->sp;
  2662. if( start >= end )
  2663. return;// nothing to pop
  2664. // free stack elements
  2665. for( i = start; i < end; i++ )
  2666. {
  2667. data = &stack->stack_data[i];
  2668. if( data->type == C_STR )
  2669. aFree(data->u.str);
  2670. if( data->type == C_RETINFO )
  2671. {
  2672. struct script_retinfo* ri = data->u.ri;
  2673. if( ri->var_function )
  2674. script_free_vars(ri->var_function);
  2675. if( data->ref )
  2676. aFree(data->ref);
  2677. aFree(ri);
  2678. }
  2679. data->type = C_NOP;
  2680. }
  2681. // move the rest of the elements
  2682. if( stack->sp > end )
  2683. {
  2684. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2685. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2686. stack->stack_data[i].type = C_NOP;
  2687. }
  2688. // adjust stack pointers
  2689. if( st->start > end ) st->start -= end - start;
  2690. else if( st->start > start ) st->start = start;
  2691. if( st->end > end ) st->end -= end - start;
  2692. else if( st->end > start ) st->end = start;
  2693. if( stack->defsp > end ) stack->defsp -= end - start;
  2694. else if( stack->defsp > start ) stack->defsp = start;
  2695. stack->sp -= end - start;
  2696. }
  2697. ///
  2698. ///
  2699. ///
  2700. /*==========================================
  2701. * スクリプト依存変数、関数依存変数の解放
  2702. *------------------------------------------*/
  2703. void script_free_vars(struct DBMap* storage)
  2704. {
  2705. if( storage )
  2706. {// destroy the storage construct containing the variables
  2707. db_destroy(storage);
  2708. }
  2709. }
  2710. void script_free_code(struct script_code* code)
  2711. {
  2712. script_free_vars( code->script_vars );
  2713. aFree( code->script_buf );
  2714. aFree( code );
  2715. }
  2716. /// Creates a new script state.
  2717. ///
  2718. /// @param script Script code
  2719. /// @param pos Position in the code
  2720. /// @param rid Who is running the script (attached player)
  2721. /// @param oid Where the code is being run (npc 'object')
  2722. /// @return Script state
  2723. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2724. {
  2725. struct script_state* st;
  2726. CREATE(st, struct script_state, 1);
  2727. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2728. st->stack->sp = 0;
  2729. st->stack->sp_max = 64;
  2730. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2731. st->stack->defsp = st->stack->sp;
  2732. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  2733. st->state = RUN;
  2734. st->script = script;
  2735. //st->scriptroot = script;
  2736. st->pos = pos;
  2737. st->rid = rid;
  2738. st->oid = oid;
  2739. st->sleep.timer = INVALID_TIMER;
  2740. return st;
  2741. }
  2742. /// Frees a script state.
  2743. ///
  2744. /// @param st Script state
  2745. void script_free_state(struct script_state* st)
  2746. {
  2747. if(st->bk_st)
  2748. {// backup was not restored
  2749. 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);
  2750. }
  2751. if( st->sleep.timer != INVALID_TIMER )
  2752. delete_timer(st->sleep.timer, run_script_timer);
  2753. script_free_vars(st->stack->var_function);
  2754. pop_stack(st, 0, st->stack->sp);
  2755. aFree(st->stack->stack_data);
  2756. aFree(st->stack);
  2757. st->stack = NULL;
  2758. st->pos = -1;
  2759. aFree(st);
  2760. }
  2761. //
  2762. // 実行部main
  2763. //
  2764. /*==========================================
  2765. * コマンドの読み取り
  2766. *------------------------------------------*/
  2767. c_op get_com(unsigned char *script,int *pos)
  2768. {
  2769. int i = 0, j = 0;
  2770. if(script[*pos]>=0x80){
  2771. return C_INT;
  2772. }
  2773. while(script[*pos]>=0x40){
  2774. i=script[(*pos)++]<<j;
  2775. j+=6;
  2776. }
  2777. return (c_op)(i+(script[(*pos)++]<<j));
  2778. }
  2779. /*==========================================
  2780. * 数値の所得
  2781. *------------------------------------------*/
  2782. int get_num(unsigned char *script,int *pos)
  2783. {
  2784. int i,j;
  2785. i=0; j=0;
  2786. while(script[*pos]>=0xc0){
  2787. i+=(script[(*pos)++]&0x7f)<<j;
  2788. j+=6;
  2789. }
  2790. return i+((script[(*pos)++]&0x7f)<<j);
  2791. }
  2792. /*==========================================
  2793. * スタックから値を取り出す
  2794. *------------------------------------------*/
  2795. int pop_val(struct script_state* st)
  2796. {
  2797. if(st->stack->sp<=0)
  2798. return 0;
  2799. st->stack->sp--;
  2800. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2801. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2802. return st->stack->stack_data[st->stack->sp].u.num;
  2803. return 0;
  2804. }
  2805. /// Ternary operators
  2806. /// test ? if_true : if_false
  2807. void op_3(struct script_state* st, int op)
  2808. {
  2809. struct script_data* data;
  2810. int flag = 0;
  2811. data = script_getdatatop(st, -3);
  2812. get_val(st, data);
  2813. if( data_isstring(data) )
  2814. flag = data->u.str[0];// "" -> false
  2815. else if( data_isint(data) )
  2816. flag = data->u.num;// 0 -> false
  2817. else
  2818. {
  2819. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2820. script_reportdata(data);
  2821. script_reportsrc(st);
  2822. script_removetop(st, -3, 0);
  2823. script_pushnil(st);
  2824. return;
  2825. }
  2826. if( flag )
  2827. script_pushcopytop(st, -2);
  2828. else
  2829. script_pushcopytop(st, -1);
  2830. script_removetop(st, -4, -1);
  2831. }
  2832. /// Binary string operators
  2833. /// s1 EQ s2 -> i
  2834. /// s1 NE s2 -> i
  2835. /// s1 GT s2 -> i
  2836. /// s1 GE s2 -> i
  2837. /// s1 LT s2 -> i
  2838. /// s1 LE s2 -> i
  2839. /// s1 ADD s2 -> s
  2840. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2841. {
  2842. int a = 0;
  2843. switch(op){
  2844. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2845. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2846. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2847. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2848. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2849. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2850. case C_ADD:
  2851. {
  2852. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2853. strcpy(buf, s1);
  2854. strcat(buf, s2);
  2855. script_pushstr(st, buf);
  2856. return;
  2857. }
  2858. default:
  2859. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2860. script_reportsrc(st);
  2861. script_pushnil(st);
  2862. st->state = END;
  2863. return;
  2864. }
  2865. script_pushint(st,a);
  2866. }
  2867. /// Binary number operators
  2868. /// i OP i -> i
  2869. void op_2num(struct script_state* st, int op, int i1, int i2)
  2870. {
  2871. int ret;
  2872. double ret_double;
  2873. switch( op )
  2874. {
  2875. case C_AND: ret = i1 & i2; break;
  2876. case C_OR: ret = i1 | i2; break;
  2877. case C_XOR: ret = i1 ^ i2; break;
  2878. case C_LAND: ret = (i1 && i2); break;
  2879. case C_LOR: ret = (i1 || i2); break;
  2880. case C_EQ: ret = (i1 == i2); break;
  2881. case C_NE: ret = (i1 != i2); break;
  2882. case C_GT: ret = (i1 > i2); break;
  2883. case C_GE: ret = (i1 >= i2); break;
  2884. case C_LT: ret = (i1 < i2); break;
  2885. case C_LE: ret = (i1 <= i2); break;
  2886. case C_R_SHIFT: ret = i1>>i2; break;
  2887. case C_L_SHIFT: ret = i1<<i2; break;
  2888. case C_DIV:
  2889. case C_MOD:
  2890. if( i2 == 0 )
  2891. {
  2892. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2893. script_reportsrc(st);
  2894. script_pushnil(st);
  2895. st->state = END;
  2896. return;
  2897. }
  2898. else if( op == C_DIV )
  2899. ret = i1 / i2;
  2900. else//if( op == C_MOD )
  2901. ret = i1 % i2;
  2902. break;
  2903. default:
  2904. switch( op )
  2905. {// operators that can overflow/underflow
  2906. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2907. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2908. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2909. default:
  2910. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2911. script_reportsrc(st);
  2912. script_pushnil(st);
  2913. return;
  2914. }
  2915. if( ret_double < (double)INT_MIN )
  2916. {
  2917. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2918. script_reportsrc(st);
  2919. ret = INT_MIN;
  2920. }
  2921. else if( ret_double > (double)INT_MAX )
  2922. {
  2923. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2924. script_reportsrc(st);
  2925. ret = INT_MAX;
  2926. }
  2927. }
  2928. script_pushint(st, ret);
  2929. }
  2930. /// Binary operators
  2931. void op_2(struct script_state *st, int op)
  2932. {
  2933. struct script_data* left, leftref;
  2934. struct script_data* right;
  2935. leftref.type = C_NOP;
  2936. left = script_getdatatop(st, -2);
  2937. right = script_getdatatop(st, -1);
  2938. if (st->op2ref) {
  2939. if (data_isreference(left)) {
  2940. leftref = *left;
  2941. }
  2942. st->op2ref = 0;
  2943. }
  2944. get_val(st, left);
  2945. get_val(st, right);
  2946. // automatic conversions
  2947. switch( op )
  2948. {
  2949. case C_ADD:
  2950. if( data_isint(left) && data_isstring(right) )
  2951. {// convert int-string to string-string
  2952. conv_str(st, left);
  2953. }
  2954. else if( data_isstring(left) && data_isint(right) )
  2955. {// convert string-int to string-string
  2956. conv_str(st, right);
  2957. }
  2958. break;
  2959. }
  2960. if( data_isstring(left) && data_isstring(right) )
  2961. {// ss => op_2str
  2962. op_2str(st, op, left->u.str, right->u.str);
  2963. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  2964. if (leftref.type != C_NOP)
  2965. {
  2966. aFree(left->u.str);
  2967. *left = leftref;
  2968. }
  2969. }
  2970. else if( data_isint(left) && data_isint(right) )
  2971. {// ii => op_2num
  2972. int i1 = left->u.num;
  2973. int i2 = right->u.num;
  2974. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  2975. op_2num(st, op, i1, i2);
  2976. if (leftref.type != C_NOP)
  2977. *left = leftref;
  2978. }
  2979. else
  2980. {// invalid argument
  2981. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2982. script_reportdata(left);
  2983. script_reportdata(right);
  2984. script_reportsrc(st);
  2985. script_removetop(st, -2, 0);
  2986. script_pushnil(st);
  2987. st->state = END;
  2988. }
  2989. }
  2990. /// Unary operators
  2991. /// NEG i -> i
  2992. /// NOT i -> i
  2993. /// LNOT i -> i
  2994. void op_1(struct script_state* st, int op)
  2995. {
  2996. struct script_data* data;
  2997. int i1;
  2998. data = script_getdatatop(st, -1);
  2999. get_val(st, data);
  3000. if( !data_isint(data) )
  3001. {// not a number
  3002. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3003. script_reportdata(data);
  3004. script_reportsrc(st);
  3005. script_pushnil(st);
  3006. st->state = END;
  3007. return;
  3008. }
  3009. i1 = data->u.num;
  3010. script_removetop(st, -1, 0);
  3011. switch( op )
  3012. {
  3013. case C_NEG: i1 = -i1; break;
  3014. case C_NOT: i1 = ~i1; break;
  3015. case C_LNOT: i1 = !i1; break;
  3016. default:
  3017. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3018. script_reportsrc(st);
  3019. script_pushnil(st);
  3020. st->state = END;
  3021. return;
  3022. }
  3023. script_pushint(st, i1);
  3024. }
  3025. /// Checks the type of all arguments passed to a built-in function.
  3026. ///
  3027. /// @param st Script state whose stack arguments should be inspected.
  3028. /// @param func Built-in function for which the arguments are intended.
  3029. static void script_check_buildin_argtype(struct script_state* st, int func)
  3030. {
  3031. char type;
  3032. int idx, invalid = 0;
  3033. script_function* sf = &buildin_func[str_data[func].val];
  3034. for( idx = 2; script_hasdata(st, idx); idx++ )
  3035. {
  3036. struct script_data* data = script_getdata(st, idx);
  3037. type = sf->arg[idx-2];
  3038. if( type == '?' || type == '*' )
  3039. {// optional argument or unknown number of optional parameters ( no types are after this )
  3040. break;
  3041. }
  3042. else if( type == 0 )
  3043. {// more arguments than necessary ( should not happen, as it is checked before )
  3044. ShowWarning("Found more arguments than necessary.\n");
  3045. invalid++;
  3046. break;
  3047. }
  3048. else
  3049. {
  3050. const char* name = NULL;
  3051. if( data_isreference(data) )
  3052. {// get name for variables to determine the type they refer to
  3053. name = reference_getname(data);
  3054. }
  3055. switch( type )
  3056. {
  3057. case 'v':
  3058. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3059. {// variant
  3060. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3061. script_reportdata(data);
  3062. invalid++;
  3063. }
  3064. break;
  3065. case 's':
  3066. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3067. {// string
  3068. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3069. script_reportdata(data);
  3070. invalid++;
  3071. }
  3072. break;
  3073. case 'i':
  3074. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3075. {// int ( params and constants are always int )
  3076. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3077. script_reportdata(data);
  3078. invalid++;
  3079. }
  3080. break;
  3081. case 'r':
  3082. if( !data_isreference(data) )
  3083. {// variables
  3084. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3085. script_reportdata(data);
  3086. invalid++;
  3087. }
  3088. break;
  3089. case 'l':
  3090. if( !data_islabel(data) && !data_isfunclabel(data) )
  3091. {// label
  3092. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3093. script_reportdata(data);
  3094. invalid++;
  3095. }
  3096. break;
  3097. }
  3098. }
  3099. }
  3100. if(invalid)
  3101. {
  3102. ShowDebug("Function: %s\n", get_str(func));
  3103. script_reportsrc(st);
  3104. }
  3105. }
  3106. /// Executes a buildin command.
  3107. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3108. int run_func(struct script_state *st)
  3109. {
  3110. struct script_data* data;
  3111. int i,start_sp,end_sp,func;
  3112. end_sp = st->stack->sp;// position after the last argument
  3113. for( i = end_sp-1; i > 0 ; --i )
  3114. if( st->stack->stack_data[i].type == C_ARG )
  3115. break;
  3116. if( i == 0 )
  3117. {
  3118. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3119. st->state = END;
  3120. script_reportsrc(st);
  3121. return 1;
  3122. }
  3123. start_sp = i-1;// C_NAME of the command
  3124. st->start = start_sp;
  3125. st->end = end_sp;
  3126. data = &st->stack->stack_data[st->start];
  3127. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  3128. func = data->u.num;
  3129. else
  3130. {
  3131. ShowError("script:run_func: not a buildin command.\n");
  3132. script_reportdata(data);
  3133. script_reportsrc(st);
  3134. st->state = END;
  3135. return 1;
  3136. }
  3137. if( script_config.warn_func_mismatch_argtypes )
  3138. {
  3139. script_check_buildin_argtype(st, func);
  3140. }
  3141. if(str_data[func].func){
  3142. if (str_data[func].func(st)) //Report error
  3143. script_reportsrc(st);
  3144. } else {
  3145. 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));
  3146. script_reportsrc(st);
  3147. st->state = END;
  3148. }
  3149. // Stack's datum are used when re-running functions [Eoe]
  3150. if( st->state == RERUNLINE )
  3151. return 0;
  3152. pop_stack(st, st->start, st->end);
  3153. if( st->state == RETFUNC )
  3154. {// return from a user-defined function
  3155. struct script_retinfo* ri;
  3156. int olddefsp = st->stack->defsp;
  3157. int nargs;
  3158. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3159. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3160. {
  3161. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3162. script_reportsrc(st);
  3163. st->state = END;
  3164. return 1;
  3165. }
  3166. script_free_vars( st->stack->var_function );
  3167. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3168. nargs = ri->nargs;
  3169. st->pos = ri->pos;
  3170. st->script = ri->script;
  3171. st->stack->var_function = ri->var_function;
  3172. st->stack->defsp = ri->defsp;
  3173. memset(ri, 0, sizeof(struct script_retinfo));
  3174. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3175. st->state = GOTO;
  3176. }
  3177. return 0;
  3178. }
  3179. /*==========================================
  3180. * script execution
  3181. *------------------------------------------*/
  3182. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3183. {
  3184. struct script_state *st;
  3185. if( rootscript == NULL || pos < 0 )
  3186. return;
  3187. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3188. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3189. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3190. st = script_alloc_state(rootscript, pos, rid, oid);
  3191. run_script_main(st);
  3192. }
  3193. void script_stop_sleeptimers(int id)
  3194. {
  3195. struct script_state* st;
  3196. for(;;)
  3197. {
  3198. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3199. if( st == NULL )
  3200. break; // no more sleep timers
  3201. script_free_state(st);
  3202. }
  3203. }
  3204. /*==========================================
  3205. * 指定ノードをsleep_dbから削除
  3206. *------------------------------------------*/
  3207. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3208. {
  3209. struct linkdb_node *retnode;
  3210. if( n == NULL)
  3211. return NULL;
  3212. if( n->prev == NULL )
  3213. sleep_db = n->next;
  3214. else
  3215. n->prev->next = n->next;
  3216. if( n->next )
  3217. n->next->prev = n->prev;
  3218. retnode = n->next;
  3219. aFree( n );
  3220. return retnode; // 次のノードを返す
  3221. }
  3222. /*==========================================
  3223. * sleep用タイマー関数
  3224. *------------------------------------------*/
  3225. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3226. {
  3227. struct script_state *st = (struct script_state *)data;
  3228. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3229. TBL_PC *sd = map_id2sd(st->rid);
  3230. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3231. { //Character mismatch. Cancel execution.
  3232. st->rid = 0;
  3233. st->state = END;
  3234. }
  3235. while( node && st->sleep.timer != INVALID_TIMER ) {
  3236. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3237. script_erase_sleepdb(node);
  3238. st->sleep.timer = INVALID_TIMER;
  3239. break;
  3240. }
  3241. node = node->next;
  3242. }
  3243. if(st->state != RERUNLINE)
  3244. st->sleep.tick = 0;
  3245. run_script_main(st);
  3246. return 0;
  3247. }
  3248. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3249. ///
  3250. /// @param st Script state to detach.
  3251. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3252. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3253. {
  3254. struct map_session_data* sd;
  3255. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3256. {
  3257. sd->st = st->bk_st;
  3258. sd->npc_id = st->bk_npcid;
  3259. /**
  3260. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3261. **/
  3262. #if SECURE_NPCTIMEOUT
  3263. /**
  3264. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3265. **/
  3266. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3267. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3268. sd->npc_idle_timer = INVALID_TIMER;
  3269. }
  3270. #endif
  3271. if(st->bk_st)
  3272. {
  3273. //Remove tag for removal.
  3274. st->bk_st = NULL;
  3275. st->bk_npcid = 0;
  3276. }
  3277. else if(dequeue_event)
  3278. {
  3279. npc_event_dequeue(sd);
  3280. }
  3281. }
  3282. else if(st->bk_st)
  3283. {// rid was set to 0, before detaching the script state
  3284. 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);
  3285. script_reportsrc(st->bk_st);
  3286. script_free_state(st->bk_st);
  3287. st->bk_st = NULL;
  3288. }
  3289. }
  3290. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3291. ///
  3292. /// @param st Script state to attach.
  3293. static void script_attach_state(struct script_state* st)
  3294. {
  3295. struct map_session_data* sd;
  3296. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3297. {
  3298. if(st!=sd->st)
  3299. {
  3300. if(st->bk_st)
  3301. {// there is already a backup
  3302. 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);
  3303. }
  3304. st->bk_st = sd->st;
  3305. st->bk_npcid = sd->npc_id;
  3306. }
  3307. sd->st = st;
  3308. sd->npc_id = st->oid;
  3309. /**
  3310. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3311. **/
  3312. #if SECURE_NPCTIMEOUT
  3313. if( sd->npc_idle_timer == INVALID_TIMER )
  3314. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3315. sd->npc_idle_tick = gettick();
  3316. #endif
  3317. }
  3318. }
  3319. /*==========================================
  3320. * スクリプトの実行メイン部分
  3321. *------------------------------------------*/
  3322. void run_script_main(struct script_state *st)
  3323. {
  3324. int cmdcount = script_config.check_cmdcount;
  3325. int gotocount = script_config.check_gotocount;
  3326. TBL_PC *sd;
  3327. struct script_stack *stack=st->stack;
  3328. struct npc_data *nd;
  3329. script_attach_state(st);
  3330. nd = map_id2nd(st->oid);
  3331. if( nd && map[nd->bl.m].instance_id > 0 )
  3332. st->instance_id = map[nd->bl.m].instance_id;
  3333. if(st->state == RERUNLINE) {
  3334. run_func(st);
  3335. if(st->state == GOTO)
  3336. st->state = RUN;
  3337. } else if(st->state != END)
  3338. st->state = RUN;
  3339. while(st->state == RUN)
  3340. {
  3341. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3342. switch(c){
  3343. case C_EOL:
  3344. if( stack->defsp > stack->sp )
  3345. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3346. else
  3347. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3348. break;
  3349. case C_INT:
  3350. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3351. break;
  3352. case C_POS:
  3353. case C_NAME:
  3354. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3355. st->pos+=3;
  3356. break;
  3357. case C_ARG:
  3358. push_val(stack,c,0);
  3359. break;
  3360. case C_STR:
  3361. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3362. while(st->script->script_buf[st->pos++]);
  3363. break;
  3364. case C_FUNC:
  3365. run_func(st);
  3366. if(st->state==GOTO){
  3367. st->state = RUN;
  3368. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3369. ShowError("run_script: infinity loop !\n");
  3370. script_reportsrc(st);
  3371. st->state=END;
  3372. }
  3373. }
  3374. break;
  3375. case C_REF:
  3376. st->op2ref = 1;
  3377. break;
  3378. case C_NEG:
  3379. case C_NOT:
  3380. case C_LNOT:
  3381. op_1(st ,c);
  3382. break;
  3383. case C_ADD:
  3384. case C_SUB:
  3385. case C_MUL:
  3386. case C_DIV:
  3387. case C_MOD:
  3388. case C_EQ:
  3389. case C_NE:
  3390. case C_GT:
  3391. case C_GE:
  3392. case C_LT:
  3393. case C_LE:
  3394. case C_AND:
  3395. case C_OR:
  3396. case C_XOR:
  3397. case C_LAND:
  3398. case C_LOR:
  3399. case C_R_SHIFT:
  3400. case C_L_SHIFT:
  3401. op_2(st, c);
  3402. break;
  3403. case C_OP3:
  3404. op_3(st, c);
  3405. break;
  3406. case C_NOP:
  3407. st->state=END;
  3408. break;
  3409. default:
  3410. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3411. st->state=END;
  3412. break;
  3413. }
  3414. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3415. ShowError("run_script: infinity loop !\n");
  3416. script_reportsrc(st);
  3417. st->state=END;
  3418. }
  3419. }
  3420. if(st->sleep.tick > 0) {
  3421. //Restore previous script
  3422. script_detach_state(st, false);
  3423. //Delay execution
  3424. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3425. st->sleep.charid = sd?sd->status.char_id:0;
  3426. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3427. run_script_timer, st->sleep.charid, (intptr_t)st);
  3428. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3429. }
  3430. else if(st->state != END && st->rid){
  3431. //Resume later (st is already attached to player).
  3432. if(st->bk_st) {
  3433. ShowWarning("Unable to restore stack! Double continuation!\n");
  3434. //Report BOTH scripts to see if that can help somehow.
  3435. ShowDebug("Previous script (lost):\n");
  3436. script_reportsrc(st->bk_st);
  3437. ShowDebug("Current script:\n");
  3438. script_reportsrc(st);
  3439. script_free_state(st->bk_st);
  3440. st->bk_st = NULL;
  3441. }
  3442. } else {
  3443. //Dispose of script.
  3444. if ((sd = map_id2sd(st->rid))!=NULL)
  3445. { //Restore previous stack and save char.
  3446. if(sd->state.using_fake_npc){
  3447. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3448. sd->state.using_fake_npc = 0;
  3449. }
  3450. //Restore previous script if any.
  3451. script_detach_state(st, true);
  3452. if (sd->state.reg_dirty&2)
  3453. intif_saveregistry(sd,2);
  3454. if (sd->state.reg_dirty&1)
  3455. intif_saveregistry(sd,1);
  3456. }
  3457. script_free_state(st);
  3458. st = NULL;
  3459. }
  3460. }
  3461. int script_config_read(char *cfgName)
  3462. {
  3463. int i;
  3464. char line[1024],w1[1024],w2[1024];
  3465. FILE *fp;
  3466. fp=fopen(cfgName,"r");
  3467. if(fp==NULL){
  3468. ShowError("File not found: %s\n", cfgName);
  3469. return 1;
  3470. }
  3471. while(fgets(line, sizeof(line), fp))
  3472. {
  3473. if(line[0] == '/' && line[1] == '/')
  3474. continue;
  3475. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3476. if(i!=2)
  3477. continue;
  3478. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3479. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3480. }
  3481. else if(strcmpi(w1,"check_cmdcount")==0) {
  3482. script_config.check_cmdcount = config_switch(w2);
  3483. }
  3484. else if(strcmpi(w1,"check_gotocount")==0) {
  3485. script_config.check_gotocount = config_switch(w2);
  3486. }
  3487. else if(strcmpi(w1,"input_min_value")==0) {
  3488. script_config.input_min_value = config_switch(w2);
  3489. }
  3490. else if(strcmpi(w1,"input_max_value")==0) {
  3491. script_config.input_max_value = config_switch(w2);
  3492. }
  3493. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3494. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3495. }
  3496. else if(strcmpi(w1,"import")==0){
  3497. script_config_read(w2);
  3498. }
  3499. else {
  3500. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3501. }
  3502. }
  3503. fclose(fp);
  3504. return 0;
  3505. }
  3506. /**
  3507. * @see DBApply
  3508. */
  3509. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3510. {
  3511. struct script_code *code = db_data2ptr(data);
  3512. if (code)
  3513. script_free_code(code);
  3514. return 0;
  3515. }
  3516. void script_run_autobonus(const char *autobonus, int id, int pos)
  3517. {
  3518. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3519. if( script )
  3520. {
  3521. current_equip_item_index = pos;
  3522. run_script(script,0,id,0);
  3523. }
  3524. }
  3525. void script_add_autobonus(const char *autobonus)
  3526. {
  3527. if( strdb_get(autobonus_db, autobonus) == NULL )
  3528. {
  3529. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3530. if( script )
  3531. strdb_put(autobonus_db, autobonus, script);
  3532. }
  3533. }
  3534. /// resets a temporary character array variable to given value
  3535. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3536. {
  3537. int key;
  3538. uint8 idx;
  3539. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3540. {
  3541. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3542. return;
  3543. }
  3544. key = add_str(varname);
  3545. if( is_string_variable(varname) )
  3546. {
  3547. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3548. {
  3549. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3550. }
  3551. }
  3552. else
  3553. {
  3554. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3555. {
  3556. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3557. }
  3558. }
  3559. }
  3560. /// sets a temporary character array variable element idx to given value
  3561. /// @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.
  3562. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3563. {
  3564. int key;
  3565. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3566. {
  3567. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3568. return;
  3569. }
  3570. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3571. {
  3572. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3573. return;
  3574. }
  3575. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3576. if( is_string_variable(varname) )
  3577. {
  3578. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3579. }
  3580. else
  3581. {
  3582. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3583. }
  3584. if( refcache )
  3585. {// save to avoid repeated add_str calls
  3586. refcache[0] = key;
  3587. }
  3588. }
  3589. #ifdef BETA_THREAD_TEST
  3590. int buildin_query_sql_sub(struct script_state* st, Sql* handle);
  3591. /* used to receive items the queryThread has already processed */
  3592. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3593. int i, cursor = 0;
  3594. bool allOk = true;
  3595. EnterSpinLock(&queryThreadLock);
  3596. for( i = 0; i < queryThreadData.count; i++ ) {
  3597. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3598. if( !entry->ok ) {
  3599. allOk = false;
  3600. continue;
  3601. }
  3602. run_script_main(entry->st);
  3603. entry->st = NULL;/* empty entries */
  3604. aFree(entry);
  3605. queryThreadData.entry[i] = NULL;
  3606. }
  3607. if( allOk ) {
  3608. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3609. delete_timer(queryThreadData.timer, queryThread_timer);
  3610. queryThreadData.timer = INVALID_TIMER;
  3611. }
  3612. /* now lets clear the mess. */
  3613. for( i = 0; i < queryThreadData.count; i++ ) {
  3614. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3615. if( entry == NULL )
  3616. continue;/* entry on hold */
  3617. /* move */
  3618. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3619. cursor++;
  3620. }
  3621. queryThreadData.count = cursor;
  3622. LeaveSpinLock(&queryThreadLock);
  3623. return 0;
  3624. }
  3625. void queryThread_add(struct script_state *st, bool type) {
  3626. int idx = 0;
  3627. struct queryThreadEntry* entry = NULL;
  3628. EnterSpinLock(&queryThreadLock);
  3629. if( queryThreadData.count++ != 0 )
  3630. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3631. idx = queryThreadData.count-1;
  3632. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3633. entry = queryThreadData.entry[idx];
  3634. entry->st = st;
  3635. entry->ok = false;
  3636. entry->type = type;
  3637. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3638. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3639. }
  3640. LeaveSpinLock(&queryThreadLock);
  3641. /* unlock the queryThread */
  3642. racond_signal(queryThreadCond);
  3643. }
  3644. /* adds a new log to the queue */
  3645. void queryThread_log(char * entry, int length) {
  3646. int idx = logThreadData.count;
  3647. EnterSpinLock(&queryThreadLock);
  3648. if( logThreadData.count++ != 0 )
  3649. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3650. CREATE(logThreadData.entry[idx], char, length + 1 );
  3651. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3652. LeaveSpinLock(&queryThreadLock);
  3653. /* unlock the queryThread */
  3654. racond_signal(queryThreadCond);
  3655. }
  3656. /* queryThread_main */
  3657. static void *queryThread_main(void *x) {
  3658. Sql *queryThread_handle = Sql_Malloc();
  3659. int i;
  3660. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) )
  3661. exit(EXIT_FAILURE);
  3662. if( strlen(default_codepage) > 0 )
  3663. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3664. Sql_ShowDebug(queryThread_handle);
  3665. if( log_config.sql_logs ) {
  3666. logmysql_handle = Sql_Malloc();
  3667. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) )
  3668. exit(EXIT_FAILURE);
  3669. if( strlen(default_codepage) > 0 )
  3670. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3671. Sql_ShowDebug(logmysql_handle);
  3672. }
  3673. while( 1 ) {
  3674. if(queryThreadTerminate > 0)
  3675. break;
  3676. EnterSpinLock(&queryThreadLock);
  3677. /* mess with queryThreadData within the lock */
  3678. for( i = 0; i < queryThreadData.count; i++ ) {
  3679. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3680. if( entry->ok )
  3681. continue;
  3682. else if ( !entry->st || !entry->st->stack ) {
  3683. entry->ok = true;/* dispose */
  3684. continue;
  3685. }
  3686. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  3687. entry->ok = true;/* we're done with this */
  3688. }
  3689. /* also check for any logs in need to be sent */
  3690. if( log_config.sql_logs ) {
  3691. for( i = 0; i < logThreadData.count; i++ ) {
  3692. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  3693. Sql_ShowDebug(logmysql_handle);
  3694. aFree(logThreadData.entry[i]);
  3695. }
  3696. logThreadData.count = 0;
  3697. }
  3698. LeaveSpinLock(&queryThreadLock);
  3699. ramutex_lock( queryThreadMutex );
  3700. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  3701. ramutex_unlock( queryThreadMutex );
  3702. }
  3703. Sql_Free(queryThread_handle);
  3704. if( log_config.sql_logs ) {
  3705. Sql_Free(logmysql_handle);
  3706. }
  3707. return NULL;
  3708. }
  3709. #endif
  3710. /*==========================================
  3711. * 終了
  3712. *------------------------------------------*/
  3713. int do_final_script() {
  3714. int i;
  3715. #ifdef DEBUG_HASH
  3716. if (battle_config.etc_log)
  3717. {
  3718. FILE *fp = fopen("hash_dump.txt","wt");
  3719. if(fp) {
  3720. int count[SCRIPT_HASH_SIZE];
  3721. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3722. int n=0;
  3723. int min=INT_MAX,max=0,zero=0;
  3724. double mean=0.0f;
  3725. double median=0.0f;
  3726. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3727. memset(count, 0, sizeof(count));
  3728. fprintf(fp,"num : hash : data_name\n");
  3729. fprintf(fp,"---------------------------------------------------------------\n");
  3730. for(i=LABEL_START; i<str_num; i++) {
  3731. unsigned int h = calc_hash(get_str(i));
  3732. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3733. ++count[h];
  3734. }
  3735. fprintf(fp,"--------------------\n\n");
  3736. memset(count2, 0, sizeof(count2));
  3737. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3738. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3739. if(min > count[i])
  3740. min = count[i]; // minimun count of collision
  3741. if(max < count[i])
  3742. max = count[i]; // maximun count of collision
  3743. if(count[i] == 0)
  3744. zero++;
  3745. ++count2[count[i]];
  3746. }
  3747. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3748. for( i=min; i <= max; ++i ){
  3749. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3750. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3751. }
  3752. for( i=min; i <= max; ++i ){
  3753. n += count2[i];
  3754. if( n*2 >= SCRIPT_HASH_SIZE )
  3755. {
  3756. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3757. median = (i+i+1)/2.0f;
  3758. else
  3759. median = i;
  3760. break;
  3761. }
  3762. }
  3763. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3764. fclose(fp);
  3765. }
  3766. }
  3767. #endif
  3768. mapreg_final();
  3769. db_destroy(scriptlabel_db);
  3770. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3771. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3772. if(sleep_db) {
  3773. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3774. while(n) {
  3775. struct script_state *st = (struct script_state *)n->data;
  3776. script_free_state(st);
  3777. n = n->next;
  3778. }
  3779. linkdb_final(&sleep_db);
  3780. }
  3781. if (str_data)
  3782. aFree(str_data);
  3783. if (str_buf)
  3784. aFree(str_buf);
  3785. for( i = 0; i < atcmd_binding_count; i++ ) {
  3786. aFree(atcmd_binding[i]);
  3787. }
  3788. if( atcmd_binding_count != 0 )
  3789. aFree(atcmd_binding);
  3790. #ifdef BETA_THREAD_TEST
  3791. /* QueryThread */
  3792. InterlockedIncrement(&queryThreadTerminate);
  3793. racond_signal(queryThreadCond);
  3794. rathread_wait(queryThread, NULL);
  3795. // Destroy cond var and mutex.
  3796. racond_destroy( queryThreadCond );
  3797. ramutex_destroy( queryThreadMutex );
  3798. /* Clear missing vars */
  3799. for( i = 0; i < queryThreadData.count; i++ ) {
  3800. aFree(queryThreadData.entry[i]);
  3801. }
  3802. aFree(queryThreadData.entry);
  3803. for( i = 0; i < logThreadData.count; i++ ) {
  3804. aFree(logThreadData.entry[i]);
  3805. }
  3806. aFree(logThreadData.entry);
  3807. #endif
  3808. return 0;
  3809. }
  3810. /*==========================================
  3811. * 初期化
  3812. *------------------------------------------*/
  3813. int do_init_script() {
  3814. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3815. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3816. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3817. mapreg_init();
  3818. #ifdef BETA_THREAD_TEST
  3819. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  3820. queryThreadData.count = 0;
  3821. CREATE(logThreadData.entry, char *, 1);
  3822. logThreadData.count = 0;
  3823. /* QueryThread Start */
  3824. InitializeSpinLock(&queryThreadLock);
  3825. queryThreadData.timer = INVALID_TIMER;
  3826. queryThreadTerminate = 0;
  3827. queryThreadMutex = ramutex_create();
  3828. queryThreadCond = racond_create();
  3829. queryThread = rathread_create(queryThread_main, NULL);
  3830. if(queryThread == NULL){
  3831. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  3832. exit(EXIT_FAILURE);
  3833. }
  3834. add_timer_func_list(queryThread_timer, "queryThread_timer");
  3835. #endif
  3836. return 0;
  3837. }
  3838. int script_reload() {
  3839. int i;
  3840. #ifdef BETA_THREAD_TEST
  3841. /* we're reloading so any queries undergoing should be...exterminated. */
  3842. EnterSpinLock(&queryThreadLock);
  3843. for( i = 0; i < queryThreadData.count; i++ ) {
  3844. aFree(queryThreadData.entry[i]);
  3845. }
  3846. queryThreadData.count = 0;
  3847. if( queryThreadData.timer != INVALID_TIMER ) {
  3848. delete_timer(queryThreadData.timer, queryThread_timer);
  3849. queryThreadData.timer = INVALID_TIMER;
  3850. }
  3851. LeaveSpinLock(&queryThreadLock);
  3852. #endif
  3853. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  3854. db_clear(scriptlabel_db);
  3855. // @commands (script based)
  3856. // Clear bindings
  3857. for( i = 0; i < atcmd_binding_count; i++ ) {
  3858. aFree(atcmd_binding[i]);
  3859. }
  3860. if( atcmd_binding_count != 0 )
  3861. aFree(atcmd_binding);
  3862. atcmd_binding_count = 0;
  3863. if(sleep_db) {
  3864. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3865. while(n) {
  3866. struct script_state *st = (struct script_state *)n->data;
  3867. script_free_state(st);
  3868. n = n->next;
  3869. }
  3870. linkdb_final(&sleep_db);
  3871. }
  3872. mapreg_reload();
  3873. return 0;
  3874. }
  3875. //-----------------------------------------------------------------------------
  3876. // buildin functions
  3877. //
  3878. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3879. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3880. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3881. /////////////////////////////////////////////////////////////////////
  3882. // NPC interaction
  3883. //
  3884. /// Appends a message to the npc dialog.
  3885. /// If a dialog doesn't exist yet, one is created.
  3886. ///
  3887. /// mes "<message>";
  3888. BUILDIN_FUNC(mes)
  3889. {
  3890. TBL_PC* sd = script_rid2sd(st);
  3891. if( sd == NULL )
  3892. return 0;
  3893. if( !script_hasdata(st, 3) )
  3894. {// only a single line detected in the script
  3895. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3896. }
  3897. else
  3898. {// parse multiple lines as they exist
  3899. int i;
  3900. for( i = 2; script_hasdata(st, i); i++ )
  3901. {
  3902. // send the message to the client
  3903. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3904. }
  3905. }
  3906. return 0;
  3907. }
  3908. /// Displays the button 'next' in the npc dialog.
  3909. /// The dialog text is cleared and the script continues when the button is pressed.
  3910. ///
  3911. /// next;
  3912. BUILDIN_FUNC(next)
  3913. {
  3914. TBL_PC* sd;
  3915. sd = script_rid2sd(st);
  3916. if( sd == NULL )
  3917. return 0;
  3918. st->state = STOP;
  3919. clif_scriptnext(sd, st->oid);
  3920. return 0;
  3921. }
  3922. /// Ends the script and displays the button 'close' on the npc dialog.
  3923. /// The dialog is closed when the button is pressed.
  3924. ///
  3925. /// close;
  3926. BUILDIN_FUNC(close)
  3927. {
  3928. TBL_PC* sd;
  3929. sd = script_rid2sd(st);
  3930. if( sd == NULL )
  3931. return 0;
  3932. st->state = END;
  3933. clif_scriptclose(sd, st->oid);
  3934. return 0;
  3935. }
  3936. /// Displays the button 'close' on the npc dialog.
  3937. /// The dialog is closed and the script continues when the button is pressed.
  3938. ///
  3939. /// close2;
  3940. BUILDIN_FUNC(close2)
  3941. {
  3942. TBL_PC* sd;
  3943. sd = script_rid2sd(st);
  3944. if( sd == NULL )
  3945. return 0;
  3946. st->state = STOP;
  3947. clif_scriptclose(sd, st->oid);
  3948. return 0;
  3949. }
  3950. /// Counts the number of valid and total number of options in 'str'
  3951. /// If max_count > 0 the counting stops when that valid option is reached
  3952. /// total is incremented for each option (NULL is supported)
  3953. static int menu_countoptions(const char* str, int max_count, int* total)
  3954. {
  3955. int count = 0;
  3956. int bogus_total;
  3957. if( total == NULL )
  3958. total = &bogus_total;
  3959. ++(*total);
  3960. // initial empty options
  3961. while( *str == ':' )
  3962. {
  3963. ++str;
  3964. ++(*total);
  3965. }
  3966. // count menu options
  3967. while( *str != '\0' )
  3968. {
  3969. ++count;
  3970. --max_count;
  3971. if( max_count == 0 )
  3972. break;
  3973. while( *str != ':' && *str != '\0' )
  3974. ++str;
  3975. while( *str == ':' )
  3976. {
  3977. ++str;
  3978. ++(*total);
  3979. }
  3980. }
  3981. return count;
  3982. }
  3983. /// Displays a menu with options and goes to the target label.
  3984. /// The script is stopped if cancel is pressed.
  3985. /// Options with no text are not displayed in the client.
  3986. ///
  3987. /// Options can be grouped together, separated by the character ':' in the text:
  3988. /// ex: menu "A:B:C",L_target;
  3989. /// All these options go to the specified target label.
  3990. ///
  3991. /// The index of the selected option is put in the variable @menu.
  3992. /// Indexes start with 1 and are consistent with grouped and empty options.
  3993. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3994. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3995. ///
  3996. /// NOTE: the client closes the npc dialog when cancel is pressed
  3997. ///
  3998. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3999. BUILDIN_FUNC(menu)
  4000. {
  4001. int i;
  4002. const char* text;
  4003. TBL_PC* sd;
  4004. sd = script_rid2sd(st);
  4005. if( sd == NULL )
  4006. return 0;
  4007. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4008. if( sd->state.menu_or_input == 0 )
  4009. {
  4010. struct StringBuf buf;
  4011. struct script_data* data;
  4012. if( script_lastdata(st) % 2 == 0 )
  4013. {// argument count is not even (1st argument is at index 2)
  4014. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4015. st->state = END;
  4016. return 1;
  4017. }
  4018. StringBuf_Init(&buf);
  4019. sd->npc_menu = 0;
  4020. for( i = 2; i < script_lastdata(st); i += 2 )
  4021. {
  4022. // menu options
  4023. text = script_getstr(st, i);
  4024. // target label
  4025. data = script_getdata(st, i+1);
  4026. if( !data_islabel(data) )
  4027. {// not a label
  4028. StringBuf_Destroy(&buf);
  4029. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4030. script_reportdata(data);
  4031. st->state = END;
  4032. return 1;
  4033. }
  4034. // append option(s)
  4035. if( text[0] == '\0' )
  4036. continue;// empty string, ignore
  4037. if( sd->npc_menu > 0 )
  4038. StringBuf_AppendStr(&buf, ":");
  4039. StringBuf_AppendStr(&buf, text);
  4040. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4041. }
  4042. st->state = RERUNLINE;
  4043. sd->state.menu_or_input = 1;
  4044. /**
  4045. * menus beyond this length crash the client (see bugreport:6402)
  4046. **/
  4047. if( StringBuf_Length(&buf) >= 2047 ) {
  4048. struct npc_data * nd = map_id2nd(st->oid);
  4049. char* menu;
  4050. CREATE(menu, char, 2048);
  4051. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4052. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4053. clif_scriptmenu(sd, st->oid, menu);
  4054. aFree(menu);
  4055. } else
  4056. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4057. StringBuf_Destroy(&buf);
  4058. if( sd->npc_menu >= 0xff )
  4059. {// 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
  4060. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4061. script_reportsrc(st);
  4062. }
  4063. }
  4064. else if( sd->npc_menu == 0xff )
  4065. {// Cancel was pressed
  4066. sd->state.menu_or_input = 0;
  4067. st->state = END;
  4068. }
  4069. else
  4070. {// goto target label
  4071. int menu = 0;
  4072. sd->state.menu_or_input = 0;
  4073. if( sd->npc_menu <= 0 )
  4074. {
  4075. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4076. st->state = END;
  4077. return 1;
  4078. }
  4079. // get target label
  4080. for( i = 2; i < script_lastdata(st); i += 2 )
  4081. {
  4082. text = script_getstr(st, i);
  4083. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4084. if( sd->npc_menu <= 0 )
  4085. break;// entry found
  4086. }
  4087. if( sd->npc_menu > 0 )
  4088. {// Invalid selection
  4089. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4090. st->state = END;
  4091. return 1;
  4092. }
  4093. if( !data_islabel(script_getdata(st, i + 1)) )
  4094. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4095. ShowError("script:menu: unexpected data in label argument\n");
  4096. script_reportdata(script_getdata(st, i + 1));
  4097. st->state = END;
  4098. return 1;
  4099. }
  4100. pc_setreg(sd, add_str("@menu"), menu);
  4101. st->pos = script_getnum(st, i + 1);
  4102. st->state = GOTO;
  4103. }
  4104. return 0;
  4105. }
  4106. /// Displays a menu with options and returns the selected option.
  4107. /// Behaves like 'menu' without the target labels.
  4108. ///
  4109. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4110. ///
  4111. /// @see menu
  4112. BUILDIN_FUNC(select)
  4113. {
  4114. int i;
  4115. const char* text;
  4116. TBL_PC* sd;
  4117. sd = script_rid2sd(st);
  4118. if( sd == NULL )
  4119. return 0;
  4120. if( sd->state.menu_or_input == 0 ) {
  4121. struct StringBuf buf;
  4122. StringBuf_Init(&buf);
  4123. sd->npc_menu = 0;
  4124. for( i = 2; i <= script_lastdata(st); ++i ) {
  4125. text = script_getstr(st, i);
  4126. if( sd->npc_menu > 0 )
  4127. StringBuf_AppendStr(&buf, ":");
  4128. StringBuf_AppendStr(&buf, text);
  4129. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4130. }
  4131. st->state = RERUNLINE;
  4132. sd->state.menu_or_input = 1;
  4133. /**
  4134. * menus beyond this length crash the client (see bugreport:6402)
  4135. **/
  4136. if( StringBuf_Length(&buf) >= 2047 ) {
  4137. struct npc_data * nd = map_id2nd(st->oid);
  4138. char* menu;
  4139. CREATE(menu, char, 2048);
  4140. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4141. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4142. clif_scriptmenu(sd, st->oid, menu);
  4143. aFree(menu);
  4144. } else
  4145. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4146. StringBuf_Destroy(&buf);
  4147. if( sd->npc_menu >= 0xff ) {
  4148. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4149. script_reportsrc(st);
  4150. }
  4151. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4152. sd->state.menu_or_input = 0;
  4153. st->state = END;
  4154. } else {// return selected option
  4155. int menu = 0;
  4156. sd->state.menu_or_input = 0;
  4157. for( i = 2; i <= script_lastdata(st); ++i ) {
  4158. text = script_getstr(st, i);
  4159. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4160. if( sd->npc_menu <= 0 )
  4161. break;// entry found
  4162. }
  4163. pc_setreg(sd, add_str("@menu"), menu);
  4164. script_pushint(st, menu);
  4165. st->state = RUN;
  4166. }
  4167. return 0;
  4168. }
  4169. /// Displays a menu with options and returns the selected option.
  4170. /// Behaves like 'menu' without the target labels, except when cancel is
  4171. /// pressed.
  4172. /// When cancel is pressed, the script continues and 255 is returned.
  4173. ///
  4174. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4175. ///
  4176. /// @see menu
  4177. BUILDIN_FUNC(prompt)
  4178. {
  4179. int i;
  4180. const char *text;
  4181. TBL_PC* sd;
  4182. sd = script_rid2sd(st);
  4183. if( sd == NULL )
  4184. return 0;
  4185. if( sd->state.menu_or_input == 0 )
  4186. {
  4187. struct StringBuf buf;
  4188. StringBuf_Init(&buf);
  4189. sd->npc_menu = 0;
  4190. for( i = 2; i <= script_lastdata(st); ++i )
  4191. {
  4192. text = script_getstr(st, i);
  4193. if( sd->npc_menu > 0 )
  4194. StringBuf_AppendStr(&buf, ":");
  4195. StringBuf_AppendStr(&buf, text);
  4196. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4197. }
  4198. st->state = RERUNLINE;
  4199. sd->state.menu_or_input = 1;
  4200. /**
  4201. * menus beyond this length crash the client (see bugreport:6402)
  4202. **/
  4203. if( StringBuf_Length(&buf) >= 2047 ) {
  4204. struct npc_data * nd = map_id2nd(st->oid);
  4205. char* menu;
  4206. CREATE(menu, char, 2048);
  4207. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4208. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4209. clif_scriptmenu(sd, st->oid, menu);
  4210. aFree(menu);
  4211. } else
  4212. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4213. StringBuf_Destroy(&buf);
  4214. if( sd->npc_menu >= 0xff )
  4215. {
  4216. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4217. script_reportsrc(st);
  4218. }
  4219. }
  4220. else if( sd->npc_menu == 0xff )
  4221. {// Cancel was pressed
  4222. sd->state.menu_or_input = 0;
  4223. pc_setreg(sd, add_str("@menu"), 0xff);
  4224. script_pushint(st, 0xff);
  4225. st->state = RUN;
  4226. }
  4227. else
  4228. {// return selected option
  4229. int menu = 0;
  4230. sd->state.menu_or_input = 0;
  4231. for( i = 2; i <= script_lastdata(st); ++i )
  4232. {
  4233. text = script_getstr(st, i);
  4234. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4235. if( sd->npc_menu <= 0 )
  4236. break;// entry found
  4237. }
  4238. pc_setreg(sd, add_str("@menu"), menu);
  4239. script_pushint(st, menu);
  4240. st->state = RUN;
  4241. }
  4242. return 0;
  4243. }
  4244. /////////////////////////////////////////////////////////////////////
  4245. // ...
  4246. //
  4247. /// Jumps to the target script label.
  4248. ///
  4249. /// goto <label>;
  4250. BUILDIN_FUNC(goto)
  4251. {
  4252. if( !data_islabel(script_getdata(st,2)) )
  4253. {
  4254. ShowError("script:goto: not a label\n");
  4255. script_reportdata(script_getdata(st,2));
  4256. st->state = END;
  4257. return 1;
  4258. }
  4259. st->pos = script_getnum(st,2);
  4260. st->state = GOTO;
  4261. return 0;
  4262. }
  4263. /*==========================================
  4264. * user-defined function call
  4265. *------------------------------------------*/
  4266. BUILDIN_FUNC(callfunc)
  4267. {
  4268. int i, j;
  4269. struct script_retinfo* ri;
  4270. struct script_code* scr;
  4271. const char* str = script_getstr(st,2);
  4272. DBMap **ref = NULL;
  4273. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4274. if( !scr )
  4275. {
  4276. ShowError("script:callfunc: function not found! [%s]\n", str);
  4277. st->state = END;
  4278. return 1;
  4279. }
  4280. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4281. {
  4282. struct script_data* data = push_copy(st->stack,i);
  4283. if( data_isreference(data) && !data->ref )
  4284. {
  4285. const char* name = reference_getname(data);
  4286. if( name[0] == '.' ) {
  4287. if ( !ref ) {
  4288. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4289. ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars);
  4290. }
  4291. data->ref = ref;
  4292. }
  4293. }
  4294. }
  4295. CREATE(ri, struct script_retinfo, 1);
  4296. ri->script = st->script;// script code
  4297. ri->var_function = st->stack->var_function;// scope variables
  4298. ri->pos = st->pos;// script location
  4299. ri->nargs = j;// argument count
  4300. ri->defsp = st->stack->defsp;// default stack pointer
  4301. push_retinfo(st->stack, ri, ref);
  4302. st->pos = 0;
  4303. st->script = scr;
  4304. st->stack->defsp = st->stack->sp;
  4305. st->state = GOTO;
  4306. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4307. return 0;
  4308. }
  4309. /*==========================================
  4310. * subroutine call
  4311. *------------------------------------------*/
  4312. BUILDIN_FUNC(callsub)
  4313. {
  4314. int i,j;
  4315. struct script_retinfo* ri;
  4316. int pos = script_getnum(st,2);
  4317. DBMap **ref = NULL;
  4318. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4319. {
  4320. ShowError("script:callsub: argument is not a label\n");
  4321. script_reportdata(script_getdata(st,2));
  4322. st->state = END;
  4323. return 1;
  4324. }
  4325. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4326. {
  4327. struct script_data* data = push_copy(st->stack,i);
  4328. if( data_isreference(data) && !data->ref )
  4329. {
  4330. const char* name = reference_getname(data);
  4331. if( name[0] == '.' && name[1] == '@' ) {
  4332. if ( !ref ) {
  4333. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4334. ref[0] = st->stack->var_function;
  4335. }
  4336. data->ref = ref;
  4337. }
  4338. }
  4339. }
  4340. CREATE(ri, struct script_retinfo, 1);
  4341. ri->script = st->script;// script code
  4342. ri->var_function = st->stack->var_function;// scope variables
  4343. ri->pos = st->pos;// script location
  4344. ri->nargs = j;// argument count
  4345. ri->defsp = st->stack->defsp;// default stack pointer
  4346. push_retinfo(st->stack, ri, ref);
  4347. st->pos = pos;
  4348. st->stack->defsp = st->stack->sp;
  4349. st->state = GOTO;
  4350. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4351. return 0;
  4352. }
  4353. /// Retrieves an argument provided to callfunc/callsub.
  4354. /// If the argument doesn't exist
  4355. ///
  4356. /// getarg(<index>{,<default_value>}) -> <value>
  4357. BUILDIN_FUNC(getarg)
  4358. {
  4359. struct script_retinfo* ri;
  4360. int idx;
  4361. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4362. {
  4363. ShowError("script:getarg: no callfunc or callsub!\n");
  4364. st->state = END;
  4365. return 1;
  4366. }
  4367. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4368. idx = script_getnum(st,2);
  4369. if( idx >= 0 && idx < ri->nargs )
  4370. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4371. else if( script_hasdata(st,3) )
  4372. script_pushcopy(st, 3);
  4373. else
  4374. {
  4375. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4376. st->state = END;
  4377. return 1;
  4378. }
  4379. return 0;
  4380. }
  4381. /// Returns from the current function, optionaly returning a value from the functions.
  4382. /// Don't use outside script functions.
  4383. ///
  4384. /// return;
  4385. /// return <value>;
  4386. BUILDIN_FUNC(return)
  4387. {
  4388. if( script_hasdata(st,2) )
  4389. {// return value
  4390. struct script_data* data;
  4391. script_pushcopy(st, 2);
  4392. data = script_getdatatop(st, -1);
  4393. if( data_isreference(data) )
  4394. {
  4395. const char* name = reference_getname(data);
  4396. if( name[0] == '.' && name[1] == '@' )
  4397. {// scope variable
  4398. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4399. get_val(st, data);// current scope, convert to value
  4400. }
  4401. else if( name[0] == '.' && !data->ref )
  4402. {// script variable, link to current script
  4403. data->ref = &st->script->script_vars;
  4404. }
  4405. }
  4406. }
  4407. else
  4408. {// no return value
  4409. script_pushnil(st);
  4410. }
  4411. st->state = RETFUNC;
  4412. return 0;
  4413. }
  4414. /// Returns a random number from 0 to <range>-1.
  4415. /// Or returns a random number from <min> to <max>.
  4416. /// If <min> is greater than <max>, their numbers are switched.
  4417. /// rand(<range>) -> <int>
  4418. /// rand(<min>,<max>) -> <int>
  4419. BUILDIN_FUNC(rand)
  4420. {
  4421. int range;
  4422. int min;
  4423. int max;
  4424. if( script_hasdata(st,3) )
  4425. {// min,max
  4426. min = script_getnum(st,2);
  4427. max = script_getnum(st,3);
  4428. if( max < min )
  4429. swap(min, max);
  4430. range = max - min + 1;
  4431. }
  4432. else
  4433. {// range
  4434. min = 0;
  4435. range = script_getnum(st,2);
  4436. }
  4437. if( range <= 1 )
  4438. script_pushint(st, min);
  4439. else
  4440. script_pushint(st, rnd()%range + min);
  4441. return 0;
  4442. }
  4443. /*==========================================
  4444. *
  4445. *------------------------------------------*/
  4446. BUILDIN_FUNC(warp)
  4447. {
  4448. int ret;
  4449. int x,y;
  4450. const char* str;
  4451. TBL_PC* sd;
  4452. sd = script_rid2sd(st);
  4453. if( sd == NULL )
  4454. return 0;
  4455. str = script_getstr(st,2);
  4456. x = script_getnum(st,3);
  4457. y = script_getnum(st,4);
  4458. if(strcmp(str,"Random")==0)
  4459. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4460. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4461. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4462. else
  4463. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4464. if( ret ) {
  4465. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4466. script_reportsrc(st);
  4467. }
  4468. return 0;
  4469. }
  4470. /*==========================================
  4471. * エリア指定ワープ
  4472. *------------------------------------------*/
  4473. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4474. {
  4475. int x2,y2,x3,y3;
  4476. unsigned int index;
  4477. index = va_arg(ap,unsigned int);
  4478. x2 = va_arg(ap,int);
  4479. y2 = va_arg(ap,int);
  4480. x3 = va_arg(ap,int);
  4481. y3 = va_arg(ap,int);
  4482. if(index == 0)
  4483. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4484. else if(x3 && y3) {
  4485. int max, tx, ty, j = 0;
  4486. // choose a suitable max number of attempts
  4487. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4488. max = 1000;
  4489. // find a suitable map cell
  4490. do {
  4491. tx = rnd()%(x3-x2+1)+x2;
  4492. ty = rnd()%(y3-y2+1)+y2;
  4493. j++;
  4494. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4495. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4496. }
  4497. else
  4498. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4499. return 0;
  4500. }
  4501. BUILDIN_FUNC(areawarp)
  4502. {
  4503. int m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4504. unsigned int index;
  4505. const char *str;
  4506. const char *mapname;
  4507. mapname = script_getstr(st,2);
  4508. x0 = script_getnum(st,3);
  4509. y0 = script_getnum(st,4);
  4510. x1 = script_getnum(st,5);
  4511. y1 = script_getnum(st,6);
  4512. str = script_getstr(st,7);
  4513. x2 = script_getnum(st,8);
  4514. y2 = script_getnum(st,9);
  4515. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4516. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4517. x3 = 0;
  4518. y3 = 0;
  4519. } else if( x3 && y3 ) {
  4520. // normalize x3/y3 coordinates
  4521. if( x3 < x2 ) swap(x3,x2);
  4522. if( y3 < y2 ) swap(y3,y2);
  4523. }
  4524. }
  4525. if( (m = map_mapname2mapid(mapname)) < 0 )
  4526. return 0;
  4527. if( strcmp(str,"Random") == 0 )
  4528. index = 0;
  4529. else if( !(index=mapindex_name2id(str)) )
  4530. return 0;
  4531. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4532. return 0;
  4533. }
  4534. /*==========================================
  4535. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4536. *------------------------------------------*/
  4537. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4538. {
  4539. int hp, sp;
  4540. hp = va_arg(ap, int);
  4541. sp = va_arg(ap, int);
  4542. pc_percentheal((TBL_PC *)bl,hp,sp);
  4543. return 0;
  4544. }
  4545. BUILDIN_FUNC(areapercentheal)
  4546. {
  4547. int hp,sp,m;
  4548. const char *mapname;
  4549. int x0,y0,x1,y1;
  4550. mapname=script_getstr(st,2);
  4551. x0=script_getnum(st,3);
  4552. y0=script_getnum(st,4);
  4553. x1=script_getnum(st,5);
  4554. y1=script_getnum(st,6);
  4555. hp=script_getnum(st,7);
  4556. sp=script_getnum(st,8);
  4557. if( (m=map_mapname2mapid(mapname))< 0)
  4558. return 0;
  4559. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4560. return 0;
  4561. }
  4562. /*==========================================
  4563. * warpchar [LuzZza]
  4564. * Useful for warp one player from
  4565. * another player npc-session.
  4566. * Using: warpchar "mapname",x,y,Char_ID;
  4567. *------------------------------------------*/
  4568. BUILDIN_FUNC(warpchar)
  4569. {
  4570. int x,y,a;
  4571. const char *str;
  4572. TBL_PC *sd;
  4573. str=script_getstr(st,2);
  4574. x=script_getnum(st,3);
  4575. y=script_getnum(st,4);
  4576. a=script_getnum(st,5);
  4577. sd = map_charid2sd(a);
  4578. if( sd == NULL )
  4579. return 0;
  4580. if(strcmp(str, "Random") == 0)
  4581. pc_randomwarp(sd, CLR_TELEPORT);
  4582. else
  4583. if(strcmp(str, "SavePoint") == 0)
  4584. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4585. else
  4586. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4587. return 0;
  4588. }
  4589. /*==========================================
  4590. * Warpparty - [Fredzilla] [Paradox924X]
  4591. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4592. * If 'from_mapname' is specified, only the party members on that map will be warped
  4593. *------------------------------------------*/
  4594. BUILDIN_FUNC(warpparty)
  4595. {
  4596. TBL_PC *sd = NULL;
  4597. TBL_PC *pl_sd;
  4598. struct party_data* p;
  4599. int type;
  4600. int mapindex;
  4601. int i;
  4602. const char* str = script_getstr(st,2);
  4603. int x = script_getnum(st,3);
  4604. int y = script_getnum(st,4);
  4605. int p_id = script_getnum(st,5);
  4606. const char* str2 = NULL;
  4607. if ( script_hasdata(st,6) )
  4608. str2 = script_getstr(st,6);
  4609. p = party_search(p_id);
  4610. if(!p)
  4611. return 0;
  4612. type = ( strcmp(str,"Random")==0 ) ? 0
  4613. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4614. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4615. : ( strcmp(str,"Leader")==0 ) ? 3
  4616. : 4;
  4617. switch (type)
  4618. {
  4619. case 3:
  4620. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4621. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4622. return 0;
  4623. pl_sd = p->data[i].sd;
  4624. mapindex = pl_sd->mapindex;
  4625. x = pl_sd->bl.x;
  4626. y = pl_sd->bl.y;
  4627. break;
  4628. case 4:
  4629. mapindex = mapindex_name2id(str);
  4630. break;
  4631. case 2:
  4632. //"SavePoint" uses save point of the currently attached player
  4633. if (( sd = script_rid2sd(st) ) == NULL )
  4634. return 0;
  4635. default:
  4636. mapindex = 0;
  4637. break;
  4638. }
  4639. for (i = 0; i < MAX_PARTY; i++)
  4640. {
  4641. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4642. continue;
  4643. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4644. continue;
  4645. if( pc_isdead(pl_sd) )
  4646. continue;
  4647. switch( type )
  4648. {
  4649. case 0: // Random
  4650. if(!map[pl_sd->bl.m].flag.nowarp)
  4651. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4652. break;
  4653. case 1: // SavePointAll
  4654. if(!map[pl_sd->bl.m].flag.noreturn)
  4655. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4656. break;
  4657. case 2: // SavePoint
  4658. if(!map[pl_sd->bl.m].flag.noreturn)
  4659. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4660. break;
  4661. case 3: // Leader
  4662. case 4: // m,x,y
  4663. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4664. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4665. break;
  4666. }
  4667. }
  4668. return 0;
  4669. }
  4670. /*==========================================
  4671. * Warpguild - [Fredzilla]
  4672. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4673. *------------------------------------------*/
  4674. BUILDIN_FUNC(warpguild)
  4675. {
  4676. TBL_PC *sd = NULL;
  4677. TBL_PC *pl_sd;
  4678. struct guild* g;
  4679. struct s_mapiterator* iter;
  4680. int type;
  4681. const char* str = script_getstr(st,2);
  4682. int x = script_getnum(st,3);
  4683. int y = script_getnum(st,4);
  4684. int gid = script_getnum(st,5);
  4685. g = guild_search(gid);
  4686. if( g == NULL )
  4687. return 0;
  4688. type = ( strcmp(str,"Random")==0 ) ? 0
  4689. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4690. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4691. : 3;
  4692. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4693. {// "SavePoint" uses save point of the currently attached player
  4694. return 0;
  4695. }
  4696. iter = mapit_getallusers();
  4697. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4698. {
  4699. if( pl_sd->status.guild_id != gid )
  4700. continue;
  4701. switch( type )
  4702. {
  4703. case 0: // Random
  4704. if(!map[pl_sd->bl.m].flag.nowarp)
  4705. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4706. break;
  4707. case 1: // SavePointAll
  4708. if(!map[pl_sd->bl.m].flag.noreturn)
  4709. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4710. break;
  4711. case 2: // SavePoint
  4712. if(!map[pl_sd->bl.m].flag.noreturn)
  4713. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4714. break;
  4715. case 3: // m,x,y
  4716. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4717. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4718. break;
  4719. }
  4720. }
  4721. mapit_free(iter);
  4722. return 0;
  4723. }
  4724. /*==========================================
  4725. *
  4726. *------------------------------------------*/
  4727. BUILDIN_FUNC(heal)
  4728. {
  4729. TBL_PC *sd;
  4730. int hp,sp;
  4731. sd = script_rid2sd(st);
  4732. if (!sd) return 0;
  4733. hp=script_getnum(st,2);
  4734. sp=script_getnum(st,3);
  4735. status_heal(&sd->bl, hp, sp, 1);
  4736. return 0;
  4737. }
  4738. /*==========================================
  4739. *
  4740. *------------------------------------------*/
  4741. BUILDIN_FUNC(itemheal)
  4742. {
  4743. TBL_PC *sd;
  4744. int hp,sp;
  4745. hp=script_getnum(st,2);
  4746. sp=script_getnum(st,3);
  4747. if(potion_flag==1) {
  4748. potion_hp = hp;
  4749. potion_sp = sp;
  4750. return 0;
  4751. }
  4752. sd = script_rid2sd(st);
  4753. if (!sd) return 0;
  4754. pc_itemheal(sd,sd->itemid,hp,sp);
  4755. return 0;
  4756. }
  4757. /*==========================================
  4758. *
  4759. *------------------------------------------*/
  4760. BUILDIN_FUNC(percentheal)
  4761. {
  4762. int hp,sp;
  4763. TBL_PC* sd;
  4764. hp=script_getnum(st,2);
  4765. sp=script_getnum(st,3);
  4766. if(potion_flag==1) {
  4767. potion_per_hp = hp;
  4768. potion_per_sp = sp;
  4769. return 0;
  4770. }
  4771. sd = script_rid2sd(st);
  4772. if( sd == NULL )
  4773. return 0;
  4774. #ifdef RENEWAL
  4775. if( sd->sc.data[SC_EXTREMITYFIST] ){
  4776. const struct TimerData *timer;
  4777. int tick = skill_get_time2(MO_EXTREMITYFIST, sd->sc.data[SC_EXTREMITYFIST]->val1);
  4778. timer = get_timer(sd->sc.data[SC_EXTREMITYFIST]->timer);
  4779. if( DIFF_TICK(tick, DIFF_TICK(timer->tick, gettick())) < 10000 )// 10 sec
  4780. sp = 0;
  4781. }
  4782. #endif
  4783. pc_percentheal(sd,hp,sp);
  4784. return 0;
  4785. }
  4786. /*==========================================
  4787. *
  4788. *------------------------------------------*/
  4789. BUILDIN_FUNC(jobchange)
  4790. {
  4791. int job, upper=-1;
  4792. job=script_getnum(st,2);
  4793. if( script_hasdata(st,3) )
  4794. upper=script_getnum(st,3);
  4795. if (pcdb_checkid(job))
  4796. {
  4797. TBL_PC* sd;
  4798. sd = script_rid2sd(st);
  4799. if( sd == NULL )
  4800. return 0;
  4801. pc_jobchange(sd, job, upper);
  4802. }
  4803. return 0;
  4804. }
  4805. /*==========================================
  4806. *
  4807. *------------------------------------------*/
  4808. BUILDIN_FUNC(jobname)
  4809. {
  4810. int class_=script_getnum(st,2);
  4811. script_pushconststr(st, (char*)job_name(class_));
  4812. return 0;
  4813. }
  4814. /// Get input from the player.
  4815. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4816. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4817. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4818. /// shorter than 'min' and 0 otherwise.
  4819. ///
  4820. /// input(<var>{,<min>{,<max>}}) -> <int>
  4821. BUILDIN_FUNC(input)
  4822. {
  4823. TBL_PC* sd;
  4824. struct script_data* data;
  4825. int uid;
  4826. const char* name;
  4827. int min;
  4828. int max;
  4829. sd = script_rid2sd(st);
  4830. if( sd == NULL )
  4831. return 0;
  4832. data = script_getdata(st,2);
  4833. if( !data_isreference(data) ){
  4834. ShowError("script:input: not a variable\n");
  4835. script_reportdata(data);
  4836. st->state = END;
  4837. return 1;
  4838. }
  4839. uid = reference_getuid(data);
  4840. name = reference_getname(data);
  4841. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4842. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4843. if( !sd->state.menu_or_input )
  4844. { // first invocation, display npc input box
  4845. sd->state.menu_or_input = 1;
  4846. st->state = RERUNLINE;
  4847. if( is_string_variable(name) )
  4848. clif_scriptinputstr(sd,st->oid);
  4849. else
  4850. clif_scriptinput(sd,st->oid);
  4851. }
  4852. else
  4853. { // take received text/value and store it in the designated variable
  4854. sd->state.menu_or_input = 0;
  4855. if( is_string_variable(name) )
  4856. {
  4857. int len = (int)strlen(sd->npc_str);
  4858. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4859. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4860. }
  4861. else
  4862. {
  4863. int amount = sd->npc_amount;
  4864. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  4865. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4866. }
  4867. st->state = RUN;
  4868. }
  4869. return 0;
  4870. }
  4871. // declare the copyarray method here for future reference
  4872. BUILDIN_FUNC(copyarray);
  4873. /// Sets the value of a variable.
  4874. /// The value is converted to the type of the variable.
  4875. ///
  4876. /// set(<variable>,<value>) -> <variable>
  4877. BUILDIN_FUNC(set)
  4878. {
  4879. TBL_PC* sd = NULL;
  4880. struct script_data* data;
  4881. //struct script_data* datavalue;
  4882. int num;
  4883. const char* name;
  4884. char prefix;
  4885. data = script_getdata(st,2);
  4886. //datavalue = script_getdata(st,3);
  4887. if( !data_isreference(data) )
  4888. {
  4889. ShowError("script:set: not a variable\n");
  4890. script_reportdata(script_getdata(st,2));
  4891. st->state = END;
  4892. return 1;
  4893. }
  4894. num = reference_getuid(data);
  4895. name = reference_getname(data);
  4896. prefix = *name;
  4897. if( not_server_variable(prefix) )
  4898. {
  4899. sd = script_rid2sd(st);
  4900. if( sd == NULL )
  4901. {
  4902. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4903. return 0;
  4904. }
  4905. }
  4906. #if 0
  4907. if( data_isreference(datavalue) )
  4908. {// the value being referenced is a variable
  4909. const char* namevalue = reference_getname(datavalue);
  4910. if( !not_array_variable(*namevalue) )
  4911. {// array variable being copied into another array variable
  4912. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  4913. {// player must be attached in order to copy a player variable
  4914. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  4915. return 0;
  4916. }
  4917. if( is_string_variable(namevalue) != is_string_variable(name) )
  4918. {// non-matching array value types
  4919. ShowWarning("script:set: two array variables do not match in type.\n");
  4920. return 0;
  4921. }
  4922. // push the maximum number of array values to the stack
  4923. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  4924. // call the copy array method directly
  4925. return buildin_copyarray(st);
  4926. }
  4927. }
  4928. #endif
  4929. if( is_string_variable(name) )
  4930. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4931. else
  4932. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  4933. // return a copy of the variable reference
  4934. script_pushcopy(st,2);
  4935. return 0;
  4936. }
  4937. /////////////////////////////////////////////////////////////////////
  4938. /// Array variables
  4939. ///
  4940. /// Returns the size of the specified array
  4941. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  4942. {
  4943. int32 ret = idx;
  4944. if( isstring )
  4945. {
  4946. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4947. {
  4948. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4949. if( str && *str )
  4950. ret = idx + 1;
  4951. script_removetop(st, -1, 0);
  4952. }
  4953. }
  4954. else
  4955. {
  4956. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4957. {
  4958. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  4959. if( num )
  4960. ret = idx + 1;
  4961. script_removetop(st, -1, 0);
  4962. }
  4963. }
  4964. return ret;
  4965. }
  4966. /// Sets values of an array, from the starting index.
  4967. /// ex: setarray arr[1],1,2,3;
  4968. ///
  4969. /// setarray <array variable>,<value1>{,<value2>...};
  4970. BUILDIN_FUNC(setarray)
  4971. {
  4972. struct script_data* data;
  4973. const char* name;
  4974. int32 start;
  4975. int32 end;
  4976. int32 id;
  4977. int32 i;
  4978. TBL_PC* sd = NULL;
  4979. data = script_getdata(st, 2);
  4980. if( !data_isreference(data) )
  4981. {
  4982. ShowError("script:setarray: not a variable\n");
  4983. script_reportdata(data);
  4984. st->state = END;
  4985. return 1;// not a variable
  4986. }
  4987. id = reference_getid(data);
  4988. start = reference_getindex(data);
  4989. name = reference_getname(data);
  4990. if( not_array_variable(*name) )
  4991. {
  4992. ShowError("script:setarray: illegal scope\n");
  4993. script_reportdata(data);
  4994. st->state = END;
  4995. return 1;// not supported
  4996. }
  4997. if( not_server_variable(*name) )
  4998. {
  4999. sd = script_rid2sd(st);
  5000. if( sd == NULL )
  5001. return 0;// no player attached
  5002. }
  5003. end = start + script_lastdata(st) - 2;
  5004. if( end > SCRIPT_MAX_ARRAYSIZE )
  5005. end = SCRIPT_MAX_ARRAYSIZE;
  5006. if( is_string_variable(name) )
  5007. {// string array
  5008. for( i = 3; start < end; ++start, ++i )
  5009. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5010. }
  5011. else
  5012. {// int array
  5013. for( i = 3; start < end; ++start, ++i )
  5014. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5015. }
  5016. return 0;
  5017. }
  5018. /// Sets count values of an array, from the starting index.
  5019. /// ex: cleararray arr[0],0,1;
  5020. ///
  5021. /// cleararray <array variable>,<value>,<count>;
  5022. BUILDIN_FUNC(cleararray)
  5023. {
  5024. struct script_data* data;
  5025. const char* name;
  5026. int32 start;
  5027. int32 end;
  5028. int32 id;
  5029. void* v;
  5030. TBL_PC* sd = NULL;
  5031. data = script_getdata(st, 2);
  5032. if( !data_isreference(data) )
  5033. {
  5034. ShowError("script:cleararray: not a variable\n");
  5035. script_reportdata(data);
  5036. st->state = END;
  5037. return 1;// not a variable
  5038. }
  5039. id = reference_getid(data);
  5040. start = reference_getindex(data);
  5041. name = reference_getname(data);
  5042. if( not_array_variable(*name) )
  5043. {
  5044. ShowError("script:cleararray: illegal scope\n");
  5045. script_reportdata(data);
  5046. st->state = END;
  5047. return 1;// not supported
  5048. }
  5049. if( not_server_variable(*name) )
  5050. {
  5051. sd = script_rid2sd(st);
  5052. if( sd == NULL )
  5053. return 0;// no player attached
  5054. }
  5055. if( is_string_variable(name) )
  5056. v = (void*)script_getstr(st, 3);
  5057. else
  5058. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5059. end = start + script_getnum(st, 4);
  5060. if( end > SCRIPT_MAX_ARRAYSIZE )
  5061. end = SCRIPT_MAX_ARRAYSIZE;
  5062. for( ; start < end; ++start )
  5063. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5064. return 0;
  5065. }
  5066. /// Copies data from one array to another.
  5067. /// ex: copyarray arr[0],arr[2],2;
  5068. ///
  5069. /// copyarray <destination array variable>,<source array variable>,<count>;
  5070. BUILDIN_FUNC(copyarray)
  5071. {
  5072. struct script_data* data1;
  5073. struct script_data* data2;
  5074. const char* name1;
  5075. const char* name2;
  5076. int32 idx1;
  5077. int32 idx2;
  5078. int32 id1;
  5079. int32 id2;
  5080. void* v;
  5081. int32 i;
  5082. int32 count;
  5083. TBL_PC* sd = NULL;
  5084. data1 = script_getdata(st, 2);
  5085. data2 = script_getdata(st, 3);
  5086. if( !data_isreference(data1) || !data_isreference(data2) )
  5087. {
  5088. ShowError("script:copyarray: not a variable\n");
  5089. script_reportdata(data1);
  5090. script_reportdata(data2);
  5091. st->state = END;
  5092. return 1;// not a variable
  5093. }
  5094. id1 = reference_getid(data1);
  5095. id2 = reference_getid(data2);
  5096. idx1 = reference_getindex(data1);
  5097. idx2 = reference_getindex(data2);
  5098. name1 = reference_getname(data1);
  5099. name2 = reference_getname(data2);
  5100. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5101. {
  5102. ShowError("script:copyarray: illegal scope\n");
  5103. script_reportdata(data1);
  5104. script_reportdata(data2);
  5105. st->state = END;
  5106. return 1;// not supported
  5107. }
  5108. if( is_string_variable(name1) != is_string_variable(name2) )
  5109. {
  5110. ShowError("script:copyarray: type mismatch\n");
  5111. script_reportdata(data1);
  5112. script_reportdata(data2);
  5113. st->state = END;
  5114. return 1;// data type mismatch
  5115. }
  5116. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5117. {
  5118. sd = script_rid2sd(st);
  5119. if( sd == NULL )
  5120. return 0;// no player attached
  5121. }
  5122. count = script_getnum(st, 4);
  5123. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5124. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5125. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5126. return 0;// nothing to copy
  5127. if( id1 == id2 && idx1 > idx2 )
  5128. {// destination might be overlapping the source - copy in reverse order
  5129. for( i = count - 1; i >= 0; --i )
  5130. {
  5131. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5132. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5133. script_removetop(st, -1, 0);
  5134. }
  5135. }
  5136. else
  5137. {// normal copy
  5138. for( i = 0; i < count; ++i )
  5139. {
  5140. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5141. {
  5142. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5143. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5144. script_removetop(st, -1, 0);
  5145. }
  5146. else// out of range - assume ""/0
  5147. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5148. }
  5149. }
  5150. return 0;
  5151. }
  5152. /// Returns the size of the array.
  5153. /// Assumes that everything before the starting index exists.
  5154. /// ex: getarraysize(arr[3])
  5155. ///
  5156. /// getarraysize(<array variable>) -> <int>
  5157. BUILDIN_FUNC(getarraysize)
  5158. {
  5159. struct script_data* data;
  5160. const char* name;
  5161. data = script_getdata(st, 2);
  5162. if( !data_isreference(data) )
  5163. {
  5164. ShowError("script:getarraysize: not a variable\n");
  5165. script_reportdata(data);
  5166. script_pushnil(st);
  5167. st->state = END;
  5168. return 1;// not a variable
  5169. }
  5170. name = reference_getname(data);
  5171. if( not_array_variable(*name) )
  5172. {
  5173. ShowError("script:getarraysize: illegal scope\n");
  5174. script_reportdata(data);
  5175. script_pushnil(st);
  5176. st->state = END;
  5177. return 1;// not supported
  5178. }
  5179. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5180. return 0;
  5181. }
  5182. /// Deletes count or all the elements in an array, from the starting index.
  5183. /// ex: deletearray arr[4],2;
  5184. ///
  5185. /// deletearray <array variable>;
  5186. /// deletearray <array variable>,<count>;
  5187. BUILDIN_FUNC(deletearray)
  5188. {
  5189. struct script_data* data;
  5190. const char* name;
  5191. int start;
  5192. int end;
  5193. int id;
  5194. TBL_PC *sd = NULL;
  5195. data = script_getdata(st, 2);
  5196. if( !data_isreference(data) )
  5197. {
  5198. ShowError("script:deletearray: not a variable\n");
  5199. script_reportdata(data);
  5200. st->state = END;
  5201. return 1;// not a variable
  5202. }
  5203. id = reference_getid(data);
  5204. start = reference_getindex(data);
  5205. name = reference_getname(data);
  5206. if( not_array_variable(*name) )
  5207. {
  5208. ShowError("script:deletearray: illegal scope\n");
  5209. script_reportdata(data);
  5210. st->state = END;
  5211. return 1;// not supported
  5212. }
  5213. if( not_server_variable(*name) )
  5214. {
  5215. sd = script_rid2sd(st);
  5216. if( sd == NULL )
  5217. return 0;// no player attached
  5218. }
  5219. end = SCRIPT_MAX_ARRAYSIZE;
  5220. if( start >= end )
  5221. return 0;// nothing to free
  5222. if( script_hasdata(st,3) )
  5223. {
  5224. int count = script_getnum(st, 3);
  5225. if( count > end - start )
  5226. count = end - start;
  5227. if( count <= 0 )
  5228. return 0;// nothing to free
  5229. // move rest of the elements backward
  5230. for( ; start + count < end; ++start )
  5231. {
  5232. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5233. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5234. script_removetop(st, -1, 0);
  5235. }
  5236. }
  5237. // clear the rest of the array
  5238. if( is_string_variable(name) )
  5239. {
  5240. for( ; start < end; ++start )
  5241. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5242. }
  5243. else
  5244. {
  5245. for( ; start < end; ++start )
  5246. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5247. }
  5248. return 0;
  5249. }
  5250. /// Returns a reference to the target index of the array variable.
  5251. /// Equivalent to var[index].
  5252. ///
  5253. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5254. BUILDIN_FUNC(getelementofarray)
  5255. {
  5256. struct script_data* data;
  5257. const char* name;
  5258. int32 id;
  5259. int i;
  5260. data = script_getdata(st, 2);
  5261. if( !data_isreference(data) )
  5262. {
  5263. ShowError("script:getelementofarray: not a variable\n");
  5264. script_reportdata(data);
  5265. script_pushnil(st);
  5266. st->state = END;
  5267. return 1;// not a variable
  5268. }
  5269. id = reference_getid(data);
  5270. name = reference_getname(data);
  5271. if( not_array_variable(*name) )
  5272. {
  5273. ShowError("script:getelementofarray: illegal scope\n");
  5274. script_reportdata(data);
  5275. script_pushnil(st);
  5276. st->state = END;
  5277. return 1;// not supported
  5278. }
  5279. i = script_getnum(st, 3);
  5280. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  5281. {
  5282. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5283. script_reportdata(data);
  5284. script_pushnil(st);
  5285. st->state = END;
  5286. return 1;// out of range
  5287. }
  5288. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5289. return 0;
  5290. }
  5291. /////////////////////////////////////////////////////////////////////
  5292. /// ...
  5293. ///
  5294. /*==========================================
  5295. *
  5296. *------------------------------------------*/
  5297. BUILDIN_FUNC(setlook)
  5298. {
  5299. int type,val;
  5300. TBL_PC* sd;
  5301. type=script_getnum(st,2);
  5302. val=script_getnum(st,3);
  5303. sd = script_rid2sd(st);
  5304. if( sd == NULL )
  5305. return 0;
  5306. pc_changelook(sd,type,val);
  5307. return 0;
  5308. }
  5309. BUILDIN_FUNC(changelook)
  5310. { // As setlook but only client side
  5311. int type,val;
  5312. TBL_PC* sd;
  5313. type=script_getnum(st,2);
  5314. val=script_getnum(st,3);
  5315. sd = script_rid2sd(st);
  5316. if( sd == NULL )
  5317. return 0;
  5318. clif_changelook(&sd->bl,type,val);
  5319. return 0;
  5320. }
  5321. /*==========================================
  5322. *
  5323. *------------------------------------------*/
  5324. BUILDIN_FUNC(cutin)
  5325. {
  5326. TBL_PC* sd;
  5327. sd = script_rid2sd(st);
  5328. if( sd == NULL )
  5329. return 0;
  5330. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5331. return 0;
  5332. }
  5333. /*==========================================
  5334. *
  5335. *------------------------------------------*/
  5336. BUILDIN_FUNC(viewpoint)
  5337. {
  5338. int type,x,y,id,color;
  5339. TBL_PC* sd;
  5340. type=script_getnum(st,2);
  5341. x=script_getnum(st,3);
  5342. y=script_getnum(st,4);
  5343. id=script_getnum(st,5);
  5344. color=script_getnum(st,6);
  5345. sd = script_rid2sd(st);
  5346. if( sd == NULL )
  5347. return 0;
  5348. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5349. return 0;
  5350. }
  5351. /*==========================================
  5352. *
  5353. *------------------------------------------*/
  5354. BUILDIN_FUNC(countitem)
  5355. {
  5356. int nameid, i;
  5357. int count = 0;
  5358. struct item_data* id = NULL;
  5359. struct script_data* data;
  5360. TBL_PC* sd = script_rid2sd(st);
  5361. if (!sd) {
  5362. script_pushint(st,0);
  5363. return 0;
  5364. }
  5365. data = script_getdata(st,2);
  5366. get_val(st, data); // convert into value in case of a variable
  5367. if( data_isstring(data) )
  5368. {// item name
  5369. id = itemdb_searchname(conv_str(st, data));
  5370. }
  5371. else
  5372. {// item id
  5373. id = itemdb_exists(conv_num(st, data));
  5374. }
  5375. if( id == NULL )
  5376. {
  5377. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5378. script_pushint(st,0);
  5379. return 1;
  5380. }
  5381. nameid = id->nameid;
  5382. for(i = 0; i < MAX_INVENTORY; i++)
  5383. if(sd->status.inventory[i].nameid == nameid)
  5384. count += sd->status.inventory[i].amount;
  5385. script_pushint(st,count);
  5386. return 0;
  5387. }
  5388. /*==========================================
  5389. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  5390. * returns number of items that meet the conditions
  5391. *------------------------------------------*/
  5392. BUILDIN_FUNC(countitem2)
  5393. {
  5394. int nameid, iden, ref, attr, c1, c2, c3, c4;
  5395. int count = 0;
  5396. int i;
  5397. struct item_data* id = NULL;
  5398. struct script_data* data;
  5399. TBL_PC* sd = script_rid2sd(st);
  5400. if (!sd) {
  5401. script_pushint(st,0);
  5402. return 0;
  5403. }
  5404. data = script_getdata(st,2);
  5405. get_val(st, data); // convert into value in case of a variable
  5406. if( data_isstring(data) )
  5407. {// item name
  5408. id = itemdb_searchname(conv_str(st, data));
  5409. }
  5410. else
  5411. {// item id
  5412. id = itemdb_exists(conv_num(st, data));
  5413. }
  5414. if( id == NULL )
  5415. {
  5416. ShowError("buildin_countitem2: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5417. script_pushint(st,0);
  5418. return 1;
  5419. }
  5420. nameid = id->nameid;
  5421. iden = script_getnum(st,3);
  5422. ref = script_getnum(st,4);
  5423. attr = script_getnum(st,5);
  5424. c1 = (short)script_getnum(st,6);
  5425. c2 = (short)script_getnum(st,7);
  5426. c3 = (short)script_getnum(st,8);
  5427. c4 = (short)script_getnum(st,9);
  5428. for(i = 0; i < MAX_INVENTORY; i++)
  5429. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  5430. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  5431. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  5432. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  5433. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  5434. sd->status.inventory[i].card[3] == c4
  5435. )
  5436. count += sd->status.inventory[i].amount;
  5437. script_pushint(st,count);
  5438. return 0;
  5439. }
  5440. /*==========================================
  5441. * 重量チェック
  5442. *------------------------------------------*/
  5443. BUILDIN_FUNC(checkweight)
  5444. {
  5445. int nameid, amount, slots;
  5446. unsigned int weight;
  5447. struct item_data* id = NULL;
  5448. struct map_session_data* sd;
  5449. struct script_data* data;
  5450. if( ( sd = script_rid2sd(st) ) == NULL )
  5451. {
  5452. return 0;
  5453. }
  5454. data = script_getdata(st,2);
  5455. get_val(st, data); // convert into value in case of a variable
  5456. if( data_isstring(data) )
  5457. {// item name
  5458. id = itemdb_searchname(conv_str(st, data));
  5459. }
  5460. else
  5461. {// item id
  5462. id = itemdb_exists(conv_num(st, data));
  5463. }
  5464. if( id == NULL )
  5465. {
  5466. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5467. script_pushint(st,0);
  5468. return 1;
  5469. }
  5470. nameid = id->nameid;
  5471. amount = script_getnum(st,3);
  5472. if( amount < 1 )
  5473. {
  5474. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5475. script_pushint(st,0);
  5476. return 1;
  5477. }
  5478. weight = itemdb_weight(nameid)*amount;
  5479. if( weight + sd->weight > sd->max_weight )
  5480. {// too heavy
  5481. script_pushint(st,0);
  5482. return 0;
  5483. }
  5484. switch( pc_checkadditem(sd, nameid, amount) )
  5485. {
  5486. case ADDITEM_EXIST:
  5487. // item is already in inventory, but there is still space for the requested amount
  5488. break;
  5489. case ADDITEM_NEW:
  5490. slots = pc_inventoryblank(sd);
  5491. if( itemdb_isstackable(nameid) )
  5492. {// stackable
  5493. if( slots < 1 )
  5494. {
  5495. script_pushint(st,0);
  5496. return 0;
  5497. }
  5498. }
  5499. else
  5500. {// non-stackable
  5501. if( slots < amount )
  5502. {
  5503. script_pushint(st,0);
  5504. return 0;
  5505. }
  5506. }
  5507. break;
  5508. case ADDITEM_OVERAMOUNT:
  5509. script_pushint(st,0);
  5510. return 0;
  5511. }
  5512. script_pushint(st,1);
  5513. return 0;
  5514. }
  5515. /*==========================================
  5516. * getitem <item id>,<amount>{,<account ID>};
  5517. * getitem "<item name>",<amount>{,<account ID>};
  5518. *------------------------------------------*/
  5519. BUILDIN_FUNC(getitem)
  5520. {
  5521. int nameid,amount,get_count,i,flag = 0;
  5522. struct item it;
  5523. TBL_PC *sd;
  5524. struct script_data *data;
  5525. data=script_getdata(st,2);
  5526. get_val(st,data);
  5527. if( data_isstring(data) )
  5528. {// "<item name>"
  5529. const char *name=conv_str(st,data);
  5530. struct item_data *item_data = itemdb_searchname(name);
  5531. if( item_data == NULL ){
  5532. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5533. return 1; //No item created.
  5534. }
  5535. nameid=item_data->nameid;
  5536. } else if( data_isint(data) )
  5537. {// <item id>
  5538. nameid=conv_num(st,data);
  5539. //Violet Box, Blue Box, etc - random item pick
  5540. if( nameid < 0 ) {
  5541. nameid = -nameid;
  5542. flag = 1;
  5543. }
  5544. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5545. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5546. return 1; //No item created.
  5547. }
  5548. } else {
  5549. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5550. return 1;
  5551. }
  5552. // <amount>
  5553. if( (amount=script_getnum(st,3)) <= 0)
  5554. return 0; //return if amount <=0, skip the useles iteration
  5555. memset(&it,0,sizeof(it));
  5556. it.nameid=nameid;
  5557. if(!flag)
  5558. it.identify=1;
  5559. else
  5560. it.identify=itemdb_isidentified(nameid);
  5561. if( script_hasdata(st,4) )
  5562. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5563. else
  5564. sd=script_rid2sd(st); // Attached player
  5565. if( sd == NULL ) // no target
  5566. return 0;
  5567. //Check if it's stackable.
  5568. if (!itemdb_isstackable(nameid))
  5569. get_count = 1;
  5570. else
  5571. get_count = amount;
  5572. for (i = 0; i < amount; i += get_count)
  5573. {
  5574. // if not pet egg
  5575. if (!pet_create_egg(sd, nameid))
  5576. {
  5577. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5578. {
  5579. clif_additem(sd, 0, 0, flag);
  5580. if( pc_candrop(sd,&it) )
  5581. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5582. }
  5583. }
  5584. }
  5585. return 0;
  5586. }
  5587. /*==========================================
  5588. *
  5589. *------------------------------------------*/
  5590. BUILDIN_FUNC(getitem2)
  5591. {
  5592. int nameid,amount,get_count,i,flag = 0;
  5593. int iden,ref,attr,c1,c2,c3,c4;
  5594. struct item_data *item_data;
  5595. struct item item_tmp;
  5596. TBL_PC *sd;
  5597. struct script_data *data;
  5598. if( script_hasdata(st,11) )
  5599. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5600. else
  5601. sd=script_rid2sd(st); // Attached player
  5602. if( sd == NULL ) // no target
  5603. return 0;
  5604. data=script_getdata(st,2);
  5605. get_val(st,data);
  5606. if( data_isstring(data) ){
  5607. const char *name=conv_str(st,data);
  5608. struct item_data *item_data = itemdb_searchname(name);
  5609. if( item_data )
  5610. nameid=item_data->nameid;
  5611. else
  5612. nameid=UNKNOWN_ITEM_ID;
  5613. }else
  5614. nameid=conv_num(st,data);
  5615. amount=script_getnum(st,3);
  5616. iden=script_getnum(st,4);
  5617. ref=script_getnum(st,5);
  5618. attr=script_getnum(st,6);
  5619. c1=(short)script_getnum(st,7);
  5620. c2=(short)script_getnum(st,8);
  5621. c3=(short)script_getnum(st,9);
  5622. c4=(short)script_getnum(st,10);
  5623. if(nameid<0) { // ランダム
  5624. nameid = -nameid;
  5625. flag = 1;
  5626. }
  5627. if(nameid > 0) {
  5628. memset(&item_tmp,0,sizeof(item_tmp));
  5629. item_data=itemdb_exists(nameid);
  5630. if (item_data == NULL)
  5631. return -1;
  5632. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5633. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5634. }
  5635. else if(item_data->type==IT_PETEGG) {
  5636. iden = 1;
  5637. ref = 0;
  5638. }
  5639. else {
  5640. iden = 1;
  5641. ref = attr = 0;
  5642. }
  5643. item_tmp.nameid=nameid;
  5644. if(!flag)
  5645. item_tmp.identify=iden;
  5646. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5647. item_tmp.identify=0;
  5648. item_tmp.refine=ref;
  5649. item_tmp.attribute=attr;
  5650. item_tmp.card[0]=(short)c1;
  5651. item_tmp.card[1]=(short)c2;
  5652. item_tmp.card[2]=(short)c3;
  5653. item_tmp.card[3]=(short)c4;
  5654. //Check if it's stackable.
  5655. if (!itemdb_isstackable(nameid))
  5656. get_count = 1;
  5657. else
  5658. get_count = amount;
  5659. for (i = 0; i < amount; i += get_count)
  5660. {
  5661. // if not pet egg
  5662. if (!pet_create_egg(sd, nameid))
  5663. {
  5664. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5665. {
  5666. clif_additem(sd, 0, 0, flag);
  5667. if( pc_candrop(sd,&item_tmp) )
  5668. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5669. }
  5670. }
  5671. }
  5672. }
  5673. return 0;
  5674. }
  5675. /*==========================================
  5676. * rentitem <item id>,<seconds>
  5677. * rentitem "<item name>",<seconds>
  5678. *------------------------------------------*/
  5679. BUILDIN_FUNC(rentitem)
  5680. {
  5681. struct map_session_data *sd;
  5682. struct script_data *data;
  5683. struct item it;
  5684. int seconds;
  5685. int nameid = 0, flag;
  5686. data = script_getdata(st,2);
  5687. get_val(st,data);
  5688. if( (sd = script_rid2sd(st)) == NULL )
  5689. return 0;
  5690. if( data_isstring(data) )
  5691. {
  5692. const char *name = conv_str(st,data);
  5693. struct item_data *itd = itemdb_searchname(name);
  5694. if( itd == NULL )
  5695. {
  5696. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5697. return 1;
  5698. }
  5699. nameid = itd->nameid;
  5700. }
  5701. else if( data_isint(data) )
  5702. {
  5703. nameid = conv_num(st,data);
  5704. if( nameid <= 0 || !itemdb_exists(nameid) )
  5705. {
  5706. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5707. return 1;
  5708. }
  5709. }
  5710. else
  5711. {
  5712. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5713. return 1;
  5714. }
  5715. seconds = script_getnum(st,3);
  5716. memset(&it, 0, sizeof(it));
  5717. it.nameid = nameid;
  5718. it.identify = 1;
  5719. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5720. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5721. {
  5722. clif_additem(sd, 0, 0, flag);
  5723. return 1;
  5724. }
  5725. return 0;
  5726. }
  5727. /*==========================================
  5728. * gets an item with someone's name inscribed [Skotlex]
  5729. * getinscribeditem item_num, character_name
  5730. * Returned Qty is always 1, only works on equip-able
  5731. * equipment
  5732. *------------------------------------------*/
  5733. BUILDIN_FUNC(getnameditem)
  5734. {
  5735. int nameid;
  5736. struct item item_tmp;
  5737. TBL_PC *sd, *tsd;
  5738. struct script_data *data;
  5739. sd = script_rid2sd(st);
  5740. if (sd == NULL)
  5741. { //Player not attached!
  5742. script_pushint(st,0);
  5743. return 0;
  5744. }
  5745. data=script_getdata(st,2);
  5746. get_val(st,data);
  5747. if( data_isstring(data) ){
  5748. const char *name=conv_str(st,data);
  5749. struct item_data *item_data = itemdb_searchname(name);
  5750. if( item_data == NULL)
  5751. { //Failed
  5752. script_pushint(st,0);
  5753. return 0;
  5754. }
  5755. nameid = item_data->nameid;
  5756. }else
  5757. nameid = conv_num(st,data);
  5758. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5759. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5760. script_pushint(st,0);
  5761. return 0;
  5762. }
  5763. data=script_getdata(st,3);
  5764. get_val(st,data);
  5765. if( data_isstring(data) ) //Char Name
  5766. tsd=map_nick2sd(conv_str(st,data));
  5767. else //Char Id was given
  5768. tsd=map_charid2sd(conv_num(st,data));
  5769. if( tsd == NULL )
  5770. { //Failed
  5771. script_pushint(st,0);
  5772. return 0;
  5773. }
  5774. memset(&item_tmp,0,sizeof(item_tmp));
  5775. item_tmp.nameid=nameid;
  5776. item_tmp.amount=1;
  5777. item_tmp.identify=1;
  5778. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5779. item_tmp.card[2]=tsd->status.char_id;
  5780. item_tmp.card[3]=tsd->status.char_id >> 16;
  5781. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  5782. script_pushint(st,0);
  5783. return 0; //Failed to add item, we will not drop if they don't fit
  5784. }
  5785. script_pushint(st,1);
  5786. return 0;
  5787. }
  5788. /*==========================================
  5789. * gets a random item ID from an item group [Skotlex]
  5790. * groupranditem group_num
  5791. *------------------------------------------*/
  5792. BUILDIN_FUNC(grouprandomitem)
  5793. {
  5794. int group;
  5795. group = script_getnum(st,2);
  5796. script_pushint(st,itemdb_searchrandomid(group));
  5797. return 0;
  5798. }
  5799. /*==========================================
  5800. *
  5801. *------------------------------------------*/
  5802. BUILDIN_FUNC(makeitem)
  5803. {
  5804. int nameid,amount,flag = 0;
  5805. int x,y,m;
  5806. const char *mapname;
  5807. struct item item_tmp;
  5808. struct script_data *data;
  5809. data=script_getdata(st,2);
  5810. get_val(st,data);
  5811. if( data_isstring(data) ){
  5812. const char *name=conv_str(st,data);
  5813. struct item_data *item_data = itemdb_searchname(name);
  5814. if( item_data )
  5815. nameid=item_data->nameid;
  5816. else
  5817. nameid=UNKNOWN_ITEM_ID;
  5818. }else
  5819. nameid=conv_num(st,data);
  5820. amount=script_getnum(st,3);
  5821. mapname =script_getstr(st,4);
  5822. x =script_getnum(st,5);
  5823. y =script_getnum(st,6);
  5824. if(strcmp(mapname,"this")==0)
  5825. {
  5826. TBL_PC *sd;
  5827. sd = script_rid2sd(st);
  5828. if (!sd) return 0; //Failed...
  5829. m=sd->bl.m;
  5830. } else
  5831. m=map_mapname2mapid(mapname);
  5832. if(nameid<0) { // ランダム
  5833. nameid = -nameid;
  5834. flag = 1;
  5835. }
  5836. if(nameid > 0) {
  5837. memset(&item_tmp,0,sizeof(item_tmp));
  5838. item_tmp.nameid=nameid;
  5839. if(!flag)
  5840. item_tmp.identify=1;
  5841. else
  5842. item_tmp.identify=itemdb_isidentified(nameid);
  5843. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5844. }
  5845. return 0;
  5846. }
  5847. /// Counts / deletes the current item given by idx.
  5848. /// Used by buildin_delitem_search
  5849. /// Relies on all input data being already fully valid.
  5850. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5851. {
  5852. int delamount;
  5853. struct item* inv = &sd->status.inventory[idx];
  5854. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5855. if( delete_items )
  5856. {
  5857. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5858. {// delete associated pet
  5859. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5860. }
  5861. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  5862. }
  5863. amount[0]-= delamount;
  5864. }
  5865. /// Searches for item(s) and checks, if there is enough of them.
  5866. /// Used by delitem and delitem2
  5867. /// Relies on all input data being already fully valid.
  5868. /// @param exact_match will also match item attributes and cards, not just name id
  5869. /// @return true when all items could be deleted, false when there were not enough items to delete
  5870. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5871. {
  5872. bool delete_items = false;
  5873. int i, amount, important;
  5874. struct item* inv;
  5875. // prefer always non-equipped items
  5876. it->equip = 0;
  5877. // when searching for nameid only, prefer additionally
  5878. if( !exact_match )
  5879. {
  5880. // non-refined items
  5881. it->refine = 0;
  5882. // card-less items
  5883. memset(it->card, 0, sizeof(it->card));
  5884. }
  5885. for(;;)
  5886. {
  5887. amount = it->amount;
  5888. important = 0;
  5889. // 1st pass -- less important items / exact match
  5890. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5891. {
  5892. inv = &sd->status.inventory[i];
  5893. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5894. {// wrong/invalid item
  5895. continue;
  5896. }
  5897. if( inv->equip != it->equip || inv->refine != it->refine )
  5898. {// not matching attributes
  5899. important++;
  5900. continue;
  5901. }
  5902. if( exact_match )
  5903. {
  5904. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5905. {// not matching exact attributes
  5906. continue;
  5907. }
  5908. }
  5909. else
  5910. {
  5911. if( sd->inventory_data[i]->type == IT_PETEGG )
  5912. {
  5913. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  5914. {// pet which cannot be deleted
  5915. continue;
  5916. }
  5917. }
  5918. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  5919. {// named/carded item
  5920. important++;
  5921. continue;
  5922. }
  5923. }
  5924. // count / delete item
  5925. buildin_delitem_delete(sd, i, &amount, delete_items);
  5926. }
  5927. // 2nd pass -- any matching item
  5928. if( amount == 0 || important == 0 )
  5929. {// either everything was already consumed or no items were skipped
  5930. ;
  5931. }
  5932. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5933. {
  5934. inv = &sd->status.inventory[i];
  5935. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5936. {// wrong/invalid item
  5937. continue;
  5938. }
  5939. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  5940. {// pet which cannot be deleted
  5941. continue;
  5942. }
  5943. if( exact_match )
  5944. {
  5945. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5946. {// not matching attributes
  5947. continue;
  5948. }
  5949. }
  5950. // count / delete item
  5951. buildin_delitem_delete(sd, i, &amount, delete_items);
  5952. }
  5953. if( amount )
  5954. {// not enough items
  5955. return false;
  5956. }
  5957. else if( delete_items )
  5958. {// we are done with the work
  5959. return true;
  5960. }
  5961. else
  5962. {// get rid of the items now
  5963. delete_items = true;
  5964. }
  5965. }
  5966. }
  5967. /// Deletes items from the target/attached player.
  5968. /// Prioritizes ordinary items.
  5969. ///
  5970. /// delitem <item id>,<amount>{,<account id>}
  5971. /// delitem "<item name>",<amount>{,<account id>}
  5972. BUILDIN_FUNC(delitem)
  5973. {
  5974. TBL_PC *sd;
  5975. struct item it;
  5976. struct script_data *data;
  5977. if( script_hasdata(st,4) )
  5978. {
  5979. int account_id = script_getnum(st,4);
  5980. sd = map_id2sd(account_id); // <account id>
  5981. if( sd == NULL )
  5982. {
  5983. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5984. st->state = END;
  5985. return 1;
  5986. }
  5987. }
  5988. else
  5989. {
  5990. sd = script_rid2sd(st);// attached player
  5991. if( sd == NULL )
  5992. return 0;
  5993. }
  5994. data = script_getdata(st,2);
  5995. get_val(st,data);
  5996. if( data_isstring(data) )
  5997. {
  5998. const char* item_name = conv_str(st,data);
  5999. struct item_data* id = itemdb_searchname(item_name);
  6000. if( id == NULL )
  6001. {
  6002. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  6003. st->state = END;
  6004. return 1;
  6005. }
  6006. it.nameid = id->nameid;// "<item name>"
  6007. }
  6008. else
  6009. {
  6010. it.nameid = conv_num(st,data);// <item id>
  6011. if( !itemdb_exists( it.nameid ) )
  6012. {
  6013. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6014. st->state = END;
  6015. return 1;
  6016. }
  6017. }
  6018. it.amount=script_getnum(st,3);
  6019. if( it.amount <= 0 )
  6020. return 0;// nothing to do
  6021. if( buildin_delitem_search(sd, &it, false) )
  6022. {// success
  6023. return 0;
  6024. }
  6025. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6026. st->state = END;
  6027. clif_scriptclose(sd, st->oid);
  6028. return 1;
  6029. }
  6030. /// Deletes items from the target/attached player.
  6031. ///
  6032. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6033. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6034. BUILDIN_FUNC(delitem2)
  6035. {
  6036. TBL_PC *sd;
  6037. struct item it;
  6038. struct script_data *data;
  6039. if( script_hasdata(st,11) )
  6040. {
  6041. int account_id = script_getnum(st,11);
  6042. sd = map_id2sd(account_id); // <account id>
  6043. if( sd == NULL )
  6044. {
  6045. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  6046. st->state = END;
  6047. return 1;
  6048. }
  6049. }
  6050. else
  6051. {
  6052. sd = script_rid2sd(st);// attached player
  6053. if( sd == NULL )
  6054. return 0;
  6055. }
  6056. data = script_getdata(st,2);
  6057. get_val(st,data);
  6058. if( data_isstring(data) )
  6059. {
  6060. const char* item_name = conv_str(st,data);
  6061. struct item_data* id = itemdb_searchname(item_name);
  6062. if( id == NULL )
  6063. {
  6064. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  6065. st->state = END;
  6066. return 1;
  6067. }
  6068. it.nameid = id->nameid;// "<item name>"
  6069. }
  6070. else
  6071. {
  6072. it.nameid = conv_num(st,data);// <item id>
  6073. if( !itemdb_exists( it.nameid ) )
  6074. {
  6075. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6076. st->state = END;
  6077. return 1;
  6078. }
  6079. }
  6080. it.amount=script_getnum(st,3);
  6081. it.identify=script_getnum(st,4);
  6082. it.refine=script_getnum(st,5);
  6083. it.attribute=script_getnum(st,6);
  6084. it.card[0]=(short)script_getnum(st,7);
  6085. it.card[1]=(short)script_getnum(st,8);
  6086. it.card[2]=(short)script_getnum(st,9);
  6087. it.card[3]=(short)script_getnum(st,10);
  6088. if( it.amount <= 0 )
  6089. return 0;// nothing to do
  6090. if( buildin_delitem_search(sd, &it, true) )
  6091. {// success
  6092. return 0;
  6093. }
  6094. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6095. st->state = END;
  6096. clif_scriptclose(sd, st->oid);
  6097. return 1;
  6098. }
  6099. /*==========================================
  6100. * Enables/Disables use of items while in an NPC [Skotlex]
  6101. *------------------------------------------*/
  6102. BUILDIN_FUNC(enableitemuse)
  6103. {
  6104. TBL_PC *sd;
  6105. sd=script_rid2sd(st);
  6106. if (sd)
  6107. sd->npc_item_flag = st->oid;
  6108. return 0;
  6109. }
  6110. BUILDIN_FUNC(disableitemuse)
  6111. {
  6112. TBL_PC *sd;
  6113. sd=script_rid2sd(st);
  6114. if (sd)
  6115. sd->npc_item_flag = 0;
  6116. return 0;
  6117. }
  6118. /*==========================================
  6119. *キャラ関係のパラメータ取得
  6120. *------------------------------------------*/
  6121. BUILDIN_FUNC(readparam)
  6122. {
  6123. int type;
  6124. TBL_PC *sd;
  6125. type=script_getnum(st,2);
  6126. if( script_hasdata(st,3) )
  6127. sd=map_nick2sd(script_getstr(st,3));
  6128. else
  6129. sd=script_rid2sd(st);
  6130. if(sd==NULL){
  6131. script_pushint(st,-1);
  6132. return 0;
  6133. }
  6134. script_pushint(st,pc_readparam(sd,type));
  6135. return 0;
  6136. }
  6137. /*==========================================
  6138. *キャラ関係のID取得
  6139. *------------------------------------------*/
  6140. BUILDIN_FUNC(getcharid)
  6141. {
  6142. int num;
  6143. TBL_PC *sd;
  6144. num = script_getnum(st,2);
  6145. if( script_hasdata(st,3) )
  6146. sd=map_nick2sd(script_getstr(st,3));
  6147. else
  6148. sd=script_rid2sd(st);
  6149. if(sd==NULL){
  6150. script_pushint(st,0); //return 0, according docs
  6151. return 0;
  6152. }
  6153. switch( num ) {
  6154. case 0: script_pushint(st,sd->status.char_id); break;
  6155. case 1: script_pushint(st,sd->status.party_id); break;
  6156. case 2: script_pushint(st,sd->status.guild_id); break;
  6157. case 3: script_pushint(st,sd->status.account_id); break;
  6158. case 4: script_pushint(st,sd->bg_id); break;
  6159. default:
  6160. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6161. script_pushint(st,0);
  6162. break;
  6163. }
  6164. return 0;
  6165. }
  6166. /*==========================================
  6167. * returns the GID of an NPC
  6168. *------------------------------------------*/
  6169. BUILDIN_FUNC(getnpcid)
  6170. {
  6171. int num = script_getnum(st,2);
  6172. struct npc_data* nd = NULL;
  6173. if( script_hasdata(st,3) )
  6174. {// unique npc name
  6175. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6176. {
  6177. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6178. script_pushint(st,0);
  6179. return 1;
  6180. }
  6181. }
  6182. switch (num) {
  6183. case 0:
  6184. script_pushint(st,nd ? nd->bl.id : st->oid);
  6185. break;
  6186. default:
  6187. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6188. script_pushint(st,0);
  6189. return 1;
  6190. }
  6191. return 0;
  6192. }
  6193. /*==========================================
  6194. *指定IDのPT名取得
  6195. *------------------------------------------*/
  6196. BUILDIN_FUNC(getpartyname)
  6197. {
  6198. int party_id;
  6199. struct party_data* p;
  6200. party_id = script_getnum(st,2);
  6201. if( ( p = party_search(party_id) ) != NULL )
  6202. {
  6203. script_pushstrcopy(st,p->party.name);
  6204. }
  6205. else
  6206. {
  6207. script_pushconststr(st,"null");
  6208. }
  6209. return 0;
  6210. }
  6211. /*==========================================
  6212. *指定IDのPT人数とメンバーID取得
  6213. *------------------------------------------*/
  6214. BUILDIN_FUNC(getpartymember)
  6215. {
  6216. struct party_data *p;
  6217. int i,j=0,type=0;
  6218. p=party_search(script_getnum(st,2));
  6219. if( script_hasdata(st,3) )
  6220. type=script_getnum(st,3);
  6221. if(p!=NULL){
  6222. for(i=0;i<MAX_PARTY;i++){
  6223. if(p->party.member[i].account_id){
  6224. switch (type) {
  6225. case 2:
  6226. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6227. break;
  6228. case 1:
  6229. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6230. break;
  6231. default:
  6232. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6233. }
  6234. j++;
  6235. }
  6236. }
  6237. }
  6238. mapreg_setreg(add_str("$@partymembercount"),j);
  6239. return 0;
  6240. }
  6241. /*==========================================
  6242. * Retrieves party leader. if flag is specified,
  6243. * return some of the leader data. Otherwise, return name.
  6244. *------------------------------------------*/
  6245. BUILDIN_FUNC(getpartyleader)
  6246. {
  6247. int party_id, type = 0, i=0;
  6248. struct party_data *p;
  6249. party_id=script_getnum(st,2);
  6250. if( script_hasdata(st,3) )
  6251. type=script_getnum(st,3);
  6252. p=party_search(party_id);
  6253. if (p) //Search leader
  6254. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6255. if (!p || i == MAX_PARTY) { //leader not found
  6256. if (type)
  6257. script_pushint(st,-1);
  6258. else
  6259. script_pushconststr(st,"null");
  6260. return 0;
  6261. }
  6262. switch (type) {
  6263. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6264. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6265. case 3: script_pushint(st,p->party.member[i].class_); break;
  6266. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6267. case 5: script_pushint(st,p->party.member[i].lv); break;
  6268. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6269. }
  6270. return 0;
  6271. }
  6272. /*==========================================
  6273. *指定IDのギルド名取得
  6274. *------------------------------------------*/
  6275. BUILDIN_FUNC(getguildname)
  6276. {
  6277. int guild_id;
  6278. struct guild* g;
  6279. guild_id = script_getnum(st,2);
  6280. if( ( g = guild_search(guild_id) ) != NULL )
  6281. {
  6282. script_pushstrcopy(st,g->name);
  6283. }
  6284. else
  6285. {
  6286. script_pushconststr(st,"null");
  6287. }
  6288. return 0;
  6289. }
  6290. /*==========================================
  6291. *指定IDのGuildMaster名取得
  6292. *------------------------------------------*/
  6293. BUILDIN_FUNC(getguildmaster)
  6294. {
  6295. int guild_id;
  6296. struct guild* g;
  6297. guild_id = script_getnum(st,2);
  6298. if( ( g = guild_search(guild_id) ) != NULL )
  6299. {
  6300. script_pushstrcopy(st,g->member[0].name);
  6301. }
  6302. else
  6303. {
  6304. script_pushconststr(st,"null");
  6305. }
  6306. return 0;
  6307. }
  6308. BUILDIN_FUNC(getguildmasterid)
  6309. {
  6310. int guild_id;
  6311. struct guild* g;
  6312. guild_id = script_getnum(st,2);
  6313. if( ( g = guild_search(guild_id) ) != NULL )
  6314. {
  6315. script_pushint(st,g->member[0].char_id);
  6316. }
  6317. else
  6318. {
  6319. script_pushint(st,0);
  6320. }
  6321. return 0;
  6322. }
  6323. /*==========================================
  6324. * キャラクタの名前
  6325. *------------------------------------------*/
  6326. BUILDIN_FUNC(strcharinfo)
  6327. {
  6328. TBL_PC *sd;
  6329. int num;
  6330. struct guild* g;
  6331. struct party_data* p;
  6332. sd=script_rid2sd(st);
  6333. if (!sd) { //Avoid crashing....
  6334. script_pushconststr(st,"");
  6335. return 0;
  6336. }
  6337. num=script_getnum(st,2);
  6338. switch(num){
  6339. case 0:
  6340. script_pushstrcopy(st,sd->status.name);
  6341. break;
  6342. case 1:
  6343. if( ( p = party_search(sd->status.party_id) ) != NULL )
  6344. {
  6345. script_pushstrcopy(st,p->party.name);
  6346. }
  6347. else
  6348. {
  6349. script_pushconststr(st,"");
  6350. }
  6351. break;
  6352. case 2:
  6353. if( ( g = guild_search(sd->status.guild_id) ) != NULL )
  6354. {
  6355. script_pushstrcopy(st,g->name);
  6356. }
  6357. else
  6358. {
  6359. script_pushconststr(st,"");
  6360. }
  6361. break;
  6362. case 3:
  6363. script_pushconststr(st,map[sd->bl.m].name);
  6364. break;
  6365. default:
  6366. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6367. script_pushconststr(st,"");
  6368. break;
  6369. }
  6370. return 0;
  6371. }
  6372. /*==========================================
  6373. * 呼び出し元のNPC情報を取得する
  6374. *------------------------------------------*/
  6375. BUILDIN_FUNC(strnpcinfo)
  6376. {
  6377. TBL_NPC* nd;
  6378. int num;
  6379. char *buf,*name=NULL;
  6380. nd = map_id2nd(st->oid);
  6381. if (!nd) {
  6382. script_pushconststr(st, "");
  6383. return 0;
  6384. }
  6385. num = script_getnum(st,2);
  6386. switch(num){
  6387. case 0: // display name
  6388. name = aStrdup(nd->name);
  6389. break;
  6390. case 1: // visible part of display name
  6391. if((buf = strchr(nd->name,'#')) != NULL)
  6392. {
  6393. name = aStrdup(nd->name);
  6394. name[buf - nd->name] = 0;
  6395. } else // Return the name, there is no '#' present
  6396. name = aStrdup(nd->name);
  6397. break;
  6398. case 2: // # fragment
  6399. if((buf = strchr(nd->name,'#')) != NULL)
  6400. name = aStrdup(buf+1);
  6401. break;
  6402. case 3: // unique name
  6403. name = aStrdup(nd->exname);
  6404. break;
  6405. case 4: // map name
  6406. name = aStrdup(map[nd->bl.m].name);
  6407. break;
  6408. }
  6409. if(name)
  6410. script_pushstr(st, name);
  6411. else
  6412. script_pushconststr(st, "");
  6413. return 0;
  6414. }
  6415. // aegis->athena slot position conversion table
  6416. 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};
  6417. /*==========================================
  6418. * GetEquipID(Pos); Pos: 1-10
  6419. *------------------------------------------*/
  6420. BUILDIN_FUNC(getequipid)
  6421. {
  6422. int i, num;
  6423. TBL_PC* sd;
  6424. struct item_data* item;
  6425. sd = script_rid2sd(st);
  6426. if( sd == NULL )
  6427. return 0;
  6428. num = script_getnum(st,2) - 1;
  6429. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6430. {
  6431. script_pushint(st,-1);
  6432. return 0;
  6433. }
  6434. // get inventory position of item
  6435. i = pc_checkequip(sd,equip[num]);
  6436. if( i < 0 )
  6437. {
  6438. script_pushint(st,-1);
  6439. return 0;
  6440. }
  6441. item = sd->inventory_data[i];
  6442. if( item != 0 )
  6443. script_pushint(st,item->nameid);
  6444. else
  6445. script_pushint(st,0);
  6446. return 0;
  6447. }
  6448. /*==========================================
  6449. * 装備名文字列(精錬メニュー用)
  6450. *------------------------------------------*/
  6451. BUILDIN_FUNC(getequipname)
  6452. {
  6453. int i, num;
  6454. TBL_PC* sd;
  6455. struct item_data* item;
  6456. sd = script_rid2sd(st);
  6457. if( sd == NULL )
  6458. return 0;
  6459. num = script_getnum(st,2) - 1;
  6460. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6461. {
  6462. script_pushconststr(st,"");
  6463. return 0;
  6464. }
  6465. // get inventory position of item
  6466. i = pc_checkequip(sd,equip[num]);
  6467. if( i < 0 )
  6468. {
  6469. script_pushint(st,-1);
  6470. return 0;
  6471. }
  6472. item = sd->inventory_data[i];
  6473. if( item != 0 )
  6474. script_pushstrcopy(st,item->jname);
  6475. else
  6476. script_pushconststr(st,"");
  6477. return 0;
  6478. }
  6479. /*==========================================
  6480. * getbrokenid [Valaris]
  6481. *------------------------------------------*/
  6482. BUILDIN_FUNC(getbrokenid)
  6483. {
  6484. int i,num,id=0,brokencounter=0;
  6485. TBL_PC *sd;
  6486. sd = script_rid2sd(st);
  6487. if( sd == NULL )
  6488. return 0;
  6489. num=script_getnum(st,2);
  6490. for(i=0; i<MAX_INVENTORY; i++) {
  6491. if(sd->status.inventory[i].attribute){
  6492. brokencounter++;
  6493. if(num==brokencounter){
  6494. id=sd->status.inventory[i].nameid;
  6495. break;
  6496. }
  6497. }
  6498. }
  6499. script_pushint(st,id);
  6500. return 0;
  6501. }
  6502. /*==========================================
  6503. * repair [Valaris]
  6504. *------------------------------------------*/
  6505. BUILDIN_FUNC(repair)
  6506. {
  6507. int i,num;
  6508. int repaircounter=0;
  6509. TBL_PC *sd;
  6510. sd = script_rid2sd(st);
  6511. if( sd == NULL )
  6512. return 0;
  6513. num=script_getnum(st,2);
  6514. for(i=0; i<MAX_INVENTORY; i++) {
  6515. if(sd->status.inventory[i].attribute){
  6516. repaircounter++;
  6517. if(num==repaircounter){
  6518. sd->status.inventory[i].attribute=0;
  6519. clif_equiplist(sd);
  6520. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6521. clif_misceffect(&sd->bl, 3);
  6522. break;
  6523. }
  6524. }
  6525. }
  6526. return 0;
  6527. }
  6528. /*==========================================
  6529. * repairall
  6530. *------------------------------------------*/
  6531. BUILDIN_FUNC(repairall)
  6532. {
  6533. int i, repaircounter = 0;
  6534. TBL_PC *sd;
  6535. sd = script_rid2sd(st);
  6536. if(sd == NULL)
  6537. return 0;
  6538. for(i = 0; i < MAX_INVENTORY; i++)
  6539. {
  6540. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  6541. {
  6542. sd->status.inventory[i].attribute = 0;
  6543. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  6544. repaircounter++;
  6545. }
  6546. }
  6547. if(repaircounter)
  6548. {
  6549. clif_misceffect(&sd->bl, 3);
  6550. clif_equiplist(sd);
  6551. }
  6552. return 0;
  6553. }
  6554. /*==========================================
  6555. * 装備チェック
  6556. *------------------------------------------*/
  6557. BUILDIN_FUNC(getequipisequiped)
  6558. {
  6559. int i = -1,num;
  6560. TBL_PC *sd;
  6561. num = script_getnum(st,2);
  6562. sd = script_rid2sd(st);
  6563. if( sd == NULL )
  6564. return 0;
  6565. if (num > 0 && num <= ARRAYLENGTH(equip))
  6566. i=pc_checkequip(sd,equip[num-1]);
  6567. if(i >= 0)
  6568. script_pushint(st,1);
  6569. else
  6570. script_pushint(st,0);
  6571. return 0;
  6572. }
  6573. /*==========================================
  6574. * 装備品精錬可能チェック
  6575. *------------------------------------------*/
  6576. BUILDIN_FUNC(getequipisenableref)
  6577. {
  6578. int i = -1,num;
  6579. TBL_PC *sd;
  6580. num = script_getnum(st,2);
  6581. sd = script_rid2sd(st);
  6582. if( sd == NULL )
  6583. return 0;
  6584. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6585. i = pc_checkequip(sd,equip[num-1]);
  6586. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6587. script_pushint(st,1);
  6588. else
  6589. script_pushint(st,0);
  6590. return 0;
  6591. }
  6592. /*==========================================
  6593. * 装備品鑑定チェック
  6594. *------------------------------------------*/
  6595. BUILDIN_FUNC(getequipisidentify)
  6596. {
  6597. int i = -1,num;
  6598. TBL_PC *sd;
  6599. num = script_getnum(st,2);
  6600. sd = script_rid2sd(st);
  6601. if( sd == NULL )
  6602. return 0;
  6603. if (num > 0 && num <= ARRAYLENGTH(equip))
  6604. i=pc_checkequip(sd,equip[num-1]);
  6605. if(i >= 0)
  6606. script_pushint(st,sd->status.inventory[i].identify);
  6607. else
  6608. script_pushint(st,0);
  6609. return 0;
  6610. }
  6611. /*==========================================
  6612. * 装備品精錬度
  6613. *------------------------------------------*/
  6614. BUILDIN_FUNC(getequiprefinerycnt)
  6615. {
  6616. int i = -1,num;
  6617. TBL_PC *sd;
  6618. num = script_getnum(st,2);
  6619. sd = script_rid2sd(st);
  6620. if( sd == NULL )
  6621. return 0;
  6622. if (num > 0 && num <= ARRAYLENGTH(equip))
  6623. i=pc_checkequip(sd,equip[num-1]);
  6624. if(i >= 0)
  6625. script_pushint(st,sd->status.inventory[i].refine);
  6626. else
  6627. script_pushint(st,0);
  6628. return 0;
  6629. }
  6630. /*==========================================
  6631. * 装備品武器LV
  6632. *------------------------------------------*/
  6633. BUILDIN_FUNC(getequipweaponlv)
  6634. {
  6635. int i = -1,num;
  6636. TBL_PC *sd;
  6637. num = script_getnum(st,2);
  6638. sd = script_rid2sd(st);
  6639. if( sd == NULL )
  6640. return 0;
  6641. if (num > 0 && num <= ARRAYLENGTH(equip))
  6642. i=pc_checkequip(sd,equip[num-1]);
  6643. if(i >= 0 && sd->inventory_data[i])
  6644. script_pushint(st,sd->inventory_data[i]->wlv);
  6645. else
  6646. script_pushint(st,0);
  6647. return 0;
  6648. }
  6649. /*==========================================
  6650. * 装備品精錬成功率
  6651. *------------------------------------------*/
  6652. BUILDIN_FUNC(getequippercentrefinery)
  6653. {
  6654. int i = -1,num;
  6655. TBL_PC *sd;
  6656. num = script_getnum(st,2);
  6657. sd = script_rid2sd(st);
  6658. if( sd == NULL )
  6659. return 0;
  6660. if (num > 0 && num <= ARRAYLENGTH(equip))
  6661. i=pc_checkequip(sd,equip[num-1]);
  6662. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6663. script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  6664. else
  6665. script_pushint(st,0);
  6666. return 0;
  6667. }
  6668. /*==========================================
  6669. * 精錬成功
  6670. *------------------------------------------*/
  6671. BUILDIN_FUNC(successrefitem)
  6672. {
  6673. int i=-1,num,ep;
  6674. TBL_PC *sd;
  6675. num = script_getnum(st,2);
  6676. sd = script_rid2sd(st);
  6677. if( sd == NULL )
  6678. return 0;
  6679. if (num > 0 && num <= ARRAYLENGTH(equip))
  6680. i=pc_checkequip(sd,equip[num-1]);
  6681. if(i >= 0) {
  6682. ep=sd->status.inventory[i].equip;
  6683. //Logs items, got from (N)PC scripts [Lupus]
  6684. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6685. sd->status.inventory[i].refine++;
  6686. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6687. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6688. clif_delitem(sd,i,1,3);
  6689. //Logs items, got from (N)PC scripts [Lupus]
  6690. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6691. clif_additem(sd,i,1,0);
  6692. pc_equipitem(sd,i,ep);
  6693. clif_misceffect(&sd->bl,3);
  6694. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6695. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6696. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6697. ){ // Fame point system [DracoRPG]
  6698. switch (sd->inventory_data[i]->wlv){
  6699. case 1:
  6700. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6701. break;
  6702. case 2:
  6703. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6704. break;
  6705. case 3:
  6706. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6707. break;
  6708. }
  6709. }
  6710. }
  6711. return 0;
  6712. }
  6713. /*==========================================
  6714. * 精錬失敗
  6715. *------------------------------------------*/
  6716. BUILDIN_FUNC(failedrefitem)
  6717. {
  6718. int i=-1,num;
  6719. TBL_PC *sd;
  6720. num = script_getnum(st,2);
  6721. sd = script_rid2sd(st);
  6722. if( sd == NULL )
  6723. return 0;
  6724. if (num > 0 && num <= ARRAYLENGTH(equip))
  6725. i=pc_checkequip(sd,equip[num-1]);
  6726. if(i >= 0) {
  6727. sd->status.inventory[i].refine = 0;
  6728. pc_unequipitem(sd,i,3);
  6729. // 精錬失敗エフェクトのパケット
  6730. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  6731. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6732. // 他の人にも失敗を通知
  6733. clif_misceffect(&sd->bl,2);
  6734. }
  6735. return 0;
  6736. }
  6737. /*==========================================
  6738. * Downgrades an Equipment Part by -1 . [Masao]
  6739. *------------------------------------------*/
  6740. BUILDIN_FUNC(downrefitem)
  6741. {
  6742. int i = -1,num,ep;
  6743. TBL_PC *sd;
  6744. num = script_getnum(st,2);
  6745. sd = script_rid2sd(st);
  6746. if( sd == NULL )
  6747. return 0;
  6748. if (num > 0 && num <= ARRAYLENGTH(equip))
  6749. i = pc_checkequip(sd,equip[num-1]);
  6750. if(i >= 0) {
  6751. ep = sd->status.inventory[i].equip;
  6752. //Logs items, got from (N)PC scripts [Lupus]
  6753. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6754. sd->status.inventory[i].refine++;
  6755. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6756. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine = sd->status.inventory[i].refine - 2);
  6757. clif_delitem(sd,i,1,3);
  6758. //Logs items, got from (N)PC scripts [Lupus]
  6759. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6760. clif_additem(sd,i,1,0);
  6761. pc_equipitem(sd,i,ep);
  6762. clif_misceffect(&sd->bl,2);
  6763. }
  6764. return 0;
  6765. }
  6766. /*==========================================
  6767. *
  6768. *------------------------------------------*/
  6769. BUILDIN_FUNC(statusup)
  6770. {
  6771. int type;
  6772. TBL_PC *sd;
  6773. type=script_getnum(st,2);
  6774. sd = script_rid2sd(st);
  6775. if( sd == NULL )
  6776. return 0;
  6777. pc_statusup(sd,type);
  6778. return 0;
  6779. }
  6780. /*==========================================
  6781. *
  6782. *------------------------------------------*/
  6783. BUILDIN_FUNC(statusup2)
  6784. {
  6785. int type,val;
  6786. TBL_PC *sd;
  6787. type=script_getnum(st,2);
  6788. val=script_getnum(st,3);
  6789. sd = script_rid2sd(st);
  6790. if( sd == NULL )
  6791. return 0;
  6792. pc_statusup2(sd,type,val);
  6793. return 0;
  6794. }
  6795. /// See 'doc/item_bonus.txt'
  6796. ///
  6797. /// bonus <bonus type>,<val1>;
  6798. /// bonus2 <bonus type>,<val1>,<val2>;
  6799. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6800. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6801. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6802. BUILDIN_FUNC(bonus)
  6803. {
  6804. int type;
  6805. int val1;
  6806. int val2 = 0;
  6807. int val3 = 0;
  6808. int val4 = 0;
  6809. int val5 = 0;
  6810. TBL_PC* sd;
  6811. sd = script_rid2sd(st);
  6812. if( sd == NULL )
  6813. return 0; // no player attached
  6814. type = script_getnum(st,2);
  6815. switch( type ) {
  6816. case SP_AUTOSPELL:
  6817. case SP_AUTOSPELL_WHENHIT:
  6818. case SP_AUTOSPELL_ONSKILL:
  6819. case SP_SKILL_ATK:
  6820. case SP_SKILL_HEAL:
  6821. case SP_SKILL_HEAL2:
  6822. case SP_ADD_SKILL_BLOW:
  6823. case SP_CASTRATE:
  6824. case SP_ADDEFF_ONSKILL:
  6825. case SP_SKILL_USE_SP_RATE:
  6826. case SP_SKILL_COOLDOWN:
  6827. case SP_SKILL_FIXEDCAST:
  6828. case SP_SKILL_VARIABLECAST:
  6829. case SP_VARCASTRATE:
  6830. case SP_SKILL_USE_SP:
  6831. // these bonuses support skill names
  6832. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6833. break;
  6834. default:
  6835. val1 = script_getnum(st,3);
  6836. break;
  6837. }
  6838. switch( script_lastdata(st)-2 ) {
  6839. case 1:
  6840. pc_bonus(sd, type, val1);
  6841. break;
  6842. case 2:
  6843. val2 = script_getnum(st,4);
  6844. pc_bonus2(sd, type, val1, val2);
  6845. break;
  6846. case 3:
  6847. val2 = script_getnum(st,4);
  6848. val3 = script_getnum(st,5);
  6849. pc_bonus3(sd, type, val1, val2, val3);
  6850. break;
  6851. case 4:
  6852. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6853. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6854. else
  6855. val2 = script_getnum(st,4);
  6856. val3 = script_getnum(st,5);
  6857. val4 = script_getnum(st,6);
  6858. pc_bonus4(sd, type, val1, val2, val3, val4);
  6859. break;
  6860. case 5:
  6861. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6862. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6863. else
  6864. val2 = script_getnum(st,4);
  6865. val3 = script_getnum(st,5);
  6866. val4 = script_getnum(st,6);
  6867. val5 = script_getnum(st,7);
  6868. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  6869. break;
  6870. default:
  6871. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  6872. break;
  6873. }
  6874. return 0;
  6875. }
  6876. BUILDIN_FUNC(autobonus)
  6877. {
  6878. unsigned int dur;
  6879. short rate;
  6880. short atk_type = 0;
  6881. TBL_PC* sd;
  6882. const char *bonus_script, *other_script = NULL;
  6883. sd = script_rid2sd(st);
  6884. if( sd == NULL )
  6885. return 0; // no player attached
  6886. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6887. return 0;
  6888. rate = script_getnum(st,3);
  6889. dur = script_getnum(st,4);
  6890. bonus_script = script_getstr(st,2);
  6891. if( !rate || !dur || !bonus_script )
  6892. return 0;
  6893. if( script_hasdata(st,5) )
  6894. atk_type = script_getnum(st,5);
  6895. if( script_hasdata(st,6) )
  6896. other_script = script_getstr(st,6);
  6897. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  6898. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6899. {
  6900. script_add_autobonus(bonus_script);
  6901. if( other_script )
  6902. script_add_autobonus(other_script);
  6903. }
  6904. return 0;
  6905. }
  6906. BUILDIN_FUNC(autobonus2)
  6907. {
  6908. unsigned int dur;
  6909. short rate;
  6910. short atk_type = 0;
  6911. TBL_PC* sd;
  6912. const char *bonus_script, *other_script = NULL;
  6913. sd = script_rid2sd(st);
  6914. if( sd == NULL )
  6915. return 0; // no player attached
  6916. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6917. return 0;
  6918. rate = script_getnum(st,3);
  6919. dur = script_getnum(st,4);
  6920. bonus_script = script_getstr(st,2);
  6921. if( !rate || !dur || !bonus_script )
  6922. return 0;
  6923. if( script_hasdata(st,5) )
  6924. atk_type = script_getnum(st,5);
  6925. if( script_hasdata(st,6) )
  6926. other_script = script_getstr(st,6);
  6927. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  6928. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6929. {
  6930. script_add_autobonus(bonus_script);
  6931. if( other_script )
  6932. script_add_autobonus(other_script);
  6933. }
  6934. return 0;
  6935. }
  6936. BUILDIN_FUNC(autobonus3)
  6937. {
  6938. unsigned int dur;
  6939. short rate,atk_type;
  6940. TBL_PC* sd;
  6941. const char *bonus_script, *other_script = NULL;
  6942. sd = script_rid2sd(st);
  6943. if( sd == NULL )
  6944. return 0; // no player attached
  6945. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6946. return 0;
  6947. rate = script_getnum(st,3);
  6948. dur = script_getnum(st,4);
  6949. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6950. bonus_script = script_getstr(st,2);
  6951. if( !rate || !dur || !atk_type || !bonus_script )
  6952. return 0;
  6953. if( script_hasdata(st,6) )
  6954. other_script = script_getstr(st,6);
  6955. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  6956. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6957. {
  6958. script_add_autobonus(bonus_script);
  6959. if( other_script )
  6960. script_add_autobonus(other_script);
  6961. }
  6962. return 0;
  6963. }
  6964. /// Changes the level of a player skill.
  6965. /// <flag> defaults to 1
  6966. /// <flag>=0 : set the level of the skill
  6967. /// <flag>=1 : set the temporary level of the skill
  6968. /// <flag>=2 : add to the level of the skill
  6969. ///
  6970. /// skill <skill id>,<level>,<flag>
  6971. /// skill <skill id>,<level>
  6972. /// skill "<skill name>",<level>,<flag>
  6973. /// skill "<skill name>",<level>
  6974. BUILDIN_FUNC(skill)
  6975. {
  6976. int id;
  6977. int level;
  6978. int flag = 1;
  6979. TBL_PC* sd;
  6980. sd = script_rid2sd(st);
  6981. if( sd == NULL )
  6982. return 0;// no player attached, report source
  6983. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6984. level = script_getnum(st,3);
  6985. if( script_hasdata(st,4) )
  6986. flag = script_getnum(st,4);
  6987. pc_skill(sd, id, level, flag);
  6988. return 0;
  6989. }
  6990. /// Changes the level of a player skill.
  6991. /// like skill, but <flag> defaults to 2
  6992. ///
  6993. /// addtoskill <skill id>,<amount>,<flag>
  6994. /// addtoskill <skill id>,<amount>
  6995. /// addtoskill "<skill name>",<amount>,<flag>
  6996. /// addtoskill "<skill name>",<amount>
  6997. ///
  6998. /// @see skill
  6999. BUILDIN_FUNC(addtoskill)
  7000. {
  7001. int id;
  7002. int level;
  7003. int flag = 2;
  7004. TBL_PC* sd;
  7005. sd = script_rid2sd(st);
  7006. if( sd == NULL )
  7007. return 0;// no player attached, report source
  7008. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7009. level = script_getnum(st,3);
  7010. if( script_hasdata(st,4) )
  7011. flag = script_getnum(st,4);
  7012. pc_skill(sd, id, level, flag);
  7013. return 0;
  7014. }
  7015. /// Increases the level of a guild skill.
  7016. ///
  7017. /// guildskill <skill id>,<amount>;
  7018. /// guildskill "<skill name>",<amount>;
  7019. BUILDIN_FUNC(guildskill)
  7020. {
  7021. int id;
  7022. int level;
  7023. TBL_PC* sd;
  7024. int i;
  7025. sd = script_rid2sd(st);
  7026. if( sd == NULL )
  7027. return 0;// no player attached, report source
  7028. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7029. level = script_getnum(st,3);
  7030. for( i=0; i < level; i++ )
  7031. guild_skillup(sd, id);
  7032. return 0;
  7033. }
  7034. /// Returns the level of the player skill.
  7035. ///
  7036. /// getskilllv(<skill id>) -> <level>
  7037. /// getskilllv("<skill name>") -> <level>
  7038. BUILDIN_FUNC(getskilllv)
  7039. {
  7040. int id;
  7041. TBL_PC* sd;
  7042. sd = script_rid2sd(st);
  7043. if( sd == NULL )
  7044. return 0;// no player attached, report source
  7045. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7046. script_pushint(st, pc_checkskill(sd,id));
  7047. return 0;
  7048. }
  7049. /// Returns the level of the guild skill.
  7050. ///
  7051. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7052. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7053. BUILDIN_FUNC(getgdskilllv)
  7054. {
  7055. int guild_id;
  7056. int skill_id;
  7057. struct guild* g;
  7058. guild_id = script_getnum(st,2);
  7059. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7060. g = guild_search(guild_id);
  7061. if( g == NULL )
  7062. script_pushint(st, -1);
  7063. else
  7064. script_pushint(st, guild_checkskill(g,skill_id));
  7065. return 0;
  7066. }
  7067. /// Returns the 'basic_skill_check' setting.
  7068. /// This config determines if the server checks the skill level of NV_BASIC
  7069. /// before allowing the basic actions.
  7070. ///
  7071. /// basicskillcheck() -> <bool>
  7072. BUILDIN_FUNC(basicskillcheck)
  7073. {
  7074. script_pushint(st, battle_config.basic_skill_check);
  7075. return 0;
  7076. }
  7077. /// Returns the GM level of the player.
  7078. ///
  7079. /// getgmlevel() -> <level>
  7080. BUILDIN_FUNC(getgmlevel)
  7081. {
  7082. TBL_PC* sd;
  7083. sd = script_rid2sd(st);
  7084. if( sd == NULL )
  7085. return 0;// no player attached, report source
  7086. script_pushint(st, pc_get_group_level(sd));
  7087. return 0;
  7088. }
  7089. /// Returns the group ID of the player.
  7090. ///
  7091. /// getgroupid() -> <int>
  7092. BUILDIN_FUNC(getgroupid)
  7093. {
  7094. TBL_PC* sd;
  7095. sd = script_rid2sd(st);
  7096. if (sd == NULL)
  7097. return 1; // no player attached, report source
  7098. script_pushint(st, pc_get_group_id(sd));
  7099. return 0;
  7100. }
  7101. /// Terminates the execution of this script instance.
  7102. ///
  7103. /// end
  7104. BUILDIN_FUNC(end)
  7105. {
  7106. st->state = END;
  7107. return 0;
  7108. }
  7109. /// Checks if the player has that effect state (option).
  7110. ///
  7111. /// checkoption(<option>) -> <bool>
  7112. BUILDIN_FUNC(checkoption)
  7113. {
  7114. int option;
  7115. TBL_PC* sd;
  7116. sd = script_rid2sd(st);
  7117. if( sd == NULL )
  7118. return 0;// no player attached, report source
  7119. option = script_getnum(st,2);
  7120. if( sd->sc.option&option )
  7121. script_pushint(st, 1);
  7122. else
  7123. script_pushint(st, 0);
  7124. return 0;
  7125. }
  7126. /// Checks if the player is in that body state (opt1).
  7127. ///
  7128. /// checkoption1(<opt1>) -> <bool>
  7129. BUILDIN_FUNC(checkoption1)
  7130. {
  7131. int opt1;
  7132. TBL_PC* sd;
  7133. sd = script_rid2sd(st);
  7134. if( sd == NULL )
  7135. return 0;// no player attached, report source
  7136. opt1 = script_getnum(st,2);
  7137. if( sd->sc.opt1 == opt1 )
  7138. script_pushint(st, 1);
  7139. else
  7140. script_pushint(st, 0);
  7141. return 0;
  7142. }
  7143. /// Checks if the player has that health state (opt2).
  7144. ///
  7145. /// checkoption2(<opt2>) -> <bool>
  7146. BUILDIN_FUNC(checkoption2)
  7147. {
  7148. int opt2;
  7149. TBL_PC* sd;
  7150. sd = script_rid2sd(st);
  7151. if( sd == NULL )
  7152. return 0;// no player attached, report source
  7153. opt2 = script_getnum(st,2);
  7154. if( sd->sc.opt2&opt2 )
  7155. script_pushint(st, 1);
  7156. else
  7157. script_pushint(st, 0);
  7158. return 0;
  7159. }
  7160. /// Changes the effect state (option) of the player.
  7161. /// <flag> defaults to 1
  7162. /// <flag>=0 : removes the option
  7163. /// <flag>=other : adds the option
  7164. ///
  7165. /// setoption <option>,<flag>;
  7166. /// setoption <option>;
  7167. BUILDIN_FUNC(setoption)
  7168. {
  7169. int option;
  7170. int flag = 1;
  7171. TBL_PC* sd;
  7172. sd = script_rid2sd(st);
  7173. if( sd == NULL )
  7174. return 0;// no player attached, report source
  7175. option = script_getnum(st,2);
  7176. if( script_hasdata(st,3) )
  7177. flag = script_getnum(st,3);
  7178. else if( !option ){// Request to remove everything.
  7179. flag = 0;
  7180. option = OPTION_FALCON|OPTION_RIDING;
  7181. #ifndef NEW_CARTS
  7182. option |= OPTION_CART;
  7183. #endif
  7184. }
  7185. if( flag ){// Add option
  7186. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  7187. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  7188. pc_setoption(sd, sd->sc.option|option);
  7189. } else// Remove option
  7190. pc_setoption(sd, sd->sc.option&~option);
  7191. return 0;
  7192. }
  7193. /// Returns if the player has a cart.
  7194. ///
  7195. /// checkcart() -> <bool>
  7196. ///
  7197. /// @author Valaris
  7198. BUILDIN_FUNC(checkcart)
  7199. {
  7200. TBL_PC* sd;
  7201. sd = script_rid2sd(st);
  7202. if( sd == NULL )
  7203. return 0;// no player attached, report source
  7204. if( pc_iscarton(sd) )
  7205. script_pushint(st, 1);
  7206. else
  7207. script_pushint(st, 0);
  7208. return 0;
  7209. }
  7210. /// Sets the cart of the player.
  7211. /// <type> defaults to 1
  7212. /// <type>=0 : removes the cart
  7213. /// <type>=1 : Normal cart
  7214. /// <type>=2 : Wooden cart
  7215. /// <type>=3 : Covered cart with flowers and ferns
  7216. /// <type>=4 : Wooden cart with a Panda doll on the back
  7217. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  7218. ///
  7219. /// setcart <type>;
  7220. /// setcart;
  7221. BUILDIN_FUNC(setcart)
  7222. {
  7223. int type = 1;
  7224. TBL_PC* sd;
  7225. sd = script_rid2sd(st);
  7226. if( sd == NULL )
  7227. return 0;// no player attached, report source
  7228. if( script_hasdata(st,2) )
  7229. type = script_getnum(st,2);
  7230. pc_setcart(sd, type);
  7231. return 0;
  7232. }
  7233. /// Returns if the player has a falcon.
  7234. ///
  7235. /// checkfalcon() -> <bool>
  7236. ///
  7237. /// @author Valaris
  7238. BUILDIN_FUNC(checkfalcon)
  7239. {
  7240. TBL_PC* sd;
  7241. sd = script_rid2sd(st);
  7242. if( sd == NULL )
  7243. return 0;// no player attached, report source
  7244. if( pc_isfalcon(sd) )
  7245. script_pushint(st, 1);
  7246. else
  7247. script_pushint(st, 0);
  7248. return 0;
  7249. }
  7250. /// Sets if the player has a falcon or not.
  7251. /// <flag> defaults to 1
  7252. ///
  7253. /// setfalcon <flag>;
  7254. /// setfalcon;
  7255. BUILDIN_FUNC(setfalcon)
  7256. {
  7257. int flag = 1;
  7258. TBL_PC* sd;
  7259. sd = script_rid2sd(st);
  7260. if( sd == NULL )
  7261. return 0;// no player attached, report source
  7262. if( script_hasdata(st,2) )
  7263. flag = script_getnum(st,2);
  7264. pc_setfalcon(sd, flag);
  7265. return 0;
  7266. }
  7267. /// Returns if the player is riding.
  7268. ///
  7269. /// checkriding() -> <bool>
  7270. ///
  7271. /// @author Valaris
  7272. BUILDIN_FUNC(checkriding)
  7273. {
  7274. TBL_PC* sd;
  7275. sd = script_rid2sd(st);
  7276. if( sd == NULL )
  7277. return 0;// no player attached, report source
  7278. if( pc_isriding(sd) || pc_isridingwug(sd) || pc_isridingdragon(sd) )
  7279. script_pushint(st, 1);
  7280. else
  7281. script_pushint(st, 0);
  7282. return 0;
  7283. }
  7284. /// Sets if the player is riding.
  7285. /// <flag> defaults to 1
  7286. ///
  7287. /// setriding <flag>;
  7288. /// setriding;
  7289. BUILDIN_FUNC(setriding)
  7290. {
  7291. int flag = 1;
  7292. TBL_PC* sd;
  7293. sd = script_rid2sd(st);
  7294. if( sd == NULL )
  7295. return 0;// no player attached, report source
  7296. if( script_hasdata(st,2) )
  7297. flag = script_getnum(st,2);
  7298. pc_setriding(sd, flag);
  7299. return 0;
  7300. }
  7301. /// Returns if the player has a warg.
  7302. ///
  7303. /// checkwug() -> <bool>
  7304. ///
  7305. BUILDIN_FUNC(checkwug)
  7306. {
  7307. TBL_PC* sd;
  7308. sd = script_rid2sd(st);
  7309. if( sd == NULL )
  7310. return 0;// no player attached, report source
  7311. if( pc_iswug(sd) )
  7312. script_pushint(st, 1);
  7313. else
  7314. script_pushint(st, 0);
  7315. return 0;
  7316. }
  7317. /// Returns if the player is wearing MADO Gear.
  7318. ///
  7319. /// checkmadogear() -> <bool>
  7320. ///
  7321. BUILDIN_FUNC(checkmadogear)
  7322. {
  7323. TBL_PC* sd;
  7324. sd = script_rid2sd(st);
  7325. if( sd == NULL )
  7326. return 0;// no player attached, report source
  7327. if( pc_ismadogear(sd) )
  7328. script_pushint(st, 1);
  7329. else
  7330. script_pushint(st, 0);
  7331. return 0;
  7332. }
  7333. /// Sets if the player is riding MADO Gear.
  7334. /// <flag> defaults to 1
  7335. ///
  7336. /// setmadogear <flag>;
  7337. /// setmadogear;
  7338. BUILDIN_FUNC(setmadogear)
  7339. {
  7340. int flag = 1;
  7341. TBL_PC* sd;
  7342. sd = script_rid2sd(st);
  7343. if( sd == NULL )
  7344. return 0;// no player attached, report source
  7345. if( script_hasdata(st,2) )
  7346. flag = script_getnum(st,2);
  7347. pc_setmadogear(sd, flag);
  7348. return 0;
  7349. }
  7350. /// Sets the save point of the player.
  7351. ///
  7352. /// save "<map name>",<x>,<y>
  7353. /// savepoint "<map name>",<x>,<y>
  7354. BUILDIN_FUNC(savepoint)
  7355. {
  7356. int x;
  7357. int y;
  7358. short map;
  7359. const char* str;
  7360. TBL_PC* sd;
  7361. sd = script_rid2sd(st);
  7362. if( sd == NULL )
  7363. return 0;// no player attached, report source
  7364. str = script_getstr(st, 2);
  7365. x = script_getnum(st,3);
  7366. y = script_getnum(st,4);
  7367. map = mapindex_name2id(str);
  7368. if( map )
  7369. pc_setsavepoint(sd, map, x, y);
  7370. return 0;
  7371. }
  7372. /*==========================================
  7373. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  7374. *------------------------------------------*/
  7375. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  7376. {
  7377. int type;
  7378. time_t timer;
  7379. struct tm *t;
  7380. type=script_getnum(st,2);
  7381. switch(type){
  7382. case 2:
  7383. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  7384. // from the system clock.)
  7385. script_pushint(st,(int)time(NULL));
  7386. break;
  7387. case 1:
  7388. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  7389. time(&timer);
  7390. t=localtime(&timer);
  7391. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  7392. break;
  7393. case 0:
  7394. default:
  7395. //type 0:(System Ticks)
  7396. script_pushint(st,gettick());
  7397. break;
  7398. }
  7399. return 0;
  7400. }
  7401. /*==========================================
  7402. * GetTime(Type);
  7403. * 1: Sec 2: Min 3: Hour
  7404. * 4: WeekDay 5: MonthDay 6: Month
  7405. * 7: Year
  7406. *------------------------------------------*/
  7407. BUILDIN_FUNC(gettime) /* Asgard Version */
  7408. {
  7409. int type;
  7410. time_t timer;
  7411. struct tm *t;
  7412. type=script_getnum(st,2);
  7413. time(&timer);
  7414. t=localtime(&timer);
  7415. switch(type){
  7416. case 1://Sec(0~59)
  7417. script_pushint(st,t->tm_sec);
  7418. break;
  7419. case 2://Min(0~59)
  7420. script_pushint(st,t->tm_min);
  7421. break;
  7422. case 3://Hour(0~23)
  7423. script_pushint(st,t->tm_hour);
  7424. break;
  7425. case 4://WeekDay(0~6)
  7426. script_pushint(st,t->tm_wday);
  7427. break;
  7428. case 5://MonthDay(01~31)
  7429. script_pushint(st,t->tm_mday);
  7430. break;
  7431. case 6://Month(01~12)
  7432. script_pushint(st,t->tm_mon+1);
  7433. break;
  7434. case 7://Year(20xx)
  7435. script_pushint(st,t->tm_year+1900);
  7436. break;
  7437. case 8://Year Day(01~366)
  7438. script_pushint(st,t->tm_yday+1);
  7439. break;
  7440. default://(format error)
  7441. script_pushint(st,-1);
  7442. break;
  7443. }
  7444. return 0;
  7445. }
  7446. /*==========================================
  7447. * GetTimeStr("TimeFMT", Length);
  7448. *------------------------------------------*/
  7449. BUILDIN_FUNC(gettimestr)
  7450. {
  7451. char *tmpstr;
  7452. const char *fmtstr;
  7453. int maxlen;
  7454. time_t now = time(NULL);
  7455. fmtstr=script_getstr(st,2);
  7456. maxlen=script_getnum(st,3);
  7457. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  7458. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  7459. tmpstr[maxlen]='\0';
  7460. script_pushstr(st,tmpstr);
  7461. return 0;
  7462. }
  7463. /*==========================================
  7464. * カプラ倉庫を開く
  7465. *------------------------------------------*/
  7466. BUILDIN_FUNC(openstorage)
  7467. {
  7468. TBL_PC* sd;
  7469. sd = script_rid2sd(st);
  7470. if( sd == NULL )
  7471. return 0;
  7472. storage_storageopen(sd);
  7473. return 0;
  7474. }
  7475. BUILDIN_FUNC(guildopenstorage)
  7476. {
  7477. TBL_PC* sd;
  7478. int ret;
  7479. sd = script_rid2sd(st);
  7480. if( sd == NULL )
  7481. return 0;
  7482. ret = storage_guild_storageopen(sd);
  7483. script_pushint(st,ret);
  7484. return 0;
  7485. }
  7486. /*==========================================
  7487. * アイテムによるスキル発動
  7488. *------------------------------------------*/
  7489. /// itemskill <skill id>,<level>
  7490. /// itemskill "<skill name>",<level>
  7491. BUILDIN_FUNC(itemskill)
  7492. {
  7493. int id;
  7494. int lv;
  7495. TBL_PC* sd;
  7496. sd = script_rid2sd(st);
  7497. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  7498. return 0;
  7499. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7500. lv = script_getnum(st,3);
  7501. sd->skillitem=id;
  7502. sd->skillitemlv=lv;
  7503. clif_item_skill(sd,id,lv);
  7504. return 0;
  7505. }
  7506. /*==========================================
  7507. * アイテム作成
  7508. *------------------------------------------*/
  7509. BUILDIN_FUNC(produce)
  7510. {
  7511. int trigger;
  7512. TBL_PC* sd;
  7513. sd = script_rid2sd(st);
  7514. if( sd == NULL )
  7515. return 0;
  7516. trigger=script_getnum(st,2);
  7517. clif_skill_produce_mix_list(sd, -1, trigger);
  7518. return 0;
  7519. }
  7520. /*==========================================
  7521. *
  7522. *------------------------------------------*/
  7523. BUILDIN_FUNC(cooking)
  7524. {
  7525. int trigger;
  7526. TBL_PC* sd;
  7527. sd = script_rid2sd(st);
  7528. if( sd == NULL )
  7529. return 0;
  7530. trigger=script_getnum(st,2);
  7531. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  7532. return 0;
  7533. }
  7534. /*==========================================
  7535. * NPCでペット作る
  7536. *------------------------------------------*/
  7537. BUILDIN_FUNC(makepet)
  7538. {
  7539. TBL_PC* sd;
  7540. int id,pet_id;
  7541. id=script_getnum(st,2);
  7542. sd = script_rid2sd(st);
  7543. if( sd == NULL )
  7544. return 0;
  7545. pet_id = search_petDB_index(id, PET_CLASS);
  7546. if (pet_id < 0)
  7547. pet_id = search_petDB_index(id, PET_EGG);
  7548. if (pet_id >= 0 && sd) {
  7549. sd->catch_target_class = pet_db[pet_id].class_;
  7550. intif_create_pet(
  7551. sd->status.account_id, sd->status.char_id,
  7552. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  7553. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  7554. 100, 0, 1, pet_db[pet_id].jname);
  7555. }
  7556. return 0;
  7557. }
  7558. /*==========================================
  7559. * NPCで経験値上げる
  7560. *------------------------------------------*/
  7561. BUILDIN_FUNC(getexp)
  7562. {
  7563. TBL_PC* sd;
  7564. int base=0,job=0;
  7565. double bonus;
  7566. sd = script_rid2sd(st);
  7567. if( sd == NULL )
  7568. return 0;
  7569. base=script_getnum(st,2);
  7570. job =script_getnum(st,3);
  7571. if(base<0 || job<0)
  7572. return 0;
  7573. // bonus for npc-given exp
  7574. bonus = battle_config.quest_exp_rate / 100.;
  7575. base = (int) cap_value(base * bonus, 0, INT_MAX);
  7576. job = (int) cap_value(job * bonus, 0, INT_MAX);
  7577. pc_gainexp(sd, NULL, base, job, true);
  7578. return 0;
  7579. }
  7580. /*==========================================
  7581. * Gain guild exp [Celest]
  7582. *------------------------------------------*/
  7583. BUILDIN_FUNC(guildgetexp)
  7584. {
  7585. TBL_PC* sd;
  7586. int exp;
  7587. sd = script_rid2sd(st);
  7588. if( sd == NULL )
  7589. return 0;
  7590. exp = script_getnum(st,2);
  7591. if(exp < 0)
  7592. return 0;
  7593. if(sd && sd->status.guild_id > 0)
  7594. guild_getexp (sd, exp);
  7595. return 0;
  7596. }
  7597. /*==========================================
  7598. * Changes the guild master of a guild [Skotlex]
  7599. *------------------------------------------*/
  7600. BUILDIN_FUNC(guildchangegm)
  7601. {
  7602. TBL_PC *sd;
  7603. int guild_id;
  7604. const char *name;
  7605. guild_id = script_getnum(st,2);
  7606. name = script_getstr(st,3);
  7607. sd=map_nick2sd(name);
  7608. if (!sd)
  7609. script_pushint(st,0);
  7610. else
  7611. script_pushint(st,guild_gm_change(guild_id, sd));
  7612. return 0;
  7613. }
  7614. /*==========================================
  7615. * モンスター発生
  7616. *------------------------------------------*/
  7617. BUILDIN_FUNC(monster)
  7618. {
  7619. const char* mapn = script_getstr(st,2);
  7620. int x = script_getnum(st,3);
  7621. int y = script_getnum(st,4);
  7622. const char* str = script_getstr(st,5);
  7623. int class_ = script_getnum(st,6);
  7624. int amount = script_getnum(st,7);
  7625. const char* event = "";
  7626. struct map_session_data* sd;
  7627. int m;
  7628. if( script_hasdata(st,8) )
  7629. {
  7630. event = script_getstr(st,8);
  7631. check_event(st, event);
  7632. }
  7633. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7634. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7635. return 1;
  7636. }
  7637. sd = map_id2sd(st->rid);
  7638. if( sd && strcmp(mapn,"this") == 0 )
  7639. m = sd->bl.m;
  7640. else
  7641. {
  7642. m = map_mapname2mapid(mapn);
  7643. if( map[m].flag.src4instance && st->instance_id )
  7644. { // Try to redirect to the instance map, not the src map
  7645. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7646. {
  7647. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7648. return 1;
  7649. }
  7650. }
  7651. }
  7652. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  7653. return 0;
  7654. }
  7655. /*==========================================
  7656. * Request List of Monster Drops
  7657. *------------------------------------------*/
  7658. BUILDIN_FUNC(getmobdrops)
  7659. {
  7660. int class_ = script_getnum(st,2);
  7661. int i, j = 0;
  7662. struct mob_db *mob;
  7663. if( !mobdb_checkid(class_) )
  7664. {
  7665. script_pushint(st, 0);
  7666. return 0;
  7667. }
  7668. mob = mob_db(class_);
  7669. for( i = 0; i < MAX_MOB_DROP; i++ )
  7670. {
  7671. if( mob->dropitem[i].nameid < 1 )
  7672. continue;
  7673. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7674. continue;
  7675. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7676. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7677. j++;
  7678. }
  7679. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7680. script_pushint(st, 1);
  7681. return 0;
  7682. }
  7683. /*==========================================
  7684. * モンスター発生
  7685. *------------------------------------------*/
  7686. BUILDIN_FUNC(areamonster)
  7687. {
  7688. const char* mapn = script_getstr(st,2);
  7689. int x0 = script_getnum(st,3);
  7690. int y0 = script_getnum(st,4);
  7691. int x1 = script_getnum(st,5);
  7692. int y1 = script_getnum(st,6);
  7693. const char* str = script_getstr(st,7);
  7694. int class_ = script_getnum(st,8);
  7695. int amount = script_getnum(st,9);
  7696. const char* event = "";
  7697. struct map_session_data* sd;
  7698. int m;
  7699. if( script_hasdata(st,10) )
  7700. {
  7701. event = script_getstr(st,10);
  7702. check_event(st, event);
  7703. }
  7704. sd = map_id2sd(st->rid);
  7705. if( sd && strcmp(mapn,"this") == 0 )
  7706. m = sd->bl.m;
  7707. else
  7708. {
  7709. m = map_mapname2mapid(mapn);
  7710. if( map[m].flag.src4instance && st->instance_id )
  7711. { // Try to redirect to the instance map, not the src map
  7712. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7713. {
  7714. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7715. return 1;
  7716. }
  7717. }
  7718. }
  7719. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  7720. return 0;
  7721. }
  7722. /*==========================================
  7723. * モンスター削除
  7724. *------------------------------------------*/
  7725. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7726. { //same fix but with killmonster instead - stripping events from mobs.
  7727. TBL_MOB* md = (TBL_MOB*)bl;
  7728. char *event=va_arg(ap,char *);
  7729. int allflag=va_arg(ap,int);
  7730. md->state.npc_killmonster = 1;
  7731. if(!allflag){
  7732. if(strcmp(event,md->npc_event)==0)
  7733. status_kill(bl);
  7734. }else{
  7735. if(!md->spawn)
  7736. status_kill(bl);
  7737. }
  7738. md->state.npc_killmonster = 0;
  7739. return 0;
  7740. }
  7741. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7742. {
  7743. TBL_MOB* md = (TBL_MOB*)bl;
  7744. char *event=va_arg(ap,char *);
  7745. int allflag=va_arg(ap,int);
  7746. if(!allflag){
  7747. if(strcmp(event,md->npc_event)==0)
  7748. status_kill(bl);
  7749. }else{
  7750. if(!md->spawn)
  7751. status_kill(bl);
  7752. }
  7753. return 0;
  7754. }
  7755. BUILDIN_FUNC(killmonster)
  7756. {
  7757. const char *mapname,*event;
  7758. int m,allflag=0;
  7759. mapname=script_getstr(st,2);
  7760. event=script_getstr(st,3);
  7761. if(strcmp(event,"All")==0)
  7762. allflag = 1;
  7763. else
  7764. check_event(st, event);
  7765. if( (m=map_mapname2mapid(mapname))<0 )
  7766. return 0;
  7767. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7768. return 0;
  7769. if( script_hasdata(st,4) ) {
  7770. if ( script_getnum(st,4) == 1 ) {
  7771. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7772. return 0;
  7773. }
  7774. }
  7775. map_freeblock_lock();
  7776. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7777. map_freeblock_unlock();
  7778. return 0;
  7779. }
  7780. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7781. { //Strips the event from the mob if it's killed the old method.
  7782. struct mob_data *md;
  7783. md = BL_CAST(BL_MOB, bl);
  7784. if (md->npc_event[0])
  7785. md->npc_event[0] = 0;
  7786. status_kill(bl);
  7787. return 0;
  7788. }
  7789. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7790. {
  7791. status_kill(bl);
  7792. return 0;
  7793. }
  7794. BUILDIN_FUNC(killmonsterall)
  7795. {
  7796. const char *mapname;
  7797. int m;
  7798. mapname=script_getstr(st,2);
  7799. if( (m = map_mapname2mapid(mapname))<0 )
  7800. return 0;
  7801. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7802. return 0;
  7803. if( script_hasdata(st,3) ) {
  7804. if ( script_getnum(st,3) == 1 ) {
  7805. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  7806. return 0;
  7807. }
  7808. }
  7809. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  7810. return 0;
  7811. }
  7812. /*==========================================
  7813. * Creates a clone of a player.
  7814. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7815. *------------------------------------------*/
  7816. BUILDIN_FUNC(clone)
  7817. {
  7818. TBL_PC *sd, *msd=NULL;
  7819. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7820. unsigned int duration = 0;
  7821. const char *map,*event="";
  7822. map=script_getstr(st,2);
  7823. x=script_getnum(st,3);
  7824. y=script_getnum(st,4);
  7825. event=script_getstr(st,5);
  7826. char_id=script_getnum(st,6);
  7827. if( script_hasdata(st,7) )
  7828. master_id=script_getnum(st,7);
  7829. if( script_hasdata(st,8) )
  7830. mode=script_getnum(st,8);
  7831. if( script_hasdata(st,9) )
  7832. flag=script_getnum(st,9);
  7833. if( script_hasdata(st,10) )
  7834. duration=script_getnum(st,10);
  7835. check_event(st, event);
  7836. m = map_mapname2mapid(map);
  7837. if (m < 0) return 0;
  7838. sd = map_charid2sd(char_id);
  7839. if (master_id) {
  7840. msd = map_charid2sd(master_id);
  7841. if (msd)
  7842. master_id = msd->bl.id;
  7843. else
  7844. master_id = 0;
  7845. }
  7846. if (sd) //Return ID of newly crafted clone.
  7847. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  7848. else //Failed to create clone.
  7849. script_pushint(st,0);
  7850. return 0;
  7851. }
  7852. /*==========================================
  7853. * イベント実行
  7854. *------------------------------------------*/
  7855. BUILDIN_FUNC(doevent)
  7856. {
  7857. const char* event = script_getstr(st,2);
  7858. struct map_session_data* sd;
  7859. if( ( sd = script_rid2sd(st) ) == NULL )
  7860. {
  7861. return 0;
  7862. }
  7863. check_event(st, event);
  7864. npc_event(sd, event, 0);
  7865. return 0;
  7866. }
  7867. /*==========================================
  7868. * NPC主体イベント実行
  7869. *------------------------------------------*/
  7870. BUILDIN_FUNC(donpcevent)
  7871. {
  7872. const char* event = script_getstr(st,2);
  7873. check_event(st, event);
  7874. if( !npc_event_do(event) ) {
  7875. struct npc_data * nd = map_id2nd(st->oid);
  7876. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  7877. script_pushint(st, 0);
  7878. } else
  7879. script_pushint(st, 1);
  7880. return 0;
  7881. }
  7882. /// for Aegis compatibility
  7883. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  7884. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  7885. {
  7886. const char* npc = script_getstr(st,2);
  7887. const char* command = script_getstr(st,3);
  7888. char event[EVENT_NAME_LENGTH];
  7889. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  7890. check_event(st, event);
  7891. npc_event_do(event);
  7892. return 0;
  7893. }
  7894. /*==========================================
  7895. * イベントタイマー追加
  7896. *------------------------------------------*/
  7897. BUILDIN_FUNC(addtimer)
  7898. {
  7899. int tick = script_getnum(st,2);
  7900. const char* event = script_getstr(st, 3);
  7901. TBL_PC* sd;
  7902. check_event(st, event);
  7903. sd = script_rid2sd(st);
  7904. if( sd == NULL )
  7905. return 0;
  7906. pc_addeventtimer(sd,tick,event);
  7907. return 0;
  7908. }
  7909. /*==========================================
  7910. * イベントタイマー削除
  7911. *------------------------------------------*/
  7912. BUILDIN_FUNC(deltimer)
  7913. {
  7914. const char *event;
  7915. TBL_PC* sd;
  7916. event=script_getstr(st, 2);
  7917. sd = script_rid2sd(st);
  7918. if( sd == NULL )
  7919. return 0;
  7920. check_event(st, event);
  7921. pc_deleventtimer(sd,event);
  7922. return 0;
  7923. }
  7924. /*==========================================
  7925. * イベントタイマーのカウント値追加
  7926. *------------------------------------------*/
  7927. BUILDIN_FUNC(addtimercount)
  7928. {
  7929. const char *event;
  7930. int tick;
  7931. TBL_PC* sd;
  7932. event=script_getstr(st, 2);
  7933. tick=script_getnum(st,3);
  7934. sd = script_rid2sd(st);
  7935. if( sd == NULL )
  7936. return 0;
  7937. check_event(st, event);
  7938. pc_addeventtimercount(sd,event,tick);
  7939. return 0;
  7940. }
  7941. /*==========================================
  7942. * NPCタイマー初期化
  7943. *------------------------------------------*/
  7944. BUILDIN_FUNC(initnpctimer)
  7945. {
  7946. struct npc_data *nd;
  7947. int flag = 0;
  7948. if( script_hasdata(st,3) )
  7949. { //Two arguments: NPC name and attach flag.
  7950. nd = npc_name2id(script_getstr(st, 2));
  7951. flag = script_getnum(st,3);
  7952. }
  7953. else if( script_hasdata(st,2) )
  7954. { //Check if argument is numeric (flag) or string (npc name)
  7955. struct script_data *data;
  7956. data = script_getdata(st,2);
  7957. get_val(st,data);
  7958. if( data_isstring(data) ) //NPC name
  7959. nd = npc_name2id(conv_str(st, data));
  7960. else if( data_isint(data) ) //Flag
  7961. {
  7962. nd = (struct npc_data *)map_id2bl(st->oid);
  7963. flag = conv_num(st,data);
  7964. }
  7965. else
  7966. {
  7967. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7968. return 1;
  7969. }
  7970. }
  7971. else
  7972. nd = (struct npc_data *)map_id2bl(st->oid);
  7973. if( !nd )
  7974. return 0;
  7975. if( flag ) //Attach
  7976. {
  7977. TBL_PC* sd = script_rid2sd(st);
  7978. if( sd == NULL )
  7979. return 0;
  7980. nd->u.scr.rid = sd->bl.id;
  7981. }
  7982. npc_settimerevent_tick(nd,0);
  7983. npc_timerevent_start(nd, st->rid);
  7984. return 0;
  7985. }
  7986. /*==========================================
  7987. * NPCタイマー開始
  7988. *------------------------------------------*/
  7989. BUILDIN_FUNC(startnpctimer)
  7990. {
  7991. struct npc_data *nd;
  7992. int flag = 0;
  7993. if( script_hasdata(st,3) )
  7994. { //Two arguments: NPC name and attach flag.
  7995. nd = npc_name2id(script_getstr(st, 2));
  7996. flag = script_getnum(st,3);
  7997. }
  7998. else if( script_hasdata(st,2) )
  7999. { //Check if argument is numeric (flag) or string (npc name)
  8000. struct script_data *data;
  8001. data = script_getdata(st,2);
  8002. get_val(st,data);
  8003. if( data_isstring(data) ) //NPC name
  8004. nd = npc_name2id(conv_str(st, data));
  8005. else if( data_isint(data) ) //Flag
  8006. {
  8007. nd = (struct npc_data *)map_id2bl(st->oid);
  8008. flag = conv_num(st,data);
  8009. }
  8010. else
  8011. {
  8012. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8013. return 1;
  8014. }
  8015. }
  8016. else
  8017. nd=(struct npc_data *)map_id2bl(st->oid);
  8018. if( !nd )
  8019. return 0;
  8020. if( flag ) //Attach
  8021. {
  8022. TBL_PC* sd = script_rid2sd(st);
  8023. if( sd == NULL )
  8024. return 0;
  8025. nd->u.scr.rid = sd->bl.id;
  8026. }
  8027. npc_timerevent_start(nd, st->rid);
  8028. return 0;
  8029. }
  8030. /*==========================================
  8031. * NPCタイマー停止
  8032. *------------------------------------------*/
  8033. BUILDIN_FUNC(stopnpctimer)
  8034. {
  8035. struct npc_data *nd;
  8036. int flag = 0;
  8037. if( script_hasdata(st,3) )
  8038. { //Two arguments: NPC name and attach flag.
  8039. nd = npc_name2id(script_getstr(st, 2));
  8040. flag = script_getnum(st,3);
  8041. }
  8042. else if( script_hasdata(st,2) )
  8043. { //Check if argument is numeric (flag) or string (npc name)
  8044. struct script_data *data;
  8045. data = script_getdata(st,2);
  8046. get_val(st,data);
  8047. if( data_isstring(data) ) //NPC name
  8048. nd = npc_name2id(conv_str(st, data));
  8049. else if( data_isint(data) ) //Flag
  8050. {
  8051. nd = (struct npc_data *)map_id2bl(st->oid);
  8052. flag = conv_num(st,data);
  8053. }
  8054. else
  8055. {
  8056. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8057. return 1;
  8058. }
  8059. }
  8060. else
  8061. nd=(struct npc_data *)map_id2bl(st->oid);
  8062. if( !nd )
  8063. return 0;
  8064. if( flag ) //Detach
  8065. nd->u.scr.rid = 0;
  8066. npc_timerevent_stop(nd);
  8067. return 0;
  8068. }
  8069. /*==========================================
  8070. * NPCタイマー情報所得
  8071. *------------------------------------------*/
  8072. BUILDIN_FUNC(getnpctimer)
  8073. {
  8074. struct npc_data *nd;
  8075. TBL_PC *sd;
  8076. int type = script_getnum(st,2);
  8077. int val = 0;
  8078. if( script_hasdata(st,3) )
  8079. nd = npc_name2id(script_getstr(st,3));
  8080. else
  8081. nd = (struct npc_data *)map_id2bl(st->oid);
  8082. if( !nd || nd->bl.type != BL_NPC )
  8083. {
  8084. script_pushint(st,0);
  8085. ShowError("getnpctimer: Invalid NPC.\n");
  8086. return 1;
  8087. }
  8088. switch( type )
  8089. {
  8090. case 0: val = npc_gettimerevent_tick(nd); break;
  8091. case 1:
  8092. if( nd->u.scr.rid )
  8093. {
  8094. sd = map_id2sd(nd->u.scr.rid);
  8095. if( !sd )
  8096. {
  8097. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8098. break;
  8099. }
  8100. val = (sd->npc_timer_id != INVALID_TIMER);
  8101. }
  8102. else
  8103. val = (nd->u.scr.timerid != INVALID_TIMER);
  8104. break;
  8105. case 2: val = nd->u.scr.timeramount; break;
  8106. }
  8107. script_pushint(st,val);
  8108. return 0;
  8109. }
  8110. /*==========================================
  8111. * NPCタイマー値設定
  8112. *------------------------------------------*/
  8113. BUILDIN_FUNC(setnpctimer)
  8114. {
  8115. int tick;
  8116. struct npc_data *nd;
  8117. tick = script_getnum(st,2);
  8118. if( script_hasdata(st,3) )
  8119. nd = npc_name2id(script_getstr(st,3));
  8120. else
  8121. nd = (struct npc_data *)map_id2bl(st->oid);
  8122. if( !nd || nd->bl.type != BL_NPC )
  8123. {
  8124. script_pushint(st,1);
  8125. ShowError("setnpctimer: Invalid NPC.\n");
  8126. return 1;
  8127. }
  8128. npc_settimerevent_tick(nd,tick);
  8129. script_pushint(st,0);
  8130. return 0;
  8131. }
  8132. /*==========================================
  8133. * attaches the player rid to the timer [Celest]
  8134. *------------------------------------------*/
  8135. BUILDIN_FUNC(attachnpctimer)
  8136. {
  8137. TBL_PC *sd;
  8138. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  8139. if( !nd || nd->bl.type != BL_NPC )
  8140. {
  8141. script_pushint(st,1);
  8142. ShowError("setnpctimer: Invalid NPC.\n");
  8143. return 1;
  8144. }
  8145. if( script_hasdata(st,2) )
  8146. sd = map_nick2sd(script_getstr(st,2));
  8147. else
  8148. sd = script_rid2sd(st);
  8149. if( !sd )
  8150. {
  8151. script_pushint(st,1);
  8152. ShowWarning("attachnpctimer: Invalid player.\n");
  8153. return 1;
  8154. }
  8155. nd->u.scr.rid = sd->bl.id;
  8156. script_pushint(st,0);
  8157. return 0;
  8158. }
  8159. /*==========================================
  8160. * detaches a player rid from the timer [Celest]
  8161. *------------------------------------------*/
  8162. BUILDIN_FUNC(detachnpctimer)
  8163. {
  8164. struct npc_data *nd;
  8165. if( script_hasdata(st,2) )
  8166. nd = npc_name2id(script_getstr(st,2));
  8167. else
  8168. nd = (struct npc_data *)map_id2bl(st->oid);
  8169. if( !nd || nd->bl.type != BL_NPC )
  8170. {
  8171. script_pushint(st,1);
  8172. ShowError("detachnpctimer: Invalid NPC.\n");
  8173. return 1;
  8174. }
  8175. nd->u.scr.rid = 0;
  8176. script_pushint(st,0);
  8177. return 0;
  8178. }
  8179. /*==========================================
  8180. * To avoid "player not attached" script errors, this function is provided,
  8181. * it checks if there is a player attached to the current script. [Skotlex]
  8182. * If no, returns 0, if yes, returns the account_id of the attached player.
  8183. *------------------------------------------*/
  8184. BUILDIN_FUNC(playerattached)
  8185. {
  8186. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  8187. script_pushint(st,0);
  8188. else
  8189. script_pushint(st,st->rid);
  8190. return 0;
  8191. }
  8192. /*==========================================
  8193. * 天の声アナウンス
  8194. *------------------------------------------*/
  8195. BUILDIN_FUNC(announce)
  8196. {
  8197. const char *mes = script_getstr(st,2);
  8198. int flag = script_getnum(st,3);
  8199. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  8200. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  8201. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  8202. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  8203. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  8204. if (flag&0x0f) // Broadcast source or broadcast region defined
  8205. {
  8206. send_target target;
  8207. 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
  8208. if (bl == NULL)
  8209. return 0;
  8210. flag &= 0x07;
  8211. target = (flag == 1) ? ALL_SAMEMAP :
  8212. (flag == 2) ? AREA :
  8213. (flag == 3) ? SELF :
  8214. ALL_CLIENT;
  8215. if (fontColor)
  8216. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  8217. else
  8218. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  8219. }
  8220. else
  8221. {
  8222. if (fontColor)
  8223. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  8224. else
  8225. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  8226. }
  8227. return 0;
  8228. }
  8229. /*==========================================
  8230. * 天の声アナウンス(特定マップ)
  8231. *------------------------------------------*/
  8232. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  8233. {
  8234. char *mes = va_arg(ap, char *);
  8235. int len = va_arg(ap, int);
  8236. int type = va_arg(ap, int);
  8237. char *fontColor = va_arg(ap, char *);
  8238. short fontType = (short)va_arg(ap, int);
  8239. short fontSize = (short)va_arg(ap, int);
  8240. short fontAlign = (short)va_arg(ap, int);
  8241. short fontY = (short)va_arg(ap, int);
  8242. if (fontColor)
  8243. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  8244. else
  8245. clif_broadcast(bl, mes, len, type, SELF);
  8246. return 0;
  8247. }
  8248. BUILDIN_FUNC(mapannounce)
  8249. {
  8250. const char *mapname = script_getstr(st,2);
  8251. const char *mes = script_getstr(st,3);
  8252. int flag = script_getnum(st,4);
  8253. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  8254. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  8255. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  8256. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  8257. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  8258. int m;
  8259. if ((m = map_mapname2mapid(mapname)) < 0)
  8260. return 0;
  8261. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  8262. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8263. return 0;
  8264. }
  8265. /*==========================================
  8266. * 天の声アナウンス(特定エリア)
  8267. *------------------------------------------*/
  8268. BUILDIN_FUNC(areaannounce)
  8269. {
  8270. const char *mapname = script_getstr(st,2);
  8271. int x0 = script_getnum(st,3);
  8272. int y0 = script_getnum(st,4);
  8273. int x1 = script_getnum(st,5);
  8274. int y1 = script_getnum(st,6);
  8275. const char *mes = script_getstr(st,7);
  8276. int flag = script_getnum(st,8);
  8277. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  8278. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  8279. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  8280. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  8281. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  8282. int m;
  8283. if ((m = map_mapname2mapid(mapname)) < 0)
  8284. return 0;
  8285. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  8286. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8287. return 0;
  8288. }
  8289. /*==========================================
  8290. * ユーザー数所得
  8291. *------------------------------------------*/
  8292. BUILDIN_FUNC(getusers)
  8293. {
  8294. int flag, val = 0;
  8295. struct map_session_data* sd;
  8296. struct block_list* bl = NULL;
  8297. flag = script_getnum(st,2);
  8298. switch(flag&0x07)
  8299. {
  8300. case 0:
  8301. if(flag&0x8)
  8302. {// npc
  8303. bl = map_id2bl(st->oid);
  8304. }
  8305. else if((sd = script_rid2sd(st))!=NULL)
  8306. {// pc
  8307. bl = &sd->bl;
  8308. }
  8309. if(bl)
  8310. {
  8311. val = map[bl->m].users;
  8312. }
  8313. break;
  8314. case 1:
  8315. val = map_getusers();
  8316. break;
  8317. default:
  8318. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  8319. script_pushint(st,0);
  8320. return 1;
  8321. }
  8322. script_pushint(st,val);
  8323. return 0;
  8324. }
  8325. /*==========================================
  8326. * Works like @WHO - displays all online users names in window
  8327. *------------------------------------------*/
  8328. BUILDIN_FUNC(getusersname)
  8329. {
  8330. TBL_PC *sd, *pl_sd;
  8331. int disp_num=1, group_level = 0;
  8332. struct s_mapiterator* iter;
  8333. sd = script_rid2sd(st);
  8334. if (!sd) return 0;
  8335. group_level = pc_get_group_level(sd);
  8336. iter = mapit_getallusers();
  8337. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  8338. {
  8339. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  8340. continue; // skip hidden sessions
  8341. if((disp_num++)%10==0)
  8342. clif_scriptnext(sd,st->oid);
  8343. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  8344. }
  8345. mapit_free(iter);
  8346. return 0;
  8347. }
  8348. /*==========================================
  8349. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  8350. *------------------------------------------*/
  8351. BUILDIN_FUNC(getmapguildusers)
  8352. {
  8353. const char *str;
  8354. int m, gid;
  8355. int i=0,c=0;
  8356. struct guild *g = NULL;
  8357. str=script_getstr(st,2);
  8358. gid=script_getnum(st,3);
  8359. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  8360. script_pushint(st,-1);
  8361. return 0;
  8362. }
  8363. g = guild_search(gid);
  8364. if (g){
  8365. for(i = 0; i < g->max_member; i++)
  8366. {
  8367. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  8368. c++;
  8369. }
  8370. }
  8371. script_pushint(st,c);
  8372. return 0;
  8373. }
  8374. /*==========================================
  8375. * マップ指定ユーザー数所得
  8376. *------------------------------------------*/
  8377. BUILDIN_FUNC(getmapusers)
  8378. {
  8379. const char *str;
  8380. int m;
  8381. str=script_getstr(st,2);
  8382. if( (m=map_mapname2mapid(str))< 0){
  8383. script_pushint(st,-1);
  8384. return 0;
  8385. }
  8386. script_pushint(st,map[m].users);
  8387. return 0;
  8388. }
  8389. /*==========================================
  8390. * エリア指定ユーザー数所得
  8391. *------------------------------------------*/
  8392. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  8393. {
  8394. int *users=va_arg(ap,int *);
  8395. (*users)++;
  8396. return 0;
  8397. }
  8398. BUILDIN_FUNC(getareausers)
  8399. {
  8400. const char *str;
  8401. int m,x0,y0,x1,y1,users=0;
  8402. str=script_getstr(st,2);
  8403. x0=script_getnum(st,3);
  8404. y0=script_getnum(st,4);
  8405. x1=script_getnum(st,5);
  8406. y1=script_getnum(st,6);
  8407. if( (m=map_mapname2mapid(str))< 0){
  8408. script_pushint(st,-1);
  8409. return 0;
  8410. }
  8411. map_foreachinarea(buildin_getareausers_sub,
  8412. m,x0,y0,x1,y1,BL_PC,&users);
  8413. script_pushint(st,users);
  8414. return 0;
  8415. }
  8416. /*==========================================
  8417. * エリア指定ドロップアイテム数所得
  8418. *------------------------------------------*/
  8419. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  8420. {
  8421. int item=va_arg(ap,int);
  8422. int *amount=va_arg(ap,int *);
  8423. struct flooritem_data *drop=(struct flooritem_data *)bl;
  8424. if(drop->item_data.nameid==item)
  8425. (*amount)+=drop->item_data.amount;
  8426. return 0;
  8427. }
  8428. BUILDIN_FUNC(getareadropitem)
  8429. {
  8430. const char *str;
  8431. int m,x0,y0,x1,y1,item,amount=0;
  8432. struct script_data *data;
  8433. str=script_getstr(st,2);
  8434. x0=script_getnum(st,3);
  8435. y0=script_getnum(st,4);
  8436. x1=script_getnum(st,5);
  8437. y1=script_getnum(st,6);
  8438. data=script_getdata(st,7);
  8439. get_val(st,data);
  8440. if( data_isstring(data) ){
  8441. const char *name=conv_str(st,data);
  8442. struct item_data *item_data = itemdb_searchname(name);
  8443. item=UNKNOWN_ITEM_ID;
  8444. if( item_data )
  8445. item=item_data->nameid;
  8446. }else
  8447. item=conv_num(st,data);
  8448. if( (m=map_mapname2mapid(str))< 0){
  8449. script_pushint(st,-1);
  8450. return 0;
  8451. }
  8452. map_foreachinarea(buildin_getareadropitem_sub,
  8453. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  8454. script_pushint(st,amount);
  8455. return 0;
  8456. }
  8457. /*==========================================
  8458. * NPCの有効化
  8459. *------------------------------------------*/
  8460. BUILDIN_FUNC(enablenpc)
  8461. {
  8462. const char *str;
  8463. str=script_getstr(st,2);
  8464. npc_enable(str,1);
  8465. return 0;
  8466. }
  8467. /*==========================================
  8468. * NPCの無効化
  8469. *------------------------------------------*/
  8470. BUILDIN_FUNC(disablenpc)
  8471. {
  8472. const char *str;
  8473. str=script_getstr(st,2);
  8474. npc_enable(str,0);
  8475. return 0;
  8476. }
  8477. /*==========================================
  8478. * 隠れているNPCの表示
  8479. *------------------------------------------*/
  8480. BUILDIN_FUNC(hideoffnpc)
  8481. {
  8482. const char *str;
  8483. str=script_getstr(st,2);
  8484. npc_enable(str,2);
  8485. return 0;
  8486. }
  8487. /*==========================================
  8488. * NPCをハイディング
  8489. *------------------------------------------*/
  8490. BUILDIN_FUNC(hideonnpc)
  8491. {
  8492. const char *str;
  8493. str=script_getstr(st,2);
  8494. npc_enable(str,4);
  8495. return 0;
  8496. }
  8497. /// Starts a status effect on the target unit or on the attached player.
  8498. ///
  8499. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  8500. BUILDIN_FUNC(sc_start)
  8501. {
  8502. struct block_list* bl;
  8503. enum sc_type type;
  8504. int tick;
  8505. int val1;
  8506. int val4 = 0;
  8507. type = (sc_type)script_getnum(st,2);
  8508. tick = script_getnum(st,3);
  8509. val1 = script_getnum(st,4);
  8510. if( script_hasdata(st,5) )
  8511. bl = map_id2bl(script_getnum(st,5));
  8512. else
  8513. bl = map_id2bl(st->rid);
  8514. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8515. {// When there isn't a duration specified, try to get it from the skill_db
  8516. tick = skill_get_time(status_sc2skill(type), val1);
  8517. }
  8518. if( potion_flag == 1 && potion_target )
  8519. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8520. bl = map_id2bl(potion_target);
  8521. tick /= 2;// Thrown potions only last half.
  8522. val4 = 1;// Mark that this was a thrown sc_effect
  8523. }
  8524. if( bl )
  8525. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  8526. return 0;
  8527. }
  8528. /// Starts a status effect on the target unit or on the attached player.
  8529. ///
  8530. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  8531. BUILDIN_FUNC(sc_start2)
  8532. {
  8533. struct block_list* bl;
  8534. enum sc_type type;
  8535. int tick;
  8536. int val1;
  8537. int val4 = 0;
  8538. int rate;
  8539. type = (sc_type)script_getnum(st,2);
  8540. tick = script_getnum(st,3);
  8541. val1 = script_getnum(st,4);
  8542. rate = script_getnum(st,5);
  8543. if( script_hasdata(st,6) )
  8544. bl = map_id2bl(script_getnum(st,6));
  8545. else
  8546. bl = map_id2bl(st->rid);
  8547. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8548. {// When there isn't a duration specified, try to get it from the skill_db
  8549. tick = skill_get_time(status_sc2skill(type), val1);
  8550. }
  8551. if( potion_flag == 1 && potion_target )
  8552. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8553. bl = map_id2bl(potion_target);
  8554. tick /= 2;// Thrown potions only last half.
  8555. val4 = 1;// Mark that this was a thrown sc_effect
  8556. }
  8557. if( bl )
  8558. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  8559. return 0;
  8560. }
  8561. /// Starts a status effect on the target unit or on the attached player.
  8562. ///
  8563. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  8564. BUILDIN_FUNC(sc_start4)
  8565. {
  8566. struct block_list* bl;
  8567. enum sc_type type;
  8568. int tick;
  8569. int val1;
  8570. int val2;
  8571. int val3;
  8572. int val4;
  8573. type = (sc_type)script_getnum(st,2);
  8574. tick = script_getnum(st,3);
  8575. val1 = script_getnum(st,4);
  8576. val2 = script_getnum(st,5);
  8577. val3 = script_getnum(st,6);
  8578. val4 = script_getnum(st,7);
  8579. if( script_hasdata(st,8) )
  8580. bl = map_id2bl(script_getnum(st,8));
  8581. else
  8582. bl = map_id2bl(st->rid);
  8583. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8584. {// When there isn't a duration specified, try to get it from the skill_db
  8585. tick = skill_get_time(status_sc2skill(type), val1);
  8586. }
  8587. if( potion_flag == 1 && potion_target )
  8588. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8589. bl = map_id2bl(potion_target);
  8590. tick /= 2;// Thrown potions only last half.
  8591. }
  8592. if( bl )
  8593. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  8594. return 0;
  8595. }
  8596. /// Ends one or all status effects on the target unit or on the attached player.
  8597. ///
  8598. /// sc_end <effect_id>{,<unit_id>};
  8599. BUILDIN_FUNC(sc_end)
  8600. {
  8601. struct block_list* bl;
  8602. int type;
  8603. type = script_getnum(st,2);
  8604. if( script_hasdata(st,3) )
  8605. bl = map_id2bl(script_getnum(st,3));
  8606. else
  8607. bl = map_id2bl(st->rid);
  8608. if( potion_flag==1 && potion_target )
  8609. {//##TODO how does this work [FlavioJS]
  8610. bl = map_id2bl(potion_target);
  8611. }
  8612. if( !bl ) return 0;
  8613. if( type >= 0 && type < SC_MAX )
  8614. {
  8615. struct status_change *sc = status_get_sc(bl);
  8616. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  8617. if (!sce) return 0;
  8618. //This should help status_change_end force disabling the SC in case it has no limit.
  8619. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8620. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8621. } else
  8622. status_change_clear(bl, 2);// remove all effects
  8623. return 0;
  8624. }
  8625. /*==========================================
  8626. * 状態異常耐性を計算した確率を返す
  8627. *------------------------------------------*/
  8628. BUILDIN_FUNC(getscrate)
  8629. {
  8630. struct block_list *bl;
  8631. int type,rate;
  8632. type=script_getnum(st,2);
  8633. rate=script_getnum(st,3);
  8634. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  8635. bl = map_id2bl(script_getnum(st,4));
  8636. else
  8637. bl = map_id2bl(st->rid);
  8638. if (bl)
  8639. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  8640. script_pushint(st,rate);
  8641. return 0;
  8642. }
  8643. /*==========================================
  8644. * getstatus <type>{, <info>};
  8645. *------------------------------------------*/
  8646. BUILDIN_FUNC(getstatus)
  8647. {
  8648. int id, type;
  8649. struct map_session_data* sd = script_rid2sd(st);
  8650. if( sd == NULL )
  8651. {// no player attached
  8652. return 0;
  8653. }
  8654. id = script_getnum(st, 2);
  8655. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  8656. if( id <= SC_NONE || id >= SC_MAX )
  8657. {// invalid status type given
  8658. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  8659. return 0;
  8660. }
  8661. if( sd->sc.count == 0 || !sd->sc.data[id] )
  8662. {// no status is active
  8663. script_pushint(st, 0);
  8664. return 0;
  8665. }
  8666. switch( type )
  8667. {
  8668. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  8669. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  8670. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  8671. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  8672. case 5:
  8673. {
  8674. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  8675. if( timer )
  8676. {// return the amount of time remaining
  8677. script_pushint(st, timer->tick - gettick());
  8678. }
  8679. }
  8680. break;
  8681. default: script_pushint(st, 1); break;
  8682. }
  8683. return 0;
  8684. }
  8685. /*==========================================
  8686. *
  8687. *------------------------------------------*/
  8688. BUILDIN_FUNC(debugmes)
  8689. {
  8690. const char *str;
  8691. str=script_getstr(st,2);
  8692. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8693. return 0;
  8694. }
  8695. /*==========================================
  8696. *捕獲アイテム使用
  8697. *------------------------------------------*/
  8698. BUILDIN_FUNC(catchpet)
  8699. {
  8700. int pet_id;
  8701. TBL_PC *sd;
  8702. pet_id= script_getnum(st,2);
  8703. sd=script_rid2sd(st);
  8704. if( sd == NULL )
  8705. return 0;
  8706. pet_catch_process1(sd,pet_id);
  8707. return 0;
  8708. }
  8709. /*==========================================
  8710. * [orn]
  8711. *------------------------------------------*/
  8712. BUILDIN_FUNC(homunculus_evolution)
  8713. {
  8714. TBL_PC *sd;
  8715. sd=script_rid2sd(st);
  8716. if( sd == NULL )
  8717. return 0;
  8718. if(merc_is_hom_active(sd->hd))
  8719. {
  8720. if (sd->hd->homunculus.intimacy > 91000)
  8721. merc_hom_evolution(sd->hd);
  8722. else
  8723. clif_emotion(&sd->hd->bl, E_SWT);
  8724. }
  8725. return 0;
  8726. }
  8727. /*==========================================
  8728. * [Xantara]
  8729. *------------------------------------------*/
  8730. BUILDIN_FUNC(homunculus_mutate)
  8731. {
  8732. int homun_id, m_class, m_id;
  8733. int homun_array[5] = {6048,6049,6050,6051,6052};
  8734. TBL_PC *sd;
  8735. sd = script_rid2sd(st);
  8736. if( sd == NULL )
  8737. return 0;
  8738. if(script_hasdata(st,2))
  8739. homun_id = script_getnum(st,2);
  8740. else
  8741. homun_id = homun_array[rnd() % 5];
  8742. if(merc_is_hom_active(sd->hd)) {
  8743. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8744. m_id = hom_class2mapid(homun_id);
  8745. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 )
  8746. hom_mutate(sd->hd, homun_id);
  8747. else
  8748. clif_emotion(&sd->hd->bl, E_SWT);
  8749. }
  8750. return 0;
  8751. }
  8752. // [Zephyrus]
  8753. BUILDIN_FUNC(homunculus_shuffle)
  8754. {
  8755. TBL_PC *sd;
  8756. sd=script_rid2sd(st);
  8757. if( sd == NULL )
  8758. return 0;
  8759. if(merc_is_hom_active(sd->hd))
  8760. merc_hom_shuffle(sd->hd);
  8761. return 0;
  8762. }
  8763. //These two functions bring the eA MAPID_* class functionality to scripts.
  8764. BUILDIN_FUNC(eaclass)
  8765. {
  8766. int class_;
  8767. if( script_hasdata(st,2) )
  8768. class_ = script_getnum(st,2);
  8769. else {
  8770. TBL_PC *sd;
  8771. sd=script_rid2sd(st);
  8772. if (!sd) {
  8773. script_pushint(st,-1);
  8774. return 0;
  8775. }
  8776. class_ = sd->status.class_;
  8777. }
  8778. script_pushint(st,pc_jobid2mapid(class_));
  8779. return 0;
  8780. }
  8781. BUILDIN_FUNC(roclass)
  8782. {
  8783. int class_ =script_getnum(st,2);
  8784. int sex;
  8785. if( script_hasdata(st,3) )
  8786. sex = script_getnum(st,3);
  8787. else {
  8788. TBL_PC *sd;
  8789. if (st->rid && (sd=script_rid2sd(st)))
  8790. sex = sd->status.sex;
  8791. else
  8792. sex = 1; //Just use male when not found.
  8793. }
  8794. script_pushint(st,pc_mapid2jobid(class_, sex));
  8795. return 0;
  8796. }
  8797. /*==========================================
  8798. * Tells client to open a hatching window, used for pet incubator
  8799. *------------------------------------------*/
  8800. BUILDIN_FUNC(birthpet)
  8801. {
  8802. TBL_PC *sd;
  8803. sd=script_rid2sd(st);
  8804. if( sd == NULL )
  8805. return 0;
  8806. if( sd->status.pet_id )
  8807. {// do not send egg list, when you already have a pet
  8808. return 0;
  8809. }
  8810. clif_sendegg(sd);
  8811. return 0;
  8812. }
  8813. /*==========================================
  8814. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  8815. *------------------------------------------*/
  8816. BUILDIN_FUNC(resetlvl)
  8817. {
  8818. TBL_PC *sd;
  8819. int type=script_getnum(st,2);
  8820. sd=script_rid2sd(st);
  8821. if( sd == NULL )
  8822. return 0;
  8823. pc_resetlvl(sd,type);
  8824. return 0;
  8825. }
  8826. /*==========================================
  8827. * ステータスリセット
  8828. *------------------------------------------*/
  8829. BUILDIN_FUNC(resetstatus)
  8830. {
  8831. TBL_PC *sd;
  8832. sd=script_rid2sd(st);
  8833. pc_resetstate(sd);
  8834. return 0;
  8835. }
  8836. /*==========================================
  8837. * script command resetskill
  8838. *------------------------------------------*/
  8839. BUILDIN_FUNC(resetskill)
  8840. {
  8841. TBL_PC *sd;
  8842. sd=script_rid2sd(st);
  8843. pc_resetskill(sd,1);
  8844. return 0;
  8845. }
  8846. /*==========================================
  8847. * Counts total amount of skill points.
  8848. *------------------------------------------*/
  8849. BUILDIN_FUNC(skillpointcount)
  8850. {
  8851. TBL_PC *sd;
  8852. sd=script_rid2sd(st);
  8853. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  8854. return 0;
  8855. }
  8856. /*==========================================
  8857. *
  8858. *------------------------------------------*/
  8859. BUILDIN_FUNC(changebase)
  8860. {
  8861. TBL_PC *sd=NULL;
  8862. int vclass;
  8863. if( script_hasdata(st,3) )
  8864. sd=map_id2sd(script_getnum(st,3));
  8865. else
  8866. sd=script_rid2sd(st);
  8867. if(sd == NULL)
  8868. return 0;
  8869. vclass = script_getnum(st,2);
  8870. if(vclass == JOB_WEDDING)
  8871. {
  8872. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  8873. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  8874. )
  8875. return 0;
  8876. }
  8877. if(!sd->disguise && vclass != sd->vd.class_) {
  8878. status_set_viewdata(&sd->bl, vclass);
  8879. //Updated client view. Base, Weapon and Cloth Colors.
  8880. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  8881. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8882. if (sd->vd.cloth_color)
  8883. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8884. clif_skillinfoblock(sd);
  8885. }
  8886. return 0;
  8887. }
  8888. /*==========================================
  8889. * 性別変換
  8890. *------------------------------------------*/
  8891. BUILDIN_FUNC(changesex)
  8892. {
  8893. int i;
  8894. TBL_PC *sd = NULL;
  8895. sd = script_rid2sd(st);
  8896. pc_resetskill(sd,4);
  8897. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  8898. for( i=0; i<EQI_MAX; i++ )
  8899. if( sd->equip_index[i] >= 0 ) pc_unequipitem(sd, sd->equip_index[i], 3);
  8900. chrif_changesex(sd);
  8901. return 0;
  8902. }
  8903. /*==========================================
  8904. * Works like 'announce' but outputs in the common chat window
  8905. *------------------------------------------*/
  8906. BUILDIN_FUNC(globalmes)
  8907. {
  8908. struct block_list *bl = map_id2bl(st->oid);
  8909. struct npc_data *nd = (struct npc_data *)bl;
  8910. const char *name=NULL,*mes;
  8911. mes=script_getstr(st,2); // メッセージの取得
  8912. if(mes==NULL) return 0;
  8913. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  8914. name=script_getstr(st,3);
  8915. } else {
  8916. name=nd->name;
  8917. }
  8918. npc_globalmessage(name,mes); // グローバルメッセージ送信
  8919. return 0;
  8920. }
  8921. /////////////////////////////////////////////////////////////////////
  8922. // NPC waiting room (chat room)
  8923. //
  8924. /// Creates a waiting room (chat room) for this npc.
  8925. ///
  8926. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  8927. BUILDIN_FUNC(waitingroom)
  8928. {
  8929. struct npc_data* nd;
  8930. int pub = 1;
  8931. const char* title = script_getstr(st, 2);
  8932. int limit = script_getnum(st, 3);
  8933. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  8934. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  8935. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  8936. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  8937. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  8938. nd = (struct npc_data *)map_id2bl(st->oid);
  8939. if( nd != NULL )
  8940. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  8941. return 0;
  8942. }
  8943. /// Removes the waiting room of the current or target npc.
  8944. ///
  8945. /// delwaitingroom "<npc_name>";
  8946. /// delwaitingroom;
  8947. BUILDIN_FUNC(delwaitingroom)
  8948. {
  8949. struct npc_data* nd;
  8950. if( script_hasdata(st,2) )
  8951. nd = npc_name2id(script_getstr(st, 2));
  8952. else
  8953. nd = (struct npc_data *)map_id2bl(st->oid);
  8954. if( nd != NULL )
  8955. chat_deletenpcchat(nd);
  8956. return 0;
  8957. }
  8958. /// Kicks all the players from the waiting room of the current or target npc.
  8959. ///
  8960. /// kickwaitingroomall "<npc_name>";
  8961. /// kickwaitingroomall;
  8962. BUILDIN_FUNC(waitingroomkickall)
  8963. {
  8964. struct npc_data* nd;
  8965. struct chat_data* cd;
  8966. if( script_hasdata(st,2) )
  8967. nd = npc_name2id(script_getstr(st,2));
  8968. else
  8969. nd = (struct npc_data *)map_id2bl(st->oid);
  8970. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8971. chat_npckickall(cd);
  8972. return 0;
  8973. }
  8974. /// Enables the waiting room event of the current or target npc.
  8975. ///
  8976. /// enablewaitingroomevent "<npc_name>";
  8977. /// enablewaitingroomevent;
  8978. BUILDIN_FUNC(enablewaitingroomevent)
  8979. {
  8980. struct npc_data* nd;
  8981. struct chat_data* cd;
  8982. if( script_hasdata(st,2) )
  8983. nd = npc_name2id(script_getstr(st, 2));
  8984. else
  8985. nd = (struct npc_data *)map_id2bl(st->oid);
  8986. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8987. chat_enableevent(cd);
  8988. return 0;
  8989. }
  8990. /// Disables the waiting room event of the current or target npc.
  8991. ///
  8992. /// disablewaitingroomevent "<npc_name>";
  8993. /// disablewaitingroomevent;
  8994. BUILDIN_FUNC(disablewaitingroomevent)
  8995. {
  8996. struct npc_data *nd;
  8997. struct chat_data *cd;
  8998. if( script_hasdata(st,2) )
  8999. nd = npc_name2id(script_getstr(st, 2));
  9000. else
  9001. nd = (struct npc_data *)map_id2bl(st->oid);
  9002. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9003. chat_disableevent(cd);
  9004. return 0;
  9005. }
  9006. /// Returns info on the waiting room of the current or target npc.
  9007. /// Returns -1 if the type unknown
  9008. /// <type>=0 : current number of users
  9009. /// <type>=1 : maximum number of users allowed
  9010. /// <type>=2 : the number of users that trigger the event
  9011. /// <type>=3 : if the trigger is disabled
  9012. /// <type>=4 : the title of the waiting room
  9013. /// <type>=5 : the password of the waiting room
  9014. /// <type>=16 : the name of the waiting room event
  9015. /// <type>=32 : if the waiting room is full
  9016. /// <type>=33 : if there are enough users to trigger the event
  9017. ///
  9018. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9019. /// getwaitingroomstate(<type>) -> <info>
  9020. BUILDIN_FUNC(getwaitingroomstate)
  9021. {
  9022. struct npc_data *nd;
  9023. struct chat_data *cd;
  9024. int type;
  9025. type = script_getnum(st,2);
  9026. if( script_hasdata(st,3) )
  9027. nd = npc_name2id(script_getstr(st, 3));
  9028. else
  9029. nd = (struct npc_data *)map_id2bl(st->oid);
  9030. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9031. {
  9032. script_pushint(st, -1);
  9033. return 0;
  9034. }
  9035. switch(type)
  9036. {
  9037. case 0: script_pushint(st, cd->users); break;
  9038. case 1: script_pushint(st, cd->limit); break;
  9039. case 2: script_pushint(st, cd->trigger&0x7f); break;
  9040. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  9041. case 4: script_pushstrcopy(st, cd->title); break;
  9042. case 5: script_pushstrcopy(st, cd->pass); break;
  9043. case 16: script_pushstrcopy(st, cd->npc_event);break;
  9044. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  9045. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  9046. default: script_pushint(st, -1); break;
  9047. }
  9048. return 0;
  9049. }
  9050. /// Warps the trigger or target amount of players to the target map and position.
  9051. /// Players are automatically removed from the waiting room.
  9052. /// Those waiting the longest will get warped first.
  9053. /// The target map can be "Random" for a random position in the current map,
  9054. /// and "SavePoint" for the savepoint map+position.
  9055. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  9056. ///
  9057. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  9058. /// The total number of teleported players is put into $@warpwaitingpcnum
  9059. ///
  9060. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  9061. /// warpwaitingpc "<map name>",<x>,<y>;
  9062. BUILDIN_FUNC(warpwaitingpc)
  9063. {
  9064. int x;
  9065. int y;
  9066. int i;
  9067. int n;
  9068. const char* map_name;
  9069. struct npc_data* nd;
  9070. struct chat_data* cd;
  9071. TBL_PC* sd;
  9072. nd = (struct npc_data *)map_id2bl(st->oid);
  9073. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9074. return 0;
  9075. map_name = script_getstr(st,2);
  9076. x = script_getnum(st,3);
  9077. y = script_getnum(st,4);
  9078. n = cd->trigger&0x7f;
  9079. if( script_hasdata(st,5) )
  9080. n = script_getnum(st,5);
  9081. for( i = 0; i < n && cd->users > 0; i++ )
  9082. {
  9083. sd = cd->usersd[0];
  9084. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  9085. {// can't teleport on this map
  9086. break;
  9087. }
  9088. if( cd->zeny )
  9089. {// fee set
  9090. if( (uint32)sd->status.zeny < cd->zeny )
  9091. {// no zeny to cover set fee
  9092. break;
  9093. }
  9094. pc_payzeny(sd, cd->zeny);
  9095. }
  9096. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  9097. if( strcmp(map_name,"Random") == 0 )
  9098. pc_randomwarp(sd,CLR_TELEPORT);
  9099. else if( strcmp(map_name,"SavePoint") == 0 )
  9100. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  9101. else
  9102. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  9103. }
  9104. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  9105. return 0;
  9106. }
  9107. /////////////////////////////////////////////////////////////////////
  9108. // ...
  9109. //
  9110. /// Detaches a character from a script.
  9111. ///
  9112. /// @param st Script state to detach the character from.
  9113. static void script_detach_rid(struct script_state* st)
  9114. {
  9115. if(st->rid)
  9116. {
  9117. script_detach_state(st, false);
  9118. st->rid = 0;
  9119. }
  9120. }
  9121. /*==========================================
  9122. * RIDのアタッチ
  9123. *------------------------------------------*/
  9124. BUILDIN_FUNC(attachrid)
  9125. {
  9126. int rid = script_getnum(st,2);
  9127. struct map_session_data* sd;
  9128. if ((sd = map_id2sd(rid))!=NULL) {
  9129. script_detach_rid(st);
  9130. st->rid = rid;
  9131. script_attach_state(st);
  9132. script_pushint(st,1);
  9133. } else
  9134. script_pushint(st,0);
  9135. return 0;
  9136. }
  9137. /*==========================================
  9138. * RIDのデタッチ
  9139. *------------------------------------------*/
  9140. BUILDIN_FUNC(detachrid)
  9141. {
  9142. script_detach_rid(st);
  9143. return 0;
  9144. }
  9145. /*==========================================
  9146. * 存在チェック
  9147. *------------------------------------------*/
  9148. BUILDIN_FUNC(isloggedin)
  9149. {
  9150. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  9151. if (script_hasdata(st,3) && sd &&
  9152. sd->status.char_id != script_getnum(st,3))
  9153. sd = NULL;
  9154. push_val(st->stack,C_INT,sd!=NULL);
  9155. return 0;
  9156. }
  9157. /*==========================================
  9158. *
  9159. *------------------------------------------*/
  9160. BUILDIN_FUNC(setmapflagnosave)
  9161. {
  9162. int m,x,y;
  9163. unsigned short mapindex;
  9164. const char *str,*str2;
  9165. str=script_getstr(st,2);
  9166. str2=script_getstr(st,3);
  9167. x=script_getnum(st,4);
  9168. y=script_getnum(st,5);
  9169. m = map_mapname2mapid(str);
  9170. mapindex = mapindex_name2id(str2);
  9171. if(m >= 0 && mapindex) {
  9172. map[m].flag.nosave=1;
  9173. map[m].save.map=mapindex;
  9174. map[m].save.x=x;
  9175. map[m].save.y=y;
  9176. }
  9177. return 0;
  9178. }
  9179. BUILDIN_FUNC(getmapflag)
  9180. {
  9181. int m,i;
  9182. const char *str;
  9183. str=script_getstr(st,2);
  9184. i=script_getnum(st,3);
  9185. m = map_mapname2mapid(str);
  9186. if(m >= 0) {
  9187. switch(i) {
  9188. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  9189. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  9190. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  9191. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  9192. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  9193. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  9194. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  9195. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  9196. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  9197. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  9198. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  9199. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  9200. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  9201. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  9202. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  9203. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  9204. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  9205. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  9206. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  9207. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  9208. /**
  9209. * No longer available, keeping here just in case it's back someday. [Ind]
  9210. **/
  9211. //case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  9212. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  9213. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  9214. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  9215. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  9216. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  9217. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  9218. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  9219. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  9220. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  9221. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  9222. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  9223. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  9224. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  9225. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  9226. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  9227. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  9228. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  9229. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  9230. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  9231. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  9232. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  9233. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  9234. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  9235. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  9236. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  9237. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  9238. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  9239. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  9240. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  9241. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  9242. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  9243. }
  9244. }
  9245. return 0;
  9246. }
  9247. BUILDIN_FUNC(setmapflag)
  9248. {
  9249. int m,i;
  9250. const char *str;
  9251. const char *val=NULL;
  9252. str=script_getstr(st,2);
  9253. i=script_getnum(st,3);
  9254. if(script_hasdata(st,4)){
  9255. val=script_getstr(st,4);
  9256. }
  9257. m = map_mapname2mapid(str);
  9258. if(m >= 0) {
  9259. switch(i) {
  9260. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  9261. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  9262. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  9263. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  9264. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  9265. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  9266. case MF_PVP: map[m].flag.pvp = 1; break;
  9267. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  9268. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  9269. case MF_GVG: map[m].flag.gvg = 1; break;
  9270. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  9271. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  9272. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  9273. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  9274. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  9275. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  9276. case MF_SNOW: map[m].flag.snow = 1; break;
  9277. case MF_FOG: map[m].flag.fog = 1; break;
  9278. case MF_SAKURA: map[m].flag.sakura = 1; break;
  9279. case MF_LEAVES: map[m].flag.leaves = 1; break;
  9280. /**
  9281. * No longer available, keeping here just in case it's back someday. [Ind]
  9282. **/
  9283. //case MF_RAIN: map[m].flag.rain = 1; break;
  9284. case MF_NOGO: map[m].flag.nogo = 1; break;
  9285. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  9286. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  9287. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  9288. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  9289. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  9290. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  9291. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  9292. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  9293. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  9294. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  9295. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  9296. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  9297. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  9298. case MF_RESTRICTED:
  9299. map[m].zone |= 1<<((int)atoi(val)+1);
  9300. map[m].flag.restricted=1;
  9301. break;
  9302. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  9303. case MF_NODROP: map[m].flag.nodrop = 1; break;
  9304. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  9305. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  9306. case MF_NOVENDING: map[m].flag.novending = 1; break;
  9307. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  9308. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  9309. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  9310. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  9311. case MF_TOWN: map[m].flag.town = 1; break;
  9312. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  9313. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  9314. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  9315. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  9316. case MF_BATTLEGROUND: map[m].flag.battleground = (!val || atoi(val) < 0 || atoi(val) > 2) ? 1 : atoi(val); break;
  9317. case MF_RESET: map[m].flag.reset = 1; break;
  9318. }
  9319. }
  9320. return 0;
  9321. }
  9322. BUILDIN_FUNC(removemapflag)
  9323. {
  9324. int m,i;
  9325. const char *str;
  9326. const char *val=NULL;
  9327. str=script_getstr(st,2);
  9328. i=script_getnum(st,3);
  9329. if(script_hasdata(st,4)){
  9330. val=script_getstr(st,4);
  9331. }
  9332. m = map_mapname2mapid(str);
  9333. if(m >= 0) {
  9334. switch(i) {
  9335. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  9336. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  9337. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  9338. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  9339. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  9340. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  9341. case MF_PVP: map[m].flag.pvp = 0; break;
  9342. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  9343. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  9344. case MF_GVG: map[m].flag.gvg = 0; break;
  9345. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  9346. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  9347. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  9348. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  9349. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  9350. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  9351. case MF_SNOW: map[m].flag.snow = 0; break;
  9352. case MF_FOG: map[m].flag.fog = 0; break;
  9353. case MF_SAKURA: map[m].flag.sakura = 0; break;
  9354. case MF_LEAVES: map[m].flag.leaves = 0; break;
  9355. /**
  9356. * No longer available, keeping here just in case it's back someday. [Ind]
  9357. **/
  9358. //case MF_RAIN: map[m].flag.rain = 0; break;
  9359. case MF_NOGO: map[m].flag.nogo = 0; break;
  9360. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  9361. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  9362. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  9363. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  9364. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  9365. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  9366. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  9367. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  9368. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  9369. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  9370. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  9371. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  9372. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  9373. case MF_RESTRICTED:
  9374. map[m].zone ^= 1<<((int)atoi(val)+1);
  9375. if (map[m].zone == 0){
  9376. map[m].flag.restricted=0;
  9377. }
  9378. break;
  9379. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  9380. case MF_NODROP: map[m].flag.nodrop = 0; break;
  9381. case MF_JEXP: map[m].jexp = 0; break;
  9382. case MF_BEXP: map[m].bexp = 0; break;
  9383. case MF_NOVENDING: map[m].flag.novending = 0; break;
  9384. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  9385. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  9386. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  9387. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  9388. case MF_TOWN: map[m].flag.town = 0; break;
  9389. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  9390. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  9391. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  9392. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  9393. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  9394. case MF_RESET: map[m].flag.reset = 0; break;
  9395. }
  9396. }
  9397. return 0;
  9398. }
  9399. BUILDIN_FUNC(pvpon)
  9400. {
  9401. int m;
  9402. const char *str;
  9403. TBL_PC* sd = NULL;
  9404. struct s_mapiterator* iter;
  9405. str = script_getstr(st,2);
  9406. m = map_mapname2mapid(str);
  9407. if( m < 0 || map[m].flag.pvp )
  9408. return 0; // nothing to do
  9409. map[m].flag.pvp = 1;
  9410. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  9411. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  9412. return 0;
  9413. iter = mapit_getallusers();
  9414. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9415. {
  9416. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  9417. continue; // not applicable
  9418. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  9419. sd->pvp_rank = 0;
  9420. sd->pvp_lastusers = 0;
  9421. sd->pvp_point = 5;
  9422. sd->pvp_won = 0;
  9423. sd->pvp_lost = 0;
  9424. }
  9425. mapit_free(iter);
  9426. return 0;
  9427. }
  9428. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  9429. {
  9430. TBL_PC* sd = (TBL_PC*)bl;
  9431. clif_pvpset(sd, 0, 0, 2);
  9432. if (sd->pvp_timer != INVALID_TIMER) {
  9433. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  9434. sd->pvp_timer = INVALID_TIMER;
  9435. }
  9436. return 0;
  9437. }
  9438. BUILDIN_FUNC(pvpoff)
  9439. {
  9440. int m;
  9441. const char *str;
  9442. str=script_getstr(st,2);
  9443. m = map_mapname2mapid(str);
  9444. if(m < 0 || !map[m].flag.pvp)
  9445. return 0; //fixed Lupus
  9446. map[m].flag.pvp = 0;
  9447. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9448. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  9449. return 0;
  9450. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  9451. return 0;
  9452. }
  9453. BUILDIN_FUNC(gvgon)
  9454. {
  9455. int m;
  9456. const char *str;
  9457. str=script_getstr(st,2);
  9458. m = map_mapname2mapid(str);
  9459. if(m >= 0 && !map[m].flag.gvg) {
  9460. map[m].flag.gvg = 1;
  9461. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9462. }
  9463. return 0;
  9464. }
  9465. BUILDIN_FUNC(gvgoff)
  9466. {
  9467. int m;
  9468. const char *str;
  9469. str=script_getstr(st,2);
  9470. m = map_mapname2mapid(str);
  9471. if(m >= 0 && map[m].flag.gvg) {
  9472. map[m].flag.gvg = 0;
  9473. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9474. }
  9475. return 0;
  9476. }
  9477. /*==========================================
  9478. * Shows an emoticon on top of the player/npc
  9479. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  9480. *------------------------------------------*/
  9481. //Optional second parameter added by [Skotlex]
  9482. BUILDIN_FUNC(emotion)
  9483. {
  9484. int type;
  9485. int player=0;
  9486. type=script_getnum(st,2);
  9487. if(type < 0 || type > 100)
  9488. return 0;
  9489. if( script_hasdata(st,3) )
  9490. player=script_getnum(st,3);
  9491. if (player) {
  9492. TBL_PC *sd = NULL;
  9493. if( script_hasdata(st,4) )
  9494. sd = map_nick2sd(script_getstr(st,4));
  9495. else
  9496. sd = script_rid2sd(st);
  9497. if (sd)
  9498. clif_emotion(&sd->bl,type);
  9499. } else
  9500. if( script_hasdata(st,4) )
  9501. {
  9502. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  9503. if(nd)
  9504. clif_emotion(&nd->bl,type);
  9505. }
  9506. else
  9507. clif_emotion(map_id2bl(st->oid),type);
  9508. return 0;
  9509. }
  9510. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  9511. {
  9512. int m=va_arg(ap,int);
  9513. int g_id=va_arg(ap,int);
  9514. int flag=va_arg(ap,int);
  9515. if(!sd || sd->bl.m != m)
  9516. return 0;
  9517. if(
  9518. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  9519. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  9520. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  9521. )
  9522. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  9523. return 1;
  9524. }
  9525. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  9526. {
  9527. struct mob_data *md=(struct mob_data *)bl;
  9528. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  9529. status_kill(bl);
  9530. return 0;
  9531. }
  9532. BUILDIN_FUNC(maprespawnguildid)
  9533. {
  9534. const char *mapname=script_getstr(st,2);
  9535. int g_id=script_getnum(st,3);
  9536. int flag=script_getnum(st,4);
  9537. int m=map_mapname2mapid(mapname);
  9538. if(m == -1)
  9539. return 0;
  9540. //Catch ALL players (in case some are 'between maps' on execution time)
  9541. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  9542. if (flag&4) //Remove script mobs.
  9543. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  9544. return 0;
  9545. }
  9546. BUILDIN_FUNC(agitstart)
  9547. {
  9548. if(agit_flag==1) return 0; // Agit already Start.
  9549. agit_flag=1;
  9550. guild_agit_start();
  9551. return 0;
  9552. }
  9553. BUILDIN_FUNC(agitend)
  9554. {
  9555. if(agit_flag==0) return 0; // Agit already End.
  9556. agit_flag=0;
  9557. guild_agit_end();
  9558. return 0;
  9559. }
  9560. BUILDIN_FUNC(agitstart2)
  9561. {
  9562. if(agit2_flag==1) return 0; // Agit2 already Start.
  9563. agit2_flag=1;
  9564. guild_agit2_start();
  9565. return 0;
  9566. }
  9567. BUILDIN_FUNC(agitend2)
  9568. {
  9569. if(agit2_flag==0) return 0; // Agit2 already End.
  9570. agit2_flag=0;
  9571. guild_agit2_end();
  9572. return 0;
  9573. }
  9574. /*==========================================
  9575. * Returns whether woe is on or off. // choice script
  9576. *------------------------------------------*/
  9577. BUILDIN_FUNC(agitcheck)
  9578. {
  9579. script_pushint(st,agit_flag);
  9580. return 0;
  9581. }
  9582. /*==========================================
  9583. * Returns whether woese is on or off. // choice script
  9584. *------------------------------------------*/
  9585. BUILDIN_FUNC(agitcheck2)
  9586. {
  9587. script_pushint(st,agit2_flag);
  9588. return 0;
  9589. }
  9590. /// Sets the guild_id of this npc.
  9591. ///
  9592. /// flagemblem <guild_id>;
  9593. BUILDIN_FUNC(flagemblem)
  9594. {
  9595. TBL_NPC* nd;
  9596. int g_id=script_getnum(st,2);
  9597. if(g_id < 0) return 0;
  9598. nd = (TBL_NPC*)map_id2nd(st->oid);
  9599. if( nd == NULL )
  9600. {
  9601. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  9602. }
  9603. else if( nd->subtype != SCRIPT )
  9604. {
  9605. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  9606. }
  9607. else
  9608. {
  9609. nd->u.scr.guild_id = g_id;
  9610. clif_guild_emblem_area(&nd->bl);
  9611. }
  9612. return 0;
  9613. }
  9614. BUILDIN_FUNC(getcastlename)
  9615. {
  9616. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9617. struct guild_castle* gc = guild_mapname2gc(mapname);
  9618. const char* name = (gc) ? gc->castle_name : "";
  9619. script_pushstrcopy(st,name);
  9620. return 0;
  9621. }
  9622. BUILDIN_FUNC(getcastledata)
  9623. {
  9624. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9625. int index = script_getnum(st,3);
  9626. struct guild_castle *gc = guild_mapname2gc(mapname);
  9627. if (gc == NULL) {
  9628. script_pushint(st,0);
  9629. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9630. return 1;
  9631. }
  9632. switch (index) {
  9633. case 1:
  9634. script_pushint(st,gc->guild_id); break;
  9635. case 2:
  9636. script_pushint(st,gc->economy); break;
  9637. case 3:
  9638. script_pushint(st,gc->defense); break;
  9639. case 4:
  9640. script_pushint(st,gc->triggerE); break;
  9641. case 5:
  9642. script_pushint(st,gc->triggerD); break;
  9643. case 6:
  9644. script_pushint(st,gc->nextTime); break;
  9645. case 7:
  9646. script_pushint(st,gc->payTime); break;
  9647. case 8:
  9648. script_pushint(st,gc->createTime); break;
  9649. case 9:
  9650. script_pushint(st,gc->visibleC); break;
  9651. default:
  9652. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  9653. script_pushint(st,gc->guardian[index-10].visible);
  9654. break;
  9655. }
  9656. script_pushint(st,0);
  9657. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9658. return 1;
  9659. }
  9660. return 0;
  9661. }
  9662. BUILDIN_FUNC(setcastledata)
  9663. {
  9664. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9665. int index = script_getnum(st,3);
  9666. int value = script_getnum(st,4);
  9667. struct guild_castle *gc = guild_mapname2gc(mapname);
  9668. if (gc == NULL) {
  9669. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9670. return 1;
  9671. }
  9672. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  9673. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9674. return 1;
  9675. }
  9676. guild_castledatasave(gc->castle_id, index, value);
  9677. return 0;
  9678. }
  9679. /* =====================================================================
  9680. * ギルド情報を要求する
  9681. * ---------------------------------------------------------------------*/
  9682. BUILDIN_FUNC(requestguildinfo)
  9683. {
  9684. int guild_id=script_getnum(st,2);
  9685. const char *event=NULL;
  9686. if( script_hasdata(st,3) ){
  9687. event=script_getstr(st,3);
  9688. check_event(st, event);
  9689. }
  9690. if(guild_id>0)
  9691. guild_npc_request_info(guild_id,event);
  9692. return 0;
  9693. }
  9694. /// Returns the number of cards that have been compounded onto the specified equipped item.
  9695. /// getequipcardcnt(<equipment slot>);
  9696. BUILDIN_FUNC(getequipcardcnt)
  9697. {
  9698. int i=-1,j,num;
  9699. TBL_PC *sd;
  9700. int count;
  9701. num=script_getnum(st,2);
  9702. sd=script_rid2sd(st);
  9703. if (num > 0 && num <= ARRAYLENGTH(equip))
  9704. i=pc_checkequip(sd,equip[num-1]);
  9705. if (i < 0 || !sd->inventory_data[i]) {
  9706. script_pushint(st,0);
  9707. return 0;
  9708. }
  9709. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9710. {
  9711. script_pushint(st,0);
  9712. return 0;
  9713. }
  9714. count = 0;
  9715. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  9716. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  9717. count++;
  9718. script_pushint(st,count);
  9719. return 0;
  9720. }
  9721. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  9722. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  9723. /// successremovecards <slot>;
  9724. BUILDIN_FUNC(successremovecards) {
  9725. int i=-1,j,c,cardflag=0;
  9726. TBL_PC* sd = script_rid2sd(st);
  9727. int num = script_getnum(st,2);
  9728. if (num > 0 && num <= ARRAYLENGTH(equip))
  9729. i=pc_checkequip(sd,equip[num-1]);
  9730. if (i < 0 || !sd->inventory_data[i]) {
  9731. return 0;
  9732. }
  9733. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9734. return 0;
  9735. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  9736. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  9737. int flag;
  9738. struct item item_tmp;
  9739. memset(&item_tmp,0,sizeof(item_tmp));
  9740. cardflag = 1;
  9741. item_tmp.nameid = sd->status.inventory[i].card[c];
  9742. item_tmp.identify = 1;
  9743. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // 持てないならドロップ
  9744. clif_additem(sd,0,0,flag);
  9745. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9746. }
  9747. }
  9748. }
  9749. if(cardflag == 1) {// カードを取り除いたアイテム所得
  9750. int flag;
  9751. struct item item_tmp;
  9752. memset(&item_tmp,0,sizeof(item_tmp));
  9753. item_tmp.nameid = sd->status.inventory[i].nameid;
  9754. item_tmp.identify = 1;
  9755. item_tmp.refine = sd->status.inventory[i].refine;
  9756. item_tmp.attribute = sd->status.inventory[i].attribute;
  9757. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  9758. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9759. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9760. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  9761. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // もてないならドロップ
  9762. clif_additem(sd,0,0,flag);
  9763. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9764. }
  9765. clif_misceffect(&sd->bl,3);
  9766. }
  9767. return 0;
  9768. }
  9769. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  9770. /// failedremovecards <slot>, <type>;
  9771. /// <type>=0 : will destroy both the item and the cards.
  9772. /// <type>=1 : will keep the item, but destroy the cards.
  9773. /// <type>=2 : will keep the cards, but destroy the item.
  9774. /// <type>=? : will just display the failure effect.
  9775. BUILDIN_FUNC(failedremovecards) {
  9776. int i=-1,j,c,cardflag=0;
  9777. TBL_PC* sd = script_rid2sd(st);
  9778. int num = script_getnum(st,2);
  9779. int typefail = script_getnum(st,3);
  9780. if (num > 0 && num <= ARRAYLENGTH(equip))
  9781. i=pc_checkequip(sd,equip[num-1]);
  9782. if (i < 0 || !sd->inventory_data[i])
  9783. return 0;
  9784. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9785. return 0;
  9786. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  9787. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  9788. cardflag = 1;
  9789. if(typefail == 2) {// add cards to inventory, clear
  9790. int flag;
  9791. struct item item_tmp;
  9792. memset(&item_tmp,0,sizeof(item_tmp));
  9793. item_tmp.nameid = sd->status.inventory[i].card[c];
  9794. item_tmp.identify = 1;
  9795. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  9796. clif_additem(sd,0,0,flag);
  9797. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9798. }
  9799. }
  9800. }
  9801. }
  9802. if(cardflag == 1) {
  9803. if(typefail == 0 || typefail == 2){ // 武具損失
  9804. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  9805. }
  9806. if(typefail == 1){ // カードのみ損失(武具を返す)
  9807. int flag;
  9808. struct item item_tmp;
  9809. memset(&item_tmp,0,sizeof(item_tmp));
  9810. item_tmp.nameid = sd->status.inventory[i].nameid;
  9811. item_tmp.identify = 1;
  9812. item_tmp.refine = sd->status.inventory[i].refine;
  9813. item_tmp.attribute = sd->status.inventory[i].attribute;
  9814. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  9815. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9816. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9817. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  9818. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  9819. clif_additem(sd,0,0,flag);
  9820. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9821. }
  9822. }
  9823. clif_misceffect(&sd->bl,2);
  9824. }
  9825. return 0;
  9826. }
  9827. /* ================================================================
  9828. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  9829. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  9830. * improved by [Lance]
  9831. * ================================================================*/
  9832. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  9833. {
  9834. int x,y,m,check_val=0,check_ID=0,i=0;
  9835. struct guild *g = NULL;
  9836. struct party_data *p = NULL;
  9837. const char *str;
  9838. const char *mapname;
  9839. unsigned int index;
  9840. mapname=script_getstr(st,2);
  9841. str=script_getstr(st,3);
  9842. x=script_getnum(st,4);
  9843. y=script_getnum(st,5);
  9844. if(script_hasdata(st,7)){
  9845. check_val=script_getnum(st,6);
  9846. check_ID=script_getnum(st,7);
  9847. }
  9848. if((m=map_mapname2mapid(mapname))< 0)
  9849. return 0;
  9850. if(!(index=mapindex_name2id(str)))
  9851. return 0;
  9852. switch(check_val){
  9853. case 1:
  9854. g = guild_search(check_ID);
  9855. if (g){
  9856. for( i=0; i < g->max_member; i++)
  9857. {
  9858. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  9859. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  9860. }
  9861. }
  9862. }
  9863. break;
  9864. case 2:
  9865. p = party_search(check_ID);
  9866. if(p){
  9867. for(i=0;i<MAX_PARTY; i++){
  9868. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  9869. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  9870. }
  9871. }
  9872. }
  9873. break;
  9874. default:
  9875. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  9876. break;
  9877. }
  9878. return 0;
  9879. }
  9880. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  9881. {
  9882. char *event=va_arg(ap,char *);
  9883. struct mob_data *md = ((struct mob_data *)bl);
  9884. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  9885. return 1;
  9886. return 0;
  9887. }
  9888. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  9889. {
  9890. const char *mapname,*event;
  9891. int m;
  9892. mapname=script_getstr(st,2);
  9893. event=script_getstr(st,3);
  9894. if( strcmp(event, "all") == 0 )
  9895. event = NULL;
  9896. else
  9897. check_event(st, event);
  9898. if( strcmp(mapname, "this") == 0 ) {
  9899. struct map_session_data *sd = script_rid2sd(st);
  9900. if( sd )
  9901. m = sd->bl.m;
  9902. else {
  9903. script_pushint(st,-1);
  9904. return 0;
  9905. }
  9906. }
  9907. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  9908. script_pushint(st,-1);
  9909. return 0;
  9910. }
  9911. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  9912. {
  9913. script_pushint(st,-1);
  9914. return 0;
  9915. }
  9916. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9917. return 0;
  9918. }
  9919. BUILDIN_FUNC(marriage)
  9920. {
  9921. const char *partner=script_getstr(st,2);
  9922. TBL_PC *sd=script_rid2sd(st);
  9923. TBL_PC *p_sd=map_nick2sd(partner);
  9924. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9925. script_pushint(st,0);
  9926. return 0;
  9927. }
  9928. script_pushint(st,1);
  9929. return 0;
  9930. }
  9931. BUILDIN_FUNC(wedding_effect)
  9932. {
  9933. TBL_PC *sd=script_rid2sd(st);
  9934. struct block_list *bl;
  9935. if(sd==NULL) {
  9936. bl=map_id2bl(st->oid);
  9937. } else
  9938. bl=&sd->bl;
  9939. clif_wedding_effect(bl);
  9940. return 0;
  9941. }
  9942. BUILDIN_FUNC(divorce)
  9943. {
  9944. TBL_PC *sd=script_rid2sd(st);
  9945. if(sd==NULL || pc_divorce(sd) < 0){
  9946. script_pushint(st,0);
  9947. return 0;
  9948. }
  9949. script_pushint(st,1);
  9950. return 0;
  9951. }
  9952. BUILDIN_FUNC(ispartneron)
  9953. {
  9954. TBL_PC *sd=script_rid2sd(st);
  9955. if(sd==NULL || !pc_ismarried(sd) ||
  9956. map_charid2sd(sd->status.partner_id) == NULL) {
  9957. script_pushint(st,0);
  9958. return 0;
  9959. }
  9960. script_pushint(st,1);
  9961. return 0;
  9962. }
  9963. BUILDIN_FUNC(getpartnerid)
  9964. {
  9965. TBL_PC *sd=script_rid2sd(st);
  9966. if (sd == NULL) {
  9967. script_pushint(st,0);
  9968. return 0;
  9969. }
  9970. script_pushint(st,sd->status.partner_id);
  9971. return 0;
  9972. }
  9973. BUILDIN_FUNC(getchildid)
  9974. {
  9975. TBL_PC *sd=script_rid2sd(st);
  9976. if (sd == NULL) {
  9977. script_pushint(st,0);
  9978. return 0;
  9979. }
  9980. script_pushint(st,sd->status.child);
  9981. return 0;
  9982. }
  9983. BUILDIN_FUNC(getmotherid)
  9984. {
  9985. TBL_PC *sd=script_rid2sd(st);
  9986. if (sd == NULL) {
  9987. script_pushint(st,0);
  9988. return 0;
  9989. }
  9990. script_pushint(st,sd->status.mother);
  9991. return 0;
  9992. }
  9993. BUILDIN_FUNC(getfatherid)
  9994. {
  9995. TBL_PC *sd=script_rid2sd(st);
  9996. if (sd == NULL) {
  9997. script_pushint(st,0);
  9998. return 0;
  9999. }
  10000. script_pushint(st,sd->status.father);
  10001. return 0;
  10002. }
  10003. BUILDIN_FUNC(warppartner)
  10004. {
  10005. int x,y;
  10006. unsigned short mapindex;
  10007. const char *str;
  10008. TBL_PC *sd=script_rid2sd(st);
  10009. TBL_PC *p_sd=NULL;
  10010. if(sd==NULL || !pc_ismarried(sd) ||
  10011. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  10012. script_pushint(st,0);
  10013. return 0;
  10014. }
  10015. str=script_getstr(st,2);
  10016. x=script_getnum(st,3);
  10017. y=script_getnum(st,4);
  10018. mapindex = mapindex_name2id(str);
  10019. if (mapindex) {
  10020. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  10021. script_pushint(st,1);
  10022. } else
  10023. script_pushint(st,0);
  10024. return 0;
  10025. }
  10026. /*================================================
  10027. * Script for Displaying MOB Information [Valaris]
  10028. *------------------------------------------------*/
  10029. BUILDIN_FUNC(strmobinfo)
  10030. {
  10031. int num=script_getnum(st,2);
  10032. int class_=script_getnum(st,3);
  10033. if(!mobdb_checkid(class_))
  10034. {
  10035. script_pushint(st,0);
  10036. return 0;
  10037. }
  10038. switch (num) {
  10039. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  10040. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  10041. case 3: script_pushint(st,mob_db(class_)->lv); break;
  10042. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  10043. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  10044. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  10045. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  10046. default:
  10047. script_pushint(st,0);
  10048. break;
  10049. }
  10050. return 0;
  10051. }
  10052. /*==========================================
  10053. * Summon guardians [Valaris]
  10054. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  10055. *------------------------------------------*/
  10056. BUILDIN_FUNC(guardian)
  10057. {
  10058. int class_=0,x=0,y=0,guardian=0;
  10059. const char *str,*map,*evt="";
  10060. struct script_data *data;
  10061. bool has_index = false;
  10062. map =script_getstr(st,2);
  10063. x =script_getnum(st,3);
  10064. y =script_getnum(st,4);
  10065. str =script_getstr(st,5);
  10066. class_=script_getnum(st,6);
  10067. if( script_hasdata(st,8) )
  10068. {// "<event label>",<guardian index>
  10069. evt=script_getstr(st,7);
  10070. guardian=script_getnum(st,8);
  10071. has_index = true;
  10072. } else if( script_hasdata(st,7) ){
  10073. data=script_getdata(st,7);
  10074. get_val(st,data);
  10075. if( data_isstring(data) )
  10076. {// "<event label>"
  10077. evt=script_getstr(st,7);
  10078. } else if( data_isint(data) )
  10079. {// <guardian index>
  10080. guardian=script_getnum(st,7);
  10081. has_index = true;
  10082. } else {
  10083. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  10084. script_reportdata(data);
  10085. return 1;
  10086. }
  10087. }
  10088. check_event(st, evt);
  10089. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  10090. return 0;
  10091. }
  10092. /*==========================================
  10093. * Invisible Walls [Zephyrus]
  10094. *------------------------------------------*/
  10095. BUILDIN_FUNC(setwall)
  10096. {
  10097. const char *map, *name;
  10098. int x, y, m, size, dir;
  10099. bool shootable;
  10100. map = script_getstr(st,2);
  10101. x = script_getnum(st,3);
  10102. y = script_getnum(st,4);
  10103. size = script_getnum(st,5);
  10104. dir = script_getnum(st,6);
  10105. shootable = script_getnum(st,7);
  10106. name = script_getstr(st,8);
  10107. if( (m = map_mapname2mapid(map)) < 0 )
  10108. return 0; // Invalid Map
  10109. map_iwall_set(m, x, y, size, dir, shootable, name);
  10110. return 0;
  10111. }
  10112. BUILDIN_FUNC(delwall)
  10113. {
  10114. const char *name = script_getstr(st,2);
  10115. map_iwall_remove(name);
  10116. return 0;
  10117. }
  10118. /// Retrieves various information about the specified guardian.
  10119. ///
  10120. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  10121. /// type: 0 - whether it is deployed or not
  10122. /// 1 - maximum hp
  10123. /// 2 - current hp
  10124. ///
  10125. BUILDIN_FUNC(guardianinfo)
  10126. {
  10127. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10128. int id = script_getnum(st,3);
  10129. int type = script_getnum(st,4);
  10130. struct guild_castle* gc = guild_mapname2gc(mapname);
  10131. struct mob_data* gd;
  10132. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  10133. {
  10134. script_pushint(st,-1);
  10135. return 0;
  10136. }
  10137. if( type == 0 )
  10138. script_pushint(st, gc->guardian[id].visible);
  10139. else
  10140. if( !gc->guardian[id].visible )
  10141. script_pushint(st,-1);
  10142. else
  10143. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  10144. script_pushint(st,-1);
  10145. else
  10146. {
  10147. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  10148. else if( type == 2 ) script_pushint(st,gd->status.hp);
  10149. else
  10150. script_pushint(st,-1);
  10151. }
  10152. return 0;
  10153. }
  10154. /*==========================================
  10155. * IDからItem名
  10156. *------------------------------------------*/
  10157. BUILDIN_FUNC(getitemname)
  10158. {
  10159. int item_id=0;
  10160. struct item_data *i_data;
  10161. char *item_name;
  10162. struct script_data *data;
  10163. data=script_getdata(st,2);
  10164. get_val(st,data);
  10165. if( data_isstring(data) ){
  10166. const char *name=conv_str(st,data);
  10167. struct item_data *item_data = itemdb_searchname(name);
  10168. if( item_data )
  10169. item_id=item_data->nameid;
  10170. }else
  10171. item_id=conv_num(st,data);
  10172. i_data = itemdb_exists(item_id);
  10173. if (i_data == NULL)
  10174. {
  10175. script_pushconststr(st,"null");
  10176. return 0;
  10177. }
  10178. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  10179. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  10180. script_pushstr(st,item_name);
  10181. return 0;
  10182. }
  10183. /*==========================================
  10184. * Returns number of slots an item has. [Skotlex]
  10185. *------------------------------------------*/
  10186. BUILDIN_FUNC(getitemslots)
  10187. {
  10188. int item_id;
  10189. struct item_data *i_data;
  10190. item_id=script_getnum(st,2);
  10191. i_data = itemdb_exists(item_id);
  10192. if (i_data)
  10193. script_pushint(st,i_data->slot);
  10194. else
  10195. script_pushint(st,-1);
  10196. return 0;
  10197. }
  10198. // TODO: add matk here if needed/once we get rid of RENEWAL
  10199. /*==========================================
  10200. * Returns some values of an item [Lupus]
  10201. * Price, Weight, etc...
  10202. getiteminfo(itemID,n), where n
  10203. 0 value_buy;
  10204. 1 value_sell;
  10205. 2 type;
  10206. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10207. if = 0, then monsters don't drop it at all (rare or a quest item)
  10208. if = -1, then this item is sold in NPC shops only
  10209. 4 sex;
  10210. 5 equip;
  10211. 6 weight;
  10212. 7 atk;
  10213. 8 def;
  10214. 9 range;
  10215. 10 slot;
  10216. 11 look;
  10217. 12 elv;
  10218. 13 wlv;
  10219. 14 view id
  10220. *------------------------------------------*/
  10221. BUILDIN_FUNC(getiteminfo)
  10222. {
  10223. int item_id,n;
  10224. int *item_arr;
  10225. struct item_data *i_data;
  10226. item_id = script_getnum(st,2);
  10227. n = script_getnum(st,3);
  10228. i_data = itemdb_exists(item_id);
  10229. if (i_data && n>=0 && n<=14) {
  10230. item_arr = (int*)&i_data->value_buy;
  10231. script_pushint(st,item_arr[n]);
  10232. } else
  10233. script_pushint(st,-1);
  10234. return 0;
  10235. }
  10236. /*==========================================
  10237. * Set some values of an item [Lupus]
  10238. * Price, Weight, etc...
  10239. setiteminfo(itemID,n,Value), where n
  10240. 0 value_buy;
  10241. 1 value_sell;
  10242. 2 type;
  10243. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10244. if = 0, then monsters don't drop it at all (rare or a quest item)
  10245. if = -1, then this item is sold in NPC shops only
  10246. 4 sex;
  10247. 5 equip;
  10248. 6 weight;
  10249. 7 atk;
  10250. 8 def;
  10251. 9 range;
  10252. 10 slot;
  10253. 11 look;
  10254. 12 elv;
  10255. 13 wlv;
  10256. 14 view id
  10257. * Returns Value or -1 if the wrong field's been set
  10258. *------------------------------------------*/
  10259. BUILDIN_FUNC(setiteminfo)
  10260. {
  10261. int item_id,n,value;
  10262. int *item_arr;
  10263. struct item_data *i_data;
  10264. item_id = script_getnum(st,2);
  10265. n = script_getnum(st,3);
  10266. value = script_getnum(st,4);
  10267. i_data = itemdb_exists(item_id);
  10268. if (i_data && n>=0 && n<=14) {
  10269. item_arr = (int*)&i_data->value_buy;
  10270. item_arr[n] = value;
  10271. script_pushint(st,value);
  10272. } else
  10273. script_pushint(st,-1);
  10274. return 0;
  10275. }
  10276. /*==========================================
  10277. * Returns value from equipped item slot n [Lupus]
  10278. getequipcardid(num,slot)
  10279. where
  10280. num = eqip position slot
  10281. slot = 0,1,2,3 (Card Slot N)
  10282. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  10283. it's useful when you want to check item cards or if it's signed
  10284. Useful for such quests as "Sign this refined item with players name" etc
  10285. Hat[0] +4 -> Player's Hat[0] +4
  10286. *------------------------------------------*/
  10287. BUILDIN_FUNC(getequipcardid)
  10288. {
  10289. int i=-1,num,slot;
  10290. TBL_PC *sd;
  10291. num=script_getnum(st,2);
  10292. slot=script_getnum(st,3);
  10293. sd=script_rid2sd(st);
  10294. if (num > 0 && num <= ARRAYLENGTH(equip))
  10295. i=pc_checkequip(sd,equip[num-1]);
  10296. if(i >= 0 && slot>=0 && slot<4)
  10297. script_pushint(st,sd->status.inventory[i].card[slot]);
  10298. else
  10299. script_pushint(st,0);
  10300. return 0;
  10301. }
  10302. /*==========================================
  10303. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  10304. *------------------------------------------*/
  10305. BUILDIN_FUNC(petskillbonus)
  10306. {
  10307. struct pet_data *pd;
  10308. TBL_PC *sd=script_rid2sd(st);
  10309. if(sd==NULL || sd->pd==NULL)
  10310. return 0;
  10311. pd=sd->pd;
  10312. if (pd->bonus)
  10313. { //Clear previous bonus
  10314. if (pd->bonus->timer != INVALID_TIMER)
  10315. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  10316. } else //init
  10317. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  10318. pd->bonus->type=script_getnum(st,2);
  10319. pd->bonus->val=script_getnum(st,3);
  10320. pd->bonus->duration=script_getnum(st,4);
  10321. pd->bonus->delay=script_getnum(st,5);
  10322. if (pd->state.skillbonus == 1)
  10323. pd->state.skillbonus=0; // waiting state
  10324. // wait for timer to start
  10325. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10326. pd->bonus->timer = INVALID_TIMER;
  10327. else
  10328. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  10329. return 0;
  10330. }
  10331. /*==========================================
  10332. * pet looting [Valaris] //Rewritten by [Skotlex]
  10333. *------------------------------------------*/
  10334. BUILDIN_FUNC(petloot)
  10335. {
  10336. int max;
  10337. struct pet_data *pd;
  10338. TBL_PC *sd=script_rid2sd(st);
  10339. if(sd==NULL || sd->pd==NULL)
  10340. return 0;
  10341. max=script_getnum(st,2);
  10342. if(max < 1)
  10343. max = 1; //Let'em loot at least 1 item.
  10344. else if (max > MAX_PETLOOT_SIZE)
  10345. max = MAX_PETLOOT_SIZE;
  10346. pd = sd->pd;
  10347. if (pd->loot != NULL)
  10348. { //Release whatever was there already and reallocate memory
  10349. pet_lootitem_drop(pd, pd->msd);
  10350. aFree(pd->loot->item);
  10351. }
  10352. else
  10353. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  10354. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  10355. pd->loot->max=max;
  10356. pd->loot->count = 0;
  10357. pd->loot->weight = 0;
  10358. return 0;
  10359. }
  10360. /*==========================================
  10361. * PCの所持品情報読み取り
  10362. *------------------------------------------*/
  10363. BUILDIN_FUNC(getinventorylist)
  10364. {
  10365. TBL_PC *sd=script_rid2sd(st);
  10366. char card_var[NAME_LENGTH];
  10367. int i,j=0,k;
  10368. if(!sd) return 0;
  10369. for(i=0;i<MAX_INVENTORY;i++){
  10370. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  10371. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  10372. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  10373. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  10374. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  10375. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  10376. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  10377. for (k = 0; k < MAX_SLOTS; k++)
  10378. {
  10379. sprintf(card_var, "@inventorylist_card%d",k+1);
  10380. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  10381. }
  10382. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  10383. j++;
  10384. }
  10385. }
  10386. pc_setreg(sd,add_str("@inventorylist_count"),j);
  10387. return 0;
  10388. }
  10389. BUILDIN_FUNC(getskilllist)
  10390. {
  10391. TBL_PC *sd=script_rid2sd(st);
  10392. int i,j=0;
  10393. if(!sd) return 0;
  10394. for(i=0;i<MAX_SKILL;i++){
  10395. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  10396. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  10397. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  10398. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  10399. j++;
  10400. }
  10401. }
  10402. pc_setreg(sd,add_str("@skilllist_count"),j);
  10403. return 0;
  10404. }
  10405. BUILDIN_FUNC(clearitem)
  10406. {
  10407. TBL_PC *sd=script_rid2sd(st);
  10408. int i;
  10409. if(sd==NULL) return 0;
  10410. for (i=0; i<MAX_INVENTORY; i++) {
  10411. if (sd->status.inventory[i].amount) {
  10412. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  10413. }
  10414. }
  10415. return 0;
  10416. }
  10417. /*==========================================
  10418. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  10419. *------------------------------------------*/
  10420. BUILDIN_FUNC(disguise)
  10421. {
  10422. int id;
  10423. TBL_PC* sd = script_rid2sd(st);
  10424. if (sd == NULL) return 0;
  10425. id = script_getnum(st,2);
  10426. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  10427. pc_disguise(sd, id);
  10428. script_pushint(st,id);
  10429. } else
  10430. script_pushint(st,0);
  10431. return 0;
  10432. }
  10433. /*==========================================
  10434. * Undisguise Player (returns 1 if success, 0 on fail)
  10435. *------------------------------------------*/
  10436. BUILDIN_FUNC(undisguise)
  10437. {
  10438. TBL_PC* sd = script_rid2sd(st);
  10439. if (sd == NULL) return 0;
  10440. if (sd->disguise) {
  10441. pc_disguise(sd, 0);
  10442. script_pushint(st,0);
  10443. } else {
  10444. script_pushint(st,1);
  10445. }
  10446. return 0;
  10447. }
  10448. /*==========================================
  10449. * NPCクラスチェンジ
  10450. * classは変わりたいclass
  10451. * typeは通常0なのかな?
  10452. *------------------------------------------*/
  10453. BUILDIN_FUNC(classchange)
  10454. {
  10455. int _class,type;
  10456. struct block_list *bl=map_id2bl(st->oid);
  10457. if(bl==NULL) return 0;
  10458. _class=script_getnum(st,2);
  10459. type=script_getnum(st,3);
  10460. clif_class_change(bl,_class,type);
  10461. return 0;
  10462. }
  10463. /*==========================================
  10464. * NPCから発生するエフェクト
  10465. *------------------------------------------*/
  10466. BUILDIN_FUNC(misceffect)
  10467. {
  10468. int type;
  10469. type=script_getnum(st,2);
  10470. if(st->oid && st->oid != fake_nd->bl.id) {
  10471. struct block_list *bl = map_id2bl(st->oid);
  10472. if (bl)
  10473. clif_specialeffect(bl,type,AREA);
  10474. } else{
  10475. TBL_PC *sd=script_rid2sd(st);
  10476. if(sd)
  10477. clif_specialeffect(&sd->bl,type,AREA);
  10478. }
  10479. return 0;
  10480. }
  10481. /*==========================================
  10482. * Play a BGM on a single client [Rikter/Yommy]
  10483. *------------------------------------------*/
  10484. BUILDIN_FUNC(playBGM)
  10485. {
  10486. const char* name;
  10487. struct map_session_data* sd;
  10488. if( ( sd = script_rid2sd(st) ) != NULL )
  10489. {
  10490. name = script_getstr(st,2);
  10491. clif_playBGM(sd, name);
  10492. }
  10493. return 0;
  10494. }
  10495. static int playBGM_sub(struct block_list* bl,va_list ap)
  10496. {
  10497. const char* name = va_arg(ap,const char*);
  10498. clif_playBGM(BL_CAST(BL_PC, bl), name);
  10499. return 0;
  10500. }
  10501. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  10502. {
  10503. const char* name = va_arg(args, const char*);
  10504. clif_playBGM(sd, name);
  10505. return 0;
  10506. }
  10507. /*==========================================
  10508. * Play a BGM on multiple client [Rikter/Yommy]
  10509. *------------------------------------------*/
  10510. BUILDIN_FUNC(playBGMall)
  10511. {
  10512. const char* name;
  10513. name = script_getstr(st,2);
  10514. if( script_hasdata(st,7) )
  10515. {// specified part of map
  10516. const char* map = script_getstr(st,3);
  10517. int x0 = script_getnum(st,4);
  10518. int y0 = script_getnum(st,5);
  10519. int x1 = script_getnum(st,6);
  10520. int y1 = script_getnum(st,7);
  10521. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  10522. }
  10523. else if( script_hasdata(st,3) )
  10524. {// entire map
  10525. const char* map = script_getstr(st,3);
  10526. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  10527. }
  10528. else
  10529. {// entire server
  10530. map_foreachpc(&playBGM_foreachpc_sub, name);
  10531. }
  10532. return 0;
  10533. }
  10534. /*==========================================
  10535. * サウンドエフェクト
  10536. *------------------------------------------*/
  10537. BUILDIN_FUNC(soundeffect)
  10538. {
  10539. TBL_PC* sd = script_rid2sd(st);
  10540. const char* name = script_getstr(st,2);
  10541. int type = script_getnum(st,3);
  10542. if(sd)
  10543. {
  10544. clif_soundeffect(sd,&sd->bl,name,type);
  10545. }
  10546. return 0;
  10547. }
  10548. int soundeffect_sub(struct block_list* bl,va_list ap)
  10549. {
  10550. char* name = va_arg(ap,char*);
  10551. int type = va_arg(ap,int);
  10552. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  10553. return 0;
  10554. }
  10555. /*==========================================
  10556. * Play a sound effect (.wav) on multiple clients
  10557. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  10558. *------------------------------------------*/
  10559. BUILDIN_FUNC(soundeffectall)
  10560. {
  10561. struct block_list* bl;
  10562. const char* name;
  10563. int type;
  10564. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  10565. if (!bl)
  10566. return 0;
  10567. name = script_getstr(st,2);
  10568. type = script_getnum(st,3);
  10569. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  10570. if(!script_hasdata(st,4))
  10571. { // area around
  10572. clif_soundeffectall(bl, name, type, AREA);
  10573. }
  10574. else
  10575. if(!script_hasdata(st,5))
  10576. { // entire map
  10577. const char* map = script_getstr(st,4);
  10578. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  10579. }
  10580. else
  10581. if(script_hasdata(st,8))
  10582. { // specified part of map
  10583. const char* map = script_getstr(st,4);
  10584. int x0 = script_getnum(st,5);
  10585. int y0 = script_getnum(st,6);
  10586. int x1 = script_getnum(st,7);
  10587. int y1 = script_getnum(st,8);
  10588. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  10589. }
  10590. else
  10591. {
  10592. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  10593. }
  10594. return 0;
  10595. }
  10596. /*==========================================
  10597. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  10598. *------------------------------------------*/
  10599. BUILDIN_FUNC(petrecovery)
  10600. {
  10601. struct pet_data *pd;
  10602. TBL_PC *sd=script_rid2sd(st);
  10603. if(sd==NULL || sd->pd==NULL)
  10604. return 0;
  10605. pd=sd->pd;
  10606. if (pd->recovery)
  10607. { //Halt previous bonus
  10608. if (pd->recovery->timer != INVALID_TIMER)
  10609. delete_timer(pd->recovery->timer, pet_recovery_timer);
  10610. } else //Init
  10611. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  10612. pd->recovery->type = (sc_type)script_getnum(st,2);
  10613. pd->recovery->delay = script_getnum(st,3);
  10614. pd->recovery->timer = INVALID_TIMER;
  10615. return 0;
  10616. }
  10617. /*==========================================
  10618. * pet healing [Valaris] //Rewritten by [Skotlex]
  10619. *------------------------------------------*/
  10620. BUILDIN_FUNC(petheal)
  10621. {
  10622. struct pet_data *pd;
  10623. TBL_PC *sd=script_rid2sd(st);
  10624. if(sd==NULL || sd->pd==NULL)
  10625. return 0;
  10626. pd=sd->pd;
  10627. if (pd->s_skill)
  10628. { //Clear previous skill
  10629. if (pd->s_skill->timer != INVALID_TIMER)
  10630. {
  10631. if (pd->s_skill->id)
  10632. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10633. else
  10634. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10635. }
  10636. } else //init memory
  10637. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10638. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  10639. //Use the lv as the amount to heal
  10640. pd->s_skill->lv=script_getnum(st,2);
  10641. pd->s_skill->delay=script_getnum(st,3);
  10642. pd->s_skill->hp=script_getnum(st,4);
  10643. pd->s_skill->sp=script_getnum(st,5);
  10644. //Use delay as initial offset to avoid skill/heal exploits
  10645. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10646. pd->s_skill->timer = INVALID_TIMER;
  10647. else
  10648. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  10649. return 0;
  10650. }
  10651. /*==========================================
  10652. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  10653. *------------------------------------------*/
  10654. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  10655. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  10656. BUILDIN_FUNC(petskillattack)
  10657. {
  10658. struct pet_data *pd;
  10659. TBL_PC *sd=script_rid2sd(st);
  10660. if(sd==NULL || sd->pd==NULL)
  10661. return 0;
  10662. pd=sd->pd;
  10663. if (pd->a_skill == NULL)
  10664. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10665. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10666. pd->a_skill->lv=script_getnum(st,3);
  10667. pd->a_skill->div_ = 0;
  10668. pd->a_skill->rate=script_getnum(st,4);
  10669. pd->a_skill->bonusrate=script_getnum(st,5);
  10670. return 0;
  10671. }
  10672. /*==========================================
  10673. * pet attack skills [Valaris]
  10674. *------------------------------------------*/
  10675. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  10676. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  10677. BUILDIN_FUNC(petskillattack2)
  10678. {
  10679. struct pet_data *pd;
  10680. TBL_PC *sd=script_rid2sd(st);
  10681. if(sd==NULL || sd->pd==NULL)
  10682. return 0;
  10683. pd=sd->pd;
  10684. if (pd->a_skill == NULL)
  10685. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10686. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10687. pd->a_skill->lv=script_getnum(st,3);
  10688. pd->a_skill->div_ = script_getnum(st,4);
  10689. pd->a_skill->rate=script_getnum(st,5);
  10690. pd->a_skill->bonusrate=script_getnum(st,6);
  10691. return 0;
  10692. }
  10693. /*==========================================
  10694. * pet support skills [Skotlex]
  10695. *------------------------------------------*/
  10696. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  10697. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  10698. BUILDIN_FUNC(petskillsupport)
  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->s_skill)
  10706. { //Clear previous skill
  10707. if (pd->s_skill->timer != INVALID_TIMER)
  10708. {
  10709. if (pd->s_skill->id)
  10710. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10711. else
  10712. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10713. }
  10714. } else //init memory
  10715. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10716. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10717. pd->s_skill->lv=script_getnum(st,3);
  10718. pd->s_skill->delay=script_getnum(st,4);
  10719. pd->s_skill->hp=script_getnum(st,5);
  10720. pd->s_skill->sp=script_getnum(st,6);
  10721. //Use delay as initial offset to avoid skill/heal exploits
  10722. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10723. pd->s_skill->timer = INVALID_TIMER;
  10724. else
  10725. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  10726. return 0;
  10727. }
  10728. /*==========================================
  10729. * Scripted skill effects [Celest]
  10730. *------------------------------------------*/
  10731. /// skilleffect <skill id>,<level>
  10732. /// skilleffect "<skill name>",<level>
  10733. BUILDIN_FUNC(skilleffect)
  10734. {
  10735. TBL_PC *sd;
  10736. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10737. int skilllv=script_getnum(st,3);
  10738. sd=script_rid2sd(st);
  10739. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  10740. return 0;
  10741. }
  10742. /*==========================================
  10743. * NPC skill effects [Valaris]
  10744. *------------------------------------------*/
  10745. /// npcskilleffect <skill id>,<level>,<x>,<y>
  10746. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  10747. BUILDIN_FUNC(npcskilleffect)
  10748. {
  10749. struct block_list *bl= map_id2bl(st->oid);
  10750. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10751. int skilllv=script_getnum(st,3);
  10752. int x=script_getnum(st,4);
  10753. int y=script_getnum(st,5);
  10754. if (bl)
  10755. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  10756. return 0;
  10757. }
  10758. /*==========================================
  10759. * Special effects [Valaris]
  10760. *------------------------------------------*/
  10761. BUILDIN_FUNC(specialeffect)
  10762. {
  10763. struct block_list *bl=map_id2bl(st->oid);
  10764. int type = script_getnum(st,2);
  10765. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10766. if(bl==NULL)
  10767. return 0;
  10768. if( script_hasdata(st,4) )
  10769. {
  10770. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10771. if(nd)
  10772. clif_specialeffect(&nd->bl, type, target);
  10773. }
  10774. else
  10775. {
  10776. if (target == SELF) {
  10777. TBL_PC *sd=script_rid2sd(st);
  10778. if (sd)
  10779. clif_specialeffect_single(bl,type,sd->fd);
  10780. } else {
  10781. clif_specialeffect(bl, type, target);
  10782. }
  10783. }
  10784. return 0;
  10785. }
  10786. BUILDIN_FUNC(specialeffect2)
  10787. {
  10788. TBL_PC *sd=script_rid2sd(st);
  10789. int type = script_getnum(st,2);
  10790. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10791. if( script_hasdata(st,4) )
  10792. sd = map_nick2sd(script_getstr(st,4));
  10793. if (sd)
  10794. clif_specialeffect(&sd->bl, type, target);
  10795. return 0;
  10796. }
  10797. /*==========================================
  10798. * Nude [Valaris]
  10799. *------------------------------------------*/
  10800. BUILDIN_FUNC(nude)
  10801. {
  10802. TBL_PC *sd = script_rid2sd(st);
  10803. int i, calcflag = 0;
  10804. if( sd == NULL )
  10805. return 0;
  10806. for( i = 0 ; i < EQI_MAX; i++ ) {
  10807. if( sd->equip_index[ i ] >= 0 ) {
  10808. if( !calcflag )
  10809. calcflag = 1;
  10810. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  10811. }
  10812. }
  10813. if( calcflag )
  10814. status_calc_pc(sd,0);
  10815. return 0;
  10816. }
  10817. /*==========================================
  10818. * gmcommand [MouseJstr]
  10819. *------------------------------------------*/
  10820. BUILDIN_FUNC(atcommand)
  10821. {
  10822. TBL_PC dummy_sd;
  10823. TBL_PC* sd;
  10824. int fd;
  10825. const char* cmd;
  10826. cmd = script_getstr(st,2);
  10827. if (st->rid) {
  10828. sd = script_rid2sd(st);
  10829. fd = sd->fd;
  10830. } else { //Use a dummy character.
  10831. sd = &dummy_sd;
  10832. fd = 0;
  10833. memset(&dummy_sd, 0, sizeof(TBL_PC));
  10834. if (st->oid)
  10835. {
  10836. struct block_list* bl = map_id2bl(st->oid);
  10837. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  10838. if (bl->type == BL_NPC)
  10839. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  10840. }
  10841. }
  10842. if (!is_atcommand(fd, sd, cmd, 0)) {
  10843. ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd);
  10844. script_reportsrc(st);
  10845. return 1;
  10846. }
  10847. return 0;
  10848. }
  10849. /*==========================================
  10850. * Displays a message for the player only (like system messages like "you got an apple" )
  10851. *------------------------------------------*/
  10852. BUILDIN_FUNC(dispbottom)
  10853. {
  10854. TBL_PC *sd=script_rid2sd(st);
  10855. const char *message;
  10856. message=script_getstr(st,2);
  10857. if(sd)
  10858. clif_disp_onlyself(sd,message,(int)strlen(message));
  10859. return 0;
  10860. }
  10861. /*==========================================
  10862. * All The Players Full Recovery
  10863. * (HP/SP full restore and resurrect if need)
  10864. *------------------------------------------*/
  10865. BUILDIN_FUNC(recovery)
  10866. {
  10867. TBL_PC* sd;
  10868. struct s_mapiterator* iter;
  10869. iter = mapit_getallusers();
  10870. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10871. {
  10872. if(pc_isdead(sd))
  10873. status_revive(&sd->bl, 100, 100);
  10874. else
  10875. status_percent_heal(&sd->bl, 100, 100);
  10876. clif_displaymessage(sd->fd,"You have been recovered!");
  10877. }
  10878. mapit_free(iter);
  10879. return 0;
  10880. }
  10881. /*==========================================
  10882. * Get your pet info: getpetinfo(n)
  10883. * n -> 0:pet_id 1:pet_class 2:pet_name
  10884. * 3:friendly 4:hungry, 5: rename flag.
  10885. *------------------------------------------*/
  10886. BUILDIN_FUNC(getpetinfo)
  10887. {
  10888. TBL_PC *sd=script_rid2sd(st);
  10889. TBL_PET *pd;
  10890. int type=script_getnum(st,2);
  10891. if(!sd || !sd->pd) {
  10892. if (type == 2)
  10893. script_pushconststr(st,"null");
  10894. else
  10895. script_pushint(st,0);
  10896. return 0;
  10897. }
  10898. pd = sd->pd;
  10899. switch(type){
  10900. case 0: script_pushint(st,pd->pet.pet_id); break;
  10901. case 1: script_pushint(st,pd->pet.class_); break;
  10902. case 2: script_pushstrcopy(st,pd->pet.name); break;
  10903. case 3: script_pushint(st,pd->pet.intimate); break;
  10904. case 4: script_pushint(st,pd->pet.hungry); break;
  10905. case 5: script_pushint(st,pd->pet.rename_flag); break;
  10906. default:
  10907. script_pushint(st,0);
  10908. break;
  10909. }
  10910. return 0;
  10911. }
  10912. /*==========================================
  10913. * Get your homunculus info: gethominfo(n)
  10914. * n -> 0:hom_id 1:class 2:name
  10915. * 3:friendly 4:hungry, 5: rename flag.
  10916. * 6: level
  10917. *------------------------------------------*/
  10918. BUILDIN_FUNC(gethominfo)
  10919. {
  10920. TBL_PC *sd=script_rid2sd(st);
  10921. TBL_HOM *hd;
  10922. int type=script_getnum(st,2);
  10923. hd = sd?sd->hd:NULL;
  10924. if(!merc_is_hom_active(hd))
  10925. {
  10926. if (type == 2)
  10927. script_pushconststr(st,"null");
  10928. else
  10929. script_pushint(st,0);
  10930. return 0;
  10931. }
  10932. switch(type){
  10933. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  10934. case 1: script_pushint(st,hd->homunculus.class_); break;
  10935. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  10936. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10937. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10938. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10939. case 6: script_pushint(st,hd->homunculus.level); break;
  10940. default:
  10941. script_pushint(st,0);
  10942. break;
  10943. }
  10944. return 0;
  10945. }
  10946. /// Retrieves information about character's mercenary
  10947. /// getmercinfo <type>[,<char id>];
  10948. BUILDIN_FUNC(getmercinfo)
  10949. {
  10950. int type, char_id;
  10951. struct map_session_data* sd;
  10952. struct mercenary_data* md;
  10953. type = script_getnum(st,2);
  10954. if( script_hasdata(st,3) )
  10955. {
  10956. char_id = script_getnum(st,3);
  10957. if( ( sd = map_charid2sd(char_id) ) == NULL )
  10958. {
  10959. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  10960. script_pushnil(st);
  10961. return 1;
  10962. }
  10963. }
  10964. else
  10965. {
  10966. if( ( sd = script_rid2sd(st) ) == NULL )
  10967. {
  10968. script_pushnil(st);
  10969. return 0;
  10970. }
  10971. }
  10972. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  10973. switch( type )
  10974. {
  10975. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  10976. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  10977. case 2:
  10978. if( md )
  10979. script_pushstrcopy(st,md->db->name);
  10980. else
  10981. script_pushconststr(st,"");
  10982. break;
  10983. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  10984. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  10985. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  10986. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  10987. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  10988. default:
  10989. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  10990. script_pushnil(st);
  10991. return 1;
  10992. }
  10993. return 0;
  10994. }
  10995. /*==========================================
  10996. * Shows wether your inventory(and equips) contain
  10997. selected card or not.
  10998. checkequipedcard(4001);
  10999. *------------------------------------------*/
  11000. BUILDIN_FUNC(checkequipedcard)
  11001. {
  11002. TBL_PC *sd=script_rid2sd(st);
  11003. if(sd){
  11004. int n,i,c=0;
  11005. c=script_getnum(st,2);
  11006. for(i=0;i<MAX_INVENTORY;i++){
  11007. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  11008. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  11009. continue;
  11010. for(n=0;n<sd->inventory_data[i]->slot;n++){
  11011. if(sd->status.inventory[i].card[n]==c){
  11012. script_pushint(st,1);
  11013. return 0;
  11014. }
  11015. }
  11016. }
  11017. }
  11018. }
  11019. script_pushint(st,0);
  11020. return 0;
  11021. }
  11022. BUILDIN_FUNC(jump_zero)
  11023. {
  11024. int sel;
  11025. sel=script_getnum(st,2);
  11026. if(!sel) {
  11027. int pos;
  11028. if( !data_islabel(script_getdata(st,3)) ){
  11029. ShowError("script: jump_zero: not label !\n");
  11030. st->state=END;
  11031. return 1;
  11032. }
  11033. pos=script_getnum(st,3);
  11034. st->pos=pos;
  11035. st->state=GOTO;
  11036. }
  11037. return 0;
  11038. }
  11039. /*==========================================
  11040. * movenpc [MouseJstr]
  11041. *------------------------------------------*/
  11042. BUILDIN_FUNC(movenpc)
  11043. {
  11044. TBL_NPC *nd = NULL;
  11045. const char *npc;
  11046. int x,y;
  11047. npc = script_getstr(st,2);
  11048. x = script_getnum(st,3);
  11049. y = script_getnum(st,4);
  11050. if ((nd = npc_name2id(npc)) == NULL)
  11051. return -1;
  11052. if (script_hasdata(st,5))
  11053. nd->ud.dir = script_getnum(st,5) % 8;
  11054. npc_movenpc(nd, x, y);
  11055. return 0;
  11056. }
  11057. /*==========================================
  11058. * message [MouseJstr]
  11059. *------------------------------------------*/
  11060. BUILDIN_FUNC(message)
  11061. {
  11062. const char *msg,*player;
  11063. TBL_PC *pl_sd = NULL;
  11064. player = script_getstr(st,2);
  11065. msg = script_getstr(st,3);
  11066. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  11067. return 0;
  11068. clif_displaymessage(pl_sd->fd, msg);
  11069. return 0;
  11070. }
  11071. /*==========================================
  11072. * npctalk (sends message to surrounding area)
  11073. *------------------------------------------*/
  11074. BUILDIN_FUNC(npctalk)
  11075. {
  11076. const char* str;
  11077. char name[NAME_LENGTH], message[256];
  11078. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  11079. str = script_getstr(st,2);
  11080. if(nd)
  11081. {
  11082. safestrncpy(name, nd->name, sizeof(name));
  11083. strtok(name, "#"); // discard extra name identifier if present
  11084. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  11085. clif_message(&nd->bl, message);
  11086. }
  11087. return 0;
  11088. }
  11089. // change npc walkspeed [Valaris]
  11090. BUILDIN_FUNC(npcspeed)
  11091. {
  11092. struct npc_data* nd;
  11093. int speed;
  11094. speed = script_getnum(st,2);
  11095. nd =(struct npc_data *)map_id2bl(st->oid);
  11096. if( nd )
  11097. {
  11098. nd->speed = speed;
  11099. nd->ud.state.speed_changed = 1;
  11100. }
  11101. return 0;
  11102. }
  11103. // make an npc walk to a position [Valaris]
  11104. BUILDIN_FUNC(npcwalkto)
  11105. {
  11106. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11107. int x=0,y=0;
  11108. x=script_getnum(st,2);
  11109. y=script_getnum(st,3);
  11110. if(nd) {
  11111. unit_walktoxy(&nd->bl,x,y,0);
  11112. }
  11113. return 0;
  11114. }
  11115. // stop an npc's movement [Valaris]
  11116. BUILDIN_FUNC(npcstop)
  11117. {
  11118. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11119. if(nd) {
  11120. unit_stop_walking(&nd->bl,1|4);
  11121. }
  11122. return 0;
  11123. }
  11124. /*==========================================
  11125. * getlook char info. getlook(arg)
  11126. *------------------------------------------*/
  11127. BUILDIN_FUNC(getlook)
  11128. {
  11129. int type,val;
  11130. TBL_PC *sd;
  11131. sd=script_rid2sd(st);
  11132. type=script_getnum(st,2);
  11133. val=-1;
  11134. switch(type) {
  11135. case LOOK_HAIR: val=sd->status.hair; break; //1
  11136. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  11137. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  11138. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  11139. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  11140. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  11141. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  11142. case LOOK_SHIELD: val=sd->status.shield; break; //8
  11143. case LOOK_SHOES: break; //9
  11144. }
  11145. script_pushint(st,val);
  11146. return 0;
  11147. }
  11148. /*==========================================
  11149. * get char save point. argument: 0- map name, 1- x, 2- y
  11150. *------------------------------------------*/
  11151. BUILDIN_FUNC(getsavepoint)
  11152. {
  11153. TBL_PC* sd;
  11154. int type;
  11155. sd = script_rid2sd(st);
  11156. if (sd == NULL) {
  11157. script_pushint(st,0);
  11158. return 0;
  11159. }
  11160. type = script_getnum(st,2);
  11161. switch(type) {
  11162. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  11163. case 1: script_pushint(st,sd->status.save_point.x); break;
  11164. case 2: script_pushint(st,sd->status.save_point.y); break;
  11165. default:
  11166. script_pushint(st,0);
  11167. break;
  11168. }
  11169. return 0;
  11170. }
  11171. /*==========================================
  11172. * Get position for char/npc/pet/mob objects. Added by Lorky
  11173. *
  11174. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  11175. * where type:
  11176. * MapName$ - String variable for output map name
  11177. * MapX - Integer variable for output coord X
  11178. * MapY - Integer variable for output coord Y
  11179. * type - type of object
  11180. * 0 - Character coord
  11181. * 1 - NPC coord
  11182. * 2 - Pet coord
  11183. * 3 - Mob coord (not released)
  11184. * 4 - Homun coord
  11185. * 5 - Mercenary coord
  11186. * 6 - Elemental coord
  11187. * CharName$ - Name object. If miss or "this" the current object
  11188. *
  11189. * Return:
  11190. * 0 - success
  11191. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  11192. *------------------------------------------*/
  11193. BUILDIN_FUNC(getmapxy)
  11194. {
  11195. struct block_list *bl = NULL;
  11196. TBL_PC *sd=NULL;
  11197. int num;
  11198. const char *name;
  11199. char prefix;
  11200. int x,y,type;
  11201. char mapname[MAP_NAME_LENGTH];
  11202. if( !data_isreference(script_getdata(st,2)) ){
  11203. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  11204. script_pushint(st,-1);
  11205. return 1;
  11206. }
  11207. if( !data_isreference(script_getdata(st,3)) ){
  11208. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  11209. script_pushint(st,-1);
  11210. return 1;
  11211. }
  11212. if( !data_isreference(script_getdata(st,4)) ){
  11213. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  11214. script_pushint(st,-1);
  11215. return 1;
  11216. }
  11217. // Possible needly check function parameters on C_STR,C_INT,C_INT
  11218. type=script_getnum(st,5);
  11219. switch (type){
  11220. case 0: //Get Character Position
  11221. if( script_hasdata(st,6) )
  11222. sd=map_nick2sd(script_getstr(st,6));
  11223. else
  11224. sd=script_rid2sd(st);
  11225. if (sd)
  11226. bl = &sd->bl;
  11227. break;
  11228. case 1: //Get NPC Position
  11229. if( script_hasdata(st,6) )
  11230. {
  11231. struct npc_data *nd;
  11232. nd=npc_name2id(script_getstr(st,6));
  11233. if (nd)
  11234. bl = &nd->bl;
  11235. } else //In case the origin is not an npc?
  11236. bl=map_id2bl(st->oid);
  11237. break;
  11238. case 2: //Get Pet Position
  11239. if(script_hasdata(st,6))
  11240. sd=map_nick2sd(script_getstr(st,6));
  11241. else
  11242. sd=script_rid2sd(st);
  11243. if (sd && sd->pd)
  11244. bl = &sd->pd->bl;
  11245. break;
  11246. case 3: //Get Mob Position
  11247. break; //Not supported?
  11248. case 4: //Get Homun Position
  11249. if(script_hasdata(st,6))
  11250. sd=map_nick2sd(script_getstr(st,6));
  11251. else
  11252. sd=script_rid2sd(st);
  11253. if (sd && sd->hd)
  11254. bl = &sd->hd->bl;
  11255. break;
  11256. case 5: //Get Mercenary Position
  11257. if(script_hasdata(st,6))
  11258. sd=map_nick2sd(script_getstr(st,6));
  11259. else
  11260. sd=script_rid2sd(st);
  11261. if (sd && sd->md)
  11262. bl = &sd->md->bl;
  11263. break;
  11264. case 6: //Get Elemental Position
  11265. if(script_hasdata(st,6))
  11266. sd=map_nick2sd(script_getstr(st,6));
  11267. else
  11268. sd=script_rid2sd(st);
  11269. if (sd && sd->ed)
  11270. bl = &sd->ed->bl;
  11271. break;
  11272. default:
  11273. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  11274. script_pushint(st,-1);
  11275. return 1;
  11276. }
  11277. if (!bl) { //No object found.
  11278. script_pushint(st,-1);
  11279. return 0;
  11280. }
  11281. x= bl->x;
  11282. y= bl->y;
  11283. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  11284. //Set MapName$
  11285. num=st->stack->stack_data[st->start+2].u.num;
  11286. name=get_str(num&0x00ffffff);
  11287. prefix=*name;
  11288. if(not_server_variable(prefix))
  11289. sd=script_rid2sd(st);
  11290. else
  11291. sd=NULL;
  11292. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  11293. //Set MapX
  11294. num=st->stack->stack_data[st->start+3].u.num;
  11295. name=get_str(num&0x00ffffff);
  11296. prefix=*name;
  11297. if(not_server_variable(prefix))
  11298. sd=script_rid2sd(st);
  11299. else
  11300. sd=NULL;
  11301. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  11302. //Set MapY
  11303. num=st->stack->stack_data[st->start+4].u.num;
  11304. name=get_str(num&0x00ffffff);
  11305. prefix=*name;
  11306. if(not_server_variable(prefix))
  11307. sd=script_rid2sd(st);
  11308. else
  11309. sd=NULL;
  11310. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  11311. //Return Success value
  11312. script_pushint(st,0);
  11313. return 0;
  11314. }
  11315. /*==========================================
  11316. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  11317. *------------------------------------------*/
  11318. BUILDIN_FUNC(logmes)
  11319. {
  11320. const char *str;
  11321. TBL_PC* sd;
  11322. sd = script_rid2sd(st);
  11323. if( sd == NULL )
  11324. return 1;
  11325. str = script_getstr(st,2);
  11326. log_npc(sd,str);
  11327. return 0;
  11328. }
  11329. BUILDIN_FUNC(summon)
  11330. {
  11331. int _class, timeout=0;
  11332. const char *str,*event="";
  11333. TBL_PC *sd;
  11334. struct mob_data *md;
  11335. int tick = gettick();
  11336. sd=script_rid2sd(st);
  11337. if (!sd) return 0;
  11338. str =script_getstr(st,2);
  11339. _class=script_getnum(st,3);
  11340. if( script_hasdata(st,4) )
  11341. timeout=script_getnum(st,4);
  11342. if( script_hasdata(st,5) ){
  11343. event=script_getstr(st,5);
  11344. check_event(st, event);
  11345. }
  11346. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  11347. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  11348. if (md) {
  11349. md->master_id=sd->bl.id;
  11350. md->special_state.ai=1;
  11351. if( md->deletetimer != INVALID_TIMER )
  11352. delete_timer(md->deletetimer, mob_timer_delete);
  11353. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  11354. mob_spawn (md); //Now it is ready for spawning.
  11355. clif_specialeffect(&md->bl,344,AREA);
  11356. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  11357. }
  11358. return 0;
  11359. }
  11360. /*==========================================
  11361. * Checks whether it is daytime/nighttime
  11362. *------------------------------------------*/
  11363. BUILDIN_FUNC(isnight)
  11364. {
  11365. script_pushint(st,(night_flag == 1));
  11366. return 0;
  11367. }
  11368. BUILDIN_FUNC(isday)
  11369. {
  11370. script_pushint(st,(night_flag == 0));
  11371. return 0;
  11372. }
  11373. /*================================================
  11374. * Check how many items/cards in the list are
  11375. * equipped - used for 2/15's cards patch [celest]
  11376. *------------------------------------------------*/
  11377. BUILDIN_FUNC(isequippedcnt)
  11378. {
  11379. TBL_PC *sd;
  11380. int i, j, k, id = 1;
  11381. int ret = 0;
  11382. sd = script_rid2sd(st);
  11383. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11384. script_pushint(st,0);
  11385. return 0;
  11386. }
  11387. for (i=0; id!=0; i++) {
  11388. FETCH (i+2, id) else id = 0;
  11389. if (id <= 0)
  11390. continue;
  11391. for (j=0; j<EQI_MAX; j++) {
  11392. int index;
  11393. index = sd->equip_index[j];
  11394. if(index < 0) continue;
  11395. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11396. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11397. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11398. if(!sd->inventory_data[index])
  11399. continue;
  11400. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  11401. if (sd->inventory_data[index]->nameid == id)
  11402. ret+= sd->status.inventory[index].amount;
  11403. } else { //Count cards.
  11404. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11405. continue; //No cards
  11406. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11407. if (sd->status.inventory[index].card[k] == id)
  11408. ret++; //[Lupus]
  11409. }
  11410. }
  11411. }
  11412. }
  11413. script_pushint(st,ret);
  11414. return 0;
  11415. }
  11416. /*================================================
  11417. * Check whether another card has been
  11418. * equipped - used for 2/15's cards patch [celest]
  11419. * -- Items checked cannot be reused in another
  11420. * card set to prevent exploits
  11421. *------------------------------------------------*/
  11422. BUILDIN_FUNC(isequipped)
  11423. {
  11424. TBL_PC *sd;
  11425. int i, j, k, id = 1;
  11426. int index, flag;
  11427. int ret = -1;
  11428. //Original hash to reverse it when full check fails.
  11429. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  11430. sd = script_rid2sd(st);
  11431. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11432. script_pushint(st,0);
  11433. return 0;
  11434. }
  11435. setitem_hash = sd->bonus.setitem_hash;
  11436. setitem_hash2 = sd->bonus.setitem_hash2;
  11437. for (i=0; id!=0; i++) {
  11438. FETCH (i+2, id) else id = 0;
  11439. if (id <= 0)
  11440. continue;
  11441. flag = 0;
  11442. for (j=0; j<EQI_MAX; j++) {
  11443. index = sd->equip_index[j];
  11444. if(index < 0) continue;
  11445. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11446. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11447. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11448. if(!sd->inventory_data[index])
  11449. continue;
  11450. if (itemdb_type(id) != IT_CARD) {
  11451. if (sd->inventory_data[index]->nameid != id)
  11452. continue;
  11453. flag = 1;
  11454. break;
  11455. } else { //Cards
  11456. if (sd->inventory_data[index]->slot == 0 ||
  11457. itemdb_isspecial(sd->status.inventory[index].card[0]))
  11458. continue;
  11459. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  11460. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  11461. unsigned int hash = 0;
  11462. if (sd->status.inventory[index].card[k] != id)
  11463. continue;
  11464. hash = 1<<((j<5?j:j-5)*4 + k);
  11465. // check if card is already used by another set
  11466. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  11467. continue;
  11468. // We have found a match
  11469. flag = 1;
  11470. // Set hash so this card cannot be used by another
  11471. if (j<5)
  11472. sd->bonus.setitem_hash |= hash;
  11473. else
  11474. sd->bonus.setitem_hash2 |= hash;
  11475. break;
  11476. }
  11477. }
  11478. if (flag) break; //Card found
  11479. }
  11480. if (ret == -1)
  11481. ret = flag;
  11482. else
  11483. ret &= flag;
  11484. if (!ret) break;
  11485. }
  11486. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  11487. sd->bonus.setitem_hash = setitem_hash;
  11488. sd->bonus.setitem_hash2 = setitem_hash2;
  11489. }
  11490. script_pushint(st,ret);
  11491. return 0;
  11492. }
  11493. /*================================================
  11494. * Check how many given inserted cards in the CURRENT
  11495. * weapon - used for 2/15's cards patch [Lupus]
  11496. *------------------------------------------------*/
  11497. BUILDIN_FUNC(cardscnt)
  11498. {
  11499. TBL_PC *sd;
  11500. int i, k, id = 1;
  11501. int ret = 0;
  11502. int index;
  11503. sd = script_rid2sd(st);
  11504. for (i=0; id!=0; i++) {
  11505. FETCH (i+2, id) else id = 0;
  11506. if (id <= 0)
  11507. continue;
  11508. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  11509. if(index < 0) continue;
  11510. if(!sd->inventory_data[index])
  11511. continue;
  11512. if(itemdb_type(id) != IT_CARD) {
  11513. if (sd->inventory_data[index]->nameid == id)
  11514. ret+= sd->status.inventory[index].amount;
  11515. } else {
  11516. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11517. continue;
  11518. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11519. if (sd->status.inventory[index].card[k] == id)
  11520. ret++;
  11521. }
  11522. }
  11523. }
  11524. script_pushint(st,ret);
  11525. // script_pushint(st,current_equip_item_index);
  11526. return 0;
  11527. }
  11528. /*=======================================================
  11529. * Returns the refined number of the current item, or an
  11530. * item with inventory index specified
  11531. *-------------------------------------------------------*/
  11532. BUILDIN_FUNC(getrefine)
  11533. {
  11534. TBL_PC *sd;
  11535. if ((sd = script_rid2sd(st))!= NULL)
  11536. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  11537. else
  11538. script_pushint(st,0);
  11539. return 0;
  11540. }
  11541. /*=======================================================
  11542. * Day/Night controls
  11543. *-------------------------------------------------------*/
  11544. BUILDIN_FUNC(night)
  11545. {
  11546. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  11547. return 0;
  11548. }
  11549. BUILDIN_FUNC(day)
  11550. {
  11551. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  11552. return 0;
  11553. }
  11554. //=======================================================
  11555. // Unequip [Spectre]
  11556. //-------------------------------------------------------
  11557. BUILDIN_FUNC(unequip)
  11558. {
  11559. int i;
  11560. size_t num;
  11561. TBL_PC *sd;
  11562. num = script_getnum(st,2);
  11563. sd = script_rid2sd(st);
  11564. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  11565. {
  11566. i = pc_checkequip(sd,equip[num-1]);
  11567. if (i >= 0)
  11568. pc_unequipitem(sd,i,1|2);
  11569. }
  11570. return 0;
  11571. }
  11572. BUILDIN_FUNC(equip)
  11573. {
  11574. int nameid=0,i;
  11575. TBL_PC *sd;
  11576. struct item_data *item_data;
  11577. sd = script_rid2sd(st);
  11578. nameid=script_getnum(st,2);
  11579. if((item_data = itemdb_exists(nameid)) == NULL)
  11580. {
  11581. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  11582. return 1;
  11583. }
  11584. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  11585. if( i < MAX_INVENTORY )
  11586. pc_equipitem(sd,i,item_data->equip);
  11587. return 0;
  11588. }
  11589. BUILDIN_FUNC(autoequip)
  11590. {
  11591. int nameid, flag;
  11592. struct item_data *item_data;
  11593. nameid=script_getnum(st,2);
  11594. flag=script_getnum(st,3);
  11595. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  11596. {
  11597. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  11598. return 1;
  11599. }
  11600. if( !itemdb_isequip2(item_data) )
  11601. {
  11602. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  11603. return 1;
  11604. }
  11605. item_data->flag.autoequip = flag>0?1:0;
  11606. return 0;
  11607. }
  11608. BUILDIN_FUNC(setbattleflag)
  11609. {
  11610. const char *flag, *value;
  11611. flag = script_getstr(st,2);
  11612. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  11613. if (battle_set_value(flag, value) == 0)
  11614. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  11615. else
  11616. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  11617. return 0;
  11618. }
  11619. BUILDIN_FUNC(getbattleflag)
  11620. {
  11621. const char *flag;
  11622. flag = script_getstr(st,2);
  11623. script_pushint(st,battle_get_value(flag));
  11624. return 0;
  11625. }
  11626. //=======================================================
  11627. // strlen [Valaris]
  11628. //-------------------------------------------------------
  11629. BUILDIN_FUNC(getstrlen)
  11630. {
  11631. const char *str = script_getstr(st,2);
  11632. int len = (str) ? (int)strlen(str) : 0;
  11633. script_pushint(st,len);
  11634. return 0;
  11635. }
  11636. //=======================================================
  11637. // isalpha [Valaris]
  11638. //-------------------------------------------------------
  11639. BUILDIN_FUNC(charisalpha)
  11640. {
  11641. const char *str=script_getstr(st,2);
  11642. int pos=script_getnum(st,3);
  11643. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  11644. script_pushint(st,val);
  11645. return 0;
  11646. }
  11647. //=======================================================
  11648. // charisupper <str>, <index>
  11649. //-------------------------------------------------------
  11650. BUILDIN_FUNC(charisupper)
  11651. {
  11652. const char *str = script_getstr(st,2);
  11653. int pos = script_getnum(st,3);
  11654. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  11655. script_pushint(st,val);
  11656. return 0;
  11657. }
  11658. //=======================================================
  11659. // charislower <str>, <index>
  11660. //-------------------------------------------------------
  11661. BUILDIN_FUNC(charislower)
  11662. {
  11663. const char *str = script_getstr(st,2);
  11664. int pos = script_getnum(st,3);
  11665. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  11666. script_pushint(st,val);
  11667. return 0;
  11668. }
  11669. //=======================================================
  11670. // charat <str>, <index>
  11671. //-------------------------------------------------------
  11672. BUILDIN_FUNC(charat) {
  11673. const char *str = script_getstr(st,2);
  11674. int pos = script_getnum(st,3);
  11675. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  11676. char output[2];
  11677. output[0] = str[pos];
  11678. output[1] = '\0';
  11679. script_pushstrcopy(st, output);
  11680. } else
  11681. script_pushconststr(st, "");
  11682. return 0;
  11683. }
  11684. //=======================================================
  11685. // setchar <string>, <char>, <index>
  11686. //-------------------------------------------------------
  11687. BUILDIN_FUNC(setchar)
  11688. {
  11689. const char *str = script_getstr(st,2);
  11690. const char *c = script_getstr(st,3);
  11691. int index = script_getnum(st,4);
  11692. char *output = aStrdup(str);
  11693. if(index >= 0 && index < strlen(output))
  11694. output[index] = *c;
  11695. script_pushstr(st, output);
  11696. return 0;
  11697. }
  11698. //=======================================================
  11699. // insertchar <string>, <char>, <index>
  11700. //-------------------------------------------------------
  11701. BUILDIN_FUNC(insertchar)
  11702. {
  11703. const char *str = script_getstr(st,2);
  11704. const char *c = script_getstr(st,3);
  11705. int index = script_getnum(st,4);
  11706. char *output;
  11707. size_t len = strlen(str);
  11708. if(index < 0)
  11709. index = 0;
  11710. else if(index > len)
  11711. index = len;
  11712. output = (char*)aMalloc(len + 2);
  11713. memcpy(output, str, index);
  11714. output[index] = c[0];
  11715. memcpy(&output[index+1], &str[index], len - index);
  11716. output[len+1] = '\0';
  11717. script_pushstr(st, output);
  11718. return 0;
  11719. }
  11720. //=======================================================
  11721. // delchar <string>, <index>
  11722. //-------------------------------------------------------
  11723. BUILDIN_FUNC(delchar)
  11724. {
  11725. const char *str = script_getstr(st,2);
  11726. int index = script_getnum(st,3);
  11727. char *output;
  11728. size_t len = strlen(str);
  11729. if(index < 0 || index > len) {
  11730. //return original
  11731. output = aStrdup(str);
  11732. script_pushstr(st, output);
  11733. return 0;
  11734. }
  11735. output = (char*)aMalloc(len);
  11736. memcpy(output, str, index);
  11737. memcpy(&output[index], &str[index+1], len - index);
  11738. script_pushstr(st, output);
  11739. return 0;
  11740. }
  11741. //=======================================================
  11742. // strtoupper <str>
  11743. //-------------------------------------------------------
  11744. BUILDIN_FUNC(strtoupper)
  11745. {
  11746. const char *str = script_getstr(st,2);
  11747. char *output = aStrdup(str);
  11748. char *cursor = output;
  11749. while (*cursor != '\0') {
  11750. *cursor = TOUPPER(*cursor);
  11751. cursor++;
  11752. }
  11753. script_pushstr(st, output);
  11754. return 0;
  11755. }
  11756. //=======================================================
  11757. // strtolower <str>
  11758. //-------------------------------------------------------
  11759. BUILDIN_FUNC(strtolower)
  11760. {
  11761. const char *str = script_getstr(st,2);
  11762. char *output = aStrdup(str);
  11763. char *cursor = output;
  11764. while (*cursor != '\0') {
  11765. *cursor = TOLOWER(*cursor);
  11766. cursor++;
  11767. }
  11768. script_pushstr(st, output);
  11769. return 0;
  11770. }
  11771. //=======================================================
  11772. // substr <str>, <start>, <end>
  11773. //-------------------------------------------------------
  11774. BUILDIN_FUNC(substr)
  11775. {
  11776. const char *str = script_getstr(st,2);
  11777. char *output;
  11778. int start = script_getnum(st,3);
  11779. int end = script_getnum(st,4);
  11780. int len = 0;
  11781. if(start >= 0 && end < strlen(str) && start <= end) {
  11782. len = end - start + 1;
  11783. output = (char*)aMalloc(len + 1);
  11784. memcpy(output, &str[start], len);
  11785. } else
  11786. output = (char*)aMalloc(1);
  11787. output[len] = '\0';
  11788. script_pushstr(st, output);
  11789. return 0;
  11790. }
  11791. //=======================================================
  11792. // explode <dest_string_array>, <str>, <delimiter>
  11793. // Note: delimiter is limited to 1 char
  11794. //-------------------------------------------------------
  11795. BUILDIN_FUNC(explode)
  11796. {
  11797. struct script_data* data = script_getdata(st, 2);
  11798. const char *str = script_getstr(st,3);
  11799. const char delimiter = script_getstr(st, 4)[0];
  11800. int32 id;
  11801. size_t len = strlen(str);
  11802. int i = 0, j = 0;
  11803. int start;
  11804. char *temp;
  11805. const char* name;
  11806. TBL_PC* sd = NULL;
  11807. temp = (char*)aMalloc(len + 1);
  11808. if( !data_isreference(data) )
  11809. {
  11810. ShowError("script:explode: not a variable\n");
  11811. script_reportdata(data);
  11812. st->state = END;
  11813. return 1;// not a variable
  11814. }
  11815. id = reference_getid(data);
  11816. start = reference_getindex(data);
  11817. name = reference_getname(data);
  11818. if( not_array_variable(*name) )
  11819. {
  11820. ShowError("script:explode: illegal scope\n");
  11821. script_reportdata(data);
  11822. st->state = END;
  11823. return 1;// not supported
  11824. }
  11825. if( !is_string_variable(name) )
  11826. {
  11827. ShowError("script:explode: not string array\n");
  11828. script_reportdata(data);
  11829. st->state = END;
  11830. return 1;// data type mismatch
  11831. }
  11832. if( not_server_variable(*name) )
  11833. {
  11834. sd = script_rid2sd(st);
  11835. if( sd == NULL )
  11836. return 0;// no player attached
  11837. }
  11838. while(str[i] != '\0') {
  11839. if(str[i] == delimiter && start < 127) { //break at delimiter but ignore after reaching last array index
  11840. temp[j] = '\0';
  11841. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  11842. j = 0;
  11843. ++i;
  11844. } else {
  11845. temp[j++] = str[i++];
  11846. }
  11847. }
  11848. //set last string
  11849. temp[j] = '\0';
  11850. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  11851. aFree(temp);
  11852. return 0;
  11853. }
  11854. //=======================================================
  11855. // implode <string_array>
  11856. // implode <string_array>, <glue>
  11857. //-------------------------------------------------------
  11858. BUILDIN_FUNC(implode)
  11859. {
  11860. struct script_data* data = script_getdata(st, 2);
  11861. const char *glue = NULL, *name, *temp;
  11862. int32 glue_len = 0, array_size, id;
  11863. size_t len = 0;
  11864. int i, k = 0;
  11865. TBL_PC* sd = NULL;
  11866. char *output;
  11867. if( !data_isreference(data) )
  11868. {
  11869. ShowError("script:implode: not a variable\n");
  11870. script_reportdata(data);
  11871. st->state = END;
  11872. return 1;// not a variable
  11873. }
  11874. id = reference_getid(data);
  11875. name = reference_getname(data);
  11876. if( not_array_variable(*name) )
  11877. {
  11878. ShowError("script:implode: illegal scope\n");
  11879. script_reportdata(data);
  11880. st->state = END;
  11881. return 1;// not supported
  11882. }
  11883. if( !is_string_variable(name) )
  11884. {
  11885. ShowError("script:implode: not string array\n");
  11886. script_reportdata(data);
  11887. st->state = END;
  11888. return 1;// data type mismatch
  11889. }
  11890. if( not_server_variable(*name) )
  11891. {
  11892. sd = script_rid2sd(st);
  11893. if( sd == NULL )
  11894. return 0;// no player attached
  11895. }
  11896. //count chars
  11897. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  11898. if(array_size == -1) //empty array check (AmsTaff)
  11899. {
  11900. ShowWarning("script:implode: array length = 0\n");
  11901. output = (char*)aMalloc(sizeof(char)*5);
  11902. sprintf(output,"%s","NULL");
  11903. } else {
  11904. for(i = 0; i <= array_size; ++i) {
  11905. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11906. len += strlen(temp);
  11907. script_removetop(st, -1, 0);
  11908. }
  11909. //allocate mem
  11910. if( script_hasdata(st,3) ) {
  11911. glue = script_getstr(st,3);
  11912. glue_len = strlen(glue);
  11913. len += glue_len * (array_size);
  11914. }
  11915. output = (char*)aMalloc(len + 1);
  11916. //build output
  11917. for(i = 0; i < array_size; ++i) {
  11918. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11919. len = strlen(temp);
  11920. memcpy(&output[k], temp, len);
  11921. k += len;
  11922. if(glue_len != 0) {
  11923. memcpy(&output[k], glue, glue_len);
  11924. k += glue_len;
  11925. }
  11926. script_removetop(st, -1, 0);
  11927. }
  11928. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  11929. len = strlen(temp);
  11930. memcpy(&output[k], temp, len);
  11931. k += len;
  11932. script_removetop(st, -1, 0);
  11933. output[k] = '\0';
  11934. }
  11935. script_pushstr(st, output);
  11936. return 0;
  11937. }
  11938. //=======================================================
  11939. // sprintf(<format>, ...);
  11940. // Implements C sprintf, except format %n. The resulting string is
  11941. // returned, instead of being saved in variable by reference.
  11942. //-------------------------------------------------------
  11943. BUILDIN_FUNC(sprintf)
  11944. {
  11945. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  11946. const char* format;
  11947. char* p;
  11948. char* q;
  11949. char* buf = NULL;
  11950. char* buf2 = NULL;
  11951. struct script_data* data;
  11952. StringBuf final_buf;
  11953. // Fetch init data
  11954. format = script_getstr(st, 2);
  11955. argc = script_lastdata(st)-2;
  11956. len = strlen(format);
  11957. // Skip parsing, where no parsing is required.
  11958. if(len==0){
  11959. script_pushconststr(st,"");
  11960. return 0;
  11961. }
  11962. // Pessimistic alloc
  11963. CREATE(buf, char, len+1);
  11964. // Need not be parsed, just solve stuff like %%.
  11965. if(argc==0){
  11966. memcpy(buf,format,len+1);
  11967. script_pushstrcopy(st, buf);
  11968. aFree(buf);
  11969. return 0;
  11970. }
  11971. safestrncpy(buf, format, len+1);
  11972. // Issue sprintf for each parameter
  11973. StringBuf_Init(&final_buf);
  11974. q = buf;
  11975. while((p = strchr(q, '%'))!=NULL){
  11976. if(p!=q){
  11977. len = p-q+1;
  11978. if(buf2_len<len){
  11979. RECREATE(buf2, char, len);
  11980. buf2_len = len;
  11981. }
  11982. safestrncpy(buf2, q, len);
  11983. StringBuf_AppendStr(&final_buf, buf2);
  11984. q = p;
  11985. }
  11986. p = q+1;
  11987. if(*p=='%'){ // %%
  11988. StringBuf_AppendStr(&final_buf, "%");
  11989. q+=2;
  11990. continue;
  11991. }
  11992. if(*p=='n'){ // %n
  11993. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  11994. script_reportsrc(st);
  11995. q+=2;
  11996. continue;
  11997. }
  11998. if(arg>=argc){
  11999. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  12000. if(buf) aFree(buf);
  12001. if(buf2) aFree(buf2);
  12002. StringBuf_Destroy(&final_buf);
  12003. script_pushconststr(st,"");
  12004. return 1;
  12005. }
  12006. if((p = strchr(q+1, '%'))==NULL){
  12007. p = strchr(q, 0); // EOS
  12008. }
  12009. len = p-q+1;
  12010. if(buf2_len<len){
  12011. RECREATE(buf2, char, len);
  12012. buf2_len = len;
  12013. }
  12014. safestrncpy(buf2, q, len);
  12015. q = p;
  12016. // Note: This assumes the passed value being the correct
  12017. // type to the current format specifier. If not, the server
  12018. // probably crashes or returns anything else, than expected,
  12019. // but it would behave in normal code the same way so it's
  12020. // the scripter's responsibility.
  12021. data = script_getdata(st, arg+3);
  12022. if(data_isstring(data)){ // String
  12023. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12024. }else if(data_isint(data)){ // Number
  12025. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12026. }else if(data_isreference(data)){ // Variable
  12027. char* name = reference_getname(data);
  12028. if(name[strlen(name)-1]=='$'){ // var Str
  12029. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12030. }else{ // var Int
  12031. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12032. }
  12033. }else{ // Unsupported type
  12034. ShowError("buildin_sprintf: Unknown argument type!\n");
  12035. if(buf) aFree(buf);
  12036. if(buf2) aFree(buf2);
  12037. StringBuf_Destroy(&final_buf);
  12038. script_pushconststr(st,"");
  12039. return 1;
  12040. }
  12041. arg++;
  12042. }
  12043. // Append anything left
  12044. if(*q){
  12045. StringBuf_AppendStr(&final_buf, q);
  12046. }
  12047. // Passed more, than needed
  12048. if(arg<argc){
  12049. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  12050. script_reportsrc(st);
  12051. }
  12052. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  12053. if(buf) aFree(buf);
  12054. if(buf2) aFree(buf2);
  12055. StringBuf_Destroy(&final_buf);
  12056. return 0;
  12057. }
  12058. //=======================================================
  12059. // sscanf(<str>, <format>, ...);
  12060. // Implements C sscanf.
  12061. //-------------------------------------------------------
  12062. BUILDIN_FUNC(sscanf){
  12063. unsigned int argc, arg = 0, len;
  12064. struct script_data* data;
  12065. struct map_session_data* sd = NULL;
  12066. const char* str;
  12067. const char* format;
  12068. const char* p;
  12069. const char* q;
  12070. char* buf = NULL;
  12071. char* buf_p;
  12072. char* ref_str = NULL;
  12073. int ref_int;
  12074. // Get data
  12075. str = script_getstr(st, 2);
  12076. format = script_getstr(st, 3);
  12077. argc = script_lastdata(st)-3;
  12078. len = strlen(format);
  12079. CREATE(buf, char, len*2+1);
  12080. // Issue sscanf for each parameter
  12081. *buf = 0;
  12082. q = format;
  12083. while((p = strchr(q, '%'))){
  12084. if(p!=q){
  12085. strncat(buf, q, (size_t)(p-q));
  12086. q = p;
  12087. }
  12088. p = q+1;
  12089. if(*p=='*' || *p=='%'){ // Skip
  12090. strncat(buf, q, 2);
  12091. q+=2;
  12092. continue;
  12093. }
  12094. if(arg>=argc){
  12095. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  12096. script_pushint(st, -1);
  12097. if(buf) aFree(buf);
  12098. if(ref_str) aFree(ref_str);
  12099. return 1;
  12100. }
  12101. if((p = strchr(q+1, '%'))==NULL){
  12102. p = strchr(q, 0); // EOS
  12103. }
  12104. len = p-q;
  12105. strncat(buf, q, len);
  12106. q = p;
  12107. // Validate output
  12108. data = script_getdata(st, arg+4);
  12109. if(!data_isreference(data) || !reference_tovariable(data)){
  12110. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  12111. script_pushint(st, -1);
  12112. if(buf) aFree(buf);
  12113. if(ref_str) aFree(ref_str);
  12114. return 1;
  12115. }
  12116. buf_p = reference_getname(data);
  12117. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  12118. script_pushint(st, -1);
  12119. if(buf) aFree(buf);
  12120. if(ref_str) aFree(ref_str);
  12121. return 0;
  12122. }
  12123. // Save value if any
  12124. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  12125. if(ref_str==NULL){
  12126. CREATE(ref_str, char, strlen(str)+1);
  12127. }
  12128. if(sscanf(str, buf, ref_str)==0){
  12129. break;
  12130. }
  12131. set_reg(st, sd, add_str(buf_p), buf_p, (void *)(ref_str), reference_getref(data));
  12132. }else{ // Number
  12133. if(sscanf(str, buf, &ref_int)==0){
  12134. break;
  12135. }
  12136. set_reg(st, sd, add_str(buf_p), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  12137. }
  12138. arg++;
  12139. // Disable used format (%... -> %*...)
  12140. buf_p = strchr(buf, 0);
  12141. memmove(buf_p-len+2, buf_p-len+1, len);
  12142. *(buf_p-len+1) = '*';
  12143. }
  12144. // Passed more, than needed
  12145. if(arg<argc){
  12146. ShowWarning("buildin_sscanf: Unused arguments passed.\n");
  12147. script_reportsrc(st);
  12148. }
  12149. script_pushint(st, arg);
  12150. if(buf) aFree(buf);
  12151. if(ref_str) aFree(ref_str);
  12152. return 0;
  12153. }
  12154. //=======================================================
  12155. // strpos(<haystack>, <needle>)
  12156. // strpos(<haystack>, <needle>, <offset>)
  12157. //
  12158. // Implements PHP style strpos. Adapted from code from
  12159. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  12160. //-------------------------------------------------------
  12161. BUILDIN_FUNC(strpos) {
  12162. const char *haystack = script_getstr(st,2);
  12163. const char *needle = script_getstr(st,3);
  12164. int i;
  12165. size_t len;
  12166. if( script_hasdata(st,4) )
  12167. i = script_getnum(st,4);
  12168. else
  12169. i = 0;
  12170. if ( strlen(needle) == 0 ) {
  12171. script_pushint(st, -1);
  12172. return 0;
  12173. }
  12174. len = strlen(haystack);
  12175. for ( ; i < len; ++i ) {
  12176. if ( haystack[i] == *needle ) {
  12177. // matched starting char -- loop through remaining chars
  12178. const char *h, *n;
  12179. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  12180. if ( *h != *n ) {
  12181. break;
  12182. }
  12183. }
  12184. if ( !*n ) { // matched all of 'needle' to null termination
  12185. script_pushint(st, i);
  12186. return 0;
  12187. }
  12188. }
  12189. }
  12190. script_pushint(st, -1);
  12191. return 0;
  12192. }
  12193. //===============================================================
  12194. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  12195. //
  12196. // Note: Finds all instances of <search> in <input> and replaces
  12197. // with <replace>. If specified will only replace as many
  12198. // instances as specified in <count>. By default will be case
  12199. // sensitive.
  12200. //---------------------------------------------------------------
  12201. BUILDIN_FUNC(replacestr)
  12202. {
  12203. const char *input = script_getstr(st, 2);
  12204. const char *find = script_getstr(st, 3);
  12205. const char *replace = script_getstr(st, 4);
  12206. size_t inputlen = strlen(input);
  12207. size_t findlen = strlen(find);
  12208. struct StringBuf output;
  12209. bool usecase = true;
  12210. int count = 0;
  12211. int numFinds = 0;
  12212. int i = 0, f = 0;
  12213. if(findlen == 0) {
  12214. ShowError("script:replacestr: Invalid search length.\n");
  12215. st->state = END;
  12216. return 1;
  12217. }
  12218. if(script_hasdata(st, 5)) {
  12219. if( !script_isstring(st,5) )
  12220. usecase = script_getnum(st, 5) != 0;
  12221. else {
  12222. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  12223. st->state = END;
  12224. return 1;
  12225. }
  12226. }
  12227. if(script_hasdata(st, 6)) {
  12228. count = script_getnum(st, 6);
  12229. if(count == 0) {
  12230. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  12231. st->state = END;
  12232. return 1;
  12233. }
  12234. }
  12235. StringBuf_Init(&output);
  12236. for(; i < inputlen; i++) {
  12237. if(count && count == numFinds) { //found enough, stop looking
  12238. break;
  12239. }
  12240. for(f = 0; f <= findlen; f++) {
  12241. if(f == findlen) { //complete match
  12242. numFinds++;
  12243. StringBuf_AppendStr(&output, replace);
  12244. i += findlen - 1;
  12245. break;
  12246. } else {
  12247. if(usecase) {
  12248. if((i + f) > inputlen || input[i + f] != find[f]) {
  12249. StringBuf_Printf(&output, "%c", input[i]);
  12250. break;
  12251. }
  12252. } else {
  12253. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12254. StringBuf_Printf(&output, "%c", input[i]);
  12255. break;
  12256. }
  12257. }
  12258. }
  12259. }
  12260. }
  12261. //append excess after enough found
  12262. if(i < inputlen)
  12263. StringBuf_AppendStr(&output, &(input[i]));
  12264. script_pushstrcopy(st, StringBuf_Value(&output));
  12265. StringBuf_Destroy(&output);
  12266. return 0;
  12267. }
  12268. //========================================================
  12269. // countstr <input>, <search>{, <usecase>}
  12270. //
  12271. // Note: Counts the number of times <search> occurs in
  12272. // <input>. By default will be case sensitive.
  12273. //--------------------------------------------------------
  12274. BUILDIN_FUNC(countstr)
  12275. {
  12276. const char *input = script_getstr(st, 2);
  12277. const char *find = script_getstr(st, 3);
  12278. size_t inputlen = strlen(input);
  12279. size_t findlen = strlen(find);
  12280. bool usecase = true;
  12281. int numFinds = 0;
  12282. int i = 0, f = 0;
  12283. if(findlen == 0) {
  12284. ShowError("script:countstr: Invalid search length.\n");
  12285. st->state = END;
  12286. return 1;
  12287. }
  12288. if(script_hasdata(st, 4)) {
  12289. if( !script_isstring(st,4) )
  12290. usecase = script_getnum(st, 4) != 0;
  12291. else {
  12292. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  12293. st->state = END;
  12294. return 1;
  12295. }
  12296. }
  12297. for(; i < inputlen; i++) {
  12298. for(f = 0; f <= findlen; f++) {
  12299. if(f == findlen) { //complete match
  12300. numFinds++;
  12301. i += findlen - 1;
  12302. break;
  12303. } else {
  12304. if(usecase) {
  12305. if((i + f) > inputlen || input[i + f] != find[f]) {
  12306. break;
  12307. }
  12308. } else {
  12309. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12310. break;
  12311. }
  12312. }
  12313. }
  12314. }
  12315. }
  12316. script_pushint(st, numFinds);
  12317. return 0;
  12318. }
  12319. /// Changes the display name and/or display class of the npc.
  12320. /// Returns 0 is successful, 1 if the npc does not exist.
  12321. ///
  12322. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  12323. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  12324. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  12325. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  12326. BUILDIN_FUNC(setnpcdisplay)
  12327. {
  12328. const char* name;
  12329. const char* newname = NULL;
  12330. int class_ = -1, size = -1;
  12331. struct script_data* data;
  12332. struct npc_data* nd;
  12333. name = script_getstr(st,2);
  12334. data = script_getdata(st,3);
  12335. if( script_hasdata(st,4) )
  12336. class_ = script_getnum(st,4);
  12337. if( script_hasdata(st,5) )
  12338. size = script_getnum(st,5);
  12339. get_val(st, data);
  12340. if( data_isstring(data) )
  12341. newname = conv_str(st,data);
  12342. else if( data_isint(data) )
  12343. class_ = conv_num(st,data);
  12344. else
  12345. {
  12346. ShowError("script:setnpcdisplay: expected a string or number\n");
  12347. script_reportdata(data);
  12348. return 1;
  12349. }
  12350. nd = npc_name2id(name);
  12351. if( nd == NULL )
  12352. {// not found
  12353. script_pushint(st,1);
  12354. return 0;
  12355. }
  12356. // update npc
  12357. if( newname )
  12358. npc_setdisplayname(nd, newname);
  12359. if( size != -1 && size != (int)nd->size )
  12360. nd->size = size;
  12361. else
  12362. size = -1;
  12363. if( class_ != -1 && nd->class_ != class_ )
  12364. npc_setclass(nd, class_);
  12365. else if( size != -1 )
  12366. { // Required to update the visual size
  12367. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  12368. clif_spawn(&nd->bl);
  12369. }
  12370. script_pushint(st,0);
  12371. return 0;
  12372. }
  12373. BUILDIN_FUNC(atoi)
  12374. {
  12375. const char *value;
  12376. value = script_getstr(st,2);
  12377. script_pushint(st,atoi(value));
  12378. return 0;
  12379. }
  12380. // case-insensitive substring search [lordalfa]
  12381. BUILDIN_FUNC(compare)
  12382. {
  12383. const char *message;
  12384. const char *cmpstring;
  12385. message = script_getstr(st,2);
  12386. cmpstring = script_getstr(st,3);
  12387. script_pushint(st,(stristr(message,cmpstring) != NULL));
  12388. return 0;
  12389. }
  12390. // [zBuffer] List of mathematics commands --->
  12391. BUILDIN_FUNC(sqrt)
  12392. {
  12393. double i, a;
  12394. i = script_getnum(st,2);
  12395. a = sqrt(i);
  12396. script_pushint(st,(int)a);
  12397. return 0;
  12398. }
  12399. BUILDIN_FUNC(pow)
  12400. {
  12401. double i, a, b;
  12402. a = script_getnum(st,2);
  12403. b = script_getnum(st,3);
  12404. i = pow(a,b);
  12405. script_pushint(st,(int)i);
  12406. return 0;
  12407. }
  12408. BUILDIN_FUNC(distance)
  12409. {
  12410. int x0, y0, x1, y1;
  12411. x0 = script_getnum(st,2);
  12412. y0 = script_getnum(st,3);
  12413. x1 = script_getnum(st,4);
  12414. y1 = script_getnum(st,5);
  12415. script_pushint(st,distance_xy(x0,y0,x1,y1));
  12416. return 0;
  12417. }
  12418. // <--- [zBuffer] List of mathematics commands
  12419. BUILDIN_FUNC(md5)
  12420. {
  12421. const char *tmpstr;
  12422. char *md5str;
  12423. tmpstr = script_getstr(st,2);
  12424. md5str = (char *)aMalloc((32+1)*sizeof(char));
  12425. MD5_String(tmpstr, md5str);
  12426. script_pushstr(st, md5str);
  12427. return 0;
  12428. }
  12429. // [zBuffer] List of dynamic var commands --->
  12430. BUILDIN_FUNC(setd)
  12431. {
  12432. TBL_PC *sd=NULL;
  12433. char varname[100];
  12434. const char *buffer;
  12435. int elem;
  12436. buffer = script_getstr(st, 2);
  12437. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  12438. elem = 0;
  12439. if( not_server_variable(*varname) )
  12440. {
  12441. sd = script_rid2sd(st);
  12442. if( sd == NULL )
  12443. {
  12444. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  12445. return 0;
  12446. }
  12447. }
  12448. if( is_string_variable(varname) ) {
  12449. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  12450. } else {
  12451. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  12452. }
  12453. return 0;
  12454. }
  12455. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  12456. {
  12457. int i, j;
  12458. TBL_PC* sd = NULL;
  12459. const char* query;
  12460. struct script_data* data;
  12461. const char* name;
  12462. int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  12463. int num_vars;
  12464. int num_cols;
  12465. // check target variables
  12466. for( i = 3; script_hasdata(st,i); ++i ) {
  12467. data = script_getdata(st, i);
  12468. if( data_isreference(data) ) { // it's a variable
  12469. name = reference_getname(data);
  12470. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  12471. sd = script_rid2sd(st);
  12472. if( sd == NULL ) { // no player attached
  12473. script_reportdata(data);
  12474. st->state = END;
  12475. return 1;
  12476. }
  12477. }
  12478. if( not_array_variable(*name) )
  12479. max_rows = 1;// not an array, limit to one row
  12480. } else {
  12481. ShowError("script:query_sql: not a variable\n");
  12482. script_reportdata(data);
  12483. st->state = END;
  12484. return 1;
  12485. }
  12486. }
  12487. num_vars = i - 3;
  12488. // Execute the query
  12489. query = script_getstr(st,2);
  12490. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  12491. Sql_ShowDebug(handle);
  12492. script_pushint(st, 0);
  12493. return 1;
  12494. }
  12495. if( Sql_NumRows(handle) == 0 ) { // No data received
  12496. Sql_FreeResult(handle);
  12497. script_pushint(st, 0);
  12498. return 0;
  12499. }
  12500. // Count the number of columns to store
  12501. num_cols = Sql_NumColumns(handle);
  12502. if( num_vars < num_cols ) {
  12503. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  12504. script_reportsrc(st);
  12505. } else if( num_vars > num_cols ) {
  12506. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  12507. script_reportsrc(st);
  12508. }
  12509. // Store data
  12510. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  12511. for( j = 0; j < num_vars; ++j ) {
  12512. char* str = NULL;
  12513. if( j < num_cols )
  12514. Sql_GetData(handle, j, &str, NULL);
  12515. data = script_getdata(st, j+3);
  12516. name = reference_getname(data);
  12517. if( is_string_variable(name) )
  12518. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  12519. else
  12520. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  12521. }
  12522. }
  12523. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  12524. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  12525. script_reportsrc(st);
  12526. }
  12527. // Free data
  12528. Sql_FreeResult(handle);
  12529. script_pushint(st, i);
  12530. return 0;
  12531. }
  12532. BUILDIN_FUNC(query_sql) {
  12533. #ifdef BETA_THREAD_TEST
  12534. if( st->state != RERUNLINE ) {
  12535. queryThread_add(st,false);
  12536. st->state = RERUNLINE;/* will continue when the query is finished running. */
  12537. } else
  12538. st->state = RUN;
  12539. return 0;
  12540. #else
  12541. return buildin_query_sql_sub(st, mmysql_handle);
  12542. #endif
  12543. }
  12544. BUILDIN_FUNC(query_logsql) {
  12545. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  12546. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  12547. script_pushint(st,-1);
  12548. return 1;
  12549. }
  12550. #ifdef BETA_THREAD_TEST
  12551. if( st->state != RERUNLINE ) {
  12552. queryThread_add(st,true);
  12553. st->state = RERUNLINE;/* will continue when the query is finished running. */
  12554. } else
  12555. st->state = RUN;
  12556. return 0;
  12557. #else
  12558. return buildin_query_sql_sub(st, logmysql_handle);
  12559. #endif
  12560. }
  12561. //Allows escaping of a given string.
  12562. BUILDIN_FUNC(escape_sql)
  12563. {
  12564. const char *str;
  12565. char *esc_str;
  12566. size_t len;
  12567. str = script_getstr(st,2);
  12568. len = strlen(str);
  12569. esc_str = (char*)aMalloc(len*2+1);
  12570. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  12571. script_pushstr(st, esc_str);
  12572. return 0;
  12573. }
  12574. BUILDIN_FUNC(getd)
  12575. {
  12576. char varname[100];
  12577. const char *buffer;
  12578. int elem;
  12579. buffer = script_getstr(st, 2);
  12580. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  12581. elem = 0;
  12582. // Push the 'pointer' so it's more flexible [Lance]
  12583. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  12584. return 0;
  12585. }
  12586. // <--- [zBuffer] List of dynamic var commands
  12587. // Pet stat [Lance]
  12588. BUILDIN_FUNC(petstat)
  12589. {
  12590. TBL_PC *sd = NULL;
  12591. struct pet_data *pd;
  12592. int flag = script_getnum(st,2);
  12593. sd = script_rid2sd(st);
  12594. if(!sd || !sd->status.pet_id || !sd->pd){
  12595. if(flag == 2)
  12596. script_pushconststr(st, "");
  12597. else
  12598. script_pushint(st,0);
  12599. return 0;
  12600. }
  12601. pd = sd->pd;
  12602. switch(flag){
  12603. case 1: script_pushint(st,(int)pd->pet.class_); break;
  12604. case 2: script_pushstrcopy(st, pd->pet.name); break;
  12605. case 3: script_pushint(st,(int)pd->pet.level); break;
  12606. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  12607. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  12608. default:
  12609. script_pushint(st,0);
  12610. break;
  12611. }
  12612. return 0;
  12613. }
  12614. BUILDIN_FUNC(callshop)
  12615. {
  12616. TBL_PC *sd = NULL;
  12617. struct npc_data *nd;
  12618. const char *shopname;
  12619. int flag = 0;
  12620. sd = script_rid2sd(st);
  12621. if (!sd) {
  12622. script_pushint(st,0);
  12623. return 0;
  12624. }
  12625. shopname = script_getstr(st, 2);
  12626. if( script_hasdata(st,3) )
  12627. flag = script_getnum(st,3);
  12628. nd = npc_name2id(shopname);
  12629. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  12630. {
  12631. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  12632. script_pushint(st,0);
  12633. return 1;
  12634. }
  12635. if( nd->subtype == SHOP )
  12636. {
  12637. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  12638. sd->state.callshop = 1;
  12639. switch( flag )
  12640. {
  12641. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  12642. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  12643. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  12644. }
  12645. }
  12646. else
  12647. clif_cashshop_show(sd, nd);
  12648. sd->npc_shopid = nd->bl.id;
  12649. script_pushint(st,1);
  12650. return 0;
  12651. }
  12652. BUILDIN_FUNC(npcshopitem)
  12653. {
  12654. const char* npcname = script_getstr(st, 2);
  12655. struct npc_data* nd = npc_name2id(npcname);
  12656. int n, i;
  12657. int amount;
  12658. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12659. { //Not found.
  12660. script_pushint(st,0);
  12661. return 0;
  12662. }
  12663. // get the count of new entries
  12664. amount = (script_lastdata(st)-2)/2;
  12665. // generate new shop item list
  12666. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  12667. for( n = 0, i = 3; n < amount; n++, i+=2 )
  12668. {
  12669. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12670. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12671. }
  12672. nd->u.shop.count = n;
  12673. script_pushint(st,1);
  12674. return 0;
  12675. }
  12676. BUILDIN_FUNC(npcshopadditem)
  12677. {
  12678. const char* npcname = script_getstr(st,2);
  12679. struct npc_data* nd = npc_name2id(npcname);
  12680. int n, i;
  12681. int amount;
  12682. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12683. { //Not found.
  12684. script_pushint(st,0);
  12685. return 0;
  12686. }
  12687. // get the count of new entries
  12688. amount = (script_lastdata(st)-2)/2;
  12689. // append new items to existing shop item list
  12690. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  12691. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  12692. {
  12693. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12694. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12695. }
  12696. nd->u.shop.count = n;
  12697. script_pushint(st,1);
  12698. return 0;
  12699. }
  12700. BUILDIN_FUNC(npcshopdelitem)
  12701. {
  12702. const char* npcname = script_getstr(st,2);
  12703. struct npc_data* nd = npc_name2id(npcname);
  12704. unsigned int nameid;
  12705. int n, i;
  12706. int amount;
  12707. int size;
  12708. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12709. { //Not found.
  12710. script_pushint(st,0);
  12711. return 0;
  12712. }
  12713. amount = script_lastdata(st)-2;
  12714. size = nd->u.shop.count;
  12715. // remove specified items from the shop item list
  12716. for( i = 3; i < 3 + amount; i++ )
  12717. {
  12718. nameid = script_getnum(st,i);
  12719. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  12720. if( n < size )
  12721. {
  12722. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  12723. size--;
  12724. }
  12725. }
  12726. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  12727. nd->u.shop.count = size;
  12728. script_pushint(st,1);
  12729. return 0;
  12730. }
  12731. //Sets a script to attach to a shop npc.
  12732. BUILDIN_FUNC(npcshopattach)
  12733. {
  12734. const char* npcname = script_getstr(st,2);
  12735. struct npc_data* nd = npc_name2id(npcname);
  12736. int flag = 1;
  12737. if( script_hasdata(st,3) )
  12738. flag = script_getnum(st,3);
  12739. if( !nd || nd->subtype != SHOP )
  12740. { //Not found.
  12741. script_pushint(st,0);
  12742. return 0;
  12743. }
  12744. if (flag)
  12745. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  12746. else
  12747. nd->master_nd = NULL;
  12748. script_pushint(st,1);
  12749. return 0;
  12750. }
  12751. /*==========================================
  12752. * Returns some values of an item [Lupus]
  12753. * Price, Weight, etc...
  12754. setitemscript(itemID,"{new item bonus script}",[n]);
  12755. Where n:
  12756. 0 - script
  12757. 1 - Equip script
  12758. 2 - Unequip script
  12759. *------------------------------------------*/
  12760. BUILDIN_FUNC(setitemscript)
  12761. {
  12762. int item_id,n=0;
  12763. const char *script;
  12764. struct item_data *i_data;
  12765. struct script_code **dstscript;
  12766. item_id = script_getnum(st,2);
  12767. script = script_getstr(st,3);
  12768. if( script_hasdata(st,4) )
  12769. n=script_getnum(st,4);
  12770. i_data = itemdb_exists(item_id);
  12771. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  12772. script_pushint(st,0);
  12773. return 0;
  12774. }
  12775. switch (n) {
  12776. case 2:
  12777. dstscript = &i_data->unequip_script;
  12778. break;
  12779. case 1:
  12780. dstscript = &i_data->equip_script;
  12781. break;
  12782. default:
  12783. dstscript = &i_data->script;
  12784. break;
  12785. }
  12786. if(*dstscript)
  12787. script_free_code(*dstscript);
  12788. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  12789. script_pushint(st,1);
  12790. return 0;
  12791. }
  12792. /* Work In Progress [Lupus]
  12793. BUILDIN_FUNC(addmonsterdrop)
  12794. {
  12795. int class_,item_id,chance;
  12796. class_=script_getnum(st,2);
  12797. item_id=script_getnum(st,3);
  12798. chance=script_getnum(st,4);
  12799. if(class_>1000 && item_id>500 && chance>0) {
  12800. script_pushint(st,1);
  12801. } else {
  12802. script_pushint(st,0);
  12803. }
  12804. }
  12805. BUILDIN_FUNC(delmonsterdrop)
  12806. {
  12807. int class_,item_id;
  12808. class_=script_getnum(st,2);
  12809. item_id=script_getnum(st,3);
  12810. if(class_>1000 && item_id>500) {
  12811. script_pushint(st,1);
  12812. } else {
  12813. script_pushint(st,0);
  12814. }
  12815. }
  12816. */
  12817. /*==========================================
  12818. * Returns some values of a monster [Lupus]
  12819. * Name, Level, race, size, etc...
  12820. getmonsterinfo(monsterID,queryIndex);
  12821. *------------------------------------------*/
  12822. BUILDIN_FUNC(getmonsterinfo)
  12823. {
  12824. struct mob_db *mob;
  12825. int mob_id;
  12826. mob_id = script_getnum(st,2);
  12827. if (!mobdb_checkid(mob_id)) {
  12828. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  12829. if ( !script_getnum(st,3) ) //requested a string
  12830. script_pushconststr(st,"null");
  12831. else
  12832. script_pushint(st,-1);
  12833. return -1;
  12834. }
  12835. mob = mob_db(mob_id);
  12836. switch ( script_getnum(st,3) ) {
  12837. case 0: script_pushstrcopy(st,mob->jname); break;
  12838. case 1: script_pushint(st,mob->lv); break;
  12839. case 2: script_pushint(st,mob->status.max_hp); break;
  12840. case 3: script_pushint(st,mob->base_exp); break;
  12841. case 4: script_pushint(st,mob->job_exp); break;
  12842. case 5: script_pushint(st,mob->status.rhw.atk); break;
  12843. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  12844. case 7: script_pushint(st,mob->status.def); break;
  12845. case 8: script_pushint(st,mob->status.mdef); break;
  12846. case 9: script_pushint(st,mob->status.str); break;
  12847. case 10: script_pushint(st,mob->status.agi); break;
  12848. case 11: script_pushint(st,mob->status.vit); break;
  12849. case 12: script_pushint(st,mob->status.int_); break;
  12850. case 13: script_pushint(st,mob->status.dex); break;
  12851. case 14: script_pushint(st,mob->status.luk); break;
  12852. case 15: script_pushint(st,mob->status.rhw.range); break;
  12853. case 16: script_pushint(st,mob->range2); break;
  12854. case 17: script_pushint(st,mob->range3); break;
  12855. case 18: script_pushint(st,mob->status.size); break;
  12856. case 19: script_pushint(st,mob->status.race); break;
  12857. case 20: script_pushint(st,mob->status.def_ele); break;
  12858. case 21: script_pushint(st,mob->status.mode); break;
  12859. case 22: script_pushint(st,mob->mexp); break;
  12860. default: script_pushint(st,-1); //wrong Index
  12861. }
  12862. return 0;
  12863. }
  12864. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  12865. {
  12866. TBL_PC *sd = NULL;
  12867. if(script_hasdata(st,2))
  12868. sd = map_nick2sd(script_getstr(st,2));
  12869. else
  12870. sd = script_rid2sd(st);
  12871. if(sd)
  12872. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  12873. else
  12874. script_pushint(st,0);
  12875. return 0;
  12876. }
  12877. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  12878. {
  12879. TBL_PC *sd = NULL;
  12880. if(script_hasdata(st,2))
  12881. sd = map_nick2sd(script_getstr(st,2));
  12882. else
  12883. sd = script_rid2sd(st);
  12884. if(sd)
  12885. script_pushint(st,(sd->chatID != 0));
  12886. else
  12887. script_pushint(st,0);
  12888. return 0;
  12889. }
  12890. BUILDIN_FUNC(searchitem)
  12891. {
  12892. struct script_data* data = script_getdata(st, 2);
  12893. const char *itemname = script_getstr(st,3);
  12894. struct item_data *items[MAX_SEARCH];
  12895. int count;
  12896. char* name;
  12897. int32 start;
  12898. int32 id;
  12899. int32 i;
  12900. TBL_PC* sd = NULL;
  12901. if ((items[0] = itemdb_exists(atoi(itemname))))
  12902. count = 1;
  12903. else {
  12904. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  12905. if (count > MAX_SEARCH) count = MAX_SEARCH;
  12906. }
  12907. if (!count) {
  12908. script_pushint(st, 0);
  12909. return 0;
  12910. }
  12911. if( !data_isreference(data) )
  12912. {
  12913. ShowError("script:searchitem: not a variable\n");
  12914. script_reportdata(data);
  12915. st->state = END;
  12916. return 1;// not a variable
  12917. }
  12918. id = reference_getid(data);
  12919. start = reference_getindex(data);
  12920. name = reference_getname(data);
  12921. if( not_array_variable(*name) )
  12922. {
  12923. ShowError("script:searchitem: illegal scope\n");
  12924. script_reportdata(data);
  12925. st->state = END;
  12926. return 1;// not supported
  12927. }
  12928. if( not_server_variable(*name) )
  12929. {
  12930. sd = script_rid2sd(st);
  12931. if( sd == NULL )
  12932. return 0;// no player attached
  12933. }
  12934. if( is_string_variable(name) )
  12935. {// string array
  12936. ShowError("script:searchitem: not an integer array reference\n");
  12937. script_reportdata(data);
  12938. st->state = END;
  12939. return 1;// not supported
  12940. }
  12941. for( i = 0; i < count; ++start, ++i )
  12942. {// Set array
  12943. void* v = (void*)__64BPRTSIZE(items[i]->nameid);
  12944. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  12945. }
  12946. script_pushint(st, count);
  12947. return 0;
  12948. }
  12949. int axtoi(const char *hexStg)
  12950. {
  12951. int n = 0; // position in string
  12952. int m = 0; // position in digit[] to shift
  12953. int count; // loop index
  12954. int intValue = 0; // integer value of hex string
  12955. int digit[11]; // hold values to convert
  12956. while (n < 10) {
  12957. if (hexStg[n]=='\0')
  12958. break;
  12959. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  12960. digit[n] = hexStg[n] & 0x0f; //convert to int
  12961. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  12962. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12963. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  12964. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12965. else break;
  12966. n++;
  12967. }
  12968. count = n;
  12969. m = n - 1;
  12970. n = 0;
  12971. while(n < count) {
  12972. // digit[n] is value of hex digit at position n
  12973. // (m << 2) is the number of positions to shift
  12974. // OR the bits into return value
  12975. intValue = intValue | (digit[n] << (m << 2));
  12976. m--; // adjust the position to set
  12977. n++; // next digit to process
  12978. }
  12979. return (intValue);
  12980. }
  12981. // [Lance] Hex string to integer converter
  12982. BUILDIN_FUNC(axtoi)
  12983. {
  12984. const char *hex = script_getstr(st,2);
  12985. script_pushint(st,axtoi(hex));
  12986. return 0;
  12987. }
  12988. // [zBuffer] List of player cont commands --->
  12989. BUILDIN_FUNC(rid2name)
  12990. {
  12991. struct block_list *bl = NULL;
  12992. int rid = script_getnum(st,2);
  12993. if((bl = map_id2bl(rid)))
  12994. {
  12995. switch(bl->type) {
  12996. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  12997. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  12998. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  12999. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  13000. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  13001. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  13002. default:
  13003. ShowError("buildin_rid2name: BL type unknown.\n");
  13004. script_pushconststr(st,"");
  13005. break;
  13006. }
  13007. } else {
  13008. ShowError("buildin_rid2name: invalid RID\n");
  13009. script_pushconststr(st,"(null)");
  13010. }
  13011. return 0;
  13012. }
  13013. BUILDIN_FUNC(pcblockmove)
  13014. {
  13015. int id, flag;
  13016. TBL_PC *sd = NULL;
  13017. id = script_getnum(st,2);
  13018. flag = script_getnum(st,3);
  13019. if(id)
  13020. sd = map_id2sd(id);
  13021. else
  13022. sd = script_rid2sd(st);
  13023. if(sd)
  13024. sd->state.blockedmove = flag > 0;
  13025. return 0;
  13026. }
  13027. BUILDIN_FUNC(pcfollow)
  13028. {
  13029. int id, targetid;
  13030. TBL_PC *sd = NULL;
  13031. id = script_getnum(st,2);
  13032. targetid = script_getnum(st,3);
  13033. if(id)
  13034. sd = map_id2sd(id);
  13035. else
  13036. sd = script_rid2sd(st);
  13037. if(sd)
  13038. pc_follow(sd, targetid);
  13039. return 0;
  13040. }
  13041. BUILDIN_FUNC(pcstopfollow)
  13042. {
  13043. int id;
  13044. TBL_PC *sd = NULL;
  13045. id = script_getnum(st,2);
  13046. if(id)
  13047. sd = map_id2sd(id);
  13048. else
  13049. sd = script_rid2sd(st);
  13050. if(sd)
  13051. pc_stop_following(sd);
  13052. return 0;
  13053. }
  13054. // <--- [zBuffer] List of player cont commands
  13055. // [zBuffer] List of mob control commands --->
  13056. //## TODO always return if the request/whatever was successfull [FlavioJS]
  13057. /// Makes the unit walk to target position or map
  13058. /// Returns if it was successfull
  13059. ///
  13060. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  13061. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  13062. BUILDIN_FUNC(unitwalk)
  13063. {
  13064. struct block_list* bl;
  13065. bl = map_id2bl(script_getnum(st,2));
  13066. if( bl == NULL )
  13067. {
  13068. script_pushint(st, 0);
  13069. }
  13070. else if( script_hasdata(st,4) )
  13071. {
  13072. int x = script_getnum(st,3);
  13073. int y = script_getnum(st,4);
  13074. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  13075. }
  13076. else
  13077. {
  13078. int map_id = script_getnum(st,3);
  13079. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  13080. }
  13081. return 0;
  13082. }
  13083. /// Kills the unit
  13084. ///
  13085. /// unitkill <unit_id>;
  13086. BUILDIN_FUNC(unitkill)
  13087. {
  13088. struct block_list* bl = map_id2bl(script_getnum(st,2));
  13089. if( bl != NULL )
  13090. status_kill(bl);
  13091. return 0;
  13092. }
  13093. /// Warps the unit to the target position in the target map
  13094. /// Returns if it was successfull
  13095. ///
  13096. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  13097. BUILDIN_FUNC(unitwarp)
  13098. {
  13099. int unit_id;
  13100. int map;
  13101. short x;
  13102. short y;
  13103. struct block_list* bl;
  13104. const char *mapname;
  13105. unit_id = script_getnum(st,2);
  13106. mapname = script_getstr(st, 3);
  13107. x = (short)script_getnum(st,4);
  13108. y = (short)script_getnum(st,5);
  13109. if (!unit_id) //Warp the script's runner
  13110. bl = map_id2bl(st->rid);
  13111. else
  13112. bl = map_id2bl(unit_id);
  13113. if( strcmp(mapname,"this") == 0 )
  13114. map = bl?bl->m:-1;
  13115. else
  13116. map = map_mapname2mapid(mapname);
  13117. if( map >= 0 && bl != NULL )
  13118. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  13119. else
  13120. script_pushint(st, 0);
  13121. return 0;
  13122. }
  13123. /// Makes the unit attack the target.
  13124. /// If the unit is a player and <action type> is not 0, it does a continuous
  13125. /// attack instead of a single attack.
  13126. /// Returns if the request was successfull.
  13127. ///
  13128. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  13129. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  13130. BUILDIN_FUNC(unitattack)
  13131. {
  13132. struct block_list* unit_bl;
  13133. struct block_list* target_bl = NULL;
  13134. struct script_data* data;
  13135. int actiontype = 0;
  13136. // get unit
  13137. unit_bl = map_id2bl(script_getnum(st,2));
  13138. if( unit_bl == NULL ) {
  13139. script_pushint(st, 0);
  13140. return 0;
  13141. }
  13142. data = script_getdata(st, 3);
  13143. get_val(st, data);
  13144. if( data_isstring(data) )
  13145. {
  13146. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  13147. if( sd != NULL )
  13148. target_bl = &sd->bl;
  13149. } else
  13150. target_bl = map_id2bl(conv_num(st, data));
  13151. // request the attack
  13152. if( target_bl == NULL )
  13153. {
  13154. script_pushint(st, 0);
  13155. return 0;
  13156. }
  13157. // get actiontype
  13158. if( script_hasdata(st,4) )
  13159. actiontype = script_getnum(st,4);
  13160. switch( unit_bl->type )
  13161. {
  13162. case BL_PC:
  13163. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  13164. script_pushint(st, 1);
  13165. return 0;
  13166. case BL_MOB:
  13167. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  13168. break;
  13169. case BL_PET:
  13170. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  13171. break;
  13172. default:
  13173. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  13174. script_pushint(st, 0);
  13175. return 1;
  13176. }
  13177. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  13178. return 0;
  13179. }
  13180. /// Makes the unit stop attacking and moving
  13181. ///
  13182. /// unitstop <unit_id>;
  13183. BUILDIN_FUNC(unitstop)
  13184. {
  13185. int unit_id;
  13186. struct block_list* bl;
  13187. unit_id = script_getnum(st,2);
  13188. bl = map_id2bl(unit_id);
  13189. if( bl != NULL )
  13190. {
  13191. unit_stop_attack(bl);
  13192. unit_stop_walking(bl,4);
  13193. if( bl->type == BL_MOB )
  13194. ((TBL_MOB*)bl)->target_id = 0;
  13195. }
  13196. return 0;
  13197. }
  13198. /// Makes the unit say the message
  13199. ///
  13200. /// unittalk <unit_id>,"<message>";
  13201. BUILDIN_FUNC(unittalk)
  13202. {
  13203. int unit_id;
  13204. const char* message;
  13205. struct block_list* bl;
  13206. unit_id = script_getnum(st,2);
  13207. message = script_getstr(st, 3);
  13208. bl = map_id2bl(unit_id);
  13209. if( bl != NULL )
  13210. {
  13211. struct StringBuf sbuf;
  13212. StringBuf_Init(&sbuf);
  13213. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  13214. clif_message(bl, StringBuf_Value(&sbuf));
  13215. if( bl->type == BL_PC )
  13216. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  13217. StringBuf_Destroy(&sbuf);
  13218. }
  13219. return 0;
  13220. }
  13221. /// Makes the unit do an emotion
  13222. ///
  13223. /// unitemote <unit_id>,<emotion>;
  13224. ///
  13225. /// @see e_* in const.txt
  13226. BUILDIN_FUNC(unitemote)
  13227. {
  13228. int unit_id;
  13229. int emotion;
  13230. struct block_list* bl;
  13231. unit_id = script_getnum(st,2);
  13232. emotion = script_getnum(st,3);
  13233. bl = map_id2bl(unit_id);
  13234. if( bl != NULL )
  13235. clif_emotion(bl, emotion);
  13236. return 0;
  13237. }
  13238. /// Makes the unit cast the skill on the target or self if no target is specified
  13239. ///
  13240. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  13241. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  13242. BUILDIN_FUNC(unitskilluseid)
  13243. {
  13244. int unit_id;
  13245. int skill_id;
  13246. int skill_lv;
  13247. int target_id;
  13248. struct block_list* bl;
  13249. unit_id = script_getnum(st,2);
  13250. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13251. skill_lv = script_getnum(st,4);
  13252. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  13253. bl = map_id2bl(unit_id);
  13254. if( bl != NULL )
  13255. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  13256. return 0;
  13257. }
  13258. /// Makes the unit cast the skill on the target position.
  13259. ///
  13260. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  13261. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  13262. BUILDIN_FUNC(unitskillusepos)
  13263. {
  13264. int unit_id;
  13265. int skill_id;
  13266. int skill_lv;
  13267. int skill_x;
  13268. int skill_y;
  13269. struct block_list* bl;
  13270. unit_id = script_getnum(st,2);
  13271. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13272. skill_lv = script_getnum(st,4);
  13273. skill_x = script_getnum(st,5);
  13274. skill_y = script_getnum(st,6);
  13275. bl = map_id2bl(unit_id);
  13276. if( bl != NULL )
  13277. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  13278. return 0;
  13279. }
  13280. // <--- [zBuffer] List of mob control commands
  13281. /// Pauses the execution of the script, detaching the player
  13282. ///
  13283. /// sleep <mili seconds>;
  13284. BUILDIN_FUNC(sleep)
  13285. {
  13286. int ticks;
  13287. ticks = script_getnum(st,2);
  13288. // detach the player
  13289. script_detach_rid(st);
  13290. if( ticks <= 0 )
  13291. {// do nothing
  13292. }
  13293. else if( st->sleep.tick == 0 )
  13294. {// sleep for the target amount of time
  13295. st->state = RERUNLINE;
  13296. st->sleep.tick = ticks;
  13297. }
  13298. else
  13299. {// sleep time is over
  13300. st->state = RUN;
  13301. st->sleep.tick = 0;
  13302. }
  13303. return 0;
  13304. }
  13305. /// Pauses the execution of the script, keeping the player attached
  13306. /// Returns if a player is still attached
  13307. ///
  13308. /// sleep2(<mili secconds>) -> <bool>
  13309. BUILDIN_FUNC(sleep2)
  13310. {
  13311. int ticks;
  13312. ticks = script_getnum(st,2);
  13313. if( ticks <= 0 )
  13314. {// do nothing
  13315. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13316. }
  13317. else if( !st->sleep.tick )
  13318. {// sleep for the target amount of time
  13319. st->state = RERUNLINE;
  13320. st->sleep.tick = ticks;
  13321. }
  13322. else
  13323. {// sleep time is over
  13324. st->state = RUN;
  13325. st->sleep.tick = 0;
  13326. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13327. }
  13328. return 0;
  13329. }
  13330. /// Awakes all the sleep timers of the target npc
  13331. ///
  13332. /// awake "<npc name>";
  13333. BUILDIN_FUNC(awake)
  13334. {
  13335. struct npc_data* nd;
  13336. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  13337. nd = npc_name2id(script_getstr(st, 2));
  13338. if( nd == NULL ) {
  13339. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  13340. return 1;
  13341. }
  13342. while( node )
  13343. {
  13344. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  13345. {// sleep timer for the npc
  13346. struct script_state* tst = (struct script_state*)node->data;
  13347. TBL_PC* sd = map_id2sd(tst->rid);
  13348. if( tst->sleep.timer == INVALID_TIMER )
  13349. {// already awake ???
  13350. node = node->next;
  13351. continue;
  13352. }
  13353. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  13354. {// char not online anymore / another char of the same account is online - Cancel execution
  13355. tst->state = END;
  13356. tst->rid = 0;
  13357. }
  13358. delete_timer(tst->sleep.timer, run_script_timer);
  13359. node = script_erase_sleepdb(node);
  13360. tst->sleep.timer = INVALID_TIMER;
  13361. if(tst->state != RERUNLINE)
  13362. tst->sleep.tick = 0;
  13363. run_script_main(tst);
  13364. }
  13365. else
  13366. {
  13367. node = node->next;
  13368. }
  13369. }
  13370. return 0;
  13371. }
  13372. /// Returns a reference to a variable of the target NPC.
  13373. /// Returns 0 if an error occurs.
  13374. ///
  13375. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  13376. BUILDIN_FUNC(getvariableofnpc)
  13377. {
  13378. struct script_data* data;
  13379. const char* name;
  13380. struct npc_data* nd;
  13381. data = script_getdata(st,2);
  13382. if( !data_isreference(data) )
  13383. {// Not a reference (aka varaible name)
  13384. ShowError("script:getvariableofnpc: not a variable\n");
  13385. script_reportdata(data);
  13386. script_pushnil(st);
  13387. st->state = END;
  13388. return 1;
  13389. }
  13390. name = reference_getname(data);
  13391. if( *name != '.' || name[1] == '@' )
  13392. {// not a npc variable
  13393. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  13394. script_reportdata(data);
  13395. script_pushnil(st);
  13396. st->state = END;
  13397. return 1;
  13398. }
  13399. nd = npc_name2id(script_getstr(st,3));
  13400. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  13401. {// NPC not found or has no script
  13402. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  13403. script_pushnil(st);
  13404. st->state = END;
  13405. return 1;
  13406. }
  13407. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  13408. return 0;
  13409. }
  13410. /// Opens a warp portal.
  13411. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  13412. ///
  13413. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  13414. ///
  13415. /// @author blackhole89
  13416. BUILDIN_FUNC(warpportal)
  13417. {
  13418. int spx;
  13419. int spy;
  13420. unsigned short mapindex;
  13421. int tpx;
  13422. int tpy;
  13423. struct skill_unit_group* group;
  13424. struct block_list* bl;
  13425. bl = map_id2bl(st->oid);
  13426. if( bl == NULL )
  13427. {
  13428. ShowError("script:warpportal: npc is needed\n");
  13429. return 1;
  13430. }
  13431. spx = script_getnum(st,2);
  13432. spy = script_getnum(st,3);
  13433. mapindex = mapindex_name2id(script_getstr(st, 4));
  13434. tpx = script_getnum(st,5);
  13435. tpy = script_getnum(st,6);
  13436. if( mapindex == 0 )
  13437. return 0;// map not found
  13438. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  13439. if( group == NULL )
  13440. return 0;// failed
  13441. group->val2 = (tpx<<16) | tpy;
  13442. group->val3 = mapindex;
  13443. return 0;
  13444. }
  13445. BUILDIN_FUNC(openmail)
  13446. {
  13447. TBL_PC* sd;
  13448. sd = script_rid2sd(st);
  13449. if( sd == NULL )
  13450. return 0;
  13451. mail_openmail(sd);
  13452. return 0;
  13453. }
  13454. BUILDIN_FUNC(openauction)
  13455. {
  13456. TBL_PC* sd;
  13457. sd = script_rid2sd(st);
  13458. if( sd == NULL )
  13459. return 0;
  13460. clif_Auction_openwindow(sd);
  13461. return 0;
  13462. }
  13463. /// Retrieves the value of the specified flag of the specified cell.
  13464. ///
  13465. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  13466. ///
  13467. /// @see cell_chk* constants in const.txt for the types
  13468. BUILDIN_FUNC(checkcell)
  13469. {
  13470. int m = map_mapname2mapid(script_getstr(st,2));
  13471. int x = script_getnum(st,3);
  13472. int y = script_getnum(st,4);
  13473. cell_chk type = (cell_chk)script_getnum(st,5);
  13474. script_pushint(st, map_getcell(m, x, y, type));
  13475. return 0;
  13476. }
  13477. /// Modifies flags of cells in the specified area.
  13478. ///
  13479. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  13480. ///
  13481. /// @see cell_* constants in const.txt for the types
  13482. BUILDIN_FUNC(setcell)
  13483. {
  13484. int m = map_mapname2mapid(script_getstr(st,2));
  13485. int x1 = script_getnum(st,3);
  13486. int y1 = script_getnum(st,4);
  13487. int x2 = script_getnum(st,5);
  13488. int y2 = script_getnum(st,6);
  13489. cell_t type = (cell_t)script_getnum(st,7);
  13490. bool flag = (bool)script_getnum(st,8);
  13491. int x,y;
  13492. if( x1 > x2 ) swap(x1,x2);
  13493. if( y1 > y2 ) swap(y1,y2);
  13494. for( y = y1; y <= y2; ++y )
  13495. for( x = x1; x <= x2; ++x )
  13496. map_setcell(m, x, y, type, flag);
  13497. return 0;
  13498. }
  13499. /*==========================================
  13500. * Mercenary Commands
  13501. *------------------------------------------*/
  13502. BUILDIN_FUNC(mercenary_create)
  13503. {
  13504. struct map_session_data *sd;
  13505. int class_, contract_time;
  13506. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  13507. return 0;
  13508. class_ = script_getnum(st,2);
  13509. if( !merc_class(class_) )
  13510. return 0;
  13511. contract_time = script_getnum(st,3);
  13512. merc_create(sd, class_, contract_time);
  13513. return 0;
  13514. }
  13515. BUILDIN_FUNC(mercenary_heal)
  13516. {
  13517. struct map_session_data *sd = script_rid2sd(st);
  13518. int hp, sp;
  13519. if( sd == NULL || sd->md == NULL )
  13520. return 0;
  13521. hp = script_getnum(st,2);
  13522. sp = script_getnum(st,3);
  13523. status_heal(&sd->md->bl, hp, sp, 0);
  13524. return 0;
  13525. }
  13526. BUILDIN_FUNC(mercenary_sc_start)
  13527. {
  13528. struct map_session_data *sd = script_rid2sd(st);
  13529. enum sc_type type;
  13530. int tick, val1;
  13531. if( sd == NULL || sd->md == NULL )
  13532. return 0;
  13533. type = (sc_type)script_getnum(st,2);
  13534. tick = script_getnum(st,3);
  13535. val1 = script_getnum(st,4);
  13536. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  13537. return 0;
  13538. }
  13539. BUILDIN_FUNC(mercenary_get_calls)
  13540. {
  13541. struct map_session_data *sd = script_rid2sd(st);
  13542. int guild;
  13543. if( sd == NULL )
  13544. return 0;
  13545. guild = script_getnum(st,2);
  13546. switch( guild )
  13547. {
  13548. case ARCH_MERC_GUILD:
  13549. script_pushint(st,sd->status.arch_calls);
  13550. break;
  13551. case SPEAR_MERC_GUILD:
  13552. script_pushint(st,sd->status.spear_calls);
  13553. break;
  13554. case SWORD_MERC_GUILD:
  13555. script_pushint(st,sd->status.sword_calls);
  13556. break;
  13557. default:
  13558. script_pushint(st,0);
  13559. break;
  13560. }
  13561. return 0;
  13562. }
  13563. BUILDIN_FUNC(mercenary_set_calls)
  13564. {
  13565. struct map_session_data *sd = script_rid2sd(st);
  13566. int guild, value, *calls;
  13567. if( sd == NULL )
  13568. return 0;
  13569. guild = script_getnum(st,2);
  13570. value = script_getnum(st,3);
  13571. switch( guild )
  13572. {
  13573. case ARCH_MERC_GUILD:
  13574. calls = &sd->status.arch_calls;
  13575. break;
  13576. case SPEAR_MERC_GUILD:
  13577. calls = &sd->status.spear_calls;
  13578. break;
  13579. case SWORD_MERC_GUILD:
  13580. calls = &sd->status.sword_calls;
  13581. break;
  13582. default:
  13583. return 0; // Invalid Guild
  13584. }
  13585. *calls += value;
  13586. *calls = cap_value(*calls, 0, INT_MAX);
  13587. return 0;
  13588. }
  13589. BUILDIN_FUNC(mercenary_get_faith)
  13590. {
  13591. struct map_session_data *sd = script_rid2sd(st);
  13592. int guild;
  13593. if( sd == NULL )
  13594. return 0;
  13595. guild = script_getnum(st,2);
  13596. switch( guild )
  13597. {
  13598. case ARCH_MERC_GUILD:
  13599. script_pushint(st,sd->status.arch_faith);
  13600. break;
  13601. case SPEAR_MERC_GUILD:
  13602. script_pushint(st,sd->status.spear_faith);
  13603. break;
  13604. case SWORD_MERC_GUILD:
  13605. script_pushint(st,sd->status.sword_faith);
  13606. break;
  13607. default:
  13608. script_pushint(st,0);
  13609. break;
  13610. }
  13611. return 0;
  13612. }
  13613. BUILDIN_FUNC(mercenary_set_faith)
  13614. {
  13615. struct map_session_data *sd = script_rid2sd(st);
  13616. int guild, value, *calls;
  13617. if( sd == NULL )
  13618. return 0;
  13619. guild = script_getnum(st,2);
  13620. value = script_getnum(st,3);
  13621. switch( guild )
  13622. {
  13623. case ARCH_MERC_GUILD:
  13624. calls = &sd->status.arch_faith;
  13625. break;
  13626. case SPEAR_MERC_GUILD:
  13627. calls = &sd->status.spear_faith;
  13628. break;
  13629. case SWORD_MERC_GUILD:
  13630. calls = &sd->status.sword_faith;
  13631. break;
  13632. default:
  13633. return 0; // Invalid Guild
  13634. }
  13635. *calls += value;
  13636. *calls = cap_value(*calls, 0, INT_MAX);
  13637. if( mercenary_get_guild(sd->md) == guild )
  13638. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  13639. return 0;
  13640. }
  13641. /*------------------------------------------
  13642. * Book Reading
  13643. *------------------------------------------*/
  13644. BUILDIN_FUNC(readbook)
  13645. {
  13646. struct map_session_data *sd;
  13647. int book_id, page;
  13648. if( (sd = script_rid2sd(st)) == NULL )
  13649. return 0;
  13650. book_id = script_getnum(st,2);
  13651. page = script_getnum(st,3);
  13652. clif_readbook(sd->fd, book_id, page);
  13653. return 0;
  13654. }
  13655. /******************
  13656. Questlog script commands
  13657. *******************/
  13658. BUILDIN_FUNC(setquest)
  13659. {
  13660. TBL_PC * sd = script_rid2sd(st);
  13661. quest_add(sd, script_getnum(st, 2));
  13662. return 0;
  13663. }
  13664. BUILDIN_FUNC(erasequest)
  13665. {
  13666. TBL_PC * sd = script_rid2sd(st);
  13667. quest_delete(sd, script_getnum(st, 2));
  13668. return 0;
  13669. }
  13670. BUILDIN_FUNC(completequest)
  13671. {
  13672. TBL_PC * sd = script_rid2sd(st);
  13673. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  13674. return 0;
  13675. }
  13676. BUILDIN_FUNC(changequest)
  13677. {
  13678. TBL_PC * sd = script_rid2sd(st);
  13679. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  13680. return 0;
  13681. }
  13682. BUILDIN_FUNC(checkquest)
  13683. {
  13684. TBL_PC * sd = script_rid2sd(st);
  13685. quest_check_type type = HAVEQUEST;
  13686. if( script_hasdata(st, 3) )
  13687. type = (quest_check_type)script_getnum(st, 3);
  13688. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  13689. return 0;
  13690. }
  13691. BUILDIN_FUNC(showevent)
  13692. {
  13693. TBL_PC *sd = script_rid2sd(st);
  13694. struct npc_data *nd = map_id2nd(st->oid);
  13695. int state, color;
  13696. if( sd == NULL || nd == NULL )
  13697. return 0;
  13698. state = script_getnum(st, 2);
  13699. color = script_getnum(st, 3);
  13700. if( color < 0 || color > 3 )
  13701. color = 0; // set default color
  13702. clif_quest_show_event(sd, &nd->bl, state, color);
  13703. return 0;
  13704. }
  13705. /*==========================================
  13706. * BattleGround System
  13707. *------------------------------------------*/
  13708. BUILDIN_FUNC(waitingroom2bg)
  13709. {
  13710. struct npc_data *nd;
  13711. struct chat_data *cd;
  13712. const char *map_name, *ev = "", *dev = "";
  13713. int x, y, i, mapindex = 0, bg_id, n;
  13714. struct map_session_data *sd;
  13715. if( script_hasdata(st,7) )
  13716. nd = npc_name2id(script_getstr(st,7));
  13717. else
  13718. nd = (struct npc_data *)map_id2bl(st->oid);
  13719. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  13720. {
  13721. script_pushint(st,0);
  13722. return 0;
  13723. }
  13724. map_name = script_getstr(st,2);
  13725. if( strcmp(map_name,"-") != 0 )
  13726. {
  13727. mapindex = mapindex_name2id(map_name);
  13728. if( mapindex == 0 )
  13729. { // Invalid Map
  13730. script_pushint(st,0);
  13731. return 0;
  13732. }
  13733. }
  13734. x = script_getnum(st,3);
  13735. y = script_getnum(st,4);
  13736. ev = script_getstr(st,5); // Logout Event
  13737. dev = script_getstr(st,6); // Die Event
  13738. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  13739. { // Creation failed
  13740. script_pushint(st,0);
  13741. return 0;
  13742. }
  13743. n = cd->users;
  13744. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  13745. {
  13746. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  13747. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  13748. else
  13749. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  13750. }
  13751. mapreg_setreg(add_str("$@arenamembersnum"), i);
  13752. script_pushint(st,bg_id);
  13753. return 0;
  13754. }
  13755. BUILDIN_FUNC(waitingroom2bg_single)
  13756. {
  13757. const char* map_name;
  13758. struct npc_data *nd;
  13759. struct chat_data *cd;
  13760. struct map_session_data *sd;
  13761. int x, y, mapindex, bg_id;
  13762. bg_id = script_getnum(st,2);
  13763. map_name = script_getstr(st,3);
  13764. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13765. return 0; // Invalid Map
  13766. x = script_getnum(st,4);
  13767. y = script_getnum(st,5);
  13768. nd = npc_name2id(script_getstr(st,6));
  13769. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  13770. return 0;
  13771. if( (sd = cd->usersd[0]) == NULL )
  13772. return 0;
  13773. if( bg_team_join(bg_id, sd) )
  13774. {
  13775. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  13776. script_pushint(st,1);
  13777. }
  13778. else
  13779. script_pushint(st,0);
  13780. return 0;
  13781. }
  13782. BUILDIN_FUNC(bg_team_setxy)
  13783. {
  13784. struct battleground_data *bg;
  13785. int bg_id;
  13786. bg_id = script_getnum(st,2);
  13787. if( (bg = bg_team_search(bg_id)) == NULL )
  13788. return 0;
  13789. bg->x = script_getnum(st,3);
  13790. bg->y = script_getnum(st,4);
  13791. return 0;
  13792. }
  13793. BUILDIN_FUNC(bg_warp)
  13794. {
  13795. int x, y, mapindex, bg_id;
  13796. const char* map_name;
  13797. bg_id = script_getnum(st,2);
  13798. map_name = script_getstr(st,3);
  13799. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13800. return 0; // Invalid Map
  13801. x = script_getnum(st,4);
  13802. y = script_getnum(st,5);
  13803. bg_team_warp(bg_id, mapindex, x, y);
  13804. return 0;
  13805. }
  13806. BUILDIN_FUNC(bg_monster)
  13807. {
  13808. int class_ = 0, x = 0, y = 0, bg_id = 0;
  13809. const char *str,*map, *evt="";
  13810. bg_id = script_getnum(st,2);
  13811. map = script_getstr(st,3);
  13812. x = script_getnum(st,4);
  13813. y = script_getnum(st,5);
  13814. str = script_getstr(st,6);
  13815. class_ = script_getnum(st,7);
  13816. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  13817. check_event(st, evt);
  13818. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  13819. return 0;
  13820. }
  13821. BUILDIN_FUNC(bg_monster_set_team)
  13822. {
  13823. struct mob_data *md;
  13824. struct block_list *mbl;
  13825. int id = script_getnum(st,2),
  13826. bg_id = script_getnum(st,3);
  13827. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  13828. return 0;
  13829. md = (TBL_MOB *)mbl;
  13830. md->bg_id = bg_id;
  13831. mob_stop_attack(md);
  13832. mob_stop_walking(md, 0);
  13833. md->target_id = md->attacked_id = 0;
  13834. clif_charnameack(0, &md->bl);
  13835. return 0;
  13836. }
  13837. BUILDIN_FUNC(bg_leave)
  13838. {
  13839. struct map_session_data *sd = script_rid2sd(st);
  13840. if( sd == NULL || !sd->bg_id )
  13841. return 0;
  13842. bg_team_leave(sd,0);
  13843. return 0;
  13844. }
  13845. BUILDIN_FUNC(bg_destroy)
  13846. {
  13847. int bg_id = script_getnum(st,2);
  13848. bg_team_delete(bg_id);
  13849. return 0;
  13850. }
  13851. BUILDIN_FUNC(bg_getareausers)
  13852. {
  13853. const char *str;
  13854. int m, x0, y0, x1, y1, bg_id;
  13855. int i = 0, c = 0;
  13856. struct battleground_data *bg = NULL;
  13857. struct map_session_data *sd;
  13858. bg_id = script_getnum(st,2);
  13859. str = script_getstr(st,3);
  13860. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  13861. {
  13862. script_pushint(st,0);
  13863. return 0;
  13864. }
  13865. x0 = script_getnum(st,4);
  13866. y0 = script_getnum(st,5);
  13867. x1 = script_getnum(st,6);
  13868. y1 = script_getnum(st,7);
  13869. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  13870. {
  13871. if( (sd = bg->members[i].sd) == NULL )
  13872. continue;
  13873. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  13874. continue;
  13875. c++;
  13876. }
  13877. script_pushint(st,c);
  13878. return 0;
  13879. }
  13880. BUILDIN_FUNC(bg_updatescore)
  13881. {
  13882. const char *str;
  13883. int m;
  13884. str = script_getstr(st,2);
  13885. if( (m = map_mapname2mapid(str)) < 0 )
  13886. return 0;
  13887. map[m].bgscore_lion = script_getnum(st,3);
  13888. map[m].bgscore_eagle = script_getnum(st,4);
  13889. clif_bg_updatescore(m);
  13890. return 0;
  13891. }
  13892. BUILDIN_FUNC(bg_get_data)
  13893. {
  13894. struct battleground_data *bg;
  13895. int bg_id = script_getnum(st,2),
  13896. type = script_getnum(st,3);
  13897. if( (bg = bg_team_search(bg_id)) == NULL )
  13898. {
  13899. script_pushint(st,0);
  13900. return 0;
  13901. }
  13902. switch( type )
  13903. {
  13904. case 0: script_pushint(st, bg->count); break;
  13905. default:
  13906. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  13907. break;
  13908. }
  13909. return 0;
  13910. }
  13911. /*==========================================
  13912. * Instancing Script Commands
  13913. *------------------------------------------*/
  13914. BUILDIN_FUNC(instance_create)
  13915. {
  13916. const char *name;
  13917. int party_id, res;
  13918. name = script_getstr(st, 2);
  13919. party_id = script_getnum(st, 3);
  13920. res = instance_create(party_id, name);
  13921. if( res == -4 ) // Already exists
  13922. {
  13923. script_pushint(st, -1);
  13924. return 0;
  13925. }
  13926. else if( res < 0 )
  13927. {
  13928. const char *err;
  13929. switch(res)
  13930. {
  13931. case -3: err = "No free instances"; break;
  13932. case -2: err = "Invalid party ID"; break;
  13933. case -1: err = "Invalid type"; break;
  13934. default: err = "Unknown"; break;
  13935. }
  13936. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  13937. script_pushint(st, -2);
  13938. return 0;
  13939. }
  13940. script_pushint(st, res);
  13941. return 0;
  13942. }
  13943. BUILDIN_FUNC(instance_destroy)
  13944. {
  13945. int instance_id;
  13946. struct map_session_data *sd;
  13947. struct party_data *p;
  13948. if( script_hasdata(st, 2) )
  13949. instance_id = script_getnum(st, 2);
  13950. else if( st->instance_id )
  13951. instance_id = st->instance_id;
  13952. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13953. instance_id = p->instance_id;
  13954. else return 0;
  13955. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13956. {
  13957. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  13958. return 0;
  13959. }
  13960. instance_destroy(instance_id);
  13961. return 0;
  13962. }
  13963. BUILDIN_FUNC(instance_attachmap)
  13964. {
  13965. const char *name;
  13966. int m;
  13967. int instance_id;
  13968. bool usebasename = false;
  13969. name = script_getstr(st,2);
  13970. instance_id = script_getnum(st,3);
  13971. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  13972. usebasename = true;
  13973. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  13974. {
  13975. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  13976. script_pushconststr(st, "");
  13977. return 0;
  13978. }
  13979. script_pushconststr(st, map[m].name);
  13980. return 0;
  13981. }
  13982. BUILDIN_FUNC(instance_detachmap)
  13983. {
  13984. struct map_session_data *sd;
  13985. struct party_data *p;
  13986. const char *str;
  13987. int m, instance_id;
  13988. str = script_getstr(st, 2);
  13989. if( script_hasdata(st, 3) )
  13990. instance_id = script_getnum(st, 3);
  13991. else if( st->instance_id )
  13992. instance_id = st->instance_id;
  13993. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13994. instance_id = p->instance_id;
  13995. else return 0;
  13996. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  13997. {
  13998. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  13999. return 0;
  14000. }
  14001. instance_del_map(m);
  14002. return 0;
  14003. }
  14004. BUILDIN_FUNC(instance_attach)
  14005. {
  14006. int instance_id;
  14007. instance_id = script_getnum(st, 2);
  14008. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  14009. return 0;
  14010. st->instance_id = instance_id;
  14011. return 0;
  14012. }
  14013. BUILDIN_FUNC(instance_id)
  14014. {
  14015. int type, instance_id;
  14016. struct map_session_data *sd;
  14017. struct party_data *p;
  14018. if( script_hasdata(st, 2) )
  14019. {
  14020. type = script_getnum(st, 2);
  14021. if( type == 0 )
  14022. instance_id = st->instance_id;
  14023. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  14024. instance_id = p->instance_id;
  14025. else
  14026. instance_id = 0;
  14027. }
  14028. else
  14029. instance_id = st->instance_id;
  14030. script_pushint(st, instance_id);
  14031. return 0;
  14032. }
  14033. BUILDIN_FUNC(instance_set_timeout)
  14034. {
  14035. int progress_timeout, idle_timeout;
  14036. int instance_id;
  14037. struct map_session_data *sd;
  14038. struct party_data *p;
  14039. progress_timeout = script_getnum(st, 2);
  14040. idle_timeout = script_getnum(st, 3);
  14041. if( script_hasdata(st, 4) )
  14042. instance_id = script_getnum(st, 4);
  14043. else if( st->instance_id )
  14044. instance_id = st->instance_id;
  14045. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14046. instance_id = p->instance_id;
  14047. else return 0;
  14048. if( instance_id > 0 )
  14049. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  14050. return 0;
  14051. }
  14052. BUILDIN_FUNC(instance_init)
  14053. {
  14054. int instance_id = script_getnum(st, 2);
  14055. if( instance[instance_id].state != INSTANCE_IDLE )
  14056. {
  14057. ShowError("instance_init: instance already initialized.\n");
  14058. return 0;
  14059. }
  14060. instance_init(instance_id);
  14061. return 0;
  14062. }
  14063. BUILDIN_FUNC(instance_announce)
  14064. {
  14065. int instance_id = script_getnum(st,2);
  14066. const char *mes = script_getstr(st,3);
  14067. int flag = script_getnum(st,4);
  14068. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  14069. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  14070. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  14071. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  14072. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  14073. int i;
  14074. struct map_session_data *sd;
  14075. struct party_data *p;
  14076. if( instance_id == 0 )
  14077. {
  14078. if( st->instance_id )
  14079. instance_id = st->instance_id;
  14080. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14081. instance_id = p->instance_id;
  14082. else return 0;
  14083. }
  14084. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  14085. return 0;
  14086. for( i = 0; i < instance[instance_id].num_map; i++ )
  14087. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  14088. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  14089. return 0;
  14090. }
  14091. BUILDIN_FUNC(instance_npcname)
  14092. {
  14093. const char *str;
  14094. int instance_id = 0;
  14095. struct map_session_data *sd;
  14096. struct party_data *p;
  14097. struct npc_data *nd;
  14098. str = script_getstr(st, 2);
  14099. if( script_hasdata(st, 3) )
  14100. instance_id = script_getnum(st, 3);
  14101. else if( st->instance_id )
  14102. instance_id = st->instance_id;
  14103. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14104. instance_id = p->instance_id;
  14105. if( instance_id && (nd = npc_name2id(str)) != NULL )
  14106. {
  14107. static char npcname[NAME_LENGTH];
  14108. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  14109. script_pushconststr(st,npcname);
  14110. }
  14111. else
  14112. {
  14113. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  14114. st->state = END;
  14115. return 1;
  14116. }
  14117. return 0;
  14118. }
  14119. BUILDIN_FUNC(has_instance)
  14120. {
  14121. struct map_session_data *sd;
  14122. struct party_data *p;
  14123. const char *str;
  14124. int m, instance_id = 0;
  14125. str = script_getstr(st, 2);
  14126. if( script_hasdata(st, 3) )
  14127. instance_id = script_getnum(st, 3);
  14128. else if( st->instance_id )
  14129. instance_id = st->instance_id;
  14130. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14131. instance_id = p->instance_id;
  14132. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  14133. {
  14134. script_pushconststr(st, "");
  14135. return 0;
  14136. }
  14137. script_pushconststr(st, map[m].name);
  14138. return 0;
  14139. }
  14140. BUILDIN_FUNC(instance_warpall)
  14141. {
  14142. struct map_session_data *pl_sd;
  14143. int m, i, instance_id;
  14144. const char *mapn;
  14145. int x, y;
  14146. unsigned short mapindex;
  14147. struct party_data *p = NULL;
  14148. mapn = script_getstr(st,2);
  14149. x = script_getnum(st,3);
  14150. y = script_getnum(st,4);
  14151. if( script_hasdata(st,5) )
  14152. instance_id = script_getnum(st,5);
  14153. else if( st->instance_id )
  14154. instance_id = st->instance_id;
  14155. else if( (pl_sd = script_rid2sd(st)) != NULL && pl_sd->status.party_id && (p = party_search(pl_sd->status.party_id)) != NULL && p->instance_id )
  14156. instance_id = p->instance_id;
  14157. else return 0;
  14158. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  14159. return 0;
  14160. if( !(p = party_search(instance[instance_id].party_id)) )
  14161. return 0;
  14162. mapindex = map_id2index(m);
  14163. for( i = 0; i < MAX_PARTY; i++ )
  14164. if( (pl_sd = p->data[i].sd) && map[pl_sd->bl.m].instance_id == st->instance_id ) pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  14165. return 0;
  14166. }
  14167. /*==========================================
  14168. * instance_check_party [malufett]
  14169. * Values:
  14170. * party_id : Party ID of the invoking character. [Required Parameter]
  14171. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  14172. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  14173. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  14174. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  14175. * Example 2: instance_check_party (getcharid(1),1,1,99);
  14176. *------------------------------------------*/
  14177. BUILDIN_FUNC(instance_check_party)
  14178. {
  14179. struct map_session_data *pl_sd;
  14180. int amount, min, max, i, party_id, c = 0;
  14181. struct party_data *p = NULL;
  14182. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  14183. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  14184. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  14185. if( min < 1 || min > MAX_LEVEL){
  14186. ShowError("instance_check_party: Invalid min level, %d\n", min);
  14187. return 0;
  14188. }else if( max < 1 || max > MAX_LEVEL){
  14189. ShowError("instance_check_party: Invalid max level, %d\n", max);
  14190. return 0;
  14191. }
  14192. if( script_hasdata(st,2) )
  14193. party_id = script_getnum(st,2);
  14194. else return 0;
  14195. if( !(p = party_search(party_id)) ){
  14196. script_pushint(st, 0); // Returns false if party does not exist.
  14197. return 0;
  14198. }
  14199. for( i = 0; i < MAX_PARTY; i++ )
  14200. if( (pl_sd = p->data[i].sd) )
  14201. if(map_id2bl(pl_sd->bl.id)){
  14202. if(pl_sd->status.base_level < min){
  14203. script_pushint(st, 0);
  14204. return 0;
  14205. }else if(pl_sd->status.base_level > max){
  14206. script_pushint(st, 0);
  14207. return 0;
  14208. }
  14209. c++;
  14210. }
  14211. if(c < amount){
  14212. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  14213. }else
  14214. script_pushint(st, 1);
  14215. return 0;
  14216. }
  14217. /*==========================================
  14218. * Custom Fonts
  14219. *------------------------------------------*/
  14220. BUILDIN_FUNC(setfont)
  14221. {
  14222. struct map_session_data *sd = script_rid2sd(st);
  14223. int font = script_getnum(st,2);
  14224. if( sd == NULL )
  14225. return 0;
  14226. if( sd->user_font != font )
  14227. sd->user_font = font;
  14228. else
  14229. sd->user_font = 0;
  14230. clif_font(sd);
  14231. return 0;
  14232. }
  14233. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  14234. {
  14235. TBL_MOB* md = (TBL_MOB*)bl;
  14236. struct block_list *tbl;
  14237. int mobid = va_arg(ap,int);
  14238. int skillid = va_arg(ap,int);
  14239. int skilllv = va_arg(ap,int);
  14240. int casttime = va_arg(ap,int);
  14241. int cancel = va_arg(ap,int);
  14242. int emotion = va_arg(ap,int);
  14243. int target = va_arg(ap,int);
  14244. if( md->class_ != mobid )
  14245. return 0;
  14246. // 0:self, 1:target, 2:master, default:random
  14247. switch( target )
  14248. {
  14249. case 0: tbl = map_id2bl(md->bl.id); break;
  14250. case 1: tbl = map_id2bl(md->target_id); break;
  14251. case 2: tbl = map_id2bl(md->master_id); break;
  14252. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  14253. }
  14254. if( !tbl )
  14255. return 0;
  14256. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  14257. unit_skillcastcancel(bl,0);
  14258. if( skill_get_casttype(skillid) == CAST_GROUND )
  14259. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  14260. else
  14261. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  14262. clif_emotion(&md->bl, emotion);
  14263. return 0;
  14264. }
  14265. /*==========================================
  14266. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  14267. *------------------------------------------*/
  14268. BUILDIN_FUNC(areamobuseskill)
  14269. {
  14270. struct block_list center;
  14271. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  14272. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  14273. {
  14274. ShowError("areamobuseskill: invalid map name.\n");
  14275. return 0;
  14276. }
  14277. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  14278. return 0;
  14279. center.m = m;
  14280. center.x = script_getnum(st,3);
  14281. center.y = script_getnum(st,4);
  14282. range = script_getnum(st,5);
  14283. mobid = script_getnum(st,6);
  14284. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  14285. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  14286. skilllv = battle_config.mob_max_skilllvl;
  14287. casttime = script_getnum(st,9);
  14288. cancel = script_getnum(st,10);
  14289. emotion = script_getnum(st,11);
  14290. target = script_getnum(st,12);
  14291. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  14292. return 0;
  14293. }
  14294. BUILDIN_FUNC(progressbar)
  14295. {
  14296. struct map_session_data * sd = script_rid2sd(st);
  14297. const char * color;
  14298. unsigned int second;
  14299. if( !st || !sd )
  14300. return 0;
  14301. st->state = STOP;
  14302. color = script_getstr(st,2);
  14303. second = script_getnum(st,3);
  14304. sd->progressbar.npc_id = st->oid;
  14305. sd->progressbar.timeout = gettick() + second*1000;
  14306. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  14307. return 0;
  14308. }
  14309. BUILDIN_FUNC(pushpc)
  14310. {
  14311. int direction, cells, dx, dy;
  14312. struct map_session_data* sd;
  14313. if((sd = script_rid2sd(st))==NULL)
  14314. {
  14315. return 0;
  14316. }
  14317. direction = script_getnum(st,2);
  14318. cells = script_getnum(st,3);
  14319. if(direction<0 || direction>7)
  14320. {
  14321. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", direction);
  14322. script_reportsrc(st);
  14323. direction%= 8; // trim spin-over
  14324. }
  14325. if(!cells)
  14326. {// zero distance
  14327. return 0;
  14328. }
  14329. else if(cells<0)
  14330. {// pushing backwards
  14331. direction = (direction+4)%8; // turn around
  14332. cells = -cells;
  14333. }
  14334. dx = dirx[direction];
  14335. dy = diry[direction];
  14336. unit_blown(&sd->bl, dx, dy, cells, 0);
  14337. return 0;
  14338. }
  14339. /// Invokes buying store preparation window
  14340. /// buyingstore <slots>;
  14341. BUILDIN_FUNC(buyingstore)
  14342. {
  14343. struct map_session_data* sd;
  14344. if( ( sd = script_rid2sd(st) ) == NULL )
  14345. {
  14346. return 0;
  14347. }
  14348. buyingstore_setup(sd, script_getnum(st,2));
  14349. return 0;
  14350. }
  14351. /// Invokes search store info window
  14352. /// searchstores <uses>,<effect>;
  14353. BUILDIN_FUNC(searchstores)
  14354. {
  14355. unsigned short effect;
  14356. unsigned int uses;
  14357. struct map_session_data* sd;
  14358. if( ( sd = script_rid2sd(st) ) == NULL )
  14359. {
  14360. return 0;
  14361. }
  14362. uses = script_getnum(st,2);
  14363. effect = script_getnum(st,3);
  14364. if( !uses )
  14365. {
  14366. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  14367. return 1;
  14368. }
  14369. if( effect > 1 )
  14370. {
  14371. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  14372. return 1;
  14373. }
  14374. searchstore_open(sd, uses, effect);
  14375. return 0;
  14376. }
  14377. /// Displays a number as large digital clock.
  14378. /// showdigit <value>[,<type>];
  14379. BUILDIN_FUNC(showdigit)
  14380. {
  14381. unsigned int type = 0;
  14382. int value;
  14383. struct map_session_data* sd;
  14384. if( ( sd = script_rid2sd(st) ) == NULL )
  14385. {
  14386. return 0;
  14387. }
  14388. value = script_getnum(st,2);
  14389. if( script_hasdata(st,3) )
  14390. {
  14391. type = script_getnum(st,3);
  14392. if( type > 3 )
  14393. {
  14394. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  14395. return 1;
  14396. }
  14397. }
  14398. clif_showdigit(sd, (unsigned char)type, value);
  14399. return 0;
  14400. }
  14401. /**
  14402. * Rune Knight
  14403. **/
  14404. BUILDIN_FUNC(makerune) {
  14405. TBL_PC* sd;
  14406. if( (sd = script_rid2sd(st)) == NULL )
  14407. return 0;
  14408. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  14409. sd->itemid = script_getnum(st,2);
  14410. return 0;
  14411. }
  14412. /**
  14413. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  14414. **/
  14415. BUILDIN_FUNC(checkdragon) {
  14416. TBL_PC* sd;
  14417. if( (sd = script_rid2sd(st)) == NULL )
  14418. return 0;
  14419. if( pc_isridingdragon(sd) )
  14420. script_pushint(st,1);
  14421. else
  14422. script_pushint(st,0);
  14423. return 0;
  14424. }
  14425. /**
  14426. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  14427. * - Toggles the dragon on a RK if he can mount;
  14428. * @param Color - when not provided uses the green dragon;
  14429. * - 1 : Green Dragon
  14430. * - 2 : Brown Dragon
  14431. * - 3 : Gray Dragon
  14432. * - 4 : Blue Dragon
  14433. * - 5 : Red Dragon
  14434. **/
  14435. BUILDIN_FUNC(setdragon) {
  14436. TBL_PC* sd;
  14437. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  14438. unsigned int option = OPTION_DRAGON1;
  14439. if( (sd = script_rid2sd(st)) == NULL )
  14440. return 0;
  14441. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  14442. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  14443. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  14444. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  14445. script_pushint(st,1);
  14446. } else {//Not mounted; Mount now.
  14447. if( color ) {
  14448. option = ( color == 1 ? OPTION_DRAGON1 :
  14449. color == 2 ? OPTION_DRAGON2 :
  14450. color == 3 ? OPTION_DRAGON3 :
  14451. color == 4 ? OPTION_DRAGON4 :
  14452. color == 5 ? OPTION_DRAGON5 : 0);
  14453. if( !option ) {
  14454. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  14455. option = OPTION_DRAGON1;
  14456. }
  14457. }
  14458. pc_setoption(sd, sd->sc.option|option);
  14459. script_pushint(st,1);
  14460. }
  14461. return 0;
  14462. }
  14463. /**
  14464. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  14465. **/
  14466. BUILDIN_FUNC(ismounting) {
  14467. TBL_PC* sd;
  14468. if( (sd = script_rid2sd(st)) == NULL )
  14469. return 0;
  14470. if( sd->sc.option&OPTION_MOUNTING )
  14471. script_pushint(st,1);
  14472. else
  14473. script_pushint(st,0);
  14474. return 0;
  14475. }
  14476. /**
  14477. * setmounting() returns 1 on success or 0 otherwise
  14478. * - Toggles new mounts on a player when he can mount
  14479. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  14480. * - Will unmount the player is he is already mounting
  14481. **/
  14482. BUILDIN_FUNC(setmounting) {
  14483. TBL_PC* sd;
  14484. if( (sd = script_rid2sd(st)) == NULL )
  14485. return 0;
  14486. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) )
  14487. script_pushint(st,0);//can't mount with one of these
  14488. else {
  14489. if( sd->sc.option&OPTION_MOUNTING )
  14490. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  14491. else
  14492. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  14493. script_pushint(st,1);//in both cases, return 1.
  14494. }
  14495. return 0;
  14496. }
  14497. /**
  14498. * Retrieves quantity of arguments provided to callfunc/callsub.
  14499. * getargcount() -> amount of arguments received in a function
  14500. **/
  14501. BUILDIN_FUNC(getargcount) {
  14502. struct script_retinfo* ri;
  14503. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  14504. ShowError("script:getargcount: used out of function or callsub label!\n");
  14505. st->state = END;
  14506. return 1;
  14507. }
  14508. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  14509. script_pushint(st, ri->nargs);
  14510. return 0;
  14511. }
  14512. /**
  14513. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  14514. **/
  14515. BUILDIN_FUNC(is_function) {
  14516. const char* str = script_getstr(st,2);
  14517. if( strdb_exists(userfunc_db, str) )
  14518. script_pushint(st,1);
  14519. else
  14520. script_pushint(st,0);
  14521. return 0;
  14522. }
  14523. /**
  14524. * get_revision() -> retrieves the current svn revision (if available)
  14525. **/
  14526. BUILDIN_FUNC(get_revision) {
  14527. const char * revision;
  14528. if ( (revision = get_svn_revision()) != 0 )
  14529. script_pushint(st,atoi(revision));
  14530. else
  14531. script_pushint(st,-1);//unknown
  14532. return 0;
  14533. }
  14534. /**
  14535. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  14536. **/
  14537. BUILDIN_FUNC(freeloop) {
  14538. if( script_getnum(st,2) )
  14539. st->freeloop = 1;
  14540. else
  14541. st->freeloop = 0;
  14542. script_pushint(st, st->freeloop);
  14543. return 0;
  14544. }
  14545. /**
  14546. * @commands (script based)
  14547. **/
  14548. BUILDIN_FUNC(bindatcmd) {
  14549. const char* atcmd;
  14550. const char* eventName;
  14551. int i, level = 0, level2 = 0;
  14552. bool create = false;
  14553. atcmd = script_getstr(st,2);
  14554. eventName = script_getstr(st,3);
  14555. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  14556. atcmd++;
  14557. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  14558. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  14559. if( atcmd_binding_count == 0 ) {
  14560. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  14561. create = true;
  14562. } else {
  14563. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  14564. if( i < atcmd_binding_count ) {/* update existent entry */
  14565. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  14566. atcmd_binding[i]->level = level;
  14567. atcmd_binding[i]->level2 = level2;
  14568. } else
  14569. create = true;
  14570. }
  14571. if( create ) {
  14572. i = atcmd_binding_count;
  14573. if( atcmd_binding_count++ != 0 )
  14574. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  14575. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  14576. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  14577. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  14578. atcmd_binding[i]->level = level;
  14579. atcmd_binding[i]->level2 = level2;
  14580. }
  14581. return 0;
  14582. }
  14583. BUILDIN_FUNC(unbindatcmd) {
  14584. const char* atcmd;
  14585. int i = 0;
  14586. atcmd = script_getstr(st, 2);
  14587. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  14588. atcmd++;
  14589. if( atcmd_binding_count == 0 ) {
  14590. script_pushint(st, 0);
  14591. return 0;
  14592. }
  14593. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  14594. if( i < atcmd_binding_count ) {
  14595. int cursor = 0;
  14596. aFree(atcmd_binding[i]);
  14597. atcmd_binding[i] = NULL;
  14598. /* compact the list now that we freed a slot somewhere */
  14599. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  14600. if( atcmd_binding[i] == NULL )
  14601. continue;
  14602. if( cursor != i ) {
  14603. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  14604. }
  14605. cursor++;
  14606. }
  14607. if( (atcmd_binding_count = cursor) == 0 )
  14608. aFree(atcmd_binding);
  14609. script_pushint(st, 1);
  14610. } else
  14611. script_pushint(st, 0);/* not found */
  14612. return 0;
  14613. }
  14614. BUILDIN_FUNC(useatcmd)
  14615. {
  14616. TBL_PC dummy_sd;
  14617. TBL_PC* sd;
  14618. int fd;
  14619. const char* cmd;
  14620. cmd = script_getstr(st,2);
  14621. if( st->rid )
  14622. {
  14623. sd = script_rid2sd(st);
  14624. fd = sd->fd;
  14625. }
  14626. else
  14627. { // Use a dummy character.
  14628. sd = &dummy_sd;
  14629. fd = 0;
  14630. memset(&dummy_sd, 0, sizeof(TBL_PC));
  14631. if( st->oid )
  14632. {
  14633. struct block_list* bl = map_id2bl(st->oid);
  14634. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  14635. if( bl->type == BL_NPC )
  14636. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  14637. }
  14638. }
  14639. // compatibility with previous implementation (deprecated!)
  14640. if( cmd[0] != atcommand_symbol )
  14641. {
  14642. cmd += strlen(sd->status.name);
  14643. while( *cmd != atcommand_symbol && *cmd != 0 )
  14644. cmd++;
  14645. }
  14646. is_atcommand(fd, sd, cmd, 1);
  14647. return 0;
  14648. }
  14649. BUILDIN_FUNC(checkre)
  14650. {
  14651. int num;
  14652. num=script_getnum(st,2);
  14653. switch(num){
  14654. case 0:
  14655. #ifdef RENEWAL
  14656. script_pushint(st, 1);
  14657. #else
  14658. script_pushint(st, 0);
  14659. #endif
  14660. break;
  14661. case 1:
  14662. #ifdef RENEWAL_CAST
  14663. script_pushint(st, 1);
  14664. #else
  14665. script_pushint(st, 0);
  14666. #endif
  14667. break;
  14668. case 2:
  14669. #ifdef RENEWAL_DROP
  14670. script_pushint(st, 1);
  14671. #else
  14672. script_pushint(st, 0);
  14673. #endif
  14674. break;
  14675. case 3:
  14676. #ifdef RENEWAL_EXP
  14677. script_pushint(st, 1);
  14678. #else
  14679. script_pushint(st, 0);
  14680. #endif
  14681. break;
  14682. case 4:
  14683. #ifdef RENEWAL_LVDMG
  14684. script_pushint(st, 1);
  14685. #else
  14686. script_pushint(st, 0);
  14687. #endif
  14688. break;
  14689. case 5:
  14690. #ifdef RENEWAL_EDP
  14691. script_pushint(st, 1);
  14692. #else
  14693. script_pushint(st, 0);
  14694. #endif
  14695. break;
  14696. case 6:
  14697. #ifdef RENEWAL_ASPD
  14698. script_pushint(st, 1);
  14699. #else
  14700. script_pushint(st, 0);
  14701. #endif
  14702. break;
  14703. default:
  14704. ShowWarning("buildin_checkre: unknown parameter.\n");
  14705. break;
  14706. }
  14707. return 0;
  14708. }
  14709. /* getrandgroupitem <group_id>,<quantity> */
  14710. BUILDIN_FUNC(getrandgroupitem) {
  14711. TBL_PC* sd;
  14712. int i, get_count = 0, flag, nameid, group = script_getnum(st, 2), qty = script_getnum(st,3);
  14713. struct item item_tmp;
  14714. if( !( sd = script_rid2sd(st) ) )
  14715. return 0;
  14716. if( qty <= 0 ) {
  14717. ShowError("getrandgroupitem: qty is <= 0!\n");
  14718. return 1;
  14719. }
  14720. if( (nameid = itemdb_searchrandomid(group)) == UNKNOWN_ITEM_ID ) {
  14721. return 1;/* itemdb_searchrandomid will already scream a error */
  14722. }
  14723. memset(&item_tmp,0,sizeof(item_tmp));
  14724. item_tmp.nameid = nameid;
  14725. item_tmp.identify = itemdb_isidentified(nameid);
  14726. //Check if it's stackable.
  14727. if (!itemdb_isstackable(nameid))
  14728. get_count = 1;
  14729. else
  14730. get_count = qty;
  14731. for (i = 0; i < qty; i += get_count) {
  14732. // if not pet egg
  14733. if (!pet_create_egg(sd, nameid)) {
  14734. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT))) {
  14735. clif_additem(sd, 0, 0, flag);
  14736. if( pc_candrop(sd,&item_tmp) )
  14737. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  14738. }
  14739. }
  14740. }
  14741. return 0;
  14742. }
  14743. // declarations that were supposed to be exported from npc_chat.c
  14744. #ifdef PCRE_SUPPORT
  14745. BUILDIN_FUNC(defpattern);
  14746. BUILDIN_FUNC(activatepset);
  14747. BUILDIN_FUNC(deactivatepset);
  14748. BUILDIN_FUNC(deletepset);
  14749. #endif
  14750. /// script command definitions
  14751. /// for an explanation on args, see add_buildin_func
  14752. struct script_function buildin_func[] = {
  14753. // NPC interaction
  14754. BUILDIN_DEF(mes,"s*"),
  14755. BUILDIN_DEF(next,""),
  14756. BUILDIN_DEF(close,""),
  14757. BUILDIN_DEF(close2,""),
  14758. BUILDIN_DEF(menu,"sl*"),
  14759. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  14760. BUILDIN_DEF(prompt,"s*"),
  14761. //
  14762. BUILDIN_DEF(goto,"l"),
  14763. BUILDIN_DEF(callsub,"l*"),
  14764. BUILDIN_DEF(callfunc,"s*"),
  14765. BUILDIN_DEF(return,"?"),
  14766. BUILDIN_DEF(getarg,"i?"),
  14767. BUILDIN_DEF(jobchange,"i?"),
  14768. BUILDIN_DEF(jobname,"i"),
  14769. BUILDIN_DEF(input,"r??"),
  14770. BUILDIN_DEF(warp,"sii"),
  14771. BUILDIN_DEF(areawarp,"siiiisii??"),
  14772. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  14773. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  14774. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  14775. BUILDIN_DEF(setlook,"ii"),
  14776. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  14777. BUILDIN_DEF(set,"rv"),
  14778. BUILDIN_DEF(setarray,"rv*"),
  14779. BUILDIN_DEF(cleararray,"rvi"),
  14780. BUILDIN_DEF(copyarray,"rri"),
  14781. BUILDIN_DEF(getarraysize,"r"),
  14782. BUILDIN_DEF(deletearray,"r?"),
  14783. BUILDIN_DEF(getelementofarray,"ri"),
  14784. BUILDIN_DEF(getitem,"vi?"),
  14785. BUILDIN_DEF(rentitem,"vi"),
  14786. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  14787. BUILDIN_DEF(getnameditem,"vv"),
  14788. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  14789. BUILDIN_DEF(makeitem,"visii"),
  14790. BUILDIN_DEF(delitem,"vi?"),
  14791. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  14792. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  14793. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  14794. BUILDIN_DEF(cutin,"si"),
  14795. BUILDIN_DEF(viewpoint,"iiiii"),
  14796. BUILDIN_DEF(heal,"ii"),
  14797. BUILDIN_DEF(itemheal,"ii"),
  14798. BUILDIN_DEF(percentheal,"ii"),
  14799. BUILDIN_DEF(rand,"i?"),
  14800. BUILDIN_DEF(countitem,"v"),
  14801. BUILDIN_DEF(countitem2,"viiiiiii"),
  14802. BUILDIN_DEF(checkweight,"vi"),
  14803. BUILDIN_DEF(readparam,"i?"),
  14804. BUILDIN_DEF(getcharid,"i?"),
  14805. BUILDIN_DEF(getnpcid,"i?"),
  14806. BUILDIN_DEF(getpartyname,"i"),
  14807. BUILDIN_DEF(getpartymember,"i?"),
  14808. BUILDIN_DEF(getpartyleader,"i?"),
  14809. BUILDIN_DEF(getguildname,"i"),
  14810. BUILDIN_DEF(getguildmaster,"i"),
  14811. BUILDIN_DEF(getguildmasterid,"i"),
  14812. BUILDIN_DEF(strcharinfo,"i"),
  14813. BUILDIN_DEF(strnpcinfo,"i"),
  14814. BUILDIN_DEF(getequipid,"i"),
  14815. BUILDIN_DEF(getequipname,"i"),
  14816. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  14817. BUILDIN_DEF(repair,"i"), // [Valaris]
  14818. BUILDIN_DEF(repairall,""),
  14819. BUILDIN_DEF(getequipisequiped,"i"),
  14820. BUILDIN_DEF(getequipisenableref,"i"),
  14821. BUILDIN_DEF(getequipisidentify,"i"),
  14822. BUILDIN_DEF(getequiprefinerycnt,"i"),
  14823. BUILDIN_DEF(getequipweaponlv,"i"),
  14824. BUILDIN_DEF(getequippercentrefinery,"i"),
  14825. BUILDIN_DEF(successrefitem,"i"),
  14826. BUILDIN_DEF(failedrefitem,"i"),
  14827. BUILDIN_DEF(downrefitem,"i"),
  14828. BUILDIN_DEF(statusup,"i"),
  14829. BUILDIN_DEF(statusup2,"ii"),
  14830. BUILDIN_DEF(bonus,"iv"),
  14831. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  14832. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  14833. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  14834. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  14835. BUILDIN_DEF(autobonus,"sii??"),
  14836. BUILDIN_DEF(autobonus2,"sii??"),
  14837. BUILDIN_DEF(autobonus3,"siiv?"),
  14838. BUILDIN_DEF(skill,"vi?"),
  14839. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  14840. BUILDIN_DEF(guildskill,"vi"),
  14841. BUILDIN_DEF(getskilllv,"v"),
  14842. BUILDIN_DEF(getgdskilllv,"iv"),
  14843. BUILDIN_DEF(basicskillcheck,""),
  14844. BUILDIN_DEF(getgmlevel,""),
  14845. BUILDIN_DEF(getgroupid,""),
  14846. BUILDIN_DEF(end,""),
  14847. BUILDIN_DEF(checkoption,"i"),
  14848. BUILDIN_DEF(setoption,"i?"),
  14849. BUILDIN_DEF(setcart,"?"),
  14850. BUILDIN_DEF(checkcart,""),
  14851. BUILDIN_DEF(setfalcon,"?"),
  14852. BUILDIN_DEF(checkfalcon,""),
  14853. BUILDIN_DEF(setriding,"?"),
  14854. BUILDIN_DEF(checkriding,""),
  14855. BUILDIN_DEF(checkwug,""),
  14856. BUILDIN_DEF(checkmadogear,""),
  14857. BUILDIN_DEF(setmadogear,"?"),
  14858. BUILDIN_DEF2(savepoint,"save","sii"),
  14859. BUILDIN_DEF(savepoint,"sii"),
  14860. BUILDIN_DEF(gettimetick,"i"),
  14861. BUILDIN_DEF(gettime,"i"),
  14862. BUILDIN_DEF(gettimestr,"si"),
  14863. BUILDIN_DEF(openstorage,""),
  14864. BUILDIN_DEF(guildopenstorage,""),
  14865. BUILDIN_DEF(itemskill,"vi"),
  14866. BUILDIN_DEF(produce,"i"),
  14867. BUILDIN_DEF(cooking,"i"),
  14868. BUILDIN_DEF(monster,"siisii?"),
  14869. BUILDIN_DEF(getmobdrops,"i"),
  14870. BUILDIN_DEF(areamonster,"siiiisii?"),
  14871. BUILDIN_DEF(killmonster,"ss?"),
  14872. BUILDIN_DEF(killmonsterall,"s?"),
  14873. BUILDIN_DEF(clone,"siisi????"),
  14874. BUILDIN_DEF(doevent,"s"),
  14875. BUILDIN_DEF(donpcevent,"s"),
  14876. BUILDIN_DEF(cmdothernpc,"ss"),
  14877. BUILDIN_DEF(addtimer,"is"),
  14878. BUILDIN_DEF(deltimer,"s"),
  14879. BUILDIN_DEF(addtimercount,"si"),
  14880. BUILDIN_DEF(initnpctimer,"??"),
  14881. BUILDIN_DEF(stopnpctimer,"??"),
  14882. BUILDIN_DEF(startnpctimer,"??"),
  14883. BUILDIN_DEF(setnpctimer,"i?"),
  14884. BUILDIN_DEF(getnpctimer,"i?"),
  14885. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  14886. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  14887. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  14888. BUILDIN_DEF(announce,"si?????"),
  14889. BUILDIN_DEF(mapannounce,"ssi?????"),
  14890. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  14891. BUILDIN_DEF(getusers,"i"),
  14892. BUILDIN_DEF(getmapguildusers,"si"),
  14893. BUILDIN_DEF(getmapusers,"s"),
  14894. BUILDIN_DEF(getareausers,"siiii"),
  14895. BUILDIN_DEF(getareadropitem,"siiiiv"),
  14896. BUILDIN_DEF(enablenpc,"s"),
  14897. BUILDIN_DEF(disablenpc,"s"),
  14898. BUILDIN_DEF(hideoffnpc,"s"),
  14899. BUILDIN_DEF(hideonnpc,"s"),
  14900. BUILDIN_DEF(sc_start,"iii?"),
  14901. BUILDIN_DEF(sc_start2,"iiii?"),
  14902. BUILDIN_DEF(sc_start4,"iiiiii?"),
  14903. BUILDIN_DEF(sc_end,"i?"),
  14904. BUILDIN_DEF(getstatus, "i?"),
  14905. BUILDIN_DEF(getscrate,"ii?"),
  14906. BUILDIN_DEF(debugmes,"s"),
  14907. BUILDIN_DEF2(catchpet,"pet","i"),
  14908. BUILDIN_DEF2(birthpet,"bpet",""),
  14909. BUILDIN_DEF(resetlvl,"i"),
  14910. BUILDIN_DEF(resetstatus,""),
  14911. BUILDIN_DEF(resetskill,""),
  14912. BUILDIN_DEF(skillpointcount,""),
  14913. BUILDIN_DEF(changebase,"i?"),
  14914. BUILDIN_DEF(changesex,""),
  14915. BUILDIN_DEF(waitingroom,"si?????"),
  14916. BUILDIN_DEF(delwaitingroom,"?"),
  14917. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  14918. BUILDIN_DEF(enablewaitingroomevent,"?"),
  14919. BUILDIN_DEF(disablewaitingroomevent,"?"),
  14920. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  14921. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  14922. BUILDIN_DEF(getwaitingroomstate,"i?"),
  14923. BUILDIN_DEF(warpwaitingpc,"sii?"),
  14924. BUILDIN_DEF(attachrid,"i"),
  14925. BUILDIN_DEF(detachrid,""),
  14926. BUILDIN_DEF(isloggedin,"i?"),
  14927. BUILDIN_DEF(setmapflagnosave,"ssii"),
  14928. BUILDIN_DEF(getmapflag,"si"),
  14929. BUILDIN_DEF(setmapflag,"si?"),
  14930. BUILDIN_DEF(removemapflag,"si?"),
  14931. BUILDIN_DEF(pvpon,"s"),
  14932. BUILDIN_DEF(pvpoff,"s"),
  14933. BUILDIN_DEF(gvgon,"s"),
  14934. BUILDIN_DEF(gvgoff,"s"),
  14935. BUILDIN_DEF(emotion,"i??"),
  14936. BUILDIN_DEF(maprespawnguildid,"sii"),
  14937. BUILDIN_DEF(agitstart,""), // <Agit>
  14938. BUILDIN_DEF(agitend,""),
  14939. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  14940. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  14941. BUILDIN_DEF(getcastlename,"s"),
  14942. BUILDIN_DEF(getcastledata,"si"),
  14943. BUILDIN_DEF(setcastledata,"sii"),
  14944. BUILDIN_DEF(requestguildinfo,"i?"),
  14945. BUILDIN_DEF(getequipcardcnt,"i"),
  14946. BUILDIN_DEF(successremovecards,"i"),
  14947. BUILDIN_DEF(failedremovecards,"ii"),
  14948. BUILDIN_DEF(marriage,"s"),
  14949. BUILDIN_DEF2(wedding_effect,"wedding",""),
  14950. BUILDIN_DEF(divorce,""),
  14951. BUILDIN_DEF(ispartneron,""),
  14952. BUILDIN_DEF(getpartnerid,""),
  14953. BUILDIN_DEF(getchildid,""),
  14954. BUILDIN_DEF(getmotherid,""),
  14955. BUILDIN_DEF(getfatherid,""),
  14956. BUILDIN_DEF(warppartner,"sii"),
  14957. BUILDIN_DEF(getitemname,"v"),
  14958. BUILDIN_DEF(getitemslots,"i"),
  14959. BUILDIN_DEF(makepet,"i"),
  14960. BUILDIN_DEF(getexp,"ii"),
  14961. BUILDIN_DEF(getinventorylist,""),
  14962. BUILDIN_DEF(getskilllist,""),
  14963. BUILDIN_DEF(clearitem,""),
  14964. BUILDIN_DEF(classchange,"ii"),
  14965. BUILDIN_DEF(misceffect,"i"),
  14966. BUILDIN_DEF(playBGM,"s"),
  14967. BUILDIN_DEF(playBGMall,"s?????"),
  14968. BUILDIN_DEF(soundeffect,"si"),
  14969. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  14970. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  14971. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  14972. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  14973. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  14974. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  14975. BUILDIN_DEF(petloot,"i"), // [Valaris]
  14976. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  14977. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  14978. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  14979. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  14980. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  14981. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  14982. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  14983. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  14984. BUILDIN_DEF(nude,""), // nude command [Valaris]
  14985. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  14986. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  14987. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  14988. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  14989. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  14990. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  14991. BUILDIN_DEF(mobcount,"ss"),
  14992. BUILDIN_DEF(getlook,"i"),
  14993. BUILDIN_DEF(getsavepoint,"i"),
  14994. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  14995. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  14996. BUILDIN_DEF(npcstop,""), // [Valaris]
  14997. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  14998. BUILDIN_DEF(checkoption1,"i"),
  14999. BUILDIN_DEF(checkoption2,"i"),
  15000. BUILDIN_DEF(guildgetexp,"i"),
  15001. BUILDIN_DEF(guildchangegm,"is"),
  15002. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  15003. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  15004. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  15005. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  15006. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  15007. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  15008. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  15009. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  15010. BUILDIN_DEF(night,""), // sets the server to night time
  15011. BUILDIN_DEF(day,""), // sets the server to day time
  15012. #ifdef PCRE_SUPPORT
  15013. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  15014. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  15015. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  15016. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  15017. #endif
  15018. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  15019. BUILDIN_DEF(getusersname,""),
  15020. BUILDIN_DEF(recovery,""),
  15021. BUILDIN_DEF(getpetinfo,"i"),
  15022. BUILDIN_DEF(gethominfo,"i"),
  15023. BUILDIN_DEF(getmercinfo,"i?"),
  15024. BUILDIN_DEF(checkequipedcard,"i"),
  15025. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  15026. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  15027. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  15028. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  15029. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  15030. BUILDIN_DEF(charat,"si"),
  15031. BUILDIN_DEF(setchar,"ssi"),
  15032. BUILDIN_DEF(insertchar,"ssi"),
  15033. BUILDIN_DEF(delchar,"si"),
  15034. BUILDIN_DEF(strtoupper,"s"),
  15035. BUILDIN_DEF(strtolower,"s"),
  15036. BUILDIN_DEF(charisupper, "si"),
  15037. BUILDIN_DEF(charislower, "si"),
  15038. BUILDIN_DEF(substr,"sii"),
  15039. BUILDIN_DEF(explode, "rss"),
  15040. BUILDIN_DEF(implode, "r?"),
  15041. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  15042. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  15043. BUILDIN_DEF(strpos,"ss?"),
  15044. BUILDIN_DEF(replacestr,"sss??"),
  15045. BUILDIN_DEF(countstr,"ss?"),
  15046. BUILDIN_DEF(setnpcdisplay,"sv??"),
  15047. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  15048. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  15049. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  15050. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  15051. // [zBuffer] List of mathematics commands --->
  15052. BUILDIN_DEF(sqrt,"i"),
  15053. BUILDIN_DEF(pow,"ii"),
  15054. BUILDIN_DEF(distance,"iiii"),
  15055. // <--- [zBuffer] List of mathematics commands
  15056. BUILDIN_DEF(md5,"s"),
  15057. // [zBuffer] List of dynamic var commands --->
  15058. BUILDIN_DEF(getd,"s"),
  15059. BUILDIN_DEF(setd,"sv"),
  15060. // <--- [zBuffer] List of dynamic var commands
  15061. BUILDIN_DEF(petstat,"i"),
  15062. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  15063. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  15064. BUILDIN_DEF(npcshopadditem,"sii*"),
  15065. BUILDIN_DEF(npcshopdelitem,"si*"),
  15066. BUILDIN_DEF(npcshopattach,"s?"),
  15067. BUILDIN_DEF(equip,"i"),
  15068. BUILDIN_DEF(autoequip,"ii"),
  15069. BUILDIN_DEF(setbattleflag,"si"),
  15070. BUILDIN_DEF(getbattleflag,"s"),
  15071. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  15072. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  15073. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  15074. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  15075. BUILDIN_DEF(axtoi,"s"),
  15076. BUILDIN_DEF(query_sql,"s*"),
  15077. BUILDIN_DEF(query_logsql,"s*"),
  15078. BUILDIN_DEF(escape_sql,"v"),
  15079. BUILDIN_DEF(atoi,"s"),
  15080. // [zBuffer] List of player cont commands --->
  15081. BUILDIN_DEF(rid2name,"i"),
  15082. BUILDIN_DEF(pcfollow,"ii"),
  15083. BUILDIN_DEF(pcstopfollow,"i"),
  15084. BUILDIN_DEF(pcblockmove,"ii"),
  15085. // <--- [zBuffer] List of player cont commands
  15086. // [zBuffer] List of mob control commands --->
  15087. BUILDIN_DEF(unitwalk,"ii?"),
  15088. BUILDIN_DEF(unitkill,"i"),
  15089. BUILDIN_DEF(unitwarp,"isii"),
  15090. BUILDIN_DEF(unitattack,"iv?"),
  15091. BUILDIN_DEF(unitstop,"i"),
  15092. BUILDIN_DEF(unittalk,"is"),
  15093. BUILDIN_DEF(unitemote,"ii"),
  15094. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  15095. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  15096. // <--- [zBuffer] List of mob control commands
  15097. BUILDIN_DEF(sleep,"i"),
  15098. BUILDIN_DEF(sleep2,"i"),
  15099. BUILDIN_DEF(awake,"s"),
  15100. BUILDIN_DEF(getvariableofnpc,"rs"),
  15101. BUILDIN_DEF(warpportal,"iisii"),
  15102. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  15103. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  15104. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  15105. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  15106. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  15107. BUILDIN_DEF(checkvending,"?"),
  15108. BUILDIN_DEF(checkchatting,"?"),
  15109. BUILDIN_DEF(openmail,""),
  15110. BUILDIN_DEF(openauction,""),
  15111. BUILDIN_DEF(checkcell,"siii"),
  15112. BUILDIN_DEF(setcell,"siiiiii"),
  15113. BUILDIN_DEF(setwall,"siiiiis"),
  15114. BUILDIN_DEF(delwall,"s"),
  15115. BUILDIN_DEF(searchitem,"rs"),
  15116. BUILDIN_DEF(mercenary_create,"ii"),
  15117. BUILDIN_DEF(mercenary_heal,"ii"),
  15118. BUILDIN_DEF(mercenary_sc_start,"iii"),
  15119. BUILDIN_DEF(mercenary_get_calls,"i"),
  15120. BUILDIN_DEF(mercenary_get_faith,"i"),
  15121. BUILDIN_DEF(mercenary_set_calls,"ii"),
  15122. BUILDIN_DEF(mercenary_set_faith,"ii"),
  15123. BUILDIN_DEF(readbook,"ii"),
  15124. BUILDIN_DEF(setfont,"i"),
  15125. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  15126. BUILDIN_DEF(progressbar,"si"),
  15127. BUILDIN_DEF(pushpc,"ii"),
  15128. BUILDIN_DEF(buyingstore,"i"),
  15129. BUILDIN_DEF(searchstores,"ii"),
  15130. BUILDIN_DEF(showdigit,"i?"),
  15131. // WoE SE
  15132. BUILDIN_DEF(agitstart2,""),
  15133. BUILDIN_DEF(agitend2,""),
  15134. BUILDIN_DEF(agitcheck2,""),
  15135. // BattleGround
  15136. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  15137. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  15138. BUILDIN_DEF(bg_team_setxy,"iii"),
  15139. BUILDIN_DEF(bg_warp,"isii"),
  15140. BUILDIN_DEF(bg_monster,"isiisi?"),
  15141. BUILDIN_DEF(bg_monster_set_team,"ii"),
  15142. BUILDIN_DEF(bg_leave,""),
  15143. BUILDIN_DEF(bg_destroy,"i"),
  15144. BUILDIN_DEF(areapercentheal,"siiiiii"),
  15145. BUILDIN_DEF(bg_get_data,"ii"),
  15146. BUILDIN_DEF(bg_getareausers,"isiiii"),
  15147. BUILDIN_DEF(bg_updatescore,"sii"),
  15148. // Instancing
  15149. BUILDIN_DEF(instance_create,"si"),
  15150. BUILDIN_DEF(instance_destroy,"?"),
  15151. BUILDIN_DEF(instance_attachmap,"si?"),
  15152. BUILDIN_DEF(instance_detachmap,"s?"),
  15153. BUILDIN_DEF(instance_attach,"i"),
  15154. BUILDIN_DEF(instance_id,"?"),
  15155. BUILDIN_DEF(instance_set_timeout,"ii?"),
  15156. BUILDIN_DEF(instance_init,"i"),
  15157. BUILDIN_DEF(instance_announce,"isi?????"),
  15158. BUILDIN_DEF(instance_npcname,"s?"),
  15159. BUILDIN_DEF(has_instance,"s?"),
  15160. BUILDIN_DEF(instance_warpall,"sii?"),
  15161. BUILDIN_DEF(instance_check_party,"i???"),
  15162. /**
  15163. * 3rd-related
  15164. **/
  15165. BUILDIN_DEF(makerune,"i"),
  15166. BUILDIN_DEF(checkdragon,""),//[Ind]
  15167. BUILDIN_DEF(setdragon,"?"),//[Ind]
  15168. BUILDIN_DEF(ismounting,""),//[Ind]
  15169. BUILDIN_DEF(setmounting,""),//[Ind]
  15170. BUILDIN_DEF(checkre,"i"),
  15171. /**
  15172. * rAthena and beyond!
  15173. **/
  15174. BUILDIN_DEF(getargcount,""),
  15175. BUILDIN_DEF(is_function,"s"),
  15176. BUILDIN_DEF(get_revision,""),
  15177. BUILDIN_DEF(freeloop,"i"),
  15178. BUILDIN_DEF(getrandgroupitem, "ii"),
  15179. /**
  15180. * @commands (script based)
  15181. **/
  15182. BUILDIN_DEF(bindatcmd, "ss??"),
  15183. BUILDIN_DEF(unbindatcmd, "s"),
  15184. BUILDIN_DEF(useatcmd, "s"),
  15185. //Quest Log System [Inkfish]
  15186. BUILDIN_DEF(setquest, "i"),
  15187. BUILDIN_DEF(erasequest, "i"),
  15188. BUILDIN_DEF(completequest, "i"),
  15189. BUILDIN_DEF(checkquest, "i?"),
  15190. BUILDIN_DEF(changequest, "ii"),
  15191. BUILDIN_DEF(showevent, "ii"),
  15192. {NULL,NULL,NULL},
  15193. };