script.c 329 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_DISP
  4. //#define DEBUG_DISASM
  5. //#define DEBUG_RUN
  6. //#define DEBUG_HASH
  7. #include "../common/cbasetypes.h"
  8. #include "../common/malloc.h"
  9. #include "../common/lock.h"
  10. #include "../common/nullpo.h"
  11. #include "../common/showmsg.h"
  12. #include "../common/strlib.h"
  13. #include "../common/timer.h"
  14. #include "../common/utils.h"
  15. #include "map.h"
  16. #include "path.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 "mapreg.h"
  27. #include "homunculus.h"
  28. #include "mercenary.h"
  29. #include "intif.h"
  30. #include "skill.h"
  31. #include "status.h"
  32. #include "chat.h"
  33. #include "battle.h"
  34. #include "party.h"
  35. #include "guild.h"
  36. #include "atcommand.h"
  37. #include "log.h"
  38. #include "unit.h"
  39. #include "pet.h"
  40. #include "mail.h"
  41. #include "script.h"
  42. #include "quest.h"
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <math.h>
  47. #ifndef WIN32
  48. #include <sys/time.h>
  49. #endif
  50. #include <time.h>
  51. #include <setjmp.h>
  52. #include <errno.h>
  53. ///////////////////////////////////////////////////////////////////////////////
  54. //## TODO possible enhancements: [FlavioJS]
  55. // - 'callfunc' supporting labels in the current npc "::LabelName"
  56. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  57. // - 'function FuncName;' function declarations reverting to global functions
  58. // if local label isn't found
  59. // - join callfunc and callsub's functionality
  60. // - use script_op2name in the DEBUG_DISASM block
  61. // - remove dynamic allocation in add_word()
  62. // - remove GETVALUE / SETVALUE
  63. // - clean up the set_reg / set_val / setd_sub mess
  64. //
  65. // struct script_state* st;
  66. //
  67. /// Returns the script_data at the target index
  68. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  69. /// Returns if the stack contains data at the target index
  70. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  71. /// Returns the index of the last data in the stack
  72. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  73. /// Pushes an int into the stack
  74. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  75. /// Pushes a string into the stack (script engine frees it automatically)
  76. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  77. /// Pushes a copy of a string into the stack
  78. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  79. /// Pushes a constant string into the stack (must never change or be freed)
  80. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  81. /// Pushes a nil into the stack
  82. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  83. /// Pushes a copy of the data in the target index
  84. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  85. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  86. #define script_isint(st,i) data_isint(script_getdata(st,i))
  87. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  88. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  89. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  90. // Note: "top" functions/defines use indexes relative to the top of the stack
  91. // -1 is the index of the data at the top
  92. /// Returns the script_data at the target index relative to the top of the stack
  93. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  94. /// Pushes a copy of the data in the target index relative to the top of the stack
  95. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  96. /// Removes the range of values [start,end[ relative to the top of the stack
  97. #define script_removetop(st,start,end) ( pop_stack((st)->stack, ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  98. //
  99. // struct script_data* data;
  100. //
  101. /// Returns if the script data is a string
  102. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  103. /// Returns if the script data is an int
  104. #define data_isint(data) ( (data)->type == C_INT )
  105. /// Returns if the script data is a reference
  106. #define data_isreference(data) ( (data)->type == C_NAME )
  107. /// Returns if the script data is a label
  108. #define data_islabel(data) ( (data)->type == C_POS )
  109. /// Returns if the script data is an internal script function label
  110. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  111. /// Returns if this is a reference to a constant
  112. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  113. /// Returns if this a reference to a param
  114. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  115. /// Returns if this a reference to a variable
  116. //##TODO confirm it's C_NAME [FlavioJS]
  117. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  118. /// Returns the unique id of the reference (id and index)
  119. #define reference_getuid(data) ( (data)->u.num )
  120. /// Returns the id of the reference
  121. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  122. /// Returns the array index of the reference
  123. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  124. /// Returns the name of the reference
  125. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  126. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  127. #define reference_getref(data) ( (data)->ref )
  128. /// Returns the value of the constant
  129. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  130. /// Returns the type of param
  131. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  132. /// Composes the uid of a reference from the id and the index
  133. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  134. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.')
  135. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' )
  136. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  137. #define FETCH(n, t) \
  138. if( script_hasdata(st,n) ) \
  139. (t)=script_getnum(st,n);
  140. #define SCRIPT_BLOCK_SIZE 512
  141. enum { LABEL_NEXTLINE=1,LABEL_START };
  142. /// temporary buffer for passing around compiled bytecode
  143. /// @see add_scriptb, set_label, parse_script
  144. static unsigned char* script_buf = NULL;
  145. static int script_pos = 0, script_size = 0;
  146. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  147. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  148. // String buffer structures.
  149. // str_data stores string information
  150. static struct str_data_struct {
  151. enum c_op type;
  152. int str;
  153. int backpatch;
  154. int label;
  155. int (*func)(struct script_state *st);
  156. int val;
  157. int next;
  158. } *str_data = NULL;
  159. static int str_data_size = 0; // size of the data
  160. static int str_num = LABEL_START; // next id to be assigned
  161. // str_buf holds the strings themselves
  162. static char *str_buf;
  163. static int str_size = 0; // size of the buffer
  164. static int str_pos = 0; // next position to be assigned
  165. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  166. #define SCRIPT_HASH_SIZE 1021
  167. int str_hash[SCRIPT_HASH_SIZE];
  168. // Specifies which string hashing method to use
  169. //#define SCRIPT_HASH_DJB2
  170. //#define SCRIPT_HASH_SDBM
  171. #define SCRIPT_HASH_ELF
  172. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  173. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  174. static int parse_options=0;
  175. DBMap* script_get_label_db(){ return scriptlabel_db; }
  176. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  177. struct Script_Config script_config = {
  178. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  179. 0, INT_MAX, // input_min_value/input_max_value
  180. "OnPCDieEvent", //die_event_name
  181. "OnPCKillEvent", //kill_pc_event_name
  182. "OnNPCKillEvent", //kill_mob_event_name
  183. "OnPCLoginEvent", //login_event_name
  184. "OnPCLogoutEvent", //logout_event_name
  185. "OnPCLoadMapEvent", //loadmap_event_name
  186. "OnPCBaseLvUpEvent", //baselvup_event_name
  187. "OnPCJobLvUpEvent" //joblvup_event_name
  188. };
  189. static jmp_buf error_jump;
  190. static char* error_msg;
  191. static const char* error_pos;
  192. static int error_report; // if the error should produce output
  193. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  194. // [Eoe / jA 1080, 1081, 1094, 1164]
  195. enum curly_type {
  196. TYPE_NULL = 0,
  197. TYPE_IF,
  198. TYPE_SWITCH,
  199. TYPE_WHILE,
  200. TYPE_FOR,
  201. TYPE_DO,
  202. TYPE_USERFUNC,
  203. TYPE_ARGLIST // function argument list
  204. };
  205. #define ARGLIST_UNDEFINED 0
  206. #define ARGLIST_NO_PAREN 1
  207. #define ARGLIST_PAREN 2
  208. static struct {
  209. struct {
  210. enum curly_type type;
  211. int index;
  212. int count;
  213. int flag;
  214. struct linkdb_node *case_label;
  215. } curly[256]; // 右カッコの情報
  216. int curly_count; // 右カッコの数
  217. int index; // スクリプト内で使用した構文の数
  218. } syntax;
  219. const char* parse_curly_close(const char* p);
  220. const char* parse_syntax_close(const char* p);
  221. const char* parse_syntax_close_sub(const char* p,int* flag);
  222. const char* parse_syntax(const char* p);
  223. static int parse_syntax_for_flag = 0;
  224. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  225. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  226. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  227. int potion_target=0;
  228. c_op get_com(unsigned char *script,int *pos);
  229. int get_num(unsigned char *script,int *pos);
  230. typedef struct script_function {
  231. int (*func)(struct script_state *st);
  232. const char *name;
  233. const char *arg;
  234. } script_function;
  235. extern script_function buildin_func[];
  236. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  237. /*==========================================
  238. * ローカルプロトタイプ宣言 (必要な物のみ)
  239. *------------------------------------------*/
  240. const char* parse_subexpr(const char* p,int limit);
  241. void push_val(struct script_stack *stack,int type,int val);
  242. int run_func(struct script_state *st);
  243. enum {
  244. MF_NOMEMO, //0
  245. MF_NOTELEPORT,
  246. MF_NOSAVE,
  247. MF_NOBRANCH,
  248. MF_NOPENALTY,
  249. MF_NOZENYPENALTY,
  250. MF_PVP,
  251. MF_PVP_NOPARTY,
  252. MF_PVP_NOGUILD,
  253. MF_GVG,
  254. MF_GVG_NOPARTY, //10
  255. MF_NOTRADE,
  256. MF_NOSKILL,
  257. MF_NOWARP,
  258. MF_PARTYLOCK,
  259. MF_NOICEWALL,
  260. MF_SNOW,
  261. MF_FOG,
  262. MF_SAKURA,
  263. MF_LEAVES,
  264. MF_RAIN, //20
  265. MF_INDOORS,
  266. MF_NOGO,
  267. MF_CLOUDS,
  268. MF_CLOUDS2,
  269. MF_FIREWORKS,
  270. MF_GVG_CASTLE,
  271. MF_GVG_DUNGEON,
  272. MF_NIGHTENABLED,
  273. MF_NOBASEEXP,
  274. MF_NOJOBEXP, //30
  275. MF_NOMOBLOOT,
  276. MF_NOMVPLOOT,
  277. MF_NORETURN,
  278. MF_NOWARPTO,
  279. MF_NIGHTMAREDROP,
  280. MF_RESTRICTED,
  281. MF_NOCOMMAND,
  282. MF_NODROP,
  283. MF_JEXP,
  284. MF_BEXP, //40
  285. MF_NOVENDING,
  286. MF_LOADEVENT,
  287. MF_NOCHAT,
  288. MF_NOEXPPENALTY,
  289. MF_GUILDLOCK
  290. };
  291. const char* script_op2name(int op)
  292. {
  293. #define RETURN_OP_NAME(type) case type: return #type
  294. switch( op )
  295. {
  296. RETURN_OP_NAME(C_NOP);
  297. RETURN_OP_NAME(C_POS);
  298. RETURN_OP_NAME(C_INT);
  299. RETURN_OP_NAME(C_PARAM);
  300. RETURN_OP_NAME(C_FUNC);
  301. RETURN_OP_NAME(C_STR);
  302. RETURN_OP_NAME(C_CONSTSTR);
  303. RETURN_OP_NAME(C_ARG);
  304. RETURN_OP_NAME(C_NAME);
  305. RETURN_OP_NAME(C_EOL);
  306. RETURN_OP_NAME(C_RETINFO);
  307. RETURN_OP_NAME(C_USERFUNC);
  308. RETURN_OP_NAME(C_USERFUNC_POS);
  309. // operators
  310. RETURN_OP_NAME(C_OP3);
  311. RETURN_OP_NAME(C_LOR);
  312. RETURN_OP_NAME(C_LAND);
  313. RETURN_OP_NAME(C_LE);
  314. RETURN_OP_NAME(C_LT);
  315. RETURN_OP_NAME(C_GE);
  316. RETURN_OP_NAME(C_GT);
  317. RETURN_OP_NAME(C_EQ);
  318. RETURN_OP_NAME(C_NE);
  319. RETURN_OP_NAME(C_XOR);
  320. RETURN_OP_NAME(C_OR);
  321. RETURN_OP_NAME(C_AND);
  322. RETURN_OP_NAME(C_ADD);
  323. RETURN_OP_NAME(C_SUB);
  324. RETURN_OP_NAME(C_MUL);
  325. RETURN_OP_NAME(C_DIV);
  326. RETURN_OP_NAME(C_MOD);
  327. RETURN_OP_NAME(C_NEG);
  328. RETURN_OP_NAME(C_LNOT);
  329. RETURN_OP_NAME(C_NOT);
  330. RETURN_OP_NAME(C_R_SHIFT);
  331. RETURN_OP_NAME(C_L_SHIFT);
  332. default:
  333. ShowDebug("script_op2name: unexpected op=%d\n", op);
  334. return "???";
  335. }
  336. #undef RETURN_OP_NAME
  337. }
  338. /// Reports on the console the src of a script error.
  339. static void script_reportsrc(struct script_state *st)
  340. {
  341. struct block_list* bl;
  342. if( st->oid == 0 )
  343. return; //Can't report source.
  344. bl = map_id2bl(st->oid);
  345. if( bl == NULL )
  346. return;
  347. switch( bl->type )
  348. {
  349. case BL_NPC:
  350. if( bl->m >= 0 )
  351. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  352. else
  353. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  354. break;
  355. default:
  356. if( bl->m >= 0 )
  357. 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);
  358. else
  359. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  360. break;
  361. }
  362. }
  363. /// Reports on the console information about the script data.
  364. static void script_reportdata(struct script_data* data)
  365. {
  366. if( data == NULL )
  367. return;
  368. switch( data->type )
  369. {
  370. case C_NOP:// no value
  371. ShowDebug("Data: nothing (nil)\n");
  372. break;
  373. case C_INT:// number
  374. ShowDebug("Data: number value=%d\n", data->u.num);
  375. break;
  376. case C_STR:
  377. case C_CONSTSTR:// string
  378. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  379. break;
  380. case C_NAME:// reference
  381. if( reference_tovariable(data) )
  382. {// variable
  383. const char* name = reference_getname(data);
  384. if( not_array_variable(*name) )
  385. ShowDebug("Data: variable name='%s'\n", name);
  386. else
  387. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  388. }
  389. else if( reference_toconstant(data) )
  390. {// constant
  391. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  392. }
  393. else if( reference_toparam(data) )
  394. {// param
  395. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  396. }
  397. else
  398. {// ???
  399. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  400. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  401. }
  402. break;
  403. case C_POS:// label
  404. ShowDebug("Data: label pos=%d\n", data->u.num);
  405. break;
  406. default:
  407. ShowDebug("Data: %s\n", script_op2name(data->type));
  408. break;
  409. }
  410. }
  411. /*==========================================
  412. * エラーメッセージ出力
  413. *------------------------------------------*/
  414. static void disp_error_message2(const char *mes,const char *pos,int report)
  415. {
  416. error_msg = aStrdup(mes);
  417. error_pos = pos;
  418. error_report = report;
  419. longjmp( error_jump, 1 );
  420. }
  421. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  422. /// Checks event parameter validity
  423. static void check_event(struct script_state *st, const char *evt)
  424. {
  425. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  426. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  427. script_reportsrc(st);
  428. }
  429. }
  430. /*==========================================
  431. * Hashes the input string
  432. *------------------------------------------*/
  433. static unsigned int calc_hash(const char* p)
  434. {
  435. unsigned int h;
  436. #if defined(SCRIPT_HASH_DJB2)
  437. h = 5381;
  438. while( *p ) // hash*33 + c
  439. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  440. #elif defined(SCRIPT_HASH_SDBM)
  441. h = 0;
  442. while( *p ) // hash*65599 + c
  443. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  444. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  445. h = 0;
  446. while( *p ){
  447. unsigned int g;
  448. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  449. g = h & 0xF0000000;
  450. if( g )
  451. {
  452. h ^= g >> 24;
  453. h &= ~g;
  454. }
  455. }
  456. #else // athena hash
  457. h = 0;
  458. while( *p )
  459. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  460. #endif
  461. return h % SCRIPT_HASH_SIZE;
  462. }
  463. /*==========================================
  464. * str_data manipulation functions
  465. *------------------------------------------*/
  466. /// Looks up string using the provided id.
  467. const char* get_str(int id)
  468. {
  469. Assert( id >= LABEL_START && id < str_size );
  470. return str_buf+str_data[id].str;
  471. }
  472. /// Returns the uid of the string, or -1.
  473. static int search_str(const char* p)
  474. {
  475. int i;
  476. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  477. if( strcasecmp(get_str(i),p) == 0 )
  478. return i;
  479. return -1;
  480. }
  481. /// Stores a copy of the string and returns its id.
  482. /// If an identical string is already present, returns its id instead.
  483. int add_str(const char* p)
  484. {
  485. int i, h;
  486. int len;
  487. h = calc_hash(p);
  488. if( str_hash[h] == 0 )
  489. {// empty bucket, add new node here
  490. str_hash[h] = str_num;
  491. }
  492. else
  493. {// scan for end of list, or occurence of identical string
  494. for( i = str_hash[h]; ; i = str_data[i].next )
  495. {
  496. if( strcasecmp(get_str(i),p) == 0 )
  497. return i; // string already in list
  498. if( str_data[i].next == 0 )
  499. break; // reached the end
  500. }
  501. // append node to end of list
  502. str_data[i].next = str_num;
  503. }
  504. // grow list if neccessary
  505. if( str_num >= str_data_size )
  506. {
  507. str_data_size += 128;
  508. RECREATE(str_data,struct str_data_struct,str_data_size);
  509. memset(str_data + (str_data_size - 128), '\0', 128);
  510. }
  511. len=(int)strlen(p);
  512. // grow string buffer if neccessary
  513. while( str_pos+len+1 >= str_size )
  514. {
  515. str_size += 256;
  516. RECREATE(str_buf,char,str_size);
  517. memset(str_buf + (str_size - 256), '\0', 256);
  518. }
  519. safestrncpy(str_buf+str_pos, p, len+1);
  520. str_data[str_num].type = C_NOP;
  521. str_data[str_num].str = str_pos;
  522. str_data[str_num].next = 0;
  523. str_data[str_num].func = NULL;
  524. str_data[str_num].backpatch = -1;
  525. str_data[str_num].label = -1;
  526. str_pos += len+1;
  527. return str_num++;
  528. }
  529. /// Appends 1 byte to the script buffer.
  530. static void add_scriptb(int a)
  531. {
  532. if( script_pos+1 >= script_size )
  533. {
  534. script_size += SCRIPT_BLOCK_SIZE;
  535. RECREATE(script_buf,unsigned char,script_size);
  536. }
  537. script_buf[script_pos++] = (uint8)(a);
  538. }
  539. /// Appends a c_op value to the script buffer.
  540. /// The value is variable-length encoded into 8-bit blocks.
  541. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  542. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  543. static void add_scriptc(int a)
  544. {
  545. while( a >= 0x40 )
  546. {
  547. add_scriptb((a&0x3f)|0x40);
  548. a = (a - 0x40) >> 6;
  549. }
  550. add_scriptb(a);
  551. }
  552. /// Appends an integer value to the script buffer.
  553. /// The value is variable-length encoded into 8-bit blocks.
  554. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  555. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  556. static void add_scripti(int a)
  557. {
  558. while( a >= 0x40 )
  559. {
  560. add_scriptb((a&0x3f)|0xc0);
  561. a = (a - 0x40) >> 6;
  562. }
  563. add_scriptb(a|0x80);
  564. }
  565. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  566. ///
  567. /// @param l The id of the str_data entry
  568. // 最大16Mまで
  569. static void add_scriptl(int l)
  570. {
  571. int backpatch = str_data[l].backpatch;
  572. switch(str_data[l].type){
  573. case C_POS:
  574. case C_USERFUNC_POS:
  575. add_scriptc(C_POS);
  576. add_scriptb(str_data[l].label);
  577. add_scriptb(str_data[l].label>>8);
  578. add_scriptb(str_data[l].label>>16);
  579. break;
  580. case C_NOP:
  581. case C_USERFUNC:
  582. // ラベルの可能性があるのでbackpatch用データ埋め込み
  583. add_scriptc(C_NAME);
  584. str_data[l].backpatch = script_pos;
  585. add_scriptb(backpatch);
  586. add_scriptb(backpatch>>8);
  587. add_scriptb(backpatch>>16);
  588. break;
  589. case C_INT:
  590. add_scripti(abs(str_data[l].val));
  591. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  592. add_scriptc(C_NEG);
  593. break;
  594. default: // assume C_NAME
  595. add_scriptc(C_NAME);
  596. add_scriptb(l);
  597. add_scriptb(l>>8);
  598. add_scriptb(l>>16);
  599. break;
  600. }
  601. }
  602. /*==========================================
  603. * ラベルを解決する
  604. *------------------------------------------*/
  605. void set_label(int l,int pos, const char* script_pos)
  606. {
  607. int i,next;
  608. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  609. { //Prevent overwriting constants values and parameters [Skotlex]
  610. disp_error_message("set_label: invalid label name",script_pos);
  611. return;
  612. }
  613. if(str_data[l].label!=-1){
  614. disp_error_message("set_label: dup label ",script_pos);
  615. return;
  616. }
  617. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  618. str_data[l].label=pos;
  619. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  620. next=GETVALUE(script_buf,i);
  621. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  622. SETVALUE(script_buf,i,pos);
  623. i=next;
  624. }
  625. }
  626. /// Skips spaces and/or comments.
  627. const char* skip_space(const char* p)
  628. {
  629. if( p == NULL )
  630. return NULL;
  631. for(;;)
  632. {
  633. while( ISSPACE(*p) )
  634. ++p;
  635. if( *p == '/' && p[1] == '/' )
  636. {// line comment
  637. while(*p && *p!='\n')
  638. ++p;
  639. }
  640. else if( *p == '/' && p[1] == '*' )
  641. {// block comment
  642. p += 2;
  643. for(;;)
  644. {
  645. if( *p == '\0' )
  646. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  647. if( *p == '*' && p[1] == '/' )
  648. {// end of block comment
  649. p += 2;
  650. break;
  651. }
  652. ++p;
  653. }
  654. }
  655. else
  656. break;
  657. }
  658. return p;
  659. }
  660. /// Skips a word.
  661. /// A word consists of undercores and/or alfanumeric characters,
  662. /// and valid variable prefixes/postfixes.
  663. static
  664. const char* skip_word(const char* p)
  665. {
  666. // prefix
  667. switch( *p )
  668. {
  669. case '@':// temporary char variable
  670. ++p; break;
  671. case '#':// account variable
  672. p += ( p[1] == '#' ? 2 : 1 ); break;
  673. case '.':// npc variable
  674. p += ( p[1] == '@' ? 2 : 1 ); break;
  675. case '$':// global variable
  676. p += ( p[1] == '@' ? 2 : 1 ); break;
  677. }
  678. while( ISALNUM(*p) || *p == '_' )
  679. ++p;
  680. // postfix
  681. if( *p == '$' )// string
  682. p++;
  683. return p;
  684. }
  685. /// Adds a word to str_data.
  686. /// @see skip_word
  687. /// @see add_str
  688. static
  689. int add_word(const char* p)
  690. {
  691. char* word;
  692. int len;
  693. int i;
  694. // Check for a word
  695. len = skip_word(p) - p;
  696. if( len == 0 )
  697. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  698. // Duplicate the word
  699. CREATE(word, char, len+1);
  700. memcpy(word, p, len);
  701. word[len] = 0;
  702. // add the word
  703. i = add_str(word);
  704. aFree(word);
  705. return i;
  706. }
  707. /// Parses a function call.
  708. /// The argument list can have parenthesis or not.
  709. /// The number of arguments is checked.
  710. static
  711. const char* parse_callfunc(const char* p, int require_paren)
  712. {
  713. const char* p2;
  714. const char* arg=NULL;
  715. int func;
  716. func = add_word(p);
  717. if( str_data[func].type == C_FUNC ){
  718. // buildin function
  719. add_scriptl(func);
  720. add_scriptc(C_ARG);
  721. arg = buildin_func[str_data[func].val].arg;
  722. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  723. // script defined function
  724. int callsub = search_str("callsub");
  725. add_scriptl(callsub);
  726. add_scriptc(C_ARG);
  727. add_scriptl(func);
  728. arg = buildin_func[str_data[callsub].val].arg;
  729. if( *arg == 0 )
  730. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  731. if( *arg != '*' )
  732. ++arg; // count func as argument
  733. } else
  734. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  735. p = skip_word(p);
  736. p = skip_space(p);
  737. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  738. syntax.curly[syntax.curly_count].count = 0;
  739. if( *p == ';' )
  740. {// <func name> ';'
  741. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  742. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  743. {// <func name> '(' ')'
  744. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  745. p = p2;
  746. /*
  747. } else if( 0 && require_paren && *p != '(' )
  748. {// <func name>
  749. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  750. */
  751. } else
  752. {// <func name> <arg list>
  753. if( require_paren ){
  754. if( *p != '(' )
  755. disp_error_message("need '('",p);
  756. ++p; // skip '('
  757. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  758. } else if( *p == '(' ){
  759. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  760. } else {
  761. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  762. }
  763. ++syntax.curly_count;
  764. while( *arg ) {
  765. p2=parse_subexpr(p,-1);
  766. if( p == p2 )
  767. break; // not an argument
  768. if( *arg != '*' )
  769. ++arg; // next argument
  770. p=skip_space(p2);
  771. if( *arg == 0 || *p != ',' )
  772. break; // no more arguments
  773. ++p; // skip comma
  774. }
  775. --syntax.curly_count;
  776. }
  777. if( *arg && *arg != '?' && *arg != '*' )
  778. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  779. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  780. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  781. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  782. if( *p != ')' )
  783. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  784. ++p;
  785. }
  786. add_scriptc(C_FUNC);
  787. return p;
  788. }
  789. /*==========================================
  790. * 項の解析
  791. *------------------------------------------*/
  792. const char* parse_simpleexpr(const char *p)
  793. {
  794. int i;
  795. p=skip_space(p);
  796. if(*p==';' || *p==',')
  797. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  798. if(*p=='('){
  799. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  800. ++syntax.curly[i].count;
  801. p=parse_subexpr(p+1,-1);
  802. p=skip_space(p);
  803. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  804. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  805. ){
  806. if( *p == ',' ){
  807. syntax.curly[i].flag = ARGLIST_PAREN;
  808. return p;
  809. } else
  810. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  811. }
  812. if( *p != ')' )
  813. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  814. ++p;
  815. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  816. char *np;
  817. i=strtoul(p,&np,0);
  818. add_scripti(i);
  819. p=np;
  820. } else if(*p=='"'){
  821. add_scriptc(C_STR);
  822. p++;
  823. while( *p && *p != '"' ){
  824. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  825. {
  826. char buf[8];
  827. size_t len = skip_escaped_c(p) - p;
  828. size_t n = sv_unescape_c(buf, p, len);
  829. if( n != 1 )
  830. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  831. p += len;
  832. add_scriptb(*buf);
  833. continue;
  834. }
  835. else if( *p == '\n' )
  836. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  837. add_scriptb(*p++);
  838. }
  839. if(!*p)
  840. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  841. add_scriptb(0);
  842. p++; //'"'
  843. } else {
  844. int l;
  845. // label , register , function etc
  846. if(skip_word(p)==p)
  847. disp_error_message("parse_simpleexpr: unexpected character",p);
  848. l=add_word(p);
  849. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  850. return parse_callfunc(p,1);
  851. p=skip_word(p);
  852. if( *p == '[' ){
  853. // array(name[i] => getelementofarray(name,i) )
  854. add_scriptl(search_str("getelementofarray"));
  855. add_scriptc(C_ARG);
  856. add_scriptl(l);
  857. p=parse_subexpr(p+1,-1);
  858. p=skip_space(p);
  859. if( *p != ']' )
  860. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  861. ++p;
  862. add_scriptc(C_FUNC);
  863. }else
  864. add_scriptl(l);
  865. }
  866. return p;
  867. }
  868. /*==========================================
  869. * 式の解析
  870. *------------------------------------------*/
  871. const char* parse_subexpr(const char* p,int limit)
  872. {
  873. int op,opl,len;
  874. const char* tmpp;
  875. p=skip_space(p);
  876. if(*p=='-'){
  877. tmpp=skip_space(p+1);
  878. if(*tmpp==';' || *tmpp==','){
  879. add_scriptl(LABEL_NEXTLINE);
  880. p++;
  881. return p;
  882. }
  883. }
  884. tmpp=p;
  885. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  886. p=parse_subexpr(p+1,10);
  887. add_scriptc(op);
  888. } else
  889. p=parse_simpleexpr(p);
  890. p=skip_space(p);
  891. while((
  892. (op=C_OP3,opl=0,len=1,*p=='?') ||
  893. (op=C_ADD,opl=8,len=1,*p=='+') ||
  894. (op=C_SUB,opl=8,len=1,*p=='-') ||
  895. (op=C_MUL,opl=9,len=1,*p=='*') ||
  896. (op=C_DIV,opl=9,len=1,*p=='/') ||
  897. (op=C_MOD,opl=9,len=1,*p=='%') ||
  898. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  899. (op=C_AND,opl=6,len=1,*p=='&') ||
  900. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  901. (op=C_OR,opl=5,len=1,*p=='|') ||
  902. (op=C_XOR,opl=4,len=1,*p=='^') ||
  903. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  904. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  905. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  906. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  907. (op=C_GT,opl=3,len=1,*p=='>') ||
  908. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  909. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  910. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  911. p+=len;
  912. if(op == C_OP3) {
  913. p=parse_subexpr(p,-1);
  914. p=skip_space(p);
  915. if( *(p++) != ':')
  916. disp_error_message("parse_subexpr: need ':'", p-1);
  917. p=parse_subexpr(p,-1);
  918. } else {
  919. p=parse_subexpr(p,opl);
  920. }
  921. add_scriptc(op);
  922. p=skip_space(p);
  923. }
  924. return p; /* return first untreated operator */
  925. }
  926. /*==========================================
  927. * 式の評価
  928. *------------------------------------------*/
  929. const char* parse_expr(const char *p)
  930. {
  931. switch(*p){
  932. case ')': case ';': case ':': case '[': case ']':
  933. case '}':
  934. disp_error_message("parse_expr: unexpected char",p);
  935. }
  936. p=parse_subexpr(p,-1);
  937. return p;
  938. }
  939. /*==========================================
  940. * 行の解析
  941. *------------------------------------------*/
  942. const char* parse_line(const char* p)
  943. {
  944. const char* p2;
  945. p=skip_space(p);
  946. if(*p==';') {
  947. // if(); for(); while(); のために閉じ判定
  948. p = parse_syntax_close(p + 1);
  949. return p;
  950. }
  951. if(*p==')' && parse_syntax_for_flag)
  952. return p+1;
  953. p = skip_space(p);
  954. if(p[0] == '{') {
  955. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  956. syntax.curly[syntax.curly_count].count = -1;
  957. syntax.curly[syntax.curly_count].index = -1;
  958. syntax.curly_count++;
  959. return p + 1;
  960. } else if(p[0] == '}') {
  961. return parse_curly_close(p);
  962. }
  963. // 構文関連の処理
  964. p2 = parse_syntax(p);
  965. if(p2 != NULL)
  966. return p2;
  967. p = parse_callfunc(p,0);
  968. p = skip_space(p);
  969. if(parse_syntax_for_flag) {
  970. if( *p != ')' )
  971. disp_error_message("parse_line: need ')'",p);
  972. } else {
  973. if( *p != ';' )
  974. disp_error_message("parse_line: need ';'",p);
  975. }
  976. // if, for , while の閉じ判定
  977. p = parse_syntax_close(p+1);
  978. return p;
  979. }
  980. // { ... } の閉じ処理
  981. const char* parse_curly_close(const char* p)
  982. {
  983. if(syntax.curly_count <= 0) {
  984. disp_error_message("parse_curly_close: unexpected string",p);
  985. return p + 1;
  986. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  987. syntax.curly_count--;
  988. // if, for , while の閉じ判定
  989. p = parse_syntax_close(p + 1);
  990. return p;
  991. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  992. // switch() 閉じ判定
  993. int pos = syntax.curly_count-1;
  994. char label[256];
  995. int l;
  996. // 一時変数を消す
  997. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  998. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  999. parse_line(label);
  1000. syntax.curly_count--;
  1001. // 無条件で終了ポインタに移動
  1002. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1003. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1004. parse_line(label);
  1005. syntax.curly_count--;
  1006. // 現在地のラベルを付ける
  1007. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1008. l=add_str(label);
  1009. set_label(l,script_pos, p);
  1010. if(syntax.curly[pos].flag) {
  1011. // default が存在する
  1012. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1013. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1014. parse_line(label);
  1015. syntax.curly_count--;
  1016. }
  1017. // 終了ラベルを付ける
  1018. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1019. l=add_str(label);
  1020. set_label(l,script_pos, p);
  1021. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1022. syntax.curly_count--;
  1023. return p+1;
  1024. } else {
  1025. disp_error_message("parse_curly_close: unexpected string",p);
  1026. return p + 1;
  1027. }
  1028. }
  1029. // 構文関連の処理
  1030. // break, case, continue, default, do, for, function,
  1031. // if, switch, while をこの内部で処理します。
  1032. const char* parse_syntax(const char* p)
  1033. {
  1034. const char *p2 = skip_word(p);
  1035. switch(*p) {
  1036. case 'B':
  1037. case 'b':
  1038. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1039. // break の処理
  1040. char label[256];
  1041. int pos = syntax.curly_count - 1;
  1042. while(pos >= 0) {
  1043. if(syntax.curly[pos].type == TYPE_DO) {
  1044. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1045. break;
  1046. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1047. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1048. break;
  1049. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1050. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1051. break;
  1052. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1053. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1054. break;
  1055. }
  1056. pos--;
  1057. }
  1058. if(pos < 0) {
  1059. disp_error_message("parse_syntax: unexpected 'break'",p);
  1060. } else {
  1061. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1062. parse_line(label);
  1063. syntax.curly_count--;
  1064. }
  1065. p = skip_space(p2);
  1066. if(*p != ';')
  1067. disp_error_message("parse_syntax: need ';'",p);
  1068. // if, for , while の閉じ判定
  1069. p = parse_syntax_close(p + 1);
  1070. return p;
  1071. }
  1072. break;
  1073. case 'c':
  1074. case 'C':
  1075. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1076. // case の処理
  1077. int pos = syntax.curly_count-1;
  1078. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1079. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1080. return p+1;
  1081. } else {
  1082. char label[256];
  1083. int l,v;
  1084. char *np;
  1085. if(syntax.curly[pos].count != 1) {
  1086. // FALLTHRU 用のジャンプ
  1087. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1088. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1089. parse_line(label);
  1090. syntax.curly_count--;
  1091. // 現在地のラベルを付ける
  1092. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1093. l=add_str(label);
  1094. set_label(l,script_pos, p);
  1095. }
  1096. // switch 判定文
  1097. p = skip_space(p2);
  1098. if(p == p2) {
  1099. disp_error_message("parse_syntax: expect space ' '",p);
  1100. }
  1101. // check whether case label is integer or not
  1102. v = strtol(p,&np,0);
  1103. if(np == p) { //Check for constants
  1104. p2 = skip_word(p);
  1105. v = p2-p; // length of word at p2
  1106. memcpy(label,p,v);
  1107. label[v]='\0';
  1108. v = search_str(label);
  1109. if (v < 0 || str_data[v].type != C_INT)
  1110. disp_error_message("parse_syntax: 'case' label not integer",p);
  1111. v = str_data[v].val;
  1112. p = skip_word(p);
  1113. } else { //Numeric value
  1114. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1115. p++;
  1116. p = skip_word(p);
  1117. if(np != p)
  1118. disp_error_message("parse_syntax: 'case' label not integer",np);
  1119. }
  1120. p = skip_space(p);
  1121. if(*p != ':')
  1122. disp_error_message("parse_syntax: expect ':'",p);
  1123. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1124. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1125. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1126. // 2回parse しないとダメ
  1127. p2 = parse_line(label);
  1128. parse_line(p2);
  1129. syntax.curly_count--;
  1130. if(syntax.curly[pos].count != 1) {
  1131. // FALLTHRU 終了後のラベル
  1132. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1133. l=add_str(label);
  1134. set_label(l,script_pos,p);
  1135. }
  1136. // check duplication of case label [Rayce]
  1137. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1138. disp_error_message("parse_syntax: dup 'case'",p);
  1139. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1140. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1141. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1142. parse_line(label);
  1143. syntax.curly_count--;
  1144. syntax.curly[pos].count++;
  1145. }
  1146. return p + 1;
  1147. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1148. // continue の処理
  1149. char label[256];
  1150. int pos = syntax.curly_count - 1;
  1151. while(pos >= 0) {
  1152. if(syntax.curly[pos].type == TYPE_DO) {
  1153. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1154. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1155. break;
  1156. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1157. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1158. break;
  1159. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1160. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1161. break;
  1162. }
  1163. pos--;
  1164. }
  1165. if(pos < 0) {
  1166. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1167. } else {
  1168. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1169. parse_line(label);
  1170. syntax.curly_count--;
  1171. }
  1172. p = skip_space(p2);
  1173. if(*p != ';')
  1174. disp_error_message("parse_syntax: need ';'",p);
  1175. // if, for , while の閉じ判定
  1176. p = parse_syntax_close(p + 1);
  1177. return p;
  1178. }
  1179. break;
  1180. case 'd':
  1181. case 'D':
  1182. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1183. // switch - default の処理
  1184. int pos = syntax.curly_count-1;
  1185. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1186. disp_error_message("parse_syntax: unexpected 'default'",p);
  1187. } else if(syntax.curly[pos].flag) {
  1188. disp_error_message("parse_syntax: dup 'default'",p);
  1189. } else {
  1190. char label[256];
  1191. int l;
  1192. // 現在地のラベルを付ける
  1193. p = skip_space(p2);
  1194. if(*p != ':') {
  1195. disp_error_message("parse_syntax: need ':'",p);
  1196. }
  1197. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1198. l=add_str(label);
  1199. set_label(l,script_pos,p);
  1200. // 無条件で次のリンクに飛ばす
  1201. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1202. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1203. parse_line(label);
  1204. syntax.curly_count--;
  1205. // default のラベルを付ける
  1206. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1207. l=add_str(label);
  1208. set_label(l,script_pos,p);
  1209. syntax.curly[syntax.curly_count - 1].flag = 1;
  1210. syntax.curly[pos].count++;
  1211. }
  1212. return p + 1;
  1213. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1214. int l;
  1215. char label[256];
  1216. p=skip_space(p2);
  1217. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1218. syntax.curly[syntax.curly_count].count = 1;
  1219. syntax.curly[syntax.curly_count].index = syntax.index++;
  1220. syntax.curly[syntax.curly_count].flag = 0;
  1221. // 現在地のラベル形成する
  1222. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1223. l=add_str(label);
  1224. set_label(l,script_pos,p);
  1225. syntax.curly_count++;
  1226. return p;
  1227. }
  1228. break;
  1229. case 'f':
  1230. case 'F':
  1231. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1232. int l;
  1233. char label[256];
  1234. int pos = syntax.curly_count;
  1235. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1236. syntax.curly[syntax.curly_count].count = 1;
  1237. syntax.curly[syntax.curly_count].index = syntax.index++;
  1238. syntax.curly[syntax.curly_count].flag = 0;
  1239. syntax.curly_count++;
  1240. p=skip_space(p2);
  1241. if(*p != '(')
  1242. disp_error_message("parse_syntax: need '('",p);
  1243. p++;
  1244. // 初期化文を実行する
  1245. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1246. p=parse_line(p);
  1247. syntax.curly_count--;
  1248. // 条件判断開始のラベル形成する
  1249. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1250. l=add_str(label);
  1251. set_label(l,script_pos,p);
  1252. p=skip_space(p);
  1253. if(*p == ';') {
  1254. // for(;;) のパターンなので必ず真
  1255. ;
  1256. } else {
  1257. // 条件が偽なら終了地点に飛ばす
  1258. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1259. add_scriptl(add_str("jump_zero"));
  1260. add_scriptc(C_ARG);
  1261. p=parse_expr(p);
  1262. p=skip_space(p);
  1263. add_scriptl(add_str(label));
  1264. add_scriptc(C_FUNC);
  1265. }
  1266. if(*p != ';')
  1267. disp_error_message("parse_syntax: need ';'",p);
  1268. p++;
  1269. // ループ開始に飛ばす
  1270. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1271. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1272. parse_line(label);
  1273. syntax.curly_count--;
  1274. // 次のループへのラベル形成する
  1275. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1276. l=add_str(label);
  1277. set_label(l,script_pos,p);
  1278. // 次のループに入る時の処理
  1279. // for 最後の ')' を ';' として扱うフラグ
  1280. parse_syntax_for_flag = 1;
  1281. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1282. p=parse_line(p);
  1283. syntax.curly_count--;
  1284. parse_syntax_for_flag = 0;
  1285. // 条件判定処理に飛ばす
  1286. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1287. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1288. parse_line(label);
  1289. syntax.curly_count--;
  1290. // ループ開始のラベル付け
  1291. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1292. l=add_str(label);
  1293. set_label(l,script_pos,p);
  1294. return p;
  1295. }
  1296. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1297. {// internal script function
  1298. const char *func_name;
  1299. func_name = skip_space(p2);
  1300. p = skip_word(func_name);
  1301. if( p == func_name )
  1302. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1303. p2 = skip_space(p);
  1304. if( *p2 == ';' )
  1305. {// function <name> ;
  1306. // function declaration - just register the name
  1307. int l;
  1308. l = add_word(func_name);
  1309. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1310. str_data[l].type = C_USERFUNC;
  1311. // if, for , while の閉じ判定
  1312. p = parse_syntax_close(p2 + 1);
  1313. return p; }
  1314. else if(*p2 == '{')
  1315. {// function <name> <line/block of code>
  1316. char label[256];
  1317. int l;
  1318. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1319. syntax.curly[syntax.curly_count].count = 1;
  1320. syntax.curly[syntax.curly_count].index = syntax.index++;
  1321. syntax.curly[syntax.curly_count].flag = 0;
  1322. ++syntax.curly_count;
  1323. // Jump over the function code
  1324. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1325. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1326. ++syntax.curly_count;
  1327. parse_line(label);
  1328. --syntax.curly_count;
  1329. // Set the position of the function (label)
  1330. l=add_word(func_name);
  1331. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1332. str_data[l].type = C_USERFUNC;
  1333. set_label(l, script_pos, p);
  1334. if( parse_options&SCRIPT_USE_LABEL_DB )
  1335. strdb_put(scriptlabel_db, get_str(l), (void*)script_pos);
  1336. return skip_space(p);
  1337. }
  1338. else
  1339. {
  1340. disp_error_message("expect ';' or '{' at function syntax",p);
  1341. }
  1342. }
  1343. break;
  1344. case 'i':
  1345. case 'I':
  1346. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1347. // if() の処理
  1348. char label[256];
  1349. p=skip_space(p2);
  1350. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1351. disp_error_message("need '('",p);
  1352. }
  1353. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1354. syntax.curly[syntax.curly_count].count = 1;
  1355. syntax.curly[syntax.curly_count].index = syntax.index++;
  1356. syntax.curly[syntax.curly_count].flag = 0;
  1357. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1358. syntax.curly_count++;
  1359. add_scriptl(add_str("jump_zero"));
  1360. add_scriptc(C_ARG);
  1361. p=parse_expr(p);
  1362. p=skip_space(p);
  1363. add_scriptl(add_str(label));
  1364. add_scriptc(C_FUNC);
  1365. return p;
  1366. }
  1367. break;
  1368. case 's':
  1369. case 'S':
  1370. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1371. // switch() の処理
  1372. char label[256];
  1373. p=skip_space(p2);
  1374. if(*p != '(') {
  1375. disp_error_message("need '('",p);
  1376. }
  1377. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1378. syntax.curly[syntax.curly_count].count = 1;
  1379. syntax.curly[syntax.curly_count].index = syntax.index++;
  1380. syntax.curly[syntax.curly_count].flag = 0;
  1381. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1382. syntax.curly_count++;
  1383. add_scriptl(add_str("set"));
  1384. add_scriptc(C_ARG);
  1385. add_scriptl(add_str(label));
  1386. p=parse_expr(p);
  1387. p=skip_space(p);
  1388. if(*p != '{') {
  1389. disp_error_message("parse_syntax: need '{'",p);
  1390. }
  1391. add_scriptc(C_FUNC);
  1392. return p + 1;
  1393. }
  1394. break;
  1395. case 'w':
  1396. case 'W':
  1397. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1398. int l;
  1399. char label[256];
  1400. p=skip_space(p2);
  1401. if(*p != '(') {
  1402. disp_error_message("need '('",p);
  1403. }
  1404. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1405. syntax.curly[syntax.curly_count].count = 1;
  1406. syntax.curly[syntax.curly_count].index = syntax.index++;
  1407. syntax.curly[syntax.curly_count].flag = 0;
  1408. // 条件判断開始のラベル形成する
  1409. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1410. l=add_str(label);
  1411. set_label(l,script_pos,p);
  1412. // 条件が偽なら終了地点に飛ばす
  1413. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1414. syntax.curly_count++;
  1415. add_scriptl(add_str("jump_zero"));
  1416. add_scriptc(C_ARG);
  1417. p=parse_expr(p);
  1418. p=skip_space(p);
  1419. add_scriptl(add_str(label));
  1420. add_scriptc(C_FUNC);
  1421. return p;
  1422. }
  1423. break;
  1424. }
  1425. return NULL;
  1426. }
  1427. const char* parse_syntax_close(const char *p) {
  1428. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1429. int flag;
  1430. do {
  1431. p = parse_syntax_close_sub(p,&flag);
  1432. } while(flag);
  1433. return p;
  1434. }
  1435. // if, for , while , do の閉じ判定
  1436. // flag == 1 : 閉じられた
  1437. // flag == 0 : 閉じられない
  1438. const char* parse_syntax_close_sub(const char* p,int* flag)
  1439. {
  1440. char label[256];
  1441. int pos = syntax.curly_count - 1;
  1442. int l;
  1443. *flag = 1;
  1444. if(syntax.curly_count <= 0) {
  1445. *flag = 0;
  1446. return p;
  1447. } else if(syntax.curly[pos].type == TYPE_IF) {
  1448. const char *bp = p;
  1449. const char *p2;
  1450. // if 最終場所へ飛ばす
  1451. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1452. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1453. parse_line(label);
  1454. syntax.curly_count--;
  1455. // 現在地のラベルを付ける
  1456. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1457. l=add_str(label);
  1458. set_label(l,script_pos,p);
  1459. syntax.curly[pos].count++;
  1460. p = skip_space(p);
  1461. p2 = skip_word(p);
  1462. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1463. // else or else - if
  1464. p = skip_space(p2);
  1465. p2 = skip_word(p);
  1466. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1467. // else - if
  1468. p=skip_space(p2);
  1469. if(*p != '(') {
  1470. disp_error_message("need '('",p);
  1471. }
  1472. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1473. add_scriptl(add_str("jump_zero"));
  1474. add_scriptc(C_ARG);
  1475. p=parse_expr(p);
  1476. p=skip_space(p);
  1477. add_scriptl(add_str(label));
  1478. add_scriptc(C_FUNC);
  1479. *flag = 0;
  1480. return p;
  1481. } else {
  1482. // else
  1483. if(!syntax.curly[pos].flag) {
  1484. syntax.curly[pos].flag = 1;
  1485. *flag = 0;
  1486. return p;
  1487. }
  1488. }
  1489. }
  1490. // if 閉じ
  1491. syntax.curly_count--;
  1492. // 最終地のラベルを付ける
  1493. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1494. l=add_str(label);
  1495. set_label(l,script_pos,p);
  1496. if(syntax.curly[pos].flag == 1) {
  1497. // このifに対するelseじゃないのでポインタの位置は同じ
  1498. return bp;
  1499. }
  1500. return p;
  1501. } else if(syntax.curly[pos].type == TYPE_DO) {
  1502. int l;
  1503. char label[256];
  1504. const char *p2;
  1505. if(syntax.curly[pos].flag) {
  1506. // 現在地のラベル形成する(continue でここに来る)
  1507. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1508. l=add_str(label);
  1509. set_label(l,script_pos,p);
  1510. }
  1511. // 条件が偽なら終了地点に飛ばす
  1512. p = skip_space(p);
  1513. p2 = skip_word(p);
  1514. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1515. disp_error_message("parse_syntax: need 'while'",p);
  1516. p = skip_space(p2);
  1517. if(*p != '(') {
  1518. disp_error_message("need '('",p);
  1519. }
  1520. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1521. add_scriptl(add_str("jump_zero"));
  1522. add_scriptc(C_ARG);
  1523. p=parse_expr(p);
  1524. p=skip_space(p);
  1525. add_scriptl(add_str(label));
  1526. add_scriptc(C_FUNC);
  1527. // 開始地点に飛ばす
  1528. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1529. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1530. parse_line(label);
  1531. syntax.curly_count--;
  1532. // 条件終了地点のラベル形成する
  1533. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1534. l=add_str(label);
  1535. set_label(l,script_pos,p);
  1536. p = skip_space(p);
  1537. if(*p != ';') {
  1538. disp_error_message("parse_syntax: need ';'",p);
  1539. return p+1;
  1540. }
  1541. p++;
  1542. syntax.curly_count--;
  1543. return p;
  1544. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1545. // 次のループに飛ばす
  1546. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1547. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1548. parse_line(label);
  1549. syntax.curly_count--;
  1550. // for 終了のラベル付け
  1551. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1552. l=add_str(label);
  1553. set_label(l,script_pos,p);
  1554. syntax.curly_count--;
  1555. return p;
  1556. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1557. // while 条件判断へ飛ばす
  1558. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1559. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1560. parse_line(label);
  1561. syntax.curly_count--;
  1562. // while 終了のラベル付け
  1563. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1564. l=add_str(label);
  1565. set_label(l,script_pos,p);
  1566. syntax.curly_count--;
  1567. return p;
  1568. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1569. int pos = syntax.curly_count-1;
  1570. char label[256];
  1571. int l;
  1572. // 戻す
  1573. sprintf(label,"return;");
  1574. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1575. parse_line(label);
  1576. syntax.curly_count--;
  1577. // 現在地のラベルを付ける
  1578. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1579. l=add_str(label);
  1580. set_label(l,script_pos,p);
  1581. syntax.curly_count--;
  1582. return p;
  1583. } else {
  1584. *flag = 0;
  1585. return p;
  1586. }
  1587. }
  1588. /*==========================================
  1589. * 組み込み関数の追加
  1590. *------------------------------------------*/
  1591. static void add_buildin_func(void)
  1592. {
  1593. int i,n;
  1594. const char* p;
  1595. for( i = 0; buildin_func[i].func; i++ )
  1596. {
  1597. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1598. // 'v' - value (either string or int or reference)
  1599. // 's' - string
  1600. // 'i' - int
  1601. // 'r' - reference (of a variable)
  1602. // 'l' - label
  1603. // '?' - one optional parameter
  1604. // '*' - unknown number of optional parameters
  1605. p = buildin_func[i].arg;
  1606. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1607. while( *p == '?' ) ++p;
  1608. if( *p == '*' ) ++p;
  1609. if( *p != 0){
  1610. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1611. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1612. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1613. } else {
  1614. n = add_str(buildin_func[i].name);
  1615. str_data[n].type = C_FUNC;
  1616. str_data[n].val = i;
  1617. str_data[n].func = buildin_func[i].func;
  1618. }
  1619. }
  1620. }
  1621. /*==========================================
  1622. * 定数データベースの読み込み
  1623. *------------------------------------------*/
  1624. static void read_constdb(void)
  1625. {
  1626. FILE *fp;
  1627. char line[1024],name[1024],val[1024];
  1628. int n,type;
  1629. sprintf(line, "%s/const.txt", db_path);
  1630. fp=fopen(line, "r");
  1631. if(fp==NULL){
  1632. ShowError("can't read %s\n", line);
  1633. return ;
  1634. }
  1635. while(fgets(line, sizeof(line), fp))
  1636. {
  1637. if(line[0]=='/' && line[1]=='/')
  1638. continue;
  1639. type=0;
  1640. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1641. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1642. n=add_str(name);
  1643. if(type==0)
  1644. str_data[n].type=C_INT;
  1645. else
  1646. str_data[n].type=C_PARAM;
  1647. str_data[n].val= (int)strtol(val,NULL,0);
  1648. }
  1649. }
  1650. fclose(fp);
  1651. }
  1652. /*==========================================
  1653. * エラー表示
  1654. *------------------------------------------*/
  1655. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1656. {
  1657. int i;
  1658. if( p == NULL || !p[0] ) return NULL;
  1659. if( line < 0 )
  1660. StringBuf_Printf(buf, "*% 5d : ", -line);
  1661. else
  1662. StringBuf_Printf(buf, " % 5d : ", line);
  1663. for(i=0;p[i] && p[i] != '\n';i++){
  1664. if(p + i != mark)
  1665. StringBuf_Printf(buf, "%c", p[i]);
  1666. else
  1667. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1668. }
  1669. StringBuf_AppendStr(buf, "\n");
  1670. return p+i+(p[i] == '\n' ? 1 : 0);
  1671. }
  1672. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1673. {
  1674. // エラーが発生した行を求める
  1675. int j;
  1676. int line = start_line;
  1677. const char *p;
  1678. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1679. StringBuf buf;
  1680. for(p=src;p && *p;line++){
  1681. const char *lineend=strchr(p,'\n');
  1682. if(lineend==NULL || error_pos<lineend){
  1683. break;
  1684. }
  1685. for( j = 0; j < 4; j++ ) {
  1686. linestart[j] = linestart[j+1];
  1687. }
  1688. linestart[4] = p;
  1689. p=lineend+1;
  1690. }
  1691. StringBuf_Init(&buf);
  1692. StringBuf_AppendStr(&buf, "\a\n");
  1693. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1694. StringBuf_Printf(&buf, " %s\n", error_msg);
  1695. for(j = 0; j < 5; j++ ) {
  1696. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1697. }
  1698. p = script_print_line(&buf, p, error_pos, -line);
  1699. for(j = 0; j < 5; j++) {
  1700. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1701. }
  1702. ShowError("%s", StringBuf_Value(&buf));
  1703. StringBuf_Destroy(&buf);
  1704. }
  1705. /*==========================================
  1706. * スクリプトの解析
  1707. *------------------------------------------*/
  1708. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1709. {
  1710. const char *p,*tmpp;
  1711. int i;
  1712. struct script_code* code = NULL;
  1713. static int first=1;
  1714. char end;
  1715. if( src == NULL )
  1716. return NULL;// empty script
  1717. memset(&syntax,0,sizeof(syntax));
  1718. if(first){
  1719. add_buildin_func();
  1720. read_constdb();
  1721. first=0;
  1722. }
  1723. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1724. script_pos=0;
  1725. script_size=SCRIPT_BLOCK_SIZE;
  1726. str_data[LABEL_NEXTLINE].type=C_NOP;
  1727. str_data[LABEL_NEXTLINE].backpatch=-1;
  1728. str_data[LABEL_NEXTLINE].label=-1;
  1729. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1730. if( options&SCRIPT_USE_LABEL_DB )
  1731. scriptlabel_db->clear(scriptlabel_db, NULL);
  1732. parse_options = options;
  1733. if( setjmp( error_jump ) != 0 ) {
  1734. //Restore program state when script has problems. [from jA]
  1735. int i;
  1736. const int size = ARRAYLENGTH(syntax.curly);
  1737. if( error_report )
  1738. script_error(src,file,line,error_msg,error_pos);
  1739. aFree( error_msg );
  1740. aFree( script_buf );
  1741. script_pos = 0;
  1742. script_size = 0;
  1743. script_buf = NULL;
  1744. for(i=LABEL_START;i<str_num;i++)
  1745. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1746. for(i=0; i<size; i++)
  1747. linkdb_final(&syntax.curly[i].case_label);
  1748. return NULL;
  1749. }
  1750. parse_syntax_for_flag=0;
  1751. p=src;
  1752. p=skip_space(p);
  1753. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1754. {// does not require brackets around the script
  1755. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1756. {// empty script and can return NULL
  1757. aFree( script_buf );
  1758. script_pos = 0;
  1759. script_size = 0;
  1760. script_buf = NULL;
  1761. return NULL;
  1762. }
  1763. end = '\0';
  1764. }
  1765. else
  1766. {// requires brackets around the script
  1767. if( *p != '{' )
  1768. disp_error_message("not found '{'",p);
  1769. p = skip_space(p+1);
  1770. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1771. {// empty script and can return NULL
  1772. aFree( script_buf );
  1773. script_pos = 0;
  1774. script_size = 0;
  1775. script_buf = NULL;
  1776. return NULL;
  1777. }
  1778. end = '}';
  1779. }
  1780. // clear references of labels, variables and internal functions
  1781. for(i=LABEL_START;i<str_num;i++){
  1782. if(
  1783. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1784. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1785. ){
  1786. str_data[i].type=C_NOP;
  1787. str_data[i].backpatch=-1;
  1788. str_data[i].label=-1;
  1789. }
  1790. }
  1791. while( syntax.curly_count != 0 || *p != end )
  1792. {
  1793. if( *p == '\0' )
  1794. disp_error_message("unexpected end of script",p);
  1795. // labelだけ特殊処理
  1796. tmpp=skip_space(skip_word(p));
  1797. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1798. i=add_word(p);
  1799. set_label(i,script_pos,p);
  1800. if( parse_options&SCRIPT_USE_LABEL_DB )
  1801. strdb_put(scriptlabel_db, get_str(i), (void*)script_pos);
  1802. p=tmpp+1;
  1803. p=skip_space(p);
  1804. continue;
  1805. }
  1806. // 他は全部一緒くた
  1807. p=parse_line(p);
  1808. p=skip_space(p);
  1809. add_scriptc(C_EOL);
  1810. set_label(LABEL_NEXTLINE,script_pos,p);
  1811. str_data[LABEL_NEXTLINE].type=C_NOP;
  1812. str_data[LABEL_NEXTLINE].backpatch=-1;
  1813. str_data[LABEL_NEXTLINE].label=-1;
  1814. }
  1815. add_scriptc(C_NOP);
  1816. // trim code to size
  1817. script_size = script_pos;
  1818. RECREATE(script_buf,unsigned char,script_pos);
  1819. // default unknown references to variables
  1820. for(i=LABEL_START;i<str_num;i++){
  1821. if(str_data[i].type==C_NOP){
  1822. int j,next;
  1823. str_data[i].type=C_NAME;
  1824. str_data[i].label=i;
  1825. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1826. next=GETVALUE(script_buf,j);
  1827. SETVALUE(script_buf,j,i);
  1828. j=next;
  1829. }
  1830. }
  1831. }
  1832. #ifdef DEBUG_DISP
  1833. for(i=0;i<script_pos;i++){
  1834. if((i&15)==0) printf("%04x : ",i);
  1835. ShowMessage("%02x ",script_buf[i]);
  1836. if((i&15)==15) printf("\n");
  1837. }
  1838. ShowMessage("\n");
  1839. #endif
  1840. #ifdef DEBUG_DISASM
  1841. {
  1842. int i = 0,j;
  1843. while(i < script_pos) {
  1844. ShowMessage("%06x ",i);
  1845. j = i;
  1846. switch(get_com(script_buf,&i)) {
  1847. case C_EOL: printf("C_EOL"); break;
  1848. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1849. case C_POS:
  1850. ShowMessage("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1851. i += 3;
  1852. break;
  1853. case C_NAME:
  1854. j = (*(int*)(script_buf+i)&0xffffff);
  1855. ShowMessage("C_NAME %s",j == 0xffffff ? "?? unknown ??" : get_str(j));
  1856. i += 3;
  1857. break;
  1858. case C_ARG: ShowMessage("C_ARG"); break;
  1859. case C_FUNC: ShowMessage("C_FUNC"); break;
  1860. case C_ADD: ShowMessage("C_ADD"); break;
  1861. case C_SUB: ShowMessage("C_SUB"); break;
  1862. case C_MUL: ShowMessage("C_MUL"); break;
  1863. case C_DIV: ShowMessage("C_DIV"); break;
  1864. case C_MOD: ShowMessage("C_MOD"); break;
  1865. case C_EQ: ShowMessage("C_EQ"); break;
  1866. case C_NE: ShowMessage("C_NE"); break;
  1867. case C_GT: ShowMessage("C_GT"); break;
  1868. case C_GE: ShowMessage("C_GE"); break;
  1869. case C_LT: ShowMessage("C_LT"); break;
  1870. case C_LE: ShowMessage("C_LE"); break;
  1871. case C_AND: ShowMessage("C_AND"); break;
  1872. case C_OR: ShowMessage("C_OR"); break;
  1873. case C_XOR: ShowMessage("C_XOR"); break;
  1874. case C_LAND: ShowMessage("C_LAND"); break;
  1875. case C_LOR: ShowMessage("C_LOR"); break;
  1876. case C_R_SHIFT: ShowMessage("C_R_SHIFT"); break;
  1877. case C_L_SHIFT: ShowMessage("C_L_SHIFT"); break;
  1878. case C_NEG: ShowMessage("C_NEG"); break;
  1879. case C_NOT: ShowMessage("C_NOT"); break;
  1880. case C_LNOT: ShowMessage("C_LNOT"); break;
  1881. case C_NOP: ShowMessage("C_NOP"); break;
  1882. case C_OP3: ShowMessage("C_OP3"); break;
  1883. case C_STR:
  1884. j = strlen(script_buf + i);
  1885. ShowMessage("C_STR %s",script_buf + i);
  1886. i+= j+1;
  1887. break;
  1888. default:
  1889. ShowMessage("unknown");
  1890. }
  1891. ShowMessage("\n");
  1892. }
  1893. }
  1894. #endif
  1895. CREATE(code,struct script_code,1);
  1896. code->script_buf = script_buf;
  1897. code->script_size = script_size;
  1898. code->script_vars = NULL;
  1899. return code;
  1900. }
  1901. /// Returns the player attached to this script, identified by the rid.
  1902. /// If there is no player attached, the script is terminated.
  1903. TBL_PC *script_rid2sd(struct script_state *st)
  1904. {
  1905. TBL_PC *sd=map_id2sd(st->rid);
  1906. if(!sd){
  1907. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1908. script_reportsrc(st);
  1909. st->state = END;
  1910. }
  1911. return sd;
  1912. }
  1913. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1914. ///
  1915. /// @param st Script state
  1916. /// @param data Variable/constant
  1917. void get_val(struct script_state* st, struct script_data* data)
  1918. {
  1919. const char* name;
  1920. char prefix;
  1921. char postfix;
  1922. TBL_PC* sd = NULL;
  1923. if( !data_isreference(data) )
  1924. return;// not a variable/constant
  1925. name = reference_getname(data);
  1926. prefix = name[0];
  1927. postfix = name[strlen(name) - 1];
  1928. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1929. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1930. {
  1931. sd = script_rid2sd(st);
  1932. if( sd == NULL )
  1933. {// needs player attached
  1934. if( postfix == '$' )
  1935. {// string variable
  1936. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1937. data->type = C_CONSTSTR;
  1938. data->u.str = "";
  1939. }
  1940. else
  1941. {// integer variable
  1942. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1943. data->type = C_INT;
  1944. data->u.num = 0;
  1945. }
  1946. return;
  1947. }
  1948. }
  1949. if( postfix == '$' )
  1950. {// string variable
  1951. switch( prefix )
  1952. {
  1953. case '@':
  1954. data->u.str = pc_readregstr(sd, data->u.num);
  1955. break;
  1956. case '$':
  1957. data->u.str = mapreg_readregstr(data->u.num);
  1958. break;
  1959. case '#':
  1960. if( name[1] == '#' )
  1961. data->u.str = pc_readaccountreg2str(sd, name);// global
  1962. else
  1963. data->u.str = pc_readaccountregstr(sd, name);// local
  1964. break;
  1965. case '.':
  1966. {
  1967. struct linkdb_node** n =
  1968. data->ref ? data->ref:
  1969. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  1970. &st->script->script_vars;// npc variable
  1971. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  1972. }
  1973. break;
  1974. default:
  1975. data->u.str = pc_readglobalreg_str(sd, name);
  1976. break;
  1977. }
  1978. if( data->u.str == NULL || data->u.str[0] == '\0' )
  1979. {// empty string
  1980. data->type = C_CONSTSTR;
  1981. data->u.str = "";
  1982. }
  1983. else
  1984. {// duplicate string
  1985. data->type = C_STR;
  1986. data->u.str = aStrdup(data->u.str);
  1987. }
  1988. }
  1989. else
  1990. {// integer variable
  1991. data->type = C_INT;
  1992. if( reference_toconstant(data) )
  1993. {
  1994. data->u.num = reference_getconstant(data);
  1995. }
  1996. else if( reference_toparam(data) )
  1997. {
  1998. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  1999. }
  2000. else
  2001. switch( prefix )
  2002. {
  2003. case '@':
  2004. data->u.num = pc_readreg(sd, data->u.num);
  2005. break;
  2006. case '$':
  2007. data->u.num = mapreg_readreg(data->u.num);
  2008. break;
  2009. case '#':
  2010. if( name[1] == '#' )
  2011. data->u.num = pc_readaccountreg2(sd, name);// global
  2012. else
  2013. data->u.num = pc_readaccountreg(sd, name);// local
  2014. break;
  2015. case '.':
  2016. {
  2017. struct linkdb_node** n =
  2018. data->ref ? data->ref:
  2019. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2020. &st->script->script_vars;// npc variable
  2021. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2022. }
  2023. break;
  2024. default:
  2025. data->u.num = pc_readglobalreg(sd, name);
  2026. break;
  2027. }
  2028. }
  2029. return;
  2030. }
  2031. void push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2032. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2033. /// The value is left in the top of the stack and needs to be removed manually.
  2034. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2035. {
  2036. struct script_data* data;
  2037. push_val2(st->stack, C_NAME, uid, ref);
  2038. data = script_getdatatop(st, -1);
  2039. get_val(st, data);
  2040. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2041. }
  2042. /*==========================================
  2043. * Stores the value of a script variable
  2044. * Return value is 0 on fail, 1 on success.
  2045. *------------------------------------------*/
  2046. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2047. {
  2048. char prefix = name[0];
  2049. if( is_string_variable(name) )
  2050. {// string variable
  2051. const char* str = (const char*)value;
  2052. switch (prefix) {
  2053. case '@':
  2054. return pc_setregstr(sd, num, str);
  2055. case '$':
  2056. return mapreg_setregstr(num, str);
  2057. case '#':
  2058. return (name[1] == '#') ?
  2059. pc_setaccountreg2str(sd, name, str) :
  2060. pc_setaccountregstr(sd, name, str);
  2061. case '.': {
  2062. char* p;
  2063. struct linkdb_node** n;
  2064. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2065. p = (char*)linkdb_erase(n, (void*)num);
  2066. if (p) aFree(p);
  2067. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2068. }
  2069. return 1;
  2070. default:
  2071. return pc_setglobalreg_str(sd, name, str);
  2072. }
  2073. }
  2074. else
  2075. {// integer variable
  2076. int val = (int)value;
  2077. if(str_data[num&0x00ffffff].type == C_PARAM)
  2078. {
  2079. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2080. {
  2081. if( st != NULL )
  2082. {
  2083. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2084. script_reportsrc(st);
  2085. st->state = END;
  2086. }
  2087. return 0;
  2088. }
  2089. return 1;
  2090. }
  2091. switch (prefix) {
  2092. case '@':
  2093. return pc_setreg(sd, num, val);
  2094. case '$':
  2095. return mapreg_setreg(num, val);
  2096. case '#':
  2097. return (name[1] == '#') ?
  2098. pc_setaccountreg2(sd, name, val) :
  2099. pc_setaccountreg(sd, name, val);
  2100. case '.': {
  2101. struct linkdb_node** n;
  2102. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2103. if (val == 0)
  2104. linkdb_erase(n, (void*)num);
  2105. else
  2106. linkdb_replace(n, (void*)num, (void*)val);
  2107. }
  2108. return 1;
  2109. default:
  2110. return pc_setglobalreg(sd, name, val);
  2111. }
  2112. }
  2113. }
  2114. int set_var(TBL_PC* sd, char* name, void* val)
  2115. {
  2116. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2117. }
  2118. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2119. {
  2120. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2121. }
  2122. /// Converts the data to a string
  2123. const char* conv_str(struct script_state* st, struct script_data* data)
  2124. {
  2125. char* p;
  2126. get_val(st, data);
  2127. if( data_isstring(data) )
  2128. {// nothing to convert
  2129. }
  2130. else if( data_isint(data) )
  2131. {// int -> string
  2132. CREATE(p, char, ITEM_NAME_LENGTH);
  2133. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2134. p[ITEM_NAME_LENGTH-1] = '\0';
  2135. data->type = C_STR;
  2136. data->u.str = p;
  2137. }
  2138. else if( data_isreference(data) )
  2139. {// reference -> string
  2140. //##TODO when does this happen (check get_val) [FlavioJS]
  2141. data->type = C_CONSTSTR;
  2142. data->u.str = reference_getname(data);
  2143. }
  2144. else
  2145. {// unsupported data type
  2146. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2147. script_reportdata(data);
  2148. script_reportsrc(st);
  2149. data->type = C_CONSTSTR;
  2150. data->u.str = "";
  2151. }
  2152. return data->u.str;
  2153. }
  2154. /// Converts the data to an int
  2155. int conv_num(struct script_state* st, struct script_data* data)
  2156. {
  2157. char* p;
  2158. long num;
  2159. get_val(st, data);
  2160. if( data_isint(data) )
  2161. {// nothing to convert
  2162. }
  2163. else if( data_isstring(data) )
  2164. {// string -> int
  2165. // the result does not overflow or underflow, it is capped instead
  2166. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2167. p = data->u.str;
  2168. errno = 0;
  2169. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2170. if( errno == ERANGE
  2171. #if LONG_MAX > INT_MAX
  2172. || num < INT_MIN || num > INT_MAX
  2173. #endif
  2174. )
  2175. {
  2176. if( num <= INT_MIN )
  2177. {
  2178. num = INT_MIN;
  2179. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2180. }
  2181. else//if( num >= INT_MAX )
  2182. {
  2183. num = INT_MAX;
  2184. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2185. }
  2186. script_reportdata(data);
  2187. script_reportsrc(st);
  2188. }
  2189. if( data->type == C_STR )
  2190. aFree(p);
  2191. data->type = C_INT;
  2192. data->u.num = (int)num;
  2193. }
  2194. #if 0
  2195. // FIXME this function is being used to retrieve the position of labels and
  2196. // probably other stuff [FlavioJS]
  2197. else
  2198. {// unsupported data type
  2199. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2200. script_reportdata(data);
  2201. script_reportsrc(st);
  2202. data->type = C_INT;
  2203. data->u.num = 0;
  2204. }
  2205. #endif
  2206. return data->u.num;
  2207. }
  2208. //
  2209. // Stack operations
  2210. //
  2211. /// Increases the size of the stack
  2212. void stack_expand(struct script_stack* stack)
  2213. {
  2214. stack->sp_max += 64;
  2215. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2216. stack->sp_max * sizeof(stack->stack_data[0]) );
  2217. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2218. 64 * sizeof(stack->stack_data[0]) );
  2219. }
  2220. /// Pushes a value into the stack
  2221. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2222. /// Pushes a value into the stack (with reference)
  2223. void push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2224. {
  2225. if( stack->sp >= stack->sp_max )
  2226. stack_expand(stack);
  2227. stack->stack_data[stack->sp].type = type;
  2228. stack->stack_data[stack->sp].u.num = val;
  2229. stack->stack_data[stack->sp].ref = ref;
  2230. stack->sp++;
  2231. }
  2232. /// Pushes a string into the stack
  2233. void push_str(struct script_stack* stack, enum c_op type, char* str)
  2234. {
  2235. if( stack->sp >= stack->sp_max )
  2236. stack_expand(stack);
  2237. stack->stack_data[stack->sp].type = type;
  2238. stack->stack_data[stack->sp].u.str = str;
  2239. stack->stack_data[stack->sp].ref = NULL;
  2240. stack->sp++;
  2241. }
  2242. /// Pushes a copy of the target position into the stack
  2243. void push_copy(struct script_stack* stack, int pos)
  2244. {
  2245. switch( stack->stack_data[pos].type )
  2246. {
  2247. case C_CONSTSTR:
  2248. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2249. break;
  2250. case C_STR:
  2251. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2252. break;
  2253. default:
  2254. push_val2(
  2255. stack,stack->stack_data[pos].type,
  2256. stack->stack_data[pos].u.num,
  2257. stack->stack_data[pos].ref
  2258. );
  2259. break;
  2260. }
  2261. }
  2262. /// Removes the values in indexes [start,end[ from the stack
  2263. void pop_stack(struct script_stack* stack, int start, int end)
  2264. {
  2265. struct script_data* data;
  2266. int i;
  2267. if( start < 0 )
  2268. start = 0;
  2269. if( end > stack->sp_max )
  2270. end = stack->sp_max;
  2271. if( start >= end )
  2272. return;// nothing to pop
  2273. // free stack elements
  2274. for( i = start; i < end; i++ )
  2275. {
  2276. data = &stack->stack_data[i];
  2277. if( data->type == C_STR )
  2278. aFree(data->u.str);
  2279. data->type = C_NOP;
  2280. }
  2281. // move the rest of the elements
  2282. if( stack->sp > end )
  2283. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2284. stack->sp -= end - start;
  2285. }
  2286. ///
  2287. ///
  2288. ///
  2289. /*==========================================
  2290. * スクリプト依存変数、関数依存変数の解放
  2291. *------------------------------------------*/
  2292. void script_free_vars(struct linkdb_node **node)
  2293. {
  2294. struct linkdb_node* n = *node;
  2295. while( n != NULL)
  2296. {
  2297. const char* name = get_str((int)(n->key)&0x00ffffff);
  2298. if( is_string_variable(name) )
  2299. aFree(n->data); // 文字型変数なので、データ削除
  2300. n = n->next;
  2301. }
  2302. linkdb_final( node );
  2303. }
  2304. /*==========================================
  2305. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2306. *------------------------------------------*/
  2307. /// @deprecated will be removed later [FlavioJS]
  2308. static void script_free_stack(struct script_stack *stack)
  2309. {
  2310. int i;
  2311. for(i = 0; i < stack->sp; i++) {
  2312. if( stack->stack_data[i].type == C_STR ) {
  2313. aFree(stack->stack_data[i].u.str);
  2314. stack->stack_data[i].type = C_INT;
  2315. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2316. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2317. script_free_vars( n );
  2318. aFree( n );
  2319. }
  2320. }
  2321. script_free_vars( stack->var_function );
  2322. aFree(stack->var_function);
  2323. aFree(stack->stack_data);
  2324. aFree(stack);
  2325. }
  2326. void script_free_code(struct script_code* code)
  2327. {
  2328. script_free_vars( &code->script_vars );
  2329. aFree( code->script_buf );
  2330. aFree( code );
  2331. }
  2332. /// Creates a new script state.
  2333. ///
  2334. /// @param script Script code
  2335. /// @param pos Position in the code
  2336. /// @param rid Who is running the script (attached player)
  2337. /// @param oid Where the code is being run (npc 'object')
  2338. /// @return Script state
  2339. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2340. {
  2341. struct script_state* st;
  2342. CREATE(st, struct script_state, 1);
  2343. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2344. st->stack->sp = 0;
  2345. st->stack->sp_max = 64;
  2346. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2347. st->stack->defsp = st->stack->sp;
  2348. CREATE(st->stack->var_function, struct linkdb_node*, 1);
  2349. st->state = RUN;
  2350. st->script = script;
  2351. //st->scriptroot = script;
  2352. st->pos = pos;
  2353. st->rid = rid;
  2354. st->oid = oid;
  2355. st->sleep.timer = INVALID_TIMER;
  2356. return st;
  2357. }
  2358. /// Frees a script state.
  2359. ///
  2360. /// @param st Script state
  2361. void script_free_state(struct script_state* st)
  2362. {
  2363. if( st->sleep.timer != INVALID_TIMER )
  2364. delete_timer(st->sleep.timer, run_script_timer);
  2365. script_free_stack(st->stack);
  2366. st->pos = -1;
  2367. aFree(st);
  2368. }
  2369. //
  2370. // 実行部main
  2371. //
  2372. /*==========================================
  2373. * コマンドの読み取り
  2374. *------------------------------------------*/
  2375. c_op get_com(unsigned char *script,int *pos)
  2376. {
  2377. int i = 0, j = 0;
  2378. if(script[*pos]>=0x80){
  2379. return C_INT;
  2380. }
  2381. while(script[*pos]>=0x40){
  2382. i=script[(*pos)++]<<j;
  2383. j+=6;
  2384. }
  2385. return (c_op)(i+(script[(*pos)++]<<j));
  2386. }
  2387. /*==========================================
  2388. * 数値の所得
  2389. *------------------------------------------*/
  2390. int get_num(unsigned char *script,int *pos)
  2391. {
  2392. int i,j;
  2393. i=0; j=0;
  2394. while(script[*pos]>=0xc0){
  2395. i+=(script[(*pos)++]&0x7f)<<j;
  2396. j+=6;
  2397. }
  2398. return i+((script[(*pos)++]&0x7f)<<j);
  2399. }
  2400. /*==========================================
  2401. * スタックから値を取り出す
  2402. *------------------------------------------*/
  2403. int pop_val(struct script_state* st)
  2404. {
  2405. if(st->stack->sp<=0)
  2406. return 0;
  2407. st->stack->sp--;
  2408. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2409. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2410. return st->stack->stack_data[st->stack->sp].u.num;
  2411. return 0;
  2412. }
  2413. /// Ternary operators
  2414. /// test ? if_true : if_false
  2415. void op_3(struct script_state* st, int op)
  2416. {
  2417. struct script_data* data;
  2418. int flag = 0;
  2419. data = script_getdatatop(st, -3);
  2420. get_val(st, data);
  2421. if( data_isstring(data) )
  2422. flag = data->u.str[0];// "" -> false
  2423. else if( data_isint(data) )
  2424. flag = data->u.num;// 0 -> false
  2425. else
  2426. {
  2427. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2428. script_reportdata(data);
  2429. script_reportsrc(st);
  2430. script_removetop(st, -3, 0);
  2431. script_pushnil(st);
  2432. return;
  2433. }
  2434. if( flag )
  2435. script_pushcopytop(st, -2);
  2436. else
  2437. script_pushcopytop(st, -1);
  2438. script_removetop(st, -4, -1);
  2439. }
  2440. /// Binary string operators
  2441. /// s1 EQ s2 -> i
  2442. /// s1 NE s2 -> i
  2443. /// s1 GT s2 -> i
  2444. /// s1 GE s2 -> i
  2445. /// s1 LT s2 -> i
  2446. /// s1 LE s2 -> i
  2447. /// s1 ADD s2 -> s
  2448. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2449. {
  2450. int a = 0;
  2451. switch(op){
  2452. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2453. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2454. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2455. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2456. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2457. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2458. case C_ADD:
  2459. {
  2460. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2461. strcpy(buf, s1);
  2462. strcat(buf, s2);
  2463. script_pushstr(st, buf);
  2464. return;
  2465. }
  2466. default:
  2467. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2468. script_reportsrc(st);
  2469. script_pushnil(st);
  2470. st->state = END;
  2471. return;
  2472. }
  2473. script_pushint(st,a);
  2474. }
  2475. /// Binary number operators
  2476. /// i OP i -> i
  2477. void op_2num(struct script_state* st, int op, int i1, int i2)
  2478. {
  2479. int ret;
  2480. double ret_double;
  2481. switch( op )
  2482. {
  2483. case C_AND: ret = i1 & i2; break;
  2484. case C_OR: ret = i1 | i2; break;
  2485. case C_XOR: ret = i1 ^ i2; break;
  2486. case C_LAND: ret = (i1 && i2); break;
  2487. case C_LOR: ret = (i1 || i2); break;
  2488. case C_EQ: ret = (i1 == i2); break;
  2489. case C_NE: ret = (i1 != i2); break;
  2490. case C_GT: ret = (i1 > i2); break;
  2491. case C_GE: ret = (i1 >= i2); break;
  2492. case C_LT: ret = (i1 < i2); break;
  2493. case C_LE: ret = (i1 <= i2); break;
  2494. case C_R_SHIFT: ret = i1>>i2; break;
  2495. case C_L_SHIFT: ret = i1<<i2; break;
  2496. case C_DIV:
  2497. case C_MOD:
  2498. if( i2 == 0 )
  2499. {
  2500. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2501. script_reportsrc(st);
  2502. script_pushnil(st);
  2503. st->state = END;
  2504. return;
  2505. }
  2506. else if( op == C_DIV )
  2507. ret = i1 / i2;
  2508. else//if( op == C_MOD )
  2509. ret = i1 % i2;
  2510. break;
  2511. default:
  2512. switch( op )
  2513. {// operators that can overflow/underflow
  2514. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2515. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2516. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2517. default:
  2518. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2519. script_reportsrc(st);
  2520. script_pushnil(st);
  2521. return;
  2522. }
  2523. if( ret_double < (double)INT_MIN )
  2524. {
  2525. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2526. script_reportsrc(st);
  2527. ret = INT_MIN;
  2528. }
  2529. else if( ret_double > (double)INT_MAX )
  2530. {
  2531. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2532. script_reportsrc(st);
  2533. ret = INT_MAX;
  2534. }
  2535. }
  2536. script_pushint(st, ret);
  2537. }
  2538. /// Binary operators
  2539. void op_2(struct script_state *st, int op)
  2540. {
  2541. struct script_data* left;
  2542. struct script_data* right;
  2543. left = script_getdatatop(st, -2);
  2544. right = script_getdatatop(st, -1);
  2545. get_val(st, left);
  2546. get_val(st, right);
  2547. // automatic conversions
  2548. switch( op )
  2549. {
  2550. case C_ADD:
  2551. if( data_isint(left) && data_isstring(right) )
  2552. {// convert int-string to string-string
  2553. conv_str(st, left);
  2554. }
  2555. else if( data_isstring(left) && data_isint(right) )
  2556. {// convert string-int to string-string
  2557. conv_str(st, right);
  2558. }
  2559. break;
  2560. }
  2561. if( data_isstring(left) && data_isstring(right) )
  2562. {// ss => op_2str
  2563. op_2str(st, op, left->u.str, right->u.str);
  2564. script_removetop(st, -3, -1);// pop the two values before the top one
  2565. }
  2566. else if( data_isint(left) && data_isint(right) )
  2567. {// ii => op_2num
  2568. int i1 = left->u.num;
  2569. int i2 = right->u.num;
  2570. script_removetop(st, -2, 0);
  2571. op_2num(st, op, i1, i2);
  2572. }
  2573. else
  2574. {// invalid argument
  2575. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2576. script_reportdata(left);
  2577. script_reportdata(right);
  2578. script_reportsrc(st);
  2579. script_removetop(st, -2, 0);
  2580. script_pushnil(st);
  2581. st->state = END;
  2582. }
  2583. }
  2584. /// Unary operators
  2585. /// NEG i -> i
  2586. /// NOT i -> i
  2587. /// LNOT i -> i
  2588. void op_1(struct script_state* st, int op)
  2589. {
  2590. struct script_data* data;
  2591. int i1;
  2592. data = script_getdatatop(st, -1);
  2593. get_val(st, data);
  2594. if( !data_isint(data) )
  2595. {// not a number
  2596. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2597. script_reportdata(data);
  2598. script_reportsrc(st);
  2599. script_pushnil(st);
  2600. st->state = END;
  2601. return;
  2602. }
  2603. i1 = data->u.num;
  2604. script_removetop(st, -1, 0);
  2605. switch( op )
  2606. {
  2607. case C_NEG: i1 = -i1; break;
  2608. case C_NOT: i1 = ~i1; break;
  2609. case C_LNOT: i1 = !i1; break;
  2610. default:
  2611. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2612. script_reportsrc(st);
  2613. script_pushnil(st);
  2614. st->state = END;
  2615. return;
  2616. }
  2617. script_pushint(st, i1);
  2618. }
  2619. /*==========================================
  2620. * 関数の実行
  2621. *------------------------------------------*/
  2622. int run_func(struct script_state *st)
  2623. {
  2624. int i,start_sp,end_sp,func;
  2625. end_sp=st->stack->sp;
  2626. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2627. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2628. ShowError("function not found\n");
  2629. // st->stack->sp=0;
  2630. st->state=END;
  2631. script_reportsrc(st);
  2632. return 1;
  2633. }
  2634. start_sp=i-1;
  2635. st->start=i-1;
  2636. st->end=end_sp;
  2637. func=st->stack->stack_data[st->start].u.num;
  2638. #ifdef DEBUG_RUN
  2639. if(battle_config.etc_log) {
  2640. ShowDebug("run_func : %s? (%d(%d)) sp=%d (%d...%d)\n", get_str(func), func, str_data[func].type, st->stack->sp, st->start, st->end);
  2641. ShowDebug("stack dump :");
  2642. for(i=0;i<end_sp;i++){
  2643. switch(st->stack->stack_data[i].type){
  2644. case C_INT:
  2645. ShowMessage(" int(%d)", st->stack->stack_data[i].u.num);
  2646. break;
  2647. case C_NAME:
  2648. ShowMessage(" name(%s)", get_str(st->stack->stack_data[i].u.num & 0xffffff);
  2649. break;
  2650. case C_ARG:
  2651. ShowMessage(" arg");
  2652. break;
  2653. case C_POS:
  2654. ShowMessage(" pos(%d)",st->stack->stack_data[i].u.num);
  2655. break;
  2656. case C_STR:
  2657. ShowMessage(" str(%s)",st->stack->stack_data[i].u.str);
  2658. break;
  2659. case C_CONSTSTR:
  2660. ShowMessage(" cstr(%s)",st->stack->stack_data[i].u.str);
  2661. break;
  2662. default:
  2663. ShowMessage(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2664. }
  2665. }
  2666. ShowMessage("\n");
  2667. }
  2668. #endif
  2669. if(str_data[func].type!=C_FUNC ){
  2670. ShowError("run_func: '"CL_WHITE"%s"CL_RESET"' (type %d) is not function and command!\n", get_str(func), str_data[func].type);
  2671. // st->stack->sp=0;
  2672. st->state=END;
  2673. script_reportsrc(st);
  2674. return 1;
  2675. }
  2676. #ifdef DEBUG_RUN
  2677. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n", get_str(func),func,str_data[func].type,st->pos);
  2678. #endif
  2679. if(str_data[func].func){
  2680. if (str_data[func].func(st)) //Report error
  2681. script_reportsrc(st);
  2682. } else {
  2683. ShowError("script:run_func: missing buildin command '%s' (id=%d type=%s)\n", get_str(func), func, script_op2name(str_data[func].type));
  2684. script_pushnil(st);
  2685. script_reportsrc(st);
  2686. st->state = END;
  2687. }
  2688. // Stack's datum are used when re-run functions [Eoe]
  2689. if(st->state != RERUNLINE) {
  2690. pop_stack(st->stack,start_sp,end_sp);
  2691. }
  2692. if(st->state==RETFUNC){
  2693. // ユーザー定義関数からの復帰
  2694. int olddefsp=st->stack->defsp;
  2695. int i;
  2696. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2697. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2698. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2699. st->state=END;
  2700. script_reportsrc(st);
  2701. return 1;
  2702. }
  2703. script_free_vars( st->stack->var_function );
  2704. aFree(st->stack->var_function);
  2705. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2706. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2707. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2708. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2709. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2710. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2711. st->state=GOTO;
  2712. }
  2713. return 0;
  2714. }
  2715. /*==========================================
  2716. * script execution
  2717. *------------------------------------------*/
  2718. void run_script_main(struct script_state *st);
  2719. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2720. {
  2721. struct script_state *st;
  2722. if( rootscript == NULL || pos < 0 )
  2723. return;
  2724. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  2725. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  2726. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  2727. st = script_alloc_state(rootscript, pos, rid, oid);
  2728. run_script_main(st);
  2729. }
  2730. void script_stop_sleeptimers(int id)
  2731. {
  2732. struct script_state* st;
  2733. for(;;)
  2734. {
  2735. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2736. if( st == NULL )
  2737. break; // no more sleep timers
  2738. script_free_state(st);
  2739. }
  2740. }
  2741. /*==========================================
  2742. * 指定ノードをsleep_dbから削除
  2743. *------------------------------------------*/
  2744. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2745. {
  2746. struct linkdb_node *retnode;
  2747. if( n == NULL)
  2748. return NULL;
  2749. if( n->prev == NULL )
  2750. sleep_db = n->next;
  2751. else
  2752. n->prev->next = n->next;
  2753. if( n->next )
  2754. n->next->prev = n->prev;
  2755. retnode = n->next;
  2756. aFree( n );
  2757. return retnode; // 次のノードを返す
  2758. }
  2759. /*==========================================
  2760. * sleep用タイマー関数
  2761. *------------------------------------------*/
  2762. int run_script_timer(int tid, unsigned int tick, int id, intptr data)
  2763. {
  2764. struct script_state *st = (struct script_state *)data;
  2765. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2766. TBL_PC *sd = map_id2sd(st->rid);
  2767. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2768. { //Character mismatch. Cancel execution.
  2769. st->rid = 0;
  2770. st->state = END;
  2771. }
  2772. while( node && st->sleep.timer != -1 ) {
  2773. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2774. script_erase_sleepdb(node);
  2775. st->sleep.timer = INVALID_TIMER;
  2776. break;
  2777. }
  2778. node = node->next;
  2779. }
  2780. if(st->state != RERUNLINE)
  2781. st->sleep.tick = 0;
  2782. run_script_main(st);
  2783. return 0;
  2784. }
  2785. /*==========================================
  2786. * スクリプトの実行メイン部分
  2787. *------------------------------------------*/
  2788. void run_script_main(struct script_state *st)
  2789. {
  2790. int cmdcount=script_config.check_cmdcount;
  2791. int gotocount=script_config.check_gotocount;
  2792. TBL_PC *sd;
  2793. //For backing up purposes
  2794. struct script_state *bk_st = NULL;
  2795. int bk_npcid = 0;
  2796. struct script_stack *stack=st->stack;
  2797. sd = map_id2sd(st->rid);
  2798. if(sd){
  2799. if(sd->st != st){
  2800. bk_st = sd->st;
  2801. bk_npcid = sd->npc_id;
  2802. }
  2803. sd->st = st;
  2804. sd->npc_id = st->oid;
  2805. }
  2806. if(st->state == RERUNLINE) {
  2807. run_func(st);
  2808. if(st->state == GOTO)
  2809. st->state = RUN;
  2810. } else if(st->state != END)
  2811. st->state = RUN;
  2812. while(st->state == RUN)
  2813. {
  2814. enum c_op c = get_com(st->script->script_buf,&st->pos);
  2815. switch(c){
  2816. case C_EOL:
  2817. if( stack->sp != stack->defsp )
  2818. {
  2819. if( stack->sp > stack->defsp )
  2820. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2821. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2822. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2823. } else
  2824. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2825. stack->sp = stack->defsp;
  2826. }
  2827. break;
  2828. case C_INT:
  2829. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2830. break;
  2831. case C_POS:
  2832. case C_NAME:
  2833. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2834. st->pos+=3;
  2835. break;
  2836. case C_ARG:
  2837. push_val(stack,c,0);
  2838. break;
  2839. case C_STR:
  2840. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2841. while(st->script->script_buf[st->pos++]);
  2842. break;
  2843. case C_FUNC:
  2844. run_func(st);
  2845. if(st->state==GOTO){
  2846. st->state = RUN;
  2847. if( gotocount>0 && (--gotocount)<=0 ){
  2848. ShowError("run_script: infinity loop !\n");
  2849. script_reportsrc(st);
  2850. st->state=END;
  2851. }
  2852. }
  2853. break;
  2854. case C_NEG:
  2855. case C_NOT:
  2856. case C_LNOT:
  2857. op_1(st ,c);
  2858. break;
  2859. case C_ADD:
  2860. case C_SUB:
  2861. case C_MUL:
  2862. case C_DIV:
  2863. case C_MOD:
  2864. case C_EQ:
  2865. case C_NE:
  2866. case C_GT:
  2867. case C_GE:
  2868. case C_LT:
  2869. case C_LE:
  2870. case C_AND:
  2871. case C_OR:
  2872. case C_XOR:
  2873. case C_LAND:
  2874. case C_LOR:
  2875. case C_R_SHIFT:
  2876. case C_L_SHIFT:
  2877. op_2(st, c);
  2878. break;
  2879. case C_OP3:
  2880. op_3(st, c);
  2881. break;
  2882. case C_NOP:
  2883. st->state=END;
  2884. break;
  2885. default:
  2886. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2887. st->state=END;
  2888. break;
  2889. }
  2890. if( cmdcount>0 && (--cmdcount)<=0 ){
  2891. ShowError("run_script: infinity loop !\n");
  2892. script_reportsrc(st);
  2893. st->state=END;
  2894. }
  2895. }
  2896. if(st->sleep.tick > 0) {
  2897. //Delay execution
  2898. st->sleep.charid = sd?sd->status.char_id:0;
  2899. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2900. run_script_timer, st->sleep.charid, (intptr)st);
  2901. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2902. //Restore previous script
  2903. if (sd) {
  2904. sd->st = bk_st;
  2905. sd->npc_id = bk_npcid;
  2906. bk_st = NULL; //Remove tag for removal.
  2907. }
  2908. }
  2909. else if(st->state != END && sd){
  2910. //Resume later (st is already attached to player).
  2911. if(bk_st) {
  2912. ShowWarning("Unable to restore stack! Double continuation!\n");
  2913. //Report BOTH scripts to see if that can help somehow.
  2914. ShowDebug("Previous script (lost):\n");
  2915. script_reportsrc(bk_st);
  2916. ShowDebug("Current script:\n");
  2917. script_reportsrc(st);
  2918. }
  2919. } else {
  2920. //Dispose of script.
  2921. if (sd)
  2922. { //Restore previous stack and save char.
  2923. if(sd->state.using_fake_npc){
  2924. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  2925. sd->state.using_fake_npc = 0;
  2926. }
  2927. //Restore previous script if any.
  2928. sd->st = bk_st;
  2929. sd->npc_id = bk_npcid;
  2930. if (!bk_st)
  2931. npc_event_dequeue(sd);
  2932. else
  2933. bk_st = NULL; //Remove tag for removal.
  2934. if (sd->state.reg_dirty&2)
  2935. intif_saveregistry(sd,2);
  2936. if (sd->state.reg_dirty&1)
  2937. intif_saveregistry(sd,1);
  2938. }
  2939. script_free_state(st);
  2940. st = NULL;
  2941. }
  2942. if (bk_st)
  2943. { //Remove previous script
  2944. script_free_state(bk_st);
  2945. bk_st = NULL;
  2946. }
  2947. }
  2948. int script_config_read(char *cfgName)
  2949. {
  2950. int i;
  2951. char line[1024],w1[1024],w2[1024];
  2952. FILE *fp;
  2953. fp=fopen(cfgName,"r");
  2954. if(fp==NULL){
  2955. ShowError("file not found: [%s]\n", cfgName);
  2956. return 1;
  2957. }
  2958. while(fgets(line, sizeof(line), fp))
  2959. {
  2960. if(line[0] == '/' && line[1] == '/')
  2961. continue;
  2962. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  2963. if(i!=2)
  2964. continue;
  2965. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  2966. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  2967. }
  2968. else if(strcmpi(w1,"check_cmdcount")==0) {
  2969. script_config.check_cmdcount = config_switch(w2);
  2970. }
  2971. else if(strcmpi(w1,"check_gotocount")==0) {
  2972. script_config.check_gotocount = config_switch(w2);
  2973. }
  2974. else if(strcmpi(w1,"input_min_value")==0) {
  2975. script_config.input_min_value = config_switch(w2);
  2976. }
  2977. else if(strcmpi(w1,"input_max_value")==0) {
  2978. script_config.input_max_value = config_switch(w2);
  2979. }
  2980. else if(strcmpi(w1,"import")==0){
  2981. script_config_read(w2);
  2982. }
  2983. }
  2984. fclose(fp);
  2985. return 0;
  2986. }
  2987. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  2988. {
  2989. struct script_code *code = (struct script_code *)data;
  2990. if(code){
  2991. script_free_vars( &code->script_vars );
  2992. aFree( code->script_buf );
  2993. aFree( code );
  2994. }
  2995. return 0;
  2996. }
  2997. /*==========================================
  2998. * 終了
  2999. *------------------------------------------*/
  3000. int do_final_script()
  3001. {
  3002. #ifdef DEBUG_HASH
  3003. if (battle_config.etc_log)
  3004. {
  3005. FILE *fp = fopen("hash_dump.txt","wt");
  3006. if(fp) {
  3007. int i,count[SCRIPT_HASH_SIZE];
  3008. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3009. int n=0;
  3010. int min=INT_MAX,max=0,zero=0;
  3011. double mean=0.0f;
  3012. double median=0.0f;
  3013. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3014. memset(count, 0, sizeof(count));
  3015. fprintf(fp,"num : hash : data_name\n");
  3016. fprintf(fp,"---------------------------------------------------------------\n");
  3017. for(i=LABEL_START; i<str_num; i++) {
  3018. unsigned int h = calc_hash(get_str(i));
  3019. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3020. ++count[h];
  3021. }
  3022. fprintf(fp,"--------------------\n\n");
  3023. memset(count2, 0, sizeof(count2));
  3024. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3025. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3026. if(min > count[i])
  3027. min = count[i]; // minimun count of collision
  3028. if(max < count[i])
  3029. max = count[i]; // maximun count of collision
  3030. if(count[i] == 0)
  3031. zero++;
  3032. ++count2[count[i]];
  3033. }
  3034. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3035. for( i=min; i <= max; ++i ){
  3036. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3037. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3038. }
  3039. for( i=min; i <= max; ++i ){
  3040. n += count2[i];
  3041. if( n*2 >= SCRIPT_HASH_SIZE )
  3042. {
  3043. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3044. median = (i+i+1)/2.0f;
  3045. else
  3046. median = i;
  3047. break;
  3048. }
  3049. }
  3050. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3051. fclose(fp);
  3052. }
  3053. }
  3054. #endif
  3055. mapreg_final();
  3056. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3057. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3058. if(sleep_db) {
  3059. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3060. while(n) {
  3061. struct script_state *st = (struct script_state *)n->data;
  3062. script_free_state(st);
  3063. n = n->next;
  3064. }
  3065. linkdb_final(&sleep_db);
  3066. }
  3067. if (str_data)
  3068. aFree(str_data);
  3069. if (str_buf)
  3070. aFree(str_buf);
  3071. return 0;
  3072. }
  3073. /*==========================================
  3074. * 初期化
  3075. *------------------------------------------*/
  3076. int do_init_script()
  3077. {
  3078. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3079. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3080. mapreg_init();
  3081. return 0;
  3082. }
  3083. int script_reload()
  3084. {
  3085. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3086. scriptlabel_db->clear(scriptlabel_db, NULL);
  3087. if(sleep_db) {
  3088. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3089. while(n) {
  3090. struct script_state *st = (struct script_state *)n->data;
  3091. script_free_state(st);
  3092. n = n->next;
  3093. }
  3094. linkdb_final(&sleep_db);
  3095. }
  3096. mapreg_reload();
  3097. return 0;
  3098. }
  3099. //-----------------------------------------------------------------------------
  3100. // buildin functions
  3101. //
  3102. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3103. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3104. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3105. /////////////////////////////////////////////////////////////////////
  3106. // NPC interaction
  3107. //
  3108. /// Appends a message to the npc dialog.
  3109. /// If a dialog doesn't exist yet, one is created.
  3110. ///
  3111. /// mes "<message>";
  3112. BUILDIN_FUNC(mes)
  3113. {
  3114. TBL_PC* sd = script_rid2sd(st);
  3115. if( sd == NULL )
  3116. return 0;
  3117. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3118. return 0;
  3119. }
  3120. /// Displays the button 'next' in the npc dialog.
  3121. /// The dialog text is cleared and the script continues when the button is pressed.
  3122. ///
  3123. /// next;
  3124. BUILDIN_FUNC(next)
  3125. {
  3126. TBL_PC* sd;
  3127. sd = script_rid2sd(st);
  3128. if( sd == NULL )
  3129. return 0;
  3130. st->state = STOP;
  3131. clif_scriptnext(sd, st->oid);
  3132. return 0;
  3133. }
  3134. /// Ends the script and displays the button 'close' on the npc dialog.
  3135. /// The dialog is closed when the button is pressed.
  3136. ///
  3137. /// close;
  3138. BUILDIN_FUNC(close)
  3139. {
  3140. TBL_PC* sd;
  3141. sd = script_rid2sd(st);
  3142. if( sd == NULL )
  3143. return 0;
  3144. st->state = END;
  3145. clif_scriptclose(sd, st->oid);
  3146. return 0;
  3147. }
  3148. /// Displays the button 'close' on the npc dialog.
  3149. /// The dialog is closed and the script continues when the button is pressed.
  3150. ///
  3151. /// close2;
  3152. BUILDIN_FUNC(close2)
  3153. {
  3154. TBL_PC* sd;
  3155. sd = script_rid2sd(st);
  3156. if( sd == NULL )
  3157. return 0;
  3158. st->state = STOP;
  3159. clif_scriptclose(sd, st->oid);
  3160. return 0;
  3161. }
  3162. /// Counts the number of valid and total number of options in 'str'
  3163. /// If max_count > 0 the counting stops when that valid option is reached
  3164. /// total is incremented for each option (NULL is supported)
  3165. static int menu_countoptions(const char* str, int max_count, int* total)
  3166. {
  3167. int count = 0;
  3168. int bogus_total;
  3169. if( total == NULL )
  3170. total = &bogus_total;
  3171. ++(*total);
  3172. // initial empty options
  3173. while( *str == ':' )
  3174. {
  3175. ++str;
  3176. ++(*total);
  3177. }
  3178. // count menu options
  3179. while( *str != '\0' )
  3180. {
  3181. ++count;
  3182. --max_count;
  3183. if( max_count == 0 )
  3184. break;
  3185. while( *str != ':' && *str != '\0' )
  3186. ++str;
  3187. while( *str == ':' )
  3188. {
  3189. ++str;
  3190. ++(*total);
  3191. }
  3192. }
  3193. return count;
  3194. }
  3195. /// Displays a menu with options and goes to the target label.
  3196. /// The script is stopped if cancel is pressed.
  3197. /// Options with no text are not displayed in the client.
  3198. ///
  3199. /// Options can be grouped together, separated by the character ':' in the text:
  3200. /// ex: menu "A:B:C",L_target;
  3201. /// All these options go to the specified target label.
  3202. ///
  3203. /// The index of the selected option is put in the variable @menu.
  3204. /// Indexes start with 1 and are consistent with grouped and empty options.
  3205. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3206. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3207. ///
  3208. /// NOTE: the client closes the npc dialog when cancel is pressed
  3209. ///
  3210. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3211. BUILDIN_FUNC(menu)
  3212. {
  3213. int i;
  3214. const char* text;
  3215. TBL_PC* sd;
  3216. sd = script_rid2sd(st);
  3217. if( sd == NULL )
  3218. return 0;
  3219. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3220. if( sd->state.menu_or_input == 0 )
  3221. {
  3222. struct StringBuf buf;
  3223. struct script_data* data;
  3224. if( script_lastdata(st) % 2 == 0 )
  3225. {// argument count is not even (1st argument is at index 2)
  3226. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3227. st->state = END;
  3228. return 1;
  3229. }
  3230. StringBuf_Init(&buf);
  3231. sd->npc_menu = 0;
  3232. for( i = 2; i < script_lastdata(st); i += 2 )
  3233. {
  3234. // menu options
  3235. text = script_getstr(st, i);
  3236. // target label
  3237. data = script_getdata(st, i+1);
  3238. if( !data_islabel(data) )
  3239. {// not a label
  3240. StringBuf_Destroy(&buf);
  3241. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3242. script_reportdata(data);
  3243. st->state = END;
  3244. return 1;
  3245. }
  3246. // append option(s)
  3247. if( text[0] == '\0' )
  3248. continue;// empty string, ignore
  3249. if( sd->npc_menu > 0 )
  3250. StringBuf_AppendStr(&buf, ":");
  3251. StringBuf_AppendStr(&buf, text);
  3252. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3253. }
  3254. st->state = RERUNLINE;
  3255. sd->state.menu_or_input = 1;
  3256. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3257. StringBuf_Destroy(&buf);
  3258. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3259. }
  3260. else if( sd->npc_menu == 0xff )
  3261. {// Cancel was pressed
  3262. sd->state.menu_or_input = 0;
  3263. st->state = END;
  3264. }
  3265. else
  3266. {// goto target label
  3267. int menu = 0;
  3268. sd->state.menu_or_input = 0;
  3269. if( sd->npc_menu <= 0 )
  3270. {
  3271. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3272. st->state = END;
  3273. return 1;
  3274. }
  3275. // get target label
  3276. for( i = 2; i < script_lastdata(st); i += 2 )
  3277. {
  3278. text = script_getstr(st, i);
  3279. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3280. if( sd->npc_menu <= 0 )
  3281. break;// entry found
  3282. }
  3283. if( sd->npc_menu > 0 )
  3284. {// Invalid selection
  3285. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3286. st->state = END;
  3287. return 1;
  3288. }
  3289. if( !data_islabel(script_getdata(st, i + 1)) )
  3290. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3291. ShowError("script:menu: unexpected data in label argument\n");
  3292. script_reportdata(script_getdata(st, i + 1));
  3293. st->state = END;
  3294. return 1;
  3295. }
  3296. pc_setreg(sd, add_str("@menu"), menu);
  3297. st->pos = script_getnum(st, i + 1);
  3298. st->state = GOTO;
  3299. }
  3300. return 0;
  3301. }
  3302. /// Displays a menu with options and returns the selected option.
  3303. /// Behaves like 'menu' without the target labels.
  3304. ///
  3305. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3306. ///
  3307. /// @see menu
  3308. BUILDIN_FUNC(select)
  3309. {
  3310. int i;
  3311. const char* text;
  3312. TBL_PC* sd;
  3313. sd = script_rid2sd(st);
  3314. if( sd == NULL )
  3315. return 0;
  3316. if( sd->state.menu_or_input == 0 )
  3317. {
  3318. struct StringBuf buf;
  3319. StringBuf_Init(&buf);
  3320. sd->npc_menu = 0;
  3321. for( i = 2; i <= script_lastdata(st); ++i )
  3322. {
  3323. text = script_getstr(st, i);
  3324. if( sd->npc_menu > 0 )
  3325. StringBuf_AppendStr(&buf, ":");
  3326. StringBuf_AppendStr(&buf, text);
  3327. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3328. }
  3329. st->state = RERUNLINE;
  3330. sd->state.menu_or_input = 1;
  3331. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3332. StringBuf_Destroy(&buf);
  3333. }
  3334. else if( sd->npc_menu == 0xff )
  3335. {// Cancel was pressed
  3336. sd->state.menu_or_input = 0;
  3337. st->state = END;
  3338. }
  3339. else
  3340. {// return selected option
  3341. int menu = 0;
  3342. sd->state.menu_or_input = 0;
  3343. for( i = 2; i <= script_lastdata(st); ++i )
  3344. {
  3345. text = script_getstr(st, i);
  3346. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3347. if( sd->npc_menu <= 0 )
  3348. break;// entry found
  3349. }
  3350. pc_setreg(sd, add_str("@menu"), menu);
  3351. script_pushint(st, menu);
  3352. st->state = RUN;
  3353. }
  3354. return 0;
  3355. }
  3356. /// Displays a menu with options and returns the selected option.
  3357. /// Behaves like 'menu' without the target labels, except when cancel is
  3358. /// pressed.
  3359. /// When cancel is pressed, the script continues and 255 is returned.
  3360. ///
  3361. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3362. ///
  3363. /// @see menu
  3364. BUILDIN_FUNC(prompt)
  3365. {
  3366. int i;
  3367. const char *text;
  3368. TBL_PC* sd;
  3369. sd = script_rid2sd(st);
  3370. if( sd == NULL )
  3371. return 0;
  3372. if( sd->state.menu_or_input == 0 )
  3373. {
  3374. struct StringBuf buf;
  3375. StringBuf_Init(&buf);
  3376. sd->npc_menu = 0;
  3377. for( i = 2; i <= script_lastdata(st); ++i )
  3378. {
  3379. text = script_getstr(st, i);
  3380. if( sd->npc_menu > 0 )
  3381. StringBuf_AppendStr(&buf, ":");
  3382. StringBuf_AppendStr(&buf, text);
  3383. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3384. }
  3385. st->state = RERUNLINE;
  3386. sd->state.menu_or_input = 1;
  3387. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3388. StringBuf_Destroy(&buf);
  3389. }
  3390. else if( sd->npc_menu == 0xff )
  3391. {// Cancel was pressed
  3392. sd->state.menu_or_input = 0;
  3393. pc_setreg(sd, add_str("@menu"), 0xff);
  3394. script_pushint(st, 0xff);
  3395. st->state = RUN;
  3396. }
  3397. else
  3398. {// return selected option
  3399. int menu = 0;
  3400. sd->state.menu_or_input = 0;
  3401. for( i = 2; i <= script_lastdata(st); ++i )
  3402. {
  3403. text = script_getstr(st, i);
  3404. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3405. if( sd->npc_menu <= 0 )
  3406. break;// entry found
  3407. }
  3408. pc_setreg(sd, add_str("@menu"), menu);
  3409. script_pushint(st, menu);
  3410. st->state = RUN;
  3411. }
  3412. return 0;
  3413. }
  3414. /////////////////////////////////////////////////////////////////////
  3415. // ...
  3416. //
  3417. /// Jumps to the target script label.
  3418. ///
  3419. /// goto <label>;
  3420. BUILDIN_FUNC(goto)
  3421. {
  3422. if( !data_islabel(script_getdata(st,2)) )
  3423. {
  3424. ShowError("script:goto: not a label\n");
  3425. script_reportdata(script_getdata(st,2));
  3426. st->state = END;
  3427. return 1;
  3428. }
  3429. st->pos = script_getnum(st,2);
  3430. st->state = GOTO;
  3431. return 0;
  3432. }
  3433. /*==========================================
  3434. * user-defined function call
  3435. *------------------------------------------*/
  3436. BUILDIN_FUNC(callfunc)
  3437. {
  3438. int i, j;
  3439. struct linkdb_node** oldval;
  3440. struct script_code *scr, *oldscr;
  3441. const char* str = script_getstr(st,2);
  3442. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3443. if( !scr )
  3444. {
  3445. ShowError("script:callfunc: function not found! [%s]\n", str);
  3446. st->state = END;
  3447. return 1;
  3448. }
  3449. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3450. push_copy(st->stack,i);
  3451. script_pushint(st,j); // push argument count
  3452. script_pushint(st,st->stack->defsp); // push current stack pointer
  3453. script_pushint(st,(int)st->script); // push current script
  3454. script_pushint(st,(int)st->stack->var_function); // push function-dependent variables
  3455. push_val(st->stack,C_RETINFO,st->pos); // push current script location
  3456. oldscr = st->script;
  3457. oldval = st->stack->var_function;
  3458. st->pos = 0;
  3459. st->script = scr;
  3460. st->stack->defsp = st->start+5+j;
  3461. st->state = GOTO;
  3462. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3463. for( i = 0; i < j; i++ )
  3464. {
  3465. struct script_data* s = script_getdatatop(st, -6-i);
  3466. if( data_isreference(s) && !s->ref )
  3467. {
  3468. const char* name = reference_getname(s);
  3469. if( name[0] == '.' && name[1] == '@' )
  3470. s->ref = oldval;
  3471. else if( name[0] == '.' )
  3472. s->ref = &oldscr->script_vars;
  3473. }
  3474. }
  3475. return 0;
  3476. }
  3477. /*==========================================
  3478. * subroutine call
  3479. *------------------------------------------*/
  3480. BUILDIN_FUNC(callsub)
  3481. {
  3482. int i,j;
  3483. struct linkdb_node** oldval;
  3484. int pos = script_getnum(st,2);
  3485. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3486. {
  3487. ShowError("script:callsub: argument is not a label\n");
  3488. script_reportdata(script_getdata(st,2));
  3489. st->state = END;
  3490. return 1;
  3491. }
  3492. oldval = st->stack->var_function;
  3493. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3494. push_copy(st->stack,i);
  3495. script_pushint(st,j); // push argument count
  3496. script_pushint(st,st->stack->defsp); // push current stack pointer
  3497. script_pushint(st,(int)st->script); // push current script
  3498. script_pushint(st,(int)st->stack->var_function); // push function-dependent variables
  3499. push_val(st->stack,C_RETINFO,st->pos); // push current script location
  3500. st->pos = pos;
  3501. st->stack->defsp = st->start+5+j;
  3502. st->state = GOTO;
  3503. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3504. for(i = 0; i < j; i++)
  3505. {
  3506. struct script_data* s = script_getdatatop(st, -6-i);
  3507. if( data_isreference(s) && !s->ref )
  3508. {
  3509. const char* name = reference_getname(s);
  3510. if( name[0] == '.' && name[1] == '@' )
  3511. s->ref = oldval;
  3512. }
  3513. }
  3514. return 0;
  3515. }
  3516. /// Retrieves an argument provided to callfunc/callsub.
  3517. /// If the argument doesn't exist
  3518. ///
  3519. /// getarg(<index>{,<default_value>}) -> <value>
  3520. BUILDIN_FUNC(getarg)
  3521. {
  3522. int idx;
  3523. int count;
  3524. int stsp;
  3525. if( st->stack->defsp < 5 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3526. {
  3527. ShowError("script:getarg: no callfunc or callsub!\n");
  3528. st->state = END;
  3529. return 1;
  3530. }
  3531. count = conv_num(st, &(st->stack->stack_data[st->stack->defsp - 5]));
  3532. stsp = st->stack->defsp - count - 5;
  3533. idx = script_getnum(st,2);
  3534. if( idx < count )
  3535. push_copy(st->stack, stsp + idx);
  3536. else if( script_hasdata(st,3) )
  3537. script_pushcopy(st, 3);
  3538. else
  3539. {
  3540. ShowError("script:getarg: index (idx=%d) out of range (count=%d) and no default value found\n", idx, count);
  3541. st->state = END;
  3542. return 1;
  3543. }
  3544. return 0;
  3545. }
  3546. /// Returns from the current function, optionaly returning a value from the functions.
  3547. /// Don't use outside script functions.
  3548. ///
  3549. /// return;
  3550. /// return <value>;
  3551. BUILDIN_FUNC(return)
  3552. {
  3553. if( script_hasdata(st,2) )
  3554. {// return value
  3555. struct script_data* data;
  3556. script_pushcopy(st, 2);
  3557. data = script_getdatatop(st, -1);
  3558. if( data_isreference(data) )
  3559. {
  3560. const char* name = reference_getname(data);
  3561. if( name[0] == '.' && name[1] == '@' )
  3562. {// temporary script variable, convert to value
  3563. get_val(st, data);
  3564. }
  3565. else if( name[0] == '.' && !data->ref )
  3566. {// script variable, link to current script
  3567. data->ref = &st->script->script_vars;
  3568. }
  3569. }
  3570. }
  3571. else
  3572. {// no return value
  3573. script_pushnil(st);
  3574. }
  3575. st->state = RETFUNC;
  3576. return 0;
  3577. }
  3578. /// Returns a random number from 0 to <range>-1.
  3579. /// Or returns a random number from <min> to <max>.
  3580. /// If <min> is greater than <max>, their numbers are switched.
  3581. /// rand(<range>) -> <int>
  3582. /// rand(<min>,<max>) -> <int>
  3583. BUILDIN_FUNC(rand)
  3584. {
  3585. int range;
  3586. int min;
  3587. int max;
  3588. if( script_hasdata(st,3) )
  3589. {// min,max
  3590. min = script_getnum(st,2);
  3591. max = script_getnum(st,3);
  3592. if( max < min )
  3593. swap(min, max);
  3594. range = max - min + 1;
  3595. }
  3596. else
  3597. {// range
  3598. min = 0;
  3599. range = script_getnum(st,2);
  3600. }
  3601. if( range <= 1 )
  3602. script_pushint(st, min);
  3603. else
  3604. script_pushint(st, rand()%range + min);
  3605. return 0;
  3606. }
  3607. /*==========================================
  3608. *
  3609. *------------------------------------------*/
  3610. BUILDIN_FUNC(warp)
  3611. {
  3612. int ret;
  3613. int x,y;
  3614. const char* str;
  3615. TBL_PC* sd;
  3616. sd = script_rid2sd(st);
  3617. if( sd == NULL )
  3618. return 0;
  3619. str = script_getstr(st,2);
  3620. x = script_getnum(st,3);
  3621. y = script_getnum(st,4);
  3622. if(strcmp(str,"Random")==0)
  3623. ret = pc_randomwarp(sd,3);
  3624. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3625. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3626. else
  3627. ret = pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3628. if( ret ) {
  3629. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3630. script_reportsrc(st);
  3631. }
  3632. return 0;
  3633. }
  3634. /*==========================================
  3635. * エリア指定ワープ
  3636. *------------------------------------------*/
  3637. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3638. {
  3639. int x,y;
  3640. unsigned int map;
  3641. map=va_arg(ap, unsigned int);
  3642. x=va_arg(ap,int);
  3643. y=va_arg(ap,int);
  3644. if(map == 0)
  3645. pc_randomwarp((TBL_PC *)bl,3);
  3646. else
  3647. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3648. return 0;
  3649. }
  3650. BUILDIN_FUNC(areawarp)
  3651. {
  3652. int x,y,m;
  3653. unsigned int index;
  3654. const char *str;
  3655. const char *mapname;
  3656. int x0,y0,x1,y1;
  3657. mapname=script_getstr(st,2);
  3658. x0=script_getnum(st,3);
  3659. y0=script_getnum(st,4);
  3660. x1=script_getnum(st,5);
  3661. y1=script_getnum(st,6);
  3662. str=script_getstr(st,7);
  3663. x=script_getnum(st,8);
  3664. y=script_getnum(st,9);
  3665. if( (m=map_mapname2mapid(mapname))< 0)
  3666. return 0;
  3667. if(strcmp(str,"Random")==0)
  3668. index = 0;
  3669. else if(!(index=mapindex_name2id(str)))
  3670. return 0;
  3671. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  3672. return 0;
  3673. }
  3674. /*==========================================
  3675. * warpchar [LuzZza]
  3676. * Useful for warp one player from
  3677. * another player npc-session.
  3678. * Using: warpchar "mapname",x,y,Char_ID;
  3679. *------------------------------------------*/
  3680. BUILDIN_FUNC(warpchar)
  3681. {
  3682. int x,y,a;
  3683. const char *str;
  3684. TBL_PC *sd;
  3685. str=script_getstr(st,2);
  3686. x=script_getnum(st,3);
  3687. y=script_getnum(st,4);
  3688. a=script_getnum(st,5);
  3689. sd = map_charid2sd(a);
  3690. if( sd == NULL )
  3691. return 0;
  3692. if(strcmp(str, "Random") == 0)
  3693. pc_randomwarp(sd, 3);
  3694. else
  3695. if(strcmp(str, "SavePoint") == 0)
  3696. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  3697. else
  3698. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  3699. return 0;
  3700. }
  3701. /*==========================================
  3702. * Warpparty - [Fredzilla]
  3703. * Syntax: warpparty "mapname",x,y,Party_ID;
  3704. *------------------------------------------*/
  3705. BUILDIN_FUNC(warpparty)
  3706. {
  3707. int x,y;
  3708. const char *str;
  3709. int p_id;
  3710. int i;
  3711. unsigned short mapindex;
  3712. TBL_PC *pl_sd;
  3713. struct party_data *p=NULL;
  3714. str=script_getstr(st,2);
  3715. x=script_getnum(st,3);
  3716. y=script_getnum(st,4);
  3717. p_id=script_getnum(st,5);
  3718. if(p_id < 1)
  3719. return 0;
  3720. p = party_search(p_id);
  3721. if (!p)
  3722. return 0;
  3723. if(strcmp(str,"Random")==0)
  3724. {
  3725. for (i = 0; i < MAX_PARTY; i++)
  3726. {
  3727. if ((pl_sd = p->data[i].sd))
  3728. {
  3729. if(map[pl_sd->bl.m].flag.nowarp)
  3730. continue;
  3731. pc_randomwarp(pl_sd,3);
  3732. }
  3733. }
  3734. }
  3735. else if(strcmp(str,"SavePointAll")==0)
  3736. {
  3737. for (i = 0; i < MAX_PARTY; i++)
  3738. {
  3739. if ((pl_sd = p->data[i].sd))
  3740. {
  3741. if(map[pl_sd->bl.m].flag.noreturn)
  3742. continue;
  3743. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3744. }
  3745. }
  3746. }
  3747. else if(strcmp(str,"SavePoint")==0)
  3748. {
  3749. pl_sd=script_rid2sd(st);
  3750. if (!pl_sd) return 0;
  3751. mapindex=pl_sd->status.save_point.map;
  3752. x=pl_sd->status.save_point.x;
  3753. y=pl_sd->status.save_point.y;
  3754. for (i = 0; i < MAX_PARTY; i++)
  3755. {
  3756. if ((pl_sd = p->data[i].sd))
  3757. {
  3758. if(map[pl_sd->bl.m].flag.noreturn)
  3759. continue;
  3760. pc_setpos(pl_sd,mapindex,x,y,3);
  3761. }
  3762. }
  3763. }
  3764. else if(strcmp(str,"Leader")==0)
  3765. {
  3766. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  3767. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  3768. return 0;
  3769. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  3770. return 0;
  3771. mapindex = p->data[i].sd->mapindex;
  3772. x = p->data[i].sd->bl.x;
  3773. y = p->data[i].sd->bl.y;
  3774. for (i = 0; i < MAX_PARTY; i++)
  3775. {
  3776. pl_sd = p->data[i].sd;
  3777. if (!pl_sd)
  3778. continue;
  3779. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  3780. continue;
  3781. pc_setpos(pl_sd,mapindex,x,y,3);
  3782. }
  3783. }
  3784. else
  3785. {
  3786. mapindex = mapindex_name2id(str);
  3787. if (!mapindex) //Show source of npc error.
  3788. return 1;
  3789. for (i = 0; i < MAX_PARTY; i++)
  3790. {
  3791. if ((pl_sd = p->data[i].sd))
  3792. {
  3793. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  3794. continue;
  3795. pc_setpos(pl_sd,mapindex,x,y,3);
  3796. }
  3797. }
  3798. }
  3799. return 0;
  3800. }
  3801. /*==========================================
  3802. * Warpguild - [Fredzilla]
  3803. * Syntax: warpguild "mapname",x,y,Guild_ID;
  3804. *------------------------------------------*/
  3805. BUILDIN_FUNC(warpguild)
  3806. {
  3807. TBL_PC *sd;
  3808. TBL_PC *pl_sd;
  3809. struct guild* g;
  3810. struct s_mapiterator* iter;
  3811. int type;
  3812. const char* str = script_getstr(st,2);
  3813. int x = script_getnum(st,3);
  3814. int y = script_getnum(st,4);
  3815. int gid = script_getnum(st,5);
  3816. sd=script_rid2sd(st);
  3817. if( sd == NULL )
  3818. return 0;
  3819. g = guild_search(gid);
  3820. if( g == NULL )
  3821. return 0;
  3822. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  3823. return 0;
  3824. type = ( strcmp(str,"Random")==0 ) ? 0
  3825. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  3826. : ( strcmp(str,"SavePoint")==0 ) ? 2
  3827. : 3;
  3828. iter = mapit_getallusers();
  3829. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  3830. {
  3831. if( pl_sd->status.guild_id != gid )
  3832. continue;
  3833. switch( type )
  3834. {
  3835. case 0: // Random
  3836. if(!map[pl_sd->bl.m].flag.nowarp)
  3837. pc_randomwarp(pl_sd,3);
  3838. break;
  3839. case 1: // SavePointAll
  3840. if(!map[pl_sd->bl.m].flag.noreturn)
  3841. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3842. break;
  3843. case 2: // SavePoint
  3844. if(!map[pl_sd->bl.m].flag.noreturn)
  3845. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3846. break;
  3847. case 3: // m,x,y
  3848. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  3849. pc_setpos(pl_sd,mapindex_name2id(str),x,y,3);
  3850. break;
  3851. }
  3852. }
  3853. mapit_free(iter);
  3854. return 0;
  3855. }
  3856. /*==========================================
  3857. *
  3858. *------------------------------------------*/
  3859. BUILDIN_FUNC(heal)
  3860. {
  3861. TBL_PC *sd;
  3862. int hp,sp;
  3863. sd = script_rid2sd(st);
  3864. if (!sd) return 0;
  3865. hp=script_getnum(st,2);
  3866. sp=script_getnum(st,3);
  3867. status_heal(&sd->bl, hp, sp, 1);
  3868. return 0;
  3869. }
  3870. /*==========================================
  3871. *
  3872. *------------------------------------------*/
  3873. BUILDIN_FUNC(itemheal)
  3874. {
  3875. TBL_PC *sd;
  3876. int hp,sp;
  3877. hp=script_getnum(st,2);
  3878. sp=script_getnum(st,3);
  3879. if(potion_flag==1) {
  3880. potion_hp = hp;
  3881. potion_sp = sp;
  3882. return 0;
  3883. }
  3884. sd = script_rid2sd(st);
  3885. if (!sd) return 0;
  3886. pc_itemheal(sd,sd->itemid,hp,sp);
  3887. return 0;
  3888. }
  3889. /*==========================================
  3890. *
  3891. *------------------------------------------*/
  3892. BUILDIN_FUNC(percentheal)
  3893. {
  3894. int hp,sp;
  3895. TBL_PC* sd;
  3896. hp=script_getnum(st,2);
  3897. sp=script_getnum(st,3);
  3898. if(potion_flag==1) {
  3899. potion_per_hp = hp;
  3900. potion_per_sp = sp;
  3901. return 0;
  3902. }
  3903. sd = script_rid2sd(st);
  3904. if( sd == NULL )
  3905. return 0;
  3906. pc_percentheal(sd,hp,sp);
  3907. return 0;
  3908. }
  3909. /*==========================================
  3910. *
  3911. *------------------------------------------*/
  3912. BUILDIN_FUNC(jobchange)
  3913. {
  3914. int job, upper=-1;
  3915. job=script_getnum(st,2);
  3916. if( script_hasdata(st,3) )
  3917. upper=script_getnum(st,3);
  3918. if (pcdb_checkid(job))
  3919. {
  3920. TBL_PC* sd;
  3921. sd = script_rid2sd(st);
  3922. if( sd == NULL )
  3923. return 0;
  3924. pc_jobchange(sd, job, upper);
  3925. }
  3926. return 0;
  3927. }
  3928. /*==========================================
  3929. *
  3930. *------------------------------------------*/
  3931. BUILDIN_FUNC(jobname)
  3932. {
  3933. int class_=script_getnum(st,2);
  3934. script_pushconststr(st,job_name(class_));
  3935. return 0;
  3936. }
  3937. /// Get input from the player.
  3938. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  3939. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  3940. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  3941. /// shorter than 'min' and 0 otherwise.
  3942. ///
  3943. /// input(<var>{,<min>{,<max>}}) -> <int>
  3944. BUILDIN_FUNC(input)
  3945. {
  3946. TBL_PC* sd;
  3947. struct script_data* data;
  3948. int uid;
  3949. const char* name;
  3950. int min;
  3951. int max;
  3952. sd = script_rid2sd(st);
  3953. if( sd == NULL )
  3954. return 0;
  3955. data = script_getdata(st,2);
  3956. if( !data_isreference(data) ){
  3957. ShowError("script:input: not a variable\n");
  3958. script_reportdata(data);
  3959. st->state = END;
  3960. return 1;
  3961. }
  3962. uid = reference_getuid(data);
  3963. name = reference_getname(data);
  3964. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  3965. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  3966. if( !sd->state.menu_or_input )
  3967. { // first invocation, display npc input box
  3968. sd->state.menu_or_input = 1;
  3969. st->state = RERUNLINE;
  3970. if( is_string_variable(name) )
  3971. clif_scriptinputstr(sd,st->oid);
  3972. else
  3973. clif_scriptinput(sd,st->oid);
  3974. }
  3975. else
  3976. { // take received text/value and store it in the designated variable
  3977. sd->state.menu_or_input = 0;
  3978. if( is_string_variable(name) )
  3979. {
  3980. size_t len = strlen(sd->npc_str);
  3981. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  3982. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  3983. }
  3984. else
  3985. {
  3986. int amount = sd->npc_amount;
  3987. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  3988. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  3989. }
  3990. st->state = RUN;
  3991. }
  3992. return 0;
  3993. }
  3994. /// Sets the value of a variable.
  3995. /// The value is converted to the type of the variable.
  3996. ///
  3997. /// set(<variable>,<value>) -> <variable>
  3998. BUILDIN_FUNC(set)
  3999. {
  4000. TBL_PC* sd = NULL;
  4001. struct script_data* data;
  4002. int num;
  4003. const char* name;
  4004. char prefix;
  4005. data = script_getdata(st,2);
  4006. if( !data_isreference(data) )
  4007. {
  4008. ShowError("script:set: not a variable\n");
  4009. script_reportdata(script_getdata(st,2));
  4010. st->state = END;
  4011. return 1;
  4012. }
  4013. num = reference_getuid(data);
  4014. name = reference_getname(data);
  4015. prefix = *name;
  4016. if( not_server_variable(prefix) )
  4017. {
  4018. sd = script_rid2sd(st);
  4019. if( sd == NULL )
  4020. {
  4021. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4022. return 0;
  4023. }
  4024. }
  4025. if( is_string_variable(name) )
  4026. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4027. else
  4028. set_reg(st,sd,num,name,(void*)script_getnum(st,3),script_getref(st,2));
  4029. // return a copy of the variable reference
  4030. script_pushcopy(st,2);
  4031. return 0;
  4032. }
  4033. /////////////////////////////////////////////////////////////////////
  4034. /// Array variables
  4035. ///
  4036. /// Returns the size of the specified array
  4037. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4038. {
  4039. int32 ret = idx;
  4040. if( isstring )
  4041. {
  4042. for( ; idx < 128; ++idx )
  4043. {
  4044. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4045. if( str && *str )
  4046. ret = idx + 1;
  4047. script_removetop(st, -1, 0);
  4048. }
  4049. }
  4050. else
  4051. {
  4052. for( ; idx < 128; ++idx )
  4053. {
  4054. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4055. if( num )
  4056. ret = idx + 1;
  4057. script_removetop(st, -1, 0);
  4058. }
  4059. }
  4060. return ret;
  4061. }
  4062. /// Sets values of an array, from the starting index.
  4063. /// ex: setarray arr[1],1,2,3;
  4064. ///
  4065. /// setarray <array variable>,<value1>{,<value2>...};
  4066. BUILDIN_FUNC(setarray)
  4067. {
  4068. struct script_data* data;
  4069. const char* name;
  4070. int32 start;
  4071. int32 end;
  4072. int32 id;
  4073. int32 i;
  4074. TBL_PC* sd = NULL;
  4075. data = script_getdata(st, 2);
  4076. if( !data_isreference(data) )
  4077. {
  4078. ShowError("script:setarray: not a variable\n");
  4079. script_reportdata(data);
  4080. st->state = END;
  4081. return 1;// not a variable
  4082. }
  4083. id = reference_getid(data);
  4084. start = reference_getindex(data);
  4085. name = reference_getname(data);
  4086. if( not_array_variable(*name) )
  4087. {
  4088. ShowError("script:setarray: illegal scope\n");
  4089. script_reportdata(data);
  4090. st->state = END;
  4091. return 1;// not supported
  4092. }
  4093. if( not_server_variable(*name) )
  4094. {
  4095. sd = script_rid2sd(st);
  4096. if( sd == NULL )
  4097. return 0;// no player attached
  4098. }
  4099. end = start + script_lastdata(st) - 2;
  4100. if( end > 127 )
  4101. end = 127;
  4102. if( is_string_variable(name) )
  4103. {// string array
  4104. for( i = 3; start < end; ++start, ++i )
  4105. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4106. }
  4107. else
  4108. {// int array
  4109. for( i = 3; start < end; ++start, ++i )
  4110. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getnum(st,i), reference_getref(data));
  4111. }
  4112. return 0;
  4113. }
  4114. /// Sets count values of an array, from the starting index.
  4115. /// ex: cleararray arr[0],0,1;
  4116. ///
  4117. /// cleararray <array variable>,<value>,<count>;
  4118. BUILDIN_FUNC(cleararray)
  4119. {
  4120. struct script_data* data;
  4121. const char* name;
  4122. int32 start;
  4123. int32 end;
  4124. int32 id;
  4125. void* v;
  4126. TBL_PC* sd = NULL;
  4127. data = script_getdata(st, 2);
  4128. if( !data_isreference(data) )
  4129. {
  4130. ShowError("script:cleararray: not a variable\n");
  4131. script_reportdata(data);
  4132. st->state = END;
  4133. return 1;// not a variable
  4134. }
  4135. id = reference_getid(data);
  4136. start = reference_getindex(data);
  4137. name = reference_getname(data);
  4138. if( not_array_variable(*name) )
  4139. {
  4140. ShowError("script:cleararray: illegal scope\n");
  4141. script_reportdata(data);
  4142. st->state = END;
  4143. return 1;// not supported
  4144. }
  4145. if( not_server_variable(*name) )
  4146. {
  4147. sd = script_rid2sd(st);
  4148. if( sd == NULL )
  4149. return 0;// no player attached
  4150. }
  4151. if( is_string_variable(name) )
  4152. v = (void*)script_getstr(st, 3);
  4153. else
  4154. v = (void*)script_getnum(st, 3);
  4155. end = start + script_getnum(st, 4);
  4156. if( end > 127 )
  4157. end = 127;
  4158. for( ; start <= end; ++start )
  4159. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4160. return 0;
  4161. }
  4162. /// Copies data from one array to another.
  4163. /// ex: copyarray arr[0],arr[2],2;
  4164. ///
  4165. /// copyarray <destination array variable>,<source array variable>,<count>;
  4166. BUILDIN_FUNC(copyarray)
  4167. {
  4168. struct script_data* data1;
  4169. struct script_data* data2;
  4170. const char* name1;
  4171. const char* name2;
  4172. int32 idx1;
  4173. int32 idx2;
  4174. int32 id1;
  4175. int32 id2;
  4176. void* v;
  4177. int32 i;
  4178. int32 count;
  4179. TBL_PC* sd = NULL;
  4180. data1 = script_getdata(st, 2);
  4181. data2 = script_getdata(st, 3);
  4182. if( !data_isreference(data1) || !data_isreference(data2) )
  4183. {
  4184. ShowError("script:copyarray: not a variable\n");
  4185. script_reportdata(data1);
  4186. script_reportdata(data2);
  4187. st->state = END;
  4188. return 1;// not a variable
  4189. }
  4190. id1 = reference_getid(data1);
  4191. id2 = reference_getid(data2);
  4192. idx1 = reference_getindex(data1);
  4193. idx2 = reference_getindex(data2);
  4194. name1 = reference_getname(data1);
  4195. name2 = reference_getname(data2);
  4196. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4197. {
  4198. ShowError("script:copyarray: illegal scope\n");
  4199. script_reportdata(data1);
  4200. script_reportdata(data2);
  4201. st->state = END;
  4202. return 1;// not supported
  4203. }
  4204. if( is_string_variable(name1) != is_string_variable(name2) )
  4205. {
  4206. ShowError("script:copyarray: type mismatch\n");
  4207. script_reportdata(data1);
  4208. script_reportdata(data2);
  4209. st->state = END;
  4210. return 1;// data type mismatch
  4211. }
  4212. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4213. {
  4214. sd = script_rid2sd(st);
  4215. if( sd == NULL )
  4216. return 0;// no player attached
  4217. }
  4218. count = script_getnum(st, 4);
  4219. if( count > 128 - idx1 )
  4220. count = 128 - idx1;
  4221. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4222. return 0;// nothing to copy
  4223. if( id1 == id2 && idx1 > idx2 )
  4224. {// destination might be overlapping the source - copy in reverse order
  4225. for( i = count - 1; i >= 0; --i )
  4226. {
  4227. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4228. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4229. script_removetop(st, -1, 0);
  4230. }
  4231. }
  4232. else
  4233. {// normal copy
  4234. for( i = 0; i < count; ++i )
  4235. {
  4236. if( idx2 + i < 128 )
  4237. {
  4238. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4239. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4240. script_removetop(st, -1, 0);
  4241. }
  4242. else// out of range - assume ""/0
  4243. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4244. }
  4245. }
  4246. return 0;
  4247. }
  4248. /// Returns the size of the array.
  4249. /// Assumes that everything before the starting index exists.
  4250. /// ex: getarraysize(arr[3])
  4251. ///
  4252. /// getarraysize(<array variable>) -> <int>
  4253. BUILDIN_FUNC(getarraysize)
  4254. {
  4255. struct script_data* data;
  4256. const char* name;
  4257. data = script_getdata(st, 2);
  4258. if( !data_isreference(data) )
  4259. {
  4260. ShowError("script:getarraysize: not a variable\n");
  4261. script_reportdata(data);
  4262. script_pushnil(st);
  4263. st->state = END;
  4264. return 1;// not a variable
  4265. }
  4266. name = reference_getname(data);
  4267. if( not_array_variable(*name) )
  4268. {
  4269. ShowError("script:getarraysize: illegal scope\n");
  4270. script_reportdata(data);
  4271. script_pushnil(st);
  4272. st->state = END;
  4273. return 1;// not supported
  4274. }
  4275. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4276. return 0;
  4277. }
  4278. /// Deletes count or all the elements in an array, from the starting index.
  4279. /// ex: deletearray arr[4],2;
  4280. ///
  4281. /// deletearray <array variable>;
  4282. /// deletearray <array variable>,<count>;
  4283. BUILDIN_FUNC(deletearray)
  4284. {
  4285. struct script_data* data;
  4286. const char* name;
  4287. int start;
  4288. int end;
  4289. int id;
  4290. TBL_PC *sd = NULL;
  4291. data = script_getdata(st, 2);
  4292. if( !data_isreference(data) )
  4293. {
  4294. ShowError("script:deletearray: not a variable\n");
  4295. script_reportdata(data);
  4296. st->state = END;
  4297. return 1;// not a variable
  4298. }
  4299. id = reference_getid(data);
  4300. start = reference_getindex(data);
  4301. name = reference_getname(data);
  4302. if( not_array_variable(*name) )
  4303. {
  4304. ShowError("script:deletearray: illegal scope\n");
  4305. script_reportdata(data);
  4306. st->state = END;
  4307. return 1;// not supported
  4308. }
  4309. if( not_server_variable(*name) )
  4310. {
  4311. sd = script_rid2sd(st);
  4312. if( sd == NULL )
  4313. return 0;// no player attached
  4314. }
  4315. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4316. if( start >= end )
  4317. return 0;// nothing to free
  4318. if( script_hasdata(st,3) )
  4319. {
  4320. int count = script_getnum(st, 3);
  4321. if( count > end - start )
  4322. count = end - start;
  4323. if( count <= 0 )
  4324. return 0;// nothing to free
  4325. // move rest of the elements backward
  4326. for( ; start + count < end; ++start )
  4327. {
  4328. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4329. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4330. script_removetop(st, -1, 0);
  4331. }
  4332. }
  4333. // clear the rest of the array
  4334. if( is_string_variable(name) )
  4335. {
  4336. for( ; start < end; ++start )
  4337. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4338. }
  4339. else
  4340. {
  4341. for( ; start < end; ++start )
  4342. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4343. }
  4344. return 0;
  4345. }
  4346. /// Returns a reference to the target index of the array variable.
  4347. /// Equivalent to var[index].
  4348. ///
  4349. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4350. BUILDIN_FUNC(getelementofarray)
  4351. {
  4352. struct script_data* data;
  4353. const char* name;
  4354. int32 id;
  4355. int i;
  4356. data = script_getdata(st, 2);
  4357. if( !data_isreference(data) )
  4358. {
  4359. ShowError("script:getelementofarray: not a variable\n");
  4360. script_reportdata(data);
  4361. script_pushnil(st);
  4362. st->state = END;
  4363. return 1;// not a variable
  4364. }
  4365. id = reference_getid(data);
  4366. name = reference_getname(data);
  4367. if( not_array_variable(*name) )
  4368. {
  4369. ShowError("script:getelementofarray: illegal scope\n");
  4370. script_reportdata(data);
  4371. script_pushnil(st);
  4372. st->state = END;
  4373. return 1;// not supported
  4374. }
  4375. i = script_getnum(st, 3);
  4376. if( i < 0 || i >= 128 )
  4377. {
  4378. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4379. script_reportdata(data);
  4380. script_pushnil(st);
  4381. st->state = END;
  4382. return 1;// out of range
  4383. }
  4384. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4385. return 0;
  4386. }
  4387. /////////////////////////////////////////////////////////////////////
  4388. /// ...
  4389. ///
  4390. /*==========================================
  4391. *
  4392. *------------------------------------------*/
  4393. BUILDIN_FUNC(setlook)
  4394. {
  4395. int type,val;
  4396. TBL_PC* sd;
  4397. type=script_getnum(st,2);
  4398. val=script_getnum(st,3);
  4399. sd = script_rid2sd(st);
  4400. if( sd == NULL )
  4401. return 0;
  4402. pc_changelook(sd,type,val);
  4403. return 0;
  4404. }
  4405. BUILDIN_FUNC(changelook)
  4406. { // As setlook but only client side
  4407. int type,val;
  4408. TBL_PC* sd;
  4409. type=script_getnum(st,2);
  4410. val=script_getnum(st,3);
  4411. sd = script_rid2sd(st);
  4412. if( sd == NULL )
  4413. return 0;
  4414. clif_changelook(&sd->bl,type,val);
  4415. return 0;
  4416. }
  4417. /*==========================================
  4418. *
  4419. *------------------------------------------*/
  4420. BUILDIN_FUNC(cutin)
  4421. {
  4422. TBL_PC* sd;
  4423. sd = script_rid2sd(st);
  4424. if( sd == NULL )
  4425. return 0;
  4426. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4427. return 0;
  4428. }
  4429. /*==========================================
  4430. *
  4431. *------------------------------------------*/
  4432. BUILDIN_FUNC(viewpoint)
  4433. {
  4434. int type,x,y,id,color;
  4435. TBL_PC* sd;
  4436. type=script_getnum(st,2);
  4437. x=script_getnum(st,3);
  4438. y=script_getnum(st,4);
  4439. id=script_getnum(st,5);
  4440. color=script_getnum(st,6);
  4441. sd = script_rid2sd(st);
  4442. if( sd == NULL )
  4443. return 0;
  4444. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4445. return 0;
  4446. }
  4447. /*==========================================
  4448. *
  4449. *------------------------------------------*/
  4450. BUILDIN_FUNC(countitem)
  4451. {
  4452. int nameid, i;
  4453. int count = 0;
  4454. struct script_data* data;
  4455. TBL_PC* sd = script_rid2sd(st);
  4456. if (!sd) {
  4457. script_pushint(st,0);
  4458. return 0;
  4459. }
  4460. data = script_getdata(st,2);
  4461. get_val(st,data);
  4462. if( data_isstring(data) ) {
  4463. const char* name = conv_str(st,data);
  4464. struct item_data* item_data;
  4465. if((item_data = itemdb_searchname(name)) != NULL)
  4466. nameid = item_data->nameid;
  4467. else
  4468. nameid = 0;
  4469. } else
  4470. nameid = conv_num(st,data);
  4471. if (nameid < 500) {
  4472. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4473. script_reportsrc(st);
  4474. script_pushint(st,0);
  4475. return 1;
  4476. }
  4477. for(i = 0; i < MAX_INVENTORY; i++)
  4478. if(sd->status.inventory[i].nameid == nameid)
  4479. count += sd->status.inventory[i].amount;
  4480. script_pushint(st,count);
  4481. return 0;
  4482. }
  4483. /*==========================================
  4484. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4485. * returns number of items that meet the conditions
  4486. *------------------------------------------*/
  4487. BUILDIN_FUNC(countitem2)
  4488. {
  4489. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4490. int count = 0;
  4491. int i;
  4492. struct script_data* data;
  4493. TBL_PC* sd = script_rid2sd(st);
  4494. if (!sd) {
  4495. script_pushint(st,0);
  4496. return 0;
  4497. }
  4498. data = script_getdata(st,2);
  4499. get_val(st,data);
  4500. if( data_isstring(data) ) {
  4501. const char* name = conv_str(st,data);
  4502. struct item_data* item_data;
  4503. if((item_data = itemdb_searchname(name)) != NULL)
  4504. nameid = item_data->nameid;
  4505. else
  4506. nameid = 0;
  4507. } else
  4508. nameid = conv_num(st,data);
  4509. iden = script_getnum(st,3);
  4510. ref = script_getnum(st,4);
  4511. attr = script_getnum(st,5);
  4512. c1 = (short)script_getnum(st,6);
  4513. c2 = (short)script_getnum(st,7);
  4514. c3 = (short)script_getnum(st,8);
  4515. c4 = (short)script_getnum(st,9);
  4516. if (nameid < 500) {
  4517. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4518. script_pushint(st,0);
  4519. return 1;
  4520. }
  4521. for(i = 0; i < MAX_INVENTORY; i++)
  4522. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4523. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4524. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4525. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4526. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4527. sd->status.inventory[i].card[3] == c4
  4528. )
  4529. count += sd->status.inventory[i].amount;
  4530. script_pushint(st,count);
  4531. return 0;
  4532. }
  4533. /*==========================================
  4534. * 重量チェック
  4535. *------------------------------------------*/
  4536. BUILDIN_FUNC(checkweight)
  4537. {
  4538. int nameid=0,amount,i;
  4539. unsigned long weight;
  4540. TBL_PC *sd;
  4541. struct script_data *data;
  4542. sd = script_rid2sd(st);
  4543. if( sd == NULL )
  4544. return 0;
  4545. data=script_getdata(st,2);
  4546. get_val(st,data);
  4547. if( data_isstring(data) ){
  4548. const char *name=conv_str(st,data);
  4549. struct item_data *item_data = itemdb_searchname(name);
  4550. if( item_data )
  4551. nameid=item_data->nameid;
  4552. }else
  4553. nameid=conv_num(st,data);
  4554. amount=script_getnum(st,3);
  4555. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4556. script_pushint(st,0);
  4557. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4558. return 1;
  4559. }
  4560. weight = itemdb_weight(nameid)*amount;
  4561. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  4562. script_pushint(st,0);
  4563. } else {
  4564. //Check if the inventory ain't full.
  4565. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  4566. i = pc_search_inventory(sd,0);
  4567. if (i >= 0) //Empty slot available.
  4568. script_pushint(st,1);
  4569. else //Inventory full
  4570. script_pushint(st,0);
  4571. }
  4572. return 0;
  4573. }
  4574. /*==========================================
  4575. * getitem <item id>,<amount>{,<character ID>};
  4576. * getitem "<item name>",<amount>{,<character ID>};
  4577. *------------------------------------------*/
  4578. BUILDIN_FUNC(getitem)
  4579. {
  4580. int nameid,amount,get_count,i,flag = 0;
  4581. struct item it;
  4582. TBL_PC *sd;
  4583. struct script_data *data;
  4584. data=script_getdata(st,2);
  4585. get_val(st,data);
  4586. if( data_isstring(data) )
  4587. {// "<item name>"
  4588. const char *name=conv_str(st,data);
  4589. struct item_data *item_data = itemdb_searchname(name);
  4590. if( item_data == NULL ){
  4591. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4592. return 1; //No item created.
  4593. }
  4594. nameid=item_data->nameid;
  4595. } else if( data_isint(data) )
  4596. {// <item id>
  4597. nameid=conv_num(st,data);
  4598. //Violet Box, Blue Box, etc - random item pick
  4599. if( nameid < 0 ) {
  4600. nameid=itemdb_searchrandomid(-nameid);
  4601. flag = 1;
  4602. }
  4603. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4604. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4605. return 1; //No item created.
  4606. }
  4607. } else {
  4608. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4609. return 1;
  4610. }
  4611. // <amount>
  4612. if( (amount=script_getnum(st,3)) <= 0)
  4613. return 0; //return if amount <=0, skip the useles iteration
  4614. memset(&it,0,sizeof(it));
  4615. it.nameid=nameid;
  4616. if(!flag)
  4617. it.identify=1;
  4618. else
  4619. it.identify=itemdb_isidentified(nameid);
  4620. if( script_hasdata(st,4) )
  4621. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  4622. else
  4623. sd=script_rid2sd(st); // Attached player
  4624. if( sd == NULL ) // no target
  4625. return 0;
  4626. //Check if it's stackable.
  4627. if (!itemdb_isstackable(nameid))
  4628. get_count = 1;
  4629. else
  4630. get_count = amount;
  4631. for (i = 0; i < amount; i += get_count)
  4632. {
  4633. // if not pet egg
  4634. if (!pet_create_egg(sd, nameid))
  4635. {
  4636. if ((flag = pc_additem(sd, &it, get_count)))
  4637. {
  4638. clif_additem(sd, 0, 0, flag);
  4639. if( pc_candrop(sd,&it) )
  4640. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4641. }
  4642. }
  4643. }
  4644. //Logs items, got from (N)PC scripts [Lupus]
  4645. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4646. log_pick_pc(sd, "N", nameid, amount, NULL);
  4647. return 0;
  4648. }
  4649. /*==========================================
  4650. *
  4651. *------------------------------------------*/
  4652. BUILDIN_FUNC(getitem2)
  4653. {
  4654. int nameid,amount,get_count,i,flag = 0;
  4655. int iden,ref,attr,c1,c2,c3,c4;
  4656. struct item_data *item_data;
  4657. struct item item_tmp;
  4658. TBL_PC *sd;
  4659. struct script_data *data;
  4660. if( script_hasdata(st,11) )
  4661. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  4662. else
  4663. sd=script_rid2sd(st); // Attached player
  4664. if( sd == NULL ) // no target
  4665. return 0;
  4666. data=script_getdata(st,2);
  4667. get_val(st,data);
  4668. if( data_isstring(data) ){
  4669. const char *name=conv_str(st,data);
  4670. struct item_data *item_data = itemdb_searchname(name);
  4671. if( item_data )
  4672. nameid=item_data->nameid;
  4673. else
  4674. nameid=UNKNOWN_ITEM_ID;
  4675. }else
  4676. nameid=conv_num(st,data);
  4677. amount=script_getnum(st,3);
  4678. iden=script_getnum(st,4);
  4679. ref=script_getnum(st,5);
  4680. attr=script_getnum(st,6);
  4681. c1=script_getnum(st,7);
  4682. c2=script_getnum(st,8);
  4683. c3=script_getnum(st,9);
  4684. c4=script_getnum(st,10);
  4685. if(nameid<0) { // ランダム
  4686. nameid=itemdb_searchrandomid(-nameid);
  4687. flag = 1;
  4688. }
  4689. if(nameid > 0) {
  4690. memset(&item_tmp,0,sizeof(item_tmp));
  4691. item_data=itemdb_exists(nameid);
  4692. if (item_data == NULL)
  4693. return -1;
  4694. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4695. if(ref > 10) ref = 10;
  4696. }
  4697. else if(item_data->type==IT_PETEGG) {
  4698. iden = 1;
  4699. ref = 0;
  4700. }
  4701. else {
  4702. iden = 1;
  4703. ref = attr = 0;
  4704. }
  4705. item_tmp.nameid=nameid;
  4706. if(!flag)
  4707. item_tmp.identify=iden;
  4708. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4709. item_tmp.identify=0;
  4710. item_tmp.refine=ref;
  4711. item_tmp.attribute=attr;
  4712. item_tmp.card[0]=c1;
  4713. item_tmp.card[1]=c2;
  4714. item_tmp.card[2]=c3;
  4715. item_tmp.card[3]=c4;
  4716. //Check if it's stackable.
  4717. if (!itemdb_isstackable(nameid))
  4718. get_count = 1;
  4719. else
  4720. get_count = amount;
  4721. for (i = 0; i < amount; i += get_count)
  4722. {
  4723. // if not pet egg
  4724. if (!pet_create_egg(sd, nameid))
  4725. {
  4726. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  4727. {
  4728. clif_additem(sd, 0, 0, flag);
  4729. if( pc_candrop(sd,&item_tmp) )
  4730. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4731. }
  4732. }
  4733. }
  4734. //Logs items, got from (N)PC scripts [Lupus]
  4735. if(log_config.enable_logs&0x40)
  4736. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4737. }
  4738. return 0;
  4739. }
  4740. /*==========================================
  4741. * rentitem <item id>
  4742. * rentitem "<item name>"
  4743. *------------------------------------------*/
  4744. BUILDIN_FUNC(rentitem)
  4745. {
  4746. struct map_session_data *sd;
  4747. struct script_data *data;
  4748. struct item it;
  4749. int seconds;
  4750. int nameid = 0, flag;
  4751. data = script_getdata(st,2);
  4752. get_val(st,data);
  4753. if( (sd = script_rid2sd(st)) == NULL )
  4754. return 0;
  4755. if( data_isstring(data) )
  4756. {
  4757. const char *name = conv_str(st,data);
  4758. struct item_data *itd = itemdb_searchname(name);
  4759. if( itd == NULL )
  4760. {
  4761. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  4762. return 1;
  4763. }
  4764. nameid = itd->nameid;
  4765. }
  4766. else if( data_isint(data) )
  4767. {
  4768. nameid = conv_num(st,data);
  4769. if( nameid <= 0 || !itemdb_exists(nameid) )
  4770. {
  4771. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  4772. return 1;
  4773. }
  4774. }
  4775. else
  4776. {
  4777. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  4778. return 1;
  4779. }
  4780. if( itemdb_isstackable(nameid) )
  4781. {
  4782. ShowError("buildin_rentitem: invalid rental item %d requested.\n", nameid);
  4783. return 1;
  4784. }
  4785. seconds = script_getnum(st,3);
  4786. memset(&it, 0, sizeof(it));
  4787. it.nameid = nameid;
  4788. it.identify = 1;
  4789. it.expire_time = (unsigned int)(time(NULL) + seconds);
  4790. if( (flag = pc_additem(sd, &it, 1)) )
  4791. {
  4792. clif_additem(sd, 0, 0, flag);
  4793. return 1;
  4794. }
  4795. clif_rental_time(sd->fd, nameid, seconds);
  4796. pc_inventory_rental_add(sd, seconds);
  4797. if( log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS )
  4798. log_pick_pc(sd, "N", nameid, 1, NULL);
  4799. return 0;
  4800. }
  4801. /*==========================================
  4802. * gets an item with someone's name inscribed [Skotlex]
  4803. * getinscribeditem item_num, character_name
  4804. * Returned Qty is always 1, only works on equip-able
  4805. * equipment
  4806. *------------------------------------------*/
  4807. BUILDIN_FUNC(getnameditem)
  4808. {
  4809. int nameid;
  4810. struct item item_tmp;
  4811. TBL_PC *sd, *tsd;
  4812. struct script_data *data;
  4813. sd = script_rid2sd(st);
  4814. if (sd == NULL)
  4815. { //Player not attached!
  4816. script_pushint(st,0);
  4817. return 0;
  4818. }
  4819. data=script_getdata(st,2);
  4820. get_val(st,data);
  4821. if( data_isstring(data) ){
  4822. const char *name=conv_str(st,data);
  4823. struct item_data *item_data = itemdb_searchname(name);
  4824. if( item_data == NULL)
  4825. { //Failed
  4826. script_pushint(st,0);
  4827. return 0;
  4828. }
  4829. nameid = item_data->nameid;
  4830. }else
  4831. nameid = conv_num(st,data);
  4832. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  4833. { //Even though named stackable items "could" be risky, they are required for certain quests.
  4834. script_pushint(st,0);
  4835. return 0;
  4836. }
  4837. data=script_getdata(st,3);
  4838. get_val(st,data);
  4839. if( data_isstring(data) ) //Char Name
  4840. tsd=map_nick2sd(conv_str(st,data));
  4841. else //Char Id was given
  4842. tsd=map_charid2sd(conv_num(st,data));
  4843. if( tsd == NULL )
  4844. { //Failed
  4845. script_pushint(st,0);
  4846. return 0;
  4847. }
  4848. memset(&item_tmp,0,sizeof(item_tmp));
  4849. item_tmp.nameid=nameid;
  4850. item_tmp.amount=1;
  4851. item_tmp.identify=1;
  4852. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  4853. item_tmp.card[2]=tsd->status.char_id;
  4854. item_tmp.card[3]=tsd->status.char_id >> 16;
  4855. if(pc_additem(sd,&item_tmp,1)) {
  4856. script_pushint(st,0);
  4857. return 0; //Failed to add item, we will not drop if they don't fit
  4858. }
  4859. //Logs items, got from (N)PC scripts [Lupus]
  4860. if(log_config.enable_logs&0x40)
  4861. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  4862. script_pushint(st,1);
  4863. return 0;
  4864. }
  4865. /*==========================================
  4866. * gets a random item ID from an item group [Skotlex]
  4867. * groupranditem group_num
  4868. *------------------------------------------*/
  4869. BUILDIN_FUNC(grouprandomitem)
  4870. {
  4871. int group;
  4872. group = script_getnum(st,2);
  4873. script_pushint(st,itemdb_searchrandomid(group));
  4874. return 0;
  4875. }
  4876. /*==========================================
  4877. *
  4878. *------------------------------------------*/
  4879. BUILDIN_FUNC(makeitem)
  4880. {
  4881. int nameid,amount,flag = 0;
  4882. int x,y,m;
  4883. const char *mapname;
  4884. struct item item_tmp;
  4885. struct script_data *data;
  4886. data=script_getdata(st,2);
  4887. get_val(st,data);
  4888. if( data_isstring(data) ){
  4889. const char *name=conv_str(st,data);
  4890. struct item_data *item_data = itemdb_searchname(name);
  4891. if( item_data )
  4892. nameid=item_data->nameid;
  4893. else
  4894. nameid=UNKNOWN_ITEM_ID;
  4895. }else
  4896. nameid=conv_num(st,data);
  4897. amount=script_getnum(st,3);
  4898. mapname =script_getstr(st,4);
  4899. x =script_getnum(st,5);
  4900. y =script_getnum(st,6);
  4901. if(strcmp(mapname,"this")==0)
  4902. {
  4903. TBL_PC *sd;
  4904. sd = script_rid2sd(st);
  4905. if (!sd) return 0; //Failed...
  4906. m=sd->bl.m;
  4907. } else
  4908. m=map_mapname2mapid(mapname);
  4909. if(nameid<0) { // ランダム
  4910. nameid=itemdb_searchrandomid(-nameid);
  4911. flag = 1;
  4912. }
  4913. if(nameid > 0) {
  4914. memset(&item_tmp,0,sizeof(item_tmp));
  4915. item_tmp.nameid=nameid;
  4916. if(!flag)
  4917. item_tmp.identify=1;
  4918. else
  4919. item_tmp.identify=itemdb_isidentified(nameid);
  4920. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  4921. }
  4922. return 0;
  4923. }
  4924. /// Deletes items from the target/attached player.
  4925. /// Prioritizes ordinary items.
  4926. ///
  4927. /// delitem <item id>,<amount>{,<account id>}
  4928. /// delitem "<item name>",<amount>{,<account id>}
  4929. BUILDIN_FUNC(delitem)
  4930. {
  4931. int nameid=0,amount,i,important_item=0;
  4932. TBL_PC *sd;
  4933. struct script_data *data;
  4934. if( script_hasdata(st,4) )
  4935. {
  4936. int account_id = script_getnum(st,4);
  4937. sd = map_id2sd(account_id); // <account id>
  4938. if( sd == NULL )
  4939. {
  4940. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  4941. st->state = END;
  4942. return 1;
  4943. }
  4944. }
  4945. else
  4946. {
  4947. sd = script_rid2sd(st);// attached player
  4948. if( sd == NULL )
  4949. return 0;
  4950. }
  4951. data = script_getdata(st,2);
  4952. get_val(st,data);
  4953. if( data_isstring(data) )
  4954. {
  4955. const char* item_name = conv_str(st,data);
  4956. struct item_data* id = itemdb_searchname(item_name);
  4957. if( id == NULL )
  4958. {
  4959. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  4960. st->state = END;
  4961. return 1;
  4962. }
  4963. nameid = id->nameid;// "<item name>"
  4964. }
  4965. else
  4966. nameid = conv_num(st,data);// <item id>
  4967. amount=script_getnum(st,3);
  4968. if( amount <= 0 )
  4969. return 0;// nothing to do
  4970. //1st pass
  4971. //here we won't delete items with CARDS, named items but we count them
  4972. for(i=0;i<MAX_INVENTORY;i++){
  4973. //we don't delete wrong item or equipped item
  4974. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  4975. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  4976. continue;
  4977. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  4978. if(sd->inventory_data[i]->type==IT_PETEGG &&
  4979. sd->status.inventory[i].card[0] == CARD0_PET)
  4980. {
  4981. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  4982. continue; //pet couldn't be sent for deletion.
  4983. } else
  4984. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  4985. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  4986. sd->status.inventory[i].card[0] ||
  4987. sd->status.inventory[i].refine) {
  4988. //this is important item, count it (except for pet eggs)
  4989. if(sd->status.inventory[i].card[0] != CARD0_PET)
  4990. important_item++;
  4991. continue;
  4992. }
  4993. if(sd->status.inventory[i].amount>=amount){
  4994. //Logs items, got from (N)PC scripts [Lupus]
  4995. if(log_config.enable_logs&0x40)
  4996. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  4997. pc_delitem(sd,i,amount,0);
  4998. return 0; //we deleted exact amount of items. now exit
  4999. } else {
  5000. amount-=sd->status.inventory[i].amount;
  5001. //Logs items, got from (N)PC scripts [Lupus]
  5002. if(log_config.enable_logs&0x40) {
  5003. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5004. }
  5005. //Logs
  5006. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5007. }
  5008. }
  5009. //2nd pass
  5010. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5011. if (important_item>0 && amount>0)
  5012. for(i=0;i<MAX_INVENTORY;i++){
  5013. //we don't delete wrong item
  5014. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5015. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5016. continue;
  5017. if(sd->status.inventory[i].amount>=amount){
  5018. //Logs items, got from (N)PC scripts [Lupus]
  5019. if(log_config.enable_logs&0x40)
  5020. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5021. pc_delitem(sd,i,amount,0);
  5022. return 0; //we deleted exact amount of items. now exit
  5023. } else {
  5024. amount-=sd->status.inventory[i].amount;
  5025. //Logs items, got from (N)PC scripts [Lupus]
  5026. if(log_config.enable_logs&0x40)
  5027. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5028. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5029. }
  5030. }
  5031. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5032. st->state = END;
  5033. return 1;
  5034. }
  5035. /// Deletes items from the target/attached player.
  5036. ///
  5037. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5038. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5039. BUILDIN_FUNC(delitem2)
  5040. {
  5041. int nameid=0,amount,i=0;
  5042. int iden,ref,attr,c1,c2,c3,c4;
  5043. TBL_PC *sd;
  5044. struct script_data *data;
  5045. if( script_hasdata(st,11) )
  5046. {
  5047. int account_id = script_getnum(st,11);
  5048. sd = map_id2sd(account_id); // <account id>
  5049. if( sd == NULL )
  5050. {
  5051. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5052. st->state = END;
  5053. return 1;
  5054. }
  5055. }
  5056. else
  5057. {
  5058. sd = script_rid2sd(st);// attached player
  5059. if( sd == NULL )
  5060. return 0;
  5061. }
  5062. data = script_getdata(st,2);
  5063. get_val(st,data);
  5064. if( data_isstring(data) )
  5065. {
  5066. const char* item_name = conv_str(st,data);
  5067. struct item_data* id = itemdb_searchname(item_name);
  5068. if( id == NULL )
  5069. {
  5070. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5071. st->state = END;
  5072. return 1;
  5073. }
  5074. nameid = id->nameid;// "<item name>"
  5075. }
  5076. else
  5077. nameid = conv_num(st,data);// <item id>
  5078. amount=script_getnum(st,3);
  5079. iden=script_getnum(st,4);
  5080. ref=script_getnum(st,5);
  5081. attr=script_getnum(st,6);
  5082. c1=script_getnum(st,7);
  5083. c2=script_getnum(st,8);
  5084. c3=script_getnum(st,9);
  5085. c4=script_getnum(st,10);
  5086. if( amount <= 0 )
  5087. return 0;// nothing to do
  5088. for(i=0;i<MAX_INVENTORY;i++){
  5089. //we don't delete wrong item or equipped item
  5090. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5091. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5092. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5093. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5094. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5095. sd->status.inventory[i].card[3]!=c4)
  5096. continue;
  5097. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5098. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5099. {
  5100. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5101. continue; //Failed to send delete the pet.
  5102. }
  5103. if(sd->status.inventory[i].amount>=amount){
  5104. //Logs items, got from (N)PC scripts [Lupus]
  5105. if(log_config.enable_logs&0x40)
  5106. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5107. pc_delitem(sd,i,amount,0);
  5108. return 0; //we deleted exact amount of items. now exit
  5109. } else {
  5110. amount-=sd->status.inventory[i].amount;
  5111. //Logs items, got from (N)PC scripts [Lupus]
  5112. if(log_config.enable_logs&0x40)
  5113. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5114. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5115. }
  5116. }
  5117. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5118. st->state = END;
  5119. return 1;
  5120. }
  5121. /*==========================================
  5122. * Enables/Disables use of items while in an NPC [Skotlex]
  5123. *------------------------------------------*/
  5124. BUILDIN_FUNC(enableitemuse)
  5125. {
  5126. TBL_PC *sd;
  5127. sd=script_rid2sd(st);
  5128. if (sd)
  5129. sd->npc_item_flag = st->oid;
  5130. return 0;
  5131. }
  5132. BUILDIN_FUNC(disableitemuse)
  5133. {
  5134. TBL_PC *sd;
  5135. sd=script_rid2sd(st);
  5136. if (sd)
  5137. sd->npc_item_flag = 0;
  5138. return 0;
  5139. }
  5140. /*==========================================
  5141. *キャラ関係のパラメータ取得
  5142. *------------------------------------------*/
  5143. BUILDIN_FUNC(readparam)
  5144. {
  5145. int type;
  5146. TBL_PC *sd;
  5147. type=script_getnum(st,2);
  5148. if( script_hasdata(st,3) )
  5149. sd=map_nick2sd(script_getstr(st,3));
  5150. else
  5151. sd=script_rid2sd(st);
  5152. if(sd==NULL){
  5153. script_pushint(st,-1);
  5154. return 0;
  5155. }
  5156. script_pushint(st,pc_readparam(sd,type));
  5157. return 0;
  5158. }
  5159. /*==========================================
  5160. *キャラ関係のID取得
  5161. *------------------------------------------*/
  5162. BUILDIN_FUNC(getcharid)
  5163. {
  5164. int num;
  5165. TBL_PC *sd;
  5166. num = script_getnum(st,2);
  5167. if( script_hasdata(st,3) )
  5168. sd=map_nick2sd(script_getstr(st,3));
  5169. else
  5170. sd=script_rid2sd(st);
  5171. if(sd==NULL){
  5172. script_pushint(st,0); //return 0, according docs
  5173. return 0;
  5174. }
  5175. switch( num ) {
  5176. case 0: script_pushint(st,sd->status.char_id); break;
  5177. case 1: script_pushint(st,sd->status.party_id); break;
  5178. case 2: script_pushint(st,sd->status.guild_id); break;
  5179. case 3: script_pushint(st,sd->status.account_id); break;
  5180. default:
  5181. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5182. script_pushint(st,0);
  5183. break;
  5184. }
  5185. return 0;
  5186. }
  5187. /*==========================================
  5188. *指定IDのPT名取得
  5189. *------------------------------------------*/
  5190. char *buildin_getpartyname_sub(int party_id)
  5191. {
  5192. struct party_data *p;
  5193. p=party_search(party_id);
  5194. if(p!=NULL){
  5195. char *buf;
  5196. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5197. memcpy(buf, p->party.name, NAME_LENGTH);
  5198. buf[NAME_LENGTH-1] = '\0';
  5199. return buf;
  5200. }
  5201. return 0;
  5202. }
  5203. BUILDIN_FUNC(getpartyname)
  5204. {
  5205. char *name;
  5206. int party_id;
  5207. party_id=script_getnum(st,2);
  5208. name=buildin_getpartyname_sub(party_id);
  5209. if(name != NULL)
  5210. script_pushstr(st,name);
  5211. else
  5212. script_pushconststr(st,"null");
  5213. return 0;
  5214. }
  5215. /*==========================================
  5216. *指定IDのPT人数とメンバーID取得
  5217. *------------------------------------------*/
  5218. BUILDIN_FUNC(getpartymember)
  5219. {
  5220. struct party_data *p;
  5221. int i,j=0,type=0;
  5222. p=party_search(script_getnum(st,2));
  5223. if( script_hasdata(st,3) )
  5224. type=script_getnum(st,3);
  5225. if(p!=NULL){
  5226. for(i=0;i<MAX_PARTY;i++){
  5227. if(p->party.member[i].account_id){
  5228. switch (type) {
  5229. case 2:
  5230. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5231. break;
  5232. case 1:
  5233. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5234. break;
  5235. default:
  5236. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5237. }
  5238. j++;
  5239. }
  5240. }
  5241. }
  5242. mapreg_setreg(add_str("$@partymembercount"),j);
  5243. return 0;
  5244. }
  5245. /*==========================================
  5246. * Retrieves party leader. if flag is specified,
  5247. * return some of the leader data. Otherwise, return name.
  5248. *------------------------------------------*/
  5249. BUILDIN_FUNC(getpartyleader)
  5250. {
  5251. int party_id, type = 0, i=0;
  5252. struct party_data *p;
  5253. party_id=script_getnum(st,2);
  5254. if( script_hasdata(st,3) )
  5255. type=script_getnum(st,3);
  5256. p=party_search(party_id);
  5257. if (p) //Search leader
  5258. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5259. if (!p || i == MAX_PARTY) { //leader not found
  5260. if (type)
  5261. script_pushint(st,-1);
  5262. else
  5263. script_pushconststr(st,"null");
  5264. return 0;
  5265. }
  5266. switch (type) {
  5267. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5268. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5269. case 3: script_pushint(st,p->party.member[i].class_); break;
  5270. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5271. case 5: script_pushint(st,p->party.member[i].lv); break;
  5272. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5273. }
  5274. return 0;
  5275. }
  5276. /*==========================================
  5277. *指定IDのギルド名取得
  5278. *------------------------------------------*/
  5279. char *buildin_getguildname_sub(int guild_id)
  5280. {
  5281. struct guild *g=NULL;
  5282. g=guild_search(guild_id);
  5283. if(g!=NULL){
  5284. char *buf;
  5285. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5286. memcpy(buf, g->name, NAME_LENGTH);
  5287. buf[NAME_LENGTH-1] = '\0';
  5288. return buf;
  5289. }
  5290. return NULL;
  5291. }
  5292. BUILDIN_FUNC(getguildname)
  5293. {
  5294. char *name;
  5295. int guild_id=script_getnum(st,2);
  5296. name=buildin_getguildname_sub(guild_id);
  5297. if(name != NULL)
  5298. script_pushstr(st,name);
  5299. else
  5300. script_pushconststr(st,"null");
  5301. return 0;
  5302. }
  5303. /*==========================================
  5304. *指定IDのGuildMaster名取得
  5305. *------------------------------------------*/
  5306. char *buildin_getguildmaster_sub(int guild_id)
  5307. {
  5308. struct guild *g=NULL;
  5309. g=guild_search(guild_id);
  5310. if(g!=NULL){
  5311. char *buf;
  5312. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5313. memcpy(buf, g->master, NAME_LENGTH);
  5314. buf[NAME_LENGTH-1] = '\0';
  5315. return buf;
  5316. }
  5317. return 0;
  5318. }
  5319. BUILDIN_FUNC(getguildmaster)
  5320. {
  5321. char *master;
  5322. int guild_id=script_getnum(st,2);
  5323. master=buildin_getguildmaster_sub(guild_id);
  5324. if(master!=0)
  5325. script_pushstr(st,master);
  5326. else
  5327. script_pushconststr(st,"null");
  5328. return 0;
  5329. }
  5330. BUILDIN_FUNC(getguildmasterid)
  5331. {
  5332. char *master;
  5333. TBL_PC *sd=NULL;
  5334. int guild_id=script_getnum(st,2);
  5335. master=buildin_getguildmaster_sub(guild_id);
  5336. if(master!=0){
  5337. if((sd=map_nick2sd(master)) == NULL){
  5338. script_pushint(st,0);
  5339. return 0;
  5340. }
  5341. script_pushint(st,sd->status.char_id);
  5342. }else{
  5343. script_pushint(st,0);
  5344. }
  5345. return 0;
  5346. }
  5347. /*==========================================
  5348. * キャラクタの名前
  5349. *------------------------------------------*/
  5350. BUILDIN_FUNC(strcharinfo)
  5351. {
  5352. TBL_PC *sd;
  5353. int num;
  5354. char *buf;
  5355. sd=script_rid2sd(st);
  5356. if (!sd) { //Avoid crashing....
  5357. script_pushconststr(st,"");
  5358. return 0;
  5359. }
  5360. num=script_getnum(st,2);
  5361. switch(num){
  5362. case 0:
  5363. script_pushstrcopy(st,sd->status.name);
  5364. break;
  5365. case 1:
  5366. buf=buildin_getpartyname_sub(sd->status.party_id);
  5367. if(buf!=0)
  5368. script_pushstr(st,buf);
  5369. else
  5370. script_pushconststr(st,"");
  5371. break;
  5372. case 2:
  5373. buf=buildin_getguildname_sub(sd->status.guild_id);
  5374. if(buf != NULL)
  5375. script_pushstr(st,buf);
  5376. else
  5377. script_pushconststr(st,"");
  5378. break;
  5379. default:
  5380. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5381. script_pushconststr(st,"");
  5382. break;
  5383. }
  5384. return 0;
  5385. }
  5386. /*==========================================
  5387. * 呼び出し元のNPC情報を取得する
  5388. *------------------------------------------*/
  5389. BUILDIN_FUNC(strnpcinfo)
  5390. {
  5391. TBL_NPC* nd;
  5392. int num;
  5393. char *buf,*name=NULL;
  5394. nd = map_id2nd(st->oid);
  5395. if (!nd) {
  5396. script_pushconststr(st, "");
  5397. return 0;
  5398. }
  5399. num = script_getnum(st,2);
  5400. switch(num){
  5401. case 0: // display name
  5402. name = aStrdup(nd->name);
  5403. break;
  5404. case 1: // visible part of display name name
  5405. if((buf = strchr(nd->name,'#')) != NULL)
  5406. {
  5407. name = aStrdup(nd->name);
  5408. name[buf - nd->name] = 0;
  5409. }
  5410. break;
  5411. case 2: // # fragment
  5412. if((buf = strchr(nd->name,'#')) != NULL)
  5413. name = aStrdup(buf+1);
  5414. break;
  5415. case 3: // unique name
  5416. name = aStrdup(nd->exname);
  5417. break;
  5418. }
  5419. if(name)
  5420. script_pushstr(st, name);
  5421. else
  5422. script_pushconststr(st, "");
  5423. return 0;
  5424. }
  5425. // aegis->athena slot position conversion table
  5426. static unsigned int equip[] = {EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_GARMENT,EQP_SHOES,EQP_ACC_L,EQP_ACC_R,EQP_HEAD_MID,EQP_HEAD_LOW};
  5427. /*==========================================
  5428. * GetEquipID(Pos); Pos: 1-10
  5429. *------------------------------------------*/
  5430. BUILDIN_FUNC(getequipid)
  5431. {
  5432. int i, num;
  5433. TBL_PC* sd;
  5434. struct item_data* item;
  5435. sd = script_rid2sd(st);
  5436. if( sd == NULL )
  5437. return 0;
  5438. num = script_getnum(st,2) - 1;
  5439. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5440. {
  5441. script_pushint(st,-1);
  5442. return 0;
  5443. }
  5444. // get inventory position of item
  5445. i = pc_checkequip(sd,equip[num]);
  5446. if( i < 0 )
  5447. {
  5448. script_pushint(st,-1);
  5449. return 0;
  5450. }
  5451. item = sd->inventory_data[i];
  5452. if( item != 0 )
  5453. script_pushint(st,item->nameid);
  5454. else
  5455. script_pushint(st,0);
  5456. return 0;
  5457. }
  5458. /*==========================================
  5459. * 装備名文字列(精錬メニュー用)
  5460. *------------------------------------------*/
  5461. BUILDIN_FUNC(getequipname)
  5462. {
  5463. int i, num;
  5464. TBL_PC* sd;
  5465. struct item_data* item;
  5466. sd = script_rid2sd(st);
  5467. if( sd == NULL )
  5468. return 0;
  5469. num = script_getnum(st,2) - 1;
  5470. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5471. {
  5472. script_pushconststr(st,"");
  5473. return 0;
  5474. }
  5475. // get inventory position of item
  5476. i = pc_checkequip(sd,equip[num]);
  5477. if( i < 0 )
  5478. {
  5479. script_pushint(st,-1);
  5480. return 0;
  5481. }
  5482. item = sd->inventory_data[i];
  5483. if( item != 0 )
  5484. script_pushstrcopy(st,item->jname);
  5485. else
  5486. script_pushconststr(st,"");
  5487. return 0;
  5488. }
  5489. /*==========================================
  5490. * getbrokenid [Valaris]
  5491. *------------------------------------------*/
  5492. BUILDIN_FUNC(getbrokenid)
  5493. {
  5494. int i,num,id=0,brokencounter=0;
  5495. TBL_PC *sd;
  5496. sd = script_rid2sd(st);
  5497. if( sd == NULL )
  5498. return 0;
  5499. num=script_getnum(st,2);
  5500. for(i=0; i<MAX_INVENTORY; i++) {
  5501. if(sd->status.inventory[i].attribute==1){
  5502. brokencounter++;
  5503. if(num==brokencounter){
  5504. id=sd->status.inventory[i].nameid;
  5505. break;
  5506. }
  5507. }
  5508. }
  5509. script_pushint(st,id);
  5510. return 0;
  5511. }
  5512. /*==========================================
  5513. * repair [Valaris]
  5514. *------------------------------------------*/
  5515. BUILDIN_FUNC(repair)
  5516. {
  5517. int i,num;
  5518. int repaircounter=0;
  5519. TBL_PC *sd;
  5520. sd = script_rid2sd(st);
  5521. if( sd == NULL )
  5522. return 0;
  5523. num=script_getnum(st,2);
  5524. for(i=0; i<MAX_INVENTORY; i++) {
  5525. if(sd->status.inventory[i].attribute==1){
  5526. repaircounter++;
  5527. if(num==repaircounter){
  5528. sd->status.inventory[i].attribute=0;
  5529. clif_equiplist(sd);
  5530. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5531. clif_misceffect(&sd->bl, 3);
  5532. break;
  5533. }
  5534. }
  5535. }
  5536. return 0;
  5537. }
  5538. /*==========================================
  5539. * 装備チェック
  5540. *------------------------------------------*/
  5541. BUILDIN_FUNC(getequipisequiped)
  5542. {
  5543. int i=-1,num;
  5544. TBL_PC *sd;
  5545. num=script_getnum(st,2);
  5546. sd = script_rid2sd(st);
  5547. if( sd == NULL )
  5548. return 0;
  5549. if (num > 0 && num <= ARRAYLENGTH(equip))
  5550. i=pc_checkequip(sd,equip[num-1]);
  5551. if(i >= 0)
  5552. script_pushint(st,1);
  5553. else
  5554. script_pushint(st,0);
  5555. return 0;
  5556. }
  5557. /*==========================================
  5558. * 装備品精錬可能チェック
  5559. *------------------------------------------*/
  5560. BUILDIN_FUNC(getequipisenableref)
  5561. {
  5562. int i=-1,num;
  5563. TBL_PC *sd;
  5564. num=script_getnum(st,2);
  5565. sd = script_rid2sd(st);
  5566. if( sd == NULL )
  5567. return 0;
  5568. if (num > 0 && num <= ARRAYLENGTH(equip))
  5569. i=pc_checkequip(sd,equip[num-1]);
  5570. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5571. {
  5572. script_pushint(st,1);
  5573. } else {
  5574. script_pushint(st,0);
  5575. }
  5576. return 0;
  5577. }
  5578. /*==========================================
  5579. * 装備品鑑定チェック
  5580. *------------------------------------------*/
  5581. BUILDIN_FUNC(getequipisidentify)
  5582. {
  5583. int i=-1,num;
  5584. TBL_PC *sd;
  5585. num=script_getnum(st,2);
  5586. sd = script_rid2sd(st);
  5587. if( sd == NULL )
  5588. return 0;
  5589. if (num > 0 && num <= ARRAYLENGTH(equip))
  5590. i=pc_checkequip(sd,equip[num-1]);
  5591. if(i >= 0)
  5592. script_pushint(st,sd->status.inventory[i].identify);
  5593. else
  5594. script_pushint(st,0);
  5595. return 0;
  5596. }
  5597. /*==========================================
  5598. * 装備品精錬度
  5599. *------------------------------------------*/
  5600. BUILDIN_FUNC(getequiprefinerycnt)
  5601. {
  5602. int i=-1,num;
  5603. TBL_PC *sd;
  5604. num=script_getnum(st,2);
  5605. sd = script_rid2sd(st);
  5606. if( sd == NULL )
  5607. return 0;
  5608. if (num > 0 && num <= ARRAYLENGTH(equip))
  5609. i=pc_checkequip(sd,equip[num-1]);
  5610. if(i >= 0)
  5611. script_pushint(st,sd->status.inventory[i].refine);
  5612. else
  5613. script_pushint(st,0);
  5614. return 0;
  5615. }
  5616. /*==========================================
  5617. * 装備品武器LV
  5618. *------------------------------------------*/
  5619. BUILDIN_FUNC(getequipweaponlv)
  5620. {
  5621. int i=-1,num;
  5622. TBL_PC *sd;
  5623. num=script_getnum(st,2);
  5624. sd = script_rid2sd(st);
  5625. if( sd == NULL )
  5626. return 0;
  5627. if (num > 0 && num <= ARRAYLENGTH(equip))
  5628. i=pc_checkequip(sd,equip[num-1]);
  5629. if(i >= 0 && sd->inventory_data[i])
  5630. script_pushint(st,sd->inventory_data[i]->wlv);
  5631. else
  5632. script_pushint(st,0);
  5633. return 0;
  5634. }
  5635. /*==========================================
  5636. * 装備品精錬成功率
  5637. *------------------------------------------*/
  5638. BUILDIN_FUNC(getequippercentrefinery)
  5639. {
  5640. int i=-1,num;
  5641. TBL_PC *sd;
  5642. num=script_getnum(st,2);
  5643. sd = script_rid2sd(st);
  5644. if( sd == NULL )
  5645. return 0;
  5646. if (num > 0 && num <= ARRAYLENGTH(equip))
  5647. i=pc_checkequip(sd,equip[num-1]);
  5648. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5649. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5650. else
  5651. script_pushint(st,0);
  5652. return 0;
  5653. }
  5654. /*==========================================
  5655. * 精錬成功
  5656. *------------------------------------------*/
  5657. BUILDIN_FUNC(successrefitem)
  5658. {
  5659. int i=-1,num,ep;
  5660. TBL_PC *sd;
  5661. num=script_getnum(st,2);
  5662. sd = script_rid2sd(st);
  5663. if( sd == NULL )
  5664. return 0;
  5665. if (num > 0 && num <= ARRAYLENGTH(equip))
  5666. i=pc_checkequip(sd,equip[num-1]);
  5667. if(i >= 0) {
  5668. ep=sd->status.inventory[i].equip;
  5669. //Logs items, got from (N)PC scripts [Lupus]
  5670. if(log_config.enable_logs&0x40)
  5671. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5672. sd->status.inventory[i].refine++;
  5673. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  5674. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5675. clif_delitem(sd,i,1);
  5676. //Logs items, got from (N)PC scripts [Lupus]
  5677. if(log_config.enable_logs&0x40)
  5678. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5679. clif_additem(sd,i,1,0);
  5680. pc_equipitem(sd,i,ep);
  5681. clif_misceffect(&sd->bl,3);
  5682. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5683. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5684. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5685. ){ // Fame point system [DracoRPG]
  5686. switch (sd->inventory_data[i]->wlv){
  5687. case 1:
  5688. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5689. break;
  5690. case 2:
  5691. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5692. break;
  5693. case 3:
  5694. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5695. break;
  5696. }
  5697. }
  5698. }
  5699. return 0;
  5700. }
  5701. /*==========================================
  5702. * 精錬失敗
  5703. *------------------------------------------*/
  5704. BUILDIN_FUNC(failedrefitem)
  5705. {
  5706. int i=-1,num;
  5707. TBL_PC *sd;
  5708. num=script_getnum(st,2);
  5709. sd = script_rid2sd(st);
  5710. if( sd == NULL )
  5711. return 0;
  5712. if (num > 0 && num <= ARRAYLENGTH(equip))
  5713. i=pc_checkequip(sd,equip[num-1]);
  5714. if(i >= 0) {
  5715. //Logs items, got from (N)PC scripts [Lupus]
  5716. if(log_config.enable_logs&0x40)
  5717. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5718. sd->status.inventory[i].refine = 0;
  5719. pc_unequipitem(sd,i,3);
  5720. // 精錬失敗エフェクトのパケット
  5721. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5722. pc_delitem(sd,i,1,0);
  5723. // 他の人にも失敗を通知
  5724. clif_misceffect(&sd->bl,2);
  5725. }
  5726. return 0;
  5727. }
  5728. /*==========================================
  5729. *
  5730. *------------------------------------------*/
  5731. BUILDIN_FUNC(statusup)
  5732. {
  5733. int type;
  5734. TBL_PC *sd;
  5735. type=script_getnum(st,2);
  5736. sd = script_rid2sd(st);
  5737. if( sd == NULL )
  5738. return 0;
  5739. pc_statusup(sd,type);
  5740. return 0;
  5741. }
  5742. /*==========================================
  5743. *
  5744. *------------------------------------------*/
  5745. BUILDIN_FUNC(statusup2)
  5746. {
  5747. int type,val;
  5748. TBL_PC *sd;
  5749. type=script_getnum(st,2);
  5750. val=script_getnum(st,3);
  5751. sd = script_rid2sd(st);
  5752. if( sd == NULL )
  5753. return 0;
  5754. pc_statusup2(sd,type,val);
  5755. return 0;
  5756. }
  5757. /// See 'doc/item_bonus.txt'
  5758. ///
  5759. /// bonus <bonus type>,<val1>;
  5760. /// bonus2 <bonus type>,<val1>,<val2>;
  5761. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5762. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5763. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5764. BUILDIN_FUNC(bonus)
  5765. {
  5766. int type;
  5767. int val1;
  5768. int val2 = 0;
  5769. int val3 = 0;
  5770. int val4 = 0;
  5771. int val5 = 0;
  5772. TBL_PC* sd;
  5773. sd = script_rid2sd(st);
  5774. if( sd == NULL )
  5775. return 0; // no player attached
  5776. type = script_getnum(st,2);
  5777. switch( type )
  5778. {
  5779. case SP_AUTOSPELL:
  5780. case SP_AUTOSPELL_WHENHIT:
  5781. case SP_SKILL_ATK:
  5782. case SP_SKILL_HEAL:
  5783. case SP_ADD_SKILL_BLOW:
  5784. case SP_CASTRATE:
  5785. // these bonuses support skill names
  5786. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  5787. break;
  5788. default:
  5789. val1 = script_getnum(st,3);
  5790. break;
  5791. }
  5792. switch( script_lastdata(st)-2 )
  5793. {
  5794. case 1:
  5795. pc_bonus(sd, type, val1);
  5796. break;
  5797. case 2:
  5798. val2 = script_getnum(st,4);
  5799. pc_bonus2(sd, type, val1, val2);
  5800. break;
  5801. case 3:
  5802. val2 = script_getnum(st,4);
  5803. val3 = script_getnum(st,5);
  5804. pc_bonus3(sd, type, val1, val2, val3);
  5805. break;
  5806. case 4:
  5807. val2 = script_getnum(st,4);
  5808. val3 = script_getnum(st,5);
  5809. val4 = script_getnum(st,6);
  5810. pc_bonus4(sd, type, val1, val2, val3, val4);
  5811. break;
  5812. case 5:
  5813. val2 = script_getnum(st,4);
  5814. val3 = script_getnum(st,5);
  5815. val4 = script_getnum(st,6);
  5816. val5 = script_getnum(st,7);
  5817. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  5818. break;
  5819. default:
  5820. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  5821. break;
  5822. }
  5823. return 0;
  5824. }
  5825. /// Bonus script that has a chance of being executed on attack.
  5826. BUILDIN_FUNC(bonusautoscript)
  5827. {
  5828. int rate, flag = 0;
  5829. const char *str;
  5830. struct script_code *script;
  5831. TBL_PC* sd;
  5832. sd = script_rid2sd(st);
  5833. if( sd == NULL )
  5834. return 0;// no player attached, report source
  5835. str = script_getstr(st,2);
  5836. rate = script_getnum(st,3);
  5837. if( script_hasdata(st,4) )
  5838. flag = script_getnum(st,4);
  5839. script = parse_script(str, "autoscript bonus", 0, 0);
  5840. if (!script)
  5841. return 1;
  5842. if (!pc_autoscript_add(sd->autoscript, ARRAYLENGTH(sd->autoscript), rate, flag, script))
  5843. {
  5844. script_free_code(script);
  5845. return 1;
  5846. }
  5847. return 0;
  5848. }
  5849. /// Bonus script that has a chance of being executed when attacked.
  5850. BUILDIN_FUNC(bonusautoscript2)
  5851. {
  5852. int rate, flag = 0;
  5853. const char *str;
  5854. struct script_code *script;
  5855. TBL_PC* sd;
  5856. sd = script_rid2sd(st);
  5857. if( sd == NULL )
  5858. return 0;// no player attached, report source
  5859. str = script_getstr(st,2);
  5860. rate = script_getnum(st,3);
  5861. if( script_hasdata(st,4) )
  5862. flag = script_getnum(st,4);
  5863. script = parse_script(str, "autoscript2 bonus", 0, 0);
  5864. if (!script)
  5865. return 1;
  5866. if (!pc_autoscript_add(sd->autoscript2, ARRAYLENGTH(sd->autoscript2), rate, flag, script))
  5867. {
  5868. script_free_code(script);
  5869. return 1;
  5870. }
  5871. return 0;
  5872. }
  5873. /// Changes the level of a player skill.
  5874. /// <flag> defaults to 1
  5875. /// <flag>=0 : set the level of the skill
  5876. /// <flag>=1 : set the temporary level of the skill
  5877. /// <flag>=2 : add to the level of the skill
  5878. ///
  5879. /// skill <skill id>,<level>,<flag>
  5880. /// skill <skill id>,<level>
  5881. /// skill "<skill name>",<level>,<flag>
  5882. /// skill "<skill name>",<level>
  5883. BUILDIN_FUNC(skill)
  5884. {
  5885. int id;
  5886. int level;
  5887. int flag = 1;
  5888. TBL_PC* sd;
  5889. sd = script_rid2sd(st);
  5890. if( sd == NULL )
  5891. return 0;// no player attached, report source
  5892. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5893. level = script_getnum(st,3);
  5894. if( script_hasdata(st,4) )
  5895. flag = script_getnum(st,4);
  5896. pc_skill(sd, id, level, flag);
  5897. return 0;
  5898. }
  5899. /// Changes the level of a player skill.
  5900. /// like skill, but <flag> defaults to 2
  5901. ///
  5902. /// addtoskill <skill id>,<amount>,<flag>
  5903. /// addtoskill <skill id>,<amount>
  5904. /// addtoskill "<skill name>",<amount>,<flag>
  5905. /// addtoskill "<skill name>",<amount>
  5906. ///
  5907. /// @see skill
  5908. BUILDIN_FUNC(addtoskill)
  5909. {
  5910. int id;
  5911. int level;
  5912. int flag = 2;
  5913. TBL_PC* sd;
  5914. sd = script_rid2sd(st);
  5915. if( sd == NULL )
  5916. return 0;// no player attached, report source
  5917. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5918. level = script_getnum(st,3);
  5919. if( script_hasdata(st,4) )
  5920. flag = script_getnum(st,4);
  5921. pc_skill(sd, id, level, flag);
  5922. return 0;
  5923. }
  5924. /// Increases the level of a guild skill.
  5925. ///
  5926. /// guildskill <skill id>,<amount>;
  5927. /// guildskill "<skill name>",<amount>;
  5928. BUILDIN_FUNC(guildskill)
  5929. {
  5930. int id;
  5931. int level;
  5932. TBL_PC* sd;
  5933. int i;
  5934. sd = script_rid2sd(st);
  5935. if( sd == NULL )
  5936. return 0;// no player attached, report source
  5937. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5938. level = script_getnum(st,3);
  5939. for( i=0; i < level; i++ )
  5940. guild_skillup(sd, id);
  5941. return 0;
  5942. }
  5943. /// Returns the level of the player skill.
  5944. ///
  5945. /// getskilllv(<skill id>) -> <level>
  5946. /// getskilllv("<skill name>") -> <level>
  5947. BUILDIN_FUNC(getskilllv)
  5948. {
  5949. int id;
  5950. TBL_PC* sd;
  5951. sd = script_rid2sd(st);
  5952. if( sd == NULL )
  5953. return 0;// no player attached, report source
  5954. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5955. script_pushint(st, pc_checkskill(sd,id));
  5956. return 0;
  5957. }
  5958. /// Returns the level of the guild skill.
  5959. ///
  5960. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  5961. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  5962. BUILDIN_FUNC(getgdskilllv)
  5963. {
  5964. int guild_id;
  5965. int skill_id;
  5966. struct guild* g;
  5967. guild_id = script_getnum(st,2);
  5968. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  5969. g = guild_search(guild_id);
  5970. if( g == NULL )
  5971. script_pushint(st, -1);
  5972. else
  5973. script_pushint(st, guild_checkskill(g,skill_id));
  5974. return 0;
  5975. }
  5976. /// Returns the 'basic_skill_check' setting.
  5977. /// This config determines if the server checks the skill level of NV_BASIC
  5978. /// before allowing the basic actions.
  5979. ///
  5980. /// basicskillcheck() -> <bool>
  5981. BUILDIN_FUNC(basicskillcheck)
  5982. {
  5983. script_pushint(st, battle_config.basic_skill_check);
  5984. return 0;
  5985. }
  5986. /// Returns the GM level of the player.
  5987. ///
  5988. /// getgmlevel() -> <level>
  5989. BUILDIN_FUNC(getgmlevel)
  5990. {
  5991. TBL_PC* sd;
  5992. sd = script_rid2sd(st);
  5993. if( sd == NULL )
  5994. return 0;// no player attached, report source
  5995. script_pushint(st, pc_isGM(sd));
  5996. return 0;
  5997. }
  5998. /// Terminates the execution of this script instance.
  5999. ///
  6000. /// end
  6001. BUILDIN_FUNC(end)
  6002. {
  6003. st->state = END;
  6004. return 0;
  6005. }
  6006. /// Checks if the player has that effect state (option).
  6007. ///
  6008. /// checkoption(<option>) -> <bool>
  6009. BUILDIN_FUNC(checkoption)
  6010. {
  6011. int option;
  6012. TBL_PC* sd;
  6013. sd = script_rid2sd(st);
  6014. if( sd == NULL )
  6015. return 0;// no player attached, report source
  6016. option = script_getnum(st,2);
  6017. if( sd->sc.option&option )
  6018. script_pushint(st, 1);
  6019. else
  6020. script_pushint(st, 0);
  6021. return 0;
  6022. }
  6023. /// Checks if the player is in that body state (opt1).
  6024. ///
  6025. /// checkoption1(<opt1>) -> <bool>
  6026. BUILDIN_FUNC(checkoption1)
  6027. {
  6028. int opt1;
  6029. TBL_PC* sd;
  6030. sd = script_rid2sd(st);
  6031. if( sd == NULL )
  6032. return 0;// no player attached, report source
  6033. opt1 = script_getnum(st,2);
  6034. if( sd->sc.opt1 == opt1 )
  6035. script_pushint(st, 1);
  6036. else
  6037. script_pushint(st, 0);
  6038. return 0;
  6039. }
  6040. /// Checks if the player has that health state (opt2).
  6041. ///
  6042. /// checkoption2(<opt2>) -> <bool>
  6043. BUILDIN_FUNC(checkoption2)
  6044. {
  6045. int opt2;
  6046. TBL_PC* sd;
  6047. sd = script_rid2sd(st);
  6048. if( sd == NULL )
  6049. return 0;// no player attached, report source
  6050. opt2 = script_getnum(st,2);
  6051. if( sd->sc.opt2&opt2 )
  6052. script_pushint(st, 1);
  6053. else
  6054. script_pushint(st, 0);
  6055. return 0;
  6056. }
  6057. /// Changes the effect state (option) of the player.
  6058. /// <flag> defaults to 1
  6059. /// <flag>=0 : removes the option
  6060. /// <flag>=other : adds the option
  6061. ///
  6062. /// setoption <option>,<flag>;
  6063. /// setoption <option>;
  6064. BUILDIN_FUNC(setoption)
  6065. {
  6066. int option;
  6067. int flag = 1;
  6068. TBL_PC* sd;
  6069. sd = script_rid2sd(st);
  6070. if( sd == NULL )
  6071. return 0;// no player attached, report source
  6072. option = script_getnum(st,2);
  6073. if( script_hasdata(st,3) )
  6074. flag = script_getnum(st,3);
  6075. else if( !option ){// Request to remove everything.
  6076. flag = 0;
  6077. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6078. }
  6079. if( flag ){// Add option
  6080. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6081. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6082. pc_setoption(sd, sd->sc.option|option);
  6083. } else// Remove option
  6084. pc_setoption(sd, sd->sc.option&~option);
  6085. return 0;
  6086. }
  6087. /// Returns if the player has a cart.
  6088. ///
  6089. /// checkcart() -> <bool>
  6090. ///
  6091. /// @author Valaris
  6092. BUILDIN_FUNC(checkcart)
  6093. {
  6094. TBL_PC* sd;
  6095. sd = script_rid2sd(st);
  6096. if( sd == NULL )
  6097. return 0;// no player attached, report source
  6098. if( pc_iscarton(sd) )
  6099. script_pushint(st, 1);
  6100. else
  6101. script_pushint(st, 0);
  6102. return 0;
  6103. }
  6104. /// Sets the cart of the player.
  6105. /// <type> defaults to 1
  6106. /// <type>=0 : removes the cart
  6107. /// <type>=1 : Normal cart
  6108. /// <type>=2 : Wooden cart
  6109. /// <type>=3 : Covered cart with flowers and ferns
  6110. /// <type>=4 : Wooden cart with a Panda doll on the back
  6111. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6112. ///
  6113. /// setcart <type>;
  6114. /// setcart;
  6115. BUILDIN_FUNC(setcart)
  6116. {
  6117. int type = 1;
  6118. TBL_PC* sd;
  6119. sd = script_rid2sd(st);
  6120. if( sd == NULL )
  6121. return 0;// no player attached, report source
  6122. if( script_hasdata(st,2) )
  6123. type = script_getnum(st,2);
  6124. pc_setcart(sd, type);
  6125. return 0;
  6126. }
  6127. /// Returns if the player has a falcon.
  6128. ///
  6129. /// checkfalcon() -> <bool>
  6130. ///
  6131. /// @author Valaris
  6132. BUILDIN_FUNC(checkfalcon)
  6133. {
  6134. TBL_PC* sd;
  6135. sd = script_rid2sd(st);
  6136. if( sd == NULL )
  6137. return 0;// no player attached, report source
  6138. if( pc_isfalcon(sd) )
  6139. script_pushint(st, 1);
  6140. else
  6141. script_pushint(st, 0);
  6142. return 0;
  6143. }
  6144. /// Sets if the player has a falcon or not.
  6145. /// <flag> defaults to 1
  6146. ///
  6147. /// setfalcon <flag>;
  6148. /// setfalcon;
  6149. BUILDIN_FUNC(setfalcon)
  6150. {
  6151. int flag = 1;
  6152. TBL_PC* sd;
  6153. sd = script_rid2sd(st);
  6154. if( sd == NULL )
  6155. return 0;// no player attached, report source
  6156. if( script_hasdata(st,2) )
  6157. flag = script_getnum(st,2);
  6158. pc_setfalcon(sd, flag);
  6159. return 0;
  6160. }
  6161. /// Returns if the player is riding.
  6162. ///
  6163. /// checkriding() -> <bool>
  6164. ///
  6165. /// @author Valaris
  6166. BUILDIN_FUNC(checkriding)
  6167. {
  6168. TBL_PC* sd;
  6169. sd = script_rid2sd(st);
  6170. if( sd == NULL )
  6171. return 0;// no player attached, report source
  6172. if( pc_isriding(sd) )
  6173. script_pushint(st, 1);
  6174. else
  6175. script_pushint(st, 0);
  6176. return 0;
  6177. }
  6178. /// Sets if the player is riding.
  6179. /// <flag> defaults to 1
  6180. ///
  6181. /// setriding <flag>;
  6182. /// setriding;
  6183. BUILDIN_FUNC(setriding)
  6184. {
  6185. int flag = 1;
  6186. TBL_PC* sd;
  6187. sd = script_rid2sd(st);
  6188. if( sd == NULL )
  6189. return 0;// no player attached, report source
  6190. if( script_hasdata(st,2) )
  6191. flag = script_getnum(st,2);
  6192. pc_setriding(sd, flag);
  6193. return 0;
  6194. }
  6195. /// Sets the save point of the player.
  6196. ///
  6197. /// save "<map name>",<x>,<y>
  6198. /// savepoint "<map name>",<x>,<y>
  6199. BUILDIN_FUNC(savepoint)
  6200. {
  6201. int x;
  6202. int y;
  6203. short map;
  6204. const char* str;
  6205. TBL_PC* sd;
  6206. sd = script_rid2sd(st);
  6207. if( sd == NULL )
  6208. return 0;// no player attached, report source
  6209. str = script_getstr(st, 2);
  6210. x = script_getnum(st,3);
  6211. y = script_getnum(st,4);
  6212. map = mapindex_name2id(str);
  6213. if( map )
  6214. pc_setsavepoint(sd, map, x, y);
  6215. return 0;
  6216. }
  6217. /*==========================================
  6218. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6219. *------------------------------------------*/
  6220. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6221. {
  6222. int type;
  6223. time_t timer;
  6224. struct tm *t;
  6225. type=script_getnum(st,2);
  6226. switch(type){
  6227. case 2:
  6228. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6229. // from the system clock.)
  6230. script_pushint(st,(int)time(NULL));
  6231. break;
  6232. case 1:
  6233. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6234. time(&timer);
  6235. t=localtime(&timer);
  6236. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6237. break;
  6238. case 0:
  6239. default:
  6240. //type 0:(System Ticks)
  6241. script_pushint(st,gettick());
  6242. break;
  6243. }
  6244. return 0;
  6245. }
  6246. /*==========================================
  6247. * GetTime(Type);
  6248. * 1: Sec 2: Min 3: Hour
  6249. * 4: WeekDay 5: MonthDay 6: Month
  6250. * 7: Year
  6251. *------------------------------------------*/
  6252. BUILDIN_FUNC(gettime) /* Asgard Version */
  6253. {
  6254. int type;
  6255. time_t timer;
  6256. struct tm *t;
  6257. type=script_getnum(st,2);
  6258. time(&timer);
  6259. t=localtime(&timer);
  6260. switch(type){
  6261. case 1://Sec(0~59)
  6262. script_pushint(st,t->tm_sec);
  6263. break;
  6264. case 2://Min(0~59)
  6265. script_pushint(st,t->tm_min);
  6266. break;
  6267. case 3://Hour(0~23)
  6268. script_pushint(st,t->tm_hour);
  6269. break;
  6270. case 4://WeekDay(0~6)
  6271. script_pushint(st,t->tm_wday);
  6272. break;
  6273. case 5://MonthDay(01~31)
  6274. script_pushint(st,t->tm_mday);
  6275. break;
  6276. case 6://Month(01~12)
  6277. script_pushint(st,t->tm_mon+1);
  6278. break;
  6279. case 7://Year(20xx)
  6280. script_pushint(st,t->tm_year+1900);
  6281. break;
  6282. case 8://Year Day(01~366)
  6283. script_pushint(st,t->tm_yday+1);
  6284. break;
  6285. default://(format error)
  6286. script_pushint(st,-1);
  6287. break;
  6288. }
  6289. return 0;
  6290. }
  6291. /*==========================================
  6292. * GetTimeStr("TimeFMT", Length);
  6293. *------------------------------------------*/
  6294. BUILDIN_FUNC(gettimestr)
  6295. {
  6296. char *tmpstr;
  6297. const char *fmtstr;
  6298. int maxlen;
  6299. time_t now = time(NULL);
  6300. fmtstr=script_getstr(st,2);
  6301. maxlen=script_getnum(st,3);
  6302. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6303. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6304. tmpstr[maxlen]='\0';
  6305. script_pushstr(st,tmpstr);
  6306. return 0;
  6307. }
  6308. /*==========================================
  6309. * カプラ倉庫を開く
  6310. *------------------------------------------*/
  6311. BUILDIN_FUNC(openstorage)
  6312. {
  6313. TBL_PC* sd;
  6314. sd = script_rid2sd(st);
  6315. if( sd == NULL )
  6316. return 0;
  6317. storage_storageopen(sd);
  6318. return 0;
  6319. }
  6320. BUILDIN_FUNC(guildopenstorage)
  6321. {
  6322. TBL_PC* sd;
  6323. int ret;
  6324. sd = script_rid2sd(st);
  6325. if( sd == NULL )
  6326. return 0;
  6327. ret = storage_guild_storageopen(sd);
  6328. script_pushint(st,ret);
  6329. return 0;
  6330. }
  6331. /*==========================================
  6332. * アイテムによるスキル発動
  6333. *------------------------------------------*/
  6334. /// itemskill <skill id>,<level>
  6335. /// itemskill "<skill name>",<level>
  6336. BUILDIN_FUNC(itemskill)
  6337. {
  6338. int id;
  6339. int lv;
  6340. TBL_PC* sd;
  6341. sd = script_rid2sd(st);
  6342. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6343. return 0;
  6344. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6345. lv = script_getnum(st,3);
  6346. sd->skillitem=id;
  6347. sd->skillitemlv=lv;
  6348. clif_item_skill(sd,id,lv);
  6349. return 0;
  6350. }
  6351. /*==========================================
  6352. * アイテム作成
  6353. *------------------------------------------*/
  6354. BUILDIN_FUNC(produce)
  6355. {
  6356. int trigger;
  6357. TBL_PC* sd;
  6358. sd = script_rid2sd(st);
  6359. if( sd == NULL )
  6360. return 0;
  6361. trigger=script_getnum(st,2);
  6362. clif_skill_produce_mix_list(sd, trigger);
  6363. return 0;
  6364. }
  6365. /*==========================================
  6366. * NPCでペット作る
  6367. *------------------------------------------*/
  6368. BUILDIN_FUNC(makepet)
  6369. {
  6370. TBL_PC* sd;
  6371. int id,pet_id;
  6372. id=script_getnum(st,2);
  6373. sd = script_rid2sd(st);
  6374. if( sd == NULL )
  6375. return 0;
  6376. pet_id = search_petDB_index(id, PET_CLASS);
  6377. if (pet_id < 0)
  6378. pet_id = search_petDB_index(id, PET_EGG);
  6379. if (pet_id >= 0 && sd) {
  6380. sd->catch_target_class = pet_db[pet_id].class_;
  6381. intif_create_pet(
  6382. sd->status.account_id, sd->status.char_id,
  6383. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6384. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6385. 100, 0, 1, pet_db[pet_id].jname);
  6386. }
  6387. return 0;
  6388. }
  6389. /*==========================================
  6390. * NPCで経験値上げる
  6391. *------------------------------------------*/
  6392. BUILDIN_FUNC(getexp)
  6393. {
  6394. TBL_PC* sd;
  6395. int base=0,job=0;
  6396. double bonus;
  6397. sd = script_rid2sd(st);
  6398. if( sd == NULL )
  6399. return 0;
  6400. base=script_getnum(st,2);
  6401. job =script_getnum(st,3);
  6402. if(base<0 || job<0)
  6403. return 0;
  6404. // bonus for npc-given exp
  6405. bonus = battle_config.quest_exp_rate / 100.;
  6406. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6407. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6408. pc_gainexp(sd, NULL, base, job);
  6409. return 0;
  6410. }
  6411. /*==========================================
  6412. * Gain guild exp [Celest]
  6413. *------------------------------------------*/
  6414. BUILDIN_FUNC(guildgetexp)
  6415. {
  6416. TBL_PC* sd;
  6417. int exp;
  6418. sd = script_rid2sd(st);
  6419. if( sd == NULL )
  6420. return 0;
  6421. exp = script_getnum(st,2);
  6422. if(exp < 0)
  6423. return 0;
  6424. if(sd && sd->status.guild_id > 0)
  6425. guild_getexp (sd, exp);
  6426. return 0;
  6427. }
  6428. /*==========================================
  6429. * Changes the guild master of a guild [Skotlex]
  6430. *------------------------------------------*/
  6431. BUILDIN_FUNC(guildchangegm)
  6432. {
  6433. TBL_PC *sd;
  6434. int guild_id;
  6435. const char *name;
  6436. guild_id = script_getnum(st,2);
  6437. name = script_getstr(st,3);
  6438. sd=map_nick2sd(name);
  6439. if (!sd)
  6440. script_pushint(st,0);
  6441. else
  6442. script_pushint(st,guild_gm_change(guild_id, sd));
  6443. return 0;
  6444. }
  6445. /*==========================================
  6446. * モンスター発生
  6447. *------------------------------------------*/
  6448. BUILDIN_FUNC(monster)
  6449. {
  6450. const char* map = script_getstr(st,2);
  6451. int x = script_getnum(st,3);
  6452. int y = script_getnum(st,4);
  6453. const char* str = script_getstr(st,5);
  6454. int class_ = script_getnum(st,6);
  6455. int amount = script_getnum(st,7);
  6456. const char* event = "";
  6457. struct map_session_data* sd;
  6458. int m;
  6459. if( script_hasdata(st,8) )
  6460. {
  6461. event = script_getstr(st,8);
  6462. check_event(st, event);
  6463. }
  6464. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6465. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6466. return 1;
  6467. }
  6468. sd = map_id2sd(st->rid);
  6469. if( sd && strcmp(map,"this") == 0 )
  6470. m = sd->bl.m;
  6471. else
  6472. m = map_mapname2mapid(map);
  6473. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6474. return 0;
  6475. }
  6476. /*==========================================
  6477. * Request List of Monster Drops
  6478. *------------------------------------------*/
  6479. BUILDIN_FUNC(getmobdrops)
  6480. {
  6481. int class_ = script_getnum(st,2);
  6482. int i, j = 0;
  6483. struct item_data *i_data;
  6484. struct mob_db *mob;
  6485. if( !mobdb_checkid(class_) )
  6486. {
  6487. script_pushint(st, 0);
  6488. return 0;
  6489. }
  6490. mob = mob_db(class_);
  6491. for( i = 0; i < MAX_MOB_DROP; i++ )
  6492. {
  6493. if( mob->dropitem[i].nameid < 1 )
  6494. continue;
  6495. if( (i_data = itemdb_exists(mob->dropitem[i].nameid)) == NULL )
  6496. continue;
  6497. mapreg_setreg(add_str("$@MobDrop_item") + (j<<24), mob->dropitem[i].nameid);
  6498. mapreg_setreg(add_str("$@MobDrop_rate") + (j<<24), mob->dropitem[i].p);
  6499. j++;
  6500. }
  6501. mapreg_setreg(add_str("$@MobDrop_count"), j);
  6502. script_pushint(st, 1);
  6503. return 0;
  6504. }
  6505. /*==========================================
  6506. * モンスター発生
  6507. *------------------------------------------*/
  6508. BUILDIN_FUNC(areamonster)
  6509. {
  6510. const char* map = script_getstr(st,2);
  6511. int x0 = script_getnum(st,3);
  6512. int y0 = script_getnum(st,4);
  6513. int x1 = script_getnum(st,5);
  6514. int y1 = script_getnum(st,6);
  6515. const char* str = script_getstr(st,7);
  6516. int class_ = script_getnum(st,8);
  6517. int amount = script_getnum(st,9);
  6518. const char* event = "";
  6519. struct map_session_data* sd;
  6520. int m;
  6521. if( script_hasdata(st,10) )
  6522. {
  6523. event = script_getstr(st,10);
  6524. check_event(st, event);
  6525. }
  6526. sd = map_id2sd(st->rid);
  6527. if( sd && strcmp(map,"this") == 0 )
  6528. m = sd->bl.m;
  6529. else
  6530. m = map_mapname2mapid(map);
  6531. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6532. return 0;
  6533. }
  6534. /*==========================================
  6535. * モンスター削除
  6536. *------------------------------------------*/
  6537. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  6538. { //same fix but with killmonster instead - stripping events from mobs.
  6539. TBL_MOB* md = (TBL_MOB*)bl;
  6540. char *event=va_arg(ap,char *);
  6541. int allflag=va_arg(ap,int);
  6542. md->state.npc_killmonster = 1;
  6543. if(!allflag){
  6544. if(strcmp(event,md->npc_event)==0)
  6545. status_kill(bl);
  6546. }else{
  6547. if(!md->spawn)
  6548. status_kill(bl);
  6549. }
  6550. md->state.npc_killmonster = 0;
  6551. return 0;
  6552. }
  6553. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6554. {
  6555. TBL_MOB* md = (TBL_MOB*)bl;
  6556. char *event=va_arg(ap,char *);
  6557. int allflag=va_arg(ap,int);
  6558. if(!allflag){
  6559. if(strcmp(event,md->npc_event)==0)
  6560. status_kill(bl);
  6561. }else{
  6562. if(!md->spawn)
  6563. status_kill(bl);
  6564. }
  6565. return 0;
  6566. }
  6567. BUILDIN_FUNC(killmonster)
  6568. {
  6569. const char *mapname,*event;
  6570. int m,allflag=0;
  6571. mapname=script_getstr(st,2);
  6572. event=script_getstr(st,3);
  6573. if(strcmp(event,"All")==0)
  6574. allflag = 1;
  6575. else
  6576. check_event(st, event);
  6577. if( (m=map_mapname2mapid(mapname))<0 )
  6578. return 0;
  6579. if( script_hasdata(st,4) ) {
  6580. if ( script_getnum(st,4) == 1 ) {
  6581. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6582. return 0;
  6583. }
  6584. }
  6585. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  6586. return 0;
  6587. }
  6588. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  6589. { //Strips the event from the mob if it's killed the old method.
  6590. struct mob_data *md;
  6591. md = BL_CAST(BL_MOB, bl);
  6592. if (md->npc_event[0])
  6593. md->npc_event[0] = 0;
  6594. status_kill(bl);
  6595. return 0;
  6596. }
  6597. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6598. {
  6599. status_kill(bl);
  6600. return 0;
  6601. }
  6602. BUILDIN_FUNC(killmonsterall)
  6603. {
  6604. const char *mapname;
  6605. int m;
  6606. mapname=script_getstr(st,2);
  6607. if( (m=map_mapname2mapid(mapname))<0 )
  6608. return 0;
  6609. if( script_hasdata(st,3) ) {
  6610. if ( script_getnum(st,3) == 1 ) {
  6611. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  6612. return 0;
  6613. }
  6614. }
  6615. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  6616. return 0;
  6617. }
  6618. /*==========================================
  6619. * Creates a clone of a player.
  6620. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6621. *------------------------------------------*/
  6622. BUILDIN_FUNC(clone)
  6623. {
  6624. TBL_PC *sd, *msd=NULL;
  6625. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6626. unsigned int duration = 0;
  6627. const char *map,*event="";
  6628. map=script_getstr(st,2);
  6629. x=script_getnum(st,3);
  6630. y=script_getnum(st,4);
  6631. event=script_getstr(st,5);
  6632. char_id=script_getnum(st,6);
  6633. if( script_hasdata(st,7) )
  6634. master_id=script_getnum(st,7);
  6635. if( script_hasdata(st,8) )
  6636. mode=script_getnum(st,8);
  6637. if( script_hasdata(st,9) )
  6638. flag=script_getnum(st,9);
  6639. if( script_hasdata(st,10) )
  6640. duration=script_getnum(st,10);
  6641. check_event(st, event);
  6642. m = map_mapname2mapid(map);
  6643. if (m < 0) return 0;
  6644. sd = map_charid2sd(char_id);
  6645. if (master_id) {
  6646. msd = map_charid2sd(master_id);
  6647. if (msd)
  6648. master_id = msd->bl.id;
  6649. else
  6650. master_id = 0;
  6651. }
  6652. if (sd) //Return ID of newly crafted clone.
  6653. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6654. else //Failed to create clone.
  6655. script_pushint(st,0);
  6656. return 0;
  6657. }
  6658. /*==========================================
  6659. * イベント実行
  6660. *------------------------------------------*/
  6661. BUILDIN_FUNC(doevent)
  6662. {
  6663. const char* event = script_getstr(st,2);
  6664. check_event(st, event);
  6665. npc_event(map_id2sd(st->rid),event,0);
  6666. return 0;
  6667. }
  6668. /*==========================================
  6669. * NPC主体イベント実行
  6670. *------------------------------------------*/
  6671. BUILDIN_FUNC(donpcevent)
  6672. {
  6673. const char* event = script_getstr(st,2);
  6674. check_event(st, event);
  6675. npc_event_do(event);
  6676. return 0;
  6677. }
  6678. /// for Aegis compatibility
  6679. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6680. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6681. {
  6682. const char* npc = script_getstr(st,2);
  6683. const char* command = script_getstr(st,3);
  6684. char event[51];
  6685. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6686. check_event(st, event);
  6687. npc_event_do(event);
  6688. return 0;
  6689. }
  6690. /*==========================================
  6691. * イベントタイマー追加
  6692. *------------------------------------------*/
  6693. BUILDIN_FUNC(addtimer)
  6694. {
  6695. int tick = script_getnum(st,2);
  6696. const char* event = script_getstr(st, 3);
  6697. TBL_PC* sd;
  6698. check_event(st, event);
  6699. sd = script_rid2sd(st);
  6700. if( sd == NULL )
  6701. return 0;
  6702. pc_addeventtimer(sd,tick,event);
  6703. return 0;
  6704. }
  6705. /*==========================================
  6706. * イベントタイマー削除
  6707. *------------------------------------------*/
  6708. BUILDIN_FUNC(deltimer)
  6709. {
  6710. const char *event;
  6711. TBL_PC* sd;
  6712. event=script_getstr(st, 2);
  6713. sd = script_rid2sd(st);
  6714. if( sd == NULL )
  6715. return 0;
  6716. check_event(st, event);
  6717. pc_deleventtimer(sd,event);
  6718. return 0;
  6719. }
  6720. /*==========================================
  6721. * イベントタイマーのカウント値追加
  6722. *------------------------------------------*/
  6723. BUILDIN_FUNC(addtimercount)
  6724. {
  6725. const char *event;
  6726. int tick;
  6727. TBL_PC* sd;
  6728. event=script_getstr(st, 2);
  6729. tick=script_getnum(st,3);
  6730. sd = script_rid2sd(st);
  6731. if( sd == NULL )
  6732. return 0;
  6733. check_event(st, event);
  6734. pc_addeventtimercount(sd,event,tick);
  6735. return 0;
  6736. }
  6737. /*==========================================
  6738. * NPCタイマー初期化
  6739. *------------------------------------------*/
  6740. BUILDIN_FUNC(initnpctimer)
  6741. {
  6742. struct npc_data *nd;
  6743. int flag = 0;
  6744. if( script_hasdata(st,3) )
  6745. { //Two arguments: NPC name and attach flag.
  6746. nd = npc_name2id(script_getstr(st, 2));
  6747. flag = script_getnum(st,3);
  6748. }
  6749. else if( script_hasdata(st,2) )
  6750. { //Check if argument is numeric (flag) or string (npc name)
  6751. struct script_data *data;
  6752. data = script_getdata(st,2);
  6753. get_val(st,data);
  6754. if( data_isstring(data) ) //NPC name
  6755. nd = npc_name2id(conv_str(st, data));
  6756. else if( data_isint(data) ) //Flag
  6757. {
  6758. nd = (struct npc_data *)map_id2bl(st->oid);
  6759. flag = conv_num(st,data);
  6760. }
  6761. else
  6762. {
  6763. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6764. return 1;
  6765. }
  6766. }
  6767. else
  6768. nd=(struct npc_data *)map_id2bl(st->oid);
  6769. if (!nd)
  6770. return 0;
  6771. if (flag) //Attach
  6772. {
  6773. TBL_PC* sd = script_rid2sd(st);
  6774. if( sd == NULL )
  6775. return 0;
  6776. nd->u.scr.rid = sd->bl.id;
  6777. }
  6778. npc_settimerevent_tick(nd,0);
  6779. npc_timerevent_start(nd, st->rid);
  6780. return 0;
  6781. }
  6782. /*==========================================
  6783. * NPCタイマー開始
  6784. *------------------------------------------*/
  6785. BUILDIN_FUNC(startnpctimer)
  6786. {
  6787. struct npc_data *nd;
  6788. int flag = 0;
  6789. if( script_hasdata(st,3) )
  6790. { //Two arguments: NPC name and attach flag.
  6791. nd = npc_name2id(script_getstr(st, 2));
  6792. flag = script_getnum(st,3);
  6793. }
  6794. else if( script_hasdata(st,2) )
  6795. { //Check if argument is numeric (flag) or string (npc name)
  6796. struct script_data *data;
  6797. data = script_getdata(st,2);
  6798. get_val(st,data);
  6799. if( data_isstring(data) ) //NPC name
  6800. nd = npc_name2id(conv_str(st, data));
  6801. else if( data_isint(data) ) //Flag
  6802. {
  6803. nd = (struct npc_data *)map_id2bl(st->oid);
  6804. flag = conv_num(st,data);
  6805. }
  6806. else
  6807. {
  6808. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6809. return 1;
  6810. }
  6811. }
  6812. else
  6813. nd=(struct npc_data *)map_id2bl(st->oid);
  6814. if (!nd)
  6815. return 0;
  6816. if (flag) //Attach
  6817. {
  6818. TBL_PC* sd = script_rid2sd(st);
  6819. if( sd == NULL )
  6820. return 0;
  6821. nd->u.scr.rid = sd->bl.id;
  6822. }
  6823. npc_timerevent_start(nd, st->rid);
  6824. return 0;
  6825. }
  6826. /*==========================================
  6827. * NPCタイマー停止
  6828. *------------------------------------------*/
  6829. BUILDIN_FUNC(stopnpctimer)
  6830. {
  6831. struct npc_data *nd;
  6832. int flag = 0;
  6833. if( script_hasdata(st,3) )
  6834. { //Two arguments: NPC name and attach flag.
  6835. nd = npc_name2id(script_getstr(st, 2));
  6836. flag = script_getnum(st,3);
  6837. }
  6838. else if( script_hasdata(st,2) )
  6839. { //Check if argument is numeric (flag) or string (npc name)
  6840. struct script_data *data;
  6841. data = script_getdata(st,2);
  6842. get_val(st,data);
  6843. if( data_isstring(data) ) //NPC name
  6844. nd = npc_name2id(conv_str(st, data));
  6845. else if( data_isint(data) ) //Flag
  6846. {
  6847. nd = (struct npc_data *)map_id2bl(st->oid);
  6848. flag = conv_num(st,data);
  6849. }
  6850. else
  6851. {
  6852. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6853. return 1;
  6854. }
  6855. }
  6856. else
  6857. nd=(struct npc_data *)map_id2bl(st->oid);
  6858. if (!nd)
  6859. return 0;
  6860. if (flag) //Detach
  6861. nd->u.scr.rid = 0;
  6862. npc_timerevent_stop(nd);
  6863. return 0;
  6864. }
  6865. /*==========================================
  6866. * NPCタイマー情報所得
  6867. *------------------------------------------*/
  6868. BUILDIN_FUNC(getnpctimer)
  6869. {
  6870. struct npc_data *nd;
  6871. TBL_PC *sd;
  6872. int type=script_getnum(st,2);
  6873. int val=0;
  6874. if( script_hasdata(st,3) )
  6875. nd = npc_name2id(script_getstr(st,3));
  6876. else
  6877. nd = (struct npc_data *)map_id2bl(st->oid);
  6878. if (!nd || nd->bl.type != BL_NPC)
  6879. {
  6880. script_pushint(st,0);
  6881. ShowError("getnpctimer: Invalid NPC.\n");
  6882. return 1;
  6883. }
  6884. switch(type){
  6885. case 0: val=npc_gettimerevent_tick(nd); break;
  6886. case 1:
  6887. if (nd->u.scr.rid) {
  6888. sd = map_id2sd(nd->u.scr.rid);
  6889. if (!sd) {
  6890. ShowError("buildin_getnpctimer: Attached player not found!\n");
  6891. break;
  6892. }
  6893. val = (sd->npc_timer_id != -1);
  6894. } else
  6895. val= (nd->u.scr.timerid !=-1);
  6896. break;
  6897. case 2: val= nd->u.scr.timeramount; break;
  6898. }
  6899. script_pushint(st,val);
  6900. return 0;
  6901. }
  6902. /*==========================================
  6903. * NPCタイマー値設定
  6904. *------------------------------------------*/
  6905. BUILDIN_FUNC(setnpctimer)
  6906. {
  6907. int tick;
  6908. struct npc_data *nd;
  6909. tick=script_getnum(st,2);
  6910. if( script_hasdata(st,3) )
  6911. nd=npc_name2id(script_getstr(st,3));
  6912. else
  6913. nd=(struct npc_data *)map_id2bl(st->oid);
  6914. if (!nd || nd->bl.type != BL_NPC)
  6915. {
  6916. script_pushint(st,1);
  6917. ShowError("setnpctimer: Invalid NPC.\n");
  6918. return 1;
  6919. }
  6920. npc_settimerevent_tick(nd,tick);
  6921. script_pushint(st,0);
  6922. return 0;
  6923. }
  6924. /*==========================================
  6925. * attaches the player rid to the timer [Celest]
  6926. *------------------------------------------*/
  6927. BUILDIN_FUNC(attachnpctimer)
  6928. {
  6929. TBL_PC *sd;
  6930. struct npc_data *nd;
  6931. nd=(struct npc_data *)map_id2bl(st->oid);
  6932. if (!nd || nd->bl.type != BL_NPC)
  6933. {
  6934. script_pushint(st,1);
  6935. ShowError("setnpctimer: Invalid NPC.\n");
  6936. return 1;
  6937. }
  6938. if( script_hasdata(st,2) )
  6939. sd=map_nick2sd(script_getstr(st,2));
  6940. else
  6941. sd = script_rid2sd(st);
  6942. if (!sd)
  6943. {
  6944. script_pushint(st,1);
  6945. ShowWarning("attachnpctimer: Invalid player.\n");
  6946. return 1;
  6947. }
  6948. nd->u.scr.rid = sd->bl.id;
  6949. script_pushint(st,0);
  6950. return 0;
  6951. }
  6952. /*==========================================
  6953. * detaches a player rid from the timer [Celest]
  6954. *------------------------------------------*/
  6955. BUILDIN_FUNC(detachnpctimer)
  6956. {
  6957. struct npc_data *nd;
  6958. if( script_hasdata(st,2) )
  6959. nd=npc_name2id(script_getstr(st,2));
  6960. else
  6961. nd=(struct npc_data *)map_id2bl(st->oid);
  6962. if (!nd || nd->bl.type != BL_NPC)
  6963. {
  6964. script_pushint(st,1);
  6965. ShowError("detachnpctimer: Invalid NPC.\n");
  6966. return 1;
  6967. }
  6968. nd->u.scr.rid = 0;
  6969. script_pushint(st,0);
  6970. return 0;
  6971. }
  6972. /*==========================================
  6973. * To avoid "player not attached" script errors, this function is provided,
  6974. * it checks if there is a player attached to the current script. [Skotlex]
  6975. * If no, returns 0, if yes, returns the account_id of the attached player.
  6976. *------------------------------------------*/
  6977. BUILDIN_FUNC(playerattached)
  6978. {
  6979. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  6980. script_pushint(st,0);
  6981. else
  6982. script_pushint(st,st->rid);
  6983. return 0;
  6984. }
  6985. /*==========================================
  6986. * 天の声アナウンス
  6987. *------------------------------------------*/
  6988. BUILDIN_FUNC(announce)
  6989. {
  6990. const char *str, *color=NULL;
  6991. int flag;
  6992. str=script_getstr(st,2);
  6993. flag=script_getnum(st,3);
  6994. if (script_hasdata(st,4))
  6995. color=script_getstr(st,4);
  6996. if(flag&0x0f){
  6997. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  6998. (struct block_list *)script_rid2sd(st);
  6999. if( bl == NULL )
  7000. return 0;
  7001. if (color)
  7002. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  7003. else
  7004. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  7005. }else {
  7006. if (color)
  7007. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  7008. else
  7009. intif_GMmessage(str,(int)strlen(str)+1,flag);
  7010. }
  7011. return 0;
  7012. }
  7013. /*==========================================
  7014. * 天の声アナウンス(特定マップ)
  7015. *------------------------------------------*/
  7016. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  7017. {
  7018. char *str, *color;
  7019. int len,flag;
  7020. str=va_arg(ap,char *);
  7021. len=va_arg(ap,int);
  7022. flag=va_arg(ap,int);
  7023. color=va_arg(ap,char *);
  7024. if (color)
  7025. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  7026. else
  7027. clif_GMmessage(bl,str,len,flag|3);
  7028. return 0;
  7029. }
  7030. BUILDIN_FUNC(mapannounce)
  7031. {
  7032. const char *mapname,*str, *color=NULL;
  7033. int flag,m;
  7034. mapname=script_getstr(st,2);
  7035. str=script_getstr(st,3);
  7036. flag=script_getnum(st,4);
  7037. if (script_hasdata(st,5))
  7038. color=script_getstr(st,5);
  7039. if( (m=map_mapname2mapid(mapname))<0 )
  7040. return 0;
  7041. map_foreachinmap(buildin_mapannounce_sub,
  7042. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  7043. return 0;
  7044. }
  7045. /*==========================================
  7046. * 天の声アナウンス(特定エリア)
  7047. *------------------------------------------*/
  7048. BUILDIN_FUNC(areaannounce)
  7049. {
  7050. const char *map,*str,*color=NULL;
  7051. int flag,m;
  7052. int x0,y0,x1,y1;
  7053. map=script_getstr(st,2);
  7054. x0=script_getnum(st,3);
  7055. y0=script_getnum(st,4);
  7056. x1=script_getnum(st,5);
  7057. y1=script_getnum(st,6);
  7058. str=script_getstr(st,7);
  7059. flag=script_getnum(st,8);
  7060. if (script_hasdata(st,9))
  7061. color=script_getstr(st,9);
  7062. if( (m=map_mapname2mapid(map))<0 )
  7063. return 0;
  7064. map_foreachinarea(buildin_mapannounce_sub,
  7065. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  7066. return 0;
  7067. }
  7068. /*==========================================
  7069. * ユーザー数所得
  7070. *------------------------------------------*/
  7071. BUILDIN_FUNC(getusers)
  7072. {
  7073. int flag=script_getnum(st,2);
  7074. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7075. int val=0;
  7076. switch(flag&0x07){
  7077. case 0: val=map[bl->m].users; break;
  7078. case 1: val=map_getusers(); break;
  7079. }
  7080. script_pushint(st,val);
  7081. return 0;
  7082. }
  7083. /*==========================================
  7084. * Works like @WHO - displays all online users names in window
  7085. *------------------------------------------*/
  7086. BUILDIN_FUNC(getusersname)
  7087. {
  7088. TBL_PC *sd, *pl_sd;
  7089. int disp_num=1;
  7090. struct s_mapiterator* iter;
  7091. sd = script_rid2sd(st);
  7092. if (!sd) return 0;
  7093. iter = mapit_getallusers();
  7094. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7095. {
  7096. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7097. continue; // skip hidden GMs
  7098. if((disp_num++)%10==0)
  7099. clif_scriptnext(sd,st->oid);
  7100. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7101. }
  7102. mapit_free(iter);
  7103. return 0;
  7104. }
  7105. /*==========================================
  7106. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7107. *------------------------------------------*/
  7108. BUILDIN_FUNC(getmapguildusers)
  7109. {
  7110. const char *str;
  7111. int m, gid;
  7112. int i=0,c=0;
  7113. struct guild *g = NULL;
  7114. str=script_getstr(st,2);
  7115. gid=script_getnum(st,3);
  7116. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7117. script_pushint(st,-1);
  7118. return 0;
  7119. }
  7120. g = guild_search(gid);
  7121. if (g){
  7122. for(i = 0; i < g->max_member; i++)
  7123. {
  7124. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7125. c++;
  7126. }
  7127. }
  7128. script_pushint(st,c);
  7129. return 0;
  7130. }
  7131. /*==========================================
  7132. * マップ指定ユーザー数所得
  7133. *------------------------------------------*/
  7134. BUILDIN_FUNC(getmapusers)
  7135. {
  7136. const char *str;
  7137. int m;
  7138. str=script_getstr(st,2);
  7139. if( (m=map_mapname2mapid(str))< 0){
  7140. script_pushint(st,-1);
  7141. return 0;
  7142. }
  7143. script_pushint(st,map[m].users);
  7144. return 0;
  7145. }
  7146. /*==========================================
  7147. * エリア指定ユーザー数所得
  7148. *------------------------------------------*/
  7149. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7150. {
  7151. int *users=va_arg(ap,int *);
  7152. (*users)++;
  7153. return 0;
  7154. }
  7155. BUILDIN_FUNC(getareausers)
  7156. {
  7157. const char *str;
  7158. int m,x0,y0,x1,y1,users=0;
  7159. str=script_getstr(st,2);
  7160. x0=script_getnum(st,3);
  7161. y0=script_getnum(st,4);
  7162. x1=script_getnum(st,5);
  7163. y1=script_getnum(st,6);
  7164. if( (m=map_mapname2mapid(str))< 0){
  7165. script_pushint(st,-1);
  7166. return 0;
  7167. }
  7168. map_foreachinarea(buildin_getareausers_sub,
  7169. m,x0,y0,x1,y1,BL_PC,&users);
  7170. script_pushint(st,users);
  7171. return 0;
  7172. }
  7173. /*==========================================
  7174. * エリア指定ドロップアイテム数所得
  7175. *------------------------------------------*/
  7176. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7177. {
  7178. int item=va_arg(ap,int);
  7179. int *amount=va_arg(ap,int *);
  7180. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7181. if(drop->item_data.nameid==item)
  7182. (*amount)+=drop->item_data.amount;
  7183. return 0;
  7184. }
  7185. BUILDIN_FUNC(getareadropitem)
  7186. {
  7187. const char *str;
  7188. int m,x0,y0,x1,y1,item,amount=0;
  7189. struct script_data *data;
  7190. str=script_getstr(st,2);
  7191. x0=script_getnum(st,3);
  7192. y0=script_getnum(st,4);
  7193. x1=script_getnum(st,5);
  7194. y1=script_getnum(st,6);
  7195. data=script_getdata(st,7);
  7196. get_val(st,data);
  7197. if( data_isstring(data) ){
  7198. const char *name=conv_str(st,data);
  7199. struct item_data *item_data = itemdb_searchname(name);
  7200. item=UNKNOWN_ITEM_ID;
  7201. if( item_data )
  7202. item=item_data->nameid;
  7203. }else
  7204. item=conv_num(st,data);
  7205. if( (m=map_mapname2mapid(str))< 0){
  7206. script_pushint(st,-1);
  7207. return 0;
  7208. }
  7209. map_foreachinarea(buildin_getareadropitem_sub,
  7210. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7211. script_pushint(st,amount);
  7212. return 0;
  7213. }
  7214. /*==========================================
  7215. * NPCの有効化
  7216. *------------------------------------------*/
  7217. BUILDIN_FUNC(enablenpc)
  7218. {
  7219. const char *str;
  7220. str=script_getstr(st,2);
  7221. npc_enable(str,1);
  7222. return 0;
  7223. }
  7224. /*==========================================
  7225. * NPCの無効化
  7226. *------------------------------------------*/
  7227. BUILDIN_FUNC(disablenpc)
  7228. {
  7229. const char *str;
  7230. str=script_getstr(st,2);
  7231. npc_enable(str,0);
  7232. return 0;
  7233. }
  7234. /*==========================================
  7235. * 隠れているNPCの表示
  7236. *------------------------------------------*/
  7237. BUILDIN_FUNC(hideoffnpc)
  7238. {
  7239. const char *str;
  7240. str=script_getstr(st,2);
  7241. npc_enable(str,2);
  7242. return 0;
  7243. }
  7244. /*==========================================
  7245. * NPCをハイディング
  7246. *------------------------------------------*/
  7247. BUILDIN_FUNC(hideonnpc)
  7248. {
  7249. const char *str;
  7250. str=script_getstr(st,2);
  7251. npc_enable(str,4);
  7252. return 0;
  7253. }
  7254. /// Starts a status effect on the target unit or on the attached player.
  7255. ///
  7256. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7257. BUILDIN_FUNC(sc_start)
  7258. {
  7259. struct block_list* bl;
  7260. enum sc_type type;
  7261. int tick;
  7262. int val1;
  7263. int val4 = 0;
  7264. type = (sc_type)script_getnum(st,2);
  7265. tick = script_getnum(st,3);
  7266. val1 = script_getnum(st,4);
  7267. if( script_hasdata(st,5) )
  7268. bl = map_id2bl(script_getnum(st,5));
  7269. else
  7270. bl = map_id2bl(st->rid);
  7271. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7272. {// When there isn't a duration specified, try to get it from the skill_db
  7273. tick = skill_get_time(status_sc2skill(type), val1);
  7274. }
  7275. if( potion_flag == 1 && potion_target )
  7276. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7277. bl = map_id2bl(potion_target);
  7278. tick /= 2;// Thrown potions only last half.
  7279. val4 = 1;// Mark that this was a thrown sc_effect
  7280. }
  7281. if( bl )
  7282. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7283. return 0;
  7284. }
  7285. /// Starts a status effect on the target unit or on the attached player.
  7286. ///
  7287. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7288. BUILDIN_FUNC(sc_start2)
  7289. {
  7290. struct block_list* bl;
  7291. enum sc_type type;
  7292. int tick;
  7293. int val1;
  7294. int val4 = 0;
  7295. int rate;
  7296. type = (sc_type)script_getnum(st,2);
  7297. tick = script_getnum(st,3);
  7298. val1 = script_getnum(st,4);
  7299. rate = script_getnum(st,5);
  7300. if( script_hasdata(st,6) )
  7301. bl = map_id2bl(script_getnum(st,6));
  7302. else
  7303. bl = map_id2bl(st->rid);
  7304. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7305. {// When there isn't a duration specified, try to get it from the skill_db
  7306. tick = skill_get_time(status_sc2skill(type), val1);
  7307. }
  7308. if( potion_flag == 1 && potion_target )
  7309. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7310. bl = map_id2bl(potion_target);
  7311. tick /= 2;// Thrown potions only last half.
  7312. val4 = 1;// Mark that this was a thrown sc_effect
  7313. }
  7314. if( bl )
  7315. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7316. return 0;
  7317. }
  7318. /// Starts a status effect on the target unit or on the attached player.
  7319. ///
  7320. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7321. BUILDIN_FUNC(sc_start4)
  7322. {
  7323. struct block_list* bl;
  7324. enum sc_type type;
  7325. int tick;
  7326. int val1;
  7327. int val2;
  7328. int val3;
  7329. int val4;
  7330. type = (sc_type)script_getnum(st,2);
  7331. tick = script_getnum(st,3);
  7332. val1 = script_getnum(st,4);
  7333. val2 = script_getnum(st,5);
  7334. val3 = script_getnum(st,6);
  7335. val4 = script_getnum(st,7);
  7336. if( script_hasdata(st,8) )
  7337. bl = map_id2bl(script_getnum(st,8));
  7338. else
  7339. bl = map_id2bl(st->rid);
  7340. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7341. {// When there isn't a duration specified, try to get it from the skill_db
  7342. tick = skill_get_time(status_sc2skill(type), val1);
  7343. }
  7344. if( potion_flag == 1 && potion_target )
  7345. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7346. bl = map_id2bl(potion_target);
  7347. tick /= 2;// Thrown potions only last half.
  7348. }
  7349. if( bl )
  7350. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7351. return 0;
  7352. }
  7353. /// Ends one or all status effects on the target unit or on the attached player.
  7354. ///
  7355. /// sc_end <effect_id>{,<unit_id>};
  7356. BUILDIN_FUNC(sc_end)
  7357. {
  7358. struct block_list* bl;
  7359. int type;
  7360. type = script_getnum(st,2);
  7361. if( script_hasdata(st,3) )
  7362. bl = map_id2bl(script_getnum(st,3));
  7363. else
  7364. bl = map_id2bl(st->rid);
  7365. if( potion_flag==1 && potion_target )
  7366. {//##TODO how does this work [FlavioJS]
  7367. bl = map_id2bl(potion_target);
  7368. }
  7369. if( !bl ) return 0;
  7370. if( type >= 0 && type < SC_MAX )
  7371. {
  7372. struct status_change *sc = status_get_sc(bl);
  7373. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7374. if (!sce) return 0;
  7375. //This should help status_change_end force disabling the SC in case it has no limit.
  7376. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7377. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  7378. } else
  7379. status_change_clear(bl, 2);// remove all effects
  7380. return 0;
  7381. }
  7382. /*==========================================
  7383. * 状態異常耐性を計算した確率を返す
  7384. *------------------------------------------*/
  7385. BUILDIN_FUNC(getscrate)
  7386. {
  7387. struct block_list *bl;
  7388. int type,rate;
  7389. type=script_getnum(st,2);
  7390. rate=script_getnum(st,3);
  7391. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7392. bl = map_id2bl(script_getnum(st,4));
  7393. else
  7394. bl = map_id2bl(st->rid);
  7395. if (bl)
  7396. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  7397. script_pushint(st,rate);
  7398. return 0;
  7399. }
  7400. /*==========================================
  7401. *
  7402. *------------------------------------------*/
  7403. BUILDIN_FUNC(debugmes)
  7404. {
  7405. const char *str;
  7406. str=script_getstr(st,2);
  7407. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7408. return 0;
  7409. }
  7410. /*==========================================
  7411. *捕獲アイテム使用
  7412. *------------------------------------------*/
  7413. BUILDIN_FUNC(catchpet)
  7414. {
  7415. int pet_id;
  7416. TBL_PC *sd;
  7417. pet_id= script_getnum(st,2);
  7418. sd=script_rid2sd(st);
  7419. if( sd == NULL )
  7420. return 0;
  7421. pet_catch_process1(sd,pet_id);
  7422. return 0;
  7423. }
  7424. /*==========================================
  7425. * [orn]
  7426. *------------------------------------------*/
  7427. BUILDIN_FUNC(homunculus_evolution)
  7428. {
  7429. TBL_PC *sd;
  7430. sd=script_rid2sd(st);
  7431. if( sd == NULL )
  7432. return 0;
  7433. if(merc_is_hom_active(sd->hd))
  7434. {
  7435. if (sd->hd->homunculus.intimacy > 91000)
  7436. merc_hom_evolution(sd->hd);
  7437. else
  7438. clif_emotion(&sd->hd->bl, 4) ; //swt
  7439. }
  7440. return 0;
  7441. }
  7442. // [Zephyrus]
  7443. BUILDIN_FUNC(homunculus_shuffle)
  7444. {
  7445. TBL_PC *sd;
  7446. sd=script_rid2sd(st);
  7447. if( sd == NULL )
  7448. return 0;
  7449. if(merc_is_hom_active(sd->hd))
  7450. merc_hom_shuffle(sd->hd);
  7451. return 0;
  7452. }
  7453. //These two functions bring the eA MAPID_* class functionality to scripts.
  7454. BUILDIN_FUNC(eaclass)
  7455. {
  7456. int class_;
  7457. if( script_hasdata(st,2) )
  7458. class_ = script_getnum(st,2);
  7459. else {
  7460. TBL_PC *sd;
  7461. sd=script_rid2sd(st);
  7462. if (!sd) {
  7463. script_pushint(st,-1);
  7464. return 0;
  7465. }
  7466. class_ = sd->status.class_;
  7467. }
  7468. script_pushint(st,pc_jobid2mapid(class_));
  7469. return 0;
  7470. }
  7471. BUILDIN_FUNC(roclass)
  7472. {
  7473. int class_ =script_getnum(st,2);
  7474. int sex;
  7475. if( script_hasdata(st,3) )
  7476. sex = script_getnum(st,3);
  7477. else {
  7478. TBL_PC *sd;
  7479. if (st->rid && (sd=script_rid2sd(st)))
  7480. sex = sd->status.sex;
  7481. else
  7482. sex = 1; //Just use male when not found.
  7483. }
  7484. script_pushint(st,pc_mapid2jobid(class_, sex));
  7485. return 0;
  7486. }
  7487. /*==========================================
  7488. *携帯卵孵化機使用
  7489. *------------------------------------------*/
  7490. BUILDIN_FUNC(birthpet)
  7491. {
  7492. TBL_PC *sd;
  7493. sd=script_rid2sd(st);
  7494. if( sd == NULL )
  7495. return 0;
  7496. clif_sendegg(sd);
  7497. return 0;
  7498. }
  7499. /*==========================================
  7500. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7501. *------------------------------------------*/
  7502. BUILDIN_FUNC(resetlvl)
  7503. {
  7504. TBL_PC *sd;
  7505. int type=script_getnum(st,2);
  7506. sd=script_rid2sd(st);
  7507. if( sd == NULL )
  7508. return 0;
  7509. pc_resetlvl(sd,type);
  7510. return 0;
  7511. }
  7512. /*==========================================
  7513. * ステータスリセット
  7514. *------------------------------------------*/
  7515. BUILDIN_FUNC(resetstatus)
  7516. {
  7517. TBL_PC *sd;
  7518. sd=script_rid2sd(st);
  7519. pc_resetstate(sd);
  7520. return 0;
  7521. }
  7522. /*==========================================
  7523. * script command resetskill
  7524. *------------------------------------------*/
  7525. BUILDIN_FUNC(resetskill)
  7526. {
  7527. TBL_PC *sd;
  7528. sd=script_rid2sd(st);
  7529. pc_resetskill(sd,1);
  7530. return 0;
  7531. }
  7532. /*==========================================
  7533. * Counts total amount of skill points.
  7534. *------------------------------------------*/
  7535. BUILDIN_FUNC(skillpointcount)
  7536. {
  7537. TBL_PC *sd;
  7538. sd=script_rid2sd(st);
  7539. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7540. return 0;
  7541. }
  7542. /*==========================================
  7543. *
  7544. *------------------------------------------*/
  7545. BUILDIN_FUNC(changebase)
  7546. {
  7547. TBL_PC *sd=NULL;
  7548. int vclass;
  7549. if( script_hasdata(st,3) )
  7550. sd=map_id2sd(script_getnum(st,3));
  7551. else
  7552. sd=script_rid2sd(st);
  7553. if(sd == NULL)
  7554. return 0;
  7555. vclass = script_getnum(st,2);
  7556. if(vclass == JOB_WEDDING)
  7557. {
  7558. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7559. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7560. )
  7561. return 0;
  7562. }
  7563. if(!sd->disguise && vclass != sd->vd.class_) {
  7564. status_set_viewdata(&sd->bl, vclass);
  7565. //Updated client view. Base, Weapon and Cloth Colors.
  7566. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7567. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7568. if (sd->vd.cloth_color)
  7569. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7570. }
  7571. return 0;
  7572. }
  7573. /*==========================================
  7574. * 性別変換
  7575. *------------------------------------------*/
  7576. BUILDIN_FUNC(changesex)
  7577. {
  7578. TBL_PC *sd = NULL;
  7579. sd = script_rid2sd(st);
  7580. chrif_changesex(sd);
  7581. return 0;
  7582. }
  7583. /*==========================================
  7584. * Works like 'announce' but outputs in the common chat window
  7585. *------------------------------------------*/
  7586. BUILDIN_FUNC(globalmes)
  7587. {
  7588. struct block_list *bl = map_id2bl(st->oid);
  7589. struct npc_data *nd = (struct npc_data *)bl;
  7590. const char *name=NULL,*mes;
  7591. mes=script_getstr(st,2); // メッセージの取得
  7592. if(mes==NULL) return 0;
  7593. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7594. name=script_getstr(st,3);
  7595. } else {
  7596. name=nd->name;
  7597. }
  7598. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7599. return 0;
  7600. }
  7601. /////////////////////////////////////////////////////////////////////
  7602. // NPC waiting room (chat room)
  7603. //
  7604. /// Creates a waiting room (chat room) for this npc.
  7605. ///
  7606. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7607. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7608. /// waitingroom "<title>",<limit>,"<event>";
  7609. /// waitingroom "<title>",<limit>;
  7610. BUILDIN_FUNC(waitingroom)
  7611. {
  7612. struct npc_data* nd;
  7613. const char* title;
  7614. const char* ev = "";
  7615. int limit;
  7616. int trigger = 0;
  7617. int pub = 1;
  7618. title = script_getstr(st, 2);
  7619. limit = script_getnum(st, 3);
  7620. if( script_hasdata(st,5) )
  7621. {
  7622. struct script_data* last = script_getdata(st, 5);
  7623. get_val(st, last);
  7624. if( data_isstring(last) )
  7625. {// ,<trigger>,"<event>"
  7626. trigger = script_getnum(st, 4);
  7627. ev = script_getstr(st, 5);
  7628. }
  7629. else
  7630. {// ,"<event>",<trigger>
  7631. ev = script_getstr(st, 4);
  7632. trigger = script_getnum(st,5);
  7633. }
  7634. }
  7635. else if( script_hasdata(st,4) )
  7636. {// ,"<event>"
  7637. ev = script_getstr(st, 4);
  7638. trigger = limit;
  7639. }
  7640. nd = (struct npc_data *)map_id2bl(st->oid);
  7641. if( nd != NULL )
  7642. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7643. return 0;
  7644. }
  7645. /// Removes the waiting room of the current or target npc.
  7646. ///
  7647. /// delwaitingroom "<npc_name>";
  7648. /// delwaitingroom;
  7649. BUILDIN_FUNC(delwaitingroom)
  7650. {
  7651. struct npc_data* nd;
  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 )
  7657. chat_deletenpcchat(nd);
  7658. return 0;
  7659. }
  7660. /// Kicks all the players from the waiting room of the current or target npc.
  7661. ///
  7662. /// kickwaitingroomall "<npc_name>";
  7663. /// kickwaitingroomall;
  7664. BUILDIN_FUNC(waitingroomkickall)
  7665. {
  7666. struct npc_data* nd;
  7667. struct chat_data* cd;
  7668. if( script_hasdata(st,2) )
  7669. nd = npc_name2id(script_getstr(st,2));
  7670. else
  7671. nd = (struct npc_data *)map_id2bl(st->oid);
  7672. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7673. chat_npckickall(cd);
  7674. return 0;
  7675. }
  7676. /// Enables the waiting room event of the current or target npc.
  7677. ///
  7678. /// enablewaitingroomevent "<npc_name>";
  7679. /// enablewaitingroomevent;
  7680. BUILDIN_FUNC(enablewaitingroomevent)
  7681. {
  7682. struct npc_data* nd;
  7683. struct chat_data* cd;
  7684. if( script_hasdata(st,2) )
  7685. nd = npc_name2id(script_getstr(st, 2));
  7686. else
  7687. nd = (struct npc_data *)map_id2bl(st->oid);
  7688. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7689. chat_enableevent(cd);
  7690. return 0;
  7691. }
  7692. /// Disables the waiting room event of the current or target npc.
  7693. ///
  7694. /// disablewaitingroomevent "<npc_name>";
  7695. /// disablewaitingroomevent;
  7696. BUILDIN_FUNC(disablewaitingroomevent)
  7697. {
  7698. struct npc_data *nd;
  7699. struct chat_data *cd;
  7700. if( script_hasdata(st,2) )
  7701. nd = npc_name2id(script_getstr(st, 2));
  7702. else
  7703. nd = (struct npc_data *)map_id2bl(st->oid);
  7704. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7705. chat_disableevent(cd);
  7706. return 0;
  7707. }
  7708. /// Returns info on the waiting room of the current or target npc.
  7709. /// Returns -1 if the type unknown
  7710. /// <type>=0 : current number of users
  7711. /// <type>=1 : maximum number of users allowed
  7712. /// <type>=2 : the number of users that trigger the event
  7713. /// <type>=3 : if the trigger is disabled
  7714. /// <type>=4 : the title of the waiting room
  7715. /// <type>=5 : the password of the waiting room
  7716. /// <type>=16 : the name of the waiting room event
  7717. /// <type>=32 : if the waiting room is full
  7718. /// <type>=33 : if there are enough users to trigger the event
  7719. ///
  7720. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7721. /// getwaitingroomstate(<type>) -> <info>
  7722. BUILDIN_FUNC(getwaitingroomstate)
  7723. {
  7724. struct npc_data *nd;
  7725. struct chat_data *cd;
  7726. int type;
  7727. type = script_getnum(st,2);
  7728. if( script_hasdata(st,3) )
  7729. nd = npc_name2id(script_getstr(st, 3));
  7730. else
  7731. nd = (struct npc_data *)map_id2bl(st->oid);
  7732. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7733. {
  7734. script_pushint(st, -1);
  7735. return 0;
  7736. }
  7737. switch(type)
  7738. {
  7739. case 0: script_pushint(st, cd->users); break;
  7740. case 1: script_pushint(st, cd->limit); break;
  7741. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7742. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7743. case 4: script_pushstrcopy(st, cd->title); break;
  7744. case 5: script_pushstrcopy(st, cd->pass); break;
  7745. case 16: script_pushstrcopy(st, cd->npc_event);break;
  7746. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7747. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7748. default: script_pushint(st, -1); break;
  7749. }
  7750. return 0;
  7751. }
  7752. /// Warps the trigger or target amount of players to the target map and position.
  7753. /// Players are automatically removed from the waiting room.
  7754. /// Those waiting the longest will get warped first.
  7755. /// The target map can be "Random" for a random position in the current map,
  7756. /// and "SavePoint" for the savepoint map+position.
  7757. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7758. ///
  7759. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7760. /// The total number of teleported players is put into $@warpwaitingpcnum
  7761. ///
  7762. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7763. /// warpwaitingpc "<map name>",<x>,<y>;
  7764. BUILDIN_FUNC(warpwaitingpc)
  7765. {
  7766. int x;
  7767. int y;
  7768. int i;
  7769. int n;
  7770. const char* map_name;
  7771. struct npc_data* nd;
  7772. struct chat_data* cd;
  7773. TBL_PC* sd;
  7774. nd = (struct npc_data *)map_id2bl(st->oid);
  7775. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7776. return 0;
  7777. map_name = script_getstr(st,2);
  7778. x = script_getnum(st,3);
  7779. y = script_getnum(st,4);
  7780. n = cd->trigger&0x7f;
  7781. if( script_hasdata(st,5) )
  7782. n = script_getnum(st,5);
  7783. for( i = 0; i < n && cd->users > 0; i++ )
  7784. {
  7785. sd = cd->usersd[0];
  7786. if( sd == NULL )
  7787. {
  7788. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  7789. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  7790. continue;// Broken npc chat room?
  7791. }
  7792. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  7793. if( strcmp(map_name,"Random") == 0 )
  7794. pc_randomwarp(sd,3);
  7795. else if( strcmp(map_name,"SavePoint") == 0 )
  7796. {
  7797. if( map[sd->bl.m].flag.noteleport )
  7798. return 0;// can't teleport on this map
  7799. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  7800. }
  7801. else
  7802. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  7803. }
  7804. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  7805. return 0;
  7806. }
  7807. /////////////////////////////////////////////////////////////////////
  7808. // ...
  7809. //
  7810. /*==========================================
  7811. * RIDのアタッチ
  7812. *------------------------------------------*/
  7813. BUILDIN_FUNC(attachrid)
  7814. {
  7815. st->rid=script_getnum(st,2);
  7816. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  7817. return 0;
  7818. }
  7819. /*==========================================
  7820. * RIDのデタッチ
  7821. *------------------------------------------*/
  7822. BUILDIN_FUNC(detachrid)
  7823. {
  7824. st->rid=0;
  7825. return 0;
  7826. }
  7827. /*==========================================
  7828. * 存在チェック
  7829. *------------------------------------------*/
  7830. BUILDIN_FUNC(isloggedin)
  7831. {
  7832. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  7833. if (script_hasdata(st,3) && sd &&
  7834. sd->status.char_id != script_getnum(st,3))
  7835. sd = NULL;
  7836. push_val(st->stack,C_INT,sd!=NULL);
  7837. return 0;
  7838. }
  7839. /*==========================================
  7840. *
  7841. *------------------------------------------*/
  7842. BUILDIN_FUNC(setmapflagnosave)
  7843. {
  7844. int m,x,y;
  7845. unsigned short mapindex;
  7846. const char *str,*str2;
  7847. str=script_getstr(st,2);
  7848. str2=script_getstr(st,3);
  7849. x=script_getnum(st,4);
  7850. y=script_getnum(st,5);
  7851. m = map_mapname2mapid(str);
  7852. mapindex = mapindex_name2id(str2);
  7853. if(m >= 0 && mapindex) {
  7854. map[m].flag.nosave=1;
  7855. map[m].save.map=mapindex;
  7856. map[m].save.x=x;
  7857. map[m].save.y=y;
  7858. }
  7859. return 0;
  7860. }
  7861. BUILDIN_FUNC(getmapflag)
  7862. {
  7863. int m,i;
  7864. const char *str;
  7865. str=script_getstr(st,2);
  7866. i=script_getnum(st,3);
  7867. m = map_mapname2mapid(str);
  7868. if(m >= 0) {
  7869. switch(i) {
  7870. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  7871. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  7872. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  7873. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  7874. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  7875. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  7876. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  7877. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  7878. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  7879. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  7880. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  7881. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  7882. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  7883. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  7884. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  7885. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  7886. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  7887. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  7888. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  7889. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  7890. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  7891. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  7892. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  7893. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  7894. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  7895. case MF_INDOORS: script_pushint(st,map[m].flag.indoors); break;
  7896. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  7897. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  7898. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  7899. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  7900. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  7901. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  7902. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  7903. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  7904. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  7905. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  7906. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  7907. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  7908. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  7909. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  7910. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  7911. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  7912. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  7913. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  7914. }
  7915. }
  7916. return 0;
  7917. }
  7918. BUILDIN_FUNC(setmapflag)
  7919. {
  7920. int m,i;
  7921. const char *str;
  7922. const char *val=NULL;
  7923. str=script_getstr(st,2);
  7924. i=script_getnum(st,3);
  7925. if(script_hasdata(st,4)){
  7926. val=script_getstr(st,4);
  7927. }
  7928. m = map_mapname2mapid(str);
  7929. if(m >= 0) {
  7930. switch(i) {
  7931. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  7932. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  7933. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  7934. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  7935. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  7936. case MF_PVP: map[m].flag.pvp=1; break;
  7937. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  7938. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  7939. case MF_GVG: map[m].flag.gvg=1; break;
  7940. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  7941. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  7942. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  7943. case MF_NOTRADE: map[m].flag.notrade=1; break;
  7944. case MF_NODROP: map[m].flag.nodrop=1; break;
  7945. case MF_NOSKILL: map[m].flag.noskill=1; break;
  7946. case MF_NOWARP: map[m].flag.nowarp=1; break;
  7947. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  7948. case MF_SNOW: map[m].flag.snow=1; break;
  7949. case MF_CLOUDS: map[m].flag.clouds=1; break;
  7950. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  7951. case MF_FOG: map[m].flag.fog=1; break;
  7952. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  7953. case MF_SAKURA: map[m].flag.sakura=1; break;
  7954. case MF_LEAVES: map[m].flag.leaves=1; break;
  7955. case MF_RAIN: map[m].flag.rain=1; break;
  7956. case MF_INDOORS: map[m].flag.indoors=1; break;
  7957. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  7958. case MF_NOGO: map[m].flag.nogo=1; break;
  7959. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  7960. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  7961. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  7962. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  7963. case MF_NORETURN: map[m].flag.noreturn=1; break;
  7964. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  7965. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  7966. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  7967. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  7968. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7969. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7970. case MF_NOVENDING: map[m].flag.novending=1; break;
  7971. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  7972. case MF_NOCHAT: map[m].flag.nochat=1; break;
  7973. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  7974. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  7975. }
  7976. }
  7977. return 0;
  7978. }
  7979. BUILDIN_FUNC(removemapflag)
  7980. {
  7981. int m,i;
  7982. const char *str;
  7983. str=script_getstr(st,2);
  7984. i=script_getnum(st,3);
  7985. m = map_mapname2mapid(str);
  7986. if(m >= 0) {
  7987. switch(i) {
  7988. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  7989. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  7990. case MF_NOSAVE: map[m].flag.nosave=0; break;
  7991. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  7992. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  7993. case MF_PVP: map[m].flag.pvp=0; break;
  7994. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  7995. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  7996. case MF_GVG: map[m].flag.gvg=0; break;
  7997. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  7998. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  7999. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  8000. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  8001. case MF_NOTRADE: map[m].flag.notrade=0; break;
  8002. case MF_NODROP: map[m].flag.nodrop=0; break;
  8003. case MF_NOSKILL: map[m].flag.noskill=0; break;
  8004. case MF_NOWARP: map[m].flag.nowarp=0; break;
  8005. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  8006. case MF_SNOW: map[m].flag.snow=0; break;
  8007. case MF_CLOUDS: map[m].flag.clouds=0; break;
  8008. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  8009. case MF_FOG: map[m].flag.fog=0; break;
  8010. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8011. case MF_SAKURA: map[m].flag.sakura=0; break;
  8012. case MF_LEAVES: map[m].flag.leaves=0; break;
  8013. case MF_RAIN: map[m].flag.rain=0; break;
  8014. case MF_INDOORS: map[m].flag.indoors=0; break;
  8015. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8016. case MF_NOGO: map[m].flag.nogo=0; break;
  8017. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8018. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8019. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8020. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8021. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8022. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8023. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8024. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8025. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8026. case MF_JEXP: map[m].jexp=100; break;
  8027. case MF_BEXP: map[m].bexp=100; break;
  8028. case MF_NOVENDING: map[m].flag.novending=0; break;
  8029. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8030. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8031. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8032. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8033. }
  8034. }
  8035. return 0;
  8036. }
  8037. BUILDIN_FUNC(pvpon)
  8038. {
  8039. int m;
  8040. const char *str;
  8041. TBL_PC* sd = NULL;
  8042. struct s_mapiterator* iter;
  8043. str = script_getstr(st,2);
  8044. m = map_mapname2mapid(str);
  8045. if( m < 0 || map[m].flag.pvp )
  8046. return 0; // nothing to do
  8047. map[m].flag.pvp = 1;
  8048. clif_send0199(m,1);
  8049. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8050. return 0;
  8051. iter = mapit_getallusers();
  8052. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8053. {
  8054. if( sd->bl.m != m || sd->pvp_timer != -1 )
  8055. continue; // not applicable
  8056. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8057. sd->pvp_rank = 0;
  8058. sd->pvp_lastusers = 0;
  8059. sd->pvp_point = 5;
  8060. sd->pvp_won = 0;
  8061. sd->pvp_lost = 0;
  8062. }
  8063. mapit_free(iter);
  8064. return 0;
  8065. }
  8066. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8067. {
  8068. TBL_PC* sd = (TBL_PC*)bl;
  8069. clif_pvpset(sd, 0, 0, 2);
  8070. if (sd->pvp_timer != -1) {
  8071. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8072. sd->pvp_timer = INVALID_TIMER;
  8073. }
  8074. return 0;
  8075. }
  8076. BUILDIN_FUNC(pvpoff)
  8077. {
  8078. int m;
  8079. const char *str;
  8080. str=script_getstr(st,2);
  8081. m = map_mapname2mapid(str);
  8082. if(m < 0 || !map[m].flag.pvp)
  8083. return 0; //fixed Lupus
  8084. map[m].flag.pvp = 0;
  8085. clif_send0199(m,0);
  8086. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8087. return 0;
  8088. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8089. return 0;
  8090. }
  8091. BUILDIN_FUNC(gvgon)
  8092. {
  8093. int m;
  8094. const char *str;
  8095. str=script_getstr(st,2);
  8096. m = map_mapname2mapid(str);
  8097. if(m >= 0 && !map[m].flag.gvg) {
  8098. map[m].flag.gvg = 1;
  8099. clif_send0199(m,3);
  8100. }
  8101. return 0;
  8102. }
  8103. BUILDIN_FUNC(gvgoff)
  8104. {
  8105. int m;
  8106. const char *str;
  8107. str=script_getstr(st,2);
  8108. m = map_mapname2mapid(str);
  8109. if(m >= 0 && map[m].flag.gvg) {
  8110. map[m].flag.gvg = 0;
  8111. clif_send0199(m,0);
  8112. }
  8113. return 0;
  8114. }
  8115. /*==========================================
  8116. * Shows an emoticon on top of the player/npc
  8117. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8118. *------------------------------------------*/
  8119. //Optional second parameter added by [Skotlex]
  8120. BUILDIN_FUNC(emotion)
  8121. {
  8122. int type;
  8123. int player=0;
  8124. type=script_getnum(st,2);
  8125. if(type < 0 || type > 100)
  8126. return 0;
  8127. if( script_hasdata(st,3) )
  8128. player=script_getnum(st,3);
  8129. if (player) {
  8130. TBL_PC *sd = script_rid2sd(st);
  8131. if (sd)
  8132. clif_emotion(&sd->bl,type);
  8133. } else
  8134. clif_emotion(map_id2bl(st->oid),type);
  8135. return 0;
  8136. }
  8137. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8138. {
  8139. int m=va_arg(ap,int);
  8140. int g_id=va_arg(ap,int);
  8141. int flag=va_arg(ap,int);
  8142. if(!sd || sd->bl.m != m)
  8143. return 0;
  8144. if(
  8145. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8146. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8147. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8148. )
  8149. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8150. return 1;
  8151. }
  8152. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8153. {
  8154. struct mob_data *md=(struct mob_data *)bl;
  8155. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8156. status_kill(bl);
  8157. return 0;
  8158. }
  8159. BUILDIN_FUNC(maprespawnguildid)
  8160. {
  8161. const char *mapname=script_getstr(st,2);
  8162. int g_id=script_getnum(st,3);
  8163. int flag=script_getnum(st,4);
  8164. int m=map_mapname2mapid(mapname);
  8165. if(m == -1)
  8166. return 0;
  8167. //Catch ALL players (in case some are 'between maps' on execution time)
  8168. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8169. if (flag&4) //Remove script mobs.
  8170. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8171. return 0;
  8172. }
  8173. BUILDIN_FUNC(agitstart)
  8174. {
  8175. if(agit_flag==1) return 0; // Agit already Start.
  8176. agit_flag=1;
  8177. guild_agit_start();
  8178. return 0;
  8179. }
  8180. BUILDIN_FUNC(agitend)
  8181. {
  8182. if(agit_flag==0) return 0; // Agit already End.
  8183. agit_flag=0;
  8184. guild_agit_end();
  8185. return 0;
  8186. }
  8187. BUILDIN_FUNC(agitstart2)
  8188. {
  8189. if(agit2_flag==1) return 0; // Agit2 already Start.
  8190. agit2_flag=1;
  8191. guild_agit2_start();
  8192. return 0;
  8193. }
  8194. BUILDIN_FUNC(agitend2)
  8195. {
  8196. if(agit2_flag==0) return 0; // Agit2 already End.
  8197. agit2_flag=0;
  8198. guild_agit2_end();
  8199. return 0;
  8200. }
  8201. /*==========================================
  8202. * Returns whether woe is on or off. // choice script
  8203. *------------------------------------------*/
  8204. BUILDIN_FUNC(agitcheck)
  8205. {
  8206. script_pushint(st,agit_flag);
  8207. return 0;
  8208. }
  8209. /*==========================================
  8210. * Returns whether woese is on or off. // choice script
  8211. *------------------------------------------*/
  8212. BUILDIN_FUNC(agitcheck2)
  8213. {
  8214. script_pushint(st,agit2_flag);
  8215. return 0;
  8216. }
  8217. /// Sets the guild_id of this npc.
  8218. ///
  8219. /// flagemblem <guild_id>;
  8220. BUILDIN_FUNC(flagemblem)
  8221. {
  8222. TBL_NPC* nd;
  8223. int g_id=script_getnum(st,2);
  8224. if(g_id < 0) return 0;
  8225. nd = (TBL_NPC*)map_id2nd(st->oid);
  8226. if( nd == NULL )
  8227. {
  8228. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8229. }
  8230. else if( nd->subtype != SCRIPT )
  8231. {
  8232. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8233. }
  8234. else
  8235. {
  8236. nd->u.scr.guild_id = g_id;
  8237. clif_guild_emblem_area(&nd->bl);
  8238. }
  8239. return 0;
  8240. }
  8241. BUILDIN_FUNC(getcastlename)
  8242. {
  8243. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8244. struct guild_castle* gc = guild_mapname2gc(mapname);
  8245. const char* name = (gc) ? gc->castle_name : "";
  8246. script_pushstrcopy(st,name);
  8247. return 0;
  8248. }
  8249. BUILDIN_FUNC(getcastledata)
  8250. {
  8251. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8252. int index = script_getnum(st,3);
  8253. struct guild_castle* gc = guild_mapname2gc(mapname);
  8254. if(script_hasdata(st,4) && index==0 && gc) {
  8255. const char* event = script_getstr(st,4);
  8256. check_event(st, event);
  8257. guild_addcastleinfoevent(gc->castle_id,17,event);
  8258. }
  8259. if(gc){
  8260. switch(index){
  8261. case 0: {
  8262. int i;
  8263. for(i=1;i<18;i++) // Initialize[AgitInit]
  8264. guild_castledataload(gc->castle_id,i);
  8265. } break;
  8266. case 1:
  8267. script_pushint(st,gc->guild_id); break;
  8268. case 2:
  8269. script_pushint(st,gc->economy); break;
  8270. case 3:
  8271. script_pushint(st,gc->defense); break;
  8272. case 4:
  8273. script_pushint(st,gc->triggerE); break;
  8274. case 5:
  8275. script_pushint(st,gc->triggerD); break;
  8276. case 6:
  8277. script_pushint(st,gc->nextTime); break;
  8278. case 7:
  8279. script_pushint(st,gc->payTime); break;
  8280. case 8:
  8281. script_pushint(st,gc->createTime); break;
  8282. case 9:
  8283. script_pushint(st,gc->visibleC); break;
  8284. case 10:
  8285. case 11:
  8286. case 12:
  8287. case 13:
  8288. case 14:
  8289. case 15:
  8290. case 16:
  8291. case 17:
  8292. script_pushint(st,gc->guardian[index-10].visible); break;
  8293. default:
  8294. script_pushint(st,0); break;
  8295. }
  8296. return 0;
  8297. }
  8298. script_pushint(st,0);
  8299. return 0;
  8300. }
  8301. BUILDIN_FUNC(setcastledata)
  8302. {
  8303. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8304. int index = script_getnum(st,3);
  8305. int value = script_getnum(st,4);
  8306. struct guild_castle* gc = guild_mapname2gc(mapname);
  8307. if(gc) {
  8308. // Save Data byself First
  8309. switch(index){
  8310. case 1:
  8311. gc->guild_id = value; break;
  8312. case 2:
  8313. gc->economy = value; break;
  8314. case 3:
  8315. gc->defense = value; break;
  8316. case 4:
  8317. gc->triggerE = value; break;
  8318. case 5:
  8319. gc->triggerD = value; break;
  8320. case 6:
  8321. gc->nextTime = value; break;
  8322. case 7:
  8323. gc->payTime = value; break;
  8324. case 8:
  8325. gc->createTime = value; break;
  8326. case 9:
  8327. gc->visibleC = value; break;
  8328. case 10:
  8329. case 11:
  8330. case 12:
  8331. case 13:
  8332. case 14:
  8333. case 15:
  8334. case 16:
  8335. case 17:
  8336. gc->guardian[index-10].visible = value; break;
  8337. default:
  8338. return 0;
  8339. }
  8340. guild_castledatasave(gc->castle_id,index,value);
  8341. }
  8342. return 0;
  8343. }
  8344. /* =====================================================================
  8345. * ギルド情報を要求する
  8346. * ---------------------------------------------------------------------*/
  8347. BUILDIN_FUNC(requestguildinfo)
  8348. {
  8349. int guild_id=script_getnum(st,2);
  8350. const char *event=NULL;
  8351. if( script_hasdata(st,3) ){
  8352. event=script_getstr(st,3);
  8353. check_event(st, event);
  8354. }
  8355. if(guild_id>0)
  8356. guild_npc_request_info(guild_id,event);
  8357. return 0;
  8358. }
  8359. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8360. /// getequipcardcnt(<equipment slot>);
  8361. BUILDIN_FUNC(getequipcardcnt)
  8362. {
  8363. int i=-1,j,num;
  8364. TBL_PC *sd;
  8365. int count;
  8366. num=script_getnum(st,2);
  8367. sd=script_rid2sd(st);
  8368. if (num > 0 && num <= ARRAYLENGTH(equip))
  8369. i=pc_checkequip(sd,equip[num-1]);
  8370. if (i < 0) {
  8371. script_pushint(st,0);
  8372. return 0;
  8373. }
  8374. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8375. {
  8376. script_pushint(st,0);
  8377. return 0;
  8378. }
  8379. count = 0;
  8380. for( j = 0; j < MAX_SLOTS; j++ )
  8381. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  8382. count++;
  8383. script_pushint(st,count);
  8384. return 0;
  8385. }
  8386. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  8387. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  8388. /// successremovecards <slot>;
  8389. BUILDIN_FUNC(successremovecards)
  8390. {
  8391. int i=-1,j,c,cardflag=0;
  8392. TBL_PC* sd = script_rid2sd(st);
  8393. int num = script_getnum(st,2);
  8394. if (num > 0 && num <= ARRAYLENGTH(equip))
  8395. i=pc_checkequip(sd,equip[num-1]);
  8396. if (i < 0) {
  8397. return 0;
  8398. }
  8399. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8400. return 0;
  8401. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8402. {
  8403. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8404. {// extract this card from the item
  8405. int flag;
  8406. struct item item_tmp;
  8407. cardflag = 1;
  8408. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8409. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8410. item_tmp.attribute=0,item_tmp.expire_time=0;
  8411. for (j = 0; j < MAX_SLOTS; j++)
  8412. item_tmp.card[j]=0;
  8413. //Logs items, got from (N)PC scripts [Lupus]
  8414. if(log_config.enable_logs&0x40)
  8415. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8416. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8417. clif_additem(sd,0,0,flag);
  8418. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8419. }
  8420. }
  8421. }
  8422. if(cardflag == 1)
  8423. { // カードを取り除いたアイテム所得
  8424. int flag;
  8425. struct item item_tmp;
  8426. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8427. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8428. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=0;
  8429. for (j = 0; j < MAX_SLOTS; j++)
  8430. item_tmp.card[j]=0;
  8431. //Logs items, got from (N)PC scripts [Lupus]
  8432. if(log_config.enable_logs&0x40)
  8433. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8434. pc_delitem(sd,i,1,0);
  8435. //Logs items, got from (N)PC scripts [Lupus]
  8436. if(log_config.enable_logs&0x40)
  8437. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8438. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8439. clif_additem(sd,0,0,flag);
  8440. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8441. }
  8442. clif_misceffect(&sd->bl,3);
  8443. }
  8444. return 0;
  8445. }
  8446. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  8447. /// failedremovecards <slot>, <type>;
  8448. /// <type>=0 : will destroy both the item and the cards.
  8449. /// <type>=1 : will keep the item, but destroy the cards.
  8450. /// <type>=2 : will keep the cards, but destroy the item.
  8451. /// <type>=? : will just display the failure effect.
  8452. BUILDIN_FUNC(failedremovecards)
  8453. {
  8454. int i=-1,j,c,cardflag=0;
  8455. TBL_PC* sd = script_rid2sd(st);
  8456. int num = script_getnum(st,2);
  8457. int typefail = script_getnum(st,3);
  8458. if (num > 0 && num <= ARRAYLENGTH(equip))
  8459. i=pc_checkequip(sd,equip[num-1]);
  8460. if (i < 0)
  8461. return 0;
  8462. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8463. return 0;
  8464. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8465. {
  8466. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8467. {
  8468. cardflag = 1;
  8469. if(typefail == 2)
  8470. {// add cards to inventory, clear
  8471. int flag;
  8472. struct item item_tmp;
  8473. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8474. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8475. item_tmp.attribute=0,item_tmp.expire_time=0;
  8476. for (j = 0; j < MAX_SLOTS; j++)
  8477. item_tmp.card[j]=0;
  8478. //Logs items, got from (N)PC scripts [Lupus]
  8479. if(log_config.enable_logs&0x40)
  8480. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8481. if((flag=pc_additem(sd,&item_tmp,1))){
  8482. clif_additem(sd,0,0,flag);
  8483. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8484. }
  8485. }
  8486. }
  8487. }
  8488. if(cardflag == 1)
  8489. {
  8490. if(typefail == 0 || typefail == 2){ // 武具損失
  8491. //Logs items, got from (N)PC scripts [Lupus]
  8492. if(log_config.enable_logs&0x40)
  8493. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8494. pc_delitem(sd,i,1,0);
  8495. }
  8496. if(typefail == 1){ // カードのみ損失(武具を返す)
  8497. int flag;
  8498. struct item item_tmp;
  8499. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8500. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8501. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=0;
  8502. //Logs items, got from (N)PC scripts [Lupus]
  8503. if(log_config.enable_logs&0x40)
  8504. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8505. for (j = 0; j < MAX_SLOTS; j++)
  8506. item_tmp.card[j]=0;
  8507. pc_delitem(sd,i,1,0);
  8508. //Logs items, got from (N)PC scripts [Lupus]
  8509. if(log_config.enable_logs&0x40)
  8510. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8511. if((flag=pc_additem(sd,&item_tmp,1))){
  8512. clif_additem(sd,0,0,flag);
  8513. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8514. }
  8515. }
  8516. clif_misceffect(&sd->bl,2);
  8517. }
  8518. return 0;
  8519. }
  8520. /* ================================================================
  8521. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8522. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8523. * improved by [Lance]
  8524. * ================================================================*/
  8525. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8526. {
  8527. int x,y,m,check_val=0,check_ID=0,i=0;
  8528. struct guild *g = NULL;
  8529. struct party_data *p = NULL;
  8530. const char *str;
  8531. const char *mapname;
  8532. unsigned int index;
  8533. mapname=script_getstr(st,2);
  8534. str=script_getstr(st,3);
  8535. x=script_getnum(st,4);
  8536. y=script_getnum(st,5);
  8537. if(script_hasdata(st,7)){
  8538. check_val=script_getnum(st,6);
  8539. check_ID=script_getnum(st,7);
  8540. }
  8541. if((m=map_mapname2mapid(mapname))< 0)
  8542. return 0;
  8543. if(!(index=mapindex_name2id(str)))
  8544. return 0;
  8545. switch(check_val){
  8546. case 1:
  8547. g = guild_search(check_ID);
  8548. if (g){
  8549. for( i=0; i < g->max_member; i++)
  8550. {
  8551. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8552. pc_setpos(g->member[i].sd,index,x,y,3);
  8553. }
  8554. }
  8555. }
  8556. break;
  8557. case 2:
  8558. p = party_search(check_ID);
  8559. if(p){
  8560. for(i=0;i<MAX_PARTY; i++){
  8561. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8562. pc_setpos(p->data[i].sd,index,x,y,3);
  8563. }
  8564. }
  8565. }
  8566. break;
  8567. default:
  8568. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8569. break;
  8570. }
  8571. return 0;
  8572. }
  8573. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8574. {
  8575. char *event=va_arg(ap,char *);
  8576. struct mob_data *md = ((struct mob_data *)bl);
  8577. if(strcmp(event,md->npc_event)==0 && md->status.hp > 0)
  8578. return 1;
  8579. return 0;
  8580. }
  8581. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8582. {
  8583. const char *mapname,*event;
  8584. int m;
  8585. mapname=script_getstr(st,2);
  8586. event=script_getstr(st,3);
  8587. check_event(st, event);
  8588. if( (m=map_mapname2mapid(mapname))<0 ) {
  8589. script_pushint(st,-1);
  8590. return 0;
  8591. }
  8592. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8593. return 0;
  8594. }
  8595. BUILDIN_FUNC(marriage)
  8596. {
  8597. const char *partner=script_getstr(st,2);
  8598. TBL_PC *sd=script_rid2sd(st);
  8599. TBL_PC *p_sd=map_nick2sd(partner);
  8600. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8601. script_pushint(st,0);
  8602. return 0;
  8603. }
  8604. script_pushint(st,1);
  8605. return 0;
  8606. }
  8607. BUILDIN_FUNC(wedding_effect)
  8608. {
  8609. TBL_PC *sd=script_rid2sd(st);
  8610. struct block_list *bl;
  8611. if(sd==NULL) {
  8612. bl=map_id2bl(st->oid);
  8613. } else
  8614. bl=&sd->bl;
  8615. clif_wedding_effect(bl);
  8616. return 0;
  8617. }
  8618. BUILDIN_FUNC(divorce)
  8619. {
  8620. TBL_PC *sd=script_rid2sd(st);
  8621. if(sd==NULL || pc_divorce(sd) < 0){
  8622. script_pushint(st,0);
  8623. return 0;
  8624. }
  8625. script_pushint(st,1);
  8626. return 0;
  8627. }
  8628. BUILDIN_FUNC(ispartneron)
  8629. {
  8630. TBL_PC *sd=script_rid2sd(st);
  8631. TBL_PC *p_sd=NULL;
  8632. if(sd==NULL || !pc_ismarried(sd) ||
  8633. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8634. script_pushint(st,0);
  8635. return 0;
  8636. }
  8637. script_pushint(st,1);
  8638. return 0;
  8639. }
  8640. BUILDIN_FUNC(getpartnerid)
  8641. {
  8642. TBL_PC *sd=script_rid2sd(st);
  8643. if (sd == NULL) {
  8644. script_pushint(st,0);
  8645. return 0;
  8646. }
  8647. script_pushint(st,sd->status.partner_id);
  8648. return 0;
  8649. }
  8650. BUILDIN_FUNC(getchildid)
  8651. {
  8652. TBL_PC *sd=script_rid2sd(st);
  8653. if (sd == NULL) {
  8654. script_pushint(st,0);
  8655. return 0;
  8656. }
  8657. script_pushint(st,sd->status.child);
  8658. return 0;
  8659. }
  8660. BUILDIN_FUNC(getmotherid)
  8661. {
  8662. TBL_PC *sd=script_rid2sd(st);
  8663. if (sd == NULL) {
  8664. script_pushint(st,0);
  8665. return 0;
  8666. }
  8667. script_pushint(st,sd->status.mother);
  8668. return 0;
  8669. }
  8670. BUILDIN_FUNC(getfatherid)
  8671. {
  8672. TBL_PC *sd=script_rid2sd(st);
  8673. if (sd == NULL) {
  8674. script_pushint(st,0);
  8675. return 0;
  8676. }
  8677. script_pushint(st,sd->status.father);
  8678. return 0;
  8679. }
  8680. BUILDIN_FUNC(warppartner)
  8681. {
  8682. int x,y;
  8683. unsigned short mapindex;
  8684. const char *str;
  8685. TBL_PC *sd=script_rid2sd(st);
  8686. TBL_PC *p_sd=NULL;
  8687. if(sd==NULL || !pc_ismarried(sd) ||
  8688. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8689. script_pushint(st,0);
  8690. return 0;
  8691. }
  8692. str=script_getstr(st,2);
  8693. x=script_getnum(st,3);
  8694. y=script_getnum(st,4);
  8695. mapindex = mapindex_name2id(str);
  8696. if (mapindex) {
  8697. pc_setpos(p_sd,mapindex,x,y,0);
  8698. script_pushint(st,1);
  8699. } else
  8700. script_pushint(st,0);
  8701. return 0;
  8702. }
  8703. /*================================================
  8704. * Script for Displaying MOB Information [Valaris]
  8705. *------------------------------------------------*/
  8706. BUILDIN_FUNC(strmobinfo)
  8707. {
  8708. int num=script_getnum(st,2);
  8709. int class_=script_getnum(st,3);
  8710. if(!mobdb_checkid(class_))
  8711. {
  8712. script_pushint(st,0);
  8713. return 0;
  8714. }
  8715. switch (num) {
  8716. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  8717. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  8718. case 3: script_pushint(st,mob_db(class_)->lv); break;
  8719. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  8720. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  8721. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  8722. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  8723. default:
  8724. script_pushint(st,0);
  8725. break;
  8726. }
  8727. return 0;
  8728. }
  8729. /*==========================================
  8730. * Summon guardians [Valaris]
  8731. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  8732. *------------------------------------------*/
  8733. BUILDIN_FUNC(guardian)
  8734. {
  8735. int class_=0,x=0,y=0,guardian=0;
  8736. const char *str,*map,*evt="";
  8737. struct script_data *data;
  8738. bool has_index = false;
  8739. map =script_getstr(st,2);
  8740. x =script_getnum(st,3);
  8741. y =script_getnum(st,4);
  8742. str =script_getstr(st,5);
  8743. class_=script_getnum(st,6);
  8744. if( script_hasdata(st,8) )
  8745. {// "<event label>",<guardian index>
  8746. evt=script_getstr(st,7);
  8747. guardian=script_getnum(st,8);
  8748. has_index = true;
  8749. } else if( script_hasdata(st,7) ){
  8750. data=script_getdata(st,7);
  8751. get_val(st,data);
  8752. if( data_isstring(data) )
  8753. {// "<event label>"
  8754. evt=script_getstr(st,7);
  8755. } else if( data_isint(data) )
  8756. {// <guardian index>
  8757. guardian=script_getnum(st,7);
  8758. has_index = true;
  8759. } else {
  8760. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  8761. script_reportdata(data);
  8762. return 1;
  8763. }
  8764. }
  8765. check_event(st, evt);
  8766. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  8767. return 0;
  8768. }
  8769. /*==========================================
  8770. * Invisible Walls [Zephyrus]
  8771. *------------------------------------------*/
  8772. BUILDIN_FUNC(setwall)
  8773. {
  8774. const char *map, *name;
  8775. int x, y, m, size, dir;
  8776. bool shootable;
  8777. map = script_getstr(st,2);
  8778. x = script_getnum(st,3);
  8779. y = script_getnum(st,4);
  8780. size = script_getnum(st,5);
  8781. dir = script_getnum(st,6);
  8782. shootable = script_getnum(st,7);
  8783. name = script_getstr(st,8);
  8784. if( (m = map_mapname2mapid(map)) < 0 )
  8785. return 0; // Invalid Map
  8786. map_iwall_set(m, x, y, size, dir, shootable, name);
  8787. return 0;
  8788. }
  8789. BUILDIN_FUNC(delwall)
  8790. {
  8791. const char *name = script_getstr(st,2);
  8792. map_iwall_remove(name);
  8793. return 0;
  8794. }
  8795. /// Retrieves various information about the specified guardian.
  8796. ///
  8797. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  8798. /// type: 0 - whether it is deployed or not
  8799. /// 1 - maximum hp
  8800. /// 2 - current hp
  8801. ///
  8802. BUILDIN_FUNC(guardianinfo)
  8803. {
  8804. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8805. int id = script_getnum(st,3);
  8806. int type = script_getnum(st,4);
  8807. struct guild_castle* gc = guild_mapname2gc(mapname);
  8808. struct mob_data* gd;
  8809. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  8810. {
  8811. script_pushint(st,-1);
  8812. return 0;
  8813. }
  8814. if( type == 0 )
  8815. script_pushint(st, gc->guardian[id].visible);
  8816. else
  8817. if( !gc->guardian[id].visible )
  8818. script_pushint(st,-1);
  8819. else
  8820. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  8821. script_pushint(st,-1);
  8822. else
  8823. {
  8824. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  8825. else if( type == 2 ) script_pushint(st,gd->status.hp);
  8826. else
  8827. script_pushint(st,-1);
  8828. }
  8829. return 0;
  8830. }
  8831. /*==========================================
  8832. * IDからItem名
  8833. *------------------------------------------*/
  8834. BUILDIN_FUNC(getitemname)
  8835. {
  8836. int item_id=0;
  8837. struct item_data *i_data;
  8838. char *item_name;
  8839. struct script_data *data;
  8840. data=script_getdata(st,2);
  8841. get_val(st,data);
  8842. if( data_isstring(data) ){
  8843. const char *name=conv_str(st,data);
  8844. struct item_data *item_data = itemdb_searchname(name);
  8845. if( item_data )
  8846. item_id=item_data->nameid;
  8847. }else
  8848. item_id=conv_num(st,data);
  8849. i_data = itemdb_exists(item_id);
  8850. if (i_data == NULL)
  8851. {
  8852. script_pushconststr(st,"null");
  8853. return 0;
  8854. }
  8855. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  8856. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  8857. script_pushstr(st,item_name);
  8858. return 0;
  8859. }
  8860. /*==========================================
  8861. * Returns number of slots an item has. [Skotlex]
  8862. *------------------------------------------*/
  8863. BUILDIN_FUNC(getitemslots)
  8864. {
  8865. int item_id;
  8866. struct item_data *i_data;
  8867. item_id=script_getnum(st,2);
  8868. i_data = itemdb_exists(item_id);
  8869. if (i_data)
  8870. script_pushint(st,i_data->slot);
  8871. else
  8872. script_pushint(st,-1);
  8873. return 0;
  8874. }
  8875. /*==========================================
  8876. * Returns some values of an item [Lupus]
  8877. * Price, Weight, etc...
  8878. getiteminfo(itemID,n), where n
  8879. 0 value_buy;
  8880. 1 value_sell;
  8881. 2 type;
  8882. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8883. if = 0, then monsters don't drop it at all (rare or a quest item)
  8884. if = 10000, then this item is sold in NPC shops only
  8885. 4 sex;
  8886. 5 equip;
  8887. 6 weight;
  8888. 7 atk;
  8889. 8 def;
  8890. 9 range;
  8891. 10 slot;
  8892. 11 look;
  8893. 12 elv;
  8894. 13 wlv;
  8895. 14 view id
  8896. *------------------------------------------*/
  8897. BUILDIN_FUNC(getiteminfo)
  8898. {
  8899. int item_id,n;
  8900. int *item_arr;
  8901. struct item_data *i_data;
  8902. item_id = script_getnum(st,2);
  8903. n = script_getnum(st,3);
  8904. i_data = itemdb_exists(item_id);
  8905. if (i_data && n>=0 && n<14) {
  8906. item_arr = (int*)&i_data->value_buy;
  8907. script_pushint(st,item_arr[n]);
  8908. } else
  8909. script_pushint(st,-1);
  8910. return 0;
  8911. }
  8912. /*==========================================
  8913. * Set some values of an item [Lupus]
  8914. * Price, Weight, etc...
  8915. setiteminfo(itemID,n,Value), where n
  8916. 0 value_buy;
  8917. 1 value_sell;
  8918. 2 type;
  8919. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8920. if = 0, then monsters don't drop it at all (rare or a quest item)
  8921. if = 10000, then this item is sold in NPC shops only
  8922. 4 sex;
  8923. 5 equip;
  8924. 6 weight;
  8925. 7 atk;
  8926. 8 def;
  8927. 9 range;
  8928. 10 slot;
  8929. 11 look;
  8930. 12 elv;
  8931. 13 wlv;
  8932. 14 view id
  8933. * Returns Value or -1 if the wrong field's been set
  8934. *------------------------------------------*/
  8935. BUILDIN_FUNC(setiteminfo)
  8936. {
  8937. int item_id,n,value;
  8938. int *item_arr;
  8939. struct item_data *i_data;
  8940. item_id = script_getnum(st,2);
  8941. n = script_getnum(st,3);
  8942. value = script_getnum(st,4);
  8943. i_data = itemdb_exists(item_id);
  8944. if (i_data && n>=0 && n<14) {
  8945. item_arr = (int*)&i_data->value_buy;
  8946. item_arr[n] = value;
  8947. script_pushint(st,value);
  8948. } else
  8949. script_pushint(st,-1);
  8950. return 0;
  8951. }
  8952. /*==========================================
  8953. * Returns value from equipped item slot n [Lupus]
  8954. getequipcardid(num,slot)
  8955. where
  8956. num = eqip position slot
  8957. slot = 0,1,2,3 (Card Slot N)
  8958. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  8959. it's useful when you want to check item cards or if it's signed
  8960. Useful for such quests as "Sign this refined item with players name" etc
  8961. Hat[0] +4 -> Player's Hat[0] +4
  8962. *------------------------------------------*/
  8963. BUILDIN_FUNC(getequipcardid)
  8964. {
  8965. int i=-1,num,slot;
  8966. TBL_PC *sd;
  8967. num=script_getnum(st,2);
  8968. slot=script_getnum(st,3);
  8969. sd=script_rid2sd(st);
  8970. if (num > 0 && num <= ARRAYLENGTH(equip))
  8971. i=pc_checkequip(sd,equip[num-1]);
  8972. if(i >= 0 && slot>=0 && slot<4)
  8973. script_pushint(st,sd->status.inventory[i].card[slot]);
  8974. else
  8975. script_pushint(st,0);
  8976. return 0;
  8977. }
  8978. /*==========================================
  8979. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  8980. *------------------------------------------*/
  8981. BUILDIN_FUNC(petskillbonus)
  8982. {
  8983. struct pet_data *pd;
  8984. TBL_PC *sd=script_rid2sd(st);
  8985. if(sd==NULL || sd->pd==NULL)
  8986. return 0;
  8987. pd=sd->pd;
  8988. if (pd->bonus)
  8989. { //Clear previous bonus
  8990. if (pd->bonus->timer != -1)
  8991. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  8992. } else //init
  8993. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  8994. pd->bonus->type=script_getnum(st,2);
  8995. pd->bonus->val=script_getnum(st,3);
  8996. pd->bonus->duration=script_getnum(st,4);
  8997. pd->bonus->delay=script_getnum(st,5);
  8998. if (pd->state.skillbonus == 1)
  8999. pd->state.skillbonus=0; // waiting state
  9000. // wait for timer to start
  9001. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9002. pd->bonus->timer = INVALID_TIMER;
  9003. else
  9004. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9005. return 0;
  9006. }
  9007. /*==========================================
  9008. * pet looting [Valaris] //Rewritten by [Skotlex]
  9009. *------------------------------------------*/
  9010. BUILDIN_FUNC(petloot)
  9011. {
  9012. int max;
  9013. struct pet_data *pd;
  9014. TBL_PC *sd=script_rid2sd(st);
  9015. if(sd==NULL || sd->pd==NULL)
  9016. return 0;
  9017. max=script_getnum(st,2);
  9018. if(max < 1)
  9019. max = 1; //Let'em loot at least 1 item.
  9020. else if (max > MAX_PETLOOT_SIZE)
  9021. max = MAX_PETLOOT_SIZE;
  9022. pd = sd->pd;
  9023. if (pd->loot != NULL)
  9024. { //Release whatever was there already and reallocate memory
  9025. pet_lootitem_drop(pd, pd->msd);
  9026. aFree(pd->loot->item);
  9027. }
  9028. else
  9029. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9030. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9031. pd->loot->max=max;
  9032. pd->loot->count = 0;
  9033. pd->loot->weight = 0;
  9034. return 0;
  9035. }
  9036. /*==========================================
  9037. * PCの所持品情報読み取り
  9038. *------------------------------------------*/
  9039. BUILDIN_FUNC(getinventorylist)
  9040. {
  9041. TBL_PC *sd=script_rid2sd(st);
  9042. char card_var[NAME_LENGTH];
  9043. int i,j=0,k;
  9044. if(!sd) return 0;
  9045. for(i=0;i<MAX_INVENTORY;i++){
  9046. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9047. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9048. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9049. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9050. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9051. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9052. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9053. for (k = 0; k < MAX_SLOTS; k++)
  9054. {
  9055. sprintf(card_var, "@inventorylist_card%d",k+1);
  9056. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9057. }
  9058. j++;
  9059. }
  9060. }
  9061. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9062. return 0;
  9063. }
  9064. BUILDIN_FUNC(getskilllist)
  9065. {
  9066. TBL_PC *sd=script_rid2sd(st);
  9067. int i,j=0;
  9068. if(!sd) return 0;
  9069. for(i=0;i<MAX_SKILL;i++){
  9070. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9071. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9072. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9073. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9074. j++;
  9075. }
  9076. }
  9077. pc_setreg(sd,add_str("@skilllist_count"),j);
  9078. return 0;
  9079. }
  9080. BUILDIN_FUNC(clearitem)
  9081. {
  9082. TBL_PC *sd=script_rid2sd(st);
  9083. int i;
  9084. if(sd==NULL) return 0;
  9085. for (i=0; i<MAX_INVENTORY; i++) {
  9086. if (sd->status.inventory[i].amount) {
  9087. //Logs items, got from (N)PC scripts [Lupus]
  9088. if(log_config.enable_logs&0x40)
  9089. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9090. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  9091. }
  9092. }
  9093. return 0;
  9094. }
  9095. /*==========================================
  9096. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9097. *------------------------------------------*/
  9098. BUILDIN_FUNC(disguise)
  9099. {
  9100. int id;
  9101. TBL_PC* sd = script_rid2sd(st);
  9102. if (sd == NULL) return 0;
  9103. id = script_getnum(st,2);
  9104. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9105. pc_disguise(sd, id);
  9106. script_pushint(st,id);
  9107. } else
  9108. script_pushint(st,0);
  9109. return 0;
  9110. }
  9111. /*==========================================
  9112. * Undisguise Player (returns 1 if success, 0 on fail)
  9113. *------------------------------------------*/
  9114. BUILDIN_FUNC(undisguise)
  9115. {
  9116. TBL_PC* sd = script_rid2sd(st);
  9117. if (sd == NULL) return 0;
  9118. if (sd->disguise) {
  9119. pc_disguise(sd, 0);
  9120. script_pushint(st,0);
  9121. } else {
  9122. script_pushint(st,1);
  9123. }
  9124. return 0;
  9125. }
  9126. /*==========================================
  9127. * NPCクラスチェンジ
  9128. * classは変わりたいclass
  9129. * typeは通常0なのかな?
  9130. *------------------------------------------*/
  9131. BUILDIN_FUNC(classchange)
  9132. {
  9133. int _class,type;
  9134. struct block_list *bl=map_id2bl(st->oid);
  9135. if(bl==NULL) return 0;
  9136. _class=script_getnum(st,2);
  9137. type=script_getnum(st,3);
  9138. clif_class_change(bl,_class,type);
  9139. return 0;
  9140. }
  9141. /*==========================================
  9142. * NPCから発生するエフェクト
  9143. *------------------------------------------*/
  9144. BUILDIN_FUNC(misceffect)
  9145. {
  9146. int type;
  9147. type=script_getnum(st,2);
  9148. if(st->oid && st->oid != fake_nd->bl.id) {
  9149. struct block_list *bl = map_id2bl(st->oid);
  9150. if (bl)
  9151. clif_misceffect2(bl,type);
  9152. } else{
  9153. TBL_PC *sd=script_rid2sd(st);
  9154. if(sd)
  9155. clif_misceffect2(&sd->bl,type);
  9156. }
  9157. return 0;
  9158. }
  9159. /*==========================================
  9160. * サウンドエフェクト
  9161. *------------------------------------------*/
  9162. BUILDIN_FUNC(soundeffect)
  9163. {
  9164. TBL_PC* sd = script_rid2sd(st);
  9165. const char* name = script_getstr(st,2);
  9166. int type = script_getnum(st,3);
  9167. if(sd)
  9168. {
  9169. if(!st->rid)
  9170. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9171. else
  9172. clif_soundeffect(sd,&sd->bl,name,type);
  9173. }
  9174. return 0;
  9175. }
  9176. int soundeffect_sub(struct block_list* bl,va_list ap)
  9177. {
  9178. char* name = va_arg(ap,char*);
  9179. int type = va_arg(ap,int);
  9180. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9181. return 0;
  9182. }
  9183. /*==========================================
  9184. * Play a sound effect (.wav) on multiple clients
  9185. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9186. *------------------------------------------*/
  9187. BUILDIN_FUNC(soundeffectall)
  9188. {
  9189. struct block_list* bl;
  9190. const char* name;
  9191. int type;
  9192. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9193. if (!bl)
  9194. return 0;
  9195. name = script_getstr(st,2);
  9196. type = script_getnum(st,3);
  9197. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9198. if(!script_hasdata(st,4))
  9199. { // area around
  9200. clif_soundeffectall(bl, name, type, AREA);
  9201. }
  9202. else
  9203. if(!script_hasdata(st,5))
  9204. { // entire map
  9205. const char* map = script_getstr(st,4);
  9206. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9207. }
  9208. else
  9209. if(script_hasdata(st,8))
  9210. { // specified part of map
  9211. const char* map = script_getstr(st,4);
  9212. int x0 = script_getnum(st,5);
  9213. int y0 = script_getnum(st,6);
  9214. int x1 = script_getnum(st,7);
  9215. int y1 = script_getnum(st,8);
  9216. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9217. }
  9218. else
  9219. {
  9220. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9221. }
  9222. return 0;
  9223. }
  9224. /*==========================================
  9225. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9226. *------------------------------------------*/
  9227. BUILDIN_FUNC(petrecovery)
  9228. {
  9229. struct pet_data *pd;
  9230. TBL_PC *sd=script_rid2sd(st);
  9231. if(sd==NULL || sd->pd==NULL)
  9232. return 0;
  9233. pd=sd->pd;
  9234. if (pd->recovery)
  9235. { //Halt previous bonus
  9236. if (pd->recovery->timer != -1)
  9237. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9238. } else //Init
  9239. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9240. pd->recovery->type = (sc_type)script_getnum(st,2);
  9241. pd->recovery->delay = script_getnum(st,3);
  9242. pd->recovery->timer = INVALID_TIMER;
  9243. return 0;
  9244. }
  9245. /*==========================================
  9246. * pet healing [Valaris] //Rewritten by [Skotlex]
  9247. *------------------------------------------*/
  9248. BUILDIN_FUNC(petheal)
  9249. {
  9250. struct pet_data *pd;
  9251. TBL_PC *sd=script_rid2sd(st);
  9252. if(sd==NULL || sd->pd==NULL)
  9253. return 0;
  9254. pd=sd->pd;
  9255. if (pd->s_skill)
  9256. { //Clear previous skill
  9257. if (pd->s_skill->timer != -1)
  9258. {
  9259. if (pd->s_skill->id)
  9260. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9261. else
  9262. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9263. }
  9264. } else //init memory
  9265. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9266. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9267. //Use the lv as the amount to heal
  9268. pd->s_skill->lv=script_getnum(st,2);
  9269. pd->s_skill->delay=script_getnum(st,3);
  9270. pd->s_skill->hp=script_getnum(st,4);
  9271. pd->s_skill->sp=script_getnum(st,5);
  9272. //Use delay as initial offset to avoid skill/heal exploits
  9273. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9274. pd->s_skill->timer = INVALID_TIMER;
  9275. else
  9276. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9277. return 0;
  9278. }
  9279. /*==========================================
  9280. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9281. *------------------------------------------*/
  9282. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  9283. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  9284. BUILDIN_FUNC(petskillattack)
  9285. {
  9286. struct pet_data *pd;
  9287. TBL_PC *sd=script_rid2sd(st);
  9288. if(sd==NULL || sd->pd==NULL)
  9289. return 0;
  9290. pd=sd->pd;
  9291. if (pd->a_skill == NULL)
  9292. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9293. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9294. pd->a_skill->lv=script_getnum(st,3);
  9295. pd->a_skill->div_ = 0;
  9296. pd->a_skill->rate=script_getnum(st,4);
  9297. pd->a_skill->bonusrate=script_getnum(st,5);
  9298. return 0;
  9299. }
  9300. /*==========================================
  9301. * pet attack skills [Valaris]
  9302. *------------------------------------------*/
  9303. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  9304. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  9305. BUILDIN_FUNC(petskillattack2)
  9306. {
  9307. struct pet_data *pd;
  9308. TBL_PC *sd=script_rid2sd(st);
  9309. if(sd==NULL || sd->pd==NULL)
  9310. return 0;
  9311. pd=sd->pd;
  9312. if (pd->a_skill == NULL)
  9313. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9314. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9315. pd->a_skill->lv=script_getnum(st,3);
  9316. pd->a_skill->div_ = script_getnum(st,4);
  9317. pd->a_skill->rate=script_getnum(st,5);
  9318. pd->a_skill->bonusrate=script_getnum(st,6);
  9319. return 0;
  9320. }
  9321. /*==========================================
  9322. * pet support skills [Skotlex]
  9323. *------------------------------------------*/
  9324. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  9325. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  9326. BUILDIN_FUNC(petskillsupport)
  9327. {
  9328. struct pet_data *pd;
  9329. TBL_PC *sd=script_rid2sd(st);
  9330. if(sd==NULL || sd->pd==NULL)
  9331. return 0;
  9332. pd=sd->pd;
  9333. if (pd->s_skill)
  9334. { //Clear previous skill
  9335. if (pd->s_skill->timer != -1)
  9336. {
  9337. if (pd->s_skill->id)
  9338. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9339. else
  9340. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9341. }
  9342. } else //init memory
  9343. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9344. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9345. pd->s_skill->lv=script_getnum(st,3);
  9346. pd->s_skill->delay=script_getnum(st,4);
  9347. pd->s_skill->hp=script_getnum(st,5);
  9348. pd->s_skill->sp=script_getnum(st,6);
  9349. //Use delay as initial offset to avoid skill/heal exploits
  9350. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9351. pd->s_skill->timer = INVALID_TIMER;
  9352. else
  9353. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9354. return 0;
  9355. }
  9356. /*==========================================
  9357. * Scripted skill effects [Celest]
  9358. *------------------------------------------*/
  9359. /// skilleffect <skill id>,<level>
  9360. /// skilleffect "<skill name>",<level>
  9361. BUILDIN_FUNC(skilleffect)
  9362. {
  9363. TBL_PC *sd;
  9364. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9365. int skilllv=script_getnum(st,3);
  9366. sd=script_rid2sd(st);
  9367. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9368. return 0;
  9369. }
  9370. /*==========================================
  9371. * NPC skill effects [Valaris]
  9372. *------------------------------------------*/
  9373. /// npcskilleffect <skill id>,<level>,<x>,<y>
  9374. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  9375. BUILDIN_FUNC(npcskilleffect)
  9376. {
  9377. struct block_list *bl= map_id2bl(st->oid);
  9378. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9379. int skilllv=script_getnum(st,3);
  9380. int x=script_getnum(st,4);
  9381. int y=script_getnum(st,5);
  9382. if (bl)
  9383. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9384. return 0;
  9385. }
  9386. /*==========================================
  9387. * Special effects [Valaris]
  9388. *------------------------------------------*/
  9389. BUILDIN_FUNC(specialeffect)
  9390. {
  9391. struct block_list *bl=map_id2bl(st->oid);
  9392. int type = script_getnum(st,2);
  9393. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9394. if(bl==NULL)
  9395. return 0;
  9396. clif_specialeffect(bl, type, target);
  9397. return 0;
  9398. }
  9399. BUILDIN_FUNC(specialeffect2)
  9400. {
  9401. TBL_PC *sd=script_rid2sd(st);
  9402. int type = script_getnum(st,2);
  9403. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9404. if(sd==NULL)
  9405. return 0;
  9406. clif_specialeffect(&sd->bl, type, target);
  9407. return 0;
  9408. }
  9409. /*==========================================
  9410. * Nude [Valaris]
  9411. *------------------------------------------*/
  9412. BUILDIN_FUNC(nude)
  9413. {
  9414. TBL_PC *sd=script_rid2sd(st);
  9415. int i,calcflag=0;
  9416. if(sd==NULL)
  9417. return 0;
  9418. for(i=0;i<11;i++)
  9419. if(sd->equip_index[i] >= 0) {
  9420. if(!calcflag)
  9421. calcflag=1;
  9422. pc_unequipitem(sd,sd->equip_index[i],2);
  9423. }
  9424. if(calcflag)
  9425. status_calc_pc(sd,0);
  9426. return 0;
  9427. }
  9428. /*==========================================
  9429. * gmcommand [MouseJstr]
  9430. *
  9431. * suggested on the forums...
  9432. * splitted into atcommand & charcommand by [Skotlex]
  9433. *------------------------------------------*/
  9434. BUILDIN_FUNC(atcommand)
  9435. {
  9436. TBL_PC dummy_sd;
  9437. TBL_PC* sd;
  9438. int fd;
  9439. const char* cmd;
  9440. cmd = script_getstr(st,2);
  9441. if (st->rid) {
  9442. sd = script_rid2sd(st);
  9443. fd = sd->fd;
  9444. } else { //Use a dummy character.
  9445. sd = &dummy_sd;
  9446. fd = 0;
  9447. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9448. if (st->oid)
  9449. {
  9450. struct block_list* bl = map_id2bl(st->oid);
  9451. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9452. if (bl->type == BL_NPC)
  9453. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9454. }
  9455. }
  9456. // compatibility with previous implementation (deprecated!)
  9457. if(cmd[0] != atcommand_symbol)
  9458. {
  9459. cmd += strlen(sd->status.name);
  9460. while(*cmd != atcommand_symbol && *cmd != 0)
  9461. cmd++;
  9462. }
  9463. is_atcommand_sub(fd, sd, cmd, 99);
  9464. return 0;
  9465. }
  9466. BUILDIN_FUNC(charcommand)
  9467. {
  9468. TBL_PC dummy_sd;
  9469. TBL_PC* sd;
  9470. TBL_PC* temp_sd;
  9471. char output[200], temp[200], command[200], charname[NAME_LENGTH], param[200];
  9472. int fd;
  9473. const char* cmd;
  9474. const char* message;
  9475. cmd = script_getstr(st,2);
  9476. if (st->rid) {
  9477. sd = script_rid2sd(st);
  9478. fd = sd->fd;
  9479. } else { //Use a dummy character.
  9480. sd = &dummy_sd;
  9481. fd = 0;
  9482. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9483. if (st->oid)
  9484. {
  9485. struct block_list* bl = map_id2bl(st->oid);
  9486. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9487. if (bl->type == BL_NPC)
  9488. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9489. }
  9490. }
  9491. if (*cmd == charcommand_symbol)
  9492. {
  9493. if (sscanf(cmd, "%99s \"%23[^\"]\" %99[^\n]", command, charname, param) > 2
  9494. || sscanf(cmd, "%99s %23s %99[^\n]", command, charname, param) > 2)
  9495. {
  9496. if ( (temp_sd = map_nick2sd(charname)) != NULL )
  9497. {
  9498. sprintf(output, "%s %s", cmd, param);
  9499. memcpy(temp, output, sizeof(output));
  9500. message = temp;
  9501. is_atcommand_sub(fd,sd,message,99);
  9502. }
  9503. }
  9504. }
  9505. else {
  9506. ShowWarning("script: buildin_charcommand: No '#' symbol!");
  9507. script_reportsrc(st);
  9508. }
  9509. return 0;
  9510. }
  9511. /*==========================================
  9512. * Displays a message for the player only (like system messages like "you got an apple" )
  9513. *------------------------------------------*/
  9514. BUILDIN_FUNC(dispbottom)
  9515. {
  9516. TBL_PC *sd=script_rid2sd(st);
  9517. const char *message;
  9518. message=script_getstr(st,2);
  9519. if(sd)
  9520. clif_disp_onlyself(sd,message,(int)strlen(message));
  9521. return 0;
  9522. }
  9523. /*==========================================
  9524. * All The Players Full Recovery
  9525. * (HP/SP full restore and resurrect if need)
  9526. *------------------------------------------*/
  9527. BUILDIN_FUNC(recovery)
  9528. {
  9529. TBL_PC* sd;
  9530. struct s_mapiterator* iter;
  9531. iter = mapit_getallusers();
  9532. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9533. {
  9534. if(pc_isdead(sd))
  9535. status_revive(&sd->bl, 100, 100);
  9536. else
  9537. status_percent_heal(&sd->bl, 100, 100);
  9538. clif_displaymessage(sd->fd,"You have been recovered!");
  9539. }
  9540. mapit_free(iter);
  9541. return 0;
  9542. }
  9543. /*==========================================
  9544. * Get your pet info: getpetinfo(n)
  9545. * n -> 0:pet_id 1:pet_class 2:pet_name
  9546. * 3:friendly 4:hungry, 5: rename flag.
  9547. *------------------------------------------*/
  9548. BUILDIN_FUNC(getpetinfo)
  9549. {
  9550. TBL_PC *sd=script_rid2sd(st);
  9551. TBL_PET *pd;
  9552. int type=script_getnum(st,2);
  9553. if(!sd || !sd->pd) {
  9554. if (type == 2)
  9555. script_pushconststr(st,"null");
  9556. else
  9557. script_pushint(st,0);
  9558. return 0;
  9559. }
  9560. pd = sd->pd;
  9561. switch(type){
  9562. case 0: script_pushint(st,pd->pet.pet_id); break;
  9563. case 1: script_pushint(st,pd->pet.class_); break;
  9564. case 2: script_pushstrcopy(st,pd->pet.name); break;
  9565. case 3: script_pushint(st,pd->pet.intimate); break;
  9566. case 4: script_pushint(st,pd->pet.hungry); break;
  9567. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9568. default:
  9569. script_pushint(st,0);
  9570. break;
  9571. }
  9572. return 0;
  9573. }
  9574. /*==========================================
  9575. * Get your homunculus info: gethominfo(n)
  9576. * n -> 0:hom_id 1:class 2:name
  9577. * 3:friendly 4:hungry, 5: rename flag.
  9578. * 6: level
  9579. *------------------------------------------*/
  9580. BUILDIN_FUNC(gethominfo)
  9581. {
  9582. TBL_PC *sd=script_rid2sd(st);
  9583. TBL_HOM *hd;
  9584. int type=script_getnum(st,2);
  9585. hd = sd?sd->hd:NULL;
  9586. if(!merc_is_hom_active(hd))
  9587. {
  9588. if (type == 2)
  9589. script_pushconststr(st,"null");
  9590. else
  9591. script_pushint(st,0);
  9592. return 0;
  9593. }
  9594. switch(type){
  9595. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9596. case 1: script_pushint(st,hd->homunculus.class_); break;
  9597. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  9598. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9599. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9600. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9601. case 6: script_pushint(st,hd->homunculus.level); break;
  9602. default:
  9603. script_pushint(st,0);
  9604. break;
  9605. }
  9606. return 0;
  9607. }
  9608. /*==========================================
  9609. * Shows wether your inventory(and equips) contain
  9610. selected card or not.
  9611. checkequipedcard(4001);
  9612. *------------------------------------------*/
  9613. BUILDIN_FUNC(checkequipedcard)
  9614. {
  9615. TBL_PC *sd=script_rid2sd(st);
  9616. int n,i,c=0;
  9617. c=script_getnum(st,2);
  9618. if(sd){
  9619. for(i=0;i<MAX_INVENTORY;i++){
  9620. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9621. for(n=0;n<MAX_SLOTS;n++){
  9622. if(sd->status.inventory[i].card[n]==c){
  9623. script_pushint(st,1);
  9624. return 0;
  9625. }
  9626. }
  9627. }
  9628. }
  9629. }
  9630. script_pushint(st,0);
  9631. return 0;
  9632. }
  9633. BUILDIN_FUNC(jump_zero)
  9634. {
  9635. int sel;
  9636. sel=script_getnum(st,2);
  9637. if(!sel) {
  9638. int pos;
  9639. if( !data_islabel(script_getdata(st,3)) ){
  9640. ShowError("script: jump_zero: not label !\n");
  9641. st->state=END;
  9642. return 1;
  9643. }
  9644. pos=script_getnum(st,3);
  9645. st->pos=pos;
  9646. st->state=GOTO;
  9647. // printf("script: jump_zero: jumpto : %d\n",pos);
  9648. } else {
  9649. // printf("script: jump_zero: fail\n");
  9650. }
  9651. return 0;
  9652. }
  9653. /*==========================================
  9654. * GetMapMobs
  9655. returns mob counts on a set map:
  9656. e.g. GetMapMobs("prontera")
  9657. use "this" - for player's map
  9658. *------------------------------------------*/
  9659. BUILDIN_FUNC(getmapmobs)
  9660. {
  9661. const char *str=NULL;
  9662. int m=-1,bx,by;
  9663. int count=0;
  9664. struct block_list *bl;
  9665. str=script_getstr(st,2);
  9666. if(strcmp(str,"this")==0){
  9667. TBL_PC *sd=script_rid2sd(st);
  9668. if(sd)
  9669. m=sd->bl.m;
  9670. else{
  9671. script_pushint(st,-1);
  9672. return 0;
  9673. }
  9674. }else
  9675. m=map_mapname2mapid(str);
  9676. if(m < 0){
  9677. script_pushint(st,-1);
  9678. return 0;
  9679. }
  9680. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  9681. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  9682. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  9683. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9684. count++;
  9685. script_pushint(st,count);
  9686. return 0;
  9687. }
  9688. /*==========================================
  9689. * movenpc [MouseJstr]
  9690. *------------------------------------------*/
  9691. BUILDIN_FUNC(movenpc)
  9692. {
  9693. TBL_NPC *nd = NULL;
  9694. const char *npc;
  9695. int x,y;
  9696. npc = script_getstr(st,2);
  9697. x = script_getnum(st,3);
  9698. y = script_getnum(st,4);
  9699. if ((nd = npc_name2id(npc)) == NULL)
  9700. return -1;
  9701. npc_movenpc(nd, x, y);
  9702. return 0;
  9703. }
  9704. /*==========================================
  9705. * message [MouseJstr]
  9706. *------------------------------------------*/
  9707. BUILDIN_FUNC(message)
  9708. {
  9709. TBL_PC *sd;
  9710. const char *msg,*player;
  9711. TBL_PC *pl_sd = NULL;
  9712. sd = script_rid2sd(st);
  9713. player = script_getstr(st,2);
  9714. msg = script_getstr(st,3);
  9715. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9716. return 0;
  9717. clif_displaymessage(pl_sd->fd, msg);
  9718. return 0;
  9719. }
  9720. /*==========================================
  9721. * npctalk (sends message to surrounding area)
  9722. *------------------------------------------*/
  9723. BUILDIN_FUNC(npctalk)
  9724. {
  9725. const char* str;
  9726. char message[255];
  9727. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  9728. str = script_getstr(st,2);
  9729. if(nd) {
  9730. memcpy(message, nd->name, NAME_LENGTH);
  9731. strtok(message, "#"); // discard extra name identifier if present
  9732. strcat(message, " : ");
  9733. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  9734. clif_message(&(nd->bl), message);
  9735. }
  9736. return 0;
  9737. }
  9738. // change npc walkspeed [Valaris]
  9739. BUILDIN_FUNC(npcspeed)
  9740. {
  9741. struct npc_data* nd;
  9742. int speed;
  9743. speed = script_getnum(st,2);
  9744. nd =(struct npc_data *)map_id2bl(st->oid);
  9745. if( nd )
  9746. {
  9747. nd->speed = speed;
  9748. nd->ud.state.speed_changed = 1;
  9749. }
  9750. return 0;
  9751. }
  9752. // make an npc walk to a position [Valaris]
  9753. BUILDIN_FUNC(npcwalkto)
  9754. {
  9755. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9756. int x=0,y=0;
  9757. x=script_getnum(st,2);
  9758. y=script_getnum(st,3);
  9759. if(nd) {
  9760. unit_walktoxy(&nd->bl,x,y,0);
  9761. }
  9762. return 0;
  9763. }
  9764. // stop an npc's movement [Valaris]
  9765. BUILDIN_FUNC(npcstop)
  9766. {
  9767. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9768. if(nd) {
  9769. unit_stop_walking(&nd->bl,1);
  9770. }
  9771. return 0;
  9772. }
  9773. /*==========================================
  9774. * getlook char info. getlook(arg)
  9775. *------------------------------------------*/
  9776. BUILDIN_FUNC(getlook)
  9777. {
  9778. int type,val;
  9779. TBL_PC *sd;
  9780. sd=script_rid2sd(st);
  9781. type=script_getnum(st,2);
  9782. val=-1;
  9783. switch(type) {
  9784. case LOOK_HAIR: val=sd->status.hair; break; //1
  9785. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  9786. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  9787. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  9788. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  9789. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  9790. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  9791. case LOOK_SHIELD: val=sd->status.shield; break; //8
  9792. case LOOK_SHOES: break; //9
  9793. }
  9794. script_pushint(st,val);
  9795. return 0;
  9796. }
  9797. /*==========================================
  9798. * get char save point. argument: 0- map name, 1- x, 2- y
  9799. *------------------------------------------*/
  9800. BUILDIN_FUNC(getsavepoint)
  9801. {
  9802. TBL_PC* sd;
  9803. int type;
  9804. sd = script_rid2sd(st);
  9805. if (sd == NULL) {
  9806. script_pushint(st,0);
  9807. return 0;
  9808. }
  9809. type = script_getnum(st,2);
  9810. switch(type) {
  9811. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  9812. case 1: script_pushint(st,sd->status.save_point.x); break;
  9813. case 2: script_pushint(st,sd->status.save_point.y); break;
  9814. default:
  9815. script_pushint(st,0);
  9816. break;
  9817. }
  9818. return 0;
  9819. }
  9820. /*==========================================
  9821. * Get position for char/npc/pet/mob objects. Added by Lorky
  9822. *
  9823. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  9824. * where type:
  9825. * MapName$ - String variable for output map name
  9826. * MapX - Integer variable for output coord X
  9827. * MapY - Integer variable for output coord Y
  9828. * type - type of object
  9829. * 0 - Character coord
  9830. * 1 - NPC coord
  9831. * 2 - Pet coord
  9832. * 3 - Mob coord (not released)
  9833. * 4 - Homun coord
  9834. * CharName$ - Name object. If miss or "this" the current object
  9835. *
  9836. * Return:
  9837. * 0 - success
  9838. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  9839. *------------------------------------------*/
  9840. BUILDIN_FUNC(getmapxy)
  9841. {
  9842. struct block_list *bl = NULL;
  9843. TBL_PC *sd=NULL;
  9844. int num;
  9845. const char *name;
  9846. char prefix;
  9847. int x,y,type;
  9848. char mapname[MAP_NAME_LENGTH];
  9849. if( !data_isreference(script_getdata(st,2)) ){
  9850. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  9851. script_pushint(st,-1);
  9852. return 1;
  9853. }
  9854. if( !data_isreference(script_getdata(st,3)) ){
  9855. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  9856. script_pushint(st,-1);
  9857. return 1;
  9858. }
  9859. if( !data_isreference(script_getdata(st,4)) ){
  9860. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  9861. script_pushint(st,-1);
  9862. return 1;
  9863. }
  9864. // Possible needly check function parameters on C_STR,C_INT,C_INT
  9865. type=script_getnum(st,5);
  9866. switch (type){
  9867. case 0: //Get Character Position
  9868. if( script_hasdata(st,6) )
  9869. sd=map_nick2sd(script_getstr(st,6));
  9870. else
  9871. sd=script_rid2sd(st);
  9872. if (sd)
  9873. bl = &sd->bl;
  9874. break;
  9875. case 1: //Get NPC Position
  9876. if( script_hasdata(st,6) )
  9877. {
  9878. struct npc_data *nd;
  9879. nd=npc_name2id(script_getstr(st,6));
  9880. if (nd)
  9881. bl = &nd->bl;
  9882. } else //In case the origin is not an npc?
  9883. bl=map_id2bl(st->oid);
  9884. break;
  9885. case 2: //Get Pet Position
  9886. if(script_hasdata(st,6))
  9887. sd=map_nick2sd(script_getstr(st,6));
  9888. else
  9889. sd=script_rid2sd(st);
  9890. if (sd && sd->pd)
  9891. bl = &sd->pd->bl;
  9892. break;
  9893. case 3: //Get Mob Position
  9894. break; //Not supported?
  9895. case 4: //Get Homun Position
  9896. if(script_hasdata(st,6))
  9897. sd=map_nick2sd(script_getstr(st,6));
  9898. else
  9899. sd=script_rid2sd(st);
  9900. if (sd && sd->hd)
  9901. bl = &sd->hd->bl;
  9902. break;
  9903. }
  9904. if (!bl) { //No object found.
  9905. script_pushint(st,-1);
  9906. return 0;
  9907. }
  9908. x= bl->x;
  9909. y= bl->y;
  9910. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  9911. //Set MapName$
  9912. num=st->stack->stack_data[st->start+2].u.num;
  9913. name=get_str(num&0x00ffffff);
  9914. prefix=*name;
  9915. if(not_server_variable(prefix))
  9916. sd=script_rid2sd(st);
  9917. else
  9918. sd=NULL;
  9919. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  9920. //Set MapX
  9921. num=st->stack->stack_data[st->start+3].u.num;
  9922. name=get_str(num&0x00ffffff);
  9923. prefix=*name;
  9924. if(not_server_variable(prefix))
  9925. sd=script_rid2sd(st);
  9926. else
  9927. sd=NULL;
  9928. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  9929. //Set MapY
  9930. num=st->stack->stack_data[st->start+4].u.num;
  9931. name=get_str(num&0x00ffffff);
  9932. prefix=*name;
  9933. if(not_server_variable(prefix))
  9934. sd=script_rid2sd(st);
  9935. else
  9936. sd=NULL;
  9937. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  9938. //Return Success value
  9939. script_pushint(st,0);
  9940. return 0;
  9941. }
  9942. /*==========================================
  9943. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  9944. *------------------------------------------*/
  9945. BUILDIN_FUNC(logmes)
  9946. {
  9947. const char *str;
  9948. TBL_PC* sd;
  9949. if( log_config.npc <= 0 )
  9950. return 0;
  9951. sd = script_rid2sd(st);
  9952. if( sd == NULL )
  9953. return 1;
  9954. str = script_getstr(st,2);
  9955. log_npc(sd,str);
  9956. return 0;
  9957. }
  9958. BUILDIN_FUNC(summon)
  9959. {
  9960. int _class, timeout=0;
  9961. const char *str,*event="";
  9962. TBL_PC *sd;
  9963. struct mob_data *md;
  9964. int tick = gettick();
  9965. sd=script_rid2sd(st);
  9966. if (!sd) return 0;
  9967. str =script_getstr(st,2);
  9968. _class=script_getnum(st,3);
  9969. if( script_hasdata(st,4) )
  9970. timeout=script_getnum(st,4);
  9971. if( script_hasdata(st,5) ){
  9972. event=script_getstr(st,5);
  9973. check_event(st, event);
  9974. }
  9975. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  9976. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  9977. if (md) {
  9978. md->master_id=sd->bl.id;
  9979. md->special_state.ai=1;
  9980. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  9981. mob_spawn (md); //Now it is ready for spawning.
  9982. clif_misceffect2(&md->bl,344);
  9983. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  9984. }
  9985. return 0;
  9986. }
  9987. /*==========================================
  9988. * Checks whether it is daytime/nighttime
  9989. *------------------------------------------*/
  9990. BUILDIN_FUNC(isnight)
  9991. {
  9992. script_pushint(st,(night_flag == 1));
  9993. return 0;
  9994. }
  9995. BUILDIN_FUNC(isday)
  9996. {
  9997. script_pushint(st,(night_flag == 0));
  9998. return 0;
  9999. }
  10000. /*================================================
  10001. * Check whether another item/card has been
  10002. * equipped - used for 2/15's cards patch [celest]
  10003. *------------------------------------------------*/
  10004. // leave this here, just in case
  10005. #if 0
  10006. BUILDIN_FUNC(isequipped)
  10007. {
  10008. TBL_PC *sd;
  10009. int i, j, k, id = 1;
  10010. int ret = -1;
  10011. sd = script_rid2sd(st);
  10012. for (i=0; id!=0; i++) {
  10013. int flag = 0;
  10014. FETCH (i+2, id) else id = 0;
  10015. if (id <= 0)
  10016. continue;
  10017. for (j=0; j<EQI_MAX; j++) {
  10018. int index;
  10019. index = sd->equip_index[j];
  10020. if(index < 0) continue;
  10021. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10022. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10023. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10024. if(!sd->inventory_data[index])
  10025. continue;
  10026. if(itemdb_type(id) != IT_CARD) { //Non card
  10027. if (sd->inventory_data[index]->nameid != id)
  10028. continue;
  10029. flag = 1;
  10030. break;
  10031. } else { //Card
  10032. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10033. continue;
  10034. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10035. if (sd->status.inventory[index].card[k] == id)
  10036. {
  10037. flag = 1;
  10038. break;
  10039. }
  10040. }
  10041. }
  10042. if (flag) break;
  10043. }
  10044. if (ret == -1)
  10045. ret = flag;
  10046. else
  10047. ret &= flag;
  10048. if (!ret) break;
  10049. }
  10050. script_pushint(st,ret);
  10051. return 0;
  10052. }
  10053. #endif
  10054. /*================================================
  10055. * Check how many items/cards in the list are
  10056. * equipped - used for 2/15's cards patch [celest]
  10057. *------------------------------------------------*/
  10058. BUILDIN_FUNC(isequippedcnt)
  10059. {
  10060. TBL_PC *sd;
  10061. int i, j, k, id = 1;
  10062. int ret = 0;
  10063. sd = script_rid2sd(st);
  10064. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10065. script_pushint(st,0);
  10066. return 0;
  10067. }
  10068. for (i=0; id!=0; i++) {
  10069. FETCH (i+2, id) else id = 0;
  10070. if (id <= 0)
  10071. continue;
  10072. for (j=0; j<EQI_MAX; j++) {
  10073. int index;
  10074. index = sd->equip_index[j];
  10075. if(index < 0) continue;
  10076. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10077. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10078. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10079. if(!sd->inventory_data[index])
  10080. continue;
  10081. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10082. if (sd->inventory_data[index]->nameid == id)
  10083. ret+= sd->status.inventory[index].amount;
  10084. } else { //Count cards.
  10085. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10086. continue; //No cards
  10087. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10088. if (sd->status.inventory[index].card[k] == id)
  10089. ret++; //[Lupus]
  10090. }
  10091. }
  10092. }
  10093. }
  10094. script_pushint(st,ret);
  10095. return 0;
  10096. }
  10097. /*================================================
  10098. * Check whether another card has been
  10099. * equipped - used for 2/15's cards patch [celest]
  10100. * -- Items checked cannot be reused in another
  10101. * card set to prevent exploits
  10102. *------------------------------------------------*/
  10103. BUILDIN_FUNC(isequipped)
  10104. {
  10105. TBL_PC *sd;
  10106. int i, j, k, id = 1;
  10107. int index, flag;
  10108. int ret = -1;
  10109. //Original hash to reverse it when full check fails.
  10110. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10111. sd = script_rid2sd(st);
  10112. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10113. script_pushint(st,0);
  10114. return 0;
  10115. }
  10116. setitem_hash = sd->setitem_hash;
  10117. setitem_hash2 = sd->setitem_hash2;
  10118. for (i=0; id!=0; i++)
  10119. {
  10120. FETCH (i+2, id) else id = 0;
  10121. if (id <= 0)
  10122. continue;
  10123. flag = 0;
  10124. for (j=0; j<EQI_MAX; j++)
  10125. {
  10126. index = sd->equip_index[j];
  10127. if(index < 0) continue;
  10128. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10129. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10130. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10131. if(!sd->inventory_data[index])
  10132. continue;
  10133. if (itemdb_type(id) != IT_CARD) {
  10134. if (sd->inventory_data[index]->nameid != id)
  10135. continue;
  10136. flag = 1;
  10137. break;
  10138. } else { //Cards
  10139. if (sd->inventory_data[index]->slot == 0 ||
  10140. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10141. continue;
  10142. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10143. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10144. unsigned int hash = 0;
  10145. if (sd->status.inventory[index].card[k] != id)
  10146. continue;
  10147. hash = 1<<((j<5?j:j-5)*4 + k);
  10148. // check if card is already used by another set
  10149. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10150. continue;
  10151. // We have found a match
  10152. flag = 1;
  10153. // Set hash so this card cannot be used by another
  10154. if (j<5)
  10155. sd->setitem_hash |= hash;
  10156. else
  10157. sd->setitem_hash2 |= hash;
  10158. break;
  10159. }
  10160. }
  10161. if (flag) break; //Card found
  10162. }
  10163. if (ret == -1)
  10164. ret = flag;
  10165. else
  10166. ret &= flag;
  10167. if (!ret) break;
  10168. }
  10169. if (!ret)
  10170. { //When check fails, restore original hash values. [Skotlex]
  10171. sd->setitem_hash = setitem_hash;
  10172. sd->setitem_hash2 = setitem_hash2;
  10173. }
  10174. script_pushint(st,ret);
  10175. return 0;
  10176. }
  10177. /*================================================
  10178. * Check how many given inserted cards in the CURRENT
  10179. * weapon - used for 2/15's cards patch [Lupus]
  10180. *------------------------------------------------*/
  10181. BUILDIN_FUNC(cardscnt)
  10182. {
  10183. TBL_PC *sd;
  10184. int i, k, id = 1;
  10185. int ret = 0;
  10186. int index;
  10187. sd = script_rid2sd(st);
  10188. for (i=0; id!=0; i++) {
  10189. FETCH (i+2, id) else id = 0;
  10190. if (id <= 0)
  10191. continue;
  10192. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10193. if(index < 0) continue;
  10194. if(!sd->inventory_data[index])
  10195. continue;
  10196. if(itemdb_type(id) != IT_CARD) {
  10197. if (sd->inventory_data[index]->nameid == id)
  10198. ret+= sd->status.inventory[index].amount;
  10199. } else {
  10200. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10201. continue;
  10202. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10203. if (sd->status.inventory[index].card[k] == id)
  10204. ret++;
  10205. }
  10206. }
  10207. }
  10208. script_pushint(st,ret);
  10209. // script_pushint(st,current_equip_item_index);
  10210. return 0;
  10211. }
  10212. /*=======================================================
  10213. * Returns the refined number of the current item, or an
  10214. * item with inventory index specified
  10215. *-------------------------------------------------------*/
  10216. BUILDIN_FUNC(getrefine)
  10217. {
  10218. TBL_PC *sd;
  10219. if ((sd = script_rid2sd(st))!= NULL)
  10220. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10221. else
  10222. script_pushint(st,0);
  10223. return 0;
  10224. }
  10225. /*=======================================================
  10226. * Day/Night controls
  10227. *-------------------------------------------------------*/
  10228. BUILDIN_FUNC(night)
  10229. {
  10230. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10231. return 0;
  10232. }
  10233. BUILDIN_FUNC(day)
  10234. {
  10235. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10236. return 0;
  10237. }
  10238. //=======================================================
  10239. // Unequip [Spectre]
  10240. //-------------------------------------------------------
  10241. BUILDIN_FUNC(unequip)
  10242. {
  10243. int i;
  10244. size_t num;
  10245. TBL_PC *sd;
  10246. num = script_getnum(st,2);
  10247. sd = script_rid2sd(st);
  10248. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10249. {
  10250. i = pc_checkequip(sd,equip[num-1]);
  10251. if (i >= 0)
  10252. pc_unequipitem(sd,i,1|2);
  10253. }
  10254. return 0;
  10255. }
  10256. BUILDIN_FUNC(equip)
  10257. {
  10258. int nameid=0,i;
  10259. TBL_PC *sd;
  10260. struct item_data *item_data;
  10261. sd = script_rid2sd(st);
  10262. nameid=script_getnum(st,2);
  10263. if((item_data = itemdb_exists(nameid)) == NULL)
  10264. {
  10265. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10266. return 1;
  10267. }
  10268. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10269. if( i < MAX_INVENTORY )
  10270. pc_equipitem(sd,i,item_data->equip);
  10271. return 0;
  10272. }
  10273. BUILDIN_FUNC(autoequip)
  10274. {
  10275. int nameid, flag;
  10276. struct item_data *item_data;
  10277. nameid=script_getnum(st,2);
  10278. flag=script_getnum(st,3);
  10279. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10280. item_data->flag.autoequip = flag>0?1:0;
  10281. }
  10282. return 0;
  10283. }
  10284. BUILDIN_FUNC(setbattleflag)
  10285. {
  10286. const char *flag, *value;
  10287. flag = script_getstr(st,2);
  10288. value = script_getstr(st,3);
  10289. if (battle_set_value(flag, value) == 0)
  10290. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10291. else
  10292. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10293. return 0;
  10294. }
  10295. BUILDIN_FUNC(getbattleflag)
  10296. {
  10297. const char *flag;
  10298. flag = script_getstr(st,2);
  10299. script_pushint(st,battle_get_value(flag));
  10300. return 0;
  10301. }
  10302. //=======================================================
  10303. // strlen [Valaris]
  10304. //-------------------------------------------------------
  10305. BUILDIN_FUNC(getstrlen)
  10306. {
  10307. const char *str = script_getstr(st,2);
  10308. int len = (str) ? (int)strlen(str) : 0;
  10309. script_pushint(st,len);
  10310. return 0;
  10311. }
  10312. //=======================================================
  10313. // isalpha [Valaris]
  10314. //-------------------------------------------------------
  10315. BUILDIN_FUNC(charisalpha)
  10316. {
  10317. const char *str=script_getstr(st,2);
  10318. int pos=script_getnum(st,3);
  10319. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10320. script_pushint(st,val);
  10321. return 0;
  10322. }
  10323. /// Changes the display name and/or display class of the npc.
  10324. /// Returns 0 is successful, 1 if the npc does not exist.
  10325. ///
  10326. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  10327. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10328. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10329. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10330. BUILDIN_FUNC(setnpcdisplay)
  10331. {
  10332. const char* name;
  10333. const char* newname = NULL;
  10334. int class_ = -1, size = -1;
  10335. struct script_data* data;
  10336. struct npc_data* nd;
  10337. name = script_getstr(st,2);
  10338. data = script_getdata(st,3);
  10339. if( script_hasdata(st,4) )
  10340. class_ = script_getnum(st,4);
  10341. if( script_hasdata(st,5) )
  10342. size = script_getnum(st,5);
  10343. get_val(st, data);
  10344. if( data_isstring(data) )
  10345. newname = conv_str(st,data);
  10346. else if( data_isint(data) )
  10347. class_ = conv_num(st,data);
  10348. else
  10349. {
  10350. ShowError("script:setnpcdisplay: expected a string or number\n");
  10351. script_reportdata(data);
  10352. return 1;
  10353. }
  10354. nd = npc_name2id(name);
  10355. if( nd == NULL )
  10356. {// not found
  10357. script_pushint(st,1);
  10358. return 0;
  10359. }
  10360. // update npc
  10361. if( newname )
  10362. npc_setdisplayname(nd, newname);
  10363. if( size != -1 && size != nd->size )
  10364. nd->size = size;
  10365. else
  10366. size = -1;
  10367. if( class_ != -1 && nd->class_ != class_ )
  10368. npc_setclass(nd, class_);
  10369. else if( size != -1 )
  10370. { // Required to update the visual size
  10371. clif_clearunit_area(&nd->bl, 0);
  10372. clif_spawn(&nd->bl);
  10373. }
  10374. script_pushint(st,0);
  10375. return 0;
  10376. }
  10377. BUILDIN_FUNC(atoi)
  10378. {
  10379. const char *value;
  10380. value = script_getstr(st,2);
  10381. script_pushint(st,atoi(value));
  10382. return 0;
  10383. }
  10384. // case-insensitive substring search [lordalfa]
  10385. BUILDIN_FUNC(compare)
  10386. {
  10387. const char *message;
  10388. const char *cmpstring;
  10389. message = script_getstr(st,2);
  10390. cmpstring = script_getstr(st,3);
  10391. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10392. return 0;
  10393. }
  10394. // [zBuffer] List of mathematics commands --->
  10395. BUILDIN_FUNC(sqrt)
  10396. {
  10397. double i, a;
  10398. i = script_getnum(st,2);
  10399. a = sqrt(i);
  10400. script_pushint(st,(int)a);
  10401. return 0;
  10402. }
  10403. BUILDIN_FUNC(pow)
  10404. {
  10405. double i, a, b;
  10406. a = script_getnum(st,2);
  10407. b = script_getnum(st,3);
  10408. i = pow(a,b);
  10409. script_pushint(st,(int)i);
  10410. return 0;
  10411. }
  10412. BUILDIN_FUNC(distance)
  10413. {
  10414. int x0, y0, x1, y1;
  10415. x0 = script_getnum(st,2);
  10416. y0 = script_getnum(st,3);
  10417. x1 = script_getnum(st,4);
  10418. y1 = script_getnum(st,5);
  10419. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10420. return 0;
  10421. }
  10422. // <--- [zBuffer] List of mathematics commands
  10423. // [zBuffer] List of dynamic var commands --->
  10424. BUILDIN_FUNC(setd)
  10425. {
  10426. TBL_PC *sd=NULL;
  10427. char varname[100];
  10428. const char *value, *buffer;
  10429. int elem;
  10430. buffer = script_getstr(st, 2);
  10431. value = script_getstr(st, 3);
  10432. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10433. elem = 0;
  10434. if( not_server_variable(*varname) )
  10435. {
  10436. sd = script_rid2sd(st);
  10437. if( sd == NULL )
  10438. {
  10439. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10440. return 0;
  10441. }
  10442. }
  10443. if(varname[strlen(varname)-1] != '$') {
  10444. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10445. } else {
  10446. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10447. }
  10448. return 0;
  10449. }
  10450. #ifndef TXT_ONLY
  10451. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10452. {
  10453. int i, j;
  10454. TBL_PC* sd = NULL;
  10455. const char* query;
  10456. struct script_data* data;
  10457. const char* name;
  10458. int max_rows = 128;// maximum number of rows
  10459. int num_vars;
  10460. int num_cols;
  10461. // check target variables
  10462. for( i = 3; script_hasdata(st,i); ++i )
  10463. {
  10464. data = script_getdata(st, i);
  10465. if( data_isreference(data) && reference_tovariable(data) )
  10466. {// it's a variable
  10467. name = reference_getname(data);
  10468. if( not_server_variable(*name) && sd == NULL )
  10469. {// requires a player
  10470. sd = script_rid2sd(st);
  10471. if( sd == NULL )
  10472. {// no player attached
  10473. script_reportdata(data);
  10474. st->state = END;
  10475. return 1;
  10476. }
  10477. }
  10478. if( not_array_variable(*name) )
  10479. max_rows = 1;// not an array, limit to one row
  10480. }
  10481. else
  10482. {
  10483. ShowError("script:query_sql: not a variable\n");
  10484. script_reportdata(data);
  10485. st->state = END;
  10486. return 1;
  10487. }
  10488. }
  10489. num_vars = i - 3;
  10490. // Execute the query
  10491. query = script_getstr(st,2);
  10492. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10493. {
  10494. Sql_ShowDebug(handle);
  10495. script_pushint(st, 0);
  10496. return 1;
  10497. }
  10498. if( Sql_NumRows(handle) == 0 )
  10499. {// No data received
  10500. Sql_FreeResult(handle);
  10501. script_pushint(st, 0);
  10502. return 0;
  10503. }
  10504. // Count the number of columns to store
  10505. num_cols = Sql_NumColumns(handle);
  10506. if( num_vars < num_cols )
  10507. {
  10508. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10509. script_reportsrc(st);
  10510. }
  10511. else if( num_vars > num_cols )
  10512. {
  10513. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10514. script_reportsrc(st);
  10515. }
  10516. // Store data
  10517. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10518. {
  10519. for( j = 0; j < num_vars; ++j )
  10520. {
  10521. char* str = NULL;
  10522. if( j < num_cols )
  10523. Sql_GetData(handle, j, &str, NULL);
  10524. data = script_getdata(st, j+3);
  10525. name = reference_getname(data);
  10526. if( is_string_variable(name) )
  10527. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10528. else
  10529. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10530. }
  10531. }
  10532. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10533. {
  10534. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10535. script_reportsrc(st);
  10536. }
  10537. // Free data
  10538. Sql_FreeResult(handle);
  10539. script_pushint(st, i);
  10540. return 0;
  10541. }
  10542. #endif
  10543. BUILDIN_FUNC(query_sql)
  10544. {
  10545. #ifndef TXT_ONLY
  10546. return buildin_query_sql_sub(st, mmysql_handle);
  10547. #else
  10548. //for TXT version, we always return -1
  10549. script_pushint(st,-1);
  10550. return 0;
  10551. #endif
  10552. }
  10553. BUILDIN_FUNC(query_logsql)
  10554. {
  10555. #ifndef TXT_ONLY
  10556. return buildin_query_sql_sub(st, logmysql_handle);
  10557. #else
  10558. //for TXT version, we always return -1
  10559. script_pushint(st,-1);
  10560. return 0;
  10561. #endif
  10562. }
  10563. //Allows escaping of a given string.
  10564. BUILDIN_FUNC(escape_sql)
  10565. {
  10566. const char *str;
  10567. char *esc_str;
  10568. size_t len;
  10569. str = script_getstr(st,2);
  10570. len = strlen(str);
  10571. esc_str = (char*)aMallocA(len*2+1);
  10572. #if defined(TXT_ONLY)
  10573. jstrescapecpy(esc_str, str);
  10574. #else
  10575. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10576. #endif
  10577. script_pushstr(st, esc_str);
  10578. return 0;
  10579. }
  10580. BUILDIN_FUNC(getd)
  10581. {
  10582. char varname[100];
  10583. const char *buffer;
  10584. int elem;
  10585. buffer = script_getstr(st, 2);
  10586. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10587. elem = 0;
  10588. // Push the 'pointer' so it's more flexible [Lance]
  10589. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  10590. return 0;
  10591. }
  10592. // <--- [zBuffer] List of dynamic var commands
  10593. // Pet stat [Lance]
  10594. BUILDIN_FUNC(petstat)
  10595. {
  10596. TBL_PC *sd = NULL;
  10597. struct pet_data *pd;
  10598. int flag = script_getnum(st,2);
  10599. sd = script_rid2sd(st);
  10600. if(!sd || !sd->status.pet_id || !sd->pd){
  10601. if(flag == 2)
  10602. script_pushconststr(st, "");
  10603. else
  10604. script_pushint(st,0);
  10605. return 0;
  10606. }
  10607. pd = sd->pd;
  10608. switch(flag){
  10609. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10610. case 2: script_pushstrcopy(st, pd->pet.name); break;
  10611. case 3: script_pushint(st,(int)pd->pet.level); break;
  10612. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10613. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10614. default:
  10615. script_pushint(st,0);
  10616. break;
  10617. }
  10618. return 0;
  10619. }
  10620. BUILDIN_FUNC(callshop)
  10621. {
  10622. TBL_PC *sd = NULL;
  10623. struct npc_data *nd;
  10624. const char *shopname;
  10625. int flag = 0;
  10626. sd = script_rid2sd(st);
  10627. if (!sd) {
  10628. script_pushint(st,0);
  10629. return 0;
  10630. }
  10631. shopname = script_getstr(st, 2);
  10632. if( script_hasdata(st,3) )
  10633. flag = script_getnum(st,3);
  10634. nd = npc_name2id(shopname);
  10635. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  10636. {
  10637. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  10638. script_pushint(st,0);
  10639. return 1;
  10640. }
  10641. if( nd->subtype == SHOP )
  10642. {
  10643. switch( flag )
  10644. {
  10645. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  10646. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  10647. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  10648. }
  10649. }
  10650. else
  10651. clif_cashshop_show(sd, nd);
  10652. sd->npc_shopid = nd->bl.id;
  10653. script_pushint(st,1);
  10654. return 0;
  10655. }
  10656. BUILDIN_FUNC(npcshopitem)
  10657. {
  10658. const char* npcname = script_getstr(st, 2);
  10659. struct npc_data* nd = npc_name2id(npcname);
  10660. int n, i;
  10661. int amount;
  10662. if( !nd || nd->subtype != SHOP )
  10663. { //Not found.
  10664. script_pushint(st,0);
  10665. return 0;
  10666. }
  10667. // get the count of new entries
  10668. amount = (script_lastdata(st)-2)/2;
  10669. // generate new shop item list
  10670. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  10671. for( n = 0, i = 3; n < amount; n++, i+=2 )
  10672. {
  10673. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10674. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10675. }
  10676. nd->u.shop.count = n;
  10677. script_pushint(st,1);
  10678. return 0;
  10679. }
  10680. BUILDIN_FUNC(npcshopadditem)
  10681. {
  10682. const char* npcname = script_getstr(st,2);
  10683. struct npc_data* nd = npc_name2id(npcname);
  10684. int n, i;
  10685. int amount;
  10686. if( !nd || nd->subtype != SHOP )
  10687. { //Not found.
  10688. script_pushint(st,0);
  10689. return 0;
  10690. }
  10691. // get the count of new entries
  10692. amount = (script_lastdata(st)-2)/2;
  10693. // append new items to existing shop item list
  10694. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  10695. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  10696. {
  10697. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10698. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10699. }
  10700. nd->u.shop.count = n;
  10701. script_pushint(st,1);
  10702. return 0;
  10703. }
  10704. BUILDIN_FUNC(npcshopdelitem)
  10705. {
  10706. const char* npcname = script_getstr(st,2);
  10707. struct npc_data* nd = npc_name2id(npcname);
  10708. int n, i;
  10709. int amount;
  10710. int size;
  10711. if( !nd || nd->subtype != SHOP )
  10712. { //Not found.
  10713. script_pushint(st,0);
  10714. return 0;
  10715. }
  10716. amount = script_lastdata(st)-2;
  10717. size = nd->u.shop.count;
  10718. // remove specified items from the shop item list
  10719. for( i = 3; i < 3 + amount; i++ )
  10720. {
  10721. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == script_getnum(st,i) );
  10722. if( n < size )
  10723. {
  10724. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  10725. size--;
  10726. }
  10727. }
  10728. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  10729. nd->u.shop.count = size;
  10730. script_pushint(st,1);
  10731. return 0;
  10732. }
  10733. //Sets a script to attach to a shop npc.
  10734. BUILDIN_FUNC(npcshopattach)
  10735. {
  10736. const char* npcname = script_getstr(st,2);
  10737. struct npc_data* nd = npc_name2id(npcname);
  10738. int flag = 1;
  10739. if( script_hasdata(st,3) )
  10740. flag = script_getnum(st,3);
  10741. if( !nd || nd->subtype != SHOP )
  10742. { //Not found.
  10743. script_pushint(st,0);
  10744. return 0;
  10745. }
  10746. if (flag)
  10747. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10748. else
  10749. nd->master_nd = NULL;
  10750. script_pushint(st,1);
  10751. return 0;
  10752. }
  10753. /*==========================================
  10754. * Returns some values of an item [Lupus]
  10755. * Price, Weight, etc...
  10756. setitemscript(itemID,"{new item bonus script}",[n]);
  10757. Where n:
  10758. 0 - script
  10759. 1 - Equip script
  10760. 2 - Unequip script
  10761. *------------------------------------------*/
  10762. BUILDIN_FUNC(setitemscript)
  10763. {
  10764. int item_id,n=0;
  10765. const char *script;
  10766. struct item_data *i_data;
  10767. struct script_code **dstscript;
  10768. item_id = script_getnum(st,2);
  10769. script = script_getstr(st,3);
  10770. if( script_hasdata(st,4) )
  10771. n=script_getnum(st,4);
  10772. i_data = itemdb_exists(item_id);
  10773. if (!i_data || script==NULL || script[0]!='{') {
  10774. script_pushint(st,0);
  10775. return 0;
  10776. }
  10777. switch (n) {
  10778. case 2:
  10779. dstscript = &i_data->unequip_script;
  10780. break;
  10781. case 1:
  10782. dstscript = &i_data->equip_script;
  10783. break;
  10784. default:
  10785. dstscript = &i_data->script;
  10786. break;
  10787. }
  10788. if(*dstscript)
  10789. script_free_code(*dstscript);
  10790. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  10791. script_pushint(st,1);
  10792. return 0;
  10793. }
  10794. /* Work In Progress [Lupus]
  10795. BUILDIN_FUNC(addmonsterdrop)
  10796. {
  10797. int class_,item_id,chance;
  10798. class_=script_getnum(st,2);
  10799. item_id=script_getnum(st,3);
  10800. chance=script_getnum(st,4);
  10801. if(class_>1000 && item_id>500 && chance>0) {
  10802. script_pushint(st,1);
  10803. } else {
  10804. script_pushint(st,0);
  10805. }
  10806. }
  10807. BUILDIN_FUNC(delmonsterdrop)
  10808. {
  10809. int class_,item_id;
  10810. class_=script_getnum(st,2);
  10811. item_id=script_getnum(st,3);
  10812. if(class_>1000 && item_id>500) {
  10813. script_pushint(st,1);
  10814. } else {
  10815. script_pushint(st,0);
  10816. }
  10817. }
  10818. */
  10819. /*==========================================
  10820. * Returns some values of a monster [Lupus]
  10821. * Name, Level, race, size, etc...
  10822. getmonsterinfo(monsterID,queryIndex);
  10823. *------------------------------------------*/
  10824. BUILDIN_FUNC(getmonsterinfo)
  10825. {
  10826. struct mob_db *mob;
  10827. int mob_id;
  10828. mob_id = script_getnum(st,2);
  10829. if (!mobdb_checkid(mob_id)) {
  10830. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  10831. if ( !script_getnum(st,3) ) //requested a string
  10832. script_pushconststr(st,"null");
  10833. else
  10834. script_pushint(st,-1);
  10835. return -1;
  10836. }
  10837. mob = mob_db(mob_id);
  10838. switch ( script_getnum(st,3) ) {
  10839. case 0: script_pushstrcopy(st,mob->jname); break;
  10840. case 1: script_pushint(st,mob->lv); break;
  10841. case 2: script_pushint(st,mob->status.max_hp); break;
  10842. case 3: script_pushint(st,mob->base_exp); break;
  10843. case 4: script_pushint(st,mob->job_exp); break;
  10844. case 5: script_pushint(st,mob->status.rhw.atk); break;
  10845. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  10846. case 7: script_pushint(st,mob->status.def); break;
  10847. case 8: script_pushint(st,mob->status.mdef); break;
  10848. case 9: script_pushint(st,mob->status.str); break;
  10849. case 10: script_pushint(st,mob->status.agi); break;
  10850. case 11: script_pushint(st,mob->status.vit); break;
  10851. case 12: script_pushint(st,mob->status.int_); break;
  10852. case 13: script_pushint(st,mob->status.dex); break;
  10853. case 14: script_pushint(st,mob->status.luk); break;
  10854. case 15: script_pushint(st,mob->status.rhw.range); break;
  10855. case 16: script_pushint(st,mob->range2); break;
  10856. case 17: script_pushint(st,mob->range3); break;
  10857. case 18: script_pushint(st,mob->status.size); break;
  10858. case 19: script_pushint(st,mob->status.race); break;
  10859. case 20: script_pushint(st,mob->status.def_ele); break;
  10860. case 21: script_pushint(st,mob->status.mode); break;
  10861. default: script_pushint(st,-1); //wrong Index
  10862. }
  10863. return 0;
  10864. }
  10865. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  10866. {
  10867. TBL_PC *sd = NULL;
  10868. if(script_hasdata(st,2))
  10869. sd = map_nick2sd(script_getstr(st,2));
  10870. else
  10871. sd = script_rid2sd(st);
  10872. if(sd)
  10873. script_pushint(st,(sd->vender_id != 0));
  10874. else
  10875. script_pushint(st,0);
  10876. return 0;
  10877. }
  10878. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  10879. {
  10880. TBL_PC *sd = NULL;
  10881. if(script_hasdata(st,2))
  10882. sd = map_nick2sd(script_getstr(st,2));
  10883. else
  10884. sd = script_rid2sd(st);
  10885. if(sd)
  10886. script_pushint(st,(sd->chatID != 0));
  10887. else
  10888. script_pushint(st,0);
  10889. return 0;
  10890. }
  10891. int axtoi(const char *hexStg)
  10892. {
  10893. int n = 0; // position in string
  10894. int m = 0; // position in digit[] to shift
  10895. int count; // loop index
  10896. int intValue = 0; // integer value of hex string
  10897. int digit[11]; // hold values to convert
  10898. while (n < 10) {
  10899. if (hexStg[n]=='\0')
  10900. break;
  10901. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  10902. digit[n] = hexStg[n] & 0x0f; //convert to int
  10903. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  10904. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10905. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  10906. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10907. else break;
  10908. n++;
  10909. }
  10910. count = n;
  10911. m = n - 1;
  10912. n = 0;
  10913. while(n < count) {
  10914. // digit[n] is value of hex digit at position n
  10915. // (m << 2) is the number of positions to shift
  10916. // OR the bits into return value
  10917. intValue = intValue | (digit[n] << (m << 2));
  10918. m--; // adjust the position to set
  10919. n++; // next digit to process
  10920. }
  10921. return (intValue);
  10922. }
  10923. // [Lance] Hex string to integer converter
  10924. BUILDIN_FUNC(axtoi)
  10925. {
  10926. const char *hex = script_getstr(st,2);
  10927. script_pushint(st,axtoi(hex));
  10928. return 0;
  10929. }
  10930. // [zBuffer] List of player cont commands --->
  10931. BUILDIN_FUNC(rid2name)
  10932. {
  10933. struct block_list *bl = NULL;
  10934. int rid = script_getnum(st,2);
  10935. if((bl = map_id2bl(rid)))
  10936. {
  10937. switch(bl->type) {
  10938. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  10939. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  10940. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  10941. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  10942. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  10943. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  10944. default:
  10945. ShowError("buildin_rid2name: BL type unknown.\n");
  10946. script_pushconststr(st,"");
  10947. break;
  10948. }
  10949. } else {
  10950. ShowError("buildin_rid2name: invalid RID\n");
  10951. script_pushconststr(st,"(null)");
  10952. }
  10953. return 0;
  10954. }
  10955. BUILDIN_FUNC(pcblockmove)
  10956. {
  10957. int id, flag;
  10958. TBL_PC *sd = NULL;
  10959. id = script_getnum(st,2);
  10960. flag = script_getnum(st,3);
  10961. if(id)
  10962. sd = map_id2sd(id);
  10963. else
  10964. sd = script_rid2sd(st);
  10965. if(sd)
  10966. sd->state.blockedmove = flag > 0;
  10967. return 0;
  10968. }
  10969. BUILDIN_FUNC(pcfollow)
  10970. {
  10971. int id, targetid;
  10972. TBL_PC *sd = NULL;
  10973. id = script_getnum(st,2);
  10974. targetid = script_getnum(st,3);
  10975. if(id)
  10976. sd = map_id2sd(id);
  10977. else
  10978. sd = script_rid2sd(st);
  10979. if(sd)
  10980. pc_follow(sd, targetid);
  10981. return 0;
  10982. }
  10983. BUILDIN_FUNC(pcstopfollow)
  10984. {
  10985. int id;
  10986. TBL_PC *sd = NULL;
  10987. id = script_getnum(st,2);
  10988. if(id)
  10989. sd = map_id2sd(id);
  10990. else
  10991. sd = script_rid2sd(st);
  10992. if(sd)
  10993. pc_stop_following(sd);
  10994. return 0;
  10995. }
  10996. // <--- [zBuffer] List of player cont commands
  10997. // [zBuffer] List of mob control commands --->
  10998. //## TODO always return if the request/whatever was successfull [FlavioJS]
  10999. /// Makes the unit walk to target position or map
  11000. /// Returns if it was successfull
  11001. ///
  11002. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11003. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11004. BUILDIN_FUNC(unitwalk)
  11005. {
  11006. struct block_list* bl;
  11007. bl = map_id2bl(script_getnum(st,2));
  11008. if( bl == NULL )
  11009. {
  11010. script_pushint(st, 0);
  11011. }
  11012. else if( script_hasdata(st,4) )
  11013. {
  11014. int x = script_getnum(st,3);
  11015. int y = script_getnum(st,4);
  11016. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11017. }
  11018. else
  11019. {
  11020. int map_id = script_getnum(st,3);
  11021. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11022. }
  11023. return 0;
  11024. }
  11025. /// Kills the unit
  11026. ///
  11027. /// unitkill <unit_id>;
  11028. BUILDIN_FUNC(unitkill)
  11029. {
  11030. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11031. if( bl != NULL )
  11032. status_kill(bl);
  11033. return 0;
  11034. }
  11035. /// Warps the unit to the target position in the target map
  11036. /// Returns if it was successfull
  11037. ///
  11038. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11039. BUILDIN_FUNC(unitwarp)
  11040. {
  11041. int unit_id;
  11042. int map;
  11043. short x;
  11044. short y;
  11045. struct block_list* bl;
  11046. unit_id = script_getnum(st,2);
  11047. map = map_mapname2mapid(script_getstr(st, 3));
  11048. x = (short)script_getnum(st,4);
  11049. y = (short)script_getnum(st,5);
  11050. bl = map_id2bl(unit_id);
  11051. if( map >= 0 && bl != NULL )
  11052. script_pushint(st, unit_warp(bl,map,x,y,0));
  11053. else
  11054. script_pushint(st, 0);
  11055. return 0;
  11056. }
  11057. /// Makes the unit attack the target.
  11058. /// If the unit is a player and <action type> is not 0, it does a continuous
  11059. /// attack instead of a single attack.
  11060. /// Returns if the request was successfull.
  11061. ///
  11062. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11063. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11064. BUILDIN_FUNC(unitattack)
  11065. {
  11066. struct block_list* unit_bl;
  11067. struct block_list* target_bl = NULL;
  11068. struct script_data* data;
  11069. int actiontype = 0;
  11070. // get unit
  11071. unit_bl = map_id2bl(script_getnum(st,2));
  11072. if( unit_bl == NULL ) {
  11073. script_pushint(st, 0);
  11074. return 0;
  11075. }
  11076. data = script_getdata(st, 3);
  11077. get_val(st, data);
  11078. if( data_isstring(data) )
  11079. {
  11080. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11081. if( sd != NULL )
  11082. target_bl = &sd->bl;
  11083. } else
  11084. target_bl = map_id2bl(conv_num(st, data));
  11085. // request the attack
  11086. if( target_bl == NULL )
  11087. {
  11088. script_pushint(st, 0);
  11089. return 0;
  11090. }
  11091. // get actiontype
  11092. if( script_hasdata(st,4) )
  11093. actiontype = script_getnum(st,4);
  11094. switch( unit_bl->type )
  11095. {
  11096. case BL_PC:
  11097. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11098. script_pushint(st, 1);
  11099. return 0;
  11100. case BL_MOB:
  11101. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11102. break;
  11103. case BL_PET:
  11104. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11105. break;
  11106. default:
  11107. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11108. script_pushint(st, 0);
  11109. return 1;
  11110. }
  11111. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11112. return 0;
  11113. }
  11114. /// Makes the unit stop attacking and moving
  11115. ///
  11116. /// unitstop <unit_id>;
  11117. BUILDIN_FUNC(unitstop)
  11118. {
  11119. int unit_id;
  11120. struct block_list* bl;
  11121. unit_id = script_getnum(st,2);
  11122. bl = map_id2bl(unit_id);
  11123. if( bl != NULL )
  11124. {
  11125. unit_stop_attack(bl);
  11126. unit_stop_walking(bl,0);
  11127. if( bl->type == BL_MOB )
  11128. ((TBL_MOB*)bl)->target_id = 0;
  11129. }
  11130. return 0;
  11131. }
  11132. /// Makes the unit say the message
  11133. ///
  11134. /// unittalk <unit_id>,"<message>";
  11135. BUILDIN_FUNC(unittalk)
  11136. {
  11137. int unit_id;
  11138. const char* message;
  11139. struct block_list* bl;
  11140. unit_id = script_getnum(st,2);
  11141. message = script_getstr(st, 3);
  11142. bl = map_id2bl(unit_id);
  11143. if( bl != NULL )
  11144. {
  11145. struct StringBuf sbuf;
  11146. StringBuf_Init(&sbuf);
  11147. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11148. clif_message(bl, StringBuf_Value(&sbuf));
  11149. if( bl->type == BL_PC )
  11150. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11151. StringBuf_Destroy(&sbuf);
  11152. }
  11153. return 0;
  11154. }
  11155. /// Makes the unit do an emotion
  11156. ///
  11157. /// unitemote <unit_id>,<emotion>;
  11158. ///
  11159. /// @see e_* in const.txt
  11160. BUILDIN_FUNC(unitemote)
  11161. {
  11162. int unit_id;
  11163. int emotion;
  11164. struct block_list* bl;
  11165. unit_id = script_getnum(st,2);
  11166. emotion = script_getnum(st,3);
  11167. bl = map_id2bl(unit_id);
  11168. if( bl != NULL )
  11169. clif_emotion(bl, emotion);
  11170. return 0;
  11171. }
  11172. /// Makes the unit cast the skill on the target or self if no target is specified
  11173. ///
  11174. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11175. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  11176. BUILDIN_FUNC(unitskilluseid)
  11177. {
  11178. int unit_id;
  11179. int skill_id;
  11180. int skill_lv;
  11181. int target_id;
  11182. struct block_list* bl;
  11183. unit_id = script_getnum(st,2);
  11184. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11185. skill_lv = script_getnum(st,4);
  11186. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11187. bl = map_id2bl(unit_id);
  11188. if( bl != NULL )
  11189. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11190. return 0;
  11191. }
  11192. /// Makes the unit cast the skill on the target position.
  11193. ///
  11194. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11195. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  11196. BUILDIN_FUNC(unitskillusepos)
  11197. {
  11198. int unit_id;
  11199. int skill_id;
  11200. int skill_lv;
  11201. int skill_x;
  11202. int skill_y;
  11203. struct block_list* bl;
  11204. unit_id = script_getnum(st,2);
  11205. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11206. skill_lv = script_getnum(st,4);
  11207. skill_x = script_getnum(st,5);
  11208. skill_y = script_getnum(st,6);
  11209. bl = map_id2bl(unit_id);
  11210. if( bl != NULL )
  11211. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11212. return 0;
  11213. }
  11214. // <--- [zBuffer] List of mob control commands
  11215. /// Pauses the execution of the script, detaching the player
  11216. ///
  11217. /// sleep <mili seconds>;
  11218. BUILDIN_FUNC(sleep)
  11219. {
  11220. int ticks;
  11221. TBL_PC* sd;
  11222. ticks = script_getnum(st,2);
  11223. sd = map_id2sd(st->rid);
  11224. // detach the player
  11225. if( sd && sd->npc_id == st->oid )
  11226. {
  11227. sd->npc_id = 0;
  11228. }
  11229. st->rid = 0;
  11230. if( ticks <= 0 )
  11231. {// do nothing
  11232. }
  11233. else if( st->sleep.tick == 0 )
  11234. {// sleep for the target amount of time
  11235. st->state = RERUNLINE;
  11236. st->sleep.tick = ticks;
  11237. }
  11238. else
  11239. {// sleep time is over
  11240. st->state = RUN;
  11241. st->sleep.tick = 0;
  11242. }
  11243. return 0;
  11244. }
  11245. /// Pauses the execution of the script, keeping the player attached
  11246. /// Returns if a player is still attached
  11247. ///
  11248. /// sleep2(<mili secconds>) -> <bool>
  11249. BUILDIN_FUNC(sleep2)
  11250. {
  11251. int ticks;
  11252. ticks = script_getnum(st,2);
  11253. if( ticks <= 0 )
  11254. {// do nothing
  11255. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11256. }
  11257. else if( !st->sleep.tick )
  11258. {// sleep for the target amount of time
  11259. st->state = RERUNLINE;
  11260. st->sleep.tick = ticks;
  11261. }
  11262. else
  11263. {// sleep time is over
  11264. st->state = RUN;
  11265. st->sleep.tick = 0;
  11266. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11267. }
  11268. return 0;
  11269. }
  11270. /// Awakes all the sleep timers of the target npc
  11271. ///
  11272. /// awake "<npc name>";
  11273. BUILDIN_FUNC(awake)
  11274. {
  11275. struct npc_data* nd;
  11276. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11277. nd = npc_name2id(script_getstr(st, 2));
  11278. if( nd == NULL )
  11279. return 0;
  11280. while( node )
  11281. {
  11282. if( (int)node->key == nd->bl.id )
  11283. {// sleep timer for the npc
  11284. struct script_state* tst = (struct script_state*)node->data;
  11285. TBL_PC* sd = map_id2sd(tst->rid);
  11286. if( tst->sleep.timer == -1 )
  11287. {// already awake ???
  11288. node = node->next;
  11289. continue;
  11290. }
  11291. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11292. {// char not online anymore / another char of the same account is online - Cancel execution
  11293. tst->state = END;
  11294. tst->rid = 0;
  11295. }
  11296. delete_timer(tst->sleep.timer, run_script_timer);
  11297. node = script_erase_sleepdb(node);
  11298. tst->sleep.timer = INVALID_TIMER;
  11299. tst->sleep.tick = 0;
  11300. run_script_main(tst);
  11301. }
  11302. else
  11303. {
  11304. node = node->next;
  11305. }
  11306. }
  11307. return 0;
  11308. }
  11309. /// Returns a reference to a variable of the target NPC.
  11310. /// Returns 0 if an error occurs.
  11311. ///
  11312. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11313. BUILDIN_FUNC(getvariableofnpc)
  11314. {
  11315. struct script_data* data;
  11316. const char* name;
  11317. struct npc_data* nd;
  11318. data = script_getdata(st,2);
  11319. if( !data_isreference(data) )
  11320. {// Not a reference (aka varaible name)
  11321. ShowError("script:getvariableofnpc: not a variable\n");
  11322. script_reportdata(data);
  11323. script_pushnil(st);
  11324. st->state = END;
  11325. return 1;
  11326. }
  11327. name = reference_getname(data);
  11328. if( *name != '.' || name[1] == '@' )
  11329. {// not a npc variable
  11330. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11331. script_reportdata(data);
  11332. script_pushnil(st);
  11333. st->state = END;
  11334. return 1;
  11335. }
  11336. nd = npc_name2id(script_getstr(st,3));
  11337. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11338. {// NPC not found or has no script
  11339. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11340. script_pushnil(st);
  11341. st->state = END;
  11342. return 1;
  11343. }
  11344. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11345. return 0;
  11346. }
  11347. /// Opens a warp portal.
  11348. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11349. ///
  11350. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11351. ///
  11352. /// @author blackhole89
  11353. BUILDIN_FUNC(warpportal)
  11354. {
  11355. int spx;
  11356. int spy;
  11357. unsigned short mapindex;
  11358. int tpx;
  11359. int tpy;
  11360. struct skill_unit_group* group;
  11361. struct block_list* bl;
  11362. bl = map_id2bl(st->oid);
  11363. if( bl == NULL )
  11364. {
  11365. ShowError("script:warpportal: npc is needed\n");
  11366. return 1;
  11367. }
  11368. spx = script_getnum(st,2);
  11369. spy = script_getnum(st,3);
  11370. mapindex = mapindex_name2id(script_getstr(st, 4));
  11371. tpx = script_getnum(st,5);
  11372. tpy = script_getnum(st,6);
  11373. if( mapindex == 0 )
  11374. return 0;// map not found
  11375. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  11376. if( group == NULL )
  11377. return 0;// failed
  11378. group->val2 = (tpx<<16) | tpy;
  11379. group->val3 = mapindex;
  11380. return 0;
  11381. }
  11382. BUILDIN_FUNC(openmail)
  11383. {
  11384. TBL_PC* sd;
  11385. sd = script_rid2sd(st);
  11386. if( sd == NULL )
  11387. return 0;
  11388. #ifndef TXT_ONLY
  11389. mail_openmail(sd);
  11390. #endif
  11391. return 0;
  11392. }
  11393. BUILDIN_FUNC(openauction)
  11394. {
  11395. TBL_PC* sd;
  11396. sd = script_rid2sd(st);
  11397. if( sd == NULL )
  11398. return 0;
  11399. #ifndef TXT_ONLY
  11400. clif_Auction_openwindow(sd);
  11401. #endif
  11402. return 0;
  11403. }
  11404. /// Retrieves the value of the specified flag of the specified cell.
  11405. ///
  11406. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  11407. ///
  11408. /// @see cell_chk* constants in const.txt for the types
  11409. BUILDIN_FUNC(checkcell)
  11410. {
  11411. int m = map_mapname2mapid(script_getstr(st,2));
  11412. int x = script_getnum(st,3);
  11413. int y = script_getnum(st,4);
  11414. cell_chk type = (cell_chk)script_getnum(st,5);
  11415. script_pushint(st, map_getcell(m, x, y, type));
  11416. return 0;
  11417. }
  11418. /// Modifies flags of cells in the specified area.
  11419. ///
  11420. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11421. ///
  11422. /// @see cell_* constants in const.txt for the types
  11423. BUILDIN_FUNC(setcell)
  11424. {
  11425. int m = map_mapname2mapid(script_getstr(st,2));
  11426. int x1 = script_getnum(st,3);
  11427. int y1 = script_getnum(st,4);
  11428. int x2 = script_getnum(st,5);
  11429. int y2 = script_getnum(st,6);
  11430. cell_t type = (cell_t)script_getnum(st,7);
  11431. bool flag = (bool)script_getnum(st,8);
  11432. int x,y;
  11433. if( x1 > x2 ) swap(x1,x2);
  11434. if( y1 > y2 ) swap(y1,y2);
  11435. for( y = y1; y <= y2; ++y )
  11436. for( x = x1; x <= x2; ++x )
  11437. map_setcell(m, x, y, type, flag);
  11438. return 0;
  11439. }
  11440. /*==========================================
  11441. * Mercenary Commands
  11442. *------------------------------------------*/
  11443. BUILDIN_FUNC(mercenary_create)
  11444. {
  11445. struct map_session_data *sd;
  11446. int class_, contract_time;
  11447. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  11448. return 0;
  11449. class_ = script_getnum(st,2);
  11450. if( !merc_class(class_) )
  11451. return 0;
  11452. contract_time = script_getnum(st,3);
  11453. merc_create(sd, class_, contract_time);
  11454. return 0;
  11455. }
  11456. BUILDIN_FUNC(mercenary_heal)
  11457. {
  11458. struct map_session_data *sd = script_rid2sd(st);
  11459. int hp, sp;
  11460. if( sd == NULL || sd->md == NULL )
  11461. return 0;
  11462. hp = script_getnum(st,2);
  11463. sp = script_getnum(st,3);
  11464. status_heal(&sd->md->bl, hp, sp, 0);
  11465. return 0;
  11466. }
  11467. BUILDIN_FUNC(mercenary_sc_start)
  11468. {
  11469. struct map_session_data *sd = script_rid2sd(st);
  11470. enum sc_type type;
  11471. int tick, val1;
  11472. if( sd == NULL || sd->md == NULL )
  11473. return 0;
  11474. type = (sc_type)script_getnum(st,2);
  11475. tick = script_getnum(st,3);
  11476. val1 = script_getnum(st,4);
  11477. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  11478. return 0;
  11479. }
  11480. BUILDIN_FUNC(mercenary_get_calls)
  11481. {
  11482. struct map_session_data *sd = script_rid2sd(st);
  11483. int guild;
  11484. if( sd == NULL )
  11485. return 0;
  11486. guild = script_getnum(st,2);
  11487. switch( guild )
  11488. {
  11489. case ARCH_MERC_GUILD:
  11490. script_pushint(st,sd->status.arch_calls);
  11491. break;
  11492. case SPEAR_MERC_GUILD:
  11493. script_pushint(st,sd->status.spear_calls);
  11494. break;
  11495. case SWORD_MERC_GUILD:
  11496. script_pushint(st,sd->status.sword_calls);
  11497. break;
  11498. default:
  11499. script_pushint(st,0);
  11500. break;
  11501. }
  11502. return 0;
  11503. }
  11504. BUILDIN_FUNC(mercenary_set_calls)
  11505. {
  11506. struct map_session_data *sd = script_rid2sd(st);
  11507. int guild, value, *calls;
  11508. if( sd == NULL )
  11509. return 0;
  11510. guild = script_getnum(st,2);
  11511. value = script_getnum(st,3);
  11512. switch( guild )
  11513. {
  11514. case ARCH_MERC_GUILD:
  11515. calls = &sd->status.arch_calls;
  11516. break;
  11517. case SPEAR_MERC_GUILD:
  11518. calls = &sd->status.spear_calls;
  11519. break;
  11520. case SWORD_MERC_GUILD:
  11521. calls = &sd->status.sword_calls;
  11522. break;
  11523. default:
  11524. return 0; // Invalid Guild
  11525. }
  11526. *calls += value;
  11527. *calls = cap_value(*calls, 0, INT_MAX);
  11528. return 0;
  11529. }
  11530. BUILDIN_FUNC(mercenary_get_faith)
  11531. {
  11532. struct map_session_data *sd = script_rid2sd(st);
  11533. int guild;
  11534. if( sd == NULL )
  11535. return 0;
  11536. guild = script_getnum(st,2);
  11537. switch( guild )
  11538. {
  11539. case ARCH_MERC_GUILD:
  11540. script_pushint(st,sd->status.arch_faith);
  11541. break;
  11542. case SPEAR_MERC_GUILD:
  11543. script_pushint(st,sd->status.spear_faith);
  11544. break;
  11545. case SWORD_MERC_GUILD:
  11546. script_pushint(st,sd->status.sword_faith);
  11547. break;
  11548. default:
  11549. script_pushint(st,0);
  11550. break;
  11551. }
  11552. return 0;
  11553. }
  11554. BUILDIN_FUNC(mercenary_set_faith)
  11555. {
  11556. struct map_session_data *sd = script_rid2sd(st);
  11557. int guild, value, *calls;
  11558. if( sd == NULL )
  11559. return 0;
  11560. guild = script_getnum(st,2);
  11561. value = script_getnum(st,3);
  11562. switch( guild )
  11563. {
  11564. case ARCH_MERC_GUILD:
  11565. calls = &sd->status.arch_faith;
  11566. break;
  11567. case SPEAR_MERC_GUILD:
  11568. calls = &sd->status.spear_faith;
  11569. break;
  11570. case SWORD_MERC_GUILD:
  11571. calls = &sd->status.sword_faith;
  11572. break;
  11573. default:
  11574. return 0; // Invalid Guild
  11575. }
  11576. *calls += value;
  11577. *calls = cap_value(*calls, 0, INT_MAX);
  11578. if( mercenary_get_guild(sd->md) == guild )
  11579. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  11580. return 0;
  11581. }
  11582. /*------------------------------------------
  11583. * Book Reading
  11584. *------------------------------------------*/
  11585. BUILDIN_FUNC(readbook)
  11586. {
  11587. struct map_session_data *sd;
  11588. int book_id, page;
  11589. if( (sd = script_rid2sd(st)) == NULL )
  11590. return 0;
  11591. book_id = script_getnum(st,2);
  11592. page = script_getnum(st,3);
  11593. clif_readbook(sd->fd, book_id, page);
  11594. return 0;
  11595. }
  11596. /******************
  11597. Questlog script commands
  11598. *******************/
  11599. BUILDIN_FUNC(getquest)
  11600. {
  11601. TBL_PC * sd = script_rid2sd(st);
  11602. struct quest qd;
  11603. int i, count = 0;
  11604. char * temp;
  11605. memset(&qd, 0, sizeof(struct quest));
  11606. qd.quest_id = script_getnum(st, 2);
  11607. qd.time = script_getnum(st, 3);
  11608. qd.state = Q_ACTIVE;
  11609. for(i=0; i<(script_lastdata(st)-3) && (i/2) < MAX_QUEST_OBJECTIVES; i+=2)
  11610. {
  11611. temp = (char*)script_getstr(st, i+4);
  11612. memcpy(&qd.objectives[i/2].name, temp, NAME_LENGTH);
  11613. temp = NULL;
  11614. qd.objectives[i/2].count = script_getnum(st, i+5);
  11615. count++;
  11616. }
  11617. qd.num_objectives = count;
  11618. quest_add(sd, &qd);
  11619. return 0;
  11620. }
  11621. BUILDIN_FUNC(deletequest)
  11622. {
  11623. TBL_PC * sd = script_rid2sd(st);
  11624. int qid = script_getnum(st, 2);
  11625. quest_delete(sd, qid);
  11626. return 0;
  11627. }
  11628. BUILDIN_FUNC(setquestobjective)
  11629. {
  11630. TBL_PC * sd = script_rid2sd(st);
  11631. int qid = script_getnum(st, 2);
  11632. int num = script_getnum(st, 3);
  11633. const char * str = script_getstr(st, 4);
  11634. int count = script_getnum(st, 5);
  11635. quest_update_objective(sd, qid, num, str, count);
  11636. return 0;
  11637. }
  11638. BUILDIN_FUNC(hasquest)
  11639. {
  11640. TBL_PC * sd = script_rid2sd(st);
  11641. int qid = script_getnum(st, 2);
  11642. script_pushint(st, quest_has_quest(sd, qid)?1:0);
  11643. return 0;
  11644. }
  11645. BUILDIN_FUNC(setqueststatus)
  11646. {
  11647. TBL_PC * sd = script_rid2sd(st);
  11648. int qid = script_getnum(st, 2);
  11649. bool active = script_getnum(st, 3)?true:false;
  11650. quest_update_status(sd, qid, active);
  11651. return 0;
  11652. }
  11653. // declarations that were supposed to be exported from npc_chat.c
  11654. #ifdef PCRE_SUPPORT
  11655. BUILDIN_FUNC(defpattern);
  11656. BUILDIN_FUNC(activatepset);
  11657. BUILDIN_FUNC(deactivatepset);
  11658. BUILDIN_FUNC(deletepset);
  11659. #endif
  11660. struct script_function buildin_func[] = {
  11661. // NPC interaction
  11662. BUILDIN_DEF(mes,"s"),
  11663. BUILDIN_DEF(next,""),
  11664. BUILDIN_DEF(close,""),
  11665. BUILDIN_DEF(close2,""),
  11666. BUILDIN_DEF(menu,"sl*"),
  11667. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  11668. BUILDIN_DEF(prompt,"s*"),
  11669. //
  11670. BUILDIN_DEF(goto,"l"),
  11671. BUILDIN_DEF(callsub,"i*"),
  11672. BUILDIN_DEF(callfunc,"s*"),
  11673. BUILDIN_DEF(return,"?"),
  11674. BUILDIN_DEF(getarg,"i?"),
  11675. BUILDIN_DEF(jobchange,"i*"),
  11676. BUILDIN_DEF(jobname,"i"),
  11677. BUILDIN_DEF(input,"v??"),
  11678. BUILDIN_DEF(warp,"sii"),
  11679. BUILDIN_DEF(areawarp,"siiiisii"),
  11680. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  11681. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  11682. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  11683. BUILDIN_DEF(setlook,"ii"),
  11684. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  11685. BUILDIN_DEF(set,"ii"),
  11686. BUILDIN_DEF(setarray,"rv*"),
  11687. BUILDIN_DEF(cleararray,"rvi"),
  11688. BUILDIN_DEF(copyarray,"rri"),
  11689. BUILDIN_DEF(getarraysize,"r"),
  11690. BUILDIN_DEF(deletearray,"r?"),
  11691. BUILDIN_DEF(getelementofarray,"ri"),
  11692. BUILDIN_DEF(getitem,"vi?"),
  11693. BUILDIN_DEF(rentitem,"vi"),
  11694. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  11695. BUILDIN_DEF(getnameditem,"is"),
  11696. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  11697. BUILDIN_DEF(makeitem,"iisii"),
  11698. BUILDIN_DEF(delitem,"ii?"),
  11699. BUILDIN_DEF(delitem2,"iiiiiiiii?"),
  11700. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  11701. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  11702. BUILDIN_DEF(cutin,"si"),
  11703. BUILDIN_DEF(viewpoint,"iiiii"),
  11704. BUILDIN_DEF(heal,"ii"),
  11705. BUILDIN_DEF(itemheal,"ii"),
  11706. BUILDIN_DEF(percentheal,"ii"),
  11707. BUILDIN_DEF(rand,"i?"),
  11708. BUILDIN_DEF(countitem,"i"),
  11709. BUILDIN_DEF(countitem2,"iiiiiiii"),
  11710. BUILDIN_DEF(checkweight,"ii"),
  11711. BUILDIN_DEF(readparam,"i*"),
  11712. BUILDIN_DEF(getcharid,"i*"),
  11713. BUILDIN_DEF(getpartyname,"i"),
  11714. BUILDIN_DEF(getpartymember,"i*"),
  11715. BUILDIN_DEF(getpartyleader,"i?"),
  11716. BUILDIN_DEF(getguildname,"i"),
  11717. BUILDIN_DEF(getguildmaster,"i"),
  11718. BUILDIN_DEF(getguildmasterid,"i"),
  11719. BUILDIN_DEF(strcharinfo,"i"),
  11720. BUILDIN_DEF(strnpcinfo,"i"),
  11721. BUILDIN_DEF(getequipid,"i"),
  11722. BUILDIN_DEF(getequipname,"i"),
  11723. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  11724. BUILDIN_DEF(repair,"i"), // [Valaris]
  11725. BUILDIN_DEF(getequipisequiped,"i"),
  11726. BUILDIN_DEF(getequipisenableref,"i"),
  11727. BUILDIN_DEF(getequipisidentify,"i"),
  11728. BUILDIN_DEF(getequiprefinerycnt,"i"),
  11729. BUILDIN_DEF(getequipweaponlv,"i"),
  11730. BUILDIN_DEF(getequippercentrefinery,"i"),
  11731. BUILDIN_DEF(successrefitem,"i"),
  11732. BUILDIN_DEF(failedrefitem,"i"),
  11733. BUILDIN_DEF(statusup,"i"),
  11734. BUILDIN_DEF(statusup2,"ii"),
  11735. BUILDIN_DEF(bonus,"iv"),
  11736. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  11737. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  11738. BUILDIN_DEF2(bonus,"bonus4","iviii"),
  11739. BUILDIN_DEF2(bonus,"bonus5","iviiii"),
  11740. BUILDIN_DEF(bonusautoscript,"si?"),
  11741. BUILDIN_DEF(bonusautoscript2,"si?"),
  11742. BUILDIN_DEF(skill,"vi?"),
  11743. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  11744. BUILDIN_DEF(guildskill,"vi"),
  11745. BUILDIN_DEF(getskilllv,"v"),
  11746. BUILDIN_DEF(getgdskilllv,"iv"),
  11747. BUILDIN_DEF(basicskillcheck,""),
  11748. BUILDIN_DEF(getgmlevel,""),
  11749. BUILDIN_DEF(end,""),
  11750. BUILDIN_DEF(checkoption,"i"),
  11751. BUILDIN_DEF(setoption,"i?"),
  11752. BUILDIN_DEF(setcart,"?"),
  11753. BUILDIN_DEF(checkcart,""),
  11754. BUILDIN_DEF(setfalcon,"?"),
  11755. BUILDIN_DEF(checkfalcon,""),
  11756. BUILDIN_DEF(setriding,"?"),
  11757. BUILDIN_DEF(checkriding,""),
  11758. BUILDIN_DEF2(savepoint,"save","sii"),
  11759. BUILDIN_DEF(savepoint,"sii"),
  11760. BUILDIN_DEF(gettimetick,"i"),
  11761. BUILDIN_DEF(gettime,"i"),
  11762. BUILDIN_DEF(gettimestr,"si"),
  11763. BUILDIN_DEF(openstorage,""),
  11764. BUILDIN_DEF(guildopenstorage,"*"),
  11765. BUILDIN_DEF(itemskill,"vi"),
  11766. BUILDIN_DEF(produce,"i"),
  11767. BUILDIN_DEF(monster,"siisii*"),
  11768. BUILDIN_DEF(getmobdrops,"i"),
  11769. BUILDIN_DEF(areamonster,"siiiisii*"),
  11770. BUILDIN_DEF(killmonster,"ss?"),
  11771. BUILDIN_DEF(killmonsterall,"s?"),
  11772. BUILDIN_DEF(clone,"siisi*"),
  11773. BUILDIN_DEF(doevent,"s"),
  11774. BUILDIN_DEF(donpcevent,"s"),
  11775. BUILDIN_DEF(cmdothernpc,"ss"),
  11776. BUILDIN_DEF(addtimer,"is"),
  11777. BUILDIN_DEF(deltimer,"s"),
  11778. BUILDIN_DEF(addtimercount,"si"),
  11779. BUILDIN_DEF(initnpctimer,"??"),
  11780. BUILDIN_DEF(stopnpctimer,"??"),
  11781. BUILDIN_DEF(startnpctimer,"??"),
  11782. BUILDIN_DEF(setnpctimer,"i?"),
  11783. BUILDIN_DEF(getnpctimer,"i?"),
  11784. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  11785. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  11786. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  11787. BUILDIN_DEF(announce,"si*"),
  11788. BUILDIN_DEF(mapannounce,"ssi*"),
  11789. BUILDIN_DEF(areaannounce,"siiiisi*"),
  11790. BUILDIN_DEF(getusers,"i"),
  11791. BUILDIN_DEF(getmapguildusers,"si"),
  11792. BUILDIN_DEF(getmapusers,"s"),
  11793. BUILDIN_DEF(getareausers,"siiii"),
  11794. BUILDIN_DEF(getareadropitem,"siiiii"),
  11795. BUILDIN_DEF(enablenpc,"s"),
  11796. BUILDIN_DEF(disablenpc,"s"),
  11797. BUILDIN_DEF(hideoffnpc,"s"),
  11798. BUILDIN_DEF(hideonnpc,"s"),
  11799. BUILDIN_DEF(sc_start,"iii?"),
  11800. BUILDIN_DEF(sc_start2,"iiii?"),
  11801. BUILDIN_DEF(sc_start4,"iiiiii?"),
  11802. BUILDIN_DEF(sc_end,"i?"),
  11803. BUILDIN_DEF(getscrate,"ii*"),
  11804. BUILDIN_DEF(debugmes,"s"),
  11805. BUILDIN_DEF2(catchpet,"pet","i"),
  11806. BUILDIN_DEF2(birthpet,"bpet",""),
  11807. BUILDIN_DEF(resetlvl,"i"),
  11808. BUILDIN_DEF(resetstatus,""),
  11809. BUILDIN_DEF(resetskill,""),
  11810. BUILDIN_DEF(skillpointcount,""),
  11811. BUILDIN_DEF(changebase,"i"),
  11812. BUILDIN_DEF(changesex,""),
  11813. BUILDIN_DEF(waitingroom,"si??"),
  11814. BUILDIN_DEF(delwaitingroom,"?"),
  11815. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  11816. BUILDIN_DEF(enablewaitingroomevent,"?"),
  11817. BUILDIN_DEF(disablewaitingroomevent,"?"),
  11818. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  11819. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  11820. BUILDIN_DEF(getwaitingroomstate,"i?"),
  11821. BUILDIN_DEF(warpwaitingpc,"sii?"),
  11822. BUILDIN_DEF(attachrid,"i"),
  11823. BUILDIN_DEF(detachrid,""),
  11824. BUILDIN_DEF(isloggedin,"i?"),
  11825. BUILDIN_DEF(setmapflagnosave,"ssii"),
  11826. BUILDIN_DEF(getmapflag,"si"),
  11827. BUILDIN_DEF(setmapflag,"si*"),
  11828. BUILDIN_DEF(removemapflag,"si"),
  11829. BUILDIN_DEF(pvpon,"s"),
  11830. BUILDIN_DEF(pvpoff,"s"),
  11831. BUILDIN_DEF(gvgon,"s"),
  11832. BUILDIN_DEF(gvgoff,"s"),
  11833. BUILDIN_DEF(emotion,"i*"),
  11834. BUILDIN_DEF(maprespawnguildid,"sii"),
  11835. BUILDIN_DEF(agitstart,""), // <Agit>
  11836. BUILDIN_DEF(agitend,""),
  11837. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  11838. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  11839. BUILDIN_DEF(getcastlename,"s"),
  11840. BUILDIN_DEF(getcastledata,"si*"),
  11841. BUILDIN_DEF(setcastledata,"sii"),
  11842. BUILDIN_DEF(requestguildinfo,"i*"),
  11843. BUILDIN_DEF(getequipcardcnt,"i"),
  11844. BUILDIN_DEF(successremovecards,"i"),
  11845. BUILDIN_DEF(failedremovecards,"ii"),
  11846. BUILDIN_DEF(marriage,"s"),
  11847. BUILDIN_DEF2(wedding_effect,"wedding",""),
  11848. BUILDIN_DEF(divorce,""),
  11849. BUILDIN_DEF(ispartneron,""),
  11850. BUILDIN_DEF(getpartnerid,""),
  11851. BUILDIN_DEF(getchildid,""),
  11852. BUILDIN_DEF(getmotherid,""),
  11853. BUILDIN_DEF(getfatherid,""),
  11854. BUILDIN_DEF(warppartner,"sii"),
  11855. BUILDIN_DEF(getitemname,"i"),
  11856. BUILDIN_DEF(getitemslots,"i"),
  11857. BUILDIN_DEF(makepet,"i"),
  11858. BUILDIN_DEF(getexp,"ii"),
  11859. BUILDIN_DEF(getinventorylist,""),
  11860. BUILDIN_DEF(getskilllist,""),
  11861. BUILDIN_DEF(clearitem,""),
  11862. BUILDIN_DEF(classchange,"ii"),
  11863. BUILDIN_DEF(misceffect,"i"),
  11864. BUILDIN_DEF(soundeffect,"si"),
  11865. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  11866. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  11867. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  11868. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  11869. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  11870. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  11871. BUILDIN_DEF(petloot,"i"), // [Valaris]
  11872. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  11873. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  11874. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  11875. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  11876. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  11877. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  11878. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  11879. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  11880. BUILDIN_DEF(nude,""), // nude command [Valaris]
  11881. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  11882. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  11883. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  11884. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  11885. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  11886. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  11887. BUILDIN_DEF(mobcount,"ss"),
  11888. BUILDIN_DEF(getlook,"i"),
  11889. BUILDIN_DEF(getsavepoint,"i"),
  11890. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  11891. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  11892. BUILDIN_DEF(npcstop,""), // [Valaris]
  11893. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  11894. BUILDIN_DEF(checkoption1,"i"),
  11895. BUILDIN_DEF(checkoption2,"i"),
  11896. BUILDIN_DEF(guildgetexp,"i"),
  11897. BUILDIN_DEF(guildchangegm,"is"),
  11898. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  11899. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  11900. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  11901. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  11902. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  11903. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  11904. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  11905. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  11906. BUILDIN_DEF(night,""), // sets the server to night time
  11907. BUILDIN_DEF(day,""), // sets the server to day time
  11908. #ifdef PCRE_SUPPORT
  11909. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  11910. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  11911. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  11912. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  11913. #endif
  11914. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  11915. BUILDIN_DEF(getusersname,"*"),
  11916. BUILDIN_DEF(recovery,""),
  11917. BUILDIN_DEF(getpetinfo,"i"),
  11918. BUILDIN_DEF(gethominfo,"i"),
  11919. BUILDIN_DEF(checkequipedcard,"i"),
  11920. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  11921. BUILDIN_DEF(globalmes,"s*"),
  11922. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  11923. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  11924. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  11925. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  11926. BUILDIN_DEF(setnpcdisplay,"sv??"),
  11927. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  11928. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  11929. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  11930. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  11931. // [zBuffer] List of mathematics commands --->
  11932. BUILDIN_DEF(sqrt,"i"),
  11933. BUILDIN_DEF(pow,"ii"),
  11934. BUILDIN_DEF(distance,"iiii"),
  11935. // <--- [zBuffer] List of mathematics commands
  11936. // [zBuffer] List of dynamic var commands --->
  11937. BUILDIN_DEF(getd,"*"),
  11938. BUILDIN_DEF(setd,"*"),
  11939. // <--- [zBuffer] List of dynamic var commands
  11940. BUILDIN_DEF(petstat,"i"),
  11941. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  11942. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  11943. BUILDIN_DEF(npcshopadditem,"sii*"),
  11944. BUILDIN_DEF(npcshopdelitem,"si*"),
  11945. BUILDIN_DEF(npcshopattach,"s?"),
  11946. BUILDIN_DEF(equip,"i"),
  11947. BUILDIN_DEF(autoequip,"ii"),
  11948. BUILDIN_DEF(setbattleflag,"ss"),
  11949. BUILDIN_DEF(getbattleflag,"s"),
  11950. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  11951. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  11952. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  11953. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  11954. BUILDIN_DEF(axtoi,"s"),
  11955. BUILDIN_DEF(query_sql,"s*"),
  11956. BUILDIN_DEF(query_logsql,"s*"),
  11957. BUILDIN_DEF(escape_sql,"s"),
  11958. BUILDIN_DEF(atoi,"s"),
  11959. // [zBuffer] List of player cont commands --->
  11960. BUILDIN_DEF(rid2name,"i"),
  11961. BUILDIN_DEF(pcfollow,"ii"),
  11962. BUILDIN_DEF(pcstopfollow,"i"),
  11963. BUILDIN_DEF(pcblockmove,"ii"),
  11964. // <--- [zBuffer] List of player cont commands
  11965. // [zBuffer] List of mob control commands --->
  11966. BUILDIN_DEF(unitwalk,"ii?"),
  11967. BUILDIN_DEF(unitkill,"i"),
  11968. BUILDIN_DEF(unitwarp,"isii"),
  11969. BUILDIN_DEF(unitattack,"iv?"),
  11970. BUILDIN_DEF(unitstop,"i"),
  11971. BUILDIN_DEF(unittalk,"is"),
  11972. BUILDIN_DEF(unitemote,"ii"),
  11973. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  11974. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  11975. // <--- [zBuffer] List of mob control commands
  11976. BUILDIN_DEF(sleep,"i"),
  11977. BUILDIN_DEF(sleep2,"i"),
  11978. BUILDIN_DEF(awake,"s"),
  11979. BUILDIN_DEF(getvariableofnpc,"rs"),
  11980. BUILDIN_DEF(warpportal,"iisii"),
  11981. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  11982. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  11983. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  11984. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  11985. BUILDIN_DEF(checkvending,"*"),
  11986. BUILDIN_DEF(checkchatting,"*"),
  11987. BUILDIN_DEF(openmail,""),
  11988. BUILDIN_DEF(openauction,""),
  11989. BUILDIN_DEF(checkcell,"siii"),
  11990. BUILDIN_DEF(setcell,"siiiiii"),
  11991. BUILDIN_DEF(getquest, "ii*"),
  11992. BUILDIN_DEF(deletequest, "i"),
  11993. BUILDIN_DEF(setquestobjective, "iisi"),
  11994. BUILDIN_DEF(setqueststatus, "ii"),
  11995. BUILDIN_DEF(hasquest, "i"),
  11996. BUILDIN_DEF(setwall,"siiiiis"),
  11997. BUILDIN_DEF(delwall,"s"),
  11998. BUILDIN_DEF(mercenary_create,"ii"),
  11999. BUILDIN_DEF(mercenary_heal,"ii"),
  12000. BUILDIN_DEF(mercenary_sc_start,"iii"),
  12001. BUILDIN_DEF(mercenary_get_calls,"i"),
  12002. BUILDIN_DEF(mercenary_get_faith,"i"),
  12003. BUILDIN_DEF(mercenary_set_calls,"ii"),
  12004. BUILDIN_DEF(mercenary_set_faith,"ii"),
  12005. BUILDIN_DEF(readbook,"ii"),
  12006. // WoE SE
  12007. BUILDIN_DEF(agitstart2,""),
  12008. BUILDIN_DEF(agitend2,""),
  12009. BUILDIN_DEF(agitcheck2,""),
  12010. {NULL,NULL,NULL},
  12011. };