script.c 323 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_FUNCIN
  4. //#define DEBUG_DISP
  5. //#define DEBUG_DISASM
  6. //#define DEBUG_RUN
  7. //#define DEBUG_HASH
  8. #include "../common/cbasetypes.h"
  9. #include "../common/timer.h"
  10. #include "../common/malloc.h"
  11. #include "../common/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/strlib.h"
  15. #include "../common/utils.h"
  16. #include "map.h"
  17. #include "clif.h"
  18. #include "chrif.h"
  19. #include "itemdb.h"
  20. #include "pc.h"
  21. #include "status.h"
  22. #include "storage.h"
  23. #include "mob.h"
  24. #include "npc.h"
  25. #include "pet.h"
  26. #include "mercenary.h" //[orn]
  27. #include "intif.h"
  28. #include "skill.h"
  29. #include "chat.h"
  30. #include "battle.h"
  31. #include "party.h"
  32. #include "guild.h"
  33. #include "atcommand.h"
  34. #include "charcommand.h"
  35. #include "log.h"
  36. #include "unit.h"
  37. #include "irc.h"
  38. #include "pet.h"
  39. #include "mail.h"
  40. #include "script.h"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <math.h>
  45. #ifndef WIN32
  46. #include <sys/time.h>
  47. #endif
  48. #include <time.h>
  49. #include <setjmp.h>
  50. #include <errno.h>
  51. ///////////////////////////////////////////////////////////////////////////////
  52. //## TODO possible enhancements: [FlavioJS]
  53. // - 'callfunc' supporting labels in the current npc "::LabelName"
  54. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  55. // - 'function FuncName;' function declarations reverting to global functions
  56. // if local label isn't found
  57. // - join callfunc and callsub's functionality
  58. //
  59. // struct script_state* st;
  60. //
  61. /// Returns the script_data at the target index
  62. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  63. /// Returns if the stack contains data at the target index
  64. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  65. /// Returns the index of the last data in the stack
  66. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  67. /// Pushes an int into the stack
  68. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  69. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  70. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  71. /// Pushes a nil into the stack
  72. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  73. /// Pushes a copy of the data in the target index
  74. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  75. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  76. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  77. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  78. // Note: "top" functions/defines use indexes relative to the top of the stack
  79. // -1 is the index of the data at the top
  80. /// Returns the script_data at the target index relative to the top of the stack
  81. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  82. /// Pushes a copy of the data in the target index relative to the top of the stack
  83. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  84. /// Removes the range of values [start,end[ relative to the top of the stack
  85. #define script_removetop(st,start,end) ( pop_stack((st)->stack, ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  86. //
  87. // struct script_data* data;
  88. //
  89. /// Returns if the script data is a string
  90. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  91. /// Returns if the script data is an int
  92. #define data_isint(data) ( (data)->type == C_INT )
  93. /// Returns if the script data is a reference
  94. #define data_isreference(data) ( (data)->type == C_NAME )
  95. /// Returns if the script data is a label
  96. #define data_islabel(data) ( (data)->type == C_POS )
  97. /// Returns if the script data is an internal script function label
  98. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  99. /// Returns if this is a reference to a constant
  100. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  101. /// Returns if this a reference to a param
  102. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  103. /// Returns if this a reference to a variable
  104. //##TODO confirm it's C_NAME [FlavioJS]
  105. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  106. /// Returns the unique id of the reference (id and index)
  107. #define reference_getuid(data) ( (data)->u.num )
  108. /// Returns the id of the reference
  109. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  110. /// Returns the array index of the reference
  111. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  112. /// Returns the name of the reference
  113. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  114. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  115. #define reference_getref(data) ( (data)->ref )
  116. /// Returns the value of the constant
  117. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  118. /// Returns the type of param
  119. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  120. /// Composes the uid of a reference from the id and the index
  121. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  122. #define FETCH(n, t) \
  123. if( script_hasdata(st,n) ) \
  124. (t)=script_getnum(st,n);
  125. #define SCRIPT_BLOCK_SIZE 512
  126. enum { LABEL_NEXTLINE=1,LABEL_START };
  127. static unsigned char * script_buf = NULL;
  128. static int script_pos,script_size;
  129. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  130. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  131. #define GETSTRING(off) (str_buf+(off))
  132. static char *str_buf;
  133. static int str_pos,str_size;
  134. static struct str_data_struct {
  135. int type;
  136. int str;
  137. int backpatch;
  138. int label;
  139. int (*func)(struct script_state *st);
  140. int val;
  141. int next;
  142. } *str_data = NULL;
  143. int str_num=LABEL_START,str_data_size;
  144. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  145. #define SCRIPT_HASH_SIZE 1021
  146. int str_hash[SCRIPT_HASH_SIZE];
  147. //#define SCRIPT_HASH_DJB2
  148. #define SCRIPT_HASH_SDBM
  149. //#define SCRIPT_HASH_ELF
  150. //#define SCRIPT_HASH_PJW
  151. static struct dbt *mapreg_db=NULL;
  152. static struct dbt *mapregstr_db=NULL;
  153. static int mapreg_dirty=-1;
  154. char mapreg_txt[256]="save/mapreg.txt";
  155. #define MAPREG_AUTOSAVE_INTERVAL (300*1000)
  156. static struct dbt *scriptlabel_db=NULL;
  157. static struct dbt *userfunc_db=NULL;
  158. static int parse_options=0;
  159. struct dbt* script_get_label_db(){ return scriptlabel_db; }
  160. struct dbt* script_get_userfunc_db(){ return userfunc_db; }
  161. struct Script_Config script_config;
  162. static jmp_buf error_jump;
  163. static char* error_msg;
  164. static const char* error_pos;
  165. static int error_report; // if the error should produce output
  166. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  167. // [Eoe / jA 1080, 1081, 1094, 1164]
  168. enum curly_type {
  169. TYPE_NULL = 0,
  170. TYPE_IF,
  171. TYPE_SWITCH,
  172. TYPE_WHILE,
  173. TYPE_FOR,
  174. TYPE_DO,
  175. TYPE_USERFUNC,
  176. TYPE_ARGLIST // function argument list
  177. };
  178. #define ARGLIST_UNDEFINED 0
  179. #define ARGLIST_NO_PAREN 1
  180. #define ARGLIST_PAREN 2
  181. static struct {
  182. struct {
  183. int type;
  184. int index;
  185. int count;
  186. int flag;
  187. struct linkdb_node *case_label;
  188. } curly[256]; // 右カッコの情報
  189. int curly_count; // 右カッコの数
  190. int index; // スクリプト内で使用した構文の数
  191. } syntax;
  192. const char* parse_curly_close(const char* p);
  193. const char* parse_syntax_close(const char* p);
  194. const char* parse_syntax_close_sub(const char* p,int* flag);
  195. const char* parse_syntax(const char* p);
  196. static int parse_syntax_for_flag = 0;
  197. extern int current_equip_item_index; //for New CARS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  198. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  199. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  200. int potion_target=0;
  201. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  202. char mapregsql_db[32] = "mapreg";
  203. char mapregsql_db_varname[32] = "varname";
  204. char mapregsql_db_index[32] = "index";
  205. char mapregsql_db_value[32] = "value";
  206. #endif
  207. int get_com(unsigned char *script,int *pos);
  208. int get_num(unsigned char *script,int *pos);
  209. struct script_function {
  210. int (*func)(struct script_state *st);
  211. const char *name;
  212. const char *arg;
  213. } buildin_func[];
  214. static struct linkdb_node *sleep_db;
  215. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.')
  216. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' )
  217. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  218. /*==========================================
  219. * ローカルプロトタイプ宣言 (必要な物のみ)
  220. *------------------------------------------*/
  221. const char* parse_subexpr(const char* p,int limit);
  222. void push_val(struct script_stack *stack,int type,int val);
  223. int run_func(struct script_state *st);
  224. int mapreg_setreg(int num,int val);
  225. int mapreg_setregstr(int num,const char *str);
  226. enum c_op {
  227. C_NOP, // end of script/no value (nil)
  228. C_POS,
  229. C_INT, // number
  230. C_PARAM, // parameter variable (see pc_readparam/pc_setparam)
  231. C_FUNC, // buildin function call
  232. C_STR, // string (free'd automatically)
  233. C_CONSTSTR, // string (not free'd)
  234. C_ARG, // start of argument list
  235. C_NAME,
  236. C_EOL, // end of line (extra stack values are cleared)
  237. C_RETINFO,
  238. C_USERFUNC, // internal script function
  239. C_USERFUNC_POS, // internal script function label
  240. // operators
  241. C_OP3, // a ? b : c
  242. C_LOR, // a || b
  243. C_LAND, // a && b
  244. C_LE, // a <= b
  245. C_LT, // a < b
  246. C_GE, // a >= b
  247. C_GT, // a > b
  248. C_EQ, // a == b
  249. C_NE, // a != b
  250. C_XOR, // a ^ b
  251. C_OR, // a | b
  252. C_AND, // a & b
  253. C_ADD, // a + b
  254. C_SUB, // a - b
  255. C_MUL, // a * b
  256. C_DIV, // a / b
  257. C_MOD, // a % b
  258. C_NEG, // - a
  259. C_LNOT, // ! a
  260. C_NOT, // ~ a
  261. C_R_SHIFT, // a >> b
  262. C_L_SHIFT // a << b
  263. };
  264. enum {
  265. MF_NOMEMO, //0
  266. MF_NOTELEPORT,
  267. MF_NOSAVE,
  268. MF_NOBRANCH,
  269. MF_NOPENALTY,
  270. MF_NOZENYPENALTY,
  271. MF_PVP,
  272. MF_PVP_NOPARTY,
  273. MF_PVP_NOGUILD,
  274. MF_GVG,
  275. MF_GVG_NOPARTY, //10
  276. MF_NOTRADE,
  277. MF_NOSKILL,
  278. MF_NOWARP,
  279. MF_PARTYLOCK,
  280. MF_NOICEWALL,
  281. MF_SNOW,
  282. MF_FOG,
  283. MF_SAKURA,
  284. MF_LEAVES,
  285. MF_RAIN, //20
  286. MF_INDOORS,
  287. MF_NOGO,
  288. MF_CLOUDS,
  289. MF_CLOUDS2,
  290. MF_FIREWORKS,
  291. MF_GVG_CASTLE,
  292. MF_GVG_DUNGEON,
  293. MF_NIGHTENABLED,
  294. MF_NOBASEEXP,
  295. MF_NOJOBEXP, //30
  296. MF_NOMOBLOOT,
  297. MF_NOMVPLOOT,
  298. MF_NORETURN,
  299. MF_NOWARPTO,
  300. MF_NIGHTMAREDROP,
  301. MF_RESTRICTED,
  302. MF_NOCOMMAND,
  303. MF_NODROP,
  304. MF_JEXP,
  305. MF_BEXP, //40
  306. MF_NOVENDING,
  307. MF_LOADEVENT,
  308. MF_NOCHAT,
  309. MF_NOEXPPENALTY,
  310. MF_GUILDLOCK
  311. };
  312. const char* script_op2name(int op)
  313. {
  314. #define RETURN_OP_NAME(type) case type: return #type
  315. switch( op )
  316. {
  317. RETURN_OP_NAME(C_NOP);
  318. RETURN_OP_NAME(C_POS);
  319. RETURN_OP_NAME(C_INT);
  320. RETURN_OP_NAME(C_PARAM);
  321. RETURN_OP_NAME(C_FUNC);
  322. RETURN_OP_NAME(C_STR);
  323. RETURN_OP_NAME(C_CONSTSTR);
  324. RETURN_OP_NAME(C_ARG);
  325. RETURN_OP_NAME(C_NAME);
  326. RETURN_OP_NAME(C_EOL);
  327. RETURN_OP_NAME(C_RETINFO);
  328. RETURN_OP_NAME(C_USERFUNC);
  329. RETURN_OP_NAME(C_USERFUNC_POS);
  330. // operators
  331. RETURN_OP_NAME(C_OP3);
  332. RETURN_OP_NAME(C_LOR);
  333. RETURN_OP_NAME(C_LAND);
  334. RETURN_OP_NAME(C_LE);
  335. RETURN_OP_NAME(C_LT);
  336. RETURN_OP_NAME(C_GE);
  337. RETURN_OP_NAME(C_GT);
  338. RETURN_OP_NAME(C_EQ);
  339. RETURN_OP_NAME(C_NE);
  340. RETURN_OP_NAME(C_XOR);
  341. RETURN_OP_NAME(C_OR);
  342. RETURN_OP_NAME(C_AND);
  343. RETURN_OP_NAME(C_ADD);
  344. RETURN_OP_NAME(C_SUB);
  345. RETURN_OP_NAME(C_MUL);
  346. RETURN_OP_NAME(C_DIV);
  347. RETURN_OP_NAME(C_MOD);
  348. RETURN_OP_NAME(C_NEG);
  349. RETURN_OP_NAME(C_LNOT);
  350. RETURN_OP_NAME(C_NOT);
  351. RETURN_OP_NAME(C_R_SHIFT);
  352. RETURN_OP_NAME(C_L_SHIFT);
  353. default:
  354. ShowDebug("script_op2name: unexpected op=%d\n", op);
  355. return "???";
  356. }
  357. #undef RETURN_OP_NAME
  358. }
  359. /// Reports on the console the src of a script error.
  360. static void script_reportsrc(struct script_state *st)
  361. {
  362. struct block_list* bl;
  363. if( st->oid == 0 )
  364. return; //Can't report source.
  365. bl = map_id2bl(st->oid);
  366. if( bl == NULL )
  367. return;
  368. switch( bl->type )
  369. {
  370. case BL_NPC:
  371. if( bl->m >= 0 )
  372. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  373. else
  374. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  375. break;
  376. default:
  377. if( bl->m >= 0 )
  378. 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);
  379. else
  380. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  381. break;
  382. }
  383. }
  384. /// Reports on the console information about the script data.
  385. static void script_reportdata(struct script_data* data)
  386. {
  387. if( data == NULL )
  388. return;
  389. switch( data->type )
  390. {
  391. case C_NOP:// no value
  392. ShowDebug("Data: nothing (nil)\n");
  393. break;
  394. case C_INT:// number
  395. ShowDebug("Data: number value=%d\n", data->u.num);
  396. break;
  397. case C_STR:
  398. case C_CONSTSTR:// string
  399. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  400. break;
  401. case C_NAME:// reference
  402. if( reference_tovariable(data) )
  403. {// variable
  404. char* name = reference_getname(data);
  405. if( not_array_variable(*name) )
  406. ShowDebug("Data: variable name='%s'\n", name);
  407. else
  408. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  409. }
  410. else if( reference_toconstant(data) )
  411. {// constant
  412. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  413. }
  414. else if( reference_toparam(data) )
  415. {// param
  416. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  417. }
  418. else
  419. {// ???
  420. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  421. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  422. }
  423. break;
  424. case C_POS:// label
  425. ShowDebug("Data: label pos=%d\n", data->u.num);
  426. break;
  427. default:
  428. ShowDebug("Data: %s\n", script_op2name(data->type));
  429. break;
  430. }
  431. }
  432. /*==========================================
  433. * エラーメッセージ出力
  434. *------------------------------------------*/
  435. static void disp_error_message2(const char *mes,const char *pos,int report)
  436. {
  437. error_msg = aStrdup(mes);
  438. error_pos = pos;
  439. error_report = report;
  440. longjmp( error_jump, 1 );
  441. }
  442. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  443. /// Checks event parameter validity
  444. static void check_event(struct script_state *st, const char *evt)
  445. {
  446. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  447. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  448. script_reportsrc(st);
  449. }
  450. }
  451. /*==========================================
  452. * 文字列のハッシュを計算
  453. *------------------------------------------*/
  454. #define calc_hash(x) (calc_hash2(x)%SCRIPT_HASH_SIZE)
  455. static unsigned int calc_hash2(const char* p)
  456. {
  457. #if defined(SCRIPT_HASH_DJB2)
  458. unsigned int h = 5381;
  459. while( *p ) // hash*33 + c
  460. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  461. return h;
  462. #elif defined(SCRIPT_HASH_SDBM)
  463. unsigned int h = 0;
  464. while( *p )
  465. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  466. return h;
  467. #elif defined(SCRIPT_HASH_ELF)
  468. unsigned int h = 0;
  469. unsigned int g;
  470. while( *p ){ // UNIX ELF hash
  471. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  472. g = h & 0xF0000000;
  473. if( g )
  474. h ^= g >> 24;
  475. h &= ~g;
  476. }
  477. return h;
  478. #elif defined(SCRIPT_HASH_PJW)
  479. unsigned int h = 0;
  480. unsigned int g;
  481. while( *p ){
  482. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  483. g = h & 0xF0000000;
  484. if( g )
  485. {
  486. h ^= g>>24;
  487. h ^= g;
  488. }
  489. }
  490. return h;
  491. #else
  492. unsigned int h = 0;
  493. while( *p ){
  494. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 );
  495. h += (unsigned char)TOLOWER(*p++);
  496. }
  497. return h;
  498. #endif
  499. }
  500. /*==========================================
  501. * str_dataの中に名前があるか検索する
  502. *------------------------------------------*/
  503. // 既存のであれば番号、無ければ-1
  504. static int search_str(const char *p)
  505. {
  506. int i;
  507. i=str_hash[calc_hash(p)];
  508. while(i){
  509. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  510. return i;
  511. }
  512. i=str_data[i].next;
  513. }
  514. return -1;
  515. }
  516. /*==========================================
  517. * str_dataに名前を登録
  518. *------------------------------------------*/
  519. // 既存のであれば番号、無ければ登録して新規番号
  520. int add_str(const char* p)
  521. {
  522. int i;
  523. int len;
  524. i=calc_hash(p);
  525. if(str_hash[i]==0){
  526. str_hash[i]=str_num;
  527. } else {
  528. i=str_hash[i];
  529. for(;;){
  530. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  531. return i;
  532. }
  533. if(str_data[i].next==0)
  534. break;
  535. i=str_data[i].next;
  536. }
  537. str_data[i].next=str_num;
  538. }
  539. if(str_num>=str_data_size){
  540. str_data_size+=128;
  541. RECREATE(str_data,struct str_data_struct,str_data_size);
  542. memset(str_data + (str_data_size - 128), '\0', 128);
  543. }
  544. len=(int)strlen(p);
  545. while(str_pos+len+1>=str_size){
  546. str_size+=256;
  547. RECREATE(str_buf,char,str_size);
  548. memset(str_buf + (str_size - 256), '\0', 256);
  549. }
  550. memcpy(str_buf+str_pos,p,len+1);
  551. str_data[str_num].type=C_NOP;
  552. str_data[str_num].str=str_pos;
  553. str_data[str_num].next=0;
  554. str_data[str_num].func=NULL;
  555. str_data[str_num].backpatch=-1;
  556. str_data[str_num].label=-1;
  557. str_pos+=len+1;
  558. return str_num++;
  559. }
  560. /*==========================================
  561. * スクリプトバッファサイズの確認と拡張
  562. *------------------------------------------*/
  563. static void expand_script_buf(void)
  564. {
  565. script_size+=SCRIPT_BLOCK_SIZE;
  566. RECREATE(script_buf,unsigned char,script_size);
  567. }
  568. /*==========================================
  569. * スクリプトバッファに1バイト書き込む
  570. *------------------------------------------*/
  571. #define add_scriptb(a) if( script_pos+1>=script_size ) expand_script_buf(); script_buf[script_pos++]=(uint8)(a)
  572. #if 0
  573. static void add_scriptb(int a)
  574. {
  575. expand_script_buf();
  576. script_buf[script_pos++]=a;
  577. }
  578. #endif
  579. /*==========================================
  580. * スクリプトバッファにデータタイプを書き込む
  581. *------------------------------------------*/
  582. static void add_scriptc(int a)
  583. {
  584. while(a>=0x40){
  585. add_scriptb((a&0x3f)|0x40);
  586. a=(a-0x40)>>6;
  587. }
  588. add_scriptb(a&0x3f);
  589. }
  590. /*==========================================
  591. * スクリプトバッファに整数を書き込む
  592. *------------------------------------------*/
  593. static void add_scripti(int a)
  594. {
  595. while(a>=0x40){
  596. add_scriptb(a|0xc0);
  597. a=(a-0x40)>>6;
  598. }
  599. add_scriptb(a|0x80);
  600. }
  601. /*==========================================
  602. * スクリプトバッファにラベル/変数/関数を書き込む
  603. *------------------------------------------*/
  604. // 最大16Mまで
  605. static void add_scriptl(int l)
  606. {
  607. int backpatch = str_data[l].backpatch;
  608. switch(str_data[l].type){
  609. case C_POS:
  610. case C_USERFUNC_POS:
  611. add_scriptc(C_POS);
  612. add_scriptb(str_data[l].label);
  613. add_scriptb(str_data[l].label>>8);
  614. add_scriptb(str_data[l].label>>16);
  615. break;
  616. case C_NOP:
  617. case C_USERFUNC:
  618. // ラベルの可能性があるのでbackpatch用データ埋め込み
  619. add_scriptc(C_NAME);
  620. str_data[l].backpatch=script_pos;
  621. add_scriptb(backpatch);
  622. add_scriptb(backpatch>>8);
  623. add_scriptb(backpatch>>16);
  624. break;
  625. case C_INT:
  626. add_scripti(abs(str_data[l].val));
  627. if(str_data[l].val < 0) //Notice that this is negative, from jA (Rayce)
  628. add_scriptc(C_NEG);
  629. break;
  630. default:
  631. // もう他の用途と確定してるので数字をそのまま
  632. add_scriptc(C_NAME);
  633. add_scriptb(l);
  634. add_scriptb(l>>8);
  635. add_scriptb(l>>16);
  636. break;
  637. }
  638. }
  639. /*==========================================
  640. * ラベルを解決する
  641. *------------------------------------------*/
  642. void set_label(int l,int pos, const char* script_pos)
  643. {
  644. int i,next;
  645. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  646. { //Prevent overwriting constants values and parameters [Skotlex]
  647. disp_error_message("set_label: invalid label name",script_pos);
  648. return;
  649. }
  650. if(str_data[l].label!=-1){
  651. disp_error_message("set_label: dup label ",script_pos);
  652. return;
  653. }
  654. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  655. str_data[l].label=pos;
  656. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  657. next=GETVALUE(script_buf,i);
  658. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  659. SETVALUE(script_buf,i,pos);
  660. i=next;
  661. }
  662. }
  663. /// Skips spaces and/or comments.
  664. const char* skip_space(const char* p)
  665. {
  666. if( p == NULL )
  667. return NULL;
  668. for(;;)
  669. {
  670. while( ISSPACE(*p) )
  671. ++p;
  672. if( *p == '/' && p[1] == '/' )
  673. {// line comment
  674. while(*p && *p!='\n')
  675. ++p;
  676. }
  677. else if( *p == '/' && p[1] == '*' )
  678. {// block comment
  679. p += 2;
  680. for(;;)
  681. {
  682. if( *p == '\0' )
  683. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  684. if( *p == '*' && p[1] == '/' )
  685. {// end of block comment
  686. p += 2;
  687. break;
  688. }
  689. ++p;
  690. }
  691. }
  692. else
  693. break;
  694. }
  695. return p;
  696. }
  697. /// Skips a word.
  698. /// A word consists of undercores and/or alfanumeric characters,
  699. /// and valid variable prefixes/postfixes.
  700. static
  701. const char* skip_word(const char* p)
  702. {
  703. // prefix
  704. switch( *p )
  705. {
  706. case '@':// temporary char variable
  707. ++p; break;
  708. case '#':// account variable
  709. p += ( p[1] == '#' ? 2 : 1 ); break;
  710. case '.':// npc variable
  711. p += ( p[1] == '@' ? 2 : 1 ); break;
  712. case '$':// global variable
  713. p += ( p[1] == '@' ? 2 : 1 ); break;
  714. }
  715. while( ISALNUM(*p) || *p == '_' )
  716. ++p;
  717. // postfix
  718. if( *p == '$' )// string
  719. p++;
  720. return p;
  721. }
  722. /// Adds a word to str_data.
  723. /// @see skip_word
  724. /// @see add_str
  725. static
  726. int add_word(const char* p)
  727. {
  728. char* word;
  729. int len;
  730. int i;
  731. // Check for a word
  732. len = skip_word(p) - p;
  733. if( len == 0 )
  734. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  735. // Duplicate the word
  736. CREATE(word, char, len+1);
  737. memcpy(word, p, len);
  738. word[len] = 0;
  739. // add the word
  740. i = add_str(word);
  741. aFree(word);
  742. return i;
  743. }
  744. /// Parses a function call.
  745. /// The argument list can have parenthesis or not.
  746. /// The number of arguments is checked.
  747. static
  748. const char* parse_callfunc(const char* p, int require_paren)
  749. {
  750. const char* p2;
  751. const char* arg=NULL;
  752. int func;
  753. func = add_word(p);
  754. if( str_data[func].type == C_FUNC ){
  755. // buildin function
  756. add_scriptl(func);
  757. add_scriptc(C_ARG);
  758. arg = buildin_func[str_data[func].val].arg;
  759. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  760. // script defined function
  761. int callsub = search_str("callsub");
  762. add_scriptl(callsub);
  763. add_scriptc(C_ARG);
  764. add_scriptl(func);
  765. arg = buildin_func[str_data[callsub].val].arg;
  766. if( *arg == 0 )
  767. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  768. if( *arg != '*' )
  769. ++arg; // count func as argument
  770. } else
  771. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  772. p = skip_word(p);
  773. p = skip_space(p);
  774. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  775. syntax.curly[syntax.curly_count].count = 0;
  776. if( *p == ';' )
  777. {// <func name> ';'
  778. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  779. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  780. {// <func name> '(' ')'
  781. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  782. p = p2;
  783. /*
  784. } else if( 0 && require_paren && *p != '(' )
  785. {// <func name>
  786. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  787. */
  788. } else
  789. {// <func name> <arg list>
  790. if( require_paren ){
  791. if( *p != '(' )
  792. disp_error_message("need '('",p);
  793. ++p; // skip '('
  794. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  795. } else if( *p == '(' ){
  796. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  797. } else {
  798. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  799. }
  800. ++syntax.curly_count;
  801. while( *arg ) {
  802. p2=parse_subexpr(p,-1);
  803. if( p == p2 )
  804. break; // not an argument
  805. if( *arg != '*' )
  806. ++arg; // next argument
  807. p=skip_space(p2);
  808. if( *arg == 0 || *p != ',' )
  809. break; // no more arguments
  810. ++p; // skip comma
  811. }
  812. --syntax.curly_count;
  813. }
  814. if( *arg && *arg != '?' && *arg != '*' )
  815. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  816. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  817. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  818. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  819. if( *p != ')' )
  820. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  821. ++p;
  822. }
  823. add_scriptc(C_FUNC);
  824. return p;
  825. }
  826. /*==========================================
  827. * 項の解析
  828. *------------------------------------------*/
  829. const char* parse_simpleexpr(const char *p)
  830. {
  831. int i;
  832. p=skip_space(p);
  833. #ifdef DEBUG_FUNCIN
  834. if(battle_config.etc_log)
  835. ShowDebug("parse_simpleexpr %s\n",p);
  836. #endif
  837. if(*p==';' || *p==',')
  838. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  839. if(*p=='('){
  840. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  841. ++syntax.curly[i].count;
  842. p=parse_subexpr(p+1,-1);
  843. p=skip_space(p);
  844. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  845. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  846. ){
  847. if( *p == ',' ){
  848. syntax.curly[i].flag = ARGLIST_PAREN;
  849. return p;
  850. } else
  851. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  852. }
  853. if( *p != ')' )
  854. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  855. ++p;
  856. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  857. char *np;
  858. i=strtoul(p,&np,0);
  859. add_scripti(i);
  860. p=np;
  861. } else if(*p=='"'){
  862. add_scriptc(C_STR);
  863. p++;
  864. while( *p && *p != '"' ){
  865. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  866. p++;
  867. else if( *p == '\n' )
  868. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  869. add_scriptb(*p++);
  870. }
  871. if(!*p)
  872. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  873. add_scriptb(0);
  874. p++; //'"'
  875. } else {
  876. int l;
  877. // label , register , function etc
  878. if(skip_word(p)==p)
  879. disp_error_message("parse_simpleexpr: unexpected character",p);
  880. l=add_word(p);
  881. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  882. return parse_callfunc(p,1);
  883. p=skip_word(p);
  884. if( *p == '[' ){
  885. // array(name[i] => getelementofarray(name,i) )
  886. add_scriptl(search_str("getelementofarray"));
  887. add_scriptc(C_ARG);
  888. add_scriptl(l);
  889. p=parse_subexpr(p+1,-1);
  890. p=skip_space(p);
  891. if( *p != ']' )
  892. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  893. ++p;
  894. add_scriptc(C_FUNC);
  895. }else
  896. add_scriptl(l);
  897. }
  898. #ifdef DEBUG_FUNCIN
  899. if(battle_config.etc_log)
  900. ShowDebug("parse_simpleexpr end %s\n",p);
  901. #endif
  902. return p;
  903. }
  904. /*==========================================
  905. * 式の解析
  906. *------------------------------------------*/
  907. const char* parse_subexpr(const char* p,int limit)
  908. {
  909. int op,opl,len;
  910. const char* tmpp;
  911. #ifdef DEBUG_FUNCIN
  912. if(battle_config.etc_log)
  913. ShowDebug("parse_subexpr %s\n",p);
  914. #endif
  915. p=skip_space(p);
  916. if(*p=='-'){
  917. tmpp=skip_space(p+1);
  918. if(*tmpp==';' || *tmpp==','){
  919. add_scriptl(LABEL_NEXTLINE);
  920. p++;
  921. return p;
  922. }
  923. }
  924. tmpp=p;
  925. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  926. p=parse_subexpr(p+1,10);
  927. add_scriptc(op);
  928. } else
  929. p=parse_simpleexpr(p);
  930. p=skip_space(p);
  931. while((
  932. (op=C_OP3,opl=0,len=1,*p=='?') ||
  933. (op=C_ADD,opl=8,len=1,*p=='+') ||
  934. (op=C_SUB,opl=8,len=1,*p=='-') ||
  935. (op=C_MUL,opl=9,len=1,*p=='*') ||
  936. (op=C_DIV,opl=9,len=1,*p=='/') ||
  937. (op=C_MOD,opl=9,len=1,*p=='%') ||
  938. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  939. (op=C_AND,opl=6,len=1,*p=='&') ||
  940. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  941. (op=C_OR,opl=5,len=1,*p=='|') ||
  942. (op=C_XOR,opl=4,len=1,*p=='^') ||
  943. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  944. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  945. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  946. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  947. (op=C_GT,opl=3,len=1,*p=='>') ||
  948. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  949. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  950. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  951. p+=len;
  952. if(op == C_OP3) {
  953. p=parse_subexpr(p,-1);
  954. p=skip_space(p);
  955. if( *(p++) != ':')
  956. disp_error_message("parse_subexpr: need ':'", p-1);
  957. p=parse_subexpr(p,-1);
  958. } else {
  959. p=parse_subexpr(p,opl);
  960. }
  961. add_scriptc(op);
  962. p=skip_space(p);
  963. }
  964. #ifdef DEBUG_FUNCIN
  965. if(battle_config.etc_log)
  966. ShowDebug("parse_subexpr end %s\n",p);
  967. #endif
  968. return p; /* return first untreated operator */
  969. }
  970. /*==========================================
  971. * 式の評価
  972. *------------------------------------------*/
  973. const char* parse_expr(const char *p)
  974. {
  975. #ifdef DEBUG_FUNCIN
  976. if(battle_config.etc_log)
  977. ShowDebug("parse_expr %s\n",p);
  978. #endif
  979. switch(*p){
  980. case ')': case ';': case ':': case '[': case ']':
  981. case '}':
  982. disp_error_message("parse_expr: unexpected char",p);
  983. }
  984. p=parse_subexpr(p,-1);
  985. #ifdef DEBUG_FUNCIN
  986. if(battle_config.etc_log)
  987. ShowDebug("parse_expr end %s\n",p);
  988. #endif
  989. return p;
  990. }
  991. /*==========================================
  992. * 行の解析
  993. *------------------------------------------*/
  994. const char* parse_line(const char* p)
  995. {
  996. const char* p2;
  997. p=skip_space(p);
  998. if(*p==';') {
  999. // if(); for(); while(); のために閉じ判定
  1000. p = parse_syntax_close(p);
  1001. return p+1;
  1002. }
  1003. if(*p==')' && parse_syntax_for_flag)
  1004. return p+1;
  1005. p = skip_space(p);
  1006. if(p[0] == '{') {
  1007. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1008. syntax.curly[syntax.curly_count].count = -1;
  1009. syntax.curly[syntax.curly_count].index = -1;
  1010. syntax.curly_count++;
  1011. return p + 1;
  1012. } else if(p[0] == '}') {
  1013. return parse_curly_close(p);
  1014. }
  1015. // 構文関連の処理
  1016. p2 = parse_syntax(p);
  1017. if(p2 != NULL)
  1018. return p2;
  1019. p = parse_callfunc(p,0);
  1020. p = skip_space(p);
  1021. if(parse_syntax_for_flag) {
  1022. if( *p != ')' )
  1023. disp_error_message("parse_line: need ')'",p);
  1024. } else {
  1025. if( *p != ';' )
  1026. disp_error_message("parse_line: need ';'",p);
  1027. }
  1028. // if, for , while の閉じ判定
  1029. p = parse_syntax_close(p+1);
  1030. return p;
  1031. }
  1032. // { ... } の閉じ処理
  1033. const char* parse_curly_close(const char* p)
  1034. {
  1035. if(syntax.curly_count <= 0) {
  1036. disp_error_message("parse_curly_close: unexpected string",p);
  1037. return p + 1;
  1038. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1039. syntax.curly_count--;
  1040. // if, for , while の閉じ判定
  1041. p = parse_syntax_close(p + 1);
  1042. return p;
  1043. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1044. // switch() 閉じ判定
  1045. int pos = syntax.curly_count-1;
  1046. char label[256];
  1047. int l;
  1048. // 一時変数を消す
  1049. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1050. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1051. parse_line(label);
  1052. syntax.curly_count--;
  1053. // 無条件で終了ポインタに移動
  1054. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1055. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1056. parse_line(label);
  1057. syntax.curly_count--;
  1058. // 現在地のラベルを付ける
  1059. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1060. l=add_str(label);
  1061. set_label(l,script_pos, p);
  1062. if(syntax.curly[pos].flag) {
  1063. // default が存在する
  1064. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1065. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1066. parse_line(label);
  1067. syntax.curly_count--;
  1068. }
  1069. // 終了ラベルを付ける
  1070. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1071. l=add_str(label);
  1072. set_label(l,script_pos, p);
  1073. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1074. syntax.curly_count--;
  1075. return p+1;
  1076. } else {
  1077. disp_error_message("parse_curly_close: unexpected string",p);
  1078. return p + 1;
  1079. }
  1080. }
  1081. // 構文関連の処理
  1082. // break, case, continue, default, do, for, function,
  1083. // if, switch, while をこの内部で処理します。
  1084. const char* parse_syntax(const char* p)
  1085. {
  1086. const char *p2 = skip_word(p);
  1087. switch(*p) {
  1088. case 'B':
  1089. case 'b':
  1090. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1091. // break の処理
  1092. char label[256];
  1093. int pos = syntax.curly_count - 1;
  1094. while(pos >= 0) {
  1095. if(syntax.curly[pos].type == TYPE_DO) {
  1096. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1097. break;
  1098. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1099. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1100. break;
  1101. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1102. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1103. break;
  1104. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1105. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1106. break;
  1107. }
  1108. pos--;
  1109. }
  1110. if(pos < 0) {
  1111. disp_error_message("parse_syntax: unexpected 'break'",p);
  1112. } else {
  1113. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1114. parse_line(label);
  1115. syntax.curly_count--;
  1116. }
  1117. p = skip_space(p2);
  1118. if(*p != ';') {
  1119. disp_error_message("parse_syntax: need ';'",p);
  1120. }
  1121. p++;
  1122. // if, for , while の閉じ判定
  1123. p = parse_syntax_close(p + 1);
  1124. return p;
  1125. }
  1126. break;
  1127. case 'c':
  1128. case 'C':
  1129. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1130. // case の処理
  1131. int pos = syntax.curly_count-1;
  1132. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1133. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1134. return p+1;
  1135. } else {
  1136. char label[256];
  1137. int l,v;
  1138. char *np;
  1139. if(syntax.curly[pos].count != 1) {
  1140. // FALLTHRU 用のジャンプ
  1141. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1142. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1143. parse_line(label);
  1144. syntax.curly_count--;
  1145. // 現在地のラベルを付ける
  1146. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1147. l=add_str(label);
  1148. set_label(l,script_pos, p);
  1149. }
  1150. // switch 判定文
  1151. p = skip_space(p2);
  1152. if(p == p2) {
  1153. disp_error_message("parse_syntax: expect space ' '",p);
  1154. }
  1155. // check whether case label is integer or not
  1156. v = strtol(p,&np,0);
  1157. if(np == p) { //Check for constants
  1158. p2 = skip_word(p);
  1159. v = p2-p; // length of word at p2
  1160. memcpy(label,p,v);
  1161. label[v]='\0';
  1162. v = search_str(label);
  1163. if (v < 0 || str_data[v].type != C_INT)
  1164. disp_error_message("parse_syntax: 'case' label not integer",p);
  1165. v = str_data[v].val;
  1166. p = skip_word(p);
  1167. } else { //Numeric value
  1168. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1169. p++;
  1170. p = skip_word(p);
  1171. if(np != p)
  1172. disp_error_message("parse_syntax: 'case' label not integer",np);
  1173. }
  1174. p = skip_space(p);
  1175. if(*p != ':')
  1176. disp_error_message("parse_syntax: expect ':'",p);
  1177. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1178. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1179. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1180. // 2回parse しないとダメ
  1181. p2 = parse_line(label);
  1182. parse_line(p2);
  1183. syntax.curly_count--;
  1184. if(syntax.curly[pos].count != 1) {
  1185. // FALLTHRU 終了後のラベル
  1186. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1187. l=add_str(label);
  1188. set_label(l,script_pos,p);
  1189. }
  1190. // check duplication of case label [Rayce]
  1191. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1192. disp_error_message("parse_syntax: dup 'case'",p);
  1193. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1194. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1195. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1196. parse_line(label);
  1197. syntax.curly_count--;
  1198. syntax.curly[pos].count++;
  1199. }
  1200. return p + 1;
  1201. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1202. // continue の処理
  1203. char label[256];
  1204. int pos = syntax.curly_count - 1;
  1205. while(pos >= 0) {
  1206. if(syntax.curly[pos].type == TYPE_DO) {
  1207. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1208. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1209. break;
  1210. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1211. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1212. break;
  1213. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1214. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1215. break;
  1216. }
  1217. pos--;
  1218. }
  1219. if(pos < 0) {
  1220. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1221. } else {
  1222. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1223. parse_line(label);
  1224. syntax.curly_count--;
  1225. }
  1226. p = skip_space(p2);
  1227. if(*p != ';')
  1228. disp_error_message("parse_syntax: need ';'",p);
  1229. p++;
  1230. // if, for , while の閉じ判定
  1231. p = parse_syntax_close(p + 1);
  1232. return p;
  1233. }
  1234. break;
  1235. case 'd':
  1236. case 'D':
  1237. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1238. // switch - default の処理
  1239. int pos = syntax.curly_count-1;
  1240. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1241. disp_error_message("parse_syntax: unexpected 'default'",p);
  1242. } else if(syntax.curly[pos].flag) {
  1243. disp_error_message("parse_syntax: dup 'default'",p);
  1244. } else {
  1245. char label[256];
  1246. int l;
  1247. // 現在地のラベルを付ける
  1248. p = skip_space(p2);
  1249. if(*p != ':') {
  1250. disp_error_message("parse_syntax: need ':'",p);
  1251. }
  1252. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1253. l=add_str(label);
  1254. set_label(l,script_pos,p);
  1255. // 無条件で次のリンクに飛ばす
  1256. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1257. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1258. parse_line(label);
  1259. syntax.curly_count--;
  1260. // default のラベルを付ける
  1261. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1262. l=add_str(label);
  1263. set_label(l,script_pos,p);
  1264. syntax.curly[syntax.curly_count - 1].flag = 1;
  1265. syntax.curly[pos].count++;
  1266. }
  1267. return p + 1;
  1268. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1269. int l;
  1270. char label[256];
  1271. p=skip_space(p2);
  1272. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1273. syntax.curly[syntax.curly_count].count = 1;
  1274. syntax.curly[syntax.curly_count].index = syntax.index++;
  1275. syntax.curly[syntax.curly_count].flag = 0;
  1276. // 現在地のラベル形成する
  1277. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1278. l=add_str(label);
  1279. set_label(l,script_pos,p);
  1280. syntax.curly_count++;
  1281. return p;
  1282. }
  1283. break;
  1284. case 'f':
  1285. case 'F':
  1286. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1287. int l;
  1288. char label[256];
  1289. int pos = syntax.curly_count;
  1290. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1291. syntax.curly[syntax.curly_count].count = 1;
  1292. syntax.curly[syntax.curly_count].index = syntax.index++;
  1293. syntax.curly[syntax.curly_count].flag = 0;
  1294. syntax.curly_count++;
  1295. p=skip_space(p2);
  1296. if(*p != '(')
  1297. disp_error_message("parse_syntax: need '('",p);
  1298. p++;
  1299. // 初期化文を実行する
  1300. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1301. p=parse_line(p);
  1302. syntax.curly_count--;
  1303. // 条件判断開始のラベル形成する
  1304. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1305. l=add_str(label);
  1306. set_label(l,script_pos,p);
  1307. p=skip_space(p);
  1308. if(*p == ';') {
  1309. // for(;;) のパターンなので必ず真
  1310. ;
  1311. } else {
  1312. // 条件が偽なら終了地点に飛ばす
  1313. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1314. add_scriptl(add_str("jump_zero"));
  1315. add_scriptc(C_ARG);
  1316. p=parse_expr(p);
  1317. p=skip_space(p);
  1318. add_scriptl(add_str(label));
  1319. add_scriptc(C_FUNC);
  1320. }
  1321. if(*p != ';')
  1322. disp_error_message("parse_syntax: need ';'",p);
  1323. p++;
  1324. // ループ開始に飛ばす
  1325. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1326. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1327. parse_line(label);
  1328. syntax.curly_count--;
  1329. // 次のループへのラベル形成する
  1330. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1331. l=add_str(label);
  1332. set_label(l,script_pos,p);
  1333. // 次のループに入る時の処理
  1334. // for 最後の ')' を ';' として扱うフラグ
  1335. parse_syntax_for_flag = 1;
  1336. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1337. p=parse_line(p);
  1338. syntax.curly_count--;
  1339. parse_syntax_for_flag = 0;
  1340. // 条件判定処理に飛ばす
  1341. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1342. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1343. parse_line(label);
  1344. syntax.curly_count--;
  1345. // ループ開始のラベル付け
  1346. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1347. l=add_str(label);
  1348. set_label(l,script_pos,p);
  1349. return p;
  1350. }
  1351. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1352. {// internal script function
  1353. const char *func_name;
  1354. func_name = skip_space(p2);
  1355. p = skip_word(func_name);
  1356. if( p == func_name )
  1357. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1358. if( *skip_space(p) == ';' )
  1359. {// function <name> ;
  1360. // function declaration - just register the name
  1361. int l;
  1362. l = add_word(func_name);
  1363. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1364. str_data[l].type = C_USERFUNC;
  1365. return skip_space(p) + 1;
  1366. }
  1367. else
  1368. {// function <name> <line/block of code>
  1369. char label[256];
  1370. int l;
  1371. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1372. syntax.curly[syntax.curly_count].count = 1;
  1373. syntax.curly[syntax.curly_count].index = syntax.index++;
  1374. syntax.curly[syntax.curly_count].flag = 0;
  1375. ++syntax.curly_count;
  1376. // Jump over the function code
  1377. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1378. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1379. ++syntax.curly_count;
  1380. parse_line(label);
  1381. --syntax.curly_count;
  1382. // Set the position of the function (label)
  1383. l=add_word(func_name);
  1384. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1385. str_data[l].type = C_USERFUNC;
  1386. set_label(l, script_pos, p);
  1387. if( parse_options&SCRIPT_USE_LABEL_DB )
  1388. strdb_put(scriptlabel_db, GETSTRING(str_data[l].str), (void*)script_pos);
  1389. return skip_space(p);
  1390. }
  1391. }
  1392. break;
  1393. case 'i':
  1394. case 'I':
  1395. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1396. // if() の処理
  1397. char label[256];
  1398. p=skip_space(p2);
  1399. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1400. disp_error_message("need '('",p);
  1401. }
  1402. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1403. syntax.curly[syntax.curly_count].count = 1;
  1404. syntax.curly[syntax.curly_count].index = syntax.index++;
  1405. syntax.curly[syntax.curly_count].flag = 0;
  1406. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1407. syntax.curly_count++;
  1408. add_scriptl(add_str("jump_zero"));
  1409. add_scriptc(C_ARG);
  1410. p=parse_expr(p);
  1411. p=skip_space(p);
  1412. add_scriptl(add_str(label));
  1413. add_scriptc(C_FUNC);
  1414. return p;
  1415. }
  1416. break;
  1417. case 's':
  1418. case 'S':
  1419. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1420. // switch() の処理
  1421. char label[256];
  1422. p=skip_space(p2);
  1423. if(*p != '(') {
  1424. disp_error_message("need '('",p);
  1425. }
  1426. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1427. syntax.curly[syntax.curly_count].count = 1;
  1428. syntax.curly[syntax.curly_count].index = syntax.index++;
  1429. syntax.curly[syntax.curly_count].flag = 0;
  1430. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1431. syntax.curly_count++;
  1432. add_scriptl(add_str("set"));
  1433. add_scriptc(C_ARG);
  1434. add_scriptl(add_str(label));
  1435. p=parse_expr(p);
  1436. p=skip_space(p);
  1437. if(*p != '{') {
  1438. disp_error_message("parse_syntax: need '{'",p);
  1439. }
  1440. add_scriptc(C_FUNC);
  1441. return p + 1;
  1442. }
  1443. break;
  1444. case 'w':
  1445. case 'W':
  1446. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1447. int l;
  1448. char label[256];
  1449. p=skip_space(p2);
  1450. if(*p != '(') {
  1451. disp_error_message("need '('",p);
  1452. }
  1453. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1454. syntax.curly[syntax.curly_count].count = 1;
  1455. syntax.curly[syntax.curly_count].index = syntax.index++;
  1456. syntax.curly[syntax.curly_count].flag = 0;
  1457. // 条件判断開始のラベル形成する
  1458. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1459. l=add_str(label);
  1460. set_label(l,script_pos,p);
  1461. // 条件が偽なら終了地点に飛ばす
  1462. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1463. syntax.curly_count++;
  1464. add_scriptl(add_str("jump_zero"));
  1465. add_scriptc(C_ARG);
  1466. p=parse_expr(p);
  1467. p=skip_space(p);
  1468. add_scriptl(add_str(label));
  1469. add_scriptc(C_FUNC);
  1470. return p;
  1471. }
  1472. break;
  1473. }
  1474. return NULL;
  1475. }
  1476. const char* parse_syntax_close(const char *p) {
  1477. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1478. int flag;
  1479. do {
  1480. p = parse_syntax_close_sub(p,&flag);
  1481. } while(flag);
  1482. return p;
  1483. }
  1484. // if, for , while , do の閉じ判定
  1485. // flag == 1 : 閉じられた
  1486. // flag == 0 : 閉じられない
  1487. const char* parse_syntax_close_sub(const char* p,int* flag)
  1488. {
  1489. char label[256];
  1490. int pos = syntax.curly_count - 1;
  1491. int l;
  1492. *flag = 1;
  1493. if(syntax.curly_count <= 0) {
  1494. *flag = 0;
  1495. return p;
  1496. } else if(syntax.curly[pos].type == TYPE_IF) {
  1497. const char *bp = p;
  1498. const char *p2;
  1499. // if 最終場所へ飛ばす
  1500. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1501. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1502. parse_line(label);
  1503. syntax.curly_count--;
  1504. // 現在地のラベルを付ける
  1505. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1506. l=add_str(label);
  1507. set_label(l,script_pos,p);
  1508. syntax.curly[pos].count++;
  1509. p = skip_space(p);
  1510. p2 = skip_word(p);
  1511. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1512. // else or else - if
  1513. p = skip_space(p2);
  1514. p2 = skip_word(p);
  1515. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1516. // else - if
  1517. p=skip_space(p2);
  1518. if(*p != '(') {
  1519. disp_error_message("need '('",p);
  1520. }
  1521. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1522. add_scriptl(add_str("jump_zero"));
  1523. add_scriptc(C_ARG);
  1524. p=parse_expr(p);
  1525. p=skip_space(p);
  1526. add_scriptl(add_str(label));
  1527. add_scriptc(C_FUNC);
  1528. *flag = 0;
  1529. return p;
  1530. } else {
  1531. // else
  1532. if(!syntax.curly[pos].flag) {
  1533. syntax.curly[pos].flag = 1;
  1534. *flag = 0;
  1535. return p;
  1536. }
  1537. }
  1538. }
  1539. // if 閉じ
  1540. syntax.curly_count--;
  1541. // 最終地のラベルを付ける
  1542. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1543. l=add_str(label);
  1544. set_label(l,script_pos,p);
  1545. if(syntax.curly[pos].flag == 1) {
  1546. // このifに対するelseじゃないのでポインタの位置は同じ
  1547. return bp;
  1548. }
  1549. return p;
  1550. } else if(syntax.curly[pos].type == TYPE_DO) {
  1551. int l;
  1552. char label[256];
  1553. const char *p2;
  1554. if(syntax.curly[pos].flag) {
  1555. // 現在地のラベル形成する(continue でここに来る)
  1556. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1557. l=add_str(label);
  1558. set_label(l,script_pos,p);
  1559. }
  1560. // 条件が偽なら終了地点に飛ばす
  1561. p = skip_space(p);
  1562. p2 = skip_word(p);
  1563. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1564. disp_error_message("parse_syntax: need 'while'",p);
  1565. p = skip_space(p2);
  1566. if(*p != '(') {
  1567. disp_error_message("need '('",p);
  1568. }
  1569. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1570. add_scriptl(add_str("jump_zero"));
  1571. add_scriptc(C_ARG);
  1572. p=parse_expr(p);
  1573. p=skip_space(p);
  1574. add_scriptl(add_str(label));
  1575. add_scriptc(C_FUNC);
  1576. // 開始地点に飛ばす
  1577. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1578. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1579. parse_line(label);
  1580. syntax.curly_count--;
  1581. // 条件終了地点のラベル形成する
  1582. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1583. l=add_str(label);
  1584. set_label(l,script_pos,p);
  1585. p = skip_space(p);
  1586. if(*p != ';') {
  1587. disp_error_message("parse_syntax: need ';'",p);
  1588. return p+1;
  1589. }
  1590. p++;
  1591. syntax.curly_count--;
  1592. return p;
  1593. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1594. // 次のループに飛ばす
  1595. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1596. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1597. parse_line(label);
  1598. syntax.curly_count--;
  1599. // for 終了のラベル付け
  1600. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1601. l=add_str(label);
  1602. set_label(l,script_pos,p);
  1603. syntax.curly_count--;
  1604. return p;
  1605. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1606. // while 条件判断へ飛ばす
  1607. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1608. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1609. parse_line(label);
  1610. syntax.curly_count--;
  1611. // while 終了のラベル付け
  1612. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1613. l=add_str(label);
  1614. set_label(l,script_pos,p);
  1615. syntax.curly_count--;
  1616. return p;
  1617. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1618. int pos = syntax.curly_count-1;
  1619. char label[256];
  1620. int l;
  1621. // 戻す
  1622. sprintf(label,"return;");
  1623. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1624. parse_line(label);
  1625. syntax.curly_count--;
  1626. // 現在地のラベルを付ける
  1627. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1628. l=add_str(label);
  1629. set_label(l,script_pos,p);
  1630. syntax.curly_count--;
  1631. return p + 1;
  1632. } else {
  1633. *flag = 0;
  1634. return p;
  1635. }
  1636. }
  1637. /*==========================================
  1638. * 組み込み関数の追加
  1639. *------------------------------------------*/
  1640. static void add_buildin_func(void)
  1641. {
  1642. int i,n;
  1643. const char* p;
  1644. for( i = 0; buildin_func[i].func; i++ )
  1645. {
  1646. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1647. // 'v' - value (either string or int or reference)
  1648. // 's' - string
  1649. // 'i' - int
  1650. // 'r' - reference (of a variable)
  1651. // 'l' - label
  1652. // '?' - one optional parameter
  1653. // '*' - unknown number of optional parameters
  1654. p = buildin_func[i].arg;
  1655. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1656. while( *p == '?' ) ++p;
  1657. if( *p == '*' ) ++p;
  1658. if( *p != 0){
  1659. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1660. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1661. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1662. } else {
  1663. n = add_str(buildin_func[i].name);
  1664. str_data[n].type = C_FUNC;
  1665. str_data[n].val = i;
  1666. str_data[n].func = buildin_func[i].func;
  1667. }
  1668. }
  1669. }
  1670. /*==========================================
  1671. * 定数データベースの読み込み
  1672. *------------------------------------------*/
  1673. static void read_constdb(void)
  1674. {
  1675. FILE *fp;
  1676. char line[1024],name[1024],val[1024];
  1677. int n,type;
  1678. sprintf(line, "%s/const.txt", db_path);
  1679. fp=fopen(line, "r");
  1680. if(fp==NULL){
  1681. ShowError("can't read %s\n", line);
  1682. return ;
  1683. }
  1684. while(fgets(line, sizeof(line), fp))
  1685. {
  1686. if(line[0]=='/' && line[1]=='/')
  1687. continue;
  1688. type=0;
  1689. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1690. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1691. n=add_str(name);
  1692. if(type==0)
  1693. str_data[n].type=C_INT;
  1694. else
  1695. str_data[n].type=C_PARAM;
  1696. str_data[n].val= (int)strtol(val,NULL,0);
  1697. }
  1698. }
  1699. fclose(fp);
  1700. }
  1701. /*==========================================
  1702. * エラー表示
  1703. *------------------------------------------*/
  1704. const char* script_print_line( const char *p, const char *mark, int line )
  1705. {
  1706. int i;
  1707. if( p == NULL || !p[0] ) return NULL;
  1708. if( line < 0 )
  1709. printf("*% 5d : ", -line);
  1710. else
  1711. printf(" % 5d : ", line);
  1712. for(i=0;p[i] && p[i] != '\n';i++){
  1713. if(p + i != mark)
  1714. printf("%c",p[i]);
  1715. else
  1716. printf("\'%c\'",p[i]);
  1717. }
  1718. printf("\n");
  1719. return p+i+(p[i] == '\n' ? 1 : 0);
  1720. }
  1721. void script_error(const char *src,const char *file,int start_line, const char *error_msg, const char *error_pos)
  1722. {
  1723. // エラーが発生した行を求める
  1724. int j;
  1725. int line = start_line;
  1726. const char *p;
  1727. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1728. for(p=src;p && *p;line++){
  1729. char *lineend=strchr(p,'\n');
  1730. if(lineend==NULL || error_pos<lineend){
  1731. break;
  1732. }
  1733. for( j = 0; j < 4; j++ ) {
  1734. linestart[j] = linestart[j+1];
  1735. }
  1736. linestart[4] = p;
  1737. p=lineend+1;
  1738. }
  1739. printf("\a\n");
  1740. printf("script error on %s line %d\n", file, line);
  1741. printf(" %s\n", error_msg);
  1742. for(j = 0; j < 5; j++ ) {
  1743. script_print_line( linestart[j], NULL, line + j - 5);
  1744. }
  1745. p = script_print_line( p, error_pos, -line);
  1746. for(j = 0; j < 5; j++) {
  1747. p = script_print_line( p, NULL, line + j + 1 );
  1748. }
  1749. }
  1750. /*==========================================
  1751. * スクリプトの解析
  1752. *------------------------------------------*/
  1753. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1754. {
  1755. const char *p,*tmpp;
  1756. int i;
  1757. struct script_code* code = NULL;
  1758. static int first=1;
  1759. char end;
  1760. if( src == NULL )
  1761. return NULL;// empty script
  1762. memset(&syntax,0,sizeof(syntax));
  1763. if(first){
  1764. add_buildin_func();
  1765. read_constdb();
  1766. }
  1767. first=0;
  1768. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1769. script_pos=0;
  1770. script_size=SCRIPT_BLOCK_SIZE;
  1771. str_data[LABEL_NEXTLINE].type=C_NOP;
  1772. str_data[LABEL_NEXTLINE].backpatch=-1;
  1773. str_data[LABEL_NEXTLINE].label=-1;
  1774. for(i=LABEL_START;i<str_num;i++){
  1775. if(
  1776. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1777. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1778. ){
  1779. str_data[i].type=C_NOP;
  1780. str_data[i].backpatch=-1;
  1781. str_data[i].label=-1;
  1782. }
  1783. }
  1784. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1785. if( options&SCRIPT_USE_LABEL_DB )
  1786. scriptlabel_db->clear(scriptlabel_db, NULL);
  1787. parse_options = options;
  1788. if( setjmp( error_jump ) != 0 ) {
  1789. //Restore program state when script has problems. [from jA]
  1790. int i;
  1791. const int size = ARRAYLENGTH(syntax.curly);
  1792. if( error_report )
  1793. script_error(src,file,line,error_msg,error_pos);
  1794. aFree( error_msg );
  1795. aFree( script_buf );
  1796. script_pos = 0;
  1797. script_size = 0;
  1798. script_buf = NULL;
  1799. for(i=LABEL_START;i<str_num;i++)
  1800. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1801. for(i=0; i<size; i++)
  1802. linkdb_final(&syntax.curly[i].case_label);
  1803. return NULL;
  1804. }
  1805. parse_syntax_for_flag=0;
  1806. p=src;
  1807. p=skip_space(p);
  1808. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1809. {// does not require brackets around the script
  1810. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1811. {// empty script and can return NULL
  1812. aFree( script_buf );
  1813. script_pos = 0;
  1814. script_size = 0;
  1815. script_buf = NULL;
  1816. return NULL;
  1817. }
  1818. end = '\0';
  1819. }
  1820. else
  1821. {// requires brackets around the script
  1822. if( *p != '{' )
  1823. disp_error_message("not found '{'",p);
  1824. p = skip_space(p+1);
  1825. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1826. {// empty script and can return NULL
  1827. aFree( script_buf );
  1828. script_pos = 0;
  1829. script_size = 0;
  1830. script_buf = NULL;
  1831. return NULL;
  1832. }
  1833. end = '}';
  1834. }
  1835. while( syntax.curly_count != 0 || *p != end )
  1836. {
  1837. if( *p == '\0' )
  1838. disp_error_message("unexpected end of script",p);
  1839. // labelだけ特殊処理
  1840. tmpp=skip_space(skip_word(p));
  1841. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1842. i=add_word(p);
  1843. set_label(i,script_pos,p);
  1844. if( parse_options&SCRIPT_USE_LABEL_DB )
  1845. strdb_put(scriptlabel_db, GETSTRING(str_data[i].str), (void*)script_pos);
  1846. p=tmpp+1;
  1847. p=skip_space(p);
  1848. continue;
  1849. }
  1850. // 他は全部一緒くた
  1851. p=parse_line(p);
  1852. p=skip_space(p);
  1853. add_scriptc(C_EOL);
  1854. set_label(LABEL_NEXTLINE,script_pos,p);
  1855. str_data[LABEL_NEXTLINE].type=C_NOP;
  1856. str_data[LABEL_NEXTLINE].backpatch=-1;
  1857. str_data[LABEL_NEXTLINE].label=-1;
  1858. }
  1859. add_scriptc(C_NOP);
  1860. script_size = script_pos;
  1861. RECREATE(script_buf,unsigned char,script_pos);
  1862. // 未解決のラベルを解決
  1863. for(i=LABEL_START;i<str_num;i++){
  1864. if(str_data[i].type==C_NOP){
  1865. int j,next;
  1866. str_data[i].type=C_NAME;
  1867. str_data[i].label=i;
  1868. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1869. next=GETVALUE(script_buf,j);
  1870. SETVALUE(script_buf,j,i);
  1871. j=next;
  1872. }
  1873. }
  1874. }
  1875. #ifdef DEBUG_DISP
  1876. for(i=0;i<script_pos;i++){
  1877. if((i&15)==0) printf("%04x : ",i);
  1878. printf("%02x ",script_buf[i]);
  1879. if((i&15)==15) printf("\n");
  1880. }
  1881. printf("\n");
  1882. #endif
  1883. #ifdef DEBUG_DISASM
  1884. {
  1885. int i = 0,j;
  1886. while(i < script_pos) {
  1887. printf("%06x ",i);
  1888. j = i;
  1889. switch(get_com(script_buf,&i)) {
  1890. case C_EOL: printf("C_EOL"); break;
  1891. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1892. case C_POS:
  1893. printf("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1894. i += 3;
  1895. break;
  1896. case C_NAME:
  1897. j = (*(int*)(script_buf+i)&0xffffff);
  1898. printf("C_NAME %s",j == 0xffffff ? "?? unknown ??" : str_buf + str_data[j].str);
  1899. i += 3;
  1900. break;
  1901. case C_ARG: printf("C_ARG"); break;
  1902. case C_FUNC: printf("C_FUNC"); break;
  1903. case C_ADD: printf("C_ADD"); break;
  1904. case C_SUB: printf("C_SUB"); break;
  1905. case C_MUL: printf("C_MUL"); break;
  1906. case C_DIV: printf("C_DIV"); break;
  1907. case C_MOD: printf("C_MOD"); break;
  1908. case C_EQ: printf("C_EQ"); break;
  1909. case C_NE: printf("C_NE"); break;
  1910. case C_GT: printf("C_GT"); break;
  1911. case C_GE: printf("C_GE"); break;
  1912. case C_LT: printf("C_LT"); break;
  1913. case C_LE: printf("C_LE"); break;
  1914. case C_AND: printf("C_AND"); break;
  1915. case C_OR: printf("C_OR"); break;
  1916. case C_XOR: printf("C_XOR"); break;
  1917. case C_LAND: printf("C_LAND"); break;
  1918. case C_LOR: printf("C_LOR"); break;
  1919. case C_R_SHIFT: printf("C_R_SHIFT"); break;
  1920. case C_L_SHIFT: printf("C_L_SHIFT"); break;
  1921. case C_NEG: printf("C_NEG"); break;
  1922. case C_NOT: printf("C_NOT"); break;
  1923. case C_LNOT: printf("C_LNOT"); break;
  1924. case C_NOP: printf("C_NOP"); break;
  1925. case C_OP3: printf("C_OP3"); break;
  1926. case C_STR:
  1927. j = strlen(script_buf + i);
  1928. printf("C_STR %s",script_buf + i);
  1929. i+= j+1;
  1930. break;
  1931. default:
  1932. printf("unknown");
  1933. }
  1934. printf("\n");
  1935. }
  1936. }
  1937. #endif
  1938. CREATE(code,struct script_code,1);
  1939. code->script_buf = script_buf;
  1940. code->script_size = script_size;
  1941. code->script_vars = NULL;
  1942. return code;
  1943. }
  1944. //
  1945. // Script state
  1946. //
  1947. enum {RUN = 0,STOP,END,RERUNLINE,GOTO,RETFUNC};
  1948. /// Returns the player attached to this script, identified by the rid.
  1949. /// If there is no player attached, the script is terminated.
  1950. TBL_PC *script_rid2sd(struct script_state *st)
  1951. {
  1952. TBL_PC *sd=map_id2sd(st->rid);
  1953. if(!sd){
  1954. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1955. st->state = END;
  1956. }
  1957. return sd;
  1958. }
  1959. /// Retrieves the value of a script data
  1960. int get_val(struct script_state* st, struct script_data* data)
  1961. {
  1962. char* name;
  1963. char prefix;
  1964. char postfix;
  1965. TBL_PC* sd = NULL;
  1966. if( !data_isreference(data) )
  1967. return 0;// not a variable
  1968. name = reference_getname(data);
  1969. prefix = name[0];
  1970. postfix = name[strlen(name) - 1];
  1971. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1972. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1973. {
  1974. sd = script_rid2sd(st);
  1975. if( sd == NULL )
  1976. {// needs player attached
  1977. if( postfix == '$' )
  1978. {// string variable
  1979. ShowError("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1980. data->type = C_CONSTSTR;
  1981. data->u.str = "";
  1982. }
  1983. else
  1984. {// integer variable
  1985. ShowError("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1986. data->type = C_INT;
  1987. data->u.num = 0;
  1988. }
  1989. return 0;
  1990. }
  1991. }
  1992. if( postfix == '$' )
  1993. {// string variable
  1994. data->type = C_CONSTSTR;
  1995. switch( prefix )
  1996. {
  1997. case '@':
  1998. data->u.str = pc_readregstr(sd, data->u.num);
  1999. break;
  2000. case '$':
  2001. data->u.str = (char *)idb_get(mapregstr_db, data->u.num);
  2002. break;
  2003. case '#':
  2004. if( name[1] == '#' )
  2005. data->u.str = pc_readaccountreg2str(sd, name);// global
  2006. else
  2007. data->u.str = pc_readaccountregstr(sd, name);// local
  2008. break;
  2009. case '.':
  2010. {
  2011. struct linkdb_node** n =
  2012. data->ref ? data->ref:
  2013. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2014. &st->script->script_vars;// npc variable
  2015. data->u.str = linkdb_search(n, (void*)reference_getuid(data));
  2016. }
  2017. break;
  2018. default:
  2019. data->u.str = pc_readglobalreg_str(sd, name);
  2020. break;
  2021. }
  2022. if( data->u.str == NULL )
  2023. data->u.str = "";
  2024. }
  2025. else
  2026. {// integer variable
  2027. data->type = C_INT;
  2028. if( reference_toconstant(data) )
  2029. {
  2030. data->u.num = reference_getconstant(data);
  2031. }
  2032. else if( reference_toparam(data) )
  2033. {
  2034. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2035. }
  2036. else
  2037. switch( prefix )
  2038. {
  2039. case '@':
  2040. data->u.num = pc_readreg(sd, data->u.num);
  2041. break;
  2042. case '$':
  2043. data->u.num = (int)idb_get(mapreg_db, data->u.num);
  2044. break;
  2045. case '#':
  2046. if( name[1] == '#' )
  2047. data->u.num = pc_readaccountreg2(sd, name);// global
  2048. else
  2049. data->u.num = pc_readaccountreg(sd, name);// local
  2050. break;
  2051. case '.':
  2052. {
  2053. struct linkdb_node** n =
  2054. data->ref ? data->ref:
  2055. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2056. &st->script->script_vars;// npc variable
  2057. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2058. }
  2059. break;
  2060. default:
  2061. data->u.num = pc_readglobalreg(sd, name);
  2062. break;
  2063. }
  2064. }
  2065. return 0;
  2066. }
  2067. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2068. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2069. {
  2070. struct script_data data;
  2071. data.type = C_NAME;
  2072. data.u.num = uid;
  2073. data.ref = ref;
  2074. get_val(st, &data);
  2075. return (data.type == C_INT ? (void*)data.u.num : (void*)data.u.str);
  2076. }
  2077. /*==========================================
  2078. * Stores the value of a script variable
  2079. * Return value is 0 on fail, 1 on success.
  2080. *------------------------------------------*/
  2081. static int set_reg(struct script_state* st, TBL_PC* sd, int num, char* name, void* value, struct linkdb_node** ref)
  2082. {
  2083. char prefix = name[0]; char postfix = name[strlen(name)-1];
  2084. if (postfix == '$') { // string variable
  2085. char* str = (char*)value;
  2086. switch (prefix) {
  2087. case '@':
  2088. return pc_setregstr(sd, num, str);
  2089. case '$':
  2090. return mapreg_setregstr(num, str);
  2091. case '#':
  2092. return (name[1] == '#') ?
  2093. pc_setaccountreg2str(sd, name, str) :
  2094. pc_setaccountregstr(sd, name, str);
  2095. case '.': {
  2096. char* p;
  2097. struct linkdb_node** n;
  2098. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2099. p = linkdb_search(n, (void*)num);
  2100. if (p) {
  2101. linkdb_erase(n, (void*)num);
  2102. aFree(p);
  2103. }
  2104. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2105. }
  2106. return 1;
  2107. default:
  2108. return pc_setglobalreg_str(sd, name, str);
  2109. }
  2110. } else { // integer variable
  2111. int val = (int)value;
  2112. if(str_data[num&0x00ffffff].type == C_PARAM)
  2113. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  2114. switch (prefix) {
  2115. case '@':
  2116. return pc_setreg(sd, num, val);
  2117. case '$':
  2118. return mapreg_setreg(num, val);
  2119. case '#':
  2120. return (name[1] == '#') ?
  2121. pc_setaccountreg2(sd, name, val) :
  2122. pc_setaccountreg(sd, name, val);
  2123. case '.': {
  2124. struct linkdb_node** n;
  2125. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2126. if (val == 0)
  2127. linkdb_erase(n, (void*)num);
  2128. else
  2129. linkdb_replace(n, (void*)num, (void*)val);
  2130. }
  2131. return 1;
  2132. default:
  2133. return pc_setglobalreg(sd, name, val);
  2134. }
  2135. }
  2136. }
  2137. int set_var(TBL_PC* sd, char* name, void* val)
  2138. {
  2139. return set_reg(NULL, sd, add_str(name), name, val, NULL);
  2140. }
  2141. /// Converts the data to a string
  2142. const char* conv_str(struct script_state* st, struct script_data* data)
  2143. {
  2144. char* p;
  2145. get_val(st, data);
  2146. if( data_isstring(data) )
  2147. {// nothing to convert
  2148. }
  2149. else if( data_isint(data) )
  2150. {// int -> string
  2151. CREATE(p, char, ITEM_NAME_LENGTH);
  2152. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2153. p[ITEM_NAME_LENGTH-1] = '\0';
  2154. data->type = C_STR;
  2155. data->u.str = p;
  2156. }
  2157. else if( data_isreference(data) )
  2158. {// reference -> string
  2159. //##TODO when does this happen (check get_val) [FlavioJS]
  2160. data->type = C_CONSTSTR;
  2161. data->u.str = reference_getname(data);
  2162. }
  2163. else
  2164. {// unsupported data type
  2165. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2166. script_reportdata(data);
  2167. script_reportsrc(st);
  2168. data->type = C_CONSTSTR;
  2169. data->u.str = "";
  2170. }
  2171. return data->u.str;
  2172. }
  2173. /// Converts the data to an int
  2174. int conv_num(struct script_state* st, struct script_data* data)
  2175. {
  2176. char* p;
  2177. long num;
  2178. get_val(st, data);
  2179. if( data_isint(data) )
  2180. {// nothing to convert
  2181. }
  2182. else if( data_isstring(data) )
  2183. {// string -> int
  2184. // the result does not overflow or underflow, it is capped instead
  2185. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2186. p = data->u.str;
  2187. errno = 0;
  2188. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2189. if( errno == ERANGE
  2190. #if LONG_MAX > INT_MAX
  2191. || num < INT_MIN || num > INT_MAX
  2192. #endif
  2193. )
  2194. {
  2195. if( num <= INT_MIN )
  2196. {
  2197. num = INT_MIN;
  2198. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2199. }
  2200. else//if( num >= INT_MAX )
  2201. {
  2202. num = INT_MAX;
  2203. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2204. }
  2205. script_reportdata(data);
  2206. script_reportsrc(st);
  2207. }
  2208. if( data->type == C_STR )
  2209. aFree(p);
  2210. data->type = C_INT;
  2211. data->u.num = (int)num;
  2212. }
  2213. #if 0
  2214. // FIXME this function is being used to retrieve the position of labels and
  2215. // probably other stuff [FlavioJS]
  2216. else
  2217. {// unsupported data type
  2218. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2219. script_reportdata(data);
  2220. script_reportsrc(st);
  2221. data->type = C_INT;
  2222. data->u.num = 0;
  2223. }
  2224. #endif
  2225. return data->u.num;
  2226. }
  2227. //
  2228. // Stack operations
  2229. //
  2230. /// Increases the size of the stack
  2231. void stack_expand(struct script_stack* stack)
  2232. {
  2233. stack->sp_max += 64;
  2234. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2235. stack->sp_max * sizeof(stack->stack_data[0]) );
  2236. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2237. 64 * sizeof(stack->stack_data[0]) );
  2238. }
  2239. /// Pushes a value into the stack
  2240. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2241. /// Pushes a value into the stack (with reference)
  2242. void push_val2(struct script_stack* stack, int type, int val, struct linkdb_node** ref)
  2243. {
  2244. if( stack->sp >= stack->sp_max )
  2245. stack_expand(stack);
  2246. stack->stack_data[stack->sp].type = type;
  2247. stack->stack_data[stack->sp].u.num = val;
  2248. stack->stack_data[stack->sp].ref = ref;
  2249. stack->sp++;
  2250. }
  2251. /// Pushes a string into the stack
  2252. void push_str(struct script_stack* stack, int type, char* str)
  2253. {
  2254. if( stack->sp >= stack->sp_max )
  2255. stack_expand(stack);
  2256. stack->stack_data[stack->sp].type = type;
  2257. stack->stack_data[stack->sp].u.str = str;
  2258. stack->stack_data[stack->sp].ref = NULL;
  2259. stack->sp++;
  2260. }
  2261. /// Pushes a copy of the target position into the stack
  2262. void push_copy(struct script_stack* stack, int pos)
  2263. {
  2264. switch( stack->stack_data[pos].type )
  2265. {
  2266. case C_CONSTSTR:
  2267. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2268. break;
  2269. case C_STR:
  2270. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2271. break;
  2272. default:
  2273. push_val2(
  2274. stack,stack->stack_data[pos].type,
  2275. stack->stack_data[pos].u.num,
  2276. stack->stack_data[pos].ref
  2277. );
  2278. break;
  2279. }
  2280. }
  2281. /// Removes the values in indexes [start,end[ from the stack
  2282. void pop_stack(struct script_stack* stack, int start, int end)
  2283. {
  2284. struct script_data* data;
  2285. int i;
  2286. if( start < 0 )
  2287. start = 0;
  2288. if( end > stack->sp_max )
  2289. end = stack->sp_max;
  2290. if( start >= end )
  2291. return;// nothing to pop
  2292. // free stack elements
  2293. for( i = start; i < end; i++ )
  2294. {
  2295. data = &stack->stack_data[i];
  2296. if( data->type == C_STR )
  2297. aFree(data->u.str);
  2298. data->type = C_NOP;
  2299. }
  2300. // move the rest of the elements
  2301. if( stack->sp > end )
  2302. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2303. stack->sp -= end - start;
  2304. }
  2305. ///
  2306. ///
  2307. ///
  2308. /*==========================================
  2309. * スクリプト依存変数、関数依存変数の解放
  2310. *------------------------------------------*/
  2311. void script_free_vars(struct linkdb_node **node)
  2312. {
  2313. struct linkdb_node *n = *node;
  2314. while(n) {
  2315. char *name = str_buf + str_data[(int)(n->key)&0x00ffffff].str;
  2316. char postfix = name[strlen(name)-1];
  2317. if( postfix == '$' ) {
  2318. // 文字型変数なので、データ削除
  2319. aFree(n->data);
  2320. }
  2321. n = n->next;
  2322. }
  2323. linkdb_final( node );
  2324. }
  2325. /*==========================================
  2326. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2327. *------------------------------------------*/
  2328. void script_free_stack(struct script_stack *stack)
  2329. {
  2330. int i;
  2331. for(i = 0; i < stack->sp; i++) {
  2332. if( stack->stack_data[i].type == C_STR ) {
  2333. aFree(stack->stack_data[i].u.str);
  2334. stack->stack_data[i].type = C_INT;
  2335. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2336. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2337. script_free_vars( n );
  2338. aFree( n );
  2339. }
  2340. }
  2341. script_free_vars( stack->var_function );
  2342. aFree(stack->var_function);
  2343. aFree(stack->stack_data);
  2344. aFree(stack);
  2345. }
  2346. void script_free_code(struct script_code* code)
  2347. {
  2348. script_free_vars( &code->script_vars );
  2349. aFree( code->script_buf );
  2350. aFree( code );
  2351. }
  2352. //
  2353. // 実行部main
  2354. //
  2355. /*==========================================
  2356. * コマンドの読み取り
  2357. *------------------------------------------*/
  2358. static int unget_com_data=-1;
  2359. int get_com(unsigned char *script,int *pos)
  2360. {
  2361. int i,j;
  2362. if(unget_com_data>=0){
  2363. i=unget_com_data;
  2364. unget_com_data=-1;
  2365. return i;
  2366. }
  2367. if(script[*pos]>=0x80){
  2368. return C_INT;
  2369. }
  2370. i=0; j=0;
  2371. while(script[*pos]>=0x40){
  2372. i=script[(*pos)++]<<j;
  2373. j+=6;
  2374. }
  2375. return i+(script[(*pos)++]<<j);
  2376. }
  2377. /*==========================================
  2378. * コマンドのプッシュバック
  2379. *------------------------------------------*/
  2380. void unget_com(int c)
  2381. {
  2382. if(unget_com_data!=-1){
  2383. if(battle_config.error_log)
  2384. ShowError("unget_com can back only 1 data\n");
  2385. }
  2386. unget_com_data=c;
  2387. }
  2388. /*==========================================
  2389. * 数値の所得
  2390. *------------------------------------------*/
  2391. int get_num(unsigned char *script,int *pos)
  2392. {
  2393. int i,j;
  2394. i=0; j=0;
  2395. while(script[*pos]>=0xc0){
  2396. i+=(script[(*pos)++]&0x7f)<<j;
  2397. j+=6;
  2398. }
  2399. return i+((script[(*pos)++]&0x7f)<<j);
  2400. }
  2401. /*==========================================
  2402. * スタックから値を取り出す
  2403. *------------------------------------------*/
  2404. int pop_val(struct script_state* st)
  2405. {
  2406. if(st->stack->sp<=0)
  2407. return 0;
  2408. st->stack->sp--;
  2409. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2410. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2411. return st->stack->stack_data[st->stack->sp].u.num;
  2412. return 0;
  2413. }
  2414. int isstr(struct script_data *c)
  2415. {
  2416. if( data_isstring(c) )
  2417. return 1;
  2418. else if( data_isreference(c) ) {
  2419. char *p = str_buf + str_data[c->u.num & 0xffffff].str;
  2420. char postfix = p[strlen(p)-1];
  2421. return (postfix == '$');
  2422. }
  2423. return 0;
  2424. }
  2425. /// Ternary operators
  2426. /// test ? if_true : if_false
  2427. void op_3(struct script_state* st, int op)
  2428. {
  2429. struct script_data* data;
  2430. int flag = 0;
  2431. data = script_getdatatop(st, -3);
  2432. get_val(st, data);
  2433. if( data_isstring(data) )
  2434. flag = data->u.str[0];// "" -> false
  2435. else if( data_isint(data) )
  2436. flag = data->u.num;// 0 -> false
  2437. else
  2438. {
  2439. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2440. script_reportdata(data);
  2441. script_reportsrc(st);
  2442. script_removetop(st, -3, 0);
  2443. script_pushnil(st);
  2444. return;
  2445. }
  2446. if( flag )
  2447. script_pushcopytop(st, -2);
  2448. else
  2449. script_pushcopytop(st, -1);
  2450. script_removetop(st, -4, -1);
  2451. }
  2452. /// Binary string operators
  2453. /// s1 EQ s2 -> i
  2454. /// s1 NE s2 -> i
  2455. /// s1 GT s2 -> i
  2456. /// s1 GE s2 -> i
  2457. /// s1 LT s2 -> i
  2458. /// s1 LE s2 -> i
  2459. /// s1 ADD s2 -> s
  2460. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2461. {
  2462. int a = 0;
  2463. switch(op){
  2464. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2465. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2466. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2467. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2468. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2469. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2470. case C_ADD:
  2471. {
  2472. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2473. strcpy(buf, s1);
  2474. strcat(buf, s2);
  2475. script_pushstr(st, buf);
  2476. return;
  2477. }
  2478. default:
  2479. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2480. script_reportsrc(st);
  2481. script_pushnil(st);
  2482. st->state = END;
  2483. return;
  2484. }
  2485. script_pushint(st,a);
  2486. }
  2487. /// Binary number operators
  2488. /// i OP i -> i
  2489. void op_2num(struct script_state* st, int op, int i1, int i2)
  2490. {
  2491. int ret;
  2492. double ret_double;
  2493. switch( op )
  2494. {
  2495. case C_AND: ret = i1 & i2; break;
  2496. case C_OR: ret = i1 | i2; break;
  2497. case C_XOR: ret = i1 ^ i2; break;
  2498. case C_LAND: ret = (i1 && i2); break;
  2499. case C_LOR: ret = (i1 || i2); break;
  2500. case C_EQ: ret = (i1 == i2); break;
  2501. case C_NE: ret = (i1 != i2); break;
  2502. case C_GT: ret = (i1 > i2); break;
  2503. case C_GE: ret = (i1 >= i2); break;
  2504. case C_LT: ret = (i1 < i2); break;
  2505. case C_LE: ret = (i1 <= i2); break;
  2506. case C_R_SHIFT: ret = i1>>i2; break;
  2507. case C_L_SHIFT: ret = i1<<i2; break;
  2508. case C_DIV:
  2509. case C_MOD:
  2510. if( i2 == 0 )
  2511. {
  2512. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2513. script_reportsrc(st);
  2514. script_pushnil(st);
  2515. st->state = END;
  2516. return;
  2517. }
  2518. else if( op == C_DIV )
  2519. ret = i1 / i2;
  2520. else//if( op == C_MOD )
  2521. ret = i1 % i2;
  2522. break;
  2523. default:
  2524. switch( op )
  2525. {// operators that can overflow/underflow
  2526. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2527. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2528. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2529. default:
  2530. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2531. script_reportsrc(st);
  2532. script_pushnil(st);
  2533. return;
  2534. }
  2535. if( ret_double < (double)INT_MIN )
  2536. {
  2537. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2538. script_reportsrc(st);
  2539. ret = INT_MIN;
  2540. }
  2541. else if( ret_double > (double)INT_MAX )
  2542. {
  2543. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2544. script_reportsrc(st);
  2545. ret = INT_MAX;
  2546. }
  2547. }
  2548. script_pushint(st, ret);
  2549. }
  2550. /// Binary operators
  2551. void op_2(struct script_state *st, int op)
  2552. {
  2553. struct script_data* left;
  2554. struct script_data* right;
  2555. left = script_getdatatop(st, -2);
  2556. right = script_getdatatop(st, -1);
  2557. get_val(st, left);
  2558. get_val(st, right);
  2559. // automatic conversions
  2560. switch( op )
  2561. {
  2562. case C_ADD:
  2563. if( data_isint(left) && data_isstring(right) )
  2564. {// convert int-string to string-string
  2565. conv_str(st, left);
  2566. }
  2567. else if( data_isstring(left) && data_isint(right) )
  2568. {// convert string-int to string-string
  2569. conv_str(st, right);
  2570. }
  2571. break;
  2572. }
  2573. if( data_isstring(left) && data_isstring(right) )
  2574. {// ss => op_2str
  2575. op_2str(st, op, left->u.str, right->u.str);
  2576. script_removetop(st, -3, -1);// pop the two values before the top one
  2577. }
  2578. else if( data_isint(left) && data_isint(right) )
  2579. {// ii => op_2num
  2580. int i1 = left->u.num;
  2581. int i2 = right->u.num;
  2582. script_removetop(st, -2, 0);
  2583. op_2num(st, op, i1, i2);
  2584. }
  2585. else
  2586. {// invalid argument
  2587. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2588. script_reportdata(left);
  2589. script_reportdata(right);
  2590. script_reportsrc(st);
  2591. script_removetop(st, -2, 0);
  2592. script_pushnil(st);
  2593. st->state = END;
  2594. }
  2595. }
  2596. /// Unary operators
  2597. /// NEG i -> i
  2598. /// NOT i -> i
  2599. /// LNOT i -> i
  2600. void op_1(struct script_state* st, int op)
  2601. {
  2602. struct script_data* data;
  2603. int i1;
  2604. data = script_getdatatop(st, -1);
  2605. get_val(st, data);
  2606. if( !data_isint(data) )
  2607. {// not a number
  2608. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2609. script_reportdata(data);
  2610. script_reportsrc(st);
  2611. script_pushnil(st);
  2612. st->state = END;
  2613. return;
  2614. }
  2615. i1 = data->u.num;
  2616. script_removetop(st, -1, 0);
  2617. switch( op )
  2618. {
  2619. case C_NEG: i1 = -i1; break;
  2620. case C_NOT: i1 = ~i1; break;
  2621. case C_LNOT: i1 = !i1; break;
  2622. default:
  2623. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2624. script_reportsrc(st);
  2625. script_pushnil(st);
  2626. st->state = END;
  2627. return;
  2628. }
  2629. script_pushint(st, i1);
  2630. }
  2631. /*==========================================
  2632. * 関数の実行
  2633. *------------------------------------------*/
  2634. int run_func(struct script_state *st)
  2635. {
  2636. int i,start_sp,end_sp,func;
  2637. end_sp=st->stack->sp;
  2638. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2639. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2640. if(battle_config.error_log)
  2641. ShowError("function not found\n");
  2642. // st->stack->sp=0;
  2643. st->state=END;
  2644. script_reportsrc(st);
  2645. return 1;
  2646. }
  2647. start_sp=i-1;
  2648. st->start=i-1;
  2649. st->end=end_sp;
  2650. func=st->stack->stack_data[st->start].u.num;
  2651. #ifdef DEBUG_RUN
  2652. if(battle_config.etc_log) {
  2653. ShowDebug("run_func : %s? (%d(%d)) sp=%d (%d...%d)\n",str_buf+str_data[func].str, func, str_data[func].type, st->stack->sp, st->start, st->end);
  2654. ShowDebug("stack dump :");
  2655. for(i=0;i<end_sp;i++){
  2656. switch(st->stack->stack_data[i].type){
  2657. case C_INT:
  2658. printf(" int(%d)",st->stack->stack_data[i].u.num);
  2659. break;
  2660. case C_NAME:
  2661. printf(" name(%s)",str_buf+str_data[st->stack->stack_data[i].u.num & 0xffffff].str);
  2662. break;
  2663. case C_ARG:
  2664. printf(" arg");
  2665. break;
  2666. case C_POS:
  2667. printf(" pos(%d)",st->stack->stack_data[i].u.num);
  2668. break;
  2669. case C_STR:
  2670. printf(" str(%s)",st->stack->stack_data[i].u.str);
  2671. break;
  2672. case C_CONSTSTR:
  2673. printf(" cstr(%s)",st->stack->stack_data[i].u.str);
  2674. break;
  2675. default:
  2676. printf(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2677. }
  2678. }
  2679. printf("\n");
  2680. }
  2681. #endif
  2682. if(str_data[func].type!=C_FUNC ){
  2683. ShowMessage ("run_func: '"CL_WHITE"%s"CL_RESET"' (type %d) is not function and command!\n", str_buf+str_data[func].str, str_data[func].type);
  2684. // st->stack->sp=0;
  2685. st->state=END;
  2686. script_reportsrc(st);
  2687. return 1;
  2688. }
  2689. #ifdef DEBUG_RUN
  2690. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n",
  2691. str_buf+str_data[func].str,func,str_data[func].type,st->pos
  2692. );
  2693. #endif
  2694. if(str_data[func].func){
  2695. if (str_data[func].func(st)) //Report error
  2696. script_reportsrc(st);
  2697. } else {
  2698. if(battle_config.error_log)
  2699. ShowError("run_func : %s? (%d(%d))\n",str_buf+str_data[func].str,func,str_data[func].type);
  2700. script_pushint(st,0);
  2701. script_reportsrc(st);
  2702. }
  2703. // Stack's datum are used when re-run functions [Eoe]
  2704. if(st->state != RERUNLINE) {
  2705. pop_stack(st->stack,start_sp,end_sp);
  2706. }
  2707. if(st->state==RETFUNC){
  2708. // ユーザー定義関数からの復帰
  2709. int olddefsp=st->stack->defsp;
  2710. int i;
  2711. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2712. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2713. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2714. st->state=END;
  2715. script_reportsrc(st);
  2716. return 1;
  2717. }
  2718. script_free_vars( st->stack->var_function );
  2719. aFree(st->stack->var_function);
  2720. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2721. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2722. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2723. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2724. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2725. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2726. st->state=GOTO;
  2727. }
  2728. return 0;
  2729. }
  2730. /*==========================================
  2731. * スクリプトの実行
  2732. *------------------------------------------*/
  2733. void run_script_main(struct script_state *st);
  2734. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2735. {
  2736. struct script_state *st;
  2737. TBL_PC *sd=NULL;
  2738. if(rootscript==NULL || pos<0)
  2739. return;
  2740. if (rid) sd = map_id2sd(rid);
  2741. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2742. //Resume script.
  2743. st = sd->st;
  2744. } else {
  2745. st = aCalloc(sizeof(struct script_state), 1);
  2746. // the script is different, make new script_state and stack
  2747. st->stack = aMalloc (sizeof(struct script_stack));
  2748. st->stack->sp=0;
  2749. st->stack->sp_max=64;
  2750. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2751. st->stack->defsp = st->stack->sp;
  2752. st->stack->var_function = aCalloc(1, sizeof(struct linkdb_node*));
  2753. st->state = RUN;
  2754. st->script = rootscript;
  2755. }
  2756. st->pos = pos;
  2757. st->rid = rid;
  2758. st->oid = oid;
  2759. st->sleep.timer = INVALID_TIMER;
  2760. run_script_main(st);
  2761. }
  2762. void script_stop_sleeptimers(int id)
  2763. {
  2764. struct script_state* st;
  2765. for(;;)
  2766. {
  2767. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2768. if( st == NULL )
  2769. break; // no more sleep timers
  2770. if( st->sleep.timer != INVALID_TIMER )
  2771. delete_timer(st->sleep.timer, run_script_timer);
  2772. script_free_stack(st->stack);
  2773. aFree(st);
  2774. }
  2775. }
  2776. /*==========================================
  2777. * 指定ノードをsleep_dbから削除
  2778. *------------------------------------------*/
  2779. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2780. {
  2781. struct linkdb_node *retnode;
  2782. if( n == NULL)
  2783. return NULL;
  2784. if( n->prev == NULL )
  2785. sleep_db = n->next;
  2786. else
  2787. n->prev->next = n->next;
  2788. if( n->next )
  2789. n->next->prev = n->prev;
  2790. retnode = n->next;
  2791. aFree( n );
  2792. return retnode; // 次のノードを返す
  2793. }
  2794. /*==========================================
  2795. * sleep用タイマー関数
  2796. *------------------------------------------*/
  2797. int run_script_timer(int tid, unsigned int tick, int id, int data)
  2798. {
  2799. struct script_state *st = (struct script_state *)data;
  2800. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2801. TBL_PC *sd = map_id2sd(st->rid);
  2802. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2803. { //Character mismatch. Cancel execution.
  2804. st->rid = 0;
  2805. st->state = END;
  2806. }
  2807. while( node && st->sleep.timer != -1 ) {
  2808. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2809. script_erase_sleepdb(node);
  2810. st->sleep.timer = -1;
  2811. break;
  2812. }
  2813. node = node->next;
  2814. }
  2815. if(st->state != RERUNLINE)
  2816. st->sleep.tick = 0;
  2817. run_script_main(st);
  2818. return 0;
  2819. }
  2820. /*==========================================
  2821. * スクリプトの実行メイン部分
  2822. *------------------------------------------*/
  2823. void run_script_main(struct script_state *st)
  2824. {
  2825. int c;
  2826. int cmdcount=script_config.check_cmdcount;
  2827. int gotocount=script_config.check_gotocount;
  2828. TBL_PC *sd;
  2829. //For backing up purposes
  2830. struct script_state *bk_st = NULL;
  2831. int bk_npcid = 0;
  2832. struct script_stack *stack=st->stack;
  2833. sd = st->rid?map_id2sd(st->rid):NULL;
  2834. if(sd){
  2835. if(sd->st != st){
  2836. bk_st = sd->st;
  2837. bk_npcid = sd->npc_id;
  2838. }
  2839. sd->st = st;
  2840. sd->npc_id = st->oid;
  2841. }
  2842. if(st->state == RERUNLINE) {
  2843. st->state = RUN;
  2844. run_func(st);
  2845. if(st->state == GOTO)
  2846. st->state = RUN;
  2847. } else if(st->state != END)
  2848. st->state = RUN;
  2849. while(st->state == RUN){
  2850. c= get_com(st->script->script_buf,&st->pos);
  2851. switch(c){
  2852. case C_EOL:
  2853. if( stack->sp != stack->defsp )
  2854. {
  2855. if( stack->sp > stack->defsp )
  2856. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2857. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2858. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2859. } else if( battle_config.error_log )
  2860. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2861. stack->sp = stack->defsp;
  2862. }
  2863. break;
  2864. case C_INT:
  2865. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2866. break;
  2867. case C_POS:
  2868. case C_NAME:
  2869. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2870. st->pos+=3;
  2871. break;
  2872. case C_ARG:
  2873. push_val(stack,c,0);
  2874. break;
  2875. case C_STR:
  2876. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2877. while(st->script->script_buf[st->pos++]);
  2878. break;
  2879. case C_FUNC:
  2880. run_func(st);
  2881. if(st->state==GOTO){
  2882. st->state = RUN;
  2883. if( gotocount>0 && (--gotocount)<=0 ){
  2884. ShowError("run_script: infinity loop !\n");
  2885. script_reportsrc(st);
  2886. st->state=END;
  2887. }
  2888. }
  2889. break;
  2890. case C_NEG:
  2891. case C_NOT:
  2892. case C_LNOT:
  2893. op_1(st ,c);
  2894. break;
  2895. case C_ADD:
  2896. case C_SUB:
  2897. case C_MUL:
  2898. case C_DIV:
  2899. case C_MOD:
  2900. case C_EQ:
  2901. case C_NE:
  2902. case C_GT:
  2903. case C_GE:
  2904. case C_LT:
  2905. case C_LE:
  2906. case C_AND:
  2907. case C_OR:
  2908. case C_XOR:
  2909. case C_LAND:
  2910. case C_LOR:
  2911. case C_R_SHIFT:
  2912. case C_L_SHIFT:
  2913. op_2(st, c);
  2914. break;
  2915. case C_OP3:
  2916. op_3(st, c);
  2917. break;
  2918. case C_NOP:
  2919. st->state=END;
  2920. break;
  2921. default:
  2922. if(battle_config.error_log)
  2923. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2924. st->state=END;
  2925. break;
  2926. }
  2927. if( cmdcount>0 && (--cmdcount)<=0 ){
  2928. ShowError("run_script: infinity loop !\n");
  2929. script_reportsrc(st);
  2930. st->state=END;
  2931. }
  2932. }
  2933. if(st->sleep.tick > 0) {
  2934. //Delay execution
  2935. st->sleep.charid = sd?sd->status.char_id:0;
  2936. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2937. run_script_timer, st->sleep.charid, (int)st);
  2938. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2939. //Restore previous script
  2940. if (sd) {
  2941. sd->st = bk_st;
  2942. sd->npc_id = bk_npcid;
  2943. bk_st = NULL; //Remove tag for removal.
  2944. }
  2945. }
  2946. else if(st->state != END && sd){
  2947. //Resume later (st is already attached to player).
  2948. if(bk_st) {
  2949. ShowWarning("Unable to restore stack! Double continuation!\n");
  2950. //Report BOTH scripts to see if that can help somehow.
  2951. ShowDebug("Previous script (lost):\n");
  2952. script_reportsrc(bk_st);
  2953. ShowDebug("Current script:\n");
  2954. script_reportsrc(st);
  2955. }
  2956. } else {
  2957. //Dispose of script.
  2958. if (sd)
  2959. { //Restore previous stack and save char.
  2960. if(sd->state.using_fake_npc){
  2961. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  2962. sd->state.using_fake_npc = 0;
  2963. }
  2964. //Restore previous script if any.
  2965. sd->st = bk_st;
  2966. sd->npc_id = bk_npcid;
  2967. if (!bk_st)
  2968. npc_event_dequeue(sd);
  2969. else
  2970. bk_st = NULL; //Remove tag for removal.
  2971. if (sd->state.reg_dirty&2)
  2972. intif_saveregistry(sd,2);
  2973. if (sd->state.reg_dirty&1)
  2974. intif_saveregistry(sd,1);
  2975. }
  2976. st->pos = -1;
  2977. script_free_stack (st->stack);
  2978. aFree(st);
  2979. }
  2980. if (bk_st)
  2981. { //Remove previous script
  2982. bk_st->pos = -1;
  2983. script_free_stack(bk_st->stack);
  2984. aFree(bk_st);
  2985. bk_st = NULL;
  2986. }
  2987. }
  2988. /*==========================================
  2989. * マップ変数の変更
  2990. *------------------------------------------*/
  2991. int mapreg_setreg(int num, int val)
  2992. {
  2993. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2994. int i = num >> 24;
  2995. char* name = str_buf + str_data[num&0x00ffffff].str;
  2996. if( val != 0 ) {
  2997. if(idb_put(mapreg_db,num,(void*)val))
  2998. ;
  2999. else if(name[1] != '@') {
  3000. char tmp_str[32*2+1];
  3001. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  3002. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%d')", mapregsql_db, mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, tmp_str, i, val) )
  3003. Sql_ShowDebug(mmysql_handle);
  3004. }
  3005. } else { // val == 0
  3006. if(name[1] != '@') { // Remove from database because it is unused.
  3007. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3008. Sql_ShowDebug(mmysql_handle);
  3009. }
  3010. idb_remove(mapreg_db,num);
  3011. }
  3012. #else
  3013. if(val != 0)
  3014. idb_put(mapreg_db,num,(void*)val);
  3015. else
  3016. idb_remove(mapreg_db,num);
  3017. #endif
  3018. mapreg_dirty = 1;
  3019. return 1;
  3020. }
  3021. /*==========================================
  3022. * 文字列型マップ変数の変更
  3023. *------------------------------------------*/
  3024. int mapreg_setregstr(int num, const char* str)
  3025. {
  3026. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  3027. int i = num >> 24;
  3028. char* name = str_buf + str_data[num&0x00ffffff].str;
  3029. if( str==NULL || *str==0 ) {
  3030. if(name[1] != '@') {
  3031. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3032. Sql_ShowDebug(mmysql_handle);
  3033. }
  3034. idb_remove(mapregstr_db,num);
  3035. mapreg_dirty = 1;
  3036. return 1;
  3037. }
  3038. if (idb_put(mapregstr_db,num, aStrdup(str)))
  3039. ;
  3040. else if(name[1] != '@') { //put returned null, so we must insert.
  3041. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  3042. char tmp_str[32*2+1];
  3043. char tmp_str2[255*2+1];
  3044. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  3045. Sql_EscapeStringLen(mmysql_handle, tmp_str2, str, strnlen(str, 255));
  3046. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%s')", mapregsql_db, mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, tmp_str, i, tmp_str2) )
  3047. Sql_ShowDebug(mmysql_handle);
  3048. }
  3049. #else
  3050. if( str==NULL || *str==0 )
  3051. idb_remove(mapregstr_db,num);
  3052. else
  3053. idb_put(mapregstr_db,num,aStrdup(str));
  3054. #endif
  3055. mapreg_dirty = 1;
  3056. return 1;
  3057. }
  3058. /*==========================================
  3059. * 永続的マップ変数の読み込み
  3060. *------------------------------------------*/
  3061. static int script_load_mapreg(void)
  3062. {
  3063. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3064. FILE* fp;
  3065. char line[1024];
  3066. if( (fp=fopen(mapreg_txt,"rt")) == NULL )
  3067. return -1;
  3068. while(fgets(line,sizeof(line),fp))
  3069. {
  3070. char buf1[256],buf2[1024];
  3071. char* p;
  3072. int n,v,s,i;
  3073. if( sscanf(line,"%255[^,],%d\t%n",buf1,&i,&n)!=2 &&
  3074. (i=0,sscanf(line,"%[^\t]\t%n",buf1,&n)!=1) )
  3075. continue;
  3076. if( buf1[strlen(buf1)-1] == '$' ) {
  3077. if( sscanf(line + n, "%[^\n\r]", buf2) != 1 ) {
  3078. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3079. continue;
  3080. }
  3081. p = aStrdup(buf2);
  3082. s = add_str(buf1);
  3083. idb_put(mapregstr_db, (i<<24)|s, p);
  3084. } else {
  3085. if( sscanf(line + n, "%d", &v) != 1 ) {
  3086. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3087. continue;
  3088. }
  3089. s = add_str(buf1);
  3090. idb_put(mapreg_db, (i<<24)|s, (void*)v);
  3091. }
  3092. }
  3093. fclose(fp);
  3094. mapreg_dirty = 0;
  3095. return 0;
  3096. #else
  3097. /*
  3098. 0 1 2
  3099. +-------------------------+
  3100. | varname | index | value |
  3101. +-------------------------+
  3102. */
  3103. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  3104. char varname[32+1];
  3105. int index;
  3106. char value[255+1];
  3107. uint32 length;
  3108. if ( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `%s`, `%s`, `%s` FROM `%s`", mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, mapregsql_db)
  3109. || SQL_ERROR == SqlStmt_Execute(stmt)
  3110. ) {
  3111. SqlStmt_ShowDebug(stmt);
  3112. SqlStmt_Free(stmt);
  3113. return -1;
  3114. }
  3115. SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &varname[0], 32, &length, NULL);
  3116. SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &index, 0, NULL, NULL);
  3117. SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &value[0], 255, NULL, NULL);
  3118. while ( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  3119. {
  3120. if( varname[length-1] == '$' ) {
  3121. int s = add_str(varname);
  3122. int i = index;
  3123. char* p = aStrdup(value);
  3124. idb_put(mapregstr_db, (i<<24)|s, p);
  3125. } else {
  3126. int s = add_str(varname);
  3127. int i = index;
  3128. int v = atoi(value);
  3129. idb_put(mapreg_db, (i<<24)|s, (void *)v);
  3130. }
  3131. }
  3132. SqlStmt_Free(stmt);
  3133. mapreg_dirty = 0;
  3134. return 0;
  3135. #endif /* TXT_ONLY */
  3136. }
  3137. /*==========================================
  3138. * 永続的マップ変数の書き込み
  3139. *------------------------------------------*/
  3140. static int script_save_mapreg_intsub(DBKey key,void *data,va_list ap)
  3141. {
  3142. int num=key.i&0x00ffffff, i=key.i>>24;
  3143. char *name=str_buf+str_data[num].str;
  3144. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3145. FILE *fp=va_arg(ap,FILE*);
  3146. if( name[1]!='@' ) {
  3147. if(i==0)
  3148. fprintf(fp,"%s\t%d\n", name, (int)data);
  3149. else
  3150. fprintf(fp,"%s,%d\t%d\n", name, i, (int)data);
  3151. }
  3152. #else
  3153. if ( name[1] != '@') {
  3154. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `%s`='%d' WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_value, (int)data, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3155. Sql_ShowDebug(mmysql_handle);
  3156. }
  3157. #endif
  3158. return 0;
  3159. }
  3160. static int script_save_mapreg_strsub(DBKey key,void *data,va_list ap)
  3161. {
  3162. int num=key.i&0x00ffffff, i=key.i>>24;
  3163. char *name=str_buf+str_data[num].str;
  3164. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3165. FILE *fp=va_arg(ap,FILE*);
  3166. if( name[1]!='@' ) {
  3167. if(i==0)
  3168. fprintf(fp,"%s\t%s\n", name, (char *)data);
  3169. else
  3170. fprintf(fp,"%s,%d\t%s\n", name, i, (char *)data);
  3171. }
  3172. #else
  3173. if ( name[1] != '@') {
  3174. char tmp_str2[2*255+1];
  3175. Sql_EscapeStringLen(mmysql_handle, tmp_str2, (char*)data, safestrnlen((char*)data, 255));
  3176. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `%s`='%s' WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_value, tmp_str2, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3177. Sql_ShowDebug(mmysql_handle);
  3178. }
  3179. #endif
  3180. return 0;
  3181. }
  3182. static int script_save_mapreg(void)
  3183. {
  3184. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3185. FILE *fp;
  3186. int lock;
  3187. if( (fp=lock_fopen(mapreg_txt,&lock))==NULL ) {
  3188. ShowError("script_save_mapreg: Unable to lock-open file [%s]\n",mapreg_txt);
  3189. return -1;
  3190. }
  3191. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub,fp);
  3192. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub,fp);
  3193. lock_fclose(fp,mapreg_txt,&lock);
  3194. #else
  3195. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub);
  3196. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub);
  3197. #endif
  3198. mapreg_dirty=0;
  3199. return 0;
  3200. }
  3201. static int script_autosave_mapreg(int tid,unsigned int tick,int id,int data)
  3202. {
  3203. if(mapreg_dirty)
  3204. if (script_save_mapreg() == -1)
  3205. ShowError("Failed to save the mapreg data!\n");
  3206. return 0;
  3207. }
  3208. int script_config_read_sub(char *cfgName)
  3209. {
  3210. int i;
  3211. char line[1024],w1[1024],w2[1024];
  3212. FILE *fp;
  3213. fp=fopen(cfgName,"r");
  3214. if(fp==NULL){
  3215. ShowError("file not found: [%s]\n", cfgName);
  3216. return 1;
  3217. }
  3218. while(fgets(line, sizeof(line), fp))
  3219. {
  3220. if(line[0] == '/' && line[1] == '/')
  3221. continue;
  3222. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3223. if(i!=2)
  3224. continue;
  3225. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3226. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3227. }
  3228. else if(strcmpi(w1,"check_cmdcount")==0) {
  3229. script_config.check_cmdcount = config_switch(w2);
  3230. }
  3231. else if(strcmpi(w1,"check_gotocount")==0) {
  3232. script_config.check_gotocount = config_switch(w2);
  3233. }
  3234. else if(strcmpi(w1,"event_script_type")==0) {
  3235. script_config.event_script_type = config_switch(w2);
  3236. }
  3237. else if(strcmpi(w1,"event_requires_trigger")==0) {
  3238. script_config.event_requires_trigger = config_switch(w2);
  3239. }
  3240. else if(strcmpi(w1,"die_event_name")==0) {
  3241. strncpy(script_config.die_event_name, w2, NAME_LENGTH-1);
  3242. if (strlen(script_config.die_event_name) != strlen(w2))
  3243. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.die_event_name);
  3244. }
  3245. else if(strcmpi(w1,"kill_pc_event_name")==0) {
  3246. strncpy(script_config.kill_pc_event_name, w2, NAME_LENGTH-1);
  3247. if (strlen(script_config.kill_pc_event_name) != strlen(w2))
  3248. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_pc_event_name);
  3249. }
  3250. else if(strcmpi(w1,"kill_mob_event_name")==0) {
  3251. strncpy(script_config.kill_mob_event_name, w2, NAME_LENGTH-1);
  3252. if (strlen(script_config.kill_mob_event_name) != strlen(w2))
  3253. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_mob_event_name);
  3254. }
  3255. else if(strcmpi(w1,"login_event_name")==0) {
  3256. strncpy(script_config.login_event_name, w2, NAME_LENGTH-1);
  3257. if (strlen(script_config.login_event_name) != strlen(w2))
  3258. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.login_event_name);
  3259. }
  3260. else if(strcmpi(w1,"logout_event_name")==0) {
  3261. strncpy(script_config.logout_event_name, w2, NAME_LENGTH-1);
  3262. if (strlen(script_config.logout_event_name) != strlen(w2))
  3263. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.logout_event_name);
  3264. }
  3265. else if(strcmpi(w1,"loadmap_event_name")==0) {
  3266. strncpy(script_config.loadmap_event_name, w2, NAME_LENGTH-1);
  3267. if (strlen(script_config.loadmap_event_name) != strlen(w2))
  3268. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.loadmap_event_name);
  3269. }
  3270. else if(strcmpi(w1,"baselvup_event_name")==0) {
  3271. strncpy(script_config.baselvup_event_name, w2, NAME_LENGTH-1);
  3272. if (strlen(script_config.baselvup_event_name) != strlen(w2))
  3273. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.baselvup_event_name);
  3274. }
  3275. else if(strcmpi(w1,"joblvup_event_name")==0) {
  3276. strncpy(script_config.joblvup_event_name, w2, NAME_LENGTH-1);
  3277. if (strlen(script_config.joblvup_event_name) != strlen(w2))
  3278. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.joblvup_event_name);
  3279. }
  3280. else if(strcmpi(w1,"import")==0){
  3281. script_config_read_sub(w2);
  3282. }
  3283. }
  3284. fclose(fp);
  3285. return 0;
  3286. }
  3287. int script_config_read(char *cfgName)
  3288. { //Script related variables should be initialized once! [Skotlex]
  3289. memset (&script_config, 0, sizeof(script_config));
  3290. script_config.warn_func_mismatch_paramnum = 1;
  3291. script_config.check_cmdcount = 65535;
  3292. script_config.check_gotocount = 2048;
  3293. script_config.event_script_type = 0;
  3294. script_config.event_requires_trigger = 1;
  3295. return script_config_read_sub(cfgName);
  3296. }
  3297. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3298. {
  3299. struct script_code *code = (struct script_code *)data;
  3300. if(code){
  3301. script_free_vars( &code->script_vars );
  3302. aFree( code->script_buf );
  3303. aFree( code );
  3304. }
  3305. return 0;
  3306. }
  3307. /*==========================================
  3308. * 終了
  3309. *------------------------------------------*/
  3310. int do_final_script()
  3311. {
  3312. #ifdef DEBUG_HASH
  3313. if (battle_config.etc_log)
  3314. {
  3315. FILE *fp = fopen("hash_dump.txt","wt");
  3316. if(fp) {
  3317. int i,count[SCRIPT_HASH_SIZE];
  3318. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3319. int n=0;
  3320. int min=INT_MAX,max=0,zero=0;
  3321. double mean=0.0f;
  3322. double median=0.0f;
  3323. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3324. memset(count, 0, sizeof(count));
  3325. fprintf(fp,"num : calced_val -> hash : data_name\n");
  3326. fprintf(fp,"---------------------------------------------------------------\n");
  3327. for(i=LABEL_START; i<str_num; i++) {
  3328. unsigned int h2=calc_hash2(str_buf+str_data[i].str);
  3329. unsigned int h =h2%SCRIPT_HASH_SIZE;
  3330. fprintf(fp,"%04d: %10u -> %3u : %s\n",i,h2,h,str_buf+str_data[i].str);
  3331. ++count[h];
  3332. }
  3333. fprintf(fp,"--------------------\n\n");
  3334. memset(count2, 0, sizeof(count2));
  3335. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3336. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3337. if(min > count[i])
  3338. min = count[i]; // minimun count of collision
  3339. if(max < count[i])
  3340. max = count[i]; // maximun count of collision
  3341. if(count[i] == 0)
  3342. zero++;
  3343. ++count2[count[i]];
  3344. }
  3345. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3346. for( i=min; i <= max; ++i ){
  3347. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3348. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3349. }
  3350. for( i=min; i <= max; ++i ){
  3351. n += count2[i];
  3352. if( n*2 >= SCRIPT_HASH_SIZE )
  3353. {
  3354. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3355. median = (i+i+1)/2.0f;
  3356. else
  3357. median = i;
  3358. break;
  3359. }
  3360. }
  3361. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3362. fclose(fp);
  3363. }
  3364. }
  3365. #endif
  3366. if(mapreg_dirty>=0)
  3367. script_save_mapreg();
  3368. mapreg_db->destroy(mapreg_db,NULL);
  3369. mapregstr_db->destroy(mapregstr_db,NULL);
  3370. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3371. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3372. if(sleep_db) {
  3373. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3374. while(n) {
  3375. struct script_state *st = (struct script_state *)n->data;
  3376. script_free_stack(st->stack);
  3377. aFree(st);
  3378. n = n->next;
  3379. }
  3380. linkdb_final(&sleep_db);
  3381. }
  3382. if (str_data)
  3383. aFree(str_data);
  3384. if (str_buf)
  3385. aFree(str_buf);
  3386. return 0;
  3387. }
  3388. /*==========================================
  3389. * 初期化
  3390. *------------------------------------------*/
  3391. int do_init_script()
  3392. {
  3393. mapreg_db= db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_BASE,sizeof(int));
  3394. mapregstr_db=db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  3395. userfunc_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_DUP_KEY,0);
  3396. scriptlabel_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA,50);
  3397. script_load_mapreg();
  3398. add_timer_func_list(script_autosave_mapreg, "script_autosave_mapreg");
  3399. add_timer_interval(gettick() + MAPREG_AUTOSAVE_INTERVAL, script_autosave_mapreg, 0, 0, MAPREG_AUTOSAVE_INTERVAL);
  3400. return 0;
  3401. }
  3402. int script_reload()
  3403. {
  3404. if(mapreg_dirty>=0)
  3405. script_save_mapreg();
  3406. mapreg_db->clear(mapreg_db, NULL);
  3407. mapregstr_db->clear(mapregstr_db, NULL);
  3408. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3409. scriptlabel_db->clear(scriptlabel_db, NULL);
  3410. if(sleep_db) {
  3411. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3412. while(n) {
  3413. struct script_state *st = (struct script_state *)n->data;
  3414. if( st->sleep.timer != INVALID_TIMER )
  3415. delete_timer(st->sleep.timer, run_script_timer);
  3416. script_free_stack(st->stack);
  3417. aFree(st);
  3418. n = n->next;
  3419. }
  3420. linkdb_final(&sleep_db);
  3421. }
  3422. script_load_mapreg();
  3423. return 0;
  3424. }
  3425. //-----------------------------------------------------------------------------
  3426. // buildin functions
  3427. //
  3428. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3429. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3430. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3431. /////////////////////////////////////////////////////////////////////
  3432. // NPC interaction
  3433. //
  3434. /// Appends a message to the npc dialog.
  3435. /// If a dialog doesn't exist yet, one is created.
  3436. ///
  3437. /// mes "<message>";
  3438. BUILDIN_FUNC(mes)
  3439. {
  3440. TBL_PC* sd = script_rid2sd(st);
  3441. if( sd == NULL )
  3442. return 1;
  3443. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3444. return 0;
  3445. }
  3446. /// Displays the button 'next' in the npc dialog.
  3447. /// The dialog text is cleared and the script continues when the button is pressed.
  3448. ///
  3449. /// next;
  3450. BUILDIN_FUNC(next)
  3451. {
  3452. TBL_PC* sd;
  3453. sd = script_rid2sd(st);
  3454. if( sd == NULL )
  3455. return 1;
  3456. st->state = STOP;
  3457. clif_scriptnext(sd, st->oid);
  3458. return 0;
  3459. }
  3460. /// Ends the script and displays the button 'close' on the npc dialog.
  3461. /// The dialog is closed when the button is pressed.
  3462. ///
  3463. /// close;
  3464. BUILDIN_FUNC(close)
  3465. {
  3466. TBL_PC* sd;
  3467. sd = script_rid2sd(st);
  3468. if( sd == NULL )
  3469. return 1;
  3470. st->state = END;
  3471. clif_scriptclose(sd, st->oid);
  3472. return 0;
  3473. }
  3474. /// Displays the button 'close' on the npc dialog.
  3475. /// The dialog is closed and the script continues when the button is pressed.
  3476. ///
  3477. /// close2;
  3478. BUILDIN_FUNC(close2)
  3479. {
  3480. TBL_PC* sd;
  3481. sd = script_rid2sd(st);
  3482. if( sd == NULL )
  3483. return 1;
  3484. st->state = STOP;
  3485. clif_scriptclose(sd, st->oid);
  3486. return 0;
  3487. }
  3488. /// Counts the number of valid and total number of options in 'str'
  3489. /// If max_count > 0 the counting stops when that valid option is reached
  3490. /// total is incremented for each option (NULL is supported)
  3491. static int menu_countoptions(const char* str, int max_count, int* total)
  3492. {
  3493. int count = 0;
  3494. int bogus_total;
  3495. if( total == NULL )
  3496. total = &bogus_total;
  3497. ++(*total);
  3498. // initial empty options
  3499. while( *str == ':' )
  3500. {
  3501. ++str;
  3502. ++(*total);
  3503. }
  3504. // count menu options
  3505. while( *str != '\0' )
  3506. {
  3507. ++count;
  3508. --max_count;
  3509. if( max_count == 0 )
  3510. break;
  3511. while( *str != ':' && *str != '\0' )
  3512. ++str;
  3513. while( *str == ':' )
  3514. {
  3515. ++str;
  3516. ++(*total);
  3517. }
  3518. }
  3519. return count;
  3520. }
  3521. /// Displays a menu with options and goes to the target label.
  3522. /// The script is stopped if cancel is pressed.
  3523. /// Options with no text are not displayed in the client.
  3524. ///
  3525. /// Options can be grouped together, separated by the character ':' in the text:
  3526. /// ex: menu "A:B:C",L_target;
  3527. /// All these options go to the specified target label.
  3528. ///
  3529. /// The index of the selected option is put in the variable @menu.
  3530. /// Indexes start with 1 and are consistent with grouped and empty options.
  3531. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3532. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3533. ///
  3534. /// NOTE: the client closes the npc dialog when cancel is pressed
  3535. ///
  3536. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3537. BUILDIN_FUNC(menu)
  3538. {
  3539. int i;
  3540. const char* text;
  3541. TBL_PC* sd;
  3542. sd = script_rid2sd(st);
  3543. if( sd == NULL )
  3544. return 1;
  3545. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3546. if( sd->state.menu_or_input == 0 )
  3547. {
  3548. struct StringBuf buf;
  3549. struct script_data* data;
  3550. if( script_lastdata(st) % 2 == 0 )
  3551. {// argument count is not even (1st argument is at index 2)
  3552. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3553. st->state = END;
  3554. return 1;
  3555. }
  3556. StringBuf_Init(&buf);
  3557. for( i = 2, sd->npc_menu = 0; i < script_lastdata(st); i += 2 )
  3558. {
  3559. // menu options
  3560. text = script_getstr(st, i);
  3561. // target label
  3562. data = script_getdata(st, i+1);
  3563. if( !data_islabel(data) )
  3564. {// not a label
  3565. StringBuf_Destroy(&buf);
  3566. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3567. script_reportdata(data);
  3568. st->state = END;
  3569. return 1;
  3570. }
  3571. // append option(s)
  3572. if( text[0] == '\0' )
  3573. continue;// empty string, ignore
  3574. if( sd->npc_menu > 0 )
  3575. StringBuf_AppendStr(&buf, ":");
  3576. StringBuf_AppendStr(&buf, text);
  3577. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3578. }
  3579. st->state = RERUNLINE;
  3580. sd->state.menu_or_input = 1;
  3581. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3582. StringBuf_Destroy(&buf);
  3583. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3584. }
  3585. else if( sd->npc_menu == 0xff )
  3586. {// Cancel was pressed
  3587. sd->state.menu_or_input = 0;
  3588. st->state = END;
  3589. }
  3590. else
  3591. {// goto target label
  3592. int menu = 0;
  3593. sd->state.menu_or_input = 0;
  3594. if( sd->npc_menu <= 0 )
  3595. {
  3596. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3597. st->state = END;
  3598. return 1;
  3599. }
  3600. // get target label
  3601. for( i = 2; i < script_lastdata(st); i += 2 )
  3602. {
  3603. text = script_getstr(st, i);
  3604. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3605. if( sd->npc_menu <= 0 )
  3606. break;// entry found
  3607. }
  3608. if( sd->npc_menu > 0 )
  3609. {// Invalid selection
  3610. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3611. st->state = END;
  3612. return 1;
  3613. }
  3614. if( !data_islabel(script_getdata(st, i + 1)) )
  3615. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3616. ShowError("script:menu: unexpected data in label argument\n");
  3617. script_reportdata(script_getdata(st, i + 1));
  3618. st->state = END;
  3619. return 1;
  3620. }
  3621. pc_setreg(sd, add_str("@menu"), menu);
  3622. st->pos = script_getnum(st, i + 1);
  3623. st->state = GOTO;
  3624. }
  3625. return 0;
  3626. }
  3627. /// Displays a menu with options and returns the selected option.
  3628. /// Behaves like 'menu' without the target labels.
  3629. ///
  3630. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3631. ///
  3632. /// @see menu
  3633. BUILDIN_FUNC(select)
  3634. {
  3635. int i;
  3636. const char* text;
  3637. TBL_PC* sd;
  3638. sd = script_rid2sd(st);
  3639. if( sd == NULL )
  3640. return 1;
  3641. if( sd->state.menu_or_input == 0 )
  3642. {
  3643. struct StringBuf buf;
  3644. StringBuf_Init(&buf);
  3645. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  3646. {
  3647. text = script_getstr(st, i);
  3648. if( sd->npc_menu > 0 )
  3649. StringBuf_AppendStr(&buf, ":");
  3650. StringBuf_AppendStr(&buf, text);
  3651. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  3652. }
  3653. st->state = RERUNLINE;
  3654. sd->state.menu_or_input = 1;
  3655. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3656. StringBuf_Destroy(&buf);
  3657. }
  3658. else if( sd->npc_menu == 0xff )
  3659. {// Cancel was pressed
  3660. sd->state.menu_or_input = 0;
  3661. st->state = END;
  3662. }
  3663. else
  3664. {// return selected option
  3665. int menu = 0;
  3666. sd->state.menu_or_input = 0;
  3667. for( i = 2; i <= script_lastdata(st); ++i )
  3668. {
  3669. text = script_getstr(st, i);
  3670. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3671. if( sd->npc_menu <= 0 )
  3672. break;// entry found
  3673. }
  3674. pc_setreg(sd, add_str("@menu"), menu);
  3675. script_pushint(st, menu);
  3676. }
  3677. return 0;
  3678. }
  3679. /// Displays a menu with options and returns the selected option.
  3680. /// Behaves like 'menu' without the target labels, except when cancel is
  3681. /// pressed.
  3682. /// When cancel is pressed, the script continues and 255 is returned.
  3683. ///
  3684. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3685. ///
  3686. /// @see menu
  3687. BUILDIN_FUNC(prompt)
  3688. {
  3689. int i;
  3690. const char *text;
  3691. TBL_PC* sd;
  3692. sd = script_rid2sd(st);
  3693. if( sd == NULL )
  3694. return 1;
  3695. if( sd->state.menu_or_input == 0 )
  3696. {
  3697. struct StringBuf buf;
  3698. StringBuf_Init(&buf);
  3699. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  3700. {
  3701. text = script_getstr(st, i);
  3702. if( sd->npc_menu > 0 )
  3703. StringBuf_AppendStr(&buf, ":");
  3704. StringBuf_AppendStr(&buf, text);
  3705. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  3706. }
  3707. st->state = RERUNLINE;
  3708. sd->state.menu_or_input = 1;
  3709. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3710. StringBuf_Destroy(&buf);
  3711. }
  3712. else if( sd->npc_menu == 0xff )
  3713. {// Cancel was pressed
  3714. sd->state.menu_or_input = 0;
  3715. pc_setreg(sd, add_str("@menu"), 0xff);
  3716. script_pushint(st, 0xff);
  3717. }
  3718. else
  3719. {// return selected option
  3720. int menu = 0;
  3721. sd->state.menu_or_input = 0;
  3722. for( i = 2; i <= script_lastdata(st); ++i )
  3723. {
  3724. text = script_getstr(st, i);
  3725. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3726. if( sd->npc_menu <= 0 )
  3727. break;// entry found
  3728. }
  3729. pc_setreg(sd, add_str("@menu"), menu);
  3730. script_pushint(st, menu);
  3731. }
  3732. return 0;
  3733. }
  3734. /////////////////////////////////////////////////////////////////////
  3735. // ...
  3736. //
  3737. /// Jumps to the target script label.
  3738. ///
  3739. /// goto <label>;
  3740. BUILDIN_FUNC(goto)
  3741. {
  3742. if( !data_islabel(script_getdata(st,2)) )
  3743. {
  3744. ShowError("script:goto: not a label\n");
  3745. script_reportdata(script_getdata(st,2));
  3746. st->state = END;
  3747. return 1;
  3748. }
  3749. st->pos = script_getnum(st,2);
  3750. st->state = GOTO;
  3751. return 0;
  3752. }
  3753. /*==========================================
  3754. * ユーザー定義関数の呼び出し
  3755. *------------------------------------------*/
  3756. BUILDIN_FUNC(callfunc)
  3757. {
  3758. int i, j;
  3759. struct linkdb_node** oldval;
  3760. struct script_code *scr, *oldscr;
  3761. const char* str = script_getstr(st,2);
  3762. scr = strdb_get(userfunc_db, str);
  3763. if( !scr )
  3764. {
  3765. ShowError("script:callfunc: function not found! [%s]\n", str);
  3766. st->state = END;
  3767. return 1;
  3768. }
  3769. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3770. push_copy(st->stack,i);
  3771. script_pushint(st,j); // 引数の数をプッシュ
  3772. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3773. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3774. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3775. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3776. oldscr = st->script;
  3777. oldval = st->stack->var_function;
  3778. st->pos = 0;
  3779. st->script = scr;
  3780. st->stack->defsp = st->start+5+j;
  3781. st->state = GOTO;
  3782. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3783. // ' 変数の引き継ぎ
  3784. for( i = 0; i < j; i++ )
  3785. {
  3786. struct script_data* s = script_getdatatop(st, -6-i);
  3787. if( data_isreference(s) && !s->ref )
  3788. {
  3789. char* name = str_buf + str_data[s->u.num&0x00ffffff].str;
  3790. // '@ 変数の引き継ぎ
  3791. if( name[0] == '.' && name[1] == '@' ) {
  3792. s->ref = oldval;
  3793. } else if( name[0] == '.' ) {
  3794. s->ref = &oldscr->script_vars;
  3795. }
  3796. }
  3797. }
  3798. return 0;
  3799. }
  3800. /*==========================================
  3801. * サブルーティンの呼び出し
  3802. *------------------------------------------*/
  3803. BUILDIN_FUNC(callsub)
  3804. {
  3805. int pos=script_getnum(st,2);
  3806. int i,j;
  3807. if(!data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2))) {
  3808. ShowError("script:callsub: argument is not a label\n");
  3809. script_reportdata(script_getdata(st,2));
  3810. st->state=END;
  3811. return 1;
  3812. } else {
  3813. struct linkdb_node **oldval = st->stack->var_function;
  3814. for(i=st->start+3,j=0;i<st->end;i++,j++)
  3815. push_copy(st->stack,i);
  3816. script_pushint(st,j); // 引数の数をプッシュ
  3817. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3818. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3819. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3820. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3821. st->pos=pos;
  3822. st->stack->defsp=st->start+5+j;
  3823. st->state=GOTO;
  3824. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3825. // ' 変数の引き継ぎ
  3826. for(i = 0; i < j; i++) {
  3827. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  3828. if( data_isreference(s) && !s->ref ) {
  3829. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  3830. // '@ 変数の引き継ぎ
  3831. if( name[0] == '.' && name[1] == '@' ) {
  3832. s->ref = oldval;
  3833. }
  3834. }
  3835. }
  3836. }
  3837. return 0;
  3838. }
  3839. /// Retrieves an argument provided to callfunc/callsub.
  3840. /// If the argument doesn't exist
  3841. ///
  3842. /// getarg(<index>{,<default_value>}) -> <value>
  3843. BUILDIN_FUNC(getarg)
  3844. {
  3845. int idx;
  3846. int count;
  3847. int stsp;
  3848. if( st->stack->defsp < 5 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3849. {
  3850. ShowError("script:getarg: no callfunc or callsub!\n");
  3851. st->state = END;
  3852. return 1;
  3853. }
  3854. count = conv_num(st, &(st->stack->stack_data[st->stack->defsp - 5]));
  3855. stsp = st->stack->defsp - count - 5;
  3856. idx = script_getnum(st,2);
  3857. if( idx < count )
  3858. push_copy(st->stack, stsp + idx);
  3859. else if( script_hasdata(st,3) )
  3860. script_pushcopy(st, 3);
  3861. else
  3862. {
  3863. ShowError("script:getarg: index (idx=%d) out of range (count=%d) and no default value found\n", idx, count);
  3864. st->state = END;
  3865. return 1;
  3866. }
  3867. return 0;
  3868. }
  3869. /// Returns from the current function, optionaly returning a value from the functions.
  3870. /// Don't use outside script functions.
  3871. ///
  3872. /// return;
  3873. /// return <value>;
  3874. BUILDIN_FUNC(return)
  3875. {
  3876. if( script_hasdata(st,2) )
  3877. {// return value
  3878. struct script_data* data;
  3879. script_pushcopy(st, 2);
  3880. data = script_getdatatop(st, -1);
  3881. if( data_isreference(data) )
  3882. {
  3883. char* name = reference_getname(data);
  3884. if( name[0] == '.' && name[1] == '@' )
  3885. {// temporary script variable, convert to value
  3886. get_val(st, data);
  3887. if( data_isstring(data) )
  3888. {// duplicate the string
  3889. data->type = C_STR;
  3890. data->u.str = aStrdup(data->u.str);
  3891. }
  3892. }
  3893. else if( name[0] == '.' && !data->ref )
  3894. {// script variable, link to current script
  3895. data->ref = &st->script->script_vars;
  3896. }
  3897. }
  3898. }
  3899. else
  3900. {// no return value
  3901. script_pushnil(st);
  3902. }
  3903. st->state = RETFUNC;
  3904. return 0;
  3905. }
  3906. /// Returns a random number from 0 to <range>-1.
  3907. /// Or returns a random number from <min> to <max>.
  3908. /// If <min> is greater than <max>, their numbers are switched.
  3909. /// rand(<range>) -> <int>
  3910. /// rand(<min>,<max>) -> <int>
  3911. BUILDIN_FUNC(rand)
  3912. {
  3913. int range;
  3914. int min;
  3915. int max;
  3916. if( script_hasdata(st,3) )
  3917. {// min,max
  3918. min = script_getnum(st,2);
  3919. max = script_getnum(st,3);
  3920. if( max < min )
  3921. swap(min, max);
  3922. range = max - min + 1;
  3923. }
  3924. else
  3925. {// range
  3926. min = 0;
  3927. range = script_getnum(st,2);
  3928. }
  3929. if( range <= 1 )
  3930. script_pushint(st, min);
  3931. else
  3932. script_pushint(st, rand()%range + min);
  3933. return 0;
  3934. }
  3935. /*==========================================
  3936. *
  3937. *------------------------------------------*/
  3938. BUILDIN_FUNC(warp)
  3939. {
  3940. int x,y;
  3941. const char *str;
  3942. TBL_PC *sd=script_rid2sd(st);
  3943. nullpo_retr(0, sd);
  3944. str=script_getstr(st,2);
  3945. x=script_getnum(st,3);
  3946. y=script_getnum(st,4);
  3947. if(strcmp(str,"Random")==0)
  3948. pc_randomwarp(sd,3);
  3949. else if(strcmp(str,"SavePoint")==0){
  3950. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3951. }else if(strcmp(str,"Save")==0){
  3952. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3953. }else
  3954. pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3955. return 0;
  3956. }
  3957. /*==========================================
  3958. * エリア指定ワープ
  3959. *------------------------------------------*/
  3960. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3961. {
  3962. int x,y;
  3963. unsigned int map;
  3964. map=va_arg(ap, unsigned int);
  3965. x=va_arg(ap,int);
  3966. y=va_arg(ap,int);
  3967. if(map == 0)
  3968. pc_randomwarp((TBL_PC *)bl,3);
  3969. else
  3970. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3971. return 0;
  3972. }
  3973. BUILDIN_FUNC(areawarp)
  3974. {
  3975. int x,y,m;
  3976. unsigned int index;
  3977. const char *str;
  3978. const char *mapname;
  3979. int x0,y0,x1,y1;
  3980. mapname=script_getstr(st,2);
  3981. x0=script_getnum(st,3);
  3982. y0=script_getnum(st,4);
  3983. x1=script_getnum(st,5);
  3984. y1=script_getnum(st,6);
  3985. str=script_getstr(st,7);
  3986. x=script_getnum(st,8);
  3987. y=script_getnum(st,9);
  3988. if( (m=map_mapname2mapid(mapname))< 0)
  3989. return 0;
  3990. if(strcmp(str,"Random")==0)
  3991. index = 0;
  3992. else if(!(index=mapindex_name2id(str)))
  3993. return 0;
  3994. map_foreachinarea(buildin_areawarp_sub,
  3995. m,x0,y0,x1,y1,BL_PC, index,x,y );
  3996. return 0;
  3997. }
  3998. /*==========================================
  3999. * warpchar [LuzZza]
  4000. * Useful for warp one player from
  4001. * another player npc-session.
  4002. * Using: warpchar "mapname",x,y,Char_ID;
  4003. *------------------------------------------*/
  4004. BUILDIN_FUNC(warpchar)
  4005. {
  4006. int x,y,a,i;
  4007. const char *str;
  4008. TBL_PC *sd, **pl_allsd;
  4009. int users;
  4010. str=script_getstr(st,2);
  4011. x=script_getnum(st,3);
  4012. y=script_getnum(st,4);
  4013. a=script_getnum(st,5);
  4014. pl_allsd = map_getallusers(&users);
  4015. for(i=0; i<users; i++) {
  4016. sd = pl_allsd[i];
  4017. if(sd->status.char_id == a) {
  4018. if(strcmp(str, "Random") == 0)
  4019. pc_randomwarp(sd, 3);
  4020. else if(strcmp(str, "SavePoint") == 0)
  4021. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  4022. else
  4023. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  4024. }
  4025. }
  4026. return 0;
  4027. }
  4028. /*==========================================
  4029. * Warpparty - [Fredzilla]
  4030. * Syntax: warpparty "mapname",x,y,Party_ID;
  4031. *------------------------------------------*/
  4032. BUILDIN_FUNC(warpparty)
  4033. {
  4034. int x,y;
  4035. const char *str;
  4036. int p_id;
  4037. int i;
  4038. unsigned short mapindex;
  4039. TBL_PC *pl_sd;
  4040. struct party_data *p=NULL;
  4041. str=script_getstr(st,2);
  4042. x=script_getnum(st,3);
  4043. y=script_getnum(st,4);
  4044. p_id=script_getnum(st,5);
  4045. if(p_id < 1)
  4046. return 0;
  4047. p = party_search(p_id);
  4048. if (!p)
  4049. return 0;
  4050. if(strcmp(str,"Random")==0)
  4051. {
  4052. for (i = 0; i < MAX_PARTY; i++)
  4053. {
  4054. if ((pl_sd = p->data[i].sd))
  4055. {
  4056. if(map[pl_sd->bl.m].flag.nowarp)
  4057. continue;
  4058. pc_randomwarp(pl_sd,3);
  4059. }
  4060. }
  4061. }
  4062. else if(strcmp(str,"SavePointAll")==0)
  4063. {
  4064. for (i = 0; i < MAX_PARTY; i++)
  4065. {
  4066. if ((pl_sd = p->data[i].sd))
  4067. {
  4068. if(map[pl_sd->bl.m].flag.noreturn)
  4069. continue;
  4070. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4071. }
  4072. }
  4073. }
  4074. else if(strcmp(str,"SavePoint")==0)
  4075. {
  4076. pl_sd=script_rid2sd(st);
  4077. if (!pl_sd) return 0;
  4078. mapindex=pl_sd->status.save_point.map;
  4079. x=pl_sd->status.save_point.x;
  4080. y=pl_sd->status.save_point.y;
  4081. for (i = 0; i < MAX_PARTY; i++)
  4082. {
  4083. if ((pl_sd = p->data[i].sd))
  4084. {
  4085. if(map[pl_sd->bl.m].flag.noreturn)
  4086. continue;
  4087. pc_setpos(pl_sd,mapindex,x,y,3);
  4088. }
  4089. }
  4090. }
  4091. else if(strcmp(str,"Leader")==0)
  4092. {
  4093. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4094. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4095. return 0;
  4096. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  4097. return 0;
  4098. mapindex = p->data[i].sd->mapindex;
  4099. x = p->data[i].sd->bl.x;
  4100. y = p->data[i].sd->bl.y;
  4101. for (i = 0; i < MAX_PARTY; i++)
  4102. {
  4103. pl_sd = p->data[i].sd;
  4104. if (!pl_sd)
  4105. continue;
  4106. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4107. continue;
  4108. pc_setpos(pl_sd,mapindex,x,y,3);
  4109. }
  4110. }
  4111. else
  4112. {
  4113. mapindex = mapindex_name2id(str);
  4114. if (!mapindex) //Show source of npc error.
  4115. return 1;
  4116. for (i = 0; i < MAX_PARTY; i++)
  4117. {
  4118. if ((pl_sd = p->data[i].sd))
  4119. {
  4120. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4121. continue;
  4122. pc_setpos(pl_sd,mapindex,x,y,3);
  4123. }
  4124. }
  4125. }
  4126. return 0;
  4127. }
  4128. /*==========================================
  4129. * Warpguild - [Fredzilla]
  4130. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4131. *------------------------------------------*/
  4132. BUILDIN_FUNC(warpguild)
  4133. {
  4134. int x,y;
  4135. unsigned short mapindex;
  4136. const char *str;
  4137. int g;
  4138. int i;
  4139. TBL_PC *pl_sd, **pl_allsd;
  4140. int users;
  4141. TBL_PC *sd;
  4142. str=script_getstr(st,2);
  4143. x=script_getnum(st,3);
  4144. y=script_getnum(st,4);
  4145. g=script_getnum(st,5);
  4146. sd=script_rid2sd(st);
  4147. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4148. return 0;
  4149. if(g < 1)
  4150. return 0;
  4151. pl_allsd = map_getallusers(&users);
  4152. if(strcmp(str,"Random")==0)
  4153. {
  4154. for (i = 0; i < users; i++)
  4155. {
  4156. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4157. {
  4158. if(map[pl_sd->bl.m].flag.nowarp)
  4159. continue;
  4160. pc_randomwarp(pl_sd,3);
  4161. }
  4162. }
  4163. }
  4164. else if(strcmp(str,"SavePointAll")==0)
  4165. {
  4166. if(map[sd->bl.m].flag.noreturn)
  4167. return 0;
  4168. for (i = 0; i < users; i++)
  4169. {
  4170. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4171. {
  4172. if(map[pl_sd->bl.m].flag.noreturn)
  4173. continue;
  4174. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4175. }
  4176. }
  4177. }
  4178. else if(strcmp(str,"SavePoint")==0)
  4179. {
  4180. if(map[sd->bl.m].flag.noreturn)
  4181. return 0;
  4182. mapindex=sd->status.save_point.map;
  4183. x=sd->status.save_point.x;
  4184. y=sd->status.save_point.y;
  4185. for (i = 0; i < users; i++)
  4186. {
  4187. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4188. {
  4189. if(map[pl_sd->bl.m].flag.noreturn)
  4190. continue;
  4191. pc_setpos(pl_sd,mapindex,x,y,3);
  4192. }
  4193. }
  4194. }
  4195. else
  4196. {
  4197. mapindex = mapindex_name2id(str);
  4198. for (i = 0; i < users; i++)
  4199. {
  4200. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4201. {
  4202. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4203. continue;
  4204. pc_setpos(pl_sd,mapindex,x,y,3);
  4205. }
  4206. }
  4207. }
  4208. return 0;
  4209. }
  4210. /*==========================================
  4211. *
  4212. *------------------------------------------*/
  4213. BUILDIN_FUNC(heal)
  4214. {
  4215. TBL_PC *sd;
  4216. int hp,sp;
  4217. sd = script_rid2sd(st);
  4218. if (!sd) return 0;
  4219. hp=script_getnum(st,2);
  4220. sp=script_getnum(st,3);
  4221. status_heal(&sd->bl, hp, sp, 1);
  4222. return 0;
  4223. }
  4224. /*==========================================
  4225. *
  4226. *------------------------------------------*/
  4227. BUILDIN_FUNC(itemheal)
  4228. {
  4229. TBL_PC *sd;
  4230. int hp,sp;
  4231. hp=script_getnum(st,2);
  4232. sp=script_getnum(st,3);
  4233. if(potion_flag==1) {
  4234. potion_hp = hp;
  4235. potion_sp = sp;
  4236. return 0;
  4237. }
  4238. sd = script_rid2sd(st);
  4239. if (!sd) return 0;
  4240. pc_itemheal(sd,sd->itemid,hp,sp);
  4241. return 0;
  4242. }
  4243. /*==========================================
  4244. *
  4245. *------------------------------------------*/
  4246. BUILDIN_FUNC(percentheal)
  4247. {
  4248. int hp,sp;
  4249. hp=script_getnum(st,2);
  4250. sp=script_getnum(st,3);
  4251. if(potion_flag==1) {
  4252. potion_per_hp = hp;
  4253. potion_per_sp = sp;
  4254. return 0;
  4255. }
  4256. pc_percentheal(script_rid2sd(st),hp,sp);
  4257. return 0;
  4258. }
  4259. /*==========================================
  4260. *
  4261. *------------------------------------------*/
  4262. BUILDIN_FUNC(jobchange)
  4263. {
  4264. int job, upper=-1;
  4265. job=script_getnum(st,2);
  4266. if( script_hasdata(st,3) )
  4267. upper=script_getnum(st,3);
  4268. if (pcdb_checkid(job))
  4269. {
  4270. pc_jobchange(script_rid2sd(st),job, upper);
  4271. if(use_irc && irc_announce_jobchange_flag)
  4272. irc_announce_jobchange(script_rid2sd(st));
  4273. }
  4274. return 0;
  4275. }
  4276. /*==========================================
  4277. *
  4278. *------------------------------------------*/
  4279. BUILDIN_FUNC(jobname)
  4280. {
  4281. int class_=script_getnum(st,2);
  4282. script_pushconststr(st,job_name(class_));
  4283. return 0;
  4284. }
  4285. /*==========================================
  4286. *
  4287. *------------------------------------------*/
  4288. BUILDIN_FUNC(input)
  4289. {
  4290. TBL_PC *sd = script_rid2sd(st);
  4291. struct script_data *data = script_getdata(st,2);
  4292. int num = data->u.num;
  4293. char *name=str_buf+str_data[num&0x00ffffff].str;
  4294. char postfix = name[strlen(name)-1];
  4295. if (!sd) return 1;
  4296. if( !data_isreference(data) ){
  4297. ShowError("script:input: not a variable\n");
  4298. script_reportdata(data);
  4299. return 1;
  4300. }
  4301. if( !sd->state.menu_or_input )
  4302. { // first invocation, display npc input box
  4303. sd->state.menu_or_input = 1;
  4304. st->state = RERUNLINE;
  4305. if( postfix == '$' )
  4306. clif_scriptinputstr(sd,st->oid);
  4307. else
  4308. clif_scriptinput(sd,st->oid);
  4309. }
  4310. else
  4311. { // take received text/value and store it in the designated variable
  4312. sd->state.menu_or_input = 0;
  4313. if( postfix == '$' )
  4314. {
  4315. set_reg(st,sd,num,name,(void*)sd->npc_str,script_getref(st,2));
  4316. }
  4317. else
  4318. {
  4319. // limit the input to a non-negative value smaller than 'vending_max_value' (for scripts that didn't check this)
  4320. sd->npc_amount = cap_value(sd->npc_amount, 0, battle_config.vending_max_value);
  4321. set_reg(st,sd,num,name,(void*)sd->npc_amount,script_getref(st,2));
  4322. }
  4323. }
  4324. return 0;
  4325. }
  4326. /*==========================================
  4327. * 変数設定
  4328. *------------------------------------------*/
  4329. BUILDIN_FUNC(set)
  4330. {
  4331. TBL_PC *sd=NULL;
  4332. int num=st->stack->stack_data[st->start+2].u.num;
  4333. char *name=str_buf+str_data[num&0x00ffffff].str;
  4334. char prefix=*name;
  4335. char postfix=name[strlen(name)-1];
  4336. if( !data_isreference(script_getdata(st,2)) ){
  4337. ShowError("script:set: not a variable\n");
  4338. script_reportdata(script_getdata(st,2));
  4339. st->state = END;
  4340. return 1;
  4341. }
  4342. if(not_server_variable(prefix))
  4343. sd=script_rid2sd(st);
  4344. if( postfix=='$' ){
  4345. // 文字列
  4346. const char *str = script_getstr(st,3);
  4347. set_reg(st,sd,num,name,(void*)str,script_getref(st,2));
  4348. }else{
  4349. // 数値
  4350. int val = script_getnum(st,3);
  4351. set_reg(st,sd,num,name,(void*)val,script_getref(st,2));
  4352. }
  4353. return 0;
  4354. }
  4355. /////////////////////////////////////////////////////////////////////
  4356. /// Array variables
  4357. ///
  4358. /// Returns the size of the specified array
  4359. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4360. {
  4361. int32 ret = idx;
  4362. if( isstring )
  4363. {
  4364. for( ; idx < 128; ++idx )
  4365. {
  4366. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4367. if( str && *str )
  4368. ret = idx + 1;
  4369. }
  4370. }
  4371. else
  4372. {
  4373. for( ; idx < 128; ++idx )
  4374. {
  4375. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4376. if( num )
  4377. ret = idx + 1;
  4378. }
  4379. }
  4380. return ret;
  4381. }
  4382. /// Sets values of an array, from the starting index.
  4383. /// ex: setarray arr[1],1,2,3;
  4384. ///
  4385. /// setarray <array variable>,<value1>{,<value2>...};
  4386. BUILDIN_FUNC(setarray)
  4387. {
  4388. struct script_data* data;
  4389. char* name;
  4390. int32 start;
  4391. int32 end;
  4392. int32 id;
  4393. int32 i;
  4394. TBL_PC* sd = NULL;
  4395. data = script_getdata(st, 2);
  4396. if( !data_isreference(data) )
  4397. {
  4398. ShowError("script:setarray: not a variable\n");
  4399. script_reportdata(data);
  4400. st->state = END;
  4401. return 1;// not a variable
  4402. }
  4403. id = reference_getid(data);
  4404. start = reference_getindex(data);
  4405. name = reference_getname(data);
  4406. if( not_array_variable(*name) )
  4407. {
  4408. ShowError("script:setarray: illegal scope\n");
  4409. script_reportdata(data);
  4410. st->state = END;
  4411. return 1;// not supported
  4412. }
  4413. if( not_server_variable(*name) )
  4414. {
  4415. sd = script_rid2sd(st);
  4416. if( sd == NULL )
  4417. return 1;// no player attached
  4418. }
  4419. end = start + script_lastdata(st) - 2;
  4420. if( end > 127 )
  4421. end = 127;
  4422. if( is_string_variable(name) )
  4423. {// string array
  4424. for( i = 3; start < end; ++start, ++i )
  4425. {
  4426. void* v = (void*)script_getstr(st,i);
  4427. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4428. }
  4429. }
  4430. else
  4431. {// int array
  4432. for( i = 3; start < end; ++start, ++i )
  4433. {
  4434. void* v = (void*)script_getnum(st,i);
  4435. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4436. }
  4437. }
  4438. return 0;
  4439. }
  4440. /// Sets count values of an array, from the starting index.
  4441. /// ex: cleararray arr[0],0,1;
  4442. ///
  4443. /// cleararray <array variable>,<value>,<count>;
  4444. BUILDIN_FUNC(cleararray)
  4445. {
  4446. struct script_data* data;
  4447. char* name;
  4448. int32 start;
  4449. int32 end;
  4450. int32 id;
  4451. void* v;
  4452. TBL_PC* sd = NULL;
  4453. data = script_getdata(st, 2);
  4454. if( !data_isreference(data) )
  4455. {
  4456. ShowError("script:cleararray: not a variable\n");
  4457. script_reportdata(data);
  4458. st->state = END;
  4459. return 1;// not a variable
  4460. }
  4461. id = reference_getid(data);
  4462. start = reference_getindex(data);
  4463. name = reference_getname(data);
  4464. if( not_array_variable(*name) )
  4465. {
  4466. ShowError("script:cleararray: illegal scope\n");
  4467. script_reportdata(data);
  4468. st->state = END;
  4469. return 1;// not supported
  4470. }
  4471. if( not_server_variable(*name) )
  4472. {
  4473. sd = script_rid2sd(st);
  4474. if( sd == NULL )
  4475. return 1;// no player attached
  4476. }
  4477. if( is_string_variable(name) )
  4478. v = (void*)script_getstr(st, 3);
  4479. else
  4480. v = (void*)script_getnum(st, 3);
  4481. end = start + script_getnum(st, 4);
  4482. if( end > 127 )
  4483. end = 127;
  4484. for( ; start < end; ++start )
  4485. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4486. return 0;
  4487. }
  4488. /// Copies data from one array to another.
  4489. /// ex: copyarray arr[0],arr[2],2;
  4490. ///
  4491. /// copyarray <destination array variable>,<source array variable>,<count>;
  4492. BUILDIN_FUNC(copyarray)
  4493. {
  4494. struct script_data* data1;
  4495. struct script_data* data2;
  4496. char* name1;
  4497. char* name2;
  4498. int32 idx1;
  4499. int32 idx2;
  4500. int32 id1;
  4501. int32 id2;
  4502. void* v;
  4503. int32 i;
  4504. int32 count;
  4505. TBL_PC* sd = NULL;
  4506. data1 = script_getdata(st, 2);
  4507. data2 = script_getdata(st, 3);
  4508. if( !data_isreference(data1) || !data_isreference(data2) )
  4509. {
  4510. ShowError("script:copyarray: not a variable\n");
  4511. script_reportdata(data1);
  4512. script_reportdata(data2);
  4513. st->state = END;
  4514. return 1;// not a variable
  4515. }
  4516. id1 = reference_getid(data1);
  4517. id2 = reference_getid(data2);
  4518. idx1 = reference_getindex(data1);
  4519. idx2 = reference_getindex(data2);
  4520. name1 = reference_getname(data1);
  4521. name2 = reference_getname(data2);
  4522. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4523. {
  4524. ShowError("script:copyarray: illegal scope\n");
  4525. script_reportdata(data1);
  4526. script_reportdata(data2);
  4527. st->state = END;
  4528. return 1;// not supported
  4529. }
  4530. if( is_string_variable(name1) != is_string_variable(name2) )
  4531. {
  4532. ShowError("script:copyarray: type mismatch\n");
  4533. script_reportdata(data1);
  4534. script_reportdata(data2);
  4535. st->state = END;
  4536. return 1;// data type mismatch
  4537. }
  4538. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4539. {
  4540. sd = script_rid2sd(st);
  4541. if( sd == NULL )
  4542. return 1;// no player attached
  4543. }
  4544. count = script_getnum(st, 4);
  4545. if( count > 128 - idx1 )
  4546. count = 128 - idx1;
  4547. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4548. return 0;// nothing to copy
  4549. if( id1 == id2 && idx1 > idx2 )
  4550. {// destination might be overlapping the source - copy in reverse order
  4551. for( i = count - 1; i >= 0; --i )
  4552. {
  4553. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4554. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4555. }
  4556. }
  4557. else
  4558. {// normal copy
  4559. for( i = 0; i < count; ++i )
  4560. {
  4561. if( idx2 + i < 128 )
  4562. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4563. else// out of range - assume ""/0
  4564. v = (void*)(is_string_variable(name1) ? "" : 0);
  4565. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4566. }
  4567. }
  4568. return 0;
  4569. }
  4570. /// Returns the size of the array.
  4571. /// Assumes that everything before the starting index exists.
  4572. /// ex: getarraysize(arr[3])
  4573. ///
  4574. /// getarraysize(<array variable>) -> <int>
  4575. BUILDIN_FUNC(getarraysize)
  4576. {
  4577. struct script_data* data;
  4578. char* name;
  4579. data = script_getdata(st, 2);
  4580. if( !data_isreference(data) )
  4581. {
  4582. ShowError("script:getarraysize: not a variable\n");
  4583. script_reportdata(data);
  4584. script_pushnil(st);
  4585. st->state = END;
  4586. return 1;// not a variable
  4587. }
  4588. name = reference_getname(data);
  4589. if( not_array_variable(*name) )
  4590. {
  4591. ShowError("script:getarraysize: illegal scope\n");
  4592. script_reportdata(data);
  4593. script_pushnil(st);
  4594. st->state = END;
  4595. return 1;// not supported
  4596. }
  4597. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4598. return 0;
  4599. }
  4600. /// Deletes count or all the elements in an array, from the starting index.
  4601. /// ex: deletearray arr[4],2;
  4602. ///
  4603. /// deletearray <array variable>;
  4604. /// deletearray <array variable>,<count>;
  4605. BUILDIN_FUNC(deletearray)
  4606. {
  4607. struct script_data* data;
  4608. char* name;
  4609. int start;
  4610. int end;
  4611. int id;
  4612. TBL_PC *sd = NULL;
  4613. data = script_getdata(st, 2);
  4614. if( !data_isreference(data) )
  4615. {
  4616. ShowError("script:deletearray: not a variable\n");
  4617. script_reportdata(data);
  4618. st->state = END;
  4619. return 1;// not a variable
  4620. }
  4621. id = reference_getid(data);
  4622. start = reference_getindex(data);
  4623. name = reference_getname(data);
  4624. if( not_array_variable(*name) )
  4625. {
  4626. ShowError("script:deletearray: illegal scope\n");
  4627. script_reportdata(data);
  4628. st->state = END;
  4629. return 1;// not supported
  4630. }
  4631. if( not_server_variable(*name) )
  4632. {
  4633. sd = script_rid2sd(st);
  4634. if( sd == NULL )
  4635. return 1;// no player attached
  4636. }
  4637. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4638. if( start >= end )
  4639. return 0;// nothing to free
  4640. if( script_hasdata(st,3) )
  4641. {
  4642. int count = script_getnum(st, 3);
  4643. if( count > end - start )
  4644. count = end - start;
  4645. if( count <= 0 )
  4646. return 0;// nothing to free
  4647. // move rest of the elements backward
  4648. for( ; start + count < end; ++start )
  4649. {
  4650. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4651. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4652. }
  4653. }
  4654. // clear the rest of the array
  4655. if( is_string_variable(name) )
  4656. {
  4657. for( ; start < end; ++start )
  4658. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4659. }
  4660. else
  4661. {
  4662. for( ; start < end; ++start )
  4663. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4664. }
  4665. return 0;
  4666. }
  4667. /// Returns a reference to the target index of the array variable.
  4668. /// Equivalent to var[index].
  4669. ///
  4670. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4671. BUILDIN_FUNC(getelementofarray)
  4672. {
  4673. struct script_data* data;
  4674. char* name;
  4675. int32 id;
  4676. int i;
  4677. data = script_getdata(st, 2);
  4678. if( !data_isreference(data) )
  4679. {
  4680. ShowError("script:getelementofarray: not a variable\n");
  4681. script_reportdata(data);
  4682. script_pushnil(st);
  4683. st->state = END;
  4684. return 1;// not a variable
  4685. }
  4686. id = reference_getid(data);
  4687. name = reference_getname(data);
  4688. if( not_array_variable(*name) )
  4689. {
  4690. ShowError("script:getelementofarray: illegal scope\n");
  4691. script_reportdata(data);
  4692. script_pushnil(st);
  4693. st->state = END;
  4694. return 1;// not supported
  4695. }
  4696. i = script_getnum(st, 3);
  4697. if( i < 0 || i >= 128 )
  4698. {
  4699. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4700. script_reportdata(data);
  4701. script_pushnil(st);
  4702. st->state = END;
  4703. return 1;// out of range
  4704. }
  4705. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4706. return 0;
  4707. }
  4708. /////////////////////////////////////////////////////////////////////
  4709. /// ...
  4710. ///
  4711. /*==========================================
  4712. *
  4713. *------------------------------------------*/
  4714. BUILDIN_FUNC(setlook)
  4715. {
  4716. int type,val;
  4717. type=script_getnum(st,2);
  4718. val=script_getnum(st,3);
  4719. pc_changelook(script_rid2sd(st),type,val);
  4720. return 0;
  4721. }
  4722. /*==========================================
  4723. *
  4724. *------------------------------------------*/
  4725. BUILDIN_FUNC(cutin)
  4726. {
  4727. clif_cutin(script_rid2sd(st),script_getstr(st,2),script_getnum(st,3));
  4728. return 0;
  4729. }
  4730. /*==========================================
  4731. *
  4732. *------------------------------------------*/
  4733. BUILDIN_FUNC(viewpoint)
  4734. {
  4735. int type,x,y,id,color;
  4736. type=script_getnum(st,2);
  4737. x=script_getnum(st,3);
  4738. y=script_getnum(st,4);
  4739. id=script_getnum(st,5);
  4740. color=script_getnum(st,6);
  4741. clif_viewpoint(script_rid2sd(st),st->oid,type,x,y,id,color);
  4742. return 0;
  4743. }
  4744. /*==========================================
  4745. *
  4746. *------------------------------------------*/
  4747. BUILDIN_FUNC(countitem)
  4748. {
  4749. int nameid, i;
  4750. int count = 0;
  4751. struct script_data* data;
  4752. TBL_PC* sd = script_rid2sd(st);
  4753. if (!sd) {
  4754. script_pushint(st,0);
  4755. return 0;
  4756. }
  4757. data = script_getdata(st,2);
  4758. get_val(st,data);
  4759. if( data_isstring(data) ) {
  4760. const char* name = conv_str(st,data);
  4761. struct item_data* item_data;
  4762. if((item_data = itemdb_searchname(name)) != NULL)
  4763. nameid = item_data->nameid;
  4764. else
  4765. nameid = 0;
  4766. } else
  4767. nameid = conv_num(st,data);
  4768. if (nameid < 500) {
  4769. if(battle_config.error_log)
  4770. {
  4771. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4772. script_reportsrc(st);
  4773. }
  4774. script_pushint(st,0);
  4775. return 1;
  4776. }
  4777. for(i = 0; i < MAX_INVENTORY; i++)
  4778. if(sd->status.inventory[i].nameid == nameid)
  4779. count += sd->status.inventory[i].amount;
  4780. script_pushint(st,count);
  4781. return 0;
  4782. }
  4783. /*==========================================
  4784. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4785. * returns number of items that meet the conditions
  4786. *------------------------------------------*/
  4787. BUILDIN_FUNC(countitem2)
  4788. {
  4789. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4790. int count = 0;
  4791. int i;
  4792. struct script_data* data;
  4793. TBL_PC* sd = script_rid2sd(st);
  4794. if (!sd) {
  4795. script_pushint(st,0);
  4796. return 0;
  4797. }
  4798. data = script_getdata(st,2);
  4799. get_val(st,data);
  4800. if( data_isstring(data) ) {
  4801. const char* name = conv_str(st,data);
  4802. struct item_data* item_data;
  4803. if((item_data = itemdb_searchname(name)) != NULL)
  4804. nameid = item_data->nameid;
  4805. else
  4806. nameid = 0;
  4807. } else
  4808. nameid = conv_num(st,data);
  4809. iden = script_getnum(st,3);
  4810. ref = script_getnum(st,4);
  4811. attr = script_getnum(st,5);
  4812. c1 = (short)script_getnum(st,6);
  4813. c2 = (short)script_getnum(st,7);
  4814. c3 = (short)script_getnum(st,8);
  4815. c4 = (short)script_getnum(st,9);
  4816. if (nameid < 500) {
  4817. if(battle_config.error_log) ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4818. script_pushint(st,0);
  4819. return 1;
  4820. }
  4821. for(i = 0; i < MAX_INVENTORY; i++)
  4822. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4823. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4824. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4825. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4826. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4827. sd->status.inventory[i].card[3] == c4
  4828. )
  4829. count += sd->status.inventory[i].amount;
  4830. script_pushint(st,count);
  4831. return 0;
  4832. }
  4833. /*==========================================
  4834. * 重量チェック
  4835. *------------------------------------------*/
  4836. BUILDIN_FUNC(checkweight)
  4837. {
  4838. int nameid=0,amount,i;
  4839. unsigned long weight;
  4840. TBL_PC *sd;
  4841. struct script_data *data;
  4842. sd = script_rid2sd(st);
  4843. data=script_getdata(st,2);
  4844. get_val(st,data);
  4845. if( data_isstring(data) ){
  4846. const char *name=conv_str(st,data);
  4847. struct item_data *item_data = itemdb_searchname(name);
  4848. if( item_data )
  4849. nameid=item_data->nameid;
  4850. }else
  4851. nameid=conv_num(st,data);
  4852. amount=script_getnum(st,3);
  4853. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4854. script_pushint(st,0);
  4855. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4856. return 1;
  4857. }
  4858. weight = itemdb_weight(nameid)*amount;
  4859. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  4860. script_pushint(st,0);
  4861. } else {
  4862. //Check if the inventory ain't full.
  4863. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  4864. i = pc_search_inventory(sd,0);
  4865. if (i >= 0) //Empty slot available.
  4866. script_pushint(st,1);
  4867. else //Inventory full
  4868. script_pushint(st,0);
  4869. }
  4870. return 0;
  4871. }
  4872. /*==========================================
  4873. * getitem <item id>,<amount>{,<character ID>};
  4874. * getitem "<item name>",<amount>{,<character ID>};
  4875. *------------------------------------------*/
  4876. BUILDIN_FUNC(getitem)
  4877. {
  4878. int nameid,amount,flag = 0;
  4879. struct item it;
  4880. TBL_PC *sd;
  4881. struct script_data *data;
  4882. data=script_getdata(st,2);
  4883. get_val(st,data);
  4884. if( data_isstring(data) )
  4885. {// "<item name>"
  4886. const char *name=conv_str(st,data);
  4887. struct item_data *item_data = itemdb_searchname(name);
  4888. if( item_data == NULL ){
  4889. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4890. return 1; //No item created.
  4891. }
  4892. nameid=item_data->nameid;
  4893. } else if( data_isint(data) )
  4894. {// <item id>
  4895. nameid=conv_num(st,data);
  4896. //Violet Box, Blue Box, etc - random item pick
  4897. if( nameid < 0 ) {
  4898. nameid=itemdb_searchrandomid(-nameid);
  4899. flag = 1;
  4900. }
  4901. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4902. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4903. return 1; //No item created.
  4904. }
  4905. } else {
  4906. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4907. return 1;
  4908. }
  4909. // <amount>
  4910. if( (amount=script_getnum(st,3)) <= 0)
  4911. return 0; //return if amount <=0, skip the useles iteration
  4912. memset(&it,0,sizeof(it));
  4913. it.nameid=nameid;
  4914. if(!flag)
  4915. it.identify=1;
  4916. else
  4917. it.identify=itemdb_isidentified(nameid);
  4918. if( script_hasdata(st,4) )
  4919. {// <character ID>
  4920. sd=map_id2sd(script_getnum(st,4));
  4921. } else
  4922. {// attached player
  4923. sd=script_rid2sd(st);
  4924. }
  4925. if( sd == NULL ) // no target
  4926. return 0;
  4927. if( pet_create_egg(sd, nameid) )
  4928. amount = 1; //This is a pet!
  4929. else if( (flag=pc_additem(sd,&it,amount)) ){
  4930. clif_additem(sd,0,0,flag);
  4931. if( pc_candrop(sd,&it) )
  4932. map_addflooritem(&it,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4933. }
  4934. //Logs items, got from (N)PC scripts [Lupus]
  4935. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4936. log_pick_pc(sd, "N", nameid, amount, NULL);
  4937. return 0;
  4938. }
  4939. /*==========================================
  4940. *
  4941. *------------------------------------------*/
  4942. BUILDIN_FUNC(getitem2)
  4943. {
  4944. int nameid,amount,flag = 0;
  4945. int iden,ref,attr,c1,c2,c3,c4;
  4946. struct item_data *item_data;
  4947. struct item item_tmp;
  4948. TBL_PC *sd;
  4949. struct script_data *data;
  4950. sd = script_rid2sd(st);
  4951. data=script_getdata(st,2);
  4952. get_val(st,data);
  4953. if( data_isstring(data) ){
  4954. const char *name=conv_str(st,data);
  4955. struct item_data *item_data = itemdb_searchname(name);
  4956. if( item_data )
  4957. nameid=item_data->nameid;
  4958. else
  4959. nameid=UNKNOWN_ITEM_ID;
  4960. }else
  4961. nameid=conv_num(st,data);
  4962. amount=script_getnum(st,3);
  4963. iden=script_getnum(st,4);
  4964. ref=script_getnum(st,5);
  4965. attr=script_getnum(st,6);
  4966. c1=script_getnum(st,7);
  4967. c2=script_getnum(st,8);
  4968. c3=script_getnum(st,9);
  4969. c4=script_getnum(st,10);
  4970. if( script_hasdata(st,11) ) //アイテムを指定したIDに渡す
  4971. sd=map_id2sd(script_getnum(st,11));
  4972. if(sd == NULL) //アイテムを渡す相手がいなかったらお帰り
  4973. return 0;
  4974. if(nameid<0) { // ランダム
  4975. nameid=itemdb_searchrandomid(-nameid);
  4976. flag = 1;
  4977. }
  4978. if(nameid > 0) {
  4979. memset(&item_tmp,0,sizeof(item_tmp));
  4980. item_data=itemdb_exists(nameid);
  4981. if (item_data == NULL)
  4982. return -1;
  4983. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4984. if(ref > 10) ref = 10;
  4985. }
  4986. else if(item_data->type==IT_PETEGG) {
  4987. iden = 1;
  4988. ref = 0;
  4989. }
  4990. else {
  4991. iden = 1;
  4992. ref = attr = 0;
  4993. }
  4994. item_tmp.nameid=nameid;
  4995. if(!flag)
  4996. item_tmp.identify=iden;
  4997. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4998. item_tmp.identify=0;
  4999. item_tmp.refine=ref;
  5000. item_tmp.attribute=attr;
  5001. item_tmp.card[0]=c1;
  5002. item_tmp.card[1]=c2;
  5003. item_tmp.card[2]=c3;
  5004. item_tmp.card[3]=c4;
  5005. if((flag = pc_additem(sd,&item_tmp,amount))) {
  5006. clif_additem(sd,0,0,flag);
  5007. map_addflooritem(&item_tmp,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5008. }
  5009. //Logs items, got from (N)PC scripts [Lupus]
  5010. if(log_config.enable_logs&0x40)
  5011. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  5012. }
  5013. return 0;
  5014. }
  5015. /*==========================================
  5016. * gets an item with someone's name inscribed [Skotlex]
  5017. * getinscribeditem item_num, character_name
  5018. * Returned Qty is always 1, only works on equip-able
  5019. * equipment
  5020. *------------------------------------------*/
  5021. BUILDIN_FUNC(getnameditem)
  5022. {
  5023. int nameid;
  5024. struct item item_tmp;
  5025. TBL_PC *sd, *tsd;
  5026. struct script_data *data;
  5027. sd = script_rid2sd(st);
  5028. if (sd == NULL)
  5029. { //Player not attached!
  5030. script_pushint(st,0);
  5031. return 0;
  5032. }
  5033. data=script_getdata(st,2);
  5034. get_val(st,data);
  5035. if( data_isstring(data) ){
  5036. const char *name=conv_str(st,data);
  5037. struct item_data *item_data = itemdb_searchname(name);
  5038. if( item_data == NULL)
  5039. { //Failed
  5040. script_pushint(st,0);
  5041. return 0;
  5042. }
  5043. nameid = item_data->nameid;
  5044. }else
  5045. nameid = conv_num(st,data);
  5046. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5047. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5048. script_pushint(st,0);
  5049. return 0;
  5050. }
  5051. data=script_getdata(st,3);
  5052. get_val(st,data);
  5053. if( data_isstring(data) ) //Char Name
  5054. tsd=map_nick2sd(conv_str(st,data));
  5055. else //Char Id was given
  5056. tsd=map_charid2sd(conv_num(st,data));
  5057. if( tsd == NULL )
  5058. { //Failed
  5059. script_pushint(st,0);
  5060. return 0;
  5061. }
  5062. memset(&item_tmp,0,sizeof(item_tmp));
  5063. item_tmp.nameid=nameid;
  5064. item_tmp.amount=1;
  5065. item_tmp.identify=1;
  5066. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5067. item_tmp.card[2]=tsd->status.char_id;
  5068. item_tmp.card[3]=tsd->status.char_id >> 16;
  5069. if(pc_additem(sd,&item_tmp,1)) {
  5070. script_pushint(st,0);
  5071. return 0; //Failed to add item, we will not drop if they don't fit
  5072. }
  5073. //Logs items, got from (N)PC scripts [Lupus]
  5074. if(log_config.enable_logs&0x40)
  5075. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5076. script_pushint(st,1);
  5077. return 0;
  5078. }
  5079. /*==========================================
  5080. * gets a random item ID from an item group [Skotlex]
  5081. * groupranditem group_num
  5082. *------------------------------------------*/
  5083. BUILDIN_FUNC(grouprandomitem)
  5084. {
  5085. int group;
  5086. group = script_getnum(st,2);
  5087. script_pushint(st,itemdb_searchrandomid(group));
  5088. return 0;
  5089. }
  5090. /*==========================================
  5091. *
  5092. *------------------------------------------*/
  5093. BUILDIN_FUNC(makeitem)
  5094. {
  5095. int nameid,amount,flag = 0;
  5096. int x,y,m;
  5097. const char *mapname;
  5098. struct item item_tmp;
  5099. struct script_data *data;
  5100. data=script_getdata(st,2);
  5101. get_val(st,data);
  5102. if( data_isstring(data) ){
  5103. const char *name=conv_str(st,data);
  5104. struct item_data *item_data = itemdb_searchname(name);
  5105. if( item_data )
  5106. nameid=item_data->nameid;
  5107. else
  5108. nameid=UNKNOWN_ITEM_ID;
  5109. }else
  5110. nameid=conv_num(st,data);
  5111. amount=script_getnum(st,3);
  5112. mapname =script_getstr(st,4);
  5113. x =script_getnum(st,5);
  5114. y =script_getnum(st,6);
  5115. if(strcmp(mapname,"this")==0)
  5116. {
  5117. TBL_PC *sd;
  5118. sd = script_rid2sd(st);
  5119. if (!sd) return 0; //Failed...
  5120. m=sd->bl.m;
  5121. } else
  5122. m=map_mapname2mapid(mapname);
  5123. if(nameid<0) { // ランダム
  5124. nameid=itemdb_searchrandomid(-nameid);
  5125. flag = 1;
  5126. }
  5127. if(nameid > 0) {
  5128. memset(&item_tmp,0,sizeof(item_tmp));
  5129. item_tmp.nameid=nameid;
  5130. if(!flag)
  5131. item_tmp.identify=1;
  5132. else
  5133. item_tmp.identify=itemdb_isidentified(nameid);
  5134. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5135. }
  5136. return 0;
  5137. }
  5138. /*==========================================
  5139. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  5140. *------------------------------------------*/
  5141. BUILDIN_FUNC(delitem)
  5142. {
  5143. int nameid=0,amount,i,important_item=0;
  5144. TBL_PC *sd;
  5145. struct script_data *data;
  5146. sd = script_rid2sd(st);
  5147. data=script_getdata(st,2);
  5148. get_val(st,data);
  5149. if( data_isstring(data) ){
  5150. const char *name=conv_str(st,data);
  5151. struct item_data *item_data = itemdb_searchname(name);
  5152. //nameid=UNKNOWN_ITEM_ID;
  5153. if( item_data )
  5154. nameid=item_data->nameid;
  5155. }else
  5156. nameid=conv_num(st,data);
  5157. amount=script_getnum(st,3);
  5158. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5159. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5160. return 0;
  5161. }
  5162. //1st pass
  5163. //here we won't delete items with CARDS, named items but we count them
  5164. for(i=0;i<MAX_INVENTORY;i++){
  5165. //we don't delete wrong item or equipped item
  5166. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5167. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5168. continue;
  5169. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5170. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5171. sd->status.inventory[i].card[0] == CARD0_PET)
  5172. {
  5173. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5174. continue; //pet couldn't be sent for deletion.
  5175. } else
  5176. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5177. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5178. sd->status.inventory[i].card[0] ||
  5179. sd->status.inventory[i].refine) {
  5180. //this is important item, count it (except for pet eggs)
  5181. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5182. important_item++;
  5183. continue;
  5184. }
  5185. if(sd->status.inventory[i].amount>=amount){
  5186. //Logs items, got from (N)PC scripts [Lupus]
  5187. if(log_config.enable_logs&0x40)
  5188. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5189. pc_delitem(sd,i,amount,0);
  5190. return 0; //we deleted exact amount of items. now exit
  5191. } else {
  5192. amount-=sd->status.inventory[i].amount;
  5193. //Logs items, got from (N)PC scripts [Lupus]
  5194. if(log_config.enable_logs&0x40) {
  5195. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5196. }
  5197. //Logs
  5198. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5199. }
  5200. }
  5201. //2nd pass
  5202. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5203. if (important_item>0 && amount>0)
  5204. for(i=0;i<MAX_INVENTORY;i++){
  5205. //we don't delete wrong item
  5206. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5207. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5208. continue;
  5209. if(sd->status.inventory[i].amount>=amount){
  5210. //Logs items, got from (N)PC scripts [Lupus]
  5211. if(log_config.enable_logs&0x40)
  5212. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5213. pc_delitem(sd,i,amount,0);
  5214. return 0; //we deleted exact amount of items. now exit
  5215. } else {
  5216. amount-=sd->status.inventory[i].amount;
  5217. //Logs items, got from (N)PC scripts [Lupus]
  5218. if(log_config.enable_logs&0x40)
  5219. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5220. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5221. }
  5222. }
  5223. return 0;
  5224. }
  5225. /*==========================================
  5226. * advanced version of delitem [modified by Mihilion]
  5227. *------------------------------------------*/
  5228. BUILDIN_FUNC(delitem2)
  5229. {
  5230. int nameid=0,amount,i=0;
  5231. int iden,ref,attr,c1,c2,c3,c4;
  5232. TBL_PC *sd;
  5233. struct script_data *data;
  5234. sd = script_rid2sd(st);
  5235. data=script_getdata(st,2);
  5236. get_val(st,data);
  5237. if( data_isstring(data) ){
  5238. const char *name=conv_str(st,data);
  5239. struct item_data *item_data = itemdb_searchname(name);
  5240. //nameid=UNKNOWN_ITEM_ID;
  5241. if( item_data )
  5242. nameid=item_data->nameid;
  5243. }else
  5244. nameid=conv_num(st,data);
  5245. amount=script_getnum(st,3);
  5246. iden=script_getnum(st,4);
  5247. ref=script_getnum(st,5);
  5248. attr=script_getnum(st,6);
  5249. c1=script_getnum(st,7);
  5250. c2=script_getnum(st,8);
  5251. c3=script_getnum(st,9);
  5252. c4=script_getnum(st,10);
  5253. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5254. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5255. return 0;
  5256. }
  5257. for(i=0;i<MAX_INVENTORY;i++){
  5258. //we don't delete wrong item or equipped item
  5259. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5260. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5261. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5262. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5263. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5264. sd->status.inventory[i].card[3]!=c4)
  5265. continue;
  5266. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5267. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5268. {
  5269. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5270. continue; //Failed to send delete the pet.
  5271. }
  5272. if(sd->status.inventory[i].amount>=amount){
  5273. //Logs items, got from (N)PC scripts [Lupus]
  5274. if(log_config.enable_logs&0x40)
  5275. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5276. pc_delitem(sd,i,amount,0);
  5277. return 0; //we deleted exact amount of items. now exit
  5278. } else {
  5279. amount-=sd->status.inventory[i].amount;
  5280. //Logs items, got from (N)PC scripts [Lupus]
  5281. if(log_config.enable_logs&0x40)
  5282. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5283. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5284. }
  5285. }
  5286. return 0;
  5287. }
  5288. /*==========================================
  5289. * Enables/Disables use of items while in an NPC [Skotlex]
  5290. *------------------------------------------*/
  5291. BUILDIN_FUNC(enableitemuse)
  5292. {
  5293. TBL_PC *sd;
  5294. sd=script_rid2sd(st);
  5295. if (sd)
  5296. sd->npc_item_flag = st->oid;
  5297. return 0;
  5298. }
  5299. BUILDIN_FUNC(disableitemuse)
  5300. {
  5301. TBL_PC *sd;
  5302. sd=script_rid2sd(st);
  5303. if (sd)
  5304. sd->npc_item_flag = 0;
  5305. return 0;
  5306. }
  5307. /*==========================================
  5308. *キャラ関係のパラメータ取得
  5309. *------------------------------------------*/
  5310. BUILDIN_FUNC(readparam)
  5311. {
  5312. int type;
  5313. TBL_PC *sd;
  5314. type=script_getnum(st,2);
  5315. if( script_hasdata(st,3) )
  5316. sd=map_nick2sd(script_getstr(st,3));
  5317. else
  5318. sd=script_rid2sd(st);
  5319. if(sd==NULL){
  5320. script_pushint(st,-1);
  5321. return 0;
  5322. }
  5323. script_pushint(st,pc_readparam(sd,type));
  5324. return 0;
  5325. }
  5326. /*==========================================
  5327. *キャラ関係のID取得
  5328. *------------------------------------------*/
  5329. BUILDIN_FUNC(getcharid)
  5330. {
  5331. int num;
  5332. TBL_PC *sd;
  5333. num = script_getnum(st,2);
  5334. if( script_hasdata(st,3) )
  5335. sd=map_nick2sd(script_getstr(st,3));
  5336. else
  5337. sd=script_rid2sd(st);
  5338. if(sd==NULL){
  5339. script_pushint(st,0); //return 0, according docs
  5340. return 0;
  5341. }
  5342. switch( num ) {
  5343. case 0: script_pushint(st,sd->status.char_id); break;
  5344. case 1: script_pushint(st,sd->status.party_id); break;
  5345. case 2: script_pushint(st,sd->status.guild_id); break;
  5346. case 3: script_pushint(st,sd->status.account_id); break;
  5347. default:
  5348. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5349. script_pushint(st,0);
  5350. break;
  5351. }
  5352. return 0;
  5353. }
  5354. /*==========================================
  5355. *指定IDのPT名取得
  5356. *------------------------------------------*/
  5357. char *buildin_getpartyname_sub(int party_id)
  5358. {
  5359. struct party_data *p;
  5360. p=party_search(party_id);
  5361. if(p!=NULL){
  5362. char *buf;
  5363. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5364. memcpy(buf, p->party.name, NAME_LENGTH);
  5365. buf[NAME_LENGTH-1] = '\0';
  5366. return buf;
  5367. }
  5368. return 0;
  5369. }
  5370. BUILDIN_FUNC(getpartyname)
  5371. {
  5372. char *name;
  5373. int party_id;
  5374. party_id=script_getnum(st,2);
  5375. name=buildin_getpartyname_sub(party_id);
  5376. if(name != NULL)
  5377. script_pushstr(st,name);
  5378. else
  5379. script_pushconststr(st,"null");
  5380. return 0;
  5381. }
  5382. /*==========================================
  5383. *指定IDのPT人数とメンバーID取得
  5384. *------------------------------------------*/
  5385. BUILDIN_FUNC(getpartymember)
  5386. {
  5387. struct party_data *p;
  5388. int i,j=0,type=0;
  5389. p=party_search(script_getnum(st,2));
  5390. if( script_hasdata(st,3) )
  5391. type=script_getnum(st,3);
  5392. if(p!=NULL){
  5393. for(i=0;i<MAX_PARTY;i++){
  5394. if(p->party.member[i].account_id){
  5395. switch (type) {
  5396. case 2:
  5397. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5398. break;
  5399. case 1:
  5400. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5401. break;
  5402. default:
  5403. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5404. }
  5405. j++;
  5406. }
  5407. }
  5408. }
  5409. mapreg_setreg(add_str("$@partymembercount"),j);
  5410. return 0;
  5411. }
  5412. /*==========================================
  5413. * Retrieves party leader. if flag is specified,
  5414. * return some of the leader data. Otherwise, return name.
  5415. *------------------------------------------*/
  5416. BUILDIN_FUNC(getpartyleader)
  5417. {
  5418. int party_id, type = 0, i=0;
  5419. struct party_data *p;
  5420. party_id=script_getnum(st,2);
  5421. if( script_hasdata(st,3) )
  5422. type=script_getnum(st,3);
  5423. p=party_search(party_id);
  5424. if (p) //Search leader
  5425. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5426. if (!p || i == MAX_PARTY) { //leader not found
  5427. if (type)
  5428. script_pushint(st,-1);
  5429. else
  5430. script_pushconststr(st,"null");
  5431. return 0;
  5432. }
  5433. switch (type) {
  5434. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5435. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5436. case 3: script_pushint(st,p->party.member[i].class_); break;
  5437. case 4: script_pushstr(st,aStrdup(mapindex_id2name(p->party.member[i].map))); break;
  5438. case 5: script_pushint(st,p->party.member[i].lv); break;
  5439. default: script_pushstr(st,aStrdup(p->party.member[i].name)); break;
  5440. }
  5441. return 0;
  5442. }
  5443. /*==========================================
  5444. *指定IDのギルド名取得
  5445. *------------------------------------------*/
  5446. char *buildin_getguildname_sub(int guild_id)
  5447. {
  5448. struct guild *g=NULL;
  5449. g=guild_search(guild_id);
  5450. if(g!=NULL){
  5451. char *buf;
  5452. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5453. memcpy(buf, g->name, NAME_LENGTH);
  5454. buf[NAME_LENGTH-1] = '\0';
  5455. return buf;
  5456. }
  5457. return NULL;
  5458. }
  5459. BUILDIN_FUNC(getguildname)
  5460. {
  5461. char *name;
  5462. int guild_id=script_getnum(st,2);
  5463. name=buildin_getguildname_sub(guild_id);
  5464. if(name != NULL)
  5465. script_pushstr(st,name);
  5466. else
  5467. script_pushconststr(st,"null");
  5468. return 0;
  5469. }
  5470. /*==========================================
  5471. *指定IDのGuildMaster名取得
  5472. *------------------------------------------*/
  5473. char *buildin_getguildmaster_sub(int guild_id)
  5474. {
  5475. struct guild *g=NULL;
  5476. g=guild_search(guild_id);
  5477. if(g!=NULL){
  5478. char *buf;
  5479. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5480. memcpy(buf, g->master, NAME_LENGTH);
  5481. buf[NAME_LENGTH-1] = '\0';
  5482. return buf;
  5483. }
  5484. return 0;
  5485. }
  5486. BUILDIN_FUNC(getguildmaster)
  5487. {
  5488. char *master;
  5489. int guild_id=script_getnum(st,2);
  5490. master=buildin_getguildmaster_sub(guild_id);
  5491. if(master!=0)
  5492. script_pushstr(st,master);
  5493. else
  5494. script_pushconststr(st,"null");
  5495. return 0;
  5496. }
  5497. BUILDIN_FUNC(getguildmasterid)
  5498. {
  5499. char *master;
  5500. TBL_PC *sd=NULL;
  5501. int guild_id=script_getnum(st,2);
  5502. master=buildin_getguildmaster_sub(guild_id);
  5503. if(master!=0){
  5504. if((sd=map_nick2sd(master)) == NULL){
  5505. script_pushint(st,0);
  5506. return 0;
  5507. }
  5508. script_pushint(st,sd->status.char_id);
  5509. }else{
  5510. script_pushint(st,0);
  5511. }
  5512. return 0;
  5513. }
  5514. /*==========================================
  5515. * キャラクタの名前
  5516. *------------------------------------------*/
  5517. BUILDIN_FUNC(strcharinfo)
  5518. {
  5519. TBL_PC *sd;
  5520. int num;
  5521. char *buf;
  5522. sd=script_rid2sd(st);
  5523. if (!sd) { //Avoid crashing....
  5524. script_pushconststr(st,"");
  5525. return 0;
  5526. }
  5527. num=script_getnum(st,2);
  5528. switch(num){
  5529. case 0:
  5530. script_pushstr(st,aStrdup(sd->status.name));
  5531. break;
  5532. case 1:
  5533. buf=buildin_getpartyname_sub(sd->status.party_id);
  5534. if(buf!=0)
  5535. script_pushstr(st,buf);
  5536. else
  5537. script_pushconststr(st,"");
  5538. break;
  5539. case 2:
  5540. buf=buildin_getguildname_sub(sd->status.guild_id);
  5541. if(buf != NULL)
  5542. script_pushstr(st,buf);
  5543. else
  5544. script_pushconststr(st,"");
  5545. break;
  5546. default:
  5547. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5548. script_pushconststr(st,"");
  5549. break;
  5550. }
  5551. return 0;
  5552. }
  5553. /*==========================================
  5554. * 呼び出し元のNPC情報を取得する
  5555. *------------------------------------------*/
  5556. BUILDIN_FUNC(strnpcinfo)
  5557. {
  5558. TBL_NPC* nd;
  5559. int num;
  5560. char *buf,*name=NULL;
  5561. nd = map_id2nd(st->oid);
  5562. if (!nd) {
  5563. script_pushconststr(st, "");
  5564. return 0;
  5565. }
  5566. num = script_getnum(st,2);
  5567. switch(num){
  5568. case 0: // display name
  5569. name = aStrdup(nd->name);
  5570. break;
  5571. case 1: // visible part of display name name
  5572. if((buf = strchr(nd->name,'#')) != NULL)
  5573. {
  5574. name = aStrdup(nd->name);
  5575. name[buf - nd->name] = 0;
  5576. }
  5577. break;
  5578. case 2: // # fragment
  5579. if((buf = strchr(nd->name,'#')) != NULL)
  5580. name = aStrdup(buf+1);
  5581. break;
  5582. case 3: // unique name
  5583. name = aStrdup(nd->exname);
  5584. break;
  5585. }
  5586. if(name)
  5587. script_pushstr(st, name);
  5588. else
  5589. script_pushconststr(st, "");
  5590. return 0;
  5591. }
  5592. unsigned int equip[10]={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};
  5593. /*==========================================
  5594. * GetEquipID(Pos); Pos: 1-10
  5595. *------------------------------------------*/
  5596. BUILDIN_FUNC(getequipid)
  5597. {
  5598. int i=-1,num;
  5599. TBL_PC *sd;
  5600. struct item_data* item;
  5601. sd=script_rid2sd(st);
  5602. if(sd == NULL)
  5603. {
  5604. ShowError("getequipid: sd == NULL\n");
  5605. return 0;
  5606. }
  5607. num=script_getnum(st,2);
  5608. if (num > 0 && num <= ARRAYLENGTH(equip))
  5609. i=pc_checkequip(sd,equip[num-1]);
  5610. if(i >= 0){
  5611. item=sd->inventory_data[i];
  5612. if(item)
  5613. script_pushint(st,item->nameid);
  5614. else
  5615. script_pushint(st,0);
  5616. }else{
  5617. script_pushint(st,-1);
  5618. }
  5619. return 0;
  5620. }
  5621. /*==========================================
  5622. * 装備名文字列(精錬メニュー用)
  5623. *------------------------------------------*/
  5624. BUILDIN_FUNC(getequipname)
  5625. {
  5626. int i=-1,num;
  5627. TBL_PC *sd;
  5628. struct item_data* item;
  5629. char *buf;
  5630. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  5631. buf=(char *)aMallocA(64*sizeof(char));
  5632. sd=script_rid2sd(st);
  5633. num=script_getnum(st,2);
  5634. if (num > 0 && num <= ARRAYLENGTH(equip))
  5635. i=pc_checkequip(sd,equip[num-1]);
  5636. if(i >= 0){
  5637. item=sd->inventory_data[i];
  5638. if(item)
  5639. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  5640. else
  5641. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5642. }else{
  5643. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5644. }
  5645. script_pushstr(st,buf);
  5646. return 0;
  5647. }
  5648. /*==========================================
  5649. * getbrokenid [Valaris]
  5650. *------------------------------------------*/
  5651. BUILDIN_FUNC(getbrokenid)
  5652. {
  5653. int i,num,id=0,brokencounter=0;
  5654. TBL_PC *sd;
  5655. sd=script_rid2sd(st);
  5656. num=script_getnum(st,2);
  5657. for(i=0; i<MAX_INVENTORY; i++) {
  5658. if(sd->status.inventory[i].attribute==1){
  5659. brokencounter++;
  5660. if(num==brokencounter){
  5661. id=sd->status.inventory[i].nameid;
  5662. break;
  5663. }
  5664. }
  5665. }
  5666. script_pushint(st,id);
  5667. return 0;
  5668. }
  5669. /*==========================================
  5670. * repair [Valaris]
  5671. *------------------------------------------*/
  5672. BUILDIN_FUNC(repair)
  5673. {
  5674. int i,num;
  5675. int repaircounter=0;
  5676. TBL_PC *sd;
  5677. sd=script_rid2sd(st);
  5678. num=script_getnum(st,2);
  5679. for(i=0; i<MAX_INVENTORY; i++) {
  5680. if(sd->status.inventory[i].attribute==1){
  5681. repaircounter++;
  5682. if(num==repaircounter){
  5683. sd->status.inventory[i].attribute=0;
  5684. clif_equiplist(sd);
  5685. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5686. clif_misceffect(&sd->bl, 3);
  5687. break;
  5688. }
  5689. }
  5690. }
  5691. return 0;
  5692. }
  5693. /*==========================================
  5694. * 装備チェック
  5695. *------------------------------------------*/
  5696. BUILDIN_FUNC(getequipisequiped)
  5697. {
  5698. int i=-1,num;
  5699. TBL_PC *sd;
  5700. num=script_getnum(st,2);
  5701. sd=script_rid2sd(st);
  5702. if (num > 0 && num <= ARRAYLENGTH(equip))
  5703. i=pc_checkequip(sd,equip[num-1]);
  5704. if(i >= 0)
  5705. script_pushint(st,1);
  5706. else
  5707. script_pushint(st,0);
  5708. return 0;
  5709. }
  5710. /*==========================================
  5711. * 装備品精錬可能チェック
  5712. *------------------------------------------*/
  5713. BUILDIN_FUNC(getequipisenableref)
  5714. {
  5715. int i=-1,num;
  5716. TBL_PC *sd;
  5717. num=script_getnum(st,2);
  5718. sd=script_rid2sd(st);
  5719. if (num > 0 && num <= ARRAYLENGTH(equip))
  5720. i=pc_checkequip(sd,equip[num-1]);
  5721. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5722. {
  5723. script_pushint(st,1);
  5724. } else {
  5725. script_pushint(st,0);
  5726. }
  5727. return 0;
  5728. }
  5729. /*==========================================
  5730. * 装備品鑑定チェック
  5731. *------------------------------------------*/
  5732. BUILDIN_FUNC(getequipisidentify)
  5733. {
  5734. int i=-1,num;
  5735. TBL_PC *sd;
  5736. num=script_getnum(st,2);
  5737. sd=script_rid2sd(st);
  5738. if (num > 0 && num <= ARRAYLENGTH(equip))
  5739. i=pc_checkequip(sd,equip[num-1]);
  5740. if(i >= 0)
  5741. script_pushint(st,sd->status.inventory[i].identify);
  5742. else
  5743. script_pushint(st,0);
  5744. return 0;
  5745. }
  5746. /*==========================================
  5747. * 装備品精錬度
  5748. *------------------------------------------*/
  5749. BUILDIN_FUNC(getequiprefinerycnt)
  5750. {
  5751. int i=-1,num;
  5752. TBL_PC *sd;
  5753. num=script_getnum(st,2);
  5754. sd=script_rid2sd(st);
  5755. if (num > 0 && num <= ARRAYLENGTH(equip))
  5756. i=pc_checkequip(sd,equip[num-1]);
  5757. if(i >= 0)
  5758. script_pushint(st,sd->status.inventory[i].refine);
  5759. else
  5760. script_pushint(st,0);
  5761. return 0;
  5762. }
  5763. /*==========================================
  5764. * 装備品武器LV
  5765. *------------------------------------------*/
  5766. BUILDIN_FUNC(getequipweaponlv)
  5767. {
  5768. int i=-1,num;
  5769. TBL_PC *sd;
  5770. num=script_getnum(st,2);
  5771. sd=script_rid2sd(st);
  5772. if (num > 0 && num <= ARRAYLENGTH(equip))
  5773. i=pc_checkequip(sd,equip[num-1]);
  5774. if(i >= 0 && sd->inventory_data[i])
  5775. script_pushint(st,sd->inventory_data[i]->wlv);
  5776. else
  5777. script_pushint(st,0);
  5778. return 0;
  5779. }
  5780. /*==========================================
  5781. * 装備品精錬成功率
  5782. *------------------------------------------*/
  5783. BUILDIN_FUNC(getequippercentrefinery)
  5784. {
  5785. int i=-1,num;
  5786. TBL_PC *sd;
  5787. num=script_getnum(st,2);
  5788. sd=script_rid2sd(st);
  5789. if (num > 0 && num <= ARRAYLENGTH(equip))
  5790. i=pc_checkequip(sd,equip[num-1]);
  5791. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5792. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5793. else
  5794. script_pushint(st,0);
  5795. return 0;
  5796. }
  5797. /*==========================================
  5798. * 精錬成功
  5799. *------------------------------------------*/
  5800. BUILDIN_FUNC(successrefitem)
  5801. {
  5802. int i=-1,num,ep;
  5803. TBL_PC *sd;
  5804. num=script_getnum(st,2);
  5805. sd=script_rid2sd(st);
  5806. if (num > 0 && num <= ARRAYLENGTH(equip))
  5807. i=pc_checkequip(sd,equip[num-1]);
  5808. if(i >= 0) {
  5809. ep=sd->status.inventory[i].equip;
  5810. //Logs items, got from (N)PC scripts [Lupus]
  5811. if(log_config.enable_logs&0x40)
  5812. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5813. sd->status.inventory[i].refine++;
  5814. pc_unequipitem(sd,i,2);
  5815. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5816. clif_delitem(sd,i,1);
  5817. //Logs items, got from (N)PC scripts [Lupus]
  5818. if(log_config.enable_logs&0x40)
  5819. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5820. clif_additem(sd,i,1,0);
  5821. pc_equipitem(sd,i,ep);
  5822. clif_misceffect(&sd->bl,3);
  5823. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5824. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5825. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5826. ){ // Fame point system [DracoRPG]
  5827. switch (sd->inventory_data[i]->wlv){
  5828. case 1:
  5829. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5830. break;
  5831. case 2:
  5832. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5833. break;
  5834. case 3:
  5835. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5836. break;
  5837. }
  5838. }
  5839. }
  5840. return 0;
  5841. }
  5842. /*==========================================
  5843. * 精錬失敗
  5844. *------------------------------------------*/
  5845. BUILDIN_FUNC(failedrefitem)
  5846. {
  5847. int i=-1,num;
  5848. TBL_PC *sd;
  5849. num=script_getnum(st,2);
  5850. sd=script_rid2sd(st);
  5851. if (num > 0 && num <= ARRAYLENGTH(equip))
  5852. i=pc_checkequip(sd,equip[num-1]);
  5853. if(i >= 0) {
  5854. //Logs items, got from (N)PC scripts [Lupus]
  5855. if(log_config.enable_logs&0x40)
  5856. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5857. sd->status.inventory[i].refine = 0;
  5858. pc_unequipitem(sd,i,3);
  5859. // 精錬失敗エフェクトのパケット
  5860. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5861. pc_delitem(sd,i,1,0);
  5862. // 他の人にも失敗を通知
  5863. clif_misceffect(&sd->bl,2);
  5864. }
  5865. return 0;
  5866. }
  5867. /*==========================================
  5868. *
  5869. *------------------------------------------*/
  5870. BUILDIN_FUNC(statusup)
  5871. {
  5872. int type;
  5873. TBL_PC *sd;
  5874. type=script_getnum(st,2);
  5875. sd=script_rid2sd(st);
  5876. pc_statusup(sd,type);
  5877. return 0;
  5878. }
  5879. /*==========================================
  5880. *
  5881. *------------------------------------------*/
  5882. BUILDIN_FUNC(statusup2)
  5883. {
  5884. int type,val;
  5885. TBL_PC *sd;
  5886. type=script_getnum(st,2);
  5887. val=script_getnum(st,3);
  5888. sd=script_rid2sd(st);
  5889. pc_statusup2(sd,type,val);
  5890. return 0;
  5891. }
  5892. /// See 'doc/item_bonus.txt'
  5893. ///
  5894. /// bonus <bonus type>,<val1>;
  5895. /// bonus2 <bonus type>,<val1>,<val2>;
  5896. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5897. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5898. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5899. BUILDIN_FUNC(bonus)
  5900. {
  5901. int type;
  5902. int type2;
  5903. int type3;
  5904. int type4;
  5905. int type5;
  5906. int val;
  5907. TBL_PC* sd;
  5908. sd = script_rid2sd(st);
  5909. if( sd == NULL )
  5910. return 1; // no player attached
  5911. type = script_getnum(st,2);
  5912. switch( script_lastdata(st) ){
  5913. case 3:
  5914. val = script_getnum(st,3);
  5915. pc_bonus(sd, type, val);
  5916. break;
  5917. case 4:
  5918. type2 = script_getnum(st,3);
  5919. val = script_getnum(st,4);
  5920. pc_bonus2(sd, type, type2, val);
  5921. break;
  5922. case 5:
  5923. type2 = script_getnum(st,3);
  5924. type3 = script_getnum(st,4);
  5925. val = script_getnum(st,5);
  5926. pc_bonus3(sd, type, type2, type3, val);
  5927. break;
  5928. case 6:
  5929. type2 = script_getnum(st,3);
  5930. type3 = script_getnum(st,4);
  5931. type4 = script_getnum(st,5);
  5932. val = script_getnum(st,6);
  5933. pc_bonus4(sd, type, type2, type3, type4, val);
  5934. break;
  5935. case 7:
  5936. type2 = script_getnum(st,3);
  5937. type3 = script_getnum(st,4);
  5938. type4 = script_getnum(st,5);
  5939. type5 = script_getnum(st,6);
  5940. val = script_getnum(st,7);
  5941. pc_bonus5(sd, type, type2, type3, type4, type5, val);
  5942. break;
  5943. default:
  5944. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 2));
  5945. }
  5946. return 0;
  5947. }
  5948. /// Bonus script that has a chance of being executed on attack.
  5949. BUILDIN_FUNC(bonusautoscript)
  5950. {
  5951. int rate, flag = 0;
  5952. const char *str;
  5953. struct script_code *script;
  5954. TBL_PC* sd;
  5955. sd = script_rid2sd(st);
  5956. if( sd == NULL )
  5957. return 1;// no player attached, report source
  5958. str = script_getstr(st,2);
  5959. rate = script_getnum(st,3);
  5960. if( script_hasdata(st,4) )
  5961. flag = script_getnum(st,4);
  5962. script = parse_script(str, "autoscript bonus", 0, 0);
  5963. if (!script)
  5964. return 1;
  5965. if (!pc_autoscript_add(sd->autoscript, ARRAYLENGTH(sd->autoscript), rate, flag, script))
  5966. {
  5967. script_free_code(script);
  5968. return 1;
  5969. }
  5970. return 0;
  5971. }
  5972. /// Bonus script that has a chance of being executed when attacked.
  5973. BUILDIN_FUNC(bonusautoscript2)
  5974. {
  5975. int rate, flag = 0;
  5976. const char *str;
  5977. struct script_code *script;
  5978. TBL_PC* sd;
  5979. sd = script_rid2sd(st);
  5980. if( sd == NULL )
  5981. return 1;// no player attached, report source
  5982. str = script_getstr(st,2);
  5983. rate = script_getnum(st,3);
  5984. if( script_hasdata(st,4) )
  5985. flag = script_getnum(st,4);
  5986. script = parse_script(str, "autoscript2 bonus", 0, 0);
  5987. if (!script)
  5988. return 1;
  5989. if (!pc_autoscript_add(sd->autoscript2, ARRAYLENGTH(sd->autoscript2), rate, flag, script))
  5990. {
  5991. script_free_code(script);
  5992. return 1;
  5993. }
  5994. return 0;
  5995. }
  5996. /// Changes the level of a player skill.
  5997. /// <flag> defaults to 1
  5998. /// <flag>=0 : set the level of the skill
  5999. /// <flag>=1 : set the temporary level of the skill
  6000. /// <flag>=2 : add to the level of the skill
  6001. ///
  6002. /// skill <skill id>,<level>,<flag>
  6003. /// skill <skill id>,<level>
  6004. BUILDIN_FUNC(skill)
  6005. {
  6006. int id;
  6007. int level;
  6008. int flag = 1;
  6009. TBL_PC* sd;
  6010. sd = script_rid2sd(st);
  6011. if( sd == NULL )
  6012. return 1;// no player attached, report source
  6013. id = script_getnum(st,2);
  6014. level = script_getnum(st,3);
  6015. if( script_hasdata(st,4) )
  6016. flag = script_getnum(st,4);
  6017. pc_skill(sd, id, level, flag);
  6018. return 0;
  6019. }
  6020. /// Changes the level of a player skill.
  6021. /// like skill, but <flag> defaults to 2
  6022. ///
  6023. /// addtoskill <skill id>,<amount>,<flag>
  6024. /// addtoskill <skill id>,<amount>
  6025. ///
  6026. /// @see skill
  6027. BUILDIN_FUNC(addtoskill)
  6028. {
  6029. int id;
  6030. int level;
  6031. int flag = 2;
  6032. TBL_PC* sd;
  6033. sd = script_rid2sd(st);
  6034. if( sd == NULL )
  6035. return 1;// no player attached, report source
  6036. id = script_getnum(st,2);
  6037. level = script_getnum(st,3);
  6038. if( script_hasdata(st,4) )
  6039. flag = script_getnum(st,4);
  6040. pc_skill(sd, id, level, flag);
  6041. return 0;
  6042. }
  6043. /// Increases the level of a guild skill.
  6044. ///
  6045. /// guildskill <skill id>,<amount>;
  6046. BUILDIN_FUNC(guildskill)
  6047. {
  6048. int id;
  6049. int level;
  6050. TBL_PC* sd;
  6051. int i;
  6052. sd = script_rid2sd(st);
  6053. if( sd == NULL )
  6054. return 1;// no player attached, report source
  6055. id = script_getnum(st,2);
  6056. level = script_getnum(st,3);
  6057. for( i=0; i < level; i++ )
  6058. guild_skillup(sd, id);
  6059. return 0;
  6060. }
  6061. /// Returns the level of the player skill.
  6062. ///
  6063. /// getskilllv(<skill id>) -> <level>
  6064. BUILDIN_FUNC(getskilllv)
  6065. {
  6066. int id;
  6067. TBL_PC* sd;
  6068. sd = script_rid2sd(st);
  6069. if( sd == NULL )
  6070. return 1;// no player attached, report source
  6071. id = script_getnum(st,2);
  6072. script_pushint(st, pc_checkskill(sd,id));
  6073. return 0;
  6074. }
  6075. /// Returns the level of the guild skill.
  6076. ///
  6077. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6078. BUILDIN_FUNC(getgdskilllv)
  6079. {
  6080. int guild_id;
  6081. int skill_id;
  6082. struct guild* g;
  6083. guild_id = script_getnum(st,2);
  6084. skill_id = script_getnum(st,3);
  6085. g = guild_search(guild_id);
  6086. if( g == NULL )
  6087. script_pushint(st, -1);
  6088. else
  6089. script_pushint(st, guild_checkskill(g,skill_id));
  6090. return 0;
  6091. }
  6092. /// Returns the 'basic_skill_check' setting.
  6093. /// This config determines if the server checks the skill level of NV_BASIC
  6094. /// before allowing the basic actions.
  6095. ///
  6096. /// basicskillcheck() -> <bool>
  6097. BUILDIN_FUNC(basicskillcheck)
  6098. {
  6099. script_pushint(st, battle_config.basic_skill_check);
  6100. return 0;
  6101. }
  6102. /// Returns the GM level of the player.
  6103. ///
  6104. /// getgmlevel() -> <level>
  6105. BUILDIN_FUNC(getgmlevel)
  6106. {
  6107. TBL_PC* sd;
  6108. sd = script_rid2sd(st);
  6109. if( sd == NULL )
  6110. return 1;// no player attached, report source
  6111. script_pushint(st, pc_isGM(sd));
  6112. return 0;
  6113. }
  6114. /// Terminates the execution of this script instance.
  6115. ///
  6116. /// end
  6117. BUILDIN_FUNC(end)
  6118. {
  6119. st->state = END;
  6120. return 0;
  6121. }
  6122. /// Checks if the player has that effect state (option).
  6123. ///
  6124. /// checkoption(<option>) -> <bool>
  6125. BUILDIN_FUNC(checkoption)
  6126. {
  6127. int option;
  6128. TBL_PC* sd;
  6129. sd = script_rid2sd(st);
  6130. if( sd == NULL )
  6131. return 1;// no player attached, report source
  6132. option = script_getnum(st,2);
  6133. if( sd->sc.option&option )
  6134. script_pushint(st, 1);
  6135. else
  6136. script_pushint(st, 0);
  6137. return 0;
  6138. }
  6139. /// Checks if the player is in that body state (opt1).
  6140. ///
  6141. /// checkoption1(<opt1>) -> <bool>
  6142. BUILDIN_FUNC(checkoption1)
  6143. {
  6144. int opt1;
  6145. TBL_PC* sd;
  6146. sd = script_rid2sd(st);
  6147. if( sd == NULL )
  6148. return 1;// no player attached, report source
  6149. opt1 = script_getnum(st,2);
  6150. if( sd->sc.opt1 == opt1 )
  6151. script_pushint(st, 1);
  6152. else
  6153. script_pushint(st, 0);
  6154. return 0;
  6155. }
  6156. /// Checks if the player has that health state (opt2).
  6157. ///
  6158. /// checkoption2(<opt2>) -> <bool>
  6159. BUILDIN_FUNC(checkoption2)
  6160. {
  6161. int opt2;
  6162. TBL_PC* sd;
  6163. sd = script_rid2sd(st);
  6164. if( sd == NULL )
  6165. return 1;// no player attached, report source
  6166. opt2 = script_getnum(st,2);
  6167. if( sd->sc.opt2&opt2 )
  6168. script_pushint(st, 1);
  6169. else
  6170. script_pushint(st, 0);
  6171. return 0;
  6172. }
  6173. /// Changes the effect state (option) of the player.
  6174. /// <flag> defaults to 1
  6175. /// <flag>=0 : removes the option
  6176. /// <flag>=other : adds the option
  6177. ///
  6178. /// setoption <option>,<flag>;
  6179. /// setoption <option>;
  6180. BUILDIN_FUNC(setoption)
  6181. {
  6182. int option;
  6183. int flag = 1;
  6184. TBL_PC* sd;
  6185. sd = script_rid2sd(st);
  6186. if( sd == NULL )
  6187. return 1;// no player attached, report source
  6188. option = script_getnum(st,2);
  6189. if( script_hasdata(st,3) )
  6190. flag = script_getnum(st,3);
  6191. else if( !option ){// Request to remove everything.
  6192. flag = 0;
  6193. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6194. }
  6195. if( flag ){// Add option
  6196. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6197. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6198. pc_setoption(sd, sd->sc.option|option);
  6199. } else// Remove option
  6200. pc_setoption(sd, sd->sc.option&~option);
  6201. return 0;
  6202. }
  6203. /// Returns if the player has a cart.
  6204. ///
  6205. /// checkcart() -> <bool>
  6206. ///
  6207. /// @author Valaris
  6208. BUILDIN_FUNC(checkcart)
  6209. {
  6210. TBL_PC* sd;
  6211. sd = script_rid2sd(st);
  6212. if( sd == NULL )
  6213. return 1;// no player attached, report source
  6214. if( pc_iscarton(sd) )
  6215. script_pushint(st, 1);
  6216. else
  6217. script_pushint(st, 0);
  6218. return 0;
  6219. }
  6220. /// Sets the cart of the player.
  6221. /// <type> defaults to 1
  6222. /// <type>=0 : removes the cart
  6223. /// <type>=1 : Normal cart
  6224. /// <type>=2 : Wooden cart
  6225. /// <type>=3 : Covered cart with flowers and ferns
  6226. /// <type>=4 : Wooden cart with a Panda doll on the back
  6227. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6228. ///
  6229. /// setcart <type>;
  6230. /// setcart;
  6231. BUILDIN_FUNC(setcart)
  6232. {
  6233. int type = 1;
  6234. TBL_PC* sd;
  6235. sd = script_rid2sd(st);
  6236. if( sd == NULL )
  6237. return 1;// no player attached, report source
  6238. if( script_hasdata(st,2) )
  6239. type = script_getnum(st,2);
  6240. pc_setcart(sd, type);
  6241. return 0;
  6242. }
  6243. /// Returns if the player has a falcon.
  6244. ///
  6245. /// checkfalcon() -> <bool>
  6246. ///
  6247. /// @author Valaris
  6248. BUILDIN_FUNC(checkfalcon)
  6249. {
  6250. TBL_PC* sd;
  6251. sd = script_rid2sd(st);
  6252. if( sd == NULL )
  6253. return 1;// no player attached, report source
  6254. if( pc_isfalcon(sd) )
  6255. script_pushint(st, 1);
  6256. else
  6257. script_pushint(st, 0);
  6258. return 0;
  6259. }
  6260. /// Sets if the player has a falcon or not.
  6261. /// <flag> defaults to 1
  6262. ///
  6263. /// setfalcon <flag>;
  6264. /// setfalcon;
  6265. BUILDIN_FUNC(setfalcon)
  6266. {
  6267. int flag = 1;
  6268. TBL_PC* sd;
  6269. sd = script_rid2sd(st);
  6270. if( sd == NULL )
  6271. return 1;// no player attached, report source
  6272. if( script_hasdata(st,2) )
  6273. flag = script_getnum(st,2);
  6274. pc_setfalcon(sd, flag);
  6275. return 0;
  6276. }
  6277. /// Returns if the player is riding.
  6278. ///
  6279. /// checkriding() -> <bool>
  6280. ///
  6281. /// @author Valaris
  6282. BUILDIN_FUNC(checkriding)
  6283. {
  6284. TBL_PC* sd;
  6285. sd = script_rid2sd(st);
  6286. if( sd == NULL )
  6287. return 1;// no player attached, report source
  6288. if( pc_isriding(sd) )
  6289. script_pushint(st, 1);
  6290. else
  6291. script_pushint(st, 0);
  6292. return 0;
  6293. }
  6294. /// Sets if the player is riding.
  6295. /// <flag> defaults to 1
  6296. ///
  6297. /// setriding <flag>;
  6298. /// setriding;
  6299. BUILDIN_FUNC(setriding)
  6300. {
  6301. int flag = 1;
  6302. TBL_PC* sd;
  6303. sd = script_rid2sd(st);
  6304. if( sd == NULL )
  6305. return 1;// no player attached, report source
  6306. if( script_hasdata(st,2) )
  6307. flag = script_getnum(st,2);
  6308. pc_setriding(sd, flag);
  6309. return 0;
  6310. }
  6311. /// Sets the save point of the player.
  6312. ///
  6313. /// save "<map name>",<x>,<y>
  6314. /// savepoint "<map name>",<x>,<y>
  6315. BUILDIN_FUNC(savepoint)
  6316. {
  6317. int x;
  6318. int y;
  6319. short map;
  6320. const char* str;
  6321. TBL_PC* sd;
  6322. sd = script_rid2sd(st);
  6323. if( sd == NULL )
  6324. return 1;// no player attached, report source
  6325. str = script_getstr(st, 2);
  6326. x = script_getnum(st,3);
  6327. y = script_getnum(st,4);
  6328. map = mapindex_name2id(str);
  6329. if( map )
  6330. pc_setsavepoint(sd, map, x, y);
  6331. return 0;
  6332. }
  6333. /*==========================================
  6334. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6335. *------------------------------------------*/
  6336. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6337. {
  6338. int type;
  6339. time_t timer;
  6340. struct tm *t;
  6341. type=script_getnum(st,2);
  6342. switch(type){
  6343. case 2:
  6344. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6345. // from the system clock.)
  6346. script_pushint(st,(int)time(NULL));
  6347. break;
  6348. case 1:
  6349. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6350. time(&timer);
  6351. t=localtime(&timer);
  6352. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6353. break;
  6354. case 0:
  6355. default:
  6356. //type 0:(System Ticks)
  6357. script_pushint(st,gettick());
  6358. break;
  6359. }
  6360. return 0;
  6361. }
  6362. /*==========================================
  6363. * GetTime(Type);
  6364. * 1: Sec 2: Min 3: Hour
  6365. * 4: WeekDay 5: MonthDay 6: Month
  6366. * 7: Year
  6367. *------------------------------------------*/
  6368. BUILDIN_FUNC(gettime) /* Asgard Version */
  6369. {
  6370. int type;
  6371. time_t timer;
  6372. struct tm *t;
  6373. type=script_getnum(st,2);
  6374. time(&timer);
  6375. t=localtime(&timer);
  6376. switch(type){
  6377. case 1://Sec(0~59)
  6378. script_pushint(st,t->tm_sec);
  6379. break;
  6380. case 2://Min(0~59)
  6381. script_pushint(st,t->tm_min);
  6382. break;
  6383. case 3://Hour(0~23)
  6384. script_pushint(st,t->tm_hour);
  6385. break;
  6386. case 4://WeekDay(0~6)
  6387. script_pushint(st,t->tm_wday);
  6388. break;
  6389. case 5://MonthDay(01~31)
  6390. script_pushint(st,t->tm_mday);
  6391. break;
  6392. case 6://Month(01~12)
  6393. script_pushint(st,t->tm_mon+1);
  6394. break;
  6395. case 7://Year(20xx)
  6396. script_pushint(st,t->tm_year+1900);
  6397. break;
  6398. case 8://Year Day(01~366)
  6399. script_pushint(st,t->tm_yday+1);
  6400. break;
  6401. default://(format error)
  6402. script_pushint(st,-1);
  6403. break;
  6404. }
  6405. return 0;
  6406. }
  6407. /*==========================================
  6408. * GetTimeStr("TimeFMT", Length);
  6409. *------------------------------------------*/
  6410. BUILDIN_FUNC(gettimestr)
  6411. {
  6412. char *tmpstr;
  6413. const char *fmtstr;
  6414. int maxlen;
  6415. time_t now = time(NULL);
  6416. fmtstr=script_getstr(st,2);
  6417. maxlen=script_getnum(st,3);
  6418. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6419. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6420. tmpstr[maxlen]='\0';
  6421. script_pushstr(st,tmpstr);
  6422. return 0;
  6423. }
  6424. /*==========================================
  6425. * カプラ倉庫を開く
  6426. *------------------------------------------*/
  6427. BUILDIN_FUNC(openstorage)
  6428. {
  6429. storage_storageopen(script_rid2sd(st));
  6430. return 0;
  6431. }
  6432. BUILDIN_FUNC(guildopenstorage)
  6433. {
  6434. TBL_PC *sd=script_rid2sd(st);
  6435. int ret;
  6436. ret = storage_guild_storageopen(sd);
  6437. script_pushint(st,ret);
  6438. return 0;
  6439. }
  6440. /*==========================================
  6441. * アイテムによるスキル発動
  6442. *------------------------------------------*/
  6443. BUILDIN_FUNC(itemskill)
  6444. {
  6445. int id,lv;
  6446. TBL_PC *sd=script_rid2sd(st);
  6447. id=script_getnum(st,2);
  6448. lv=script_getnum(st,3);
  6449. if(!sd || sd->ud.skilltimer != -1)
  6450. return 0;
  6451. sd->skillitem=id;
  6452. sd->skillitemlv=lv;
  6453. clif_item_skill(sd,id,lv);
  6454. return 0;
  6455. }
  6456. /*==========================================
  6457. * アイテム作成
  6458. *------------------------------------------*/
  6459. BUILDIN_FUNC(produce)
  6460. {
  6461. int trigger;
  6462. TBL_PC *sd=script_rid2sd(st);
  6463. trigger=script_getnum(st,2);
  6464. clif_skill_produce_mix_list(sd, trigger);
  6465. return 0;
  6466. }
  6467. /*==========================================
  6468. * NPCでペット作る
  6469. *------------------------------------------*/
  6470. BUILDIN_FUNC(makepet)
  6471. {
  6472. TBL_PC *sd = script_rid2sd(st);
  6473. int id,pet_id;
  6474. id=script_getnum(st,2);
  6475. pet_id = search_petDB_index(id, PET_CLASS);
  6476. if (pet_id < 0)
  6477. pet_id = search_petDB_index(id, PET_EGG);
  6478. if (pet_id >= 0 && sd) {
  6479. sd->catch_target_class = pet_db[pet_id].class_;
  6480. intif_create_pet(
  6481. sd->status.account_id, sd->status.char_id,
  6482. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6483. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6484. 100, 0, 1, pet_db[pet_id].jname);
  6485. }
  6486. return 0;
  6487. }
  6488. /*==========================================
  6489. * NPCで経験値上げる
  6490. *------------------------------------------*/
  6491. BUILDIN_FUNC(getexp)
  6492. {
  6493. TBL_PC *sd = script_rid2sd(st);
  6494. int base=0,job=0;
  6495. double bonus;
  6496. nullpo_retr(0, sd);
  6497. base=script_getnum(st,2);
  6498. job =script_getnum(st,3);
  6499. if(base<0 || job<0)
  6500. return 0;
  6501. // bonus for npc-given exp
  6502. bonus = battle_config.quest_exp_rate / 100.;
  6503. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6504. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6505. pc_gainexp(sd, NULL, base, job);
  6506. return 0;
  6507. }
  6508. /*==========================================
  6509. * Gain guild exp [Celest]
  6510. *------------------------------------------*/
  6511. BUILDIN_FUNC(guildgetexp)
  6512. {
  6513. TBL_PC *sd = script_rid2sd(st);
  6514. int exp;
  6515. exp = script_getnum(st,2);
  6516. if(exp < 0)
  6517. return 0;
  6518. if(sd && sd->status.guild_id > 0)
  6519. guild_getexp (sd, exp);
  6520. return 0;
  6521. }
  6522. /*==========================================
  6523. * Changes the guild master of a guild [Skotlex]
  6524. *------------------------------------------*/
  6525. BUILDIN_FUNC(guildchangegm)
  6526. {
  6527. TBL_PC *sd;
  6528. int guild_id;
  6529. const char *name;
  6530. guild_id = script_getnum(st,2);
  6531. name = script_getstr(st,3);
  6532. sd=map_nick2sd(name);
  6533. if (!sd)
  6534. script_pushint(st,0);
  6535. else
  6536. script_pushint(st,guild_gm_change(guild_id, sd));
  6537. return 0;
  6538. }
  6539. /*==========================================
  6540. * モンスター発生
  6541. *------------------------------------------*/
  6542. BUILDIN_FUNC(monster)
  6543. {
  6544. int class_,amount,x,y;
  6545. const char *str,*map,*event="";
  6546. map =script_getstr(st,2);
  6547. x =script_getnum(st,3);
  6548. y =script_getnum(st,4);
  6549. str =script_getstr(st,5);
  6550. class_=script_getnum(st,6);
  6551. amount=script_getnum(st,7);
  6552. if( script_hasdata(st,8) ){
  6553. event=script_getstr(st,8);
  6554. check_event(st, event);
  6555. }
  6556. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6557. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6558. return 1;
  6559. }
  6560. mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,amount,event);
  6561. return 0;
  6562. }
  6563. /*==========================================
  6564. * モンスター発生
  6565. *------------------------------------------*/
  6566. BUILDIN_FUNC(areamonster)
  6567. {
  6568. int class_,amount,x0,y0,x1,y1;
  6569. const char *str,*map,*event="";
  6570. map =script_getstr(st,2);
  6571. x0 =script_getnum(st,3);
  6572. y0 =script_getnum(st,4);
  6573. x1 =script_getnum(st,5);
  6574. y1 =script_getnum(st,6);
  6575. str =script_getstr(st,7);
  6576. class_=script_getnum(st,8);
  6577. amount=script_getnum(st,9);
  6578. if( script_hasdata(st,10) ){
  6579. event=script_getstr(st,10);
  6580. check_event(st, event);
  6581. }
  6582. mob_once_spawn_area(map_id2sd(st->rid),map,x0,y0,x1,y1,str,class_,amount,event);
  6583. return 0;
  6584. }
  6585. /*==========================================
  6586. * モンスター削除
  6587. *------------------------------------------*/
  6588. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6589. {
  6590. TBL_MOB* md = (TBL_MOB*)bl;
  6591. char *event=va_arg(ap,char *);
  6592. int allflag=va_arg(ap,int);
  6593. if(!allflag){
  6594. if(strcmp(event,md->npc_event)==0)
  6595. status_kill(bl);
  6596. }else{
  6597. if(!md->spawn)
  6598. status_kill(bl);
  6599. }
  6600. return 0;
  6601. }
  6602. BUILDIN_FUNC(killmonster)
  6603. {
  6604. const char *mapname,*event;
  6605. int m,allflag=0;
  6606. mapname=script_getstr(st,2);
  6607. event=script_getstr(st,3);
  6608. if(strcmp(event,"All")==0)
  6609. allflag = 1;
  6610. else
  6611. check_event(st, event);
  6612. if( (m=map_mapname2mapid(mapname))<0 )
  6613. return 0;
  6614. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6615. return 0;
  6616. }
  6617. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6618. {
  6619. status_kill(bl);
  6620. return 0;
  6621. }
  6622. BUILDIN_FUNC(killmonsterall)
  6623. {
  6624. const char *mapname;
  6625. int m;
  6626. mapname=script_getstr(st,2);
  6627. if( (m=map_mapname2mapid(mapname))<0 )
  6628. return 0;
  6629. map_foreachinmap(buildin_killmonsterall_sub,
  6630. m,BL_MOB);
  6631. return 0;
  6632. }
  6633. /*==========================================
  6634. * Creates a clone of a player.
  6635. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6636. *------------------------------------------*/
  6637. BUILDIN_FUNC(clone)
  6638. {
  6639. TBL_PC *sd, *msd=NULL;
  6640. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6641. unsigned int duration = 0;
  6642. const char *map,*event="";
  6643. map=script_getstr(st,2);
  6644. x=script_getnum(st,3);
  6645. y=script_getnum(st,4);
  6646. event=script_getstr(st,5);
  6647. char_id=script_getnum(st,6);
  6648. if( script_hasdata(st,7) )
  6649. master_id=script_getnum(st,7);
  6650. if( script_hasdata(st,8) )
  6651. mode=script_getnum(st,8);
  6652. if( script_hasdata(st,9) )
  6653. flag=script_getnum(st,9);
  6654. if( script_hasdata(st,10) )
  6655. duration=script_getnum(st,10);
  6656. check_event(st, event);
  6657. m = map_mapname2mapid(map);
  6658. if (m < 0) return 0;
  6659. sd = map_charid2sd(char_id);
  6660. if (master_id) {
  6661. msd = map_charid2sd(master_id);
  6662. if (msd)
  6663. master_id = msd->bl.id;
  6664. else
  6665. master_id = 0;
  6666. }
  6667. if (sd) //Return ID of newly crafted clone.
  6668. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6669. else //Failed to create clone.
  6670. script_pushint(st,0);
  6671. return 0;
  6672. }
  6673. /*==========================================
  6674. * イベント実行
  6675. *------------------------------------------*/
  6676. BUILDIN_FUNC(doevent)
  6677. {
  6678. const char* event = script_getstr(st,2);
  6679. check_event(st, event);
  6680. npc_event(map_id2sd(st->rid),event,0);
  6681. return 0;
  6682. }
  6683. /*==========================================
  6684. * NPC主体イベント実行
  6685. *------------------------------------------*/
  6686. BUILDIN_FUNC(donpcevent)
  6687. {
  6688. const char* event = script_getstr(st,2);
  6689. check_event(st, event);
  6690. npc_event_do(event);
  6691. return 0;
  6692. }
  6693. /// for Aegis compatibility
  6694. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6695. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6696. {
  6697. const char* npc = script_getstr(st,2);
  6698. const char* command = script_getstr(st,3);
  6699. char event[51];
  6700. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6701. check_event(st, event);
  6702. npc_event_do(event);
  6703. return 0;
  6704. }
  6705. /*==========================================
  6706. * イベントタイマー追加
  6707. *------------------------------------------*/
  6708. BUILDIN_FUNC(addtimer)
  6709. {
  6710. int tick = script_getnum(st,2);
  6711. const char* event = script_getstr(st, 3);
  6712. check_event(st, event);
  6713. pc_addeventtimer(script_rid2sd(st),tick,event);
  6714. return 0;
  6715. }
  6716. /*==========================================
  6717. * イベントタイマー削除
  6718. *------------------------------------------*/
  6719. BUILDIN_FUNC(deltimer)
  6720. {
  6721. const char *event;
  6722. event=script_getstr(st, 2);
  6723. check_event(st, event);
  6724. pc_deleventtimer(script_rid2sd(st),event);
  6725. return 0;
  6726. }
  6727. /*==========================================
  6728. * イベントタイマーのカウント値追加
  6729. *------------------------------------------*/
  6730. BUILDIN_FUNC(addtimercount)
  6731. {
  6732. const char *event;
  6733. int tick;
  6734. event=script_getstr(st, 2);
  6735. tick=script_getnum(st,3);
  6736. check_event(st, event);
  6737. pc_addeventtimercount(script_rid2sd(st),event,tick);
  6738. return 0;
  6739. }
  6740. /*==========================================
  6741. * NPCタイマー初期化
  6742. *------------------------------------------*/
  6743. BUILDIN_FUNC(initnpctimer)
  6744. {
  6745. struct npc_data *nd;
  6746. int flag = 0;
  6747. if( script_hasdata(st,3) )
  6748. { //Two arguments: NPC name and attach flag.
  6749. nd = npc_name2id(script_getstr(st, 2));
  6750. flag = script_getnum(st,3);
  6751. } else
  6752. if( script_hasdata(st,2) )
  6753. { //Check if argument is numeric (flag) or string (npc name)
  6754. struct script_data *data;
  6755. data = script_getdata(st,2);
  6756. get_val(st,data);
  6757. if( data_isstring(data) ) //NPC name
  6758. nd = npc_name2id(script_getstr(st, 2));
  6759. else if( data_isint(data) ) { //Flag
  6760. nd = (struct npc_data *)map_id2bl(st->oid);
  6761. flag = script_getnum(st,3);
  6762. } else {
  6763. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6764. return 1;
  6765. }
  6766. } else
  6767. nd=(struct npc_data *)map_id2bl(st->oid);
  6768. if (!nd) return 0;
  6769. if (flag) { //Attach
  6770. TBL_PC* sd = script_rid2sd(st);
  6771. if (sd) nd->u.scr.rid = sd->bl.id;
  6772. }
  6773. npc_settimerevent_tick(nd,0);
  6774. npc_timerevent_start(nd, st->rid);
  6775. return 0;
  6776. }
  6777. /*==========================================
  6778. * NPCタイマー開始
  6779. *------------------------------------------*/
  6780. BUILDIN_FUNC(startnpctimer)
  6781. {
  6782. struct npc_data *nd;
  6783. int flag = 0;
  6784. if( script_hasdata(st,3) )
  6785. { //Two arguments: NPC name and attach flag.
  6786. nd = npc_name2id(script_getstr(st, 2));
  6787. flag = script_getnum(st,3);
  6788. } else
  6789. if( script_hasdata(st,2) )
  6790. { //Check if argument is numeric (flag) or string (npc name)
  6791. struct script_data *data;
  6792. data = script_getdata(st,2);
  6793. get_val(st,data);
  6794. if( data_isstring(data) ) //NPC name
  6795. nd = npc_name2id(script_getstr(st, 2));
  6796. else if( data_isint(data) ) { //Flag
  6797. nd = (struct npc_data *)map_id2bl(st->oid);
  6798. flag = script_getnum(st,3);
  6799. } else {
  6800. ShowError("startnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6801. return 1;
  6802. }
  6803. } else
  6804. nd=(struct npc_data *)map_id2bl(st->oid);
  6805. if (!nd) return 0;
  6806. if (flag) { //Attach
  6807. TBL_PC* sd = script_rid2sd(st);
  6808. if (sd) nd->u.scr.rid = sd->bl.id;
  6809. }
  6810. npc_timerevent_start(nd, st->rid);
  6811. return 0;
  6812. }
  6813. /*==========================================
  6814. * NPCタイマー停止
  6815. *------------------------------------------*/
  6816. BUILDIN_FUNC(stopnpctimer)
  6817. {
  6818. struct npc_data *nd;
  6819. int flag = 0;
  6820. if( script_hasdata(st,3) )
  6821. { //Two arguments: NPC name and attach flag.
  6822. nd = npc_name2id(script_getstr(st, 2));
  6823. flag = script_getnum(st,3);
  6824. } else
  6825. if( script_hasdata(st,2) )
  6826. { //Check if argument is numeric (flag) or string (npc name)
  6827. struct script_data *data;
  6828. data = script_getdata(st,2);
  6829. get_val(st,data);
  6830. if( data_isstring(data) ) //NPC name
  6831. nd = npc_name2id(script_getstr(st, 2));
  6832. else if( data_isint(data) ) { //Flag
  6833. nd = (struct npc_data *)map_id2bl(st->oid);
  6834. flag = script_getnum(st,3);
  6835. } else {
  6836. ShowError("stopnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6837. return 1;
  6838. }
  6839. } else
  6840. nd=(struct npc_data *)map_id2bl(st->oid);
  6841. if (!nd) return 0;
  6842. if (flag) //Detach
  6843. nd->u.scr.rid = 0;
  6844. npc_timerevent_stop(nd);
  6845. return 0;
  6846. }
  6847. /*==========================================
  6848. * NPCタイマー情報所得
  6849. *------------------------------------------*/
  6850. BUILDIN_FUNC(getnpctimer)
  6851. {
  6852. struct npc_data *nd;
  6853. TBL_PC *sd;
  6854. int type=script_getnum(st,2);
  6855. int val=0;
  6856. if( script_hasdata(st,3) )
  6857. nd = npc_name2id(script_getstr(st,3));
  6858. else
  6859. nd = (struct npc_data *)map_id2bl(st->oid);
  6860. if (!nd || nd->bl.type != BL_NPC)
  6861. {
  6862. script_pushint(st,0);
  6863. if (battle_config.error_log)
  6864. ShowError("getnpctimer: Invalid NPC\n");
  6865. return 1;
  6866. }
  6867. switch(type){
  6868. case 0: val=npc_gettimerevent_tick(nd); break;
  6869. case 1:
  6870. if (nd->u.scr.rid) {
  6871. sd = map_id2sd(nd->u.scr.rid);
  6872. if (!sd) {
  6873. if(battle_config.error_log)
  6874. ShowError("buildin_getnpctimer: Attached player not found!\n");
  6875. break;
  6876. }
  6877. val = (sd->npc_timer_id != -1);
  6878. } else
  6879. val= (nd->u.scr.timerid !=-1);
  6880. break;
  6881. case 2: val= nd->u.scr.timeramount; break;
  6882. }
  6883. script_pushint(st,val);
  6884. return 0;
  6885. }
  6886. /*==========================================
  6887. * NPCタイマー値設定
  6888. *------------------------------------------*/
  6889. BUILDIN_FUNC(setnpctimer)
  6890. {
  6891. int tick;
  6892. struct npc_data *nd;
  6893. tick=script_getnum(st,2);
  6894. if( script_hasdata(st,3) )
  6895. nd=npc_name2id(script_getstr(st,3));
  6896. else
  6897. nd=(struct npc_data *)map_id2bl(st->oid);
  6898. npc_settimerevent_tick(nd,tick);
  6899. return 0;
  6900. }
  6901. /*==========================================
  6902. * attaches the player rid to the timer [Celest]
  6903. *------------------------------------------*/
  6904. BUILDIN_FUNC(attachnpctimer)
  6905. {
  6906. TBL_PC *sd;
  6907. struct npc_data *nd;
  6908. nd=(struct npc_data *)map_id2bl(st->oid);
  6909. if( script_hasdata(st,2) )
  6910. sd=map_nick2sd(script_getstr(st,2));
  6911. else
  6912. sd = script_rid2sd(st);
  6913. if (sd==NULL)
  6914. return 0;
  6915. nd->u.scr.rid = sd->bl.id;
  6916. return 0;
  6917. }
  6918. /*==========================================
  6919. * detaches a player rid from the timer [Celest]
  6920. *------------------------------------------*/
  6921. BUILDIN_FUNC(detachnpctimer)
  6922. {
  6923. struct npc_data *nd;
  6924. if( script_hasdata(st,2) )
  6925. nd=npc_name2id(script_getstr(st,2));
  6926. else
  6927. nd=(struct npc_data *)map_id2bl(st->oid);
  6928. nd->u.scr.rid = 0;
  6929. return 0;
  6930. }
  6931. /*==========================================
  6932. * To avoid "player not attached" script errors, this function is provided,
  6933. * it checks if there is a player attached to the current script. [Skotlex]
  6934. * If no, returns 0, if yes, returns the account_id of the attached player.
  6935. *------------------------------------------*/
  6936. BUILDIN_FUNC(playerattached)
  6937. {
  6938. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  6939. script_pushint(st,0);
  6940. else
  6941. script_pushint(st,st->rid);
  6942. return 0;
  6943. }
  6944. /*==========================================
  6945. * 天の声アナウンス
  6946. *------------------------------------------*/
  6947. BUILDIN_FUNC(announce)
  6948. {
  6949. const char *str, *color=NULL;
  6950. int flag;
  6951. str=script_getstr(st,2);
  6952. flag=script_getnum(st,3);
  6953. if (script_hasdata(st,4))
  6954. color=script_getstr(st,4);
  6955. if(flag&0x0f){
  6956. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  6957. (struct block_list *)script_rid2sd(st);
  6958. if (color)
  6959. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  6960. else
  6961. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  6962. }else {
  6963. if (color)
  6964. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  6965. else
  6966. intif_GMmessage(str,(int)strlen(str)+1,flag);
  6967. }
  6968. return 0;
  6969. }
  6970. /*==========================================
  6971. * 天の声アナウンス(特定マップ)
  6972. *------------------------------------------*/
  6973. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  6974. {
  6975. char *str, *color;
  6976. int len,flag;
  6977. str=va_arg(ap,char *);
  6978. len=va_arg(ap,int);
  6979. flag=va_arg(ap,int);
  6980. color=va_arg(ap,char *);
  6981. if (color)
  6982. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  6983. else
  6984. clif_GMmessage(bl,str,len,flag|3);
  6985. return 0;
  6986. }
  6987. BUILDIN_FUNC(mapannounce)
  6988. {
  6989. const char *mapname,*str, *color=NULL;
  6990. int flag,m;
  6991. mapname=script_getstr(st,2);
  6992. str=script_getstr(st,3);
  6993. flag=script_getnum(st,4);
  6994. if (script_hasdata(st,5))
  6995. color=script_getstr(st,5);
  6996. if( (m=map_mapname2mapid(mapname))<0 )
  6997. return 0;
  6998. map_foreachinmap(buildin_mapannounce_sub,
  6999. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  7000. return 0;
  7001. }
  7002. /*==========================================
  7003. * 天の声アナウンス(特定エリア)
  7004. *------------------------------------------*/
  7005. BUILDIN_FUNC(areaannounce)
  7006. {
  7007. const char *map,*str,*color=NULL;
  7008. int flag,m;
  7009. int x0,y0,x1,y1;
  7010. map=script_getstr(st,2);
  7011. x0=script_getnum(st,3);
  7012. y0=script_getnum(st,4);
  7013. x1=script_getnum(st,5);
  7014. y1=script_getnum(st,6);
  7015. str=script_getstr(st,7);
  7016. flag=script_getnum(st,8);
  7017. if (script_hasdata(st,9))
  7018. color=script_getstr(st,9);
  7019. if( (m=map_mapname2mapid(map))<0 )
  7020. return 0;
  7021. map_foreachinarea(buildin_mapannounce_sub,
  7022. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  7023. return 0;
  7024. }
  7025. /*==========================================
  7026. * ユーザー数所得
  7027. *------------------------------------------*/
  7028. BUILDIN_FUNC(getusers)
  7029. {
  7030. int flag=script_getnum(st,2);
  7031. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7032. int val=0;
  7033. switch(flag&0x07){
  7034. case 0: val=map[bl->m].users; break;
  7035. case 1: val=map_getusers(); break;
  7036. }
  7037. script_pushint(st,val);
  7038. return 0;
  7039. }
  7040. /*==========================================
  7041. * Works like @WHO - displays all online users names in window
  7042. *------------------------------------------*/
  7043. BUILDIN_FUNC(getusersname)
  7044. {
  7045. TBL_PC *sd, *pl_sd = NULL, **pl_allsd;
  7046. int i=0,disp_num=1, users;
  7047. sd = script_rid2sd(st);
  7048. if (!sd) return 0;
  7049. pl_allsd = map_getallusers(&users);
  7050. for (i=0;i<users;i++)
  7051. {
  7052. pl_sd = pl_allsd[i];
  7053. if( !(battle_config.hide_GM_session && pc_isGM(pl_sd)) )
  7054. {
  7055. if((disp_num++)%10==0)
  7056. clif_scriptnext(sd,st->oid);
  7057. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7058. }
  7059. }
  7060. return 0;
  7061. }
  7062. /*==========================================
  7063. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7064. *------------------------------------------*/
  7065. BUILDIN_FUNC(getmapguildusers)
  7066. {
  7067. const char *str;
  7068. int m, gid;
  7069. int i=0,c=0;
  7070. struct guild *g = NULL;
  7071. str=script_getstr(st,2);
  7072. gid=script_getnum(st,3);
  7073. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7074. script_pushint(st,-1);
  7075. return 0;
  7076. }
  7077. g = guild_search(gid);
  7078. if (g){
  7079. for(i = 0; i < g->max_member; i++)
  7080. {
  7081. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7082. c++;
  7083. }
  7084. }
  7085. script_pushint(st,c);
  7086. return 0;
  7087. }
  7088. /*==========================================
  7089. * マップ指定ユーザー数所得
  7090. *------------------------------------------*/
  7091. BUILDIN_FUNC(getmapusers)
  7092. {
  7093. const char *str;
  7094. int m;
  7095. str=script_getstr(st,2);
  7096. if( (m=map_mapname2mapid(str))< 0){
  7097. script_pushint(st,-1);
  7098. return 0;
  7099. }
  7100. script_pushint(st,map[m].users);
  7101. return 0;
  7102. }
  7103. /*==========================================
  7104. * エリア指定ユーザー数所得
  7105. *------------------------------------------*/
  7106. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7107. {
  7108. int *users=va_arg(ap,int *);
  7109. (*users)++;
  7110. return 0;
  7111. }
  7112. BUILDIN_FUNC(getareausers)
  7113. {
  7114. const char *str;
  7115. int m,x0,y0,x1,y1,users=0;
  7116. str=script_getstr(st,2);
  7117. x0=script_getnum(st,3);
  7118. y0=script_getnum(st,4);
  7119. x1=script_getnum(st,5);
  7120. y1=script_getnum(st,6);
  7121. if( (m=map_mapname2mapid(str))< 0){
  7122. script_pushint(st,-1);
  7123. return 0;
  7124. }
  7125. map_foreachinarea(buildin_getareausers_sub,
  7126. m,x0,y0,x1,y1,BL_PC,&users);
  7127. script_pushint(st,users);
  7128. return 0;
  7129. }
  7130. /*==========================================
  7131. * エリア指定ドロップアイテム数所得
  7132. *------------------------------------------*/
  7133. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7134. {
  7135. int item=va_arg(ap,int);
  7136. int *amount=va_arg(ap,int *);
  7137. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7138. if(drop->item_data.nameid==item)
  7139. (*amount)+=drop->item_data.amount;
  7140. return 0;
  7141. }
  7142. BUILDIN_FUNC(getareadropitem)
  7143. {
  7144. const char *str;
  7145. int m,x0,y0,x1,y1,item,amount=0;
  7146. struct script_data *data;
  7147. str=script_getstr(st,2);
  7148. x0=script_getnum(st,3);
  7149. y0=script_getnum(st,4);
  7150. x1=script_getnum(st,5);
  7151. y1=script_getnum(st,6);
  7152. data=script_getdata(st,7);
  7153. get_val(st,data);
  7154. if( data_isstring(data) ){
  7155. const char *name=conv_str(st,data);
  7156. struct item_data *item_data = itemdb_searchname(name);
  7157. item=UNKNOWN_ITEM_ID;
  7158. if( item_data )
  7159. item=item_data->nameid;
  7160. }else
  7161. item=conv_num(st,data);
  7162. if( (m=map_mapname2mapid(str))< 0){
  7163. script_pushint(st,-1);
  7164. return 0;
  7165. }
  7166. map_foreachinarea(buildin_getareadropitem_sub,
  7167. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7168. script_pushint(st,amount);
  7169. return 0;
  7170. }
  7171. /*==========================================
  7172. * NPCの有効化
  7173. *------------------------------------------*/
  7174. BUILDIN_FUNC(enablenpc)
  7175. {
  7176. const char *str;
  7177. str=script_getstr(st,2);
  7178. npc_enable(str,1);
  7179. return 0;
  7180. }
  7181. /*==========================================
  7182. * NPCの無効化
  7183. *------------------------------------------*/
  7184. BUILDIN_FUNC(disablenpc)
  7185. {
  7186. const char *str;
  7187. str=script_getstr(st,2);
  7188. npc_enable(str,0);
  7189. return 0;
  7190. }
  7191. /*==========================================
  7192. * 隠れているNPCの表示
  7193. *------------------------------------------*/
  7194. BUILDIN_FUNC(hideoffnpc)
  7195. {
  7196. const char *str;
  7197. str=script_getstr(st,2);
  7198. npc_enable(str,2);
  7199. return 0;
  7200. }
  7201. /*==========================================
  7202. * NPCをハイディング
  7203. *------------------------------------------*/
  7204. BUILDIN_FUNC(hideonnpc)
  7205. {
  7206. const char *str;
  7207. str=script_getstr(st,2);
  7208. npc_enable(str,4);
  7209. return 0;
  7210. }
  7211. /// Starts a status effect on the target unit or on the attached player.
  7212. ///
  7213. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7214. BUILDIN_FUNC(sc_start)
  7215. {
  7216. struct block_list* bl;
  7217. int type;
  7218. int tick;
  7219. int val1;
  7220. int val4 = 0;
  7221. type = script_getnum(st,2);
  7222. tick = script_getnum(st,3);
  7223. val1 = script_getnum(st,4);
  7224. if( script_hasdata(st,5) )
  7225. bl = map_id2bl(script_getnum(st,5));
  7226. else
  7227. bl = map_id2bl(st->rid);
  7228. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7229. {// When there isn't a duration specified, try to get it from the skill_db
  7230. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7231. }
  7232. if( potion_flag == 1 && potion_target )
  7233. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7234. bl = map_id2bl(potion_target);
  7235. tick /= 2;// Thrown potions only last half.
  7236. val4 = 1;// Mark that this was a thrown sc_effect
  7237. }
  7238. if( bl )
  7239. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 1|2|8);
  7240. return 0;
  7241. }
  7242. /// Starts a status effect on the target unit or on the attached player.
  7243. ///
  7244. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7245. BUILDIN_FUNC(sc_start2)
  7246. {
  7247. struct block_list* bl;
  7248. int type;
  7249. int tick;
  7250. int val1;
  7251. int val4 = 0;
  7252. int rate;
  7253. type = script_getnum(st,2);
  7254. tick = script_getnum(st,3);
  7255. val1 = script_getnum(st,4);
  7256. rate = script_getnum(st,5);
  7257. if( script_hasdata(st,6) )
  7258. bl = map_id2bl(script_getnum(st,6));
  7259. else
  7260. bl = map_id2bl(st->rid);
  7261. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7262. {// When there isn't a duration specified, try to get it from the skill_db
  7263. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7264. }
  7265. if( potion_flag == 1 && potion_target )
  7266. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7267. bl = map_id2bl(potion_target);
  7268. tick /= 2;// Thrown potions only last half.
  7269. val4 = 1;// Mark that this was a thrown sc_effect
  7270. }
  7271. if( bl )
  7272. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 1|2|8);
  7273. return 0;
  7274. }
  7275. /// Starts a status effect on the target unit or on the attached player.
  7276. ///
  7277. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7278. BUILDIN_FUNC(sc_start4)
  7279. {
  7280. struct block_list* bl;
  7281. int type;
  7282. int tick;
  7283. int val1;
  7284. int val2;
  7285. int val3;
  7286. int val4;
  7287. type = script_getnum(st,2);
  7288. tick = script_getnum(st,3);
  7289. val1 = script_getnum(st,4);
  7290. val2 = script_getnum(st,5);
  7291. val3 = script_getnum(st,6);
  7292. val4 = script_getnum(st,7);
  7293. if( script_hasdata(st,8) )
  7294. bl = map_id2bl(script_getnum(st,8));
  7295. else
  7296. bl = map_id2bl(st->rid);
  7297. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7298. {// When there isn't a duration specified, try to get it from the skill_db
  7299. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7300. }
  7301. if( potion_flag == 1 && potion_target )
  7302. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7303. bl = map_id2bl(potion_target);
  7304. tick /= 2;// Thrown potions only last half.
  7305. }
  7306. if( bl )
  7307. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 1|2|8);
  7308. return 0;
  7309. }
  7310. /// Ends one or all status effects on the target unit or on the attached player.
  7311. ///
  7312. /// sc_end <effect_id>{,<unit_id>};
  7313. BUILDIN_FUNC(sc_end)
  7314. {
  7315. struct block_list* bl;
  7316. int type;
  7317. type = script_getnum(st,2);
  7318. if( script_hasdata(st,3) )
  7319. bl = map_id2bl(script_getnum(st,3));
  7320. else
  7321. bl = map_id2bl(st->rid);
  7322. if( potion_flag==1 && potion_target )
  7323. {//##TODO how does this work [FlavioJS]
  7324. bl = map_id2bl(potion_target);
  7325. }
  7326. if( bl )
  7327. {
  7328. if( type >= 0 )
  7329. status_change_end(bl, type, INVALID_TIMER);
  7330. else
  7331. status_change_clear(bl, 2);// remove all effects
  7332. }
  7333. return 0;
  7334. }
  7335. /*==========================================
  7336. * 状態異常耐性を計算した確率を返す
  7337. *------------------------------------------*/
  7338. BUILDIN_FUNC(getscrate)
  7339. {
  7340. struct block_list *bl;
  7341. int type,rate;
  7342. type=script_getnum(st,2);
  7343. rate=script_getnum(st,3);
  7344. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7345. bl = map_id2bl(script_getnum(st,4));
  7346. else
  7347. bl = map_id2bl(st->rid);
  7348. if (bl)
  7349. rate = status_get_sc_def(bl,type, 10000, 10000, 0);
  7350. script_pushint(st,rate);
  7351. return 0;
  7352. }
  7353. /*==========================================
  7354. *
  7355. *------------------------------------------*/
  7356. BUILDIN_FUNC(debugmes)
  7357. {
  7358. const char *str;
  7359. str=script_getstr(st,2);
  7360. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7361. return 0;
  7362. }
  7363. /*==========================================
  7364. *捕獲アイテム使用
  7365. *------------------------------------------*/
  7366. BUILDIN_FUNC(catchpet)
  7367. {
  7368. int pet_id;
  7369. TBL_PC *sd;
  7370. pet_id= script_getnum(st,2);
  7371. sd=script_rid2sd(st);
  7372. pet_catch_process1(sd,pet_id);
  7373. return 0;
  7374. }
  7375. /*==========================================
  7376. * [orn]
  7377. *------------------------------------------*/
  7378. BUILDIN_FUNC(homunculus_evolution)
  7379. {
  7380. TBL_PC *sd;
  7381. sd=script_rid2sd(st);
  7382. if(merc_is_hom_active(sd->hd))
  7383. {
  7384. if (sd->hd->homunculus.intimacy > 91000)
  7385. merc_hom_evolution(sd->hd);
  7386. else
  7387. clif_emotion(&sd->hd->bl, 4) ; //swt
  7388. }
  7389. return 0;
  7390. }
  7391. // [Zephyrus]
  7392. BUILDIN_FUNC(homunculus_shuffle)
  7393. {
  7394. TBL_PC *sd;
  7395. sd=script_rid2sd(st);
  7396. if(merc_is_hom_active(sd->hd))
  7397. merc_hom_shuffle(sd->hd);
  7398. return 0;
  7399. }
  7400. //These two functions bring the eA MAPID_* class functionality to scripts.
  7401. BUILDIN_FUNC(eaclass)
  7402. {
  7403. int class_;
  7404. if( script_hasdata(st,2) )
  7405. class_ = script_getnum(st,2);
  7406. else {
  7407. TBL_PC *sd;
  7408. sd=script_rid2sd(st);
  7409. if (!sd) {
  7410. script_pushint(st,-1);
  7411. return 0;
  7412. }
  7413. class_ = sd->status.class_;
  7414. }
  7415. script_pushint(st,pc_jobid2mapid(class_));
  7416. return 0;
  7417. }
  7418. BUILDIN_FUNC(roclass)
  7419. {
  7420. int class_ =script_getnum(st,2);
  7421. int sex;
  7422. if( script_hasdata(st,3) )
  7423. sex = script_getnum(st,3);
  7424. else {
  7425. TBL_PC *sd;
  7426. if (st->rid && (sd=script_rid2sd(st)))
  7427. sex = sd->status.sex;
  7428. else
  7429. sex = 1; //Just use male when not found.
  7430. }
  7431. script_pushint(st,pc_mapid2jobid(class_, sex));
  7432. return 0;
  7433. }
  7434. /*==========================================
  7435. *携帯卵孵化機使用
  7436. *------------------------------------------*/
  7437. BUILDIN_FUNC(birthpet)
  7438. {
  7439. TBL_PC *sd;
  7440. sd=script_rid2sd(st);
  7441. clif_sendegg(sd);
  7442. return 0;
  7443. }
  7444. /*==========================================
  7445. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7446. *------------------------------------------*/
  7447. BUILDIN_FUNC(resetlvl)
  7448. {
  7449. TBL_PC *sd;
  7450. int type=script_getnum(st,2);
  7451. sd=script_rid2sd(st);
  7452. pc_resetlvl(sd,type);
  7453. return 0;
  7454. }
  7455. /*==========================================
  7456. * ステータスリセット
  7457. *------------------------------------------*/
  7458. BUILDIN_FUNC(resetstatus)
  7459. {
  7460. TBL_PC *sd;
  7461. sd=script_rid2sd(st);
  7462. pc_resetstate(sd);
  7463. return 0;
  7464. }
  7465. /*==========================================
  7466. * script command resetskill
  7467. *------------------------------------------*/
  7468. BUILDIN_FUNC(resetskill)
  7469. {
  7470. TBL_PC *sd;
  7471. sd=script_rid2sd(st);
  7472. pc_resetskill(sd,1);
  7473. return 0;
  7474. }
  7475. /*==========================================
  7476. * Counts total amount of skill points.
  7477. *------------------------------------------*/
  7478. BUILDIN_FUNC(skillpointcount)
  7479. {
  7480. TBL_PC *sd;
  7481. sd=script_rid2sd(st);
  7482. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7483. return 0;
  7484. }
  7485. /*==========================================
  7486. *
  7487. *------------------------------------------*/
  7488. BUILDIN_FUNC(changebase)
  7489. {
  7490. TBL_PC *sd=NULL;
  7491. int vclass;
  7492. if( script_hasdata(st,3) )
  7493. sd=map_id2sd(script_getnum(st,3));
  7494. else
  7495. sd=script_rid2sd(st);
  7496. if(sd == NULL)
  7497. return 0;
  7498. vclass = script_getnum(st,2);
  7499. if(vclass == JOB_WEDDING)
  7500. {
  7501. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7502. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7503. )
  7504. return 0;
  7505. }
  7506. if(!sd->disguise && vclass != sd->vd.class_) {
  7507. status_set_viewdata(&sd->bl, vclass);
  7508. //Updated client view. Base, Weapon and Cloth Colors.
  7509. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7510. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7511. if (sd->vd.cloth_color)
  7512. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7513. }
  7514. return 0;
  7515. }
  7516. /*==========================================
  7517. * 性別変換
  7518. *------------------------------------------*/
  7519. BUILDIN_FUNC(changesex)
  7520. {
  7521. TBL_PC *sd = NULL;
  7522. sd = script_rid2sd(st);
  7523. if (sd->status.sex == 0) {
  7524. sd->status.sex = 1;
  7525. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7526. sd->status.class_ -= 1;
  7527. } else if (sd->status.sex == 1) {
  7528. sd->status.sex = 0;
  7529. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7530. sd->status.class_ += 1;
  7531. }
  7532. chrif_char_ask_name(-1, sd->status.name, 5, 0, 0, 0, 0, 0, 0); // type: 5 - changesex
  7533. chrif_save(sd,0);
  7534. return 0;
  7535. }
  7536. /*==========================================
  7537. * Works like 'announce' but outputs in the common chat window
  7538. *------------------------------------------*/
  7539. BUILDIN_FUNC(globalmes)
  7540. {
  7541. struct block_list *bl = map_id2bl(st->oid);
  7542. struct npc_data *nd = (struct npc_data *)bl;
  7543. const char *name=NULL,*mes;
  7544. mes=script_getstr(st,2); // メッセージの取得
  7545. if(mes==NULL) return 0;
  7546. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7547. name=script_getstr(st,3);
  7548. } else {
  7549. name=nd->name;
  7550. }
  7551. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7552. return 0;
  7553. }
  7554. /////////////////////////////////////////////////////////////////////
  7555. // NPC waiting room (chat room)
  7556. //
  7557. /// Creates a waiting room (chat room) for this npc.
  7558. ///
  7559. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7560. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7561. /// waitingroom "<title>",<limit>,"<event>";
  7562. /// waitingroom "<title>",<limit>;
  7563. BUILDIN_FUNC(waitingroom)
  7564. {
  7565. struct npc_data* nd;
  7566. const char* title;
  7567. const char* ev = "";
  7568. int limit;
  7569. int trigger = 0;
  7570. int pub = 1;
  7571. title = script_getstr(st, 2);
  7572. limit = script_getnum(st, 3);
  7573. if( script_hasdata(st,5) )
  7574. {
  7575. struct script_data* last = script_getdata(st, 5);
  7576. get_val(st, last);
  7577. if( data_isstring(last) )
  7578. {// ,<trigger>,"<event>"
  7579. trigger = script_getnum(st, 4);
  7580. ev = script_getstr(st, 5);
  7581. }
  7582. else
  7583. {// ,"<event>",<trigger>
  7584. ev = script_getstr(st, 4);
  7585. trigger = script_getnum(st,5);
  7586. }
  7587. }
  7588. else if( script_hasdata(st,4) )
  7589. {// ,"<event>"
  7590. ev = script_getstr(st, 4);
  7591. trigger = limit;
  7592. }
  7593. if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL )
  7594. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7595. return 0;
  7596. }
  7597. /// Removes the waiting room of the current or target npc.
  7598. ///
  7599. /// delwaitingroom "<npc_name>";
  7600. /// delwaitingroom;
  7601. BUILDIN_FUNC(delwaitingroom)
  7602. {
  7603. struct npc_data* nd;
  7604. if( script_hasdata(st,2) )
  7605. nd = npc_name2id(script_getstr(st, 2));
  7606. else
  7607. nd = (struct npc_data *)map_id2bl(st->oid);
  7608. if( nd != NULL )
  7609. chat_deletenpcchat(nd);
  7610. return 0;
  7611. }
  7612. /// Kicks all the players from the waiting room of the current or target npc.
  7613. ///
  7614. /// kickwaitingroomall "<npc_name>";
  7615. /// kickwaitingroomall;
  7616. BUILDIN_FUNC(waitingroomkickall)
  7617. {
  7618. struct npc_data* nd;
  7619. struct chat_data* cd;
  7620. if( script_hasdata(st,2) )
  7621. nd = npc_name2id(script_getstr(st,2));
  7622. else
  7623. nd = (struct npc_data *)map_id2bl(st->oid);
  7624. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7625. chat_npckickall(cd);
  7626. return 0;
  7627. }
  7628. /// Enables the waiting room event of the current or target npc.
  7629. ///
  7630. /// enablewaitingroomevent "<npc_name>";
  7631. /// enablewaitingroomevent;
  7632. BUILDIN_FUNC(enablewaitingroomevent)
  7633. {
  7634. struct npc_data* nd;
  7635. struct chat_data* cd;
  7636. if( script_hasdata(st,2) )
  7637. nd = npc_name2id(script_getstr(st, 2));
  7638. else
  7639. nd = (struct npc_data *)map_id2bl(st->oid);
  7640. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7641. chat_enableevent(cd);
  7642. return 0;
  7643. }
  7644. /// Disables the waiting room event of the current or target npc.
  7645. ///
  7646. /// disablewaitingroomevent "<npc_name>";
  7647. /// disablewaitingroomevent;
  7648. BUILDIN_FUNC(disablewaitingroomevent)
  7649. {
  7650. struct npc_data *nd;
  7651. struct chat_data *cd;
  7652. if( script_hasdata(st,2) )
  7653. nd = npc_name2id(script_getstr(st, 2));
  7654. else
  7655. nd = (struct npc_data *)map_id2bl(st->oid);
  7656. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7657. chat_disableevent(cd);
  7658. return 0;
  7659. }
  7660. /// Returns info on the waiting room of the current or target npc.
  7661. /// Returns -1 if the type unknown
  7662. /// <type>=0 : current number of users
  7663. /// <type>=1 : maximum number of users allowed
  7664. /// <type>=2 : the number of users that trigger the event
  7665. /// <type>=3 : if the trigger is disabled
  7666. /// <type>=4 : the title of the waiting room
  7667. /// <type>=5 : the password of the waiting room
  7668. /// <type>=16 : the name of the waiting room event
  7669. /// <type>=32 : if the waiting room is full
  7670. /// <type>=33 : if there are enough users to trigger the event
  7671. ///
  7672. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7673. /// getwaitingroomstate(<type>) -> <info>
  7674. BUILDIN_FUNC(getwaitingroomstate)
  7675. {
  7676. struct npc_data *nd;
  7677. struct chat_data *cd;
  7678. int type;
  7679. type = script_getnum(st,2);
  7680. if( script_hasdata(st,3) )
  7681. nd = npc_name2id(script_getstr(st, 3));
  7682. else
  7683. nd = (struct npc_data *)map_id2bl(st->oid);
  7684. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7685. {
  7686. script_pushint(st, -1);
  7687. return 0;
  7688. }
  7689. switch(type)
  7690. {
  7691. case 0: script_pushint(st, cd->users); break;
  7692. case 1: script_pushint(st, cd->limit); break;
  7693. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7694. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7695. case 4: script_pushconststr(st, cd->title); break;
  7696. case 5: script_pushconststr(st, cd->pass); break;
  7697. case 16: script_pushconststr(st, cd->npc_event);break;
  7698. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7699. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7700. default: script_pushint(st, -1); break;
  7701. }
  7702. return 0;
  7703. }
  7704. /// Warps the trigger or target amount of players to the target map and position.
  7705. /// Players are automatically removed from the waiting room.
  7706. /// Those waiting the longest will get warped first.
  7707. /// The target map can be "Random" for a random position in the current map,
  7708. /// and "SavePoint" for the savepoint map+position.
  7709. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7710. ///
  7711. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7712. /// The total number of teleported players is put into $@warpwaitingpcnum
  7713. ///
  7714. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7715. /// warpwaitingpc "<map name>",<x>,<y>;
  7716. BUILDIN_FUNC(warpwaitingpc)
  7717. {
  7718. int x;
  7719. int y;
  7720. int i;
  7721. int n;
  7722. const char* map_name;
  7723. struct npc_data* nd;
  7724. struct chat_data* cd;
  7725. TBL_PC* sd;
  7726. nd = (struct npc_data *)map_id2bl(st->oid);
  7727. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7728. return 0;
  7729. map_name = script_getstr(st,2);
  7730. x = script_getnum(st,3);
  7731. y = script_getnum(st,4);
  7732. n = cd->trigger&0x7f;
  7733. if( script_hasdata(st,5) )
  7734. n = script_getnum(st,5);
  7735. for( i = 0; i < n && cd->users > 0; i++ )
  7736. {
  7737. sd = cd->usersd[0];
  7738. if( sd == NULL )
  7739. {
  7740. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  7741. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  7742. continue;// Broken npc chat room?
  7743. }
  7744. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  7745. if( strcmp(map_name,"Random") == 0 )
  7746. pc_randomwarp(sd,3);
  7747. else if( strcmp(map_name,"SavePoint") == 0 )
  7748. {
  7749. if( map[sd->bl.m].flag.noteleport )
  7750. return 0;// can't teleport on this map
  7751. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  7752. }
  7753. else
  7754. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  7755. }
  7756. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  7757. return 0;
  7758. }
  7759. /////////////////////////////////////////////////////////////////////
  7760. // ...
  7761. //
  7762. /*==========================================
  7763. * RIDのアタッチ
  7764. *------------------------------------------*/
  7765. BUILDIN_FUNC(attachrid)
  7766. {
  7767. st->rid=script_getnum(st,2);
  7768. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  7769. return 0;
  7770. }
  7771. /*==========================================
  7772. * RIDのデタッチ
  7773. *------------------------------------------*/
  7774. BUILDIN_FUNC(detachrid)
  7775. {
  7776. st->rid=0;
  7777. return 0;
  7778. }
  7779. /*==========================================
  7780. * 存在チェック
  7781. *------------------------------------------*/
  7782. BUILDIN_FUNC(isloggedin)
  7783. {
  7784. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  7785. if (script_hasdata(st,3) && sd &&
  7786. sd->status.char_id != script_getnum(st,3))
  7787. sd = NULL;
  7788. push_val(st->stack,C_INT,sd!=NULL);
  7789. return 0;
  7790. }
  7791. /*==========================================
  7792. *
  7793. *------------------------------------------*/
  7794. BUILDIN_FUNC(setmapflagnosave)
  7795. {
  7796. int m,x,y;
  7797. unsigned short mapindex;
  7798. const char *str,*str2;
  7799. str=script_getstr(st,2);
  7800. str2=script_getstr(st,3);
  7801. x=script_getnum(st,4);
  7802. y=script_getnum(st,5);
  7803. m = map_mapname2mapid(str);
  7804. mapindex = mapindex_name2id(str2);
  7805. if(m >= 0 && mapindex) {
  7806. map[m].flag.nosave=1;
  7807. map[m].save.map=mapindex;
  7808. map[m].save.x=x;
  7809. map[m].save.y=y;
  7810. }
  7811. return 0;
  7812. }
  7813. BUILDIN_FUNC(setmapflag)
  7814. {
  7815. int m,i;
  7816. const char *str;
  7817. const char *val=NULL;
  7818. str=script_getstr(st,2);
  7819. i=script_getnum(st,3);
  7820. if(script_hasdata(st,4)){
  7821. val=script_getstr(st,4);
  7822. }
  7823. m = map_mapname2mapid(str);
  7824. if(m >= 0) {
  7825. switch(i) {
  7826. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  7827. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  7828. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  7829. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  7830. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  7831. case MF_PVP: map[m].flag.pvp=1; break;
  7832. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  7833. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  7834. case MF_GVG: map[m].flag.gvg=1; break;
  7835. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  7836. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  7837. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  7838. case MF_NOTRADE: map[m].flag.notrade=1; break;
  7839. case MF_NODROP: map[m].flag.nodrop=1; break;
  7840. case MF_NOSKILL: map[m].flag.noskill=1; break;
  7841. case MF_NOWARP: map[m].flag.nowarp=1; break;
  7842. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  7843. case MF_SNOW: map[m].flag.snow=1; break;
  7844. case MF_CLOUDS: map[m].flag.clouds=1; break;
  7845. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  7846. case MF_FOG: map[m].flag.fog=1; break;
  7847. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  7848. case MF_SAKURA: map[m].flag.sakura=1; break;
  7849. case MF_LEAVES: map[m].flag.leaves=1; break;
  7850. case MF_RAIN: map[m].flag.rain=1; break;
  7851. case MF_INDOORS: map[m].flag.indoors=1; break;
  7852. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  7853. case MF_NOGO: map[m].flag.nogo=1; break;
  7854. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  7855. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  7856. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  7857. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  7858. case MF_NORETURN: map[m].flag.noreturn=1; break;
  7859. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  7860. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  7861. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  7862. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  7863. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7864. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7865. case MF_NOVENDING: map[m].flag.novending=1; break;
  7866. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  7867. case MF_NOCHAT: map[m].flag.nochat=1; break;
  7868. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  7869. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  7870. }
  7871. }
  7872. return 0;
  7873. }
  7874. BUILDIN_FUNC(removemapflag)
  7875. {
  7876. int m,i;
  7877. const char *str;
  7878. str=script_getstr(st,2);
  7879. i=script_getnum(st,3);
  7880. m = map_mapname2mapid(str);
  7881. if(m >= 0) {
  7882. switch(i) {
  7883. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  7884. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  7885. case MF_NOSAVE: map[m].flag.nosave=0; break;
  7886. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  7887. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  7888. case MF_PVP: map[m].flag.pvp=0; break;
  7889. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  7890. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  7891. case MF_GVG: map[m].flag.gvg=0; break;
  7892. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  7893. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  7894. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  7895. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  7896. case MF_NOTRADE: map[m].flag.notrade=0; break;
  7897. case MF_NODROP: map[m].flag.nodrop=0; break;
  7898. case MF_NOSKILL: map[m].flag.noskill=0; break;
  7899. case MF_NOWARP: map[m].flag.nowarp=0; break;
  7900. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  7901. case MF_SNOW: map[m].flag.snow=0; break;
  7902. case MF_CLOUDS: map[m].flag.clouds=0; break;
  7903. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  7904. case MF_FOG: map[m].flag.fog=0; break;
  7905. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  7906. case MF_SAKURA: map[m].flag.sakura=0; break;
  7907. case MF_LEAVES: map[m].flag.leaves=0; break;
  7908. case MF_RAIN: map[m].flag.rain=0; break;
  7909. case MF_INDOORS: map[m].flag.indoors=0; break;
  7910. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  7911. case MF_NOGO: map[m].flag.nogo=0; break;
  7912. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  7913. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  7914. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  7915. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  7916. case MF_NORETURN: map[m].flag.noreturn=0; break;
  7917. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  7918. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  7919. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  7920. case MF_NOCOMMAND: map[m].nocommand=0; break;
  7921. case MF_JEXP: map[m].jexp=100; break;
  7922. case MF_BEXP: map[m].bexp=100; break;
  7923. case MF_NOVENDING: map[m].flag.novending=0; break;
  7924. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  7925. case MF_NOCHAT: map[m].flag.nochat=0; break;
  7926. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  7927. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  7928. }
  7929. }
  7930. return 0;
  7931. }
  7932. BUILDIN_FUNC(pvpon)
  7933. {
  7934. int m,i,users;
  7935. const char *str;
  7936. TBL_PC *pl_sd=NULL, **pl_allsd;
  7937. str=script_getstr(st,2);
  7938. m = map_mapname2mapid(str);
  7939. if(m >= 0 && !map[m].flag.pvp) {
  7940. map[m].flag.pvp = 1;
  7941. clif_send0199(m,1);
  7942. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  7943. return 0;
  7944. pl_allsd = map_getallusers(&users);
  7945. for(i=0;i<users;i++)
  7946. {
  7947. if ((pl_sd = pl_allsd[i]) && m == pl_sd->bl.m && pl_sd->pvp_timer == -1)
  7948. {
  7949. pl_sd->pvp_timer=add_timer(gettick()+200,pc_calc_pvprank_timer,pl_sd->bl.id,0);
  7950. pl_sd->pvp_rank=0;
  7951. pl_sd->pvp_lastusers=0;
  7952. pl_sd->pvp_point=5;
  7953. pl_sd->pvp_won = 0;
  7954. pl_sd->pvp_lost = 0;
  7955. }
  7956. }
  7957. }
  7958. return 0;
  7959. }
  7960. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  7961. {
  7962. TBL_PC* sd = (TBL_PC*)bl;
  7963. clif_pvpset(sd, 0, 0, 2);
  7964. if (sd->pvp_timer != -1) {
  7965. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  7966. sd->pvp_timer = -1;
  7967. }
  7968. return 0;
  7969. }
  7970. BUILDIN_FUNC(pvpoff)
  7971. {
  7972. int m;
  7973. const char *str;
  7974. str=script_getstr(st,2);
  7975. m = map_mapname2mapid(str);
  7976. if(m < 0 || !map[m].flag.pvp)
  7977. return 0; //fixed Lupus
  7978. map[m].flag.pvp = 0;
  7979. clif_send0199(m,0);
  7980. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  7981. return 0;
  7982. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  7983. return 0;
  7984. }
  7985. BUILDIN_FUNC(gvgon)
  7986. {
  7987. int m;
  7988. const char *str;
  7989. str=script_getstr(st,2);
  7990. m = map_mapname2mapid(str);
  7991. if(m >= 0 && !map[m].flag.gvg) {
  7992. map[m].flag.gvg = 1;
  7993. clif_send0199(m,3);
  7994. }
  7995. return 0;
  7996. }
  7997. BUILDIN_FUNC(gvgoff)
  7998. {
  7999. int m;
  8000. const char *str;
  8001. str=script_getstr(st,2);
  8002. m = map_mapname2mapid(str);
  8003. if(m >= 0 && map[m].flag.gvg) {
  8004. map[m].flag.gvg = 0;
  8005. clif_send0199(m,0);
  8006. }
  8007. return 0;
  8008. }
  8009. /*==========================================
  8010. * Shows an emoticon on top of the player/npc
  8011. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8012. *------------------------------------------*/
  8013. //Optional second parameter added by [Skotlex]
  8014. BUILDIN_FUNC(emotion)
  8015. {
  8016. int type;
  8017. int player=0;
  8018. type=script_getnum(st,2);
  8019. if(type < 0 || type > 100)
  8020. return 0;
  8021. if( script_hasdata(st,3) )
  8022. player=script_getnum(st,3);
  8023. if (player) {
  8024. TBL_PC *sd = script_rid2sd(st);
  8025. if (sd)
  8026. clif_emotion(&sd->bl,type);
  8027. } else
  8028. clif_emotion(map_id2bl(st->oid),type);
  8029. return 0;
  8030. }
  8031. static int buildin_maprespawnguildid_sub_pc(DBKey key, void *data, va_list ap)
  8032. {
  8033. int m=va_arg(ap,int);
  8034. int g_id=va_arg(ap,int);
  8035. int flag=va_arg(ap,int);
  8036. TBL_PC *sd = (TBL_PC*)data;
  8037. if(!sd || sd->bl.m != m)
  8038. return 0;
  8039. if(
  8040. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8041. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8042. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8043. )
  8044. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8045. return 1;
  8046. }
  8047. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8048. {
  8049. struct mob_data *md=(struct mob_data *)bl;
  8050. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8051. status_kill(bl);
  8052. return 0;
  8053. }
  8054. BUILDIN_FUNC(maprespawnguildid)
  8055. {
  8056. const char *mapname=script_getstr(st,2);
  8057. int g_id=script_getnum(st,3);
  8058. int flag=script_getnum(st,4);
  8059. int m=map_mapname2mapid(mapname);
  8060. if(m == -1)
  8061. return 0;
  8062. //Catch ALL players (in case some are 'between maps' on execution time)
  8063. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8064. if (flag&4) //Remove script mobs.
  8065. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8066. return 0;
  8067. }
  8068. BUILDIN_FUNC(agitstart)
  8069. {
  8070. if(agit_flag==1) return 0; // Agit already Start.
  8071. agit_flag=1;
  8072. guild_agit_start();
  8073. return 0;
  8074. }
  8075. BUILDIN_FUNC(agitend)
  8076. {
  8077. if(agit_flag==0) return 0; // Agit already End.
  8078. agit_flag=0;
  8079. guild_agit_end();
  8080. return 0;
  8081. }
  8082. /*==========================================
  8083. * Returns whether woe is on or off. // choice script
  8084. *------------------------------------------*/
  8085. BUILDIN_FUNC(agitcheck)
  8086. {
  8087. script_pushint(st,agit_flag);
  8088. return 0;
  8089. }
  8090. BUILDIN_FUNC(flagemblem)
  8091. {
  8092. int g_id=script_getnum(st,2);
  8093. if(g_id < 0) return 0;
  8094. // printf("Script.c: [FlagEmblem] GuildID=%d, Emblem=%d.\n", g->guild_id, g->emblem_id);
  8095. ((struct npc_data *)map_id2bl(st->oid))->u.scr.guild_id = g_id;
  8096. return 0;
  8097. }
  8098. BUILDIN_FUNC(getcastlename)
  8099. {
  8100. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8101. struct guild_castle* gc = guild_mapname2gc(mapname);
  8102. char* name = (gc) ? gc->castle_name : "";
  8103. script_pushconststr(st,name);
  8104. return 0;
  8105. }
  8106. BUILDIN_FUNC(getcastledata)
  8107. {
  8108. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8109. int index = script_getnum(st,3);
  8110. struct guild_castle* gc = guild_mapname2gc(mapname);
  8111. if(script_hasdata(st,4) && index==0 && gc) {
  8112. const char* event = script_getstr(st,4);
  8113. check_event(st, event);
  8114. guild_addcastleinfoevent(gc->castle_id,17,event);
  8115. }
  8116. if(gc){
  8117. switch(index){
  8118. case 0: {
  8119. int i;
  8120. for(i=1;i<26;i++) // Initialize[AgitInit]
  8121. guild_castledataload(gc->castle_id,i);
  8122. } break;
  8123. case 1:
  8124. script_pushint(st,gc->guild_id); break;
  8125. case 2:
  8126. script_pushint(st,gc->economy); break;
  8127. case 3:
  8128. script_pushint(st,gc->defense); break;
  8129. case 4:
  8130. script_pushint(st,gc->triggerE); break;
  8131. case 5:
  8132. script_pushint(st,gc->triggerD); break;
  8133. case 6:
  8134. script_pushint(st,gc->nextTime); break;
  8135. case 7:
  8136. script_pushint(st,gc->payTime); break;
  8137. case 8:
  8138. script_pushint(st,gc->createTime); break;
  8139. case 9:
  8140. script_pushint(st,gc->visibleC); break;
  8141. case 10:
  8142. case 11:
  8143. case 12:
  8144. case 13:
  8145. case 14:
  8146. case 15:
  8147. case 16:
  8148. case 17:
  8149. script_pushint(st,gc->guardian[index-10].visible); break;
  8150. case 18:
  8151. case 19:
  8152. case 20:
  8153. case 21:
  8154. case 22:
  8155. case 23:
  8156. case 24:
  8157. case 25:
  8158. script_pushint(st,gc->guardian[index-18].hp); break;
  8159. default:
  8160. script_pushint(st,0); break;
  8161. }
  8162. return 0;
  8163. }
  8164. script_pushint(st,0);
  8165. return 0;
  8166. }
  8167. BUILDIN_FUNC(setcastledata)
  8168. {
  8169. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8170. int index = script_getnum(st,3);
  8171. int value = script_getnum(st,4);
  8172. struct guild_castle* gc = guild_mapname2gc(mapname);
  8173. if(gc) {
  8174. // Save Data byself First
  8175. switch(index){
  8176. case 1:
  8177. gc->guild_id = value; break;
  8178. case 2:
  8179. gc->economy = value; break;
  8180. case 3:
  8181. gc->defense = value; break;
  8182. case 4:
  8183. gc->triggerE = value; break;
  8184. case 5:
  8185. gc->triggerD = value; break;
  8186. case 6:
  8187. gc->nextTime = value; break;
  8188. case 7:
  8189. gc->payTime = value; break;
  8190. case 8:
  8191. gc->createTime = value; break;
  8192. case 9:
  8193. gc->visibleC = value; break;
  8194. case 10:
  8195. case 11:
  8196. case 12:
  8197. case 13:
  8198. case 14:
  8199. case 15:
  8200. case 16:
  8201. case 17:
  8202. gc->guardian[index-10].visible = value; break;
  8203. case 18:
  8204. case 19:
  8205. case 20:
  8206. case 21:
  8207. case 22:
  8208. case 23:
  8209. case 24:
  8210. case 25:
  8211. gc->guardian[index-18].hp = value;
  8212. if (gc->guardian[index-18].id)
  8213. { //Update this mob's HP.
  8214. struct block_list *bl = map_id2bl(gc->guardian[index-18].id);
  8215. if (!bl)
  8216. { //Wrong target?
  8217. gc->guardian[index-18].id = 0;
  8218. break;
  8219. }
  8220. if (value < 1) {
  8221. status_kill(bl);
  8222. break;
  8223. }
  8224. status_set_hp(bl, value, 0);
  8225. }
  8226. break;
  8227. default:
  8228. return 0;
  8229. }
  8230. guild_castledatasave(gc->castle_id,index,value);
  8231. }
  8232. return 0;
  8233. }
  8234. /* =====================================================================
  8235. * ギルド情報を要求する
  8236. * ---------------------------------------------------------------------*/
  8237. BUILDIN_FUNC(requestguildinfo)
  8238. {
  8239. int guild_id=script_getnum(st,2);
  8240. const char *event=NULL;
  8241. if( script_hasdata(st,3) ){
  8242. event=script_getstr(st,3);
  8243. check_event(st, event);
  8244. }
  8245. if(guild_id>0)
  8246. guild_npc_request_info(guild_id,event);
  8247. return 0;
  8248. }
  8249. /* =====================================================================
  8250. * カードの数を得る
  8251. * ---------------------------------------------------------------------*/
  8252. BUILDIN_FUNC(getequipcardcnt)
  8253. {
  8254. int i=-1,num;
  8255. TBL_PC *sd;
  8256. int c=MAX_SLOTS;
  8257. num=script_getnum(st,2);
  8258. sd=script_rid2sd(st);
  8259. if (num > 0 && num <= ARRAYLENGTH(equip))
  8260. i=pc_checkequip(sd,equip[num-1]);
  8261. if (i < 0) {
  8262. script_pushint(st,0);
  8263. return 0;
  8264. }
  8265. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8266. {
  8267. script_pushint(st,0);
  8268. return 0;
  8269. }
  8270. do{
  8271. if(sd->status.inventory[i].card[c-1] &&
  8272. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8273. script_pushint(st,(c));
  8274. return 0;
  8275. }
  8276. }while(c--);
  8277. script_pushint(st,0);
  8278. return 0;
  8279. }
  8280. /* ================================================================
  8281. * カード取り外し成功
  8282. * ----------------------------------------------------------------*/
  8283. BUILDIN_FUNC(successremovecards)
  8284. {
  8285. int i=-1,j,num,cardflag=0,flag;
  8286. TBL_PC *sd;
  8287. struct item item_tmp;
  8288. int c=MAX_SLOTS;
  8289. num=script_getnum(st,2);
  8290. sd=script_rid2sd(st);
  8291. if (num > 0 && num <= ARRAYLENGTH(equip))
  8292. i=pc_checkequip(sd,equip[num-1]);
  8293. if (i < 0) {
  8294. script_pushint(st,0);
  8295. return 0;
  8296. }
  8297. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8298. return 0;
  8299. do{
  8300. if(sd->status.inventory[i].card[c-1] &&
  8301. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8302. cardflag = 1;
  8303. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8304. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8305. item_tmp.attribute=0;
  8306. for (j = 0; j < MAX_SLOTS; j++)
  8307. item_tmp.card[j]=0;
  8308. //Logs items, got from (N)PC scripts [Lupus]
  8309. if(log_config.enable_logs&0x40)
  8310. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8311. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8312. clif_additem(sd,0,0,flag);
  8313. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8314. }
  8315. }
  8316. }while(c--);
  8317. if(cardflag == 1){ // カードを取り除いたアイテム所得
  8318. flag=0;
  8319. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8320. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8321. item_tmp.attribute=sd->status.inventory[i].attribute;
  8322. //Logs items, got from (N)PC scripts [Lupus]
  8323. if(log_config.enable_logs&0x40)
  8324. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8325. for (j = 0; j < MAX_SLOTS; j++)
  8326. item_tmp.card[j]=0;
  8327. pc_delitem(sd,i,1,0);
  8328. //Logs items, got from (N)PC scripts [Lupus]
  8329. if(log_config.enable_logs&0x40)
  8330. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8331. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8332. clif_additem(sd,0,0,flag);
  8333. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8334. }
  8335. clif_misceffect(&sd->bl,3);
  8336. return 0;
  8337. }
  8338. return 0;
  8339. }
  8340. /* ================================================================
  8341. * カード取り外し失敗 slot,type
  8342. * type=0: 両方損失、1:カード損失、2:武具損失、3:損失無し
  8343. * ----------------------------------------------------------------*/
  8344. BUILDIN_FUNC(failedremovecards)
  8345. {
  8346. int i=-1,j,num,cardflag=0,flag,typefail;
  8347. TBL_PC *sd;
  8348. struct item item_tmp;
  8349. int c=MAX_SLOTS;
  8350. num=script_getnum(st,2);
  8351. typefail=script_getnum(st,3);
  8352. sd=script_rid2sd(st);
  8353. if (num > 0 && num <= ARRAYLENGTH(equip))
  8354. i=pc_checkequip(sd,equip[num-1]);
  8355. if (i < 0) {
  8356. script_pushint(st,0);
  8357. return 0;
  8358. }
  8359. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8360. return 0;
  8361. do{
  8362. if(sd->status.inventory[i].card[c-1] &&
  8363. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8364. cardflag = 1;
  8365. if(typefail == 2){ // 武具のみ損失なら、カードは受け取らせる
  8366. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8367. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8368. item_tmp.attribute=0;
  8369. for (j = 0; j < MAX_SLOTS; j++)
  8370. item_tmp.card[j]=0;
  8371. //Logs items, got from (N)PC scripts [Lupus]
  8372. if(log_config.enable_logs&0x40)
  8373. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8374. if((flag=pc_additem(sd,&item_tmp,1))){
  8375. clif_additem(sd,0,0,flag);
  8376. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8377. }
  8378. }
  8379. }
  8380. }while(c--);
  8381. if(cardflag == 1){
  8382. if(typefail == 0 || typefail == 2){ // 武具損失
  8383. //Logs items, got from (N)PC scripts [Lupus]
  8384. if(log_config.enable_logs&0x40)
  8385. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8386. pc_delitem(sd,i,1,0);
  8387. clif_misceffect(&sd->bl,2);
  8388. return 0;
  8389. }
  8390. if(typefail == 1){ // カードのみ損失(武具を返す)
  8391. flag=0;
  8392. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8393. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8394. item_tmp.attribute=sd->status.inventory[i].attribute;
  8395. //Logs items, got from (N)PC scripts [Lupus]
  8396. if(log_config.enable_logs&0x40)
  8397. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8398. for (j = 0; j < MAX_SLOTS; j++)
  8399. item_tmp.card[j]=0;
  8400. pc_delitem(sd,i,1,0);
  8401. //Logs items, got from (N)PC scripts [Lupus]
  8402. if(log_config.enable_logs&0x40)
  8403. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8404. if((flag=pc_additem(sd,&item_tmp,1))){
  8405. clif_additem(sd,0,0,flag);
  8406. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8407. }
  8408. }
  8409. clif_misceffect(&sd->bl,2);
  8410. return 0;
  8411. }
  8412. return 0;
  8413. }
  8414. /* ================================================================
  8415. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8416. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8417. * improved by [Lance]
  8418. * ================================================================*/
  8419. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8420. {
  8421. int x,y,m,check_val=0,check_ID=0,i=0;
  8422. struct guild *g = NULL;
  8423. struct party_data *p = NULL;
  8424. const char *str;
  8425. const char *mapname;
  8426. unsigned int index;
  8427. mapname=script_getstr(st,2);
  8428. str=script_getstr(st,3);
  8429. x=script_getnum(st,4);
  8430. y=script_getnum(st,5);
  8431. if(script_hasdata(st,7)){
  8432. check_val=script_getnum(st,6);
  8433. check_ID=script_getnum(st,7);
  8434. }
  8435. if((m=map_mapname2mapid(mapname))< 0)
  8436. return 0;
  8437. if(!(index=mapindex_name2id(str)))
  8438. return 0;
  8439. switch(check_val){
  8440. case 1:
  8441. g = guild_search(check_ID);
  8442. if (g){
  8443. for( i=0; i < g->max_member; i++)
  8444. {
  8445. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8446. pc_setpos(g->member[i].sd,index,x,y,3);
  8447. }
  8448. }
  8449. }
  8450. break;
  8451. case 2:
  8452. p = party_search(check_ID);
  8453. if(p){
  8454. for(i=0;i<MAX_PARTY; i++){
  8455. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8456. pc_setpos(p->data[i].sd,index,x,y,3);
  8457. }
  8458. }
  8459. }
  8460. break;
  8461. default:
  8462. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8463. break;
  8464. }
  8465. return 0;
  8466. }
  8467. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8468. {
  8469. char *event=va_arg(ap,char *);
  8470. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  8471. return 1;
  8472. return 0;
  8473. }
  8474. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8475. {
  8476. const char *mapname,*event;
  8477. int m;
  8478. mapname=script_getstr(st,2);
  8479. event=script_getstr(st,3);
  8480. check_event(st, event);
  8481. if( (m=map_mapname2mapid(mapname))<0 ) {
  8482. script_pushint(st,-1);
  8483. return 0;
  8484. }
  8485. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8486. return 0;
  8487. }
  8488. BUILDIN_FUNC(marriage)
  8489. {
  8490. const char *partner=script_getstr(st,2);
  8491. TBL_PC *sd=script_rid2sd(st);
  8492. TBL_PC *p_sd=map_nick2sd(partner);
  8493. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8494. script_pushint(st,0);
  8495. return 0;
  8496. }
  8497. script_pushint(st,1);
  8498. return 0;
  8499. }
  8500. BUILDIN_FUNC(wedding_effect)
  8501. {
  8502. TBL_PC *sd=script_rid2sd(st);
  8503. struct block_list *bl;
  8504. if(sd==NULL) {
  8505. bl=map_id2bl(st->oid);
  8506. } else
  8507. bl=&sd->bl;
  8508. clif_wedding_effect(bl);
  8509. return 0;
  8510. }
  8511. BUILDIN_FUNC(divorce)
  8512. {
  8513. TBL_PC *sd=script_rid2sd(st);
  8514. if(sd==NULL || pc_divorce(sd) < 0){
  8515. script_pushint(st,0);
  8516. return 0;
  8517. }
  8518. script_pushint(st,1);
  8519. return 0;
  8520. }
  8521. BUILDIN_FUNC(ispartneron)
  8522. {
  8523. TBL_PC *sd=script_rid2sd(st);
  8524. TBL_PC *p_sd=NULL;
  8525. if(sd==NULL || !pc_ismarried(sd) ||
  8526. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8527. script_pushint(st,0);
  8528. return 0;
  8529. }
  8530. script_pushint(st,1);
  8531. return 0;
  8532. }
  8533. BUILDIN_FUNC(getpartnerid)
  8534. {
  8535. TBL_PC *sd=script_rid2sd(st);
  8536. if (sd == NULL) {
  8537. script_pushint(st,0);
  8538. return 0;
  8539. }
  8540. script_pushint(st,sd->status.partner_id);
  8541. return 0;
  8542. }
  8543. BUILDIN_FUNC(getchildid)
  8544. {
  8545. TBL_PC *sd=script_rid2sd(st);
  8546. if (sd == NULL) {
  8547. script_pushint(st,0);
  8548. return 0;
  8549. }
  8550. script_pushint(st,sd->status.child);
  8551. return 0;
  8552. }
  8553. BUILDIN_FUNC(getmotherid)
  8554. {
  8555. TBL_PC *sd=script_rid2sd(st);
  8556. if (sd == NULL) {
  8557. script_pushint(st,0);
  8558. return 0;
  8559. }
  8560. script_pushint(st,sd->status.mother);
  8561. return 0;
  8562. }
  8563. BUILDIN_FUNC(getfatherid)
  8564. {
  8565. TBL_PC *sd=script_rid2sd(st);
  8566. if (sd == NULL) {
  8567. script_pushint(st,0);
  8568. return 0;
  8569. }
  8570. script_pushint(st,sd->status.father);
  8571. return 0;
  8572. }
  8573. BUILDIN_FUNC(warppartner)
  8574. {
  8575. int x,y;
  8576. unsigned short mapindex;
  8577. const char *str;
  8578. TBL_PC *sd=script_rid2sd(st);
  8579. TBL_PC *p_sd=NULL;
  8580. if(sd==NULL || !pc_ismarried(sd) ||
  8581. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8582. script_pushint(st,0);
  8583. return 0;
  8584. }
  8585. str=script_getstr(st,2);
  8586. x=script_getnum(st,3);
  8587. y=script_getnum(st,4);
  8588. mapindex = mapindex_name2id(str);
  8589. if (mapindex) {
  8590. pc_setpos(p_sd,mapindex,x,y,0);
  8591. script_pushint(st,1);
  8592. } else
  8593. script_pushint(st,0);
  8594. return 0;
  8595. }
  8596. /*================================================
  8597. * Script for Displaying MOB Information [Valaris]
  8598. *------------------------------------------------*/
  8599. BUILDIN_FUNC(strmobinfo)
  8600. {
  8601. int num=script_getnum(st,2);
  8602. int class_=script_getnum(st,3);
  8603. if(!mobdb_checkid(class_))
  8604. {
  8605. script_pushint(st,0);
  8606. return 0;
  8607. }
  8608. switch (num) {
  8609. case 1: script_pushconststr(st,mob_db(class_)->name); break;
  8610. case 2: script_pushconststr(st,mob_db(class_)->jname); break;
  8611. case 3: script_pushint(st,mob_db(class_)->lv); break;
  8612. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  8613. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  8614. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  8615. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  8616. default:
  8617. script_pushint(st,0);
  8618. break;
  8619. }
  8620. return 0;
  8621. }
  8622. /*==========================================
  8623. * Summon guardians [Valaris]
  8624. * guardian "<map name>",<x>,<y>,"<name to show>",<mob id>,{,"<event label>"}{,<guardian index>};
  8625. *------------------------------------------*/
  8626. BUILDIN_FUNC(guardian)
  8627. {
  8628. int class_=0,x=0,y=0,guardian=0;
  8629. const char *str,*map,*evt="";
  8630. struct script_data *data;
  8631. map =script_getstr(st,2);
  8632. x =script_getnum(st,3);
  8633. y =script_getnum(st,4);
  8634. str =script_getstr(st,5);
  8635. class_=script_getnum(st,6);
  8636. if( script_hasdata(st,8) )
  8637. {// "<event label>",<guardian index>
  8638. evt=script_getstr(st,7);
  8639. guardian=script_getnum(st,8);
  8640. } else if( script_hasdata(st,7) ){
  8641. data=script_getdata(st,7);
  8642. get_val(st,data);
  8643. if( data_isstring(data) )
  8644. {// "<event label>"
  8645. evt=script_getstr(st,7);
  8646. } else if( data_isint(data) )
  8647. {// <guardian index>
  8648. guardian=script_getnum(st,7);
  8649. } else {
  8650. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  8651. script_reportdata(data);
  8652. return 1;
  8653. }
  8654. }
  8655. check_event(st, evt);
  8656. mob_spawn_guardian(map,x,y,str,class_,evt,guardian);
  8657. return 0;
  8658. }
  8659. /*================================================
  8660. * Script for Displaying Guardian Info [Valaris]
  8661. *------------------------------------------------*/
  8662. BUILDIN_FUNC(guardianinfo)
  8663. {
  8664. int guardian=script_getnum(st,2);
  8665. TBL_PC *sd=script_rid2sd(st);
  8666. struct guild_castle *gc=guild_mapname2gc(map[sd->bl.m].name);
  8667. if (guardian < 0 || guardian >= MAX_GUARDIANS || gc==NULL)
  8668. {
  8669. script_pushint(st,-1);
  8670. return 0;
  8671. }
  8672. if(gc->guardian[guardian].visible)
  8673. script_pushint(st,gc->guardian[guardian].hp);
  8674. else script_pushint(st,-1);
  8675. return 0;
  8676. }
  8677. /*==========================================
  8678. * IDからItem名
  8679. *------------------------------------------*/
  8680. BUILDIN_FUNC(getitemname)
  8681. {
  8682. int item_id=0;
  8683. struct item_data *i_data;
  8684. char *item_name;
  8685. struct script_data *data;
  8686. data=script_getdata(st,2);
  8687. get_val(st,data);
  8688. if( data_isstring(data) ){
  8689. const char *name=conv_str(st,data);
  8690. struct item_data *item_data = itemdb_searchname(name);
  8691. if( item_data )
  8692. item_id=item_data->nameid;
  8693. }else
  8694. item_id=conv_num(st,data);
  8695. i_data = itemdb_exists(item_id);
  8696. if (i_data == NULL)
  8697. {
  8698. script_pushconststr(st,"null");
  8699. return 0;
  8700. }
  8701. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  8702. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  8703. script_pushstr(st,item_name);
  8704. return 0;
  8705. }
  8706. /*==========================================
  8707. * Returns number of slots an item has. [Skotlex]
  8708. *------------------------------------------*/
  8709. BUILDIN_FUNC(getitemslots)
  8710. {
  8711. int item_id;
  8712. struct item_data *i_data;
  8713. item_id=script_getnum(st,2);
  8714. i_data = itemdb_exists(item_id);
  8715. if (i_data)
  8716. script_pushint(st,i_data->slot);
  8717. else
  8718. script_pushint(st,-1);
  8719. return 0;
  8720. }
  8721. /*==========================================
  8722. * Returns some values of an item [Lupus]
  8723. * Price, Weight, etc...
  8724. getiteminfo(itemID,n), where n
  8725. 0 value_buy;
  8726. 1 value_sell;
  8727. 2 type;
  8728. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8729. if = 0, then monsters don't drop it at all (rare or a quest item)
  8730. if = 10000, then this item is sold in NPC shops only
  8731. 4 sex;
  8732. 5 equip;
  8733. 6 weight;
  8734. 7 atk;
  8735. 8 def;
  8736. 9 range;
  8737. 10 slot;
  8738. 11 look;
  8739. 12 elv;
  8740. 13 wlv;
  8741. 14 view id
  8742. *------------------------------------------*/
  8743. BUILDIN_FUNC(getiteminfo)
  8744. {
  8745. int item_id,n;
  8746. int *item_arr;
  8747. struct item_data *i_data;
  8748. item_id = script_getnum(st,2);
  8749. n = script_getnum(st,3);
  8750. i_data = itemdb_exists(item_id);
  8751. if (i_data && n>=0 && n<14) {
  8752. item_arr = (int*)&i_data->value_buy;
  8753. script_pushint(st,item_arr[n]);
  8754. } else
  8755. script_pushint(st,-1);
  8756. return 0;
  8757. }
  8758. /*==========================================
  8759. * Set some values of an item [Lupus]
  8760. * Price, Weight, etc...
  8761. setiteminfo(itemID,n,Value), where n
  8762. 0 value_buy;
  8763. 1 value_sell;
  8764. 2 type;
  8765. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8766. if = 0, then monsters don't drop it at all (rare or a quest item)
  8767. if = 10000, then this item is sold in NPC shops only
  8768. 4 sex;
  8769. 5 equip;
  8770. 6 weight;
  8771. 7 atk;
  8772. 8 def;
  8773. 9 range;
  8774. 10 slot;
  8775. 11 look;
  8776. 12 elv;
  8777. 13 wlv;
  8778. 14 view id
  8779. * Returns Value or -1 if the wrong field's been set
  8780. *------------------------------------------*/
  8781. BUILDIN_FUNC(setiteminfo)
  8782. {
  8783. int item_id,n,value;
  8784. int *item_arr;
  8785. struct item_data *i_data;
  8786. item_id = script_getnum(st,2);
  8787. n = script_getnum(st,3);
  8788. value = script_getnum(st,4);
  8789. i_data = itemdb_exists(item_id);
  8790. if (i_data && n>=0 && n<14) {
  8791. item_arr = (int*)&i_data->value_buy;
  8792. item_arr[n] = value;
  8793. script_pushint(st,value);
  8794. } else
  8795. script_pushint(st,-1);
  8796. return 0;
  8797. }
  8798. /*==========================================
  8799. * Returns value from equipped item slot n [Lupus]
  8800. getequipcardid(num,slot)
  8801. where
  8802. num = eqip position slot
  8803. slot = 0,1,2,3 (Card Slot N)
  8804. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  8805. it's useful when you want to check item cards or if it's signed
  8806. Useful for such quests as "Sign this refined item with players name" etc
  8807. Hat[0] +4 -> Player's Hat[0] +4
  8808. *------------------------------------------*/
  8809. BUILDIN_FUNC(getequipcardid)
  8810. {
  8811. int i=-1,num,slot;
  8812. TBL_PC *sd;
  8813. num=script_getnum(st,2);
  8814. slot=script_getnum(st,3);
  8815. sd=script_rid2sd(st);
  8816. if (num > 0 && num <= ARRAYLENGTH(equip))
  8817. i=pc_checkequip(sd,equip[num-1]);
  8818. if(i >= 0 && slot>=0 && slot<4)
  8819. script_pushint(st,sd->status.inventory[i].card[slot]);
  8820. else
  8821. script_pushint(st,0);
  8822. return 0;
  8823. }
  8824. /*==========================================
  8825. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  8826. *------------------------------------------*/
  8827. BUILDIN_FUNC(petskillbonus)
  8828. {
  8829. struct pet_data *pd;
  8830. TBL_PC *sd=script_rid2sd(st);
  8831. if(sd==NULL || sd->pd==NULL)
  8832. return 0;
  8833. pd=sd->pd;
  8834. if (pd->bonus)
  8835. { //Clear previous bonus
  8836. if (pd->bonus->timer != -1)
  8837. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  8838. } else //init
  8839. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  8840. pd->bonus->type=script_getnum(st,2);
  8841. pd->bonus->val=script_getnum(st,3);
  8842. pd->bonus->duration=script_getnum(st,4);
  8843. pd->bonus->delay=script_getnum(st,5);
  8844. if (pd->state.skillbonus == 1)
  8845. pd->state.skillbonus=0; // waiting state
  8846. // wait for timer to start
  8847. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  8848. pd->bonus->timer=-1;
  8849. else
  8850. pd->bonus->timer=add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  8851. return 0;
  8852. }
  8853. /*==========================================
  8854. * pet looting [Valaris] //Rewritten by [Skotlex]
  8855. *------------------------------------------*/
  8856. BUILDIN_FUNC(petloot)
  8857. {
  8858. int max;
  8859. struct pet_data *pd;
  8860. TBL_PC *sd=script_rid2sd(st);
  8861. if(sd==NULL || sd->pd==NULL)
  8862. return 0;
  8863. max=script_getnum(st,2);
  8864. if(max < 1)
  8865. max = 1; //Let'em loot at least 1 item.
  8866. else if (max > MAX_PETLOOT_SIZE)
  8867. max = MAX_PETLOOT_SIZE;
  8868. pd = sd->pd;
  8869. if (pd->loot != NULL)
  8870. { //Release whatever was there already and reallocate memory
  8871. pet_lootitem_drop(pd, pd->msd);
  8872. aFree(pd->loot->item);
  8873. }
  8874. else
  8875. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  8876. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  8877. pd->loot->max=max;
  8878. pd->loot->count = 0;
  8879. pd->loot->weight = 0;
  8880. return 0;
  8881. }
  8882. /*==========================================
  8883. * PCの所持品情報読み取り
  8884. *------------------------------------------*/
  8885. BUILDIN_FUNC(getinventorylist)
  8886. {
  8887. TBL_PC *sd=script_rid2sd(st);
  8888. char card_var[NAME_LENGTH];
  8889. int i,j=0,k;
  8890. if(!sd) return 0;
  8891. for(i=0;i<MAX_INVENTORY;i++){
  8892. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  8893. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  8894. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  8895. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  8896. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  8897. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  8898. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  8899. for (k = 0; k < MAX_SLOTS; k++)
  8900. {
  8901. sprintf(card_var, "@inventorylist_card%d",k+1);
  8902. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  8903. }
  8904. j++;
  8905. }
  8906. }
  8907. pc_setreg(sd,add_str("@inventorylist_count"),j);
  8908. return 0;
  8909. }
  8910. BUILDIN_FUNC(getskilllist)
  8911. {
  8912. TBL_PC *sd=script_rid2sd(st);
  8913. int i,j=0;
  8914. if(!sd) return 0;
  8915. for(i=0;i<MAX_SKILL;i++){
  8916. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  8917. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  8918. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  8919. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  8920. j++;
  8921. }
  8922. }
  8923. pc_setreg(sd,add_str("@skilllist_count"),j);
  8924. return 0;
  8925. }
  8926. BUILDIN_FUNC(clearitem)
  8927. {
  8928. TBL_PC *sd=script_rid2sd(st);
  8929. int i;
  8930. if(sd==NULL) return 0;
  8931. for (i=0; i<MAX_INVENTORY; i++) {
  8932. if (sd->status.inventory[i].amount) {
  8933. //Logs items, got from (N)PC scripts [Lupus]
  8934. if(log_config.enable_logs&0x40)
  8935. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  8936. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  8937. }
  8938. }
  8939. return 0;
  8940. }
  8941. /*==========================================
  8942. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  8943. *------------------------------------------*/
  8944. BUILDIN_FUNC(disguise)
  8945. {
  8946. int id;
  8947. TBL_PC* sd = script_rid2sd(st);
  8948. if (sd == NULL) return 0;
  8949. id = script_getnum(st,2);
  8950. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  8951. pc_disguise(sd, id);
  8952. script_pushint(st,id);
  8953. } else
  8954. script_pushint(st,0);
  8955. return 0;
  8956. }
  8957. /*==========================================
  8958. * Undisguise Player (returns 1 if success, 0 on fail)
  8959. *------------------------------------------*/
  8960. BUILDIN_FUNC(undisguise)
  8961. {
  8962. TBL_PC* sd = script_rid2sd(st);
  8963. if (sd == NULL) return 0;
  8964. if (sd->disguise) {
  8965. pc_disguise(sd, 0);
  8966. script_pushint(st,0);
  8967. } else {
  8968. script_pushint(st,1);
  8969. }
  8970. return 0;
  8971. }
  8972. /*==========================================
  8973. * NPCクラスチェンジ
  8974. * classは変わりたいclass
  8975. * typeは通常0なのかな?
  8976. *------------------------------------------*/
  8977. BUILDIN_FUNC(classchange)
  8978. {
  8979. int _class,type;
  8980. struct block_list *bl=map_id2bl(st->oid);
  8981. if(bl==NULL) return 0;
  8982. _class=script_getnum(st,2);
  8983. type=script_getnum(st,3);
  8984. clif_class_change(bl,_class,type);
  8985. return 0;
  8986. }
  8987. /*==========================================
  8988. * NPCから発生するエフェクト
  8989. *------------------------------------------*/
  8990. BUILDIN_FUNC(misceffect)
  8991. {
  8992. int type;
  8993. type=script_getnum(st,2);
  8994. if(st->oid && st->oid != fake_nd->bl.id) {
  8995. struct block_list *bl = map_id2bl(st->oid);
  8996. if (bl)
  8997. clif_misceffect2(bl,type);
  8998. } else{
  8999. TBL_PC *sd=script_rid2sd(st);
  9000. if(sd)
  9001. clif_misceffect2(&sd->bl,type);
  9002. }
  9003. return 0;
  9004. }
  9005. /*==========================================
  9006. * サウンドエフェクト
  9007. *------------------------------------------*/
  9008. BUILDIN_FUNC(soundeffect)
  9009. {
  9010. TBL_PC* sd = script_rid2sd(st);
  9011. const char* name = script_getstr(st,2);
  9012. int type = script_getnum(st,3);
  9013. if(sd)
  9014. {
  9015. if(!st->rid)
  9016. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9017. else
  9018. clif_soundeffect(sd,&sd->bl,name,type);
  9019. }
  9020. return 0;
  9021. }
  9022. int soundeffect_sub(struct block_list* bl,va_list ap)
  9023. {
  9024. char* name = va_arg(ap,char*);
  9025. int type = va_arg(ap,int);
  9026. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9027. return 0;
  9028. }
  9029. /*==========================================
  9030. * Play a sound effect (.wav) on multiple clients
  9031. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9032. *------------------------------------------*/
  9033. BUILDIN_FUNC(soundeffectall)
  9034. {
  9035. struct block_list* bl;
  9036. const char* name;
  9037. int type;
  9038. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9039. if (!bl)
  9040. return 0;
  9041. name = script_getstr(st,2);
  9042. type = script_getnum(st,3);
  9043. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9044. if(!script_hasdata(st,4))
  9045. { // area around
  9046. clif_soundeffectall(bl, name, type, AREA);
  9047. }
  9048. else
  9049. if(!script_hasdata(st,5))
  9050. { // entire map
  9051. const char* map = script_getstr(st,4);
  9052. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9053. }
  9054. else
  9055. if(script_hasdata(st,9))
  9056. { // specified part of map
  9057. const char* map = script_getstr(st,5);
  9058. int x0 = script_getnum(st,6);
  9059. int y0 = script_getnum(st,7);
  9060. int x1 = script_getnum(st,8);
  9061. int y1 = script_getnum(st,9);
  9062. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9063. }
  9064. else
  9065. {
  9066. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9067. }
  9068. return 0;
  9069. }
  9070. /*==========================================
  9071. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9072. *------------------------------------------*/
  9073. BUILDIN_FUNC(petrecovery)
  9074. {
  9075. struct pet_data *pd;
  9076. TBL_PC *sd=script_rid2sd(st);
  9077. if(sd==NULL || sd->pd==NULL)
  9078. return 0;
  9079. pd=sd->pd;
  9080. if (pd->recovery)
  9081. { //Halt previous bonus
  9082. if (pd->recovery->timer != -1)
  9083. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9084. } else //Init
  9085. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9086. pd->recovery->type=script_getnum(st,2);
  9087. pd->recovery->delay=script_getnum(st,3);
  9088. pd->recovery->timer=-1;
  9089. return 0;
  9090. }
  9091. /*==========================================
  9092. * pet healing [Valaris] //Rewritten by [Skotlex]
  9093. *------------------------------------------*/
  9094. BUILDIN_FUNC(petheal)
  9095. {
  9096. struct pet_data *pd;
  9097. TBL_PC *sd=script_rid2sd(st);
  9098. if(sd==NULL || sd->pd==NULL)
  9099. return 0;
  9100. pd=sd->pd;
  9101. if (pd->s_skill)
  9102. { //Clear previous skill
  9103. if (pd->s_skill->timer != -1)
  9104. {
  9105. if (pd->s_skill->id)
  9106. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9107. else
  9108. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9109. }
  9110. } else //init memory
  9111. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9112. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9113. //Use the lv as the amount to heal
  9114. pd->s_skill->lv=script_getnum(st,2);
  9115. pd->s_skill->delay=script_getnum(st,3);
  9116. pd->s_skill->hp=script_getnum(st,4);
  9117. pd->s_skill->sp=script_getnum(st,5);
  9118. //Use delay as initial offset to avoid skill/heal exploits
  9119. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9120. pd->s_skill->timer=-1;
  9121. else
  9122. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9123. return 0;
  9124. }
  9125. /*==========================================
  9126. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9127. *------------------------------------------*/
  9128. BUILDIN_FUNC(petskillattack)
  9129. {
  9130. struct pet_data *pd;
  9131. TBL_PC *sd=script_rid2sd(st);
  9132. if(sd==NULL || sd->pd==NULL)
  9133. return 0;
  9134. pd=sd->pd;
  9135. if (pd->a_skill == NULL)
  9136. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9137. pd->a_skill->id=script_getnum(st,2);
  9138. pd->a_skill->lv=script_getnum(st,3);
  9139. pd->a_skill->div_ = 0;
  9140. pd->a_skill->rate=script_getnum(st,4);
  9141. pd->a_skill->bonusrate=script_getnum(st,5);
  9142. return 0;
  9143. }
  9144. /*==========================================
  9145. * pet attack skills [Valaris]
  9146. *------------------------------------------*/
  9147. BUILDIN_FUNC(petskillattack2)
  9148. {
  9149. struct pet_data *pd;
  9150. TBL_PC *sd=script_rid2sd(st);
  9151. if(sd==NULL || sd->pd==NULL)
  9152. return 0;
  9153. pd=sd->pd;
  9154. if (pd->a_skill == NULL)
  9155. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9156. pd->a_skill->id=script_getnum(st,2);
  9157. pd->a_skill->lv=script_getnum(st,3);
  9158. pd->a_skill->div_ = script_getnum(st,4);
  9159. pd->a_skill->rate=script_getnum(st,5);
  9160. pd->a_skill->bonusrate=script_getnum(st,6);
  9161. return 0;
  9162. }
  9163. /*==========================================
  9164. * pet support skills [Skotlex]
  9165. *------------------------------------------*/
  9166. BUILDIN_FUNC(petskillsupport)
  9167. {
  9168. struct pet_data *pd;
  9169. TBL_PC *sd=script_rid2sd(st);
  9170. if(sd==NULL || sd->pd==NULL)
  9171. return 0;
  9172. pd=sd->pd;
  9173. if (pd->s_skill)
  9174. { //Clear previous skill
  9175. if (pd->s_skill->timer != -1)
  9176. {
  9177. if (pd->s_skill->id)
  9178. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9179. else
  9180. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9181. }
  9182. } else //init memory
  9183. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9184. pd->s_skill->id=script_getnum(st,2);
  9185. pd->s_skill->lv=script_getnum(st,3);
  9186. pd->s_skill->delay=script_getnum(st,4);
  9187. pd->s_skill->hp=script_getnum(st,5);
  9188. pd->s_skill->sp=script_getnum(st,6);
  9189. //Use delay as initial offset to avoid skill/heal exploits
  9190. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9191. pd->s_skill->timer=-1;
  9192. else
  9193. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9194. return 0;
  9195. }
  9196. /*==========================================
  9197. * Scripted skill effects [Celest]
  9198. *------------------------------------------*/
  9199. BUILDIN_FUNC(skilleffect)
  9200. {
  9201. TBL_PC *sd;
  9202. int skillid=script_getnum(st,2);
  9203. int skilllv=script_getnum(st,3);
  9204. sd=script_rid2sd(st);
  9205. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9206. return 0;
  9207. }
  9208. /*==========================================
  9209. * NPC skill effects [Valaris]
  9210. *------------------------------------------*/
  9211. BUILDIN_FUNC(npcskilleffect)
  9212. {
  9213. struct block_list *bl= map_id2bl(st->oid);
  9214. int skillid=script_getnum(st,2);
  9215. int skilllv=script_getnum(st,3);
  9216. int x=script_getnum(st,4);
  9217. int y=script_getnum(st,5);
  9218. if (bl)
  9219. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9220. return 0;
  9221. }
  9222. /*==========================================
  9223. * Special effects [Valaris]
  9224. *------------------------------------------*/
  9225. BUILDIN_FUNC(specialeffect)
  9226. {
  9227. struct block_list *bl=map_id2bl(st->oid);
  9228. if(bl==NULL)
  9229. return 0;
  9230. clif_specialeffect(bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9231. return 0;
  9232. }
  9233. BUILDIN_FUNC(specialeffect2)
  9234. {
  9235. TBL_PC *sd=script_rid2sd(st);
  9236. if(sd==NULL)
  9237. return 0;
  9238. clif_specialeffect(&sd->bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9239. return 0;
  9240. }
  9241. /*==========================================
  9242. * Nude [Valaris]
  9243. *------------------------------------------*/
  9244. BUILDIN_FUNC(nude)
  9245. {
  9246. TBL_PC *sd=script_rid2sd(st);
  9247. int i,calcflag=0;
  9248. if(sd==NULL)
  9249. return 0;
  9250. for(i=0;i<11;i++)
  9251. if(sd->equip_index[i] >= 0) {
  9252. if(!calcflag)
  9253. calcflag=1;
  9254. pc_unequipitem(sd,sd->equip_index[i],2);
  9255. }
  9256. if(calcflag)
  9257. status_calc_pc(sd,0);
  9258. return 0;
  9259. }
  9260. /*==========================================
  9261. * gmcommand [MouseJstr]
  9262. *
  9263. * suggested on the forums...
  9264. * splitted into atcommand & charcommand by [Skotlex]
  9265. *------------------------------------------*/
  9266. BUILDIN_FUNC(atcommand)
  9267. {
  9268. TBL_PC dummy_sd;
  9269. TBL_PC* sd;
  9270. int fd;
  9271. const char* cmd;
  9272. cmd = script_getstr(st,2);
  9273. if (st->rid) {
  9274. sd = script_rid2sd(st);
  9275. fd = sd->fd;
  9276. } else { //Use a dummy character.
  9277. sd = &dummy_sd;
  9278. fd = 0;
  9279. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9280. if (st->oid)
  9281. {
  9282. struct block_list* bl = map_id2bl(st->oid);
  9283. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9284. if (bl->type == BL_NPC)
  9285. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9286. }
  9287. }
  9288. // compatibility with previous implementation (deprecated!)
  9289. if(cmd[0] != atcommand_symbol)
  9290. {
  9291. cmd += strlen(sd->status.name);
  9292. while(*cmd != atcommand_symbol && *cmd != 0)
  9293. cmd++;
  9294. }
  9295. is_atcommand_sub(fd, sd, cmd, 99);
  9296. return 0;
  9297. }
  9298. BUILDIN_FUNC(charcommand)
  9299. {
  9300. TBL_PC dummy_sd;
  9301. TBL_PC* sd;
  9302. int fd;
  9303. const char* cmd;
  9304. cmd = script_getstr(st,2);
  9305. if (st->rid) {
  9306. sd = script_rid2sd(st);
  9307. fd = sd->fd;
  9308. } else { //Use a dummy character.
  9309. sd = &dummy_sd;
  9310. fd = 0;
  9311. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9312. if (st->oid)
  9313. {
  9314. struct block_list* bl = map_id2bl(st->oid);
  9315. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9316. if (bl->type == BL_NPC)
  9317. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9318. }
  9319. }
  9320. // compatibility with previous implementation (deprecated!)
  9321. if(cmd[0] != charcommand_symbol)
  9322. {
  9323. cmd += strlen(sd->status.name);
  9324. while(*cmd != charcommand_symbol && *cmd != 0)
  9325. cmd++;
  9326. }
  9327. is_charcommand_sub(fd, sd, cmd, 99);
  9328. return 0;
  9329. }
  9330. /*==========================================
  9331. * Displays a message for the player only (like system messages like "you got an apple" )
  9332. *------------------------------------------*/
  9333. BUILDIN_FUNC(dispbottom)
  9334. {
  9335. TBL_PC *sd=script_rid2sd(st);
  9336. const char *message;
  9337. message=script_getstr(st,2);
  9338. if(sd)
  9339. clif_disp_onlyself(sd,message,(int)strlen(message));
  9340. return 0;
  9341. }
  9342. /*==========================================
  9343. * All The Players Full Recovery
  9344. * (HP/SP full restore and resurrect if need)
  9345. *------------------------------------------*/
  9346. BUILDIN_FUNC(recovery)
  9347. {
  9348. TBL_PC *sd, **all_sd;
  9349. int i = 0, users;
  9350. all_sd = map_getallusers(&users);
  9351. for (i = 0; i < users; i++)
  9352. {
  9353. sd = all_sd[i];
  9354. if(pc_isdead(sd))
  9355. status_revive(&sd->bl, 100, 100);
  9356. else
  9357. status_percent_heal(&sd->bl, 100, 100);
  9358. clif_displaymessage(sd->fd,"You have been recovered!");
  9359. }
  9360. return 0;
  9361. }
  9362. /*==========================================
  9363. * Get your pet info: getpetinfo(n)
  9364. * n -> 0:pet_id 1:pet_class 2:pet_name
  9365. * 3:friendly 4:hungry, 5: rename flag.
  9366. *------------------------------------------*/
  9367. BUILDIN_FUNC(getpetinfo)
  9368. {
  9369. TBL_PC *sd=script_rid2sd(st);
  9370. TBL_PET *pd;
  9371. int type=script_getnum(st,2);
  9372. if(!sd || !sd->pd) {
  9373. if (type == 2)
  9374. script_pushconststr(st,"null");
  9375. else
  9376. script_pushint(st,0);
  9377. return 0;
  9378. }
  9379. pd = sd->pd;
  9380. switch(type){
  9381. case 0: script_pushint(st,pd->pet.pet_id); break;
  9382. case 1: script_pushint(st,pd->pet.class_); break;
  9383. case 2: script_pushstr(st,aStrdup(pd->pet.name)); break;
  9384. case 3: script_pushint(st,pd->pet.intimate); break;
  9385. case 4: script_pushint(st,pd->pet.hungry); break;
  9386. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9387. default:
  9388. script_pushint(st,0);
  9389. break;
  9390. }
  9391. return 0;
  9392. }
  9393. /*==========================================
  9394. * Get your homunculus info: gethominfo(n)
  9395. * n -> 0:hom_id 1:class 2:name
  9396. * 3:friendly 4:hungry, 5: rename flag.
  9397. * 6: level
  9398. *------------------------------------------*/
  9399. BUILDIN_FUNC(gethominfo)
  9400. {
  9401. TBL_PC *sd=script_rid2sd(st);
  9402. TBL_HOM *hd;
  9403. int type=script_getnum(st,2);
  9404. hd = sd?sd->hd:NULL;
  9405. if(!merc_is_hom_active(hd))
  9406. {
  9407. if (type == 2)
  9408. script_pushconststr(st,"null");
  9409. else
  9410. script_pushint(st,0);
  9411. return 0;
  9412. }
  9413. switch(type){
  9414. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9415. case 1: script_pushint(st,hd->homunculus.class_); break;
  9416. case 2: script_pushstr(st,aStrdup(hd->homunculus.name)); break;
  9417. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9418. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9419. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9420. case 6: script_pushint(st,hd->homunculus.level); break;
  9421. default:
  9422. script_pushint(st,0);
  9423. break;
  9424. }
  9425. return 0;
  9426. }
  9427. /*==========================================
  9428. * Shows wether your inventory(and equips) contain
  9429. selected card or not.
  9430. checkequipedcard(4001);
  9431. *------------------------------------------*/
  9432. BUILDIN_FUNC(checkequipedcard)
  9433. {
  9434. TBL_PC *sd=script_rid2sd(st);
  9435. int n,i,c=0;
  9436. c=script_getnum(st,2);
  9437. if(sd){
  9438. for(i=0;i<MAX_INVENTORY;i++){
  9439. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9440. for(n=0;n<MAX_SLOTS;n++){
  9441. if(sd->status.inventory[i].card[n]==c){
  9442. script_pushint(st,1);
  9443. return 0;
  9444. }
  9445. }
  9446. }
  9447. }
  9448. }
  9449. script_pushint(st,0);
  9450. return 0;
  9451. }
  9452. BUILDIN_FUNC(jump_zero)
  9453. {
  9454. int sel;
  9455. sel=script_getnum(st,2);
  9456. if(!sel) {
  9457. int pos;
  9458. if( !data_islabel(script_getdata(st,3)) ){
  9459. ShowError("script: jump_zero: not label !\n");
  9460. st->state=END;
  9461. return 1;
  9462. }
  9463. pos=script_getnum(st,3);
  9464. st->pos=pos;
  9465. st->state=GOTO;
  9466. // printf("script: jump_zero: jumpto : %d\n",pos);
  9467. } else {
  9468. // printf("script: jump_zero: fail\n");
  9469. }
  9470. return 0;
  9471. }
  9472. /*==========================================
  9473. * GetMapMobs
  9474. returns mob counts on a set map:
  9475. e.g. GetMapMobs("prontera")
  9476. use "this" - for player's map
  9477. *------------------------------------------*/
  9478. BUILDIN_FUNC(getmapmobs)
  9479. {
  9480. const char *str=NULL;
  9481. int m=-1,bx,by,i;
  9482. int count=0,c;
  9483. struct block_list *bl;
  9484. str=script_getstr(st,2);
  9485. if(strcmp(str,"this")==0){
  9486. TBL_PC *sd=script_rid2sd(st);
  9487. if(sd)
  9488. m=sd->bl.m;
  9489. else{
  9490. script_pushint(st,-1);
  9491. return 0;
  9492. }
  9493. }else
  9494. m=map_mapname2mapid(str);
  9495. if(m < 0){
  9496. script_pushint(st,-1);
  9497. return 0;
  9498. }
  9499. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++){
  9500. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++){
  9501. bl = map[m].block_mob[bx+by*map[m].bxs];
  9502. c = map[m].block_mob_count[bx+by*map[m].bxs];
  9503. for(i=0;i<c && bl;i++,bl=bl->next){
  9504. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9505. count++;
  9506. }
  9507. }
  9508. }
  9509. script_pushint(st,count);
  9510. return 0;
  9511. }
  9512. /*==========================================
  9513. * movenpc [MouseJstr]
  9514. *------------------------------------------*/
  9515. BUILDIN_FUNC(movenpc)
  9516. {
  9517. TBL_NPC *nd = NULL;
  9518. const char *npc;
  9519. int x,y;
  9520. npc = script_getstr(st,2);
  9521. x = script_getnum(st,3);
  9522. y = script_getnum(st,4);
  9523. if ((nd = npc_name2id(npc)) == NULL)
  9524. return -1;
  9525. npc_movenpc(nd, x, y);
  9526. return 0;
  9527. }
  9528. /*==========================================
  9529. * message [MouseJstr]
  9530. *------------------------------------------*/
  9531. BUILDIN_FUNC(message)
  9532. {
  9533. TBL_PC *sd;
  9534. const char *msg,*player;
  9535. TBL_PC *pl_sd = NULL;
  9536. sd = script_rid2sd(st);
  9537. player = script_getstr(st,2);
  9538. msg = script_getstr(st,3);
  9539. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9540. return 0;
  9541. clif_displaymessage(pl_sd->fd, msg);
  9542. return 0;
  9543. }
  9544. /*==========================================
  9545. * npctalk (sends message to surrounding area)
  9546. *------------------------------------------*/
  9547. BUILDIN_FUNC(npctalk)
  9548. {
  9549. const char* str;
  9550. char message[255];
  9551. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  9552. str = script_getstr(st,2);
  9553. if(nd) {
  9554. memcpy(message, nd->name, NAME_LENGTH);
  9555. strtok(message, "#"); // discard extra name identifier if present
  9556. strcat(message, " : ");
  9557. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  9558. clif_message(&(nd->bl), message);
  9559. }
  9560. return 0;
  9561. }
  9562. // change npc walkspeed [Valaris]
  9563. BUILDIN_FUNC(npcspeed)
  9564. {
  9565. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9566. int x=0;
  9567. x=script_getnum(st,2);
  9568. if(nd) {
  9569. nd->speed=x;
  9570. }
  9571. return 0;
  9572. }
  9573. // make an npc walk to a position [Valaris]
  9574. BUILDIN_FUNC(npcwalkto)
  9575. {
  9576. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9577. int x=0,y=0;
  9578. x=script_getnum(st,2);
  9579. y=script_getnum(st,3);
  9580. if(nd) {
  9581. unit_walktoxy(&nd->bl,x,y,0);
  9582. }
  9583. return 0;
  9584. }
  9585. // stop an npc's movement [Valaris]
  9586. BUILDIN_FUNC(npcstop)
  9587. {
  9588. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9589. if(nd) {
  9590. unit_stop_walking(&nd->bl,1);
  9591. }
  9592. return 0;
  9593. }
  9594. /*==========================================
  9595. * getlook char info. getlook(arg)
  9596. *------------------------------------------*/
  9597. BUILDIN_FUNC(getlook)
  9598. {
  9599. int type,val;
  9600. TBL_PC *sd;
  9601. sd=script_rid2sd(st);
  9602. type=script_getnum(st,2);
  9603. val=-1;
  9604. switch(type) {
  9605. case LOOK_HAIR: val=sd->status.hair; break; //1
  9606. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  9607. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  9608. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  9609. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  9610. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  9611. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  9612. case LOOK_SHIELD: val=sd->status.shield; break; //8
  9613. case LOOK_SHOES: break; //9
  9614. }
  9615. script_pushint(st,val);
  9616. return 0;
  9617. }
  9618. /*==========================================
  9619. * get char save point. argument: 0- map name, 1- x, 2- y
  9620. *------------------------------------------*/
  9621. BUILDIN_FUNC(getsavepoint)
  9622. {
  9623. TBL_PC* sd;
  9624. int type;
  9625. sd = script_rid2sd(st);
  9626. if (sd == NULL) {
  9627. script_pushint(st,0);
  9628. return 0;
  9629. }
  9630. type = script_getnum(st,2);
  9631. switch(type) {
  9632. case 0: script_pushstr(st,aStrdup(mapindex_id2name(sd->status.save_point.map))); break;
  9633. case 1: script_pushint(st,sd->status.save_point.x); break;
  9634. case 2: script_pushint(st,sd->status.save_point.y); break;
  9635. default:
  9636. script_pushint(st,0);
  9637. break;
  9638. }
  9639. return 0;
  9640. }
  9641. /*==========================================
  9642. * Get position for char/npc/pet/mob objects. Added by Lorky
  9643. *
  9644. * int getMapXY(MapName$,MaxX,MapY,type,[CharName$]);
  9645. * where type:
  9646. * MapName$ - String variable for output map name
  9647. * MapX - Integer variable for output coord X
  9648. * MapY - Integer variable for output coord Y
  9649. * type - type of object
  9650. * 0 - Character coord
  9651. * 1 - NPC coord
  9652. * 2 - Pet coord
  9653. * 3 - Mob coord (not released)
  9654. * 4 - Homun coord
  9655. * CharName$ - Name object. If miss or "this" the current object
  9656. *
  9657. * Return:
  9658. * 0 - success
  9659. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  9660. *------------------------------------------*/
  9661. BUILDIN_FUNC(getmapxy)
  9662. {
  9663. struct block_list *bl = NULL;
  9664. TBL_PC *sd=NULL;
  9665. int num;
  9666. char *name;
  9667. char prefix;
  9668. int x,y,type;
  9669. char mapname[MAP_NAME_LENGTH];
  9670. memset(mapname, 0, sizeof(mapname));
  9671. if( !data_isreference(script_getdata(st,2)) ){
  9672. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  9673. script_pushint(st,-1);
  9674. return 1;
  9675. }
  9676. if( !data_isreference(script_getdata(st,3)) ){
  9677. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  9678. script_pushint(st,-1);
  9679. return 1;
  9680. }
  9681. if( !data_isreference(script_getdata(st,4)) ){
  9682. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  9683. script_pushint(st,-1);
  9684. return 1;
  9685. }
  9686. // Possible needly check function parameters on C_STR,C_INT,C_INT
  9687. type=script_getnum(st,5);
  9688. switch (type){
  9689. case 0: //Get Character Position
  9690. if( script_hasdata(st,6) )
  9691. sd=map_nick2sd(script_getstr(st,6));
  9692. else
  9693. sd=script_rid2sd(st);
  9694. if (sd)
  9695. bl = &sd->bl;
  9696. break;
  9697. case 1: //Get NPC Position
  9698. if( script_hasdata(st,6) )
  9699. {
  9700. struct npc_data *nd;
  9701. nd=npc_name2id(script_getstr(st,6));
  9702. if (nd)
  9703. bl = &nd->bl;
  9704. } else //In case the origin is not an npc?
  9705. bl=map_id2bl(st->oid);
  9706. break;
  9707. case 2: //Get Pet Position
  9708. if(script_hasdata(st,6))
  9709. sd=map_nick2sd(script_getstr(st,6));
  9710. else
  9711. sd=script_rid2sd(st);
  9712. if (sd && sd->pd)
  9713. bl = &sd->pd->bl;
  9714. break;
  9715. case 3: //Get Mob Position
  9716. break; //Not supported?
  9717. case 4: //Get Homun Position
  9718. if(script_hasdata(st,6))
  9719. sd=map_nick2sd(script_getstr(st,6));
  9720. else
  9721. sd=script_rid2sd(st);
  9722. if (sd && sd->hd)
  9723. bl = &sd->hd->bl;
  9724. break;
  9725. }
  9726. if (!bl) { //No object found.
  9727. script_pushint(st,-1);
  9728. return 0;
  9729. }
  9730. x= bl->x;
  9731. y= bl->y;
  9732. memcpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  9733. //Set MapName$
  9734. num=st->stack->stack_data[st->start+2].u.num;
  9735. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9736. prefix=*name;
  9737. if(not_server_variable(prefix))
  9738. sd=script_rid2sd(st);
  9739. else
  9740. sd=NULL;
  9741. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  9742. //Set MapX
  9743. num=st->stack->stack_data[st->start+3].u.num;
  9744. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9745. prefix=*name;
  9746. if(not_server_variable(prefix))
  9747. sd=script_rid2sd(st);
  9748. else
  9749. sd=NULL;
  9750. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  9751. //Set MapY
  9752. num=st->stack->stack_data[st->start+4].u.num;
  9753. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9754. prefix=*name;
  9755. if(not_server_variable(prefix))
  9756. sd=script_rid2sd(st);
  9757. else
  9758. sd=NULL;
  9759. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  9760. //Return Success value
  9761. script_pushint(st,0);
  9762. return 0;
  9763. }
  9764. /*==========================================
  9765. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  9766. *------------------------------------------*/
  9767. BUILDIN_FUNC(logmes)
  9768. {
  9769. const char *str;
  9770. if (log_config.npc <= 0 ) return 0;
  9771. str = script_getstr(st,2);
  9772. log_npc(script_rid2sd(st),str);
  9773. return 0;
  9774. }
  9775. BUILDIN_FUNC(summon)
  9776. {
  9777. int _class, timeout=0;
  9778. const char *str,*event="";
  9779. TBL_PC *sd;
  9780. struct mob_data *md;
  9781. int tick = gettick();
  9782. sd=script_rid2sd(st);
  9783. if (!sd) return 0;
  9784. str =script_getstr(st,2);
  9785. _class=script_getnum(st,3);
  9786. if( script_hasdata(st,4) )
  9787. timeout=script_getnum(st,4);
  9788. if( script_hasdata(st,5) ){
  9789. event=script_getstr(st,5);
  9790. check_event(st, event);
  9791. }
  9792. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  9793. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  9794. if (md) {
  9795. md->master_id=sd->bl.id;
  9796. md->special_state.ai=1;
  9797. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  9798. mob_spawn (md); //Now it is ready for spawning.
  9799. clif_misceffect2(&md->bl,344);
  9800. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  9801. }
  9802. return 0;
  9803. }
  9804. /*==========================================
  9805. * Checks whether it is daytime/nighttime
  9806. *------------------------------------------*/
  9807. BUILDIN_FUNC(isnight)
  9808. {
  9809. script_pushint(st,(night_flag == 1));
  9810. return 0;
  9811. }
  9812. BUILDIN_FUNC(isday)
  9813. {
  9814. script_pushint(st,(night_flag == 0));
  9815. return 0;
  9816. }
  9817. /*================================================
  9818. * Check whether another item/card has been
  9819. * equipped - used for 2/15's cards patch [celest]
  9820. *------------------------------------------------*/
  9821. // leave this here, just in case
  9822. #if 0
  9823. BUILDIN_FUNC(isequipped)
  9824. {
  9825. TBL_PC *sd;
  9826. int i, j, k, id = 1;
  9827. int ret = -1;
  9828. sd = script_rid2sd(st);
  9829. for (i=0; id!=0; i++) {
  9830. int flag = 0;
  9831. FETCH (i+2, id) else id = 0;
  9832. if (id <= 0)
  9833. continue;
  9834. for (j=0; j<EQI_MAX; j++) {
  9835. int index;
  9836. index = sd->equip_index[j];
  9837. if(index < 0) continue;
  9838. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9839. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9840. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9841. if(!sd->inventory_data[index])
  9842. continue;
  9843. if(itemdb_type(id) != IT_CARD) { //Non card
  9844. if (sd->inventory_data[index]->nameid != id)
  9845. continue;
  9846. flag = 1;
  9847. break;
  9848. } else { //Card
  9849. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9850. continue;
  9851. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9852. if (sd->status.inventory[index].card[k] == id)
  9853. {
  9854. flag = 1;
  9855. break;
  9856. }
  9857. }
  9858. }
  9859. if (flag) break;
  9860. }
  9861. if (ret == -1)
  9862. ret = flag;
  9863. else
  9864. ret &= flag;
  9865. if (!ret) break;
  9866. }
  9867. script_pushint(st,ret);
  9868. return 0;
  9869. }
  9870. #endif
  9871. /*================================================
  9872. * Check how many items/cards in the list are
  9873. * equipped - used for 2/15's cards patch [celest]
  9874. *------------------------------------------------*/
  9875. BUILDIN_FUNC(isequippedcnt)
  9876. {
  9877. TBL_PC *sd;
  9878. int i, j, k, id = 1;
  9879. int ret = 0;
  9880. sd = script_rid2sd(st);
  9881. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9882. script_pushint(st,0);
  9883. return 0;
  9884. }
  9885. for (i=0; id!=0; i++) {
  9886. FETCH (i+2, id) else id = 0;
  9887. if (id <= 0)
  9888. continue;
  9889. for (j=0; j<EQI_MAX; j++) {
  9890. int index;
  9891. index = sd->equip_index[j];
  9892. if(index < 0) continue;
  9893. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9894. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9895. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9896. if(!sd->inventory_data[index])
  9897. continue;
  9898. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  9899. if (sd->inventory_data[index]->nameid == id)
  9900. ret+= sd->status.inventory[index].amount;
  9901. } else { //Count cards.
  9902. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9903. continue; //No cards
  9904. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9905. if (sd->status.inventory[index].card[k] == id)
  9906. ret++; //[Lupus]
  9907. }
  9908. }
  9909. }
  9910. }
  9911. script_pushint(st,ret);
  9912. return 0;
  9913. }
  9914. /*================================================
  9915. * Check whether another card has been
  9916. * equipped - used for 2/15's cards patch [celest]
  9917. * -- Items checked cannot be reused in another
  9918. * card set to prevent exploits
  9919. *------------------------------------------------*/
  9920. BUILDIN_FUNC(isequipped)
  9921. {
  9922. TBL_PC *sd;
  9923. int i, j, k, id = 1;
  9924. int index, flag;
  9925. int ret = -1;
  9926. //Original hash to reverse it when full check fails.
  9927. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  9928. sd = script_rid2sd(st);
  9929. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9930. script_pushint(st,0);
  9931. return 0;
  9932. }
  9933. setitem_hash = sd->setitem_hash;
  9934. setitem_hash2 = sd->setitem_hash2;
  9935. for (i=0; id!=0; i++)
  9936. {
  9937. FETCH (i+2, id) else id = 0;
  9938. if (id <= 0)
  9939. continue;
  9940. flag = 0;
  9941. for (j=0; j<EQI_MAX; j++)
  9942. {
  9943. index = sd->equip_index[j];
  9944. if(index < 0) continue;
  9945. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9946. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9947. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9948. if(!sd->inventory_data[index])
  9949. continue;
  9950. if (itemdb_type(id) != IT_CARD) {
  9951. if (sd->inventory_data[index]->nameid != id)
  9952. continue;
  9953. flag = 1;
  9954. break;
  9955. } else { //Cards
  9956. if (sd->inventory_data[index]->slot == 0 ||
  9957. itemdb_isspecial(sd->status.inventory[index].card[0]))
  9958. continue;
  9959. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  9960. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  9961. unsigned int hash = 0;
  9962. if (sd->status.inventory[index].card[k] != id)
  9963. continue;
  9964. hash = 1<<((j<5?j:j-5)*4 + k);
  9965. // check if card is already used by another set
  9966. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  9967. continue;
  9968. // We have found a match
  9969. flag = 1;
  9970. // Set hash so this card cannot be used by another
  9971. if (j<5)
  9972. sd->setitem_hash |= hash;
  9973. else
  9974. sd->setitem_hash2 |= hash;
  9975. break;
  9976. }
  9977. }
  9978. if (flag) break; //Card found
  9979. }
  9980. if (ret == -1)
  9981. ret = flag;
  9982. else
  9983. ret &= flag;
  9984. if (!ret) break;
  9985. }
  9986. if (!ret)
  9987. { //When check fails, restore original hash values. [Skotlex]
  9988. sd->setitem_hash = setitem_hash;
  9989. sd->setitem_hash2 = setitem_hash2;
  9990. }
  9991. script_pushint(st,ret);
  9992. return 0;
  9993. }
  9994. /*================================================
  9995. * Check how many given inserted cards in the CURRENT
  9996. * weapon - used for 2/15's cards patch [Lupus]
  9997. *------------------------------------------------*/
  9998. BUILDIN_FUNC(cardscnt)
  9999. {
  10000. TBL_PC *sd;
  10001. int i, k, id = 1;
  10002. int ret = 0;
  10003. int index;
  10004. sd = script_rid2sd(st);
  10005. for (i=0; id!=0; i++) {
  10006. FETCH (i+2, id) else id = 0;
  10007. if (id <= 0)
  10008. continue;
  10009. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10010. if(index < 0) continue;
  10011. if(!sd->inventory_data[index])
  10012. continue;
  10013. if(itemdb_type(id) != IT_CARD) {
  10014. if (sd->inventory_data[index]->nameid == id)
  10015. ret+= sd->status.inventory[index].amount;
  10016. } else {
  10017. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10018. continue;
  10019. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10020. if (sd->status.inventory[index].card[k] == id)
  10021. ret++;
  10022. }
  10023. }
  10024. }
  10025. script_pushint(st,ret);
  10026. // script_pushint(st,current_equip_item_index);
  10027. return 0;
  10028. }
  10029. /*=======================================================
  10030. * Returns the refined number of the current item, or an
  10031. * item with inventory index specified
  10032. *-------------------------------------------------------*/
  10033. BUILDIN_FUNC(getrefine)
  10034. {
  10035. TBL_PC *sd;
  10036. if ((sd = script_rid2sd(st))!= NULL)
  10037. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10038. else
  10039. script_pushint(st,0);
  10040. return 0;
  10041. }
  10042. /*=======================================================
  10043. * Allows 2 Parents to adopt a character as a Baby
  10044. *-------------------------------------------------------*/
  10045. BUILDIN_FUNC(adopt)
  10046. {
  10047. int ret;
  10048. const char *parent1 = script_getstr(st,2);
  10049. const char *parent2 = script_getstr(st,3);
  10050. const char *child = script_getstr(st,4);
  10051. TBL_PC *p1_sd = map_nick2sd(parent1);
  10052. TBL_PC *p2_sd = map_nick2sd(parent2);
  10053. TBL_PC *c_sd = map_nick2sd(child);
  10054. if (!p1_sd || !p2_sd || !c_sd ||
  10055. p1_sd->status.base_level < 70 ||
  10056. p2_sd->status.base_level < 70)
  10057. return 0;
  10058. ret = pc_adoption(p1_sd, p2_sd, c_sd);
  10059. script_pushint(st,ret);
  10060. return 0;
  10061. }
  10062. /*=======================================================
  10063. * Day/Night controls
  10064. *-------------------------------------------------------*/
  10065. BUILDIN_FUNC(night)
  10066. {
  10067. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10068. return 0;
  10069. }
  10070. BUILDIN_FUNC(day)
  10071. {
  10072. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10073. return 0;
  10074. }
  10075. //=======================================================
  10076. // Unequip [Spectre]
  10077. //-------------------------------------------------------
  10078. BUILDIN_FUNC(unequip)
  10079. {
  10080. int i;
  10081. size_t num;
  10082. TBL_PC *sd;
  10083. num = script_getnum(st,2);
  10084. sd = script_rid2sd(st);
  10085. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10086. {
  10087. i = pc_checkequip(sd,equip[num-1]);
  10088. if (i >= 0)
  10089. pc_unequipitem(sd,i,2);
  10090. return 0;
  10091. }
  10092. return 0;
  10093. }
  10094. BUILDIN_FUNC(equip)
  10095. {
  10096. int nameid=0,i;
  10097. TBL_PC *sd;
  10098. struct item_data *item_data;
  10099. sd = script_rid2sd(st);
  10100. nameid=script_getnum(st,2);
  10101. if((item_data = itemdb_exists(nameid)) == NULL)
  10102. {
  10103. if(battle_config.error_log)
  10104. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10105. return 1;
  10106. }
  10107. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10108. if( i < MAX_INVENTORY )
  10109. pc_equipitem(sd,i,item_data->equip);
  10110. return 0;
  10111. }
  10112. BUILDIN_FUNC(autoequip)
  10113. {
  10114. int nameid, flag;
  10115. struct item_data *item_data;
  10116. nameid=script_getnum(st,2);
  10117. flag=script_getnum(st,3);
  10118. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10119. item_data->flag.autoequip = flag>0?1:0;
  10120. }
  10121. return 0;
  10122. }
  10123. BUILDIN_FUNC(setbattleflag)
  10124. {
  10125. const char *flag, *value;
  10126. flag = script_getstr(st,2);
  10127. value = script_getstr(st,3);
  10128. if (battle_set_value(flag, value) == 0)
  10129. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10130. else
  10131. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10132. return 0;
  10133. }
  10134. BUILDIN_FUNC(getbattleflag)
  10135. {
  10136. const char *flag;
  10137. flag = script_getstr(st,2);
  10138. script_pushint(st,battle_get_value(flag));
  10139. return 0;
  10140. }
  10141. //=======================================================
  10142. // strlen [Valaris]
  10143. //-------------------------------------------------------
  10144. BUILDIN_FUNC(getstrlen)
  10145. {
  10146. const char *str = script_getstr(st,2);
  10147. int len = (str) ? (int)strlen(str) : 0;
  10148. script_pushint(st,len);
  10149. return 0;
  10150. }
  10151. //=======================================================
  10152. // isalpha [Valaris]
  10153. //-------------------------------------------------------
  10154. BUILDIN_FUNC(charisalpha)
  10155. {
  10156. const char *str=script_getstr(st,2);
  10157. int pos=script_getnum(st,3);
  10158. int val = ( str && pos>0 && (unsigned int)pos<strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10159. script_pushint(st,val);
  10160. return 0;
  10161. }
  10162. // [Lance]
  10163. BUILDIN_FUNC(fakenpcname)
  10164. {
  10165. const char *name;
  10166. const char *newname;
  10167. int look;
  10168. name = script_getstr(st,2);
  10169. newname = script_getstr(st,3);
  10170. look = script_getnum(st,4);
  10171. if(look > 32767 || look < -32768) {
  10172. ShowError("buildin_fakenpcname: Invalid look value %d\n",look);
  10173. return 1; // Safety measure to prevent runtime errors
  10174. }
  10175. npc_changename(name,newname,(short)look);
  10176. return 0;
  10177. }
  10178. BUILDIN_FUNC(atoi)
  10179. {
  10180. const char *value;
  10181. value = script_getstr(st,2);
  10182. script_pushint(st,atoi(value));
  10183. return 0;
  10184. }
  10185. // case-insensitive substring search [lordalfa]
  10186. BUILDIN_FUNC(compare)
  10187. {
  10188. const char *message;
  10189. const char *cmpstring;
  10190. message = script_getstr(st,2);
  10191. cmpstring = script_getstr(st,3);
  10192. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10193. return 0;
  10194. }
  10195. // [zBuffer] List of mathematics commands --->
  10196. BUILDIN_FUNC(sqrt)
  10197. {
  10198. double i, a;
  10199. i = script_getnum(st,2);
  10200. a = sqrt(i);
  10201. script_pushint(st,(int)a);
  10202. return 0;
  10203. }
  10204. BUILDIN_FUNC(pow)
  10205. {
  10206. double i, a, b;
  10207. a = script_getnum(st,2);
  10208. b = script_getnum(st,3);
  10209. i = pow(a,b);
  10210. script_pushint(st,(int)i);
  10211. return 0;
  10212. }
  10213. BUILDIN_FUNC(distance)
  10214. {
  10215. int x0, y0, x1, y1;
  10216. x0 = script_getnum(st,2);
  10217. y0 = script_getnum(st,3);
  10218. x1 = script_getnum(st,4);
  10219. y1 = script_getnum(st,5);
  10220. script_pushint(st,distance(x0-x1, y0-y1));
  10221. return 0;
  10222. }
  10223. BUILDIN_FUNC(checkcell)
  10224. {
  10225. int m;
  10226. const char *map = script_getstr(st, 2);
  10227. m = mapindex_name2id(map);
  10228. if(m){
  10229. script_pushint(st,map_getcell(m, script_getnum(st,3), script_getnum(st,4),script_getnum(st,5)));
  10230. } else {
  10231. script_pushint(st,0);
  10232. }
  10233. return 0;
  10234. }
  10235. // <--- [zBuffer] List of mathematics commands
  10236. // [zBuffer] List of dynamic var commands --->
  10237. //FIXME: some other functions are using this private function
  10238. void setd_sub(struct script_state *st, TBL_PC *sd, char *varname, int elem, void *value, struct linkdb_node **ref)
  10239. {
  10240. set_reg(st, sd, add_str(varname)+(elem<<24), varname, value, ref);
  10241. return;
  10242. }
  10243. BUILDIN_FUNC(setd)
  10244. {
  10245. TBL_PC *sd=NULL;
  10246. char varname[100];
  10247. const char *value, *buffer;
  10248. int elem;
  10249. buffer = script_getstr(st, 2);
  10250. value = script_getstr(st, 3);
  10251. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10252. elem = 0;
  10253. if(st->rid)
  10254. sd = script_rid2sd(st);
  10255. if(varname[strlen(varname)-1] != '$') {
  10256. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10257. } else {
  10258. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10259. }
  10260. return 0;
  10261. }
  10262. BUILDIN_FUNC(query_sql)
  10263. {
  10264. #ifndef TXT_ONLY
  10265. int i, j;
  10266. TBL_PC* sd = NULL;
  10267. const char* query;
  10268. struct script_data* data;
  10269. char* name;
  10270. int max_rows = 128;// maximum number of rows
  10271. int num_vars;
  10272. int num_cols;
  10273. // check target variables
  10274. for( i = 3; script_hasdata(st,i); ++i )
  10275. {
  10276. data = script_getdata(st, i);
  10277. if( data_isreference(data) && reference_tovariable(data) )
  10278. {// it's a variable
  10279. name = reference_getname(data);
  10280. if( not_server_variable(*name) && sd == NULL )
  10281. {// requires a player
  10282. sd = script_rid2sd(st);
  10283. if( sd == NULL )
  10284. {// no player attached
  10285. script_reportdata(data);
  10286. st->state = END;
  10287. return 1;
  10288. }
  10289. }
  10290. if( not_array_variable(*name) )
  10291. max_rows = 1;// not an array, limit to one row
  10292. }
  10293. else
  10294. {
  10295. ShowError("script:query_sql: not a variable\n");
  10296. script_reportdata(data);
  10297. st->state = END;
  10298. return 1;
  10299. }
  10300. }
  10301. num_vars = i - 3;
  10302. // Execute the query
  10303. query = script_getstr(st,2);
  10304. if( SQL_ERROR == Sql_Query(mmysql_handle, query) )
  10305. {
  10306. Sql_ShowDebug(mmysql_handle);
  10307. script_pushint(st, 0);
  10308. return 1;
  10309. }
  10310. if( Sql_NumRows(mmysql_handle) == 0 )
  10311. {// No data received
  10312. Sql_FreeResult(mmysql_handle);
  10313. script_pushint(st, 0);
  10314. return 0;
  10315. }
  10316. // Count the number of columns to store
  10317. num_cols = Sql_NumColumns(mmysql_handle);
  10318. if( num_vars < num_cols )
  10319. {
  10320. ShowWarning("script:query_sql: Too many columns, discarting last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10321. script_reportsrc(st);
  10322. }
  10323. else if( num_vars > num_cols )
  10324. {
  10325. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10326. script_reportsrc(st);
  10327. }
  10328. // Store data
  10329. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(mmysql_handle); ++i )
  10330. {
  10331. for( j = 0; j < num_vars; ++j )
  10332. {
  10333. char* str = NULL;
  10334. if( j < num_cols )
  10335. Sql_GetData(mmysql_handle, j, &str, NULL);
  10336. data = script_getdata(st, j+3);
  10337. name = reference_getname(data);
  10338. if( is_string_variable(name) )
  10339. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10340. else
  10341. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10342. }
  10343. }
  10344. if( i == max_rows && max_rows < Sql_NumRows(mmysql_handle) )
  10345. {
  10346. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(mmysql_handle));
  10347. script_reportsrc(st);
  10348. }
  10349. // Free data
  10350. Sql_FreeResult(mmysql_handle);
  10351. script_pushint(st, i);
  10352. #else
  10353. //for TXT version, we always return -1
  10354. script_pushint(st,-1);
  10355. #endif
  10356. return 0;
  10357. }
  10358. //Allows escaping of a given string.
  10359. BUILDIN_FUNC(escape_sql)
  10360. {
  10361. const char *str;
  10362. char *esc_str;
  10363. size_t len;
  10364. str = script_getstr(st,2);
  10365. len = strlen(str);
  10366. esc_str = aMallocA(len*2+1);
  10367. #if defined(TXT_ONLY)
  10368. jstrescapecpy(esc_str, str);
  10369. #else
  10370. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10371. #endif
  10372. script_pushstr(st, esc_str);
  10373. return 0;
  10374. }
  10375. BUILDIN_FUNC(getd)
  10376. {
  10377. char varname[100];
  10378. const char *buffer;
  10379. //struct script_data dat;
  10380. int elem;
  10381. buffer = script_getstr(st, 2);
  10382. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10383. elem = 0;
  10384. // Push the 'pointer' so it's more flexible [Lance]
  10385. push_val(st->stack,C_NAME,
  10386. (elem<<24) | add_str(varname));
  10387. return 0;
  10388. }
  10389. // <--- [zBuffer] List of dynamic var commands
  10390. // Pet stat [Lance]
  10391. BUILDIN_FUNC(petstat)
  10392. {
  10393. TBL_PC *sd = NULL;
  10394. struct pet_data *pd;
  10395. int flag = script_getnum(st,2);
  10396. sd = script_rid2sd(st);
  10397. if(!sd || !sd->status.pet_id || !sd->pd){
  10398. if(flag == 2)
  10399. script_pushconststr(st, "");
  10400. else
  10401. script_pushint(st,0);
  10402. return 0;
  10403. }
  10404. pd = sd->pd;
  10405. switch(flag){
  10406. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10407. case 2: script_pushstr(st, aStrdup(pd->pet.name)); break;
  10408. case 3: script_pushint(st,(int)pd->pet.level); break;
  10409. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10410. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10411. default:
  10412. script_pushint(st,0);
  10413. break;
  10414. }
  10415. return 0;
  10416. }
  10417. BUILDIN_FUNC(callshop)
  10418. {
  10419. TBL_PC *sd = NULL;
  10420. struct npc_data *nd;
  10421. const char *shopname;
  10422. int flag = 0;
  10423. sd = script_rid2sd(st);
  10424. if (!sd) {
  10425. script_pushint(st,0);
  10426. return 0;
  10427. }
  10428. shopname = script_getstr(st, 2);
  10429. if( script_hasdata(st,3) )
  10430. flag = script_getnum(st,3);
  10431. nd = npc_name2id(shopname);
  10432. if (!nd || nd->bl.type!=BL_NPC || nd->bl.subtype!=SHOP) {
  10433. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)", shopname);
  10434. script_pushint(st,0);
  10435. return 1;
  10436. }
  10437. switch (flag) {
  10438. case 1: //Buy window
  10439. npc_buysellsel(sd,nd->bl.id,0);
  10440. break;
  10441. case 2: //Sell window
  10442. npc_buysellsel(sd,nd->bl.id,1);
  10443. break;
  10444. default: //Show menu
  10445. clif_npcbuysell(sd,nd->bl.id);
  10446. break;
  10447. }
  10448. sd->npc_shopid = nd->bl.id;
  10449. script_pushint(st,1);
  10450. return 0;
  10451. }
  10452. #ifndef MAX_SHOPITEM
  10453. #define MAX_SHOPITEM 100
  10454. #endif
  10455. BUILDIN_FUNC(npcshopitem)
  10456. {
  10457. struct npc_data *nd= NULL;
  10458. int n = 0;
  10459. int i = 3;
  10460. int amount;
  10461. const char* npcname = script_getstr(st, 2);
  10462. nd = npc_name2id(npcname);
  10463. if(!nd || nd->bl.subtype!=SHOP)
  10464. { //Not found.
  10465. script_pushint(st,0);
  10466. return 0;
  10467. }
  10468. // st->end - 2 = nameid + value # ... / 2 = number of items ... + 1 to end it
  10469. amount = ((st->end-2)/2)+1;
  10470. //Reinsert as realloc could change the pointer address.
  10471. map_deliddb(&nd->bl);
  10472. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10473. sizeof(nd->u.shop_item[0]) * amount);
  10474. map_addiddb(&nd->bl);
  10475. // Reset sell list.
  10476. memset(nd->u.shop_item, 0, sizeof(nd->u.shop_item[0]) * amount);
  10477. n = 0;
  10478. while (script_hasdata(st,i)) {
  10479. nd->u.shop_item[n].nameid = script_getnum(st,i);
  10480. i++;
  10481. nd->u.shop_item[n].value = script_getnum(st,i);
  10482. i++;
  10483. n++;
  10484. }
  10485. return 0;
  10486. }
  10487. BUILDIN_FUNC(npcshopadditem)
  10488. {
  10489. struct npc_data *nd=NULL;
  10490. int n = 0;
  10491. int i = 3;
  10492. int amount;
  10493. const char* npcname = script_getstr(st, 2);
  10494. nd = npc_name2id(npcname);
  10495. if (!nd || nd->bl.subtype!=SHOP)
  10496. { //Not found.
  10497. script_pushint(st,0);
  10498. return 0;
  10499. }
  10500. amount = ((st->end-2)/2)+1;
  10501. while (nd->u.shop_item[n].nameid && n < MAX_SHOPITEM)
  10502. n++;
  10503. //Reinsert as realloc could change the pointer address.
  10504. map_deliddb(&nd->bl);
  10505. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10506. sizeof(nd->u.shop_item[0]) * (amount+n));
  10507. map_addiddb(&nd->bl);
  10508. while(script_hasdata(st,i)){
  10509. nd->u.shop_item[n].nameid = script_getnum(st,i);
  10510. i++;
  10511. nd->u.shop_item[n].value = script_getnum(st,i);
  10512. i++;
  10513. n++;
  10514. }
  10515. // Marks the last of our stuff..
  10516. nd->u.shop_item[n].value = 0;
  10517. nd->u.shop_item[n].nameid = 0;
  10518. script_pushint(st,1);
  10519. return 0;
  10520. }
  10521. BUILDIN_FUNC(npcshopdelitem)
  10522. {
  10523. struct npc_data *nd=NULL;
  10524. int n=0;
  10525. int i=3;
  10526. int size = 0;
  10527. const char* npcname = script_getstr(st, 2);
  10528. nd = npc_name2id(npcname);
  10529. if (!nd || nd->bl.subtype!=SHOP)
  10530. { //Not found.
  10531. script_pushint(st,0);
  10532. return 0;
  10533. }
  10534. while (nd->u.shop_item[size].nameid)
  10535. size++;
  10536. while (script_hasdata(st,i)) {
  10537. for(n=0;nd->u.shop_item[n].nameid && n < MAX_SHOPITEM;n++) {
  10538. if (nd->u.shop_item[n].nameid == script_getnum(st,i)) {
  10539. // We're moving 1 extra empty block. Junk data is eliminated later.
  10540. memmove(&nd->u.shop_item[n], &nd->u.shop_item[n+1], sizeof(nd->u.shop_item[0])*(size-n));
  10541. }
  10542. }
  10543. i++;
  10544. }
  10545. size = 0;
  10546. while (nd->u.shop_item[size].nameid)
  10547. size++;
  10548. //Reinsert as realloc could change the pointer address.
  10549. map_deliddb(&nd->bl);
  10550. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10551. sizeof(nd->u.shop_item[0]) * (size+1));
  10552. map_addiddb(&nd->bl);
  10553. script_pushint(st,1);
  10554. return 0;
  10555. }
  10556. //Sets a script to attach to an npc.
  10557. BUILDIN_FUNC(npcshopattach)
  10558. {
  10559. struct npc_data *nd=NULL;
  10560. const char* npcname = script_getstr(st, 2);
  10561. int flag = 1;
  10562. if( script_hasdata(st,3) )
  10563. flag = script_getnum(st,3);
  10564. nd = npc_name2id(npcname);
  10565. if (!nd || nd->bl.subtype!=SHOP)
  10566. { //Not found.
  10567. script_pushint(st,0);
  10568. return 0;
  10569. }
  10570. if (flag)
  10571. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10572. else
  10573. nd->master_nd = NULL;
  10574. script_pushint(st,1);
  10575. return 0;
  10576. }
  10577. /*==========================================
  10578. * Returns some values of an item [Lupus]
  10579. * Price, Weight, etc...
  10580. setitemscript(itemID,"{new item bonus script}",[n]);
  10581. Where n:
  10582. 0 - script
  10583. 1 - Equip script
  10584. 2 - Unequip script
  10585. *------------------------------------------*/
  10586. BUILDIN_FUNC(setitemscript)
  10587. {
  10588. int item_id,n=0;
  10589. const char *script;
  10590. struct item_data *i_data;
  10591. struct script_code **dstscript;
  10592. item_id = script_getnum(st,2);
  10593. script = script_getstr(st,3);
  10594. if( script_hasdata(st,4) )
  10595. n=script_getnum(st,4);
  10596. i_data = itemdb_exists(item_id);
  10597. if (!i_data || script==NULL || script[0]!='{') {
  10598. script_pushint(st,0);
  10599. return 0;
  10600. }
  10601. switch (n) {
  10602. case 2:
  10603. dstscript = &i_data->unequip_script;
  10604. break;
  10605. case 1:
  10606. dstscript = &i_data->equip_script;
  10607. break;
  10608. default:
  10609. dstscript = &i_data->script;
  10610. break;
  10611. }
  10612. if(*dstscript)
  10613. script_free_code(*dstscript);
  10614. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  10615. script_pushint(st,1);
  10616. return 0;
  10617. }
  10618. /* Work In Progress [Lupus]
  10619. BUILDIN_FUNC(addmonsterdrop)
  10620. {
  10621. int class_,item_id,chance;
  10622. class_=script_getnum(st,2);
  10623. item_id=script_getnum(st,3);
  10624. chance=script_getnum(st,4);
  10625. if(class_>1000 && item_id>500 && chance>0) {
  10626. script_pushint(st,1);
  10627. } else {
  10628. script_pushint(st,0);
  10629. }
  10630. }
  10631. BUILDIN_FUNC(delmonsterdrop)
  10632. {
  10633. int class_,item_id;
  10634. class_=script_getnum(st,2);
  10635. item_id=script_getnum(st,3);
  10636. if(class_>1000 && item_id>500) {
  10637. script_pushint(st,1);
  10638. } else {
  10639. script_pushint(st,0);
  10640. }
  10641. }
  10642. */
  10643. /*==========================================
  10644. * Returns some values of a monster [Lupus]
  10645. * Name, Level, race, size, etc...
  10646. getmonsterinfo(monsterID,queryIndex);
  10647. *------------------------------------------*/
  10648. BUILDIN_FUNC(getmonsterinfo)
  10649. {
  10650. struct mob_db *mob;
  10651. int mob_id;
  10652. mob_id = script_getnum(st,2);
  10653. if (!mobdb_checkid(mob_id)) {
  10654. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i", mob_id);
  10655. script_pushint(st,-1);
  10656. return -1;
  10657. }
  10658. mob = mob_db(mob_id);
  10659. switch ( script_getnum(st,3) ) {
  10660. case 0: script_pushconststr(st,mob->jname); break;
  10661. case 1: script_pushint(st,mob->lv); break;
  10662. case 2: script_pushint(st,mob->status.max_hp); break;
  10663. case 3: script_pushint(st,mob->base_exp); break;
  10664. case 4: script_pushint(st,mob->job_exp); break;
  10665. case 5: script_pushint(st,mob->status.rhw.atk); break;
  10666. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  10667. case 7: script_pushint(st,mob->status.def); break;
  10668. case 8: script_pushint(st,mob->status.mdef); break;
  10669. case 9: script_pushint(st,mob->status.str); break;
  10670. case 10: script_pushint(st,mob->status.agi); break;
  10671. case 11: script_pushint(st,mob->status.vit); break;
  10672. case 12: script_pushint(st,mob->status.int_); break;
  10673. case 13: script_pushint(st,mob->status.dex); break;
  10674. case 14: script_pushint(st,mob->status.luk); break;
  10675. case 15: script_pushint(st,mob->status.rhw.range); break;
  10676. case 16: script_pushint(st,mob->range2); break;
  10677. case 17: script_pushint(st,mob->range3); break;
  10678. case 18: script_pushint(st,mob->status.size); break;
  10679. case 19: script_pushint(st,mob->status.race); break;
  10680. case 20: script_pushint(st,mob->status.def_ele); break;
  10681. case 21: script_pushint(st,mob->status.mode); break;
  10682. default: script_pushint(st,-1); //wrong Index
  10683. }
  10684. return 0;
  10685. }
  10686. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  10687. {
  10688. TBL_PC *sd = NULL;
  10689. if(script_hasdata(st,2))
  10690. sd = map_nick2sd(script_getstr(st,2));
  10691. else
  10692. sd = script_rid2sd(st);
  10693. if(sd)
  10694. script_pushint(st,(sd->vender_id != 0));
  10695. else
  10696. script_pushint(st,0);
  10697. return 0;
  10698. }
  10699. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  10700. {
  10701. TBL_PC *sd = NULL;
  10702. if(script_hasdata(st,2))
  10703. sd = map_nick2sd(script_getstr(st,2));
  10704. else
  10705. sd = script_rid2sd(st);
  10706. if(sd)
  10707. script_pushint(st,(sd->chatID != 0));
  10708. else
  10709. script_pushint(st,0);
  10710. return 0;
  10711. }
  10712. int axtoi(const char *hexStg)
  10713. {
  10714. int n = 0; // position in string
  10715. int m = 0; // position in digit[] to shift
  10716. int count; // loop index
  10717. int intValue = 0; // integer value of hex string
  10718. int digit[11]; // hold values to convert
  10719. while (n < 10) {
  10720. if (hexStg[n]=='\0')
  10721. break;
  10722. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  10723. digit[n] = hexStg[n] & 0x0f; //convert to int
  10724. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  10725. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10726. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  10727. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10728. else break;
  10729. n++;
  10730. }
  10731. count = n;
  10732. m = n - 1;
  10733. n = 0;
  10734. while(n < count) {
  10735. // digit[n] is value of hex digit at position n
  10736. // (m << 2) is the number of positions to shift
  10737. // OR the bits into return value
  10738. intValue = intValue | (digit[n] << (m << 2));
  10739. m--; // adjust the position to set
  10740. n++; // next digit to process
  10741. }
  10742. return (intValue);
  10743. }
  10744. // [Lance] Hex string to integer converter
  10745. BUILDIN_FUNC(axtoi)
  10746. {
  10747. const char *hex = script_getstr(st,2);
  10748. script_pushint(st,axtoi(hex));
  10749. return 0;
  10750. }
  10751. // [zBuffer] List of player cont commands --->
  10752. BUILDIN_FUNC(rid2name)
  10753. {
  10754. struct block_list *bl = NULL;
  10755. int rid = script_getnum(st,2);
  10756. if((bl = map_id2bl(rid)))
  10757. {
  10758. switch(bl->type) {
  10759. case BL_MOB: script_pushconststr(st,((TBL_MOB*)bl)->name); break;
  10760. case BL_PC: script_pushconststr(st,((TBL_PC*)bl)->status.name); break;
  10761. case BL_NPC: script_pushconststr(st,((TBL_NPC*)bl)->exname); break;
  10762. case BL_PET: script_pushconststr(st,((TBL_PET*)bl)->pet.name); break;
  10763. case BL_HOM: script_pushconststr(st,((TBL_HOM*)bl)->homunculus.name); break;
  10764. default:
  10765. ShowError("buildin_rid2name: BL type unknown.\n");
  10766. script_pushconststr(st,"");
  10767. break;
  10768. }
  10769. } else {
  10770. ShowError("buildin_rid2name: invalid RID\n");
  10771. script_pushconststr(st,"(null)");
  10772. }
  10773. return 0;
  10774. }
  10775. BUILDIN_FUNC(pcblockmove)
  10776. {
  10777. int id, flag;
  10778. TBL_PC *sd = NULL;
  10779. id = script_getnum(st,2);
  10780. flag = script_getnum(st,3);
  10781. if(id)
  10782. sd = map_id2sd(id);
  10783. else
  10784. sd = script_rid2sd(st);
  10785. if(sd)
  10786. sd->state.blockedmove = flag > 0;
  10787. return 0;
  10788. }
  10789. BUILDIN_FUNC(pcfollow)
  10790. {
  10791. int id, targetid;
  10792. TBL_PC *sd = NULL;
  10793. id = script_getnum(st,2);
  10794. targetid = script_getnum(st,3);
  10795. if(id)
  10796. sd = map_id2sd(id);
  10797. else
  10798. sd = script_rid2sd(st);
  10799. if(sd)
  10800. pc_follow(sd, targetid);
  10801. return 0;
  10802. }
  10803. BUILDIN_FUNC(pcstopfollow)
  10804. {
  10805. int id;
  10806. TBL_PC *sd = NULL;
  10807. id = script_getnum(st,2);
  10808. if(id)
  10809. sd = map_id2sd(id);
  10810. else
  10811. sd = script_rid2sd(st);
  10812. if(sd)
  10813. pc_stop_following(sd);
  10814. return 0;
  10815. }
  10816. // <--- [zBuffer] List of player cont commands
  10817. // [zBuffer] List of mob control commands --->
  10818. //## TODO always return if the request/whatever was successfull [FlavioJS]
  10819. BUILDIN_FUNC(mobspawn)
  10820. {
  10821. int class_,x,y,id;
  10822. const char *str,*map;
  10823. // Who?
  10824. str =script_getstr(st,2);
  10825. // What?
  10826. class_ =script_getnum(st,3);
  10827. // Where?
  10828. map =script_getstr(st,4);
  10829. x =script_getnum(st,5);
  10830. y =script_getnum(st,6);
  10831. id = mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,1,"");
  10832. script_pushint(st,id);
  10833. return 0;
  10834. }
  10835. BUILDIN_FUNC(mobremove)
  10836. {
  10837. int id;
  10838. struct block_list *bl = NULL;
  10839. id = script_getnum(st,2);
  10840. bl = map_id2bl(id);
  10841. if (bl && bl->type == BL_MOB)
  10842. unit_free(bl,0);
  10843. return 0;
  10844. }
  10845. BUILDIN_FUNC(getmobdata)
  10846. {
  10847. int num, id;
  10848. char *name;
  10849. struct mob_data *md = NULL;
  10850. TBL_PC *sd = st->rid?map_id2sd(st->rid):NULL;
  10851. id = script_getnum(st,2);
  10852. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB || !data_isreference(script_getdata(st,3)) ){
  10853. ShowWarning("buildin_getmobdata: Error in argument!\n");
  10854. return -1;
  10855. }
  10856. num=st->stack->stack_data[st->start+3].u.num;
  10857. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10858. setd_sub(st,sd,name,0,(void *)(int)md->class_,script_getref(st,3));
  10859. setd_sub(st,sd,name,1,(void *)(int)md->level,script_getref(st,3));
  10860. setd_sub(st,sd,name,2,(void *)(int)md->status.hp,script_getref(st,3));
  10861. setd_sub(st,sd,name,3,(void *)(int)md->status.max_hp,script_getref(st,3));
  10862. setd_sub(st,sd,name,4,(void *)(int)md->master_id,script_getref(st,3));
  10863. setd_sub(st,sd,name,5,(void *)(int)md->bl.m,script_getref(st,3));
  10864. setd_sub(st,sd,name,6,(void *)(int)md->bl.x,script_getref(st,3));
  10865. setd_sub(st,sd,name,7,(void *)(int)md->bl.y,script_getref(st,3));
  10866. setd_sub(st,sd,name,8,(void *)(int)md->status.speed,script_getref(st,3));
  10867. setd_sub(st,sd,name,9,(void *)(int)md->status.mode,script_getref(st,3));
  10868. setd_sub(st,sd,name,10,(void *)(int)md->special_state.ai,script_getref(st,3));
  10869. setd_sub(st,sd,name,11,(void *)(int)md->sc.option,script_getref(st,3));
  10870. setd_sub(st,sd,name,12,(void *)(int)md->vd->sex,script_getref(st,3));
  10871. setd_sub(st,sd,name,13,(void *)(int)md->vd->class_,script_getref(st,3));
  10872. setd_sub(st,sd,name,14,(void *)(int)md->vd->hair_style,script_getref(st,3));
  10873. setd_sub(st,sd,name,15,(void *)(int)md->vd->hair_color,script_getref(st,3));
  10874. setd_sub(st,sd,name,16,(void *)(int)md->vd->head_bottom,script_getref(st,3));
  10875. setd_sub(st,sd,name,17,(void *)(int)md->vd->head_mid,script_getref(st,3));
  10876. setd_sub(st,sd,name,18,(void *)(int)md->vd->head_top,script_getref(st,3));
  10877. setd_sub(st,sd,name,19,(void *)(int)md->vd->cloth_color,script_getref(st,3));
  10878. setd_sub(st,sd,name,20,(void *)(int)md->vd->shield,script_getref(st,3));
  10879. setd_sub(st,sd,name,21,(void *)(int)md->vd->weapon,script_getref(st,3));
  10880. setd_sub(st,sd,name,22,(void *)(int)md->vd->shield,script_getref(st,3));
  10881. setd_sub(st,sd,name,23,(void *)(int)md->ud.dir,script_getref(st,3));
  10882. setd_sub(st,sd,name,24,(void *)(int)md->state.killer,script_getref(st,3));
  10883. setd_sub(st,sd,name,25,(void *)(int)md->callback_flag,script_getref(st,3));
  10884. setd_sub(st,sd,name,26,(void *)(int)md->state.no_random_walk, script_getref(st,3));
  10885. return 0;
  10886. }
  10887. /// Changes the data of a mob
  10888. ///
  10889. /// setmobdata <mob unit id>,<type>,<value>;
  10890. BUILDIN_FUNC(setmobdata)
  10891. {
  10892. struct block_list* mob_bl;
  10893. mob_bl = map_id2bl(script_getnum(st,2));
  10894. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  10895. {
  10896. TBL_MOB* md = (TBL_MOB*)mob_bl;
  10897. int type;
  10898. int value;
  10899. type = script_getnum(st,3);
  10900. value = script_getnum(st,4);
  10901. switch( type )
  10902. {
  10903. case 0: md->class_ = (short)value; break;
  10904. case 1: md->level = (unsigned short)value; break;
  10905. case 2: md->status.hp = (unsigned int)value; break;
  10906. case 3: md->status.max_hp = (unsigned int)value; break;
  10907. case 4: md->master_id = value; break;
  10908. case 5: md->bl.m = (short)value; break;
  10909. case 6: md->bl.x = (short)value; break;
  10910. case 7: md->bl.y = (short)value; break;
  10911. case 8: md->status.speed = (unsigned short)value; break;
  10912. case 9: md->status.mode = (unsigned short)value; break;
  10913. case 10: md->special_state.ai = (unsigned int)value; break;
  10914. case 11: md->sc.option = (unsigned short)value; break;
  10915. case 12: md->vd->sex = (char)value; break;
  10916. case 13: md->vd->class_ = (unsigned short)value; break;
  10917. case 14: md->vd->hair_style = (unsigned short)value; break;
  10918. case 15: md->vd->hair_color = (unsigned short)value; break;
  10919. case 16: md->vd->head_bottom = (unsigned short)value; break;
  10920. case 17: md->vd->head_mid = (unsigned short)value; break;
  10921. case 18: md->vd->head_top = (unsigned short)value; break;
  10922. case 19: md->vd->cloth_color = (unsigned short)value; break;
  10923. case 20: md->vd->shield = (unsigned short)value; break;
  10924. case 21: md->vd->weapon = (unsigned short)value; break;
  10925. case 22: md->vd->shield = (unsigned short)value; break;
  10926. case 23: md->ud.dir = (unsigned char)value; break;
  10927. case 24: md->state.killer = value > 0 ? 1 : 0; break;
  10928. case 25: md->callback_flag = (short)value; break;
  10929. case 26: md->state.no_random_walk = value > 0 ? 1 : 0; break;
  10930. default:
  10931. ShowError("script:setmobdata: unknown data identifier %d\n", type);
  10932. return 1;
  10933. }
  10934. }
  10935. return 0;
  10936. }
  10937. /// Makes the mob assist the target unit as a slave
  10938. ///
  10939. /// mobassist <mob unit id>,"<player name>";
  10940. /// mobassist <mob unit id>,<target id>;
  10941. BUILDIN_FUNC(mobassist)
  10942. {
  10943. struct block_list* mob_bl;
  10944. // get mob
  10945. mob_bl = map_id2bl(script_getnum(st,2));
  10946. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  10947. {
  10948. TBL_MOB* md = (TBL_MOB*)mob_bl;
  10949. struct block_list* target_bl = NULL;
  10950. struct script_data* data;
  10951. // get target
  10952. data = script_getdata(st, 3);
  10953. get_val(st, data);
  10954. if( data_isstring(data) )
  10955. {
  10956. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  10957. if( sd != NULL )
  10958. target_bl = &sd->bl;
  10959. }
  10960. if( target_bl == NULL )
  10961. target_bl = map_id2bl(conv_num(st, data));
  10962. // set mob as slave
  10963. if( target_bl != NULL )
  10964. {
  10965. struct unit_data* ud;
  10966. md->master_id = target_bl->id;
  10967. md->state.killer = 1;
  10968. mob_convertslave(md);
  10969. ud = unit_bl2ud(mob_bl);
  10970. if( ud != NULL )
  10971. {
  10972. if( ud->target != 0 )
  10973. md->target_id = ud->target;
  10974. else if( ud->skilltarget != 0 )
  10975. md->target_id = ud->skilltarget;
  10976. if( md->target_id != 0 )
  10977. unit_walktobl(&md->bl, map_id2bl(md->target_id), 65025, 2);
  10978. }
  10979. }
  10980. }
  10981. return 0;
  10982. }
  10983. /// Attaches the current npc or the target npc to the mob unit
  10984. ///
  10985. /// mobattach <mob unit id>{,"<npc name>"};
  10986. BUILDIN_FUNC(mobattach)
  10987. {
  10988. struct block_list* mob_bl;
  10989. mob_bl = map_id2bl(script_getnum(st,2));
  10990. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  10991. {
  10992. TBL_MOB* md = (TBL_MOB*)mob_bl;
  10993. TBL_NPC* nd = NULL;
  10994. if( script_hasdata(st,3) )
  10995. nd = npc_name2id(script_getstr(st, 3));
  10996. else
  10997. {
  10998. struct block_list* npc_bl = map_id2bl(st->oid);
  10999. if( npc_bl != NULL && npc_bl->type == BL_NPC )
  11000. nd = (TBL_NPC*)npc_bl;
  11001. }
  11002. if( nd != NULL )
  11003. md->nd = nd;
  11004. }
  11005. return 0;
  11006. }
  11007. /// Makes the unit walk to target position or map
  11008. /// Returns if it was successfull
  11009. ///
  11010. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11011. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11012. BUILDIN_FUNC(unitwalk)
  11013. {
  11014. struct block_list* bl;
  11015. bl = map_id2bl(script_getnum(st,2));
  11016. if( bl == NULL )
  11017. {
  11018. script_pushint(st, 0);
  11019. }
  11020. else if( script_hasdata(st,4) )
  11021. {
  11022. int x = script_getnum(st,3);
  11023. int y = script_getnum(st,4);
  11024. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11025. }
  11026. else
  11027. {
  11028. int map_id = script_getnum(st,3);
  11029. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11030. }
  11031. return 0;
  11032. }
  11033. /// Kills the unit
  11034. ///
  11035. /// unitkill <unit_id>;
  11036. BUILDIN_FUNC(unitkill)
  11037. {
  11038. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11039. if( bl != NULL )
  11040. status_kill(bl);
  11041. return 0;
  11042. }
  11043. /// Warps the unit to the target position in the target map
  11044. /// Returns if it was successfull
  11045. ///
  11046. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11047. BUILDIN_FUNC(unitwarp)
  11048. {
  11049. int unit_id;
  11050. int map;
  11051. short x;
  11052. short y;
  11053. struct block_list* bl;
  11054. unit_id = script_getnum(st,2);
  11055. map = map_mapname2mapid(script_getstr(st, 3));
  11056. x = (short)script_getnum(st,4);
  11057. y = (short)script_getnum(st,5);
  11058. bl = map_id2bl(unit_id);
  11059. if( map >= 0 && bl != NULL )
  11060. script_pushint(st, unit_warp(bl,map,x,y,0));
  11061. else
  11062. script_pushint(st, 0);
  11063. return 0;
  11064. }
  11065. /// Makes the unit attack the target.
  11066. /// If the unit is a player and <action type> is not 0, it does a continuous
  11067. /// attack instead of a single attack.
  11068. /// Returns if the request was successfull.
  11069. ///
  11070. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11071. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11072. BUILDIN_FUNC(unitattack)
  11073. {
  11074. struct block_list* unit_bl;
  11075. struct block_list* target_bl = NULL;
  11076. struct script_data* data;
  11077. int actiontype = 0;
  11078. // get unit
  11079. unit_bl = map_id2bl(script_getnum(st,2));
  11080. if( unit_bl == NULL ) {
  11081. script_pushint(st, 0);
  11082. return 0;
  11083. }
  11084. data = script_getdata(st, 3);
  11085. get_val(st, data);
  11086. if( data_isstring(data) )
  11087. {
  11088. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11089. if( sd != NULL )
  11090. target_bl = &sd->bl;
  11091. } else
  11092. target_bl = map_id2bl(conv_num(st, data));
  11093. // request the attack
  11094. if( target_bl == NULL )
  11095. {
  11096. script_pushint(st, 0);
  11097. return 0;
  11098. }
  11099. // get actiontype
  11100. if( script_hasdata(st,4) )
  11101. actiontype = script_getnum(st,4);
  11102. switch( unit_bl->type )
  11103. {
  11104. case BL_PC:
  11105. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11106. script_pushint(st, 1);
  11107. return 0;
  11108. case BL_MOB:
  11109. ((TBL_MOB *)unit_bl)->state.killer = 1;
  11110. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11111. break;
  11112. case BL_PET:
  11113. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11114. break;
  11115. default:
  11116. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11117. script_pushint(st, 0);
  11118. return 1;
  11119. }
  11120. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11121. return 0;
  11122. }
  11123. /// Makes the unit stop attacking and moving
  11124. ///
  11125. /// unitstop <unit_id>;
  11126. BUILDIN_FUNC(unitstop)
  11127. {
  11128. int unit_id;
  11129. struct block_list* bl;
  11130. unit_id = script_getnum(st,2);
  11131. bl = map_id2bl(unit_id);
  11132. if( bl != NULL )
  11133. {
  11134. unit_stop_attack(bl);
  11135. unit_stop_walking(bl,0);
  11136. if( bl->type == BL_MOB )
  11137. ((TBL_MOB*)bl)->target_id = 0;
  11138. }
  11139. return 0;
  11140. }
  11141. /// Makes the unit say the message
  11142. ///
  11143. /// unittalk <unit_id>,"<message>";
  11144. BUILDIN_FUNC(unittalk)
  11145. {
  11146. int unit_id;
  11147. const char* message;
  11148. struct block_list* bl;
  11149. unit_id = script_getnum(st,2);
  11150. message = script_getstr(st, 3);
  11151. bl = map_id2bl(unit_id);
  11152. if( bl != NULL )
  11153. {
  11154. struct StringBuf sbuf;
  11155. StringBuf_Init(&sbuf);
  11156. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11157. clif_message(bl, StringBuf_Value(&sbuf));
  11158. if( bl->type == BL_PC )
  11159. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11160. StringBuf_Destroy(&sbuf);
  11161. }
  11162. return 0;
  11163. }
  11164. /// Makes the unit do an emotion
  11165. ///
  11166. /// unitemote <unit_id>,<emotion>;
  11167. ///
  11168. /// @see e_* in const.txt
  11169. BUILDIN_FUNC(unitemote)
  11170. {
  11171. int unit_id;
  11172. int emotion;
  11173. struct block_list* bl;
  11174. unit_id = script_getnum(st,2);
  11175. emotion = script_getnum(st,3);
  11176. bl = map_id2bl(unit_id);
  11177. if( bl != NULL )
  11178. clif_emotion(bl, emotion);
  11179. return 0;
  11180. }
  11181. /// Makes the unit cast the skill on the target or self if no target is specified
  11182. ///
  11183. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11184. BUILDIN_FUNC(unitskilluseid)
  11185. {
  11186. int unit_id;
  11187. int skill_id;
  11188. int skill_lv;
  11189. int target_id;
  11190. struct block_list* bl;
  11191. unit_id = script_getnum(st,2);
  11192. skill_id = script_getnum(st,3);
  11193. skill_lv = script_getnum(st,4);
  11194. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11195. bl = map_id2bl(unit_id);
  11196. if( bl != NULL )
  11197. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11198. return 0;
  11199. }
  11200. /// Makes the unit cast the skill on the target position.
  11201. ///
  11202. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11203. BUILDIN_FUNC(unitskillusepos)
  11204. {
  11205. int unit_id;
  11206. int skill_id;
  11207. int skill_lv;
  11208. int skill_x;
  11209. int skill_y;
  11210. struct block_list* bl;
  11211. unit_id = script_getnum(st,2);
  11212. skill_id = script_getnum(st,3);
  11213. skill_lv = script_getnum(st,4);
  11214. skill_x = script_getnum(st,5);
  11215. skill_y = script_getnum(st,6);
  11216. bl = map_id2bl(unit_id);
  11217. if( bl != NULL )
  11218. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11219. return 0;
  11220. }
  11221. // <--- [zBuffer] List of mob control commands
  11222. /// Pauses the execution of the script, detaching the player
  11223. ///
  11224. /// sleep <mili seconds>;
  11225. BUILDIN_FUNC(sleep)
  11226. {
  11227. int ticks;
  11228. TBL_PC* sd;
  11229. ticks = script_getnum(st,2);
  11230. sd = map_id2sd(st->rid);
  11231. // detach the player
  11232. if( sd && sd->npc_id == st->oid )
  11233. {
  11234. sd->npc_id = 0;
  11235. }
  11236. st->rid = 0;
  11237. if( ticks <= 0 )
  11238. {// do nothing
  11239. }
  11240. else if( st->sleep.tick == 0 )
  11241. {// sleep for the target amount of time
  11242. st->state = RERUNLINE;
  11243. st->sleep.tick = ticks;
  11244. }
  11245. else
  11246. {// sleep time is over
  11247. st->sleep.tick = 0;
  11248. }
  11249. return 0;
  11250. }
  11251. /// Pauses the execution of the script, keeping the player attached
  11252. /// Returns if a player is still attached
  11253. ///
  11254. /// sleep2(<mili secconds>) -> <bool>
  11255. BUILDIN_FUNC(sleep2)
  11256. {
  11257. int ticks;
  11258. ticks = script_getnum(st,2);
  11259. if( ticks <= 0 )
  11260. {// do nothing
  11261. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11262. }
  11263. else if( !st->sleep.tick )
  11264. {// sleep for the target amount of time
  11265. st->state = RERUNLINE;
  11266. st->sleep.tick = ticks;
  11267. }
  11268. else
  11269. {// sleep time is over
  11270. st->sleep.tick = 0;
  11271. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11272. }
  11273. return 0;
  11274. }
  11275. /// Awakes all the sleep timers of the target npc
  11276. ///
  11277. /// awake "<npc name>";
  11278. BUILDIN_FUNC(awake)
  11279. {
  11280. struct npc_data* nd;
  11281. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11282. nd = npc_name2id(script_getstr(st, 2));
  11283. if( nd == NULL )
  11284. return 0;
  11285. while( node )
  11286. {
  11287. if( (int)node->key == nd->bl.id )
  11288. {// sleep timer for the npc
  11289. struct script_state* tst = (struct script_state*)node->data;
  11290. TBL_PC* sd = map_id2sd(tst->rid);
  11291. if( tst->sleep.timer == -1 )
  11292. {// already awake ???
  11293. node = node->next;
  11294. continue;
  11295. }
  11296. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11297. {// char not online anymore / another char of the same account is online - Cancel execution
  11298. tst->state = END;
  11299. tst->rid = 0;
  11300. }
  11301. delete_timer(tst->sleep.timer, run_script_timer);
  11302. node = script_erase_sleepdb(node);
  11303. tst->sleep.timer = -1;
  11304. tst->sleep.tick = 0;
  11305. run_script_main(tst);
  11306. }
  11307. else
  11308. {
  11309. node = node->next;
  11310. }
  11311. }
  11312. return 0;
  11313. }
  11314. /// Returns a reference to a variable of the target NPC.
  11315. /// Returns 0 if an error occurs.
  11316. ///
  11317. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11318. BUILDIN_FUNC(getvariableofnpc)
  11319. {
  11320. struct script_data* data;
  11321. char* name;
  11322. struct npc_data* nd;
  11323. data = script_getdata(st,2);
  11324. if( !data_isreference(data) )
  11325. {// Not a reference (aka varaible name)
  11326. ShowError("script:getvariableofnpc: not a variable\n");
  11327. script_reportdata(data);
  11328. script_pushnil(st);
  11329. st->state = END;
  11330. return 1;
  11331. }
  11332. name = reference_getname(data);
  11333. if( *name != '.' || name[1] == '@' )
  11334. {// not a npc variable
  11335. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11336. script_reportdata(data);
  11337. script_pushnil(st);
  11338. st->state = END;
  11339. return 1;
  11340. }
  11341. nd = npc_name2id(script_getstr(st,3));
  11342. if( nd == NULL || nd->bl.subtype != SCRIPT || nd->u.scr.script == NULL )
  11343. {// NPC not found or has no script
  11344. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11345. script_pushnil(st);
  11346. st->state = END;
  11347. return 1;
  11348. }
  11349. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11350. return 0;
  11351. }
  11352. /// Opens a warp portal.
  11353. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11354. ///
  11355. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11356. ///
  11357. /// @author blackhole89
  11358. BUILDIN_FUNC(warpportal)
  11359. {
  11360. int spx;
  11361. int spy;
  11362. unsigned short mapindex;
  11363. int tpx;
  11364. int tpy;
  11365. struct skill_unit_group* group;
  11366. struct block_list* bl;
  11367. bl = map_id2bl(st->oid);
  11368. if( bl == NULL )
  11369. {
  11370. ShowError("script:warpportal: npc is needed\n");
  11371. return 1;
  11372. }
  11373. spx = script_getnum(st,2);
  11374. spy = script_getnum(st,3);
  11375. mapindex = mapindex_name2id(script_getstr(st, 4));
  11376. tpx = script_getnum(st,5);
  11377. tpy = script_getnum(st,6);
  11378. if( mapindex == 0 )
  11379. return 0;// map not found
  11380. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 1);
  11381. if( group == NULL )
  11382. return 0;// failed
  11383. group->val2 = (tpx<<16) | tpy;
  11384. group->val3 = mapindex;
  11385. return 0;
  11386. }
  11387. BUILDIN_FUNC(openmail)
  11388. {
  11389. #ifndef TXT_ONLY
  11390. mail_openmail(script_rid2sd(st));
  11391. #endif
  11392. return 0;
  11393. }
  11394. // declarations that were supposed to be exported from npc_chat.c
  11395. #ifdef PCRE_SUPPORT
  11396. BUILDIN_FUNC(defpattern);
  11397. BUILDIN_FUNC(activatepset);
  11398. BUILDIN_FUNC(deactivatepset);
  11399. BUILDIN_FUNC(deletepset);
  11400. #endif
  11401. struct script_function buildin_func[] = {
  11402. // NPC interaction
  11403. BUILDIN_DEF(mes,"s"),
  11404. BUILDIN_DEF(next,""),
  11405. BUILDIN_DEF(close,""),
  11406. BUILDIN_DEF(close2,""),
  11407. BUILDIN_DEF(menu,"sl*"),
  11408. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  11409. BUILDIN_DEF(prompt,"s*"),
  11410. //
  11411. BUILDIN_DEF(goto,"l"),
  11412. BUILDIN_DEF(callsub,"i*"),
  11413. BUILDIN_DEF(callfunc,"s*"),
  11414. BUILDIN_DEF(return,"?"),
  11415. BUILDIN_DEF(getarg,"i?"),
  11416. BUILDIN_DEF(jobchange,"i*"),
  11417. BUILDIN_DEF(jobname,"i"),
  11418. BUILDIN_DEF(input,"v"),
  11419. BUILDIN_DEF(warp,"sii"),
  11420. BUILDIN_DEF(areawarp,"siiiisii"),
  11421. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  11422. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  11423. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  11424. BUILDIN_DEF(setlook,"ii"),
  11425. BUILDIN_DEF(set,"ii"),
  11426. BUILDIN_DEF(setarray,"rv*"),
  11427. BUILDIN_DEF(cleararray,"rvi"),
  11428. BUILDIN_DEF(copyarray,"rri"),
  11429. BUILDIN_DEF(getarraysize,"r"),
  11430. BUILDIN_DEF(deletearray,"r?"),
  11431. BUILDIN_DEF(getelementofarray,"ri"),
  11432. BUILDIN_DEF(getitem,"vi?"),
  11433. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  11434. BUILDIN_DEF(getnameditem,"is"),
  11435. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  11436. BUILDIN_DEF(makeitem,"iisii"),
  11437. BUILDIN_DEF(delitem,"ii"),
  11438. BUILDIN_DEF(delitem2,"iiiiiiiii"),
  11439. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  11440. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  11441. BUILDIN_DEF(cutin,"si"),
  11442. BUILDIN_DEF(viewpoint,"iiiii"),
  11443. BUILDIN_DEF(heal,"ii"),
  11444. BUILDIN_DEF(itemheal,"ii"),
  11445. BUILDIN_DEF(percentheal,"ii"),
  11446. BUILDIN_DEF(rand,"i?"),
  11447. BUILDIN_DEF(countitem,"i"),
  11448. BUILDIN_DEF(countitem2,"iiiiiiii"),
  11449. BUILDIN_DEF(checkweight,"ii"),
  11450. BUILDIN_DEF(readparam,"i*"),
  11451. BUILDIN_DEF(getcharid,"i*"),
  11452. BUILDIN_DEF(getpartyname,"i"),
  11453. BUILDIN_DEF(getpartymember,"i*"),
  11454. BUILDIN_DEF(getpartyleader,"i?"),
  11455. BUILDIN_DEF(getguildname,"i"),
  11456. BUILDIN_DEF(getguildmaster,"i"),
  11457. BUILDIN_DEF(getguildmasterid,"i"),
  11458. BUILDIN_DEF(strcharinfo,"i"),
  11459. BUILDIN_DEF(strnpcinfo,"i"),
  11460. BUILDIN_DEF(getequipid,"i"),
  11461. BUILDIN_DEF(getequipname,"i"),
  11462. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  11463. BUILDIN_DEF(repair,"i"), // [Valaris]
  11464. BUILDIN_DEF(getequipisequiped,"i"),
  11465. BUILDIN_DEF(getequipisenableref,"i"),
  11466. BUILDIN_DEF(getequipisidentify,"i"),
  11467. BUILDIN_DEF(getequiprefinerycnt,"i"),
  11468. BUILDIN_DEF(getequipweaponlv,"i"),
  11469. BUILDIN_DEF(getequippercentrefinery,"i"),
  11470. BUILDIN_DEF(successrefitem,"i"),
  11471. BUILDIN_DEF(failedrefitem,"i"),
  11472. BUILDIN_DEF(statusup,"i"),
  11473. BUILDIN_DEF(statusup2,"ii"),
  11474. BUILDIN_DEF(bonus,"ii"),
  11475. BUILDIN_DEF2(bonus,"bonus2","iii"),
  11476. BUILDIN_DEF2(bonus,"bonus3","iiii"),
  11477. BUILDIN_DEF2(bonus,"bonus4","iiiii"),
  11478. BUILDIN_DEF2(bonus,"bonus5","iiiiii"),
  11479. BUILDIN_DEF(bonusautoscript,"si?"),
  11480. BUILDIN_DEF(bonusautoscript2,"si?"),
  11481. BUILDIN_DEF(skill,"ii?"),
  11482. BUILDIN_DEF(addtoskill,"ii?"), // [Valaris]
  11483. BUILDIN_DEF(guildskill,"ii"),
  11484. BUILDIN_DEF(getskilllv,"i"),
  11485. BUILDIN_DEF(getgdskilllv,"ii"),
  11486. BUILDIN_DEF(basicskillcheck,""),
  11487. BUILDIN_DEF(getgmlevel,""),
  11488. BUILDIN_DEF(end,""),
  11489. BUILDIN_DEF(checkoption,"i"),
  11490. BUILDIN_DEF(setoption,"i?"),
  11491. BUILDIN_DEF(setcart,"?"),
  11492. BUILDIN_DEF(checkcart,""),
  11493. BUILDIN_DEF(setfalcon,"?"),
  11494. BUILDIN_DEF(checkfalcon,""),
  11495. BUILDIN_DEF(setriding,"?"),
  11496. BUILDIN_DEF(checkriding,""),
  11497. BUILDIN_DEF2(savepoint,"save","sii"),
  11498. BUILDIN_DEF(savepoint,"sii"),
  11499. BUILDIN_DEF(gettimetick,"i"),
  11500. BUILDIN_DEF(gettime,"i"),
  11501. BUILDIN_DEF(gettimestr,"si"),
  11502. BUILDIN_DEF(openstorage,""),
  11503. BUILDIN_DEF(guildopenstorage,"*"),
  11504. BUILDIN_DEF(itemskill,"ii"),
  11505. BUILDIN_DEF(produce,"i"),
  11506. BUILDIN_DEF(monster,"siisii*"),
  11507. BUILDIN_DEF(areamonster,"siiiisii*"),
  11508. BUILDIN_DEF(killmonster,"ss"),
  11509. BUILDIN_DEF(killmonsterall,"s"),
  11510. BUILDIN_DEF(clone,"siisi*"),
  11511. BUILDIN_DEF(doevent,"s"),
  11512. BUILDIN_DEF(donpcevent,"s"),
  11513. BUILDIN_DEF(cmdothernpc,"ss"),
  11514. BUILDIN_DEF(addtimer,"is"),
  11515. BUILDIN_DEF(deltimer,"s"),
  11516. BUILDIN_DEF(addtimercount,"si"),
  11517. BUILDIN_DEF(initnpctimer,"??"),
  11518. BUILDIN_DEF(stopnpctimer,"??"),
  11519. BUILDIN_DEF(startnpctimer,"??"),
  11520. BUILDIN_DEF(setnpctimer,"i?"),
  11521. BUILDIN_DEF(getnpctimer,"i?"),
  11522. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  11523. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  11524. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  11525. BUILDIN_DEF(announce,"si*"),
  11526. BUILDIN_DEF(mapannounce,"ssi*"),
  11527. BUILDIN_DEF(areaannounce,"siiiisi*"),
  11528. BUILDIN_DEF(getusers,"i"),
  11529. BUILDIN_DEF(getmapguildusers,"si"),
  11530. BUILDIN_DEF(getmapusers,"s"),
  11531. BUILDIN_DEF(getareausers,"siiii"),
  11532. BUILDIN_DEF(getareadropitem,"siiiii"),
  11533. BUILDIN_DEF(enablenpc,"s"),
  11534. BUILDIN_DEF(disablenpc,"s"),
  11535. BUILDIN_DEF(hideoffnpc,"s"),
  11536. BUILDIN_DEF(hideonnpc,"s"),
  11537. BUILDIN_DEF(sc_start,"iii?"),
  11538. BUILDIN_DEF(sc_start2,"iiii?"),
  11539. BUILDIN_DEF(sc_start4,"iiiiii?"),
  11540. BUILDIN_DEF(sc_end,"i?"),
  11541. BUILDIN_DEF(getscrate,"ii*"),
  11542. BUILDIN_DEF(debugmes,"s"),
  11543. BUILDIN_DEF2(catchpet,"pet","i"),
  11544. BUILDIN_DEF2(birthpet,"bpet",""),
  11545. BUILDIN_DEF(resetlvl,"i"),
  11546. BUILDIN_DEF(resetstatus,""),
  11547. BUILDIN_DEF(resetskill,""),
  11548. BUILDIN_DEF(skillpointcount,""),
  11549. BUILDIN_DEF(changebase,"i"),
  11550. BUILDIN_DEF(changesex,""),
  11551. BUILDIN_DEF(waitingroom,"si??"),
  11552. BUILDIN_DEF(delwaitingroom,"?"),
  11553. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  11554. BUILDIN_DEF(enablewaitingroomevent,"?"),
  11555. BUILDIN_DEF(disablewaitingroomevent,"?"),
  11556. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  11557. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  11558. BUILDIN_DEF(getwaitingroomstate,"i?"),
  11559. BUILDIN_DEF(warpwaitingpc,"sii?"),
  11560. BUILDIN_DEF(attachrid,"i"),
  11561. BUILDIN_DEF(detachrid,""),
  11562. BUILDIN_DEF(isloggedin,"i?"),
  11563. BUILDIN_DEF(setmapflagnosave,"ssii"),
  11564. BUILDIN_DEF(setmapflag,"si*"),
  11565. BUILDIN_DEF(removemapflag,"si"),
  11566. BUILDIN_DEF(pvpon,"s"),
  11567. BUILDIN_DEF(pvpoff,"s"),
  11568. BUILDIN_DEF(gvgon,"s"),
  11569. BUILDIN_DEF(gvgoff,"s"),
  11570. BUILDIN_DEF(emotion,"i*"),
  11571. BUILDIN_DEF(maprespawnguildid,"sii"),
  11572. BUILDIN_DEF(agitstart,""), // <Agit>
  11573. BUILDIN_DEF(agitend,""),
  11574. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  11575. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  11576. BUILDIN_DEF(getcastlename,"s"),
  11577. BUILDIN_DEF(getcastledata,"si*"),
  11578. BUILDIN_DEF(setcastledata,"sii"),
  11579. BUILDIN_DEF(requestguildinfo,"i*"),
  11580. BUILDIN_DEF(getequipcardcnt,"i"),
  11581. BUILDIN_DEF(successremovecards,"i"),
  11582. BUILDIN_DEF(failedremovecards,"ii"),
  11583. BUILDIN_DEF(marriage,"s"),
  11584. BUILDIN_DEF2(wedding_effect,"wedding",""),
  11585. BUILDIN_DEF(divorce,""),
  11586. BUILDIN_DEF(ispartneron,""),
  11587. BUILDIN_DEF(getpartnerid,""),
  11588. BUILDIN_DEF(getchildid,""),
  11589. BUILDIN_DEF(getmotherid,""),
  11590. BUILDIN_DEF(getfatherid,""),
  11591. BUILDIN_DEF(warppartner,"sii"),
  11592. BUILDIN_DEF(getitemname,"i"),
  11593. BUILDIN_DEF(getitemslots,"i"),
  11594. BUILDIN_DEF(makepet,"i"),
  11595. BUILDIN_DEF(getexp,"ii"),
  11596. BUILDIN_DEF(getinventorylist,""),
  11597. BUILDIN_DEF(getskilllist,""),
  11598. BUILDIN_DEF(clearitem,""),
  11599. BUILDIN_DEF(classchange,"ii"),
  11600. BUILDIN_DEF(misceffect,"i"),
  11601. BUILDIN_DEF(soundeffect,"si"),
  11602. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  11603. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  11604. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  11605. BUILDIN_DEF(guardianinfo,"i"), // display guardian data [Valaris]
  11606. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  11607. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  11608. BUILDIN_DEF(petloot,"i"), // [Valaris]
  11609. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  11610. BUILDIN_DEF(petskillattack,"iiii"), // [Skotlex]
  11611. BUILDIN_DEF(petskillattack2,"iiiii"), // [Valaris]
  11612. BUILDIN_DEF(petskillsupport,"iiiii"), // [Skotlex]
  11613. BUILDIN_DEF(skilleffect,"ii"), // skill effect [Celest]
  11614. BUILDIN_DEF(npcskilleffect,"iiii"), // npc skill effect [Valaris]
  11615. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  11616. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  11617. BUILDIN_DEF(nude,""), // nude command [Valaris]
  11618. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  11619. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  11620. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  11621. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  11622. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  11623. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  11624. BUILDIN_DEF(mobcount,"ss"),
  11625. BUILDIN_DEF(getlook,"i"),
  11626. BUILDIN_DEF(getsavepoint,"i"),
  11627. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  11628. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  11629. BUILDIN_DEF(npcstop,""), // [Valaris]
  11630. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  11631. BUILDIN_DEF(checkoption1,"i"),
  11632. BUILDIN_DEF(checkoption2,"i"),
  11633. BUILDIN_DEF(guildgetexp,"i"),
  11634. BUILDIN_DEF(guildchangegm,"is"),
  11635. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  11636. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  11637. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  11638. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  11639. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  11640. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  11641. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  11642. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  11643. BUILDIN_DEF(adopt,"sss"), // allows 2 parents to adopt a child
  11644. BUILDIN_DEF(night,""), // sets the server to night time
  11645. BUILDIN_DEF(day,""), // sets the server to day time
  11646. #ifdef PCRE_SUPPORT
  11647. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  11648. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  11649. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  11650. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  11651. #endif
  11652. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  11653. BUILDIN_DEF(getusersname,"*"),
  11654. BUILDIN_DEF(recovery,""),
  11655. BUILDIN_DEF(getpetinfo,"i"),
  11656. BUILDIN_DEF(gethominfo,"i"),
  11657. BUILDIN_DEF(checkequipedcard,"i"),
  11658. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  11659. BUILDIN_DEF(globalmes,"s*"),
  11660. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  11661. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  11662. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  11663. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  11664. BUILDIN_DEF(fakenpcname,"ssi"), // [Lance]
  11665. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  11666. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  11667. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  11668. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  11669. // [zBuffer] List of mathematics commands --->
  11670. BUILDIN_DEF(sqrt,"i"),
  11671. BUILDIN_DEF(pow,"ii"),
  11672. BUILDIN_DEF(distance,"iiii"),
  11673. BUILDIN_DEF(checkcell,"siii"),
  11674. // <--- [zBuffer] List of mathematics commands
  11675. // [zBuffer] List of dynamic var commands --->
  11676. BUILDIN_DEF(getd,"*"),
  11677. BUILDIN_DEF(setd,"*"),
  11678. // <--- [zBuffer] List of dynamic var commands
  11679. BUILDIN_DEF(petstat,"i"),
  11680. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  11681. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  11682. BUILDIN_DEF(npcshopadditem,"sii*"),
  11683. BUILDIN_DEF(npcshopdelitem,"si*"),
  11684. BUILDIN_DEF(npcshopattach,"s?"),
  11685. BUILDIN_DEF(equip,"i"),
  11686. BUILDIN_DEF(autoequip,"ii"),
  11687. BUILDIN_DEF(setbattleflag,"ss"),
  11688. BUILDIN_DEF(getbattleflag,"s"),
  11689. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  11690. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  11691. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  11692. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  11693. BUILDIN_DEF(axtoi,"s"),
  11694. BUILDIN_DEF(query_sql,"s*"),
  11695. BUILDIN_DEF(escape_sql,"s"),
  11696. BUILDIN_DEF(atoi,"s"),
  11697. // [zBuffer] List of player cont commands --->
  11698. BUILDIN_DEF(rid2name,"i"),
  11699. BUILDIN_DEF(pcfollow,"ii"),
  11700. BUILDIN_DEF(pcstopfollow,"i"),
  11701. BUILDIN_DEF(pcblockmove,"ii"),
  11702. // <--- [zBuffer] List of player cont commands
  11703. // [zBuffer] List of mob control commands --->
  11704. BUILDIN_DEF(mobspawn,"*"),
  11705. BUILDIN_DEF(mobremove,"i"),
  11706. BUILDIN_DEF(getmobdata,"i*"),
  11707. BUILDIN_DEF(setmobdata,"iii"),
  11708. BUILDIN_DEF(mobassist,"i?"),
  11709. BUILDIN_DEF(mobattach,"i?"),
  11710. BUILDIN_DEF(unitwalk,"ii?"),
  11711. BUILDIN_DEF(unitkill,"i"),
  11712. BUILDIN_DEF(unitwarp,"isii"),
  11713. BUILDIN_DEF(unitattack,"iv?"),
  11714. BUILDIN_DEF(unitstop,"i"),
  11715. BUILDIN_DEF(unittalk,"is"),
  11716. BUILDIN_DEF(unitemote,"ii"),
  11717. BUILDIN_DEF(unitskilluseid,"iii?"), // originally by Qamera [Celest]
  11718. BUILDIN_DEF(unitskillusepos,"iiiii"), // [Celest]
  11719. // <--- [zBuffer] List of mob control commands
  11720. BUILDIN_DEF(sleep,"i"),
  11721. BUILDIN_DEF(sleep2,"i"),
  11722. BUILDIN_DEF(awake,"s"),
  11723. BUILDIN_DEF(getvariableofnpc,"rs"),
  11724. BUILDIN_DEF(warpportal,"iisii"),
  11725. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  11726. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  11727. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  11728. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  11729. BUILDIN_DEF(checkvending,"*"),
  11730. BUILDIN_DEF(checkchatting,"*"),
  11731. BUILDIN_DEF(openmail,""),
  11732. {NULL,NULL,NULL},
  11733. };